Abstract
We present an in-context learning agent for formal theorem-proving in environments like Lean and Coq. Current state-of-the-art models for the problem are finetuned on environment-specific proof data. By contrast, our approach, called COPRA, repeatedly asks a high-capacity, general-purpose large language model (GPT-4) to propose tactic applications from within a stateful backtracking search. Proposed tactics are executed in the underlying proof environment. Feedback from the execution is used to build the prompt for the next model query, along with selected information from the search history and lemmas retrieved from an external database. We evaluate our implementation of COPRA on the miniF2F benchmark for Lean and a set of Coq tasks from the CompCert project. On these benchmarks, COPRA significantly outperforms few-shot invocations of GPT-4. It also compares favorably against finetuning-based approaches, outperforming ReProver, a state-of-the-art finetuned approach for Lean, in terms of the pass@1 metric.
Introduction
Formal theorem-proving involves writing proofs in languages like Lean or Coq that can be mechanically verified. This is a "grand challenge" for AI because it requires perfect logical precision over many steps. Traditional state-of-the-art AI provers typically rely on fine-tuning smaller language models on massive datasets of human-written proofs. However, this approach is computationally expensive and often struggles with "out-of-distribution" problems.
COPRA (In-context Prover Agent) offers a different path by treating theorem-proving as a stateful search problem guided by a general-purpose LLM (GPT-4). By leveraging environment feedback and retrieval-augmented generation (RAG) instead of domain-specific fine-tuning, COPRA achieves state-of-the-art performance on competitive benchmarks.
Methodology
Instead of generating a whole proof at once, COPRA works step-by-step through an iterative loop:
- Prompt Synthesis: It builds a prompt containing the current goal, hypotheses, the history of previous steps, and relevant lemmas retrieved from an external database using BM25 search.
- Tactic Selection: GPT-4 proposes a "tactic" (a command to transform the proof state).
- Execution & Feedback: The tactic is executed in the actual proof environment (Lean or Coq). If it fails, the error message is captured.
- Stateful Backtracking: If a tactic fails or leads to a dead end, COPRA records the failure in a "failure dictionary" and backtracks to a previous state to try a different path.
A key innovation is the prompt serialization protocol, which formats the complex state of a proof search—including goals, stacks, and error messages—into a structured text format the LLM can reason about effectively.
Experimental Results
COPRA was evaluated on two distinct environments: miniF2F (Lean), consisting of high-school competition math problems, and CompCert (Coq), involving software verification theorems.
COPRA set a new state-of-the-art for open-source provers on the miniF2F-test benchmark. As shown in the table below, COPRA (with retrieval and informal hints) proved 30.74% of theorems, outperforming the state-of-the-art fine-tuned model, ReProver (25.00%).
Key Insights
- Feedback over Fine-tuning: COPRA's ability to "read" error messages and adjust its strategy allows it to outperform models specifically trained on Lean data.
- Search Efficiency: While GPT-4 is slower per query, COPRA finds proofs faster in terms of total wall-clock time because its search is much more directed, requiring 16x fewer queries than the baseline ReProver.
- The Importance of Scale: Ablation studies showed that smaller models like GPT-3.5 or CodeLlama performed significantly worse, suggesting that the high-level reasoning required for stateful search is an emergent property of very large models.
Conclusion
COPRA demonstrates that high-capacity models can master formal logic through sophisticated in-context learning. Because it does not rely on fine-tuning, the same architecture works for both Lean and Coq with minimal changes. Future work includes using COPRA to synthetically generate training data for smaller models, potentially creating a "distilled" version that is both cost-effective and highly capable.
Summary updated