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:
- State a
lawwithrequiresandensuresalongside a total function (as we saw withclampFeein Anatomy of a Lema program). - Pack the invariant into a type itself, so callers must establish the precondition before calling the function at all.
Pythonic
law nonnegative_step(x: Int):
requires x > 0
ensures x + 1 >= 0Lean
The arrow expresses implication: the postcondition follows when the precondition holds. This view shows the statement, without a proof term.
∀ x : Int, x > 0 → x + 1 ≥ 0Explanation
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:
structure NonZero where
val : UInt64
ok : val != 0 = true
structure SlotId (capacity : Nat) where
index : Nat
inBounds : index < capacityLook at what happens when a function accepts (divisor : NonZero) or (slot : SlotId 64):
- Callers cannot forget the check. A caller holding an arbitrary
x : UInt64from network input must branch onif h : x != 0 then ... else ...first. Inside thethenbranch, the typechecker hands them the exact evidencehneeded to constructNonZero; inside theelsebranch, they handle the zero case cleanly. - Downstream code needs zero defensive checks. Once a
NonZeroorSlotId 64value has been constructed, every helper function deeper in the call graph knowsval != 0andindex < 64without 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):
Dependent record with two fields
-
val : UInt64Type · Data -
ok : val != 0 = trueProp · Proof
Single unboxed 64-bit register
-
r0 : u648 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:
- 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 aPropproof. - Every proof field and proof argument disappears during compilation. When the compiler lowers accepted store objects to VM bytecode (The compiler and VM), every
Propfield and theorem parameter is erased completely. At runtime, aNonZerois 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 acontract). - Tagged unions (
inductive): Standard sum and recursive data types such asOption,Except,List, command vocabularies (KvCmd), andProg. - Single indexed families: Families such as length-indexed
Vecor graph reachabilityReach 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