chore: Move withFreshUserNames to Lean/Meta/Basic.lean (#9783)

This PR generalizes and moves `withFreshUserNames` to
Lean/Meta/Basic.lean where it can be reused.
This commit is contained in:
Sebastian Graf 2025-08-07 12:27:52 +02:00 committed by GitHub
parent ae699a6b13
commit 04a3968206
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 13 deletions

View file

@ -88,19 +88,11 @@ def ifOutOfFuel (x : VCGenM α) (k : VCGenM α) : VCGenM α := do
| Fuel.limited 0 => x
| _ => k
def withFreshUserNames (k : VCGenM α) : VCGenM α := do
let mut lctx ← getLCtx
for i in [(← read).initialCtxSize:lctx.numIndices] do
let some decl := lctx.decls[i]! | continue
let n := decl.userName
if !n.hasMacroScopes then
lctx := lctx.setUserName decl.fvarId (← mkFreshUserName n)
withLCtx' lctx k
def emitVC (subGoal : Expr) (name : Name) : VCGenM Expr := withFreshUserNames do
let m ← liftM <| mkFreshExprSyntheticOpaqueMVar subGoal (tag := name)
modify fun s => { s with vcs := s.vcs.push m.mvarId! }
return m
def emitVC (subGoal : Expr) (name : Name) : VCGenM Expr := do
withFreshUserNamesSinceIdx (← read).initialCtxSize do
let m ← liftM <| mkFreshExprSyntheticOpaqueMVar subGoal (tag := name)
modify fun s => { s with vcs := s.vcs.push m.mvarId! }
return m
def addSubGoalAsVC (goal : MVarId) : VCGenM PUnit := do
modify fun s => { s with vcs := s.vcs.push goal }

View file

@ -1951,6 +1951,18 @@ def withErasedFVars [MonadLCtx n] [MonadLiftT MetaM n] (fvarIds : Array FVarId)
let localInsts' := localInsts.filter (!fvarIds.contains ·.fvar.fvarId!)
withLCtx lctx' localInsts' k
/--
Ensures that the user names of all local declarations after index `idx` have a macro scope.
-/
def withFreshUserNamesSinceIdx [MonadLCtx n] [MonadLiftT MetaM n] (idx : Nat) (k : n α) : n α := do
let mut lctx ← getLCtx
for i in [idx:lctx.numIndices] do
let some decl := lctx.decls[i]! | continue
let n := decl.userName
if !n.hasMacroScopes then
lctx := lctx.setUserName decl.fvarId (← liftMetaM <| mkFreshUserName n)
withLCtx' lctx k
private def withMVarContextImp (mvarId : MVarId) (x : MetaM α) : MetaM α := do
let mvarDecl ← mvarId.getDecl
withLocalContextImp mvarDecl.lctx mvarDecl.localInstances x