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

Robin Bate Boerop robin_bb at acm.org
Sat Apr 22 14:31:21 EDT 2006


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