Skip to content

Commit 5179aba

Browse files
committed
refactor(translate): put executeReservedNameAction in a more natural place (leanprover-community#34999)
This PR cleans up a piece of the implementation of `to_additive`/`to_dual`. The function `executeReservedNameAction` needs to be run on generated translations that haven't been realized yet. This PR moves this function call to the place of the original check that checks whether the declaration exists or is a reserved name.
1 parent 9c01b6b commit 5179aba

1 file changed

Lines changed: 19 additions & 18 deletions

File tree

Mathlib/Tactic/Translate/Core.lean

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,24 @@ def findTranslationName? (env : Environment) (t : TranslateData) (n : Name) : Op
208208

209209
/-- Get the translation for the given name,
210210
falling back to translating a prefix of the name if the full name can't be translated.
211-
This allows translating automatically generated declarations such as `IsRegular.casesOn`. -/
212-
def findPrefixTranslation? (env : Environment) (n : Name) (t : TranslateData) :
213-
Option TranslationInfo :=
214-
findTranslation? env t n <|> do
215-
let .str n postFix := n | failure
216-
let info ← go n [postFix]
217-
guard (env.contains info.translation || isReservedName env info.translation)
211+
This allows translating automatically generated declarations such as `IsRegular.casesOn`.
212+
We make sure that the new constant is realized. -/
213+
def findPrefixTranslation? (n : Name) (t : TranslateData) : CoreM (Option TranslationInfo) := do
214+
let env ← getEnv
215+
if let some info := findTranslation? env t n then
216+
return info
217+
let .str n postFix := n | return none
218+
let some info := go env n [postFix] | return none
219+
if env.contains (skipRealize := false) info.translation then
220+
return info
221+
if isReservedName env info.translation then
222+
executeReservedNameAction info.translation
218223
return info
224+
return none
219225
where
220226
/-- Loop through the prefixes of `n` to try to find a translation.
221227
In such a case, we inherit the `relevantArg` option from the translation. -/
222-
go (n : Name) (postFixes : List String) : Option TranslationInfo := Id.run do
228+
go (env : Environment) (n : Name) (postFixes : List String) : Option TranslationInfo := Id.run do
223229
if let some info := findTranslation? env t n then
224230
return some {
225231
translation := postFixes.foldl .str info.translation
@@ -230,7 +236,7 @@ where
230236
translation := postFixes.foldl .str (mkPrivateName env info.translation)
231237
relevantArg := info.relevantArg }
232238
let .str n postFix := n | return none
233-
return go n (postFix :: postFixes)
239+
return go env n (postFix :: postFixes)
234240

235241
/-- Add a translation to the translations map. If the translation attribute is dual,
236242
also add the reverse translation. -/
@@ -246,7 +252,7 @@ def insertTranslation (t : TranslateData) (src tgt : Name) (reorder : Reorder)
246252
where
247253
/-- Insert only one direction of a translation. -/
248254
insertTranslationAux (src : Name) (t : TranslateData) (info : TranslationInfo) : CoreM Unit := do
249-
if let some info' := t.translations.find? (← getEnv) src then
255+
if let some info' := findTranslation? (← getEnv) t src then
250256
-- After `insert_to_additive_translation`, we may end up adding same translation again.
251257
-- So in that case, don't log a warning.
252258
if info.translation != info'.translation then
@@ -386,12 +392,7 @@ e.g. `g x₁ x₂ x₃ ... xₙ` becomes `g x₂ x₁ x₃ ... xₙ` if `reorder
386392
-/
387393
partial def applyReplacementFun (t : TranslateData) (e : Expr)
388394
(dontTranslate : Array FVarId := #[]) : MetaM Expr := do
389-
let e' ← visit e |>.run {}
390-
-- Make sure any new reserved names in the expr are realized
391-
e'.getUsedConstants.forM fun n => do
392-
if !(← hasConst (skipRealize := false) n) && isReservedName (← getEnv) n then
393-
executeReservedNameAction n
394-
return e'
395+
visit e |>.run {}
395396
where
396397
/-- The implementation of this function is based on `Meta.transform`.
397398
We can't use `Meta.transform`, because that would cause the types of free variables to be
@@ -434,11 +435,11 @@ where
434435
expression. However, we will still recurse into all the non-numeral arguments."
435436
let args := args.modify 1 changeNumeral
436437
return mkAppN f (← args.mapM visit)
437-
let some { translation := n₁, reorder, relevantArg } := findPrefixTranslation? env n₀ t |
438+
let some { translation := n₁, reorder, relevantArg } findPrefixTranslation? n₀ t |
438439
return mkAppN f (← args.mapM visit)
439440
-- Use `relevantArg` to test if the head should be translated.
440441
if h : relevantArg < args.size then
441-
if let some fixed := shouldTranslate env t args[relevantArg] dontTranslate then
442+
if let some fixed := shouldTranslate (← getEnv) t args[relevantArg] dontTranslate then
442443
trace[translate_detail]
443444
"The application of {n₀} contains the fixed type {fixed} so it is not changed."
444445
return mkAppN f (← args.mapM visit)

0 commit comments

Comments
 (0)