Records
Fields have names
A product type with positional fields works, and the caller has to remember the order: Person "Ada" 36 is fine until a string and a number are both plausible in two positions. Record syntax gives every field a name in the declaration, and the names are used to build and read values.
data Person = Person
{ name :: String
, age :: Int
}
deriving (Show)
The braces belong to the declaration, not to a separate type: this is the same product type as the previous lesson, with the field order written down. A value is built with the field names as well, and the compiler checks that every field is given exactly once:
ada :: Person
ada = Person {name = "Ada", age = 36}
Construction with braces also allows a different order, which is the first benefit: Person {age = 36, name = "Ada"} is the same value, and a reader never has to count arguments.
Accessors are functions
For every field the compiler writes a function from the record to the field: name :: Person -> String and age :: Person -> Int. They are ordinary functions, so they compose, map and pass as arguments, exactly like the helper functions of the earlier lessons.
module Main (main) where
data Person = Person
{ name :: String
, age :: Int
}
deriving (Show)
birthday :: Person -> Person
birthday p = p {age = age p + 1}
main :: IO ()
main = do
let ada = Person {name = "Ada", age = 36}
print ada
print (birthday ada)
print (name ada)
print (age ada)
ghc -Wall -O1 -o person_rec person_rec.hs
./person_rec
Person {name = "Ada", age = 36}
Person {name = "Ada", age = 37}
"Ada"
36
birthday reads a field with the accessor and returns a new value with one field changed; the original is untouched, as always in Haskell. The last two lines show the accessors used directly: a record is not a struct with hidden fields, it is a value with functions that read them.
Update syntax
p {age = 37} is record update: it copies the record and replaces the named fields, leaving the rest as they were. Several fields can change at once, and the expression is not a mutation: it builds a new value.
module Main (main) where
data Config = Config
{ host :: String
, port :: Int
, debug :: Bool
}
deriving (Show)
defaults :: Config
defaults = Config {host = "localhost", port = 8080, debug = False}
main :: IO ()
main = do
print defaults
print (defaults {port = 9090})
print (defaults {debug = True, port = 1})
ghc -Wall -O1 -o config config.hs
./config
Config {host = "localhost", port = 8080, debug = False}
Config {host = "localhost", port = 9090, debug = False}
Config {host = "localhost", port = 1, debug = True}
This is the shape of configuration in real Haskell: one base value with the defaults, and updates that override one or two fields. It replaces a builder object with a value, and the type checker still sees every field.
The derived Show
The derived Show of a record prints the constructor and the fields with their names, which is why the output above is Config {host = "localhost", port = 8080, debug = False} rather than three bare values. That format is also valid Haskell syntax: the printed value can be read back, which the lesson on Read uses.
Two limits are worth knowing. Field names live in the module namespace, so two records cannot both have a name field in one module without the DuplicateRecordFields extension; and an accessor such as name is partial in the sense that it only works on its own record type, which the type checker enforces. The next lessons on type classes and newtype show the tools for fields shared across types.
Exercises
- Add an email field to Person and update the construction and the update syntax.
- Write older :: Person -> Person -> Person that returns the person with the greater age.
- Print the name of every person in a list with map name.
- Build a Config from the defaults with two fields changed, then change one more in a second update.
- Explain what the compiler generates for each field of a record.
- Add deriving (Show, Eq) and compare two records for equality.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the compiler generate for every field of a record?
Answer
A function from the record to that field
Each field name becomes an accessor function, which is why
age preads the field. -
What does
p {age = 37}do?Answer
Returns a copy of p with the age field replaced
Record update builds a new value; the original record is unchanged.
-
What does this program print?
module Main (main) where data Person = Person { name :: String , age :: Int } deriving (Show) main :: IO () main = print (Person {name = "Ada", age = 36})Answer
Person {name = "Ada", age = 36}
The derived Show of a record prints the constructor and the field names with their values.
-
What is the type of the accessor
ageforPerson?Answer
Person -> IntAn accessor takes the record and returns the field, which is why it can be mapped over a list of records.
-
What does
defaults {port = 9090}change?Answer
Only the port, and only in the new value
Update syntax names the fields to replace and leaves the rest untouched in a fresh copy.
-
What does this program print?
module Main (main) where data Config = Config { host :: String , port :: Int } deriving (Show) main :: IO () main = print (Config {host = "x", port = 1} {port = 2})Answer
Config {host = "x", port = 2}
The update replaces the port with 2 and keeps the host, so the printed record shows both fields.
-
Can a record be constructed with the fields in a different order than the declaration?
Answer
Yes, names order the fields, not positions
Brace construction matches by name, so the order in the source does not matter.
-
Why can two records in one module not both have a field called
nameby default?Answer
Accessors are top-level functions, so the names would clash
Each field name becomes a function in the module scope; DuplicateRecordFields relaxes the rule.
-
What does
map name peopleproduce for a list of Person records?Answer
The list of their names
The accessor is a function, so map applies it to every record and collects the names.
-
What does the derived Show of a record print?
Answer
The constructor and the fields with names and values
For a record the derived instance uses the brace syntax with field names, which is also readable back as Haskell.
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