Marcin 'Qrczak' Kowalczyk wrote:

> Wed, 16 Feb 2000 15:45:07 +0300 (MSK), S.D.Mechveliani <[EMAIL PROTECTED]> pisze:
>
> > I fear, I am loosing the thread. The discussion was on the
> > overlapping instances. And this latter question is maybe, on giving
> > a polymorphic function to another function as the argument. I am
> > not an implementor and doubt about `dictionaries', still could we
> > put it more concretely?
>
> f:: Eq a => [a] -> [a]
> f (x1:x2:xs) = if Just x1 == Just x2 then xs else []

<dictionary expansion deleted>

>
>
> Now, what happens with overlapping instances, when the user defines:
>
> instance Eq (Maybe String) where
>     Nothing == Nothing = True
>     Just a  == Just b  = length a == length b
>     _       == _       = False
>
> and uses f ["foo","bar","baz"]? f has the implementation like above
> and has only one (polymorphic) implementation, so it produces the
> dictionary of Eq (Maybe String) basing on the general eqMaybe and
> equality on strings (synthesized from the equality on characters and
> lists), ignoring the more specific instance.

And this would be a bug.  In the scope of the `instance Eq (Maybe String)', the
signature is too general.  Built in to the signature is the assumption that `Eq (Maybe
a)' can be reduced to `Eq a'.  This is fine when there's no overlapping.  However, with
the overlap in scope, it is not correct to perform the reduction, because it is too
early to commit to the more general reduction.  Hugs98 recognizes this, and reports:


     ERROR "t.hs" (line 8): Cannot justify constraints in explicitly typed binding
     *** Expression    : f
     *** Type          : Eq a => [a] -> [a]
     *** Given context : Eq a
     *** Constraints   : Eq (Maybe a)

Ghc-4.06 accepts this program, making the incorrect reduction, but this has already 
been
fixed in the CVS version.

I believe that the rest of Marcin's arguments in his note are predicated on the
incorrect reduction happening.

To my mind, the biggest flaw with overlapping instances is the separate compilation
issue: to whit, if the `instance Eq (Maybe String)' was in a different module, not
imported by the module defining `f', then Marcin's definition of `f' (with context
`Maybe a') would be fine, and would not `see' the overlap.   But in a way, this is
arguably correct: since the designer of `f' didn't forsee the overlap, it shouldn't
affect the meaning of the function he defined.

--Jeff

Reply via email to