Part 1 — Core proofs

1.8 Inspect and repair a proof

Tutorial sequence · Previous: 1.7 Define names and track assumptions

Goal: save a proof checkpoint, diagnose a no_match tactic refusal after rewriting the wrong occurrence, restore the checkpoint, and close the goal.

Diagnosing an unsuccessful tactic

Given \(P = Q\) and the goal \(((P \land R) \lor (P \land R)) = ((P \land R) \lor (Q \land R))\), rewriting occurrence 1 of \(P\) is logically valid but leads to a state that rfl() cannot close.

Mojo

checkpoint_retry.mojo Download
"""Restore an exploratory rewrite and retry at a different occurrence."""

from std.testing import assert_true
from judgment import Session, implies


def main() raises:
    var s = Session()
    var P = s.prop("P")
    var Q = s.prop("Q")
    var R = s.prop("R")
    var start = (P & R) | (P & R)
    var wanted = (P & R) | (Q & R)
    var statement = implies(P == Q, start == wanted)
    var proof = s.theorem("retry_rewrite", statement)
    var equal = proof.intro("equal")
    var saved = proof.checkpoint()
    var t = proof.tactics()

    # This checked rewrite succeeds, but reflexivity cannot finish this route.
    _ = proof.lhs().at(P, occurrence=1).rw(equal)
    var trial = proof.by(t.rfl())
    assert_true(trial.status() == "no_match" and not trial.changed())
    assert_true(proof.target().same_as(((Q & R) | (P & R)) == wanted))
    print("First occurrence:", trial.status())

    # Undo the earlier direct rewrite; by only stages its own tactic program.
    proof.restore(saved)
    assert_true(proof.target().same_as(start == wanted))
    print("Restored original goal:", proof.target().same_as(start == wanted))

    # Restoration invalidates old foci, so select the occurrence afresh.
    _ = proof.lhs().at(P, occurrence=2).rw(equal)
    proof.rfl()
    var theorem = proof.finish()
    assert_true(theorem.statement().same_as(statement))
    assert_true(len(theorem.axioms()) == 0 and len(theorem.admissions()) == 0)
    print(theorem)
    print("Axiom dependencies:", len(theorem.axioms()))

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

Output

First occurrence: no_match
Restored original goal: True
⊢ P = Q → (P ∧ R ∨ P ∧ R) = (P ∧ R ∨ Q ∧ R)
Axiom dependencies: 0

Python

checkpoint_retry.py Download
"""Restore an exploratory rewrite and retry at a different occurrence."""

from judgment import Session, implies


def main():
    s = Session()
    P = s.prop("P")
    Q = s.prop("Q")
    R = s.prop("R")
    start = (P & R) | (P & R)
    wanted = (P & R) | (Q & R)
    statement = implies(P == Q, start == wanted)
    proof = s.theorem("retry_rewrite", statement)
    equal = proof.intro("equal")
    saved = proof.checkpoint()
    t = proof.tactics()

    # This checked rewrite succeeds, but reflexivity cannot finish this route.
    _ = proof.lhs().at(P, occurrence=1).rw(equal)
    trial = proof.by(t.rfl())
    assert trial.status() == "no_match" and not trial.changed()
    assert proof.target().same_as(((Q & R) | (P & R)) == wanted)
    print("First occurrence:", trial.status())

    # Undo the earlier direct rewrite; by only stages its own tactic program.
    proof.restore(saved)
    assert proof.target().same_as(start == wanted)
    print("Restored original goal:", proof.target().same_as(start == wanted))

    # Restoration invalidates old foci, so select the occurrence afresh.
    _ = proof.lhs().at(P, occurrence=2).rw(equal)
    proof.rfl()
    theorem = proof.finish()
    assert theorem.statement().same_as(statement)
    assert len(theorem.axioms()) == 0 and len(theorem.admissions()) == 0
    print(theorem)
    print("Axiom dependencies:", len(theorem.axioms()))


if __name__ == "__main__":
    main()

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

Output

First occurrence: no_match
Restored original goal: True
⊢ P = Q → (P ∧ R ∨ P ∧ R) = (P ∧ R ∨ Q ∧ R)
Axiom dependencies: 0
  1. proof.checkpoint() snapshots the current proof draft before the rewrite.
  2. Rewriting occurrence 1 succeeds; proof.by(t.rfl()) then returns a report with status no_match and trial.changed() == False. The earlier rewrite remains in effect on proof.target().
  3. proof.restore(saved) rewinds the proof draft to the checkpoint. Previously acquired focus handles become invalid and must be re-acquired.
  4. Rewriting with occurrence=2 produces a reflexive goal that rfl() closes.

A no_match report (or a LIMIT error) means that attempt did not close the goal; it does not establish that the goal is false.

Inspection API

Call Returns
proof.target().format() Proposition of the selected goal.
proof.format() All open and deferred goals with local contexts and status.
proof.obligations() Structured list of remaining goals.
proof.select(name) Focus handle for a named obligation.
fact.statement().format() Exact statement proved by a Fact.

See Troubleshooting for structured LibError / JudgmentError handling. Note that sorry() defers a goal (still blocking finish()), whereas admit(...) closes a goal by recording an explicit admission in admissions().

Exercise

Remove proof.restore(saved) while keeping the second rewrite. Why does the second rewrite fail?

Answer

no_match leaves the preceding rewrite intact. Only one P remains on the left-hand side, so occurrence=2 fails to match.

Next: 1.9 See what remains to prove.

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