[Haskell-cafe] mapTuple

Udo Stenzel u.stenzel at web.de
Thu Jan 11 13:03:04 EST 2007

Marco Túlio Gontijo e Silva wrote:
> is there a way to defined something as a map to use in tuples? I tried
> this:
> mapTuple f (a, b) = (f a, f b)
> But the type inferred to it is not as generic as I wanted:
> mapTuple :: (t -> t1) -> (t, t) -> (t1, t1)

What you seem to want to do is impossible.  Just want type would you
want to assign to mapTuple?  I bet you can't even express that in
natural language, no wonder it's impossible in Haskell.

Types that are possible, not completely useless, but still impossible to
infer, could be:

mapTuple :: (forall a . a -> a) -> (a,a) -> (a,a)
mapTuple :: (forall a . a -> f a) -> (a,a) -> (f a, f a)
	(for suitable f or f in a suitable class)

Most generalizations turn out to be useless, because there aren't any
functions of the needed type that could be passed to mapTuple.
> mapTuple show ("string", True)

So you want

mapTuple :: forall c . (c a, c b)
         => (forall a . c a => a -> r) -> (a,b) -> (r,r)

which infortunately is only pseudo-Haskell, since c would be a variable
that ranges over type classes, and that doesn't exist.  I guess you
might be able to fake sauch a variable by explicitly passing a
dictionary, but that's more complicated than passing the show function
twice.  So I'd recommend to just forget about it and live with mapTuple
taking two functions.  In that case you don't even need mapTuple, since
it already exists under the name of (***) in Control.Arrow, along with
other goodies.

You could readily restrict mapTuple to the Show class, giving

showTupleWith :: (Show a, Show b)
              => (forall a . Show a => a -> r) -> (a,b) -> (r,r)

but there aren't that many functions it would accept as argument, mostly
just show composed with something that operates on strings.

If your life was a horse, you'd have to shoot it.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070111/bcf6484f/attachment.bin

More information about the Haskell-Cafe mailing list