5.4 Save and reopen a binary library¶
Tutorial sequence · Previous: 5.3 Concurrent proof search
You will do: save a theorem with its proof, reopen a particular version, check its stored proof rows, and use it in a fresh session. The same workflow is available in Mojo and Python, including publication.
Enable Store¶
Store is the optional package for persistent libraries and OpenTheory files.
Build the Store variant of the prebuilt core once (libjudgment_store.so on
Linux, libjudgment_store.dylib on macOS) for both Mojo and Python:
pixi run build-core-store
- Mojo (thin prebuilt-core client): import
Library,Identity,Article, andBudgetdirectly fromjudgment, and run withpixi run python bindings/build_core.py --variant store --run PROGRAM.mojo. - Python: point
JUDGMENT_COREat the Store shared library (or let Python auto-detect it in.cache/prebuilt-core/lib/):
export JUDGMENT_CORE="$PWD/.cache/prebuilt-core/lib/libjudgment_store.so" # .dylib on macOS
export PYTHONPATH=bindings/python
The Mojo example source below also runs directly against src/ (pixi run build-store),
where Library and Identity come from judgment_store and Budget from termbank.
The language tabs below show the complete imports and commands.
From an in-memory fact to a saved version¶
We use the small equality True = True, proved by reflexivity. It needs no additional axiom and will also work in the next lesson's interchange format.
Mojo
"""Save a checked theorem and recover it in a fresh session."""
from std.tempfile import mkdtemp
from std.testing import assert_equal
from judgment import Session
from judgment_store import Identity, Library
from termbank import Budget
def main() raises:
# Use your own persistent directory to keep the library between runs.
var path = String(mkdtemp(), "/my_library")
var budget = Budget(10_000_000)
var library = Library.create(path, "none")
var s = Session.open(library.profile_snapshot(budget))
var proof = s.theorem("truth_refl", s.true_() == s.true_())
proof.rfl()
var theorem = proof.finish()
var change = library.change()
change.add(s.export(theorem, "basics.truth_refl"))
var version = change.publish()
print("Published theorems:", len(Library.open(path).theorems()))
# This part can run in another process, with only the directory and version.
var saved_version = String(version)
var reopened = Library.open(path, version=Optional(Identity.from_hex(saved_version)))
var loaded = Session.open(reopened.snapshot(budget))
var recovered = loaded.import_fact("basics.truth_refl")
assert_equal(reopened.version(), version)
assert_equal(len(recovered.axioms()), 0)
assert_equal(len(recovered.admissions()), 0)
print("Reopened:", recovered)
var reuse = loaded.theorem("reused", recovered.statement())
reuse.exact(recovered)
print("Reused:", reuse.finish())
Run from the repository root pixi run mojo run -I src docs/examples/store/binary_roundtrip.mojo
Output
Published theorems: 1
Reopened: ⊢ True = True
Reused: ⊢ True = True
Python
"""Save a checked theorem and recover it in a fresh session."""
from pathlib import Path
from tempfile import TemporaryDirectory
from judgment import Budget, Identity, Library, Session
def main():
# Use your own persistent directory to keep the library between runs.
with TemporaryDirectory() as directory:
path = str(Path(directory) / "my_library")
budget = Budget(10_000_000)
library = Library.create(path, "none")
s = Session.open(library.profile_snapshot(budget))
proof = s.theorem("truth_refl", s.true_() == s.true_())
proof.rfl()
theorem = proof.finish()
change = library.change()
change.add(s.export(theorem, "basics.truth_refl"))
version = change.publish()
print("Published theorems:", len(Library.open(path).theorems()))
# This part can run in another process, with only the directory and version.
saved_version = str(version)
reopened = Library.open(path, version=Identity.from_hex(saved_version))
loaded = Session.open(reopened.snapshot(budget))
recovered = loaded.import_fact("basics.truth_refl")
assert reopened.version() == version
assert len(recovered.axioms()) == 0
assert len(recovered.admissions()) == 0
print("Reopened:", recovered)
reuse = loaded.theorem("reused", recovered.statement())
reuse.exact(recovered)
print("Reused:", reuse.finish())
if __name__ == "__main__":
main()
Run from the repository root PYTHONPATH=bindings/python:docs/examples/python/store pixi run python docs/examples/python/store/binary_roundtrip.py
Output
Published theorems: 1
Reopened: ⊢ True = True
Reused: ⊢ True = True
The example uses a temporary directory to make repeated runs independent.
For real work, replace that path with a directory you intend to retain; the
Python example deletes its temporary directory on exit. A library is a directory
of versioned binary files, not a pickled Session or a single theorem file.
Follow the values through the first half of the program:
Library.create(path, "none")creates an empty library over the logic core. The profile namenonemeans no additional profile axioms. Open itsprofile_snapshot(budget)to author over the same foundation.proof.finish()produces a checked fact in the current session.s.export(theorem, "basics.truth_refl")collects the theorem's proof and dependencies into a portableTheoremExport. It does not write a file.library.change()starts a pending change, andchange.add(export)stages the theorem. Use dot-separated snake_case names for saved aliases.change.publish()replays and checks the exported proof, writes a complete version, and atomically advancesCURRENT. It returns the version identity.
The original library still describes the version it opened. Reopen the
directory to see the new version. If another writer publishes first, publication
refuses with CONFLICT; change.rebase() can update the pending change before
retrying. See editing libraries for
replacement and withdrawal.
Reopen and check before reuse¶
The second half can run in a different process. It needs the library directory,
the theorem's name, and, if reproducibility matters, the saved version identity.
Library.open(path) selects CURRENT; passing version selects that exact
version even after later publications. Save its 64-digit text alongside your
application's configuration; Identity.from_hex(saved_version) reconstructs the
version value in either language. It validates the spelling, not the library's
proofs.
Opening the catalog does not check every proof. reopened.snapshot(budget)
loads the version into a fresh checked bank and rechecks every proof row.
Session.open(snapshot) then gives the receiving session access to that checked
content. loaded.import_fact(name) retrieves the named fact from the snapshot;
it is ready for exact or other proof steps.
The loader checks the binary format and file identities, resolves declared dependencies, and checks proof rules and their reconstructed conclusions through the kernel. Content hashes detect changed bytes; kernel checking establishes the derivation under its recorded assumptions. Neither step proves that added axioms are true or consistent. Inspect dependencies as in Lesson 1.9.
library.recheck(budget) performs a full check and returns a report when you do
not need a session. Calling it before snapshot is unnecessary: snapshot
already checks the rows. A work limit is a refused or incomplete load, not a
successfully checked library. Budget counts work steps; it is not a RAM limit.
Choose the import behavior deliberately¶
| Workflow | What is checked or assumed |
|---|---|
Library.open |
Reads and verifies the version manifest; does not load and check all proof rows. |
snapshot → Session.open → import_fact |
Loads and checks every stored proof row before retrieving the fact. This lesson's path. |
profile_snapshot → Session.open → import_theorem(library, name) |
Loads the foundation, then imports selected statements and their dependency information. It trusts the producer's statements instead of replaying their stored proofs. |
The third route can be useful for selected imports, but it is a different
evidence path. An imported statement is not evidence that this receiving session
has checked the producer's derivation. Keep that distinction when reviewing
fact.imported() and dependency reports.
To publish a whole session snapshot, change.add_snapshot(snapshot, source,
budget) is available in both languages. It has stricter foundation and provenance
requirements and cannot share a change with other additions. Start with named
theorem exports until you need that broader operation.
Try it¶
After publishing, replace reopened.snapshot(budget) with
reopened.profile_snapshot(budget), leaving import_fact unchanged. Will it find
the saved theorem?
Explanation
No. A profile snapshot contains the foundation, not the library's theorem rows.
Use the full checked snapshot to recover the theorem by import_fact. Choosing
import_theorem instead would request the different, statement-only workflow
described above.