DeepSeek-Prover-V1.5: Lean 4 Theorem Proving Guide

The Problem: The Hallucination & Verification Ceiling of Informal Math LLMs
In modern enterprise automation, automated algorithmic verification, and autonomous agent workflows like AiFetchly, deploying generative Large Language Models (LLMs) for complex mathematical deduction and formal logic has revealed a fundamental limitation: the unreliability of informal Chain-of-Thought (CoT).
When frontier language models tackle Olympiad-level mathematics, financial derivatives pricing, or software compiler correctness, they generate prose in natural language interspersed with LaTeX formulas.
However, in rigorous deductive disciplines, natural language reasoning suffers from three fatal architectural flaws:
The Informal Mathematics Failure Cascade:
User Problem: Prove that for all positive integers n, f(n) <= 2^n
│
▼
┌────────────────────────────────────────────────────────┐
│ Informal Natural Language Chain-of-Thought (GPT-4o) │
│ Step 1: Base case n = 1 holds trivially... │
│ Step 2: Assume induction hypothesis holds for k... │
│ Step 3: "By algebraic simplification, we clearly see" │
│ ❌ Hallucinates false intermediate lemma! │
│ Step 4: Concludes "Q.E.D." with high confidence! │
└────────────────────────────────────────────────────────┘
│
▼
Result: Authoritative, convincing prose that contains a subtle, fatal mathematical error! 1. The Subtlety of “Almost Correct” Informal Proofs
In literature or conversational chat, an 85% accurate response is helpful. In formal mathematics and safety-critical software verification, an 85% accurate proof is 0% valid:
- Natural language LLMs frequently “hand-wave” over non-trivial boundary cases (e.g., dividing by zero, assuming uniform continuity without proof, or misapplying parity arguments).
- Because language models are trained via statistical next-token prediction, they mimic the rhetorical structure of a mathematician’s prose without executing a deterministic validity check.
- A human engineer or auditor must meticulously check every single algebraic line to spot hallucinations, defeating the purpose of autonomous automation.
2. Reward Hacking in Neural Verifiers and Process Reward Models
To reduce mathematical hallucinations, research labs introduced Process-Supervised Reward Models (PRMs):
- PRMs are secondary neural networks trained to assign a score to each reasoning step.
- However, neural reward models are themselves prone to reward hacking and perceptual blindness: the generator LLM learns verbose, sycophantic phrasing that scores high on the PRM while still harboring logical fallacies.
- Neural verification lacks a definitive ground truth.
3. The Enormous Search Space of Formal Proof Tactics
To guarantee mathematical truth, computer scientists use Interactive Theorem Provers (ITPs) like Lean 4, Isabelle, and Coq:
- In Lean 4, a proof is written as a sequence of typed programmatic commands called tactics (
induction,rw,apply,simp,linarith). - The Lean 4 compiler kernel acts as an infallible, deterministic type-checker. If a proof compiles, it is mathematically 100% sound.
- However, navigating the formal tactic search space is an exponential search problem: at any given step, hundreds of tactics and thousands of mathematical theorems from
Mathlibcould apply, easily trapping standard search algorithms in combinatorial deadlocks.
Engineering teams required a foundation model trained to interact directly with the Lean 4 proof assistant, using compiler feedback as the reward signal to explore proof trees.
The Solution: DeepSeek-Prover-V1.5 System Architecture
Developed by the DeepSeek AI research team, DeepSeek-Prover-V1.5 is an open-source formal theorem-proving foundation model specialized in Lean 4.
Built upon DeepSeekMath-Base (7B parameters), DeepSeek-Prover-V1.5 bridges the gap between neural generative models and symbolic theorem provers through three revolutionary innovations:
- Formal Lean 4 Pre-training and Tactic Modeling: Native understanding of Lean 4 syntax, dependent type theory, and the extensive
Mathlibmathematical library. - Reinforcement Learning from Proof Assistant Feedback (RLPAF): Aligning model policy using binary, deterministic compiler feedback from the Lean 4 kernel rather than fallible human or neural reward models.
- Reward-Maximized Monte-Carlo Tree Search (RMaxTS): An advanced proof tree search algorithm that balances step confidence with intrinsic exploration rewards to discover non-trivial deductive pathways.
The DeepSeek-Prover-V1.5 Interactive Proving Loop:
Formal Theorem Statement (Lean 4 Code)
│
▼
┌────────────────────────────────────────────────────────┐
│ RMaxTS Proof Tree Search Engine │
│ • Maintains tree of proof states & subgoals │
│ • Intrinsic reward balances exploration & exploitation │
└────────────────────────────────────────────────────────┘
│ ▲
│ (Candidate Tactic Proposals) │ (Tactic State / Error Diagnostics)
▼ │
┌────────────────────────┐ ┌───────────────────────────┐
│ DeepSeek-Prover-V1.5 │ │ Lean 4 Verification REPL │
│ Policy Network (7B) │──►│ • Deterministic Kernel │
│ • RLPAF-aligned model │ │ • Typechecks tactic step │
│ • Emits tactic actions │ │ • Generates new subgoals │
└────────────────────────┘ └───────────────────────────┘
│
▼
100% Formally Verified Proof (Compiles with zero warnings / Q.E.D.) The Three Core Pillars of DeepSeek-Prover-V1.5
1. DeepSeekMath-Base & Formal Lean 4 Tokenization
DeepSeek-Prover-V1.5 initializes from DeepSeekMath-Base:
- Pre-trained on 120 billion math-related tokens, including arXiv preprints, Common Crawl math pages, and formal repositories.
- Fine-tuned on a curated dataset of over 8 million formal proof steps and tactic-state pairs extracted from the Lean 4
Mathlibecosystem.
2. Reinforcement Learning from Proof Assistant Feedback (RLPAF)
The defining algorithmic breakthrough of DeepSeek-Prover is RLPAF:
- Unlike RLHF (which uses subjective human preferences) or RLAIF (which uses another fallible LLM), RLPAF utilizes the Lean 4 compiler executable as the reward oracle.
- If a proposed tactic triggers a compiler syntax error, reward is penalizing ($R = -1$).
- If a tactic compiles and reduces the remaining sub-goals, reward is positive ($R = +0.5$).
- If the tactic closes all open goals and the theorem compiles successfully (
Q.E.D.), maximum reward is granted ($R = +1.0$). - This objective reward signal served as the direct conceptual predecessor to the rule-based reinforcement learning (GRPO) utilized in DeepSeek-R1!
3. RMaxTS: Reward-Maximized Monte-Carlo Tree Search
Rather than relying on naive single-pass greedy decoding:
- Prover-V1.5 executes RMaxTS, a customized Monte-Carlo Tree Search variant.
- It models theorem proving as a Markov Decision Process (MDP): states are Lean 4 tactic goals, actions are tactic commands, and transitions are evaluated by the Lean 4 REPL.
- To prevent getting trapped in repetitive tactic loops (e.g., repeatedly trying
simp), RMaxTS adds an intrinsic exploration bonus based on node novelty, forcing the search tree into diverse mathematical branches.
Deep Dive: Lean 4 Interactive Theorem Proving & Search Dynamics
Understanding how DeepSeek-Prover-V1.5 proves theorems requires examining its tactic-state transitions and tree expansion.
Lean 4 Tactic-State Transition in DeepSeek-Prover:
Initial Goal:
n : ℕ
⊢ 2 * (∑ i in range n, (i + 1)) = n * (n + 1)
Step 1: Tactic Generated -> `induction n with | zero => ... | succ n ih => ...`
Lean 4 Kernel Check: PASS
Subgoals Created:
Subgoal 1 (Base Case): ⊢ 2 * (∑ i in range 0, (i + 1)) = 0 * (0 + 1)
Subgoal 2 (Inductive): ih : 2 * ... ⊢ 2 * (∑ i in range (n + 1), ...) = ...
Step 2: Subgoal 1 Tactic -> `simp`
Lean 4 Kernel Check: PASS (Subgoal 1 Closed!)
Step 3: Subgoal 2 Tactic -> `rw [sum_range_succ, mul_add, ih]; ring`
Lean 4 Kernel Check: PASS (Subgoal 2 Closed! No goals remaining!)
Outcome: Theorem Formally Verified! 1. Tactic-State Transition Dynamics in Lean 4
In Lean 4, proving is interactive:
- The environment provides the tactic state: the active hypotheses (local context) and the current target goal (
⊢ P). - The model outputs a candidate tactic (e.g.,
omega,apply Nat.dvd_trans,ring). - The Lean 4 REPL executes the tactic:
- If invalid, the REPL returns the exact compiler diagnostic error.
- If valid, the goal is either solved or transformed into one or more smaller subgoals.
- DeepSeek-Prover-V1.5 conditions each tactic generation step on both the original theorem and the exact current state emitted by the Lean 4 kernel.
2. Intrinsic-Reward Exploration in RMaxTS
In standard Monte-Carlo Tree Search (like AlphaGo), the value of a state is backpropagated up the tree. However, in formal mathematics, most search branches hit dead ends ($R = 0$).
To prevent the search from stalling, RMaxTS introduces an exploration bonus:
$$U(s, a) = c_{\text{puct}} \cdot P(s, a) \cdot \frac{\sqrt{\sum_b N(s, b)}}{1 + N(s, a)} + \mathcal{R}_{\text{intrinsic}}(s, a)$$
Where $\mathcal{R}_{\text{intrinsic}}$ rewards tactic paths that discover structurally novel proof states or discharge difficult subgoals, enabling DeepSeek-Prover-V1.5 to solve competition problems that require 20+ deductive steps.
Benchmark & Capability Comparison: Formal vs. Informal Reasoning
The table below contrasts DeepSeek-Prover-V1.5 against informal frontier models and earlier formal provers:
| Dimension / Metric | GPT-4o (Informal CoT) | Claude 3.5 Sonnet (Informal) | DeepSeek-Prover-V1 (Baseline) | DeepSeek-Prover-V1.5 (RLPAF + RMaxTS) |
|---|---|---|---|---|
| Reasoning Modality | Natural Language / LaTeX | Natural Language / LaTeX | Formal Lean 4 Whole-Proof | Formal Lean 4 Interactive Tactic State |
| Verification Method | None (Probabilistic text) | None (Probabilistic text) | Lean 4 compiler check | Lean 4 Deterministic Kernel |
| Hallucination Rate | Moderate to High on math | Low to Moderate on math | 0% (Checked by compiler) | 0% (100% Deterministic Guarantee) |
| Training Paradigm | RLHF / Neural PRM | RLHF / Constitutional AI | SFT on Mathlib | RLPAF (Compiler-in-the-loop RL) |
| Inference Search | Single-pass greedy / beam | Single-pass greedy / beam | Pass@32 sampling | RMaxTS (Monte-Carlo Tree Search) |
| miniF2F-test Accuracy | N/A (Informal) | N/A (Informal) | 46.3% | 63.5% (New Open SOTA) |
| ProofNet Accuracy | N/A (Informal) | N/A (Informal) | 16.8% | 25.3% (Undergraduate Level SOTA) |
Practical Implementation: Running Formal Theorem Proving in Lean 4
Let us implement a Python pipeline interfacing with the Lean 4 REPL and DeepSeek-Prover-V1.5 to automatically prove an algebraic theorem.
Step 1: Install Lean 4 and Dependencies
First, ensure elan (the Lean version manager) and lake are installed:
# Install elan (Lean 4 toolchain manager)
curl https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh -sSf | sh -s -- -y
source $HOME/.elan/env
# Install Python Lean 4 REPL client and transformers
pip install torch transformers pexpect Step 2: Implement the Proof Search Pipeline with DeepSeek-Prover
Save the following script as prove_theorem.py:
import torch
import time
from transformers import AutoModelForCausalLM, AutoTokenizer
class Lean4ProverAgent:
def __init__(self, model_id: str = "deepseek-ai/DeepSeek-Prover-V1.5-RL"):
"""
Initializes DeepSeek-Prover-V1.5 with bfloat16 precision.
"""
print(f"Loading {model_id}...")
start = time.perf_counter()
self.tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
self.model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.bfloat16,
device_map="auto",
trust_remote_code=True
).eval()
print(f"Prover loaded in {(time.perf_counter() - start):.2f}s.")
def generate_proof_candidates(self, formal_statement: str, num_candidates: int = 5) -> list[str]:
"""
Generates candidate formal proofs conditioned on the Lean 4 theorem declaration.
"""
prompt = f"Complete the following formal proof in Lean 4:\n\n{formal_statement}\nProof:\n"
inputs = self.tokenizer(prompt, return_tensors="pt").to(self.model.device)
start = time.perf_counter()
with torch.no_grad():
outputs = self.model.generate(
**inputs,
max_new_tokens=512,
num_return_sequences=num_candidates,
do_sample=True,
temperature=0.7,
top_p=0.95,
pad_token_id=self.tokenizer.eos_token_id
)
elapsed = time.perf_counter() - start
print(f"Generated {num_candidates} candidate proof branches in {elapsed:.2f}s.")
proofs = []
for out in outputs:
generated_text = self.tokenizer.decode(out[inputs.input_ids.shape[1]:], skip_special_tokens=True)
proofs.append(generated_text)
return proofs
if __name__ == "__main__":
prover = Lean4ProverAgent()
# Sample elementary theorem in Lean 4
lean_theorem = """
import Mathlib.Data.Nat.Basic
theorem sum_squares_identity (a b : ℕ) : (a + b)^2 = a^2 + 2 * a * b + b^2 := by
"""
print(f"\nTarget Theorem:\n{lean_theorem}")
candidates = prover.generate_proof_candidates(lean_theorem, num_candidates=3)
print("\n--- Proposed Candidate Tactic Sequences ---")
for i, proof in enumerate(candidates):
print(f"\n[Candidate {i+1}]:\n{proof.strip()}") Step 3: Verifying Proof Steps with the Lean 4 REPL
When candidate sequences are piped to the Lean 4 verification daemon:
- The daemon executes each tactic line by line.
- When candidate
ringorlinarithis verified, the kernel confirmsdeclaration checked with 0 errors. - The theorem is certified, providing mathematical certainty without human oversight.
Production Best Practices: Integrating Formal Verification into AI Agents
When deploying formal verification techniques alongside autonomous scraping and data extraction in AiFetchly, adopting the following best practices ensures system integrity:
1. Provably Correct Data Schemas and Transformations in aiFetchly
When enterprise data pipelines extract mission-critical financial figures or medical records from the web:
- Model business invariants (such as balance-sheet balance equations: $\text{Assets} = \text{Liabilities} + \text{Equity}$) as formal constraints.
- Use lightweight formal verifiers to validate extracted SQL data transformations mathematically before committing them to production data warehouses.
2. Smart Contract & Database Invariant Verification
Autonomous agent actions involving financial transactions, automated purchases, or smart contract executions must have zero probability of hallucination:
- Compile execution rules into formal specifications.
- Require formal proof certificates before authorizing high-value autonomous agent transactions.
3. Proof Cache Pruning and Parallel MCTS Worker Pools
In high-throughput environments:
- Cache proven sub-lemmas in local Redis/SQLite stores. When new proofs require identical lemmas, reuse the compiled Lean 4 proof term directly, avoiding redundant MCTS exploration passes.