Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 0038a04

Browse files
b-mehtakim-em
andcommitted
feat(data/int/cast): int cast division lemmas (#13929)
Adds lemmas for passing int cast through division, and renames the nat versions from `nat.cast_dvd` to `nat.cast_div`. Also some golf. Co-authored-by: Scott Morrison <scott.morrison@gmail.com>
1 parent 4602370 commit 0038a04

File tree

9 files changed

+30
-15
lines changed

9 files changed

+30
-15
lines changed

src/algebra/char_zero.lean

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,12 @@ lemma cast_add_one_ne_zero (n : ℕ) : (n + 1 : R) ≠ 0 :=
101101
by exact_mod_cast n.succ_ne_zero
102102

103103
@[simp, norm_cast]
104-
theorem cast_dvd_char_zero {k : Type*} [field k] [char_zero k] {m n : ℕ}
104+
theorem cast_div_char_zero {k : Type*} [field k] [char_zero k] {m n : ℕ}
105105
(n_dvd : n ∣ m) : ((m / n : ℕ) : k) = m / n :=
106106
begin
107-
by_cases hn : n = 0,
108-
{ subst hn,
109-
simp },
110-
{ exact cast_dvd n_dvd (cast_ne_zero.mpr hn), },
107+
rcases eq_or_ne n 0 with rfl | hn,
108+
{ simp },
109+
{ exact cast_div n_dvd (cast_ne_zero.2 hn), },
111110
end
112111

113112
end nat

src/data/int/cast.lean

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ by simp [sub_eq_add_neg]
111111
| -[1+ m] -[1+ n] := show (((m + 1) * (n + 1) : ℕ) : α) = -(m + 1) * -(n + 1),
112112
by rw [nat.cast_mul, nat.cast_add_one, nat.cast_add_one, neg_mul_neg]
113113

114+
@[simp] theorem cast_div [field α] {m n : ℤ} (n_dvd : n ∣ m) (n_nonzero : (n : α) ≠ 0) :
115+
((m / n : ℤ) : α) = m / n :=
116+
begin
117+
rcases n_dvd with ⟨k, rfl⟩,
118+
have : n ≠ 0, { rintro rfl, simpa using n_nonzero },
119+
rw [int.mul_div_cancel_left _ this, int.cast_mul, mul_div_cancel_left _ n_nonzero],
120+
end
121+
114122
/-- `coe : ℤ → α` as an `add_monoid_hom`. -/
115123
def cast_add_hom (α : Type*) [add_group α] [has_one α] : ℤ →+ α := ⟨coe, cast_zero, cast_add⟩
116124

src/data/int/char_zero.lean

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ theorem cast_injective [add_group α] [has_one α] [char_zero α] : function.inj
3535
theorem cast_ne_zero [add_group α] [has_one α] [char_zero α] {n : ℤ} : (n : α) ≠ 0 ↔ n ≠ 0 :=
3636
not_congr cast_eq_zero
3737

38+
@[simp, norm_cast]
39+
theorem cast_div_char_zero {k : Type*} [field k] [char_zero k] {m n : ℤ}
40+
(n_dvd : n ∣ m) : ((m / n : ℤ) : k) = m / n :=
41+
begin
42+
rcases eq_or_ne n 0 with rfl | hn,
43+
{ simp [int.div_zero] },
44+
{ exact cast_div n_dvd (cast_ne_zero.mpr hn), },
45+
end
46+
3847
end int
3948

4049
lemma ring_hom.injective_int {α : Type*} [ring α] (f : ℤ →+* α) [char_zero α] :

src/data/nat/cast.lean

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,12 @@ eq_sub_of_add_eq $ by rw [← cast_add, tsub_add_cancel_of_le h]
132132
| (n+1) := (cast_add _ _).trans $
133133
show ((m * n : ℕ) : α) + m = m * (n + 1), by rw [cast_mul n, left_distrib, mul_one]
134134

135-
@[simp] theorem cast_dvd {α : Type*} [field α] {m n : ℕ} (n_dvd : n ∣ m) (n_nonzero : (n:α) ≠ 0) :
135+
@[simp] theorem cast_div [field α] {m n : ℕ} (n_dvd : n ∣ m) (n_nonzero : (n : α) ≠ 0) :
136136
((m / n : ℕ) : α) = m / n :=
137137
begin
138138
rcases n_dvd with ⟨k, rfl⟩,
139139
have : n ≠ 0, {rintro rfl, simpa using n_nonzero},
140-
rw nat.mul_div_cancel_left _ (pos_iff_ne_zero.2 this),
141-
rw [nat.cast_mul, mul_div_cancel_left _ n_nonzero],
140+
rw [nat.mul_div_cancel_left _ this.bot_lt, cast_mul, mul_div_cancel_left _ n_nonzero],
142141
end
143142

144143
/-- `coe : ℕ → α` as a `ring_hom` -/

src/data/nat/totient.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ begin
335335
{ rw [←cast_prod, cast_ne_zero, ←zero_lt_iff, ←prod_factorization_eq_prod_factors],
336336
exact prod_pos (λ p hp, pos_of_mem_factorization hp) },
337337
simp only [totient_eq_div_factors_mul n, prod_prime_factors_dvd n, cast_mul, cast_prod,
338-
cast_dvd_char_zero, mul_comm_div', mul_right_inj' hn', div_eq_iff hpQ, ←prod_mul_distrib],
338+
cast_div_char_zero, mul_comm_div', mul_right_inj' hn', div_eq_iff hpQ, ←prod_mul_distrib],
339339
refine prod_congr rfl (λ p hp, _),
340340
have hp := pos_of_mem_factors (list.mem_to_finset.mp hp),
341341
have hp' : (p : ℚ) ≠ 0 := cast_ne_zero.mpr hp.ne.symm,

src/number_theory/bernoulli.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ begin
148148
have := factorial_mul_factorial_dvd_factorial_add i j,
149149
field_simp [mul_comm _ (bernoulli' i), mul_assoc, add_choose],
150150
rw_mod_cast [mul_comm (j + 1), mul_div_assoc, ← mul_assoc],
151-
rw [cast_mul, cast_mul, mul_div_mul_right, cast_dvd_char_zero, cast_mul],
151+
rw [cast_mul, cast_mul, mul_div_mul_right, cast_div_char_zero, cast_mul],
152152
assumption',
153153
end
154154

@@ -256,7 +256,7 @@ begin
256256
have hj : (j.succ : ℚ) ≠ 0 := by exact_mod_cast succ_ne_zero j,
257257
field_simp [← h, mul_ne_zero hj (hfact j), hfact i, mul_comm _ (bernoulli i), mul_assoc],
258258
rw_mod_cast [mul_comm (j + 1), mul_div_assoc, ← mul_assoc],
259-
rw [cast_mul, cast_mul, mul_div_mul_right _ _ hj, add_choose, cast_dvd_char_zero],
259+
rw [cast_mul, cast_mul, mul_div_mul_right _ _ hj, add_choose, cast_div_char_zero],
260260
apply factorial_mul_factorial_dvd_factorial_add,
261261
end
262262

@@ -287,7 +287,7 @@ begin
287287
rw [choose_eq_factorial_div_factorial h.le, eq_comm, div_eq_iff (hne q.succ), succ_eq_add_one,
288288
mul_assoc _ _ ↑q.succ!, mul_comm _ ↑q.succ!, ← mul_assoc, div_mul_eq_mul_div,
289289
mul_comm (↑n ^ (q - m + 1)), ← mul_assoc _ _ (↑n ^ (q - m + 1)), ← one_div, mul_one_div,
290-
div_div_eq_div_mul, tsub_add_eq_add_tsub (le_of_lt_succ h), cast_dvd, cast_mul],
290+
div_div_eq_div_mul, tsub_add_eq_add_tsub (le_of_lt_succ h), cast_div, cast_mul],
291291
{ ring },
292292
{ exact factorial_mul_factorial_dvd_factorial h.le },
293293
{ simp [hne] } },

src/number_theory/von_mangoldt.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ begin
124124
{ rw [cast_ne_zero],
125125
rintro rfl,
126126
exact hn (by simpa using mn) },
127-
rw [nat.cast_dvd mn this, real.log_div (cast_ne_zero.2 hn) this, neg_sub, mul_sub] },
127+
rw [nat.cast_div mn this, real.log_div (cast_ne_zero.2 hn) this, neg_sub, mul_sub] },
128128
rw [this, sum_sub_distrib, ←sum_mul, ←int.cast_sum, ←coe_mul_zeta_apply, eq_comm, sub_eq_self,
129129
moebius_mul_coe_zeta, mul_eq_zero, int.cast_eq_zero],
130130
rcases eq_or_ne n 1 with hn | hn;

src/ring_theory/discriminant.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ begin
203203
have hle : 1 ≤ pb.dim,
204204
{ rw [← hn, nat.one_le_iff_ne_zero, ← zero_lt_iff, finite_dimensional.finrank_pos_iff],
205205
apply_instance },
206-
rw [hn, nat.cast_dvd h₂ hne, nat.cast_mul, nat.cast_sub hle],
206+
rw [hn, nat.cast_div h₂ hne, nat.cast_mul, nat.cast_sub hle],
207207
field_simp,
208208
ring,
209209
end

src/ring_theory/power_series/well_known.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ begin
110110
symmetry,
111111
rw [div_eq_iff, div_mul_eq_mul_div, one_mul, choose_eq_factorial_div_factorial],
112112
norm_cast,
113-
rw cast_dvd_char_zero,
113+
rw cast_div_char_zero,
114114
{ apply factorial_mul_factorial_dvd_factorial (mem_range_succ_iff.1 hx), },
115115
{ apply mem_range_succ_iff.1 hx, },
116116
{ rintros h, apply factorial_ne_zero n, rw cast_eq_zero.1 h, },

0 commit comments

Comments
 (0)