Part 5 — Concurrency, libraries & integration

5.6 Snapshot publication, replacement, and provenance auditing

Tutorial sequence · Previous: 5.5 Evolve a library and external claims

Goal: publish an entire sealed snapshot with add_snapshot, query categories(), dependents(), and statement identities, upgrade a theorem in place with replace(), and audit dependencies, class-instance reliance, and attached external claims.

Complete the Store setup from Lesson 5.4 first.

Library lifecycle operations

Operation Role
change.add_snapshot(snapshot, source, budget) Publishes all checked declarations and theorems from a sealed Session snapshot in one atomic version.
lib.categories() / lib.find("judgment.stmt." + sid) Lists top-level name prefixes or looks up a theorem by canonical statement identity sid.
lib.dependents(name) Lists published theorems citing name.
change.replace(name, export) Replaces a theorem's stored proof and publishes a rechecked version.

Deep provenance auditing on Fact and Step

Beyond axioms() and admissions(), Fact and Step expose finer provenance queries:

  • fact.dependencies(): full dependency entries underlying the theorem.
  • fact.trusted_class_layer(): whether the theorem rests on a statement-only imported class layer rather than rechecked proofs.
  • step.depends_on_instance_of(type_constructor): whether a derivation relied on a type-class instance for type_constructor.
  • session.register_claim(frame, prop, query, decider, version): attaches external solver provenance to an assumption frame, exposed via step.claims().

Mojo

advanced_store_audit.mojo Download
"""Publish a sealed snapshot, replace a theorem, query dependents, and audit provenance and claims."""

from std.tempfile import mkdtemp
from std.testing import assert_equal, assert_false, assert_true
from judgment import Session
from judgment_store import Library
from termbank import Budget


def main() raises:
    var budget = Budget(20_000_000)
    var path = String(mkdtemp(), "/audit_lib")
    var lib = Library.create(path, "none")

    # 1. Publish a sealed session snapshot atomically, then add a dependent theorem in a new version.
    var s = Session.open(lib.profile_snapshot(budget))
    var t1 = s.theorem("basics.truth_refl", s.true_() == s.true_())
    t1.rfl()
    _ = t1.finish()
    var snap_change = lib.change()
    snap_change.add_snapshot(s.seal(), "", budget)
    _ = snap_change.publish()

    var v1 = Library.open(path)
    var s2 = Session.open(v1.snapshot(budget, workers=1))
    var base_fact = s2.import_fact("basics.truth_refl")
    var eq_true = s2.true_() == s2.true_()
    var t2 = s2.theorem("derived.truth_pair", eq_true & eq_true)
    var cited = t2.use(base_fact)
    t2.exact(t2.conjoin(cited, cited))
    var ext = v1.change()
    ext.add(s2.export(t2.finish(), "derived.truth_pair"))
    _ = ext.publish()

    var current = Library.open(path)
    var info = current.find("basics.truth_refl").value().copy()
    assert_true(Bool(current.find(String("judgment.stmt.", info.sid))))
    var cats = current.categories()
    assert_true("basics" in cats and "derived" in cats)
    var deps = current.dependents("basics.truth_refl")
    assert_equal(len(deps), 1)
    print("Library categories:", len(cats), "| dependents of basics.truth_refl:", len(deps))

    # 2. Replace a theorem's proof in place and recheck the updated library version.
    var updater = Session.open(current.snapshot(budget, workers=1))
    var alt_base = updater.import_fact("basics.truth_refl")
    var alt_eq = updater.true_() == updater.true_()
    var alt = updater.theorem("alt_pair", alt_eq & alt_eq)
    var alt_cited = alt.use(alt_base)
    alt.exact(alt.conjoin(alt_cited, alt_cited))
    var rep = current.change()
    rep.replace("derived.truth_pair", updater.export(alt.finish(), "derived.truth_pair"))
    _ = rep.publish()
    var reopened = Library.open(path)
    assert_true(reopened.recheck(budget, workers=1).rows > 0)
    print("Replaced derived.truth_pair and rechecked library: ok")

    # 3. Audit dependencies, trusted class layer, instance reliance, and external claims.
    var audit = Session.open(reopened.snapshot(budget, workers=1))
    var recovered = audit.import_fact("derived.truth_pair")
    assert_false(recovered.trusted_class_layer())
    assert_equal(len(recovered.axioms()), 0)
    assert_true(len(recovered.dependencies()) >= 1)

    var T = audit.declare_type("T")
    var list_ = audit.type_constructor("list", 1)
    var cb = audit.class_("tag")
    _ = cb.type_var("a")
    var tag = cb.finish(audit.true_())
    var lw = audit.theorem("list_tag")
    var a = lw.type_var("a")
    lw.goal(tag.membership(list_(a)))
    var gs = lw.apply(tag.intro())
    gs[0].exact(gs[0].truth())
    audit.instance(list_, [[]], tag, lw.finish())
    var member_step = audit.of_class(list_(T), tag)
    assert_true(member_step.depends_on_instance_of(list_))

    var P = audit.prop("P")
    var frame = audit.assume(audit.base(), P, "claimed_p")
    audit.register_claim(frame, P, "P?", "external_solver", "2.0")
    var hyp_step = audit.hypothesis(frame)
    assert_equal(len(hyp_step.claims()), 1)
    print("Instance dependency tracked:", member_step.depends_on_instance_of(list_), "| attached claims:", len(hyp_step.claims()))

