arXiv:2610.30843cs.LGOct 2026

Manuscript Offprint

arXiv:2610.30843

Lean Finder V2: Semantic Search for Mathlib That Disambiguates User Queries

Jialin Lu, Xiaoyu Liu, Hengrui Luo, Rodrigo Stehling, Linxi Liu, Weiran Sun, Yuepeng Wang, Wuyang Chen

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

ABSTRACT

We introduce Lean Finder V2, a semantic search framework for Mathlib that disambiguates user queries through complementary model-level adversarial training and input-level query rewriting. Human mathematicians and coding agents rarely search Mathlib by providing detailed context. In practice, they query short properties, notation, aliases, or hypotheses, leaving much of the intended mathematics implicit. In contrast, existing Mathlib search engines unrealistically assume context-rich queries. The scarcity of real-world short queries paired with ground-truth Mathlib statements makes this ambiguity even more intractable. We argue that this is essentially a data problem. Through adversarial training, we scale up the generation of labeled short and ambiguous queries, and learn to disambiguate user queries at both model-level fine-tuning and input-level query rewriting. On the human-curated MathlibQR benchmark, our Lean Finder V2 sets a new state of the art, with at least 11% relative improvement over the previous best Mathlib semantic search engines. On top of our state-of-the-art retriever, optimized query rewriting further improves MathlibQR retrieval. On unlabeled queries from human and coding-agent interactions, Lean Finder V2 is ranked as the strongest system. Integrated with Claude Code, Lean Finder V2 improves proof success on downstream theorem-proving task. Lean Finder V2 is available at: https://leanfinder.github.io.

1 INTRODUCTION

Advances in the Lean proof assistant and its central library Mathlib are turning formal mathematics into a collaborative and verifiable workflow (De Moura et al., 2015; Moura & Ullrich, 2021). Spanning a broad range of mathematics, Mathlib now contains more than 400,000 theorems and definitions across roughly 10,000 files (Lean Prover Community, 2026). At this scale, effective theorem search is essential for both human and automated formalization, enabling users and systems to efficiently identify and reuse relevant results from Mathlib's large and growing library, and its quality shapes end-to-end proof success (Lu et al., 2026; Gao et al., 2026; Avigad & Massot, 2020). Alongside rapid progress in specialized Lean provers (Ren et al., 2025; Lin et al., 2025), agentic systems have achieved tremendous success from competition mathematics to frontier research. Many of these systems rely on Mathlib search engines to locate reusable results during proof construction (Liu et al., 2026; Varambally et al., 2026; Ju et al., 2026; Dressler, 2025).

Despite this progress, current theorem retrieval faces a central bottleneck: theorem-search queries from both mathematicians and coding agents are inherently ambiguous. Human mathematicians and coding agents rarely search by restating a complete theorem. They use short properties, notation, aliases, or hypotheses, and different users use different terms referring to the same mathematical result, leaving much of the intended mathematics implicit. Such everyday queries are challenging precisely because they expose wide ambiguity gaps, yet existing systems leave these gaps unaddressed. Lu et al. (2026) synthesized realistic queries from intent clusters mined from Lean discussions, but the resulting data are static and generated independently of its retriever's current failure modes. Other Lean search systems likewise rely largely on fixed, relatively detailed data, such as informalizations, formal statements, or proof states (Gao et al., 2026; 2024; Asher, 2025; Yang et al., 2023). We call such inputs context-rich: they preserve much of the source declaration's detail, unlike concise

∗^{*}Equal contribution.

Figure

(a) Query-length distributions.

Figure

(b) Recall@10 across query types.

Figure 1: Real-world queries expose the ambiguity gap. (a) Human and coding-agent queries contain far less explicit context than statement informalizations which are commonly used to train Lean search engines, as shown by the length gap. Our generated queries used to train retrievers better match the length of real-world queries. Table 6 shows more details. (b) Every existing Lean search engine performs worse on ambiguous queries compared to context-rich ones, with Lean Finder V2 closing the gap to the smallest 2.6 percentage points and having the best performance on both query types. This further demonstrates previous Lean search engines leave the ambiguity gap unaddressed.

