"""Prove guarded rational-function and polynomial identities, split carrier cases, and check literal facts."""
from fractions import Fraction
from judgment import (
    Budget, ProfileLevel, ScalarAtom, Session, TransferResult, build_scalar, build_scalar_poly,
    class_split, identity, implies, is_generic, literal_facts, reify, session, to_guard, transfer,
)


def main():
    budget = Budget(1_000_000_000)
    profile = build_scalar(budget, ProfileLevel.POWERS_AND_ROOTS)

    # 1. Rational-function transfer derives the exact generic guard and a checked proof.
    s = session(profile, ["x"], work=200_000_000)
    x = s.import_symbol("x")
    zero = profile.constant(s, "zero")
    one = profile.constant(s, "one")
    plus = profile.constant(s, "plus")
    times = profile.constant(s, "times")
    neg = profile.constant(s, "neg")
    inv = profile.constant(s, "inv")
    finite = profile.constant(s, "finite")

    x_minus_one = plus(x, neg(one))
    lhs = times(plus(times(x, x), neg(one)), inv(x_minus_one))
    rhs = plus(x, one)
    tb = Budget(100_000_000)
    proved = transfer(s, profile, s.base(), lhs, rhs, tb)
    assert proved.kind() == TransferResult.PROVED
    guard = proved.guard()
    assert len(guard) == 2
    assert guard[0].same_as(finite(x))
    assert guard[1].same_as(~(x_minus_one == zero))
    step = proved.step()
    assert len(step.hypotheses()) == 0
    assert step.statement().same_as(implies(guard[0] & guard[1], lhs == rhs))
    print("Rational transfer guard atoms:", len(guard))
    print("Transfer guard:", guard[0].format(), "and", guard[1].format())

    # 2. Translate guard atoms, split G1's four carrier cases, and prove literal domain facts.
    nz_atom = ScalarAtom(ScalarAtom.NONZERO, [x_minus_one])
    real_atom = ScalarAtom(ScalarAtom.REAL, [x])
    assert is_generic(nz_atom)
    assert not is_generic(real_atom)
    nz_guard = to_guard(s, profile, nz_atom, tb)
    assert nz_guard.same_as(guard[1])

    cases = class_split(s, profile, s.base(), x)
    assert len(cases) == 4
    assert cases[0].witness is None
    assert cases[3].witness is not None

    lit_steps = literal_facts(s, profile, Fraction(1, 2), tb)
    assert len(lit_steps) == 4
    print("Guard atom generic:", is_generic(nz_atom), "| class_split cases:", len(cases), "| literal_facts for 1/2:", len(lit_steps))

    # 3. Polynomial reification proves algebraic identities under atom finiteness.
    poly = build_scalar_poly(profile, budget)
    ps = Session.open(poly.sealed_snapshot(), work=200_000_000)
    scalar_ty = ps.import_type("scalar.Scalar")
    a = ps.symbol("a", scalar_ty)
    b = ps.symbol("b", scalar_ty)
    p_plus = poly.constant(ps, "plus")
    p_times = poly.constant(ps, "times")
    p_one = poly.constant(ps, "one")
    p_pow = poly.constant(ps, "pow")
    p_finite = poly.constant(ps, "finite")

    two = p_plus(p_one, p_one)
    expanded = p_plus(p_plus(p_pow(a, two), p_times(two, p_times(a, b))), p_pow(b, two))
    squared = p_pow(p_plus(a, b), two)
    rb = Budget(50_000_000)
    reified = reify(ps, poly, ps.base(), squared, rb)
    assert len(reified.atoms) == 2
    id_step = identity(ps, poly, ps.base(), squared, expanded, rb)
    assert id_step is not None
    assert len(id_step.hypotheses()) == 0
    expected = implies(p_finite(reified.atoms[0]) & p_finite(reified.atoms[1]), squared == expanded)
    assert id_step.statement().same_as(expected)
    print("Polynomial reification atoms:", len(reified.atoms))
    print("Polynomial identity: proved (a + b)^2 = a^2 + 2ab + b^2 under finite(a) and finite(b)")


if __name__ == "__main__":
    main()
