[Haskell-cafe] FunDeps conflict

Brian Hulley brianh at metamilk.com
Tue Jun 6 07:01:11 EDT 2006

Joel Björnson wrote:
> Hi.
> I have a question regarding type classes and FunDeps.
> Consider the following code :
>> class Class2 a b | a -> b
>> class IsFoo a
>> data Bar a = Bar a
>> instance IsFoo a => Class2 a a
>> instance IsFoo a => Class2 (Bar a) a
> The last two instantiations will yield a 'Functional dependencies
> conflict error'. From what I understand, this is because (Bar a)
> *MAY* instantiate the type class isFoo, which would obviously lead to
> a conflict between the two instantiations.

Possibly you meant to write:

      instance IsFoo a => Class2 a a
      instance Class2 (Bar a) a

in which case the conflict does arise because (Bar a) could be an instance 
of IsFoo, but afaik there is no way to tell the compiler that (Bar a) can 
never be an instance of IsFoo, since all classes are "open" ie new instances 
can be defined later.

Having said this it does seem to me rather strange that there is no way to 
specify that (Bar a) cannot ever be an instance of IsFoo, since fundeps 
themselves limit the allowable instances for a class so you'd think the 
fundep itself would be enough to tell the compiler that there can be no 
instances of IsFoo (Bar a) in the same way that:

   class Class3 a b | a -> b
   instance Class3 Bool Char
   instance Class3 Int Float

tells the compiler there can't be any future instance such as Class3 Int 
Char etc, but perhaps it is necessary to keep the possible instances for 
each type class strictly independent of any other typeclass to avoid too 
much complexity.

There is a proposal for Haskell' for closed classes but this is listed under 
the "probably no" section so it's unlikely this will be adopted.


Regards, Brian.

Logic empowers us and Love gives us purpose.
Yet still phantoms restless for eras long past,
congealed in the present in unthought forms,
strive mightily unseen to destroy us.


More information about the Haskell-Cafe mailing list