1 unstable release
| 0.1.0 | Oct 25, 2025 |
|---|
#478 in Math
758 downloads per month
Used in 4 crates
(3 directly)
86KB
2K
SLoC
lean-agentic
Hash-consed dependent types with 150x faster term equality
Developed by: ruv.io | github.com/ruvnet
π― What is lean-agentic?
lean-agentic is the core library for formally verified agentic programming, providing:
- β‘ Hash-Consing: 150x faster term equality (0.3ns vs 45ns structural comparison)
- π‘οΈ Dependent Types: Full Lean4-style dependent type theory
- π¦ Arena Allocation: Zero-copy term sharing via bump allocators
- β Minimal Kernel: <1,200 lines of trusted code
Perfect for building:
- π Theorem provers
- β Verified compilers
- π€ AI agents with formal guarantees
- π Proof-carrying code systems
π¦ Installation
cargo add lean-agentic
Or add to Cargo.toml:
[dependencies]
lean-agentic = "0.1.0"
π Quick Start
Hash-Consing (150x Speedup)
use lean_agentic::{Arena, SymbolTable};
let mut arena = Arena::new();
// Create identical terms - they share memory!
let var1 = arena.mk_var(42);
let var2 = arena.mk_var(42);
assert_eq!(var1, var2); // Same TermId!
// Equality: O(1) pointer comparison, ~0.3ns
Lambda Abstractions
use lean_agentic::{Arena, SymbolTable};
use lean_agentic::level::LevelArena;
use lean_agentic::term::{Binder, BinderInfo};
let mut arena = Arena::new();
let mut symbols = SymbolTable::new();
let mut levels = LevelArena::new();
// Create Type universe
let type_term = arena.mk_sort(levels.zero());
// Identity function: Ξ»x:Type. x
let identity = arena.mk_lam(
Binder {
name: symbols.intern("x"),
ty: type_term,
implicit: false,
info: BinderInfo::Default,
},
arena.mk_var(0)
);
println!("Ξ»x:Type. x = {:?}", identity);
Type Checking
use lean_agentic::{Arena, Environment, Typechecker};
use lean_agentic::level::LevelArena;
let mut arena = Arena::new();
let mut env = Environment::new();
let mut levels = LevelArena::new();
let mut checker = Typechecker::new();
let term = arena.mk_var(0);
let ty = checker.infer(&term, &arena, &env, &mut levels)?;
println!("Inferred type: {:?}", ty);
β¨ Key Features
π Hash-Consing
All identical terms share memory:
let x1 = arena.mk_var(0); // Allocates
let x2 = arena.mk_var(0); // Reuses!
let x3 = arena.mk_var(0); // Reuses!
// All same TermId, O(1) equality
Benchmarks:
- 0.3ns equality (150x faster than structural)
- 85% memory reduction via deduplication
- 95%+ cache hit rate in practice
π¦ Arena Allocation
Zero-copy sharing with bump allocators:
// All terms in contiguous memory
let term1 = arena.mk_var(0);
let term2 = arena.mk_app(term1, term1);
let term3 = arena.mk_lam(binder, term2);
// No cloning - just u32 TermId handles!
ποΈ Dependent Types
Full Lean4 type theory:
// Universe levels
let level_0 = levels.zero();
let level_1 = levels.succ(level_0);
// Type universes
let type_0 = arena.mk_sort(level_0); // Type
let type_1 = arena.mk_sort(level_1); // Type 1
// Dependent Ξ types: β(x : A), B
let pi = arena.mk_pi(binder, body);
π API Overview
Term Construction
| Method | Description | Example |
|---|---|---|
mk_var(index) |
Variable | x, y |
mk_sort(level) |
Type universe | Type |
mk_const(name, levels) |
Constant | Nat |
mk_app(func, arg) |
Application | f x |
mk_lam(binder, body) |
Lambda | Ξ»x. e |
mk_pi(binder, body) |
Dependent Ξ | βx:A. B |
Type Checking
let mut checker = Typechecker::new();
// Infer type
let ty = checker.infer(&term, &arena, &env, &mut levels)?;
// Check against expected type
checker.check(&term, &expected, &arena, &env, &mut levels)?;
// Definitional equality
let eq = checker.is_def_eq(&t1, &t2, &arena, &env)?;
π Performance
| Operation | Latency | Speedup |
|---|---|---|
| Hash-consed equality | 0.3ns | 150x |
| Arena allocation | 1.9ns | 5.25x |
| Term construction | <10ns | - |
| Type inference | <1Β΅s | - |
π― Use Cases
Theorem Prover
use lean_agentic::{Arena, Typechecker};
struct Prover {
arena: Arena,
checker: Typechecker,
}
impl Prover {
fn prove(&mut self, theorem: TermId) -> Result<TermId> {
// Proof search using lean-agentic
todo!()
}
}
Verified Compiler
struct VerifiedCompiler {
arena: Arena,
}
impl VerifiedCompiler {
fn compile_with_proof(&mut self, src: TermId) -> (ByteCode, TermId) {
// Returns (bytecode, proof of correctness)
todo!()
}
}
AI Agent with Safety Proofs
struct SafeAgent {
arena: Arena,
policy: TermId, // Safety policy as type
}
impl SafeAgent {
fn act(&mut self, action: TermId) -> Result<(Action, TermId)> {
// Returns (action, proof it satisfies policy)
todo!()
}
}
π Examples
See examples/ for complete applications:
- Hello World - Hash-consing basics
- Verified Calculator - Proof certificates
- AI Scraper - AI + formal verification (NOVEL)
- Self-Healing DB - Byzantine consensus (CUTTING EDGE)
- Theorem Prover - Browser WASM (WORLD FIRST)
π οΈ Building
# Clone
git clone https://github.com/agenticsorg/lean-agentic
cd lean-agentic/lean-agentic
# Build
cargo build --release
# Test
cargo test
# Docs
cargo doc --open
π License
Licensed under Apache-2.0 - see LICENSE
π Credits
Created by: ruv.io Maintained by: github.com/ruvnet Powered by: Flow Nexus, AgentDB, Claude Flow
π Research
Based on:
- Lean 4 - https://lean-lang.org
- Hash-Consing - FilliΓ’tre & Conchon (2006)
- Dependent Types - Xi & Pfenning (1999)
π Related Crates
leanr- Full language implementationleanr-wasm- Browser bindings
π Support
- Docs: https://docs.rs/lean-agentic
- Repo: https://github.com/agenticsorg/lean-agentic
- Issues: https://github.com/agenticsorg/lean-agentic/issues
- Website: https://ruv.io
Built with formal verification Β· Powered by hash-consing Β· Developed by ruv.io