Skip to content

Types and invariants

How Lema types carry preconditions and invariants directly in function signatures, why proof fields erase to zero runtime cost, and why the data-type core stays intentionally small.

In Anatomy of a Lema program, total pure functions and data types formed the foundation underneath every command vocabulary and Prog tree. Where does Lema’s type system sit relative to other languages?

In conventional static typecheckers (such as TypeScript, Go, Java, or Rust), types and runtime values live in separate worlds. A type signature can say divide(a: u64, b: u64) -> u64 or append(xs: List<T>, ys: List<T>) -> List<T>, while rules that depend on specific values (b != 0, index < buffer.len(), or “the output buffer has length m + n”) have to be written in docstrings, checked with runtime panics, or covered by sample unit tests.

Like Lean, Idris, Agda, and Rocq (and closely related to refinement-type languages such as F* and Liquid Haskell), Lema is a dependently typed language: types can mention ordinary values and preconditions directly. Instead of scattering defensive if b == 0 checks across every layer, you have two clean ways to express an invariant:

  1. State a law with requires and ensures alongside a total function (as we saw with clampFee in Anatomy of a Lema program).
  2. Pack the invariant into a type itself, so callers must establish the precondition before calling the function at all.

Pythonic

Pythonic syntax
law nonnegative_step(x: Int):
    requires x > 0
    ensures x + 1 >= 0

Lean

The arrow expresses implication: the postcondition follows when the precondition holds. This view shows the statement, without a proof term.

The law’s proposition
∀ x : Int, x > 0 → x + 1 ≥ 0

Explanation

If the integer x is greater than zero, then x + 1 is nonnegative. The promise is conditional: it says what follows when the input satisfies the precondition.

These are logical conditions for a proof. They do not insert runtime assertions into calls, and this statement alone is not proof evidence.

Carrying invariants inside data types

Suppose you are writing financial settlement math where dividing by zero or reading past a fixed ring capacity must never happen. In Lean syntax, a record can pair a runtime value with a logical proof field:

A value paired with its invariant
structure NonZero where
  val : UInt64
  ok  : val != 0 = true

structure SlotId (capacity : Nat) where
  index : Nat
  inBounds : index < capacity

Look at what happens when a function accepts (divisor : NonZero) or (slot : SlotId 64):

  • Callers cannot forget the check. A caller holding an arbitrary x : UInt64 from network input must branch on if h : x != 0 then ... else ... first. Inside the then branch, the typechecker hands them the exact evidence h needed to construct NonZero; inside the else branch, they handle the zero case cleanly.
  • Downstream code needs zero defensive checks. Once a NonZero or SlotId 64 value has been constructed, every helper function deeper in the call graph knows val != 0 and index < 64 without re-testing it at every layer.

Types can also index over values directly, such as a length-indexed vector Vec α n where appending a Vec α m and a Vec α n has return type Vec α (m + n), catching off-by-one framing bugs before the program ever runs.

Zero-cost proof erasure at runtime

If NonZero has two fields (val and ok), does carrying invariants around make your program allocate extra structs and pass proof objects at runtime?

No. Lema enforces a strict boundary between computational data (Type, such as UInt64, ByteArray, or List) and logical propositions (Prop, such as val != 0 = true or index < capacity):

Zero-cost proof erasure What the Lean kernel checks vs. what executes on the Lema VM
Type kept · Prop -> ∅
Admission time · Lean kernel structure NonZero

Dependent record with two fields

  • val : UInt64 Type · Data
  • ok : val != 0 = true Prop · Proof
Runtime · Lema register VM LemaIR bytecode

Single unboxed 64-bit register

  • r0 : u64 8 bytes immediate · 0 wrapper structs
  • ∅ (ok erased) 0 bytes in RAM · 0 runtime checks

Logical fields in Prop enforce preconditions at check time and erase completely before VM bytecode generation.

Two rules make this boundary predictable:

  1. Runtime code cannot branch on how a proof was written. A program can branch on computational data (if x == 0), never on the internal steps of a Prop proof.
  2. Every proof field and proof argument disappears during compilation. When the compiler lowers accepted store objects to VM bytecode (The compiler and VM), every Prop field and theorem parameter is erased completely. At runtime, a NonZero is just a raw unboxed 64-bit integer in a VM register, with zero extra memory and zero runtime assertion checks.

Keeping the data-type core small and auditable

Theorem provers designed for pure mathematics often support elaborate type declarations (such as mutually recursive type groups or types nested inside containers) whose soundness rules complicate the kernel and compiler.

Because every accepted Lema declaration is replayed from canonical store objects and lowered directly to register bytecode, Lema keeps its supported data-type subset intentionally focused:

  • Records (structure): Named product types grouping data fields (or grouping laws inside a contract).
  • Tagged unions (inductive): Standard sum and recursive data types such as Option, Except, List, command vocabularies (KvCmd), and Prog.
  • Single indexed families: Families such as length-indexed Vec or graph reachability Reach u v, validated upfront before entering the store.

Mutually recursive and nested inductive declarations are refused with clear guidance toward simpler list-spine or tag-indexed representations. See Language and authoring rules for the complete syntax reference, or continue to Contracts, selections and proofs to see how teams combine types, interfaces, and contracts in practice.

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