[Haskell] type inference & instance extensions

Jonathan Cast jonathanccast at fastmail.fm
Tue Jan 27 13:24:31 EST 2009

On Tue, 2009-01-27 at 10:05 -0800, Corey O'Connor wrote:
> On Tue, Jan 27, 2009 at 4:51 AM,  <oleg at okmij.org> wrote:
> > Doug McIlroy wrote:
> >> A fragment of an attempt to make pairs serve as complex numbers,
> >> using ghc/hugs extensions:
> >>
> >>         instance Num a => Num (a,a) where
> >>                 (x,y) * (u,v) = (x*u-y*v, x*v+y*u)
> > The recent versions of GHC have a nifty equality constraint, so the
> > code can be written simply
> I'm confused on why
> instance Num a => Num (a, a) where

The head of this instance is Num (a, a), while

> is not equivalent to
> instance (Num a, Num b, a ~ b) => Num (a, b) where

the head of this instance is Num (a, b)

So if GHC doesn't already have an equation a ~ b in scope, it'll skip
the first instance during instance search, but find the second one
(incidentally introducing the equation a ~ b as above).  So GHC is more
aggressive about using the second instance.

(Or, to put it another way: instance selection ignores the context on
the instance.  Or, to put it another way: the context on the instance is
part of the *output* of instance selection, not the input).


More information about the Haskell mailing list