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

Commit 8836a42

Browse files
committed
fix(linear_algebra/quadratic_form/basic): align diamonds in the nat- and int- action (#12541)
This also provides `fun_like` and `zero_hom_class` instances. The `has_scalar` code has been moved unchanged from further down in the file. This change makes `coe_fn_sub` eligible for `dsimp`, since it can now be proved by `rfl`.
1 parent 9e28852 commit 8836a42

File tree

1 file changed

+71
-51
lines changed

1 file changed

+71
-51
lines changed

src/linear_algebra/quadratic_form/basic.lean

Lines changed: 71 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,16 @@ namespace quadratic_form
103103

104104
variables {Q : quadratic_form R M}
105105

106+
instance fun_like : fun_like (quadratic_form R M) M (λ _, R) :=
107+
{ coe := to_fun,
108+
coe_injective' := λ x y h, by cases x; cases y; congr' }
109+
110+
/-- Helper instance for when there's too many metavariables to apply
111+
`fun_like.has_coe_to_fun` directly. -/
106112
instance : has_coe_to_fun (quadratic_form R M) (λ _, M → R) := ⟨to_fun⟩
107113

108114
/-- The `simp` normal form for a quadratic form is `coe_fn`, not `to_fun`. -/
109-
@[simp] lemma to_fun_eq_apply : Q.to_fun = ⇑ Q := rfl
115+
@[simp] lemma to_fun_eq_coe : Q.to_fun = ⇑ Q := rfl
110116

111117
lemma map_smul (a : R) (x : M) : Q (a • x) = a * a * Q x := Q.to_fun_smul a x
112118

@@ -116,6 +122,10 @@ by { rw [←one_smul R x, ←add_smul, map_smul], norm_num }
116122
@[simp] lemma map_zero : Q 0 = 0 :=
117123
by rw [←@zero_smul R _ _ _ _ (0 : M), map_smul, zero_mul, zero_mul]
118124

125+
instance zero_hom_class : zero_hom_class (quadratic_form R M) M R :=
126+
{ map_zero := λ _, map_zero,
127+
..quadratic_form.fun_like }
128+
119129
@[simp] lemma map_neg (x : M) : Q (-x) = Q x :=
120130
by rw [←@neg_one_smul R _ _ _ _ x, map_smul, neg_one_mul, neg_neg, one_mul]
121131

@@ -200,12 +210,48 @@ end of_tower
200210

201211
variable {Q' : quadratic_form R M}
202212

203-
@[ext] lemma ext (H : ∀ (x : M), Q x = Q' x) : Q = Q' :=
204-
by { cases Q, cases Q', congr, funext, apply H }
213+
@[ext] lemma ext (H : ∀ (x : M), Q x = Q' x) : Q = Q' := fun_like.ext _ _ H
214+
215+
lemma congr_fun (h : Q = Q') (x : M) : Q x = Q' x := fun_like.congr_fun h _
205216

206-
lemma congr_fun (h : Q = Q') (x : M) : Q x = Q' x := h ▸ rfl
217+
lemma ext_iff : Q = Q' ↔ (∀ x, Q x = Q' x) := fun_like.ext_iff
207218

208-
lemma ext_iff : Q = Q' ↔ (∀ x, Q x = Q' x) := ⟨congr_fun, ext⟩
219+
/-- Copy of a `quadratic_form` with a new `to_fun` equal to the old one. Useful to fix definitional
220+
equalities. -/
221+
protected def copy (Q : quadratic_form R M) (Q' : M → R) (h : Q' = ⇑Q) : quadratic_form R M :=
222+
{ to_fun := Q',
223+
to_fun_smul := h.symm ▸ Q.to_fun_smul,
224+
polar_add_left' := h.symm ▸ Q.polar_add_left',
225+
polar_smul_left' := h.symm ▸ Q.polar_smul_left',
226+
polar_add_right' := h.symm ▸ Q.polar_add_right',
227+
polar_smul_right' := h.symm ▸ Q.polar_smul_right' }
228+
229+
section has_scalar
230+
231+
variables [monoid S] [distrib_mul_action S R] [smul_comm_class S R R]
232+
233+
/-- `quadratic_form R M` inherits the scalar action from any algebra over `R`.
234+
235+
When `R` is commutative, this provides an `R`-action via `algebra.id`. -/
236+
instance : has_scalar S (quadratic_form R M) :=
237+
⟨ λ a Q,
238+
{ to_fun := a • Q,
239+
to_fun_smul := λ b x, by rw [pi.smul_apply, map_smul, pi.smul_apply, mul_smul_comm],
240+
polar_add_left' := λ x x' y, by simp only [polar_smul, polar_add_left, smul_add],
241+
polar_smul_left' := λ b x y, begin
242+
simp only [polar_smul, polar_smul_left, ←mul_smul_comm, smul_eq_mul],
243+
end,
244+
polar_add_right' := λ x y y', by simp only [polar_smul, polar_add_right, smul_add],
245+
polar_smul_right' := λ b x y, begin
246+
simp only [polar_smul, polar_smul_right, ←mul_smul_comm, smul_eq_mul],
247+
end } ⟩
248+
249+
@[simp] lemma coe_fn_smul (a : S) (Q : quadratic_form R M) : ⇑(a • Q) = a • Q := rfl
250+
251+
@[simp] lemma smul_apply (a : S) (Q : quadratic_form R M) (x : M) :
252+
(a • Q) x = a • Q x := rfl
253+
254+
end has_scalar
209255

210256
instance : has_zero (quadratic_form R M) :=
211257
⟨ { to_fun := λ x, 0,
@@ -242,36 +288,31 @@ instance : has_add (quadratic_form R M) :=
242288
instance : has_neg (quadratic_form R M) :=
243289
⟨ λ Q,
244290
{ to_fun := -Q,
245-
to_fun_smul := λ a x,
246-
by simp only [pi.neg_apply, map_smul, mul_neg],
247-
polar_add_left' := λ x x' y,
248-
by simp only [polar_neg, polar_add_left, neg_add],
249-
polar_smul_left' := λ a x y,
250-
by simp only [polar_neg, polar_smul_left, mul_neg, smul_eq_mul],
251-
polar_add_right' := λ x y y',
252-
by simp only [polar_neg, polar_add_right, neg_add],
253-
polar_smul_right' := λ a x y,
254-
by simp only [polar_neg, polar_smul_right, mul_neg, smul_eq_mul] } ⟩
291+
to_fun_smul := λ a x,
292+
by simp only [pi.neg_apply, map_smul, mul_neg],
293+
polar_add_left' := λ x x' y,
294+
by simp only [polar_neg, polar_add_left, neg_add],
295+
polar_smul_left' := λ a x y,
296+
by simp only [polar_neg, polar_smul_left, mul_neg, smul_eq_mul],
297+
polar_add_right' := λ x y y',
298+
by simp only [polar_neg, polar_add_right, neg_add],
299+
polar_smul_right' := λ a x y,
300+
by simp only [polar_neg, polar_smul_right, mul_neg, smul_eq_mul] } ⟩
255301

256302
@[simp] lemma coe_fn_neg (Q : quadratic_form R M) : ⇑(-Q) = -Q := rfl
257303

258304
@[simp] lemma neg_apply (Q : quadratic_form R M) (x : M) : (-Q) x = -Q x := rfl
259305

260-
instance : add_comm_group (quadratic_form R M) :=
261-
{ add := (+),
262-
zero := 0,
263-
neg := has_neg.neg,
264-
add_comm := λ Q Q', by { ext, simp only [add_apply, add_comm] },
265-
add_assoc := λ Q Q' Q'', by { ext, simp only [add_apply, add_assoc] },
266-
add_left_neg := λ Q, by { ext, simp only [add_apply, neg_apply, zero_apply, add_left_neg] },
267-
add_zero := λ Q, by { ext, simp only [zero_apply, add_apply, add_zero] },
268-
zero_add := λ Q, by { ext, simp only [zero_apply, add_apply, zero_add] } }
306+
instance : has_sub (quadratic_form R M) :=
307+
⟨ λ Q Q', (Q + -Q').copy (Q - Q') (sub_eq_add_neg _ _) ⟩
308+
309+
@[simp] lemma coe_fn_sub (Q Q' : quadratic_form R M) : ⇑(Q - Q') = Q - Q' := rfl
269310

270-
@[simp] lemma coe_fn_sub (Q Q' : quadratic_form R M) : ⇑(Q - Q') = Q - Q' :=
271-
by simp only [quadratic_form.coe_fn_neg, add_left_inj, quadratic_form.coe_fn_add, sub_eq_add_neg]
311+
@[simp] lemma sub_apply (Q Q' : quadratic_form R M) (x : M) : (Q - Q') x = Q x - Q' x := rfl
272312

273-
@[simp] lemma sub_apply (Q Q' : quadratic_form R M) (x : M) : (Q - Q') x = Q x - Q' x :=
274-
by simp only [quadratic_form.neg_apply, add_left_inj, quadratic_form.add_apply, sub_eq_add_neg]
313+
instance : add_comm_group (quadratic_form R M) :=
314+
fun_like.coe_injective.add_comm_group _
315+
coe_fn_zero coe_fn_add coe_fn_neg coe_fn_sub (λ _ _, coe_fn_smul _ _) (λ _ _, coe_fn_smul _ _)
275316

276317
/-- `@coe_fn (quadratic_form R M)` as an `add_monoid_hom`.
277318
@@ -299,38 +340,17 @@ open_locale big_operators
299340

300341
end sum
301342

302-
section has_scalar
343+
section distrib_mul_action
303344

304345
variables [monoid S] [distrib_mul_action S R] [smul_comm_class S R R]
305346

306-
/-- `quadratic_form R M` inherits the scalar action from any algebra over `R`.
307-
308-
When `R` is commutative, this provides an `R`-action via `algebra.id`. -/
309-
instance : has_scalar S (quadratic_form R M) :=
310-
⟨ λ a Q,
311-
{ to_fun := a • Q,
312-
to_fun_smul := λ b x, by rw [pi.smul_apply, map_smul, pi.smul_apply, mul_smul_comm],
313-
polar_add_left' := λ x x' y, by simp only [polar_smul, polar_add_left, smul_add],
314-
polar_smul_left' := λ b x y, begin
315-
simp only [polar_smul, polar_smul_left, ←mul_smul_comm, smul_eq_mul],
316-
end,
317-
polar_add_right' := λ x y y', by simp only [polar_smul, polar_add_right, smul_add],
318-
polar_smul_right' := λ b x y, begin
319-
simp only [polar_smul, polar_smul_right, ←mul_smul_comm, smul_eq_mul],
320-
end } ⟩
321-
322-
@[simp] lemma coe_fn_smul (a : S) (Q : quadratic_form R M) : ⇑(a • Q) = a • Q := rfl
323-
324-
@[simp] lemma smul_apply (a : S) (Q : quadratic_form R M) (x : M) :
325-
(a • Q) x = a • Q x := rfl
326-
327347
instance : distrib_mul_action S (quadratic_form R M) :=
328348
{ mul_smul := λ a b Q, ext (λ x, by simp only [smul_apply, mul_smul]),
329349
one_smul := λ Q, ext (λ x, by simp only [quadratic_form.smul_apply, one_smul]),
330350
smul_add := λ a Q Q', by { ext, simp only [add_apply, smul_apply, smul_add] },
331351
smul_zero := λ a, by { ext, simp only [zero_apply, smul_apply, smul_zero] }, }
332352

333-
end has_scalar
353+
end distrib_mul_action
334354

335355
section module
336356

0 commit comments

Comments
 (0)