15

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?

2 Answers 2

17

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.

1
  • 1
    Strange decision, that.
    – dfeuer
    Sep 3, 2015 at 4:30
3

(:) is not a normal function, but is one of the two data constructors in the (not valid haskell 98) definition you posted (Edit I guess that's arguable since this definition is not real syntax anyway...)

data [] a = [] | a : [] a

But it does indeed have the type you posted (:) :: a -> [a] -> [a]

The definition of lists above is not valid haskell-98 because terms with non-alphanumeric characters denote infix functions, so [] can't be a constant data constructor.

The second, debatable reason has to do with (:). In haskell 98 we can have infix data constructors, but they must start with a colon. So (:) is a sort of special case of this rule.

This special syntax was added so you can get nice pattern matches like (a:as), and so that there's the correspondence between the empty [] list and the other special list syntax which looks like [1,2,3].

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.