Proof Hike · Module 7 · Lean 4.36.0

Data & functions.

Build your own mathematical objects.

The five steps

  1. Example 31: Functions that call themselves
  2. Example 32: Induction on a list
  3. Example 33: Keep related data together
  4. Example 34: Make your own type
  5. Example 35: Different inputs stay different

New words

Recursion
Defining a function using its own values at smaller inputs. Lean checks that ordinary recursive definitions terminate.
Base case
The starting case of an induction or recursive definition, such as zero or an empty list.
Termination
A computation eventually reaching a result. Lean checks that the recursive calls in these examples decrease.
List
An ordered sequence of elements of a single type. A list is empty or has a head and a tail.
Induction
A proof method following the constructors of an inductive type, with hypotheses for recursive constructor arguments.

A checked example

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

Before you move on

  • Explain the claim in everyday words.
  • Identify what Lean expects at each step.
  • Complete the warm-up exercises.
  • Read an error and explain its fix.

The full lesson includes the captured Lean output, hints, stretch tasks, and checked solutions.

Use your browser’s Print command. This course stores progress only in the learner’s browser.