functional dependencies - bug?

Christian Maeder maeder@Informatik.Uni-Bremen.DE
Fri, 28 Jun 2002 19:08:48 +0200


does anybody have experiences with "Type Classes with Functional
Dependencies" (LNCS 1782, ESOP 2000, March 200, by Mark P. Jones)?

I've tried the example in the above paper:

class Collects e ce | ce -> e where
    empty :: ce
    insert :: e -> ce -> ce
    member :: e -> ce -> Bool

This works correctly for:

instance Eq a => Collects a [a] where
instance Collects Char Int where

and complains correctly for: 

instance Collects Char Int where
instance Collects Bool Int where 

with "Functional dependencies conflict between instance declarations"

Now to my problem with a context in the class declaration:

class Eq ce => Collects e ce | ce -> e where
    empty :: ce

This works as above, but correctly complains for:

data Stupid = Stupid -- without equality
instance Collects Bool Stupid where

with "No instance for (Eq Stupid)".

However, if I supply an (admittingly stupid) default definition for
"empty" within the class declaration:

class Eq ce => Collects e ce | ce -> e where
    empty :: ce
    empty = error("empty")

I no longer get the above "No instance for (Eq Stupid)"-Message, and I
guess that is a bug (or a feature that I don't understand).

When I change the context to "Eq e" everything is fine again:

instance Collects Stupid Int where

complains as above.

I don't know when (even meaningful) default definitions cause that
context conditions are not checked. In fact I noticed this error only
after I've removed a default definition just to find out that my
instance declaration were wrong that seemed to be correct before (but
instances for contexts were missing).

Are there any other (known) pitfalls with functional dependencies?

Regards Christian