Product types
A product type
A product type has one constructor and several fields: a value holds all of them at once, which is the multiplication side of the algebra. A point has an x and a y, a person has a name and an age, and the constructor is the only way to build a value of the type.
data Point = Point Int Int
deriving (Show)
origin :: Point
origin = Point 0 0
The type name and the constructor name are written the same here, which is legal and common: Point on the left of the = is the type, and Point on the right is the function Int -> Int -> Point. Other names are allowed, and a constructor that differs from its type is a hint that the type may later have more than one constructor.
Building and taking apart
A constructor builds a value by applying it to its fields; a pattern takes the value apart by naming the fields in the same order. The two functions below are the whole interface of the type, and neither needs a getter or a setter.
module Main (main) where
data Point = Point Int Int
deriving (Show)
origin :: Point
origin = Point 0 0
move :: Point -> Int -> Int -> Point
move (Point x y) dx dy = Point (x + dx) (y + dy)
main :: IO ()
main = do
print origin
print (move origin 2 3)
print (move (move origin 2 3) (-1) 1)
ghc -Wall -O1 -o point point.hs
./point
Point 0 0
Point 2 3
Point 1 4
move takes the point apart in its pattern and builds a new one, which is the functional update: there is no mutation, and origin is unchanged after both calls. The third line shows that the result of one call is an ordinary value, so it can be the input of the next.
data against a tuple
A pair (Int, Int) holds the same two numbers, so why declare a type? Three reasons, and each one is a real bug that the tuple allows:
- A tuple has no name, so a function that takes (Int, Int) of width and height accepts a pair of coordinates by mistake. A Point and a Size are different types even with the same fields.
- A tuple has no single place to add a field or a function; a type has a declaration where the new field is added and the compiler finds the places that must handle it.
- A tuple has no Show of its own beyond the generic one, while a type can derive it and, later, add instances that make sense for the domain.
type Width = Int
type Height = Int
area :: (Width, Height) -> Int
area (w, h) = w * h
Type synonyms such as Width document the tuple but do not separate it: (Width, Height) is still (Int, Int), and a pair in the wrong order compiles. The newtype of a later lesson is the tool that makes the two distinct without the cost of a wrapper constructor.
A record with two fields
The second program keeps a person and updates one field. The type has a string and a number, and the functions show both ways to take it apart: a pattern and a conversion to a pair.
module Main (main) where
data Person = Person String Int
deriving (Show)
birthday :: Person -> Person
birthday (Person name age) = Person name (age + 1)
describe :: Person -> String
describe (Person name age) = name ++ " is " ++ show age
asPair :: Person -> (String, Int)
asPair (Person name age) = (name, age)
main :: IO ()
main = do
let ada = Person "Ada" 36
print ada
print (birthday ada)
putStrLn (describe (birthday ada))
print (asPair ada)
print (fst (asPair ada))
ghc -Wall -O1 -o person person.hs
./person
Person "Ada" 36
Person "Ada" 37
Ada is 37
("Ada",36)
"Ada"
birthday rebuilds the value with one field changed, which is the same shape as move and the pattern the next lesson replaces with record update syntax. The pair conversion is there to show that the two representations are interchangeable at the boundary: a function that already takes a pair can be used with a person by writing the conversion once, and the type keeps the meaning at home.
Exercises
- Add a field to Person for a city and update every function that builds the type.
- Write scale :: Point -> Int -> Point that multiplies both coordinates.
- Write distance :: Point -> Point -> Int for a grid where distance is the sum of the differences.
- Declare data Size = Size Int Int and a function that takes a Size; then try to pass a Point and read the error.
- Rewrite describe with a case on the person instead of a pattern in the equation.
- Explain why a newtype would be needed to make Width and Height distinct.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How many values does a product type with two fields hold at once?
Answer
Both fields, always, which is what product means
A product type is a tuple of fields: every value carries all of them.
-
What is
Pointindata Point = Point Int Int?Answer
The type name on the left and the constructor on the right
The same name is used for the type and its only constructor, which is legal and common.
-
What does this program print?
module Main (main) where data Point = Point Int Int deriving (Show) main :: IO () main = print (Point 2 3)Answer
Point 2 3
The derived Show prints the constructor name followed by its fields.
-
What does
move (Point 1 2) 3 4return withmove (Point x y) dx dy = Point (x + dx) (y + dy)?Answer
Point 4 6
The fields are added to the offsets: 1 + 3 and 2 + 4.
-
Why prefer a named type to a bare pair of Ints?
Answer
The type keeps two different meanings apart and gives the compiler a place to check
A tuple of two Ints accepts any two Ints, while the named type rejects a value of the wrong meaning.
-
Does
birthday (Person name age) = Person name (age + 1)change the original value?Answer
No: it returns a new value and the original is unchanged
Values are immutable; the function builds a new Person with one field different.
-
What does
asPair (Person "Ada" 36)return?Answer
("Ada",36)
The function rebuilds the fields in the order of the pair type.
-
What does a type synonym such as
type Width = Intdo?Answer
It gives a second name to the same type, without separating it
A synonym is documentation for the reader; the compiler still sees Int and accepts any Int.
-
What does this program print?
module Main (main) where data Person = Person String Int deriving (Show) main :: IO () main = print (Person "Ada" 36)Answer
Person "Ada" 36
The derived Show prints the constructor, the string in quotes and the number.
-
Which change requires a new field to be handled everywhere?
Answer
Adding a field to a product type, because every construction must supply it
A constructor has a fixed arity, so every place that builds the value must pass the new field.
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