How is (,) defined internally?

It is trivial to override a function

(,) :: a -> b -> (a,b) (,) ab = (a,b) 

The strange thing (for me) 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 it's done? Why is this not done for all standard functions in tuples, for example zip?

Hoogle gives me no results , and I don’t see how the Haskell template could do this, so I assume that this should be some kind of magic in the compiler. But for me it is very non-Haskelli.

+7
source share
3 answers

How it's done?

Compiler support. Haskell reports contain (,) to support at least 15 arguments (6.1.4), but the GHC goes a little further and generates a lot more (the last time we tested this, it could process hundreds or even thousands). zip and other tuple functions must be defined for 7-tuples. I do not know if GHC generates them for large volumes.

+6
source

I understand that (,) not a regular function, it is a constructor with special syntax, tightly associated with the language. This is similar to the syntax [1, 2, 3] , which you cannot define yourself because it is tightly coupled.

+2
source

It is specified in the language definition and is tightly coupled to the compiler. You do not define tuples in Haskell; the Haskell definition includes tuples.

There is a syntax (,,) for tuples, which are anonymous types of products, which are very fundamental. They also go well with type inferences, as each component is present and can be inferred.

There is no recognized syntax for anonymous sum types, and they can be poorly reproduced with an output type.

Haskell offers a user-defined infix syntax, but no user-defined brackets (outside of the wide possibilities of quasi-cycling).

+2
source

All Articles