Data
The builtin types of data and their operations, words of a given width, the eliminators that recursion goes through, and pairs.
| Type | Values | Operations |
|---|---|---|
Bool | True, False | if c then a else b, &&, ||, ==, !=, Bool/elim |
Natural | 0, 42 | +, *, Natural/subtract, Natural/div, Natural/mod, Natural/isZero, Natural/show, Natural/elim |
Integer | +1, -1 | Integer/add, Integer/multiply, Integer/negate, Integer/clamp, Integer/show |
Double | 1.5, NaN, Infinity | Double/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 a | Some x, None a | merge, Optional/elim |
Word bits | Word/fromNatural 8 255 | Word/add, Word/sub, Word/mul, Word/and, Word/shl, Word/toNatural |
Builtins lists every operation with its type.
{ 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"
}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:
1 == 1Error 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:
let twice
: forall {bits : Natural} -> Word bits -> Word bits
= \{bits} x -> Word/add x x
in twice (Word/fromNatural 8 130)Normal form
Word/fromNatural 8 4Word/fromNaturalkeeps the low bits of its input.Word/fromIntegerencodes negative integers in two’s complement, such as the-7in the signed division below.Word/toNaturalreads a word as unsigned,Word/toIntegeras signed.Word/divandWord/modare unsigned.Word/sdivandWord/smodare signed, with the quotient rounded toward zero. A division or a remainder by zero gives zero.Word/addmodandWord/mulmodcompute exactly before they reduce by their third operand.Word/shl,Word/shrandWord/sartake the count of the shift first, as aNatural. A count of at least the width gives zero, or copies of the sign bit forWord/sar.Word/powtakes a natural exponent, andWord/clzcounts the leading zero bits.Word/equal,Word/lt,Word/sltandWord/isZerogive booleans.
{ 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)
}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:
Word/elim
(\(_ : Word 8) -> Natural)
(\(n : Natural) -> n)
(Word/fromNatural 8 300)Normal form
44For 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:
Natural/elimType
forall {b : Level} (P : Natural -> Type b)
-> P 0
-> (forall (n : Natural) -> P n -> P (n + 1))
-> forall (n : Natural)
-> P nThe 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):
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 }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:
let p = (1, True)
let triple = (1, ("two", 3.0))
in { first = p.1, second = p.2, rest = triple.2 }Normal form
{ first = 1, rest = ("two", 3.0), second = True }In a dependent pair, the type of the second component mentions the first: