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

Commit 46b0528

Browse files
committed
refactor(data/polynomial): Move some lemmas to monoid_algebra (#4627)
The `add_monoid_algebra.mul_apply_antidiagonal` lemma is copied verbatim from `monoid_algebra.mul_apply_antidiagonal`.
1 parent abaf3c2 commit 46b0528

File tree

4 files changed

+56
-79
lines changed

4 files changed

+56
-79
lines changed

src/algebra/monoid_algebra.lean

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,19 @@ instance [comm_semiring k] [comm_monoid G] : comm_semiring (monoid_algebra k G)
114114
end,
115115
.. monoid_algebra.semiring }
116116

117+
instance [semiring k] [nontrivial k] [monoid G] : nontrivial (monoid_algebra k G) :=
118+
begin
119+
refine nontrivial_of_ne 0 1 _, intro h,
120+
have : (0 : G →₀ k) 1 = 0 := rfl,
121+
simpa [h, one_def] using this,
122+
end
123+
117124
/-! #### Derived instances -/
118125
section derived_instances
119126

127+
instance [semiring k] [subsingleton k] : unique (monoid_algebra k G) :=
128+
finsupp.unique_of_right
129+
120130
instance [ring k] : add_group (monoid_algebra k G) :=
121131
finsupp.add_group
122132

@@ -532,9 +542,19 @@ instance [comm_semiring k] [add_comm_monoid G] : comm_semiring (add_monoid_algeb
532542
end,
533543
.. add_monoid_algebra.semiring }
534544

545+
instance [semiring k] [nontrivial k] [add_monoid G] : nontrivial (add_monoid_algebra k G) :=
546+
begin
547+
refine nontrivial_of_ne 0 1 _, intro h,
548+
have : (0 : G →₀ k) 0 = 0 := rfl,
549+
simpa [h, one_def] using this,
550+
end
551+
535552
/-! #### Derived instances -/
536553
section derived_instances
537554

555+
instance [semiring k] [subsingleton k] : unique (add_monoid_algebra k G) :=
556+
finsupp.unique_of_right
557+
538558
instance [ring k] : add_group (add_monoid_algebra k G) :=
539559
finsupp.add_group
540560

@@ -571,6 +591,25 @@ begin
571591
simp only [finsupp.sum_apply, single_apply],
572592
end
573593

594+
lemma mul_apply_antidiagonal (f g : add_monoid_algebra k G) (x : G) (s : finset (G × G))
595+
(hs : ∀ {p : G × G}, p ∈ s ↔ p.1 + p.2 = x) :
596+
(f * g) x = ∑ p in s, (f p.1 * g p.2) :=
597+
let F : G × G → k := λ p, if p.1 + p.2 = x then f p.1 * g p.2 else 0 in
598+
calc (f * g) x = (∑ a₁ in f.support, ∑ a₂ in g.support, F (a₁, a₂)) :
599+
mul_apply f g x
600+
... = ∑ p in f.support.product g.support, F p : finset.sum_product.symm
601+
... = ∑ p in (f.support.product g.support).filter (λ p : G × G, p.1 + p.2 = x), f p.1 * g p.2 :
602+
(finset.sum_filter _ _).symm
603+
... = ∑ p in s.filter (λ p : G × G, p.1 ∈ f.support ∧ p.2 ∈ g.support), f p.1 * g p.2 :
604+
sum_congr (by { ext, simp only [mem_filter, mem_product, hs, and_comm] }) (λ _ _, rfl)
605+
... = ∑ p in s, f p.1 * g p.2 : sum_subset (filter_subset _) $ λ p hps hp,
606+
begin
607+
simp only [mem_filter, mem_support_iff, not_and, not_not] at hp ⊢,
608+
by_cases h1 : f p.1 = 0,
609+
{ rw [h1, zero_mul] },
610+
{ rw [hp hps h1, mul_zero] }
611+
end
612+
574613
lemma support_mul (a b : add_monoid_algebra k G) :
575614
(a * b).support ⊆ a.support.bind (λa₁, b.support.bind $ λa₂, {a₁ + a₂}) :=
576615
subset.trans support_sum $ bind_mono $ assume a₁ _,

