Part 5 — Concurrency, libraries & integration

5.10 Check a symbolic transformation

Tutorial sequence · Previous: 5.9 Transport theorems between theories with interpretations

Suppose your application knows a = b and wants to replace f(a) with f(b). It can build the justification from checked rules, compare the result with the equation it expected, and save the resulting conditional theorem for later use. This example follows that whole path, including a rejected replacement and a transaction that keeps only the results it names.

You only need a type A, three constants of that type, and a function f: A → A. No arithmetic profile or new axiom is needed. Read your first proof first if sessions and proofs are new to you.

Mojo

certified_rewrite.mojo Download
"""Check a proposed substitution, preserve its guard, and retain a reusable theorem."""

from std.testing import assert_true
from judgment import Session, arrow, implies
from judgment_shell import LibErrorKind


def main() raises:
    var s = Session()
    var a_type = s.declare_type("A")
    var a = s.symbol("a", a_type)
    var b = s.symbol("b", a_type)
    var c = s.symbol("c", a_type)
    var f = s.symbol("f", arrow(a_type, a_type))

    var tx = s.transaction()
    var outer = s.base()
    var guard = a == b
    var assumed = s.assume(outer, guard, "ab")
    var input = f(a)
    var candidate = f(b)
    var expected = input == candidate

    # Check congruence under the guard hypothesis a = b.
    var same_function = s.eq_refl(assumed, f)
    var argument_equality = s.hypothesis(assumed)
    var checked = s.congruence(same_function, [argument_equality])
    assert_true(checked.statement().same_as(expected))
    assert_true(len(checked.hypotheses()) == 1)
    assert_true(checked.hypotheses()[0].same_as(guard))

    # The same evidence cannot justify a different replacement.
    var wrong = input == f(c)
    var rejected = False
    try:
        _ = s.convert(checked, wrong)
    except e:
        rejected = e.kind == LibErrorKind.KERNEL
    assert_true(rejected)

    var exported = s.imp_intro(assumed, checked)
    var conditional = implies(guard, expected)
    assert_true(exported.statement().same_as(conditional))
    assert_true(len(exported.hypotheses()) == 0)
    var saved = s.prove_step("rewrite_f", exported)
    var retained = tx.commit(expressions=[a, b, f], facts=[saved])

    # Use retained handles and look up the saved theorem; do not reuse old inner handles.
    var ra = retained.expressions()[0]
    var rb = retained.expressions()[1]
    var rf = retained.expressions()[2]
    var theorem = s.fact("rewrite_f")
    var context = s.base()
    var rule = s.use(context, theorem)
    var final_guard = ra == rb
    var final_expected = rf(ra) == rf(rb)
    assert_true(rule.statement().same_as(implies(final_guard, final_expected)))
    assert_true(len(theorem.axioms()) == 0)

    var premise = s.assume(context, final_guard, "guard")
    var result = s.imp_elim(s.lift_to(rule, premise), s.hypothesis(premise))
    assert_true(result.statement().same_as(final_expected))
    assert_true(len(result.hypotheses()) == 1)
    assert_true(result.hypotheses()[0].same_as(final_guard))
    print("Accepted: f(a) = f(b), under a = b")
    print("Rejected: the same evidence for f(a) = f(c)")
    print("Retained theorem: a = b implies f(a) = f(b)")
    print("Reuse still requires evidence for a = b")

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

Output

Accepted: f(a) = f(b), under a = b
Rejected: the same evidence for f(a) = f(c)
Retained theorem: a = b implies f(a) = f(b)
Reuse still requires evidence for a = b

Python

certified_rewrite.py Download
"""Check a proposed substitution, preserve its guard, and retain a reusable theorem."""

from judgment import Session, arrow, implies, JudgmentError
from judgment import LibErrorKind


