Part 2 — Structuring proofs & automation

2.3 Conditional rewrites and deferred calculations

Tutorial sequence · Previous: 2.2 Structure larger proofs

Goal: apply conditional equality rules with explicit side conditions, defer calculation steps as subgoals, and share a deferred lemma across branches.

Conditional and deferred calculation steps

Given \(f : A \to A\), \(\forall t:A.\; D(t) \to f(t) = t\), \(D(f(x))\), and \(D(x)\), we prove \(f(f(x)) = x\) via

\[ f(f(x)) = f(x) = x \]

using both immediate and deferred steps:

  1. Immediate conditional step (chain.eq): supplies using=law and premises=[inside] inline; matching instantiates \(t := f(x)\) and checks inside : D(f(x)) immediately.
  2. Deferred calculation step (chain.step): declares the next expression to=x and returns a Proof handle cancel for \(f(x) = x\). Applying law backward spawns the side condition \(D(x)\) as an explicit subgoal.

Mojo

conditional_calculations.mojo Download
"""Keep conditional calculation steps and their required evidence explicit."""

from std.testing import assert_equal
from judgment import Session, arrow, forall, implies


def main() raises:
    var s = Session()
    var proof = s.theorem("cancel_twice")
    var A = proof.type_var("A")
    var f = proof.var("f", arrow(A, A))
    var D = proof.var("D", arrow(A, s.prop_type()))
    var t = proof.bound_var("t", A)
    var law = proof.given("fix_on_D", forall(t, implies(D(t), f(t) == t)))
    var x = proof.var("x", A)
    var inside = proof.given("inside", D(f(x)))
    var at_x = proof.given("at_x", D(x))
    proof.goal(f(f(x)) == x)

    var chain = proof.calc(f(f(x)))
    # The endpoints and supplied condition determine the rule's arguments.
    chain.eq(
        f(x),
        using=law,
        premises=[inside],
        label="fix at f(x)",
    )

    # Apply infers x from the equality and exposes D(x) as a separate goal.
    var cancel = chain.step("cancel", to=x)
    var premises = cancel.apply(law)
    assert_equal(len(premises), 1)
    assert_equal(premises[0].target().same_as(D(x)), True)
    premises[0].sorry()
    print(chain.format())
    var remaining = chain.obligations()
    assert_equal(len(remaining), 1)
    print("Required premise:", remaining[0].target().format())

    # Having D(x) in the context does not discharge it automatically.
    var domain = chain.select(remaining[0])
    domain.resume()
    domain.exact(at_x)
    assert_equal(len(chain.obligations()), 0)
    proof.exact(chain.finish())
    var theorem = proof.finish()
    assert_equal(len(theorem.axioms()), 0)
    assert_equal(len(theorem.admissions()), 0)
    print(theorem)

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

Output

f(f(x))
= f(x) [fix at f(x): checked]
= x [cancel: deferred]
Required premise: D(x)
[A] ⊢ ∀f:(A → A). ∀D:(A → Prop). (∀t:A. D(t) → f(t) = t) → (∀x:A. D(f(x)) → D(x) → f(f(x)) = x)

Python

conditional_calculations.py Download
"""Keep conditional calculation steps and their required evidence explicit."""

from judgment import Session, arrow, forall, implies


def main():
    s = Session()
    proof = s.theorem("cancel_twice")
    A = proof.type_var("A")
    f = proof.var("f", arrow(A, A))
    D = proof.var("D", arrow(A, s.prop_type()))
    t = proof.bound_var("t", A)
    law = proof.given("fix_on_D", forall(t, implies(D(t), f(t) == t)))
    x = proof.var("x", A)
    inside = proof.given("inside", D(f(x)))
    at_x = proof.given("at_x", D(x))
    proof.goal(f(f(x)) == x)

    chain = proof.calc(f(f(x)))
    # The endpoints and supplied condition determine the rule's arguments.
    chain.eq(
        f(x),
        using=law,
        premises=[inside],
        label="fix at f(x)",
    )

    # Apply infers x from the equality and exposes D(x) as a separate goal.
    cancel = chain.step("cancel", to=x)
    premises = cancel.apply(law)
    assert len(premises) == 1
    assert premises[0].target().same_as(D(x)) == True
    premises[0].sorry()
    print(chain.format())
    remaining = chain.obligations()
    assert len(remaining) == 1
    print("Required premise:", remaining[0].target().format())

    # Having D(x) in the context does not discharge it automatically.
    domain = chain.select(remaining[0])
    domain.resume()
    domain.exact(at_x)
    assert len(chain.obligations()) == 0
    proof.exact(chain.finish())
    theorem = proof.finish()
    assert len(theorem.axioms()) == 0
    assert len(theorem.admissions()) == 0
    print(theorem)


if __name__ == "__main__":
    main()

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

Output

f(f(x))
= f(x) [fix at f(x): checked]
= x [cancel: deferred]
Required premise: D(x)
[A] ⊢ ∀f:(A → A). ∀D:(A → Prop). (∀t:A. D(t) → f(t) = t) → (∀x:A. D(f(x)) → D(x) → f(f(x)) = x)

