Skip to content

Reuse a proof across implementations

Prove a general theorem once (twice_id), apply it to two different implementations (keep and addZero), see how splitting statement, template, and proof keeps hashes stable, and inspect semantic changes with lema diff.

In Lesson 4 (Select a contract and prove it), you proved a contract about one specific function (double). As your project grows, you do not want your AI coding agent to re-prove caller theorems from scratch every time you optimize or swap out an internal implementation.

Instead, Lema lets you prove a general theorem once against a contract hypothesis, and apply that theorem to any implementation that satisfies the contract. Along the way, you will look inside the five content-addressed objects that make up a declaration (declaration, statement, template, proof, and satisfaction) to see why refactoring a proof never invalidates downstream code.

1. Prove a general theorem once (twice_id)

Review the reusable identity theorem from Lesson 1 across the Lean, Pythonic, and Explanation tabs:

Pythonic

This readable view shows the statements. The Lean tab contains the complete module with proof terms and its publication namespaces.

Pythonic syntax
law twice_id(f: Nat -> Nat):
    requires forall x: Nat :: f(x) == x
    ensures forall x: Nat :: f(f(x)) == x

def keep(x: Nat) -> Nat:
    x

law keep_law(x: Nat):
    keep(x) == x

law keep_twice(x: Nat):
    keep(keep(x)) == x

Lean

Reusable identity law
namespace Lema
namespace Reuse

theorem twice_id :
    ∀ (f : Nat → Nat), (∀ x, f x = x) → ∀ x, f (f x) = x :=
  fun f law x => (congrArg f (law x)).trans (law x)

def keep (x : Nat) : Nat := x

theorem keep_law (x : Nat) : keep x = x := rfl

theorem keep_twice (x : Nat) : keep (keep x) = x :=
  twice_id keep keep_law x

end Reuse
end Lema

Explanation

If a function leaves every input unchanged, applying it twice also leaves every input unchanged. twice_id proves this once for any function with that property.

keep supplies a concrete implementation, and keep_law proves the required premise. keep_twice reuses twice_id with that implementation and evidence.

The Lean tab includes the proofs. The Pythonic view states the same promises without their proof terms.

Look at how twice_id is structured:

  • Instead of hardcoding a concrete function like keep, twice_id takes any function f : Nat → Nat together with a proof (law : ∀ x, f x = x) that f returns its input unchanged.
  • From that contract hypothesis alone, twice_id proves ∀ x, f (f x) = x: first rewriting the inner f x to x using congrArg f (law x), and then rewriting the outer f x to x using .trans (law x).
  • Then keep_twice applies twice_id to keep and keep_law in one line (twice_id keep keep_law x).

Save this module as src/Reuse.lean in your hello project and publish it with lema add:

Shell
cat > src/Reuse.lean <<'EOF'
set_option autoImplicit false
namespace Lema
namespace Reuse

theorem twice_id :
    ∀ (f : Nat → Nat), (∀ x, f x = x) → ∀ x, f (f x) = x :=
  fun f law x => (congrArg f (law x)).trans (law x)

def keep (x : Nat) : Nat := x

theorem keep_law (x : Nat) : keep x = x := rfl

theorem keep_twice (x : Nat) : keep (keep x) = x :=
  twice_id keep keep_law x

end Reuse
end Lema
EOF
lema add src/Reuse.lean
Output
added src/Reuse.lean
  Reuse.twice_id    #c6535c1bba1f  (theorem, checked, 0 axioms)
  Reuse.keep        #04527d69167f  (def, checked, 0 axioms)
  Reuse.keep_law    #3f1c38da3919  (theorem, checked, 0 axioms)
  Reuse.keep_twice  #b28938636f86  (theorem, checked, 0 axioms)

2. How splitting statement, template, and proof keeps hashes stable

