Skip to content

Commit

Permalink
filtering of tactics
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhartha-gadgil committed Sep 10, 2024
1 parent 6c3497f commit 62a7c53
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions MetaExamples/LeanSearch.lean
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ def defaultTerm (expectedType? : Option Expr) : MetaM Expr := do
return mkConst ``True.intro
| none => return mkConst ``True.intro

def checkTactic (target: Expr)(tac: Syntax):
TermElabM (Option Nat) :=
withoutModifyingState do
try
let goal ← mkFreshExprMVar target
let (goals, _) ←
withoutErrToSorry do
Elab.runTactic goal.mvarId! tac
(← read) (← get)
return some goals.length
catch _ =>
return none

open Command
syntax (name := lean_search_cmd) "#lean_search" str : command
@[command_elab lean_search_cmd] def leanSearchImpl : CommandElab :=
Expand Down Expand Up @@ -135,13 +148,29 @@ syntax (name := lean_search_term) "lean_search#" str : term

syntax (name := lean_search_tactic) "lean_search?" str : tactic
@[tactic lean_search_tactic] def leanSearchTacticImpl : Tactic :=
fun stx => do
fun stx => withMainContext do
match stx with
| `(tactic|lean_search? $s) =>
let s := s.getString
if s.endsWith "." || s.endsWith "?" then
let target ← getMainTarget
let suggestionGroups ← getQueryTacticSuggestionGroups s
for (name, sg) in suggestionGroups do
let sg ← sg.filterM fun s =>
let sugTxt := s.suggestion
match sugTxt with
| .string s => do
logInfo m!"Checking tactic: {s}"
logInfo m!"Target: {← getMainTarget}"
let stx? := runParserCategory (← getEnv) `tactic s
match stx? with
| Except.ok stx =>
let n? ← checkTactic target stx
return n?.isSome
| Except.error e =>
logInfo m!"Error: {e} while obtaining syntax tree"
pure false
| _ => pure false
TryThis.addSuggestions stx sg (header:= s!"From: {name}")
else
logWarning "Lean search query should end with a full stop (period) or a question mark."
Expand All @@ -159,7 +188,7 @@ An example of using the leansearch API. The search is triggered when the sentenc
example := lean_search# "There are infinitely many odd numbers"

example : 35 := by
lean_search? "If a natural number n is less than m, then the successor of n is less than the successor of m"
lean_search? "If a natural number n is less than m, then the successor of n is less than the successor of m."
sorry

open Parser
Expand Down

0 comments on commit 62a7c53

Please sign in to comment.