Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moogle syntax #3

Merged
merged 7 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions LeanSearchClient/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ register_option leansearch.queries : Nat :=
{ defValue := 6
group := "leansearch"
descr := "Number of results requested from leansearch (default 6)" }

register_option moogle.queries : Nat :=
{ defValue := 6
group := "moogle"
descr := "Number of results requested from leansearch (default 6)" }
34 changes: 34 additions & 0 deletions LeanSearchClient/MoogleExamples.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import LeanSearchClient.Syntax

/-!
# Moogle Examples

Examples of using the Moogle API. The search is triggered when the sentence ends with a full stop (period) or a question mark.
-/

/--
warning: #moogle query should end with a `.` or `?`.
Note this command sends your query to an external service at https://www.moogle.ai/api/search.
-/
#guard_msgs in
#moogle "If a natural number n is less than m, then the successor of n is less than the successor of m"

/--
warning: #moogle query should end with a `.` or `?`.
Note this command sends your query to an external service at https://www.moogle.ai/api/search.
-/
#guard_msgs in
example := #moogle "If a natural number n is less than m, then the successor of n is less than the successor of m"

set_option moogle.queries 1

/--
info: From: Nat.lt (type: Nat → Nat → Prop)
• have : Nat → Nat → Prop := Nat.lt
---
warning: declaration uses 'sorry'
-/
#guard_msgs in
example : 3 ≤ 5 := by
#moogle "If a natural number n is less than m, then the successor of n is less than the successor of m."
sorry
175 changes: 155 additions & 20 deletions LeanSearchClient/Syntax.lean
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@ import LeanSearchClient.Basic
# LeanSearchClient

