Skip to content

Commit e695749

Browse files
authored
chore: rename Nat.div_eq to Nat.div_eq_ite (#14462)
This PR renames `Nat.div_eq` to `Nat.div_eq_ite` and `Nat.mod_eq` to `Nat.mod_eq_ite`. This will free up `Nat.div_eq` for a lemma analogous to `Nat.add_eq`.
1 parent 3cdf38c commit e695749

8 files changed

Lines changed: 30 additions & 22 deletions

File tree

src/Init/Data/Fin/Lemmas.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ theorem sub_ofNat [NeZero n] (x : Fin n) (y : Nat) :
11221122

11231123
private theorem _root_.Nat.mod_eq_sub_of_lt_two_mul {x n} (h₁ : n ≤ x) (h₂ : x < 2 * n) :
11241124
x % n = x - n := by
1125-
rw [Nat.mod_eq, if_pos (by omega), Nat.mod_eq_of_lt (by omega)]
1125+
rw [Nat.mod_eq_ite, if_pos (by omega), Nat.mod_eq_of_lt (by omega)]
11261126

11271127
theorem coe_sub_iff_le {a b : Fin n} : (↑(a - b) : Nat) = a - b ↔ b ≤ a := by
11281128
rw [sub_def, le_def]

src/Init/Data/Nat/Bitwise/Lemmas.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ private theorem succ_mod_two : succ x % 2 = 1 - x % 2 := by
261261
trivial
262262
| succ x hyp =>
263263
have p : 2 ≤ x + 2 := Nat.le_add_left _ _
264-
simp [Nat.mod_eq (x+2) 2, p, hyp]
264+
simp [Nat.mod_eq_ite (x+2) 2, p, hyp]
265265
cases Nat.mod_two_eq_zero_or_one x with | _ p => simp [p]
266266

267267
private theorem testBit_succ_zero : testBit (x + 1) 0 = !(testBit x 0) := by
@@ -302,7 +302,7 @@ theorem testBit_two_pow_add_gt {i j : Nat} (j_lt_i : j < i) (x : Nat) :
302302
testBit (x % 2^j) i = (decide (i < j) && testBit x i) := by
303303
induction x using Nat.strongRecOn generalizing j i with
304304
| ind x hyp =>
305-
rw [mod_eq]
305+
rw [mod_eq_ite]
306306
rcases Nat.lt_or_ge x (2^j) with x_lt_j | x_ge_j
307307
· have not_j_le_x := Nat.not_le_of_gt x_lt_j
308308
simp [not_j_le_x]

src/Init/Data/Nat/Div/Basic.lean

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private theorem div.go.fuel_congr (x y fuel1 fuel2 : Nat) (hy : 0 < y) (h1 : x <
3636
next => rfl
3737
termination_by structural fuel1
3838

39-
theorem div_eq (x y : Nat) : x / y = if 0 < y ∧ y ≤ x then (x - y) / y + 1 else 0 := by
39+
theorem div_eq_ite (x y : Nat) : x / y = if 0 < y ∧ y ≤ x then (x - y) / y + 1 else 0 := by
4040
change Nat.div _ _ = ite _ (Nat.div _ _ + 1) _
4141
unfold Nat.div
4242
split
@@ -52,6 +52,10 @@ theorem div_eq (x y : Nat) : x / y = if 0 < y ∧ y ≤ x then (x - y) / y + 1 e
5252
next =>
5353
simp only [false_and, ↓reduceIte, *]
5454

55+
@[deprecated div_eq_ite (since := "2026-07-20")]
56+
theorem div_eq (x y : Nat) : x / y = if 0 < y ∧ y ≤ x then (x - y) / y + 1 else 0 :=
57+
Nat.div_eq_ite x y
58+
5559
/--
5660
An induction principle customized for reasoning about the recursion pattern of natural number
5761
division by iterated subtraction.
@@ -71,7 +75,7 @@ decreasing_by apply div_rec_lemma; assumption
7175
theorem div_le_self (n k : Nat) : n / k ≤ n := by
7276
induction n using Nat.strongRecOn with
7377
| ind n ih =>
74-
rw [div_eq]
78+
rw [div_eq_ite]
7579
-- Note: manual split to avoid Classical.em which is not yet defined
7680
cases (inferInstance : Decidable (0 < k ∧ k ≤ n)) with
7781
| isFalse h => simp [h]
@@ -83,7 +87,7 @@ theorem div_le_self (n k : Nat) : n / k ≤ n := by
8387
exact succ_le_of_lt (Nat.lt_of_le_of_lt this hSub)
8488

8589
theorem div_lt_self {n k : Nat} (hLtN : 0 < n) (hLtK : 1 < k) : n / k < n := by
86-
rw [div_eq]
90+
rw [div_eq_ite]
8791
cases (inferInstance : Decidable (0 < k ∧ k ≤ n)) with
8892
| isFalse h => simp [hLtN, h]
8993
| isTrue h =>
@@ -155,9 +159,13 @@ protected theorem modCore_eq_mod (n m : Nat) : Nat.modCore n m = n % m := by
155159
rw [Nat.modCore_eq]
156160
exact if_neg fun ⟨_hlt, hle⟩ => h hle
157161

158-
theorem mod_eq (x y : Nat) : x % y = if 0 < y ∧ y ≤ x then (x - y) % y else x := by
162+
theorem mod_eq_ite (x y : Nat) : x % y = if 0 < y ∧ y ≤ x then (x - y) % y else x := by
159163
rw [←Nat.modCore_eq_mod, ←Nat.modCore_eq_mod, Nat.modCore_eq]
160164

165+
@[deprecated mod_eq_ite (since := "2026-07-20")]
166+
theorem mod_eq (x y : Nat) : x % y = if 0 < y ∧ y ≤ x then (x - y) % y else x :=
167+
Nat.mod_eq_ite x y
168+
161169
/--
162170
An induction principle customized for reasoning about the recursion pattern of `Nat.mod`.
163171
-/
@@ -173,13 +181,13 @@ def mod.inductionOn.{u}
173181
have : (if 0 < 00 ≤ a then (a - 0) % 0 else a) = a :=
174182
have h : ¬ (0 < 00 ≤ a) := fun ⟨h₁, _⟩ => absurd h₁ (Nat.lt_irrefl _)
175183
if_neg h
176-
(mod_eq a 0).symm ▸ this
184+
(mod_eq_ite a 0).symm ▸ this
177185

178186
theorem mod_eq_of_lt {a b : Nat} (h : a < b) : a % b = a :=
179187
have : (if 0 < b ∧ b ≤ a then (a - b) % b else a) = a :=
180188
have h' : ¬(0 < b ∧ b ≤ a) := fun ⟨_, h₁⟩ => absurd h₁ (Nat.not_le_of_gt h)
181189
if_neg h'
182-
(mod_eq a b).symm ▸ this
190+
(mod_eq_ite a b).symm ▸ this
183191

184192
@[simp] theorem one_mod_eq_zero_iff {n : Nat} : 1 % n = 0 ↔ n = 1 := by
185193
match n with
@@ -197,7 +205,7 @@ theorem mod_eq_of_lt {a b : Nat} (h : a < b) : a % b = a :=
197205
theorem mod_eq_sub_mod {a b : Nat} (h : a ≥ b) : a % b = (a - b) % b :=
198206
match eq_zero_or_pos b with
199207
| Or.inl h₁ => h₁.symm ▸ (Nat.sub_zero a).symm ▸ rfl
200-
| Or.inr h₁ => (mod_eq a b).symm ▸ if_pos ⟨h₁, h⟩
208+
| Or.inr h₁ => (mod_eq_ite a b).symm ▸ if_pos ⟨h₁, h⟩
201209

202210
@[simp] protected theorem sub_mod_add_mod_cancel (a b : Nat) [NeZero a] : a - b % a + b % a = a := by
203211
rw [Nat.sub_add_cancel]
@@ -231,7 +239,7 @@ theorem mod_one (x : Nat) : x % 1 = 0 := by
231239
exact this _ h
232240

233241
theorem div_add_mod (m n : Nat) : n * (m / n) + m % n = m := by
234-
rw [div_eq, mod_eq]
242+
rw [div_eq_ite, mod_eq_ite]
235243
have h : Decidable (0 < n ∧ n ≤ m) := inferInstance
236244
cases h with
237245
| isFalse h => simp [h]
@@ -242,10 +250,10 @@ theorem div_add_mod (m n : Nat) : n * (m / n) + m % n = m := by
242250
decreasing_by apply div_rec_lemma; assumption
243251

244252
theorem div_eq_sub_div (h₁ : 0 < b) (h₂ : b ≤ a) : a / b = (a - b) / b + 1 := by
245-
rw [div_eq a, if_pos]; constructor <;> assumption
253+
rw [div_eq_ite a, if_pos]; constructor <;> assumption
246254

247255
theorem mod_add_div (m k : Nat) : m % k + k * (m / k) = m := by
248-
induction m, k using mod.inductionOn with rw [div_eq, mod_eq]
256+
induction m, k using mod.inductionOn with rw [div_eq_ite, mod_eq_ite]
249257
| base x y h => simp [h]
250258
| ind x y h IH => simp [h]; rw [Nat.mul_succ, ← Nat.add_assoc, IH, Nat.sub_add_cancel h.2]
251259

@@ -263,14 +271,14 @@ theorem mod_eq_sub_div_mul {x k : Nat} : x % k = x - (x / k) * k := by
263271
rwa [mod_one, Nat.zero_add, Nat.one_mul] at this
264272

265273
@[simp] protected theorem div_zero (n : Nat) : n / 0 = 0 := by
266-
rw [div_eq]; simp [Nat.lt_irrefl]
274+
rw [div_eq_ite]; simp [Nat.lt_irrefl]
267275

268276
@[simp] protected theorem zero_div (b : Nat) : 0 / b = 0 :=
269-
(div_eq 0 b).trans <| if_neg <| And.rec Nat.not_le_of_gt
277+
(div_eq_ite 0 b).trans <| if_neg <| And.rec Nat.not_le_of_gt
270278

271279
theorem le_div_iff_mul_le (k0 : 0 < k) : x ≤ y / k ↔ x * k ≤ y := by
272280
induction y, k using mod.inductionOn generalizing x with
273-
(rw [div_eq]; simp [h]; cases x with | zero => simp [zero_le] | succ x => ?_)
281+
(rw [div_eq_ite]; simp [h]; cases x with | zero => simp [zero_le] | succ x => ?_)
274282
| base y k h =>
275283
simp only [add_one, succ_mul, false_iff, Nat.not_le, Nat.succ_ne_zero]
276284
refine Nat.lt_of_lt_of_le ?_ (Nat.le_add_left ..)
@@ -412,7 +420,7 @@ theorem mul_mod_mul_left (z x y : Nat) : (z * x) % (z * y) = z * (x % y) :=
412420
exact IH _ (sub_lt (Nat.lt_of_lt_of_le y0 yn) y0)
413421

414422
theorem div_eq_of_lt (h₀ : a < b) : a / b = 0 := by
415-
rw [div_eq a, if_neg]
423+
rw [div_eq_ite a, if_neg]
416424
intro h₁
417425
apply Nat.not_le_of_gt h₀ h₁.right
418426

src/Init/Data/Nat/Div/Lemmas.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ theorem succ_div_of_dvd {a b : Nat} (h : b ∣ a + 1) :
123123
| zero => simp at h
124124
| succ b =>
125125
by_cases h' : b ≤ a
126-
· rw [Nat.div_eq]
126+
· rw [Nat.div_eq_ite]
127127
simp only [zero_lt_succ, Nat.add_le_add_iff_right, h', and_self, ↓reduceIte,
128128
Nat.reduceSubDiff, Nat.add_right_cancel_iff]
129129
obtain ⟨_|k, h⟩ := Nat.dvd_of_mod_eq_zero h

src/Init/Data/Nat/Lemmas.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,7 @@ theorem mul_add_div {m : Nat} (m_pos : m > 0) (x y : Nat) : (m * x + y) / m = x
15641564
match x with
15651565
| 0 => simp
15661566
| x + 1 =>
1567-
rw [Nat.mul_succ, Nat.add_assoc _ m, mul_add_div m_pos x (m+y), div_eq]
1567+
rw [Nat.mul_succ, Nat.add_assoc _ m, mul_add_div m_pos x (m+y), div_eq_ite]
15681568
simp +arith [m_pos]
15691569

15701570
theorem mul_add_mod (m x y : Nat) : (m * x + y) % m = y % m := by

src/Init/Data/Nat/Log2.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ theorem log2_terminates : ∀ n, n ≥ 2 → n / 2 < n
1919
| 2, _ => by decide
2020
| 3, _ => by decide
2121
| n+4, _ => by
22-
rw [div_eq, if_pos]
22+
rw [div_eq_ite, if_pos]
2323
refine succ_lt_succ (Nat.lt_trans ?_ (lt_succ_self _))
2424
exact log2_terminates (n+2) (by simp)
2525
simp

tests/elab/860.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ private theorem pack_loop_terminates : (n : Nat) → n / 2 < n.succ
44
| 0 => by decide
55
| 1 => by decide
66
| n+2 => by
7-
rw [Nat.div_eq]
7+
rw [Nat.div_eq_ite]
88
split
99
· rw [Nat.add_sub_self_right]
1010
have := pack_loop_terminates n

tests/elab/binrec.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ theorem Nat.div2_lt (h : n ≠ 0) : n / 2 < n := by
2727
| 2 => decide
2828
| 3 => decide
2929
| n+4 =>
30-
rw [div_eq, if_pos]
30+
rw [div_eq_ite, if_pos]
3131
refine succ_lt_succ (Nat.lt_trans ?_ (lt_succ_self _))
3232
exact @div2_lt (n+2) (by simp +arith)
3333
simp +arith

0 commit comments

Comments
 (0)