Declaration component | Meaning |
---|---|
infixr | the operator is right-associative |
infixl | the operator is left-associative |
infix | the operator is non-associative |
optional digit | binding precedence of the operator (range 0...9, default 9) |
op1, ... , opn | operators |
To parse expressions involving operators and functions, Haskell uses fixity declarations to figure out where parenthesis go. In order, it
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 + (negate 5) * 2 - 3 * 4 ^ 2 ^ 1
1 + ((negate 5) * 2) - (3 * (4 ^ 2 ^ 1))
(1 + ((negate 5) * 2)) - (3 * (4 ^ (2 ^ 1)))
More details in section 4.4.2 of the Haskell 98 report.