Part 1 — Core proofs

1.2 Prove by cases

Tutorial sequence · Previous: 1.1 Prove your first theorem

Goal: prove \((P \lor Q) \to (Q \lor P)\) by disjunction elimination (cases) and introduction (choose_left, choose_right).

Disjunction elimination

\[ (P \lor Q) \to (Q \lor P) \]

Use | (not Python's or) to construct disjunctions. Eliminating P | Q splits the proof into two independent branches; both must be closed before finish() succeeds.

Mojo

choosing_disjuncts.mojo Download
"""Swap a disjunction by explicitly choosing what each case proves."""

from judgment import Session, implies


def main() raises:
    var s = Session()
    var P = s.prop("P")
    var Q = s.prop("Q")
    var proof = s.theorem("swap_disjunction", implies(P | Q, Q | P))
    var either = proof.intro("either")
    _ = proof.cases(either, left_name="hp", right_name="hq")

    # In the P case, prove the right side of Q | P.
    var p_case = proof.select("left")
    p_case.choose_right()
    p_case.exact(p_case.fact("hp"))

    # In the Q case, prove the left side of Q | P.
    var q_case = proof.select("right")
    q_case.choose_left()
    q_case.exact(q_case.fact("hq"))

    var theorem = proof.finish()
    print(theorem)
    print("Axiom dependencies:", len(theorem.axioms()))

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

Output

⊢ P ∨ Q → Q ∨ P
Axiom dependencies: 0

Python

choosing_disjuncts.py Download
"""Swap a disjunction by explicitly choosing what each case proves."""

from judgment import Session, implies


def main():
    s = Session()
    P = s.prop("P")
    Q = s.prop("Q")
    proof = s.theorem("swap_disjunction", implies(P | Q, Q | P))
    either = proof.intro("either")
    _ = proof.cases(either, left_name="hp", right_name="hq")

    # In the P case, prove the right side of Q | P.
    p_case = proof.select("left")
    p_case.choose_right()
    p_case.exact(p_case.fact("hp"))

    # In the Q case, prove the left side of Q | P.
    q_case = proof.select("right")
    q_case.choose_left()
    q_case.exact(q_case.fact("hq"))

    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/choosing_disjuncts.py

Output

⊢ P ∨ Q → Q ∨ P
Axiom dependencies: 0

intro("either") binds P | Q, and cases(either, ...) spawns two named subgoals (here selected by name rather than using the returned list):

Branch Local assumption Goal before choosing Goal after choosing
"left" hp : P Q ∨ P choose_right() leaves P.
"right" hq : Q Q ∨ P choose_left() leaves Q.

Branch names refer to the eliminated disjunction (P | Q), while choose_left and choose_right select a side of the target disjunction (Q | P). Each branch accesses its own local context via branch.fact(...); assumptions from sibling branches are out of scope.

Connective summary

Context or goal Operation Effect
Hypothesis P & Q left / right Projects one conjunct.
Goal P & Q split() (or conjoin) Creates two subgoals (or builds evidence directly).
Hypothesis P ∨ Q cases Splits into two branches, one per disjunct.
Goal P ∨ Q choose_left() / choose_right() Reduces the goal to the chosen disjunct.

Call finish() on the root proof handle once all branches are closed. Axiom dependencies: 0 confirms the theorem depends on no axioms.

Exercises

In the P branch, replace choose_right() with choose_left() while keeping exact(hp). Why does exact fail?

Answer

choose_left() sets the goal to Q, but this branch only has hp : P.

Restore choose_right() and omit q_case.exact(...). Does proof.finish() succeed?

Answer

No. finish() refuses any proof with an open branch.

Next: 1.3 Talk about arbitrary values.

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