Sum types and exhaustive matching
A sum type
A sum type is a type whose values are one of several alternatives, never more than one at a time. data Direction = North | South | East | West has exactly four values, and a variable of that type always holds one of them. The vertical bar in the declaration is the sum: the values are the union of the constructor cases, and nothing else.
data Direction = North | South | East | West
deriving (Show)
opposite :: Direction -> Direction
opposite North = South
opposite South = North
opposite East = West
opposite West = East
This is where the type earns its keep. There is no fifth direction, no null and no invalid value: every function over Direction lists the four cases, and the compiler knows the list is complete. Compare it with an Int code where 1 is north, 2 is south and a typo is 7.
Covering every constructor
Two functions over the type, both written with one equation per constructor, and both total because the four cases are all there:
module Main (main) where
data Direction = North | South | East | West
deriving (Show)
opposite :: Direction -> Direction
opposite North = South
opposite South = North
opposite East = West
opposite West = East
turnRight :: Direction -> Direction
turnRight North = East
turnRight East = South
turnRight South = West
turnRight West = North
main :: IO ()
main = do
print (map opposite [North, South, East, West])
print (map turnRight [North, East, South, West])
ghc -Wall -O1 -o directions directions.hs
./directions
[South,North,West,East]
[East,South,West,North]
The build is clean: no warning, because every constructor is matched in both functions. That is the promise a sum type makes possible, and it is worth building types with few constructors and clear names rather than passing integers around: the compiler can only check what the type says.
The missing case
Drop one equation and GHC says which one, with the constructor name in the message. Here opposite knows three of the four directions:
module Main (main) where
data Direction = North | South | East | West
deriving (Show)
opposite :: Direction -> Direction
opposite North = South
opposite South = North
opposite East = West
main :: IO ()
main = do
print (opposite North)
print (opposite East)
ghc -Wall -O1 -o partial partial.hs
./partial
partial.hs:7:1: warning: [GHC-62161] [-Wincomplete-patterns]
Pattern match(es) are non-exhaustive
In an equation for 'opposite':
Patterns of type 'Direction' not matched: West
|
7 | opposite North = South
| ^^^^^^^^^^^^^^^^^^^^^^...
South
West
The warning names West and the line where the case analysis starts. The program still builds and the two calls that avoid West still print; the third call that passes West would fail at run time with a pattern-match error. That is the trade: the type makes the gap visible in the build, and -Wall makes it an alarm rather than a surprise in production.
Checked at compile time
The check is exhaustive for the constructors that exist when the module is compiled. Add Up and Down to Direction and the next build of the module that defines opposite reports the two new cases.
data Direction = North | South | East | West | Up | Down
opposite :: Direction -> Direction
opposite North = South
opposite South = North
opposite East = West
opposite West = East
-- Up and Down are not handled: the build now warns twice
A module that pattern matches on a type defined in another module does not recompile when the type changes unless its interface changed, so the warning appears in the module with the gap when that module is built, which a full build does. Keeping the constructors in the same module as the functions that match them is the simplest way to keep the check tight.
The habit that follows: a function over a sum type has one equation per constructor, and a wildcard is a decision to accept every future constructor as well. A wildcard in a match over your own type is often right while the type is young and wrong once it has meaning; the compiler tells you which constructors you are ignoring only when you do not ignore them all with _.
Exercises
- Add Up and Down to Direction, complete both functions, and check that the build is clean.
- Write turnLeft as three applications of turnRight and check that it equals turnRight . turnRight . turnRight.
- Write a function isVertical :: Direction -> Bool with two equations and a wildcard, then without the wildcard.
- Remove the West equation from opposite and read the warning; then call opposite West and read the run-time error.
- Declare a type of three shapes with fields, and write area with one equation per constructor.
- Explain why a sum type is safer than an Int code for the same four values.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How many values does
data Direction = North | South | East | Westhave?Answer
Exactly four
A sum type has one value per constructor, and no others: four constructors, four values.
-
What does GHC do when a function misses one constructor of a sum type?
Answer
It warns with GHC-62161 and names the missing constructor
The check is a warning under -Wall; the build continues and the gap fails only if it is reached.
-
What does this program print?
module Main (main) where data Direction = North | South | East | West deriving (Show) opposite :: Direction -> Direction opposite North = South opposite South = North opposite East = West opposite West = East main :: IO () main = print (map opposite [North, South, East, West])Answer
[South,North,West,East]
Each direction is replaced by its opposite, in the order the list gives.
-
Why is a sum type safer than an Int code for the same values?
Answer
The compiler knows the constructors, so a missing case is a warning and a wrong value is a type error
The type restricts the values to the declared constructors and lets the compiler check the matches.
-
What happens at run time when a missing constructor reaches the match?
Answer
The program fails with a pattern-match error
There is no branch to take, so evaluation fails; the compiler warned about it at build time.
-
What does
map opposite [North, South]produce?Answer
[South,North]
map applies the function to each element and keeps the order of the list.
-
When does GHC re-check a function that matches on a type from another module?
Answer
When that module is rebuilt and the type changed
The check belongs to the build of the module that defines the function, and a full rebuild surfaces the gap.
-
What does a wildcard in a match over your own sum type mean?
Answer
Every constructor not named is accepted, including future ones
The wildcard accepts everything left, so adding a constructor does not produce a warning for that function.
-
What does this program print?
module Main (main) where data Direction = North | South | East | West deriving (Show) turnRight :: Direction -> Direction turnRight North = East turnRight East = South turnRight South = West turnRight West = North main :: IO () main = print (turnRight (turnRight North))Answer
South
Two right turns from North pass through East and end at South.
-
Which function is not total for
data Direction = North | South | East | West?Answer
One with equations for three constructors and no wildcard
Three constructors leave the fourth unhandled with no fallback, so the function fails for that value.
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