Concept 4 of 8

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.

A proof by induction that double n equals n + n
1 / 6

Step 1 of 6: Read the return type as a promise

Proof source · both branches
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  doubleIsPlus
Your result
Complete example · with imports
Full example and walkthrough
Type
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  doubleIsPlus
Your result
Complete example · with imports

Type

forall (n : Natural)
-> Natural/elim
     (\(_ : Natural) -> Natural)
     0
     (\(_ rest : Natural) -> rest + 2)
     n
   === n + n
  1. Read the return type as a promise. The type of doubleIsPlus depends on its input: for every number n, it is a proof that double n equals n + n. (lines 10: double n === n + n)
  2. Name the promise for one input. Natural/elim proves by induction when the type of its result depends on the number. Its first argument says what it proves for each k: the equation double k === k + k. (lines 13: double k === k + k)
  3. Start with zero. For 0, both double 0 and 0 + 0 compute to 0, so refl, the proof that a value equals itself, proves the equation. (lines 14: refl)
  4. Keep the result from the smaller input. Read hypothesis : double k === k + k as the result returned by the call at k. You can use it, but the caller now needs double (k + 1) === (k + 1) + (k + 1). (lines 15: hypothesis : double k === k + k)
  5. Add two to both sides. std:Equal.cong adds 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 the rest + 2 branch of double; on the right the two extra ones add up to two. (lines 16: std:Equal.cong)
  6. Return the proof to the caller. Applied to n, the eliminator proves the equation for every input. omegyu type prints the proved equation with double unfolded, 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:

Rejected
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  wrong
Your result
Complete example · with imports

Error 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:

Normal form
let bounded =
      \(n : Natural)
       {valid : std:Natural.lessThan n 4 === True}
       (value : Word n) ->
        value
in  bounded 3 (Word/fromNatural 3 5)
Your result
Complete example · with imports

Normal form

Word/fromNatural 3 5

In 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:

Rejected
let swap = \(n m : Natural) -> (refl : n * m === m * n)
in  swap
Your result
Complete example · with imports

Error 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:

Type
std:Arith.timesComm
Your result
Complete example · with imports

Type

std:Arith.NatFacts -> forall (n m : Natural) -> n * m === m * n

The 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.