[Haskell-cafe] mapTuple

Cale Gibbard cgibbard at gmail.com
Fri Jan 12 21:59:23 EST 2007


On 12/01/07, Grady Lemoine <grady.lemoine at gmail.com> wrote:
> Is there anything in particular you're trying to accomplish?  It seems
> like this is the type of thing you'd accomplish with typeclasses if
> you had a less general problem than you've presented.  For example,
>
> > mapShowTuple :: (Show a, Show b) => (a, b) -> (String, String)
> > mapShowTuple (x, y) = (show x, show y)
>
> That said, it would be nice do be able to do something a little more
> general, but still with a specific typeclass, like
>
> > mapNumOpTuple :: (Num a, Num b) => (Num c => c -> c) -> (a, b) -> (a, b)
> > mapNumOpTuple f (x, y) = (f x, f y)
>
> (This unfortunately fails to typecheck; GHC chokes with "Illegal
> polymorphic or qualified type".  On the other hand, I'm still pretty
> new to Haskell myself, so maybe someone else knows how to write this
> correctly without doing complex type hackery.)

It's close:

{-# OPTIONS_GHC -fglasgow-exts #-}

mapNumOpPair :: (Num a, Num b) => (forall c. Num c => c -> c) -> (a, b) -> (a,b)
mapNumOpPair f (x,y) = (f x, f y)

> It would also be nice to be able to generalize over all typeclasses,
> e.g. (pseudo-code here)
>
> mapTypeclassOpTuple :: for all typeclasses C ((C a, C b) => (C c => c
> -> c) -> (a, b) -> (a, b))
>
> but I don't even know how that would fit into Haskell's syntax.  I
> suspect it's an idea that's been discussed, and I just don't know the
> term for it.

That's an interesting idea, typeclass variables. It would require a
bit of a kind system for typeclasses, but that's not so hard. I
somehow doubt it would get used all *that* much though. Can anyone
think of a clever way to apply this?

 - Cale


More information about the Haskell-Cafe mailing list