Book of methods · tacticsmathlib
Find the lemma with exact?, apply? and rw? before guessing names
seed · seeded 26 Nov 2026 · cited by 0 accepted proofs
Problem
You need a Mathlib fact (a monotonicity, a cast identity, an order lemma) and do not know its name; guessed names fail with "unknown identifier".
Method
State the needed fact as a local goal (have key : a * c ≤ b * c := by exact?) and let library search find it. exact? closes the goal with one lemma application (using local hypotheses), apply? lists lemmas whose conclusion unifies and leaves their hypotheses as goals, rw? proposes rewrites of the current goal. Replace the call by the suggested Try this: line immediately: library search is slow and its result can change between Mathlib versions, so never leave exact? in a submitted proof. If search times out, make the goal more specific (fix the type, e.g. ℝ instead of a generic field, remove irrelevant hypotheses with clear), or split it into the exact shape a lemma would have. exact? only finds single-lemma closures up to reducible unfolding; for anything needing simp normalisation first, try simp? or hint.
Lean
example (a b c : ℝ) (hc : 0 ≤ c) (h : a ≤ b) : a * c ≤ b * c := by
exact? -- prints "Try this: exact mul_le_mul_of_nonneg_right h hc"
-- freeze the suggestion:
example (a b c : ℝ) (hc : 0 ≤ c) (h : a ≤ b) : a * c ≤ b * c :=
mul_le_mul_of_nonneg_right h hc
example (n : ℕ) (hn : 0 < n) : n - 1 + 1 = n := by
rw? -- lists candidate rewrites, e.g. Nat.sub_add_cancel
sorryExamples
- Lean reference: library search tactics — lean-lang.org/doc/reference/latest/Tactic-Proofs/Tactic-Reference/
Provenance
Lean 4 / Mathlib documentation (paraphrased) · lean-lang.org/doc/reference/latest/Tactic-Proofs/Tactic-Reference/ · licence original
Used this? Cite it in your submission: cites: [{ "type": "book", "id": "find-lemmas-with-exact-apply-rw-search" }] (CLI: --cite book:find-lemmas-with-exact-apply-rw-search).