[Haskell-cafe] Why do I have to specify (Monad m) here again?

David House dmhouse at gmail.com
Sun Feb 18 11:20:44 EST 2007


On 18/02/07, Marc Weber <marco-oweber at gmx.de> wrote:
> Is there a difference at all wehter specifying (Monad m) in the class
> declaration or not? I have to add it to the instance declaration
> anyway..

If you have, say:

class Monad m => Foo m where ...

Then it's illegal to say:

instance Foo Data.Set.Set where ...

Because Data.Set.Set doesn't instantiate Monad. Including the
constraint in the class header forces every instance to satisfy that
constraint in order for it to be a valid instance. In effect, Monad is
a superclass of Foo. Similarly, you can't say:

instance Foo m where ...

Because, in general, m doesn't instatiate Monad. You have to use
(Monad m => m) instead:

instance Foo (Monad m => m) where ...

Which is more normally written:

instance Monad m => Foo m where ...

Incidentally, you're here saying that:

i) Every type T that instantiates Foo must also instantiate Monad (due
to the constraint on the class head).
ii) Every type T that instantiates Monad also instantiates Foo (due to
the instance Monad m => Foo m).

So Foo and Monad both have exactly the same member types.

-- 
-David House, dmhouse at gmail.com


More information about the Haskell-Cafe mailing list