[Haskell-cafe] Multi-parameter Type Class

Paul Keir pkeir at dcs.gla.ac.uk
Thu Dec 11 09:37:41 EST 2008


Hi all,

I've been trying to refactor my tree conversion code to make
better use of type classes; and I've discovered multi-parameter
type classes and functional dependencies. I have a class with a
function a2b, and I'd like "map" to be used when it's a list of
type a.

I've created a simple failing example:

data Foo = Foo Bar    deriving(Show)
data Bar = Bar String deriving(Show)

class ZOT a b | a -> b where
  zot :: a -> b

instance ZOT Foo Integer where
  zot x = 17

instance ZOT Bar String where
  zot x = "Eighteen"

instance ZOT [x] [y] where   -- This bit
  zot xs = map zot xs        -- fails

main = do print $ zot $ Foo $ Bar "Blah"
          print $ zot $ Bar "Blah"
          print $ zot $ [Bar "Blah", Bar "Blah"] -- No map here please

I know this would work if the third instance of zot
explicitly took [Bar] and [String]. Can I not instead generalise
for all the ADTs in my tree in the way I've outlined? Must I
instantiate for the type of each list pair?

Cheers,
Paul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20081211/32583b06/attachment.htm


More information about the Haskell-Cafe mailing list