Dependent types and proofs
Types that mention values, equations as types, and proofs by computation and by induction, which the kernel checks as it checks any program.
A test can check that doubling three gives six. Here we want a reason that works for any natural number. The return type double n === n + n says exactly what a caller gets: evidence of that equation for its particular n. This is a dependent type. Think of the proof as recursion with two branches: zero, or one more than a smaller number.
Step 1 of 6: Read the return type as a promise
let doubleIsPlus
: forall (n : Natural) -> double n === n + n
= \n ->
Natural/elim
(\(k : Natural) -> double k === k + k)
refl
(\(k : Natural) (hypothesis : double k === k + k) ->
std:Equal.cong (\(m : Natural) -> m + 2) hypothesis)
n
in doubleIsPlusFull example and walkthrough
let double
: Natural -> Natural
= \n ->
Natural/elim
(\(_ : Natural) -> Natural)
0
(\(_ : Natural) (rest : Natural) -> rest + 2)
n
let doubleIsPlus
: forall (n : Natural) -> double n === n + n
= \n ->
Natural/elim
(\(k : Natural) -> double k === k + k)
refl
(\(k : Natural) (hypothesis : double k === k + k) ->
std:Equal.cong (\(m : Natural) -> m + 2) hypothesis)
n
in doubleIsPlusType
forall (n : Natural)
-> Natural/elim
(\(_ : Natural) -> Natural)
0
(\(_ rest : Natural) -> rest + 2)
n
=== n + n- Read the return type as a promise. The type of
doubleIsPlusdepends on its input: for every numbern, it is a proof thatdouble nequalsn + n. (lines 10:double n === n + n) - Name the promise for one input.
Natural/elimproves by induction when the type of its result depends on the number. Its first argument says what it proves for eachk: the equationdouble k === k + k. (lines 13:double k === k + k) - Start with zero. For
0, bothdouble 0and0 + 0compute to0, sorefl, the proof that a value equals itself, proves the equation. (lines 14:refl) - Keep the result from the smaller input. Read
hypothesis : double k === k + kas the result returned by the call atk. You can use it, but the caller now needsdouble (k + 1) === (k + 1) + (k + 1). (lines 15:hypothesis : double k === k + k) - Add two to both sides.
std:Equal.congadds two to both sides of the smaller proof here:double k + 2 === (k + k) + 2. Why does that fit? The left side of the goal takes therest + 2branch ofdouble; on the right the two extra ones add up to two. (lines 16:std:Equal.cong) - Return the proof to the caller. Applied to
n, the eliminator proves the equation for every input.omegyu typeprints the proved equation withdoubleunfolded, because the name means nothing outside the program. (lines 17–18:doubleIsPlus)
Proofs by computation
When both sides of an equation compute to the same normal form, refl
proves it, and assert : T is refl checked against T. If the two sides do not compute to the same thing, this particular proof is rejected; a different proof may still work. refl in the following program does not prove the equation above,
because double n cannot compute while n is unknown:
let double
: Natural -> Natural
= \n ->
Natural/elim
(\(_ : Natural) -> Natural)
0
(\(_ : Natural) (rest : Natural) -> rest + 2)
n
let wrong
: forall (n : Natural) -> double n === n + n
= \n -> refl
in wrongError E0003
error[E0003]: type mismatch
┌─ example.omg:11:13
│
11 │ = \n -> refl
│ ^^^^ this has type _ n === _ n
│
= expected: double n === n + n
actual: _ n === _ n
= because
n + n
is not
Natural/elim
(\(_ : Natural) -> Natural)
0
(\(_ rest : Natural) -> rest + 2)
n
= in a context where
n : Natural
Constraints as implicit arguments
An implicit argument can be a proof. bounded takes a width n with the
proof that n is less than 4, and the elaborator finds that proof when
the comparison computes to True:
let bounded =
\(n : Natural)
{valid : std:Natural.lessThan n 4 === True}
(value : Word n) ->
value
in bounded 3 (Word/fromNatural 3 5)Normal form
Word/fromNatural 3 5In the rejected call, the width 5 makes the required equation False === True. That is different from a valid constraint whose proof cannot be inferred. In the reference call bounded n value, the caller already has the needed proof, valid.
Proofs that need arithmetic
The kernel computes +, * and the other operations on literals, and
knows a few of their laws on unknowns, such as n + 0 = n. It does not
know that multiplication commutes, so refl cannot prove n * m === m * n here:
let swap = \(n m : Natural) -> (refl : n * m === m * n)
in swapError E0003
error[E0003]: type mismatch
┌─ example.omg:1:33
│
1 │ let swap = \(n m : Natural) -> (refl : n * m === m * n)
│ ^^^^ this has type _ n m === _ n m
│
= expected: n * m === m * n
actual: _ n m === _ n m
= because m is not n
= in a context where
m : Natural
n : Natural
std:Arith proves such laws without axioms. It starts
from std:Arith.NatFacts, the record of the six defining equations of the
builtins that the type checker does not decide on variables. A theorem that
needs them takes them as an argument, so what it rests on is in its type:
std:Arith.timesCommType
std:Arith.NatFacts -> forall (n m : Natural) -> n * m === m * nThe module checks examples on literals. Those checks do not establish the
six equations for every input: the NatFacts argument is a hypothesis of
timesComm. See the arithmetic laws and their types.
Propositions as types
A -> B is implication, forall is universal quantification, exists is
existential quantification, A * B is conjunction, and <>, the union
without alternatives, is falsity. Every closed term computes to a normal
form, and no normal form has type <>, so no program proves falsity or a
false equation. docs/design.md argues this; no machine
has checked it.