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

Commit c53f9f1

Browse files
committed
refactor(algebra/euclidean_domain): clean up proofs
1 parent 9f0d1d8 commit c53f9f1

File tree

3 files changed

+102
-222
lines changed

3 files changed

+102
-222
lines changed

algebra/euclidean_domain.lean

Lines changed: 93 additions & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -1,249 +1,155 @@
11
/-
22
Copyright (c) 2018 Louis Carlin. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
4-
Authors: Louis Carlin
4+
Authors: Louis Carlin, Mario Carneiro
55
66
Euclidean domains and Euclidean algorithm (extended to come)
77
A lot is based on pre-existing code in mathlib for natural number gcds
88
-/
9-
import tactic.ring
9+
import algebra.group algebra.order
1010

1111
universe u
1212

13-
class euclidean_domain (α : Type u) [decidable_eq α] extends integral_domain α :=
13+
class euclidean_domain (α : Type u) extends integral_domain α :=
1414
(quotient : α → α → α)
1515
(remainder : α → α → α)
16-
(quotient_mul_add_remainder_eq : ∀ a b, (quotient a b) * b + (remainder a b) = a) -- This could be changed to the same order as int.mod_add_div. We normally write qb+r rather than r + qb though.
16+
-- This could be changed to the same order as int.mod_add_div.
17+
-- We normally write qb+r rather than r + qb though.
18+
(quotient_mul_add_remainder_eq : ∀ a b, b * quotient a b + remainder a b = a)
1719
(valuation : α → ℕ)
18-
(valuation_remainder_lt : ∀ a b, b ≠ 0 → valuation (remainder a b) < valuation b)
19-
(le_valuation_mul : ∀ a b, b ≠ 0 → valuation a ≤ valuation (a*b))
20-
21-
/-
22-
le_valuation_mul is often not a required in definitions of a euclidean domain since given the other properties we can show there is a (noncomputable) euclidean domain α with the property le_valuation_mul.
23-
So potentially this definition could be split into two different ones (euclidean_domain_weak and euclidean_domain_strong) with a noncomputable function from weak to strong.
24-
I've currently divided the lemmas into strong and weak depending on whether they require le_valuation_mul or not.
25-
-/
20+
(val_remainder_lt : ∀ a {b}, b ≠ 0 → valuation (remainder a b) < valuation b)
21+
/- le_valuation_mul is often not a required in definitions of a euclidean
22+
domain since given the other properties we can show there is a
23+
(noncomputable) euclidean domain α with the property le_valuation_mul.
24+
So potentially this definition could be split into two different ones
25+
(euclidean_domain_weak and euclidean_domain_strong) with a noncomputable
26+
function from weak to strong. I've currently divided the lemmas into
27+
strong and weak depending on whether they require le_valuation_mul or not. -/
28+
(val_le_mul_left : ∀ a {b}, b ≠ 0 → valuation a ≤ valuation (a * b))
2629

2730
namespace euclidean_domain
2831
variable {α : Type u}
29-
variables [decidable_eq α] [euclidean_domain α]
32+
variables [euclidean_domain α]
3033

3134
instance : has_div α := ⟨quotient⟩
3235

3336
instance : has_mod α := ⟨remainder⟩
3437

35-
instance : has_sizeof α := ⟨valuation⟩
36-
37-
lemma gcd_decreasing (a b : α) (w : a ≠ 0) : has_well_founded.r (b % a) a := valuation_remainder_lt b a w
38-
39-
def gcd : α → α → α
40-
| a b := if a_zero : a = 0 then b
41-
else have h : has_well_founded.r (b % a) a := gcd_decreasing a b a_zero,
42-
gcd (b%a) a
38+
theorem div_add_mod (a b : α) : b * (a / b) + a % b = a :=
39+
quotient_mul_add_remainder_eq _ _
4340

44-
/- weak lemmas -/
41+
theorem val_mod_lt : ∀ a {b : α}, b ≠ 0 → valuation (a % b) < valuation b :=
42+
val_remainder_lt
4543

46-
@[simp] lemma mod_zero (a : α) : a % 0 = a := by simpa using
47-
quotient_mul_add_remainder_eq a 0
44+
theorem val_le_mul_right {a : α} (b) (h : a 0) : valuation b ≤ valuation (a * b) :=
45+
by rw mul_comm; exact val_le_mul_left b h
4846

