Operators, precedence and fixity

Precedence

An operator is a function with a symbolic name, and it carries a precedence from 0 to 9 and an associativity. Precedence decides which operator binds first when they are written without parentheses, exactly as in arithmetic: * is 7 and + is 6, so multiplication runs first.

module Main (main) where main :: IO () main = do print (2 + 3 * 4 :: Int) print ((2 + 3) * 4 :: Int) print (2 ^ (3 :: Int) ^ (2 :: Int) :: Int) print (10 - 3 - 2 :: Int) print ([1] <+> [2] <+> [3] :: [Int]) print (1 `max` 2 :: Int) infixr 5 <+> (<+>) :: [a] -> [a] -> [a] xs <+> ys = xs ++ ys

ghc -Wall -O1 -o fixity fixity.hs
./fixity

14
20
512
5
[1,2,3]
2

The first two lines are the arithmetic everyone expects. The third is the one where precedence meets associativity: ^ is infixr 8, so 2 ^ 3 ^ 2 is 2 ^ (3 ^ 2) and gives 512, not 64. The fourth line shows the other direction: - is infixl 6, so the subtraction groups from the left and gives 5.

Associativity

Associativity only matters between operators of the same precedence. Three rules, each with a letter:

1 : 2 : [] -- infixr 5, reads as 1 : (2 : []) [1] ++ [2] ++ [3] -- infixr 5, reads as [1] ++ ([2] ++ [3]) 10 - 3 - 2 -- infixl 6, reads as (10 - 3) - 2 1 == 2 == 3 -- infix 4, a parse error without parentheses

A precedence is a number from 0 to 9, and a chain of different operators is grouped by precedence first: 1 + 2 * 3 multiplies first because 7 is larger than 6. When two operators share a precedence and the associativity is infix, the compiler refuses to guess; when they share a precedence and one is left and the other right, it also refuses, which is why mixing operators is a place where explicit parentheses are worth the characters.

A custom operator

Any function whose name is made of symbols can be used infix, and a fixity declaration gives it a precedence and an associativity. Without one it defaults to infixl 9, the tightest binding, which is rarely what a new operator wants.

module Main (main) where infixl 6 <+> (<+>) :: Int -> Int -> Int x <+> y = x * 10 + y main :: IO () main = do print (1 <+> 2) print (1 <+> 2 <+> 3) print (1 + 2 <+> 3) print (show (1 <+> 2))

ghc -Wall -O1 -o custom custom.hs
./custom

12
123
33
"12"

The operator appends a digit in decimal: 1 <+> 2 is 12, and because it is infixl 6 the second line groups as (1 <+> 2) <+> 3, which is 123. The third line is the reason a fixity declaration is not decoration: + is also 6 and also left, so 1 + 2 <+> 3 is (1 + 2) <+> 3 = 33, not 1 + (2 <+> 3) = 24. A different precedence, say infixl 7, would produce 24, and that is a real design decision rather than a detail.

Backticks

A named function with two arguments can be written infix by putting it between backticks: 1 `max` 2 is max 1 2, and xs `elem` ys reads better than elem xs ys. Backticked functions default to infixl 9 and can be given a fixity declaration like any operator, which is how `div` and `mod` get precedence 7 in the Prelude.

1 `max` 2 -- 2 10 `div` 3 -- 3 [1, 2] `zip` "ab" -- [(1,'a'),(2,'b')] 2 `elem` [1, 2, 3] -- True

The rule of thumb: an operator for a new algebraic idea, a backticked name when an existing function reads better in the middle. An operator invented for a single call is a cost to the reader, because the name is not in the Prelude and the fixity is not known; the section on composition and $ shows the two operators that are worth knowing before any new one.

Exercises

  1. Print 2 ^ 3 ^ 2 and then (2 ^ 3) ^ 2, and explain the difference.
  2. Declare an operator <*> with infixl 7 that multiplies and adds one, then chain it three times.
  3. Use backticks to write div and mod infix, and check the precedence against +.
  4. Compile 1 == 2 == 3 and read the parse error; add the parentheses that make it legal.
  5. Change infixl 6 to infixr 6 in the custom operator and predict the second and third lines.
  6. Explain why : is right associative and ++ is right associative too, while - is not.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does 2 + 3 * 4 evaluate to?

    Answer

    14

    Multiplication has the higher precedence, so 3 * 4 is 12 and 2 + 12 is 14.

  2. What does 2 ^ 3 ^ 2 evaluate to?

    Answer

    512

    Exponentiation is infixr 8, so it groups from the right: 2 ^ (3 ^ 2) = 2 ^ 9.

  3. What does 10 - 3 - 2 evaluate to?

    Answer

    5

    Subtraction is infixl 6, so the expression groups as (10 - 3) - 2.

  4. What is the default fixity of a new operator with no declaration?

    Answer

    infixl 9

    Haskell gives an undeclared operator the tightest binding, left associative, which is infixl 9.

  5. What does this program print? module Main (main) where infixl 6 +. (+.) :: Int -> Int -> Int x +. y = x * 10 + y main :: IO () main = print (1 +. 2 +. 3)

    Answer

    123

    The operator is left associative, so (1 +. 2) +. 3 appends the digits in order.

  6. Why is 1 == 2 == 3 a parse error?

    Answer

    Equality is infix 4: it has no associativity, so it cannot chain

    An infix operator with no associativity needs parentheses when it appears twice at the same precedence.

  7. What does 1 max 2 mean?

    Answer

    max 1 2

    Backticks turn a named two-argument function into an infix operator with its arguments in order.

  8. What is the precedence of a backticked function with no declaration?

    Answer

    infixl 9

    Backticks do not change the default: an undeclared operator still binds at 9, left associative.

  9. What does this program print? module Main (main) where main :: IO () main = print (2 * 3 + 4 :: Int)

    Answer

    10

    Multiplication binds tighter than addition, so 2 * 3 is 6 and 6 + 4 is 10.

  10. What does (2 + 3) * 4 evaluate to?

    Answer

    20

    The parentheses override the precedence and the sum is computed first.

The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev