Skip to content

Commit 74f0ed4

Browse files
Improve LocalContext and LocalInstance extraction for sorries
1 parent 593cb88 commit 74f0ed4

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

REPL/Main.lean

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,7 @@ def sorries (trees : List InfoTree) (env? : Option Environment) (rootGoals? : Op
102102
fun ⟨ctx, g, pos, endPos⟩ => do
103103
let (goal, proofState) ← match g with
104104
| .tactic g => do
105-
let lctx ← ctx.runMetaM {} do
106-
match ctx.mctx.findDecl? g with
107-
| some decl => return decl.lctx
108-
| none => throwError "unknown metavariable '{g}'"
109-
let s ← ProofSnapshot.create ctx lctx env? [g] rootGoals?
105+
let s ← ProofSnapshot.create ctx none env? [g] rootGoals?
110106
pure ("\n".intercalate <| (← s.ppGoals).map fun s => s!"{s}", some s)
111107
| .term lctx (some t) => do
112108
let s ← ProofSnapshot.create ctx lctx env? [] rootGoals? [t]
@@ -197,7 +193,7 @@ def getProofStatus (proofState : ProofSnapshot) : M m String := do
197193
unless (← Meta.isDefEq pft expectedType) do
198194
return s!"Error: proof has type {pft} but root goal has type {expectedType}"
199195

200-
let pf ← abstractAllLambdaFVars pf
196+
let pf ← abstractAllLambdaFVars pf >>= instantiateMVars
201197
let pft ← Meta.inferType pf >>= instantiateMVars
202198

203199
if pf.hasExprMVar then

REPL/Snapshots.lean

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -185,33 +185,44 @@ def runString (p : ProofSnapshot) (t : String) : IO ProofSnapshot :=
185185
/-- Pretty print the current goals in the `ProofSnapshot`. -/
186186
def ppGoals (p : ProofSnapshot) : IO (List Format) :=
187187
Prod.fst <$> p.runMetaM do p.tacticState.goals.mapM (Meta.ppGoal ·)
188+
188189
/--
189190
Construct a `ProofSnapshot` from a `ContextInfo` and optional `LocalContext`, and a list of goals.
190-
191191
For convenience, we also allow a list of `Expr`s, and these are appended to the goals
192192
as fresh metavariables with the given types.
193193
-/
194194
def create (ctx : ContextInfo) (lctx? : Option LocalContext) (env? : Option Environment)
195195
(goals : List MVarId) (rootGoals? : Option (List MVarId)) (types : List Expr := [])
196196
: IO ProofSnapshot := do
197-
ctx.runMetaM (lctx?.getD {}) do
198-
let goals := goals ++ (← types.mapM fun t => Expr.mvarId! <$> Meta.mkFreshExprMVar (some t))
199-
let s ← getThe Core.State
200-
let s := match env? with
201-
| none => s
202-
| some env => { s with env }
203-
pure <|
204-
{ coreState := s
205-
coreContext := ← readThe Core.Context
206-
metaState := ← getThe Meta.State
207-
metaContext := ← readThe Meta.Context
208-
termState := {}
209-
termContext := {}
210-
tacticState := { goals }
211-
tacticContext := { elaborator := .anonymous }
212-
rootGoals := match rootGoals? with
213-
| none => goals
214-
| some gs => gs }
197+
-- Get the local context from the goals if not provided.
198+
let lctx ← match lctx? with
199+
| none => match goals with
200+
| g :: _ => ctx.runMetaM {} do pure (← g.getDecl).lctx
201+
| [] => match rootGoals? with
202+
| some (g :: _) => ctx.runMetaM {} do pure (← g.getDecl).lctx
203+
| _ => pure {}
204+
| some lctx => pure lctx
205+
206+
ctx.runMetaM lctx do
207+
-- update local instances, which is necessary when `types` is non-empty
208+
Meta.withLocalInstances (lctx.decls.toList.filterMap id) do
209+
let goals := goals ++ (← types.mapM fun t => Expr.mvarId! <$> Meta.mkFreshExprMVar (some t))
210+
let s ← getThe Core.State
211+
let s := match env? with
212+
| none => s
213+
| some env => { s with env }
214+
pure <|
215+
{ coreState := s
216+
coreContext := ← readThe Core.Context
217+
metaState := ← getThe Meta.State
218+
metaContext := ← readThe Meta.Context
219+
termState := {}
220+
termContext := {}
221+
tacticState := { goals }
222+
tacticContext := { elaborator := .anonymous }
223+
rootGoals := match rootGoals? with
224+
| none => goals
225+
| some gs => gs }
215226

216227
open Lean.Core in
217228
/-- A copy of `Core.State` with the `Environment`, caches, and logging omitted. -/

0 commit comments

Comments
 (0)