"""Run three candidate programs on forks of one goal and commit the lowest-index success."""

from fp.effects.async_runtime import IORuntimeConfig, Profile
from judgment_shell import ExprData, Session, TacticData, arrow, exists
from judgment_search import SearchLimits, first


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

    # Candidates in priority order: the first does not match, the last two solve.
    var witness = TacticData.witness(ExprData.name("a"))
    var candidates: List[TacticData] = [TacticData.split(), witness.then(TacticData.exact("ra")), witness.then(TacticData.assumption())]

    # Each runs in its own fork with 1,000 units; two run at once. The lowest
    # index that succeeds is selected, whatever finishes first.
    var config = IORuntimeConfig()
    config.profile = Profile.current_thread()
    var result = first(proof, candidates, 1000, SearchLimits(workers=2), config)
    print("selected:", result.index(), result.status(), result.evidence())
    print("outcomes:", result.statuses()[0], result.statuses()[1])
    print(proof.finish())
