"""Open quantifiers and cases with one tactic, then finish each branch directly; name nested binders freshly."""

from judgment import Session, arrow, exists, forall, implies


def main():
    s = Session()
    proof = s.theorem("exists_or_split")
    A = proof.type_var("A")
    F = proof.var("F", arrow(A, s.prop_type()))
    G = proof.var("G", arrow(A, s.prop_type()))
    x = proof.bound_var("x", A)
    proof.goal(implies(exists(x, F(x) | G(x)), exists(x, F(x)) | exists(x, G(x))))
    t = proof.tactics()
    # Each step uses the names the earlier steps gave.
    opening = t.intro("h").then(t.obtain("h", "w", "hw")).then(t.cases("hw", "hf", "hg"))
    print(proof.by(opening).status())
    # The handle selects the left branch; direct steps find the witness and premises by name.
    proof.choose_left()
    proof.witness(proof.variable("w"))
    proof.exact(proof.fact("hf"))
    right = proof.select("right")
    print(right.by(t.choose_right().then(t.witness("w")).then(t.exact("hg"))).status())
    print(proof.finish())

    # fresh=True picks x, x_1, x_2, ... so one repeated step opens nested universals.
    nested = s.theorem("nested_fresh")
    B = nested.type_var("B")
    R = nested.var("R", arrow(B, s.prop_type()))
    a = nested.bound_var("a", B)
    b = nested.bound_var("b", B)
    nested.goal(forall(a, forall(b, implies(R(a), R(a) | R(b)))))
    t = nested.tactics()
    print(nested.by(t.intro_var("x", fresh=True).repeat().then(t.intro("h"))).status())
    print(nested.target().format())
    nested.choose_left()
    nested.exact(nested.fact("h"))
    print(nested.finish())


if __name__ == "__main__":
    main()
