2.5 Tactic combinators and deferred rewrites¶
Tutorial sequence · Previous: 2.4 Tactics, proof holes, and discovery
Goal: combine or_else, repeat, and all_goals to close heterogeneous
subgoals atomically, open nested binders with fresh=True, and defer targeted
occurrence rewrites with proof.rewrite(...).
Tactic combinators and deferred rewrites¶
| Combinator | Semantics |
|---|---|
t1.then(t2) |
Execute t1, then execute t2 on the resulting subgoals. |
t1.or_else(t2) |
Execute t1; on no_match, execute t2 on the unchanged goal. |
t.all_goals() |
Apply t to every open subgoal produced so far. |
t.repeat() |
Iterate t (up to a bounded limit) until no further progress occurs. |
To replace a focused occurrence immediately and defer its equality proof as a
named subgoal, call proof.rewrite(name, at=focus, to=replacement).
Mojo
"""Use repeat, or_else, and all_goals tactic combinators alongside deferred targeted rewrites."""
from std.testing import assert_equal
from judgment import Session, arrow, implies
def main() raises:
var s = Session()
var A = s.declare_type("A")
var f = s.symbol("f", arrow(A, A))
# 1. Combine repeat, or_else, and all_goals to discharge heterogeneous subgoals at once.
var mixed = s.theorem("mixed_goals")
var x = mixed.var("x", A)
var P = mixed.prop("P")
mixed.goal(implies(P, (x == x) & (P & (f(x) == f(x)))))
var t = mixed.tactics()
var close_leaf = t.rfl().or_else(t.assumption())
var step = t.split().or_else(close_leaf).all_goals()
var pipeline = t.intro("hp").then(step.repeat()).require_closed()
var report = mixed.by(pipeline)
assert_equal(report.status(), "solved")
print("Combinator pipeline:", report.status(), "|", mixed.finish())
# 2. Deferred targeted rewrites: outline the rewrite plan now, prove each equality later.
var chain = s.theorem("deferred_congr")
var abc = chain.vars("a b c", A)
var a, b, c = abc[0], abc[1], abc[2]
var ab = chain.given("ab", a == b)
var bc = chain.given("bc", b == c)
chain.goal(f(a) == f(c))
var step1 = chain.rewrite("a_to_b", at=chain.lhs().at(a), to=b)
var step2 = chain.rewrite("b_to_c", at=chain.lhs().at(b), to=c)
chain.rfl()
assert_equal(len(chain.obligations()), 2)
print("Pending equality obligations:", len(chain.obligations()))
step1.exact(ab)
step2.exact(bc)
var theorem = chain.finish()
assert_equal(len(theorem.axioms()), 0)
print("Deferred rewrites proved:", theorem)
Run from the repository root pixi run core-run docs/examples/library/tactic_combinators.mojo
Output
Combinator pipeline: solved | ⊢ ∀x:A. ∀P:Prop. P → x = x ∧ (P ∧ f(x) = f(x))
Pending equality obligations: 2
Deferred rewrites proved: ⊢ ∀a:A. ∀b:A. ∀c:A. a = b → b = c → f(a) = f(c)
Python
"""Use repeat, or_else, and all_goals tactic combinators alongside deferred targeted rewrites."""
from judgment import Session, arrow, implies
def main():
s = Session()
A = s.declare_type("A")
f = s.symbol("f", arrow(A, A))
# 1. Combine repeat, or_else, and all_goals to discharge heterogeneous subgoals at once.
mixed = s.theorem("mixed_goals")
x = mixed.var("x", A)
P = mixed.prop("P")
mixed.goal(implies(P, (x == x) & (P & (f(x) == f(x)))))
t = mixed.tactics()
close_leaf = t.rfl().or_else(t.assumption())
step = t.split().or_else(close_leaf).all_goals()
pipeline = t.intro("hp").then(step.repeat()).require_closed()
report = mixed.by(pipeline)
assert report.status() == "solved"
print("Combinator pipeline:", report.status(), "|", mixed.finish())
# 2. Deferred targeted rewrites: outline the rewrite plan now, prove each equality later.
chain = s.theorem("deferred_congr")
a, b, c = chain.vars("a b c", A)
ab = chain.given("ab", a == b)
bc = chain.given("bc", b == c)
chain.goal(f(a) == f(c))
step1 = chain.rewrite("a_to_b", at=chain.lhs().at(a), to=b)
step2 = chain.rewrite("b_to_c", at=chain.lhs().at(b), to=c)
chain.rfl()
assert len(chain.obligations()) == 2
print("Pending equality obligations:", len(chain.obligations()))
step1.exact(ab)
step2.exact(bc)
theorem = chain.finish()
assert len(theorem.axioms()) == 0
print("Deferred rewrites proved:", theorem)
if __name__ == "__main__":
main()
Run from the repository root PYTHONPATH=bindings/python:docs/examples/python/library pixi run python docs/examples/python/library/tactic_combinators.py
Output
Combinator pipeline: solved | ⊢ ∀x:A. ∀P:Prop. P → x = x ∧ (P ∧ f(x) = f(x))
Pending equality obligations: 2
Deferred rewrites proved: ⊢ ∀a:A. ∀b:A. ∀c:A. a = b → b = c → f(a) = f(c)
In t.split().or_else(close_leaf).all_goals().repeat(), each pass splits any
conjunction subgoal and closes leaf subgoals (rfl closes x = x and
f(x) = f(x), while or_else(t.assumption()) closes P). Repeating to a
fixed point discharges all three leaves atomically.
Nested quantifiers with fresh=True¶
The same combinators compose with intro_var, obtain, and cases. Passing
fresh=True to intro_var("x", fresh=True).repeat() introduces arbitrarily
nested universal quantifiers as x, x_1, x_2, … without name clashes:
Mojo
"""Open quantifiers and cases with one tactic, then finish each branch directly; name nested binders freshly."""
from judgment import Session, arrow, exists, forall, implies
def main() raises:
var s = Session()
var proof = s.theorem("exists_or_split")
var A = proof.type_var("A")
var F = proof.var("F", arrow(A, s.prop_type()))
var G = proof.var("G", arrow(A, s.prop_type()))
var x = proof.bound_var("x", A)
proof.goal(implies(exists(x, F(x) | G(x)), exists(x, F(x)) | exists(x, G(x))))
var t = proof.tactics()
# Each step uses the names the earlier steps gave.
var opening = t.intro("h").then(t.obtain("h", "w", "hw")).then(t.cases("hw", "hf", "hg"))
print(proof.by(opening).status())
# The handle selects the left branch; direct steps find the witness and premises by name.
proof.choose_left()
proof.witness(proof.variable("w"))
proof.exact(proof.fact("hf"))
var right = proof.select("right")
print(right.by(t.choose_right().then(t.witness("w")).then(t.exact("hg"))).status())
print(proof.finish())
# fresh=True picks x, x_1, x_2, ... so one repeated step opens nested universals.
var nested = s.theorem("nested_fresh")
var B = nested.type_var("B")
var R = nested.var("R", arrow(B, s.prop_type()))
var a = nested.bound_var("a", B)
var b = nested.bound_var("b", B)
nested.goal(forall(a, forall(b, implies(R(a), R(a) | R(b)))))
t = nested.tactics()
print(nested.by(t.intro_var("x", fresh=True).repeat().then(t.intro("h"))).status())
print(nested.target().format())
nested.choose_left()
nested.exact(nested.fact("h"))
print(nested.finish())
Run from the repository root pixi run core-run docs/examples/library/quantifier_tactics.mojo
Output
progress
solved
[A] ⊢ ∀F:(A → Prop). ∀G:(A → Prop). (∃x:A. F(x) ∨ G(x)) → (∃x:A. F(x)) ∨ (∃x:A. G(x))
progress
R(x) ∨ R(x_1)
[B] ⊢ ∀R:(B → Prop). ∀a:B. ∀b:B. R(a) → R(a) ∨ R(b)
Python
"""Open quantifiers and cases with one tactic, then finish each branch directly; name nested binders freshly."""
from judgment import Session, arrow, exists, forall, implies
def main():
s = Session()
proof = s.theorem("exists_or_split")
A = proof.type_var("A")
F = proof.var("F", arrow(A, s.prop_type()))
G = proof.var("G", arrow(A, s.prop_type()))
x = proof.bound_var("x", A)
proof.goal(implies(exists(x, F(x) | G(x)), exists(x, F(x)) | exists(x, G(x))))
t = proof.tactics()
# Each step uses the names the earlier steps gave.
opening = t.intro("h").then(t.obtain("h", "w", "hw")).then(t.cases("hw", "hf", "hg"))
print(proof.by(opening).status())
# The handle selects the left branch; direct steps find the witness and premises by name.
proof.choose_left()
proof.witness(proof.variable("w"))
proof.exact(proof.fact("hf"))
right = proof.select("right")
print(right.by(t.choose_right().then(t.witness("w")).then(t.exact("hg"))).status())
print(proof.finish())
# fresh=True picks x, x_1, x_2, ... so one repeated step opens nested universals.
nested = s.theorem("nested_fresh")
B = nested.type_var("B")
R = nested.var("R", arrow(B, s.prop_type()))
a = nested.bound_var("a", B)
b = nested.bound_var("b", B)
nested.goal(forall(a, forall(b, implies(R(a), R(a) | R(b)))))
t = nested.tactics()
print(nested.by(t.intro_var("x", fresh=True).repeat().then(t.intro("h"))).status())
print(nested.target().format())
nested.choose_left()
nested.exact(nested.fact("h"))
print(nested.finish())
if __name__ == "__main__":
main()
Run from the repository root PYTHONPATH=bindings/python:docs/examples/python/library pixi run python docs/examples/python/library/quantifier_tactics.py
Output
progress
solved
[A] ⊢ ∀F:(A → Prop). ∀G:(A → Prop). (∃x:A. F(x) ∨ G(x)) → (∃x:A. F(x)) ∨ (∃x:A. G(x))
progress
R(x) ∨ R(x_1)
[B] ⊢ ∀R:(B → Prop). ∀a:B. ∀b:B. R(a) → R(a) ∨ R(b)
Exercise¶
In tactic_combinators, replace t.split().or_else(close_leaf).all_goals()
with t.split().all_goals().repeat() followed by .then(close_leaf.all_goals()).
Why does it stop early on (x = x) ∧ (P ∧ f(x) = f(x))?
Answer
After the first split, the left subgoal x = x is not a conjunction while the
right subgoal P ∧ f(x) = f(x) is. On the second pass, t.split().all_goals()
fails on x = x, terminating the loop before splitting the right branch.
t.split().or_else(close_leaf) (or t.split().or_else(t.skip())) handles
non-conjunction leaves inside all_goals().