"""Shared, explicitly assumed commutative ring laws for arithmetic examples."""

from judgment_shell import Expr, Fact, LibError, Session, TypeExpr


def assume_commutative_ring_laws(
    mut s: Session, kind: TypeExpr, zero: Expr, one: Expr, prefix: String,
) raises LibError -> List[Tuple[String, Fact]]:
    """Declare eight named axioms using the carrier's bound arithmetic notation.

    Addition, multiplication, and negation must already be bound. Each axiom is
    published as prefix.slot. The returned entries can be extended with order
    or coefficient laws before constructing ArithmeticLaws. This assumes laws;
    it does not prove them or register exact 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", kind)
        var y = zero
        var z = zero
        if arities[i] > 1:
            y = law.var("y", kind)
        if arities[i] > 2:
            z = law.var("z", kind)
        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^
