LeanExplore: A search engine for Lean 4 declarations

Justin Asher · 2025

Read LeanExplore: A search engine for Lean 4 declarations by Justin Asher (2025) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2506-11085.txt.

Introduction

Justin Asher writes alone in a 2025 paper on searching a proof library. Lean 4, a proof assistant, now holds vast libraries that users struggle to navigate. His search engine, LeanExplore, covers several major packages, from Mathlib to PhysLean. Users can search statements by meaning, in plain words or in formal form.

Ranking blends three signals: meaning scores from a model fed code, doc text, informal versions, and titles; a classic keyword score; and a link based importance score. The engine runs on a public website and through a Python interface. Proof libraries grow faster than any user's memory, so meaning based search may become core infrastructure for formal mathematics.

Catalog record

CREATORJustin Asher
YEAR2025
COLLECTIONPAPERS
FORMATTXT
FILE SIZE51 KB
VERSION1.0
LICENSEhttp://creativecommons.org/licenses/by/4.0/
RIGHTSCC BY
SOURCEarXiv

Source: https://arxiv.org/abs/2506-11085

Browse the full library · About the Tower of Babel

Full text

The opening of the work, from the library's hosted text:

LeanExplore: A search engine for Lean 4 declarations Justin Asher Abstract The expanding Lean 4 ecosystem poses challenges for navigating its vast libraries. This paper introduces LeanExplore, a search engine for Lean 4 declarations. LeanExplore enables users to semantically search for statements, both formally and informally, across select Lean 4 packages 1 1 1 Currently indexed packages include: Batteries, Init, Lean, Mathlib, PhysLean, and Std. . This search capability is powered by a hybrid ranking strategy, integrating scores from a multi-source semantic embedding model (capturing conceptual meaning from formal Lean code, docstrings, AI-generated informal translations, and declaration titles), BM25+ for keyword-based lexical relevance, and a PageRank-based score reflecting declaration importance and interconnectedness. The search engine is accessible via a dedicated website 2 2 2 https://www.leanexplore.com/ and a Python API 3 3 3 https://github.com/justincasher/lean-explore . Furthermore, the database can be downloaded, allowing users to self-host the service. LeanExplore integrates easily with LLMs via the model context protocol (MCP), enabling users to chat with an AI assistant about Lean declarations or utilize the search engine for building theorem-proving agents. This work details LeanExplore’s architecture, data processing, functionalities, and its potential to enhance Lean 4 workflows and AI-driven mathematical research. 1 Introduction Lean 4 ( de Moura and Ullrich 2021 ) is a type-theoretic programming language and interactive theorem prover, enabling users to formally verify the correctness of mathematical theorems and complex software systems. Its ecosystem has seen significant expansion, largely driven by comprehensive libraries such as Mathlib ( Leanprover Community ) , which formalizes a vast corpus of graduate-level mathematics, and emerging specialized packages like PhysLean for physics ( HEPLean Community ) . This growth, while a testament to Lean’s power and community engagement, introduces substantial challenges in navigating the extensive collection of existing code and formalized knowledge. The task of formalizing new mathematical statements or developing verified software often requires users to locate, understand, and utilize numerous existing definitions, theorems, and tactics—a discovery process that can be particularly arduous, even for experienced researchers. Effectively navigating these large codebases is frequently hampered because not all declarations are accompanied by descriptive docstrings, and conventional keyword-based search methods often prove inadequate. For instance, a naive search for a well-known concept like the "fundamental theorem of calculus" within the extensive Mathlib documentation ( The Mathlib Community ) yields no direct results. This example illustrates the significant difficulty in bridging the gap between conceptual mathematical ideas and their specific, formal representations in Lean, especially when attempting to search using natural language. To mitigate these difficulties, we introduce LeanExplore, a search engine for Lean 4 declarations. Implemented both as a website and a Python package, LeanExplore offers robust semantic search capabilities alongside a suite of specialized tools. These resources are engineered to significantly enhance how users locate, understand, and interact with the vast and growing body of Lean code. Crucially, the system design facilitates not only more intuitive human engagement with Lean but also the effective integration of its formal capabilities as a powerful tool for artificial intelligence. This paper makes several key contributions: • We introduce a hybrid search methodology tailored for the Lean 4 domain, significantly improving declaration retrieval. At its core, this approach ranks declarations by combining scores from three distinct signals: multi-source semantic embeddings (derived from formal Lean code, docstrings, AI-generated informal translations, and declaration titles), BM25+ for keyword-based lexical relevance, and a PageRank-based score reflecting declaration importance and interconnectedness. This integrated ranking strategy enables users to interact with the database semantically, through keywords, or by known titles, offering versatile and effective discovery. • We detail a flexible system architecture that supports two distinct operational modes: local data access for offline use and maximal user control, and a remote API mode for zero-setup access to centrally managed datasets. • We describe the design and implementation of a robust data pipeline that transforms Lean 4 source code into a structured and semantically rich knowledge base. This pipeline introduces StatementGroup s, a novel abstraction for grouping related declarations. Furthermore, it employs an LLM-driven, inductive process that leverages inter-declaration dependencies to generate high-quality informal English translations for formal statements, significantly enhancing the data for effective semantic search. • We introduce a model context protocol (MCP) server implementation, which exposes LeanExplore’s functionalities as a set of tools consumable by external AI agent systems. This feature aims to foster new avenues for AI research in formal mathematics by allowing AI to directly leverage Lean’s structured knowledge. We demonstrate the effectiveness of this MCP through an AI chatbot that users can access via the CLI. The remainder of this paper is structured as follows: Section 2 provides an overview of relevant background and related work. Section 3 elaborates on the system architecture, focusing on its data processing and search capabilities. Section 4 describes the core features and functionalities. Section 5 discusses various use cases and illustrative scenarios. Section 6 presents the experimental evaluation of LeanExplore’s search efficacy. Finally, Section 7 outlines future work and directions, and Section 8 concludes the paper. 2 Background and related work The ability to search formal mathematics databases is not a unique challenge for Lean. Users of many other interactive theorem provers (ITPs), such as Coq, Isabelle, and Agda, have also faced this navigation difficulty ( Beckert et al. 2014 ) . While many ITPs offer built-in search commands, these often require users to guess specific patterns or names and may be limited to currently loaded theories. To address this, external tools have emerged. For instance, Isabelle’s Sledgehammer connects to external automated theorem provers (ATPs) to find relevant lemmas and proofs ( Blanchette et al. 2011 ) , and its FindFacts tool indexes the entire corpus for text-based search ( Huch and Krauss 2022 ) . Agda’s “Search About” command leverages its rich type system for contextual search ( Agda Team 2020 ) . Within the Lean ( de Moura and Ullrich 2021 ) ecosystem itself, prior to LeanExplore, tools like Loogle provided pattern-based search for Mathlib ( Leanprover Community ; Lean FRO ) , and more recent semantic search engines like Moogle.ai ( Morph Labs ) and LeanSearch ( Gao et al. 2024 ) have applied embeddings to allow natural language queries over Mathlib. The evolution of code search provides important parallels. Early systems often treated code as plain text, allowing keyword or regular expression queries. However, the structured nature of code led to the development of syntax-aware and semantic search techniques. Tools like Hoogle for Haskell, which enables searching by type signature, demonstrated the power of leveraging language-specific semantics ( Mitchell 2008 ) . Searching formal languages presents unique challenges due to their precise syntax and semantics, where minor textual variations can drastically alter meaning, necessitating high precision and an understanding of logical structure. Semantic search, which aims to understand user intent and contextual meaning rather than just matching keywords, has become pivotal. This is typically powered by embeddings—dense vector representations where semantic similarity corresponds to proximity in the vector space ( Bengio et al. 2013 ) . While general text embeddings (e.g., from sentence transformers ( Reimers and Gurevych 2019 ) ) provide a baseline, there’s a strong trend towards specialized embeddings for code (e.g., CodeBERT ( Feng et al. 2020

Read the complete text