49-
lemma dvd_mod_self (a : α) : a ∣ a % a :=
47+
lemma mul_div_cancel_left {a : α} (b) (a0 : a ≠ 0) : a * b / a = b :=
48+
eq.symm $ eq_of_sub_eq_zero $ classical.by_contradiction $ λ h,
5049
begin
51-
let d := (a/a)*a, -- ring tactic can't solve things without this
52-
have : a%a = a - (a/a)*a, from
53-
calc
54-
a%a = d + a%a - d : by ring
55-
... = (a/a)*a + a%a - (a/a)*a : by dsimp [d]; refl
56-
... = a - (a/a)*a : by simp [(%), (/), quotient_mul_add_remainder_eq a a],
57-
rw this,
58-
exact dvd_sub (dvd_refl a) (dvd_mul_left a (a/a)),
50+
have := val_le_mul_left a h,
51+
rw [mul_sub, sub_eq_iff_eq_add'.2 (div_add_mod (a*b) a).symm] at this,
52+
exact not_lt_of_le this (val_mod_lt _ a0)
5953
end
6054

61-
lemma mod_lt : ∀ (a : α) {b : α}, valuation b > valuation (0:α) → valuation (a%b) < valuation b :=
62-
begin
63-
intros a b h,
64-
by_cases b_zero : (b=0),
65-
{ rw b_zero at h,
66-
have := lt_irrefl (valuation (0:α)),
67-
contradiction },
68-
{ exact valuation_remainder_lt a b b_zero }
69-
end
55+
lemma mul_div_cancel (a) {b : α} (b0 : b ≠ 0) : a * b / b = a :=
56+
by rw mul_comm; exact mul_div_cancel_left a b0
7057

71-
lemma neq_zero_lt_mod_lt (a b : α) : b ≠ 0 → valuation (a%b) < valuation b
72-
| hnb := valuation_remainder_lt a b hnb
58+
@[simp] lemma mod_zero (a : α) : a % 0 = a :=
59+
by simpa using div_add_mod a 0
7360

74-
lemma dvd_mod {a b c : α} : c ∣ a → c ∣ b → c ∣ a % b :=
75-
begin
76-
intros dvd_a dvd_b,
77-
have : remainder a b = a - quotient a b * b, from
78-
calc
79-
a%b = quotient a b * b + a%b - quotient a b * b : by ring
80-
... = a - quotient a b * b : by {dsimp[(%)]; rw (quotient_mul_add_remainder_eq a b)},
81-
dsimp [(%)],
82-
rw this,
83-
exact dvd_sub dvd_a (dvd_mul_of_dvd_right dvd_b (a/b)),
84-
end
61+
@[simp] lemma mod_eq_zero {a b : α} : a % b = 0 ↔ b ∣ a :=
62+
⟨λ h, by rw [← div_add_mod a b]; simp [h],
63+
λ ⟨c, e⟩, begin
64+
rw [e, ← add_left_cancel_iff, div_add_mod, add_zero],
65+
haveI := classical.dec,
66+
by_cases b0 : b = 0; simp [b0, mul_div_cancel_left],
67+
end
8568

86-
/- strong lemmas -/
69+
@[simp] lemma mod_self (a : α) : a % a = 0 :=
70+
mod_eq_zero.2 (dvd_refl _)
71+
72+
lemma dvd_mod_iff {a b c : α} (h : c ∣ b) : c ∣ a % b ↔ c ∣ a :=
73+
by rw [dvd_add_iff_right (dvd_mul_of_dvd_left h _), div_add_mod]
8774

8875
lemma val_lt_one (a : α) : valuation a < valuation (1:α) → a = 0 :=
89-
begin
90-
intro a_lt,
91-
by_cases a = 0,
92-
{ exact h },
93-
{ have := le_valuation_mul (1:α) a h,
94-
simp at this,
95-
have := not_le_of_lt a_lt,
96-
contradiction }
97-
end
76+
by haveI := classical.dec; exact
77+
not_imp_not.1 (λ h, by simpa using val_le_mul_left 1 h)
9878

9979
lemma val_dvd_le : ∀ a b : α, b ∣ a → a ≠ 0 → valuation b ≤ valuation a
100-
| _ b ⟨d, rfl⟩ ha :=
101-
begin
102-
by_cases d = 0,
103-
{ simp[h] at ha,
104-
contradiction },
105-
{ exact le_valuation_mul b d h }
106-
end
80+
| _ b ⟨d, rfl⟩ ha := val_le_mul_left b $
81+
mt (by intro h; simp [h]) ha
10782

10883
@[simp] lemma mod_one (a : α) : a % 1 = 0 :=
109-
val_lt_one _ (valuation_remainder_lt a 1 one_ne_zero)
84+
mod_eq_zero.2 (one_dvd _)
11085

11186
@[simp] lemma zero_mod (b : α) : 0 % b = 0 :=
112-
begin
113-
have h : remainder 0 b = b * (-quotient 0 b ), from
114-
calc
115-
remainder 0 b = quotient 0 b * b + remainder 0 b + b * (-quotient 0 b ) : by ring
116-
... = b * (-quotient 0 b ) : by rw [quotient_mul_add_remainder_eq 0 b, zero_add],
117-
by_cases quotient_zero : (-quotient 0 b) = 0,
118-
{ simp[quotient_zero] at h,
119-
exact h },
120-
{ by_cases h'' : b = 0,
121-
{ rw h'',
122-
simp },
123-
{ have := not_le_of_lt (valuation_remainder_lt 0 b h''),
124-
rw h at this,
125-
have := le_valuation_mul b (-quotient 0 b) quotient_zero,
126-
contradiction }}
127-
end
87+
mod_eq_zero.2 (dvd_zero _)
12888

129-
@[simp] lemma zero_div (b : α) (hnb : b ≠ 0) : 0 / b = 0 :=
130-
begin
131-
have h1 : remainder 0 b = 0, from zero_mod b,
132-
have h2 := quotient_mul_add_remainder_eq 0 b,
133-
simp[h1] at h2,
134-
cases eq_zero_or_eq_zero_of_mul_eq_zero h2,
135-
{ exact h },
136-
{ contradiction }
137-
end
89+
@[simp] lemma zero_div {a : α} (a0 : a ≠ 0) : 0 / a = 0 :=
90+
by simpa using mul_div_cancel 0 a0
13891

139-
@[simp] lemma mod_self (a : α) : a % a = 0 :=
140-
let ⟨m, a_mul⟩ := dvd_mod_self a in
141-
begin
142-
by_cases m = 0,
143-
{ rw [h, mul_zero] at a_mul,
144-
exact a_mul },
145-
{ by_cases a_zero : a = 0,
146-
{ rw a_zero,
147-
simp },
148-
{ have := le_valuation_mul a m h,
149-
rw ←a_mul at this,
150-
have := not_le_of_lt (valuation_remainder_lt a a a_zero),
151-
contradiction}}
152-
end
92+
@[simp] lemma div_self {a : α} (a0 : a ≠ 0) : a / a = 1 :=
93+
by simpa using mul_div_cancel 1 a0
15394

154-
lemma div_self (a : α) : a ≠ 0 → a / a = (1:α) :=
155-
begin
156-
intro hna,
157-
have wit_aa := quotient_mul_add_remainder_eq a a,
158-
have a_mod_a := mod_self a,
159-
dsimp [(%)] at a_mod_a,
160-
simp [a_mod_a] at wit_aa,
161-
have h1 : 1 * a = a, from one_mul a,
162-
conv at wit_aa {for a [4] {rw ←h1}},
163-
exact eq_of_mul_eq_mul_right hna wit_aa
164-
end
95+
section gcd
96+
variable [decidable_eq α]
16597

166-
/- weak gcd lemmas -/
98+
def gcd : α → α → α
99+
| a := λ b, if a0 : a = 0 then b else
100+
have h:_ := val_mod_lt b a0,
101+
gcd (b%a) a
102+
using_well_founded {rel_tac :=
103+
λ _ _, `[exact ⟨_, measure_wf valuation⟩]}
167104

168105
@[simp] theorem gcd_zero_left (a : α) : gcd 0 a = a :=
169-
begin
170-
rw gcd,
171-
simp,
172-
end
106+
by rw gcd; simp
107+
108+
@[simp] theorem gcd_zero_right (a : α) : gcd a 0 = a :=
109+
by rw gcd; by_cases a0 : a = 0; simp [a0]
110+
111+
theorem gcd_val (a b : α) : gcd a b = gcd (b % a) a :=
112+
by rw gcd; by_cases a0 : a = 0; simp [a0]
173113

174114
@[elab_as_eliminator]
175-
theorem gcd.induction {P : α → α → Prop}
176-
(a b : α)
177-
(H0 : ∀ x, P 0 x)
178-
(H1 : ∀ a b, a ≠ 0 → P (b%a) a → P a b) :
179-
P a b :=
180-
@well_founded.induction _ _ (has_well_founded.wf α) (λa, ∀b, P a b) a
181-
begin
182-
intros c IH,
183-
by_cases c = 0,
184-
{ rw h,
185-
exact H0 },
186-
{ intro b,
187-
exact H1 c b (h) (IH (b%c) (neq_zero_lt_mod_lt b c h) c) }
188-
end
189-
b
190-
191-
theorem gcd_dvd (a b : α) : (gcd a b ∣ a) ∧ (gcd a b ∣ b) :=
115+
theorem gcd.induction {P : α → α → Prop} : ∀ a b : α,
116+
(∀ x, P 0 x) →
117+
(∀ a b, a ≠ 0 → P (b % a) a → P a b) →
118+
P a b
119+
| a := λ b H0 H1, if a0 : a = 0 then by simp [a0, H0] else
120+
have h:_ := val_mod_lt b a0,
121+
H1 _ _ a0 (gcd.induction (b%a) a H0 H1)
122+
using_well_founded {rel_tac :=
123+
λ _ _, `[exact ⟨_, measure_wf valuation⟩]}
124+
125+
theorem gcd_dvd (a b : α) : gcd a b ∣ a ∧ gcd a b ∣ b :=
192126
gcd.induction a b
193127
(λ b, by simp)
194-
begin
195-
intros a b aneq h_dvd,
196-
rw gcd,
197-
simp [aneq],
198-
induction h_dvd,
199-
split,
200-
{ exact h_dvd_right },
201-
{ conv {for b [2] {rw ←(quotient_mul_add_remainder_eq b a)}},
202-
have h_dvd_right_a:= dvd_mul_of_dvd_right h_dvd_right (b/a),
203-
exact dvd_add h_dvd_right_a h_dvd_left }
204-
end
128+
(λ a b aneq ⟨IH₁, IH₂⟩,
129+
by rw gcd_val; exact
130+
⟨IH₂, (dvd_mod_iff IH₂).1 IH₁⟩)
205131

206132
theorem gcd_dvd_left (a b : α) : gcd a b ∣ a := (gcd_dvd a b).left
207133

208134
theorem gcd_dvd_right (a b : α) : gcd a b ∣ b := (gcd_dvd a b).right
209135

210136
theorem dvd_gcd {a b c : α} : c ∣ a → c ∣ b → c ∣ gcd a b :=
211137
gcd.induction a b
212-
begin
213-
intros b dvd_0 dvd_b,
214-
simp,
215-
exact dvd_b
216-
end
217-
begin
218-
intros a b hna d dvd_a dvd_b,
219-
rw gcd,
220-
simp [hna],
221-
exact d (dvd_mod dvd_b dvd_a) dvd_a,
222-
end
223-
224-
/- strong gcd lemmas -/
138+
(by simp {contextual := tt})
139+
(λ a b a0 IH ca cb,
140+
by rw gcd_val; exact
141+
IH ((dvd_mod_iff ca).2 cb) ca)
225142

226-
@[simp] theorem gcd_zero_right (a : α) : gcd a 0 = a :=
227-
begin
228-
unfold1 gcd,
229-
split_ifs; simp *
230-
end
143+
theorem gcd_eq_left {a b : α} : gcd a b = a ↔ a ∣ b :=
144+
⟨λ h, by rw ← h; apply gcd_dvd_right,
145+
λ h, by rw [gcd_val, mod_eq_zero.2 h, gcd_zero_left]⟩
231146

232147
@[simp] theorem gcd_one_left (a : α) : gcd 1 a = 1 :=
233-
begin
234-
rw [gcd],
235-
simp,
236-
end
237-
238-
theorem gcd_next (a b : α) : gcd a b = gcd (b % a) a :=
239-
begin
240-
by_cases (a=0),
241-
{ simp [h] },
242-
{ rw gcd,
243-
simp [h] }
244-
end
148+
gcd_eq_left.2 (one_dvd _)
245149

246150
@[simp] theorem gcd_self (a : α) : gcd a a = a :=
247-
by rw [gcd_next a a, mod_self a, gcd_zero_left]
151+
gcd_eq_left.2 (dvd_refl _)
152+
153+
end gcd
248154

249155
end euclidean_domain

analysis/topology/infinite_sum.lean

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ have u_subset : u ⊆ fsts.sigma snds,
122122
have hc : c ∈ snds b, from mem_bind.mpr ⟨_, hu, by simp; refl⟩,
123123
by simp [mem_sigma, hb, hc] ,
124124
mem_at_top_sets.mpr $ exists.intro fsts $ assume bs (hbs : fsts ⊆ bs),
125-
have h : ∀cs:(Πb, b ∈ bsfinset (γ b)),
125+
have h : ∀cs : Π b ∈ bs, finset (γ b),
126126
(⋂b (hb : b ∈ bs), (λp:Πb, finset (γ b), p b) ⁻¹' {cs' | cs b hb ⊆ cs' }) ∩
127127
(λp, bs.sum (λb, (p b).sum (λc, f ⟨b, c⟩))) ⁻¹' s ≠ ∅,
128128
from assume cs,
@@ -132,7 +132,7 @@ mem_at_top_sets.mpr $ exists.intro fsts $ assume bs (hbs : fsts ⊆ bs),
132132
have (bs.sigma cs').sum f ∈ s,
133133
from hu _ $ finset.subset.trans u_subset $ sigma_mono hbs $
134134
assume b, @finset.subset_union_right (γ b) _ _ _,
135-
ne_empty_iff_exists_mem.mpr $ exists.intro cs' $
135+
set.ne_empty_iff_exists_mem.mpr $ exists.intro cs' $
136136
by simp [sum_eq, this]; { intros b hb, simp [cs', hb, finset.subset_union_right] },
137137
have tendsto (λp:(Πb:β, finset (γ b)), bs.sum (λb, (p b).sum (λc, f ⟨b, c⟩)))
138138
(⨅b (h : b ∈ bs), at_top.vmap (λp, p b)) (nhds (bs.sum g)),
@@ -147,8 +147,8 @@ mem_at_top_sets.mpr $ exists.intro fsts $ assume bs (hbs : fsts ⊆ bs),
147147
assume s₁ s₂ s₃ hs₁ hs₃ p hs₂ p' hp cs hp',
148148
have (⋂b (h : b ∈ bs), (λp:(Πb, finset (γ b)), p b) ⁻¹' {cs' | cs b h ⊆ cs' }) ≤ (⨅b∈bs, p b),
149149
from infi_le_infi $ assume b, infi_le_infi $ assume hb,
150-
le_trans (preimage_mono $ hp' b hb) (hp b hb),
151-
neq_bot_of_le_neq_bot (h _) (le_trans (inter_subset_inter (le_trans this hs₂) hs₃) hs₁),
150+
le_trans (set.preimage_mono $ hp' b hb) (hp b hb),
151+
neq_bot_of_le_neq_bot (h _) (le_trans (set.inter_subset_inter (le_trans this hs₂) hs₃) hs₁),
152152
hss' this
153153

154154
lemma has_sum_sigma [regular_space α] {γ : β → Type*} {f : (Σb:β, γ b) → α}
@@ -168,7 +168,7 @@ suffices at_top.map (λs:finset β, s.sum f) ≤ at_top.map (λs:finset γ, s.su
168168
from le_trans this hf,
169169
by rw [map_at_top_eq, map_at_top_eq];
170170
from (le_infi $ assume b, let ⟨v, hv⟩ := h_eq b in infi_le_of_le v $
171-
by simp [image_subset_iff]; exact hv)
171+
by simp [set.image_subset_iff]; exact hv)
172172

173173
lemma is_sum_iff_is_sum
174174
(h₁ : ∀u:finset γ, ∃v:finset β, ∀v', v ⊆ v' → ∃u', u ⊆ u' ∧ u'.sum g = v'.sum f)

0 commit comments

Comments
 (0)