lean4-htt/src/Lean/Meta/Tactic/Cleanup.lean
Alok Singh 1e1ed16a05
doc: correct typos in documentation and comments (#11465)
This PR fixes various typos across the codebase in documentation and
comments.

- `infered` → `inferred` (ParserCompiler.lean)
- `declartation` → `declaration` (Cleanup.lean)
- `certian` → `certain` (CasesInfo.lean)
- `wil` → `will` (Cache.lean)
- `the the` → `the` (multiple files - PrefixTree.lean, Sum/Basic.lean,
List/Nat/Perm.lean, Time.lean, Bounded.lean, Lake files)
- `to to` → `to` (MutualInductive.lean, simp_bubblesort_256.lean)
- Grammar improvements in Bounded.lean and Time.lean

All changes are to comments and documentation only - no functional
changes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-02 06:38:05 +00:00

83 lines
3.4 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
module
prelude
public import Lean.Meta.Basic
import Lean.Meta.CollectFVars
import Lean.Meta.Tactic.Clear
namespace Lean.Meta
private partial def cleanupCore (mvarId : MVarId) (toPreserve : Array FVarId) (indirectProps : Bool) : MetaM MVarId := do
mvarId.withContext do
mvarId.checkNotAssigned `cleanup
let used ← collectUsed |>.run' (false, {})
let mut lctx ← getLCtx
for localDecl in lctx do
unless used.contains localDecl.fvarId do
lctx := lctx.erase localDecl.fvarId
let localInsts := (← getLocalInstances).filter fun inst => used.contains inst.fvar.fvarId!
let mvarNew ← mkFreshExprMVarAt lctx localInsts (← instantiateMVars (← mvarId.getType)) .syntheticOpaque (← mvarId.getTag)
mvarId.assign mvarNew
return mvarNew.mvarId!
where
addUsedFVars (e : Expr) : StateRefT (Bool × FVarIdSet) MetaM Unit := do
let (_, s) ← (← instantiateMVars e).collectFVars |>.run {}
for fvarId in s.fvarSet do
addUsedFVar fvarId
addDeps (fvarId : FVarId) : StateRefT (Bool × FVarIdSet) MetaM Unit := do
let localDecl ← fvarId.getDecl
addUsedFVars localDecl.type
if let some val := localDecl.value? then
addUsedFVars val
addUsedFVar (fvarId : FVarId) : StateRefT (Bool × FVarIdSet) MetaM Unit := do
unless (← get).2.contains fvarId do
modify fun (_, s) => (true, s.insert fvarId)
addDeps fvarId
/-- We include `p` in the used-set, if `p` is a proposition that contains a `x` that is in the used-set. -/
collectPropsStep : StateRefT (Bool × FVarIdSet) MetaM Unit := do
for localDecl in (← getLCtx) do
let usedSet := (← get).2
unless usedSet.contains localDecl.fvarId do
if (← isProp localDecl.type) then
if (← dependsOnPred localDecl.type usedSet.contains) then
addUsedFVar localDecl.fvarId
if let some v := localDecl.value? then
if (← dependsOnPred v usedSet.contains) then
addUsedFVar localDecl.fvarId
collectProps : StateRefT (Bool × FVarIdSet) MetaM Unit := do
modify fun s => (false, s.2)
collectPropsStep
if (← get).1 then
collectProps
collectUsed : StateRefT (Bool × FVarIdSet) MetaM FVarIdSet := do
addUsedFVars (← instantiateMVars (← mvarId.getType))
toPreserve.forM addUsedFVar
if indirectProps then collectProps
return (← get).2
/--
Auxiliary tactic for cleaning the local context. It removes local declarations (aka hypotheses) that are *not* relevant.
We say a variable `x` is "relevant" if
- It occurs in the `toPreserve` array, or
- It occurs in the target type, or
- There is a relevant variable `y` that depends on `x`, or
- If `indirectProps` is true, the type of `x` is a proposition and it depends on a relevant variable `y`.
- If `indirectProps` is true, `x` is a local declaration and its value mentions a relevant variable `y`.
By default, `toPreserve := #[]` and `indirectProps := true`. These settings are used in the mathlib tactic `extract_goal`
to give the user more control over which variables to include.
-/
@[inline] public def _root_.Lean.MVarId.cleanup (mvarId : MVarId) (toPreserve : Array FVarId := #[]) (indirectProps : Bool := true) : MetaM MVarId := do
cleanupCore mvarId toPreserve indirectProps
end Lean.Meta