Documentation menu
Language

Unions

Union types and their alternatives, merge and otherwise, alternatives without a payload, the empty union, and widening to a larger union.

Find a section4 sections

A union type lists alternatives, each with or without a payload. A constructor, such as Shape.Circle, is a function from the payload, or a value when the alternative has no payload. merge handlers u takes a value u of the union apart, with one handler for each alternative:

Normal form
let Shape =
      < Circle : Natural
      | Rectangle : { width : Natural, height : Natural }
      | Point
      >
let area
    : Shape -> Natural
    = \shape ->
        merge
          { Circle = \(r : Natural) -> 3 * r * r
          , Rectangle =
              \(r : { width : Natural, height : Natural }) ->
                r.width * r.height
          , Point = 0
          }
          shape
in  [ area (Shape.Circle 2)
    , area (Shape.Rectangle { width = 2, height = 3 })
    , area Shape.Point
    ]
Your result
Complete example · with imports

Normal form

[ 12, 6, 0 ]

A missing handler is an error, and so is a handler of an alternative that the union does not have:

Rejected
let Shape = < Circle : Natural | Point >
let area
    : Shape -> Natural
    = \shape -> merge { Circle = \(r : Natural) -> 3 * r * r } shape
in  area Shape.Point
Your result
Complete example · with imports

Error E0009

error[E0009]: this `merge` does not handle every alternative
  ┌─ example.omg:4:17
  │
4 │     = \shape -> merge { Circle = \(r : Natural) -> 3 * r * r } shape
  │                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no handler for `Point`
  │
  = help: add a handler for each missing alternative: `Point = ...`
  = help: or handle all the others at once: `merge handlers union otherwise (\rest -> ...)`
Rejected
let Shape = < Circle : Natural | Point >
in  merge
      { Circle = \(r : Natural) -> r, Point = 0, Square = 1 }
      Shape.Point
Your result
Complete example · with imports

Error E0010

error[E0010]: this `merge` has handlers for alternatives the union does not have
  ┌─ example.omg:3:50
  │
3 │       { Circle = \(r : Natural) -> r, Point = 0, Square = 1 }
  │                                                  ^^^^^^ the union has no alternative `Square`
  │
  = help: remove the handler, or check the spelling of the alternative

Handling some alternatives

merge handlers u otherwise f handles some alternatives, and passes a value of any other alternative to f, as a value of the union of the alternatives that have no handler:

Normal form
let Shape = < Circle : Natural | Square : Natural | Point >
let round
    : Shape -> Bool
    = \shape ->
        merge
          { Circle = \(_ : Natural) -> True }
          shape
          otherwise (\(_ : < Square : Natural | Point >) -> False)
in  [ round (Shape.Circle 1), round Shape.Point ]
Your result
Complete example · with imports

Normal form

[ True, False ]

merge also takes an Optional apart, with the handlers None and Some:

Normal form
merge { None = 0, Some = \(n : Natural) -> n } (Some 3)
Your result
Complete example · with imports

Normal form

3

Alternatives without a payload

An alternative without a payload is an alternative whose payload is the empty record: < Point > is < Point : unit >. Both Shape.Point and Shape.Point {=} are accepted, and so are the handlers Point = 0 and Point = \(_ : unit) -> 0:

Normal form
let Shape = < Circle : Natural | Point >
in  [ merge
        { Circle = \(r : Natural) -> r, Point = \(_ : unit) -> 0 }
        Shape.Point
    , merge
        { Circle = \(r : Natural) -> r, Point = 0 }
        (Shape.Point {=})
    ]
Your result
Complete example · with imports

Normal form

[ 0, 0 ]

An alternative is a label, optionally followed by : and the type of its payload. It is never a type by itself, so an alternative without a payload cannot be named after one of the builtin types that payloads hold: Bool, Natural, Integer, Double, Text, unit and Type. The rejected type below uses Natural and Text as labels, not payloads.

Rejected
let Bad = < Natural | Text >
in  Bad
Your result
Complete example · with imports

Error E0115

error[E0115]: `Natural` is a label here, not a type
  ┌─ example.omg:1:13
  │
1 │ let Bad = < Natural | Text >
  │             ^^^^^^^ an alternative without payload, named `Natural`
  │                       ---- and this one, named `Text`
  │
  = an alternative of a union type is a label, optionally followed by `:` and the type of its payload
  = help: to hold values of these types, give each alternative a label: `< N : Natural | T : Text >`
  = help: to name the alternatives so, write the names between backticks: `` < `Natural` | `Text` > ``

To hold values of these types, give each alternative a label. To name the alternatives after them anyway, write the names between backticks, as in `Natural` and `Text`:

Type
{ held = < N : Natural | T : Text | K : Type 1 >
, labels = < `Natural` | `Text` >
}
Your result
Complete example · with imports

Type

{ held : Type 2, labels : Type }

The other builtin names of types are ordinary labels: the type constructors, as in < List | Grid > and < Optional | Required >, and the universes above Type, as in < Kind | Name >.

The empty union

<> is the union without alternatives. It has no values, and absurd eliminates it, giving a value of any type:

Type
let exFalso
    : forall {a : Type} -> <> -> a
    = \{a} impossible -> absurd (\(_ : <>) -> a) impossible
in  exFalso
Your result
Complete example · with imports

Type

forall {a : Type} -> <> -> a

Widening

A value of a union type can be used where a union type with more alternatives is expected: as an argument, under an annotation or as a branch of an if.

Normal form
let Small = < A : Natural | B >
let Big = < C : Text | A : Natural | B >
let widen : Small -> Big = \s -> s
in  widen (Small.A 1)
Your result
Complete example · with imports

Normal form

< A : Natural | B | C : Text >.A 1