lean-dojo/LeanCopilot
LLMs as Copilots for Theorem Proving in Lean
Lean Copilot – LLM‑powered proof assistance for Lean 4
What it is
- A library that lets you call large language models (LLMs) directly from Lean 4 code to automate theorem‑proving tasks.
- Provides three main tactics:
suggest_tactics– generates a list of possible Lean tactics for the current goal.search_proof– combines LLM‑generated tactics with theaesopprover to search for a complete proof.select_premises– retrieves likely useful lemmas from a frozen snapshot of Lean 4 and Mathlib 4.
- Supports built‑in models hosted on Hugging Face (via CTranslate2) and any user‑provided model (local binary or remote server).
Why it matters
- Bridges symbolic theorem proving with modern neural language models, enabling faster proof development and experimentation.
- Works natively inside the Lean ecosystem, so you can keep the whole workflow in one place – no external scripts needed.
- Open‑source and accompanied by a peer‑reviewed paper (accepted to NeuS 2025).
Key components
| Component | Role |
|---|---|
| NativeGenerator / NativeEncoder | Runs small transformer models locally via CTranslate2 (C++ library linked through Lean’s FFI). |
| ExternalGenerator / ExternalEncoder | Calls a model served elsewhere (e.g., a Python HTTP server) using a simple JSON API defined in external_model_api.yaml. |
| GenericGenerator / GenericEncoder | Lets you plug any Lean type that implements the generate / encode type‑class methods. |
| Lake integration | Provides a moreLinkArgs snippet and a require LeanCopilot entry so downstream packages can depend on it just like any other Lean library. |
| Dockerfile / build scripts | Ready‑made environment for Linux/macOS/WSL users who run into native‑dependency issues. |
Getting started (quick steps)
- Add the dependency to your
lakefile.leanorlakefile.tomlas shown in the README. - Run
lake update LeanCopilotand thenlake exe LeanCopilot/downloadto fetch the pre‑trained models (≈ 200 MiB each). - Build the native code with
lake build(orlake build LeanCopilotif you haven’t imported it yet). - In a Lean file,
import LeanCopilotand try:
Click a suggested tactic to insert it, or runexample (n m : Nat) : n + m = m + n := by suggest_tacticssearch_proofto let the system try a multi‑step proof automatically.
Advanced usage
- Adjust generation hyper‑parameters (temperature, beam size, number of returned tactics) via the
suggest_tacticsoptions documented inLeanCopilotTests/TacticSuggestion.lean. - Swap the default model for a larger one by editing the
ModelAPIs.leanexamples and pointingExternalGeneratorat your own server. - Skip building CTranslate2/OpenBLAS from source by supplying system library paths with Lake’s
-KsystemOpenblasand-KsystemCtranslate2Libflags.
Caveats to be aware of
select_premisesreturns the original theorem name from the snapshot; some derived lemmas may appear under a different name.search_proofcan occasionally produce a proof that fails termination checks; renaming the theorem temporarily works around this.- On Linux, executables (
lean_exe) that depend on Lean Copilot need an extra linker flag to pull in the systemlibstdc++because Lean’s own binary links againstlibc++.
Community & support
- Discussions: https://github.com/lean-dojo/LeanCopilot/discussions
- Issues: open a GitHub issue with OS details, steps to reproduce, and debug logs.
- Contributions: pull requests are welcome; the project follows the usual Lean community conventions.
Citation If you use Lean Copilot in research, cite the accompanying paper:
@article{song2024leancopilot,
title={Lean copilot: Large language models as copilots for theorem proving in lean},
author={Song, Peiyang and Yang, Kaiyu and Anandkumar, Anima},
journal={arXiv preprint arXiv:2404.12534},
year={2024}
}
Lean Copilot makes LLMs first‑class citizens in the Lean theorem‑proving world, offering a practical bridge between neural language models and formal mathematics.
Related
- Project
- Project
- Project
- Project
- Project