Skip to content

Commit c27bc4e

Browse files
jcommelinRuben-VandeVeldeVierkantor
committed
refactor(ZMod): remove coe out of ZMod (#9839)
Co-authored-by: Ruben Van de Velde <65514131+Ruben-VandeVelde@users.noreply.github.com> Co-authored-by: Vierkantor <vierkantor@vierkantor.com>
1 parent fe3b2b2 commit c27bc4e

File tree

9 files changed

+71
-70
lines changed

9 files changed

+71
-70
lines changed

Mathlib/Data/ZMod/Algebra.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ See note [reducible non-instances]. -/
2929
@[reducible]
3030
def algebra' (h : m ∣ n) : Algebra (ZMod n) R :=
3131
{ ZMod.castHom h R with
32-
smul := fun a r => a * r
32+
smul := fun a r => cast a * r
3333
commutes' := fun a r =>
34-
show (a * r : R) = r * a by
34+
show (cast a * r : R) = r * cast a by
3535
rcases ZMod.int_cast_surjective a with ⟨k, rfl⟩
3636
show ZMod.castHom h R k * r = r * ZMod.castHom h R k
3737
rw [map_intCast, Int.cast_comm]

Mathlib/Data/ZMod/Basic.lean

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -148,24 +148,21 @@ variable [AddGroupWithOne R]
148148
/-- Cast an integer modulo `n` to another semiring.
149149
This function is a morphism if the characteristic of `R` divides `n`.
150150
See `ZMod.castHom` for a bundled version. -/
151-
@[coe] def cast : ∀ {n : ℕ}, ZMod n → R
151+
def cast : ∀ {n : ℕ}, ZMod n → R
152152
| 0 => Int.cast
153153
| _ + 1 => fun i => i.val
154154
#align zmod.cast ZMod.cast
155155

156-
-- see Note [coercion into rings]
157-
instance (priority := 900) (n : ℕ) : CoeTC (ZMod n) R :=
158-
⟨cast⟩
159156

160157
@[simp]
161-
theorem cast_zero : ((0 : ZMod n) : R) = 0 := by
158+
theorem cast_zero : (cast (0 : ZMod n) : R) = 0 := by
162159
delta ZMod.cast
163160
cases n
164161
· exact Int.cast_zero
165162
· simp
166163
#align zmod.cast_zero ZMod.cast_zero
167164

168-
theorem cast_eq_val [NeZero n] (a : ZMod n) : (a : R) = a.val := by
165+
theorem cast_eq_val [NeZero n] (a : ZMod n) : (cast a : R) = a.val := by
169166
cases n
170167
· cases NeZero.ne 0 rfl
171168
rfl
@@ -174,14 +171,14 @@ theorem cast_eq_val [NeZero n] (a : ZMod n) : (a : R) = a.val := by
174171
variable {S : Type*} [AddGroupWithOne S]
175172

176173
@[simp]
177-
theorem _root_.Prod.fst_zmod_cast (a : ZMod n) : (a : R × S).fst = a := by
174+
theorem _root_.Prod.fst_zmod_cast (a : ZMod n) : (cast a : R × S).fst = cast a := by
178175
cases n
179176
· rfl
180177
· simp [ZMod.cast]
181178
#align prod.fst_zmod_cast Prod.fst_zmod_cast
182179

183180
@[simp]
184-
theorem _root_.Prod.snd_zmod_cast (a : ZMod n) : (a : R × S).snd = a := by
181+
theorem _root_.Prod.snd_zmod_cast (a : ZMod n) : (cast a : R × S).snd = cast a := by
185182
cases n
186183
· rfl
187184
· simp [ZMod.cast]
@@ -208,22 +205,21 @@ theorem nat_cast_zmod_surjective [NeZero n] : Function.Surjective ((↑) : ℕ
208205
/-- So-named because the outer coercion is `Int.cast` into `ZMod`. For `Int.cast` into an arbitrary
209206
ring, see `ZMod.int_cast_cast`. -/
210207
@[norm_cast]
211-
theorem int_cast_zmod_cast (a : ZMod n) : ((a : ℤ) : ZMod n) = a := by
208+
theorem int_cast_zmod_cast (a : ZMod n) : ((cast a : ℤ) : ZMod n) = a := by
212209
cases n
213210
· simp [ZMod.cast, ZMod]
214211
· dsimp [ZMod.cast, ZMod]
215212
erw [Int.cast_ofNat, Fin.cast_val_eq_self]
216213
#align zmod.int_cast_zmod_cast ZMod.int_cast_zmod_cast
217214

218-
theorem int_cast_rightInverse : Function.RightInverse ((↑) : ZMod n → ℤ) ((↑) : ℤ → ZMod n) :=
215+
theorem int_cast_rightInverse : Function.RightInverse (cast : ZMod n → ℤ) ((↑) : ℤ → ZMod n) :=
219216
int_cast_zmod_cast
220217
#align zmod.int_cast_right_inverse ZMod.int_cast_rightInverse
221218

222219
theorem int_cast_surjective : Function.Surjective ((↑) : ℤ → ZMod n) :=
223220
int_cast_rightInverse.surjective
224221
#align zmod.int_cast_surjective ZMod.int_cast_surjective
225222

226-
@[norm_cast]
227223
theorem cast_id : ∀ (n) (i : ZMod n), (ZMod.cast i : ZMod n) = i
228224
| 0, _ => Int.cast_id
229225
| _ + 1, i => nat_cast_zmod_val i
@@ -238,15 +234,15 @@ variable (R) [Ring R]
238234

239235
/-- The coercions are respectively `Nat.cast` and `ZMod.cast`. -/
240236
@[simp]
241-
theorem nat_cast_comp_val [NeZero n] : ((↑) : ℕ → R) ∘ (val : ZMod n → ℕ) = (↑) := by
237+
theorem nat_cast_comp_val [NeZero n] : ((↑) : ℕ → R) ∘ (val : ZMod n → ℕ) = cast := by
242238
cases n
243239
· cases NeZero.ne 0 rfl
244240
rfl
245241
#align zmod.nat_cast_comp_val ZMod.nat_cast_comp_val
246242

247243
/-- The coercions are respectively `Int.cast`, `ZMod.cast`, and `ZMod.cast`. -/
248244
@[simp]
249-
theorem int_cast_comp_cast : ((↑) : ℤ → R) ∘ ((↑) : ZMod n → ℤ) = (↑) := by
245+
theorem int_cast_comp_cast : ((↑) : ℤ → R) ∘ (cast : ZMod n → ℤ) = cast := by
250246
cases n
251247
· exact congr_arg (Int.cast ∘ ·) ZMod.cast_id'
252248
· ext
@@ -256,17 +252,18 @@ theorem int_cast_comp_cast : ((↑) : ℤ → R) ∘ ((↑) : ZMod n → ℤ) =
256252
variable {R}
257253

258254
@[simp]
259-
theorem nat_cast_val [NeZero n] (i : ZMod n) : (i.val : R) = i :=
255+
theorem nat_cast_val [NeZero n] (i : ZMod n) : (i.val : R) = cast i :=
260256
congr_fun (nat_cast_comp_val R) i
261257
#align zmod.nat_cast_val ZMod.nat_cast_val
262258

263259
@[simp]
264-
theorem int_cast_cast (i : ZMod n) : ((i : ℤ) : R) = i :=
260+
theorem int_cast_cast (i : ZMod n) : ((cast i : ℤ) : R) = cast i :=
265261
congr_fun (int_cast_comp_cast R) i
266262
#align zmod.int_cast_cast ZMod.int_cast_cast
267263

268-
theorem coe_add_eq_ite {n : ℕ} (a b : ZMod n) :
269-
(↑(a + b) : ℤ) = if (n : ℤ) ≤ a + b then (a : ℤ) + b - n else a + b := by
264+
theorem cast_add_eq_ite {n : ℕ} (a b : ZMod n) :
265+
(cast (a + b) : ℤ) =
266+
if (n : ℤ) ≤ cast a + cast b then (cast a + cast b - n : ℤ) else cast a + cast b := by
270267
cases' n with n
271268
· simp; rfl
272269
change Fin (n + 1) at a b
@@ -277,7 +274,7 @@ theorem coe_add_eq_ite {n : ℕ} (a b : ZMod n) :
277274
· rw [Nat.cast_sub h]
278275
congr
279276
· rfl
280-
#align zmod.coe_add_eq_ite ZMod.coe_add_eq_ite
277+
#align zmod.coe_add_eq_ite ZMod.cast_add_eq_ite
281278

282279
section CharDvd
283280

@@ -287,7 +284,7 @@ section CharDvd
287284
variable {m : ℕ} [CharP R m]
288285

289286
@[simp]
290-
theorem cast_one (h : m ∣ n) : ((1 : ZMod n) : R) = 1 := by
287+
theorem cast_one (h : m ∣ n) : (cast (1 : ZMod n) : R) = 1 := by
291288
cases' n with n
292289
· exact Int.cast_one
293290
show ((1 % (n + 1) : ℕ) : R) = 1
@@ -300,7 +297,7 @@ theorem cast_one (h : m ∣ n) : ((1 : ZMod n) : R) = 1 := by
300297
exact Nat.lt_of_sub_eq_succ rfl
301298
#align zmod.cast_one ZMod.cast_one
302299

303-
theorem cast_add (h : m ∣ n) (a b : ZMod n) : ((a + b : ZMod n) : R) = a + b := by
300+
theorem cast_add (h : m ∣ n) (a b : ZMod n) : (cast (a + b : ZMod n) : R) = cast a + cast b := by
304301
cases n
305302
· apply Int.cast_add
306303
symm
@@ -310,7 +307,7 @@ theorem cast_add (h : m ∣ n) (a b : ZMod n) : ((a + b : ZMod n) : R) = a + b :
310307
exact h.trans (Nat.dvd_sub_mod _)
311308
#align zmod.cast_add ZMod.cast_add
312309

313-
theorem cast_mul (h : m ∣ n) (a b : ZMod n) : ((a * b : ZMod n) : R) = a * b := by
310+
theorem cast_mul (h : m ∣ n) (a b : ZMod n) : (cast (a * b : ZMod n) : R) = cast a * cast b := by
314311
cases n
315312
· apply Int.cast_mul
316313
symm
@@ -325,40 +322,40 @@ theorem cast_mul (h : m ∣ n) (a b : ZMod n) : ((a * b : ZMod n) : R) = a * b :
325322
See also `ZMod.lift` for a generalized version working in `AddGroup`s.
326323
-/
327324
def castHom (h : m ∣ n) (R : Type*) [Ring R] [CharP R m] : ZMod n →+* R where
328-
toFun := (↑)
325+
toFun := cast
329326
map_zero' := cast_zero
330327
map_one' := cast_one h
331328
map_add' := cast_add h
332329
map_mul' := cast_mul h
333330
#align zmod.cast_hom ZMod.castHom
334331

335332
@[simp]
336-
theorem castHom_apply {h : m ∣ n} (i : ZMod n) : castHom h R i = i :=
333+
theorem castHom_apply {h : m ∣ n} (i : ZMod n) : castHom h R i = cast i :=
337334
rfl
338335
#align zmod.cast_hom_apply ZMod.castHom_apply
339336

340-
@[simp, norm_cast]
341-
theorem cast_sub (h : m ∣ n) (a b : ZMod n) : ((a - b : ZMod n) : R) = (a : R) - b :=
337+
@[simp]
338+
theorem cast_sub (h : m ∣ n) (a b : ZMod n) : (cast (a - b : ZMod n) : R) = cast a - cast b :=
342339
(castHom h R).map_sub a b
343340
#align zmod.cast_sub ZMod.cast_sub
344341

345-
@[simp, norm_cast]
346-
theorem cast_neg (h : m ∣ n) (a : ZMod n) : ((-a : ZMod n) : R) = -(a : R) :=
342+
@[simp]
343+
theorem cast_neg (h : m ∣ n) (a : ZMod n) : (cast (-a : ZMod n) : R) = -(cast a) :=
347344
(castHom h R).map_neg a
348345
#align zmod.cast_neg ZMod.cast_neg
349346

350-
@[simp, norm_cast]
351-
theorem cast_pow (h : m ∣ n) (a : ZMod n) (k : ℕ) : ((a ^ k : ZMod n) : R) = (a : R) ^ k :=
347+
@[simp]
348+
theorem cast_pow (h : m ∣ n) (a : ZMod n) (k : ℕ) : (cast (a ^ k : ZMod n) : R) = (cast a) ^ k :=
352349
(castHom h R).map_pow a k
353350
#align zmod.cast_pow ZMod.cast_pow
354351

355352
@[simp, norm_cast]
356-
theorem cast_nat_cast (h : m ∣ n) (k : ℕ) : ((k : ZMod n) : R) = k :=
353+
theorem cast_nat_cast (h : m ∣ n) (k : ℕ) : (cast (k : ZMod n) : R) = k :=
357354
map_natCast (castHom h R) k
358355
#align zmod.cast_nat_cast ZMod.cast_nat_cast
359356

360357
@[simp, norm_cast]
361-
theorem cast_int_cast (h : m ∣ n) (k : ℤ) : ((k : ZMod n) : R) = k :=
358+
theorem cast_int_cast (h : m ∣ n) (k : ℤ) : (cast (k : ZMod n) : R) = k :=
362359
map_intCast (castHom h R) k
363360
#align zmod.cast_int_cast ZMod.cast_int_cast
364361

@@ -372,37 +369,37 @@ section CharEq
372369
variable [CharP R n]
373370

374371
@[simp]
375-
theorem cast_one' : ((1 : ZMod n) : R) = 1 :=
372+
theorem cast_one' : (cast (1 : ZMod n) : R) = 1 :=
376373
cast_one dvd_rfl
377374
#align zmod.cast_one' ZMod.cast_one'
378375

379376
@[simp]
380-
theorem cast_add' (a b : ZMod n) : ((a + b : ZMod n) : R) = a + b :=
377+
theorem cast_add' (a b : ZMod n) : (cast (a + b : ZMod n) : R) = cast a + cast b :=
381378
cast_add dvd_rfl a b
382379
#align zmod.cast_add' ZMod.cast_add'
383380

384381
@[simp]
385-
theorem cast_mul' (a b : ZMod n) : ((a * b : ZMod n) : R) = a * b :=
382+
theorem cast_mul' (a b : ZMod n) : (cast (a * b : ZMod n) : R) = cast a * cast b :=
386383
cast_mul dvd_rfl a b
387384
#align zmod.cast_mul' ZMod.cast_mul'
388385

389386
@[simp]
390-
theorem cast_sub' (a b : ZMod n) : ((a - b : ZMod n) : R) = a - b :=
387+
theorem cast_sub' (a b : ZMod n) : (cast (a - b : ZMod n) : R) = cast a - cast b :=
391388
cast_sub dvd_rfl a b
392389
#align zmod.cast_sub' ZMod.cast_sub'
393390

394391
@[simp]
395-
theorem cast_pow' (a : ZMod n) (k : ℕ) : ((a ^ k : ZMod n) : R) = (a : R) ^ k :=
392+
theorem cast_pow' (a : ZMod n) (k : ℕ) : (cast (a ^ k : ZMod n) : R) = (cast a : R) ^ k :=
396393
cast_pow dvd_rfl a k
397394
#align zmod.cast_pow' ZMod.cast_pow'
398395

399396
@[simp, norm_cast]
400-
theorem cast_nat_cast' (k : ℕ) : ((k : ZMod n) : R) = k :=
397+
theorem cast_nat_cast' (k : ℕ) : (cast (k : ZMod n) : R) = k :=
401398
cast_nat_cast dvd_rfl k
402399
#align zmod.cast_nat_cast' ZMod.cast_nat_cast'
403400

404401
@[simp, norm_cast]
405-
theorem cast_int_cast' (k : ℤ) : ((k : ZMod n) : R) = k :=
402+
theorem cast_int_cast' (k : ℤ) : (cast (k : ZMod n) : R) = k :=
406403
cast_int_cast dvd_rfl k
407404
#align zmod.cast_int_cast' ZMod.cast_int_cast'
408405

@@ -492,7 +489,7 @@ theorem val_int_cast {n : ℕ} (a : ℤ) [NeZero n] : ↑(a : ZMod n).val = a %
492489
rw [← ZMod.int_cast_eq_int_cast_iff', Int.cast_ofNat, ZMod.nat_cast_val, ZMod.cast_id]
493490
#align zmod.val_int_cast ZMod.val_int_cast
494491

495-
theorem coe_int_cast {n : ℕ} (a : ℤ) : (a : ZMod n) = a % n := by
492+
theorem coe_int_cast {n : ℕ} (a : ℤ) : cast (a : ZMod n) = a % n := by
496493
cases n
497494
· rw [Int.ofNat_zero, Int.emod_zero, Int.cast_id]; rfl
498495
· rw [← val_int_cast, val]; rfl
@@ -508,14 +505,14 @@ theorem val_neg_one (n : ℕ) : (-1 : ZMod n.succ).val = n := by
508505
#align zmod.val_neg_one ZMod.val_neg_one
509506

510507
/-- `-1 : ZMod n` lifts to `n - 1 : R`. This avoids the characteristic assumption in `cast_neg`. -/
511-
theorem cast_neg_one {R : Type*} [Ring R] (n : ℕ) : (-1 : ZMod n) = (n - 1 : R) := by
508+
theorem cast_neg_one {R : Type*} [Ring R] (n : ℕ) : cast (-1 : ZMod n) = (n - 1 : R) := by
512509
cases' n with n
513510
· dsimp [ZMod, ZMod.cast]; simp
514511
· rw [← nat_cast_val, val_neg_one, Nat.cast_succ, add_sub_cancel]
515512
#align zmod.cast_neg_one ZMod.cast_neg_one
516513

517514
theorem cast_sub_one {R : Type*} [Ring R] {n : ℕ} (k : ZMod n) :
518-
((k - 1 : ZMod n) : R) = (if k = 0 then (n : R) else k) - 1 := by
515+
(cast (k - 1 : ZMod n) : R) = (if k = 0 then (n : R) else cast k) - 1 := by
519516
split_ifs with hk
520517
· rw [hk, zero_sub, ZMod.cast_neg_one]
521518
· cases n
@@ -581,7 +578,7 @@ theorem cast_injective_of_le {m n : ℕ} [nzm : NeZero m] (h : m ≤ n) :
581578
exact f
582579

583580
theorem cast_zmod_eq_zero_iff_of_le {m n : ℕ} [NeZero m] (h : m ≤ n) (a : ZMod m) :
584-
(a : ZMod n) = 0 ↔ a = 0 := by
581+
(cast a : ZMod n) = 0 ↔ a = 0 := by
585582
rw [← ZMod.cast_zero (n := m)]
586583
exact Injective.eq_iff' (cast_injective_of_le h) rfl
587584

@@ -798,7 +795,8 @@ def chineseRemainder {m n : ℕ} (h : m.Coprime n) : ZMod (m * n) ≃+* ZMod m
798795
let to_fun : ZMod (m * n) → ZMod m × ZMod n :=
799796
ZMod.castHom (show m.lcm n ∣ m * n by simp [Nat.lcm_dvd_iff]) (ZMod m × ZMod n)
800797
let inv_fun : ZMod m × ZMod n → ZMod (m * n) := fun x =>
801-
if m * n = 0 then if m = 1 then RingHom.snd _ (ZMod n) x else RingHom.fst (ZMod m) _ x
798+
if m * n = 0 then
799+
if m = 1 then cast (RingHom.snd _ (ZMod n) x) else cast (RingHom.fst (ZMod m) _ x)
802800
else Nat.chineseRemainder h x.1.val x.2.val
803801
have inv : Function.LeftInverse inv_fun to_fun ∧ Function.RightInverse inv_fun to_fun :=
804802
if hmn0 : m * n = 0 then by
@@ -824,8 +822,8 @@ def chineseRemainder {m n : ℕ} (h : m.Coprime n) : ZMod (m * n) ≃+* ZMod m
824822
rw [if_neg hmn0, ZMod.eq_iff_modEq_nat, ← Nat.modEq_and_modEq_iff_modEq_mul h,
825823
Prod.fst_zmod_cast, Prod.snd_zmod_cast]
826824
refine'
827-
⟨(Nat.chineseRemainder h (x : ZMod m).val (x : ZMod n).val).2.left.trans _,
828-
(Nat.chineseRemainder h (x : ZMod m).val (x : ZMod n).val).2.right.trans _⟩
825+
⟨(Nat.chineseRemainder h (cast x : ZMod m).val (cast x : ZMod n).val).2.left.trans _,
826+
(Nat.chineseRemainder h (cast x : ZMod m).val (cast x : ZMod n).val).2.right.trans _⟩
829827
· rw [← ZMod.eq_iff_modEq_nat, ZMod.nat_cast_zmod_val, ZMod.nat_cast_val]
830828
· rw [← ZMod.eq_iff_modEq_nat, ZMod.nat_cast_zmod_val, ZMod.nat_cast_val]
831829
exact ⟨left_inv, left_inv.rightInverse_of_card_le (by simp)⟩
@@ -953,7 +951,7 @@ theorem val_cast_eq_val_of_lt {m n : ℕ} [nzm : NeZero m] {a : ZMod m}
953951
| succ n => exact Fin.val_cast_of_lt h
954952

955953
theorem cast_cast_zmod_of_le {m n : ℕ} [hm : NeZero m] (h : m ≤ n) (a : ZMod m) :
956-
((a : ZMod n) : ZMod m) = a := by
954+
(cast (cast a : ZMod n) : ZMod m) = a := by
957955
have : NeZero n := ⟨((Nat.zero_lt_of_ne_zero hm.out).trans_le h).ne'⟩
958956
rw [cast_eq_val, val_cast_eq_val_of_lt (a.val_lt.trans_le h), nat_cast_zmod_val]
959957

@@ -1222,15 +1220,15 @@ instance subsingleton_ringEquiv [Semiring R] : Subsingleton (ZMod n ≃+* R) :=
12221220
#align zmod.subsingleton_ring_equiv ZMod.subsingleton_ringEquiv
12231221

12241222
@[simp]
1225-
theorem ringHom_map_cast [Ring R] (f : R →+* ZMod n) (k : ZMod n) : f k = k := by
1223+
theorem ringHom_map_cast [Ring R] (f : R →+* ZMod n) (k : ZMod n) : f (cast k) = k := by
12261224
cases n
12271225
· dsimp [ZMod, ZMod.cast] at f k ⊢; simp
12281226
· dsimp [ZMod, ZMod.cast] at f k ⊢
12291227
erw [map_natCast, Fin.cast_val_eq_self]
12301228
#align zmod.ring_hom_map_cast ZMod.ringHom_map_cast
12311229

12321230
theorem ringHom_rightInverse [Ring R] (f : R →+* ZMod n) :
1233-
Function.RightInverse ((↑) : ZMod n → R) f :=
1231+
Function.RightInverse (cast : ZMod n → R) f :=
12341232
ringHom_map_cast f
12351233
#align zmod.ring_hom_right_inverse ZMod.ringHom_rightInverse
12361234

@@ -1269,7 +1267,7 @@ def lift : { f : ℤ →+ A // f n = 0 } ≃ (ZMod n →+ A) :=
12691267
simp only [f.map_zsmul, zsmul_zero, f.mem_ker, hf]
12701268
· intro h
12711269
refine' h (AddSubgroup.mem_zmultiples _)).trans <|
1272-
(Int.castAddHom (ZMod n)).liftOfRightInverse (↑) int_cast_zmod_cast
1270+
(Int.castAddHom (ZMod n)).liftOfRightInverse cast int_cast_zmod_cast
12731271
#align zmod.lift ZMod.lift
12741272

12751273
variable (f : { f : ℤ →+ A // f n = 0 })

Mathlib/Data/ZMod/Module.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ namespace ZMod
4848

4949
theorem map_smul (f : F) (c : ZMod n) (x : M) : f (c • x) = c • f x := by
5050
rw [← ZMod.int_cast_zmod_cast c]
51-
exact map_int_cast_smul f _ _ c x
51+
exact map_int_cast_smul f _ _ (cast c) x
5252

5353
theorem smul_mem (hx : x ∈ K) (c : ZMod n) : c • x ∈ K := by
5454
rw [← ZMod.int_cast_zmod_cast c, ← zsmul_eq_smul_cast]
55-
exact zsmul_mem hx c
55+
exact zsmul_mem hx (cast c)
5656

5757
end ZMod
5858

0 commit comments

Comments
 (0)