Factorials, binomial coefficients, and Fibonacci numbers in Lean
A hands-on introduction to computing, conjecturing, and proving in Lean
This tutorial assumes no prior experience with Lean. It alternates short explanations with exercises, and it uses only core Lean, without Mathlib.
Everything runs in the browser at live.lean-lang.org. Each link opens a new tab. Close the previous tab when you open the next exercise, to avoid overloading the server. Each exercise file starts with the solutions to the earlier ones. The editor keeps your code in the address bar, so to save your work, bookmark the page or copy the address.
Session 1: Define, compute, conjecture
Recursion
A recursive definition gives the value at in terms of the value at The base case is 0.
def fact : Nat → Nat
| 0 => 1
| n + 1 => (n + 1) * fact n#eval fact 5computes a value.example : fact 5 = 120 := by rflstates a fact, and Lean checks it.
Exercise 1: Factorial and sums
Prove fact 6 = 720 two ways, complete sumTo, and add up odd numbers.
Binomial coefficients
is the number of ways to choose items from a collection of
Pascal's rule. To choose items from consider the first one. Either we choose it, and then we choose more from the other or we don't, and we choose all from the other
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1def choose : Nat → Nat → Nat
| _, 0 => 1
| 0, _ + 1 => 0
| n + 1, k + 1 => choose n k + choose n (k + 1)Fibonacci numbers have two base cases.
Exercise 2: Experiments
Print Pascal's triangle, compute, and write down conjectures.
Conjectures
Do Exercise 2 before reading this list.
- Row sums:
- and when
- The shallow diagonals of Pascal's triangle add up to Fibonacci numbers.
Proof by induction
To prove for every :
- Base case. Prove
- Induction step. Assume (the induction hypothesis) and prove
Example:
- Base: the empty sum is
- Step:
theorem sum_odds (n : Nat) : sumTo (fun i => 2 * i + 1) n = n * n := by
induction n with
| zero => rfl
| succ n ih => grind [sumTo]We set up the induction. The call to grind unfolds sumTo, uses ih, and does the algebra.
Exercise 3: First proofs
Gauss's formula, the sum of Fibonacci numbers, and 0 < fact n.
Session 2: Prove
Proofs about choose
In the next demo, cases n is similar to induction n, but it splits the proof into cases n = 0 and n = m + 1 without an induction hypothesis. t1 <;> t2 means "do t1, then t2 in every resulting case."
We also need a strengthened version of induction. Suppose the goal is to prove when Pascal's rule needs the hypothesis for both and
induction n: the hypothesis is for this one Not enough.induction n generalizing k: the hypothesis is for every
Exercise 4: Binomial coefficients
Prove choose n 1 = n and choose n n = 1.
A better implementation of the Fibonacci numbers
fib computes the same values over and over:
fib 5
├── fib 3
│ ├── fib 1
│ └── fib 2
│ ├── fib 0
│ └── fib 1
└── fib 4
├── fib 2
│ ├── fib 0
│ └── fib 1
└── fib 3
├── fib 1
└── fib 2
├── fib 0
└── fib 1A faster way: carry two consecutive values along, replacing by at each step.
def fibLoop : Nat → Nat → Nat → Nat
| 0, a, _ => a
| n + 1, a, b => fibLoop n b (a + b)
def fastFib (n : Nat) : Nat := fibLoop n 0 1| steps left | a | b |
|---|---|---|
| 5 | 0 | 1 |
| 4 | 1 | 1 |
| 3 | 1 | 2 |
| 2 | 2 | 3 |
| 1 | 3 | 5 |
| 0 | 5 | 8 |
So fastFib 5 = 5. To prove fastFib n = fib n for all we prove something stronger: if the loop starts from and then after steps it returns
theorem fibLoop_eq (n m : Nat) :
fibLoop n (fib m) (fib (m + 1)) = fib (n + m)Exercise 5: Fast Fibonacci is correct
Prove fibLoop_eq, then deduce fastFib n = fib n.
The big theorems
Row sums. By Pascal's rule, each entry of row contributes to exactly two entries of row So each row sum is twice the one before, and
The committee identity. Choose a committee of from people, then its chair. Or choose the chair first, then the other members from the remaining
The factorial formula, by induction on :
We state it with multiplication because division on Nat rounds down.
Exercise 6: Choose a challenge
- Prove the committee identity.
- Finish Exercises 4 and 5.
- Pascal's diagonals, on paper.
- Your own conjecture.
