[Haskell-cafe] Re: Type class help please

Adrian Hey ahey at iee.org
Wed May 16 06:46:51 EDT 2007


Adrian Hey wrote:
> oleg at pobox.com wrote:
>> Adrian Hey wrote:
>>> -- Instances of GT are instances of Eq --
>>> instance (GT map key, Eq a) => Eq (map a) where
>>>   map1 == map2 = assocsAscending map1 == assocsAscending map2
>>> ...
>>>      Overlapping instances for Eq [(key, a)]
>>>        arising from use of `==' at Test.hs:10:16-59
>>>      Matching instances:
>>>        instance (Eq a) => Eq [a] -- Defined in GHC.Base
>>>        instance (GT map key, Eq a) => Eq (map a) -- Defined at 
>>> Test.hs:9:0
>>>
>>> How can my new instance overlap with the old (ghc) instance unless
>>> [] is also an instance of GT for some key type (which it isn't).
>>> Could someone explain?
>>
>> You are right in your explanation of the GHC behavior. Your instance 
>> |Eq (map a)| indeed overlaps the `standard` instance |Eq [a]|. The
>> overlap may be easier to see if we write [a] as ([] a), which is what
>> it is, an application of the type constructor [] to the type variable
>> a. So, the type [a] (aka [] a) is a particular instance of the type
>> (map a), with `map' being []. This is the overlapping that GHC is
>> complaining about.
> 
> So if I understand correctly, it's complaining about a potential
> overlap, not an actual overlap (there is no actual overlap until []
> is made an instance of GT AFAICS).
> 
> Also, I suspect I'm still missing something important here, for
> example I don't understand why, if it overlaps for [], it doesn't
> overlap with other instances (like Maybe for example). Or am I
> just not getting the error for Maybe because ghc stops after
> the first error?

Ah, this brings me to something else I don't quite understand
about this error, but kind of explains the absence of similar
errors for Maybe.

It's the "arising from use of `==' at Test.hs:10:16-59" part.

I don't understand why, but if I use..

-- Instances of GT are instances of Eq --
instance (GT map key, Eq a) => Eq (map a) where
  map1 == map2 = True

..the error goes away. So it's not objecting to such instances
in general, but rather the specific use of == in the definition.

So now I'm really confused. Why, if it's possible that this
instance overlaps with another for [], does it make any
difference whether or not I've used == in the definition?

Surely, if there's any ambiguity about which instance of Eq to use
for [] (should [] be made an instance of GT), then the ambiguity
exists whether or not I've used == in the definition. Hmm..

Regards
--
Adrian Hey
























More information about the Haskell-Cafe mailing list