Leo3: Rust Bindings for Lean4
Leo3 provides safe, ergonomic Rust bindings to the Lean4 theorem prover, inspired by PyO3's architecture for Python-Rust bindings.
Project Status
⚠️ Work in Progress - Leo3 is in early development. The core architecture is established, but many features are not yet implemented.
Architecture
Leo3 is organized as a workspace with multiple crates:
leo3/
├── leo3 # Main crate - high-level safe abstractions
├── leo3-ffi # Raw FFI bindings to Lean4's C API
├── leo3-build-config # Build-time Lean4 detection and configuration
├── leo3-macros # Procedural macro entry points
└── leo3-macros-backend # Procedural macro implementations
Design Philosophy
Leo3 follows PyO3's proven architecture patterns:
- Layered Abstraction: Clear separation between raw FFI, safe wrappers, and user-facing API
- Zero-Cost:
#[repr(transparent)]wrappers with compile-time safety - Lifetime Safety: The
Lean<'l>token ensures Lean runtime initialization at compile-time - Automatic Memory Management: Smart pointers handle reference counting
- Macro-Driven Ergonomics: Procedural macros reduce boilerplate
Core Concepts
The Lean<'l> Token
All Lean operations require a Lean<'l> token, proving the runtime is initialized:
use *;
Smart Pointers
Leo3 provides two primary smart pointer types:
LeanBound<'l, T>: Tied toLean<'l>lifetime, used for most operationsLeanRef<T>: Can cross initialization boundaries, useful for storage
Both handle reference counting automatically.
Type System
Lean types are wrapped in safe Rust types:
// Strings
let s = new?;
let rust_str = to_str?;
// Natural numbers
let n = from_usize?;
let value = to_usize?;
// Arrays
let arr = new?;
let size = size;
Installation
Note: Lean4 must be installed on your system. Leo3 will attempt to detect it via:
LEAN_HOMEorELAN_HOMEenvironment variableslakecommandelantoolchain managerleanin PATH
Add to your Cargo.toml:
[]
= "0.1.0"
Example Usage
use *;
Feature Flags
macros(default): Enable procedural macros (#[leanfn],#[leanclass], etc.)
Comparison with PyO3
Leo3 adapts PyO3's architecture for Lean4:
| PyO3 Concept | Leo3 Equivalent | Notes |
|---|---|---|
Python<'py> |
Lean<'l> |
Runtime initialization token |
Bound<'py, T> |
LeanBound<'l, T> |
Lifetime-bound smart pointer |
Py<T> |
LeanRef<T> |
Unbound smart pointer |
#[pyfunction] |
#[leanfn] |
Function export macro |
#[pyclass] |
#[leanclass] |
Class export macro (planned) |
PyObject |
lean_object |
Base object type |
| GIL | Lean runtime | No equivalent (Lean has no GIL) |
Key Differences from PyO3
- No GIL: Lean4 doesn't have a Global Interpreter Lock, simplifying thread safety
- Different Memory Model: Lean uses reference counting without generational GC
- Theorem Proving Focus: Leo3 will support proof objects and tactics
- Dependent Types: Future support for Lean's dependent type system
Roadmap
Completed ✅
- Project structure and workspace setup
- FFI bindings layer (
leo3-ffi) - Build configuration and Lean4 detection
- Core abstractions (
Lean<'l>, smart pointers) - Basic type wrappers (String, Nat, Array)
- Proc macro infrastructure
- Calling Lean4 functions from Rust 🎉
- Dynamic library loading
- Module initialization
- Function calling with type conversion
- Working example (see
examples/call-lean/) - Complete documentation (CALLING_LEAN_FUNCTIONS.md)
In Progress 🚧
-
#[leanfn]macro implementation - Complete FFI bindings (more Lean API functions)
- String and Array parameter passing
- Error handling improvements
Planned 📋
- IO monad support
- Type conversions (Rust ↔ Lean)
-
#[leanclass]for Rust structs as Lean classes -
#[leanmodule]for module creation - Proof object support
- Tactic integration
- Environment and context management
- Async support
- Documentation generation
- Integration tests with actual Lean4
- CI/CD pipeline
- Example projects
Development
Building
# Build all crates
# Build specific crate
# Run tests
Testing
Currently, most tests are unit tests. Integration tests requiring a Lean4 installation will be added.
Contributing
Contributions are welcome! Areas that need work:
- Completing FFI bindings
- Implementing macro code generation
- Adding more type wrappers
- Documentation
- Examples
- Tests
Acknowledgments
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.