Part 4 — Symbolic math, Scalar & naturals

4.3 Guarded algebra, Scalar polynomials, and the theorem library

Tutorial sequence · Previous: 4.2 Extended complex numbers with Scalar

Goal: derive guarded rational-function equalities with transfer, translate ScalarAtom guards with to_guard and is_generic, split G1's four carrier cases with class_split, generate literal domain facts with literal_facts, and prove polynomial identities with reify and identity.

Guarded rational-function transfer

Consider

\[ \frac{x^2 - 1}{x - 1} = x + 1. \]

Over the extended complex carrier, this equation fails at \(x = 1\) (\(0/0 = \mathrm{indet} \neq 2\)) and at \(x = \tilde{\infty}\). transfer decides rational-function equality, computes the exact generic guard, and returns a kernel-checked derivation of guard → lhs = rhs.

Companion symbolic helpers

  • to_guard and is_generic: translate any F-5 ScalarAtom (FINITE, NONZERO, REAL, INTEGER, LT, LE, RE, IM, OFFCUT, UNWOUND) into a HOL proposition in the active session, and test whether an atom is generic (FINITE or NONZERO).
  • class_split: splits a Scalar expression into G1's four exhaustive carrier cases (ClassCase codes 0 finite, 1 Indeterminate, 2 ComplexInfinity, 3 directed infinity with its fixed finite nonzero witness).
  • literal_facts: derives four checked domain steps (real, finite, rational, and integer or its negation) for an exact rational value.

Polynomial reification and identities

build_scalar_poly installs the P-4 polynomial theory (reopened via Session.open(poly.sealed_snapshot())). Over arbitrary Scalar subexpressions (including opaque atoms), reify converts an expression into a canonical formal polynomial literal paired with a checked evaluation step, and identity proves finite(atoms) → lhs = rhs whenever both sides share the same canonical polynomial.

Mojo

transfer_and_poly.mojo Download
"""Prove guarded rational-function and polynomial identities, split carrier cases, and check literal facts."""
from apn_mojo import Integer, Rational
from judgment import (
    ProfileLevel, ScalarAtom, Session, TransferResult, build_scalar, build_scalar_poly, class_split,
    identity, implies, is_generic, literal_facts, reify, session, to_guard, transfer,
)
from std.testing import assert_equal, assert_false, assert_true
from termbank import Budget


def main() raises:
    var budget = Budget(1_000_000_000)
    var profile = build_scalar(budget, ProfileLevel.POWERS_AND_ROOTS)

    # 1. Rational-function transfer derives the exact generic guard and a checked proof.
    var s = session(profile, ["x"], work=200_000_000)
    var x = s.import_symbol("x")
    var zero = profile.constant(s, "zero")
    var one = profile.constant(s, "one")
    var plus = profile.constant(s, "plus")
    var times = profile.constant(s, "times")
    var neg = profile.constant(s, "neg")
    var inv = profile.constant(s, "inv")
    var finite = profile.constant(s, "finite")

    var x_minus_one = plus(x, neg(one))
    var lhs = times(plus(times(x, x), neg(one)), inv(x_minus_one))
    var rhs = plus(x, one)
    var tb = Budget(100_000_000)
    var proved = transfer(s, profile, s.base(), lhs, rhs, tb)
    assert_equal(Int(proved.kind()), Int(TransferResult.PROVED))
    var guard = proved.guard()
    assert_equal(len(guard), 2)
    assert_true(guard[0].same_as(finite(x)))
    assert_true(guard[1].same_as(~(x_minus_one == zero)))
    var step = proved.step()
    assert_equal(len(step.hypotheses()), 0)
    assert_true(step.statement().same_as(implies(guard[0] & guard[1], lhs == rhs)))
    print("Rational transfer guard atoms:", len(guard))
    print("Transfer guard:", guard[0].format(), "and", guard[1].format())

    # 2. Translate guard atoms, split G1's four carrier cases, and prove literal domain facts.
    var nz_atom = ScalarAtom(ScalarAtom.NONZERO, [x_minus_one])
    var real_atom = ScalarAtom(ScalarAtom.REAL, [x])
    assert_true(is_generic(nz_atom))
    assert_false(is_generic(real_atom))
    var nz_guard = to_guard(s, profile, nz_atom, tb)
    assert_true(nz_guard.same_as(guard[1]))

    var cases = class_split(s, profile, s.base(), x)
    assert_equal(len(cases), 4)
    assert_false(Bool(cases[0].witness))
    assert_true(Bool(cases[3].witness))

    var lit_steps = literal_facts(s, profile, Rational(Integer(1), Integer(2)), tb)
    assert_equal(len(lit_steps), 4)
    print("Guard atom generic:", is_generic(nz_atom), "| class_split cases:", len(cases), "| literal_facts for 1/2:", len(lit_steps))

    # 3. Polynomial reification proves algebraic identities under atom finiteness.
    var poly = build_scalar_poly(profile, budget)
    var ps = Session.open(poly.sealed_snapshot(), work=200_000_000)
    var scalar_ty = ps.import_type("scalar.Scalar")
    var a = ps.symbol("a", scalar_ty)
    var b = ps.symbol("b", scalar_ty)
    var p_plus = poly.constant(ps, "plus")
    var p_times = poly.constant(ps, "times")
    var p_one = poly.constant(ps, "one")
    var p_pow = poly.constant(ps, "pow")
    var p_finite = poly.constant(ps, "finite")

    var two = p_plus(p_one, p_one)
    var expanded = p_plus(p_plus(p_pow(a, two), p_times(two, p_times(a, b))), p_pow(b, two))
    var squared = p_pow(p_plus(a, b), two)
    var rb = Budget(50_000_000)
    var reified = reify(ps, poly, ps.base(), squared, rb)
    assert_equal(len(reified.atoms), 2)
    var id_step = identity(ps, poly, ps.base(), squared, expanded, rb)
    assert_true(Bool(id_step))
    assert_equal(len(id_step.value().hypotheses()), 0)
    var expected = implies(p_finite(reified.atoms[0]) & p_finite(reified.atoms[1]), squared == expanded)
    assert_true(id_step.value().statement().same_as(expected))
    print("Polynomial reification atoms:", len(reified.atoms))
    print("Polynomial identity: proved (a + b)^2 = a^2 + 2ab + b^2 under finite(a) and finite(b)")

