Part 2 — Structuring proofs & automation

2.1 Negation, equivalence, and using witnesses

Tutorial sequence · Previous: 1.9 See what remains to prove

Goal: prove contraposition and equivalence symmetry constructively, and eliminate existential hypotheses with obtain.

Negation and equivalence

  • Negation (~P or not_(P)): defined constructively as \(P \to \bot\). Calling intro on ~P introduces P and sets the target to False. Given nq : ~Q and q : Q, contradiction(nq, q) derives False.
  • Equivalence (iff(P, Q)): represents \((P \to Q) \land (Q \to P)\). split() on an equivalence goal creates "forward" and "backward" branches; left(eqv) and right(eqv) project the two directions from an equivalence fact.

The example proves contraposition, \((P \to Q) \to (\neg Q \to \neg P)\), and equivalence symmetry, \((P \leftrightarrow Q) \to (Q \leftrightarrow P)\), with no classical axioms:

Mojo

logical_proofs.mojo Download
"""Prove contraposition and equivalence symmetry with explicit logical steps."""

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


def main() raises:
    var s = Session()
    var P = s.prop("P")
    var Q = s.prop("Q")
    var target = implies(implies(P, Q), implies(~Q, ~P))
    var proof = s.theorem("contraposition", target)
    var pq = proof.intro("pq")
    var nq = proof.intro("nq")
    var hp = proof.intro("hp")
    proof.exact(proof.contradiction(nq, proof.use(pq, using=[hp])))
    var theorem = proof.finish()
    assert_true(theorem.statement().same_as(target))
    assert_equal(len(theorem.axioms()), 0)
    print(theorem)

    var symmetry = s.theorem("iff_symmetry", implies(iff(P, Q), iff(Q, P)))
    var equivalence = symmetry.intro("equivalence")
    _ = symmetry.split()
    var forward = symmetry.select("forward")
    var backward = symmetry.select("backward")
    forward.exact(symmetry.right(equivalence))
    backward.sorry()
    print("Deferred directions:", len(symmetry.obligations()))
    backward = symmetry.select("backward")
    backward.resume()
    backward.exact(symmetry.left(equivalence))
    theorem = symmetry.finish()
    assert_equal(len(theorem.axioms()), 0)
    print(theorem)

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

Output

⊢ (P → Q) → ¬Q → ¬P
Deferred directions: 1
⊢ (P ↔ Q) → (Q ↔ P)

Python

logical_proofs.py Download
"""Prove contraposition and equivalence symmetry with explicit logical steps."""

from judgment import Session, iff, implies


def main():
    s = Session()
    P = s.prop("P")
    Q = s.prop("Q")
    target = implies(implies(P, Q), implies(~Q, ~P))
    proof = s.theorem("contraposition", target)
    pq = proof.intro("pq")
    nq = proof.intro("nq")
    hp = proof.intro("hp")
    proof.exact(proof.contradiction(nq, proof.use(pq, using=[hp])))
    theorem = proof.finish()
    assert theorem.statement().same_as(target)
    assert len(theorem.axioms()) == 0
    print(theorem)

    symmetry = s.theorem("iff_symmetry", implies(iff(P, Q), iff(Q, P)))
    equivalence = symmetry.intro("equivalence")
    _ = symmetry.split()
    forward = symmetry.select("forward")
    backward = symmetry.select("backward")
    forward.exact(symmetry.right(equivalence))
    backward.sorry()
    print("Deferred directions:", len(symmetry.obligations()))
    backward = symmetry.select("backward")
    backward.resume()
    backward.exact(symmetry.left(equivalence))
    theorem = symmetry.finish()
    assert len(theorem.axioms()) == 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/logical_proofs.py

Output

⊢ (P → Q) → ¬Q → ¬P
Deferred directions: 1
⊢ (P ↔ Q) → (Q ↔ P)

Because ~P is P → False, intro("hp") assumes P and leaves False. proof.use(pq, using=[hp]) derives Q, and proof.contradiction(nq, ...) closes False.

The core logic is constructive: double-negation elimination (\(\neg\neg P \to P\)) is not derivable without a classical profile (as in Part 4).

Existential elimination (obtain)

In Lesson 1.3, witness(value) proved an existential goal. To eliminate an existential hypothesis h : ∃x. P(x) ∧ Q(x), call proof.obtain(h, "w"):

Return value Meaning
w (Expr) Fresh arbitrary witness scoped to the current branch.
hw (Fact) Checked evidence that w satisfies the existential body (P(w) ∧ Q(w)).

Mojo

obtaining_witnesses.mojo Download
"""Use an existential fact through its scoped witness and checked property."""

from judgment import Session, arrow, exists, implies


def main() raises:
    var s = Session()
    var proof = s.theorem("project_exists")
    var A = proof.type_var("A")
    var P = proof.var("P", arrow(A, s.prop_type()))
    var Q = proof.var("Q", arrow(A, s.prop_type()))
    var x = proof.bound_var("x", A)
    proof.goal(implies(exists(x, P(x) & Q(x)), exists(x, P(x))))

    var h = proof.intro()
    var (w, hw) = proof.obtain(h, "w")
    print("Witness:", w.format())
    print("Its property:", hw.statement().format())
    proof.witness(w)
    proof.exact(proof.left(hw))

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

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

Output

Witness: w
Its property: P(w) ∧ Q(w)
[A] ⊢ ∀P:(A → Prop). ∀Q:(A → Prop). (∃x:A. P(x) ∧ Q(x)) → (∃x:A. P(x))
Axiom dependencies: 0

Python

obtaining_witnesses.py Download
"""Use an existential fact through its scoped witness and checked property."""

from judgment import Session, arrow, exists, implies


def main():
    s = Session()
    proof = s.theorem("project_exists")
    A = proof.type_var("A")
    P = proof.var("P", arrow(A, s.prop_type()))
    Q = proof.var("Q", arrow(A, s.prop_type()))
    x = proof.bound_var("x", A)
    proof.goal(implies(exists(x, P(x) & Q(x)), exists(x, P(x))))

    h = proof.intro()
    w, hw = proof.obtain(h, "w")
    print("Witness:", w.format())
    print("Its property:", hw.statement().format())
    proof.witness(w)
    proof.exact(proof.left(hw))

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

Output

Witness: w
Its property: P(w) ∧ Q(w)
[A] ⊢ ∀P:(A → Prop). ∀Q:(A → Prop). (∃x:A. P(x) ∧ Q(x)) → (∃x:A. P(x))
Axiom dependencies: 0

The witness w is scoped to the branch and cannot escape into sibling branches or outer frames. proof.finish() discharges both w and hw via constructive existential elimination.

Exercises

In logical_proofs, change contraposition to implies(~~P, P). Can it be proved using only intro and contradiction?

Answer

No. Introducing ~~P leaves the goal P rather than False. Constructive logic derives P → ~~P and (P → Q) → (~Q → ~P), but double-negation elimination requires excluded middle.

In obtaining_witnesses, what happens if proof.witness(w) is called before proof.obtain(h, "w")?

Answer

No witness w is in scope yet; obtain introduces the scoped variable and its checked property P(w) ∧ Q(w).

Next: 2.2 Claims, abbreviations, and binders.

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