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

Yitzchak Gale gale at sefer.org
Sun Feb 18 13:58:35 EST 2007

Hi Marc,

Marc Weber wrote:
>         class (Monad m) => GetMV m a where
>         ...
>         instance GetMV m c where (2)
> ...it would be save to assume that the programmer doesn't want a
> failure but success.
> Thus ghc could infer (3) automatically but doesn't

The Monad m on the class declaration does
not mean that the programmer _wants_ m to be
a monad. It means that the programmer _promises_
to make m a monad - even more, the programmer
wants the compiler to force anyone using this
class to first make m a monad, and otherwise
enforce type safety by failing to compile.

This is very good, because then any future use
of an instance of GetMV can be safely assumed
to have also a Monad instance, no matter what its
type. One place you can use that nice assumption
is in the method declarations within the original
class declaration itself. Another is in function
definitions, as you say.

But when writing an instance declaration, it is
time to pay up. You made a promise. Now you
have to show the compiler where to get that Monad
instance. There are two ways of doing that.
One way is if the type itself already has a separate
Monad instance. The other way is if the type is
not fully specified - it is a variable - then you can
"pass the buck" and say that whoever uses this
instance must first make sure that the value of
the type variable is a type that already has a
Monad instance.


More information about the Haskell-Cafe mailing list