"""Prove a generic theorem once, then use it for a targeted rewrite."""

from judgment import Session, arrow


def main():
    s = Session()
    point = s.declare_type("Point")
    map_ = s.symbol("map", arrow(point, point))

    source = s.theorem("congruence")
    A = source.type_var("A")
    a = source.var("a", A)
    b = source.var("b", A)
    f = source.var("f", arrow(A, A))
    h = source.given("h", a == b)
    source.goal(f(a) == f(b))
    _ = source.lhs().at(a).rw(h)
    source.rfl()
    _ = source.finish()

    proof = s.theorem("map_twice")
    x = proof.var("x", point)
    y = proof.var("y", point)
    equal = proof.given("equal", x == y)
    proof.goal(map_(map_(x)) == map_(map_(y)))

    _ = proof.lhs().at(map_(x)).rw(s.fact("congruence"), using=[equal])
    proof.rfl()
    theorem = proof.finish()
    assert theorem.statement().format() == "∀x:Point. ∀y:Point. x = y → map(map(x)) = map(map(y))"
    print(theorem)


if __name__ == "__main__":
    main()
