Part 4 — Symbolic math, Scalar & naturals

4.2 Extended complex numbers with Scalar

Tutorial sequence · Previous: 4.1 Exact arithmetic and polynomials

Goal: construct judgment's extended-complex Scalar profile, open a Scalar session with finite symbolic variables, use exact rational literals and ACU conversion, and inspect checked theorems from the profile and its polynomial extension.

Whereas Lesson 4.1 declared a minimal arithmetic theory by hand, the scalar package provides a complete, explicitly axiomatized profile of extended complex numbers built directly on Session and judgment_core.

The Scalar carrier

Rather than handling division by zero, infinity, and branch cuts through silent conventions, scalar places every exceptional value in one total carrier type scalar.Scalar:

Carrier class Examples Semantics
Finite complex numbers \(0\), \(1\), \(1/2\), \(-3\), \(i\), \(\pi\) Standard complex field arithmetic; satisfies scalar.finite(x).
Directed infinities \(+\infty\), \(-\infty\), \(+i\infty\), \(\mathrm{dinf}(u)\) Infinity with unit direction \(u\) (e.g., \(\tanh(+\infty) = 1\)).
Complex infinity scalar.cinf (\(\tilde{\infty}\)) Unsigned infinity: \(1/0 = \tilde{\infty}\), \(\mathrm{arccot}(\tilde{\infty}) = 0\).
Indeterminate scalar.indet Explicit undefined value: \(0/0 = \mathrm{indet}\), \((+\infty) - (+\infty) = \mathrm{indet}\).

Because all operations are total on one carrier, generic algebraic laws carry explicit domain guards (for instance, \(x / x = 1\) holds under finite(x) ∧ x ≠ 0).

Profile levels and session setup

build_scalar(budget, level) constructs and seals a ScalarProfile up to the requested ProfileLevel:

  • ProfileLevel.FOUNDATION: classical HOL foundation, axioms G1–G7, and carrier definitions.
  • ProfileLevel.EXACT_ARITHMETIC: ACU operators (plus, times), domain predicates (finite, real, rational, integer, positive), and exact rational literals.
  • ProfileLevel.POWERS_AND_ROOTS: powers, roots, and rational-function transfer lemmas.
  • ProfileLevel.ALL (equivalent to ProfileLevel.ELEMENTARY_FUNCTIONS): all 24 elementary functions (exp, log, sin, cos, tan, sinh, cosh, tanh, and their inverses), 14 branch-cut predicates, and 282 formula and infinity-limit theorems.

Mojo

build_foundation.mojo Download
"""Build the basic Scalar profile and give a session symbol its default finiteness assumption."""
from scalar import ProfileLevel, build_scalar, default_context, session
from std.testing import assert_equal, assert_false
from termbank import Budget


def main() raises:
    var budget = Budget(10_000_000)
    var profile = build_scalar(budget, ProfileLevel.FOUNDATION)
    assert_false(profile.has_literal_kind())
    var s = session(profile, ["x"], work=1_000_000)
    _ = default_context(s)
    assert_equal(len(profile.definitions()), 8)
    assert_equal(len(profile.theorems()), 8)
    print("Foundation: 8 definitions, 8 proofs, 1 finite session symbol")

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

Output

Foundation: 8 definitions, 8 proofs, 1 finite session symbol

Python

build_foundation.py Download
"""Build the basic Scalar profile and give a session symbol its default finiteness assumption."""
from judgment import Budget, ProfileLevel, build_scalar, default_context, session


def main():
    budget = Budget(10_000_000)
    profile = build_scalar(budget, ProfileLevel.FOUNDATION)
    assert not profile.has_literal_kind()
    s = session(profile, ["x"], work=1_000_000)
    _ = default_context(s)
    assert len(profile.definitions()) == 8
    assert len(profile.theorems()) == 8
    print("Foundation: 8 definitions, 8 proofs, 1 finite session symbol")


main()

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

Output