Run from the repository root pixi run mojo run -I src docs/examples/scalar/transfer_and_poly.mojo

Output

Rational transfer guard atoms: 2
Transfer guard: scalar.finite(x) and ¬(… = scalar.zero)
Guard atom generic: True | class_split cases: 4 | literal_facts for 1/2: 4
Polynomial reification atoms: 2
Polynomial identity: proved (a + b)^2 = a^2 + 2ab + b^2 under finite(a) and finite(b)

Python

transfer_and_poly.py Download
"""Prove guarded rational-function and polynomial identities, split carrier cases, and check literal facts."""
from fractions import Fraction
from judgment import (
    Budget, ProfileLevel, ScalarAtom, Session, TransferResult, build_scalar, build_scalar_poly,
    class_split, identity, implies, is_generic, literal_facts, reify, session, to_guard, transfer,
)


def main():
    budget = Budget(1_000_000_000)
    profile = build_scalar(budget, ProfileLevel.POWERS_AND_ROOTS)

    # 1. Rational-function transfer derives the exact generic guard and a checked proof.
    s = session(profile, ["x"], work=200_000_000)
    x = s.import_symbol("x")
    zero = profile.constant(s, "zero")
    one = profile.constant(s, "one")
    plus = profile.constant(s, "plus")
    times = profile.constant(s, "times")
    neg = profile.constant(s, "neg")
    inv = profile.constant(s, "inv")
    finite = profile.constant(s, "finite")

    x_minus_one = plus(x, neg(one))
    lhs = times(plus(times(x, x), neg(one)), inv(x_minus_one))
    rhs = plus(x, one)
    tb = Budget(100_000_000)
    proved = transfer(s, profile, s.base(), lhs, rhs, tb)
    assert proved.kind() == TransferResult.PROVED
    guard = proved.guard()
    assert len(guard) == 2
    assert guard[0].same_as(finite(x))
    assert guard[1].same_as(~(x_minus_one == zero))
    step = proved.step()
    assert len(step.hypotheses()) == 0
    assert step.statement().same_as(implies(guard[0] & guard[1], lhs == rhs))
    print("Rational transfer guard atoms:", len(guard))
    print("Transfer guard:", guard[0].format(), "and", guard[1].format())

    # 2. Translate guard atoms, split G1's four carrier cases, and prove literal domain facts.
    nz_atom = ScalarAtom(ScalarAtom.NONZERO, [x_minus_one])
    real_atom = ScalarAtom(ScalarAtom.REAL, [x])
    assert is_generic(nz_atom)
    assert not is_generic(real_atom)
    nz_guard = to_guard(s, profile, nz_atom, tb)
    assert nz_guard.same_as(guard[1])

    cases = class_split(s, profile, s.base(), x)
    assert len(cases) == 4
    assert cases[0].witness is None
    assert cases[3].witness is not None

    lit_steps = literal_facts(s, profile, Fraction(1, 2), tb)
    assert len(lit_steps) == 4
    print("Guard atom generic:", is_generic(nz_atom), "| class_split cases:", len(cases), "| literal_facts for 1/2:", len(lit_steps))

    # 3. Polynomial reification proves algebraic identities under atom finiteness.
    poly = build_scalar_poly(profile, budget)
    ps = Session.open(poly.sealed_snapshot(), work=200_000_000)
    scalar_ty = ps.import_type("scalar.Scalar")
    a = ps.symbol("a", scalar_ty)
    b = ps.symbol("b", scalar_ty)
    p_plus = poly.constant(ps, "plus")
    p_times = poly.constant(ps, "times")
    p_one = poly.constant(ps, "one")
    p_pow = poly.constant(ps, "pow")
    p_finite = poly.constant(ps, "finite")

    two = p_plus(p_one, p_one)
    expanded = p_plus(p_plus(p_pow(a, two), p_times(two, p_times(a, b))), p_pow(b, two))
    squared = p_pow(p_plus(a, b), two)
    rb = Budget(50_000_000)
    reified = reify(ps, poly, ps.base(), squared, rb)
    assert len(reified.atoms) == 2
    id_step = identity(ps, poly, ps.base(), squared, expanded, rb)
    assert id_step is not None
    assert len(id_step.hypotheses()) == 0
    expected = implies(p_finite(reified.atoms[0]) & p_finite(reified.atoms[1]), squared == expanded)
    assert id_step.statement().same_as(expected)
    print("Polynomial reification atoms:", len(reified.atoms))
    print("Polynomial identity: proved (a + b)^2 = a^2 + 2ab + b^2 under finite(a) and finite(b)")


