Part 2 — Structuring proofs & automation

2.4 Tactics, proof holes, and theorem discovery

Tutorial sequence · Previous: 2.3 Conditional rewrites and calculations

Goal: compose atomic tactic pipelines, construct session-free TacticData search candidates, refine partial proof terms containing holes, and query applicable lemmas with suggest.

Composing tactics

proof.tactics() returns a Tactics factory producing composable Tactic values. Chain steps with .then(...), enforce goal closure with .require_closed(), and execute with proof.by(...):

Mojo

composed_tactics.mojo Download
"""Compose explicit tactics and inspect their outcome."""

from judgment import Session, implies


def main() raises:
    var s = Session()
    var proof = s.theorem("duplicate")
    var P = proof.prop("P")
    proof.goal(implies(P, P & P))
    var t = proof.tactics()
    var report = proof.by(t.intro().then(t.split()).then(t.assumption()).require_closed())
    print(report.status())
    print(proof.finish())

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

Output

solved
⊢ ∀P:Prop. P → P ∧ P

Python

composed_tactics.py Download
"""Compose explicit tactics and inspect their outcome."""

from judgment import Session, implies


def main():
    s = Session()
    proof = s.theorem("duplicate")
    P = proof.prop("P")
    proof.goal(implies(P, P & P))
    t = proof.tactics()
    report = proof.by(t.intro().then(t.split()).then(t.assumption()).require_closed())
    print(report.status())
    print(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/composed_tactics.py

Output

solved
⊢ ∀P:Prop. P → P ∧ P

t.intro().then(t.split()).then(t.assumption()).require_closed() executes atomically: if it fails to close the goal, the proof state is unchanged. report.status() reports "solved", "progress", "no_change", or "no_match".

Session-free data tactics (GoalData and TacticData)

To separate strategy selection (in a planner, search loop, or worker thread) from execution inside a Session:

  • proof.current_goal().data() extracts a plain GoalData snapshot (target, variables, premises, path, and revision token) with no session handle.
  • TacticData encodes tactic scripts as plain data (TacticData.split(), TacticData.witness(ExprData.name("a")), TacticData.exact("ra"), chained with .then(...)).
  • proof.trial(proof.compile(...)) evaluates a compiled candidate without mutating the proof state.
  • proof.by(candidate) commits a Candidate, checking its path and revision token to reject stale candidates.

Mojo

tactic_data.mojo Download
"""Describe a goal as data, propose two candidate programs, try both, and commit the one that solves."""

from judgment import Candidate, ExprData, Session, TacticData, TypeData, arrow, exists, implies


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)))

    # A producer sees only data: the goal's path, revision, target, and context.
    var goal = proof.current_goal().data()
    print(goal.target().format(), goal.variables()[0], goal.premises()[0])

    # Two candidate programs, built without any session reference.
    var first = TacticData.split().then(TacticData.assumption())
    var second = TacticData.witness(ExprData.name("a")).then(TacticData.exact("ra"))
    var candidates = [Candidate(goal.path(), goal.revision(), first), Candidate(goal.path(), goal.revision(), second)]

    # Trials report without changing the draft; the first that solves is committed.
    for candidate in candidates:
        var report = proof.trial(proof.compile(candidate.tactic()))
        print(report.status())
        if report.status() == "solved":
            print(proof.by(candidate).status())
            break
    print(proof.finish())

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

Output

∃y:T. R(y) a ra
no_match
solved
solved
⊢ ∀a:T. R(a) → (∃y:T. R(y))

Python

tactic_data.py Download
"""Describe a goal as data, propose two candidate programs, try both, and commit the one that solves."""

