Skip to content

Commit 7017600

Browse files
committed
perf: improve some instances in GroupTheory/Congruence (#6874)
1 parent b3b5ba1 commit 7017600

1 file changed

Lines changed: 35 additions & 17 deletions

File tree

Mathlib/GroupTheory/Congruence.lean

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -732,10 +732,8 @@ variable [MulOneClass M] [MulOneClass N] [MulOneClass P] (c : Con M)
732732
/-- The quotient of a monoid by a congruence relation is a monoid. -/
733733
@[to_additive "The quotient of an `AddMonoid` by an additive congruence relation is
734734
an `AddMonoid`."]
735-
instance mulOneClass : MulOneClass c.Quotient
736-
where
735+
instance mulOneClass : MulOneClass c.Quotient where
737736
one := ((1 : M) : c.Quotient)
738-
mul := (· * ·)
739737
mul_one x := Quotient.inductionOn' x fun _ => congr_arg ((↑) : M → c.Quotient) <| mul_one _
740738
one_mul x := Quotient.inductionOn' x fun _ => congr_arg ((↑) : M → c.Quotient) <| one_mul _
741739
#align con.mul_one_class Con.mulOneClass
@@ -1148,7 +1146,7 @@ protected theorem pow {M : Type*} [Monoid M] (c : Con M) :
11481146
#align add_con.nsmul AddCon.nsmul
11491147

11501148
@[to_additive]
1151-
instance [MulOneClass M] (c : Con M) : One c.Quotient where
1149+
instance one [MulOneClass M] (c : Con M) : One c.Quotient where
11521150
-- Using Quotient.mk'' here instead of c.toQuotient
11531151
-- since c.toQuotient is not reducible.
11541152
-- This would lead to non-defeq diamonds since this instance ends up in
@@ -1176,32 +1174,52 @@ instance {M : Type*} [Monoid M] (c : Con M) : Pow c.Quotient ℕ where
11761174
@[to_additive "The quotient of an `AddSemigroup` by an additive congruence relation is
11771175
an `AddSemigroup`."]
11781176
instance semigroup {M : Type*} [Semigroup M] (c : Con M) : Semigroup c.Quotient :=
1179-
Function.Surjective.semigroup _ Quotient.surjective_Quotient_mk'' fun _ _ => rfl
1177+
{ (Function.Surjective.semigroup _ Quotient.surjective_Quotient_mk'' fun _ _ => rfl :
1178+
Semigroup c.Quotient) with
1179+
/- The `toMul` field is given explicitly for performance reasons.
1180+
This avoids any need to unfold `Function.Surjective.semigroup` when the type checker is checking
1181+
that instance diagrams commute -/
1182+
toMul := Con.hasMul _ }
11801183
#align con.semigroup Con.semigroup
11811184
#align add_con.add_semigroup AddCon.addSemigroup
11821185

11831186
/-- The quotient of a commutative semigroup by a congruence relation is a semigroup. -/
11841187
@[to_additive "The quotient of an `AddCommSemigroup` by an additive congruence relation is
11851188
an `AddCommSemigroup`."]
11861189
instance commSemigroup {M : Type*} [CommSemigroup M] (c : Con M) : CommSemigroup c.Quotient :=
1187-
Function.Surjective.commSemigroup _ Quotient.surjective_Quotient_mk'' fun _ _ => rfl
1190+
{ (Function.Surjective.commSemigroup _ Quotient.surjective_Quotient_mk'' fun _ _ => rfl :
1191+
CommSemigroup c.Quotient) with
1192+
/- The `toSemigroup` field is given explicitly for performance reasons.
1193+
This avoids any need to unfold `Function.Surjective.commSemigroup` when the type checker is
1194+
checking that instance diagrams commute -/
1195+
toSemigroup := Con.semigroup _ }
11881196
#align con.comm_semigroup Con.commSemigroup
11891197
#align add_con.add_comm_semigroup AddCon.addCommSemigroup
11901198

11911199
/-- The quotient of a monoid by a congruence relation is a monoid. -/
11921200
@[to_additive "The quotient of an `AddMonoid` by an additive congruence relation is
11931201
an `AddMonoid`."]
11941202
instance monoid {M : Type*} [Monoid M] (c : Con M) : Monoid c.Quotient :=
1195-
Function.Surjective.monoid _ Quotient.surjective_Quotient_mk'' rfl (fun _ _ => rfl) fun _ _ => rfl
1203+
{ (Function.Surjective.monoid _ Quotient.surjective_Quotient_mk'' rfl
1204+
(fun _ _ => rfl) fun _ _ => rfl : Monoid c.Quotient) with
1205+
/- The `toSemigroup` and `toOne` fields are given explicitly for performance reasons.
1206+
This avoids any need to unfold `Function.Surjective.monoid` when the type checker is
1207+
checking that instance diagrams commute -/
1208+
toSemigroup := Con.semigroup _
1209+
toOne := Con.one _ }
11961210
#align con.monoid Con.monoid
11971211
#align add_con.add_monoid AddCon.addMonoid
11981212

11991213
/-- The quotient of a `CommMonoid` by a congruence relation is a `CommMonoid`. -/
12001214
@[to_additive "The quotient of an `AddCommMonoid` by an additive congruence
12011215
relation is an `AddCommMonoid`."]
12021216
instance commMonoid {M : Type*} [CommMonoid M] (c : Con M) : CommMonoid c.Quotient :=
1203-
Function.Surjective.commMonoid _ Quotient.surjective_Quotient_mk'' rfl (fun _ _ => rfl) fun _ _ =>
1204-
rfl
1217+
{ (Function.Surjective.commMonoid _ Quotient.surjective_Quotient_mk'' rfl
1218+
(fun _ _ => rfl) fun _ _ => rfl : CommMonoid c.Quotient) with
1219+
/- The `toMonoid` field is given explicitly for performance reasons.
1220+
This avoids any need to unfold `Function.Surjective.commMonoid` when the type checker is
1221+
checking that instance diagrams commute -/
1222+
toMonoid := Con.monoid _ }
12051223
#align con.comm_monoid Con.commMonoid
12061224
#align add_con.add_comm_monoid AddCon.addCommMonoid
12071225

@@ -1291,9 +1309,12 @@ instance zpowinst : Pow c.Quotient ℤ :=
12911309
@[to_additive "The quotient of an `AddGroup` by an additive congruence relation is
12921310
an `AddGroup`."]
12931311
instance group : Group c.Quotient :=
1294-
Function.Surjective.group Quotient.mk''
1295-
Quotient.surjective_Quotient_mk'' rfl (fun _ _ => rfl) (fun _ => rfl)
1296-
(fun _ _ => rfl) (fun _ _ => rfl) fun _ _ => rfl
1312+
{ (Function.Surjective.group Quotient.mk''
1313+
Quotient.surjective_Quotient_mk'' rfl (fun _ _ => rfl) (fun _ => rfl)
1314+
(fun _ _ => rfl) (fun _ _ => rfl) fun _ _ => rfl : Group c.Quotient) with
1315+
toMonoid := Con.monoid _
1316+
toInv := Con.hasInv _
1317+
toDiv := Con.hasDiv _ }
12971318
#align con.group Con.group
12981319
#align add_con.add_group AddCon.addGroup
12991320

@@ -1371,18 +1392,15 @@ theorem coe_smul {α M : Type*} [MulOneClass M] [SMul α M] [IsScalarTower α M
13711392

13721393
@[to_additive]
13731394
instance mulAction {α M : Type*} [Monoid α] [MulOneClass M] [MulAction α M] [IsScalarTower α M M]
1374-
(c : Con M) : MulAction α c.Quotient
1375-
where
1376-
smul := (· • ·)
1395+
(c : Con M) : MulAction α c.Quotient where
13771396
one_smul := Quotient.ind' fun _ => congr_arg Quotient.mk'' <| one_smul _ _
13781397
mul_smul _ _ := Quotient.ind' fun _ => congr_arg Quotient.mk'' <| mul_smul _ _ _
13791398
#align con.mul_action Con.mulAction
13801399
#align add_con.add_action AddCon.addAction
13811400

13821401
instance mulDistribMulAction {α M : Type*} [Monoid α] [Monoid M] [MulDistribMulAction α M]
13831402
[IsScalarTower α M M] (c : Con M) : MulDistribMulAction α c.Quotient :=
1384-
{ c.mulAction with
1385-
smul_one := fun _ => congr_arg Quotient.mk'' <| smul_one _
1403+
{ smul_one := fun _ => congr_arg Quotient.mk'' <| smul_one _
13861404
smul_mul := fun _ => Quotient.ind₂' fun _ _ => congr_arg Quotient.mk'' <| smul_mul' _ _ _ }
13871405
#align con.mul_distrib_mul_action Con.mulDistribMulAction
13881406

0 commit comments

Comments
 (0)