Skip to content

The compiler and VM

How Lema compiles accepted store objects directly to register bytecode with zero Lean or C compiler calls, executes them under bounded leases, and checks execution properties.

When you build an application in a theorem prover, a practical systems question comes up right away: how do you turn mathematical definitions and proofs into a running service without dragging along a multi-gigabyte proof toolchain, a C compiler, and a system linker on every deployment machine?

Lema solves this by separating proof admission from application execution (Decision 0052). The pinned Lean 4.34.1 kernel is the sole authority when code enters the store. Once an application’s dependency closure is accepted in the store, compiling, testing, packaging, and running that application happens entirely in Rust with zero calls to lean, clang, or a C linker.

  1. Stage 1 · Store closure

    Accepted store objects

    Starts from an execution record naming the entry declaration and its accepted SHA-256 dependency closure in the store.

  2. Stage 2 · LemaIR compiler

    Proof erasure & register IR

    Erases all Prop types and proof witnesses, specializes fixed-width scalars, and caches lowered LemaIR units by content digest.

  3. Stage 3 · Register VM

    Flat bytecode & reference counting

    Prepares contiguous instruction arrays and dense jump tables, executing in heap-metered register frames with no tracing GC.

  4. Stage 4 · Supervised runner

    Bounded leases & capabilities

    Enforces step, heap, integer-width, and wall-clock limits while dispatching tasks, actors, sessions, and the 6 host capabilities.

1. Lowering store objects to LemaIR (and erasing proofs)

In a file-based language, building an executable starts by re-parsing text files on disk. In Lema, lema build, lema test, and lema run start directly from accepted store objects:

  1. Complete proof erasure: Every Prop field, theorem witness (Safe, Implements, Replaces), and logical parameter is erased completely. Proof objects do not even need to be read during compilation, because a stored template object already has its inline proof holes cut out (Code stored by identity).
  2. Unboxed scalars and explicit moves: Fixed-width integers (UInt8 through UInt64, USize, Bool), small natural numbers that fit in a machine word, and tag-only enum cases compile to immediate register values without heap allocation. Every register read is marked either as a last-use move (allowing unique buffers to update in place) or a clone.
  3. Content-addressed compilation cache: Each lowered unit is cached inside the store under its object identity and compiler digest, so unchanged dependencies never recompile across edits or repeated lema run calls.

2. Deterministic memory and resource bounds

The Lema virtual machine is a register machine designed for predictable server workloads rather than unpredictable garbage-collection pauses:

  • Reference counting with copy-on-write (no tracing GC): Heap values (ByteArray, Array, String, records, and closures) use reference counting. When a buffer has a single owner and is moved into an update operation, the VM mutates it in place; if another register shares it, the VM copies it first.
  • Heap-allocated call frames: Function calls and closures allocate their register windows inside explicitly metered VM memory rather than growing the host OS thread stack, preventing native stack overflows on deep call chains.
  • Hard supervisor limits: Every run executes inside a supervised worker process (lema-vm-runner) enforcing explicit caps on instruction steps, peak heap bytes, big-integer bit width (stopping accidental exponential Nat blowups), call-stack depth, and wall-clock timeout.

Inside that worker, pure computation runs until the Prog tree either finishes (.done) or yields a command step (.step), which the runner dispatches to its task/actor/session scheduler, an isolated exchange/serve secret sub-run, or the six Layer 1 host capabilities (Capabilities and the host boundary).

3. Oracle testing and static LemaIR guarantees

Because the compiler and VM are written in Rust rather than proved inside Lean, Lema backs them with two complementary engineering checks:

Differential testing against Lema.Ir.Semantics

Inside the Lema library, Lema.Ir.Semantics formalizes a small-step operational model of LemaIR covering register moves, closures, arithmetic, and byte/array primitives. Its corpus theorems are checked by the Lean 4.34.1 kernel, and automated differential test suites run the Rust VM on the same LemaIR programs to verify that outputs and step counts match.

Fail-closed static LemaIR guarantee receipts

Some security and performance rules depend on compiled register code rather than pure mathematical functions (for example, whether two mathematically equal expressions branch on a secret key). Before cutting a release, Lema’s static bytecode analyzers inspect the linked LemaIR closure and issue versioned receipts:

Static guaranteeWhat the analyzer checks over LemaIRRole in the workflow
ConstantTimeVerifies that no secret-tainted register controls a branch (Switch), computes an array/slice index, or enters a variable-time big-integer primitive.Mandatory release gate for any closure touching secret, and verified on ChaCha20Poly1305, X25519, and Ed25519.sign.
SecretFlow.<role>Verifies that secret bytes fetched for a role (SigV4, BearerHeader, Ed25519Sign, etc.) flow only into that role’s approved cryptographic sink and never leak into outputs or logs.Mandatory release gate alongside ConstantTime for every secret-using application.
Allocation & width checksChecks hot-path invariants such as fixed-width-only arithmetic (NoBigIntPrimitives), allocation-free inner loops (NoBoxOrCtorInCycles), unique slice updates (NoCopySliceCall), flat actor messages (FlatMessage), and linear actor state loops (ExclusiveActorLoop).Enforced on cryptographic routines and concurrent actor loops.

For step-by-step build and deployment commands, see Build a VM application and Release an artifact.

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