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

Commit df0161a

Browse files
Chris HughesChris Hughes
authored andcommitted
refactor(unique_factorization_domain): simplify definition of UFD
1 parent 2e63635 commit df0161a

File tree

4 files changed

+233
-3
lines changed

4 files changed

+233
-3
lines changed

data/multiset.lean

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,9 @@ lemma prod_hom [comm_monoid α] [comm_monoid β] (f : α → β) [is_monoid_hom
774774
multiset.induction_on s (by simp [is_monoid_hom.map_one f])
775775
(by simp [is_monoid_hom.map_mul f] {contextual := tt})
776776

777+
lemma dvd_prod [comm_semiring α] {a : α} {s : multiset α} : a ∈ s → a ∣ s.prod :=
778+
quotient.induction_on s (λ l a h, by simpa using list.dvd_prod h) a
779+
777780
lemma sum_hom [add_comm_monoid α] [add_comm_monoid β] (f : α → β) [is_add_monoid_hom f] (s : multiset α) :
778781
(s.map f).sum = f s.sum :=
779782
multiset.induction_on s (by simp [is_add_monoid_hom.map_zero f])

ring_theory/associated.lean

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ open lattice
1313
/-- is unit -/
1414
def is_unit [monoid α] (a : α) : Prop := ∃u:units α, a = u
1515

16+
@[simp] lemma is_unit_unit [monoid α] (u : units α) : is_unit (u : α) := ⟨u, rfl⟩
17+
1618
@[simp] theorem is_unit_zero_iff [semiring α] : is_unit (0 : α) ↔ (0:α) = 1 :=
1719
⟨λ ⟨⟨_, a, (a0 : 0 * a = 1), _⟩, rfl⟩, by rwa zero_mul at a0,
1820
λ h, begin
@@ -70,6 +72,12 @@ theorem mul_dvd_of_is_unit_left [comm_semiring α] {x y z : α} (h : is_unit x)
7072
theorem mul_dvd_of_is_unit_right [comm_semiring α] {x y z : α} (h : is_unit y) : x * y ∣ z ↔ x ∣ z :=
7173
by rw [mul_comm, mul_dvd_of_is_unit_left h]
7274

75+
@[simp] lemma unit_mul_dvd_iff [comm_semiring α] {a b : α} {u : units α} : (u : α) * a ∣ b ↔ a ∣ b :=
76+
mul_dvd_of_is_unit_left (is_unit_unit _)
77+
78+
@[simp] lemma mul_unit_dvd_iff [comm_semiring α] {a b : α} {u : units α} : a * u ∣ b ↔ a ∣ b :=
79+
mul_dvd_of_is_unit_right (is_unit_unit _)
80+
7381
theorem is_unit_of_dvd_unit {α} [comm_semiring α] {x y : α}
7482
(xy : x ∣ y) (hu : is_unit y) : is_unit x :=
7583
is_unit_iff_dvd_one.2 $ dvd_trans xy $ is_unit_iff_dvd_one.1 hu
@@ -97,7 +105,7 @@ lemma dvd_and_not_dvd_iff [integral_domain α] {x y : α} :
97105
def prime [comm_semiring α] (p : α) : Prop :=
98106
p ≠ 0 ∧ ¬ is_unit p ∧ (∀a b, p ∣ a * b → p ∣ a ∨ p ∣ b)
99107

100-
lemma not_prime_zero [integral_domain α] : ¬ prime (0 : α)
108+
@[simp] lemma not_prime_zero [integral_domain α] : ¬ prime (0 : α)
101109
| ⟨h, _⟩ := h rfl
102110

103111
@[simp] lemma not_prime_one [comm_semiring α] : ¬ prime (1 : α) :=
@@ -263,6 +271,78 @@ begin
263271
exact ⟨units.mk_of_mul_eq_one c d (this.symm), by rw [units.mk_of_mul_eq_one, units.val_coe]⟩
264272
end
265273

274+
lemma exists_associated_mem_of_dvd_prod [integral_domain α] {p : α}
275+
(hp : prime p) {s : multiset α} : (∀ r ∈ s, prime r) → p ∣ s.prod → ∃ q ∈ s, p ~ᵤ q :=
276+
multiset.induction_on s (by simp [mt is_unit_iff_dvd_one.2 hp.2.1])
277+
(λ a s ih hs hps, begin
278+
rw [multiset.prod_cons] at hps,
279+
cases hp.2.2 _ _ hps with h h,
280+
{ use [a, by simp],
281+
cases h with u hu,
282+
cases ((irreducible_of_prime (hs a (multiset.mem_cons.2
283+
(or.inl rfl)))).2 p u hu).resolve_left hp.2.1 with v hv,
284+
exact ⟨v, by simp [hu, hv]⟩ },
285+
{ rcases ih (λ r hr, hs _ (multiset.mem_cons.2 (or.inr hr))) h with ⟨q, hq₁, hq₂⟩,
286+
exact ⟨q, multiset.mem_cons.2 (or.inr hq₁), hq₂⟩ }
287+
end)
288+
289+
lemma dvd_iff_dvd_of_rel_left [comm_semiring α] {a b c : α} (h : a ~ᵤ b) : a ∣ c ↔ b ∣ c :=
290+
let ⟨u, hu⟩ := h in hu ▸ mul_unit_dvd_iff.symm
291+
292+
@[simp] lemma dvd_mul_unit_iff [comm_semiring α] {a b : α} {u : units α} : a ∣ b * u ↔ a ∣ b :=
293+
⟨λ ⟨d, hd⟩, ⟨d * (u⁻¹ : units α), by simp [(mul_assoc _ _ _).symm, hd.symm]⟩,
294+
λ h, dvd.trans h (by simp)⟩
295+
296+
lemma dvd_iff_dvd_of_rel_right [comm_semiring α] {a b c : α} (h : b ~ᵤ c) : a ∣ b ↔ a ∣ c :=
297+
let ⟨u, hu⟩ := h in hu ▸ dvd_mul_unit_iff.symm
298+
299+
lemma eq_zero_iff_of_associated [comm_semiring α] {a b : α} (h : a ~ᵤ b) : a = 0 ↔ b = 0 :=
300+
⟨λ ha, let ⟨u, hu⟩ := h in by simp [hu.symm, ha],
301+
λ hb, let ⟨u, hu⟩ := h.symm in by simp [hu.symm, hb]⟩
302+
303+
lemma ne_zero_iff_of_associated [comm_semiring α] {a b : α} (h : a ~ᵤ b) : a ≠ 0 ↔ b ≠ 0 :=
304+
by haveI := classical.dec; exact not_iff_not.2 (eq_zero_iff_of_associated h)
305+
306+
lemma prime_of_associated [comm_semiring α] {p q : α} (h : p ~ᵤ q) (hp : prime p) : prime q :=
307+
⟨(ne_zero_iff_of_associated h).1 hp.1,
308+
let ⟨u, hu⟩ := h in
309+
⟨λ ⟨v, hv⟩, hp.2.1 ⟨v * u⁻¹, by simp [hv.symm, hu.symm]⟩,
310+
hu ▸ by simp [mul_unit_dvd_iff]; exact hp.2.2⟩⟩
311+
312+
lemma prime_iff_of_associated [comm_semiring α] {p q : α}
313+
(h : p ~ᵤ q) : prime p ↔ prime q :=
314+
⟨prime_of_associated h, prime_of_associated h.symm⟩
315+
316+
lemma is_unit_iff_of_associated [monoid α] {a b : α} (h : a ~ᵤ b) : is_unit a ↔ is_unit b :=
317+
let ⟨u, hu⟩ := h in λ ⟨v, hv⟩, ⟨v * u, by simp [hv, hu.symm]⟩,
318+
let ⟨u, hu⟩ := h.symm in λ ⟨v, hv⟩, ⟨v * u, by simp [hv, hu.symm]⟩⟩
319+
320+
lemma irreducible_of_associated [comm_semiring α] {p q : α} (h : p ~ᵤ q)
321+
(hp : irreducible p) : irreducible q :=
322+
⟨mt (is_unit_iff_of_associated h).2 hp.1,
323+
let ⟨u, hu⟩ := h in λ a b hab,
324+
have hpab : p = a * (b * (u⁻¹ : units α)),
325+
from calc p = (p * u) * (u ⁻¹ : units α) : by simp
326+
... = _ : by rw hu; simp [hab, mul_assoc],
327+
(hp.2 _ _ hpab).elim or.inl (λ ⟨v, hv⟩, or.inr ⟨v * u, by simp [hv.symm]⟩)⟩
328+
329+
lemma irreducible_iff_of_associated [comm_semiring α] {p q : α} (h : p ~ᵤ q) :
330+
irreducible p ↔ irreducible q :=
331+
⟨irreducible_of_associated h, irreducible_of_associated h.symm⟩
332+
333+
lemma associated_mul_left_cancel [integral_domain α] {a b c d : α}
334+
(h : a * b ~ᵤ c * d) (h₁ : a ~ᵤ c) (ha : a ≠ 0) : b ~ᵤ d :=
335+
let ⟨u, hu⟩ := h in let ⟨v, hv⟩ := associated.symm h₁ in
336+
⟨u * (v : units α), (domain.mul_left_inj ha).1
337+
begin
338+
rw [← hv, mul_assoc c (v : α) d, mul_left_comm c, ← hu],
339+
simp [hv.symm, mul_assoc, mul_comm, mul_left_comm]
340+
end
341+
342+
lemma associated_mul_right_cancel [integral_domain α] {a b c d : α} :
343+
a * b ~ᵤ c * d → b ~ᵤ d → b ≠ 0 → a ~ᵤ c :=
344+
by rw [mul_comm a, mul_comm c]; exact associated_mul_left_cancel
345+
266346
def associates (α : Type*) [monoid α] : Type* :=
267347
quotient (associated.setoid α)
268348

ring_theory/principal_ideal_domain.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ This is not added as type class instance, since the `factors` might be computed
218218
E.g. factors could return normalized values.
219219
-/
220220
noncomputable def to_unique_factorization_domain : unique_factorization_domain α :=
221+
unique_factorization_domain.of_unique_irreducible_factorization
221222
{ factors := factors,
222223
factors_prod := assume a ha, associated.symm (factors_spec a ha).2,
223224
irreducible_factors := assume a ha, (factors_spec a ha).1,

ring_theory/unique_factorization_domain.lean

Lines changed: 148 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,162 @@ local infix ` ~ᵤ ` : 50 := associated
1414

1515
/-- Unique factorization domains.
1616
17-
In a unique factorization domain each element (except zero) is uniquely represented as a multiset
18-
of irreducible factors. Uniqueness is only up to associated elements.
17+
In a unique factorization domain each element (except zero) is uniquely
18+
represented as a multiset of irreducible factors.
19+
Uniqueness is only up to associated elements.
20+
21+
This is equivalent to defining a unique factorization domain as a domain in
22+
which each element (except zero) is represented as a multiset of prime factors.
23+
This definition is used.
24+
25+
To define a UFD using the traditional definition in terms of multisets of irreducible
26+
factors, use the definition `of_unique_irreducible_factorization
27+
1928
-/
2029
class unique_factorization_domain (α : Type*) [integral_domain α] :=
2130
(factors : α → multiset α)
2231
(factors_prod : ∀{a : α}, a ≠ 0 → (factors a).prod ~ᵤ a)
32+
(prime_factors : ∀{a : α}, a ≠ 0 → ∀x∈factors a, prime x)
33+
34+
namespace unique_factorization_domain
35+
36+
variables [integral_domain α] [unique_factorization_domain α]
37+
38+
@[elab_as_eliminator] lemma induction_on_prime {P : α → Prop}
39+
(a : α) (h₁ : P 0) (h₂ : ∀ x : α, is_unit x → P x)
40+
(h₃ : ∀ a p : α, a ≠ 0 → prime p → P a → P (p * a)) : P a :=
41+
by haveI := classical.dec_eq α; exact
42+
if ha0 : a = 0 then ha0.symm ▸ h₁
43+
else @multiset.induction_on _
44+
(λ s : multiset α, ∀ (a : α), a ≠ 0 → s.prod ~ᵤ a → (∀ p ∈ s, prime p) → P a)
45+
(factors a)
46+
(λ _ _ h _, h₂ _ ((is_unit_iff_of_associated h.symm).2 is_unit_one))
47+
(λ p s ih a ha0 ⟨u, hu⟩ hsp,
48+
have ha : a = (p * u) * s.prod, by simp [hu.symm, mul_comm, mul_assoc],
49+
have hs0 : s.prod ≠ 0, from λ _ : s.prod = 0, by simp * at *,
50+
ha.symm ▸ h₃ _ _ hs0
51+
(prime_of_associated ⟨u, rfl⟩ (hsp p (multiset.mem_cons_self _ _)))
52+
(ih _ hs0 (by refl) (λ p hp, hsp p (multiset.mem_cons.2 (or.inr hp)))))
53+
_
54+
ha0
55+
(factors_prod ha0)
56+
(prime_factors ha0)
57+
58+
lemma factors_irreducible {a : α} (ha : irreducible a) :
59+
∃ p, a ~ᵤ p ∧ factors a = p :: 0 :=
60+
by haveI := classical.dec_eq α; exact
61+
multiset.induction_on (factors a)
62+
(λ h, (ha.1 (associated_one_iff_is_unit.1 h.symm)).elim)
63+
(λ p s _ hp hs, let ⟨u, hu⟩ := hp in ⟨p,
64+
have hs0 : s = 0, from classical.by_contradiction
65+
(λ hs0, let ⟨q, hq⟩ := multiset.exists_mem_of_ne_zero hs0 in
66+
(hs q (by simp [hq])).2.1 $
67+
(ha.2 ((p * u) * (s.erase q).prod) _
68+
(by rw [mul_right_comm _ _ q, mul_assoc, ← multiset.prod_cons,
69+
multiset.cons_erase hq]; simp [hu.symm, mul_comm, mul_assoc])).resolve_left $
70+
mt is_unit_of_mul_is_unit_left $ mt is_unit_of_mul_is_unit_left
71+
(hs p (multiset.mem_cons_self _ _)).2.1),
72+
⟨associated.symm (by clear _let_match; simp * at *), hs0 ▸ rfl⟩⟩)
73+
(factors_prod (nonzero_of_irreducible ha))
74+
(prime_factors (nonzero_of_irreducible ha))
75+
76+
lemma irreducible_iff_prime {p : α} : irreducible p ↔ prime p :=
77+
by letI := classical.dec_eq α; exact
78+
if hp0 : p = 0 then by simp [hp0]
79+
else
80+
⟨λ h, let ⟨q, hq⟩ := factors_irreducible h in
81+
have prime q, from hq.2 ▸ prime_factors hp0 _ (by simp [hq.2]),
82+
suffices prime (factors p).prod,
83+
from prime_of_associated (factors_prod hp0) this,
84+
hq.2.symm ▸ by simp [this],
85+
irreducible_of_prime⟩
86+
87+
lemma irreducible_factors : ∀{a : α}, a ≠ 0 → ∀x∈factors a, irreducible x :=
88+
by simp only [irreducible_iff_prime]; exact @prime_factors _ _ _
89+
90+
lemma unique : ∀{f g : multiset α},
91+
(∀x∈f, irreducible x) → (∀x∈g, irreducible x) → f.prod ~ᵤ g.prod →
92+
multiset.rel associated f g :=
93+
by haveI := classical.dec_eq α; exact
94+
λ f, multiset.induction_on f
95+
(λ g _ hg h,
96+
multiset.rel_zero_left.2 $
97+
multiset.eq_zero_of_forall_not_mem (λ x hx,
98+
have is_unit g.prod, by simpa [associated_one_iff_is_unit] using h.symm,
99+
(hg x hx).1 (is_unit_iff_dvd_one.2 (dvd.trans (multiset.dvd_prod hx)
100+
(is_unit_iff_dvd_one.1 this)))))
101+
(λ p f ih g hf hg hfg,
102+
let ⟨b, hbg, hb⟩ := exists_associated_mem_of_dvd_prod
103+
(irreducible_iff_prime.1 (hf p (by simp)))
104+
(λ q hq, irreducible_iff_prime.1 (hg _ hq)) $
105+
(dvd_iff_dvd_of_rel_right hfg).1
106+
(show p ∣ (p :: f).prod, by simp) in
107+
begin
108+
rw ← multiset.cons_erase hbg,
109+
exact multiset.rel.cons hb (ih (λ q hq, hf _ (by simp [hq]))
110+
(λ q (hq : q ∈ g.erase b), hg q (multiset.mem_of_mem_erase hq))
111+
(associated_mul_left_cancel
112+
(by rwa [← multiset.prod_cons, ← multiset.prod_cons, multiset.cons_erase hbg]) hb
113+
(nonzero_of_irreducible (hf p (by simp)))))
114+
end)
115+
116+
end unique_factorization_domain
117+
118+
structure unique_irreducible_factorization (α : Type*) [integral_domain α] :=
119+
(factors : α → multiset α)
120+
(factors_prod : ∀{a : α}, a ≠ 0 → (factors a).prod ~ᵤ a)
23121
(irreducible_factors : ∀{a : α}, a ≠ 0 → ∀x∈factors a, irreducible x)
24122
(unique : ∀{f g : multiset α},
25123
(∀x∈f, irreducible x) → (∀x∈g, irreducible x) → f.prod ~ᵤ g.prod → multiset.rel associated f g)
26124

125+
namespace unique_factorization_domain
126+
127+
def of_unique_irreducible_factorization {α : Type*} [integral_domain α]
128+
(o : unique_irreducible_factorization α) : unique_factorization_domain α :=
129+
by letI := classical.dec_eq α; exact
130+
{ prime_factors := λ a h p (hpa : p ∈ o.factors a),
131+
have hpi : irreducible p, from o.irreducible_factors h _ hpa,
132+
⟨nonzero_of_irreducible hpi, hpi.1,
133+
λ a b ⟨x, hx⟩,
134+
if hab0 : a * b = 0
135+
then (eq_zero_or_eq_zero_of_mul_eq_zero hab0).elim
136+
(λ ha0, by simp [ha0])
137+
(λ hb0, by simp [hb0])
138+
else
139+
have hx0 : x ≠ 0, from λ hx0, by simp * at *,
140+
have ha0 : a ≠ 0, from ne_zero_of_mul_ne_zero_right hab0,
141+
have hb0 : b ≠ 0, from ne_zero_of_mul_ne_zero_left hab0,
142+
have multiset.rel associated (p :: o.factors x) (o.factors a + o.factors b),
143+
from o.unique
144+
(λ i hi, (multiset.mem_cons.1 hi).elim
145+
(λ hip, hip.symm ▸ hpi)
146+
(o.irreducible_factors hx0 _))
147+
(show ∀ x ∈ o.factors a + o.factors b, irreducible x,
148+
from λ x hx, (multiset.mem_add.1 hx).elim
149+
(o.irreducible_factors (ne_zero_of_mul_ne_zero_right hab0) _)
150+
(o.irreducible_factors (ne_zero_of_mul_ne_zero_left hab0) _)) $
151+
calc multiset.prod (p :: o.factors x)
152+
~ᵤ a * b : by rw [hx, multiset.prod_cons];
153+
exact associated_mul_mul (by refl)
154+
(o.factors_prod hx0)
155+
... ~ᵤ (o.factors a).prod * (o.factors b).prod :
156+
associated_mul_mul
157+
(o.factors_prod ha0).symm
158+
(o.factors_prod hb0).symm
159+
... = _ : by rw multiset.prod_add,
160+
let ⟨q, hqf, hq⟩ := multiset.exists_mem_of_rel_of_mem this
161+
(multiset.mem_cons_self p _) in
162+
(multiset.mem_add.1 hqf).elim
163+
(λ hqa, or.inl $ (dvd_iff_dvd_of_rel_left hq).2 $
164+
(dvd_iff_dvd_of_rel_right (o.factors_prod ha0)).1
165+
(multiset.dvd_prod hqa))
166+
(λ hqb, or.inr $ (dvd_iff_dvd_of_rel_left hq).2 $
167+
(dvd_iff_dvd_of_rel_right (o.factors_prod hb0)).1
168+
(multiset.dvd_prod hqb))⟩,
169+
..o }
170+
171+
end unique_factorization_domain
172+
27173
namespace associates
28174
open unique_factorization_domain associated lattice
29175
variables [integral_domain α] [unique_factorization_domain α] [decidable_eq (associates α)]

0 commit comments

Comments
 (0)