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.
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 ]Full example and walkthrough
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 ]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)- A row variable.
greettakes a record with anameof typeText. Its implicit argumentr : Row Typestands for the record's other fields. (lines 1–3:...r) - The fields of Ada.
greetalready accounts forname. Ingreet ada, the remaining field isage = 36, so the missing type is(| age : Natural |). The program passes the whole record, not just its age. (lines 4:age = 36) - The fields of the office. At
greet officethe same function still readsperson.name. This time the leftover field isfloor = 3, sorchanges to(| floor : Natural |). Nothing insidegreetchanges. (lines 5:floor = 3) - 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:
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 }
}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:
let r = { a = 1, b = 2 }
let s = { a = 10, ...r }
in { visible = s.a, uncovered = (s without a).a }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:
recover (Outcome.Missing "config")runs theMissinghandler. It returnsFoundwith the text"default config".recover (Outcome.Denied 3)has no handler for that case. Theotherwisebranch passes the original denial back to the caller.
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.