lean4-htt/tests/lean/run/grind_try_extend.lean
Leonardo de Moura 007bd18bcb
feat: extensible evalAndSuggest for try? (#7831)
This PR adds extensibility to the `evalAndSuggest` procedure used to
implement `try?`. Users can now implement their own handlers for any
tactic. The new test demonstrates how this feature works.
2025-04-06 01:01:37 +00:00

31 lines
907 B
Text

import Lean
open Lean Meta Elab Tactic Try
-- Install a `TryTactic` handler for `assumption`
@[try_tactic assumption]
def evalTryApply : TryTactic := fun tac => do
-- We just use the default implementation, but return a different tactic.
evalAssumption tac
`(tactic| (trace "worked"; assumption))
/-- info: Try this: · trace "worked"; assumption -/
#guard_msgs (info) in
example (h : False) : False := by
try? (max := 1) -- at most one solution
-- `try?` uses `evalAndSuggest` the attribute `[try_tactic]` is used to extend `evalAndSuggest`.
-- Let's define our own `try?` that uses `evalAndSuggest`
elab stx:"my_try?" : tactic => do
-- Things to try
let toTry ← `(tactic| attempt_all | assumption | apply True | rfl)
evalAndSuggest stx toTry
/--
info: Try these:
• · trace "worked"; assumption
• rfl
-/
#guard_msgs (info) in
example (a : Nat) (h : a = a) : a = a := by
my_try?