Skip to content

Commit 37be209

Browse files
committed
chore(DirectSum): clean up instances (leanprover-community#37135)
Clean up instances on `DirectSum` using `InferInstanceAs`. Clean up instances in `TensorProduct/Graded`, which depend on the `DirectSum` instances. Golf away some `backward` options.
1 parent f9b278b commit 37be209

6 files changed

Lines changed: 54 additions & 114 deletions

File tree

Mathlib/Algebra/DirectSum/Basic.lean

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ variable {β}
105105
theorem add_apply (g₁ g₂ : ⨁ i, β i) (i : ι) : (g₁ + g₂) i = g₁ i + g₂ i :=
106106
rfl
107107

108+
@[simp]
109+
theorem sum_apply {α} (s : Finset α) (g : α → ⨁ i, β i) (i : ι) :
110+
(∑ a ∈ s, g a) i = ∑ a ∈ s, g a i :=
111+
DFinsupp.finset_sum_apply s g i
112+
108113
section DecidableEq
109114

110115
variable [DecidableEq ι]
@@ -135,16 +140,12 @@ lemma of_apply {i : ι} (j : ι) (x : β i) : of β i x j = if h : i = j then Eq
135140
DFinsupp.single_apply
136141

137142
theorem mk_apply_of_mem {s : Finset ι} {f : ∀ i : (↑s : Set ι), β i.val} {n : ι} (hn : n ∈ s) :
138-
mk β s f n = f ⟨n, hn⟩ := by
139-
dsimp only [Finset.coe_sort_coe, mk, AddMonoidHom.coe_mk, ZeroHom.coe_mk]
140-
-- Previously, `DFinsupp.mk_apply` was in the `dsimp only`
141-
rw [DFinsupp.mk_apply, dif_pos hn]
143+
mk β s f n = f ⟨n, hn⟩ :=
144+
DFinsupp.mk_of_mem hn
142145

143146
theorem mk_apply_of_notMem {s : Finset ι} {f : ∀ i : (↑s : Set ι), β i.val} {n : ι} (hn : n ∉ s) :
144-
mk β s f n = 0 := by
145-
dsimp only [Finset.coe_sort_coe, mk, AddMonoidHom.coe_mk, ZeroHom.coe_mk]
146-
-- Previously, `DFinsupp.mk_apply` was in the `dsimp only`
147-
rw [DFinsupp.mk_apply, dif_neg hn]
147+
mk β s f n = 0 :=
148+
DFinsupp.mk_of_notMem hn
148149

149150
@[simp]
150151
theorem support_zero [∀ (i : ι) (x : β i), Decidable (x ≠ 0)] : (0 : ⨁ i, β i).support = ∅ :=
@@ -163,11 +164,9 @@ theorem sum_support_of [∀ (i : ι) (x : β i), Decidable (x ≠ 0)] (x : ⨁ i
163164
(∑ i ∈ x.support, of β i (x i)) = x :=
164165
DFinsupp.sum_single
165166

166-
set_option backward.isDefEq.respectTransparency false in
167167
theorem sum_univ_of [Fintype ι] (x : ⨁ i, β i) :
168168
∑ i ∈ Finset.univ, of β i (x i) = x := by
169-
apply DFinsupp.ext (fun i ↦ ?_)
170-
rw [DFinsupp.finset_sum_apply]
169+
ext i
171170
simp [of_apply]
172171

173172
theorem mk_injective (s : Finset ι) : Function.Injective (mk β s) :=
@@ -217,12 +216,13 @@ def toAddMonoid : (⨁ i, β i) →+ γ :=
217216
theorem toAddMonoid_of (i) (x : β i) : toAddMonoid φ (of β i x) = φ i x :=
218217
DFinsupp.liftAddHom_apply_single φ i x
219218

220-
set_option backward.isDefEq.respectTransparency false in
221219
theorem toAddMonoid.unique (f : ⨁ i, β i) : ψ f = toAddMonoid (fun i => ψ.comp (of β i)) f := by
222220
congr
223221
-- Porting note (https://github.com/leanprover-community/mathlib4/issues/11041): `ext` applies addHom_ext' here, which isn't what we want.
224222
apply DFinsupp.addHom_ext'
225-
simp [toAddMonoid, of]
223+
intro
224+
simp [toAddMonoid]
225+
rfl
226226

227227
lemma toAddMonoid_injective : Injective (toAddMonoid : (∀ i, β i →+ γ) → (⨁ i, β i) →+ γ) :=
228228
DFinsupp.liftAddHom.injective

Mathlib/Algebra/DirectSum/Decomposition.lean

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,6 @@ theorem AddSubmonoidClass.IsHomogeneous.ext
207207

208208
end AddCommMonoid
209209

210-
/-- The `-` in the statements below doesn't resolve without this line.
211-
212-
This seems to be a problem of synthesized vs inferred typeclasses disagreeing. If we replace
213-
the statement of `decompose_neg` with `@Eq (⨁ i, ℳ i) (decompose ℳ (-x)) (-decompose ℳ x)`
214-
instead of `decompose ℳ (-x) = -decompose ℳ x`, which forces the typeclasses needed by `⨁ i, ℳ i`
215-
to be found by unification rather than synthesis, then everything works fine without this
216-
instance. -/
217-
instance addCommGroupSetLike [AddCommGroup M] [SetLike σ M] [AddSubgroupClass σ M] (ℳ : ι → σ) :
218-
AddCommGroup (⨁ i, ℳ i) := by infer_instance
219-
220210
section AddCommGroup
221211

222212
variable [DecidableEq ι] [AddCommGroup M]

Mathlib/Algebra/DirectSum/Module.lean

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ variable {ι : Type v}
4040
variable {M : ι → Type w} [∀ i, AddCommMonoid (M i)] [∀ i, Module R (M i)]
4141

4242
instance : Module R (⨁ i, M i) :=
43-
DFinsupp.module
43+
inferInstanceAs <| Module R (Π₀ i, M i)
4444

4545
instance {S : Type*} [Semiring S] [∀ i, Module S (M i)] [∀ i, SMulCommClass R S (M i)] :
4646
SMulCommClass R S (⨁ i, M i) :=
47-
DFinsupp.smulCommClass
47+
inferInstanceAs <| SMulCommClass R S (Π₀ i, M i)
4848

4949
instance {S : Type*} [Semiring S] [SMul R S] [∀ i, Module S (M i)] [∀ i, IsScalarTower R S (M i)] :
5050
IsScalarTower R S (⨁ i, M i) :=
51-
DFinsupp.isScalarTower
51+
inferInstanceAs <| IsScalarTower R S (Π₀ i, M i)
5252

5353
instance [∀ i, Module Rᵐᵒᵖ (M i)] [∀ i, IsCentralScalar R (M i)] : IsCentralScalar R (⨁ i, M i) :=
54-
DFinsupp.isCentralScalar
54+
inferInstanceAs <| IsCentralScalar R (Π₀ i, M i)
5555

5656
theorem smul_apply (b : R) (v : ⨁ i, M i) (i : ι) : (b • v) i = b • v i :=
5757
DFinsupp.smul_apply _ _ _

Mathlib/Algebra/DirectSum/Ring.lean

Lines changed: 31 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,12 @@ def mulHom : (⨁ i, A i) →+ (⨁ i, A i) →+ ⨁ i, A i :=
189189
instance instMul : Mul (⨁ i, A i) where
190190
mul := fun a b => mulHom A a b
191191

192-
instance : NonUnitalNonAssocSemiring (⨁ i, A i) :=
193-
{ (inferInstance : AddCommMonoid _) with
194-
zero_mul := fun _ => by simp only [Mul.mul, HMul.hMul, map_zero, AddMonoidHom.zero_apply]
195-
mul_zero := fun _ => by simp only [Mul.mul, HMul.hMul, map_zero]
196-
left_distrib := fun _ _ _ => by simp only [Mul.mul, HMul.hMul, map_add]
197-
right_distrib := fun _ _ _ => by
198-
simp only [Mul.mul, HMul.hMul, map_add, AddMonoidHom.add_apply] }
192+
instance : NonUnitalNonAssocSemiring (⨁ i, A i) where
193+
zero_mul := fun _ => by simp only [Mul.mul, HMul.hMul, map_zero, AddMonoidHom.zero_apply]
194+
mul_zero := fun _ => by simp only [Mul.mul, HMul.hMul, map_zero]
195+
left_distrib := fun _ _ _ => by simp only [Mul.mul, HMul.hMul, map_add]
196+
right_distrib := fun _ _ _ => by
197+
simp only [Mul.mul, HMul.hMul, map_add, AddMonoidHom.add_apply]
199198

200199
variable {A}
201200

@@ -217,23 +216,20 @@ variable [∀ i, AddCommMonoid (A i)] [AddMonoid ι] [GSemiring A]
217216

218217
open AddMonoidHom (flipHom coe_comp compHom flip_apply)
219218

220-
set_option backward.privateInPublic true in
221219
private nonrec theorem one_mul (x : ⨁ i, A i) : 1 * x = x := by
222220
suffices mulHom A One.one = AddMonoidHom.id (⨁ i, A i) from DFunLike.congr_fun this x
223221
apply addHom_ext; intro i xi
224222
simp only [One.one]
225223
rw [mulHom_of_of]
226224
exact of_eq_of_gradedMonoid_eq (one_mul <| GradedMonoid.mk i xi)
227225

228-
set_option backward.privateInPublic true in
229226
private nonrec theorem mul_one (x : ⨁ i, A i) : x * 1 = x := by
230227
suffices (mulHom A).flip One.one = AddMonoidHom.id (⨁ i, A i) from DFunLike.congr_fun this x
231228
apply addHom_ext; intro i xi
232229
simp only [One.one]
233230
rw [flip_apply, mulHom_of_of]
234231
exact of_eq_of_gradedMonoid_eq (mul_one <| GradedMonoid.mk i xi)
235232

236-
set_option backward.privateInPublic true in
237233
private theorem mul_assoc (a b c : ⨁ i, A i) : a * b * c = a * (b * c) := by
238234
-- (`fun a b c => a * b * c` as a bundled hom) = (`fun a b c => a * (b * c)` as a bundled hom)
239235
suffices AddMonoidHom.mulLeft₃ = AddMonoidHom.mulRight₃ by
@@ -248,20 +244,17 @@ private theorem mul_assoc (a b c : ⨁ i, A i) : a * b * c = a * (b * c) := by
248244
instance instNatCast : NatCast (⨁ i, A i) where
249245
natCast := fun n => of _ _ (GSemiring.natCast n)
250246

251-
set_option backward.privateInPublic true in
252-
set_option backward.privateInPublic.warn false in
253247
/-- The `Semiring` structure derived from `GSemiring A`. -/
254-
instance semiring : Semiring (⨁ i, A i) :=
255-
{ (inferInstance : NonUnitalNonAssocSemiring _) with
256-
one_mul := one_mul A
257-
mul_one := mul_one A
258-
mul_assoc := mul_assoc A
259-
toNatCast := instNatCast _
260-
natCast_zero := by simp only [NatCast.natCast, GSemiring.natCast_zero, map_zero]
261-
natCast_succ := fun n => by
262-
simp_rw [NatCast.natCast, GSemiring.natCast_succ]
263-
rw [map_add]
264-
rfl }
248+
instance semiring : Semiring (⨁ i, A i) where
249+
one_mul := private one_mul A
250+
mul_one := private mul_one A
251+
mul_assoc := private mul_assoc A
252+
toNatCast := instNatCast _
253+
natCast_zero := by simp only [NatCast.natCast, GSemiring.natCast_zero, map_zero]
254+
natCast_succ := fun n => by
255+
simp_rw [NatCast.natCast, GSemiring.natCast_succ]
256+
rw [map_add]
257+
rfl
265258

266259
theorem ofPow {i} (a : A i) (n : ℕ) :
267260
of _ i a ^ n = of _ (n • i) (GradedMonoid.GMonoid.gnpow _ a) := by
@@ -297,10 +290,8 @@ theorem mul_eq_dfinsuppSum [∀ (i : ι) (x : A i), Decidable (x ≠ 0)] (a a' :
297290
dsimp only [DirectSum]
298291
rw [DFinsupp.sumAddHom_apply, AddMonoidHom.dfinsuppSum_apply]
299292
apply congrArg _
300-
simp_rw [flip_apply]
301293
funext x
302-
simp [AddMonoidHom.dfinsuppSum_apply, DFinsupp.sumAddHom_apply, DirectSum.gMulHom,
303-
DirectSum.toAddMonoid]
294+
simp [AddMonoidHom.dfinsuppSum_apply, DFinsupp.sumAddHom_apply, DirectSum.toAddMonoid]
304295

305296
/-- A heavily unfolded version of the definition of multiplication -/
306297
theorem mul_eq_sum_support_ghas_mul [∀ (i : ι) (x : A i), Decidable (x ≠ 0)] (a a' : ⨁ i, A i) :
@@ -315,20 +306,16 @@ section CommSemiring
315306

316307
variable [∀ i, AddCommMonoid (A i)] [AddCommMonoid ι] [GCommSemiring A]
317308

318-
set_option backward.privateInPublic true in
319309
private theorem mul_comm (a b : ⨁ i, A i) : a * b = b * a := by
320310
suffices mulHom A = (mulHom A).flip by
321311
rw [← mulHom_apply, this, AddMonoidHom.flip_apply, mulHom_apply]
322312
apply addHom_ext; intro ai ax; apply addHom_ext; intro bi bx
323313
rw [AddMonoidHom.flip_apply, mulHom_of_of, mulHom_of_of]
324314
exact of_eq_of_gradedMonoid_eq (GCommSemiring.mul_comm ⟨ai, ax⟩ ⟨bi, bx⟩)
325315

326-
set_option backward.privateInPublic true in
327-
set_option backward.privateInPublic.warn false in
328316
/-- The `CommSemiring` structure derived from `GCommSemiring A`. -/
329-
instance commSemiring : CommSemiring (⨁ i, A i) :=
330-
{ DirectSum.semiring A with
331-
mul_comm := mul_comm A }
317+
instance commSemiring : CommSemiring (⨁ i, A i) where
318+
mul_comm := private mul_comm A
332319

333320
end CommSemiring
334321

@@ -337,9 +324,7 @@ section NonUnitalNonAssocRing
337324
variable [∀ i, AddCommGroup (A i)] [Add ι] [GNonUnitalNonAssocSemiring A]
338325

339326
/-- The `Ring` derived from `GSemiring A`. -/
340-
instance nonAssocRing : NonUnitalNonAssocRing (⨁ i, A i) :=
341-
{ (inferInstance : NonUnitalNonAssocSemiring (⨁ i, A i)),
342-
(inferInstance : AddCommGroup (⨁ i, A i)) with }
327+
instance nonAssocRing : NonUnitalNonAssocRing (⨁ i, A i) where
343328

344329
end NonUnitalNonAssocRing
345330

@@ -349,13 +334,11 @@ variable [∀ i, AddCommGroup (A i)] [AddMonoid ι] [GRing A]
349334

350335
-- Porting note: overspecified fields in ml4
351336
/-- The `Ring` derived from `GSemiring A`. -/
352-
instance ring : Ring (⨁ i, A i) :=
353-
{ DirectSum.semiring A,
354-
(inferInstance : AddCommGroup (⨁ i, A i)) with
355-
toIntCast.intCast := fun z => of A 0 <| (GRing.intCast z)
356-
intCast_ofNat := fun _ => congrArg (of A 0) <| GRing.intCast_ofNat _
357-
intCast_negSucc := fun _ =>
358-
(congrArg (of A 0) <| GRing.intCast_negSucc_ofNat _).trans <| map_neg _ _ }
337+
instance ring : Ring (⨁ i, A i) where
338+
toIntCast.intCast z := of A 0 <| (GRing.intCast z)
339+
intCast_ofNat _ := congrArg (of A 0) <| GRing.intCast_ofNat _
340+
intCast_negSucc _ :=
341+
(congrArg (of A 0) <| GRing.intCast_negSucc_ofNat _).trans <| map_neg _ _
359342

360343
end Ring
361344

@@ -364,9 +347,7 @@ section CommRing
364347
variable [∀ i, AddCommGroup (A i)] [AddCommMonoid ι] [GCommRing A]
365348

366349
/-- The `CommRing` derived from `GCommSemiring A`. -/
367-
instance commRing : CommRing (⨁ i, A i) :=
368-
{ DirectSum.ring A,
369-
DirectSum.commSemiring A with }
350+
instance commRing : CommRing (⨁ i, A i) where
370351

371352
end CommRing
372353

@@ -610,26 +591,22 @@ variable (ι)
610591
/-- A direct sum of copies of a `NonUnitalNonAssocSemiring` inherits the multiplication structure.
611592
-/
612593
instance NonUnitalNonAssocSemiring.directSumGNonUnitalNonAssocSemiring {R : Type*} [AddMonoid ι]
613-
[NonUnitalNonAssocSemiring R] : DirectSum.GNonUnitalNonAssocSemiring fun _ : ι => R :=
614-
{ -- Porting note: removed Mul.gMul ι with and we seem ok
615-
mul_zero := mul_zero
616-
zero_mul := zero_mul
617-
mul_add := mul_add
618-
add_mul := add_mul }
594+
[NonUnitalNonAssocSemiring R] : DirectSum.GNonUnitalNonAssocSemiring fun _ : ι => R where
595+
mul_zero := mul_zero
596+
zero_mul := zero_mul
597+
mul_add := mul_add
598+
add_mul := add_mul
619599

620600
/-- A direct sum of copies of a `Semiring` inherits the multiplication structure. -/
621601
instance Semiring.directSumGSemiring {R : Type*} [AddMonoid ι] [Semiring R] :
622602
DirectSum.GSemiring fun _ : ι => R where
623-
__ := NonUnitalNonAssocSemiring.directSumGNonUnitalNonAssocSemiring ι
624-
__ := Monoid.gMonoid ι
625603
natCast n := n
626604
natCast_zero := Nat.cast_zero
627605
natCast_succ := Nat.cast_succ
628606

629607
/-- A direct sum of copies of a `Ring` inherits the multiplication structure. -/
630608
instance Ring.directSumGRing {R : Type*} [AddMonoid ι] [Ring R] :
631609
DirectSum.GRing fun _ : ι => R where
632-
__ := Semiring.directSumGSemiring ι
633610
intCast z := z
634611
intCast_ofNat := Int.cast_natCast
635612
intCast_negSucc_ofNat := Int.cast_negSucc
@@ -644,13 +621,9 @@ example {R : Type*} [AddMonoid ι] [Semiring R] (i j : ι) (a b : R) :
644621
/-- A direct sum of copies of a `CommSemiring` inherits the commutative multiplication structure. -/
645622
instance CommSemiring.directSumGCommSemiring {R : Type*} [AddCommMonoid ι] [CommSemiring R] :
646623
DirectSum.GCommSemiring fun _ : ι => R where
647-
__ := Semiring.directSumGSemiring ι
648-
__ := CommMonoid.gCommMonoid ι
649624

650625
/-- A direct sum of copies of a `CommRing` inherits the commutative multiplication structure. -/
651626
instance CommRing.directSumGCommRing {R : Type*} [AddCommMonoid ι] [CommRing R] :
652627
DirectSum.GCommRing fun _ : ι => R where
653-
__ := Ring.directSumGRing ι
654-
__ := CommMonoid.gCommMonoid ι
655628

656629
end Uniform

0 commit comments

Comments
 (0)