Select a contract and prove it
Practice Lema's interface-first, proof-optional workflow by selecting a contract in .lema syntax, running your code while the contract is open, and discharging it with Store::check_witness.
In traditional theorem provers, you often cannot finish compiling a module until every theorem inside it has a complete proof. That makes exploratory coding painful: you want to sketch your interface, state the contract laws you care about, run your implementation on test inputs right away, and hand the proof obligations to an AI coding agent such as Claude Code or Codex once the design feels right.
Lema is built around an interface-first, proof-optional workflow (Decision 0051). Because contracts are defined as Prop-valued claims or structures (contract) and bound to implementations through immutable contract selection records (lema.contract/v1), the Lean 4.34.1 kernel accepts your contracts and implementations on day one with zero proofs and zero sorry axioms.
1. Define a contract and select an implementation (src/Double.lema)
Inside the hello project from Lesson 3 (Create and explore your first project), inspect this contract and implementation:
Pythonic
def PreservesZero(f: Nat -> Nat) -> Prop:
f(0) == 0
def double(n: Nat) -> Nat:
n + n
select PreservesZero(double)Lean
Selection is a Lema codebase operation. The final comment names the selected proposition; these declarations do not supply its proof.
def PreservesZero (f : Nat → Nat) : Prop :=
f 0 = 0
def double (n : Nat) : Nat := n + n
-- Selected proposition: PreservesZero doubleExplanation
The contract asks whether double returns zero when given zero. double computes n + n, so its value at zero is zero.
The selection records an open obligation about this exact implementation. The store still needs an accepted matching theorem before it can mark the selection checked.
Why does the Lean 4.34.1 kernel accept PreservesZero, double, and select PreservesZero(double) before any proof exists?
PreservesZerohas type(Nat -> Nat) -> Prop. It defines the question (“given a functionf, doesf(0) == 0hold?”), which is a well-typed definition in Lean that needs no proof.doublehas typeNat -> Nat. It computesn + n.select PreservesZero(double)constructs the closed target propositionPreservesZero(double)(Hello.double(0) == 0), verifies in the Lean kernel thattheorem : T -> T := fun h => his a well-typed proposition, pinsHello.double’s exact SHA-256 declaration ID intarget.realizations, and writes an immutablelema.contract/v1record withstatus: "open".
Save those lines to src/Double.lema and add the file to your project:
cat > src/Double.lema <<'EOF'
def PreservesZero(f: Nat -> Nat) -> Prop:
f(0) == 0
def double(n: Nat) -> Nat:
n + n
select PreservesZero(double)
EOF
lema add src/Double.lemaadded src/Double.lema
Hello.PreservesZero #9a14c702e8b1 (def, checked, 0 axioms)
Hello.double #4e82d190f3a6 (def, checked, 0 axioms)
select Hello.PreservesZero(Hello.double) #c307b451a9d2 (contract, open)2. Test and run your implementation while the contract is still open
Even though select Hello.PreservesZero(Hello.double) is open, Hello.double is already an accepted executable definition in .lema/store. You can write a quick pure test root in src/Main.lean and run lema test and lema run immediately:
set_option autoImplicit false
namespace Lema
namespace Hello
def checkDouble : ByteArray :=
if Received.double 21 == 42 then "42 ok\n".toUTF8 else "fail\n".toUTF8
end Hello
end LemaAdd src/Main.lean and run lema test:
lema add src/Main.lean
lema test Hello.checkDouble --expect "42 ok"test Hello.checkDouble: passed (output matched "42 ok")This is why Lema works so naturally for Python developers and vibecoders: you can iterate on your data types, functions, and VM tests at full speed without getting blocked by unfinished proofs, while lema status and lema diff keep a precise ledger of every contract selection that still needs a proof.
3. Have your coding agent supply the proof (src/DoubleProof.lean)
Now ask your AI coding agent (or write a four-line .lean module yourself) to prove PreservesZero(double). Save this module as src/DoubleProof.lean:
set_option autoImplicit false
namespace Lema
namespace Hello
theorem double_preserves_zero : Received.PreservesZero Received.double :=
rfl
end Hello
end LemaWhy does rfl prove Received.PreservesZero Received.double? Because unfolding PreservesZero double yields double 0 = 0, and unfolding double 0 yields 0 + 0 = 0, which reduces to 0 = 0 by definition.
Publish src/DoubleProof.lean with lema add:
lema add src/DoubleProof.leanadded src/DoubleProof.lean
Hello.double_preserves_zero #7f19a384c2e0 (theorem, checked, 0 axioms)
contract Hello.PreservesZero(Hello.double) #e812d904b7c3 (checked by #7f19a384c2e0)4. What Store::check_witness just verified under the hood
Look closely at the second line of output above: contract Hello.PreservesZero(Hello.double) (checked by #7f19a384c2e0).
When lema add accepted Hello.double_preserves_zero, the store ran Store::check_witness (crates/lema-store/src/records/contract.rs) against your open selection select Hello.PreservesZero(Hello.double). To prevent an AI coding agent from spoofing a contract check, Store::check_witness enforces two independent requirements:
- Statement match: The theorem’s statement must match the selection target’s
statement(up to closed universe-level spelling equivalence). - Realization binding match (
target.realizations): Every constant cited inside the theorem’ssatisfactionobject must resolve to the exact same declaration SHA-256 ID pinned in the selection’starget.realizations.
What if a coding agent got stuck proving a contract about your real function Hello.double (#4e82d190f3a6), so it defined a trivial shadow function with the same name in a scratch module and proved PreservesZero about that function instead? Store::check_witness catches the mismatch immediately and refuses to discharge the selection:
theorem #... binds Hello.double to #11223344..., and the selection binds it to #4e82d190f3a6: it is about another implementationBecause lema.contract/v1 records are immutable, passing Store::check_witness writes a new immutable lema.contract/v1 record (#e812d904b7c3) under the same selection_key with status: "checked" and by: "7f19a384c2e0...".
5. Multi-law contracts and partial law progress (LawfulEscrowStorage)
Real systems often group several related laws into a single contract block. Inspect how Lema’s Escrow Ledger specifies its account storage interface across the Pythonic, Lean, and Explanation tabs:
Pythonic
You can publish this contract structure and select it for an implementation on day one without proving the laws first. Your implementation compiles and runs on the VM while the selection is tracked as open.
contract LawfulEscrowStorage(ops: EscrowStorage):
law load_after_save(db: StoreHandle, u: Nat, acc: Account):
ops.load_account(ops.save_account(db, u, acc), u) == acc
law load_other_unchanged(db: StoreHandle, u1: Nat, u2: Nat, acc: Account):
requires u1 != u2
ensures ops.load_account(ops.save_account(db, u1, acc), u2) == ops.load_account(db, u2)
select LawfulEscrowStorage(MemoryStorage)Lean
structure LawfulEscrowStorage (ops : EscrowStorage) : Prop where
load_after_save : ∀ (db : StoreHandle) (u : Nat) (acc : Account),
ops.load_account (ops.save_account db u acc) u = acc
load_other_unchanged : ∀ (db : StoreHandle) (u1 u2 : Nat) (acc : Account),
u1 ≠ u2 →
ops.load_account (ops.save_account db u1 acc) u2 = ops.load_account db u2
-- Selected contract record: LawfulEscrowStorage MemoryStorageExplanation
A key-value or account store needs two complementary laws: reading back the key you just saved returns the new account (load_after_save), and saving one user’s account never clobbers another user’s account (load_other_unchanged).
Separating the operational interface (EscrowStorage) from the proposition-valued contract (LawfulEscrowStorage) lets you write, build, and run MemoryStorage immediately, while recording LawfulEscrowStorage(MemoryStorage) as an open contract selection in the store.
Later, an AI coding agent can prove each law in a separate module. Proving one law records partial progress (1/2 laws proved, status still open); proving the whole contract marks the selection checked and unlocks generic conservation theorems.
In LawfulEscrowStorage(ops: EscrowStorage), two laws work together:
load_after_save: Reading back the accountuyou just saved returns the newacc.load_other_unchanged: Saving accountu1never alters a different user’s accountu2(requires u1 != u2).
When you write select LawfulEscrowStorage(MemoryStorage) in a .lema file, the store builds both the whole-contract target (LawfulEscrowStorage MemoryStorage) and individual standalone targets for each field (laws[0].target for load_after_save and laws[1].target for load_other_unchanged).
That gives you and your coding agent granular progress tracking in lema status and lema diff:
- Proving one law at a time (
laws[i].by): If your agent provesload_after_savefirst,Store::check_witnessmatcheslaws[0].targetand writes a new immutablelema.contract/v1record withlaws[0].by = <theorem_id>while keeping the top-level contractstatus: "open"(1/2 laws proved). An individual law witness records real progress, and it never falsely marks the whole contractchecked. - Proving the whole contract (
status: "checked", by: <theorem_id>): Once your agent supplies a theorem provingLawfulEscrowStorage MemoryStorageas a whole,Store::check_witnessverifies the whole-selection target and writes a newlema.contract/v1record with top-levelstatus: "checked"andby: <theorem_id>, discharging the revision obligation.
6. Inspect lema diff and commit the semantic revision
Run lema diff in your hello project to inspect the state of your contracts before committing:
lema diffref: main (generation 2 -> working tree)
exports (added: 3, modified: 1):
+ Hello.PreservesZero #9a14c702e8b1 (def, checked)
+ Hello.double #4e82d190f3a6 (def, checked)
+ Hello.double_preserves_zero #7f19a384c2e0 (theorem, checked)
~ Hello.checkDouble #3b08e124a5f9 (def, checked)
contracts (2 selected: 1 checked, 1 open):
✓ select Hello.PreservesZero(Hello.double) [checked by Hello.double_preserves_zero #7f19a384c2e0]
~ select Hello.Advances(Hello.advance) [open, carried from parent]Commit the new revision to main with lema commit:
lema commit -m "Add double and prove PreservesZero(double)"committed revision #a19f402e8d11 to main (generation 3, 1 obligation discharged, 1 open obligation carried)7. See what happens when a buggy edit breaks a proved contract
Suppose someone later edits double(n: Nat) in src/Double.lema from n + n to n + 1 (which returns 1 at 0 instead of 0) and runs lema add src/Double.lema:
lema addelaborates the newHello.double(n + 1), which receives a new SHA-256 declaration ID (#6d01b832e4a9), and re-adds downstream modulesrc/DoubleProof.lean.- When re-adding
src/DoubleProof.lean, Lean tries to checktheorem double_preserves_zero : PreservesZero double := rflagainst the newdouble. Because0 + 1reduces to1, not0, Lean rejects the proof with a caret diagnostic atrfl:error: src/DoubleProof.lean:6:2: type mismatch rfl has type Hello.double 0 = Hello.double 0 expected Hello.PreservesZero Hello.double - Even if the author deleted
src/DoubleProof.leaninstead of fixing it, the old theorem (#7f19a384c2e0) still binds the oldHello.doubleID (#4e82d190f3a6), soselect PreservesZero(double)for the newHello.double(#6d01b832e4a9) immediately shows up inlema statusandlema diffasopen. No stale proof can ever carry over to a modified implementation.
Next
Continue to Lesson 5: Reuse a proof across implementations to prove a general theorem once (twice_id), apply it to two different implementations (keep and addZero), and see why splitting statement, template, and proof keeps hashes stable across proof refactors.
Lema v0.3.2 · Updated 29 October 2026 · Version and compatibility