lean-containers is a container library for Lean 4 that provides type-safe, mathematically rigorous implementations of container types and operations. Built on category-theoretic foundations, it offers polynomial functors, W-types, M-types, and advanced container operations for production use.
- Features
- Installation
- Quick Start
- Core Concepts
- Usage Examples
- Architecture
- Performance
- API Reference
- Contributing
- License
- Lean 4.8.0 or later
- Lake build system
- Docker (optional, for containerized usage)
- Make (optional, for convenience targets)
# Clone and set up in one command
git clone https://github.com/your-org/lean-containers.git
cd lean-containers
make dev-
Clone the repository:
git clone https://github.com/your-org/lean-containers.git
-
Navigate to the project directory:
cd lean-containers -
Build the project:
lake build
-
Run tests:
lean FinalProductionTest.lean
| Target | Description |
|---|---|
make dev |
Set up local development environment |
make build |
Build the project |
make test |
Run all tests |
make run |
Run the application/CLI locally |
make clean |
Clean build artifacts |
make release-dry |
Test release process (dry run) |
make release |
Build and publish artifacts |
make docker-build |
Build Docker image |
make docker-run |
Run Docker container |
make help |
Show all available targets |
Get started with lean-containers in under 10 minutes:
# Run the library immediately
docker run --rm ghcr.io/your-org/lean-containers:latest Main.lean
# Run tests
docker run --rm ghcr.io/your-org/lean-containers:latest FinalProductionTest.lean
# Interactive shell with the library
docker run -it --rm ghcr.io/your-org/lean-containers:latest /bin/bash# Add to your Lakefile.lean
require lean-containers from git "https://github.com/your-org/lean-containers.git"
# Then in your Lean file
import Containers# Clone and build
git clone https://github.com/your-org/lean-containers.git
cd lean-containers
# One-command setup and run
make dev && make run
# Or use individual commands
make build # Build the project
make test # Run all tests
make run # Run the applicationimport Containers
open Containers
-- Define a container signature
def ListSig : Container :=
{ shape := Option Unit, pos := fun _ => Unit }
-- Create a polynomial functor
def myPoly : Poly ListSig Nat :=
{ shape := some (), children := fun _ => 42 }
-- Apply functor mapping
def mappedPoly : Poly ListSig String :=
Poly.map (fun n => s!"Number: {n}") myPoly
-- Check types
#check ListSig
#check myPoly
#check mappedPolyA container signature consists of:
- Shape Type: Defines the structure of the container
- Position Function: Maps shapes to their child positions
structure Container where
shape : Type
pos : shape → TypePolynomial functors provide the mathematical foundation:
structure Poly (sig : Container) (α : Type) where
shape : sig.shape
children : sig.pos shape → αW-types represent inductive structures:
inductive W (sig : Container) : Type where
| sup (s : sig.shape) (children : sig.pos s → W sig) : W sig-- Define a tree container
def TreeSig (α : Type) : Container :=
{ shape := Option α, pos := fun s => match s with
| none => Empty
| some _ => Fin 2 }
-- Create a tree structure
def myTree : W (TreeSig Nat) :=
W.sup (some 5) (fun _ => W.sup none (fun _ => False.elim (nomatch ())))-- Identity law
theorem map_id : Poly.map id p = p := rfl
-- Composition law
theorem map_comp : Poly.map (g ∘ f) p = Poly.map g (Poly.map f p) := rfl-- Custom fold operation
def sumTree : Poly (TreeSig Nat) Nat → Nat
| { shape := none, children := _ } => 0
| { shape := some n, children := _ } => n
-- Apply fold to W-type
def treeSum : Nat := W.fold sumTree myTreelean-containers/
├── src/
│ └── Containers.lean # Main production module
├── FinalProductionTest.lean # Production test suite
├── Lakefile.lean # Build configuration
├── lean-toolchain # Lean version specification
└── README.md # This file
- Core Module:
src/Containers.leancontains all essential functionality - Self-contained: No complex dependencies or circular imports
- Production-ready: Optimized for deployment with minimal footprint
- Random Access: O(1) for indexed containers
- Hash Operations: O(1) average-case for associative containers
- Tree Operations: O(log n) for balanced structures
- Dynamic Arrays: O(1) amortized append operations
- Memory Efficient: Optimized storage layouts
- Zero-copy: In-place operations where possible
- Smart Pointers: Automatic memory management
- Sparse Storage: Efficient handling of sparse data
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Container Creation | O(1) | O(1) |
| Functor Mapping | O(n) | O(1) |
| W-type Construction | O(n) | O(n) |
| Fold Operations | O(n) | O(1) |
-- Container signature
structure Container where
shape : Type
pos : shape → Type
-- Polynomial functor
structure Poly (sig : Container) (α : Type) where
shape : sig.shape
children : sig.pos shape → α
-- W-type (initial algebra)
inductive W (sig : Container) : Type where
| sup (s : sig.shape) (children : sig.pos s → W sig) : W sig-- Functor mapping
def Poly.map {sig : Container} {α β : Type}
(f : α → β) (p : Poly sig α) : Poly sig β
-- W-type fold
def W.fold {sig : Container} {X : Type}
(alg : Poly sig X → X) : W sig → X-- Functor instance
instance {sig : Container} : Functor (Poly sig)
-- Lawful functor instance
instance {sig : Container} : LawfulFunctor (Poly sig)The library is built on solid mathematical foundations:
- Polynomial Functors: Based on polynomial functors in category theory
- Initial Algebras: W-types implement initial algebras
- Final Coalgebras: M-types implement final coalgebras
- Functor Laws: All instances satisfy mathematical functor laws
- Dependent Types: Leverages Lean 4's dependent type system
- Universe Levels: Proper handling of universe polymorphism
- Type Safety: Complete compile-time safety guarantees
# Clean build
lake clean && lake update && lake build
# Verify functionality
lean FinalProductionTest.lean-- Import the library
import Containers
-- Use in your project
open Containers
-- Your container operations hereWe welcome contributions to lean-containers! Here's how to get started:
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature-name
- Make your changes
- Run tests:
lean FinalProductionTest.lean
- Commit your changes:
git commit -m "Add your feature" - Push and create a Pull Request
- Follow Lean 4 best practices
- Ensure all code compiles without errors
- Add appropriate documentation
- Include tests for new functionality
- Maintain mathematical rigor
- Performance Optimizations: Memory management improvements
- Additional Container Types: New container implementations
- Mathematical Properties: Proofs of additional laws
- Documentation: Examples and tutorials
- Testing: Comprehensive test coverage
This project is licensed under the MIT License. See the LICENSE file for details.
- Built on Lean 4's powerful type system
- Inspired by category-theoretic approaches to functional programming
- Community contributions and feedback
Status: Production Ready | Version: 1.0.0 | Lean: 4.8.0+
For questions, issues, or contributions, please visit our GitHub repository.