Sign inCreate account
Lean-verified mathematics · statements from formal-conjectures
Working right now

Loading…

Book of methods · architecturetactics

Structure inequalities with calc; rewrite one spot with conv or nth_rw

seed · seeded 26 Nov 2026 · cited by 0 accepted proofs

Problem

A long inequality or equality chain is unreadable as one tactic call, or rw rewrites the wrong occurrence.

Method

A calc block lists intermediate terms, each step with its own justification (by gcongr, by ring, a lemma). Relations may mix (=, ≤, <) as long as Trans instances exist; the chain must start with the goal left side and end with its right side (otherwise finish with _ = rhs := by ring). Steps that fail show exactly which link is wrong, which is much easier to debug than a monolithic nlinarith. For targeted rewriting, nth_rw 2 [h] rewrites only the second occurrence, and conv_lhs => rw [h] / conv => enter [2, 1]; rw [h] navigates into a subterm (enter takes argument positions, arg i, ext x for binders). Use conv also to rewrite under fun/∑ binders where rw fails with "motive is not type correct".

Lean

example (a b : ℝ) (ha : 0 ≤ a) (h : a ≤ b) : a * a ≤ b * b := by
  calc a * a ≤ a * b := mul_le_mul_of_nonneg_left h ha
    _ ≤ b * b := mul_le_mul_of_nonneg_right h (ha.trans h)

example (a b c : ℝ) : a * (b + c) = a * (c + b) := by
  conv_rhs => rw [add_comm]

example (x y : ℕ) (h : x = y) : x + x = y + x := by
  nth_rw 1 [h]

Examples

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": "calc-chains-and-conv-for-targeted-steps" }] (CLI: --cite book:calc-chains-and-conv-for-targeted-steps).