[Haskell-cafe] duplicate instance declarations. Why?

Brent Yorgey byorgey at seas.upenn.edu
Sat Oct 25 17:06:56 EDT 2008

On Fri, Oct 24, 2008 at 10:20:39AM +0200, Alberto G. Corona  wrote:
> with:
> >{-# OPTIONS -fglasgow-exts  -fallow-undecidable-instances  #-}
> >class A a
> >class R a
> >class S a
> >instance  R a => A a
> >instance S a => A a
> ----------

This point commonly trips people up (it used to trip me up). In
addition to what others have said, I hope I can provide a bit more

If you have 

> instance  R a => A a

it *seems* like it should mean, "Any a which is an instance of R is
also an instance of A".  However, that's *not* what it means!  It
actually means "Any type which matches 'a' (that is, any type at all)
can be an instance of A; and if some type is used as an instance of A,
then it must also be an instance of R as well."

GHC picks which instance to use by looking *only* at stuff to the
right of => in instance declarations.  Only after an instance has been
chosen is the stuff to the left of the => considered.

Hopefully now it is clear why the code above is a duplicate instance
declaration; there's no way to distinguish the instances by looking
only at stuff to the right of => .


More information about the Haskell-Cafe mailing list