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

Commit a47cda9

Browse files
committed
refactor(algebra/group_with_zero/defs): use is_*cancel_mul_zero (#17963)
1 parent 9116dd6 commit a47cda9

File tree

14 files changed

+92
-120
lines changed

14 files changed

+92
-120
lines changed

archive/imo/imo1988_q6.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,6 @@ begin
292292
{ simp only [mul_one, one_mul, add_comm, zero_add] at h,
293293
have y_dvd : y ∣ y * k := dvd_mul_right y k,
294294
rw [← h, ← add_assoc, nat.dvd_add_left (dvd_mul_left y y)] at y_dvd,
295-
obtain rfl|rfl := (nat.dvd_prime nat.prime_two).mp y_dvd; apply nat.eq_of_mul_eq_mul_left,
296-
exacts [zero_lt_one, h.symm, zero_lt_two, h.symm] } }
295+
obtain rfl|rfl := (nat.dvd_prime nat.prime_two).mp y_dvd; apply mul_left_cancel₀,
296+
exacts [one_ne_zero, h.symm, two_ne_zero, h.symm] } }
297297
end

src/algebra/associated.lean

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -889,22 +889,19 @@ instance : no_zero_divisors (associates α) :=
889889
have a = 0 ∨ b = 0, from mul_eq_zero.1 this,
890890
this.imp (assume h, h.symm ▸ rfl) (assume h, h.symm ▸ rfl))⟩
891891

892-
lemma eq_of_mul_eq_mul_left :
893-
∀(a b c : associates α), a ≠ 0 → a * b = a * c → b = c :=
894-
begin
895-
rintros ⟨a⟩ ⟨b⟩ ⟨c⟩ ha h,
896-
rcases quotient.exact' h with ⟨u, hu⟩,
897-
have hu : a * (b * ↑u) = a * c, { rwa [← mul_assoc] },
898-
exact quotient.sound' ⟨u, mul_left_cancel₀ (mk_ne_zero.1 ha) hu⟩
899-
end
900-
901-
lemma eq_of_mul_eq_mul_right :
902-
∀(a b c : associates α), b ≠ 0 → a * b = c * b → a = c :=
903-
λ a b c bne0, (mul_comm b a) ▸ (mul_comm b c) ▸ (eq_of_mul_eq_mul_left b a c bne0)
892+
instance : cancel_comm_monoid_with_zero (associates α) :=
893+
{ mul_left_cancel_of_ne_zero :=
894+
begin
895+
rintros ⟨a⟩ ⟨b⟩ ⟨c⟩ ha h,
896+
rcases quotient.exact' h with ⟨u, hu⟩,
897+
rw [mul_assoc] at hu,
898+
exact quotient.sound' ⟨u, mul_left_cancel₀ (mk_ne_zero.1 ha) hu⟩
899+
end,
900+
.. (infer_instance : comm_monoid_with_zero (associates α)) }
904901

905902
lemma le_of_mul_le_mul_left (a b c : associates α) (ha : a ≠ 0) :
906903
a * b ≤ a * c → b ≤ c
907-
| ⟨d, hd⟩ := ⟨d, eq_of_mul_eq_mul_left a _ _ ha $ by rwa ← mul_assoc⟩
904+
| ⟨d, hd⟩ := ⟨d, mul_left_cancel₀ ha $ by rwa ← mul_assoc⟩
908905

909906
lemma one_or_eq_of_le_of_prime :
910907
∀(p m : associates α), prime p → m ≤ p → (m = 1 ∨ m = p)
@@ -922,11 +919,6 @@ match h m d dvd_rfl with
922919
or.inl $ bot_unique $ associates.le_of_mul_le_mul_left d m 1 ‹d ≠ 0this
923920
end
924921

