Part 2 — Structuring proofs & automation

2.6 Checked term transformations

Tutorial sequence · Previous: 2.5 Tactic combinators and deferred rewrites

Goal: rewrite symbolic expressions directly via Session.transform and Proof.transform without pre-stating a target equation, retrieving both the transformed term (output()) and its checked equality proof (evidence()).

Proving s.theorem(name, lhs == rhs) requires knowing rhs in advance. For simplifiers, normalizers, or compiler passes, you typically start from an input term e, apply a sequence of definitions and rewrite laws, and obtain whatever normal form results together with a checked proof of input = output.

Session-level and proof-level transformations

s.transform(expr) (session scope) and proof.transform(expr) (active proof scope, with access to local variables and hypotheses) return a Transformation:

Method Role
tr.focus() / tr.at(subexpr) Select the current term or a subexpression within it.
.unfold(def) / .rw(law) / .change(...) Apply a checked step, advancing tr.output().
tr.input() / tr.output() Return the initial and current expressions.
tr.evidence() Return a checked Step proving tr.input() = tr.output().

Mojo

term_transformations.mojo Download
"""Rewrite symbolic expressions directly with Transformation and obtain checked equality evidence."""

from std.testing import assert_equal, assert_true
from judgment import Session, arrow, lambda_


def main() raises:
    var s = Session()
    var A = s.declare_type("A")
    var f = s.symbol("f", arrow(A, A))
    var a = s.symbol("a", A)
    var b = s.symbol("b", A)

    # Define twice(x) = f(f(x)) and state an idempotence law for f.
    var def_builder = s.definition("twice")
    var x = def_builder.var("x", A)
    var twice = def_builder.finish(f(f(x)))
    var law_builder = s.axiom("f_idem")
    var u = law_builder.var("u", A)
    var f_idem = law_builder.finish(f(f(u)) == f(u))

    # Session-level Transformation: unfold twice(a), then rewrite f(f(a)) to f(a).
    var tr = s.transform(twice(a))
    _ = tr.focus().unfold(twice)
    _ = tr.focus().rw(f_idem)
    var eq_step = tr.evidence()
    assert_true(tr.input().same_as(twice(a)))
    assert_true(tr.output().same_as(f(a)))
    assert_true(eq_step.statement().same_as(twice(a) == f(a)))
    print("Input:", tr.input().format())
    print("Output:", tr.output().format())
    print("Checked evidence:", eq_step.statement().format())

    # Proof-level Transformation: rewrite under a local hypothesis and close the goal.
    var proof = s.theorem("twice_congr")
    var ab = proof.given("ab", a == b)
    proof.goal(twice(a) == f(b))
    var ptr = proof.transform(twice(a))
    _ = ptr.focus().unfold(twice)
    _ = ptr.focus().rw(f_idem)
    _ = ptr.at(a).rw(ab)
    proof.exact(ptr.evidence())
    var theorem = proof.finish()
    assert_equal(len(theorem.axioms()), 1)
    print("Proved theorem:", theorem)

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

Output

Input: twice(a)
Output: f(a)
Checked evidence: twice(a) = f(a)
Proved theorem: [assuming 1 axiom] ⊢ a = b → twice(a) = f(b)
  axioms: f_idem

Python

term_transformations.py Download
"""Rewrite symbolic expressions directly with Transformation and obtain checked equality evidence."""

from judgment import Session, arrow


def main():
    s = Session()
    A = s.declare_type("A")
    f = s.symbol("f", arrow(A, A))
    a = s.symbol("a", A)
    b = s.symbol("b", A)

    # Define twice(x) = f(f(x)) and state an idempotence law for f.
    def_builder = s.definition("twice")
    x = def_builder.var("x", A)
    twice = def_builder.finish(f(f(x)))
    law_builder = s.axiom("f_idem")
    u = law_builder.var("u", A)
    f_idem = law_builder.finish(f(f(u)) == f(u))

    # Session-level Transformation: unfold twice(a), then rewrite f(f(a)) to f(a).
    tr = s.transform(twice(a))
    tr.focus().unfold(twice)
    tr.focus().rw(f_idem)
    eq_step = tr.evidence()
    assert tr.input().same_as(twice(a))
    assert tr.output().same_as(f(a))
    assert eq_step.statement().same_as(twice(a) == f(a))
    print("Input:", tr.input().format())
    print("Output:", tr.output().format())
    print("Checked evidence:", eq_step.statement().format())

    # Proof-level Transformation: rewrite under a local hypothesis and close the goal.
    proof = s.theorem("twice_congr")
    ab = proof.given("ab", a == b)
    proof.goal(twice(a) == f(b))
    ptr = proof.transform(twice(a))
    ptr.focus().unfold(twice)
    ptr.focus().rw(f_idem)
    ptr.at(a).rw(ab)
    proof.exact(ptr.evidence())
    theorem = proof.finish()
    assert len(theorem.axioms()) == 1
    print("Proved theorem:", theorem)


if __name__ == "__main__":
    main()

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

Output

Input: twice(a)
Output: f(a)
Checked evidence: twice(a) = f(a)
Proved theorem: [assuming 1 axiom] ⊢ a = b → twice(a) = f(b)
  axioms: f_idem
  1. Session scope (s.transform): unfolds twice(a) to f(f(a)) and rewrites by f_idem to f(a), yielding checked evidence for twice(a) = f(a) without pre-stating f(a).
  2. Proof scope (proof.transform): starts from twice(a) under local hypothesis ab : a = b, combines unfolding, law rewriting, and ptr.at(a).rw(ab) to reach f(b), and passes ptr.evidence() directly to proof.exact(...).

Axioms used during the transformation (here f_idem) propagate into the final theorem's dependency closure.

Exercise

In term_transformations, apply ptr.at(a).rw(ab) before unfold(twice) and rw(f_idem). Does the transformation still establish twice(a) = f(b)?

Answer

Yes. Rewriting a first turns twice(a) into twice(b), unfold(twice) produces f(f(b)), and rw(f_idem) produces f(b), yielding the same checked equality twice(a) = f(b).

Next: 2.7 Admissions, transactions, and sealed theories.

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