Stage 07 · Step 31 of 50

Functions that call themselves.

Core Lean12 minutesLean 4.36.0

01The idea

A recipe can tell you to make a smaller version first. Recursive functions do that too.

Indy · Your guide for this idea
Indy pointing

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

Recursion: factorial 5 asks smaller copies of itself until the base caseThree columns. On the left, a staircase of calls going down: factorial 5, factorial 4, factorial 3, factorial 2, factorial 1, factorial 0. In the middle, what the definition says for each: factorial 5 equals 5 times factorial 4, and so on down to factorial 1 equals 1 times factorial 0. Factorial 0 is the base case and equals 1, so no more calls are made. On the right, the answers come back up: 1, then 1 times 1 is 1, 2 times 1 is 2, 3 times 2 is 6, 4 times 6 is 24, and finally 5 times 24 is 120.GOING DOWNWHAT THE DEFINITION SAYSANSWERS COMING BACK UPfactorial 5= 5 * factorial 45 * 24 = 120factorial 4= 4 * factorial 34 * 6 = 24factorial 3= 3 * factorial 23 * 2 = 6factorial 2= 2 * factorial 12 * 1 = 2factorial 1= 1 * factorial 01 * 1 = 1factorial 0= 1base case: no more calls1Every call uses a smaller input, so the staircase must reach 0 and stop.

Swipe sideways to see the whole picture.

To find 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.

E31.leanTry it live (opens official Lean editor)
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.

After line 13
⊢ factorial 5 = 120
After line 13: example : factorial 5 = 120 := by
⊢ factorial 5 = 120
After line 14: rfl
no goals

05Lean says

Captured from Lean 4.36.0
120
21
no goals

A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.

07Your turn

sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.

  1. Warm-up

    Use the supplied factorial definition to prove its value at four.

  2. A little stretch

    Use the supplied Fibonacci definition to prove its value at seven.

Exercises/E31.leanTry it live (opens official Lean editor)
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

08A quick check

1. Why does Lean accept these recursive definitions?

Read the answer and explanation

Recursive calls use smaller natural-number inputs. Lean verifies that these recursive calls are structurally decreasing.

2. What value does factorial assign to zero?

Read the answer and explanation

One. The base case is one. It makes the standard factorial recurrence work.

One more idea in your toolkit.

Take your time. Progress is yours to mark.