A Semantic Search Engine for Mathlib4

Guoxiong Gao, Haocheng Ju, Jiedong Jiang, Zihan Qin, Bin Dong

Abstract: The interactive theorem prover Lean enables the verification of formal mathematical proofs and is backed by an expanding community. Central to this ecosystem is its mathematical library, mathlib4, which lays the groundwork for the formalization of an expanding range of mathematical theories. However, searching for theorems in mathlib4 can be challenging. To successfully search in mathlib4, users often need to be familiar with its naming conventions or documentation strings. Therefore, creating a semantic search engine that can be used easily by individuals with varying familiarity with mathlib4 is very important. In this paper, we present a semantic search engine for mathlib4 that accepts informal queries and finds the relevant theorems. We also establish a benchmark for assessing the performance of various search engines for mathlib4.

Introduction

The Lean interactive theorem prover has become a cornerstone for formalizing mathematics. Its primary library, mathlib4, is vast but often inaccessible to newcomers. Finding a specific theorem typically requires knowledge of strict naming conventions (e.g., exists_ratio_deriv_eq_ratio_slope) or the presence of specific documentation strings. Traditional keyword-based search tools often fail when a user queries a theorem by its common mathematical name, such as "Cauchy’s Mean Value Theorem," if that name isn't explicitly in the code or docstring. This paper introduces a semantic search engine designed to understand the underlying mathematical meaning of queries, bridging the gap between natural language and formal Lean code.

Methodology

The proposed system utilizes a multi-stage pipeline to transform mathlib4 into a searchable semantic database.

The multi-stage pipeline for the semantic search engine, from informalization to query augmentation and retrieval.
  1. Informalization: Using GPT-3.5, formal Lean theorem statements are translated into natural language. To ensure accuracy, the model is provided with "contextual annotations," including docstrings and definitions of related functions extracted via hyperlinks.
  2. Bilingual Corpus: The search index stores pairs of original formal Lean code and their corresponding informal descriptions.
  3. Query Augmentation: To handle vague user inputs, GPT-4 expands queries into detailed "augmented queries" containing both precise natural language and predicted Lean-style formal statements.
  4. Dense Retrieval: The system employs the E5-mistral-7b embedding model to convert the bilingual corpus and augmented queries into high-dimensional vectors, performing retrieval via cosine similarity in a Chroma DB vector database.

Evaluation Framework

To evaluate performance, the authors developed the Mathlib4 Semantic Search Benchmark. This dataset includes 50 distinct queries across 18 intent groups, covering fields like calculus, algebra, and number theory. Queries are categorized into four formats: natural language descriptions, LaTeX formulas, theorem names, and Lean 4 term descriptions.

Results

The system was compared against baselines like BM25 and Moogle. The E5-mistral-7b model, integrated with the full pipeline (augmented corpus and query), achieved state-of-the-art results with an nDCG@20 of 0.733 and a Recall@10 of 0.913.

Performance comparison (NDCG@20) across various mathematical categories for formal, informal, and combined (F+IF) retrieval methods.

As shown in the results, the "Bilingual" approach (MIS_F+IF) consistently outperformed purely formal or informal methods across most categories. The system proved exceptionally effective at identifying theorems by their common names (e.g., "Schroeder Bernstein Theorem"), a significant improvement over traditional formal search tools.

Conclusion and Future Work

While the current system shows significant promise, there are areas for improvement. The context extraction process currently relies on hyperlink mapping, which can miss deep dependencies; future iterations could leverage a Lean language server for more precise mapping. Additionally, the computational cost of the 7B parameter embedding model is high. Future work will focus on expanding the benchmark and exploring fine-tuned, smaller embedding models specifically trained on mathematical formal-informal pairs to enhance precision and accessibility.

Summary updated

BibTeX
@inproceedings{gao2024semantic,
  author = {Guoxiong Gao and Haocheng Ju and Jiedong Jiang and Zihan Qin and Bin Dong},
  title = {{A Semantic Search Engine for Mathlib4}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  url = {https://openreview.net/forum?id=wIOw9EAN1H},
}
Figure