1.4 Rewrite with a reason¶
Tutorial sequence · Previous: 1.3 Talk about arbitrary values
Goal: prove congruence \(\forall a, b:A.\;\forall f:(A \to A).\;a = b \to f(a) = f(b)\) using targeted subterm rewriting.
Statement and example¶
Use arrow(A, A) for function types and implies for logical implication.
Calling f(a) builds a symbolic HOL application.
Mojo
"""State a generic theorem with typed parameters and rewrite an argument."""
from std.testing import assert_true
from judgment import Session, arrow
def main() raises:
var s = Session()
var proof = s.theorem("congruence")
var A = proof.type_var("A")
var a = proof.var("a", A)
var b = proof.var("b", A)
var f = proof.var("f", arrow(A, A))
var h = proof.given("h", a == b)
proof.goal(f(a) == f(b))
_ = proof.lhs().at(a).rw(h)
proof.rfl()
var theorem = proof.finish()
assert_true(theorem.type_params()[0].same_as(A))
assert_true(theorem.statement().format() == "∀a:A. ∀b:A. ∀f:(A → A). a = b → f(a) = f(b)")
print(theorem)
Run from the repository root pixi run core-run docs/examples/library/typed_proof.mojo
Output
[A] ⊢ ∀a:A. ∀b:A. ∀f:(A → A). a = b → f(a) = f(b)
Python
"""State a generic theorem with typed parameters and rewrite an argument."""
from judgment import Session, arrow
def main():
s = Session()
proof = s.theorem("congruence")
A = proof.type_var("A")
a = proof.var("a", A)
b = proof.var("b", A)
f = proof.var("f", arrow(A, A))
h = proof.given("h", a == b)
proof.goal(f(a) == f(b))
_ = proof.lhs().at(a).rw(h)
proof.rfl()
theorem = proof.finish()
assert theorem.type_params()[0].same_as(A)
assert theorem.statement().format() == "∀a:A. ∀b:A. ∀f:(A → A). a = b → f(a) = f(b)"
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/typed_proof.py
Output
[A] ⊢ ∀a:A. ∀b:A. ∀f:(A → A). a = b → f(a) = f(b)
In the theorem header, proof.given("h", a == b) introduces a named premise
before goal(...) fixes the conclusion. Unlike intro, which destructs an
implication already in the goal, given attaches a premise in the header that
becomes an implication when finish() discharges it.
Focused rewriting¶
The focus chain identifies the exact occurrence to rewrite:
| Call | Role |
|---|---|
proof.lhs() |
Focuses on the left-hand side f(a) of an equality goal. |
.at(a) |
Selects a within that focus. |
.rw(h) |
Rewrites the focused subterm using h : a = b, producing f(b). |
This reduces the goal to f(b) = f(b), closed by proof.rfl(). proof.finish()
records the congruence derivation for the original goal.
When a subterm appears multiple times, pass 1-based occurrence=k to at(...)
(covered in Lesson 1.8).
Note that rfl() checks definitional equality under conversion only; it does
not search local hypotheses for equalities, so h must be applied explicitly
via rw.
Exercise¶
Rewrite the right-hand side instead using proof.rhs().at(b).rw(h, reverse=True)
followed by rfl(). What is the goal immediately before rfl()?
Answer
f(a) = f(a). Rewriting in reverse replaces b with a in f(b) while
proving the same theorem a = b → f(a) = f(b).