Run from the repository root pixi run mojo run -I src docs/examples/store/advanced_store_audit.mojo

Output

Library categories: 2 | dependents of basics.truth_refl: 1
Replaced derived.truth_pair and rechecked library: ok
Instance dependency tracked: True | attached claims: 1

Python

advanced_store_audit.py Download
"""Publish a sealed snapshot, replace a theorem, query dependents, and audit provenance and claims."""

from pathlib import Path
from tempfile import TemporaryDirectory

from judgment import Budget, Library, Session


def main():
    budget = Budget(20_000_000)
    with TemporaryDirectory() as directory:
        path = str(Path(directory) / "audit_lib")
        lib = Library.create(path, "none")

        # 1. Publish a sealed session snapshot atomically, then add a dependent theorem in a new version.
        s = Session.open(lib.profile_snapshot(budget))
        t1 = s.theorem("basics.truth_refl", s.true_() == s.true_())
        t1.rfl()
        t1.finish()
        snap_change = lib.change()
        snap_change.add_snapshot(s.seal(), "", budget)
        snap_change.publish()

        v1 = Library.open(path)
        s2 = Session.open(v1.snapshot(budget, workers=1))
        base_fact = s2.import_fact("basics.truth_refl")
        eq_true = s2.true_() == s2.true_()
        t2 = s2.theorem("derived.truth_pair", eq_true & eq_true)
        cited = t2.use(base_fact)
        t2.exact(t2.conjoin(cited, cited))
        ext = v1.change()
        ext.add(s2.export(t2.finish(), "derived.truth_pair"))
        ext.publish()

        current = Library.open(path)
        info = current.find("basics.truth_refl")
        assert info is not None
        assert current.find(f"judgment.stmt.{info.sid}") is not None
        cats = current.categories()
        assert "basics" in cats and "derived" in cats
        deps = current.dependents("basics.truth_refl")
        assert len(deps) == 1
        print("Library categories:", len(cats), "| dependents of basics.truth_refl:", len(deps))

        # 2. Replace a theorem's proof in place and recheck the updated library version.
        updater = Session.open(current.snapshot(budget, workers=1))
        alt_base = updater.import_fact("basics.truth_refl")
        alt_eq = updater.true_() == updater.true_()
        alt = updater.theorem("alt_pair", alt_eq & alt_eq)
        alt_cited = alt.use(alt_base)
        alt.exact(alt.conjoin(alt_cited, alt_cited))
        rep = current.change()
        rep.replace("derived.truth_pair", updater.export(alt.finish(), "derived.truth_pair"))
        rep.publish()
        reopened = Library.open(path)
        assert reopened.recheck(budget, workers=1).rows > 0
        print("Replaced derived.truth_pair and rechecked library: ok")

        # 3. Audit dependencies, trusted class layer, instance reliance, and external claims.
        audit = Session.open(reopened.snapshot(budget, workers=1))
        recovered = audit.import_fact("derived.truth_pair")
        assert not recovered.trusted_class_layer()
        assert len(recovered.axioms()) == 0
        assert len(recovered.dependencies()) >= 1

        T = audit.declare_type("T")
        list_ = audit.type_constructor("list", 1)
        cb = audit.class_("tag")
        cb.type_var("a")
        tag = cb.finish(audit.true_())
        lw = audit.theorem("list_tag")
        a = lw.type_var("a")
        lw.goal(tag.membership(list_(a)))
        gs = lw.apply(tag.intro())
        gs[0].exact(gs[0].truth())
        audit.instance(list_, [[]], tag, lw.finish())
        member_step = audit.of_class(list_(T), tag)
        assert member_step.depends_on_instance_of(list_)

        P = audit.prop("P")
        frame = audit.assume(audit.base(), P, "claimed_p")
        audit.register_claim(frame, P, "P?", "external_solver", "2.0")
        hyp_step = audit.hypothesis(frame)
        assert len(hyp_step.claims()) == 1
        print("Instance dependency tracked:", member_step.depends_on_instance_of(list_), "| attached claims:", len(hyp_step.claims()))


if __name__ == "__main__":
    main()

Run from the repository root PYTHONPATH=bindings/python:docs/examples/python/store pixi run python docs/examples/python/store/advanced_store_audit.py

Output

Library categories: 2 | dependents of basics.truth_refl: 1
Replaced derived.truth_pair and rechecked library: ok
Instance dependency tracked: True | attached claims: 1

Because theorems loaded via reopened.snapshot(budget) are rechecked from their proof rows by judgment_core, recovered.trusted_class_layer() is False and recovered.axioms() is empty.

Exercise

In advanced_store_audit, call current.change().withdraw("basics.truth_refl") immediately after checking len(deps) == 1. What error is raised?

Answer

StoreErrorKind.LIBRARY, because derived.truth_pair cites basics.truth_refl (as returned by current.dependents("basics.truth_refl")). Withdraw dependents first, or use replace(...) to update in place.

Next: 5.7 Exchange proofs with OpenTheory.

judgment v1.0.0 for Mojo 1.2 · Apache License 2.0 with LLVM Exceptions · Source on GitHub