Skip to content

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

Pythonic syntax
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.

Lean syntax
def PreservesZero (f : Nat → Nat) : Prop :=
  f 0 = 0

def double (n : Nat) : Nat := n + n

-- Selected proposition: PreservesZero double

Explanation

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?

  • PreservesZero has type (Nat -> Nat) -> Prop. It defines the question (“given a function f, does f(0) == 0 hold?”), which is a well-typed definition in Lean that needs no proof.
  • double has type Nat -> Nat. It computes n + n.
  • select PreservesZero(double) constructs the closed target proposition PreservesZero(double) (Hello.double(0) == 0), verifies in the Lean kernel that theorem : T -> T := fun h => h is a well-typed proposition, pins Hello.double’s exact SHA-256 declaration ID in target.realizations, and writes an immutable lema.contract/v1 record with status: "open".

Save those lines to src/Double.lema and add the file to your project:

Shell
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.lema
Output
added 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:

src/Main.lean
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 Lema

Add src/Main.lean and run lema test:

Shell
lema add src/Main.lean
lema test Hello.checkDouble --expect "42 ok"
Output
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:

src/DoubleProof.lean
set_option autoImplicit false
namespace Lema
namespace Hello

theorem double_preserves_zero : Received.PreservesZero Received.double :=
  rfl

end Hello
end Lema

Why 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:

Shell
lema add src/DoubleProof.lean
Output
added 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:

  1. Statement match: The theorem’s statement must match the selection target’s statement (up to closed universe-level spelling equivalence).
  2. Realization binding match (target.realizations): Every constant cited inside the theorem’s satisfaction object must resolve to the exact same declaration SHA-256 ID pinned in the selection’s target.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:

Refusal when a theorem proves the claim about a different implementation
theorem #... binds Hello.double to #11223344..., and the selection binds it to #4e82d190f3a6: it is about another implementation

Because 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.

Pythonic contract view (EscrowStorage & LawfulEscrowStorage)
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

Lean view (EscrowStorage & LawfulEscrowStorage)
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 MemoryStorage

Explanation

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 account u you just saved returns the new acc.
  • load_other_unchanged: Saving account u1 never alters a different user’s account u2 (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 proves load_after_save first, Store::check_witness matches laws[0].target and writes a new immutable lema.contract/v1 record with laws[0].by = <theorem_id> while keeping the top-level contract status: "open" (1/2 laws proved). An individual law witness records real progress, and it never falsely marks the whole contract checked.
  • Proving the whole contract (status: "checked", by: <theorem_id>): Once your agent supplies a theorem proving LawfulEscrowStorage MemoryStorage as a whole, Store::check_witness verifies the whole-selection target and writes a new lema.contract/v1 record with top-level status: "checked" and by: <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:

Shell
lema diff
Output
ref: 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:

Shell
lema commit -m "Add double and prove PreservesZero(double)"
Output
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:

  1. lema add elaborates the new Hello.double (n + 1), which receives a new SHA-256 declaration ID (#6d01b832e4a9), and re-adds downstream module src/DoubleProof.lean.
  2. When re-adding src/DoubleProof.lean, Lean tries to check theorem double_preserves_zero : PreservesZero double := rfl against the new double. Because 0 + 1 reduces to 1, not 0, Lean rejects the proof with a caret diagnostic at rfl:
    error: src/DoubleProof.lean:6:2: type mismatch
      rfl
    has type
      Hello.double 0 = Hello.double 0
    expected
      Hello.PreservesZero Hello.double
  3. Even if the author deleted src/DoubleProof.lean instead of fixing it, the old theorem (#7f19a384c2e0) still binds the old Hello.double ID (#4e82d190f3a6), so select PreservesZero(double) for the new Hello.double (#6d01b832e4a9) immediately shows up in lema status and lema diff as open. 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