5.5 Evolve a library and track external claims¶
Tutorial sequence · Previous: 5.4 Save and reopen a binary library
Goal: evolve a binary proof library across multiple versions, observe
dependency-checked withdrawals, resolve concurrent-writer conflicts with
rebase(), and attach external solver claim provenance to checked theorems.
Complete the Store setup from Lesson 5.4 first.
Versioned edits, dependency protection, and optimistic concurrency¶
Library.change() enforces three invariants on library evolution:
- Atomic versions:
change.publish()writes a complete new version and advancesCURRENTatomically; prior versions remain addressable byIdentity. - Referential integrity:
change.withdraw(key)succeeds only when no theorem in the version citeskey; otherwise it raisesStoreErrorKind.LIBRARY. - Optimistic concurrency: if another writer advances
CURRENTbeforepublish(),publish()raisesStoreErrorKind.CONFLICT. Callingchange.rebase()updates the pending change onto the latest version sopublish()can be retried.
Mojo
"""Create a proof-DAG library, add theorems from articles, refuse a withdrawal that something depends on, and publish
a change again after a conflict."""
from std.tempfile import mkdtemp
from judgment_store import Library, StoreErrorKind
# An OpenTheory article: ⊢ p = p for a variable p : bool, by reflexivity.
comptime TYPES = String(
"6\nversion\n",
"\"bool\"\ntypeOp\nnil\nopType\n0\ndef\npop\n",
"\"->\"\ntypeOp\n0\nref\n0\nref\nnil\ncons\ncons\nopType\n1\ndef\npop\n",
"\"->\"\ntypeOp\n0\nref\n1\nref\nnil\ncons\ncons\nopType\n2\ndef\npop\n",
"\"p\"\n0\nref\nvar\n3\ndef\npop\n3\nref\nvarTerm\n4\ndef\npop\n",
"\"=\"\nconst\n2\nref\nconstTerm\n4\nref\nappTerm\n4\nref\nappTerm\n5\ndef\npop\n",
)
comptime REFL = String(TYPES, "4\nref\nrefl\nnil\n5\nref\nthm\n")
# ⊢ (p = p) = (p = p), from ⊢ p = p cited as an axiom: the library's theorem.
comptime TWICE = String(
TYPES,
"nil\n5\nref\naxiom\n6\ndef\npop\n6\nref\n6\nref\ndeductAntisym\n",
"\"=\"\nconst\n2\nref\nconstTerm\n5\nref\nappTerm\n5\nref\nappTerm\n7\ndef\npop\nnil\n7\nref\nthm\n",
)
def article(dir: String, name: String, text: String) raises -> String:
var path = String(dir, "/", name, ".art")
with open(path, "w") as f:
f.write(text)
return path
def theorems(lib: Library) -> Int:
var n = 0
for t in lib.theorems():
if t.kind == "theorem":
n += 1
return n
def main() raises:
var dir = mkdtemp()
var path = dir + "/lib"
# A new library: its first version holds the profile's statements and nothing else.
var lib = Library.create(path, "none")
print("Created; theorems:", theorems(lib))
# Each change is published as the next version, atomically.
var change = lib.change()
change.add_article(article(dir, "refl", REFL))
var previous = change.publish()
change = Library.open(path).change()
change.add_article(article(dir, "twice", TWICE))
_ = change.publish()
lib = Library.open(path)
print("After two changes; theorems:", theorems(lib), "| base is the previous version:", change.base() == previous)
# p = p is cited by the second theorem's proof: withdrawing it is refused. Nothing cites the second theorem.
for t in lib.theorems():
if t.kind != "theorem":
continue
var withdraw = lib.change()
try:
withdraw.withdraw(String(t.sid))
print("Withdrawing a theorem nothing cites: accepted")
except e:
print("Withdrawing a cited theorem:", e.kind)
# Two writers open the same version. The second publishes after the first: it is refused, rebased onto the new
# version, and published there.
var first = Library.open(path).change()
var second = Library.open(path).change()
first.add_article(article(dir, "first", REFL))
second.add_article(article(dir, "second", REFL))
var by_first = first.publish()
try:
_ = second.publish()
except e:
print("The second writer:", e.kind)
second.rebase()
_ = second.publish()
print("Published over the first writer's version:", second.base() == by_first)
Run from the repository root pixi run mojo run -I src docs/examples/store/change_library.mojo
Output
Created; theorems: 0
After two changes; theorems: 2 | base is the previous version: True
Withdrawing a theorem nothing cites: accepted
Withdrawing a cited theorem: LIBRARY
The second writer: CONFLICT
Published over the first writer's version: True
Python
"""Create a proof-DAG library, add theorems from articles, refuse a withdrawal that something depends on, and publish
a change again after a conflict."""
from pathlib import Path
from tempfile import TemporaryDirectory
from judgment import Library, StoreError
# An OpenTheory article: ⊢ p = p for a variable p : bool, by reflexivity.
TYPES = (
"6\nversion\n"
'"bool"\ntypeOp\nnil\nopType\n0\ndef\npop\n'
'"->"\ntypeOp\n0\nref\n0\nref\nnil\ncons\ncons\nopType\n1\ndef\npop\n'
'"->"\ntypeOp\n0\nref\n1\nref\nnil\ncons\ncons\nopType\n2\ndef\npop\n'
'"p"\n0\nref\nvar\n3\ndef\npop\n3\nref\nvarTerm\n4\ndef\npop\n'
'"="\nconst\n2\nref\nconstTerm\n4\nref\nappTerm\n4\nref\nappTerm\n5\ndef\npop\n'
)
REFL = TYPES + "4\nref\nrefl\nnil\n5\nref\nthm\n"
# ⊢ (p = p) = (p = p), from ⊢ p = p cited as an axiom: the library's theorem.
TWICE = (
TYPES
+ "nil\n5\nref\naxiom\n6\ndef\npop\n6\nref\n6\nref\ndeductAntisym\n"
+ '"="\nconst\n2\nref\nconstTerm\n5\nref\nappTerm\n5\nref\nappTerm\n7\ndef\npop\nnil\n7\nref\nthm\n'
)
def article(directory: Path, name: str, text: str) -> str:
path = directory / f"{name}.art"
path.write_text(text)
return str(path)
def theorems(lib: Library) -> int:
return sum(1 for t in lib.theorems() if t.kind == "theorem")
def main():
with TemporaryDirectory() as tmp:
directory = Path(tmp)
path = str(directory / "lib")
# A new library: its first version holds the profile's statements and nothing else.
lib = Library.create(path, "none")
print("Created; theorems:", theorems(lib))
# Each change is published as the next version, atomically.
change = lib.change()
change.add_article(article(directory, "refl", REFL))
previous = change.publish()
change = Library.open(path).change()
change.add_article(article(directory, "twice", TWICE))
change.publish()
lib = Library.open(path)
print("After two changes; theorems:", theorems(lib), "| base is the previous version:", change.base() == previous)
# p = p is cited by the second theorem's proof: withdrawing it is refused. Nothing cites the second theorem.
for t in lib.theorems():
if t.kind != "theorem":
continue
withdraw = lib.change()
try:
withdraw.withdraw(str(t.sid))
print("Withdrawing a theorem nothing cites: accepted")
except StoreError as e:
print("Withdrawing a cited theorem:", e.kind)
# Two writers open the same version. The second publishes after the first: it is refused, rebased onto the new
# version, and published there.
first = Library.open(path).change()
second = Library.open(path).change()
first.add_article(article(directory, "first", REFL))
second.add_article(article(directory, "second", REFL))
by_first = first.publish()
try:
second.publish()
except StoreError as e:
print("The second writer:", e.kind)
second.rebase()
second.publish()
print("Published over the first writer's version:", second.base() == by_first)
if __name__ == "__main__":
main()
Run from the repository root PYTHONPATH=bindings/python:docs/examples/python/store pixi run python docs/examples/python/store/change_library.py
Output
Created; theorems: 0
After two changes; theorems: 2 | base is the previous version: True
Withdrawing a theorem nothing cites: accepted
Withdrawing a cited theorem: LIBRARY
The second writer: CONFLICT
Published over the first writer's version: True
To replace an existing theorem in place, use change.replace(name, export).
Query lib.dependents(key) to inspect dependents before withdrawing an entry.
See changing a library for the full contract.
External solver claims and provenance¶
When an external solver or decision procedure supplies a side condition, you
can record provenance metadata while keeping every theorem checked through
judgment_core.
In direct-package Mojo, judgment_shell provides ClaimTable, assume_claim,
export_claims, and label:
assume_claim(s, claims, frame, prop, query, decider, version, budget)attaches structured provenance (query,decider,version) to a local assumption.export_claimsdischarges the assumption into a guarded implication whoselabelretains the claim metadata.- Publishing that guarded theorem to a
Libraryand rechecking it in a fresh session checks the guarded implication itself; a plain citation in the fresh session carriesEvidenceLevel.CHECKED.
"""Export checked proof-DAG theorems into a library and track external claims in a session."""
from std.tempfile import mkdtemp
from std.testing import assert_equal, assert_true
from judgment_shell import (
ClaimTable, EvidenceLevel, Session, assume_claim, export_claims, label,
)
from judgment_store import Library
from termbank import Budget
def main() raises:
var budget = Budget(10000000)
var path = String(mkdtemp(), "/lib")
var lib = Library.create(path, "none")
# Track an external claim in a session and export its guarded implication.
var s = Session.open(lib.profile_snapshot(budget))
var root = s.base()
var a = s.type_var("a")
var fx = s.fix(root, "x", a)
var x = fx[1]
var reflexive = x == x
var claims = ClaimTable(s)
var h = assume_claim(s, claims, fx[0], reflexive, "x = x?", "solver", "1", budget)
var guarded = export_claims(s, claims, s.hypothesis(h), [h, fx[0]], budget)
var guarded_label = label(s, claims, guarded, budget)
assert_equal(len(guarded_label.claims), 1)
print("Stored claim:", guarded_label.claims[0].query, "from", guarded_label.claims[0].decider, guarded_label.claims[0].version)
# Close and publish the guarded theorem into the proof-DAG library, then recheck it.
var fact = s.prove_step("claimed.refl", guarded)
var change = lib.change()
change.add(s.export(fact, "claimed.refl"))
_ = change.publish()
var reopened = Library.open(path)
var check = reopened.recheck(budget, workers=1)
assert_true(check.rows > 0)
var s2 = Session.open(reopened.snapshot(budget, workers=1))
var cited = s2.cite(s2.import_fact("claimed.refl"))
var fresh_claims = ClaimTable(s2)
var bare = label(s2, fresh_claims, cited, budget)
assert_equal(bare.level, EvidenceLevel.CHECKED)
assert_equal(len(bare.claims), 0)
print("Rechecked library theorem; plain citation has", len(bare.claims), "claims")
Run from the repository root pixi run mojo run -I src docs/examples/store/records_and_claims.mojo
Output
Stored claim: x = x? from solver 1
Rechecked library theorem; plain citation has 0 claims
External claims never bypass kernel checking: they remain explicit in guards and evidence labels until discharged by a proof.
Exercise¶
In change_library, remove second.rebase() before the final
second.publish(). What happens?
Answer
second.publish() raises StoreErrorKind.CONFLICT again because the pending
change still targets the superseded base version.
Next: 5.6 Snapshot publication, replacement, and provenance auditing.