"""Use partial inverses in nested branches with a shared deferred claim."""

from judgment import Expr, JudgmentError, Proof, Session, arrow, forall, iff, implies


def cancel_on_domain(proof, inverse_name, domain_name, image):
    """Prove the forward implication using both explicit domain conditions."""
    same_image = proof.intro("same_image")
    inverse = proof.fact(inverse_name)
    domain = proof.fact(domain_name)
    in_domain_x = proof.left(domain)
    in_domain_y = proof.right(domain)

    _ = proof.lhs().rw(inverse, reverse=True, using=[in_domain_x])
    _ = proof.lhs().at(image).rw(same_image)
    _ = proof.lhs().rw(inverse, using=[in_domain_y])
    proof.rfl()


def main():
    s = Session()
    proof = s.theorem("partial_inverses")
    A = proof.type_var("A")
    B = proof.type_var("B")
    f = proof.var("f", arrow(A, B))
    g = proof.var("g", arrow(B, A))
    h = proof.var("h", arrow(B, A))
    D = proof.var("D", arrow(A, s.prop_type()))
    E = proof.var("E", arrow(A, s.prop_type()))
    t = proof.bound_var("t", A)
    _ = proof.given("inverse_on_D", forall(t, implies(D(t), g(f(t)) == t)))
    _ = proof.given("inverse_on_E", forall(t, implies(E(t), h(f(t)) == t)))
    x = proof.var("x", A)
    y = proof.var("y", A)
    equivalent = iff(f(x) == f(y), x == y)
    proof.goal(implies(D(x) & D(y), equivalent) & implies(E(x) & E(y), equivalent))

    # Both backward directions will use this claim. It remains an obligation.
    reverse = proof.have("same_inputs_same_image", implies(x == y, f(x) == f(y)))
    reverse.sorry()

    _ = proof.split()
    on_d = proof.select("left")
    on_e = proof.select("right")
    _ = on_d.intro("in_D")
    _ = on_e.intro("in_E")
    _ = on_d.split()
    _ = on_e.split()
    assert len(proof.obligations()) == 5
    for goal in proof.obligations():
        print("[" + ", ".join(goal.path()) + "]", goal.status(), ":", goal.target().format())

    # Complete the right domain first; the left paths remain unchanged.
    forward = proof.select(["right", "forward"])
    cancel_on_domain(forward, "inverse_on_E", "in_E", f(x))
    backward = proof.select(["right", "backward"])
    backward.exact(backward.fact("same_inputs_same_image"))

    forward = proof.select(["left", "forward"])
    selected = forward.current_goal()
    assert selected.target().same_as(implies(f(x) == f(y), x == y)) == True
    cancel_on_domain(forward, "inverse_on_D", "in_D", f(x))
    backward = proof.select(["left", "backward"])
    backward.exact(backward.fact("same_inputs_same_image"))

    remaining = proof.obligations()
    assert len(remaining) == 1
    assert remaining[0].status() == "deferred"
    print("Remaining:", remaining[0].name(), "[", remaining[0].status(), "]")
    recovered = proof.select("same_inputs_same_image")
    assert recovered.current_goal().status() == "deferred"
    recovered.resume()
    equal_inputs = recovered.intro("equal_inputs")
    _ = recovered.lhs().at(x).rw(equal_inputs)
    recovered.rfl()

    theorem = proof.finish()
    assert len(theorem.axioms()) == 0
    assert len(theorem.admissions()) == 0
    print(theorem)


if __name__ == "__main__":
    main()
