Skip to content

Latest commit

 

History

History
165 lines (123 loc) · 6.4 KB

File metadata and controls

165 lines (123 loc) · 6.4 KB

LeanGenerator: Automated Lean Mathematical Library Generation

A Practical Framework for Auto-Generating Missing Mathlib Libraries

1. Core Concept

LeanGenerator is an automated formal mathematical library generation system for Lean 4 and mathlib. It leverages LLMs and a self-synthesizing code harness to automate the full pipeline—definition → lemma → theorem → proof → compilation → packaging—for domains not yet covered in mathlib.

Inspired by AutoHarness, the core innovation is treating the proof generation logic as learnable harness code that iteratively refines itself through Lean compiler feedback, rather than relying on static prompting or manual engineering. The output is a directly importable, verifiable, mathlib-compliant formal library.


2. Foundations: Lean's Native Automation Support

Lean is intentionally designed for automated reasoning, proof search, and code synthesis.

2.1 Built-in Automated Proof Tools

  • by tidy: Automated proof simplification and cleanup
  • by simp: Powerful term rewriting and simplification
  • aesop: Automated proof search with extensible tactics
  • hint: Suggests relevant proof tactics

2.2 Mature AI Code Generation

  • Official Lean 4 models and LeanCopilot for real-time code completion
  • Modern LLMs (GPT-4o, DeepSeek, etc.) generate syntactically correct Lean code
  • Formal feedback loops enable self-correction

2.3 Verifiable Closed Loop

  • Strong static typing and compiler-enforced correctness
  • Proof search, repair, and validation are fully automatable
  • Lean compiler error messages drive iterative harness refinement

3. Harness-Driven Library Construction Workflow

3.1 User Input

Specify a target mathematical domain missing from mathlib.

Example: Tate pairing on elliptic curves over finite fields

3.2 Knowledge Acquisition

  • Retrieve papers, textbooks, and formal definitions
  • Extract axioms, definitions, theorems, and properties

3.3 Generate Lean Definitions

structure TatePairing (F : Type) [Field F] (E : EllipticCurve F) where
  pairing : E → E → F
  bilinear : ∀ P Q R, pairing (P + Q) R = pairing P R * pairing Q R
  non_degenerate : ∀ P, (∀ Q, pairing P Q = 1) → P = 0

3.4 Generate Lemma / Theorem Skeletons

lemma TatePairing.symmetric {F E} [Field F] [EllipticCurve E]
  (t : TatePairing F E) :
  ∀ P Q, t.pairing P Q = t.pairing Q P :=
by sorry

3.5 Harness Synthesis & Iterative Proof Search

This is the core engine of LeanGenerator, directly adapted from the AutoHarness methodology.

Harness Variants

Three harness types mediate between the LLM and the Lean compiler:

Variant Description
Proof-Filter Generates a set of candidate tactic sequences; LLM ranks by plausibility
Proof-Verifier LLM proposes a proof; harness validates via compiler, rejects with error context
Tactic-Policy Pure Python/Lean script selects tactics without runtime LLM calls (lowest cost)

Core harness function signatures:

def propose_proof(goal: str) -> str:
    """Generate a candidate tactic sequence for the given proof goal."""

def is_valid_proof(goal: str, proof: str) -> bool:
    """Validate proof by invoking the Lean compiler; return True if no errors."""

Harness Refinement Loop (AutoHarness-style)

Initialize tree of harness code hypotheses
    ↓
Thompson Sampling → select node to refine
    ↓
Execute harness across 10 parallel Lean environments (up to 1000 steps)
    ↓
Collect up to 5 failed proof attempts with compiler error messages
    ↓
Feed errors + original harness code → LLM for mutation/refinement
    ↓
Update tree; repeat until compilation success rate = 100% or timeout

Termination criterion: compilation success rate reaches 100% across sampled theorems, or iteration budget exhausted.

Convergence: empirically, most proof domains converge within ~15 refinement iterations.

3.6 Compilation and Self-Repair

  • Real-time type and syntax checking via Lean compiler
  • Harness automatically repairs imports, type errors, and proof failures
  • Iterative refinement until error-free; compiler errors are first-class feedback signals

3.7 Library Engineering & Packaging

  • Auto-generate project directory structure
  • Create lakefile.toml
  • Manage import chains, documentation, and test files
  • Ensure mathlib-compliant style and naming conventions

3.8 Final Output

A buildable, importable Lean package ready for use or PR into mathlib.


4. Available Supporting Tools

Category Tools
Automated proving aesop, simp, tidy
Code completion LeanCopilot
Paper formalization Formalize24, Lean Search
Harness execution Parallel Lean 4 environments
Project scaffolding Lake, lakefile.toml generator
Environment Lean 4, mathlib, Lake

5. Capability Boundaries

Achievable Today

  • End-to-end generation of complete Lean libraries for missing domains
  • Automatic creation of structures, typeclasses, definitions, lemmas, theorems
  • Automated proof of low-to-medium difficulty theorems via harness refinement
  • Full compilation, self-repair, documentation, and packaging
  • Strict adherence to mathlib style and standards
  • Smaller LLMs outperforming larger ones via learned harness (cost efficiency)

Not Yet Feasible

  • Original mathematical research or open problem solving
  • Fully unsupervised understanding of ambiguous natural language
  • Fully automated proofs of deep mathematical theorems

6. Innovation & Value

  • Harness as learnable code: The proof generation logic itself is refined through compiler feedback—not just the proofs
  • Gradient-free optimization: LLM acts as a mutation operator; no fine-tuning required
  • Paradigm shift: From single-theorem automation to library-level formal proof engineering
  • Fill mathlib gaps: Rapidly generate folklore lemmas, niche branches, and connecting tissue
  • Low cost: Small LLMs + learned harness can outperform large models; drastically faster than manual development
  • Verifiable & reusable: Output is compile-ready, provable, and integrable

7. Use Cases

  • Formal mathematics research: Rapidly prototype missing foundational libraries
  • Education & tooling: Generate domain-specific mathematical modules
  • AI + formal methods: Validate automated formalization pipelines
  • Mathlib community: Batch-fill gaps in underrepresented domains