Scalar

Numeric evaluation and filtering

The scalar.numeric and scalar.filter modules, also exported from scalar, provide testing evidence for expressions built from a scalar profile. Each call takes a live session opened over the profile's snapshot, or over a snapshot that extends it. The calls add no declarations and prove nothing. They accept profiles built with ProfileLevel.FOUNDATION or later (before ProfileLevel.EXACT_ARITHMETIC there are no rational literals); numeric evaluation is a separate service, just as the polynomial theory and theorem library have separate builders.

Each call reads the profile's declarations into the session in a transaction of its own and charges the session's work allowance, set when the session is opened; there is no other limit on the number of calls a session takes, and an exhausted allowance raises LIMIT. The Python client exposes the same calls and records through the prebuilt core.

Exact assignments and results

ExactValue(re, im, pi_re, pi_im, roots) represents re + i*im + (pi_re + i*pi_im)*pi + sum(q*sqrt(r)). All coefficients and radicands are apn_mojo.Rational; omitted coordinates are zero. Each root pair is (q, r). A negative radicand uses the principal imaginary square root. ExactPoint(assignment) holds (Expr, ExactValue) pairs. Each key must be a live, monomorphic constant of the session, of type Scalar, that is not a profile declaration, and occur once. An omitted assignment is empty.

def evaluate(session: Session, profile: ScalarProfile, term: Expr, at: ExactPoint,
             precision: Int = 64, max_precision: Int = 512) raises LibError -> NumericValue

Precisions are in bits, starting at two or more. Unresolved cuts, zeros, and infinity directions cause a retry at twice the precision, including a final retry at max_precision when it is not a power-of-two multiple of the start. Unsupported functions, free locals, and unassigned constants are inconclusive. Malformed settings, wrong types, invalid handles, and invalid assignments raise LibError; numeric capacity failures retain a diagnostic under LIMIT.

NumericValue.kind Active fields
BALL ball: Optional[ComplexBall]
INFINITE infinite: Optional[InfiniteClass]
INCONCLUSIVE reason: String

InfiniteClass.kind is DIRECTED, COMPLEX_INFINITY, or INDETERMINATE. Only DIRECTED has a direction: Optional[ComplexBall], enclosing the unit direction. INDETERMINATE is an actual carrier value, distinct from missing numeric information. The inactive optional fields are absent.

apn_mojo's ComplexBall is a rectangle of two real balls. Exact rational, π-linear, and radical coordinate information is retained alongside enclosures where arithmetic preserves it. This distinguishes an exact boundary from a small interval crossing that boundary. Unresolved information is never rounded onto a cut. Derived exact metadata is optional and capped at 4,096 bits of coefficients/radicands and 64 radical cross-products; dropping it retains the ball enclosure and may make a boundary decision inconclusive. The extended carrier's arithmetic, powers, roots, exponential, logarithm, and all 24 elementary functions use the profile's specified values.

Guard decisions

def decide(session: Session, profile: ScalarProfile, atom: ScalarAtom, at: ExactPoint,
           precision: Int = 64, max_precision: Int = 512) raises LibError -> GuardDecision

GuardDecision.HOLDS, .FAILS, and .UNDECIDED implement the F-5 atoms, including extended-real order and the distinct closures of each cut. ScalarAtom's arities, coordinate-bound restrictions, and function identities are validated. None of these decisions can discharge a proof guard; that still requires kernel evidence.

def refute(session: Session, profile: ScalarProfile, lhs: Expr, rhs: Expr, guard: Expr,
           variables: List[Expr], config: FilterConfig = FilterConfig()) raises LibError -> FilterVerdict

The variables are distinct Scalar constants of the session. The guard is a proposition of the session over those variables and the profile's constants. Unknown guard propositions remain undecided.

FilterConfig has these defaults:

Field Default Meaning
seed 0x5CA1A7 SplitMix64 state
random_points 256 Random count; also the cap on a multivariable structured product
scales [1, 4, 16] Positive rational box half-widths, cycled in order
precision 80 Initial precision in bits
max_precision 320 Maximum retry precision
max_undecided_fraction 0.1 Allowed unresolved fraction of attempted points
workers 0 Numeric workers: zero selects available logical cores, up to 64; 1–64 selects explicitly

Independent assignments run in bounded fp_mojo worker jobs over one immutable compiled program. Assignment generation and outcome selection retain sample order: the seed, first counterexample, and reported counts are unchanged by worker count. At most eight assignments per worker are in flight; speculative later results do not change a returned earlier refutation.

