Chapter 5 · Verified programs
Specifications
Step 1 of 21 · Concept
A definition can choose
In this lesson you write a function, state what it must do as theorems, and prove that it does.
if c then a else b gives a when c holds, and b otherwise:
def maxN (a b : Nat) : Nat := if a ≤ b then b else a
Lean must be able to decide c, true or false. a ≤ b is Decidable (lesson 18), so this if works.
Step 2 of 21 · Quiz
Predict the missing value
The first line of the output is shown. Type the missing line.
def maxN (a b : Nat) : Nat := if a ≤ b then b else a
#eval maxN 3 7
#eval maxN 7 3
Step 3 of 21 · Demo
See maxN run
Press Run and compare with your answer.
Step 4 of 21 · Concept
split proves a fact about an if, one branch at a time
After rw [maxN] the goal holds the if, and omega cannot see inside it:
def maxN (a b : Nat) : Nat := if a ≤ b then b else a
theorem le_maxN (a b : Nat) : a ≤ maxN a b := by
rw [maxN]
omega
Main.lean:4:2: error: omega could not prove the goal:
a possible counterexample may satisfy the constraints
d ≥ 0
c ≥ 0
c - d ≥ 1
where
c := ↑a
d := ↑(if a ≤ b then b else a)
omega treats the whole if as an unknown number, d. split leaves one goal per branch instead.
Each branch gets the condition, or its negation, as a hypothesis named h✝. Lean made that name: you cannot type
it, but omega uses it.
Step 5 of 21 · Trace
Watch both branches
le_maxN says maxN a b is at least a. Press Next: each frame shows the goal as the panel will show it when you run this proof.
theorem le_maxN (a b : Nat) : a ≤ maxN a b := by
rw [maxN]
split
· omega
· omega- Line 2 is next. The goal names
maxN a b, whichomegacannot unfold. rw [maxN]put theifinto the goal. Line 3 is next.splitleft two goals:case isTruewithh✝ : a ≤ b, thencase isFalse. Line 4 is next.- The first
omegacloseda ≤ b, usingh✝. Theelsebranch,a ≤ a, is next. - The second
omegacloseda ≤ a: No goals, both branches are proved.
Step 6 of 21 · Quiz
Predict the second goal
The goal panel shows this. What is the second goal after split? Every goal keeps a b : Nat, so the options leave it
out.
a b : Nat
⊢ a ≤ if a ≤ b then b else a
Step 7 of 21 · Try it
Try it: the branch that was left
The proof of le_maxN stops after its first branch. Lean says:
Main.lean:2:46: error: unsolved goals
case isFalse
a b : Nat
h✝ : ¬a ≤ b
⊢ a ≤ a
Press Run and read the message. Add a bullet that closes the second branch. Any proof Lean accepts passes.
The goal left is case isFalse, ⊢ a ≤ a. A second · line, at the same indentation as the first, closes it.
Step 8 of 21 · Concept
by_cases with simp picks the branch
Lesson 7's by_cases is another way. by_cases h : a ≤ b leaves case pos with h : a ≤ b, and case neg with
h : ¬a ≤ b. This time you chose the name h.
In each case, simp [maxN, h] unfolds maxN and uses h to pick the branch of the if:
by_cases h : a ≤ b
· simp [maxN, h]
· simp [maxN, h]
When simp leaves arithmetic, omega finishes it with h.
Step 9 of 21 · Demo
See both cases
Press Run: Lean accepts it. Put the cursor on by_cases and read the two goals, with h in each.
Step 10 of 21 · Exercise
Your turn: b is at most the max
Prove le_maxN_right, with split or with by_cases. Any proof Lean accepts passes.
omega right after rw [maxN] fails: the if is in the way. Take it apart first.
On the by_cases route, the second branch's simp [maxN, h] leaves ⊢ b ≤ a. omega closes it, using h : ¬a ≤ b.
rw [maxN]
split
Step 11 of 21 · Concept
A Bool function and its facts
A function can answer yes or no with a Bool. == compares two values and gives true or false (lesson 18):
def isEven (n : Nat) : Bool := n % 2 == 0
A fact about it is an equation, isEven 4 = true. For a given number, decide proves it.
For every n, isEven n = true says "n is even". simp [isEven] turns isEven (n + n) = true into
(n + n) % 2 = 0: arithmetic, for omega.
Step 12 of 21 · Exercise
Your turn: even and odd
Prove the three theorems: 10 is even, 7 is not, and n + n is even for every n.
decide on even_double fails with Expected type must not contain free variables: the goal has a variable, n.
decide proves the facts about given numbers. For even_double, simp [isEven] leaves ⊢ (n + n) % 2 = 0.
theorem even_double (n : Nat) : isEven (n + n) = true := by
simp [isEven]
Step 13 of 21 · Concept
A specification pins a function down
A specification is a list of theorems that pin a function down. You have seen three proved about maxN:
| theorem | says |
|---|---|
a ≤ maxN a b | at least a |
b ≤ maxN a b | at least b |
maxN a b = a ∨ maxN a b = b | one of the two |
A function that meets all three gives the larger of a and b, so they say what maxN must do.
Step 14 of 21 · Quiz
Why the third theorem?
fun a b => a + b meets the first two theorems: a ≤ a + b and b ≤ a + b. What does the specification say about
it?
a ≤ f a b
b ≤ f a b
f a b = a ∨ f a b = b
Step 15 of 21 · Concept
A wrong function fails its specification
Here the branches are swapped:
def maxBad (a b : Nat) : Nat := if a ≤ b then a else b
No proof of a ≤ maxBad a b exists: it is false for some numbers. Lean can check a case you choose.
#eval maxBad 7 3 gives 3, and 7 ≤ 3 is false. Such a case is a counterexample: one case that breaks a
theorem.
decide on 7 ≤ maxBad 7 3 is refused: Lean says the proposition is false.
Step 16 of 21 · Demo
See the counterexample
Lean refuses line 3 with this message:
Main.lean:3:31: error: Tactic `decide` proved that the proposition
7 ≤ maxBad 7 3
is false
Press Run. #eval prints 3, and decide refuses line 3.
Step 17 of 21 · Quiz
Which value shows the bug?
Which line shows that maxBad breaks a ≤ maxBad a b?
def maxBad (a b : Nat) : Nat := if a ≤ b then a else b
Step 18 of 21 · Exercise
Your turn: specify the minimum
- Write
minN: the smaller ofaandb. - Prove its specification, all three theorems: the third rules out a wrong
minN, as formaxN.
An if, as in maxN, writes minN. Then omega right after rw [minN] fails: it cannot see behind the if.
The first two proofs have the shape of le_maxN: unfold, split, a bullet per branch. The third, of maxN_cases.
def minN (a b : Nat) : Nat := if a ≤ b then a else b
Step 19 of 21 · Check yourself
Check yourself: a clamp
No shape this time.
clamp lo hi x keeps x between lo and hi: for x < lo it gives lo, for hi < x it gives hi. With
h : lo ≤ hi, prove both theorems. After a second split, the first condition shows as h✝¹.
One if at a time: only the second branch of the first if holds another if.
Step 20 of 21 · Challenge · optional
Challenge: your max is the library's
Optional.
Prove that maxN is the library's max. The library's Nat.max_def says how max is defined:
max n m = if n ≤ m then m else n.
Nat.max_def is a fact you can rewrite with, like Nat.add_comm in lesson 9.
Turn both sides into an if: one fact unfolds maxN, the other rewrites max.
rw closes the goal itself once its two sides are the same (lesson 9).
Step 21 of 21 · Recap
Recap
if c then a else bchooses;splitproves a fact about it, one goal per branch, withh✝.by_cases h : a ≤ bandsimp [maxN, h]pick the branch with a name you chose.- A
Boolfunction's facts are equations:isEven n = true. - A specification is a list of theorems that pin a function down.
#evalfinds a counterexample for a wrong one.
Next: lesson 20 practises choosing the tactic from the goal.
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.
def maxN (a b : Nat) : Nat := if a ≤ b then b else a
#eval maxN 3 7
#eval maxN 7 3
def maxN (a b : Nat) : Nat := if a ≤ b then b else a
theorem le_maxN (a b : Nat) : a ≤ maxN a b := by
rw [maxN]
split
· omega
def maxN (a b : Nat) : Nat := if a ≤ b then b else a
theorem maxN_cases (a b : Nat) :
maxN a b = a ∨ maxN a b = b := by
by_cases h : a ≤ b
· simp [maxN, h]
· simp [maxN, h]
def maxN (a b : Nat) : Nat := if a ≤ b then b else a
theorem le_maxN_right (a b : Nat) : b ≤ maxN a b := by
sorry
def isEven (n : Nat) : Bool := n % 2 == 0
theorem even_ten : isEven 10 = true := by
sorry
theorem odd_seven : isEven 7 = false := by
sorry
theorem even_double (n : Nat) : isEven (n + n) = true := by
sorry
def maxBad (a b : Nat) : Nat := if a ≤ b then a else b
#eval maxBad 7 3
example : 7 ≤ maxBad 7 3 := by decide
-- the smaller of a and b
def minN (a b : Nat) : Nat := sorry
theorem minN_le_left (a b : Nat) : minN a b ≤ a := by
sorry
theorem minN_le_right (a b : Nat) : minN a b ≤ b := by
sorry
theorem minN_cases (a b : Nat) :
minN a b = a ∨ minN a b = b := by
sorry
def clamp (lo hi x : Nat) : Nat :=
if x < lo then lo else if hi < x then hi else x
theorem clamp_ge (lo hi x : Nat) (h : lo ≤ hi) :
lo ≤ clamp lo hi x := by
sorry
theorem clamp_le (lo hi x : Nat) (h : lo ≤ hi) :
clamp lo hi x ≤ hi := by
sorry
def maxN (a b : Nat) : Nat := if a ≤ b then b else a
theorem maxN_eq_max (a b : Nat) : maxN a b = max a b := by
sorry