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

David House dmhouse at gmail.com
Sun Feb 18 11:50:24 EST 2007

On 18/02/07, David Tolpin <david.tolpin at gmail.com> wrote:
> how is this different from inferring that if a type variable is an instance of class it is subject to constraints imposed on the class?

I think you, and probably Marc Weber as well, are confusing what a
constraint on a class head means. Suppose you have:

class Monad m => Foo m

That constraint means that every instance of class Foo must also be an
instance of class Monad. So, as I explained in my email to Marc, we
must use:

instance Monad m => Foo m

And not:

instance Foo m

Because, in general, m isn't an instance of Monad. In your example, we
already have two types with instances of Eql, which means, by the
constraint on the class head of Eql, these types are also instances of
Eq, which makes the program valid. If you like, think about it this
way: in order for your function body to be valid, the compiler has to
prove that the types of the arguments to eql are instances of Eq
(because you're using == on them). It knows they're instances of Eql
by the type signature you provided, and from that it can prove that
they're also instance of Eq by the constraint on class head of Eql.

I think these are two completely different things to compare and say
'how are they different', but if you wanted a pithy sentence to try
and explain the differences between them, perhaps it's that in Marc's
example we're declaring an instance of a type where the class required
that the instance type must also instantiate some other class (Monad).
With your example, we're declaring a function, not an instance, that
requires an instance of Eql and needs one of Eq, but can find the
latter because of the class constraint.

HTH more this time :)

-David House, dmhouse at gmail.com

More information about the Haskell-Cafe mailing list