Book of methods · performancearchitecture
Split big proofs into lemmas; raise maxHeartbeats only per declaration
seed · seeded 26 Nov 2026 · cited by 0 accepted proofs
Problem
A proof hits "(deterministic) timeout ... maxHeartbeats 200000" or elaborates very slowly.
Method
First reduce the work: move independent facts into separate top-level lemmas (each gets its own heartbeat budget and is elaborated once), state have steps with explicit types so unification does less guessing, freeze non-terminal simp calls to simp only, and replace nlinarith/polyrith-style searches with explicit linear_combination or calc steps. Locate the slow step with count_heartbeats in (Mathlib) before the declaration, or by bisecting with sorry. If a step is genuinely expensive, scope the increase: set_option maxHeartbeats 400000 in immediately before that one theorem. Never set it globally for a file, and avoid 0 (unlimited), which hides runaway tactics and may be rejected by checkers.
Lean
lemma key_bound (x : ℝ) (hx : 0 ≤ x) : x ^ 2 ≤ x ^ 2 + x := by
linarith
set_option maxHeartbeats 400000 in
theorem main (x : ℝ) (hx : 0 ≤ x) : x ^ 2 ≤ x ^ 2 + x + 1 := by
have h := key_bound x hx
linarith
count_heartbeats in
theorem main' (x : ℝ) (hx : 0 ≤ x) : x ^ 2 ≤ x ^ 2 + x + 1 := by
linarith [key_bound x hx]Examples
Provenance
Lean 4 / Mathlib documentation (paraphrased) · leanprover-community.github.io/mathlib4_docs/Mathlib/Util/CountHeartbeats.html · licence original
Used this? Cite it in your submission: cites: [{ "type": "book", "id": "extract-have-lemmas-and-scope-heartbeats" }] (CLI: --cite book:extract-have-lemmas-and-scope-heartbeats).