data and algebraic data types
data declares a type
data introduces a type and the constructors that build its values. The simplest form is a set of names with no arguments: a type whose values are exactly the names listed, like an enumeration in another language but checked by the compiler.
data Color = Red | Green | Blue
deriving (Show)
name :: Color -> String
name Red = "red"
name Green = "green"
name Blue = "blue"
Color is the type, Red, Green and Blue are the values, and the vertical bar separates alternatives. A value of another type cannot be a Color: there is no conversion and no null, and a function that takes a Color cannot be called with a string. That is the difference between a type and a tag.
Constructors are functions
A constructor with arguments is an ordinary function that builds a value of the type. Circle takes a Double and returns a Shape, so it can be mapped over a list, partially applied and stored like any other function.
data Shape
= Circle Double
| Rect Double Double
deriving (Show)
area :: Shape -> Double
area (Circle r) = 3.141592653589793 * r * r
area (Rect w h) = w * h
The type has two constructors with different numbers of arguments, and area has one equation per constructor. The compiler checks that both are covered; a missing Rect equation is the -Wincomplete-patterns warning of the next lesson. This is the sum of products that gives algebraic data types their name: a choice between constructors, each holding a tuple of fields.
deriving Show
deriving (Show) asks the compiler to write the show instance, which is what print needs to display a value. Without it the type is perfectly usable and impossible to print: the compiler refuses print Red with a "No instance for Show Color" error.
module Main (main) where
data Color = Red | Green | Blue
deriving (Show)
name :: Color -> String
name Red = "red"
name Green = "green"
name Blue = "blue"
main :: IO ()
main = do
print Red
putStrLn (name Blue)
print [Red, Green, Blue]
ghc -Wall -O1 -o colors colors.hs
./colors
Red
blue
[Red,Green,Blue]
The derived show prints the constructor name exactly as it is written in the declaration, and a list of values as the bracketed list of their shows. It is a debugging format, not a user-facing one: the name function is what a reader should see, which is why the program prints blue for the user and Red for the developer.
Matching on your own type
A constructor pattern works in a function equation, a case, a let and a lambda, with the same rules as the list patterns of the earlier lessons. The program below computes the area of shapes and shows that a constructor is a function by mapping it over a list.
module Main (main) where
data Shape
= Circle Double
| Rect Double Double
deriving (Show)
area :: Shape -> Double
area (Circle r) = 3.141592653589793 * r * r
area (Rect w h) = w * h
main :: IO ()
main = do
print (Circle 2.0)
print (Rect 3.0 4.0)
print (area (Circle 2.0))
print (area (Rect 3.0 4.0))
print (map area [Circle 1.0, Rect 2.0 5.0])
ghc -Wall -O1 -o shapes shapes.hs
./shapes
Circle 2.0
Rect 3.0 4.0
12.566370614359172
12.0
[3.141592653589793,10.0]
Two things to notice. First, the derived show of a constructor with fields prints the fields after the name, which is how Circle 2.0 appears. Second, the area of a circle of radius 2 is 12.566370614359172, not 12.57: the number is the closest Double, and rounding is a separate decision that belongs to the display code.
Exercises
- Declare a type Traffic = Stop | Caution | Go with deriving (Show) and print its three values.
- Add a constructor Triangle Double Double to Shape and the matching equation to area.
- Write perimeter :: Shape -> Double for the two constructors and check a unit circle.
- Remove the deriving (Show) line and read the error print produces.
- Use a constructor as a function: map Circle over [1.0, 2.0] and print the result.
- Write a function that turns a Color into its (Int, Int, Int) components.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
data Color = Red | Green | Bluedeclare?Answer
A type with three constructors and no fields
data introduces the type Color and the three values that build it.
-
What is
Circleindata Shape = Circle Double | Rect Double Double?Answer
A constructor, which is a function from Double to Shape
A constructor with arguments is a function that builds a value of the type.
-
What does
deriving (Show)give a type?Answer
A show instance, so print can display its values
The compiler writes the Show instance, which print needs; without it print is a type error.
-
How does the derived Show print a value of a type with no fields?
Answer
As the constructor name exactly as written
The printed form is the constructor name, such as Red or Green.
-
What does this program print?
module Main (main) where data Color = Red | Green | Blue deriving (Show) main :: IO () main = print [Red, Green, Blue]Answer
[Red,Green,Blue]
A list of values prints as the bracketed list of their derived shows.
-
What is the type of the constructor
Rect?Answer
Double -> Double -> ShapeRect takes two Doubles one at a time, as every two-argument function does.
-
How many equations does a function over a two-constructor type need to be total?
Answer
Two, one per constructor
Each constructor needs a case; a missing one is an incomplete pattern warning.
-
What does this program print?
module Main (main) where data Shape = Circle Double | Rect Double Double deriving (Show) area :: Shape -> Double area (Circle r) = 3.0 * r * r area (Rect w h) = w * h main :: IO () main = print (area (Rect 2.0 5.0))Answer
10.0
The Rect branch multiplies the two fields, so 2.0 * 5.0 is 10.0.
-
Why can a constructor be passed to
map?Answer
It is a function from its fields to the type
Circle :: Double -> Shape, so map Circle [1.0, 2.0] builds a list of shapes.
-
What happens if you
printa value whose type has no Show instance?Answer
The compiler rejects the program with a missing instance error
print needs Show, and the instance is checked at compile time; deriving (Show) supplies it.
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