Theorem Reranker

Describe a theorem in English, LaTeX or a mix of both, and get back the matching declaration from Mathlib, Lean's library of formalised mathematics.

When
2026
Built with
Python, PyTorch, sentence-transformers, FAISS, Gradio, Kaggle GPUs, Weights & Biases
Code
CosmicVeil/rag-theorem-evaluation-project

Overview

Mathlib has over a hundred thousand theorems, and finding the one you need usually means already knowing its name. This project searches all 136,608 of them from a plain description. A fine-tuned embedding model narrows the corpus to 100 candidates, then a cross-encoder reads the top 20 closely and puts them in order.

On a 151-query benchmark I wrote, the right theorem comes first 79.5% of the time and appears in the top 10 for 94.7% of queries. The best off-the-shelf baseline gets 56.3% first.

Results

How it works

  1. Each theorem is indexed with its Lean statement, docstring and an informal English statement from the FrenzyMath dataset.
  2. A bge-base embedding model, fine-tuned with contrastive learning on 74,833 pairs, embeds the query and scans every theorem vector for the 100 closest.
  3. A cross-encoder reranker, fine-tuned to notice one-symbol differences, reads the query next to each of the top 20 and re-sorts them.
  4. A Gradio app serves both stages, with a switch to compare against the first stage alone.