Error Reference › Lean 4
Lean 4

Maximum recursion depth has been reached (Lean 4)

A Lean procedure (elaboration, simp, decide, instance search, macro expansion) nested deeper than maxRecDepth — 512 by default — and stopped.

What it looks like

Main.lean:2:2: error: maximum recursion depth has been reached
use `set_option maxRecDepth <num>` to increase limit
use `set_option diagnostics true` to get diagnostic information

What does "Maximum recursion depth has been reached (Lean 4)" mean?

Lean counts how deeply its own procedures nest and stops at maxRecDepth, 512 by default. It is a guard against stack overflow, not a timeout: raising it is safe when the work is legitimately deep, but if something loops, a higher limit only delays the error. The oldest Lean 4 releases printed the same advice on one line: (use `set_option maxRecDepth <num>` to increase limit).

What causes "Maximum recursion depth has been reached (Lean 4)"?

  • decide, rfl or #eval on a large statement or number, which forces deep unfolding.
  • A very long list, term or literal, or a deeply nested expression.
  • simp lemmas or instances that keep rewriting into each other.
  • A macro or notation that expands into itself.

How do I confirm the cause?

  1. Note which tactic or command the error points at — decide, simp and #eval are the usual suspects.
  2. Try set_option maxRecDepth 4000 in: if the error persists at any limit, something is looping.
  3. Add set_option diagnostics true in to see which definitions and instances were unfolded most.

How do I fix "Maximum recursion depth has been reached (Lean 4)"?

  • Raise the limit for one declaration: set_option maxRecDepth 2000 in before the theorem.
  • Use a proof that does not unfold everything: norm_num, simp or omega instead of decide on large numbers.
  • Look for a loop: switch to simp only [...] and add lemmas back one at a time.
  • Split a huge term into named intermediate definitions.

How do I stop it happening again?

  • Prove large concrete facts with proof-producing tactics (norm_num, simp, omega) rather than brute-force evaluation.
  • Avoid simp lemmas whose right-hand side can be rewritten back into the left-hand side.
Got a different error? Paste it into the Explain This Error tool → Identifies the family and the fix — runs locally, nothing uploaded.

Related errors