Part 1 — Core proofs

1.5 Write an equality calculation

Tutorial sequence · Previous: 1.4 Rewrite with a reason

Goal: prove transitivity of equality (\(a = b \to b = c \to a = c\)) using an explicit equational chain (calc).

Statement and example

\[ a = b = c. \]

proof.vars("a b c", A) declares multiple term parameters of type A in one call and returns them as a list. The two given hypotheses become implications in the finished theorem (a = b → b = c → a = c).

Mojo

quick_start_calculation.mojo Download
"""Prove an equality by stating each intermediate expression and its reason."""

from judgment import Session


def main() raises:
    var s = Session()
    var proof = s.theorem("transitivity")
    var A = proof.type_var("A")
    var values = proof.vars("a b c", A)
    var a, b, c = values[0], values[1], values[2]
    var ab = proof.given("ab", a == b)
    var bc = proof.given("bc", b == c)
    proof.goal(a == c)

    var chain = proof.calc(a)
    chain.eq(b, using=ab, label="ab")
    chain.eq(c, using=bc, label="bc")
    print(chain.format())
    proof.exact(chain.finish())
    print(proof.finish())

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

Output

a
= b [ab: checked]
= c [bc: checked]
[A] ⊢ ∀a:A. ∀b:A. ∀c:A. a = b → b = c → a = c

Python

quick_start_calculation.py Download
"""Prove an equality by stating each intermediate expression and its reason."""

from judgment import Session


def main():
    s = Session()
    proof = s.theorem("transitivity")
    A = proof.type_var("A")
    values = proof.vars("a b c", A)
    a, b, c = values[0], values[1], values[2]
    ab = proof.given("ab", a == b)
    bc = proof.given("bc", b == c)
    proof.goal(a == c)

    chain = proof.calc(a)
    chain.eq(b, using=ab, label="ab")
    chain.eq(c, using=bc, label="bc")
    print(chain.format())
    proof.exact(chain.finish())
    print(proof.finish())


if __name__ == "__main__":
    main()

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

Output

a
= b [ab: checked]
= c [bc: checked]
[A] ⊢ ∀a:A. ∀b:A. ∀c:A. a = b → b = c → a = c

Equational chains

proof.calc(a) starts a chain at a. Each chain.eq(next, using=..., label=...) extends the chain to next using checked equality evidence; label is purely for human-readable output.

Call Effect
chain.finish() Produces checked evidence for the equality between the chain's endpoints under its active hypotheses.
proof.exact(...) Closes the theorem's goal with that evidence.
proof.finish() Verifies all obligations and registers the named theorem.

The [ab: checked] and [bc: checked] lines confirm that each link was checked relative to the active hypotheses; the resulting theorem retains a = b and b = c as premises.

Exercise

Change the second step to chain.eq(c, using=ab, label="bc"). Does the label affect checking?

Answer

No. The chain expects evidence for b = c, whereas ab proves a = b, so the kernel rejects the step regardless of its label.

Next: 1.6 Reuse a theorem.

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