Part 5 — Concurrency, libraries & integration

5.2 Work budgets, proof forks, and cancellation

Tutorial sequence · Previous: 5.1 Transactions, snapshots, and forks

Goal: inspect and manage deterministic session work allowances, isolate speculative proof attempts with Proof.fork, and cancel forked tactics with StopSignal.

Every input-dependent step in judgment charges a deterministic work counter (ground rule G3), bounding runtime independently of wall-clock timing.

Governance primitives

API Role
s.work_remaining() / s.spend(n) / s.add_work(n) Query remaining work units, deduct explicit units, or extend the allowance.
proof.fork_cost() / proof.fork(work, stop) Query fork copy cost, then create an isolated child Proof draft with its own allowance work and optional StopSignal.
StopSignal() / .request() / .requested() Cooperative cancellation flag. Once set, subsequent tactic or trial steps on forks holding the signal return status() == "resource_exhausted" with changed() == False.

Mojo

budgets_and_forks.mojo Download
"""Account for work budgets, isolate speculative proof forks, and cancel attempts with StopSignal."""

from std.testing import assert_equal, assert_false, assert_true
from judgment import Session, StopSignal, implies


def main() raises:
    var s = Session(work=200_000)
    var P = s.prop("P")
    var Q = s.prop("Q")

    # 1. Inspect, charge, and top up session work allowances.
    var before = s.work_remaining()
    s.spend(1_000)
    assert_equal(s.work_remaining(), before - 1_000)
    s.add_work(5_000)
    assert_equal(s.work_remaining(), before + 4_000)
    print("Budget accounting: spend and add_work exact")

    # 2. Fork a proof draft to explore an isolated branch without mutating the parent draft.
    var proof = s.theorem("swap", implies(P & Q, Q & P))
    var h = proof.intro("h")
    assert_true(proof.fork_cost() > 0)
    var branch = proof.fork(40_000)
    var bh = branch.fact("h")
    branch.exact(branch.conjoin(branch.right(bh), branch.left(bh)))
    assert_equal(len(branch.obligations()), 0)
    assert_equal(len(proof.obligations()), 1)
    print("Proof fork solved independently; parent obligations:", len(proof.obligations()))

    # 3. Cancel a forked proof cleanly with StopSignal.
    var stop = StopSignal()
    assert_false(stop.requested())
    var canceled = proof.fork(40_000, stop)
    stop.request()
    assert_true(stop.requested())
    var ct = canceled.tactics()
    var report = canceled.by(ct.split())
    assert_equal(report.status(), "resource_exhausted")
    assert_false(report.changed())
    print("Signaled fork status:", report.status(), "| changed:", report.changed())

    # The original proof draft remains intact and finishes cleanly.
    proof.exact(proof.conjoin(proof.right(h), proof.left(h)))
    print("Parent finished:", proof.finish())

Run from the repository root pixi run core-run docs/examples/library/budgets_and_forks.mojo

Output

Budget accounting: spend and add_work exact
Proof fork solved independently; parent obligations: 1
Signaled fork status: resource_exhausted | changed: False
Parent finished: ⊢ P ∧ Q → Q ∧ P

Python

budgets_and_forks.py Download
"""Account for work budgets, isolate speculative proof forks, and cancel attempts with StopSignal."""

from judgment import Session, StopSignal, implies


def main():
    s = Session(work=200_000)
    P = s.prop("P")
    Q = s.prop("Q")

    # 1. Inspect, charge, and top up session work allowances.
    before = s.work_remaining()
    s.spend(1_000)
    assert s.work_remaining() == before - 1_000
    s.add_work(5_000)
    assert s.work_remaining() == before + 4_000
    print("Budget accounting: spend and add_work exact")

    # 2. Fork a proof draft to explore an isolated branch without mutating the parent draft.
    proof = s.theorem("swap", implies(P & Q, Q & P))
    h = proof.intro("h")
    assert proof.fork_cost() > 0
    branch = proof.fork(40_000)
    bh = branch.fact("h")
    branch.exact(branch.conjoin(branch.right(bh), branch.left(bh)))
    assert len(branch.obligations()) == 0
    assert len(proof.obligations()) == 1
    print("Proof fork solved independently; parent obligations:", len(proof.obligations()))

    # 3. Cancel a forked proof cleanly with StopSignal.
    stop = StopSignal()
    assert not stop.requested()
    canceled = proof.fork(40_000, stop)
    stop.request()
    assert stop.requested()
    ct = canceled.tactics()
    report = canceled.by(ct.split())
    assert report.status() == "resource_exhausted"
    assert not report.changed()
    print("Signaled fork status:", report.status(), "| changed:", report.changed())

    # The original proof draft remains intact and finishes cleanly.
    proof.exact(proof.conjoin(proof.right(h), proof.left(h)))
    print("Parent finished:", proof.finish())


if __name__ == "__main__":
    main()

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

Output

Budget accounting: spend and add_work exact
Proof fork solved independently; parent obligations: 1
Signaled fork status: resource_exhausted | changed: False
Parent finished: ⊢ P ∧ Q → Q ∧ P

Key properties demonstrated:

  1. Deterministic accounting: spend and add_work modify work_remaining() by exact unit counts, independent of wall-clock time.
  2. Proof-fork isolation: closing branch discharges all obligations on branch without altering the parent proof draft (which retains its open obligation).
  3. Atomic cancellation: after stop.request(), canceled.by(ct.split()) returns status() == "resource_exhausted" and changed() == False, leaving both canceled and the parent proof unchanged.

In Lesson 5.3, judgment_search builds on Proof.fork and StopSignal to race candidate tactics across threads and cancel higher-index workers once a lower-index candidate succeeds.

Exercise

In budgets_and_forks, move stop.request() after canceled.by(ct.split()). What status does report.status() return?

Answer

"progress" with changed() == True, splitting Q ∧ P into subgoals Q and P.

Next: 5.3 Concurrent proof search and parallel lemma checking.

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