Skip to content

Commit ebbb9b3

Browse files
committed
perf(reassoc, to_app, elementwise): don't pass the same proof to the kernel again (#30650)
In `reassoc`, `to_app`, `elementwise`, the proof of the original lemma was being used to prove the modified lemma. This is silly, because the modified lemma can be proved using the original lemma directly. Hence, this PR modifies `addRelatedDecl` to include this optimization. Previously, `addRelatedDecl` would pass around both the type and the value of the original declaration. This was necessary, because the inferred type of the value might not be identical to the type it is given. Now this is not necessary anymore, because the type of the constant is exactly the type that it is given, so the type can always be obtained with `inferType`.
1 parent 7e683cc commit ebbb9b3

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

Mathlib/Tactic/CategoryTheory/Elementwise.lean

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ for the first universe parameter to `HasForget`.
8282
The `simpSides` option controls whether to simplify both sides of the equality, for simpNF
8383
purposes.
8484
-/
85-
def elementwiseExpr (src : Name) (type pf : Expr) (simpSides := true) :
85+
def elementwiseExpr (src : Name) (pf : Expr) (simpSides := true) :
8686
MetaM (Expr × Option (Level × Level)) := do
87-
let type := (← instantiateMVars type).cleanupAnnotations
87+
let type := (← instantiateMVars (← inferType pf)).cleanupAnnotations
8888
forallTelescope type fun fvars type' => do
89-
mkHomElementwise type' (← mkExpectedTypeHint (mkAppN pf fvars) type') fun eqPf instConcr? => do
89+
mkHomElementwise type' (mkAppN pf fvars) fun eqPf instConcr? => do
9090
-- First simplify using elementwise-specific lemmas
9191
let mut eqPf' ← simpType (simpOnlyNames elementwiseThms (config := { decide := false })) eqPf
9292
if (← inferType eqPf') == .const ``True [] then
@@ -213,8 +213,8 @@ initialize registerBuiltinAttribute {
213213
| `(attr| elementwise $[nosimp%$nosimp?]? $[(attr := $stx?,*)]?) => MetaM.run' do
214214
if (kind != AttributeKind.global) then
215215
throwError "`elementwise` can only be used as a global attribute"
216-
addRelatedDecl src "_apply" ref stx? fun type value levels => do
217-
let (newValue, level?) ← elementwiseExpr src type value (simpSides := nosimp?.isNone)
216+
addRelatedDecl src "_apply" ref stx? fun value levels => do
217+
let (newValue, level?) ← elementwiseExpr src value (simpSides := nosimp?.isNone)
218218
let newLevels ← if let some (levelW, levelUF) := level? then do
219219
let w := mkUnusedName levels `w
220220
let uF := mkUnusedName levels `uF
@@ -252,7 +252,7 @@ normal form. `elementwise_of%` does not do this.
252252
-/
253253
elab "elementwise_of% " t:term : term => do
254254
let e ← Term.elabTerm t none
255-
let (pf, _) ← elementwiseExpr .anonymous (← inferType e) e (simpSides := false)
255+
let (pf, _) ← elementwiseExpr .anonymous e (simpSides := false)
256256
return pf
257257

258258
-- TODO: elementwise tactic

Mathlib/Tactic/CategoryTheory/Reassoc.lean

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,12 @@ def registerReassocExpr (f : Expr → MetaM (Expr × Array MVarId)) : IO Unit :=
102102
reassocImplRef.modify (·.push f)
103103

104104
/--
105-
Reassociates the morphisms in `type?` using the registered handlers,
105+
Reassociates the morphisms in the type of `pf` using the registered handlers,
106106
using `reassocExprHom` as the default.
107-
If `type?` is not given, it is assumed to be the type of `pf`.
108107
109108
Returns the proof of the lemma along with instance metavariables that need synthesis.
110109
-/
111-
def reassocExpr (pf : Expr) (type? : Option Expr) : MetaM (Expr × Array MVarId) := do
112-
let pf ← if let some type := type? then mkExpectedTypeHint pf type else pure pf
110+
def reassocExpr (pf : Expr) : MetaM (Expr × Array MVarId) := do
113111
forallTelescopeReducing (← inferType pf) fun xs _ => do
114112
let pf := mkAppN pf xs
115113
let handlers ← reassocImplRef.get
@@ -120,8 +118,8 @@ def reassocExpr (pf : Expr) (type? : Option Expr) : MetaM (Expr × Array MVarId)
120118
/--
121119
Version of `reassocExpr` for the `TermElabM` monad. Handles instance metavariables automatically.
122120
-/
123-
def reassocExpr' (pf : Expr) (type? : Option Expr) : TermElabM Expr := do
124-
let (e, insts) ← reassocExpr pf type?
121+
def reassocExpr' (pf : Expr) : TermElabM Expr := do
122+
let (e, insts) ← reassocExpr pf
125123
for inst in insts do
126124
inst.withContext do
127125
unless ← Term.synthesizeInstMVarCore inst do
@@ -136,9 +134,9 @@ initialize registerBuiltinAttribute {
136134
| `(attr| reassoc $[(attr := $stx?,*)]?) => MetaM.run' do
137135
if (kind != AttributeKind.global) then
138136
throwError "`reassoc` can only be used as a global attribute"
139-
addRelatedDecl src "_assoc" ref stx? fun type value levels => do
137+
addRelatedDecl src "_assoc" ref stx? fun value levels => do
140138
Term.TermElabM.run' <| Term.withSynthesize do
141-
let pf ← reassocExpr' value type
139+
let pf ← reassocExpr' value
142140
pure (pf, levels)
143141
| _ => throwUnsupportedSyntax }
144142

@@ -152,6 +150,6 @@ This also works for equations between isomorphisms, provided that
152150
-/
153151
elab "reassoc_of% " t:term : term => do
154152
let e ← Term.withSynthesizeLight <| Term.elabTerm t none
155-
reassocExpr' e none
153+
reassocExpr' e
156154

157155
end CategoryTheory

Mathlib/Tactic/CategoryTheory/ToApp.lean

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,8 @@ initialize registerBuiltinAttribute {
122122
| `(attr| to_app $[(attr := $stx?,*)]?) => MetaM.run' do
123123
if (kind != AttributeKind.global) then
124124
throwError "`to_app` can only be used as a global attribute"
125-
addRelatedDecl src "_app" ref stx? fun type value levels => do
125+
addRelatedDecl src "_app" ref stx? fun value levels => do
126126
let levelMVars ← levels.mapM fun _ => mkFreshLevelMVar
127-
let value ← mkExpectedTypeHint value type
128127
let value := value.instantiateLevelParams levels levelMVars
129128
let newValue ← toAppExpr (← toCatExpr value)
130129
let r := (← getMCtx).levelMVarToParam (fun _ => false) (fun _ => false) newValue

Mathlib/Util/AddRelatedDecl.lean

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ Arguments:
3030
* `src : Name` is the existing declaration that we are modifying.
3131
* `suffix : String` will be appended to `src` to form the name of the new declaration.
3232
* `ref : Syntax` is the syntax where the user requested the related declaration.
33-
* `construct type value levels : MetaM (Expr × List Name)`
34-
given the type, value, and universe variables of the original declaration,
33+
* `construct value levels : MetaM (Expr × List Name)`
34+
given an `Expr.const` referring to the original declaration, and its universe variables,
3535
should construct the value of the new declaration,
3636
along with the names of its universe variables.
3737
* `attrs` is the attributes that should be applied to both the new and the original declaration,
@@ -42,14 +42,15 @@ Arguments:
4242
-/
4343
def addRelatedDecl (src : Name) (suffix : String) (ref : Syntax)
4444
(attrs? : Option (Syntax.TSepArray `Lean.Parser.Term.attrInstance ","))
45-
(construct : Expr → Expr → List Name → MetaM (Expr × List Name)) :
45+
(construct : Expr → List Name → MetaM (Expr × List Name)) :
4646
MetaM Unit := do
4747
let tgt := match src with
4848
| Name.str n s => Name.mkStr n <| s ++ suffix
4949
| x => x
5050
addDeclarationRangesFromSyntax tgt (← getRef) ref
5151
let info ← getConstInfo src
52-
let (newValue, newLevels) ← construct info.type info.value! info.levelParams
52+
let value := .const src (info.levelParams.map mkLevelParam)
53+
let (newValue, newLevels) ← construct value info.levelParams
5354
let newValue ← instantiateMVars newValue
5455
let newType ← instantiateMVars (← inferType newValue)
5556
match info with

0 commit comments

Comments
 (0)