[Haskell-cafe] Inheritance and Wrappers

MattMan guitarmatt007 at hotmail.com
Mon Jan 31 20:58:02 CET 2011


tldr: Can I make arbitrary instances of one class instantiate another without
using wrappers?

I'm new to Haskell, and am trying to implement some simple typeclasses for
doing algebra.  For example I have type class (simplified for the sake of
argument)

class AbGroup a where
    add :: a -> a -> a

I would like any type instantiating Num to also be an abelian group:

instance (Num a) => AbGroup a where
 add i j = i+j,

but this doesn't compile because a is a variable, not a type constructor(or
something).  The problem is fixed, in a sense, If I introduce a wrapper
class

data Wrapper a = Wrap a
instance (Num a) => AbGroup (Wrapper a) where
     add (Wrap i) (Wrap j) = Wrap(i+j)

However, this is clumsy.  Is there something else I can do?  Thanks!

-- 
View this message in context: http://haskell.1045720.n5.nabble.com/Inheritance-and-Wrappers-tp3365126p3365126.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.



More information about the Haskell-Cafe mailing list