[Haskell-cafe] class question

Stefan O'Rear stefanor at cox.net
Sun Jan 21 12:42:23 EST 2007


On Sun, Jan 21, 2007 at 06:21:34PM +0100, Hans van Thiel wrote:
> class (Num a, Monoid a) => NumMon a where
>    e = 0
>    add x y = x + y

> What am I doing wrong? Many thanks in advance.

Nothing!

What you are trying to do, declare a class B containing all members
of a class A, is simply not supported in Haskell.

This is the closest you'll get with Haskell98:

> newtype Wrap a = Wrap (Wrap a)
> instance Num a => Monoid (Wrap a) where
>     e = Wrap 0
>     add (Wrap x) (Wrap y) = Wrap (x + y)

This is the closest you'll get with GHC extensions:

> {-# OPTIONS_GHC -fallow-undecidable-instances #-}
> instance Num a => Monoid a where
>     e = 0
>     add = (+)


Hope this helps.


More information about the Haskell-Cafe mailing list