Code stored by identity
Why Lema stores declarations as content-addressed statements, templates, and proofs instead of source files, how syntax views work, and why AI coding agents move faster against a semantic store.
Most programming languages and theorem provers treat mutable text files on disk as the source of truth. If Storage.lean imports Sort.lean and an AI coding agent reformats a comment, renames a local variable, or cleans up a private proof inside Sort.lean, the file changes on disk and downstream modules have to be re-parsed and re-checked.
Unison demonstrated a cleaner foundation: instead of living in fragile text files, code can live in a content-addressed codebase store, where definitions are identified by the cryptographic hash of their typed structure and text files act as rendered views. Lema adopts Unison’s content-addressed store model directly and extends it to a dependently typed proof language, storing every definition, contract statement, and proof by canonical SHA-256 identity (Where the ideas come from).
Splitting promise, code, and proof into separate identities
When Lema accepts a declaration into the store, it does not hash the declaration as one giant blob. Instead, it separates what is promised, how it computes, and why the promise holds into distinct content-addressed objects:
Renaming an exported symbol updates workspace metadata without changing any SHA-256 object ID below.
What is promised
Canonical proposition type. Downstream proofs cite only this hash.
Executable code shape
Typed AST with inline proof holes cut out. Feeds the LemaIR compiler cache.
Why the promise holds
Links target code to its proof witness. Refactoring a proof changes only this branch.
Rewriting or simplifying a proof updates only the proof and satisfaction hashes; caller citations and compiled VM caches stay untouched.
Separating those hashes eliminates the rebuild cascades that slow down verification and agent workflows:
| What changed | Which hash changes? | Downstream impact |
|---|---|---|
Rename an exported function or local variable (x to item) | None (human names live in project bindings outside object hashes) | Zero re-checking and zero recompilation. |
| Rewrite or simplify a proof (200 tactic lines down to 15) | proof and satisfaction only (statement stays identical) | Zero downstream callers re-checked; zero VM cache invalidation. |
| Add a proof for an already-running implementation | Adds new proof and statement objects citing existing code | Zero change to the implementation’s template hash or compiled VM bytecode. |
| Optimize an implementation’s algorithm | template changes | Callers that depend only on abstract contract laws stay untouched once the new implementation proves those laws. |
Syntax as a lens, not a file format
Because the store holds canonical typed expressions rather than raw text files, surface syntax becomes a view over stored objects:
- Authoring in, canonical objects out: When you or an agent run
lema add, Lema elaborates Pythonic Lema declarations or Lean declarations against your project’s pinned scope and exports canonical objects into the store. Once accepted, the store owns the definition; text files are optional authoring inputs, not what the compiler or kernel trusts (How the store accepts code). - Rendering without hidden surprises: Pretty-printers can be dangerous if hiding implicit type arguments lets two different specifications look identical on screen. Whenever Lema renders a stored contract into Pythonic Lema syntax (for
lema view,lema diff, or code review), it structurally re-parses the rendered text in Rust and recomputes the exact SHA-256statementhash from scratch. If the recomputed hash does not match the stored object byte for byte, Lema refuses the ambiguous view and switches to explicit notation.
Why AI coding agents work better against a store
When you pair with an AI coding agent such as Claude Code or Codex, a semantic store removes three everyday failure modes of file-based repositories:
- Focused context instead of whole-file grepping: Instead of reading thousands of lines of mixed implementation and tactic scripts to understand an API, an agent queries
lema statusorlema viewto read compact contract signatures and exact 12-character short IDs. - Isolated workspaces on one store: Multiple agents can work concurrently in separate store workspaces (Semantic version control) sharing the same immutable object cache without overwriting one another’s working files.
- Instant semantic diffs: When an agent finishes a large refactor,
lema diffcompares object hashes directly: if no exportedstatementhash changed, you know immediately that the agent did not weaken any specification.
What content addressing leaves to other layers
Content addressing solves identity, caching, and pluggable syntax views, while neighboring subsystems handle three distinct jobs:
- Hashing is not proof checking. Computing an object’s SHA-256 hash names which mathematical expression it is; knowing the expression is well-typed and proved comes from a kernel acceptance receipt (How the store accepts code).
- Different code shapes have different
templatehashes.n + 0andnare distinct expressions, so they have differenttemplateIDs. Proving that two different implementations behave identically on all inputs is the job of contracts and replacement theorems (Evolving programs, handles and concurrency).
For the exact JSON encoding rules of canonical objects, see Objects and identity.
Lema v0.3.2 · Updated 29 October 2026 · Version and compatibility