In this file, we provide syntax for search using the [leansearch API](https://leansearch.net/)
and the [Moogle API](https://www.moogle.ai/api/search).
from within Lean. It allows you to search for Lean tactics and theorems using natural language.

We provide syntax to make a query and generate `TryThis` options to click or
use a code action to use the results.

The queries are of three forms:
The queries are of three forms. For leansearch these are:

* `Command` syntax: `#leansearch "search query"` as a command.
* `Term` syntax: `#leansearch "search query"` as a term.
* `Tactic` syntax: `#leansearch "search query"` as a tactic.

The corresponding syntax for Moogle is:

* `Command` syntax: `#moogle "search query"` as a command.
* `Term` syntax: `#moogle "search query"` as a term.
* `Tactic` syntax: `#moogle "search query"` as a tactic.

In all cases results are displayed in the Lean Infoview and clicking these replaces the query text.
In the cases of a query for tactics only valid tactics are displayed.
-/
Expand All @@ -30,27 +37,48 @@ namespace LeanSearchClient

open Lean Meta Elab Tactic Parser Term

def getQueryJson (s : String) (num_results : Nat := 6) : IO <| Array Json := do
def getLeanSearchQueryJson (s : String) (num_results : Nat := 6) : IO <| Array Json := do
let apiUrl := "https://leansearch.net/api/search"
let s' := s.replace " " "%20"
let q := apiUrl ++ s!"?query={s'}&num_results={num_results}"
let s ← IO.Process.output {cmd := "curl", args := #["-X", "GET", q]}
let js := Json.parse s.stdout |>.toOption |>.get!
return js.getArr? |>.toOption |>.get!

def getMoogleQueryJson (s : String) (num_results : Nat := 6) : IO <| Array Json := do
let apiUrl := "https://www.moogle.ai/api/search"
let data := Json.arr
#[Json.mkObj [("isFind", false), ("contents", s)]]
let s ← IO.Process.output {cmd := "curl", args := #[apiUrl, "-H", "content-type: application/json", "--data", data.pretty]}
match Json.parse s.stdout with
| Except.error e =>
IO.throwServerError s!"Could not parse JSON from {s.stdout}; error: {e}"
| Except.ok js =>
let result? := js.getObjValAs? Json "data"
match result? with
| Except.ok result =>
match result.getArr? with
| Except.ok arr => return arr[0:num_results]
| Except.error e => IO.throwServerError s!"Could not obtain array from {js}; error: {e}"
| _ => IO.throwServerError s!"Could not obtain data from {js}"


structure SearchResult where
name : String
type? : Option String
docString? : Option String
doc_url? : Option String
kind? : Option String
deriving Repr

def queryNum : CoreM Nat := do
def leansearchQueryNum : CoreM Nat := do
return leansearch.queries.get (← getOptions)

def moogleQueryNum : CoreM Nat := do
return moogle.queries.get (← getOptions)
namespace SearchResult

def ofJson? (js : Json) : Option SearchResult :=
def ofLeanSearchJson? (js : Json) : Option SearchResult :=
match js.getObjValAs? String "formal_name" with
| Except.ok name =>
let type? := js.getObjValAs? String "formal_type" |>.toOption
Expand All @@ -61,10 +89,35 @@ def ofJson? (js : Json) : Option SearchResult :=
some {name := name, type? := type?, docString? := doc?, doc_url? := docurl?, kind? := kind?}
| _ => none

def query (s : String) (num_results : Nat) :
def ofMoogleJson? (js : Json) : MetaM <| Option SearchResult :=
match js.getObjValAs? String "declarationName" with
| Except.ok name => do
let type? ←
try
let info ← getConstInfo name.toName
let fmt ← PrettyPrinter.ppExpr info.type
pure <| some fmt.pretty
catch _ =>
pure none
let doc? := js.getObjValAs? String "declarationDocString" |>.toOption
let doc? := doc?.filter fun s => s != ""
let docurl? := none
let kind? := js.getObjValAs? String "declarationType" |>.toOption
return some {name := name, type? := type?, docString? := doc?, doc_url? := docurl?, kind? := kind?}
| _ => return none

def queryLeanSearch (s : String) (num_results : Nat) :
IO <| Array SearchResult := do
let jsArr ← getQueryJson s num_results
return jsArr.filterMap ofJson?
let jsArr ← getLeanSearchQueryJson s num_results
return jsArr.filterMap ofLeanSearchJson?

def queryMoogle (s : String) (num_results : Nat) :
MetaM <| Array SearchResult := do
let jsArr ← getMoogleQueryJson s num_results
jsArr.filterMapM ofMoogleJson?

-- #eval queryMoogle "There are infinitely many primes" 12

siddhartha-gadgil marked this conversation as resolved.
Show resolved Hide resolved

def toCommandSuggestion (sr : SearchResult) : TryThis.Suggestion :=
let data := match sr.docString? with
Expand All @@ -87,25 +140,44 @@ def toTacticSuggestions (sr : SearchResult) : Array TryThis.Suggestion :=

end SearchResult

def getQueryCommandSuggestions (s : String) (num_results : Nat) :
def getLeanSearchQueryCommandSuggestions (s : String) (num_results : Nat) :
IO <| Array TryThis.Suggestion := do
let searchResults ← SearchResult.query s num_results
let searchResults ← SearchResult.queryLeanSearch s num_results
return searchResults.map SearchResult.toCommandSuggestion

def getQueryTermSuggestions (s : String) (num_results : Nat) :
def getLeanSearchQueryTermSuggestions (s : String) (num_results : Nat) :
IO <| Array TryThis.Suggestion := do
let searchResults ← SearchResult.query s num_results
let searchResults ← SearchResult.queryLeanSearch s num_results
return searchResults.map SearchResult.toTermSuggestion

def getQueryTacticSuggestionGroups (s : String) (num_results : Nat) :
def getLeanSearchQueryTacticSuggestionGroups (s : String) (num_results : Nat) :
IO <| Array (String × Array TryThis.Suggestion) := do
let searchResults ← SearchResult.query s num_results
let searchResults ← SearchResult.queryLeanSearch s num_results
return searchResults.map fun sr =>
let fullName := match sr.type? with
| some type => s!"{sr.name} (type: {type})"
| none => sr.name
(fullName, sr.toTacticSuggestions)

def getMoogleQueryCommandSuggestions (s: String)(num_results : Nat) :
MetaM <| Array TryThis.Suggestion := do
let searchResults ← SearchResult.queryMoogle s num_results
return searchResults.map SearchResult.toCommandSuggestion

def getMoogleQueryTermSuggestions (s: String)(num_results : Nat) :
MetaM <| Array TryThis.Suggestion := do
let searchResults ← SearchResult.queryMoogle s num_results
return searchResults.map SearchResult.toTermSuggestion

def getMoogleQueryTacticSuggestionGroups (s: String)(num_results : Nat) :
MetaM <| Array (String × Array TryThis.Suggestion) := do
let searchResults ← SearchResult.queryMoogle s num_results
return searchResults.map fun sr =>
let fullName := match sr.type? with
| some type => s!"{sr.name} (type: {type})"
| none => sr.name
(fullName, sr.toTacticSuggestions)

def defaultTerm (expectedType? : Option Expr) : MetaM Expr := do
match expectedType? with
| some type =>
Expand All @@ -128,10 +200,14 @@ def checkTactic (target : Expr) (tac : Syntax) :
catch _ =>
return none

def incompleteQuery : String :=
def incompleteLeanSearchQuery : String :=
"#leansearch query should end with a `.` or `?`.\n\
Note this command sends your query to an external service at https://leansearch.net/."

def incompleteMoogleQuery : String :=
"#moogle query should end with a `.` or `?`.\n\
Note this command sends your query to an external service at https://www.moogle.ai/api/search."

siddhartha-gadgil marked this conversation as resolved.
Show resolved Hide resolved
open Command

syntax (name := leansearch_cmd) "#leansearch" str : command
Expand All @@ -142,10 +218,10 @@ syntax (name := leansearch_cmd) "#leansearch" str : command
| `(command| #leansearch $s) =>
let s := s.getString
if s.endsWith "." || s.endsWith "?" then
let suggestions ← getQueryCommandSuggestions s (← queryNum)
let suggestions ← getLeanSearchQueryCommandSuggestions s (← leansearchQueryNum)
TryThis.addSuggestions stx suggestions (header := "Lean Search Results")
else
logWarning incompleteQuery
logWarning incompleteLeanSearchQuery
| _ => throwUnsupportedSyntax

syntax (name := leansearch_term) "#leansearch" str : term
Expand All @@ -156,10 +232,10 @@ syntax (name := leansearch_term) "#leansearch" str : term
| `(#leansearch $s) =>
let s := s.getString
if s.endsWith "." || s.endsWith "?" then
let suggestions ← getQueryTermSuggestions s (← queryNum)
let suggestions ← getLeanSearchQueryTermSuggestions s (← leansearchQueryNum)
TryThis.addSuggestions stx suggestions (header := "Lean Search Results")
else
logWarning incompleteQuery
logWarning incompleteLeanSearchQuery
defaultTerm expectedType?
| _ => throwUnsupportedSyntax

Expand All @@ -173,7 +249,66 @@ syntax (name := leansearch_tactic) "#leansearch" str : tactic
if s.endsWith "." || s.endsWith "?" then
let target ← getMainTarget
let suggestionGroups ←
getQueryTacticSuggestionGroups s (← queryNum)
getLeanSearchQueryTacticSuggestionGroups s (← leansearchQueryNum)
for (name, sg) in suggestionGroups do
let sg ← sg.filterM fun s =>
let sugTxt := s.suggestion
match sugTxt with
| .string s => do
let stx? := runParserCategory (← getEnv) `tactic s
match stx? with
| Except.ok stx =>
let n? ← checkTactic target stx
return n?.isSome
| Except.error _ =>
pure false
| _ => pure false
unless sg.isEmpty do
TryThis.addSuggestions stx sg (header := s!"From: {name}")
else
logWarning incompleteLeanSearchQuery
| _ => throwUnsupportedSyntax

syntax (name := moogle_cmd) "#moogle" str : command

@[command_elab moogle_cmd] def moogleCommandImpl : CommandElab :=
fun stx => Command.liftTermElabM do
match stx with
| `(command| #moogle $s) =>
let s := s.getString
if s.endsWith "." || s.endsWith "?" then
let suggestions ← getMoogleQueryCommandSuggestions s (← moogleQueryNum)
TryThis.addSuggestions stx suggestions (header := "Moogle Results")
else
logWarning incompleteMoogleQuery
| _ => throwUnsupportedSyntax

syntax (name := moogle_term) "#moogle" str : term

@[term_elab moogle_term] def moogleTermImpl : TermElab :=
fun stx expectedType? => do
match stx with
| `(#moogle $s) =>
let s := s.getString
if s.endsWith "." || s.endsWith "?" then
let suggestions ← getMoogleQueryTermSuggestions s (← moogleQueryNum)
TryThis.addSuggestions stx suggestions (header := "Moogle Results")
else
logWarning incompleteMoogleQuery
defaultTerm expectedType?
| _ => throwUnsupportedSyntax

syntax (name := moogle_tactic) "#moogle" str : tactic

@[tactic moogle_tactic] def moogleTacticImpl : Tactic :=
fun stx => withMainContext do
match stx with
| `(tactic|#moogle $s) =>
let s := s.getString
if s.endsWith "." || s.endsWith "?" then
let target ← getMainTarget
let suggestionGroups ←
getMoogleQueryTacticSuggestionGroups s (← moogleQueryNum)
for (name, sg) in suggestionGroups do
let sg ← sg.filterM fun s =>
let sugTxt := s.suggestion
Expand All @@ -190,5 +325,5 @@ syntax (name := leansearch_tactic) "#leansearch" str : tactic
unless sg.isEmpty do
TryThis.addSuggestions stx sg (header := s!"From: {name}")
else
logWarning incompleteQuery
logWarning incompleteMoogleQuery
| _ => throwUnsupportedSyntax