Skip to content

Commit 6651502

Browse files
committed
feat: use environment from before the declaration, so exact? doesn't give circular proofs
1 parent fa73f05 commit 6651502

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

REPL/Main.lean

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ def recordProofSnapshot (proofState : ProofSnapshot) : M m Nat := do
9595
modify fun s => { s with proofStates := s.proofStates.push proofState }
9696
return id
9797

98-
def sorries (trees : List InfoTree) : M m (List Sorry) :=
98+
def sorries (trees : List InfoTree) (env? : Option Environment) : M m (List Sorry) :=
9999
trees.bind InfoTree.sorries |>.mapM
100100
fun ⟨ctx, g, pos, endPos⟩ => do
101101
let (goal, proofState) ← match g with
102102
| .tactic g => do
103-
let s ← ProofSnapshot.create ctx none [g]
103+
let s ← ProofSnapshot.create ctx none env? [g]
104104
pure ("\n".intercalate <| (← s.ppGoals).map fun s => s!"{s}", some s)
105105
| .term lctx (some t) => do
106-
let s ← ProofSnapshot.create ctx lctx [] [t]
106+
let s ← ProofSnapshot.create ctx lctx env? [] [t]
107107
pure ("\n".intercalate <| (← s.ppGoals).map fun s => s!"{s}", some s)
108108
| .term _ none => unreachable!
109109
let proofStateId ← proofState.mapM recordProofSnapshot
@@ -118,7 +118,7 @@ def ppTactic (ctx : ContextInfo) (stx : Syntax) : IO Format :=
118118
def tactics (trees : List InfoTree) : M m (List Tactic) :=
119119
trees.bind InfoTree.tactics |>.mapM
120120
fun ⟨ctx, stx, goals, pos, endPos⟩ => do
121-
let proofState := some (← ProofSnapshot.create ctx none goals)
121+
let proofState := some (← ProofSnapshot.create ctx none none goals)
122122
let goals := s!"{(← ctx.ppGoals goals)}".trim
123123
let tactic := Format.pretty (← ppTactic ctx stx)
124124
let proofStateId ← proofState.mapM recordProofSnapshot
@@ -139,7 +139,7 @@ def createProofStepReponse (proofState : ProofSnapshot) (old? : Option ProofSnap
139139
| none => pure trees
140140
-- For debugging purposes, sometimes we print out the trees here:
141141
-- trees.forM fun t => do IO.println (← t.format)
142-
let sorries ← sorries trees
142+
let sorries ← sorries trees none
143143
let id ← recordProofSnapshot proofState
144144
return {
145145
proofState := id
@@ -194,15 +194,15 @@ def runCommand (s : Command) : M IO (CommandResponse ⊕ Error) := do
194194
| none => pure (none, true)
195195
if notFound then
196196
return .inr ⟨"Unknown environment."
197-
let cmdState? := cmdSnapshot?.map fun c => c.cmdState
197+
let initialCmdState? := cmdSnapshot?.map fun c => c.cmdState
198198
let (cmdState, messages, trees) ← try
199-
IO.processInput s.cmd cmdState?
199+
IO.processInput s.cmd initialCmdState?
200200
catch ex =>
201201
return .inr ⟨ex.toString⟩
202202
let messages ← messages.mapM fun m => Message.of m
203203
-- For debugging purposes, sometimes we print out the trees here:
204204
-- trees.forM fun t => do IO.println (← t.format)
205-
let sorries ← sorries trees
205+
let sorries ← sorries trees (initialCmdState?.map (·.env))
206206
let tactics ← match s.allTactics with
207207
| some true => tactics trees
208208
| _ => pure []

REPL/Snapshots.lean

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,17 @@ Construct a `ProofSnapshot` from a `ContextInfo` and optional `LocalContext`, an
189189
For convenience, we also allow a list of `Expr`s, and these are appended to the goals
190190
as fresh metavariables with the given types.
191191
-/
192-
def create (ctx : ContextInfo) (lctx? : Option LocalContext)
192+
def create (ctx : ContextInfo) (lctx? : Option LocalContext) (env? : Option Environment)
193193
(goals : List MVarId) (types : List Expr := []) : IO ProofSnapshot := do
194194
ctx.runMetaM (lctx?.getD {}) do
195195
let goals := goals ++ (← types.mapM fun t => Expr.mvarId! <$> Meta.mkFreshExprMVar (some t))
196196
goals.head!.withContext do
197+
let s ← getThe Core.State
198+
let s := match env? with
199+
| none => s
200+
| some env => { s with env }
197201
pure <|
198-
{ coreState := ← getThe Core.State
202+
{ coreState := s
199203
coreContext := ← readThe Core.Context
200204
metaState := ← getThe Meta.State
201205
metaContext := ← readThe Meta.Context

test/Mathlib/test/exact.expected.out

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{"env": 0}
2+
3+
{"sorries":
4+
[{"proofState": 0,
5+
"pos": {"line": 1, "column": 27},
6+
"goal": "⊢ 0 < 1",
7+
"endPos": {"line": 1, "column": 32}}],
8+
"messages":
9+
[{"severity": "warning",
10+
"pos": {"line": 1, "column": 8},
11+
"endPos": {"line": 1, "column": 12},
12+
"data": "declaration uses 'sorry'"}],
13+
"env": 1}
14+
15+
{"proofState": 1,
16+
"messages":
17+
[{"severity": "info",
18+
"pos": {"line": 0, "column": 0},
19+
"endPos": {"line": 0, "column": 0},
20+
"data": "Try this: exact Nat.le.refl"}],
21+
"goals": []}
22+
23+
{"sorries":
24+
[{"proofState": 2,
25+
"pos": {"line": 1, "column": 27},
26+
"goal": "⊢ 3 = 7",
27+
"endPos": {"line": 1, "column": 32}}],
28+
"messages":
29+
[{"severity": "warning",
30+
"pos": {"line": 1, "column": 8},
31+
"endPos": {"line": 1, "column": 12},
32+
"data": "declaration uses 'sorry'"}],
33+
"env": 2}
34+
35+
{"message":
36+
"Lean error:\n`exact?` could not close the goal. Try `apply?` to see partial suggestions."}
37+

test/Mathlib/test/exact.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{"cmd": "import Mathlib\n\nset_option maxHeartbeats 500000"}
2+
3+
{"cmd": "theorem test : 0 < 1 := by sorry", "env": 0}
4+
5+
{"tactic": "exact?", "proofState": 0}
6+
7+
{"cmd": "theorem test : 3 = 7 := by sorry", "env": 0}
8+
9+
{"tactic": "exact?", "proofState": 2}
10+

0 commit comments

Comments
 (0)