Part 1 — Core proofs

1.6 Reuse a theorem

Tutorial sequence · Previous: 1.5 Write an equality calculation

Goal: prove conjunction symmetry via backward rule application (apply) of a previously proved lemma.

Defining and applying a reusable lemma

We first prove

\[ \forall L:\mathrm{Prop}.\;\forall R:\mathrm{Prop}.\;L \to R \to (L \land R) \]

as a theorem join, then apply it backward to prove \((P \land Q) \to (Q \land P)\).

Mojo

applying_theorems.mojo Download
"""Use a theorem backward, proving its premises as independent subgoals."""

from judgment import Session, implies


def main() raises:
    var s = Session()
    var join = s.theorem("join")
    var Left = join.prop("Left")
    var Right = join.prop("Right")
    var left = join.given("left", Left)
    var right = join.given("right", Right)
    join.goal(Left & Right)
    join.exact(join.conjoin(left, right))
    var conjunction = join.finish()

    var P = s.prop("P")
    var Q = s.prop("Q")
    var proof = s.theorem("swap_by_apply", implies(P & Q, Q & P))
    var h = proof.intro("pair")
    _ = proof.apply(conjunction)
    var first = proof.select("premise_1")
    var second = proof.select("premise_2")
    print("First premise:", first.target().format())
    print("Second premise:", second.target().format())

    # These can be proved in either order, deferred, or selected later.
    second.exact(second.left(h))
    first.exact(first.right(h))
    var theorem = proof.finish()
    print(theorem)
    print("Axiom dependencies:", len(theorem.axioms()))

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

Output

First premise: Q
Second premise: P
⊢ P ∧ Q → Q ∧ P
Axiom dependencies: 0

Python

applying_theorems.py Download
"""Use a theorem backward, proving its premises as independent subgoals."""

from judgment import Session, implies


def main():
    s = Session()
    join = s.theorem("join")
    Left = join.prop("Left")
    Right = join.prop("Right")
    left = join.given("left", Left)
    right = join.given("right", Right)
    join.goal(Left & Right)
    join.exact(join.conjoin(left, right))
    conjunction = join.finish()

    P = s.prop("P")
    Q = s.prop("Q")
    proof = s.theorem("swap_by_apply", implies(P & Q, Q & P))
    h = proof.intro("pair")
    _ = proof.apply(conjunction)
    first = proof.select("premise_1")
    second = proof.select("premise_2")
    print("First premise:", first.target().format())
    print("Second premise:", second.target().format())

    # These can be proved in either order, deferred, or selected later.
    second.exact(second.left(h))
    first.exact(first.right(h))
    theorem = proof.finish()
    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/applying_theorems.py

Output

First premise: Q
Second premise: P
⊢ P ∧ Q → Q ∧ P
Axiom dependencies: 0

Backward chaining with apply

After intro("pair"), the goal is Q & P. Calling proof.apply(conjunction) matches the conclusion L ∧ R against Q ∧ P (instantiating L := Q, R := P) and spawns one subgoal per premise:

Subgoal Instantiated premise Closed with
premise_1: Q L right(h)
premise_2: P R left(h)

Subgoals can be solved in any order (the example solves premise_2 first). Calling finish() on the root proof handle checks the completed derivation.

You can pass the Fact returned by finish() directly or look it up later with s.fact("join"). apply infers the proposition instantiations from the goal; for APIs that require explicit type and term arguments, see Lesson 3.1.

Exercise

In the join proof, replace join.conjoin(left, right) with join.conjoin(right, left) while keeping the goal Left & Right. Does exact accept it?

Answer

No. exact checks syntactic/definitional match against the goal (Left & Right) and does not infer commutativity of conjunction.

Next: 1.7 Define names and track assumptions.

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