Why Lean insists
A definition becomes an equation that proofs may use. If Lean accepted def bad (n : Nat) : Nat := bad n + 1, the equation bad n = bad n + 1 would give 0 = 1 after subtracting. So it refuses:
def bad (n : Nat) : Nat := bad n + 1
Lean reports fail to show termination for bad, with two reasons: the parameter n is unchanged in the recursive calls, so structural recursion is impossible, and there is no decreasing argument for well-founded recursion.
Structural recursion: the default
When each recursive call is on a constructor’s argument, such as n inside n + 1, or the tail xs of x :: xs, the recursion is structural and Lean accepts it without comment:
def sumTo : Nat → Nat
| 0 => 0
| n + 1 => (n + 1) + sumTo n
Lean translates structural recursion into uses of the type’s recursor (see Inductive types and their recursors), so its equations hold by rfl on concrete inputs.
Well-founded recursion: a decreasing measure
Halving is not structural: n / 2 is not a constructor argument of n. Lean then searches for a measure, a natural number that decreases at every call, and tries to prove the decrease automatically. You can state both parts yourself:
def log2' (n : Nat) : Nat :=
if h : n < 2 then 0 else 1 + log2' (n / 2)
termination_by n
decreasing_by omega
termination_by n names the measure; decreasing_by proves the goal Lean generates, n / 2 < n under the hypothesis ¬n < 2 (the h : makes the condition available). Measures can be any expression with a well-founded order, such as xs.length, or a pair compared lexicographically. Lean finds the pair (m, n) by itself for Ackermann’s function:
def ack : Nat → Nat → Nat
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
#eval ack 2 3
It prints 9. Behind the scenes Lean proved that every call decreases the pair in lexicographic order.
partial: runs, but proves nothing
For a function whose termination is open, such as the Collatz iteration, partial def compiles it without a termination proof:
partial def collatz (n : Nat) : Nat :=
if n ≤ 1 then 0
else if n % 2 = 0 then 1 + collatz (n / 2)
else 1 + collatz (3 * n + 1)
#eval collatz 27
It prints 111, the number of steps from 27 to 1. Lean only requires the return type to be inhabited, and treats the function as opaque. Even the base case cannot be proved:
example : collatz 1 = 0 := rfl
example : collatz 1 = 0 := by
unfold collatz
The first fails with a type mismatch, the second with Tactic unfold failed to unfold collatz. You can run collatz, but you cannot prove anything about its values.
partial removes the specification
Making a definition partial to silence a termination error looks harmless, because the code still runs. It removes every equation the function had, so no theorem about it can be proved. If you need theorems, keep the def and supply a measure; partial is for code you only run.
Sources and related cards
The Lean reference manual on recursive definitions explains structural recursion, well-founded recursion and partial. Related cards: Inductive types and their recursors, induction and its goal states, Two places for a specification. In the course: Lecture 4.