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:
- infixl: group from the left, as subtraction and ++ do.
- infixr: group from the right, as : and ^ do.
- infix: no associativity, so the operator cannot be chained without parentheses. Comparison operators such as == are of this kind, which is why 1 == 2 == 3 is an error and not a Bool.
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
- Print 2 ^ 3 ^ 2 and then (2 ^ 3) ^ 2, and explain the difference.
- Declare an operator <*> with infixl 7 that multiplies and adds one, then chain it three times.
- Use backticks to write div and mod infix, and check the precedence against +.
- Compile 1 == 2 == 3 and read the parse error; add the parentheses that make it legal.
- Change infixl 6 to infixr 6 in the custom operator and predict the second and third lines.
- 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.
-
What does
2 + 3 * 4evaluate to?Answer
14
Multiplication has the higher precedence, so 3 * 4 is 12 and 2 + 12 is 14.
-
What does
2 ^ 3 ^ 2evaluate to?Answer
512
Exponentiation is infixr 8, so it groups from the right: 2 ^ (3 ^ 2) = 2 ^ 9.
-
What does
10 - 3 - 2evaluate to?Answer
5
Subtraction is infixl 6, so the expression groups as (10 - 3) - 2.
-
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.
-
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.
-
Why is
1 == 2 == 3a 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.
-
What does
1max2mean?Answer
max 1 2Backticks turn a named two-argument function into an infix operator with its arguments in order.
-
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.
-
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.
-
What does
(2 + 3) * 4evaluate 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