def main():
    s = Session()
    a_type = s.declare_type("A")
    a = s.symbol("a", a_type)
    b = s.symbol("b", a_type)
    c = s.symbol("c", a_type)
    f = s.symbol("f", arrow(a_type, a_type))

    tx = s.transaction()
    outer = s.base()
    guard = a == b
    assumed = s.assume(outer, guard, "ab")
    input = f(a)
    candidate = f(b)
    expected = input == candidate

    # Check congruence under the guard hypothesis a = b.
    same_function = s.eq_refl(assumed, f)
    argument_equality = s.hypothesis(assumed)
    checked = s.congruence(same_function, [argument_equality])
    assert checked.statement().same_as(expected)
    assert len(checked.hypotheses()) == 1
    assert checked.hypotheses()[0].same_as(guard)

    # The same evidence cannot justify a different replacement.
    wrong = input == f(c)
    rejected = False
    try:
        _ = s.convert(checked, wrong)
    except JudgmentError as e:
        rejected = e.kind == LibErrorKind.KERNEL
    assert rejected

    exported = s.imp_intro(assumed, checked)
    conditional = implies(guard, expected)
    assert exported.statement().same_as(conditional)
    assert len(exported.hypotheses()) == 0
    saved = s.prove_step("rewrite_f", exported)
    retained = tx.commit(expressions=[a, b, f], facts=[saved])

    # Use retained handles and look up the saved theorem; do not reuse old inner handles.
    ra = retained.expressions()[0]
    rb = retained.expressions()[1]
    rf = retained.expressions()[2]
    theorem = s.fact("rewrite_f")
    context = s.base()
    rule = s.use(context, theorem)
    final_guard = ra == rb
    final_expected = rf(ra) == rf(rb)
    assert rule.statement().same_as(implies(final_guard, final_expected))
    assert len(theorem.axioms()) == 0

    premise = s.assume(context, final_guard, "guard")
    result = s.imp_elim(s.lift_to(rule, premise), s.hypothesis(premise))
    assert result.statement().same_as(final_expected)
    assert len(result.hypotheses()) == 1
    assert result.hypotheses()[0].same_as(final_guard)
    print("Accepted: f(a) = f(b), under a = b")
    print("Rejected: the same evidence for f(a) = f(c)")
    print("Retained theorem: a = b implies f(a) = f(b)")
    print("Reuse still requires evidence for a = b")


if __name__ == "__main__":
    main()

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

Output

Accepted: f(a) = f(b), under a = b
Rejected: the same evidence for f(a) = f(c)
Retained theorem: a = b implies f(a) = f(b)
Reuse still requires evidence for a = b

Specify what the application wants checked

The application constructs the input f(a), the candidate f(b), and the expected equation f(a) = f(b). The guard a = b is an explicit assumption: s.assume(outer, guard, "ab") extends the base frame with it and returns the new frame. Frames are the contexts of session steps: the base frame, a fixed variable, or an assumption.

An actual search engine would decide which replacement to propose. Here that choice is fixed so the example can concentrate on the checking boundary. The justification has three steps, each a checked rule:

  1. s.eq_refl(assumed, f) proves f = f in the assumption's frame.
  2. s.hypothesis(assumed) cites the available a = b.
  3. s.congruence combines the function equality with the argument equality into f(a) = f(b).

The resulting step proves precisely the expected equation, and its hypotheses() list the one assumption it uses. Asking for the same evidence to prove f(a) = f(c) is refused: s.convert changes a proved proposition only to a definitionally equal one, and a valid derivation of one result cannot satisfy a request for another.

Inspect and preserve the condition

s.imp_intro(assumed, checked) discharges the assumption frame. The statement becomes

\[ a = b \;\to\; f(a) = f(b). \]

The new step has no outstanding hypotheses because the condition is now in its proposition. It has not established a = b. The assertions check both the proposition and the hypothesis lists.

s.prove_step("rewrite_f", exported) publishes the closed step as a named theorem. Later, s.use(context, theorem) instantiates it, and its axioms() list is empty: the derivation used no admitted mathematical axiom. Its statement still needs the declared symbols and the logical vocabulary; this check is not permission to move the theorem into an unrelated session.

Keep the result and use it again

The work runs inside s.transaction(). Committing it with tx.commit(expressions=[a, b, f], facts=[saved]) keeps the theorem and returns replacement handles for exactly the expressions and facts named. Every other handle made inside the transaction, including the old frames and steps of the derivation, is stale after the commit. The application therefore reads the retained expressions from the Retained result and looks the theorem up by name; it does not reuse handles from inside the transaction. tx.discard() would instead drop everything made inside.

The final imp_elim combines the retained theorem with evidence for a = b. lift_to moves the instantiated theorem into the frame of a new assumption, whose hypothesis supplies that evidence, so the resulting equality still lists the assumption. In an application, the evidence could instead be a theorem already checked in the session.

Use this pattern when an optimizer, simplifier, or solver proposes a result: choose the expected claim, check the derivation, inspect its conditions, and retain the evidence your caller needs.

Next: 5.11 Check a proof against a promised conclusion.

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