Theory

Theory

Can you tell what a counter will read without running it? Build three proofs about a counter, from reading back its starting value to counting any number of ticks.

A test calls a function and checks the answer. A proof can be a function too: give it an input and it returns evidence about that input. We'll keep the counter and change where the evidence comes from.

  1. Easy: compute. Read back the number you just stored.

  2. Medium: use a promise. If two counts agree, will they still agree after a tick?

  3. Hard: prove the next step. Start at zero and account for every tick.

At each stop, predict the answer, then open the question to compare your reasoning. To try a change yourself, choose Edit on the example, then Check. Rewind restores the published proof. You can also follow the questions and answers without running the editor.

Easy: compute the answer

What did the counter remember?

start takes a number and builds a record. Its ordinary type, Natural -> { count : Natural }, says you can read a number back. It doesn't tell you which number. Look at the return type of readStart: === is not a test that returns True or False. It forms a type whose values are evidence that the two sides are equal.

Read back the starting count
let start = \(n : Natural) -> { count = n }
let readStart : forall (n : Natural) -> (start n).count === n
              = \n -> refl
in  readStart
Your result
Complete example · with imports

The compiler accepts this program

Why does the kernel accept refl?

Leave n unknown. Unfold start n to get { count = n }. Select count and you're back at n. The goal is now n === n; refl is the proof that a value equals itself. The proof program is \n -> refl, a function that supplies that evidence for any n. Its result type mentions its argument: that's a dependent function.

Suppose you store 3 instead. Does a passing test at 3 rescue the proof?

No. Change count = n to count = 3 and check. Reading the counter created with 3 still returns 3, but the proof now promises 3 === n for an unknown n. In particular, it would have to work for 4.

Medium: use a promise

Two counters start at a and b. You don't know either number, but the caller gives you same : a === b. Your job is to prove that ticking both counters preserves their agreement. refl alone won't do: a and b are different variables. std:Equal.cong takes a function and evidence that its inputs are equal, and gives back evidence that its outputs are equal.

Tick both counters
let tick = \(n : Natural) -> n + 1
let together : forall (a b : Natural) -> a === b -> tick a === tick b
             = \a b same -> std:Equal.cong tick same
in  together
Your result
Complete example · with imports

The compiler accepts this program

If a tick adds two instead of one, does the proof still work?

Yes. Change n + 1 to n + 2 and check. The proof uses the same function on both sides; it never uses the size of the increment. Now try replacing std:Equal.cong tick same with refl. That fails: it discards the only evidence connecting a to b.

Hard: prove the next step

Start at zero. Does a counter read n after n ticks? With an unknown n, the checker can't just run a loop n times. Instead, build the proof the way you'd write a recursive function: give an answer for zero, then turn the answer for k into one for k + 1. This is induction.

Count every tick
let tick = \(n : Natural) -> n + 1
let ticks = Natural/elim (\(_ : Natural) -> Natural) 0
              (\(_ rest : Natural) -> tick rest)
let counted : forall (n : Natural) -> ticks n === n
            = Natural/elim (\(k : Natural) -> ticks k === k)
                refl
                (\(k : Natural) (ih : ticks k === k) -> std:Equal.cong tick ih)
in  counted
Your result
Complete example · with imports

The compiler accepts this program

Read the two uses of Natural/elim side by side. The first builds a number: zero, or tick rest. The second builds a proof: at zero, ticks 0 computes to 0. In the step, ih is the proof returned for the smaller input, not a new assumption about all numbers. The medium proof's move gives tick (ticks k) === tick k. The left is how ticks (k + 1) is computed; the right is k + 1. That's precisely the next claim.

Change the tick to add two again. Which part of this proof breaks first?

The base case still checks. In the step, applying tick to ih now proves that the new count is k + 2, assuming the previous count was k. The goal still says k + 1. Try the edit, then rewind: this claim depends on the increment, unlike the medium claim.

The elaborator fills in details such as which equality refl is meant to prove. The kernel checks the resulting program against its type. These are claims about the functions above, not a guarantee about deployed bytecode. Next, step through a different induction proof in Learn or see what is checked, tested and assumed. For a contract author's use of these types, read Omegyu vs. Solidity.

References

Start with Coquand and Huet’s Calculus of Constructions for the idea behind readStart: a function can return evidence about its input.

Browse all references

Where the rules come from

These are the foundations, alternatives and limits cited in the language design notes. None proves the full kernel or EVM backend correct.

Checking a proof

Filling in what the reader didn't write

Records and their alternatives

Universes and the limits of the proofs