tags:

views:

340

answers:

3

Why I can't construct large tuples in Haskell? Why there's a tuple size limit?

Prelude> (1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)

<interactive>:1:0:
    No instance for (Show
                       (t,
                        t1,
                        t2,
                        ...
                        t23))
      arising from a use of `print' at <interactive>:1:0-48
    Possible fix:
      add an instance declaration for
      (Show
         (t,
          t1,
          t2,
          ...
          t23))
    In a stmt of a 'do' expression: print it
+7  A: 

You can construct larger tuples, but you'll need to define them yourself. Secondly, you don't have a Show instance, so you would also need to write an instance for that.

In general, it is a bad idea to use large tuples, when you should be using a smarter data type, such as a vector. We discourage the use by limiting the size provided by default.

Don Stewart
+9  A: 

Much lamented among Haskellers, tuples are not compositional. So any typeclass must be defined on every size of tuple. I think the report says that instances only need to be defined up to 10ples or something like that.

I never use more than a triple in practice. If you are using tuples to do some kind of type-level logic, build a compositional variant and use that instead. For example:

infixr 9 :*
data a :* b = a :* !b

Then the type of a 5ple of Ints would be:

Int :* Int :* Int :* Int :* Int :* ()

The unit () at the end is important for type level logic as well as for strictness correctness (you wouldn't want the last element to be strict and all the others to be lazy).

Note the bang in the declaration. It means that the right side of a tuple is strict, so that a type like the above 5ple can be flattened down into a single chunk of memory, so that later elements are not more expensive to access than earlier ones.

luqui
+8  A: 
KennyTM
I believe GHC limits tuples to 60-something elements, but the language standard doesn't. Either way, it's just silly to have a tuple that big :)
pumpkin
@pumpkin: Ah right. Updated.
KennyTM