"""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)
