Documentation menu
Language

Rows

Rows as first-class values, row polymorphism over records and unions, rows of capabilities, and the record types that a row lifts to a higher universe.

Find a section3 sections

The fields of a record type form a row, and so do the alternatives of a union type. Rows are first class: (| name : Text, age : Natural |) is a value of type Row Type, and ...r splices a row r into a record type or a union type.

Type
(| name : Text, age : Natural |)
Your result
Complete example · with imports

Type

Row Type

Row polymorphism

A function whose argument type ends with a row variable takes every record that has the fields it names. The type checker infers the row at each application, as it infers an implicit argument:

Normal form
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

Normal form

[ "Hello, Ada!", "Hello, the office!" ]

The same works for unions. recover handles one alternative and passes on the others, whatever they are: otherwise receives them as a value of < ...r >, which is widened to the result type.

Normal form
let recover
    : forall {r : Row Type}
      -> < Missing : Text | ...r >
      -> < Found : Text | ...r >
    = \{r} outcome ->
        merge
          { Missing =
              \(name : Text) ->
                < Found : Text | ...r >.Found "default ${name}"
          }
          outcome
          otherwise (\(rest : < ...r >) -> rest)
let Outcome = < Missing : Text | Denied : Natural >
in  [ recover (Outcome.Missing "config"), recover (Outcome.Denied 3) ]
Your result
Complete example · with imports

Normal form

[ < Denied : Natural | Found : Text >.Found "default config"
, < Denied : Natural | Found : Text >.Denied 3
]

Labels are scoped: a field or an alternative hides the ones of the same name that a row variable may bring, and a handler or a selection concerns the visible one (scoped labels).

Rows of capabilities

Rows also express what an action may do. A read-only action that is polymorphic in the rest of its row of capabilities runs in a context that also gives writes, and its body cannot use the writes, because it works for every row. std:EVM/Effect and std:EVM/Contract give contract handlers their modes so (actions and modes). The mechanism does not depend on the EVM: effect-row-inclusion.omg uses it with a monad of its own.

Lifted types

A record type or a union type lives in the largest of the universes of its fields and of its row. A row of a higher universe without entries therefore lifts the type to that universe: { x : Natural, y : Natural, ...((| |) : Row (Type 1)) } is a type of Type 1.

A record written where a lifted type is expected is a value of that type. A value of the unlifted type is not, because there is no cumulativity:

Rejected
let Point = { x : Natural, y : Natural, ...((| |) : Row (Type 1)) }
let origin : Point = { x = 0, y = 0 }
let lost = { x = 0, y = 0 }
in  (lost : Point)
Your result
Complete example · with imports

Error E0003

error[E0003]: type mismatch
  ┌─ example.omg:4:6
  │
4 │ in  (lost : Point)
  │      ^^^^ this has type { x : Natural, y : Natural }
  │
  = expected: Point
      actual: { x : Natural, y : Natural }
  = because { x : Natural, y : Natural, ...(||) : Row (Type 1) }
      is lifted to Type 1,
    and { x : Natural, y : Natural } is not lifted
  = a record type lifted to a universe above the one of its fields, as `unit @l` is, has the same values as at any other level, but it is another type: there is no cumulativity
  = help: a record written where its type is known (`{=}`, `{ a = x }`) is a value of that type at every level: build the record there, or give it the type that is expected where it is built (`let r : unit @1 = {=}`)

A row variable stands for a lift only when it stands for no fields, and only when its type lets it be of that universe, as in forall {l : Level} {r : Row (Type l)} -> …. So a function that selects x from a Point needs to know its type:

Rejected
let Point = { x : Natural, y : Natural, ...((| |) : Row (Type 1)) }
let origin : Point = { x = 0, y = 0 }
in  (\p -> p.x) origin
Your result
Complete example · with imports

Error E0003

error[E0003]: type mismatch
  ┌─ example.omg:3:17
  │
3 │ in  (\p -> p.x) origin
  │                 ^^^^^^ this has type Point
  │
  = expected: { x : _, ..._ }
      actual: Point
  = because { x : Natural, y : Natural } is not lifted,
    and { x : Natural, y : Natural, ...(||) : Row (Type 1) }
      is lifted to Type 1
  = a record type lifted to a universe above the one of its fields, as `unit @l` is, has the same values as at any other level, but it is another type: there is no cumulativity
  = the field `y` of { x : Natural, y : Natural } is what a row variable
      stands for; the entries of a row all have one type, so a row with
      entries does not lift the type it ends, and a row variable stands
      for a lift only when it stands for no fields
  = help: take the value at its type in full rather than through a row variable (`\(r : T) -> ...`), or rebuild it at the level of its fields (`{ x = r.x, y = r.y }`)
Normal form
let Point = { x : Natural, y : Natural, ...((| |) : Row (Type 1)) }
let origin : Point = { x = 0, y = 0 }
in  (\(p : Point) -> p.x) origin
Your result
Complete example · with imports

Normal form

0

The record operators keep the lift:

Type
let Point = { x : Natural, y : Natural, ...((| |) : Row (Type 1)) }
let origin : Point = { x = 0, y = 0 }
in  { moved = origin with x = 1, reduced = origin without y }
Your result
Complete example · with imports

Type

{ moved : { x : Natural, y : Natural, ...(||) : Row (Type 1) }
, reduced : { x : Natural, ...(||) : Row (Type 1) }
}

A lift that the fields already reach changes nothing:

Type
let same = assert : { a : Type, ...((| |) : Row (Type 1)) } === { a : Type }
in  same
Your result
Complete example · with imports

Type

{ a : Type, ...(||) : Row (Type 1) } === { a : Type }

A record written where a value of an unknown type of a universe above Type is expected is a value of that universe:

Type
Some @1 {=}
Your result
Complete example · with imports

Type

Optional (unit @1)