[Haskell-cafe] Polymorphic algebraic type constructors
Graham Klyne
gk at ninebynine.org
Mon Jun 21 13:03:21 EDT 2004
If I have a polymorphic algebraic type (T a) with several type
constructors, only one of which actually references the type parameter, is
there any way to express type conversion for the type-parameter-independent
constructors without actually mentioning all the constructors?
Here's a simple example based on Either:
[[
data A = A String deriving (Show, Eq)
data B = B String deriving (Show, Eq)
f :: (a->b) -> Either String a -> Either String b
f g (Right a) = (Right $ g a)
f g (Left s) = (Left s)
-- f g (s) = (s) -- doesn't work
a2b (A s) = (B s)
t1 = f a2b (Left "x")
t2 = f a2b (Right (A "y"))
]]
The second case for 'f' throws a type error when the constructor 'Left' is
omitted, because the type of 's' is fixed to be Either String A when the
required result (in this case, because of a2b) is Either String B.
#g
------------
Graham Klyne
For email:
http://www.ninebynine.org/#Contact
More information about the Haskell-Cafe
mailing list