Chapter 5 · Verified programs
A verified function
Step 1 of 16 · Concept
A function and what it promises
This lesson ends the course with one file: a function, three promises about it, a helper, and four proofs.
The function is rev. It reverses a list of numbers, one equation per case, as in lesson 15:
def rev : List Nat → List Nat
| [] => []
| x :: xs => rev xs ++ [x]
When Lean accepts the proofs, rev is verified: it keeps its promises for every list.
Step 2 of 16 · Quiz
Predict the reversal
Type what this file prints.
def rev : List Nat → List Nat
| [] => []
| x :: xs => rev xs ++ [x]
#eval rev [1, 2, 3]
Step 3 of 16 · Demo
See it run
Press Run and compare with your answer.
Step 4 of 16 · Concept
Three promises
A run shows one list. A theorem speaks for every list. These three say what rev does:
| theorem | statement |
|---|---|
rev_append | rev (xs ++ ys) = rev ys ++ rev xs |
rev_rev | rev (rev xs) = xs |
len_rev | len (rev xs) = len xs |
Reversing a join reverses each part and swaps them. Reversing twice gives the list back. Reversing keeps the length.
len is lesson 15's. len_rev needs a helper, len_app, which lesson 16 proved for its own app. Here it is
stated for ++, the library's join, and proved the same way: four proofs in all.
Step 5 of 16 · Concept
rev_rev needs a lemma first
Induction on xs alone gets stuck in the cons case:
def rev : List Nat → List Nat
| [] => []
| x :: xs => rev xs ++ [x]
theorem rev_rev (xs : List Nat) : rev (rev xs) = xs := by
induction xs with
| nil => rfl
| cons x xs ih => simp [rev]
Main.lean:7:17: error: unsolved goals
case cons
x : Nat
xs : List Nat
ih : rev (rev xs) = xs
⊢ rev (rev xs ++ [x]) = x :: xs
The goal reverses a join, rev xs ++ [x], and ih does not fit it. rev_append does. A theorem proved to help
prove another is a lemma: prove it first.
Step 6 of 16 · Quiz
What does the stuck goal need?
The cons case of rev_rev stops here. What does it need?
case cons
x : Nat
xs : List Nat
ih : rev (rev xs) = xs
⊢ rev (rev xs ++ [x]) = x :: xs
Step 7 of 16 · Exercise
Your turn: rev of a join
The same steps as lesson 16: induction, unfold, use ih. simp knows the library's facts about ++, such as
[] ++ ys = ys, and that ++ groups either way.
Prove rev_append by induction. Any proof Lean accepts passes.
Which list does ++ take apart? Induction on ys gets stuck at ⊢ rev (xs ++ y :: ys) = rev ys ++ y :: rev xs: ++ takes apart its first list, xs. Lean also warns, since the goal had no place for ih:
warning: This simp argument is unused:
ih
In each case, simp with rev unfolds the definition. The cons case also needs the hypothesis for the tail.
induction xs with
| nil => simp [rev]
Step 8 of 16 · Exercise
Your turn: reversing twice
rev_append is proved for you. Prove rev_rev by induction, with rev_append as a simp fact, as lesson 16 used
simp [len_app]. Any proof Lean accepts passes.
Without rev_append, simp [rev, ih] leaves the goal of the step rev_rev needs a lemma first: ⊢ rev (rev xs ++ [x]) = x :: xs. Lean also warns, since the goal had no place for ih:
warning: This simp argument is unused:
ih
The nil case computes. In the cons case, simp needs the definition and ih, and one more fact: the theorem about rev of a join.
induction xs with
| nil => rfl
Step 9 of 16 · Exercise
Your turn: the length stays
len_app is proved for you, for ++. Prove len_rev by induction, with len_app as a simp fact. Any proof Lean accepts
passes.
Without len_app, simp cannot take apart the length of a join: the goal stays ⊢ len (rev xs ++ [x]) = 1 + len xs, and Lean warns:
warning: This simp argument is unused:
ih
After simp [rev, len, len_app, ih] the goal is ⊢ len xs + 1 = 1 + len xs: a sum in the other order. Lesson 11 has the tactic for that.
| cons x xs ih =>
simp [rev, len, len_app, ih]
Step 10 of 16 · Quiz
What does the check promise?
Lean accepts your proof of rev_rev. For which lists is rev (rev xs) = xs now proved?
theorem rev_rev (xs : List Nat) : rev (rev xs) = xs := by
-- your proof, with no sorry
Step 11 of 16 · Check yourself
Check yourself: the verified file
No shape this time.
Complete the four proofs. The last two lines print what rev_rev and len_rev rest on. While a proof they use
says sorry, its line shows sorryAx.
With this lesson's proofs, Run then prints:
[3, 2, 1]
'rev_rev' depends on axioms: [propext]
'len_rev' depends on axioms: [propext, Quot.sound]
Another correct proof may list other standard axioms, but never sorryAx.
Prove the theorems from the top down: each proof may use the theorems before it.
Step 12 of 16 · Concept
grind, the automation of a next course
Core Lean has more tactics. One of them, grind, closes many goals in one step.
It closes lesson 20's regroup and two_up, and each case of an induction once you have started it. Without the induction, it fails on
rev_append.
Like simp, it builds a proof the kernel checks: #print axioms lists only standard axioms.
The check in this course refuses grind. It would hide the steps this course taught: unfold, use the hypothesis,
finish. You know those steps now, so you know what grind does in your place.
Step 13 of 16 · Demo
See grind work
Press Run. grind closes the cons case. #print axioms shows what rev_rev rests on: three standard axioms,
and no sorryAx.
Step 14 of 16 · Challenge · optional
Challenge: your rev is the library's
Optional.
Prove rev_eq: your rev gives the same list as the library's xs.reverse (lesson 15). The library's
List.reverse_cons says how reverse takes apart a list: (a :: as).reverse = as.reverse ++ [a].
Induction on xs. In the nil case both sides compute to [].
In the cons case, rw [rev, List.reverse_cons] makes both sides joins. They differ only in rev xs and xs.reverse.
ih says rev xs = xs.reverse, and rw takes several facts in one list, in order.
Step 15 of 16 · Concept
Where to go next
The course ends here. Three ways to go on:
- The Playground keeps a
Main.leanof your own. No task, no check: your own theorems. - The Lean project's books, Theorem Proving in Lean 4 and Functional Programming in Lean, teach the rest of the language. Both are listed at lean-lang.org.
- Mathlib, the library of mathematics written in Lean, is not part of this site's Lean. Install Lean, or use Lean's official online editor, to use it.
Step 16 of 16 · Recap
Recap
- A verified function is a function plus theorems that pin it down.
- A stuck goal asks for a lemma first. Your own theorems are facts for
simp. #print axiomsshows what a theorem rests on: nosorryAx, nothing unproved.- You can now state what a small function on numbers or lists promises, and prove it. Beyond this course:
grind, which you saw, and the mathematics in Mathlib.
Next: the Playground, with a Main.lean of your own.
Review
3 questions from this lesson, in a new order each time. Answer from memory: 2 of 3 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
def rev : List Nat → List Nat
| [] => []
| x :: xs => rev xs ++ [x]
#eval rev [1, 2, 3]
def rev : List Nat → List Nat
| [] => []
| x :: xs => rev xs ++ [x]
theorem rev_append (xs ys : List Nat) :
rev (xs ++ ys) = rev ys ++ rev xs := by
sorry
def rev : List Nat → List Nat
| [] => []
| x :: xs => rev xs ++ [x]
theorem rev_append (xs ys : List Nat) :
rev (xs ++ ys) = rev ys ++ rev xs := by
induction xs with
| nil => simp [rev]
| cons x xs ih => simp [rev, ih]
theorem rev_rev (xs : List Nat) : rev (rev xs) = xs := by
sorry
def len : List Nat → Nat
| [] => 0
| _ :: xs => 1 + len xs
def rev : List Nat → List Nat
| [] => []
| x :: xs => rev xs ++ [x]
theorem len_app (xs ys : List Nat) :
len (xs ++ ys) = len xs + len ys := by
induction xs with
| nil => simp [len]
| cons x xs ih =>
simp [len, ih]
omega
theorem len_rev (xs : List Nat) : len (rev xs) = len xs := by
sorry
def len : List Nat → Nat
| [] => 0
| _ :: xs => 1 + len xs
def rev : List Nat → List Nat
| [] => []
| x :: xs => rev xs ++ [x]
#eval rev [1, 2, 3]
theorem len_app (xs ys : List Nat) :
len (xs ++ ys) = len xs + len ys := by
sorry
theorem rev_append (xs ys : List Nat) :
rev (xs ++ ys) = rev ys ++ rev xs := by
sorry
theorem rev_rev (xs : List Nat) : rev (rev xs) = xs := by
sorry
theorem len_rev (xs : List Nat) : len (rev xs) = len xs := by
sorry
#print axioms rev_rev
#print axioms len_rev
def rev : List Nat → List Nat
| [] => []
| x :: xs => rev xs ++ [x]
theorem rev_append (xs ys : List Nat) :
rev (xs ++ ys) = rev ys ++ rev xs := by
induction xs with
| nil => simp [rev]
| cons x xs ih => simp [rev, ih]
theorem rev_rev (xs : List Nat) : rev (rev xs) = xs := by
induction xs with
| nil => rfl
| cons x xs ih => grind [rev, rev_append]
#print axioms rev_rev
def rev : List Nat → List Nat
| [] => []
| x :: xs => rev xs ++ [x]
theorem rev_eq (xs : List Nat) : rev xs = xs.reverse := by
sorry