[Haskell-cafe] Define combination of type classes?

Fritz Ruehr fruehr at willamette.edu
Thu Mar 23 20:45:42 EST 2006

What is the easiest way to name a combination of type classes, i.e., to 
abbreviate the fact that a certain type is an instance of several 
classes simultaneously? I have a vague sense that this is do-able, but 
that I am messing up by trying to use an empty class body as below.

So in the code below, I try to use FooBar to abbreviate the conjunction 
of Foo and Bar. But while f (which uses a FooBar constraint) has a 
valid definition, it can't be used. On the other hand, g (which uses 
the long-winded constraint), is both a valid defined and useable.

(In a real example, imagine that FooBar names a conjunction of a half 
dozen things, so that the g-like form really is onerous, whereas the 
f-like form would be sweet and tidy :) .)

   --  Fritz


class Foo a where
   foo :: a -> Int

class Bar a where
   bar :: a -> a

class (Foo a, Bar a) => FooBar a

f :: FooBar a => a -> Int
f a = foo (bar a)

g :: (Foo a, Bar a) => a -> Int
g a = foo (bar a)

instance Foo Char where
   foo = ord

instance Bar Char where
   bar = id

More information about the Haskell-Cafe mailing list