A research system for translating mathematical statements from natural language and LaTeX into verified Lean 4 code, with Z3-powered canonicalization and CEGIS-based iterative learning.
This project implements a complete pipeline for:
- Extracting theorems from arXiv papers and LaTeX documents
- Parsing mathematical statements into a validated intermediate representation (IR)
- Canonicalizing expressions to recognize equivalent formulations (e.g.,
x+yβ‘y+x) - Translating to Lean 4 with type checking and proof obligations
- Learning from failures via CEGIS (Counter-Example Guided Inductive Synthesis)
- Verifying complete chapters of mathematical foundations in Lean 4
- LaTeX β IR: Z3 constraint solving for parsing complex mathematical notation
- IR β Lean: Z3-guided template selection and code synthesis
- NOT for theorem proving: Z3 validates structure, not mathematical correctness
- Recognizes equivalent expressions using Z3 UNSAT checks
- Rules: Commutativity, associativity, De Morgan, double negation, implication, distributivity
- Benefits: 30-50% deduplication, caching by canonical form, cross-paper pattern matching
- Tests: 20/20 passing (100%) including 6 canonicalization proofs
- Downloads random papers from arXiv
- Extracts all theorems/definitions/axioms
- Handles real-world LaTeX variations (5+ theorem styles)
- Progressive vocabulary learning via
definitions.json - Zero-regression testing on previous papers
- Counter-example guided refinement
- Learns translation rules from failures
- Maintains training examples in
cegis_results/ - Iterative improvement until convergence
- Auto-generates 30+ chapter mathematical textbook
- Every theorem proven in Lean 4 (no
sorry) - Automatic structure augmentation when definitions are insufficient
- Axiom minimization with immediate reproving
- Benchmarks generated from proven theorems
arXiv Paper β LaTeX Extraction β Statement Parser β Semantic Analyzer
β
ValidatedIRExpr (Z3)
β
Canonicalization Engine (Z3 UNSAT)
β
Vocabulary Lookup (definitions.json)
β
IR-to-Lean Translation (Z3 templates)
β
Lean Type Checking + Verification
β
CEGIS Learning (if failure)
β
Regression Testing
# Clone the repository
git clone https://github.com/thehalleyyoung/cav-nlp.git
cd cav-nlp
# Create virtual environment (Python 3.11+)
python3.11 -m venv .venv
source .venv/bin/activate
# Install dependencies
pip install z3-solver arxiv pyparsing
# Install Lean 4 and lake (for Lean verification)
curl https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh -sSf | sh# Basic Z3 validation tests (7/7 tests)
python test_z3_validated_ir_hard.py
# Extreme test suite with canonicalization (20/20 tests)
python test_z3_extreme.py
# See Level 11 canonicalization results
python test_z3_extreme.py 2>&1 | grep -A 200 "LEVEL 11"# Use the arXiv-to-Lean agent prompt
# See: .github/prompts/arxiv_to_lean_agent.prompt.md
# Or run the paper harvester directly
python arxiv_paper_harvester.py# Learn from cached papers
python run_cegis_on_papers.py --cache-only --max-papers 50 --max-iterations 50
# Download and learn from new papers
python run_cegis_on_papers.py --max-papers 100 --max-iterations 100# Generate foundations for a topic (e.g., "protocol" or "semiosis")
python run_lean_verified_foundations.py protocol PROPOSAL.md
# This will:
# - Generate 30 chapters of LaTeX (~1500 pages)
# - Prove all theorems in Lean 4 (no sorry)
# - Auto-augment structures when needed
# - Minimize axioms (immediate reproving)
# - Generate benchmarks from proven theoremsz3_validated_ir.py: Z3-powered intermediate representation with structure validationcanonicalization_engine.py: Z3 UNSAT-based expression canonicalizationrun_cegis_on_papers.py: CEGIS learning loop over arXiv corpusrun_lean_verified_foundations.py: Automated textbook generation with Lean verificationarxiv_paper_harvester.py: Paper download and theorem extraction
.github/prompts/arxiv_to_lean_agent.prompt.md: Complete agent for single-paper refinement
ACTIVE_SYSTEM.md: Current system architecture and design decisionsAXIOM_POLICY.md: Policy for axiom addition and minimizationCANONICALIZATION_README.md: Canonicalization system documentationSTRUCTURE_AUGMENTATION.md: Automatic structure augmentation guideZ3_CANONICALIZATION_SUMMARY.md: Z3 canonicalization test results (20/20)Z3_VALIDATED_IR_README.md: IR system design and validation strategyUSAGE_GUIDE.md: Detailed usage instructions
test_z3_extreme.py: 20/20 tests passing (100%)- Levels 1-6: Basic Z3 validation (7/7)
- Levels 7-10: Advanced features (7/7)
- Level 11: Canonicalization (6/6) β¨
test_z3_validated_ir_hard.py: Hard validation casestest_mini_cegis.py: CEGIS learning validation
foundations-protocol-lean/: Protocol theory foundations (Lean 4)foundations-semiosis-lean/: Semiosis foundations (Lean 4)
β
commutativity: Z3 proved x+y β‘ y+x
β
associativity: Z3 proved (x+y)+z β‘ x+(y+z)
β
de_morgan: Z3 proved Β¬(Pβ§Q) β‘ Β¬Pβ¨Β¬Q
β
double_negation: Z3 proved ¬¬P ①P
β
implication: Z3 proved PβQ β‘ Β¬Pβ¨Q
β
distributivity: Z3 proved x*(y+z) β‘ x*y+x*z
Level 11: 6/6 passed
Overall: 20/20 tests passed (100.0%)
- Training examples accumulated in
cegis_results/training_examples.json - Iterative refinement until convergence
- Zero regressions on previous papers
- Complete chapters with all theorems proven
- No
sorrystatements allowed - Automatic structure augmentation when needed
- Axiom minimization via immediate reproving
What Z3 IS used for:
- β LaTeX β IR: Structure extraction via string constraints
- β IR β Lean: Template selection and code synthesis
- β Canonicalization: Equivalence checking (UNSAT = equivalent)
- β Scope checking: Variable binding validation
- β Type consistency: Sort checking across expressions
What Z3 is NOT used for:
- β Mathematical theorem proving (Lean does this)
- β Verifying mathematical correctness
- β Proving theorems are true
- Deduplication:
x+y,y+x,x + yβ same canonical form (30-50% reduction) - Caching: Store translations by canonical form, not surface syntax
- Pattern Matching: Match modulo equivalence
- Cross-Paper Learning: Recognize equivalent formulations from different papers
When theorems fail due to insufficient structure definitions:
- System analyzes what's missing
- Augments structures with needed fields/instances
- Identifies affected axioms
- Attempts to reprove axioms as theorems
- Retries original theorem with enhanced structures
This enables organic growth where foundations evolve naturally from theorem requirements.
This project builds on research in:
- Formal mathematics (Lean, Mathlib)
- SMT solving (Z3)
- Natural language semantics (Ganesalingam, Grosof)
- Program synthesis (CEGIS)
- Mathematical controlled English (Naproche, Mizar)
See individual files for detailed bibliographies and citations.
This is an active research project. Key areas for contribution:
- Additional canonicalization rules
- More robust LaTeX parsing
- Extended vocabulary coverage
- Integration with other proof assistants
- Performance optimizations
MIT License - See LICENSE file for details
Halley Young
- Lean 4 and Mathlib community
- Z3 SMT solver (Microsoft Research)
- arXiv for open access to mathematical papers
Status: Active development | Tests: 20/20 passing (100%) | Lean Verification: Complete chapters with zero sorry