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

Robin Bate Boerop robin_bb at acm.org
Sat Apr 22 12:23:02 EDT 2006


I am having trouble making a type system for my EDSL in Haskell.  Can  
someone help?  A simplified description of my problem follows.

I have three classes, A, B1, and B2.  I want all instances of B1 to  
be instances of A.  I want all instances of B2 to be instances of A.   
None of the classes have methods.

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

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

Of course, this is a simplified example.  In a complicated type class  
hierarchy, in a program with many instances of, say B1, having to  
provide instance declarations for A is laborious.

Is there a way to avoid this?  How can I tell Haskell "all instances  
of B1 are automatically instances of A".

-- 
Robin Bate Boerop





More information about the Haskell mailing list