"""Explicit teaching assumptions for reusable exact arithmetic profiles.

These are assumed ordered-ring/field laws over an abstract carrier, not a
construction of the mathematical integers or rationals.
"""

from judgment_shell import Arithmetic, ArithmeticLaws, Expr, LibError, LibErrorKind, Session, TypeExpr, arrow, iff, implies, not_
from ring_assumptions import assume_commutative_ring_laws


@fieldwise_init
struct NumericSetup(Copyable):
    """Typed operations and the complete explicit law bundle."""

    var kind: TypeExpr
    var numbers: Arithmetic
    var lt: Expr
    var le: Expr
    var inv: Optional[Expr]
    var laws: ArithmeticLaws

    def register_integers(self, name: String) raises LibError -> Arithmetic:
        """Explicitly enable exact integers using this provider's operations and laws.

        Args:
            name: The client's fresh literal-kind name and private law-name prefix.

        Returns:
            The checked Arithmetic profile with canonical integer arithmetic enabled.

        Raises:
            LibError: For invalid operations, evidence, names, or exhausted work.
                Failure publishes no theory or profile change.
        """
        return self.numbers.integers(name, self.lt, self.le, self.laws)

    def register_rationals(self, name: String) raises LibError -> Arithmetic:
        """Explicitly enable exact rationals using this provider's operations and laws.

        Args:
            name: The client's fresh literal-kind name and private law-name prefix.

        Returns:
            The checked Arithmetic profile with canonical rational arithmetic enabled.

        Raises:
            LibError: UNSUPPORTED if this setup has no inverse; otherwise the
                registration's errors for invalid operations, evidence, names,
                or exhausted work. Failure publishes no theory or profile change.
        """
        if not self.inv:
            raise LibError(LibErrorKind.UNSUPPORTED, "rational registration requires a setup with an inverse")
        return self.numbers.rationals(name, self.lt, self.le, self.inv.value(), self.laws)


def numeric_setup(mut s: Session, prefix: String, rational: Bool) raises -> NumericSetup:
    """Declare operations and assume each law explicitly, before exact registration."""
    var T = s.declare_type(prefix + ".T")
    var zero = s.symbol(prefix + ".zero", T)
    var one = s.symbol(prefix + ".one", T)
    var binary = arrow(T, arrow(T, T))
    var add = s.symbol(prefix + ".add", binary)
    var mul = s.symbol(prefix + ".mul", binary)
    var neg = s.symbol(prefix + ".neg", arrow(T, T))
    var relation = arrow(T, arrow(T, s.prop_type()))
    var lt = s.symbol(prefix + ".lt", relation)
    var le = s.symbol(prefix + ".le", relation)
    var inv: Optional[Expr] = None
    if rational:
        inv = s.symbol(prefix + ".inv", arrow(T, T))
    var numbers = s.arithmetic(T, zero, one, add, mul, neg)
    var entries = assume_commutative_ring_laws(s, T, zero, one, prefix)
    var names: List[String] = ["lt_irrefl", "lt_trans", "lt_total", "lt_add", "lt_mul", "zero_lt_one", "le_iff"]
    var arities: List[Int] = [1, 3, 2, 3, 2, 0, 2]
    if rational:
        names.append("mul_inverse")
        arities.append(1)
    for i in range(len(names)):
        var law = s.axiom(prefix + "." + names[i])
        var x = zero
        var y = zero
        var z = zero
        if arities[i] > 0:
            x = law.var("x", T)
        if arities[i] > 1:
            y = law.var("y", T)
        if arities[i] > 2:
            z = law.var("z", T)
        var statement: Expr
        if i == 0:
            statement = not_(lt(x, x))
        elif i == 1:
            statement = implies(lt(x, y), implies(lt(y, z), lt(x, z)))
        elif i == 2:
            statement = lt(x, y) | ((x == y) | lt(y, x))
        elif i == 3:
            statement = implies(lt(x, y), lt(x + z, y + z))
        elif i == 4:
            statement = implies(lt(zero, x), implies(lt(zero, y), lt(zero, x * y)))
        elif i == 5:
            statement = lt(zero, one)
        elif i == 6:
            statement = iff(le(x, y), lt(x, y) | (x == y))
        else:
            statement = implies(not_(x == zero), x * inv.value()(x) == one)
        entries.append((names[i], law.finish(statement)))
    return NumericSetup(T, numbers, lt, le, inv, ArithmeticLaws(entries^))