Foundation: 8 definitions, 8 proofs, 1 finite session symbol

session(profile, ["x"], work=...) opens a fresh Session over the sealed profile snapshot, declares x : scalar.Scalar, and initializes default_context(s) with the local hypothesis scalar.finite(x) (not a global axiom).

Exact literals, ACU conversion, infinity theorems, and polynomials

Building ProfileLevel.EXACT_ARITHMETIC together with build_scalar_poly enables four mechanisms in one session:

Mojo

exact_and_library.mojo Download
"""Use Scalar exact rational literals, ACU conversion, infinity theorems, and polynomial theory."""
from apn_mojo import Integer, Rational
from judgment import (
    ProfileLevel, build_scalar, build_scalar_poly, default_context, session,
)
from judgment_core import LiteralValue
from std.testing import assert_equal, assert_true
from termbank import Budget


def main() raises:
    var budget = Budget(200_000_000)
    var profile = build_scalar(budget, ProfileLevel.EXACT_ARITHMETIC)
    var s = session(profile, ["x", "y"], work=100_000_000)
    _ = default_context(s)

    # Exact rational literals fold to the same canonical node during construction.
    var kind = profile.literal_kind(s)
    var half = s.literal(kind, LiteralValue(Rational(Integer(1), Integer(2))))
    var third = s.literal(kind, LiteralValue(Rational(Integer(1), Integer(3))))
    var five_sixths = s.literal(kind, LiteralValue(Rational(Integer(5), Integer(6))))
    var plus = profile.constant(s, "plus")
    var one = profile.constant(s, "one")
    assert_true(plus(half, third).same_as(five_sixths))
    print("Exact rational folding: 1/2 + 1/3 matches", five_sixths.format())

    # ACU operators identify reordered sums by reflexivity.
    var x = s.import_symbol("x")
    var y = s.import_symbol("y")
    var reorder = s.theorem("reorder_sum", plus(x, plus(y, one)) == plus(plus(one, x), y))
    reorder.rfl()
    var reordered = reorder.finish()
    assert_equal(len(reordered.admissions()), 0)
    print("ACU reflexivity: proved x + (y + 1) = (1 + x) + y")

    # Extended-complex conventions are explicit checked theorems.
    var one_div_zero = profile.fact(s, "one_div_zero")
    var zero_div_zero = profile.fact(s, "zero_div_zero")
    var zero_times_inf = profile.fact(s, "zero_times_infinity")
    assert_equal(len(one_div_zero.admissions()), 0)
    assert_equal(len(zero_div_zero.admissions()), 0)
    assert_equal(len(zero_times_inf.admissions()), 0)
    print("1/0 theorem:", one_div_zero.statement().format())
    print("0/0 theorem:", zero_div_zero.statement().format())
    print("0 * inf theorem:", zero_times_inf.statement().format())

    # Extend the profile with the formal polynomial theory over Scalar.
    var poly = build_scalar_poly(profile, budget)
    _ = poly^
    print("Built Scalar polynomial extension: ok")

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

Output

Exact rational folding: 1/2 + 1/3 matches 5/6
ACU reflexivity: proved x + (y + 1) = (1 + x) + y
1/0 theorem: scalar.inv(scalar.zero) = scalar.cinf
0/0 theorem: scalar.times(scalar.zero, scalar.inv(scalar.zero)) = scalar.indet
0 * inf theorem: scalar.times(scalar.zero, scalar.dinf(scalar.one)) = scalar.indet
Built Scalar polynomial extension: ok

Python

exact_and_library.py Download
"""Use Scalar exact rational literals, ACU conversion, infinity theorems, and polynomial theory."""
from fractions import Fraction

from judgment import (
    Budget, LiteralValue, ProfileLevel, build_scalar, build_scalar_poly, default_context, session,
)


