[Haskell-cafe] class question

Hans van Thiel hthiel.char at zonnet.nl
Sun Jan 21 12:21:34 EST 2007


Hello,

I'm trying to understand classes and defined:

class Monoid a where
   e :: a
   add :: a -> a -> a
   add a e = a
   add e a = a

instance Monoid a Int where
    e = 0
    add x y = x + y

This works just fine, also for instance definitions of Char, Bool and
lists. But defining:

class (Num a, Monoid a) => NumMon a where
   e = 0
   add x y = x + y

reults in error messages.
-- Hugs: ERROR "Chap12.hs":57 - No member "e" in class "NumMon"
-- GHCi: Chap12.hs:58:3: `add' is not a (visible) method of class
NumMon'

I've also tried instance definitions of NumMon (leaving out 'where' and
what's next in the class declaration) and get similar messages.
The definition of NumMon by itself, just the first line, is accepted by
Hugs and GHCi both.

What am I doing wrong? Many thanks in advance.

Hans van Thiel



More information about the Haskell-Cafe mailing list