925-
instance : cancel_comm_monoid_with_zero (associates α) :=
926-
{ mul_left_cancel_of_ne_zero := eq_of_mul_eq_mul_left,
927-
mul_right_cancel_of_ne_zero := eq_of_mul_eq_mul_right,
928-
.. (infer_instance : comm_monoid_with_zero (associates α)) }
929-
930922
instance : canonically_ordered_monoid (associates α) :=
931923
{ exists_mul_of_le := λ a b, id,
932924
le_self_mul := λ a b, ⟨b, rfl⟩,

src/algebra/char_p/basic.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,9 @@ or.elim (eq_zero_or_eq_zero_of_mul_eq_zero this)
411411
have p ∣ e, from (cast_eq_zero_iff R p e).mp he,
412412
have e ∣ p, from dvd_of_mul_left_eq d (eq.symm hmul),
413413
have e = p, from dvd_antisymm ‹e ∣ p› ‹p ∣ e›,
414-
have h₀ : p > 0, from gt_of_ge_of_gt hp (nat.zero_lt_succ 1),
414+
have h₀ : 0 < p, from two_pos.trans_le hp,
415415
have d * p = 1 * p, by rw ‹e = p› at hmul; rw [one_mul]; exact eq.symm hmul,
416-
show d = 1 ∨ d = p, from or.inl (eq_of_mul_eq_mul_right h₀ this))
416+
show d = 1 ∨ d = p, from or.inl (mul_right_cancel₀ h₀.ne' this))
417417

418418
section nontrivial
419419

src/algebra/group_with_zero/defs.lean

Lines changed: 63 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,75 @@ class mul_zero_class (M₀ : Type*) extends has_mul M₀, has_zero M₀ :=
3838
(zero_mul : ∀ a : M₀, 0 * a = 0)
3939
(mul_zero : ∀ a : M₀, a * 0 = 0)
4040

41+
section mul_zero_class
42+
43+
variables [mul_zero_class M₀] {a b : M₀}
44+
45+
@[ematch, simp] lemma zero_mul (a : M₀) : 0 * a = 0 :=
46+
mul_zero_class.zero_mul a
47+
48+
@[ematch, simp] lemma mul_zero (a : M₀) : a * 0 = 0 :=
49+
mul_zero_class.mul_zero a
50+
51+
end mul_zero_class
52+
4153
/-- A mixin for left cancellative multiplication by nonzero elements. -/
4254
@[protect_proj] class is_left_cancel_mul_zero (M₀ : Type u) [has_mul M₀] [has_zero M₀] : Prop :=
4355
(mul_left_cancel_of_ne_zero : ∀ {a b c : M₀}, a ≠ 0 → a * b = a * c → b = c)
4456

57+
section is_left_cancel_mul_zero
58+
59+
variables [has_mul M₀] [has_zero M₀] [is_left_cancel_mul_zero M₀] {a b c : M₀}
60+
61+
lemma mul_left_cancel₀ (ha : a ≠ 0) (h : a * b = a * c) : b = c :=
62+
is_left_cancel_mul_zero.mul_left_cancel_of_ne_zero ha h
63+
64+
lemma mul_right_injective₀ (ha : a ≠ 0) : function.injective ((*) a) :=
65+
λ b c, mul_left_cancel₀ ha
66+
67+
end is_left_cancel_mul_zero
68+
4569
/-- A mixin for right cancellative multiplication by nonzero elements. -/
4670
@[protect_proj] class is_right_cancel_mul_zero (M₀ : Type u) [has_mul M₀] [has_zero M₀] : Prop :=
4771
(mul_right_cancel_of_ne_zero : ∀ {a b c : M₀}, b ≠ 0 → a * b = c * b → a = c)
4872

73+
section is_right_cancel_mul_zero
74+
75+
variables [has_mul M₀] [has_zero M₀] [is_right_cancel_mul_zero M₀] {a b c : M₀}
76+
77+
lemma mul_right_cancel₀ (hb : b ≠ 0) (h : a * b = c * b) : a = c :=
78+
is_right_cancel_mul_zero.mul_right_cancel_of_ne_zero hb h
79+
80+
lemma mul_left_injective₀ (hb : b ≠ 0) : function.injective (λ a, a * b) :=
81+
λ a c, mul_right_cancel₀ hb
82+
83+
end is_right_cancel_mul_zero
84+
4985
/-- A mixin for cancellative multiplication by nonzero elements. -/
5086
@[protect_proj] class is_cancel_mul_zero (M₀ : Type u) [has_mul M₀] [has_zero M₀]
5187
extends is_left_cancel_mul_zero M₀, is_right_cancel_mul_zero M₀ : Prop
5288

53-
section mul_zero_class
89+
section comm_semigroup_with_zero
5490

55-
variables [mul_zero_class M₀] {a b : M₀}
91+
variables [comm_semigroup M₀] [has_zero M₀]
5692

57-
@[ematch, simp] lemma zero_mul (a : M₀) : 0 * a = 0 :=
58-
mul_zero_class.zero_mul a
93+
lemma is_left_cancel_mul_zero.to_is_right_cancel_mul_zero [is_left_cancel_mul_zero M₀] :
94+
is_right_cancel_mul_zero M₀ :=
95+
⟨λ a b c ha h, mul_left_cancel₀ ha $ (mul_comm _ _).trans $ (h.trans (mul_comm _ _))⟩
5996

60-
@[ematch, simp] lemma mul_zero (a : M₀) : a * 0 = 0 :=
61-
mul_zero_class.mul_zero a
97+
lemma is_right_cancel_mul_zero.to_is_left_cancel_mul_zero [is_right_cancel_mul_zero M₀] :
98+
is_left_cancel_mul_zero M₀ :=
99+
⟨λ a b c ha h, mul_right_cancel₀ ha $ (mul_comm _ _).trans $ (h.trans (mul_comm _ _))⟩
62100

63-
end mul_zero_class
101+
lemma is_left_cancel_mul_zero.to_is_cancel_mul_zero [is_left_cancel_mul_zero M₀] :
102+
is_cancel_mul_zero M₀ :=
103+
{ .. ‹is_left_cancel_mul_zero M₀›, .. is_left_cancel_mul_zero.to_is_right_cancel_mul_zero }
104+
105+
lemma is_right_cancel_mul_zero.to_is_cancel_mul_zero [is_right_cancel_mul_zero M₀] :
106+
is_cancel_mul_zero M₀ :=
107+
{ .. ‹is_right_cancel_mul_zero M₀›, .. is_right_cancel_mul_zero.to_is_left_cancel_mul_zero }
108+
109+
end comm_semigroup_with_zero
64110

65111
/-- Predicate typeclass for expressing that `a * b = 0` implies `a = 0` or `b = 0`
66112
for all `a` and `b` of type `G₀`. -/
@@ -97,31 +143,11 @@ class cancel_monoid_with_zero (M₀ : Type*) extends monoid_with_zero M₀ :=
97143
(mul_left_cancel_of_ne_zero : ∀ {a b c : M₀}, a ≠ 0 → a * b = a * c → b = c)
98144
(mul_right_cancel_of_ne_zero : ∀ {a b c : M₀}, b ≠ 0 → a * b = c * b → a = c)
99145

100-
section cancel_monoid_with_zero
101-
102-
variables [cancel_monoid_with_zero M₀] {a b c : M₀}
103-
104-
lemma mul_left_cancel₀ (ha : a ≠ 0) (h : a * b = a * c) : b = c :=
105-
cancel_monoid_with_zero.mul_left_cancel_of_ne_zero ha h
106-
107-
lemma mul_right_cancel₀ (hb : b ≠ 0) (h : a * b = c * b) : a = c :=
108-
cancel_monoid_with_zero.mul_right_cancel_of_ne_zero hb h
109-
110-
lemma mul_right_injective₀ (ha : a ≠ 0) : function.injective ((*) a) :=
111-
λ b c, mul_left_cancel₀ ha
112-
113-
lemma mul_left_injective₀ (hb : b ≠ 0) : function.injective (λ a, a * b) :=
114-
λ a c, mul_right_cancel₀ hb
115-
116146
/-- A `cancel_monoid_with_zero` satisfies `is_cancel_mul_zero`. -/
117147
@[priority 100]
118-
instance cancel_monoid_with_zero.to_is_cancel_mul_zero : is_cancel_mul_zero M₀ :=
119-
{ mul_left_cancel_of_ne_zero := λ a b c ha h,
120-
cancel_monoid_with_zero.mul_left_cancel_of_ne_zero ha h,
121-
mul_right_cancel_of_ne_zero := λ a b c hb h,
122-
cancel_monoid_with_zero.mul_right_cancel_of_ne_zero hb h, }
123-
124-
end cancel_monoid_with_zero
148+
instance cancel_monoid_with_zero.to_is_cancel_mul_zero [cancel_monoid_with_zero M₀] :
149+
is_cancel_mul_zero M₀ :=
150+
{ .. ‹cancel_monoid_with_zero M₀› }
125151

126152
/-- A type `M` is a commutative “monoid with zero” if it is a commutative monoid with zero
127153
element, and `0` is left and right absorbing. -/
@@ -132,8 +158,13 @@ class comm_monoid_with_zero (M₀ : Type*) extends comm_monoid M₀, monoid_with
132158
`0` is left and right absorbing,
133159
and left/right multiplication by a non-zero element is injective. -/
134160
@[protect_proj, ancestor comm_monoid_with_zero cancel_monoid_with_zero]
135-
class cancel_comm_monoid_with_zero (M₀ : Type*) extends
136-
comm_monoid_with_zero M₀, cancel_monoid_with_zero M₀.
161+
class cancel_comm_monoid_with_zero (M₀ : Type*) extends comm_monoid_with_zero M₀ :=
162+
(mul_left_cancel_of_ne_zero : ∀ {a b c : M₀}, a ≠ 0 → a * b = a * c → b = c)
163+
164+
@[priority 100]
165+
instance cancel_comm_monoid_with_zero.to_cancel_monoid_with_zero
166+
[h : cancel_comm_monoid_with_zero M₀] : cancel_monoid_with_zero M₀ :=
167+
{ .. h, .. @is_left_cancel_mul_zero.to_is_right_cancel_mul_zero M₀ _ _ { .. h } }
137168

138169
/-- A type `G₀` is a “group with zero” if it is a monoid with zero element (distinct from `1`)
139170
such that every nonzero element is invertible.
@@ -147,42 +178,6 @@ class group_with_zero (G₀ : Type u) extends
147178
(inv_zero : (0 : G₀)⁻¹ = 0)
148179
(mul_inv_cancel : ∀ a:G₀, a ≠ 0 → a * a⁻¹ = 1)
149180

150-
namespace comm_monoid_with_zero
151-
152-
variable [comm_monoid_with_zero M₀]
153-
154-
lemma is_left_cancel_mul_zero.to_is_right_cancel_mul_zero [is_left_cancel_mul_zero M₀] :
155-
is_right_cancel_mul_zero M₀ :=
156-
{ mul_right_cancel_of_ne_zero := λ a b c ha h,
157-
begin
158-
rw [mul_comm, mul_comm c] at h,
159-
exact is_left_cancel_mul_zero.mul_left_cancel_of_ne_zero ha h,
160-
end }
161-
162-
lemma is_right_cancel_mul_zero.to_is_left_cancel_mul_zero [is_right_cancel_mul_zero M₀] :
163-
is_left_cancel_mul_zero M₀ :=
164-
{ mul_left_cancel_of_ne_zero := λ a b c ha h,
165-
begin
166-
rw [mul_comm a, mul_comm a c] at h,
167-
exact is_right_cancel_mul_zero.mul_right_cancel_of_ne_zero ha h,
168-
end }
169-
170-
lemma is_left_cancel_mul_zero.to_is_cancel_mul_zero [is_left_cancel_mul_zero M₀] :
171-
is_cancel_mul_zero M₀ :=
172-
{ mul_left_cancel_of_ne_zero := λ _ _ _,
173-
is_left_cancel_mul_zero.mul_left_cancel_of_ne_zero,
174-
mul_right_cancel_of_ne_zero := λ _ _ _,
175-
is_left_cancel_mul_zero.to_is_right_cancel_mul_zero.mul_right_cancel_of_ne_zero }
176-
177-
lemma is_right_cancel_mul_zero.to_is_cancel_mul_zero [is_right_cancel_mul_zero M₀] :
178-
is_cancel_mul_zero M₀ :=
179-
{ mul_left_cancel_of_ne_zero := λ _ _ _,
180-
is_right_cancel_mul_zero.to_is_left_cancel_mul_zero.mul_left_cancel_of_ne_zero,
181-
mul_right_cancel_of_ne_zero := λ _ _ _,
182-
is_right_cancel_mul_zero.mul_right_cancel_of_ne_zero }
183-
184-
end comm_monoid_with_zero
185-
186181
section group_with_zero
187182

188183
variables [group_with_zero G₀]

src/algebra/ring/regular.lean

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,12 @@ section is_domain
7171
@[priority 100] -- see Note [lower instance priority]
7272
instance is_domain.to_cancel_monoid_with_zero [semiring α] [is_domain α] :
7373
cancel_monoid_with_zero α :=
74-
{ mul_left_cancel_of_ne_zero := λ a b c ha h,
75-
is_cancel_mul_zero.mul_left_cancel_of_ne_zero ha h,
76-
mul_right_cancel_of_ne_zero := λ a b c ha h,
77-
is_cancel_mul_zero.mul_right_cancel_of_ne_zero ha h,
78-
.. semiring.to_monoid_with_zero α }
74+
{ .. semiring.to_monoid_with_zero α, .. ‹is_domain α› }
7975

8076
variables [comm_semiring α] [is_domain α]
8177

8278
@[priority 100] -- see Note [lower instance priority]
8379
instance is_domain.to_cancel_comm_monoid_with_zero : cancel_comm_monoid_with_zero α :=
84-
{ mul_left_cancel_of_ne_zero := λ a b c ha H, is_domain.mul_left_cancel_of_ne_zero ha H,
85-
mul_right_cancel_of_ne_zero := λ a b c hb H, is_domain.mul_right_cancel_of_ne_zero hb H,
86-
.. (infer_instance : comm_semiring α) }
80+
{ .. ‹comm_semiring α›, .. ‹is_domain α› }
8781

8882
end is_domain

src/data/nat/basic.lean

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,10 @@ instance : semiring ℕ := infer_instance
7979

8080
protected lemma nat.nsmul_eq_mul (m n : ℕ) : m • n = m * n := rfl
8181

82-
theorem nat.eq_of_mul_eq_mul_right {n m k : ℕ} (Hm : 0 < m) (H : n * m = k * m) : n = k :=
83-
by rw [mul_comm n m, mul_comm k m] at H; exact nat.eq_of_mul_eq_mul_left Hm H
84-
8582
instance nat.cancel_comm_monoid_with_zero : cancel_comm_monoid_with_zero ℕ :=
8683
{ mul_left_cancel_of_ne_zero :=
8784
λ _ _ _ h1 h2, nat.eq_of_mul_eq_mul_left (nat.pos_of_ne_zero h1) h2,
88-
mul_right_cancel_of_ne_zero :=
89-
λ _ _ _ h1 h2, nat.eq_of_mul_eq_mul_right (nat.pos_of_ne_zero h1) h2,
90-
.. (infer_instance : comm_monoid_with_zero ℕ) }
85+
.. nat.comm_semiring }
9186

