[Haskell-cafe] Multi-parameter Type Class
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
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?
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe