For (->) we have:
Prelude> :i (->)
data (->) t1 t2 -- Defined in ‘GHC.Prim’
What does this syntax mean? I thought the data declaration keyword required a type constructor on the LHS and one or more value constructors on the RHS?
And how is (->) generally spoken or pronounced?
(->) is the function type and people generally pronounce it as "to" (ie a -> b would be pronounced "a to b").
(->) is a primitive construct built into GHC: the compiler handles it specially. However, some tools like :i that work with normal types see a fake definition of (->) as if it was an empty data type:
data (->) a b
This allows :i to give you information about a built-in operation as if it was a normal definition.
The (->) a b syntax is the prefix version of an infix name, just like you can define normal operators in prefix form:
a ~+ b = a * b + b
is the same as
(~+) a b = a * b + b
If you actually tried defining (->) this way yourself, you would get an error:
<interactive>:20:1-13: error:
Illegal binding of built-in syntax: (->)
However, you can define empty data types like this yourself if you give them a valid name:
data Foo a b
This makes Foo a type with two (phantom) arguments that has no values except ⊥, just like Void from Data.Void.
If you want to play around with the infix syntax, you can define a type operator with the TypeOperators extension enabled, with the same meaning as Foo:
data a +~ b
or
data (+~) a b
This might be useful if, for example, we want to emulate OCaml and write pair typess as a * b rather than (a, b):
type a * b = (a, b)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With