Why does Lema store five separate objects in .lema/store for each declaration instead of hashing the .lean source file? Inspect Reuse.twice_id (#c6535c1bba1f…) using lema call show:

Shell
lema call show --params '{"id": "c6535c1bba1f…"}'
Output (the declaration object)
{
  "object": {
    "schema": "lema.core/v4",
    "kind": "declaration",
    "flavor": "theorem",
    "namespace": "4305157d7b8a…",
    "statement": "07d8af57d41a…",
    "template": "9500cf36ed78…",
    "proofs": [
      "32f786cce2d5…"
    ],
    "binding": "a6553280b13f…",
    "hints": null
  },
  "receipt": {
    "axioms": []
  },
  "names": [
    "Reuse.twice_id"
  ]
}

Look at how those five content-addressed objects divide the work (Code stored by identity):

Object kindSHA-256 ID in twice_idWhat it contains and why it is separate
declaration#c6535c1bba1f...The top-level accepted unit (flavor: "theorem" or "def"), tying together the four objects below.
statement#07d8af57d41a...The logical claim (∀ f, (∀ x, f x = x) → ∀ x, f (f x) = x) with local variable names erased (de Bruijn indexed). Downstream theorems cite twice_id by this Statement ID alone.
template#9500cf36ed78...The computational body with every proof subterm replaced by a numbered hole (slot 0). For a theorem, the entire value is a proof, so its template is slot 0 alone. For a def, downstream callers cite the definition by its Template ID.
proof#32f786cce2d5...The deduction term (fun f law x => (congrArg f (law x)).trans (law x)) that fills slot 0.
satisfaction#a6553280b13f...Stored in binding; records the realizations array that maps each cited Statement ID or Template ID to the concrete accepted declaration ID used during checking.

What does this separation buy you in practice?

  • When keep_twice cites twice_id and keep_law, keep_twice’s own statement and proof objects embed only the Statement IDs of twice_id (#07d8af57d41a...) and keep_law (#72b9342dc5a0...), plus the Template ID of keep (#00c081105265...).
  • If your AI coding agent later simplifies or speeds up the proof of twice_id without changing its statement, the Statement ID (#07d8af57d41a...) stays identical, and keep_twice’s statement and proof hashes do not change at all.

3. Apply twice_id to a second implementation (addZero)

Now add a second implementation addZero to src/Reuse.lean that computes 0 + x instead of x.

In Lean’s standard library, addition on Nat is defined by recursion on the second argument (x + 0 simplifies to x by definition, whereas 0 + x requires the standard library lemma Nat.zero_add x to prove 0 + x = x). Even though addZero has a different implementation body and a different proof (Nat.zero_add) than keep (rfl), it satisfies the exact same contract (∀ x, addZero x = x), so we can apply twice_id to it directly:

src/Reuse.lean (adding addZero, addZero_law, and addZero_twice)
set_option autoImplicit false
namespace Lema
namespace Reuse

theorem twice_id :
    ∀ (f : Nat → Nat), (∀ x, f x = x) → ∀ x, f (f x) = x :=
  fun f law x => (congrArg f (law x)).trans (law x)

def keep (x : Nat) : Nat := x

theorem keep_law (x : Nat) : keep x = x := rfl

theorem keep_twice (x : Nat) : keep (keep x) = x :=
  twice_id keep keep_law x

def addZero (x : Nat) : Nat := 0 + x

theorem addZero_law (x : Nat) : addZero x = x :=
  Nat.zero_add x

theorem addZero_twice (x : Nat) : addZero (addZero x) = x :=
  twice_id addZero addZero_law x

end Reuse
end Lema

Publish the updated file with lema add src/Reuse.lean and render the Pythonic contract view with lema view:

Shell
lema add src/Reuse.lean
lema view Reuse.twice_id Reuse.keep_twice Reuse.addZero_twice
Output (lema view)
law Reuse.twice_id(f: Nat -> Nat):
    requires forall x: Nat :: f(x) == x
    ensures forall x: Nat :: f(f(x)) == x

law Reuse.keep_twice(x: Nat):
    Reuse.keep(Reuse.keep(x)) == x

law Reuse.addZero_twice(x: Nat):
    Reuse.addZero(Reuse.addZero(x)) == x

Why did addZero_twice need zero new reasoning about 0 + (0 + x)? Because twice_id depends only on the contract (∀ x, f x = x), never on how f computes its answer internally.

By contrast, if someone had proved keep_twice by unfolding keep’s implementation body directly (theorem keep_twice (x : Nat) : keep (keep x) = x := rfl) instead of going through twice_id and keep_law, trying that same rfl proof on addZero (addZero x) = x would fail immediately because 0 + (0 + x) does not reduce to x by definition. Proving theorems through contract laws keeps your codebase modular when implementations change.

4. Inspect semantic changes with lema diff and commit

Run lema diff to see how Lema’s semantic change report (lema.change-report/v1) classifies your additions:

Shell
lema diff
Output
ref: main (generation 3 -> working tree)
conservative: true

exports (added: 7):
  + Reuse.twice_id       #c6535c1bba1f  (theorem, checked)
  + Reuse.keep           #04527d69167f  (def, checked)
  + Reuse.keep_law       #3f1c38da3919  (theorem, checked)
  + Reuse.keep_twice     #b28938636f86  (theorem, checked)
  + Reuse.addZero        #ef20c6e6b2e2  (def, checked)
  + Reuse.addZero_law    #c12172915e59  (theorem, checked)
  + Reuse.addZero_twice  #099fdcb4c82f  (theorem, 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]

Notice the conservative: true line at the top of the report. Lema’s semantic change reporter (crates/lema-store/src/changes.rs) classifies every change between two snapshots into seven semantic categories (rename, exports, rebind, substitute, evolve, policy, and material):

  • Adding new checked exports (exports) or refactoring a proof while keeping the exact same statement ID (rebind) requires only the new kernel receipts.
  • Substituting one implementation for another under the same export name (substitute) checks whether you supplied a replacement witness (Replace an implementation) or contract selection evidence, and reports old_still_used if any downstream exports in the snapshot still point to the old implementation’s SHA-256 ID.

Commit this revision with lema commit:

Shell
lema commit -m "Add reusable twice_id theorem for keep and addZero"
Output
committed revision #d482f109c3a7 to main (generation 4, 1 open obligation carried)

Next

Continue to Lesson 6: Run and release a program on the Lema VM to run pure ByteArray tests with lema test, execute interactive Prog applications on the Lema VM with lema run, watch source files with lema dev, and create a verified release with lema release and lema run --release.

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