Equality and proofs
Equations as types, refl and assert, proofs of computed constraints as implicit arguments, propositions as types, and proofs about numbers with std:Arith and std:Ring.
x === y is the type of the proofs that x and y are equal. refl
proves it when both sides have the same normal form, and Equal/elim takes
a proof apart. std:Equal has the usual lemmas: sym,
trans, cong, subst and cast.
let plusZero : forall (n : Natural) -> n + 0 === n = \n -> refl
let test = assert : std:List.length [ 1, 2, 3 ] === 3
in { plusZero, test }Type
{ plusZero : forall (n : Natural) -> n === n
, test : std:List.length [ 1, 2, 3 ] === 3
}The type of plusZero shows the equation after computation: to the type
checker, n + 0 is n.
assert : T is refl checked against the equation T. When the equation
does not hold, the error shows the normal forms of both sides, which is how
tests are written:
let test = assert : std:List.length [ 1, 2 ] === 3
in testError E0015
error[E0015]: assertion failed
┌─ example.omg:1:12
│
1 │ let test = assert : std:List.length [ 1, 2 ] === 3
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the two sides are not equal
│
= the left-hand side normalises to: 2
and the right-hand side to: 3
= an assertion holds when both sides have the same normal form
Dependent types and proofs builds a proof by induction, one step at a time.
Constraints as implicit arguments
An implicit argument can be the proof of a computed constraint. After
ordinary inference, the elaborator gives refl when both sides are equal
by computation, or reuses a proof of the same equation that is in scope.
Look at bounded n value inside forward. The call omits the proof argument. Its caller already has valid for this very n, so the elaborator can supply that proof.
let bounded =
\(n : Natural)
{valid : std:Natural.lessThan n 4 === True}
(value : Word n) ->
value
let forward =
\(n : Natural)
{valid : std:Natural.lessThan n 4 === True}
(value : Word n) ->
bounded n value
in forwardType
forall (n : Natural) {valid : std:Natural.lessThan n 4 === True}
-> Word n
-> Word nThe kernel checks the resulting proof. The elaborator does not choose data
to make a predicate true, search for theorems or fill explicit _ holes:
give the proof with @ when a valid constraint needs more reasoning. A
constraint that is false has no proof:
let bounded =
\(n : Natural)
{valid : std:Natural.lessThan n 4 === True}
(value : Word n) ->
value
in bounded 5 (Word/fromNatural 5 5)Error E0020
error[E0020]: cannot infer the implicit argument `valid`
┌─ example.omg:6:5
│
6 │ in bounded 5 (Word/fromNatural 5 5)
│ ^^^^^^^^^ nothing determines the implicit argument `valid`
│
= it must have type False === True
= help: add a type annotation, or give the implicit argument explicitly with `@`
As with other implicit arguments, an implicit binder at the end of a function's type stays in its type. Put a constraint before an explicit argument when an application should infer it.
Propositions as types
| Type | Proposition |
|---|---|
A -> B | A implies B |
forall (x : A) -> P x | every x has the property P |
exists (x : A) * P x | some x has the property P |
A * B | A and B |
<> | falsity |
x === y | x equals y |
A closed term of type <> would be a proof of falsity. Every closed term
computes to a normal form and no normal form has type <>, so none exists:
this is the consistency of Omegyu's theory, which
docs/design.md argues and no machine has checked.
Proofs about numbers
The type checker computes +, *, Natural/subtract, Natural/div and
Natural/mod on literals, whatever their size, and knows some of their
laws on variables: n + 0, (n + 1) + m, 0 * n, n * 1 and
Natural/subtract 0 n, among others. It does not know laws such as
n * m === m * n.
std:Arith proves those from a hypothesis:
std:Arith.NatFacts, the record of the six defining equations of *,
Natural/subtract, Natural/div and Natural/mod that the type checker
does not decide on variables.
std:Arith.NatFactsNormal form
{ divMod :
forall (n d : Natural)
-> Natural/div n (d + 1) * (d + 1) + Natural/mod n (d + 1) === n
, divZero : forall (n : Natural) -> Natural/div n 0 === 0
, modLess :
forall (n d : Natural)
-> Natural/isZero
(Natural/subtract (d + 1) (Natural/mod n (d + 1) + 1))
=== True
, modZero : forall (n : Natural) -> Natural/mod n 0 === n
, subtractSucc :
forall (a b : Natural)
-> Natural/subtract (a + 1) (b + 1) === Natural/subtract a b
, timesSucc : forall (n m : Natural) -> (n + 1) * m === n * m + m
}A theorem that needs arithmetic takes the facts as an argument, so that
what it rests on is in its type: both double and fits below take this argument. std:Arith checks instances on literals; those checks are not a proof of NatFacts for arbitrary variables. See
what is assumed.
let A = std:Arith
let double
: forall (h : A.NatFacts) (n : Natural) -> 2 * n === n + n
= \h n -> h.timesSucc 1 n
-- A fact about literals: one lemma, and the type checker's computation.
let fits
: forall (h : A.NatFacts) -> A.Lt 997 1024
= \h -> A.decideLtSound h 997 1024 refl
in { double, fits }Type
{ double : std:Arith.NatFacts -> forall (n : Natural) -> 2 * n === n + n
, fits : std:Arith.NatFacts -> std:Arith.Lt 997 1024
}A.Le a b, which says that a is at most b, is
exists (d : Natural) * a + d === b:
std:Arith.Le 1 3Normal form
exists (d : Natural) * d + 1 === 3A.leElim takes such a proof apart, and gives the difference to a function
as a variable. Its projection .1 is computed by the proof of the
inequality, which the type checker would run whenever it compares a type
that mentions it.
Polynomial identities
std:Ring proves identities between polynomials by
computing their normal forms. An expression of type std:Ring.Expr
evaluates, by definition, to the polynomial that it stands for, and ring
turns equal normal forms into an equation between the polynomials:
let A = std:Arith
let R = std:Ring
let square
: forall (h : A.NatFacts) (n : Natural)
-> (n + 1) * (n + 1) === n * n + 2 * n + 1
= \h n ->
let x = R.var 0
in R.ring
h
[ n ]
(R.mul (R.add x (R.const 1)) (R.add x (R.const 1)))
(R.add
(R.add (R.mul x x) (R.mul (R.const 2) x))
(R.const 1))
refl
in squareType
std:Arith.NatFacts
-> forall (n : Natural)
-> (n + 1) * (n + 1) === n * n + 2 * n + 1std:Ring.ringLe proves inequalities in the same way.