[Haskell-cafe] :, infix operator, infix constructor, et cetera

Andrew Coppin andrewcoppin at btinternet.com
Sun Aug 26 04:34:23 EDT 2007

Daniel C. Bastos wrote:
> There is something called infix constructors and something else called
> infix operators. I'm guessing that an infix operator is really a
> function, and an infix constructor I don't know what it is. How would
> you guys describe them?

An "infix operator" is literally a normal function with a funny name, 
for example (++). If a function name consists only of symbols, not 
letters, it works infix instead of prefix. On the other hand, you can 
take a normal function name and make it infix using backticks:

  filter odd [1,2,3]

  odd `filter` [1,2,3]

An "infix constructor" is a constructor rather than a function. 
Normally, functions start with a lowercase letter, and constructors 
start with an uppercase letter. But when the names are symbols, 
everything becomes infix, and an infix constructor starts with a ":", 
and an infix function starts with any other symbol.

  data Tree x = Leaf x | Branch (Tree x) (Tree x)

  data Tree x = Leaf x | (Tree x) :## (Tree x)

> I learned how to define (++), and then I wanted to see how (:) would be
> defined.

It's a constructor. Like Leaf and Branch in the example above.

If the list type wasn't already defined, you could define it as

  data List x = Node x (List x) | End

and then write lists as

  Node 1 (Node 2 (Node 3 End))

However, that's a lot of typing, so the actual definition looks more like

  data [x] = x : [x] | []

But that won't compile, because it doesn't obey the syntax rules of 
Haskell. You could, however, write

  data List x = x : (List x) | End

and it would work.

  1 : (2 : (3 : End))

> What does ``built-in syntax'' mean? 

The way lists are written (most especially the "[1,2,3]" syntax) is 
hard-wired into the compiler and cannot be changed. (Similarly for 
strings, actually.)

> The program
>> data [a] = [] | a : [a]
>> infixr 5 :
> gives
> %runhugs.exe Colon.lhs 
> runhugs: Error occurred
> ERROR "Colon.lhs":3 - Syntax error in data declaration (unexpected `[')

It doesn't like you calling the type "[x]". If you call it "List x" like 
I showed above, it should work.

> What does that 5 do in ``infixr 5 :''?

5 is the precedence.

You know how "2 + 3 * 4" is treated as "2 + (3 * 4)"? That's because (*) 
has a higher precedence than (+).

More information about the Haskell-Cafe mailing list