Sampling visits cell interiors, cut pieces and branch points, exact affine singularities, and guard boundaries before seeded random points. Rational complex affine changes of coordinates transport cuts and poles; common π factors cancel when the resulting exact points are representable. Arbitrary nonlinear preimages are not solved. A line arrangement supplies representatives on both sides of its boundaries. Multivariable products interleave equal-rank assignments with breadth-first order by the sum of per-variable sample ranks. This gives thin real-axis guards early coverage within deterministic truncation.

FilterVerdict.kind Result
PASSED points guard-satisfying, conclusive samples were checked
REFUTED witness, lhs, and rhs retain the first exact assignment and both results
INCONCLUSIVE undecided and total report unresolved and attempted samples

total includes points discarded because their guard was false. A search with no conclusive, guard-satisfying sample is inconclusive. PASSED establishes nothing about a rule's universal truth. Disjoint finite enclosures, distinct finite/nonfinite classes, or certified distinct infinities can refute it.

See the numeric theory and numeric semantics contract.

Declarations

ExactValue

Mojo

struct ExactValue(Copyable)
Field Type Description
re Rational Rational real part.
im Rational Rational imaginary part.
pi_re Rational Coefficient of pi in the real part.
pi_im Rational Coefficient of pi in the imaginary part.
roots List[Tuple[Rational, Rational]] Pairs (coefficient, radicand); negative radicands contribute to the imaginary part.

Python

from judgment import ExactValue

Python constructor:

def __init__(self, re=0, im=0, pi_re=0, pi_im=0, roots=[])
Field Description
re Rational real part.
im Rational imaginary part.
pi_re Coefficient of pi in the real part.
pi_im Coefficient of pi in the imaginary part.
roots Pairs (coefficient, radicand); negative radicands contribute to the imaginary part.

The exact point a+bi+(c+di)pi plus real multiples of principal roots.

Defined in src/scalar/_numeric_types.mojo.

ExactValue.__init__

Mojo

def __init__(out self, re: Rational = Rational(0), im: Rational = Rational(0), pi_re: Rational = Rational(0), pi_im: Rational = Rational(0), var roots: List[Tuple[Rational, Rational]] = [])

Python

Use the Python constructor shown above. Python does not mirror Mojo constructor overloads.

Record exact coordinates.

Arguments.

  • re: Rational real part.
  • im: Rational imaginary part.
  • pi_re: Real coefficient of pi.
  • pi_im: Imaginary coefficient of pi.
  • roots: Rational multiples of principal square roots.

ExactPoint

Mojo

struct ExactPoint(Copyable)
Field Type Description
assignment List[Tuple[DagExpr, ExactValue]] Ordered pairs mapping session constant expressions to exact values.

Python

from judgment import ExactPoint

Python constructor:

def __init__(self, assignment=[])
Field Description
assignment Ordered pairs mapping session constant expressions to exact values.

An ordered exact assignment, with live session handles checked when a numeric operation consumes it.

Defined in src/scalar/_dag_numeric_api.mojo.

ExactPoint.__init__

Mojo

def __init__(out self, var assignment: List[Tuple[DagExpr, ExactValue]] = [])

Python

Use the Python constructor shown above. Python does not mirror Mojo constructor overloads.

Construct an exact assignment from ordered (Expr, ExactValue) pairs.

Arguments.

  • assignment: Ordered constant-to-value pairs.

InfiniteClass

Mojo

@fieldwise_init
struct InfiniteClass(Copyable)
Field Type Description
kind UInt8 DIRECTED, COMPLEX_INFINITY, or INDETERMINATE.
direction Optional[ComplexBall] Present exactly for DIRECTED.

Python

from judgment import InfiniteClass

Python constructor:

def __init__(self, kind, direction)
Field Description
kind DIRECTED, COMPLEX_INFINITY, or INDETERMINATE.
direction Present exactly for DIRECTED.

A nonfinite scalar value; directed values enclose a unit direction.

Defined in src/scalar/_numeric_types.mojo.

InfiniteClass.DIRECTED

Mojo

comptime DIRECTED = UInt8(0)

Python

InfiniteClass.DIRECTED

A directed infinity.

InfiniteClass.COMPLEX_INFINITY

Mojo

comptime COMPLEX_INFINITY = UInt8(1)

Python

InfiniteClass.COMPLEX_INFINITY

ComplexInfinity.

InfiniteClass.INDETERMINATE

Mojo

comptime INDETERMINATE = UInt8(2)

Python

InfiniteClass.INDETERMINATE

