Concept 5 of 8

Rows, records and unions

How a row variable stands for the fields that a function does not name, and how one mechanism types records, unions and the operations that an action may use.

The fields of a record type form a row, and so do the alternatives of a union type. Rows are first-class values: (| name : Text, age : Natural |) has type Row Type. In the record type below, ...r splices in the other fields. A function over { name : Text, ...r } takes every record that has a name, whatever its other fields, and the type checker infers r at each application.

A row variable taking the other fields of two records
1 / 4

Step 1 of 4: A row variable

let greet
    : forall {r : Row Type} -> { name : Text, ...r } -> Text
    = \person -> "Hello, ${person.name}!"
let ada = { name = "Ada", age = 36 }
let office = { name = "the office", floor = 3 }
in  [ greet ada, greet office ]
Your result
Complete example · with imports
Full example and walkthrough
Elaborated
let greet
    : forall {r : Row Type} -> { name : Text, ...r } -> Text
    = \person -> "Hello, ${person.name}!"
let ada = { name = "Ada", age = 36 }
let office = { name = "the office", floor = 3 }
in  [ greet ada, greet office ]
Your result
Complete example · with imports

Elaborated term

let greet =
      ((\{r : Row @1 Type} (person : { name : Text, ...r }) ->
          "Hello, ${person.name}!") :
         forall {r : Row @1 Type} -> { name : Text, ...r } -> Text)
let ada = { name = "Ada", age = 36 }
let office = { name = "the office", floor = 3 }
in  ([ greet @((| age : Natural |) : Row Type) ada
     , greet @((| floor : Natural |) : Row Type) office
     ] :
       List Text)
  1. A row variable. greet takes a record with a name of type Text. Its implicit argument r : Row Type stands for the record's other fields. (lines 1–3: ...r)
  2. The fields of Ada. greet already accounts for name. In greet ada, the remaining field is age = 36, so the missing type is (| age : Natural |). The program passes the whole record, not just its age. (lines 4: age = 36)
  3. The fields of the office. At greet office the same function still reads person.name. This time the leftover field is floor = 3, so r changes to (| floor : Natural |). Nothing inside greet changes. (lines 5: floor = 3)
  4. One function. The elaborated term below shows the two rows that were inferred. The program's value is [ "Hello, Ada!", "Hello, the office!" ]. (lines 6: greet ada, greet office)

In the elaborated term, Row @1 Type is Row Type with its implicit level written out: Type is a type of Type 1, and so is Row Type.

Records

A record holds named fields, and its type lists the types of the fields. The order of the fields does not matter:

Normal form
let ada = { name = "Ada", age = 36, address = { city = "Paris" } }
in  { name = ada.name
    , some = ada.{ name, age }
    , older = ada with age = 37
    , moved = ada with address.city = "London"
    , anonymous = ada without address
    , contact = { email = "ada@example.org", ...ada }
    }
Your result
Complete example · with imports

Normal form

{ anonymous = { age = 36, name = "Ada" }
, contact =
    { address = { city = "Paris" }
    , age = 36
    , email = "ada@example.org"
    , name = "Ada"
    }
, moved = { address = { city = "London" }, age = 36, name = "Ada" }
, name = "Ada"
, older = { address = { city = "Paris" }, age = 37, name = "Ada" }
, some = { age = 36, name = "Ada" }
}

Labels are scoped: extending a record with a field that it already has hides the old field, and without uncovers it again. This is why a row variable can stand for any fields, even fields named like the ones that a function names:

Normal form
let r = { a = 1, b = 2 }
let s = { a = 10, ...r }
in  { visible = s.a, uncovered = (s without a).a }
Your result
Complete example · with imports

Normal form

{ uncovered = 1, visible = 10 }

Unions

In the record above, ...r means “the other fields I can leave alone”. In a union, ...r means “the other cases my caller might supply”. Follow the same recovery function on two inputs:

Without a fallback, a missing case is an error. The rejected merge handles circles but not Point.

Rows of operations

Rows also type what an action may do. An action of a contract receives a record of operations whose type ends with a row variable, so it works in every context that gives at least those operations. Actions and modes follows this through, and the reference covers rows in full.