Proof libraries & OpenTheory

Loading a library

This guide opens the current version of a proof-DAG library, rechecks its proof rows with the stateless checker, opens a Session over its snapshot, and imports or finds its theorems. See what a load lets you believe.

The call

var lib = Library.open(path)
var recheck = lib.recheck(budget, workers=1)
var snap = lib.snapshot(budget, workers=1)
var s = Session.open(snap)
  • Library.open(path, version=None) opens the directory's CURRENT version (or an ancestor, by its manifest hash) and checks its manifest without reading a partition. A library written before format 2.2 is refused with VERSION, a missing directory with MISSING_DEPENDENCY, and a version that is not CURRENT or an ancestor of it with FORMAT.
  • lib.recheck(budget, workers) loads every partition into a fresh bank over the library's profile and checks every row in parallel across workers threads; a row the checker refuses is LIBRARY (re-checking).
  • lib.snapshot(budget, workers) runs the same load and returns the bank as a Snapshot over the whole version for Session.open(snap).
  • lib.profile_snapshot(budget) returns a Snapshot holding only the core and the profile; a session opened over it imports individual theorems statement-only with session.import_theorem(lib, name) (or several with import_theorems(lib, names)), without loading the library. A statement-only import trusts the library version's producer for that statement (what a load lets you believe).

Composing file operations with IO

Library.open_io, create_io, is_current_io, snapshot_io, profile_snapshot_io, and recheck_io return deferred fp_mojo IO actions. Constructing an action performs no file access. Run it with fp.effects.async_runtime.run, or compose it with other IO actions. run returns an outcome containing the value, typed StoreError failures, or cancellation.

An open action without a version selects CURRENT when it runs. Actions created from an opened library retain that library's version and manifest; the partition bytes are read when the action runs. Load actions take an owned steps allowance and optional workers count. They retain no borrowed caller budget or session.

The synchronous methods above run the same implementations through IO and restore the supplied budget before returning or raising. Catalogue methods such as find and theorems operate on the already decoded manifest. Statement-only session imports remain synchronous: their IO leaves return owned manifest and declaration bytes, and the caller then mutates and checks its own workspace. A live session or writer is never sent to a file worker.

Work and workers

The load checks the committed rows in chunks, in parallel, with judgment_core's stateless checker over the bank. Its result is the same at every worker count (G1). The budget is charged for every byte and row read, and a refusal or an exhausted budget leaves the caller's session and the library unchanged (G9).

The manifest lists the partitions in load order. A load reads each one, checks its file hash and its blocks' digests, commits its rows after the earlier partitions' rows, then decides every theory and checks every row before it seals the bank (loading, for implementers). A file that the manifest names at several positions is loaded at each of them. One upcoming partition is read and hashed while the current one is processed. Blocks inflate and decode concurrently, then join in row order; the prior partition's tag prefix is shared without copying the whole library. Compressed and expanded block queues have byte bounds as well as worker bounds. File descriptors stay pinned until every read has joined.

Finding a theorem

Use lib.theorems(under=""), lib.categories(under=""), and lib.find(key) to browse or look up theorems by human-readable name or canonical judgment.stmt.<hex> statement identity. Once a session is opened over lib.snapshot(budget), session.import_fact(name) retrieves any theorem by its library name or canonical statement name.

Example

Mojo

load_library.mojo Download
"""Load a published proof-DAG library, recheck it, open a session snapshot, and import theorems."""
from std.tempfile import mkdtemp
from std.testing import assert_equal, assert_true
from judgment_shell import Session
from judgment_store import Library, StoreErrorKind
from termbank import Budget


def publish_example(path: String) raises:
    var lib = Library.create(path, "none")
    var budget = Budget(10000000)
    var s = Session.open(lib.profile_snapshot(budget))
    var t_fact = s.prove_step("basics.truth", s.truth(s.base()))
    var t_step = s.truth(s.base())
    var both_fact = s.prove_step("pairs.both", s.and_intro(t_step, t_step))
    var change = lib.change()
    change.add(s.export(t_fact, "basics.truth"))
    change.add(s.export(both_fact, "pairs.both"))
    _ = change.publish()


def main() raises:
    var path = String(mkdtemp(), "/lib")
    publish_example(path)

    # Open the library and independently recheck every stored proof row.
    var lib = Library.open(path)
    var budget = Budget(10000000)
    var check = lib.recheck(budget, workers=1)
    assert_equal(len(lib.theorems()), 2)
    assert_true(check.rows > 0)
    print("Rechecked", len(lib.theorems()), "theorems")

    # Look up a theorem by name or by canonical statement identity.
    var info = lib.find("pairs.both").value().copy()
    assert_true(Bool(lib.find(String("judgment.stmt.", info.sid))))
    assert_true(not lib.find("pairs.none"))
    print("Found pairs.both:", info.name, "| unknown absent:", not lib.find("pairs.none"))

    # Open a session over the full rechecked snapshot and cite the theorem.
    var snap = lib.snapshot(budget, workers=1)
    var s = Session.open(snap)
    var fact = s.import_fact("pairs.both")
    print("Cited from snapshot:", fact)

    # Opening a non-existent library directory fails with a typed StoreError.
    var refused = String("accepted")
    try:
        _ = Library.open(String(path, "/missing"))
    except error:
        refused = String(error.kind)
    assert_true(refused != "accepted")
    print("Missing library refused:", refused)

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

Output

Rechecked 2 theorems
Found pairs.both: pairs.both | unknown absent: True
Cited from snapshot: ⊢ True ∧ True
Missing library refused: MISSING_DEPENDENCY

Python

load_library.py Download
"""Load a published proof-DAG library, recheck it, open a session snapshot, and import theorems.

This example opens the library that `docs/examples/store/load_library.mojo`
publishes in `publish_example`, at the path in `JUDGMENT_EXAMPLE_LIBRARY` (the
documentation gate publishes it there). See `binary_roundtrip.py` to create and
publish a library from Python.
"""
import os

from judgment import Budget, Library, Session, StoreError


def main():
    path = os.environ["JUDGMENT_EXAMPLE_LIBRARY"]

    # Open the library and independently recheck every stored proof row.
    lib = Library.open(path)
    budget = Budget(10000000)
    check = lib.recheck(budget, workers=1)
    assert len(lib.theorems()) == 2
    assert check.rows > 0
    print("Rechecked", len(lib.theorems()), "theorems")

    # Look up a theorem by name or by canonical statement identity.
    info = lib.find("pairs.both")
    assert lib.find(f"judgment.stmt.{info.sid}") is not None
    assert lib.find("pairs.none") is None
    print("Found pairs.both:", info.name, "| unknown absent:", lib.find("pairs.none") is None)

    # Open a session over the full rechecked snapshot and cite the theorem.
    snap = lib.snapshot(budget, workers=1)
    s = Session.open(snap)
    fact = s.import_fact("pairs.both")
    print("Cited from snapshot:", fact)

    # Opening a non-existent library directory fails with a typed StoreError.
    refused = "accepted"
    try:
        Library.open(path + "/missing")
    except StoreError as error:
        refused = str(error.kind)
    assert refused != "accepted"
    print("Missing library refused:", refused)


main()

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

Output

Rechecked 2 theorems
Found pairs.both: pairs.both | unknown absent: True
Cited from snapshot: ⊢ True ∧ True
Missing library refused: MISSING_DEPENDENCY

See also

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