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

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


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