Search Lean and Mathlib with Loogle
masterlean.nvim), and LeanSearchClient.repository·master·Indexed 41 days ago
https://github.com/nomeata/loogleA search engine for Lean and Mathlib declarations that supports pattern matching, constant search, and subexpression queries. Available as a CLI tool, web service with a JSON API, Zulip bot, and integrations for VS Code and Neovim.
lean.nvim), and LeanSearchClient.The Leanprover Zulip chat includes a loogle bot. To use it, mention the bot in a public stream followed by your query:
@**loogle** query
The bot will respond with the first two hits from the loogle search engine.
Loogle can be used directly within code editors via third-party extensions:
lean.nvimThe loogle web service can be run locally using the provided server.py wrapper. The wrapper must be executed from within the loogle repository directory.
To run the server, pass the directory of the project you wish to search via --project-dir. Any arguments following -- are forwarded directly to the loogle binary.
Example:
./server.py --project-dir /path/to/project -- --module RootModuleSupported Wrapper Arguments:
--host--port--loogle-bin--project-dirAPI Endpoint:
/json?q=... (instead of the standard /?q=... web interface). Note that the JSON format stability is not guaranteed../server.py --project-dir /path/to/project -- --module RootModuleLoogle supports several search modes to find definitions and lemmas:
Real.sin finds lemmas mentioning the sine function."differ" finds lemmas with "differ" in their name._ * (_ ^ _) finds products where the second argument is raised to a power.?a, ?b, etc., to match terms. Parameters are matched in any order.
Example: (?a -> ?b) -> List ?a -> List ?b will match List ?a -> (?a -> ?b) -> List ?b.|- prefix to search for lemmas where the conclusion (the part to the right of all → and ∀) matches a specific shape.
Example: |- tsum _ = _ * tsum _.🔍 Real.sin
🔍 "differ"
🔍 _ * (_ ^ _)
🔍 Real.sqrt ?a * Real.sqrt ?a
🔍 (?a -> ?b) -> List ?a -> List ?b
🔍 |- tsum _ = _ * tsum _To use the loogle binary to search a specific Lake project, the binary must be built with the same Lean toolchain as the target project. Follow these steps:
lean-toolchain into the loogle directory to ensure toolchain parity:cp /path/to/project/lean-toolchain .lake buildlake env, pointing it to the built loogle binary and specifying the module to search:lake env /path/to/loogle/.lake/build/bin/loogle --module MyModule "<query>"Indexing Behavior:
.olean file. Subsequent calls are fast and the index is automatically rebuilt if .olean files change.--index-file PATH to specify a writable location.--index-mode none.cp /path/to/project/lean-toolchain .
lake build
lake env /path/to/loogle/.lake/build/bin/loogle --module MyModule "<query>"You can refine your search by specifying whether you are looking for definitions (data) or theorems (propositions):
⊢ (_ : Type _).⊢ (_ : Prop).Combining Filters: You can pass multiple filters separated by commas to perform an AND search. Each filter's metavariables (?a) are assigned independently.
Example of a complex combined search:
Real.sin, "two", tsum, _ * _, _ ^ _, |- _ < _ → _
This finds lemmas that mention Real.sin AND tsum, have "two" in the name, include a product and a power, AND have a hypothesis of the form _ < _.
⊢ (_ : Type _)
⊢ (_ : Prop)
Real.sin, "two", tsum, _ * _, _ ^ _, |- _ < _ → _The server.py script provides an HTTP frontend for the Loogle search tool. It manages a pool of loogle subprocesses (workers) to handle search queries. You can configure the server using several command-line arguments.
To run the server with default settings (localhost:8088, 1 worker), use:
python3 server.pyTo forward specific arguments to the underlying loogle binary, use the -- separator. For example, to pass --module and --max-results to the loogle subprocess:
python3 server.py -- --module Init.Data.List.Basic --max-results 50python3 server.pyThe loogle command-line tool allows searching Lean/Mathlib declarations.
Usage:
loogle [OPTIONS] [QUERY]
Options:
USAGE: loogle [OPTIONS] [QUERY]
OPTIONS:
--help
--interactive, -i read querys from stdin
--json, -j print result in JSON format
--module mod import this module (default: Mathlib)
--path path search for .olean files here (default: the build time path)
--index-mode MODE how to manage the on-disk search index. One of:
use (default) load if present and up-to-date,
otherwise build and write
read load existing index; refuse to start if it
is missing or out of date
write always (re)build the index and write it
none build in memory and discard on exit
--index-file PATH override the default index path. The default lives
next to the root module's .olean (with .loogle-index
extension); pass this if that location is read-only.
--max-results n limit the number of returned hits (default: 200)The server supports a /zulipbot endpoint for integration with Zulip. This endpoint expects a POST request with a JSON body containing message data.
Endpoint: POST /zulipbot
Content-Type: application/json
Request Body Example:
{
"data": "@**loogle** Real.sin"
}Behavior:
data field, looking for the @**loogle** trigger.content string suitable for Zulip, including markdown links to documentation or suggestions if the query was ambiguous.The Loogle server provides a JSON endpoint for programmatic access to search results. To use it, send a GET request to the /json path with a query parameter q.
Endpoint: GET /json?q=<query>
Example Request:
GET /json?q=Real.sin
Response Format: Returns a JSON object containing search results. If the server is overloaded, it returns an HTTP 503 with a JSON error message. If a worker crashes, it may return an error field in the JSON.
Client Identification:
To ensure proper metrics tracking, clients should identify themselves via the x-loogle-client header or the User-Agent header. Supported identifiers include:
vscode-lean4 (via x-loogle-client)LeanSearchClient (via User-Agent)vscode-loogle (via User-Agent)nvim (via User-Agent)lean-lsp-mcp (via User-Agent)meta-agent (via User-Agent)AutoLeanPaper (via User-Agent)zulip (via POST to /zulipbot)json (default for other programmatic clients)If the prometheus_client package is installed, the server exposes a /metrics endpoint for monitoring.
Key Metrics:
versions: Info about Lean and mathlib versions.queries: Total number of queries.errors_backend: Queries failed because the backend crashed or timed out.errors_loogle: Queries where loogle itself returned an error response.errors_denylist: Requests rejected by the user-agent denylist.load_shed: Requests rejected because all workers were busy.restarts: Backend worker restarts (labeled by reason: sandbox, died, or unresponsive).workers_lost: Workers permanently removed from the pool after a failed restart.results: Histogram of results per query.heartbeats: Histogram of heartbeats per query.clients: Counter of clients used (labeled by client type).workers_available: Gauge of workers currently in the pool ready to serve requests.