"""Use Scalar exact rational literals, ACU conversion, infinity theorems, and polynomial theory."""
from fractions import Fraction

from judgment import (
    Budget, LiteralValue, ProfileLevel, build_scalar, build_scalar_poly, default_context, session,
)


def main():
    budget = Budget(200_000_000)
    profile = build_scalar(budget, ProfileLevel.EXACT_ARITHMETIC)
    s = session(profile, ["x", "y"], work=100_000_000)
    _ = default_context(s)

    # Exact rational literals fold to the same canonical node during construction.
    kind = profile.literal_kind(s)
    half = s.literal(kind, LiteralValue(Fraction(1, 2)))
    third = s.literal(kind, LiteralValue(Fraction(1, 3)))
    five_sixths = s.literal(kind, LiteralValue(Fraction(5, 6)))
    plus = profile.constant(s, "plus")
    one = profile.constant(s, "one")
    assert plus(half, third).same_as(five_sixths)
    print("Exact rational folding: 1/2 + 1/3 matches", five_sixths.format())

    # ACU operators identify reordered sums by reflexivity.
    x = s.import_symbol("x")
    y = s.import_symbol("y")
    reorder = s.theorem("reorder_sum", plus(x, plus(y, one)) == plus(plus(one, x), y))
    reorder.rfl()
    reordered = reorder.finish()
    assert len(reordered.admissions()) == 0
    print("ACU reflexivity: proved x + (y + 1) = (1 + x) + y")

    # Extended-complex conventions are explicit checked theorems.
    one_div_zero = profile.fact(s, "one_div_zero")
    zero_div_zero = profile.fact(s, "zero_div_zero")
    zero_times_inf = profile.fact(s, "zero_times_infinity")
    assert len(one_div_zero.admissions()) == 0
    assert len(zero_div_zero.admissions()) == 0
    assert len(zero_times_inf.admissions()) == 0
    print("1/0 theorem:", one_div_zero.statement().format())
    print("0/0 theorem:", zero_div_zero.statement().format())
    print("0 * inf theorem:", zero_times_inf.statement().format())

    # Extend the profile with the formal polynomial theory over Scalar.
    poly = build_scalar_poly(profile, budget)
    del poly
    print("Built Scalar polynomial extension: ok")


if __name__ == "__main__":
    main()