real-world queries that express the same intent through a few salient cues. Figure 1(a) quantifies this mismatch: human and coding-agent queries have median lengths of 16 and 15 tokens (i.e., ambiguous), respectively, compared with 82 tokens (i.e., context-rich) for the statement informalizations commonly used for training previous Mathlib search engines. This gap is visible even when a shorter query remains semantically aligned with the same target theorem:

One target, two levels of query ambiguity

Target statement:

theorem Function.Even.const {α β : Type*} [Neg α] (b : β) : Function.Even (fun _ : α → b)

Context-rich query: “for a type α with a negation and any type β, every constant function f:α→βf: \alpha \to \beta, f(x)=bf(x) = b for some b∈βb \in \beta, satisfies f(−x)=f(x)f(-x) = f(x) for all x∈αx \in \alpha, i.e., constant functions are even”

Ambiguous query: “constant function invariant under input negation”

Note: Same-colored spans express the same content in both queries. Context-rich query contains information that is not in ambiguous query.

Both queries ask for the same mathematical fact, and the ambiguous query remains faithful to it. Yet leaving the Mathlib concept of an even function implicit moves the target from rank one to rank seven. Therefore, we ask our core question:

How to disambiguate Mathlib queries, at both model level and input level?

In this paper, we introduce Lean Finder V2, a semantic search framework for Mathlib that excels on ambiguous queries while retaining strong performance on context-rich queries. Lean Finder V2 addresses a data bottleneck caused by the scarcity of labeled ambiguous queries. We synthesize ambiguous query-declaration pairs as training data to improve the retriever through model-level adversarial training, and the same synthetic data is used in input-level prompt optimization for query rewriting. At the model level, a RL-trained attacker generates challenging and ambiguous queries for Mathlib declarations, with a difficulty reward that targets weaknesses of the retriever and an alignment reward that keeps each query faithful to its source declaration. These synthetic query-declaration pairs improve the retriever, whose feedback recalibrates query difficulty. At the input level, we use GEPA (Agrawal et al., 2026) to optimize a query-rewriting prompt on the same synthetic data with a ranking-based objective, making the intent of ambiguous queries explicit before retrieval. Lean Finder V2 sets a new state of the art on MathlibQR (Gao et al., 2026), with 14% relative improvement in Recall@10 over the previous best semantic search engine (Figure 1(b)). On top of the state-of-the-art retriever, the optimized rewriting prompt further improves retrieval on MathlibQR. When integrated with Claude Code, Lean Finder V2 also improves downstream proof success.

We summarize our contributions as follows:

  1. Lean Finder V2 sets a new state of the art on MathlibQR (Gao et al., 2026) while retaining strong performance on context-rich queries (Lu et al., 2026), achieving 14% relative gains over the previous-best semantic search system in Recall@10 on MathlibQR. On unlabeled queries for Mathlib drawn from real user interactions, Lean Finder V2 is ranked as most helpful 18+% more often than the previous best system by frontier LLM judges (Claude Opus 5 and GPT-5.6 Sol).

  2. Through retriever-guided adversarial training, we construct a training corpus of over 600K labeled ambiguous query-declaration pairs. The same corpus powers both model-level fine-tuning and input-level query-rewriting prompt optimization to disambiguate user queries. We will release this corpus to support future research.

  3. We present a study of input-side query rewriting. Using the same ambiguous query-declaration pairs for prompt optimization, GEPA-optimized query rewriting further improves MathlibQR retrieval for Lean Finder V2.

  4. Integrated with Claude Code, Lean Finder V2 improves downstream theorem proving, achieving a proof success rate of 39.3% compared to the next-best 36.0% on FATE-H (Jiang et al., 2026).