Home / Docs / Language guide / Kinds and types

kind

Release v0.4.0Edition 0.1 and laterU0101U1002U2001U2002U5001

What it does

kind declares a type. It takes one of four forms:

Records and sums are nominal, which means a type is known by its name: two declarations of the same shape are two types. Aliases and refinements are structural over their base, which means they are the type they rename or narrow, so a Seats value is an Int wherever an Int is expected.

Learn more: Records and Sum kinds, and is.

una
module shop

pub kind Seats = Int in 0..5000

pub kind Availability = SoldOut | Few | Open

pub kind Lookup = Found(name: Text) | Missing

pub kind Event
  name: Text
  remaining: Seats

pub kind NonEmpty[T] = List[T] where it.len > 0

pub kind Count = Int

Forms

In the grammar below, each rule gives a name, =, what the name stands for and ;, quoted words are written as shown, NEWLINE, INDENT and DEDENT stand for a line break and for one more or one less level of indentation, [ ] marks an optional part, { } a part that may repeat, | separates alternatives, ( ) groups them, and (* *) holds a comment.

ebnf
kind_decl       = [ "pub" ] "kind" upper_ident [ type_params ] kind_body ;
type_params     = "[" upper_ident { "," upper_ident } "]" ;
kind_body       = alias_body | sum_body | record_body ;
alias_body      = "=" type [ "where" line_expr ] NEWLINE ;
sum_body        = "=" ( "|" variant { "|" variant }
                      | variant "|" variant { "|" variant } ) NEWLINE ;
record_body     = NEWLINE INDENT field { field } DEDENT ;
field           = lower_ident ":" type NEWLINE ;
variant         = upper_ident [ "(" field_sig { "," field_sig } ")" ] ;
field_sig       = lower_ident ":" type ;

Examples

A refinement is checked where a value enters it. Here the verifier, the part of una check that proves what the program must keep, refutes that an arbitrary Int lies in 0..5000:

una
-- fails with U5001
module shop

pub kind Seats = Int in 0..5000

pub to_seats(count: Int) -> Seats = count

The other direction needs no proof: a Seats value is an Int:

una
module shop

pub kind Seats = Int in 0..5000

pub widen(s: Seats) -> Int = s

A refinement of a refined kind conjoins both predicates, so the verifier knows both here:

una
module shop

pub kind Stock = Int in 0..100

pub kind LowStock = Stock where it < 10

pub low_stock_level(level: LowStock) -> Int
  ensures result < 10 and result >= 0
=
  level

Records are nominal: the same fields do not make the same type:

una
-- fails with U2001
module shop

pub kind Warehouse
  aisle: Int
  shelf: Int

pub kind Bin
  aisle: Int
  shelf: Int

pub as_bin(warehouse: Warehouse) -> Bin = warehouse

Edge cases

Recursive kinds. In edition 0.2, a kind may contain itself through any field, when it has a finite value:

una
module shop

pub kind Bundle = Item(sku: Int) | Pair(left: Bundle, right: Bundle)

A kind with no finite value is rejected:

una
-- fails with U2002
module shop

pub kind Chain = | Link(rest: Chain)

Refined type arguments. In editions 0.1 and 0.2, a refined kind cannot be a type argument (the type in brackets, as in List[Quantity]), inside a kind either, since refinements are top-level:

una
-- fails with U2002
module shop

pub kind Quantity = Int where it > 0

pub kind Order
  quantities: List[Quantity]

One name per declaration. Two declarations of a module may not share a name in one namespace, variants included:

una
-- fails with U1002
module shop

pub kind Size = | Small

pub kind Fit = | Small

Further rules:

Diagnostics

Edition

Every form is in editions 0.1 and 0.2. Edition 0.3 accepts refined type arguments, as List[Quantity]: every value put into such a position must be proven to satisfy the refinement, and every value read from it carries it. Recursion differs: edition 0.1 allows a kind to contain itself only through List or Option fields.

una
-- edition 0.1
-- fails with U2002
module shop

pub kind Bundle = Item(sku: Int) | Pair(left: Bundle, right: Bundle)

Normative source