Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does data (->) a b mean?

Tags:

haskell

ghci

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?

like image 745
andro Avatar asked Dec 21 '25 01:12

andro


1 Answers

(->) 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)
like image 174
Tikhon Jelvis Avatar answered Dec 23 '25 13:12

Tikhon Jelvis