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

Sebastian Sylvan sebastian.sylvan at gmail.com
Sun Feb 18 15:56:03 EST 2007

On 2/18/07, David Tolpin <david.tolpin at gmail.com> wrote:
> On Mon, 19 Feb 2007 00:30:47 +0400, Sebastian Sylvan <sebastian.sylvan at gmail.com> wrote:
> > Well, I guess the H98 report would be a good start. But there are
> > multiple tutorials on type classes that will cover this, most of which
> > are available from haskell.org
> Sebastian,
> I did read H98 and would like an exact reference.

I'm sorry, I don't have the time to look it up for you. There are tons
of resource on this issue. I'm trying to explain it to you, if you
don't believe me then you'll just have to make do without my help.

> >
> > The key point is that Haskell won't guess, and in particular it won't
> > contradict what you tell it. I think that's the major flaw in your
> > reasoning, you expect Haskell to take an explicit type that you, the
> > programmer, supplies, and change it into something else.
> Why is this rule applied differently to type declarations and to instances?

It isn't. What makes you think it is? If you supply a type it will
trust you with that type. It won't change it into something else.
That's always true.

> > In the original example you are explicitly telling Haskell that "m" is
> > *not* in the Monad (or any other) class.
> I am not telling that. I am telling that m is an instance of a class all instances of which are in the Monad class. How is this different from specifying class constraint in type declarations?

No, you're saying "I want to instantiate this type into a class which
*requires* that all instances are in the Monad class". The thing is
that there are multiple ways of meeting this requriement, and Haskell
has no way of guessing which way you intended to do it.

A type variable "m" without any class constraints means exactly that,
"here's a type variable, it has no class constraints". The *absence*
of class constraints is significant -- it doesn't mean "I don't care",
it means "I care, and there are no constraints". And a type variable
with no constraints in a context which requires constraints is a type

How is Haskell supposed to know if you want to inject a "Monad" class
constraint or not, in your original example? You could just as well
add, say, "MonadIO" or "MonadPlus" or any other classes which have
Monad in their class hierarchy, any one of which would satisfy the
requirement that "m" needs to be in the "Monad" class. Also, the
misstake might be due to a spelling error, or any number of other
errors. I don't understand why you would want Haskell to ignore the
explicit type you are giving (in the instance declaration), which has
no constraints, and do something else instead.

Sebastian Sylvan
UIN: 44640862

More information about the Haskell-Cafe mailing list