src/data/polynomial/algebra_map.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ begin
234234
congr, apply_congr, skip,
235235
rw [coeff_mul_X_sub_C, sub_mul, mul_assoc, ←pow_succ],
236236
},
237-
simp [sum_range_sub', coeff_single],
237+
simp [sum_range_sub', coeff_monomial],
238238
end
239239

240240
theorem not_is_unit_X_sub_C [nontrivial R] {r : R} : ¬ is_unit (X - C r) :=

src/data/polynomial/basic.lean

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ variables {R : Type u} {a : R} {m n : ℕ}
3333
section semiring
3434
variables [semiring R] {p q : polynomial R}
3535

36-
instance : inhabited (polynomial R) := finsupp.inhabited
36+
instance : inhabited (polynomial R) := add_monoid_algebra.inhabited _ _
3737
instance : semiring (polynomial R) := add_monoid_algebra.semiring
3838
instance : has_scalar R (polynomial R) := add_monoid_algebra.has_scalar
3939
instance : semimodule R (polynomial R) := add_monoid_algebra.semimodule
4040

41-
instance subsingleton [subsingleton R] : subsingleton (polynomial R) :=
42-
⟨λ _ _, ext (λ _, subsingleton.elim _ _)⟩
41+
instance [subsingleton R] : unique (polynomial R) := add_monoid_algebra.unique
4342

4443
@[simp] lemma support_zero : (0 : polynomial R).support = ∅ := rfl
4544

@@ -48,15 +47,15 @@ def monomial (n : ℕ) (a : R) : polynomial R := finsupp.single n a
4847

4948
@[simp] lemma monomial_zero_right (n : ℕ) :
5049
monomial n (0 : R) = 0 :=
51-
by simp [monomial]
50+
finsupp.single_zero
5251

5352
lemma monomial_add (n : ℕ) (r s : R) :
5453
monomial n (r + s) = monomial n r + monomial n s :=
55-
by simp [monomial]
54+
finsupp.single_add
5655

5756
lemma monomial_mul_monomial (n m : ℕ) (r s : R) :
5857
monomial n r * monomial m s = monomial (n + m) (r * s) :=
59-
by simp only [monomial, single_mul_single]
58+
add_monoid_algebra.single_mul_single
6059

6160

6261
/-- `X` is the polynomial variable (aka indeterminant). -/
@@ -80,19 +79,12 @@ by rw [mul_assoc, X_pow_mul, ←mul_assoc]
8079
lemma commute_X (p : polynomial R) : commute X p := X_mul
8180

8281
/-- coeff p n is the coefficient of X^n in p -/
83-
def coeff (p : polynomial R) := p.to_fun
82+
def coeff (p : polynomial R) : ℕ → R := @coe_fn (ℕ →₀ R) _ p
8483

8584
@[simp] lemma coeff_mk (s) (f) (h) : coeff (finsupp.mk s f h : polynomial R) = f := rfl
8685

8786
lemma coeff_monomial : coeff (monomial n a) m = if n = m then a else 0 :=
88-
by { dsimp [monomial, single, finsupp.single], congr, }
89-
90-
/--
91-
This lemma is needed for occasions when we break through the abstraction from
92-
`polynomial` to `finsupp`; ideally it wouldn't be necessary at all.
93-
-/
94-
lemma coeff_single : coeff (single n a) m = if n = m then a else 0 :=
95-
coeff_monomial
87+
by { dsimp [monomial, coeff], rw finsupp.single_apply, congr }
9688

9789
@[simp] lemma coeff_zero (n : ℕ) : coeff (0 : polynomial R) n = 0 := rfl
9890

@@ -105,37 +97,20 @@ coeff_monomial
10597
lemma coeff_X : coeff (X : polynomial R) n = if 1 = n then 1 else 0 := coeff_monomial
10698

10799
theorem ext_iff {p q : polynomial R} : p = q ↔ ∀ n, coeff p n = coeff q n :=
108-
⟨λ h n, h ▸ rfl, finsupp.ext⟩
100+
finsupp.ext_iff
109101

110102
@[ext] lemma ext {p q : polynomial R} : (∀ n, coeff p n = coeff q n) → p = q :=
111-
(@ext_iff _ _ p q).2
103+
finsupp.ext
112104

113105
-- this has the same content as the subsingleton
114106
lemma eq_zero_of_eq_zero (h : (0 : R) = (1 : R)) (p : polynomial R) : p = 0 :=
115107
by rw [←one_smul R p, ←h, zero_smul]
116108

117109
lemma support_monomial (n) (a : R) (H : a ≠ 0) : (monomial n a).support = singleton n :=
118-
begin
119-
ext,
120-
have m3 : a_1 ∈ (monomial n a).support ↔ coeff (monomial n a) a_1 ≠ 0 := (monomial n a).mem_support_to_fun a_1,
121-
rw [finset.mem_singleton, m3, coeff_monomial],
122-
split_ifs,
123-
{ rwa [h, eq_self_iff_true, iff_true], },
124-
{ rw [← @not_not (a_1=n)],
125-
apply not_congr,
126-
rw [eq_self_iff_true, true_iff, ← ne.def],
127-
symmetry,
128-
assumption, },
129-
end
110+
finsupp.support_single_ne_zero H
130111

131112
lemma support_monomial' (n) (a : R) : (monomial n a).support ⊆ singleton n :=
132-
begin
133-
by_cases h : a = 0,
134-
{ rw [h, monomial_zero_right, support_zero],
135-
exact finset.empty_subset {n}, },
136-
{ rw support_monomial n a h,
137-
exact finset.subset.refl {n}, },
138-
end
113+
finsupp.support_single_subset
139114

140115
lemma X_pow_eq_monomial (n) : X ^ n = monomial n (1:R) :=
141116
begin
@@ -167,7 +142,6 @@ section comm_semiring
167142
variables [comm_semiring R]
168143

169144
instance : comm_semiring (polynomial R) := add_monoid_algebra.comm_semiring
170-
instance : comm_monoid (polynomial R) := comm_semiring.to_comm_monoid (polynomial R)
171145

172146
end comm_semiring
173147

@@ -188,11 +162,7 @@ instance [comm_ring R] : comm_ring (polynomial R) := add_monoid_algebra.comm_rin
188162
section nonzero_semiring
189163

190164
variables [semiring R] [nontrivial R]
191-
instance : nontrivial (polynomial R) :=
192-
begin
193-
refine nontrivial_of_ne 0 1 _, intro h,
194-
have := coeff_zero 0, revert this, rw h, simp,
195-
end
165+
instance : nontrivial (polynomial R) := add_monoid_algebra.nontrivial
196166

197167
lemma X_ne_zero : (X : polynomial R) ≠ 0 :=
198168
mt (congr_arg (λ p, coeff p 1)) (by simp)

src/data/polynomial/coeff.lean

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ by { rw [mem_support_to_fun, not_not], refl, }
4848
variable (R)
4949
/-- The nth coefficient, as a linear map. -/
5050
def lcoeff (n : ℕ) : polynomial R →ₗ[R] R :=
51-
{ to_fun := λ f, coeff f n,
52-
map_add' := λ f g, coeff_add f g n,
53-
map_smul' := λ r p, coeff_smul p r n }
51+
finsupp.leval n
5452
variable {R}
5553

5654
@[simp] lemma lcoeff_apply (n : ℕ) (f : polynomial R) : lcoeff R n f = coeff f n := rfl
@@ -61,24 +59,7 @@ variable {R}
6159

6260
lemma coeff_mul (p q : polynomial R) (n : ℕ) :
6361
coeff (p * q) n = ∑ x in nat.antidiagonal n, coeff p x.1 * coeff q x.2 :=
64-
have hite : ∀ a : ℕ × ℕ, ite (a.1 + a.2 = n) (coeff p (a.fst) * coeff q (a.snd)) 00
65-
→ a.1 + a.2 = n, from λ a ha, by_contradiction
66-
(λ h, absurd (eq.refl (0 : R)) (by rwa if_neg h at ha)),
67-
calc coeff (p * q) n = ∑ a in p.support, ∑ b in q.support,
68-
ite (a + b = n) (coeff p a * coeff q b) 0 :
69-
by { simp only [add_monoid_algebra.mul_def, coeff_sum, coeff_single], refl }
70-
... = ∑ v in p.support.product q.support, ite (v.1 + v.2 = n) (coeff p v.1 * coeff q v.2) 0 :
71-
by rw sum_product
72-
... = ∑ x in nat.antidiagonal n, coeff p x.1 * coeff q x.2 :
73-
begin
74-
refine sum_bij_ne_zero (λ x _ _, x)
75-
(λ x _ hx, nat.mem_antidiagonal.2 (hite x hx)) (λ _ _ _ _ _ _ h, h)
76-
(λ x h₁ h₂, ⟨x, _, _, rfl⟩) _,
77-
{ rw [mem_product, mem_support_iff, mem_support_iff],
78-
exact ne_zero_and_ne_zero_of_mul h₂ },
79-
{ rw nat.mem_antidiagonal at h₁, rwa [if_pos h₁] },
80-
{ intros x h hx, rw [if_pos (hite x hx)] }
81-
end
62+
add_monoid_algebra.mul_apply_antidiagonal p q n _ (λ x, nat.mem_antidiagonal)
8263

8364
@[simp] lemma mul_coeff_zero (p q : polynomial R) : coeff (p * q) 0 = coeff p 0 * coeff q 0 :=
8465
by simp [coeff_mul]
@@ -94,27 +75,14 @@ lemma coeff_C_mul_X (x : R) (k n : ℕ) :
9475
by rw [← single_eq_C_mul_X]; simp [monomial, single, eq_comm, coeff]; congr
9576

9677
@[simp] lemma coeff_C_mul (p : polynomial R) : coeff (C a * p) n = a * coeff p n :=
97-
begin
98-
conv in (a * _) { rw [← @sum_single _ _ _ p, coeff_sum] },
99-
rw [add_monoid_algebra.mul_def, ←monomial_zero_left, monomial, sum_single_index],
100-
{ simp only [coeff_single, finsupp.mul_sum, coeff_sum],
101-
apply sum_congr rfl,
102-
assume i hi, by_cases i = n; simp [h] },
103-
{ simp [finsupp.sum] }
104-
end
78+
add_monoid_algebra.single_zero_mul_apply p a n
10579

10680
lemma C_mul' (a : R) (f : polynomial R) : C a * f = a • f :=
10781
ext $ λ n, coeff_C_mul f
10882

10983
@[simp] lemma coeff_mul_C (p : polynomial R) (n : ℕ) (a : R) :
11084
coeff (p * C a) n = coeff p n * a :=
111-
begin
112-
conv_rhs { rw [← @finsupp.sum_single _ _ _ p, coeff_sum] },
113-
rw [add_monoid_algebra.mul_def, ←monomial_zero_left], simp_rw [sum_single_index],
114-
{ simp only [coeff_single, finsupp.sum_mul, coeff_sum],
115-
apply sum_congr rfl,
116-
assume i hi, by_cases i = n; simp [h], },
117-
end
85+
add_monoid_algebra.mul_single_zero_apply p a n
11886

11987
lemma coeff_X_pow (k n : ℕ) :
12088
coeff (X^k : polynomial R) n = if n = k then 1 else 0 :=

0 commit comments

Comments
 (0)