lifting functions to tuples?

Duncan Coutts duncan.coutts at worcester.oxford.ac.uk
Thu Nov 20 23:04:38 EST 2003


On Tue, 2003-11-18 at 15:46, Abraham Egnor wrote:
> The classic way to write a lift function for tuples is, of course:
> 
> liftTup f (a, b) = (f a, f b)
> 
> which has a type of (a -> b) -> (a, a) -> (b, b).  I've been wondering if
> it would be possible to write a function that doesn't require the types in
> the tuple to be the same, just that the types in the second tuple are the
> result of applying the type transformation implied in the function to be
> lifted to the types in the first tuple.  Now, in Haskell98, this isn't
> possible because of the monomorphism restriction; however, ghc
> conveniently has a way to disable that.  However, I'm still having
> problems figuring out if it's even doable within the current constraints
> of the glasgow-extended type system.

How about this:

liftTup :: (forall a. a -> f a) -> (x, y) -> (f x, f y)
liftTup f (x,y) = (f x, f y)

Or ghc can infer the type if you write it like this:

liftTup (f::forall a.a ->f a) (x::x,y::y) = (f x::f x, f y::f y)

or simply:

liftTup (f::forall a.a ->f a) (x,y) = (f x, f y)


It works too! (ghci -fglasgow-exts)

> liftTup Just (1, 'c')
(Just 1,Just 'c')

Duncan



More information about the Haskell mailing list