The carrier's Indeterminate, distinct from missing numeric information.

NumericValue

Mojo

@fieldwise_init
struct NumericValue(Copyable)
Field Type Description
kind UInt8 BALL, INFINITE, or INCONCLUSIVE.
ball Optional[ComplexBall] Finite enclosure, present exactly for BALL.
infinite Optional[InfiniteClass] Nonfinite classification, present exactly for INFINITE.
reason String Explanation for INCONCLUSIVE; empty for a classified value.

Python

from judgment import NumericValue

Python constructor:

def __init__(self, kind, ball, infinite, reason)
Field Description
kind BALL, INFINITE, or INCONCLUSIVE.
ball Finite enclosure, present exactly for BALL.
infinite Nonfinite classification, present exactly for INFINITE.
reason Explanation for INCONCLUSIVE; empty for a classified value.

Numeric testing evidence; never a checked judgment or proof.

Defined in src/scalar/_numeric_types.mojo.

NumericValue.BALL

Mojo

comptime BALL = UInt8(0)

Python

NumericValue.BALL

A finite complex enclosure.

NumericValue.INFINITE

Mojo

comptime INFINITE = UInt8(1)

Python

NumericValue.INFINITE

A classified nonfinite value.

NumericValue.INCONCLUSIVE

Mojo

comptime INCONCLUSIVE = UInt8(2)

Python

NumericValue.INCONCLUSIVE

Insufficient information at the allowed precision.

GuardDecision

Mojo

@fieldwise_init
struct GuardDecision(Equatable, ImplicitlyCopyable)
Field Type Description
code UInt8 HOLDS, FAILS, or UNDECIDED.

Python

from judgment import GuardDecision

Python constructor:

def __init__(self, code)
Field Description
code HOLDS, FAILS, or UNDECIDED.

Three-valued guard evidence for one exact point.

Defined in src/scalar/_numeric_types.mojo.

GuardDecision.HOLDS

Mojo

comptime HOLDS = Self(0)

Python

GuardDecision.HOLDS

The guard is certified true numerically.

GuardDecision.FAILS

Mojo

comptime FAILS = Self(1)

Python

GuardDecision.FAILS

The guard is certified false numerically.

GuardDecision.UNDECIDED

Mojo

comptime UNDECIDED = Self(2)

Python

GuardDecision.UNDECIDED

The available enclosures do not decide the guard.

evaluate

Mojo

def evaluate(session: DagSession, profile: ScalarProfile, term: DagExpr, at: ExactPoint, precision: Int = 64, max_precision: Int = 512) raises LibError -> NumericValue

Python

def evaluate(session, profile, term, at, precision=64, max_precision=512)

Evaluate a Scalar expression numerically at an Expr-keyed exact point; the result is untrusted evidence.

Arguments.

  • session: Live session descended from profile.
  • profile: Owning Scalar profile.
  • term: Scalar expression to evaluate.
  • at: Exact value assignment keyed by DagExpr symbols.
  • precision: Initial working bit precision.
  • max_precision: Maximum adaptive bit precision.

Returns. Numeric enclosure or classification.

Raises.

  • LibError: On malformed assignment/precision, foreign/stale handles, or exhaustion.

Defined in src/scalar/_dag_api.mojo.

decide

Mojo

def decide(session: DagSession, profile: ScalarProfile, atom: ScalarAtom, at: ExactPoint, precision: Int = 64, max_precision: Int = 512) raises LibError -> GuardDecision

Python

def decide(session, profile, atom, at, precision=64, max_precision=512)

Decide a guard atom numerically at an Expr-keyed exact point; the result is untrusted evidence.

Arguments.

  • session: Live session descended from profile.
  • profile: Owning Scalar profile.
  • atom: Guard atom to decide.
  • at: Exact value assignment keyed by DagExpr symbols.
  • precision: Initial working bit precision.
  • max_precision: Maximum adaptive bit precision.

Returns. Numeric guard decision.

Raises.

  • LibError: On malformed assignment/precision, foreign/stale handles, or exhaustion.

Defined in src/scalar/_dag_api.mojo.

FilterConfig

Mojo

struct FilterConfig(Copyable)
Field Type Description
seed UInt64 Initial SplitMix64 state.
random_points Int Number of random samples and cap on a multivariable structured product.
scales List[Rational] Positive box half-widths, visited cyclically by random sampling.
precision Int Initial working precision in bits.
max_precision Int Largest adaptive retry precision.
max_undecided_fraction Float64 Largest tolerated fraction of attempted points remaining undecided.
workers Int Concurrent numeric workers; zero uses the available logical cores, up to 64.

