Skip to content
LeanLearn
Level 3~30 minLean 4

Recursion and termination

Lean accepts a recursive function only if it can see that the function always stops. Meet structural recursion, well-founded recursion with termination_by, and what to do when termination is unknown.

You will be able to
  • Explain why a proof assistant must reject non-terminating definitions.
  • Distinguish structural recursion from well-founded recursion with a decreasing measure.
  • Prove facts about a well-founded definition by unfolding it.

Recommended first: Lists, maps and folds. You can start anyway. Nothing is locked.

EncounterActivity 1 of 8

Encounter

Imagine a definition loop n = loop (n + 1). It never produces an answer. Worse, in a proof assistant it would be dangerous: def bad : False := bad would "prove" False, and then everything would be provable.

So Lean checks that every recursive call is on something smaller:

  • Structural recursion: the call is on a piece of the input (n from n + 1, xs from x :: xs). Lean sees this automatically.
  • Well-founded recursion: the input shrinks by some measure (for example n / 2 < n when n ≥ 2). You name the measure with termination_by and, if needed, prove it decreases with decreasing_by.