def main():
    budget = Budget(200_000_000)
    profile = build_scalar(budget, ProfileLevel.EXACT_ARITHMETIC)
    s = session(profile, ["x", "y"], work=100_000_000)
    _ = default_context(s)

    # Exact rational literals fold to the same canonical node during construction.
    kind = profile.literal_kind(s)
    half = s.literal(kind, LiteralValue(Fraction(1, 2)))
    third = s.literal(kind, LiteralValue(Fraction(1, 3)))
    five_sixths = s.literal(kind, LiteralValue(Fraction(5, 6)))
    plus = profile.constant(s, "plus")
    one = profile.constant(s, "one")
    assert plus(half, third).same_as(five_sixths)
    print("Exact rational folding: 1/2 + 1/3 matches", five_sixths.format())

    # ACU operators identify reordered sums by reflexivity.
    x = s.import_symbol("x")
    y = s.import_symbol("y")
    reorder = s.theorem("reorder_sum", plus(x, plus(y, one)) == plus(plus(one, x), y))
    reorder.rfl()
    reordered = reorder.finish()
    assert len(reordered.admissions()) == 0
    print("ACU reflexivity: proved x + (y + 1) = (1 + x) + y")

    # Extended-complex conventions are explicit checked theorems.
    one_div_zero = profile.fact(s, "one_div_zero")
    zero_div_zero = profile.fact(s, "zero_div_zero")
    zero_times_inf = profile.fact(s, "zero_times_infinity")
    assert len(one_div_zero.admissions()) == 0
    assert len(zero_div_zero.admissions()) == 0
    assert len(zero_times_inf.admissions()) == 0
    print("1/0 theorem:", one_div_zero.statement().format())
    print("0/0 theorem:", zero_div_zero.statement().format())
    print("0 * inf theorem:", zero_times_inf.statement().format())

    # Extend the profile with the formal polynomial theory over Scalar.
    poly = build_scalar_poly(profile, budget)
    del poly
    print("Built Scalar polynomial extension: ok")


if __name__ == "__main__":
    main()

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

Output

Exact rational folding: 1/2 + 1/3 matches 5/6
ACU reflexivity: proved x + (y + 1) = (1 + x) + y
1/0 theorem: scalar.inv(scalar.zero) = scalar.cinf
0/0 theorem: scalar.times(scalar.zero, scalar.inv(scalar.zero)) = scalar.indet
0 * inf theorem: scalar.times(scalar.zero, scalar.dinf(scalar.one)) = scalar.indet
Built Scalar polynomial extension: ok
  1. Exact rational folding: plus(half, third) reduces on construction to 5/6, confirmed by same_as.
  2. ACU reflexivity: plus is registered with unit zero, so x + (y + 1) = (1 + x) + y closes by rfl().
  3. Explicit infinity laws: profile.fact(s, "one_div_zero"), zero_div_zero, and zero_times_infinity supply 1/0 = ComplexInfinity, 0/0 = Indeterminate, and 0 * (+∞) = Indeterminate as checked facts.
  4. Formal polynomials: build_scalar_poly(profile, budget) builds and seals the P-4 polynomial theory over Scalar. For the full 24 elementary functions and 380+ algebraic and infinity-limit theorems (such as div_self_finite and tanh_at_positive_infinity), build ProfileLevel.ALL and call build_library(profile, budget, poly).

Profile axioms (scalar.G1–G7 and classical.*) are tagged PROFILE_AXIOM. In the HOL model and bridges, every scalar axiom is discharged conservatively from Isabelle/HOL's judgment_mathlib.

Exercise

In exact_and_library, change ProfileLevel.EXACT_ARITHMETIC to ProfileLevel.FOUNDATION. Which call fails first, and why?

Answer

profile.literal_kind(s) raises NO_MATCH, because FOUNDATION does not install exact rational literals or ACU operators. Use at least ProfileLevel.EXACT_ARITHMETIC for literals and ProfileLevel.ALL for build_library.

Next: 4.3 Guarded algebra, Scalar polynomials, and the theorem library.

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