Python

from judgment import FilterConfig

Python constructor:

def __init__(self, seed=6070695, random_points=256, scales=[Fraction(1), Fraction(4), Fraction(16)], precision=80, max_precision=320, max_undecided_fraction=0.1, workers=0)
Field Description
seed Initial SplitMix64 state.
random_points Number of random samples and cap on a multivariable structured product.
scales Positive box half-widths, visited cyclically by random sampling.
precision Initial working precision in bits.
max_precision Largest adaptive retry precision.
max_undecided_fraction Largest tolerated fraction of attempted points remaining undecided.
workers Concurrent numeric workers; zero uses the available logical cores, up to 64.

Reproducible sampling and precision limits for Algorithm N-4.

Defined in src/scalar/_filter_core.mojo.

FilterConfig.__init__

Mojo

def __init__(out self, seed: UInt64 = 0x5CA1A7, random_points: Int = 256, var scales: List[Rational] = [Rational(1), Rational(4), Rational(16)], precision: Int = 80, max_precision: Int = 320, max_undecided_fraction: Float64 = 0.1, workers: Int = 0)

Python

Use the Python constructor shown above. Python does not mirror Mojo constructor overloads.

Record a filter configuration, validated by refute.

Arguments.

  • seed: SplitMix64 seed.
  • random_points: Random sample count and multivariable product cap.
  • scales: Positive rational box sizes.
  • precision: Initial bit precision.
  • max_precision: Maximum bit precision.
  • max_undecided_fraction: Allowed unresolved fraction, from zero to one.
  • workers: Concurrent sample workers, 1 to 64; zero selects automatically.

FilterVerdict

Mojo

@fieldwise_init
struct FilterVerdict(Copyable)
Field Type Description
kind UInt8 Outcome tag (PASSED, REFUTED, or INCONCLUSIVE).
points Int Number of sample points evaluated.
witness Optional[ExactPoint] First refuting exact assignment paired with the input variables, if refuted.
lhs Optional[NumericValue] Evaluated left-hand value at the witness point, if refuted.
rhs Optional[NumericValue] Evaluated right-hand value at the witness point, if refuted.
undecided Int Count of sampled points whose comparison remained undecided.
total Int Total sample points attempted across the grid.

Python

from judgment import FilterVerdict

Python constructor:

def __init__(self, kind, points, witness, lhs, rhs, undecided, total)
Field Description
kind Outcome tag (PASSED, REFUTED, or INCONCLUSIVE).
points Number of sample points evaluated.
witness First refuting exact assignment paired with the input variables, if refuted.
lhs Evaluated left-hand value at the witness point, if refuted.
rhs Evaluated right-hand value at the witness point, if refuted.
undecided Count of sampled points whose comparison remained undecided.
total Total sample points attempted across the grid.

The unchanged filter result, with its first witness paired with the original variable handles.

Defined in src/scalar/_dag_numeric_api.mojo.

FilterVerdict.PASSED

Mojo

comptime PASSED = _PASSED

Python

FilterVerdict.PASSED

No counterexample was found across the sampled points.

FilterVerdict.REFUTED

Mojo

comptime REFUTED = _REFUTED

Python

FilterVerdict.REFUTED

A finite counterexample witness was found.

FilterVerdict.INCONCLUSIVE

Mojo

comptime INCONCLUSIVE = _INCONCLUSIVE

Python

FilterVerdict.INCONCLUSIVE

Sampling did not separate the expressions decisively.

refute

Mojo

def refute(session: DagSession, profile: ScalarProfile, lhs: DagExpr, rhs: DagExpr, guard: DagExpr, variables: List[DagExpr], config: FilterConfig = FilterConfig()) raises LibError -> FilterVerdict

Python

def refute(session, profile, lhs, rhs, guard, variables, config=FilterConfig())

Test a guarded equation numerically across sampled points, returning any counterexample keyed by DagExpr.

Arguments.

  • session: Live session descended from profile.
  • profile: Owning Scalar profile.
  • lhs: Left-hand Scalar expression.
  • rhs: Right-hand Scalar expression.
  • guard: Guard proposition restricting valid sample points.
  • variables: Ordered Scalar symbol expressions to sample.
  • config: Sampling and precision configuration.

Returns. Numeric filter verdict with any Expr-keyed counterexample witness.

Raises.

  • LibError: On invalid handles, malformed config, or exhaustion.

Defined in src/scalar/_dag_api.mojo.

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