Skip to content

Commit 70a5aef

Browse files
committed
feat: config options at finish
This PR adds support for configuration options at `finish` and `finish?`.
1 parent 8ff5b8e commit 70a5aef

File tree

5 files changed

+53
-28
lines changed

5 files changed

+53
-28
lines changed

src/Init/Grind/Interactive.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ syntax (name := casesTrace) "cases?" grindFilter : grind
129129
syntax (name := done) "done" : grind
130130

131131
/-- `finish` tries to close the current goal using `grind`'s default strategy -/
132-
syntax (name := finish) "finish" : grind
132+
syntax (name := finish) "finish" ppSpace configItem* : grind
133133

134134
/-- `finish?` tries to close the current goal using `grind`'s default strategy and suggests a tactic script. -/
135-
syntax (name := finishTrace) "finish?" : grind
135+
syntax (name := finishTrace) "finish?" ppSpace configItem* : grind
136136

137137
/--
138138
The `have` tactic is for adding opaque definitions and hypotheses to the local context of the main goal.

src/Lean/Elab/Tactic/Grind/BuiltinTactic.lean

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,16 @@ def evalGrindSeq : GrindTactic := fun stx =>
8282

8383
open Meta Grind
8484

85-
@[builtin_grind_tactic finish] def evalFinish : GrindTactic := fun _ => do
86-
let goal ← getMainGoal
87-
if let some goal ← liftGrindM <| solve goal then
88-
let params := (← read).params
89-
let result ← liftGrindM do mkResult params (some goal)
90-
throwError "`finish` failed\n{← result.toMessageData}"
91-
else
92-
replaceMainGoal []
85+
@[builtin_grind_tactic finish] def evalFinish : GrindTactic := fun stx => withMainContext do
86+
let `(grind| finish $[$configItems]*) := stx | throwUnsupportedSyntax
87+
withConfigItems configItems do
88+
let goal ← getMainGoal
89+
if let some goal ← liftGrindM <| solve goal then
90+
let params := (← read).params
91+
let result ← liftGrindM do mkResult params (some goal)
92+
throwError "`finish` failed\n{← result.toMessageData}"
93+
else
94+
replaceMainGoal []
9395

9496
/--
9597
Helper function to executing "check" tactics that return a flag indicating
@@ -432,10 +434,7 @@ where
432434

433435
@[builtin_grind_tactic setConfig] def elabSetConfig : GrindTactic := fun stx => do
434436
let `(grind| set_config $[$items:configItem]* in $seq:grindSeq) := stx | throwUnsupportedSyntax
435-
let config := (← read).ctx.config
436-
let config ← elabConfigItems config items
437-
withReader (fun c => { c with ctx.config := config }) do
438-
evalGrindTactic seq
437+
withConfigItems items do evalGrindTactic seq
439438

440439
@[builtin_grind_tactic mbtc] def elabMBTC : GrindTactic := fun _ => do
441440
liftGoalM do

src/Lean/Elab/Tactic/Grind/Config.lean

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Authors: Leonardo de Moura
55
-/
66
module
77
prelude
8-
public import Lean.CoreM
8+
public import Lean.Elab.Tactic.Grind.Basic
99
meta import Lean.Elab.Tactic.ConfigSetter
1010
public section
1111
namespace Lean.Elab.Tactic.Grind
@@ -29,4 +29,13 @@ def elabConfigItems (init : Grind.Config) (items : Array (TSyntax `Lean.Parser.T
2929
| _ => throwErrorAt item "unexpected configuration option"
3030
return config
3131

32+
def withConfigItems (items : Array (TSyntax `Lean.Parser.Tactic.configItem))
33+
(k : GrindTacticM α) : GrindTacticM α := do
34+
if items.isEmpty then
35+
k
36+
else
37+
let config := (← read).ctx.config
38+
let config ← elabConfigItems config items
39+
withReader (fun c => { c with ctx.config := config }) do k
40+
3241
end Lean.Elab.Tactic.Grind

src/Lean/Elab/Tactic/Grind/Trace.lean

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Authors: Leonardo de Moura
66
module
77
prelude
88
public import Lean.Elab.Tactic.Grind.Basic
9+
import Lean.Elab.Tactic.Grind.Config
910
import Init.Grind.Interactive
1011
import Lean.Meta.Tactic.TryThis
1112
import Lean.Meta.Tactic.Grind.Action
@@ -26,18 +27,20 @@ def mkFinish (maxIterations : Nat) : IO Action := do
2627
def maxIterations := 1000 -- **TODO**: Add option
2728

2829
@[builtin_grind_tactic finishTrace] def evalFinishTrace : GrindTactic := fun stx => do
29-
let a ← mkFinish maxIterations
30-
let goal ← getMainGoal
31-
withTracing do
32-
match (← liftGrindM <| a.run goal) with
33-
| .closed seq =>
34-
replaceMainGoal []
35-
let seq := Action.mkGrindSeq seq
36-
Tactic.TryThis.addSuggestion stx { suggestion := .tsyntax seq }
37-
| .stuck gs =>
38-
let goal :: _ := gs | throwError "`finish?` failed, but resulting goal is not available"
39-
let params := (← read).params
40-
let result ← liftGrindM do mkResult params (some goal)
41-
throwError "`finish?` failed\n{← result.toMessageData}"
30+
let `(grind| finish? $[$configItems]*) := stx | throwUnsupportedSyntax
31+
withConfigItems configItems do
32+
let a ← mkFinish maxIterations
33+
let goal ← getMainGoal
34+
withTracing do
35+
match (← liftGrindM <| a.run goal) with
36+
| .closed seq =>
37+
replaceMainGoal []
38+
let seq := Action.mkGrindSeq seq
39+
Tactic.TryThis.addSuggestion stx { suggestion := .tsyntax seq }
40+
| .stuck gs =>
41+
let goal :: _ := gs | throwError "`finish?` failed, but resulting goal is not available"
42+
let params := (← read).params
43+
let result ← liftGrindM do mkResult params (some goal)
44+
throwError "`finish?` failed\n{← result.toMessageData}"
4245

4346
end Lean.Elab.Tactic.Grind

tests/lean/run/grind_set_config.lean

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,17 @@ example : f (x + 100) = a := by
2020
set_config (lia := true) in
2121
have : 3*x ≠ 1
2222
sorry
23+
24+
opaque foo : Nat → Nat
25+
axiom fooAx1 : foo (x + 1) = foo x
26+
axiom fooAx2 : foo 010
27+
28+
example : foo 405 := by
29+
grind [fooAx1] =>
30+
have := fooAx2
31+
finish (gen := 50) (ematch := 50)
32+
33+
example : foo 105 := by
34+
grind [fooAx1] =>
35+
have := fooAx2
36+
finish? (gen := 10) (ematch := 10)

0 commit comments

Comments
 (0)