"""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 judgment import Arithmetic, ArithmeticLaws, Expr, Fact, Session, TypeExpr, arrow, iff, implies, not_


def assume_ring_laws(
    mut s: Session, carrier: TypeExpr, zero: Expr, one: Expr, prefix: String,
) raises -> List[Tuple[String, Fact]]:
    """Assume the eight commutative ring laws on a carrier with bound arithmetic."""
    var names: List[String] = [
        "add_assoc", "add_comm", "add_zero",
        "mul_assoc", "mul_comm", "mul_one",
        "add_inverse", "left_distrib",
    ]
    var arities: List[Int] = [3, 2, 1, 3, 2, 1, 1, 3]
    var entries = List[Tuple[String, Fact]]()
    for i in range(len(names)):
        var law = s.axiom(prefix + "." + names[i])
        var x = law.var("x", carrier)
        var y = zero
        var z = zero
        if arities[i] > 1:
            y = law.var("y", carrier)
        if arities[i] > 2:
            z = law.var("z", carrier)
        var statement: Expr
        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((names[i], law.finish(statement)))
    return entries^


@fieldwise_init
struct ArithmeticTheory(Copyable):
    """The example's rational base profile and formal-polynomial profile."""

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


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

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

    var q_names: List[String] = [
        "lt_irrefl", "lt_trans", "lt_total", "lt_add", "lt_mul", "zero_lt_one", "le_iff", "mul_inverse",
    ]
    var q_arities: List[Int] = [1, 3, 2, 3, 2, 0, 2, 1]
    for i in range(len(q_names)):
        var law = s.axiom("Q." + q_names[i])
        var x = q_zero
        var y = q_zero
        var z = q_zero
        if q_arities[i] > 0:
            x = law.var("x", q_type)
        if q_arities[i] > 1:
            y = law.var("y", q_type)
        if q_arities[i] > 2:
            z = law.var("z", q_type)
        var statement: Expr
        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((q_names[i], law.finish(statement)))
    var rationals = q_profile.rationals("rationals", q_lt, q_le, q_inv, ArithmeticLaws(q_laws^))

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

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

    var p_names: List[String] = ["coeff_zero", "coeff_one", "coeff_add", "coeff_mul"]
    var p_arities: List[Int] = [0, 0, 2, 2]
    for i in range(len(p_names)):
        var law = s.axiom("P." + p_names[i])
        var a = q_zero
        var c = q_zero
        if p_arities[i] > 0:
            a = law.var("a", q_type)
        if p_arities[i] > 1:
            c = law.var("c", q_type)
        var statement: Expr
        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((p_names[i], law.finish(statement)))
    var polynomials = p_profile.polynomials("polynomials", rationals, p_coeff, p_indet, ArithmeticLaws(p_laws^))
    return ArithmeticTheory(q_type, rationals, p_type, polynomials)
