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

Commit f92fd0d

Browse files
committed
refactor(algebra/divisibility, associated): generalize instances in divisibility, associated (#3714)
generalizes the divisibility relation to noncommutative monoids adds missing headers to algebra/divisibility generalizes the instances in many of the lemmas in algebra/associated reunites (some of the) divisibility API for ordinals with general monoids Co-authored-by: Aaron Anderson <65780815+awainverse@users.noreply.github.com>
1 parent 57df7f5 commit f92fd0d

21 files changed

+246
-216
lines changed

src/algebra/associated.lean

Lines changed: 56 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Johannes Hölzl, Jens Wagemaker
55
-/
66
import data.multiset.basic
7+
import algebra.divisibility
78

89
/-!
910
# Associated, prime, and irreducible elements.
@@ -14,36 +15,23 @@ variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
1415
lemma is_unit_pow [monoid α] {a : α} (n : ℕ) : is_unit a → is_unit (a ^ n) :=
1516
λ ⟨u, hu⟩, ⟨u ^ n, by simp *⟩
1617

17-
theorem is_unit_iff_dvd_one [comm_semiring α] {x : α} : is_unit x ↔ x ∣ 1 :=
18+
theorem is_unit_iff_dvd_one [comm_monoid α] {x : α} : is_unit x ↔ x ∣ 1 :=
1819
by rintro ⟨u, rfl⟩; exact ⟨_, u.mul_inv.symm⟩,
1920
λ ⟨y, h⟩, ⟨⟨x, y, h.symm, by rw [h, mul_comm]⟩, rfl⟩⟩
2021

21-
theorem is_unit_iff_forall_dvd [comm_semiring α] {x : α} :
22+
theorem is_unit_iff_forall_dvd [comm_monoid α] {x : α} :
2223
is_unit x ↔ ∀ y, x ∣ y :=
2324
is_unit_iff_dvd_one.trans ⟨λ h y, dvd.trans h (one_dvd _), λ h, h _⟩
2425

25-
theorem mul_dvd_of_is_unit_left [comm_semiring α] {x y z : α} (h : is_unit x) : x * y ∣ z ↔ y ∣ z :=
26-
⟨dvd_trans (dvd_mul_left _ _),
27-
dvd_trans $ by simpa using mul_dvd_mul_right (is_unit_iff_dvd_one.1 h) y⟩
28-
29-
theorem mul_dvd_of_is_unit_right [comm_semiring α] {x y z : α} (h : is_unit y) : x * y ∣ z ↔ x ∣ z :=
30-
by rw [mul_comm, mul_dvd_of_is_unit_left h]
31-
32-
@[simp] lemma unit_mul_dvd_iff [comm_semiring α] {a b : α} {u : units α} : (u : α) * a ∣ b ↔ a ∣ b :=
33-
mul_dvd_of_is_unit_left (is_unit_unit _)
34-
35-
lemma mul_unit_dvd_iff [comm_semiring α] {a b : α} {u : units α} : a * u ∣ b ↔ a ∣ b :=
36-
units.coe_mul_dvd _ _ _
37-
38-
theorem is_unit_of_dvd_unit {α} [comm_semiring α] {x y : α}
26+
theorem is_unit_of_dvd_unit {α} [comm_monoid α] {x y : α}
3927
(xy : x ∣ y) (hu : is_unit y) : is_unit x :=
4028
is_unit_iff_dvd_one.2 $ dvd_trans xy $ is_unit_iff_dvd_one.1 hu
4129

4230
theorem is_unit_int {n : ℤ} : is_unit n ↔ n.nat_abs = 1 :=
4331
begin rintro ⟨u, rfl⟩, exact (int.units_eq_one_or u).elim (by simp) (by simp) end,
4432
λ h, is_unit_iff_dvd_one.2 ⟨n, by rw [← int.nat_abs_mul_self, h]; refl⟩⟩
4533

46-
lemma is_unit_of_dvd_one [comm_semiring α] : ∀a ∣ 1, is_unit (a:α)
34+
lemma is_unit_of_dvd_one [comm_monoid α] : ∀a ∣ 1, is_unit (a:α)
4735
| a ⟨b, eq⟩ := ⟨units.mk_of_mul_eq_one a b eq.symm, rfl⟩
4836

4937
lemma dvd_and_not_dvd_iff [integral_domain α] {x y : α} :
@@ -58,29 +46,33 @@ lemma pow_dvd_pow_iff [integral_domain α] {x : α} {n m : ℕ} (h0 : x ≠ 0) (
5846
begin
5947
split,
6048
{ intro h, rw [← not_lt], intro hmn, apply h1,
61-
have : x * x ^ m 1 * x ^ m,
62-
{ rw [← pow_succ, one_mul], exact dvd_trans (pow_dvd_pow _ (nat.succ_le_of_lt hmn)) h },
63-
rwa [mul_dvd_mul_iff_right, ← is_unit_iff_dvd_one] at this, apply pow_ne_zero m h0 },
49+
have : x ^ m * x ∣ x ^ m * 1,
50+
{ rw [← pow_succ', mul_one], exact dvd_trans (pow_dvd_pow _ (nat.succ_le_of_lt hmn)) h },
51+
rwa [mul_dvd_mul_iff_left, ← is_unit_iff_dvd_one] at this, apply pow_ne_zero m h0 },
6452
{ apply pow_dvd_pow }
6553
end
6654

67-
/-- prime element of a semiring -/
68-
def prime [comm_semiring α] (p : α) : Prop :=
55+
section prime
56+
variables [comm_monoid_with_zero α]
57+
58+
/-- prime element of a `comm_monoid_with_zero` -/
59+
def prime (p : α) : Prop :=
6960
p ≠ 0 ∧ ¬ is_unit p ∧ (∀a b, p ∣ a * b → p ∣ a ∨ p ∣ b)
7061

7162
namespace prime
63+
variables {p : α} (hp : prime p)
7264

73-
lemma ne_zero [comm_semiring α] {p : α} (hp : prime p) : p ≠ 0 :=
65+
lemma ne_zero (hp : prime p) : p ≠ 0 :=
7466
hp.1
7567

76-
lemma not_unit [comm_semiring α] {p : α} (hp : prime p) : ¬ is_unit p :=
68+
lemma not_unit (hp : prime p) : ¬ is_unit p :=
7769
hp.2.1
7870

79-
lemma div_or_div [comm_semiring α] {p : α} (hp : prime p) {a b : α} (h : p ∣ a * b) :
71+
lemma div_or_div (hp : prime p) {a b : α} (h : p ∣ a * b) :
8072
p ∣ a ∨ p ∣ b :=
8173
hp.2.2 a b h
8274

83-
lemma dvd_of_dvd_pow [comm_semiring α] {p : α} (hp : prime p) {a : α} {n : ℕ} (h : p ∣ a^n) :
75+
lemma dvd_of_dvd_pow (hp : prime p) {a : α} {n : ℕ} (h : p ∣ a^n) :
8476
p ∣ a :=
8577
begin
8678
induction n with n ih,
@@ -96,13 +88,13 @@ end
9688

9789
end prime
9890

99-
@[simp] lemma not_prime_zero [comm_semiring α] : ¬ prime (0 : α) :=
91+
@[simp] lemma not_prime_zero : ¬ prime (0 : α) :=
10092
λ h, h.ne_zero rfl
10193

102-
@[simp] lemma not_prime_one [comm_semiring α] : ¬ prime (1 : α) :=
94+
@[simp] lemma not_prime_one : ¬ prime (1 : α) :=
10395
λ h, h.not_unit is_unit_one
10496

105-
lemma exists_mem_multiset_dvd_of_prime [comm_semiring α] {s : multiset α} {p : α} (hp : prime p) :
97+
lemma exists_mem_multiset_dvd_of_prime {s : multiset α} {p : α} (hp : prime p) :
10698
p ∣ s.prod → ∃a∈s, p ∣ a :=
10799
multiset.induction_on s (assume h, (hp.not_unit $ is_unit_of_dvd_one _ h).elim) $
108100
assume a s ih h,
@@ -112,6 +104,8 @@ assume a s ih h,
112104
| or.inr h := let ⟨a, has, h⟩ := ih h in ⟨a, multiset.mem_cons_of_mem has, h⟩
113105
end
114106

107+
end prime
108+
115109
/-- `irreducible p` states that `p` is non-unit and only factors into units.
116110
117111
We explicitly avoid stating that `p` is non-zero, this would require a semiring. Assuming only a
@@ -134,11 +128,11 @@ end irreducible
134128
@[simp] theorem not_irreducible_one [monoid α] : ¬ irreducible (1 : α) :=
135129
by simp [irreducible]
136130

137-
@[simp] theorem not_irreducible_zero [semiring α] : ¬ irreducible (0 : α)
131+
@[simp] theorem not_irreducible_zero [monoid_with_zero α] : ¬ irreducible (0 : α)
138132
| ⟨hn0, h⟩ := have is_unit (0:α) ∨ is_unit (0:α), from h 0 0 ((mul_zero 0).symm),
139133
this.elim hn0 hn0
140134

141-
theorem irreducible.ne_zero [semiring α] : ∀ {p:α}, irreducible p → p ≠ 0
135+
theorem irreducible.ne_zero [monoid_with_zero α] : ∀ {p:α}, irreducible p → p ≠ 0
142136
| _ hp rfl := not_irreducible_zero hp
143137

144138
theorem of_irreducible_mul {α} [monoid α] {x y : α} :
@@ -179,17 +173,15 @@ have hpd : p ∣ x * y, from ⟨z, by rwa [mul_right_inj' hp0] at h⟩,
179173
(λ ⟨d, hd⟩, or.inr ⟨d, by simp [*, pow_succ, mul_comm, mul_left_comm, mul_assoc]⟩)
180174

181175
/-- If `p` and `q` are irreducible, then `p ∣ q` implies `q ∣ p`. -/
182-
lemma dvd_symm_of_irreducible [comm_semiring α] {p q : α}
176+
lemma dvd_symm_of_irreducible [monoid α] {p q : α}
183177
(hp : irreducible p) (hq : irreducible q) : p ∣ q → q ∣ p :=
184178
begin
185179
tactic.unfreeze_local_instances,
186180
rintros ⟨q', rfl⟩,
187-
exact is_unit.mul_right_dvd_of_dvd
188-
(or.resolve_left (of_irreducible_mul hq) hp.not_unit)
189-
(dvd_refl p)
181+
rw is_unit.mul_right_dvd (or.resolve_left (of_irreducible_mul hq) hp.not_unit),
190182
end
191183

192-
lemma dvd_symm_iff_of_irreducible [comm_semiring α] {p q : α}
184+
lemma dvd_symm_iff_of_irreducible [monoid α] {p q : α}
193185
(hp : irreducible p) (hq : irreducible q) : p ∣ q ↔ q ∣ p :=
194186
⟨dvd_symm_of_irreducible hp hq, dvd_symm_of_irreducible hq hp⟩
195187

@@ -239,25 +231,28 @@ lemma associated_mul_mul [comm_monoid α] {a₁ a₂ b₁ b₂ : α} :
239231
a₁ ~ᵤ b₁ → a₂ ~ᵤ b₂ → (a₁ * a₂) ~ᵤ (b₁ * b₂)
240232
| ⟨c₁, h₁⟩ ⟨c₂, h₂⟩ := ⟨c₁ * c₂, by simp [h₁.symm, h₂.symm, mul_assoc, mul_comm, mul_left_comm]⟩
241233

242-
lemma dvd_of_associated [comm_ring α] {a b : α} : a ~ᵤ b → a ∣ b := λ ⟨u, hu⟩, ⟨u, hu.symm⟩
234+
lemma dvd_of_associated [monoid α] {a b : α} : a ~ᵤ b → a ∣ b := λ ⟨u, hu⟩, ⟨u, hu.symm⟩
243235

244-
lemma dvd_dvd_of_associated [comm_ring α] {a b : α} (h : a ~ᵤ b) : a ∣ b ∧ b ∣ a :=
236+
lemma dvd_dvd_of_associated [monoid α] {a b : α} (h : a ~ᵤ b) : a ∣ b ∧ b ∣ a :=
245237
⟨dvd_of_associated h, dvd_of_associated h.symm⟩
246238

247-
theorem associated_of_dvd_dvd [integral_domain α] {a b : α} (hab : a ∣ b) (hba : b ∣ a) : a ~ᵤ b :=
239+
theorem associated_of_dvd_dvd [cancel_monoid_with_zero α]
240+
{a b : α} (hab : a ∣ b) (hba : b ∣ a) : a ~ᵤ b :=
248241
begin
249-
haveI := classical.dec_eq α,
250242
rcases hab with ⟨c, rfl⟩,
251243
rcases hba with ⟨d, a_eq⟩,
252244
by_cases ha0 : a = 0,
253245
{ simp [*] at * },
254-
have : a * 1 = a * (c * d),
255-
{ simpa [mul_assoc] using a_eq },
256-
have : 1 = (c * d), from mul_left_cancel' ha0 this,
257-
exact ⟨units.mk_of_mul_eq_one c d (this.symm), by rw [units.mk_of_mul_eq_one, units.val_coe]⟩
246+
have hac0 : a * c ≠ 0,
247+
{ intro con, rw [con, zero_mul] at a_eq, apply ha0 a_eq, },
248+
have : a * (c * d) = a * 1 := by rw [← mul_assoc, ← a_eq, mul_one],
249+
have hcd : (c * d) = 1, from mul_left_cancel' ha0 this,
250+
have : a * c * (d * c) = a * c * 1 := by rw [← mul_assoc, ← a_eq, mul_one],
251+
have hdc : d * c = 1, from mul_left_cancel' hac0 this,
252+
exact ⟨⟨c, d, hcd, hdc⟩, rfl⟩
258253
end
259254

260-
theorem dvd_dvd_iff_associated [integral_domain α] {a b : α} : a ∣ b ∧ b ∣ a ↔ a ~ᵤ b :=
255+
theorem dvd_dvd_iff_associated [cancel_monoid_with_zero α] {a b : α} : a ∣ b ∧ b ∣ a ↔ a ~ᵤ b :=
261256
⟨λ ⟨h1, h2⟩, associated_of_dvd_dvd h1 h2, dvd_dvd_of_associated⟩
262257

263258
lemma exists_associated_mem_of_dvd_prod [integral_domain α] {p : α}
@@ -275,14 +270,11 @@ multiset.induction_on s (by simp [mt is_unit_iff_dvd_one.2 hp.not_unit])
275270
exact ⟨q, multiset.mem_cons.2 (or.inr hq₁), hq₂⟩ }
276271
end)
277272

278-
lemma dvd_iff_dvd_of_rel_left [comm_semiring α] {a b c : α} (h : a ~ᵤ b) : a ∣ c ↔ b ∣ c :=
279-
let ⟨u, hu⟩ := h in hu ▸ mul_unit_dvd_iff.symm
280-
281-
lemma dvd_mul_unit_iff [comm_semiring α] {a b : α} {u : units α} : a ∣ b * u ↔ a ∣ b :=
282-
units.dvd_coe_mul _ _ _
273+
lemma dvd_iff_dvd_of_rel_left [comm_monoid_with_zero α] {a b c : α} (h : a ~ᵤ b) : a ∣ c ↔ b ∣ c :=
274+
let ⟨u, hu⟩ := h in hu ▸ units.mul_right_dvd.symm
283275

284276
lemma dvd_iff_dvd_of_rel_right [comm_semiring α] {a b c : α} (h : b ~ᵤ c) : a ∣ b ↔ a ∣ c :=
285-
let ⟨u, hu⟩ := h in hu ▸ dvd_mul_unit_iff.symm
277+
let ⟨u, hu⟩ := h in hu ▸ units.dvd_mul_right.symm
286278

287279
lemma eq_zero_iff_of_associated [comm_semiring α] {a b : α} (h : a ~ᵤ b) : a = 0 ↔ b = 0 :=
288280
⟨λ ha, let ⟨u, hu⟩ := h in by simp [hu.symm, ha],
@@ -295,7 +287,7 @@ lemma prime_of_associated [comm_semiring α] {p q : α} (h : p ~ᵤ q) (hp : pri
295287
⟨(ne_zero_iff_of_associated h).1 hp.ne_zero,
296288
let ⟨u, hu⟩ := h in
297289
⟨λ ⟨v, hv⟩, hp.not_unit ⟨v * u⁻¹, by simp [hv, hu.symm]⟩,
298-
hu ▸ by { simp [mul_unit_dvd_iff], intros a b, exact hp.div_or_div }⟩⟩
290+
hu ▸ by { simp [units.mul_right_dvd], intros a b, exact hp.div_or_div }⟩⟩
299291

300292
lemma prime_iff_of_associated [comm_semiring α] {p q : α}
301293
(h : p ~ᵤ q) : prime p ↔ prime q :=
@@ -386,11 +378,9 @@ instance : comm_monoid (associates α) :=
386378
assume a b, show ⟦a * b⟧ = ⟦b * a⟧, by rw [mul_comm] }
387379

388380
instance : preorder (associates α) :=
389-
{ le := λa b, ∃c, a * c = b,
390-
le_refl := assume a, ⟨1, by simp⟩,
391-
le_trans := assume a b c ⟨f₁, h₁⟩ ⟨f₂, h₂⟩, ⟨f₁ * f₂, h₂ ▸ h₁ ▸ (mul_assoc _ _ _).symm⟩}
392-
393-
instance : has_dvd (associates α) := ⟨(≤)⟩
381+
{ le := has_dvd.dvd,
382+
le_refl := dvd_refl,
383+
le_trans := λ a b c, dvd_trans}
394384

395385
@[simp] lemma mk_one : associates.mk (1 : α) = 1 := rfl
396386

@@ -439,10 +429,10 @@ section order
439429
theorem mul_mono {a b c d : associates α} (h₁ : a ≤ b) (h₂ : c ≤ d) :
440430
a * c ≤ b * d :=
441431
let ⟨x, hx⟩ := h₁, ⟨y, hy⟩ := h₂ in
442-
⟨x * y, by simp [hx.symm, hy.symm, mul_comm, mul_assoc, mul_left_comm]⟩
432+
⟨x * y, by simp [hx, hy, mul_comm, mul_assoc, mul_left_comm]⟩
443433

444434
theorem one_le {a : associates α} : 1 ≤ a :=
445-
⟨a, one_mul a
435+
dvd.intro _ (one_mul a)
446436

447437
theorem prod_le_prod {p q : multiset (associates α)} (h : p ≤ q) : p.prod ≤ q.prod :=
448438
begin
@@ -493,9 +483,9 @@ variables [comm_semiring α]
493483
theorem dvd_of_mk_le_mk {a b : α} : associates.mk a ≤ associates.mk b → a ∣ b
494484
| ⟨c', hc'⟩ := (quotient.induction_on c' $ assume c hc,
495485
let ⟨d, hd⟩ := (quotient.exact hc).symm in
496-
⟨(↑d⁻¹) * c,
497-
calc b = (a * c) * ↑d⁻¹ : by rw [← hd, mul_assoc, units.mul_inv, mul_one]
498-
... = a * (↑d⁻¹ * c) : by ac_refl⟩) hc'
486+
⟨(↑d) * c,
487+
calc b = (a * c) * ↑d : hd.symm
488+
... = a * (↑d * c) : by ac_refl⟩) hc'
499489

500490
theorem mk_le_mk_of_dvd {a b : α} : a ∣ b → associates.mk a ≤ associates.mk b :=
501491
assume ⟨c, hc⟩, ⟨associates.mk c, by simp [hc]; refl⟩
@@ -518,7 +508,7 @@ hp.2.2 a b h
518508
lemma exists_mem_multiset_le_of_prime {s : multiset (associates α)} {p : associates α}
519509
(hp : prime p) :
520510
p ≤ s.prod → ∃a∈s, p ≤ a :=
521-
multiset.induction_on s (assume ⟨d, eq⟩, (hp.ne_one (mul_eq_one_iff.1 eq).1).elim) $
511+
multiset.induction_on s (assume ⟨d, eq⟩, (hp.ne_one (mul_eq_one_iff.1 eq.symm).1).elim) $
522512
assume a s ih h,
523513
have p ≤ a * s.prod, by simpa using h,
524514
match hp.le_or_le this with
@@ -549,13 +539,8 @@ section integral_domain
549539
variable [integral_domain α]
550540

551541
instance : partial_order (associates α) :=
552-
{ le_antisymm := assume a' b',
553-
quotient.induction_on₂ a' b' $ assume a b ⟨f₁', h₁⟩ ⟨f₂', h₂⟩,
554-
(quotient.induction_on₂ f₁' f₂' $ assume f₁ f₂ h₁ h₂,
555-
let ⟨c₁, h₁⟩ := quotient.exact h₁, ⟨c₂, h₂⟩ := quotient.exact h₂ in
556-
quotient.sound $ associated_of_dvd_dvd
557-
(h₁ ▸ dvd_mul_of_dvd_left (dvd_mul_right _ _) _)
558-
(h₂ ▸ dvd_mul_of_dvd_left (dvd_mul_right _ _) _)) h₁ h₂
542+
{ le_antisymm := λ a' b', quotient.induction_on₂ a' b' (λ a b hab hba,
543+
quot.sound $ associated_of_dvd_dvd (dvd_of_mk_le_mk hab) (dvd_of_mk_le_mk hba))
559544
.. associates.preorder }
560545

561546
instance : order_bot (associates α) :=
@@ -565,7 +550,7 @@ instance : order_bot (associates α) :=
565550

566551
instance : order_top (associates α) :=
567552
{ top := 0,
568-
le_top := assume a, ⟨0, mul_zero a⟩,
553+
le_top := assume a, ⟨0, (mul_zero a).symm⟩,
569554
.. associates.partial_order }
570555

571556
instance : no_zero_divisors (associates α) :=

0 commit comments

Comments
 (0)