Proof libraries & OpenTheory

Publishing a library

This guide creates a proof-DAG library (format 2.2), exports checked theorems from a Session, publishes versions atomically, and handles concurrent writers. Read what a proof library is first for the terms.

The call

var lib = Library.create(path, "none")
var change = lib.change()
change.add(session.export(fact, "basics.truth"))
var version = change.publish()

Library.create(path, profile) initializes a library directory over the named profile ("none", "classical", or "isabelle") and publishes its first version, which provides the profile's statements and has no theorem of its own. It refuses a directory that already has CURRENT (CONFLICT).

Declaring theorems and assumptions

Open a Session over lib.profile_snapshot(budget) (or lib.snapshot(budget) to build on existing theorems), prove theorems with the session's checked rules or tactics, and export each closed theorem with session.export(fact, name). An export carries the theorem's proof rows and explicit references to what it imports; no raw bank or storage handle crosses the API boundary, and publication checks every row again.

Publishing changes

lib.change() returns a LibraryChange anchored at lib.version():

  • change.add(export) adds an exported theorem from a Session.
  • change.add_article(path, source="") converts and adds an OpenTheory article. A change adds articles or session theorems, not both.
  • change.add_snapshot(snapshot, source, budget) adds a whole sealed snapshot, such as scalar.build_library's, to a library with no declarations of its own over the snapshot's profile ("none" for one built over the logic core). See publishing a snapshot.
  • change.replace(name, export) replaces an existing theorem's proof.
  • change.withdraw(key) withdraws a theorem or axiom that no declaration of the version cites (LIBRARY otherwise).
  • change.publish() writes the new partitions and the format-2.2 manifest and makes the new version CURRENT, only if CURRENT is still change.base() (otherwise it refuses with CONFLICT).
  • change.rebase() moves a conflicted change onto the current version.

Publication is deterministic (G1): the same changes on the same base produce the same files and the same version identity. Asking whether a statement is already provided (Rule L1) writes nothing, so the files do not depend on which statements a session or a conversion looked up.

Deferred publication

change^.publish_io() consumes the pending change and returns a deferred IO action. Its LibraryChangeResult holds both the updated change and a result field (Result[Identity, StoreError]). A publication conflict or other domain refusal returns the pending change, so the application can inspect the refusal, recover it with completed^.into_change(), and call rebase_io. add_snapshot_io similarly returns the pending state and remaining work allowance. It captures an immutable view of the supplied snapshot when constructed; transcription and checking run inside IO.

The synchronous publish, rebase, and add_snapshot methods execute the same operation inside a current-thread IO host, restoring the pending state before reporting a refusal. Neither API changes the atomic CURRENT update or the kernel checks described above.

An unopened owned action may be dropped without writing anything. Canceling it before execution drops its pending change. Once blocking publication starts, cancellation waits for it to finish; it cannot undo a version that has already been committed. An application that needs the publication result for recovery should allow that action to complete and inspect its returned state.

Partition output uses bounded parallel compression and hashing. Installation of the completed partition overlaps encoding of the next one. All writes join before metadata publication and the atomic CURRENT update.

Publishing a snapshot

A theory built by a program, rather than proved theorem by theorem, is published whole:

var built = build_library(profile, budget, poly)
var lib = Library.create(path, "none")
var change = lib.change()
change.add_snapshot(built.snapshot, "judgment|scalar|x6|library", budget)
var version = change.publish()

Every declaration past the core goes into the version, in order: types, constants, axioms, definitions, ACU operators, literal kinds and theorems with their proofs. As in any library, axioms and theorems are written under their statements' canonical names, and their own names become aliases, normalized when they are not theorem names (scalar.G1 is found as scalar.g1). The same build gives the same version. The rows are transcribed into a fresh bank and checked there when add_snapshot runs, and checked again by every load, so nothing of the snapshot is trusted. source labels the snapshot's axioms; it is required when there are any. The change holds nothing else, and the base must have no declarations of its own.

After a load (lib.snapshot(budget)), the snapshot's axioms are the library's own axioms. Axioms that were PROFILE_AXIOM in the snapshot, such as Scalar's G1–G7 and the classical principles it declares, are not PROFILE_AXIOM in a session over the loaded version (Definition E-1): the writer records their class only as a provenance label, and a load does not turn a label into a classification. Their statements, and the axioms each theorem rests on (Fact.axioms), are unchanged.

Naming theorems

A theorem's name is a dotted name of snake_case components, such as basics.truth or pairs.both (valid_theorem_name). The components form the library's catalogue: lib.categories(under=...) and lib.theorems(under=...) list it by category from the manifest alone, without reading a partition (changing a library).

Example

publish_library.mojo Download
"""Publish theorems into a proof-DAG library and verify deterministic versions and conflict checks."""
from std.tempfile import mkdtemp
from std.testing import assert_equal, assert_true
from judgment_shell import Session
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")

    # Author two checked theorems over the library's profile snapshot and export them.
    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 first_change = lib.change()
    first_change.add(s.export(t_fact, "basics.truth"))
    first_change.add(s.export(both_fact, "pairs.both"))
    var first = first_change.publish()
    print("Published version 1 with", len(Library.open(path).theorems()), "theorems")

    # Publication is deterministic: the same exports yield the same version identity.
    var twin_path = String(mkdtemp(), "/twin")
    var twin = Library.create(twin_path, "none")
    var twin_change = twin.change()
    twin_change.add(s.export(t_fact, "basics.truth"))
    twin_change.add(s.export(both_fact, "pairs.both"))
    var same = twin_change.publish()
    print("Same exports, same version:", same == first)

    # A stale change built on the initial version is refused with CONFLICT.
    var stale = lib.change()
    stale.add(s.export(t_fact, "basics.extra"))
    var kind = String("accepted")
    try:
        _ = stale.publish()
    except error:
        kind = String(error.kind)
    assert_equal(kind, "CONFLICT")
    print("Publishing from a stale base:", kind)

    # Rebasing onto CURRENT succeeds and advances CURRENT.
    stale.rebase()
    var second = stale.publish()
    var current: String
    with open(String(path, "/CURRENT"), "r") as f:
        current = f.read()
    assert_true(not (second == first))
    print("CURRENT names version 2:", current == String(second, "\n"))

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

Output

Published version 1 with 2 theorems
Same exports, same version: True
Publishing from a stale base: CONFLICT
CURRENT names version 2: True

See also

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