Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `(:)` built-in?

Tags:

haskell

I notice that the these two definitions are no where to be found in the most official resource of Haskell documentation:

(:) :: a -> [a] -> [a]
data [] a = [] | a : []

I checked Hoogle and there are no entries for data [] or (:). Are these two not supposed to be "normal" function and data type?


Edit: In the Haskell 2010 Language Report, they say that

-- The (:) operator is built-in syntax, and cannot legally be given  
-- a fixity declaration; but its fixity is given by:  
--   infixr 5  :  

But why? Is there a particular reason for this function to be singled out?

like image 623
xzhu Avatar asked Sep 02 '15 20:09

xzhu


1 Answers

It's built-in syntax, not really a built-in function. The function (:) is just one of the two constructors of the built-in type []. However:

  • The name of the type, [], is not valid Haskell syntax for a type-name; so it must be built-in syntax.
  • Similarly, the type syntax [a] for an instantiation of [] is built-in syntax.
  • The name of the other constructor, [], is not valid Haskell syntax for a constructor name; so it must be built-in syntax.
  • The list literal syntax [ x, y, z ] is built-in syntax (obviously).

Since essentially everything else about the type [] is built-in syntax, the decision was made to make : built-in syntax as well, rather than having it be the one exception that actually was a valid name in the language.

like image 178
Jonathan Cast Avatar answered Sep 20 '22 20:09

Jonathan Cast