Skip to content

Build software like mathematics

Why AI-generated code changes the economics of software engineering, how human-readable contracts pair with agent-written implementations and proofs, and why Lema keeps its unverified host boundary small.

For seventy years, writing code was slow and expensive, so software engineering optimized around writing less of it and spot-checking it with sample tests. AI coding agents invert that constraint. Generating a thousand lines of code now takes seconds; the bottleneck is knowing with certainty what that code actually does across every edge case, failure mode, and refactor.

Unit tests check finitely many points: “on these five inputs, the function returned what we expected.” When a human wrote the code carefully by hand, five representative inputs gave reasonable confidence. When an agent generates a complex parser, state machine, or storage engine at machine speed, passing five sample tests leaves every untested input length, integer boundary, and partial write failure open.

Mathematics solved the trust-at-scale problem centuries ago:

  1. State a clear theorem (the contract) in concise, unambiguous notation.
  2. Construct a proof showing that an implementation satisfies that statement for all valid inputs.
  3. Check the proof mechanically with a small, uncompromising kernel that accepts zero hand-waving.
  4. Build the next layer on the statement alone, never needing to re-read or re-verify the proof beneath it.

Lema brings that exact architecture to everyday systems and application programming.

Human-readable contracts, agent-authored code and proofs

Formal verification historically stayed inside specialist research labs for a practical reason: writing interactive proofs by hand is painstaking, and proof assistant codebases mix unreadable proof scripts right into module interfaces.

Lema splits the human role and the agent role along the boundary each is best at:

  • Humans read and write concise contracts in Pythonic Lema syntax. You do not need type theory to read or write a specification. Contracts use familiar def, contract, law, requires, ensures, forall, and select blocks that fit on one screen.
  • Agents write implementations and proofs in Lean syntax. Under the surface, Lema lowers code into canonical typed expressions checked from scratch by a pinned Lean 4.34.1 kernel. Coding agents already know Lean 4 syntax and proof tactics from public mathematics libraries, while Lema handles dependency isolation, sandboxing, and execution on a dedicated Rust virtual machine.
  • Both syntaxes are views over the same stored objects. Rather than treating source files on disk as the source of truth, Lema stores canonical typed definitions, statements, and proofs by SHA-256 content hash. When you inspect a contract in Pythonic Lema syntax, Lema renders it directly from the stored specification and verifies structurally that the displayed view matches the stored mathematical statement.
Workflow architecture How human specifications, agent synthesis, and the kernel meet
Pythonic + Lean -> Store
Human engineer · Intent Pythonic view

Concise specification

contract Sorted · requires · ensures

Fits on one screen so reviewers inspect exact domain rules without reading tactic scripts.

AI coding agent · Draft Lean view

Implementation and proof tactics

def sortBy · theorem sortBy_perm

Synthesizes fast algorithms and candidate proof witnesses inside an isolated sandbox.

Mechanical arbiter checked · Lean 4.34.1

Source-free kernel replay (addDeclCore)

Verifies every expression and axiom footprint from scratch before committing to the store.

statement Promise hash

Cited by downstream callers; insulated from proof edits.

template -> Lema VM Register code

Compiles directly in Rust with zero calls to Lean or C compilers.

proof ∅ Erased at runtime

Audited on admission; adds zero bytes or branches to execution.

Humans review concise contracts; agents synthesize code and proofs; the pinned Lean 4.34.1 kernel arbitrates before any object enters the store.

Switch between the Pythonic, Lean, and Explanation tabs below to see the same stored contract from each perspective:

Pythonic

Both views below are rendered from the same accepted statement objects in the store. A sorting contract needs both laws: returning an empty list would be in order, so the second law requires every original element and its count to survive.

Pythonic contract view (Std.Canonical.sortBy)
law sortBy_sorted(le: alpha -> alpha -> Bool, l: List(alpha)):
    requires forall a: alpha, b: alpha, c: alpha :: (le(a, b) == true and le(b, c) == true) -> le(a, c) == true
    requires forall a: alpha, b: alpha :: le(a, b) == true or le(b, a) == true
    ensures Pairwise(fn(a: alpha, b: alpha) -> le(a, b) == true, sortBy(le, l))

law sortBy_perm(le: alpha -> alpha -> Bool, l: List(alpha)):
    Perm(sortBy(le, l), l)

Lean

Lean view (Std.Canonical.sortBy)
theorem sortBy_sorted {α : Type} (le : α → α → Bool)
    (trans : ∀ a b c, le a b = true → le b c = true → le a c = true)
    (total : ∀ a b, le a b = true ∨ le b a = true) :
    ∀ l : List α, (sortBy le l).Pairwise (fun a b => le a b = true)

theorem sortBy_perm {α : Type} (le : α → α → Bool) :
    ∀ l : List α, (sortBy le l).Perm l

Explanation

The first law (sortBy_sorted) promises that for any comparison function le that is transitive and total, every item in sortBy(le, l) is ordered before or equal to the items that follow it.

The second law (sortBy_perm) closes the loophole where a buggy implementation simply drops elements or returns an empty list: it promises that the output list is an exact permutation of the input list, preserving every element and its multiplicity.

Because both laws live in the content-addressed store as accepted statement objects, callers such as canonical HTTP header sorting depend only on these statement hashes, never on the internal sorting implementation or proof steps.

Guarantees that accumulate instead of rotting

In a traditional file-based repository (or even a standard theorem prover repository), refactoring an internal helper function or cleaning up a proof script changes the file and triggers cascading rebuilds across every downstream caller.

Because Lema addresses every declaration by separating what is claimed (statement), how it computes (template), and why the claim holds (proof) into distinct SHA-256 hashes (Code stored by identity):

  • Proofs never leak into callers. Downstream theorems cite an upstream law only by its statement hash. An agent can rewrite a messy 200-line proof into a clean 20-line proof without changing the statement ID or re-checking a single caller.
  • Proofs are optional on day one and erase to zero cost at runtime. You can sketch an interface, write an implementation, select a contract as open, and run your service on the Lema VM right away (Contracts, selections and proofs). When an agent later proves the contract, every proof term is erased during compilation so your binary pays zero runtime penalty.
  • Implementations can be upgraded safely. When an agent replaces a naive reference implementation with a faster one and proves they behave identically (Replaces), existing callers inherit their safety guarantees without changing a line of client code.

Prove inside the language, keep the host boundary tiny

In most everyday application languages, TLS, HTTP framing, cryptography, and crash recovery live inside a large C or Rust runtime library outside any formal model.

Following the verified-systems tradition of projects such as Project Everest (in F*), Fiat-Crypto (in Rocq), and seL4, Lema keeps its unverified host boundary down at six flat-byte operating system capabilities (tcp, udp, block, random, clock, and secret) and builds the rest of the stack inside Lema where models and implementations carry kernel-checked proofs. Copy-on-write 4 KiB block storage (BlockStore), RFC cryptography (ChaCha20Poly1305, X25519, Ed25519, Sha256), X.509 validation, TLS 1.3, HTTP/1.1, and SigV4-signed S3 clients and servers all live above that boundary as ordinary Lema modules.

Where to go next

Lema v0.3.2 · Updated 29 October 2026 · Version and compatibility