Skip to content

Commit ee16c22

Browse files
committed
perf: de-instance some WithLp norm structures used for type synonyms (#31819)
The recent refactor of `WithLp` into a one-field structure caused an unexpected slowdown in completely unrelated files. Some sleuthing by @kbuzzard revealed that this was caused by Lean performing unification on some new instances which are only intended for type synonyms. Because these are only intended for synonyms, we de-instance them here, and they can be copied over when a user creates a synonym. During the investigation, we also uncovered that one of the statements (`normSMulClassSeminormedAddCommGroupToProd`) for the `Prod` synonyms was using the wrong norm, and Lean had secretly elaborated the statement using `Prod.toNorm` instead. We fix this error and prevent stray elaboration by locally disabling the usual `Prod`/`Pi` norm instances.
1 parent 6d74fdd commit ee16c22

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

Mathlib/Analysis/Matrix/Normed.lean

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ protected theorem linftyOpIsBoundedSMul
251251
[SeminormedRing R] [SeminormedAddCommGroup α] [Module R α] [IsBoundedSMul R α] :
252252
IsBoundedSMul R (Matrix m n α) :=
253253
letI := PiLp.pseudoMetricSpaceToPi 1 (fun _ : n ↦ α)
254+
letI := PiLp.isBoundedSMulSeminormedAddCommGroupToPi (R := R) 1 (fun _ : n ↦ α)
254255
inferInstanceAs (IsBoundedSMul R (m → n → α))
255256

256257
/-- This applies to the sup norm of L1 norm. -/
@@ -259,6 +260,7 @@ protected theorem linftyOpNormSMulClass
259260
[SeminormedRing R] [SeminormedAddCommGroup α] [Module R α] [NormSMulClass R α] :
260261
NormSMulClass R (Matrix m n α) :=
261262
letI := PiLp.seminormedAddCommGroupToPi 1 (fun _ : n ↦ α)
263+
letI := PiLp.normSMulClassSeminormedAddCommGroupToPi (R := R) 1 (fun _ : n ↦ α)
262264
inferInstanceAs (NormSMulClass R (m → n → α))
263265

264266
/-- Normed space instance (using sup norm of L1 norm) for matrices over a normed space. Not
@@ -268,6 +270,7 @@ matrix. -/
268270
protected def linftyOpNormedSpace [NormedField R] [SeminormedAddCommGroup α] [NormedSpace R α] :
269271
NormedSpace R (Matrix m n α) :=
270272
letI := PiLp.seminormedAddCommGroupToPi 1 (fun _ : n ↦ α)
273+
letI := PiLp.normedSpaceSeminormedAddCommGroupToPi (R := R) 1 (fun _ : n ↦ α)
271274
inferInstanceAs (NormedSpace R (m → n → α))
272275

273276
section SeminormedAddCommGroup
@@ -508,6 +511,7 @@ theorem frobeniusIsBoundedSMul [SeminormedRing R] [SeminormedAddCommGroup α] [M
508511
[IsBoundedSMul R α] :
509512
IsBoundedSMul R (Matrix m n α) :=
510513
letI := PiLp.seminormedAddCommGroupToPi 2 (fun _ : n ↦ α)
514+
letI := PiLp.isBoundedSMulSeminormedAddCommGroupToPi (R := R) 2 (fun _ : n ↦ α)
511515
PiLp.isBoundedSMulSeminormedAddCommGroupToPi 2 _
512516

513517
/-- This applies to the Frobenius norm. -/
@@ -516,6 +520,7 @@ theorem frobeniusNormSMulClass [SeminormedRing R] [SeminormedAddCommGroup α] [M
516520
[NormSMulClass R α] :
517521
NormSMulClass R (Matrix m n α) :=
518522
letI := PiLp.seminormedAddCommGroupToPi 2 (fun _ : n ↦ α)
523+
letI := PiLp.normSMulClassSeminormedAddCommGroupToPi (R := R) 2 (fun _ : n ↦ α)
519524
PiLp.normSMulClassSeminormedAddCommGroupToPi 2 _
520525

521526
/-- Normed space instance (using the Frobenius norm) for matrices over a normed space. Not
@@ -525,6 +530,7 @@ matrix. -/
525530
def frobeniusNormedSpace [NormedField R] [SeminormedAddCommGroup α] [NormedSpace R α] :
526531
NormedSpace R (Matrix m n α) :=
527532
letI := PiLp.seminormedAddCommGroupToPi 2 (fun _ : n ↦ α)
533+
letI := PiLp.normedSpaceSeminormedAddCommGroupToPi (R := R) 2 (fun _ : n ↦ α)
528534
PiLp.normedSpaceSeminormedAddCommGroupToPi 2 _
529535

530536
section SeminormedAddCommGroup

Mathlib/Analysis/Normed/Lp/PiLp.lean

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,9 @@ group structure. These are meant to be used to put the desired instances on type
10951095
of `Π i, α i`. See for instance `Matrix.frobeniusSeminormedAddCommGroup`.
10961096
-/
10971097

1098+
-- This prevents Lean from elaborating terms of `Π i, α i` with an unintended norm.
1099+
attribute [-instance] Pi.seminormedAddGroup
1100+
10981101
variable [Fact (1 ≤ p)] [Fintype ι]
10991102

11001103
/-- This definition allows to endow `Π i, α i` with the Lp distance with the uniformity and
@@ -1127,7 +1130,7 @@ lemma nnnorm_seminormedAddCommGroupToPi [∀ i, SeminormedAddCommGroup (α i)] (
11271130
@NNNorm.nnnorm _ (seminormedAddCommGroupToPi p α).toSeminormedAddGroup.toNNNorm x =
11281131
‖toLp p x‖₊ := rfl
11291132

1130-
instance isBoundedSMulSeminormedAddCommGroupToPi
1133+
lemma isBoundedSMulSeminormedAddCommGroupToPi
11311134
[∀ i, SeminormedAddCommGroup (α i)] {R : Type*} [SeminormedRing R]
11321135
[∀ i, Module R (α i)] [∀ i, IsBoundedSMul R (α i)] :
11331136
letI := pseudoMetricSpaceToPi p α
@@ -1137,7 +1140,7 @@ instance isBoundedSMulSeminormedAddCommGroupToPi
11371140
· simpa [dist_pseudoMetricSpaceToPi] using dist_smul_pair x (toLp p y) (toLp p z)
11381141
· simpa [dist_pseudoMetricSpaceToPi] using dist_pair_smul x y (toLp p z)
11391142

1140-
instance normSMulClassSeminormedAddCommGroupToPi
1143+
lemma normSMulClassSeminormedAddCommGroupToPi
11411144
[∀ i, SeminormedAddCommGroup (α i)] {R : Type*} [SeminormedRing R]
11421145
[∀ i, Module R (α i)] [∀ i, NormSMulClass R (α i)] :
11431146
letI := seminormedAddCommGroupToPi p α
@@ -1146,7 +1149,9 @@ instance normSMulClassSeminormedAddCommGroupToPi
11461149
refine ⟨fun x y ↦ ?_⟩
11471150
simp [norm_seminormedAddCommGroupToPi, norm_smul]
11481151

1149-
instance normedSpaceSeminormedAddCommGroupToPi
1152+
/-- This definition allows to endow `Π i, α i` with a normed space structure corresponding to
1153+
the Lp norm. It is useful for type synonyms of `Π i, α i`. -/
1154+
abbrev normedSpaceSeminormedAddCommGroupToPi
11501155
[∀ i, SeminormedAddCommGroup (α i)] {R : Type*} [NormedField R]
11511156
[∀ i, NormedSpace R (α i)] :
11521157
letI := seminormedAddCommGroupToPi p α

Mathlib/Analysis/Normed/Lp/ProdLp.lean

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,9 @@ of `α × β`. See for instance `TrivSqZeroExt.instL1SeminormedAddCommGroup`.
10041004

10051005
variable (α β : Type*)
10061006

1007+
-- This prevents Lean from elaborating terms of `α × β` with an unintended norm.
1008+
attribute [-instance] Prod.toNorm
1009+
10071010
/-- This definition allows to endow `α × β` with the Lp distance with the uniformity and bornology
10081011
being defeq to the product ones. It is useful to endow a type synonym of `a × β` with the
10091012
Lp distance. -/
@@ -1036,7 +1039,7 @@ lemma nnnorm_seminormedAddCommGroupToProd [SeminormedAddCommGroup α] [Seminorme
10361039
@NNNorm.nnnorm _ (seminormedAddCommGroupToProd p α β).toSeminormedAddGroup.toNNNorm x =
10371040
‖toLp p x‖₊ := rfl
10381041

1039-
instance isBoundedSMulSeminormedAddCommGroupToProd
1042+
lemma isBoundedSMulSeminormedAddCommGroupToProd
10401043
[SeminormedAddCommGroup α] [SeminormedAddCommGroup β] {R : Type*} [SeminormedRing R]
10411044
[Module R α] [Module R β] [IsBoundedSMul R α] [IsBoundedSMul R β] :
10421045
letI := pseudoMetricSpaceToProd p α β
@@ -1046,23 +1049,23 @@ instance isBoundedSMulSeminormedAddCommGroupToProd
10461049
· simpa [dist_pseudoMetricSpaceToProd] using dist_smul_pair x (toLp p y) (toLp p z)
10471050
· simpa [dist_pseudoMetricSpaceToProd] using dist_pair_smul x y (toLp p z)
10481051

1049-
instance normSMulClassSeminormedAddCommGroupToProd
1052+
lemma normSMulClassSeminormedAddCommGroupToProd
10501053
[SeminormedAddCommGroup α] [SeminormedAddCommGroup β] {R : Type*} [SeminormedRing R]
10511054
[Module R α] [Module R β] [NormSMulClass R α] [NormSMulClass R β] :
10521055
letI := seminormedAddCommGroupToProd p α β
10531056
NormSMulClass R (α × β) := by
10541057
letI := seminormedAddCommGroupToProd p α β
1055-
refine ⟨fun x y ↦ ?_⟩
1056-
rw [norm_smul]
1058+
exact ⟨fun x y ↦ norm_smul x (toLp p y)⟩
10571059

1058-
instance normedSpaceSeminormedAddCommGroupToProd
1060+
/-- This definition allows to endow `α × β` with a normed space structure corresponding to
1061+
the Lp norm. It is useful for type synonyms of `α × β`. -/
1062+
abbrev normedSpaceSeminormedAddCommGroupToProd
10591063
[SeminormedAddCommGroup α] [SeminormedAddCommGroup β] {R : Type*} [NormedField R]
10601064
[NormedSpace R α] [NormedSpace R β] :
10611065
letI := seminormedAddCommGroupToProd p α β
10621066
NormedSpace R (α × β) := by
10631067
letI := seminormedAddCommGroupToProd p α β
1064-
refine ⟨fun x y ↦ ?_⟩
1065-
simp [norm_seminormedAddCommGroupToProd, norm_smul]
1068+
exact ⟨fun x y ↦ norm_smul_le x (toLp p y)⟩
10661069

10671070
/-- This definition allows to endow `α × β` with the Lp norm with the uniformity and bornology
10681071
being defeq to the product ones. It is useful to endow a type synonym of `α × β` with the

0 commit comments

Comments
 (0)