Even when at_x : D(x) is already in scope, cancel.apply(law) does not discharge it implicitly. domain.exact(at_x) closes the obligation explicitly, and chain.finish() verifies all step obligations before returning equality evidence.

Conditional rewrites across nested branches

In the next example, \(f : A \to B\) has two partial left inverses: \(g\) on domain \(D\) and \(h\) on domain \(E\). On either domain, \(f(x) = f(y) \leftrightarrow x = y\). Both backward directions use \(x = y \to f(x) = f(y)\), which is introduced once as a deferred have claim, while a host helper function drives both domain branches via rw(rule, using=[...]) (and reverse=True):

Mojo

conditional_rewrites.mojo Download
"""Use partial inverses in nested branches with a shared deferred claim."""

from std.testing import assert_equal
from judgment_shell import Expr, LibError, Proof, Session, arrow, forall, iff, implies


def cancel_on_domain(mut proof: Proof, inverse_name: String, domain_name: String, image: Expr) raises LibError:
    """Prove the forward implication using both explicit domain conditions."""
    var same_image = proof.intro("same_image")
    var inverse = proof.fact(inverse_name)
    var domain = proof.fact(domain_name)
    var in_domain_x = proof.left(domain)
    var in_domain_y = proof.right(domain)

    _ = proof.lhs().rw(inverse, reverse=True, using=[in_domain_x])
    _ = proof.lhs().at(image).rw(same_image)
    _ = proof.lhs().rw(inverse, using=[in_domain_y])
    proof.rfl()


def main() raises:
    var s = Session()
    var proof = s.theorem("partial_inverses")
    var A = proof.type_var("A")
    var B = proof.type_var("B")
    var f = proof.var("f", arrow(A, B))
    var g = proof.var("g", arrow(B, A))
    var h = proof.var("h", arrow(B, A))
    var D = proof.var("D", arrow(A, s.prop_type()))
    var E = proof.var("E", arrow(A, s.prop_type()))
    var t = proof.bound_var("t", A)
    _ = proof.given("inverse_on_D", forall(t, implies(D(t), g(f(t)) == t)))
    _ = proof.given("inverse_on_E", forall(t, implies(E(t), h(f(t)) == t)))
    var x = proof.var("x", A)
    var y = proof.var("y", A)
    var equivalent = iff(f(x) == f(y), x == y)
    proof.goal(implies(D(x) & D(y), equivalent) & implies(E(x) & E(y), equivalent))

    # Both backward directions will use this claim. It remains an obligation.
    var reverse = proof.have("same_inputs_same_image", implies(x == y, f(x) == f(y)))
    reverse.sorry()

    _ = proof.split()
    var on_d = proof.select("left")
    var on_e = proof.select("right")
    _ = on_d.intro("in_D")
    _ = on_e.intro("in_E")
    _ = on_d.split()
    _ = on_e.split()
    assert_equal(len(proof.obligations()), 5)
    for goal in proof.obligations():
        print(goal.path(), goal.status(), ":", goal.target().format())

    # Complete the right domain first; the left paths remain unchanged.
    var forward = proof.select(["right", "forward"])
    cancel_on_domain(forward, "inverse_on_E", "in_E", f(x))
    var backward = proof.select(["right", "backward"])
    backward.exact(backward.fact("same_inputs_same_image"))

    forward = proof.select(["left", "forward"])
    var selected = forward.current_goal()
    assert_equal(selected.target().same_as(implies(f(x) == f(y), x == y)), True)
    cancel_on_domain(forward, "inverse_on_D", "in_D", f(x))
    backward = proof.select(["left", "backward"])
    backward.exact(backward.fact("same_inputs_same_image"))

    var remaining = proof.obligations()
    assert_equal(len(remaining), 1)
    assert_equal(remaining[0].status(), "deferred")
    print("Remaining:", remaining[0].name(), "[", remaining[0].status(), "]")
    var recovered = proof.select("same_inputs_same_image")
    assert_equal(recovered.current_goal().status(), "deferred")
    recovered.resume()
    var equal_inputs = recovered.intro("equal_inputs")
    _ = recovered.lhs().at(x).rw(equal_inputs)
    recovered.rfl()

    var theorem = proof.finish()
    assert_equal(len(theorem.axioms()), 0)
    assert_equal(len(theorem.admissions()), 0)
    print(theorem)

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

Output

[claim, same_inputs_same_image] deferred : x = y → f(x) = f(y)
[left, forward] open : f(x) = f(y) → x = y
[left, backward] open : x = y → f(x) = f(y)
[right, forward] open : f(x) = f(y) → x = y
[right, backward] open : x = y → f(x) = f(y)
Remaining: same_inputs_same_image [ deferred ]
[A, B] ⊢ ∀f:(A → B). ∀g:(B → A). ∀h:(B → A). ∀D:(A → Prop). ∀E:(A → Prop). (∀t:A. D(t) → g(f(t)) = t) → (∀t:A. E(t) → h(f(t)) = t) → (∀x:A. ∀y:A. (D(x) ∧ D(y) → (f(x) = f(y) ↔ x = y)) ∧ (E(x) ∧ E(y) → (f(x) = f(y) ↔ x = y)))