9287
attribute [simp] nat.not_lt_zero nat.succ_ne_zero nat.succ_ne_self
9388
nat.zero_ne_one nat.one_ne_zero

src/data/nat/choose/basic.lean

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,8 @@ end
124124
lemma choose_mul {n k s : ℕ} (hkn : k ≤ n) (hsk : s ≤ k) :
125125
n.choose k * k.choose s = n.choose s * (n - s).choose (k - s) :=
126126
begin
127-
have h : 0 < (n - k)! * (k - s)! * s! :=
128-
mul_pos (mul_pos (factorial_pos _) (factorial_pos _)) (factorial_pos _),
129-
refine eq_of_mul_eq_mul_right h _,
127+
have h : (n - k)! * (k - s)! * s! ≠ 0, by apply_rules [mul_ne_zero, factorial_ne_zero],
128+
refine mul_right_cancel₀ h _,
130129
calc
131130
n.choose k * k.choose s * ((n - k)! * (k - s)! * s!)
132131
= n.choose k * (k.choose s * s! * (k - s)!) * (n - k)!

src/data/nat/gcd/basic.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ end
104104

105105
theorem gcd_div {m n k : ℕ} (H1 : k ∣ m) (H2 : k ∣ n) :
106106
gcd (m / k) (n / k) = gcd m n / k :=
107-
or.elim (nat.eq_zero_or_pos k)
107+
(decidable.eq_or_ne k 0).elim
108108
(λk0, by rw [k0, nat.div_zero, nat.div_zero, nat.div_zero, gcd_zero_right])
109-
(λH3, nat.eq_of_mul_eq_mul_right H3 $ by rw [
109+
(λH3, mul_right_cancel₀ H3 $ by rw [
110110
nat.div_mul_cancel (dvd_gcd H1 H2), ←gcd_mul_right,
111111
nat.div_mul_cancel H1, nat.div_mul_cancel H2])
112112

src/data/rat/defs.lean

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ begin
162162
all_goals { exact int.coe_nat_dvd.2 (nat.gcd_dvd_left _ _) } },
163163
intros a c h,
164164
suffices bd : b / a.gcd b = d / c.gcd d,
165-
{ refine ⟨_, bd⟩,
166-
apply nat.eq_of_mul_eq_mul_left hb,
165+
{ refine ⟨mul_left_cancel₀ hb.ne' _, bd⟩,
167166
rw [← nat.mul_div_assoc _ (nat.gcd_dvd_left _ _), mul_comm,
168167
nat.mul_div_assoc _ (nat.gcd_dvd_right _ _), bd,
169168
← nat.mul_div_assoc _ (nat.gcd_dvd_right _ _), h, mul_comm,

src/number_theory/cyclotomic/primitive_roots.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,12 @@ begin
362362
pnat.pow_coe, pnat.pow_coe, nat.totient_prime_pow hpri.out (k - s).succ_pos,
363363
nat.totient_prime_pow hpri.out k.succ_pos, mul_comm _ (↑p - 1), mul_assoc,
364364
mul_comm (↑p ^ (k.succ - 1))] at this,
365-
replace this := nat.eq_of_mul_eq_mul_left (tsub_pos_iff_lt.2 (nat.prime.one_lt hpri.out)) this,
365+
replace this := mul_left_cancel₀ (tsub_pos_iff_lt.2 hpri.out.one_lt).ne' this,
366366
have Hex : k.succ - 1 = (k - s).succ - 1 + s,
367367
{ simp only [nat.succ_sub_succ_eq_sub, tsub_zero],
368368
exact (nat.sub_add_cancel hs).symm },
369369
rw [Hex, pow_add] at this,
370-
exact nat.eq_of_mul_eq_mul_left (pow_pos hpri.out.pos _) this },
370+
exact mul_left_cancel₀ (pow_ne_zero _ hpri.out.ne_zero) this },
371371
all_goals { apply_instance }
372372
end
373373

0 commit comments

Comments
 (0)