Stage 07 · Step 31 of 50
Functions that call themselves.
01The idea
A recipe can tell you to make a smaller version first. Recursive functions do that too.
A base case ends the chain of smaller calls.
02In maths
Factorial has value one at zero. The Fibonacci sequence starts with zero and one.
Picture it
Swipe sideways to see the whole picture.
factorial 5, the definition asks for factorial 4, which asks for factorial 3, and so on. Each call is on a smaller input, so the chain must land on the base case factorial 0 = 1, and then the answers multiply back up to 120. rfl makes Lean run exactly this computation to check factorial 5 = 120.03In Lean
This code comes directly from the file checked by Lean.
def factorial : Nat → Nat
| 0 => 1
| n + 1 => (n + 1) * factorial n
def fibonacci : Nat → Nat
| 0 => 0
| 1 => 1
| n + 2 => fibonacci n + fibonacci (n + 1)
#eval factorial 5
#eval fibonacci 8
example : factorial 5 = 120 := by
rfl
A recursive function is defined using calls to itself on smaller inputs. Termination means the computation eventually reaches a result.
Lean checks a decreasing pattern for these calls. Both functions have explicit base cases.
The Fibonacci pattern with two successors leaves two strictly smaller arguments for the recursive calls.
The final claim checks a concrete result of recursion. Lean follows the definition down to zero and verifies the product.
04Step by step
Choose a line to see Lean’s recorded goal state after that line.
⊢ factorial 5 = 120
After line 13: example : factorial 5 = 120 := by
⊢ factorial 5 = 120
After line 14: rfl
no goals
05Lean says
120 21 no goals
A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.
06A common slip
Lean is telling us something useful here.
def badFactorial : Nat → Nat
| n + 1 => (n + 1) * badFactorial n
Missing cases: Nat.zero
There is no result for the zero constructor.
The fix: Add the base case for zero.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Use the supplied factorial definition to prove its value at four.
Show a hint
Zero has factorial one.
One more hint
At a successor, multiply that number by the previous factorial.
- A little stretch
Use the supplied Fibonacci definition to prove its value at seven.
Show a hint
Give separate results at zero and one.
One more hint
At later inputs, add the two preceding Fibonacci values.
def fact : Nat → Nat
| 0 => 1
| n + 1 => (n + 1) * fact n
example : fact 4 = 24 := by
sorry
def fib : Nat → Nat
| 0 => 0
| 1 => 1
| n + 2 => fib n + fib (n + 1)
example : fib 7 = 13 := by
sorry
Compare with the checked solutions
There are often several valid proofs. These solutions were compiled with the same pinned Lean version.
def fact : Nat → Nat
| 0 => 1
| n + 1 => (n + 1) * fact n
example : fact 4 = 24 := by
rfl
def fib : Nat → Nat
| 0 => 0
| 1 => 1
| n + 2 => fib n + fib (n + 1)
example : fib 7 = 13 := by
rfl
no goals
08A quick check
09New words & tools
Take your time. Progress is yours to mark.