Chapter 3 · Numbers and induction
Automation you can check
Step 1 of 22 · Concept
Automation builds proofs too
In this lesson three tactics do arithmetic for you, and you see why their proofs are still checked.
| tactic | closes |
|---|---|
decide | a yes-or-no fact about given numbers |
omega | linear arithmetic on unknown numbers |
simp | a goal that library facts rewrite until it is true |
Step 2 of 22 · Concept
decide needs given values
decide (lesson 1) works out a yes-or-no fact about given numbers, such as 10 < 20 or 7 * 8 = 56.
With a variable it has nothing to work out, and it refuses:
example (n : Nat) : n < n + 1 := by
decide
Main.lean:2:2: error: Expected type must not contain free variables
n < n + 1
Hint: Use the `+revert` option to automatically clean up and revert free variables
A free variable is a name such as n that stands for any number.
Step 3 of 22 · Quiz
Which goal does decide close?
Each goal is in its own example, and a, b, n are Nat variables. Which goal does decide close?
Step 4 of 22 · Concept
omega does linear arithmetic
omega proves goals about Nat that are linear: built from these, with the hypotheses as facts.
| kind | what omega reads |
|---|---|
| numbers | +, - (stopping at 0), * by a given number |
| division | / and % by a given number |
| comparisons | ≤, <, =, ≠ |
It fails on a * b, two unknowns multiplied: that is not linear.
Step 5 of 22 · Quiz
Which does omega prove?
a and b are Nat variables. Which goal does omega prove?
Step 6 of 22 · Demo
See omega work
Press Run: Lean accepts all three. The first and the third take a hypothesis; omega finds it among the
hypotheses itself.
Step 7 of 22 · Demo
See omega refuse
omega lists what a counterexample would have to satisfy. It names each unknown with a letter of its own, under where. ↑
marks a Nat that omega works with as an integer; read past it.
Press Run and read the message: c is b * a, d is a * b, two numbers omega cannot relate.
Step 8 of 22 · Exercise
Your turn: three goals for omega
Prove the three theorems. Each is linear arithmetic, but one needs a step before omega.
omega reads facts about numbers, but it does not look inside the ∀ of h. On o1 alone it knows nothing about f 3:
error: omega could not prove the goal:
a possible counterexample may satisfy the constraints
a ≥ 5
where
a := ↑(f 3)
First turn h into a fact about 3, with specialize (lesson 8). Then omega can use it.
In o3, 2 * n and % 2 are * and % by a given number: linear, so omega alone proves it.
Step 9 of 22 · Concept
simp rewrites until nothing changes
simp rewrites the goal with the library's simplification facts, such as Nat.mul_one (n * 1 = n) and
Nat.add_zero (n + 0 = n).
It rewrites as often as a fact fits, wherever it fits. When the goal becomes true, such as a = a, simp closes it.
Step 10 of 22 · Concept
simp only uses only what you list
You can choose the facts:
| tactic | rewrites with |
|---|---|
simp [h] | the library's facts, and h too |
simp only [h, Nat.add_zero] | h and Nat.add_zero, nothing else |
simp only shows a reader exactly which facts the proof uses. A hypothesis such as h : a = b works like a library
fact: it replaces a by b.
Step 11 of 22 · Concept
simp at h simplifies a hypothesis
simp at h rewrites the hypothesis h instead of the goal. With h : a * 1 + 0 = b, it leaves h : a = b.
When no fact applies, simp refuses:
example (a b : Nat) (h : a = b) : a = b := by
simp at h
exact h
Main.lean:2:2: error: `simp` made no progress
Step 12 of 22 · Quiz
What is left after simp only?
The goal panel shows this. What does it show after simp only [Nat.add_zero]?
a b : Nat
h : a = b
⊢ a + 0 = b
Step 13 of 22 · Demo
See simp work
Press Run: Lean accepts both. In the first, simp [h] uses the library's facts and h. In the second,
simp only uses the two facts listed.
Step 14 of 22 · Try it
Try it: simplify a hypothesis
Prove from_h: simplify h with simp at h, then close the goal with it. Any proof Lean accepts passes, but practise simp at h here.
Put the cursor on simp at h: after it, the panel shows h : a = b, and the goal is a = b.
Step 15 of 22 · Concept
Each one builds a proof the kernel checks
A tactic runs in the elaborator and leaves a proof term. The kernel checks that term as it checks yours (lesson 2).
So omega may have a bug, and a false theorem still cannot pass.
#print axioms shows what such a proof rests on: nothing, or the standard axioms propext, Quot.sound and
Classical.choice (lesson 7). The check accepts those three.
Step 16 of 22 · Demo
Ask what they rest on
Press Run. Each tool proves one theorem; #print axioms lists what each proof rests on.
Step 17 of 22 · Concept
native_decide trusts compiled code
native_decide compiles the question into a program, runs it, and believes its answer. The kernel never checks that
answer.
So Lean records the answer as a new axiom, named after the theorem, and #print axioms shows it. The check accepts
only the three standard axioms, so it refuses native_decide.
Use decide: the kernel checks its work. A big case is slower, and a very big one may stop with this:
error: maximum recursion depth has been reached
Step 18 of 22 · Demo
See the extra axiom
Press Run. Lean accepts big, but #print axioms names an axiom no other theorem has.
Step 19 of 22 · Quiz
Why does the check refuse it?
Lean accepts big, and 2 ^ 20 really is 1048576. Why does the check refuse this proof?
theorem big : 2 ^ 20 = 1048576 := by native_decide
#print axioms bigStep 20 of 22 · Check yourself
Check yourself: the right tool for each
Prove t1, t2 and t3, one tactic each. For t2 and t3, only one of the three tools works on its own, with no facts listed. Any proof Lean accepts passes.
Given numbers; linear arithmetic from a hypothesis; a function's argument to simplify.
Step 21 of 22 · Challenge · optional
Challenge: every number under 5
Optional.
Prove below_five. omega refuses n * n; another tool of this lesson can check the statement.
n * n multiplies n by itself, not by a given number, so omega gives up:
error: omega could not prove the goal:
n < 5 leaves only five numbers: 0, 1, 2, 3 and 4. A proposition about every number under a bound is a yes-or-no fact.
Use the tool for yes-or-no facts on the goal as it is, before any intro. Then n is no free variable: it checks 0 to 4 in turn.
Step 22 of 22 · Recap
Recap
decidedecides facts about given values, and refuses a variable.omegaproves linear arithmetic onNatfrom the hypotheses, and fails ona * b.simprewrites with library facts;simp only [...]uses only those listed;simp at hrewritesh.- The kernel checks every proof they build; the check accepts only the three standard axioms.
Next: lesson 12 builds numbers from Nat.zero and Nat.succ, and defines functions by recursion.
Review
4 questions from this lesson, in a new order each time. Answer from memory: 3 of 4 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
example (a b : Nat) (h : a < b) : a + 1 ≤ b := by omega
example (n : Nat) : n / 2 ≤ n := by omega
example (a : Nat) (h : a ≠ 0) : a - 1 + 1 = a := by omega
example (a b : Nat) : a * b = b * a := by omega
theorem o1 (f : Nat → Nat) (h : ∀ n, f n ≤ n + 1) :
f 3 < 5 := by
sorry
theorem o2 (a b : Nat) (h1 : a ≤ b) (h2 : b ≤ a + 2) :
b - a ≤ 2 := by
sorry
theorem o3 (n : Nat) : 2 * n % 2 = 0 := by
sorry
example (a b : Nat) (h : a = b) : a * 1 + 0 = b := by
simp [h]
example (a b : Nat) (h : a = b) : a + 0 = b := by
simp only [h, Nat.add_zero]
theorem from_h (a b : Nat) (h : a * 1 + 0 = b) : a = b := by
sorry
theorem t_decide : 10 < 20 := by decide
theorem t_omega (a b : Nat) (h : a < b) : a + 1 ≤ b := by omega
theorem t_simp (a : Nat) : a * 1 + 0 = a := by simp
#print axioms t_decide
#print axioms t_omega
#print axioms t_simp
theorem big : 2 ^ 20 = 1048576 := by native_decide
#print axioms big
theorem t1 : 12 * 12 = 144 := by
sorry
theorem t2 (a b : Nat) (h : a + 2 = b) : a < b := by
sorry
theorem t3 (f : Nat → Nat) (a : Nat) :
f (a * 1 + 0) = f a := by
sorry
theorem below_five : ∀ n, n < 5 → n * n < 25 := by
sorry