Skip to content

Commit 934dbc9

Browse files
committed
feat: finish? produces partial tactic scripts
This PR ensures that `finish?` produces partial tactic scripts containing `sorry`s. We may add an option to disable this feature in the future. It is enabled by default because it provides a useful way to debug `grind` failures.
1 parent a24d1a6 commit 934dbc9

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

src/Lean/Meta/Tactic/Grind/Action.lean

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,15 @@ def loop (n : Nat) (x : Action) : Action := fun goal _ kp =>
123123
Runs action `a` on the given `goal`.
124124
-/
125125
def run (goal : Goal) (a : Action) : GrindM ActionResult := do
126-
let k := fun goal => if goal.inconsistent then return .closed [] else return .stuck [goal]
126+
let k := fun goal => do
127+
if goal.inconsistent then
128+
return .closed []
129+
else if (← getConfig).trace then
130+
goal.mvarId.admit
131+
let sorryTac ← `(grind| sorry)
132+
return .closed [sorryTac]
133+
else
134+
return .stuck [goal]
127135
a goal k k
128136

129137
/--

src/Lean/Meta/Tactic/Grind/Split.lean

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,12 @@ hypotheses introduced during case analysis. If the proof is of the form `fun _ _
294294
-/
295295
private def getFalseProof? (mvarId : MVarId) : MetaM (Option Expr) := mvarId.withContext do
296296
let proof ← instantiateMVars (mkMVar mvarId)
297-
go proof
297+
if proof.hasSyntheticSorry then
298+
/- **Note**: We do not perform non-chronological backtracking if the proof
299+
contains synthetic `sorry`. -/
300+
return none
301+
else
302+
go proof
298303
where
299304
go (proof : Expr) : MetaM (Option Expr) := do
300305
match_expr proof with
@@ -344,11 +349,25 @@ private def isCompressibleAlts (alts : Array (List (TSyntax `grind))) : Bool :=
344349
else
345350
true
346351

352+
def isSorryAlt (alt : List (TSyntax `grind)) : Bool :=
353+
match alt with
354+
| [tac] => match tac with
355+
| `(grind| sorry) => true
356+
| _ => false
357+
| _ => false
358+
347359
private def mkCasesResultSeq (cases : TSyntax `grind) (alts : Array (List (TSyntax `grind)))
348360
(compress : Bool) : CoreM (List (TSyntax `grind)) := do
349361
if compress && isCompressibleAlts alts then
350-
if h : alts.size > 0 then
351-
return [(← mkCasesAndThen cases alts[0]!)]
362+
if alts.size > 0 then
363+
let firstAlt := alts[0]!
364+
if isSorryAlt firstAlt then
365+
/-
366+
**Note**: It is a bit pointless to return a script of the form `cases #<anchor> <;> sorry`
367+
-/
368+
return firstAlt
369+
else
370+
return [(← mkCasesAndThen cases firstAlt)]
352371
else
353372
return [cases]
354373
else

tests/lean/run/grind_finish_trace.lean

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,42 @@ example (as bs cs : Array α) (v₁ v₂ : α)
6060
(h₆ : j < as.size)
6161
: cs[j] = as[j] := by
6262
grind => finish?
63+
64+
set_option warn.sorry false
65+
66+
/--
67+
info: Try this:
68+
[apply]
69+
cases #c4b6
70+
next =>
71+
ring
72+
cases #8c9f
73+
next => ring
74+
next => sorry
75+
next =>
76+
ring
77+
cases #8c9f
78+
next => ring
79+
next => sorry
80+
-/
81+
#guard_msgs in
82+
example {α : Type} [CommRing α] (a b c d e : α) :
83+
(a^2 = c * b ∨ e^2 = d * c) →
84+
(b^2 = d*c ∨ b^2 = c*d) →
85+
a*b*(b*a) = c^2*b*d := by
86+
grind => finish?
87+
88+
/--
89+
info: Try this:
90+
[apply]
91+
instantiate only [= Nat.min_def]
92+
cases #7640
93+
next => sorry
94+
next =>
95+
ring
96+
lia
97+
-/
98+
#guard_msgs in
99+
example (as : Array α) (lo hi i j : Nat) (h₁ : lo ≤ i) (_ : i < j) (_ : j ≤ hi) (_ : j < as.size)
100+
(_ : ¬as.size = 0) : min lo (as.size - 1) < i := by
101+
grind => finish?

0 commit comments

Comments
 (0)