Reputation: 6366
It's trivial to redefine the function
(,) :: a -> b -> (a,b)
(,) a b = (a,b)
The weird (to me) thing is that this function is defined for arbitrary length tuples. So, for example, there is actually a function:
(,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) :: a -> b -> c -> ... -> (a,b,c,...)
How is this done? Why isn't it done for all the standard functions on tuples, like zip?
Hoogle gives me no results, and I don't see how template Haskell could do this, so I assume it must be some sort of magic inside the compiler. But that seems very un-Haskelly to me.
Upvotes: 6
Views: 305
Reputation: 8153
It is specified in the language definition and hard-wired into the compiler. You do not define tuples in Haskell, The definition of Haskell includes tuples.
There is accepted (,,,) syntax for tuples, which are anonymous product types, which are quite fundamental. These also play perfectly well with type inference, since each component is present and can be inferred.
There is no accepted syntax for anonymous sum types, and they may not play well with type inference.
Haskell offers user-definable infix syntax but not user-definable brackets (outside of the broad quasi-quoting possibilities).
Upvotes: 1
Reputation: 62848
My understanding is that (,)
isn't a regular function, it's a constructor with special syntax, hard-wired into the langage. It's similar to syntax [1, 2, 3]
, which you can't define yourself because it's hard-wired.
Upvotes: 1
Reputation: 129854
How is this done?
Compiler support. Haskell language report mandates (,)
to be supported for at least up to 15 arguments (6.1.4), but GHC goes a bit further and generates them for a lot more (last time we've tested this, it could handle hundreds or even thousands). zip
and other tuple functions have to be defined for up to 7-tuples. I don't know if GHC generates those for larger amounts.
Upvotes: 5