if __name__ == "__main__":
    main()

Run from the repository root PYTHONPATH=bindings/python:docs/examples/python/scalar pixi run python docs/examples/python/scalar/transfer_and_poly.py

Output

Rational transfer guard atoms: 2
Transfer guard: scalar.finite(x) and ¬(… = scalar.zero)
Guard atom generic: True | class_split cases: 4 | literal_facts for 1/2: 4
Polynomial reification atoms: 2
Polynomial identity: proved (a + b)^2 = a^2 + 2ab + b^2 under finite(a) and finite(b)
Operation Input Derived guard / output Result
transfer (x^2 - 1) / (x - 1) vs. x + 1 finite(x) and x - 1 ≠ 0 Checked Step for guard → lhs = rhs
to_guard / is_generic ScalarAtom(NONZERO, [x - 1]) ~(x - 1 = 0), generic = True HOL guard Expr
class_split x 4 ClassCase frames (witness on case 3) Exhaustive G1 case frames
literal_facts 1/2 real, finite, rational, ~integer 4 checked domain Steps
reify + identity (a + b)^2 vs. a^2 + 2ab + b^2 finite(a) and finite(b) Checked Step for guard → lhs = rhs

Every derived step is checked by judgment_core. When two expressions are not algebraically equal, transfer returns TransferResult.NOT_EQUAL and identity returns None. Over opaque atoms (for example, sin(x)^2 + cos(x)^2 vs. 1), identity returning None means the terms are not polynomially identical, not that they are mathematically unequal.

Minimal exact-arithmetic setup

When only exact rational folding and literal_facts are needed (without powers or polynomials), ProfileLevel.EXACT_ARITHMETIC builds substantially faster:

build_exact_arithmetic.mojo Download
"""Build a Scalar profile with exact rational arithmetic and prove literal domains."""
from apn_mojo import Integer, Rational
from judgment_core import LiteralValue
from judgment_shell import literal_at
from scalar import ProfileLevel, build_scalar, literal_facts, session
from std.testing import assert_equal, assert_true
from termbank import Budget


def main() raises:
    var budget = Budget(100_000_000)
    var profile = build_scalar(budget, ProfileLevel.EXACT_ARITHMETIC)
    var s = session(profile, work=10_000_000)
    var root = s.base()
    var kind = profile.literal_kind(s)
    var half = literal_at(s, root, kind, LiteralValue(Rational(Integer(1), Integer(2))), budget)
    var third = literal_at(s, root, kind, LiteralValue(Rational(Integer(1), Integer(3))), budget)
    var expected = literal_at(s, root, kind, LiteralValue(Rational(Integer(5), Integer(6))), budget)
    var plus = profile.constant(s, "plus")
    assert_true(plus(half, third).same_as(expected))
    var facts = literal_facts(s, profile, Rational(Integer(1), Integer(2)), budget)
    assert_equal(len(facts), 4)
    print("Exact arithmetic: 1/2 + 1/3 = 5/6; 4 checked domain facts for 1/2")

Run from the repository root pixi run mojo run -I src docs/examples/scalar/build_exact_arithmetic.mojo

Output

Exact arithmetic: 1/2 + 1/3 = 5/6; 4 checked domain facts for 1/2

Indexed library and HOL model

  • Indexed theorem library (ScalarLibrary.index): discrimination tree over 380+ checked theorems (arithmetic, order, domains, infinity, powers, elementary) plus validated CommutativeRing, OrderedField, and PolynomialEvaluation bundles (theorem library reference).
  • HOL model and analytic laws: build_model, build_bridges, build_classes, and build_analysis construct a conservative model of Scalar inside judgment_mathlib, discharging G1–G7 and proving 105 analytic identities (\(\sqrt{(-1)^2} = 1\), \(\log(e^z)\), inverse trigonometric ranges).

Exercise

In transfer_and_poly.mojo, change rhs from plus(x, one) to plus(x, plus(one, one)) (\(x + 2\)). What does transfer return?

Answer

TransferResult.NOT_EQUAL. Calling proved.step() then raises SHAPE.

Next: 4.4 Test conjectures numerically and find counterexamples.

judgment v1.0.0 for Mojo 1.2 · Apache License 2.0 with LLVM Exceptions · Source on GitHub