Part 5 — Concurrency, libraries & integration

5.1 Scope work: transactions, snapshots, and forks

Tutorial sequence · Previous: 4.5 Natural numbers and primitive recursion

A session writes its rows into a termbank.dag workspace, which keeps uncommitted rows apart from the checked ones. Session scopes speculative work in three ways:

  • Session.transaction() opens a nested scope. commit(...) keeps the scope's work and returns a Retained with replacement handles for the expressions, types, facts, steps, declarations, definitions, and subsets you name, in the order you name them. Every other handle made inside the scope is stale after the commit. discard() returns the session to its state when the scope opened, and every handle made inside is stale. Transactions close innermost first.
  • Session.snapshot() records the session's current point as a TheorySnapshot; no transaction may be open. Session.checkout(snapshot) returns to that point and charges the work of restoring the session's tables; Session.rollback(snapshot) returns without the charge. Handles to rows made after the snapshot are then stale.
  • Session.fork(work) makes an independent child session with its own allowance work. The parent pays for copying its tables, and fork_cost() reports that charge. Neither session sees the other's later rows, and each refuses the other's handles. Session.merge(fork) commits the fork's work into its parent and closes the fork. The parent must not have made a declaration or a variable since the fork.

A failed session operation is atomic in every scope: the session's rows and registrations stay as they were. The work it spent stays spent.

Choose a mechanism

Mechanism Scope Use it for
transaction(), then commit or discard A nested scope of one session A candidate derivation, tactic attempt, or declaration that is either kept or leaves no trace
snapshot(), then checkout or rollback A point in the session's history, outside every transaction Returning to a known point after several exploratory steps
fork(work), then merge or drop An independent child session Isolated or concurrent exploration, as judgment_search does

The mechanisms combine with limits. snapshot, checkout, fork, and merge need every transaction closed. A fork made after a snapshot prevents a return to it: checkout and rollback then raise SHAPE.

Within one proof, Proof.checkpoint() and Proof.restore(checkpoint) save and restore a draft's goals and selection; see retry a targeted rewrite. To keep a session's work for other sessions, Session.seal() closes the session and returns a checked Snapshot, and Session.open(snapshot) starts a new session over it (work over a sealed snapshot). The termbank API reference describes Workspace isolation of uncommitted rows, and the proof-authoring guide covers scoped work and forks.

Example

This program commits a transaction that proves a theorem, keeping its proposition and fact. It checks that a handle made inside the committed transaction and one made inside a discarded transaction are refused. Last, it checks out the snapshot taken before either transaction, where the theorem does not exist:

Mojo

scoped_work.mojo Download
"""Retain checked results across transactions, discard scratch work, and check out snapshots."""

from std.testing import assert_true
from judgment import Session
from judgment_shell import LibErrorKind


def main() raises:
    var s = Session()
    var base = s.snapshot()

    var tx = s.transaction()
    var candidate = s.prop("candidate")
    var proof = s.theorem("candidate_refl", candidate == candidate)
    proof.rfl()
    var old_fact = proof.finish()

    var retained = tx.commit(expressions=[candidate], facts=[old_fact])
    var kept_term = retained.expressions()[0]
    var kept_fact = retained.facts()[0]
    var expected = kept_term == kept_term
    assert_true(kept_fact.statement().same_as(expected))
    assert_true(s.fact("candidate_refl").statement().same_as(expected))

    var old_rejected = False
    try:
        _ = old_fact.statement()
    except e:
        old_rejected = e.kind == LibErrorKind.KERNEL
    assert_true(old_rejected)

    var scratch = s.transaction()
    var temporary = s.prop("temporary")
    scratch.discard()

    var discarded_rejected = False
    try:
        _ = temporary.format()
    except e:
        discarded_rejected = e.kind == LibErrorKind.KERNEL
    assert_true(discarded_rejected)

    s.checkout(base)
    var rolled_back = False
    try:
        _ = s.fact("candidate_refl")
    except e:
        rolled_back = e.kind == LibErrorKind.NO_MATCH
    assert_true(rolled_back)
    print("Kept proof: candidate = candidate")
    print("Old transaction handle: rejected")
    print("Discarded transaction handle: rejected")
    print("Checked out base snapshot: candidate_refl absent")

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

Output

Kept proof: candidate = candidate
Old transaction handle: rejected
Discarded transaction handle: rejected
Checked out base snapshot: candidate_refl absent

Python

scoped_work.py Download
"""Retain checked results across transactions, discard scratch work, and check out snapshots."""

from judgment import Session, JudgmentError
from judgment import LibErrorKind


def main():
    s = Session()
    base = s.snapshot()

    tx = s.transaction()
    candidate = s.prop("candidate")
    proof = s.theorem("candidate_refl", candidate == candidate)
    proof.rfl()
    old_fact = proof.finish()

    retained = tx.commit(expressions=[candidate], facts=[old_fact])
    kept_term = retained.expressions()[0]
    kept_fact = retained.facts()[0]
    expected = kept_term == kept_term
    assert kept_fact.statement().same_as(expected)
    assert s.fact("candidate_refl").statement().same_as(expected)

    old_rejected = False
    try:
        _ = old_fact.statement()
    except JudgmentError as e:
        old_rejected = e.kind == LibErrorKind.KERNEL
    assert old_rejected

    scratch = s.transaction()
    temporary = s.prop("temporary")
    scratch.discard()

    discarded_rejected = False
    try:
        _ = temporary.format()
    except JudgmentError as e:
        discarded_rejected = e.kind == LibErrorKind.KERNEL
    assert discarded_rejected

    s.checkout(base)
    rolled_back = False
    try:
        _ = s.fact("candidate_refl")
    except JudgmentError as e:
        rolled_back = e.kind == LibErrorKind.NO_MATCH
    assert rolled_back
    print("Kept proof: candidate = candidate")
    print("Old transaction handle: rejected")
    print("Discarded transaction handle: rejected")
    print("Checked out base snapshot: candidate_refl absent")


if __name__ == "__main__":
    main()

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

Output

Kept proof: candidate = candidate
Old transaction handle: rejected
Discarded transaction handle: rejected
Checked out base snapshot: candidate_refl absent

The handles that commit returns are the ones to keep using: kept_fact proves kept_term = kept_term, while old_fact, made inside the transaction, is refused with KERNEL. The theorem stays registered by name, so s.fact("candidate_refl") finds it. temporary, made inside the discarded transaction, is refused with KERNEL. After checkout(base) returns the session to the first snapshot, looking up candidate_refl raises NO_MATCH.

Next: 5.2 Work budgets, proof forks, and cancellation.

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