[Haskell-beginners] Type polymorphism with size

Brent Yorgey byorgey at seas.upenn.edu
Tue Nov 18 20:16:37 EST 2008


On Tue, Nov 18, 2008 at 02:18:46PM -0800, Michael Snoyman wrote:
> 
> Very interesting solution to the problem. I tried it out and it works
> perfectly... but it's just too much of a hack for my tastes (no offense; I

None taken. =)

> 
> class RepTuple a b | a -> b where
>     toList :: a -> [b]
>     tMap :: (b -> b) -> a -> a
> 
> instance RepTuple (a, a) a where
>     toList (a, b) = [a, b]
>     tMap f (a, b) = (f a, f b)
> 
> And so on and so forth for every kind of tuple. Of course, this runs into
> the issue of the single case, for which I used the OneTuple library
> (actually, I wrote my own right now, but I intend to just use the OneTuple
> library).
> 

This is reasonable too.  It's just a tradeoff of hackishness vs. code
length/tediousness. I.e. in the solution with type-level naturals, you
don't need a separate instance like this for every number you're going
to use.  And no one really likes writing things like 
tMap f (a,b,c,d,e,g,h,i,j) = ...  =)

-Brent


More information about the Beginners mailing list