Haskell LanguageFixity declarations


Syntax

  1. infix [integer] ops
  2. infixl [integer] ops
  3. infixr [integer] ops

Parameters

Declaration componentMeaning
infixrthe operator is right-associative
infixlthe operator is left-associative
infixthe operator is non-associative
optional digitbinding precedence of the operator (range 0...9, default 9)
op1, ... , opnoperators

Remarks

To parse expressions involving operators and functions, Haskell uses fixity declarations to figure out where parenthesis go. In order, it

  1. wraps function applications in parens
  2. uses binding precendence to wrap groups of terms all seperated by operators of the same precedence
  3. uses the associativity of those operators to figure out how to add parens to these groups

Notice that we assume here that the operators in any given group from step 2 must all have the same associativity. In fact, Haskell will reject any program where this condition is not met.

As an example of the above algorithm, we can step though the process of adding parenthesis to 1 + negate 5 * 2 - 3 * 4 ^ 2 ^ 1.

infixl 6 +
infixl 6 -
infixl 7 *
infixr 8 ^
  1. 1 + (negate 5) * 2 - 3 * 4 ^ 2 ^ 1
  2. 1 + ((negate 5) * 2) - (3 * (4 ^ 2 ^ 1))
  3. (1 + ((negate 5) * 2)) - (3 * (4 ^ (2 ^ 1)))

More details in section 4.4.2 of the Haskell 98 report.