Documentation menu
Language

Data

The builtin types of data and their operations, words of a given width, the eliminators that recursion goes through, and pairs.

Find a section3 sections
TypeValuesOperations
BoolTrue, Falseif c then a else b, &&, ||, ==, !=, Bool/elim
Natural0, 42+, *, Natural/subtract, Natural/div, Natural/mod, Natural/isZero, Natural/show, Natural/elim
Integer+1, -1Integer/add, Integer/multiply, Integer/negate, Integer/clamp, Integer/show
Double1.5, NaN, InfinityDouble/add, Double/subtract, Double/multiply, Double/divide, Double/lessThan, Double/show
Text"abc"++, "${x}", Text/length, Text/replace, Text/equal, Text/chars
List a[ 1, 2 ], [] : List Natural#, which appends, List/length, List/elim
Optional aSome x, None amerge, Optional/elim
Word bitsWord/fromNatural 8 255Word/add, Word/sub, Word/mul, Word/and, Word/shl, Word/toNatural

Builtins lists every operation with its type.

Normal form
{ bool = True && False || True
, natural = Natural/subtract 2 10
, integer = Integer/add -3 +5
, double = Double/multiply 1.5 2.0
, text = "abc" ++ Natural/show 4
, list = [ 1, 2 ] # [ 3 ]
, optional = Some "x"
}
Your result
Complete example · with imports

Normal form

{ bool = True
, double = 3.0
, integer = +2
, list = [ 1, 2, 3 ]
, natural = 8
, optional = Some "x"
, text = "abc4"
}

== and != compare booleans. Numbers and texts are compared with functions, such as std:Natural.equal and Text/equal:

Rejected
1 == 1
Your result
Complete example · with imports

Error E0003

error[E0003]: type mismatch
  ┌─ example.omg:1:1
  │
1 │ 1 == 1
  │ ^ this has type Natural
  │
  = expected: Bool
      actual: Natural

Words

Word : Natural -> Type gives the type of the words of a width. Arithmetic wraps modulo 2^bits, and Word 0 holds only zero. A width can be computed or quantified over, and words of different widths do not mix:

Normal form
let twice
    : forall {bits : Natural} -> Word bits -> Word bits
    = \{bits} x -> Word/add x x
in  twice (Word/fromNatural 8 130)
Your result
Complete example · with imports

Normal form

Word/fromNatural 8 4
  • Word/fromNatural keeps the low bits of its input. Word/fromInteger encodes negative integers in two’s complement, such as the -7 in the signed division below.

  • Word/toNatural reads a word as unsigned, Word/toInteger as signed.

  • Word/div and Word/mod are unsigned. Word/sdiv and Word/smod are signed, with the quotient rounded toward zero. A division or a remainder by zero gives zero.

  • Word/addmod and Word/mulmod compute exactly before they reduce by their third operand.

  • Word/shl, Word/shr and Word/sar take the count of the shift first, as a Natural. A count of at least the width gives zero, or copies of the sign bit for Word/sar.

  • Word/pow takes a natural exponent, and Word/clz counts the leading zero bits.

  • Word/equal, Word/lt, Word/slt and Word/isZero give booleans.

Normal form
{ byZero = Word/div (Word/fromNatural 8 7) (Word/fromNatural 8 0)
, sdiv = Word/sdiv (Word/fromInteger 8 -7) (Word/fromNatural 8 2)
, sar = Word/sar 9 (Word/fromInteger 8 -1)
, addmod =
    Word/addmod
      (Word/fromNatural 8 200)
      (Word/fromNatural 8 100)
      (Word/fromNatural 8 7)
, signed = Word/toInteger (Word/fromNatural 8 255)
}
Your result
Complete example · with imports

Normal form

{ addmod = Word/fromNatural 8 6
, byZero = Word/fromNatural 8 0
, sar = Word/fromNatural 8 255
, sdiv = Word/fromNatural 8 253
, signed = -1
}

A normal form shows a word as Word/fromNatural bits value, which reads back as the same word. std:Word gives the aliases Word8, Word64 and Word256, and zero, one, resize and resizeSigned.

Choose a result type before supplying operands: std:Word.arithmetic std:Word.wrapping 8 returns eight-bit word operations, while std:Word.arithmetic std:Word.checked 8 returns operations with Optional (Word 8) results. Adding one to 255 produces zero with the first policy and None with the second. Neither policy decides how a contract reports failure. The word laws distinguish equations at every width from the 256-bit theorems: under Arith.NatFacts, successful checked additions and additions of a product agree with unbounded arithmetic.

Word/elim applied to P, handler and w computes, or proves, something about any word from a function of its unsigned value. On a literal word, the handler receives the canonical value, which is below 2^bits:

Normal form
Word/elim
  (\(_ : Word 8) -> Natural)
  (\(n : Natural) -> n)
  (Word/fromNatural 8 300)
Your result
Complete example · with imports

Normal form

44

For widths up to 4096, the unsigned observations of addition, subtraction, multiplication, division, remainder, the exact modular operations and the logical shifts unfold, on unknown operands, to equations of naturals. std:Arith proves laws of Word 256 from them, without axioms about words.

Eliminators

Each data type has an eliminator, which is its induction principle. Natural/elim computes by recursion on a number, or proves a property by induction on it, which is the same thing:

Type
Natural/elim
Your result
Complete example · with imports

Type

forall {b : Level} (P : Natural -> Type b)
-> P 0
-> (forall (n : Natural) -> P n -> P (n + 1))
-> forall (n : Natural)
-> P n

The type checker knows a few laws of the builtin operations on unknown values. n + 0, 0 + n and n are the same to it, and so are (xs # ys) # zs and xs # (ys # zs):

Type
let zero = \(n : Natural) -> (refl : 0 + n === n)
let assoc =
      \(xs ys zs : List Natural) -> (refl : (xs # ys) # zs === xs # (ys # zs))
in  { zero, assoc }
Your result
Complete example · with imports

Type

{ assoc :
    forall (xs ys zs : List Natural)
    -> xs # (ys # zs) === xs # (ys # zs)
, zero : forall (n : Natural) -> n === n
}

Pairs

(a, b) is a pair, and p.1 and p.2 are its components. (a, b, c) is (a, (b, c)). A * B is the type of the pairs of an A and a B, and also the product of two numbers:

Normal form
let p = (1, True)
let triple = (1, ("two", 3.0))
in  { first = p.1, second = p.2, rest = triple.2 }
Your result
Complete example · with imports

Normal form

{ first = 1, rest = ("two", 3.0), second = True }

In a dependent pair, the type of the second component mentions the first:

Type
let sized
    : exists (n : Natural) * std:Vec.Vec Text n
    = (2, std:Vec.cons "a" (std:Vec.cons "b" std:Vec.nil))
in  sized
Your result
Complete example · with imports

Type

exists (n : Natural) * std:Vec.Vec Text n