typeclass

Type classes in Scala

Hi stackoverflow. Having a background in Haskell I am currently trying to get familiar with Scala. I encountered some problems trying to translate a small, extensible expression language from Haskell into Scala. The underlying issue of writing a data type that is extensible with both new data-variants and operations is commonly known a...

Scala: Do implicit conversions work on Any?

I would like to store some objects from different type hierarchy into List[Any] or similar container, but perform implicit conversions on them later on to do something like type class. Here is an example: abstract class Price[A] { def price(a: A): Int } trait Car case class Prius(year: Int) extends Car trait Food case class FriedChic...

Scala: Generic implicit converters?

I would like to define a generic implicit converter that works for all subtypes of type T. For example: abstract class Price[A] { def price(a: Any): Int } trait Car case class Prius(year: Int) extends Car trait Food case class FriedChicken() extends Food object Def { implicit def carToPrice[A <: Car](car: A): Price[A] = new Price[...

Type class pattern in Scala doesn't consider inheritance?

I am designing an API using type classes in some cases however I have encountered a problem with implicit resolution. As shown below, if there is an implicit object for type A but an object of type B extends A is passed to the method, then an implicit object cannot be found. Is there a way to make this work or do callers have to put im...

How do I use Haskell's type system to enforce correctness while still being able to pattern-match?

Let's say that I have an adt representing some kind of tree structure: data Tree = ANode (Maybe Tree) (Maybe Tree) AValType | BNode (Maybe Tree) (Maybe Tree) BValType | CNode (Maybe Tree) (Maybe Tree) CValType As far as I know there's no way of pattern matching against type constructors (or the matching functions i...

In Haskell, is there any way to express that a type should be an instance of a typeclass in more than one way?

(Sorry in advance if the question is stupid or obvious -- I don't have a lot of experience with Haskell). Is there a way to express that a type should be an instance of a typeclass in more than one way? This is best illustrated with an example (which is probably somewhat silly): In mathematics, we can say that a semiring is a set that i...

General conversion type class

I'd like to see if it is feasible to have a type class for converting one thing into another and back again from a mapping of [(a,b)]. This example should illustrate what I'd like to do: data XX = One | Two | Three deriving (Show, Eq) data YY = Eno | Owt | Eerht deriving (Show, Eq) instance Convert XX YY where mapping = [(One, Eno)...

How would I translate a Haskell type class into F#?

I'm trying to translate the Haskell core library's Arrows into F# (I think it's a good exercise to understanding Arrows and F# better, and I might be able to use them in a project I'm working on.) However, a direct translation isn't possible due to the difference in paradigms. Haskell uses type-classes to express this stuff, but I'm no...

Degenerate typeclass instance declaration using constant value

Hi. Hopefully some of you can help me clear up this confusion I have. I'm sorry for the bad title - if anyone has suggestions I'll change it. Let's set up some context first. I've reduced everything down to the essentials, so bear with me if the example code below is somewhat contrived. Let's say we have class Foo a where foo :: a ...