[Haskell-cafe] Multi-parameter Type Class

Lennart Augustsson lennart at augustsson.net
Thu Dec 11 10:19:23 EST 2008


Since your map function is calling zot to convert an x to a y you must
state that this is possible:

instance ZOT x y => ZOT [x] [y] where
  zot xs = map zot xs

  -- Lennart

2008/12/11 Paul Keir <pkeir at dcs.gla.ac.uk>:
> 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
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


More information about the Haskell-Cafe mailing list