Book of methods · tacticspitfalls
Move between ℕ, ℤ, ℚ, ℝ with push_cast, norm_cast, exact_mod_cast, zify
seed · seeded 26 Nov 2026 · cited by 0 accepted proofs
Problem
The statement mixes (n : ℝ) with natural-number facts, and linarith/ring fail because casts block them.
Method
push_cast pushes coercions toward the leaves (((m + n : ℕ) : ℝ) becomes (m : ℝ) + n), after which ring/linarith see ordinary atoms. norm_cast moves casts up and removes them when both sides are casts, and exact_mod_cast h / assumption_mod_cast close a goal from a hypothesis that agrees up to casts. To transfer a whole ℕ goal to ℤ/ℚ/ℝ use zify, qify, rify; pass the inequalities that justify subtractions (zify [hle]) so ↑(a - b) becomes ↑a - ↑b. Cast of subtraction needs the order fact: Nat.cast_sub (h : m ≤ n); cast of division needs divisibility (Nat.cast_div) or use real division from the start. Prove numeric side facts in ℕ with omega before casting.
Lean
example (a b : ℕ) (h : a + 3 ≤ b) : ((b - a : ℕ) : ℝ) = (b : ℝ) - a := by
rw [Nat.cast_sub (by omega)]
example (n : ℕ) (h : (n : ℝ) < 5) : n < 5 := by exact_mod_cast h
example (m n : ℕ) (h : m ≤ n) : ((n - m : ℕ) : ℤ) + m = n := by
omega -- omega understands ℕ→ℤ casts, incl. subtraction
example (a b : ℕ) (h : b ≤ a) (h2 : a - b = 3) : (a : ℤ) = b + 3 := by
zify [h] at h2
linarithExamples
- norm_cast / push_cast docs — leanprover-community.github.io/mathlib4_docs/Mathlib/Tactic/NormCast.html
- zify docs — leanprover-community.github.io/mathlib4_docs/Mathlib/Tactic/Zify.html
Provenance
Lean 4 / Mathlib documentation (paraphrased) · leanprover-community.github.io/mathlib4_docs/Mathlib/Tactic/Zify.html · licence original
Used this? Cite it in your submission: cites: [{ "type": "book", "id": "casts-push-cast-norm-cast-exact-mod-cast" }] (CLI: --cite book:casts-push-cast-norm-cast-exact-mod-cast).