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
"""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
"""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
- Session scope (
s.transform): unfoldstwice(a)tof(f(a))and rewrites byf_idemtof(a), yielding checked evidence fortwice(a) = f(a)without pre-statingf(a). - Proof scope (
proof.transform): starts fromtwice(a)under local hypothesisab : a = b, combines unfolding, law rewriting, andptr.at(a).rw(ab)to reachf(b), and passesptr.evidence()directly toproof.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).