"""Use Scalar exact rational literals, ACU conversion, infinity theorems, and polynomial theory."""
from apn_mojo import Integer, Rational
from judgment import (
    ProfileLevel, build_scalar, build_scalar_poly, default_context, session,
)
from judgment_core import LiteralValue
from std.testing import assert_equal, assert_true
from termbank import Budget


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

    # Exact rational literals fold to the same canonical node during construction.
    var kind = profile.literal_kind(s)
    var half = s.literal(kind, LiteralValue(Rational(Integer(1), Integer(2))))
    var third = s.literal(kind, LiteralValue(Rational(Integer(1), Integer(3))))
    var five_sixths = s.literal(kind, LiteralValue(Rational(Integer(5), Integer(6))))
    var plus = profile.constant(s, "plus")
    var one = profile.constant(s, "one")
    assert_true(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.
    var x = s.import_symbol("x")
    var y = s.import_symbol("y")
    var reorder = s.theorem("reorder_sum", plus(x, plus(y, one)) == plus(plus(one, x), y))
    reorder.rfl()
    var reordered = reorder.finish()
    assert_equal(len(reordered.admissions()), 0)
    print("ACU reflexivity: proved x + (y + 1) = (1 + x) + y")

    # Extended-complex conventions are explicit checked theorems.
    var one_div_zero = profile.fact(s, "one_div_zero")
    var zero_div_zero = profile.fact(s, "zero_div_zero")
    var zero_times_inf = profile.fact(s, "zero_times_infinity")
    assert_equal(len(one_div_zero.admissions()), 0)
    assert_equal(len(zero_div_zero.admissions()), 0)
    assert_equal(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.
    var poly = build_scalar_poly(profile, budget)
    _ = poly^
    print("Built Scalar polynomial extension: ok")
