Part 4 — Symbolic math, Scalar & naturals

4.1a Declare the arithmetic theory

Tutorial sequence · Back to: 4.1 Exact arithmetic and polynomial literals

This is the complete setup module used by the arithmetic tutorial. It declares its assumptions through the same public Session API an application uses. It imports no test fixtures or private modules.

The module declares two carriers, Q.T and P.T, with declare_type, and their operations with symbol. s.arithmetic(carrier, zero, one, add, mul, neg) binds the notation +, *, and - to those operations without assuming any law. Each law is then admitted with an axiom header: s.axiom(name) declares the law's term parameters with var, and finish(statement) states it. These are explicit assumptions; their names do not imply that a proof has been supplied.

Exact literals are enabled by passing the laws, by slot name, in an ArithmeticLaws bundle:

Registration Slots Assumptions supplied here
rationals(name, lt, le, inv, laws) on Q.T sixteen the eight ring laws (add_assoc, add_comm, add_zero, mul_assoc, mul_comm, mul_one, add_inverse, left_distrib), the seven order laws (lt_irrefl to le_iff), and the guarded inverse law mul_inverse
polynomials(name, base, coeff, indet, laws) on P.T twelve the eight ring laws and preservation of zero, one, addition, and multiplication by the coefficient embedding (coeff_zero to coeff_mul)

On each carrier the associativity, commutativity, and unit laws of addition and multiplication register the two operations as canonical ACU operators. The remaining rational laws are R6–R7, O1–O7, and F2 of Definition C-5; the remaining polynomial laws are Q1–Q6 of Definition C-5P. Both carriers have no domain predicate, so the domain-closure laws are omitted. Neither kind binds a power operation, so this setup needs no exponent or power laws. A missing, extra, or misstated slot is refused, and the literal implementations remain a closed set: the module installs no arithmetic callback.

Read assume_ring_laws and arithmetic_theory first for the declaration order. They show the exact propositions cited by the two registrations.

Mojo

arithmetic_theory.mojo Download
"""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)

Python

arithmetic_theory.py Download
"""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)
judgment v1.0.0 for Mojo 1.2 · Apache License 2.0 with LLVM Exceptions · Source on GitHub