Skip to content

Commit 1b5be7f

Browse files
committed
perf(to_additive): hard code OfNat and OfNat.ofNat for numeral translation (leanprover-community#34696)
This PR removes the environment extension used by `to_additive` to determine which numerals to translate. The only two entries in this environment extension are `OfNat` and `OfNat.ofNat`, and I don't see why this would change. See [#mathlib4 > Performance cost of environment extensions](https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/Performance.20cost.20of.20environment.20extensions/with/571319366)
1 parent 3735095 commit 1b5be7f

2 files changed

Lines changed: 11 additions & 40 deletions

File tree

Mathlib/Tactic/ToAdditive.lean

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ set_option linter.privateModule false
1818
attribute [to_additive_do_translate] Empty PEmpty Unit PUnit
1919
attribute [to_additive_ignore_args 2] Subtype
2020

21-
attribute [translate_change_numeral 2] OfNat OfNat.ofNat
22-
2321
attribute [to_additive] One
2422
attribute [to_additive existing Zero.toOfNat0] One.toOfNat1
2523
attribute [to_additive existing Zero.ofOfNat0] One.ofOfNat1

Mathlib/Tactic/Translate/Core.lean

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,6 @@ syntax attrArgs :=
108108
-- We omit a doc-string on these syntaxes to instead show the `to_additive` or `to_dual` doc-string
109109
attribute [nolint docBlame] attrArgs bracketedOption
110110

111-
/-- An attribute that stores all the declarations that deal with numeric literals on variable types.
112-
113-
Numeral literals occur in expressions without type information, so in order to decide whether `1`
114-
needs to be changed to `0`, the context around the numeral is relevant.
115-
Most numerals will be in an `OfNat.ofNat` application, though tactics can add numeral literals
116-
inside arbitrary functions. By default we assume that we do not change numerals, unless it is
117-
in a function application with the `translate_change_numeral` attribute.
118-
119-
`@[translate_change_numeral n₁ ...]` should be added to all functions that take one or more
120-
numerals as argument that should be changed if `shouldTranslate` succeeds on the first argument,
121-
i.e. when the numeral is only translated if the first argument is a variable
122-
(or consists of variables).
123-
The arguments `n₁ ...` are the positions of the numeral arguments (starting counting from 1). -/
124-
syntax (name := translate_change_numeral) "translate_change_numeral" (ppSpace num)* : attr
125-
126111
initialize registerTraceClass `translate
127112
initialize registerTraceClass `translate_detail
128113

@@ -164,17 +149,6 @@ register_option linter.translateRedundant : Bool := {
164149
defValue := true
165150
descr := "Linter used by translate attributes that checks if the attribute is redundant" }
166151

167-
@[inherit_doc translate_change_numeral]
168-
initialize changeNumeralAttr : NameMapExtension (List Nat) ←
169-
registerNameMapAttribute {
170-
name := `translate_change_numeral
171-
descr :=
172-
"Auxiliary attribute for `to_additive` that stores functions that have numerals as argument."
173-
add := fun
174-
| _, `(attr| translate_change_numeral $[$arg]*) =>
175-
pure <| arg.map (·.1.isNatLit?.get!.pred) |>.toList
176-
| _, _ => throwUnsupportedSyntax }
177-
178152
/-- `ArgInfo` stores information about how a constant should be translated. -/
179153
structure ArgInfo where
180154
/-- The arguments that should be reordered when translating, using cycle notation. -/
@@ -428,18 +402,17 @@ where
428402
mkAppN (.app (mkAppN (.const projName bf.constLevels!) bargs) b) args
429403
| .const n₀ ls₀ =>
430404
withTraceNode `translate_detail (fun res => return m!"{exceptEmoji res} replacing at {e}") do
431-
-- Replace numeral `1` with `0` when required
432-
if t.changeNumeral then
433-
if let some numeralArgs := changeNumeralAttr.find? env n₀ then
434-
if let some firstArg := args[0]? then
435-
if shouldTranslate env t firstArg dontTranslate |>.isNone then
436-
-- In this case, we still update all arguments of `g` that are not numerals,
437-
-- since all other arguments can contain subexpressions like
438-
-- `(fun x ↦ ℕ) (1 : G)`, and we have to update the `(1 : G)` to `(0 : G)`
439-
trace[translate_detail] "applyReplacementFun: We change the numerals in this \
440-
expression. However, we will still recurse into all the non-numeral arguments."
441-
let args := numeralArgs.foldl (·.modify · changeNumeral) args
442-
return mkAppN f (← args.mapM visit)
405+
-- Replace numeral `1` with `0` in applications of `OfNat` and `OfNat.ofNat`.
406+
if t.changeNumeral && n₀ matches ``OfNat | ``OfNat.ofNat then
407+
if let some firstArg := args[0]? then
408+
if shouldTranslate env t firstArg dontTranslate |>.isNone then
409+
-- In this case, we still update all arguments of `g` that are not numerals,
410+
-- since all other arguments can contain subexpressions like
411+
-- `(fun x ↦ ℕ) (1 : G)`, and we have to update the `(1 : G)` to `(0 : G)`
412+
trace[translate_detail] "applyReplacementFun: We change the numerals in this \
413+
expression. However, we will still recurse into all the non-numeral arguments."
414+
let args := args.modify 1 changeNumeral
415+
return mkAppN f (← args.mapM visit)
443416
let some n₁ := findTranslation? env t n₀ <|> do
444417
let n₁ := findPrefixTranslation env n₀ t
445418
guard (n₀ != n₁ && (isReservedName env n₁ || env.contains n₁)); some n₁

0 commit comments

Comments
 (0)