Chapter 5 · Verified programs
Practice: choose the tactic
Step 1 of 14 · Concept
The goal tells you the tactic
In this lesson you practise choosing tactics, and no task names one. Read the goal first: its shape names the first tactic to try. A hypothesis's shape says how to use it.
When a hypothesis fits the goal, exact or apply uses it (lesson 5). A recursive function calls for
induction (lesson 13). After each tactic, read the goal panel again.
Stuck on one goal? A sorry there lets you check the rest, but the check refuses it.
Step 2 of 14 · Quiz
First tactic?
The goal panel shows this. Which first tactic fits?
P Q : Nat → Prop
⊢ ∀ (n : Nat), P n → Q n
Step 3 of 14 · Quiz
First tactic?
The goal panel shows this. Which first tactic fits?
p q r : Prop
hpr : p → r
hqr : q → r
h : p ∨ q
⊢ r
Step 4 of 14 · Quiz
First tactic?
The goal panel shows this. Which first tactic fits?
a b : Nat
h : a + b = 10
h2 : a = 4
⊢ b = 6
Step 5 of 14 · Concept
exact? finds a library fact
When the goal looks like a fact the library should have, exact? searches for it (lesson 9, Rewriting). For
⊢ n ≤ n + 3 it prints:
Try this:
[apply] exact Nat.le_add_right n 3
Replace exact? with that line, without the [apply] marker. The file then says which fact it used, and Lean
checks it. The check refuses exact? itself.
Step 6 of 14 · Try it
Try it: let Lean find the fact
Replace sorry with exact? and press Run. Then replace exact? with the line it suggests, without [apply].
Copy the line under Try this: from exact on, and keep it indented like the other tactics.
Step 7 of 14 · Exercise
Your turn: regroup
Prove regroup: the same three facts, grouped the other way.
exact ⟨h.1, h.2⟩ is refused: h.1 has type p, but the first part of the goal is p ∧ q. And h.2 is q ∧ r, not r.
Take h apart first, then its second part: two obtains, as in lesson 6, And, or. Then build the goal.
obtain ⟨hp, hqr⟩ := h
Step 8 of 14 · Exercise
Your turn: two steps up
h turns a proof of P n into a proof of P (n + 1), for every n. From P 0, prove P 2.
exact h 1 h0 is refused: h0 has type P 0, but h 1 needs a proof of P 1.
h 1 turns a proof of P 1 into a proof of P 2: a number, then a proof, as in lesson 8, For all, there exists. Where does a proof of P 1 come from?
exact h 1 (...)
Step 9 of 14 · Exercise
Your turn: copies of a number
rep n x is a list of n copies of x, and len is lesson 15's. Prove that rep n x has length n.
Induction on x gets stuck: rep takes apart its first number, n, never x.
Induction on n, as in lesson 13, Induction. In case succ, simp [rep, len, ih] leaves ⊢ 1 + k = k + 1.
induction n with
| zero => rfl
Step 10 of 14 · Exercise
Your turn: an even number plus two
n is even: it is 2 * m for some m. Prove that n + 2 is even too.
exists m leaves ⊢ n + 2 = 2 * m, which is false: with hm : n = 2 * m, n + 2 is 2 * m + 2.
Take h apart with obtain (lesson 8, For all, there exists). Which number, times 2, is 2 * m + 2?
obtain ⟨m, hm⟩ := h
Step 11 of 14 · Quiz
When you are stuck
No tactic seems to fit the goal. What should you do first?
Step 12 of 14 · Check yourself
Check yourself: three goals in one theorem
No shape this time.
Prove mixed: a proposition, a number, and a number that exists, joined by ∧.
Split the goal first, one part at a time. Each part then has a tactic of its own.
Step 13 of 14 · Challenge · optional
Challenge: every copy is x
Optional.
y ∈ xs (type \in; it has no ASCII form) says that y is in the list xs. Prove that every y in rep n x is x. The library's List.mem_cons
says y ∈ x :: xs ↔ y = x ∨ y ∈ xs.
Induction on n. In case zero, rep 0 x is [], and no y is in []: simp [rep] sees that.
In case succ, intro y hy, then simp [rep, List.mem_cons] at hy turns hy into y = x ∨ y ∈ rep k x.
Each side of that ∨ is a case. In the second, ih takes y and the proof that y ∈ rep k x.
Step 14 of 14 · Recap
Recap
- The goal's shape names the first tactic; a hypothesis's shape says how to use it.
exact?finds a library fact; paste its line without[apply].sorryis a step that lets you check the rest, never an answer.- A recursive function calls for induction on the argument it takes apart.
Next: lesson 21 verifies a whole function: a definition and the theorems about it, in one file.
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.
theorem times_swap (a b : Nat) : a * b = b * a := by
sorry
theorem regroup (p q r : Prop) (h : p ∧ (q ∧ r)) :
(p ∧ q) ∧ r := by
sorry
theorem two_up (P : Nat → Prop) (h : ∀ n, P n → P (n + 1))
(h0 : P 0) : P 2 := by
sorry
def len : List Nat → Nat
| [] => 0
| _ :: xs => 1 + len xs
def rep : Nat → Nat → List Nat
| 0, _ => []
| n + 1, x => x :: rep n x
theorem rep_len (n x : Nat) : len (rep n x) = n := by
sorry
theorem even_plus_two (n : Nat) (h : ∃ m, n = 2 * m) :
∃ m, n + 2 = 2 * m := by
sorry
theorem mixed (p : Prop) (hp : p) (n : Nat) (h : n + 1 = 4) :
p ∧ n = 3 ∧ ∃ m, m + n = 5 := by
sorry
def rep : Nat → Nat → List Nat
| 0, _ => []
| n + 1, x => x :: rep n x
theorem rep_all (n x : Nat) : ∀ y, y ∈ rep n x → y = x := by
sorry