Part 5 — Concurrency, libraries & integration

5.3 Concurrent proof search and parallel lemma checking

Tutorial sequence · Previous: 5.2 Work budgets, proof forks, and cancellation

Goal: race candidate TacticData scripts across isolated Session.fork workspaces with deterministic lowest-index selection (first), and check a dependency DAG of lemmas concurrently (check_lemmas).

(Direct-package Mojo only. Build once with pixi run build-search.)

Concurrent proof search raises two hazards: shared mutable session state and schedule-dependent proof outcomes. judgment_search eliminates both:

  • Each candidate runs in an isolated Session.fork workspace with a fixed work allowance.
  • Candidates cross thread boundaries as plain TacticData (Lesson 2.4), never live session handles.
  • first(...) always selects the lowest-index succeeding candidate regardless of worker count or completion order, cancels higher-index workers, and replays the winning candidate deterministically into the parent proof.

Racing candidates with first

Three TacticData candidates target \(\exists y:T.\ R(y)\) given \(R(a)\):

  • Index 0: split() (returns no_match).
  • Index 1: witness(a) followed by exact("ra") (succeeds).
  • Index 2: witness(a) followed by assumption() (succeeds).

Even with workers=2, first deterministically selects index 1:

first.mojo Download
"""Run three candidate programs on forks of one goal and commit the lowest-index success."""

from fp.effects.async_runtime import IORuntimeConfig, Profile
from judgment_shell import ExprData, Session, TacticData, arrow, exists
from judgment_search import SearchLimits, first


def main() raises:
    var s = Session()
    var T = s.declare_type("T")
    var R = s.symbol("R", arrow(T, s.prop_type()))
    var proof = s.theorem("witnessed")
    var a = proof.var("a", T)
    _ = proof.given("ra", R(a))
    var y = proof.bound_var("y", T)
    proof.goal(exists(y, R(y)))

    # Candidates in priority order: the first does not match, the last two solve.
    var witness = TacticData.witness(ExprData.name("a"))
    var candidates: List[TacticData] = [TacticData.split(), witness.then(TacticData.exact("ra")), witness.then(TacticData.assumption())]

    # Each runs in its own fork with 1,000 units; two run at once. The lowest
    # index that succeeds is selected, whatever finishes first.
    var config = IORuntimeConfig()
    config.profile = Profile.current_thread()
    var result = first(proof, candidates, 1000, SearchLimits(workers=2), config)
    print("selected:", result.index(), result.status(), result.evidence())
    print("outcomes:", result.statuses()[0], result.statuses()[1])
    print(proof.finish())

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

Output

selected: 1 solved replay
outcomes: no_match solved
⊢ ∀a:T. R(a) → (∃y:T. R(y))

Before spawning forks, first verifies that total work across all candidate forks plus replay fits within the proof's remaining allowance, failing with LIMIT before forking if it does not.

Parallel lemma checking (check_lemmas)

check_lemmas(s, jobs, workers, config) executes each LemmaJob in its own fork once its declared dependencies finish, checks proofs concurrently, and registers finished theorems in deterministic job order:

lemmas.mojo Download
"""Check three lemmas in forks of one session at once and register them in order."""

from fp.effects.async_runtime import IORuntimeConfig, Profile
from judgment_shell import Expr, Fact, LibError, Proof, Session, TacticData, implies
from judgment_search import LemmaJob, check_lemmas


def st_identity(s: Session) raises LibError -> Expr:
    var P = s.import_symbol("P")
    return implies(P, P)


def st_constant(s: Session) raises LibError -> Expr:
    var P = s.import_symbol("P")
    var Q = s.import_symbol("Q")
    return implies(P, implies(Q, P))


def st_weakened(s: Session) raises LibError -> Expr:
    var P = s.import_symbol("P")
    var Q = s.import_symbol("Q")
    return implies(Q, implies(P, P))


def identity(mut proof: Proof, uses: List[Fact]) raises LibError:
    _ = proof.by(proof.compile(TacticData.intro("p").then(TacticData.exact("p"))))


def constant(mut proof: Proof, uses: List[Fact]) raises LibError:
    _ = proof.by(proof.compile(TacticData.intro("p").then(TacticData.intro("q")).then(TacticData.exact("p"))))


def weakened(mut proof: Proof, uses: List[Fact]) raises LibError:
    # The identity lemma is a declared dependency: it arrives as uses[0].
    _ = proof.intro("q")
    proof.exact(uses[0])


def main() raises:
    var s = Session()
    _ = s.prop("P")
    _ = s.prop("Q")
    var jobs: List[LemmaJob] = [
        LemmaJob("identity", st_identity, identity, 2000),
        LemmaJob("constant", st_constant, constant, 2000),
        LemmaJob("weakened", st_weakened, weakened, 2000, ["identity"]),
    ]
    var config = IORuntimeConfig()
    config.profile = Profile.current_thread()
    var report = check_lemmas(s, jobs, 2, config)
    var evidence = report.evidence()
    var facts = report.facts()
    for i in range(len(facts)):
        print(jobs[i].name, evidence[i], facts[i])
    print("registered:", s.fact("weakened"))

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

Output

identity replay ⊢ P → P
constant replay ⊢ P → Q → P
weakened replay ⊢ Q → P → P
registered: ⊢ Q → P → P

Here identity and constant execute concurrently, and weakened receives the checked Fact for identity via uses[0] upon completion. The search layer is untrusted; every registered theorem is checked by judgment_core.

See the judgment_search API reference for cancellation (StopSignal), budgets, and error-ordering semantics.

Exercise

In first.mojo, swap candidates 1 and 2 while keeping candidate 0 (split()). What index does first select, and does changing workers=1 to workers=4 change the result?

Answer

Index 1 (now the assumption() candidate). Changing workers does not alter the selected index or outcomes up to that index.

Next: 5.4 Save and reopen a binary library.

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