from judgment import Candidate, ExprData, Session, TacticData, TypeData, arrow, exists, implies


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

    # A producer sees only data: the goal's path, revision, target, and context.
    goal = proof.current_goal().data()
    print(goal.target().format(), goal.variables()[0], goal.premises()[0])

    # Two candidate programs, built without any session reference.
    first = TacticData.split().then(TacticData.assumption())
    second = TacticData.witness(ExprData.name("a")).then(TacticData.exact("ra"))
    candidates = [Candidate(goal.path(), goal.revision(), first), Candidate(goal.path(), goal.revision(), second)]

    # Trials report without changing the draft; the first that solves is committed.
    for candidate in candidates:
        report = proof.trial(proof.compile(candidate.tactic()))
        print(report.status())
        if report.status() == "solved":
            print(proof.by(candidate).status())
            break
    print(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/tactic_data.py

Output

∃y:T. R(y) a ra
no_match
solved
solved
⊢ ∀a:T. R(a) → (∃y:T. R(y))

The same data boundary underlies deterministic concurrent search in Lesson 5.3.

Partial proof recipes and holes

To supply part of a proof term while leaving subgoals as named holes, use recipe(...), hole(name, target), and proof.refine(...):

Mojo

partial_recipes.mojo Download
"""Mix direct evidence with named proof holes."""

from judgment import Session, hole, implies, recipe


def main() raises:
    var s = Session()
    var proof = s.theorem("and_swap")
    var parameters = proof.props("P Q")
    var P, Q = parameters[0], parameters[1]
    proof.goal(implies(P & Q, Q & P))
    var h = proof.intro()

    var holes = proof.refine(recipe(proof.right(h)).conjoin(hole("prove_P", P)))
    var missing = holes["prove_P"]
    missing.exact(proof.left(h))
    print(proof.finish())

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

Output

⊢ ∀P:Prop. ∀Q:Prop. P ∧ Q → Q ∧ P

Python

partial_recipes.py Download
"""Mix direct evidence with named proof holes."""

from judgment import Session, hole, implies, recipe


def main():
    s = Session()
    proof = s.theorem("and_swap")
    parameters = proof.props("P Q")
    P, Q = parameters[0], parameters[1]
    proof.goal(implies(P & Q, Q & P))
    h = proof.intro()

    holes = proof.refine(recipe(proof.right(h)).conjoin(hole("prove_P", P)))
    missing = holes["prove_P"]
    missing.exact(proof.left(h))
    print(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/partial_recipes.py

Output

⊢ ∀P:Prop. ∀Q:Prop. P ∧ Q → Q ∧ P

proof.refine(...) checks that the partial recipe matches the target and returns a map from hole names (here "prove_P") to live Proof subgoals.

Theorem discovery (suggest)

proof.suggest(prefix) scans registered theorems matching prefix, checks applicability against the current goal without modifying proof state, and returns ranked Suggestion values listing each lemma's name and remaining premises:

Mojo

theorem_discovery.mojo Download
"""Inspect a theorem application before selecting it."""

from judgment import Session


def main() raises:
    var s = Session()
    var identity = s.theorem("identity_refl")
    var A = identity.type_var("A")
    var x = identity.var("x", A)
    identity.goal(x == x)
    identity.rfl()
    _ = identity.finish()

    var P = s.prop("P")
    var proof = s.theorem("discovered", P == P)
    var candidates = proof.suggest("identity")
    print("Candidate:", candidates[0].name())
    print("Remaining premises:", len(candidates[0].premises()))
    _ = proof.apply(candidates[0])
    print(proof.finish())

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

Output

Candidate: identity_refl
Remaining premises: 0
⊢ P = P

Python

theorem_discovery.py Download
"""Inspect a theorem application before selecting it."""

from judgment import Session


def main():
    s = Session()
    identity = s.theorem("identity_refl")
    A = identity.type_var("A")
    x = identity.var("x", A)
    identity.goal(x == x)
    identity.rfl()
    _ = identity.finish()

    P = s.prop("P")
    proof = s.theorem("discovered", P == P)
    candidates = proof.suggest("identity")
    print("Candidate:", candidates[0].name())
    print("Remaining premises:", len(candidates[0].premises()))
    _ = proof.apply(candidates[0])
    print(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/theorem_discovery.py

Output

Candidate: identity_refl
Remaining premises: 0
⊢ P = P

Pass the selected suggestion to proof.apply(candidates[0]); suggest never modifies the proof on its own.

Exercise

In tactic_data, reverse the order of first and second in candidates. How many trial calls execute before proof.by(candidate) commits?

Answer

One. When the solving candidate is tested first, proof.trial(...) returns "solved", proof.by(candidate) commits immediately, and the loop exits.

Next: 2.5 Tactic combinators and deferred rewrites.

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