"""An explicit teaching theory: rational and polynomial arithmetic with admitted laws.

These helpers only call the public Session API. They declare the field and
polynomial laws as AXIOMS, not as proved facts or imported library theorems.
The complete assumptions are visible below. This is example setup, not a
bundled standard number theory or a replacement for a theorem library.
"""

from dataclasses import dataclass

from judgment import Arithmetic, ArithmeticLaws, Session, TypeExpr, arrow, iff, implies, not_


def assume_ring_laws(s, carrier, zero, one, prefix):
    """Assume the eight commutative ring laws on a carrier with bound arithmetic."""
    names = ["add_assoc", "add_comm", "add_zero", "mul_assoc", "mul_comm", "mul_one", "add_inverse", "left_distrib"]
    arities = [3, 2, 1, 3, 2, 1, 1, 3]
    entries = []
    for i, name in enumerate(names):
        law = s.axiom(prefix + "." + name)
        x = law.var("x", carrier)
        y = law.var("y", carrier) if arities[i] > 1 else zero
        z = law.var("z", carrier) if arities[i] > 2 else zero
        if i == 0:
            statement = (x + y) + z == x + (y + z)
        elif i == 1:
            statement = x + y == y + x
        elif i == 2:
            statement = zero + x == x
        elif i == 3:
            statement = (x * y) * z == x * (y * z)
        elif i == 4:
            statement = x * y == y * x
        elif i == 5:
            statement = one * x == x
        elif i == 6:
            statement = x + (-x) == zero
        else:
            statement = x * (y + z) == x * y + x * z
        entries.append((name, law.finish(statement)))
    return entries


@dataclass
class ArithmeticTheory:
    """The example's rational base profile and formal-polynomial profile."""

    rational_type: TypeExpr
    rational: Arithmetic
    polynomial_type: TypeExpr
    polynomial: Arithmetic


def arithmetic_theory(s: Session) -> ArithmeticTheory:
    """Build the teaching theory, with every mathematical law explicitly admitted."""
    q_type = s.declare_type("Q.T")
    q_zero = s.symbol("Q.zero", q_type)
    q_one = s.symbol("Q.one", q_type)
    q_bin = arrow(q_type, arrow(q_type, q_type))
    q_add = s.symbol("Q.add", q_bin)
    q_mul = s.symbol("Q.mul", q_bin)
    q_neg = s.symbol("Q.neg", arrow(q_type, q_type))
    q_cmp = arrow(q_type, arrow(q_type, s.prop_type()))
    q_lt = s.symbol("Q.lt", q_cmp)
    q_le = s.symbol("Q.le", q_cmp)
    q_inv = s.symbol("Q.inv", arrow(q_type, q_type))

    q_profile = s.arithmetic(q_type, q_zero, q_one, q_add, q_mul, q_neg)
    q_laws = assume_ring_laws(s, q_type, q_zero, q_one, "Q")

    q_names = ["lt_irrefl", "lt_trans", "lt_total", "lt_add", "lt_mul", "zero_lt_one", "le_iff", "mul_inverse"]
    q_arities = [1, 3, 2, 3, 2, 0, 2, 1]
    for i, name in enumerate(q_names):
        law = s.axiom("Q." + name)
        x = law.var("x", q_type) if q_arities[i] > 0 else q_zero
        y = law.var("y", q_type) if q_arities[i] > 1 else q_zero
        z = law.var("z", q_type) if q_arities[i] > 2 else q_zero
        if i == 0:
            statement = not_(q_lt(x, x))
        elif i == 1:
            statement = implies(q_lt(x, y), implies(q_lt(y, z), q_lt(x, z)))
        elif i == 2:
            statement = q_lt(x, y) | ((x == y) | q_lt(y, x))
        elif i == 3:
            statement = implies(q_lt(x, y), q_lt(x + z, y + z))
        elif i == 4:
            statement = implies(q_lt(q_zero, x), implies(q_lt(q_zero, y), q_lt(q_zero, x * y)))
        elif i == 5:
            statement = q_lt(q_zero, q_one)
        elif i == 6:
            statement = iff(q_le(x, y), q_lt(x, y) | (x == y))
        else:
            statement = implies(not_(x == q_zero), x * q_inv(x) == q_one)
        q_laws.append((name, law.finish(statement)))
    rationals = q_profile.rationals("rationals", q_lt, q_le, q_inv, ArithmeticLaws(q_laws))

    p_type = s.declare_type("P.T")
    p_zero = s.symbol("P.zero", p_type)
    p_one = s.symbol("P.one", p_type)
    p_bin = arrow(p_type, arrow(p_type, p_type))
    p_add = s.symbol("P.add", p_bin)
    p_mul = s.symbol("P.mul", p_bin)
    p_neg = s.symbol("P.neg", arrow(p_type, p_type))
    p_coeff = s.symbol("P.coeff", arrow(q_type, p_type))
    p_indet = s.symbol("P.indeterminate", arrow(q_type, p_type))

    p_profile = s.arithmetic(p_type, p_zero, p_one, p_add, p_mul, p_neg)
    p_laws = assume_ring_laws(s, p_type, p_zero, p_one, "P")

    p_names = ["coeff_zero", "coeff_one", "coeff_add", "coeff_mul"]
    p_arities = [0, 0, 2, 2]
    for i, name in enumerate(p_names):
        law = s.axiom("P." + name)
        a = law.var("a", q_type) if p_arities[i] > 0 else q_zero
        c = law.var("c", q_type) if p_arities[i] > 1 else q_zero
        if i == 0:
            statement = p_coeff(rationals.number(0)) == p_zero
        elif i == 1:
            statement = p_coeff(rationals.number(1)) == p_one
        elif i == 2:
            statement = p_coeff(a + c) == p_coeff(a) + p_coeff(c)
        else:
            statement = p_coeff(a * c) == p_coeff(a) * p_coeff(c)
        p_laws.append((name, law.finish(statement)))
    polynomials = p_profile.polynomials("polynomials", rationals, p_coeff, p_indet, ArithmeticLaws(p_laws))
    return ArithmeticTheory(q_type, rationals, p_type, polynomials)
