TorchLean brings tensor computation, machine learning, and formal verification together in Lean 4. The cool part is that Lean is both a functional programming language and a theorem prover, so you can write computations, build and train models, and prove mathematical properties in the same language. You can start with tensors and linear algebra, use the library for general numerical programming, or work directly with the specifications and proofs.
A few highlights we’re excited about:
- Tensors that carry their shapes. Lean checks that the dimensions fit when you compose tensor operations. You can use the array and linear algebra library on its own, without building a neural network.
- Build and train models in Lean. Start with regression or other classical models, train a transformer or generative model, or develop a reinforcement learning agent. TorchLean owns automatic differentiation and optimizers, with CPU execution and a GPU backend built on LibTorch’s tensor operations.
- A shared graph for computation and proofs. A typed computation graph records the operations in a model and the shapes of their inputs and outputs. Lowering records them as operators of one table, the same table the autograd tape records, with one rule for each operator’s value and gradient. We use the graph to describe the calculation, execute it, and state mathematical properties about it. The formalization map connects the definitions to their proofs.
- Floating-point behavior is part of the mathematics. Through FloatLib, TorchLean supports configurable binary formats, executable arithmetic, and proofs about rounding and numerical error. The floating-point guide explains the arithmetic models and how they relate to native execution.
- Ask questions about a whole range of inputs. For a classifier, we can establish conditions under which its prediction stays the same throughout an input region. The verification tools include interval bounds and certificate checking, with Lean proofs for the mathematical guarantees.
- Write your own tensor calculations. Apply an ordinary Lean function with
f.run input, choose CPU or GPU at the call site, or addgrad := trueto record supported operations on the existing autograd tape. The custom computations example covers parameters, recursive updates and configured binary precision, including binary128 values and gradients on GPU. - Gradients with proofs. For registered real-valued operators, derivative certificates show
that the stored backward rule is the adjoint of the Fréchet derivative under its stated domain
conditions. The
op_derivtactic can prove the corresponding property for a backward pass you write. The autograd proofs chapter shows both. - Recursion over trees on the GPU. Derive
Sig.Inductivefor an inductive type, andFold.ofturns a structurally recursive function over it into a fold that runs level by level on the GPU, with a proof that the fold computes the function. The custom computations example folds a tree.
The guide walks through the library step by step. If you’d rather start by running something, try the examples.