feat: add withMVarContext

cc @dselsam @kha
This commit is contained in:
Leonardo de Moura 2019-12-02 10:49:34 -08:00
parent 43d7ae3938
commit 00a5860ce6
2 changed files with 19 additions and 1 deletions

View file

@ -344,7 +344,7 @@ do lctx ← getLCtx;
def getFVarLocalDecl (fvar : Expr) : MetaM LocalDecl :=
getLocalDecl fvar.fvarId!
def getMVarDecl (mvarId : Name) : MetaM MetavarDecl :=
def getMVarDecl (mvarId : MVarId) : MetaM MetavarDecl :=
do mctx ← getMCtx;
match mctx.findDecl mvarId with
| some d => pure d
@ -717,6 +717,19 @@ do s ← get;
modify $ fun s => { mctx := s.mctx.incDepth, .. s };
finally x (modify $ fun s => { cache := savedCache, mctx := savedMCtx, .. s })
/--
Execute `x` using the given metavariable `LocalContext` and `LocalInstances`.
The type class resolution cache is flushed when executing `x` if its `LocalInstances` are
different from the current ones. -/
@[inline] def withMVarContext {α} (mvarId : MVarId) (x : MetaM α) : MetaM α :=
do mvarDecl ← getMVarDecl mvarId;
localInsts ← getLocalInstances;
adaptReader (fun (ctx : Context) => { lctx := mvarDecl.lctx, localInstances := mvarDecl.localInstances, .. ctx }) $
if localInsts == mvarDecl.localInstances then
x
else
resettingSynthInstanceCache x
end Meta
end Lean

View file

@ -199,6 +199,11 @@ structure LocalInstance :=
abbrev LocalInstances := Array LocalInstance
def LocalInstance.beq (i₁ i₂ : LocalInstance) : Bool :=
i₁.fvar == i₂.fvar
instance LocalInstance.hasBeq : HasBeq LocalInstance := ⟨LocalInstance.beq⟩
structure MetavarDecl :=
(userName : Name := Name.anonymous)
(lctx : LocalContext)