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

Commit 8312419

Browse files
kim-emrobertylewis
andcommitted
refactor(data/polynomial): remove has_coe_to_fun, and @[reducible] on monomial (#3420)
I'm going to refactor in stages, trying to clean up some of the cruftier aspects of `data/polynomial/*`. This PR: 1. removes the `has_coe_to_fun` on polynomial Co-authored-by: Scott Morrison <scott.morrison@gmail.com> Co-authored-by: Rob Lewis <Rob.y.lewis@gmail.com>
1 parent eca55c9 commit 8312419

File tree

8 files changed

+56
-35
lines changed

8 files changed

+56
-35
lines changed

src/data/polynomial/basic.lean

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,9 @@ instance : semimodule R (polynomial R) := add_monoid_algebra.semimodule
4343
instance subsingleton [subsingleton R] : subsingleton (polynomial R) :=
4444
⟨λ _ _, ext (λ _, subsingleton.elim _ _)⟩
4545

46-
/-- The coercion turning a `polynomial` into the function which reports the coefficient of a given
47-
monomial `X^n` -/
48-
def coeff_coe_to_fun : has_coe_to_fun (polynomial R) :=
49-
finsupp.has_coe_to_fun
50-
51-
local attribute [instance] coeff_coe_to_fun
52-
53-
5446
@[simp] lemma support_zero : (0 : polynomial R).support = ∅ := rfl
5547

5648
/-- `monomial s a` is the monomial `a * X^s` -/
57-
@[reducible]
5849
def monomial (n : ℕ) (a : R) : polynomial R := finsupp.single n a
5950

6051
@[simp] lemma monomial_zero_right (n : ℕ) :
@@ -65,6 +56,11 @@ lemma monomial_add (n : ℕ) (r s : R) :
6556
monomial n (r + s) = monomial n r + monomial n s :=
6657
by simp [monomial]
6758

59+
lemma monomial_mul_monomial (n m : ℕ) (r s : R) :
60+
monomial n r * monomial m s = monomial (n + m) (r * s) :=
61+
by simp only [monomial, single_mul_single]
62+
63+
6864
/-- `X` is the polynomial variable (aka indeterminant). -/
6965
def X : polynomial R := monomial 1 1
7066

@@ -89,17 +85,21 @@ by rw [mul_assoc, X_pow_mul, ←mul_assoc]
8985
/-- coeff p n is the coefficient of X^n in p -/
9086
def coeff (p : polynomial R) := p.to_fun
9187

92-
lemma apply_eq_coeff : p n = coeff p n := rfl
93-
94-
9588
@[simp] lemma coeff_mk (s) (f) (h) : coeff (finsupp.mk s f h : polynomial R) = f := rfl
9689

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

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

102-
@[simp] lemma coeff_one_zero : coeff (1 : polynomial R) 0 = 1 := coeff_single
102+
@[simp] lemma coeff_one_zero : coeff (1 : polynomial R) 0 = 1 := coeff_monomial
103103

104104

105105
instance [has_repr R] : has_repr (polynomial R) :=

src/data/polynomial/coeff.lean

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ section coeff
3131

3232
@[simp, priority 990]
3333
lemma coeff_one (n : ℕ) : coeff (1 : polynomial R) n = if 0 = n then 1 else 0 :=
34-
coeff_single
34+
coeff_monomial
3535

3636
@[simp]
3737
lemma coeff_add (p q : polynomial R) (n : ℕ) : coeff (p + q) n = coeff p n + coeff q n := rfl
@@ -64,7 +64,7 @@ have hite : ∀ a : ℕ × ℕ, ite (a.1 + a.2 = n) (coeff p (a.fst) * coeff q (
6464
(λ h, absurd (eq.refl (0 : R)) (by rwa if_neg h at ha)),
6565
calc coeff (p * q) n = ∑ a in p.support, ∑ b in q.support,
6666
ite (a + b = n) (coeff p a * coeff q b) 0 :
67-
by simp only [mul_def, coeff_sum, coeff_single]; refl
67+
by { simp only [mul_def, coeff_sum, coeff_single], refl }
6868
... = ∑ v in p.support.product q.support, ite (v.1 + v.2 = n) (coeff p v.1 * coeff q v.2) 0 :
6969
by rw sum_product
7070
... = ∑ x in nat.antidiagonal n, coeff p x.1 * coeff q x.2 :
@@ -92,8 +92,8 @@ by rw [← single_eq_C_mul_X]; simp [monomial, single, eq_comm, coeff]; congr
9292
@[simp] lemma coeff_C_mul (p : polynomial R) : coeff (C a * p) n = a * coeff p n :=
9393
begin
9494
conv in (a * _) { rw [← @sum_single _ _ _ p, coeff_sum] },
95-
rw [mul_def, ←monomial_zero_left, sum_single_index],
96-
{ simp [coeff_single, finsupp.mul_sum, coeff_sum],
95+
rw [mul_def, ←monomial_zero_left, monomial, sum_single_index],
96+
{ simp only [coeff_single, finsupp.mul_sum, coeff_sum],
9797
apply sum_congr rfl,
9898
assume i hi, by_cases i = n; simp [h] },
9999
{ simp [finsupp.sum] }
@@ -104,15 +104,15 @@ end
104104
begin
105105
conv_rhs { rw [← @finsupp.sum_single _ _ _ p, coeff_sum] },
106106
rw [mul_def, ←monomial_zero_left], simp_rw [sum_single_index],
107-
{ simp [coeff_single, finsupp.sum_mul, coeff_sum],
107+
{ simp only [coeff_single, finsupp.sum_mul, coeff_sum],
108108
apply sum_congr rfl,
109109
assume i hi, by_cases i = n; simp [h], },
110110
end
111111

112112
lemma monomial_one_eq_X_pow : ∀{n}, monomial n (1 : R) = X^n
113113
| 0 := rfl
114114
| (n+1) :=
115-
calc monomial (n + 1) (1 : R) = monomial n 1 * X : by rw [X, single_mul_single, mul_one]
115+
calc monomial (n + 1) (1 : R) = monomial n 1 * X : by rw [X, monomial_mul_monomial, mul_one]
116116
... = X^n * X : by rw [monomial_one_eq_X_pow]
117117
... = X^(n+1) : by simp only [pow_add, pow_one]
118118

src/data/polynomial/degree/basic.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ end
161161
by simp only [←C_eq_nat_cast, nat_degree_C]
162162

163163
@[simp] lemma degree_monomial (n : ℕ) (ha : a ≠ 0) : degree (C a * X ^ n) = n :=
164-
by rw [← single_eq_C_mul_X, degree, support_single_ne_zero ha]; refl
164+
by rw [← single_eq_C_mul_X, degree, monomial, support_single_ne_zero ha]; refl
165165

166166
lemma degree_monomial_le (n : ℕ) (a : R) : degree (C a * X ^ n) ≤ n :=
167167
if h : a = 0 then by rw [h, C_0, zero_mul]; exact bot_le else le_of_eq (degree_monomial n h)

src/data/polynomial/derivative.lean

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ end
4949
finsupp.sum_zero_index
5050

5151
lemma derivative_monomial (a : R) (n : ℕ) : derivative (C a * X ^ n) = C (a * n) * X^(n - 1) :=
52-
by rw [← single_eq_C_mul_X, ← single_eq_C_mul_X, derivative, sum_single_index, single_eq_C_mul_X];
53-
simp only [zero_mul, C_0]; refl
52+
begin
53+
rw [← single_eq_C_mul_X, ← single_eq_C_mul_X, derivative, monomial,
54+
sum_single_index, single_eq_C_mul_X],
55+
simp only [zero_mul, C_0],
56+
end
5457

5558
@[simp] lemma derivative_C {a : R} : derivative (C a) = 0 :=
5659
suffices derivative (C a * X^0) = C (a * 0:R) * X ^ 0,

src/data/polynomial/div.lean

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ variables {R : Type u} {S : Type v} {T : Type w} {A : Type z} {a b : R} {n : ℕ
3030
section semiring
3131
variables [semiring R] {p q : polynomial R}
3232

33+
section
34+
/--
35+
The coercion turning a `polynomial` into the function which reports the coefficient of a given
36+
monomial `X^n`
37+
-/
38+
-- TODO we would like to completely remove this, but this requires fixing some proofs
39+
def coeff_coe_to_fun : has_coe_to_fun (polynomial R) :=
40+
finsupp.has_coe_to_fun
41+
42+
local attribute [instance] coeff_coe_to_fun
43+
44+
lemma apply_eq_coeff : p n = coeff p n := rfl
45+
end
46+
3347
/-- `div_X p` return a polynomial `q` such that `q * X + C (p.coeff 0) = p`.
3448
It can be used in a semiring where the usual division algorithm is not possible -/
3549
def div_X (p : polynomial R) : polynomial R :=

src/data/polynomial/eval.lean

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ begin
209209
refine ext (λ n, _),
210210
rw [comp, eval₂],
211211
conv in (C _ * _) { rw ← single_eq_C_mul_X },
212-
rw finsupp.sum_single
212+
congr,
213+
convert finsupp.sum_single _,
213214
end
214215

215216
@[simp] lemma X_comp : X.comp p = p := eval₂_X _ _

src/data/polynomial/monomial.lean

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,16 @@ end C
6161

6262
section coeff
6363

64-
@[simp] lemma coeff_X_one : coeff (X : polynomial R) 1 = 1 := coeff_single
64+
@[simp] lemma coeff_X_one : coeff (X : polynomial R) 1 = 1 := coeff_monomial
6565

66-
@[simp] lemma coeff_X_zero : coeff (X : polynomial R) 0 = 0 := coeff_single
66+
@[simp] lemma coeff_X_zero : coeff (X : polynomial R) 0 = 0 := coeff_monomial
6767

68-
lemma coeff_X : coeff (X : polynomial R) n = if 1 = n then 1 else 0 := coeff_single
68+
lemma coeff_X : coeff (X : polynomial R) n = if 1 = n then 1 else 0 := coeff_monomial
6969

7070
lemma coeff_C : coeff (C a) n = ite (n = 0) a 0 :=
71-
by { convert coeff_single using 2, simp [eq_comm], }
71+
by { convert coeff_monomial using 2, simp [eq_comm], }
7272

73-
@[simp] lemma coeff_C_zero : coeff (C a) 0 = a := coeff_single
73+
@[simp] lemma coeff_C_zero : coeff (C a) 0 = a := coeff_monomial
7474

7575
theorem nonzero.of_polynomial_ne (h : p ≠ q) : nontrivial R :=
7676
⟨⟨0, 1, λ h01 : 0 = 1, h $
@@ -79,7 +79,7 @@ theorem nonzero.of_polynomial_ne (h : p ≠ q) : nontrivial R :=
7979
lemma single_eq_C_mul_X : ∀{n}, monomial n a = C a * X^n
8080
| 0 := (mul_one _).symm
8181
| (n+1) :=
82-
calc monomial (n + 1) a = monomial n a * X : by rw [X, single_mul_single, mul_one]
82+
calc monomial (n + 1) a = monomial n a * X : by { rw [X, monomial_mul_monomial, mul_one], }
8383
... = (C a * X^n) * X : by rw [single_eq_C_mul_X]
8484
... = C a * X^(n+1) : by simp only [pow_add, mul_assoc, pow_one]
8585

src/ring_theory/polynomial_algebra.lean

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Authors: Scott Morrison
55
-/
66
import ring_theory.tensor_product
77
import ring_theory.matrix_algebra
8-
import data.polynomial.algebra_map
8+
import data.polynomial
99

1010
/-!
1111
# Algebra isomorphism between matrices of polynomials and polynomials of matrices
@@ -132,8 +132,10 @@ begin
132132
simp only [lift.tmul],
133133
dsimp [to_fun_bilinear, to_fun_linear_right, to_fun],
134134
ext k,
135-
simp_rw [coeff_sum, coeff_single, finsupp.sum,
136-
finset.sum_ite_eq', finsupp.mem_support_iff, ne.def, coeff_mul, finset_sum_coeff, coeff_single,
135+
-- TODO This is a bit annoying: the polynomial API is breaking down.
136+
have apply_eq_coeff : ∀ {p : ℕ →₀ R} {n : ℕ}, p n = coeff p n := by { intros, refl },
137+
simp_rw [coeff_sum, coeff_monomial, finsupp.sum, finset.sum_ite_eq', finsupp.mem_support_iff,
138+
ne.def, coeff_mul, finset_sum_coeff, coeff_monomial,
137139
finset.sum_ite_eq', finsupp.mem_support_iff, ne.def,
138140
mul_ite, mul_zero, ite_mul, zero_mul, apply_eq_coeff],
139141
simp_rw [ite_mul_zero_left (¬coeff p₁ _ = 0) (a₁ * (algebra_map R A) (coeff p₁ _))],
@@ -147,7 +149,7 @@ begin
147149
dsimp [to_fun_linear],
148150
simp only [lift.tmul],
149151
dsimp [to_fun_bilinear, to_fun_linear_right, to_fun],
150-
rw [← C_1, ←monomial_zero_left, finsupp.sum_single_index],
152+
rw [← C_1, ←monomial_zero_left, monomial, finsupp.sum_single_index],
151153
{ simp, refl, },
152154
{ simp, },
153155
end
@@ -208,6 +210,7 @@ begin
208210
rw [inv_fun, eval₂_monomial, alg_hom.coe_to_ring_hom, algebra.tensor_product.include_left_apply,
209211
algebra.tensor_product.tmul_pow, one_pow, algebra.tensor_product.tmul_mul_tmul,
210212
mul_one, one_mul, to_fun_alg_hom_apply_tmul, ←monomial_one_eq_X_pow],
213+
dsimp [monomial],
211214
rw [finsupp.sum_single_index]; simp, }
212215
end
213216

@@ -294,7 +297,7 @@ begin
294297
{ intros p q hp hq, ext,
295298
simp [hp, hq, coeff_add, add_val, std_basis_matrix_add], },
296299
{ intros k x,
297-
simp only [mat_poly_equiv_coeff_apply_aux_1, coeff_single],
300+
simp only [mat_poly_equiv_coeff_apply_aux_1, coeff_monomial],
298301
split_ifs; { funext, simp, }, }
299302
end
300303

0 commit comments

Comments
 (0)