"""Describe a goal as data, propose two candidate programs, try both, and commit the one that solves."""

from judgment import Candidate, ExprData, Session, TacticData, TypeData, arrow, exists, implies


def main():
    s = Session()
    T = s.declare_type("T")
    R = s.symbol("R", arrow(T, s.prop_type()))
    proof = s.theorem("witnessed")
    a = proof.var("a", T)
    _ = proof.given("ra", R(a))
    y = proof.bound_var("y", T)
    proof.goal(exists(y, R(y)))

    # A producer sees only data: the goal's path, revision, target, and context.
    goal = proof.current_goal().data()
    print(goal.target().format(), goal.variables()[0], goal.premises()[0])

    # Two candidate programs, built without any session reference.
    first = TacticData.split().then(TacticData.assumption())
    second = TacticData.witness(ExprData.name("a")).then(TacticData.exact("ra"))
    candidates = [Candidate(goal.path(), goal.revision(), first), Candidate(goal.path(), goal.revision(), second)]

    # Trials report without changing the draft; the first that solves is committed.
    for candidate in candidates:
        report = proof.trial(proof.compile(candidate.tactic()))
        print(report.status())
        if report.status() == "solved":
            print(proof.by(candidate).status())
            break
    print(proof.finish())


if __name__ == "__main__":
    main()
