[Haskell] How to avoid both duplicate instances and extraneousinstance declarations?

Gerrit van den Geest G.vandenGeest at students.uu.nl
Sat Apr 22 15:11:35 EDT 2006


You want all instances of B1 to be instances of A and all instances of B2 to 
be instances of A.

This means:



forall a. B1 a => A a

and

forall a. B2 a => A a



I think this is not possible with type-classes and the type class extensions 
in Haskell, because the compiler can't construct the proofs 
deterministically anymore. The compiler has two choices, use an instance of 
B1 to construct a proof for A, or use an instance of B2 to construct a proof 
for A.



However, if the compiler has to proof (A Int) and there is no instance (B1 
Int), but there is an instance (B2 Int). I can imagine that a compiler is 
smart enough to use (B2 Int)  ....




Gerrit


> Gerrit,
>
> Thanks for your help.  Yes, your suggestion below will allow me to  give 
> only an instance declaration for B1 (and get the A declaration  for free, 
> if you will).
>
> However, I meant to suggest that there were other T's which are not 
> instances of B1 but of B2.  I will be back in the same situation for 
> those T's as I was previously in with T.  I can't add
>
> instance B2 a => A a
>
> in addition to
>
> instance B1 a => A a
>
> because of the duplicate instance situation.  So, my problem remains.
>
> -- 
> Robin Bate Boerop
>
>
>
> On 22-Apr-06, at 2:01 PM, Gerrit van den Geest wrote:
>
>> Robin,
>>
>>> The following does NOT work, because of a duplicate instance 
>>> declaration for A:
>>>
>>> class A a
>>> class B1 b1
>>> class B2 b2
>>> instance B1 x => A x
>>> instance B2 x => A x  -- duplicate instance, won't compile
>>> data T = T
>>> instance B1 T
>>
>> Yes, this doesn't work and I think there is no GHC extension that 
>> supports this.
>>
>>> The following DOES work, but it requires that I explicitly give 
>>> instance declarations of A for all instances of B1 and B2:
>>>
>>> class A a
>>> class A a => B1 a
>>> class A a => B2 a
>>> data T = T
>>> instance A T  -- I don't want to have to specify this!
>>> instance B1 T
>>
>> If you replace the instance you don't want to specify with:
>>> instance B1 a => A a
>> and use the following flags to start ghc:
>>> -fglasgow-exts -fallow-undecidable-instances
>>
>> You can add other datatypes, and you only have to give an instance  for 
>> class B1.
>>
>> Good luck!
>>
>> Gerrit
>>
>>
>>
>
> 



More information about the Haskell mailing list