In Haskell, fixity determines how operators are parsed concerning their precedence and associativity. This plays a critical role in how expressions involving multiple operators are interpreted, especially when parentheses are omitted. Understanding fixity allows you to define custom operators with clear and predictable behavior, improving the readability and correctness of your Haskell programs.
This article explores what fixity is, the types of fixity available in Haskell, and how to define fixity for your custom operators.
What Is Fixity?
Fixity refers to two properties of operators in Haskell:
- Precedence: A numeric value that specifies the priority of an operator relative to others.
- Associativity: Determines the direction in which operators of the same precedence are grouped:
- Left-associative: Grouped from left to right.
- Right-associative: Grouped from right to left.
- Non-associative: Operators cannot be chained without parentheses.
Together, these properties dictate how expressions with multiple operators are evaluated.
Default Fixity
In Haskell, operators without an explicit fixity declaration have a default precedence of 9 and are left-associative.
Example:
a - b - c
-- Interpreted as (a - b) - c due to left-associativity
Types of Fixity
Haskell provides three types of fixity:
1. Infixl (Left-Associative)
Left-associative operators group from left to right.
Example:
infixl 6 + -- Declaration: '+' is left-associative with precedence 6
3 + 4 + 5 -- Interpreted as (3 + 4) + 5
2. Infixr (Right-Associative)
Right-associative operators group from right to left. This is common for operators like :
, which constructs lists.
Example:
infixr 5 : -- Declaration: ':' is right-associative with precedence 5
1 : 2 : [] -- Interpreted as 1 : (2 : [])
3. Infix (Non-Associative)
Non-associative operators cannot be chained without explicit parentheses. This is useful when chaining is ambiguous or nonsensical.
Example:
infix 4 == -- Declaration: '==' is non-associative with precedence 4
3 == 4 == 5
-- Error: Non-associative operators cannot be chained
(3 == 4) == 5
-- This works with explicit parentheses
Precedence
Precedence is a numeric value ranging from 0 (lowest) to 9 (highest). Operators with higher precedence bind more tightly, meaning they are evaluated first in the absence of parentheses.
Example:
infixl 6 + -- '+' has precedence 6
infixl 7 * -- '*' has precedence 7
3 + 4 * 5 -- Interpreted as 3 + (4 * 5) due to '*' having higher precedence
Defining Fixity for Custom Operators
When defining custom operators, you can declare their fixity using the infixl
, infixr
, or infix
keywords followed by their precedence level.
Example:
-- Define a custom operator `***` for multiplying and adding
(***) :: Int -> Int -> Int
a *** b = a * b + b
-- Declare fixity
infixl 6 ***
-- Usage
3 *** 4 *** 5 = 85 -- Interpreted as (3 *** 4) *** 5
Changing Fixity of Existing Operators
While you can’t modify the fixity of predefined operators in Haskell, you can create aliases or wrappers with custom fixity.
Example:
-- Define an alias for subtraction with custom fixity
(.-) :: Int -> Int -> Int
a .- b = a - b
infixr 5 .-
10 .- 3 .- 2 = 9 -- Interpreted as 10 .- (3 .- 2)
Practical Examples of Fixity in Action
1. Combining Operators with Different Precedence
infixl 6 +
infixl 7 *
3 + 4 * 5 = 23 -- Interpreted as 3 + (4 * 5)
Here, *
binds more tightly than +
, so it is evaluated first.
2. Chaining Right-Associative Operators
infixr 5 `append`
append :: [a] -> [a] -> [a]
append xs ys = xs ++ ys
[1] `append` [2] `append` [3] = [1,2,3]
-- Interpreted as [1] `append` ([2] `append` [3])
Right-associative operators are often used for operations like list construction or function composition.
3. Preventing Ambiguity with Non-Associative Fixity
infix 4 ==
result = (3 == 3) == (4 == 4) -- Requires parentheses to resolve ambiguity
Tips for Using Fixity
- Keep Precedence Intuitive: Assign precedence levels that match natural mathematical or logical conventions. For example, multiplication should bind more tightly than addition.
- Avoid Ambiguity: Use non-associative fixity for operators where chaining is ambiguous or meaningless.
- Document Fixity: When defining custom operators, include comments or documentation explaining their fixity to maintain code clarity.
- Leverage Default Fixity: Use the default precedence and associativity when no special behavior is required.
Conclusion
Fixity in Haskell is a powerful feature that determines how operators interact in expressions, impacting their readability and correctness. By understanding and correctly defining precedence and associativity, you can create expressive, intuitive custom operators while avoiding ambiguity. Mastering fixity is an essential step toward writing clear and maintainable Haskell code.
Leave a Reply