"""Write an algebra calculation first, then prove each requested equality."""

from judgment import Session
from arithmetic_setup import arithmetic_setup


def main() raises:
    var s = Session()
    var algebra = arithmetic_setup(s)
    var proof = s.theorem("sum_squared_deferred")
    var ab = proof.vars("a b", algebra.kind)
    var a, b = ab[0], ab[1]
    proof.goal((a + b)**2 == a**2 + 2 * a * b + b**2)

    var square = proof.rewrite("square", at=proof.lhs(), to=(a + b) * (a + b))
    var expand = proof.rewrite("expand", at=proof.lhs(), to=a**2 + a * b + b * a + b**2)
    var collect = proof.rewrite("collect", at=proof.lhs(), to=a**2 + 2 * a * b + b**2)
    proof.rfl()

    # The calculation is complete, but its three equality claims remain.
    print("Equality obligations:", len(proof.obligations()))
    _ = collect.apply(algebra.collect)
    _ = expand.apply(algebra.expand)
    square.rfl()

    var theorem = proof.finish()
    print(theorem)