Python

conditional_rewrites.py Download
"""Use partial inverses in nested branches with a shared deferred claim."""

from judgment import Expr, JudgmentError, Proof, Session, arrow, forall, iff, implies


def cancel_on_domain(proof, inverse_name, domain_name, image):
    """Prove the forward implication using both explicit domain conditions."""
    same_image = proof.intro("same_image")
    inverse = proof.fact(inverse_name)
    domain = proof.fact(domain_name)
    in_domain_x = proof.left(domain)
    in_domain_y = proof.right(domain)

    _ = proof.lhs().rw(inverse, reverse=True, using=[in_domain_x])
    _ = proof.lhs().at(image).rw(same_image)
    _ = proof.lhs().rw(inverse, using=[in_domain_y])
    proof.rfl()


def main():
    s = Session()
    proof = s.theorem("partial_inverses")
    A = proof.type_var("A")
    B = proof.type_var("B")
    f = proof.var("f", arrow(A, B))
    g = proof.var("g", arrow(B, A))
    h = proof.var("h", arrow(B, A))
    D = proof.var("D", arrow(A, s.prop_type()))
    E = proof.var("E", arrow(A, s.prop_type()))
    t = proof.bound_var("t", A)
    _ = proof.given("inverse_on_D", forall(t, implies(D(t), g(f(t)) == t)))
    _ = proof.given("inverse_on_E", forall(t, implies(E(t), h(f(t)) == t)))
    x = proof.var("x", A)
    y = proof.var("y", A)
    equivalent = iff(f(x) == f(y), x == y)
    proof.goal(implies(D(x) & D(y), equivalent) & implies(E(x) & E(y), equivalent))

    # Both backward directions will use this claim. It remains an obligation.
    reverse = proof.have("same_inputs_same_image", implies(x == y, f(x) == f(y)))
    reverse.sorry()

    _ = proof.split()
    on_d = proof.select("left")
    on_e = proof.select("right")
    _ = on_d.intro("in_D")
    _ = on_e.intro("in_E")
    _ = on_d.split()
    _ = on_e.split()
    assert len(proof.obligations()) == 5
    for goal in proof.obligations():
        print("[" + ", ".join(goal.path()) + "]", goal.status(), ":", goal.target().format())

    # Complete the right domain first; the left paths remain unchanged.
    forward = proof.select(["right", "forward"])
    cancel_on_domain(forward, "inverse_on_E", "in_E", f(x))
    backward = proof.select(["right", "backward"])
    backward.exact(backward.fact("same_inputs_same_image"))

    forward = proof.select(["left", "forward"])
    selected = forward.current_goal()
    assert selected.target().same_as(implies(f(x) == f(y), x == y)) == True
    cancel_on_domain(forward, "inverse_on_D", "in_D", f(x))
    backward = proof.select(["left", "backward"])
    backward.exact(backward.fact("same_inputs_same_image"))

    remaining = proof.obligations()
    assert len(remaining) == 1
    assert remaining[0].status() == "deferred"
    print("Remaining:", remaining[0].name(), "[", remaining[0].status(), "]")
    recovered = proof.select("same_inputs_same_image")
    assert recovered.current_goal().status() == "deferred"
    recovered.resume()
    equal_inputs = recovered.intro("equal_inputs")
    _ = recovered.lhs().at(x).rw(equal_inputs)
    recovered.rfl()

    theorem = proof.finish()
    assert len(theorem.axioms()) == 0
    assert len(theorem.admissions()) == 0
    print(theorem)


if __name__ == "__main__":
    main()

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

Output

[claim, same_inputs_same_image] deferred : x = y → f(x) = f(y)
[left, forward] open : f(x) = f(y) → x = y
[left, backward] open : x = y → f(x) = f(y)
[right, forward] open : f(x) = f(y) → x = y
[right, backward] open : x = y → f(x) = f(y)
Remaining: same_inputs_same_image [ deferred ]
[A, B] ⊢ ∀f:(A → B). ∀g:(B → A). ∀h:(B → A). ∀D:(A → Prop). ∀E:(A → Prop). (∀t:A. D(t) → g(f(t)) = t) → (∀t:A. E(t) → h(f(t)) = t) → (∀x:A. ∀y:A. (D(x) ∧ D(y) → (f(x) = f(y) ↔ x = y)) ∧ (E(x) ∧ E(y) → (f(x) = f(y) ↔ x = y)))
  • proof.obligations() reports every open and deferred branch by path ([left, forward], [right, backward], [claim, same_inputs_same_image]).
  • Closing the right branch first does not renumber or shift paths in left.
  • cancel_on_domain is ordinary Mojo/Python code driving checked Proof operations; user-defined helpers never extend the trusted base.

Exercise

In conditional_calculations, pass premises=[at_x] instead of premises=[inside] on the first chain.eq step. Why is it rejected?

Answer

Proving \(f(f(x)) = f(x)\) instantiates the law at \(t = f(x)\), requiring \(D(f(x))\). at_x proves \(D(x)\), which does not match.

Next: 2.4 Tactics, proof holes, and theorem discovery.

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