"""Replace a premise loop with one atomic, explicitly chosen tactic sequence."""

from judgment import Session, implies


def main():
    s = Session()
    source = s.theorem("join")
    Left = source.prop("Left")
    Right = source.prop("Right")
    left = source.given("left", Left)
    right = source.given("right", Right)
    source.goal(Left & Right)
    source.exact(source.conjoin(left, right))
    join = source.finish()

    P = s.prop("P")
    Q = s.prop("Q")
    goal = implies(P, implies(Q, P & Q))

    # Direct application returns premises for an explicit loop.
    direct = s.theorem("direct", goal)
    _ = direct.intro("p")
    _ = direct.intro("q")
    premises = direct.apply(join)
    for i in range(len(premises)):
        premises[i].assumption()
    print(direct.finish())

    # Composition makes application and every premise proof one atomic attempt.
    proof = s.theorem("composed", goal)
    _ = proof.intro("p")
    _ = proof.intro("q")
    t = proof.tactics()
    solve = t.apply(join).then(t.assumption()).require_closed()
    print("Composed:", proof.by(solve).status())
    print(proof.finish())

    # The same program cannot prove Q without evidence; even its first premise
    # solution rolls back, so the caller retains the complete original goal.
    partial = s.theorem("partial", implies(P, P & Q))
    _ = partial.intro("p")
    before = partial.format()
    print("Missing premise:", partial.by(solve).status())
    print("Draft unchanged:", partial.format() == before)


if __name__ == "__main__":
    main()
