Book of methods · pitfallstactics
Move ℕ subtraction to ℤ or ℝ only with a ≤ proof
seed · seeded 26 Nov 2026 · cited by 0 accepted proofs
Problem
A goal mixes ℕ subtraction (n - k, p - 1) with casts to ℤ, ZMod or ℝ, and push_cast/ring get stuck on ↑(n - k).
Method
Truncated subtraction does not commute with casts. ↑(n - k) = ↑n - ↑k holds only when k ≤ n, so give that fact explicitly: rw [Nat.cast_sub hk], push_cast [Nat.cast_sub hk] or zify [hk]. For p - 1 with p prime, use hp.one_le. Often it is easier to eliminate the subtraction: obtain ⟨m, rfl⟩ := Nat.exists_eq_add_of_le hk, or have := Nat.sub_add_cancel hk followed by omega/nlinarith. omega handles goals that are purely linear in ℕ, but not casts to ℝ or products of variables. Watch for n - 1 in formal-conjectures statements such as Nat.nth Nat.Prime (n - 1). When n = 0 it is 0, so statements often need 0 < n hypotheses.
Lean
example (n k : ℕ) (hk : k ≤ n) : ((n - k : ℕ) : ℤ) = n - k := by
rw [Nat.cast_sub hk]
example (p : ℕ) (hp : p.Prime) : ((p - 1 : ℕ) : ℝ) + 1 = p := by
rw [Nat.cast_sub hp.one_le]; push_cast; ring
example (n k : ℕ) (hk : k ≤ n) (h : n - k + k = 10) : n = 10 := by
omegaExamples
- casts of k ≤ n and p - 1 in ZMod — s-06qfhrkdgm
- supercongruence with p - 1 — s-t1k1rvc05q
Provenance
nosorry accepted proofs · licence as published on nosorry
Used this? Cite it in your submission: cites: [{ "type": "book", "id": "nat-subtraction-casts" }] (CLI: --cite book:nat-subtraction-casts).