Part 4 — Symbolic math, Scalar & naturals

4.4 Test conjectures numerically and find counterexamples

Tutorial sequence · Previous: 4.3 Guarded algebra and polynomials

Goal: evaluate Scalar expressions at exact points with cut-aware ball arithmetic (evaluate), decide guard predicates (decide), and run deterministic parallel counterexample search (refute) before attempting a formal proof.

Numeric evaluation, guard decision, and refutation

Function Operation Output
evaluate(s, profile, term, at) Evaluates term at an ExactPoint using adaptive-precision complex ball arithmetic with exact rational, \(\pi\), and radical boundary tracking. ComplexBall, InfiniteClass (DIRECTED, COMPLEX_INFINITY, INDETERMINATE), or INCONCLUSIVE.
decide(s, profile, atom, at) Evaluates a ScalarAtom predicate (FINITE, NONZERO, REAL, OFFCUT, UNWOUND, …) at an ExactPoint. HOLDS, FAILS, or UNDECIDED.
refute(s, profile, lhs, rhs, guard, vars, config) Tests structured points (poles, branch points, cut pieces, guard boundaries) and seeded random points deterministically across worker threads. REFUTED with an exact counterexample, or PASSED (testing evidence only, never a proof).

Detecting a missing guard

For \((x^2 - 1)/(x - 1) = x + 1\) (Lesson 4.3), the example below detects the missing pole guard \(x - 1 \neq 0\), decides exact NONZERO and principal-strip (UNWOUND, \(\operatorname{Im} x \in (-\pi, \pi]\)) guards, and passes once the full guard is supplied:

Mojo

numeric_filter.mojo Download
"""Evaluate Scalar expressions, decide branch-cut guards, and refute missing conditions."""
from apn_mojo import Rational
from apn_mojo.ball import Ball
from apn_mojo.complex_ball import ComplexBall, contains
from judgment import (
    ExactPoint, ExactValue, FilterConfig, FilterVerdict, GuardDecision, InfiniteClass, NumericValue,
    ProfileLevel, build_scalar, decide, evaluate, refute, session,
)
from scalar import ScalarAtom
from std.testing import assert_equal, assert_true
from termbank import Budget


def main() raises:
    var budget = Budget(20_000_000)
    var profile = build_scalar(budget, ProfileLevel.FOUNDATION)
    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")

    # Compare (x^2 - 1) / (x - 1) with x + 1 at x = 1.
    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 at_one = ExactPoint([(x, ExactValue(Rational(1)))])
    var lhs_val = evaluate(s, profile, lhs, at_one)
    var rhs_val = evaluate(s, profile, rhs, at_one)
    assert_equal(lhs_val.kind, NumericValue.INFINITE)
    assert_equal(lhs_val.infinite.value().kind, InfiniteClass.INDETERMINATE)
    assert_equal(rhs_val.kind, NumericValue.BALL)
    assert_true(contains(rhs_val.ball.value(), ComplexBall(Ball("2"), Ball("0"))))
    print("At x = 1: lhs is Indeterminate, rhs encloses 2")

    # Decide guard atoms at exact points (nonzero and principal-strip unwound checks).
    var nonzero_atom = ScalarAtom(ScalarAtom.NONZERO, [x])
    var unwound_atom = ScalarAtom(ScalarAtom.UNWOUND, [x])
    var at_one_nz = decide(s, profile, nonzero_atom, ExactPoint([(x, ExactValue(Rational(1)))]))
    var at_zero_nz = decide(s, profile, nonzero_atom, ExactPoint([(x, ExactValue(Rational(0)))]))
    var at_strip = decide(s, profile, unwound_atom, ExactPoint([(x, ExactValue(Rational(0), Rational(1)))]))
    var outside_strip = decide(s, profile, unwound_atom, ExactPoint([(x, ExactValue(Rational(0), Rational(4)))]))
    assert_true(at_one_nz == GuardDecision.HOLDS and at_zero_nz == GuardDecision.FAILS)
    assert_true(at_strip == GuardDecision.HOLDS and outside_strip == GuardDecision.FAILS)
    print("Guards: nonzero(1) HOLDS, nonzero(0) FAILS | unwound(i) HOLDS, unwound(4i) FAILS")

    # Refute the missing-guard conjecture under finite(x) alone; pass with x - 1 != 0.
    var config = FilterConfig(random_points=16)
    var missing_guard = refute(s, profile, lhs, rhs, finite(x), [x], config)
    assert_equal(missing_guard.kind, FilterVerdict.REFUTED)
    ref witness = missing_guard.witness.value().assignment[0][1]
    assert_equal(witness.re.to_string(), Rational(1).to_string())
    assert_equal(witness.im.to_string(), Rational(0).to_string())
    print("Under finite(x) alone: REFUTED at x = 1")

    var full_guard = finite(x) & ~(x_minus_one == zero)
    var guarded = refute(s, profile, lhs, rhs, full_guard, [x], config)
    assert_equal(guarded.kind, FilterVerdict.PASSED)
    assert_true(guarded.points > 0)
    print("Under finite(x) and x - 1 != 0: PASSED (testing evidence, not a proof)")

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

Output

At x = 1: lhs is Indeterminate, rhs encloses 2
Guards: nonzero(1) HOLDS, nonzero(0) FAILS | unwound(i) HOLDS, unwound(4i) FAILS
Under finite(x) alone: REFUTED at x = 1
Under finite(x) and x - 1 != 0: PASSED (testing evidence, not a proof)

Python

numeric_filter.py Download
"""Evaluate Scalar expressions, decide branch-cut guards, and refute missing conditions."""
from fractions import Fraction

from judgment import (
    Budget, ExactPoint, ExactValue, FilterConfig, FilterVerdict, GuardDecision, InfiniteClass,
    NumericValue, ProfileLevel, ScalarAtom, build_scalar, decide, evaluate, refute, session,
)


def main():
    budget = Budget(20_000_000)
    profile = build_scalar(budget, ProfileLevel.FOUNDATION)
    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")

    # Compare (x^2 - 1) / (x - 1) with x + 1 at x = 1.
    x_minus_one = plus(x, neg(one))
    lhs = times(plus(times(x, x), neg(one)), inv(x_minus_one))
    rhs = plus(x, one)
    at_one = ExactPoint([(x, ExactValue(1))])
    lhs_val = evaluate(s, profile, lhs, at_one)
    rhs_val = evaluate(s, profile, rhs, at_one)
    assert lhs_val.kind == NumericValue.INFINITE
    assert lhs_val.infinite.kind == InfiniteClass.INDETERMINATE
    assert rhs_val.kind == NumericValue.BALL
    ball = rhs_val.ball
    assert abs(ball.real_mid - Fraction(2)) <= ball.real_rad
    print("At x = 1: lhs is Indeterminate, rhs encloses 2")

    # Decide guard atoms at exact points (nonzero and principal-strip unwound checks).
    nonzero_atom = ScalarAtom(ScalarAtom.NONZERO, [x])
    unwound_atom = ScalarAtom(ScalarAtom.UNWOUND, [x])
    at_one_nz = decide(s, profile, nonzero_atom, ExactPoint([(x, ExactValue(1))]))
    at_zero_nz = decide(s, profile, nonzero_atom, ExactPoint([(x, ExactValue(0))]))
    at_strip = decide(s, profile, unwound_atom, ExactPoint([(x, ExactValue(0, 1))]))
    outside_strip = decide(s, profile, unwound_atom, ExactPoint([(x, ExactValue(0, 4))]))
    assert at_one_nz == GuardDecision.HOLDS and at_zero_nz == GuardDecision.FAILS
    assert at_strip == GuardDecision.HOLDS and outside_strip == GuardDecision.FAILS
    print("Guards: nonzero(1) HOLDS, nonzero(0) FAILS | unwound(i) HOLDS, unwound(4i) FAILS")

    # Refute the missing-guard conjecture under finite(x) alone; pass with x - 1 != 0.
    config = FilterConfig(random_points=16)
    missing_guard = refute(s, profile, lhs, rhs, finite(x), [x], config)
    assert missing_guard.kind == FilterVerdict.REFUTED
    witness = missing_guard.witness.assignment[0][1]
    assert witness.re == Fraction(1) and witness.im == Fraction(0)
    print("Under finite(x) alone: REFUTED at x = 1")

    full_guard = finite(x) & ~(x_minus_one == zero)
    guarded = refute(s, profile, lhs, rhs, full_guard, [x], config)
    assert guarded.kind == FilterVerdict.PASSED
    assert guarded.points > 0
    print("Under finite(x) and x - 1 != 0: PASSED (testing evidence, not a proof)")


if __name__ == "__main__":
    main()

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

Output

At x = 1: lhs is Indeterminate, rhs encloses 2
Guards: nonzero(1) HOLDS, nonzero(0) FAILS | unwound(i) HOLDS, unwound(4i) FAILS
Under finite(x) alone: REFUTED at x = 1
Under finite(x) and x - 1 != 0: PASSED (testing evidence, not a proof)

Key semantic properties:

  • Explicit poles and infinities: at \(x = 1\), evaluate classifies lhs as InfiniteClass.INDETERMINATE and rhs as a ball enclosing 2, separating them without floating-point NaN ambiguity.
  • Cut-aware guards: decide evaluates ScalarAtom predicates exactly on boundaries (NONZERO, UNWOUND; under ProfileLevel.ELEMENTARY_FUNCTIONS, OFFCUT checks branch cuts of log, sqrt, and inverse trigonometric functions without rounding boundary points into the interior).
  • Determinism across worker counts: changing workers in FilterConfig does not alter the sampling order, first counterexample, or sample counts.

Evidence boundary

FilterVerdict.PASSED reports only that every conclusive, guard-satisfying sample agreed; it never produces a Fact or discharges a proof obligation. Use refute to reject false conjectures or incomplete guards cheaply, then prove surviving identities through Session or scalar.transfer.

For exact coordinate constructors (including \(\pi\)-linear terms and radicals in ExactValue), see the numeric semantics reference.

Exercise

In numeric_filter, replace full_guard with s.true_() when calling refute. What verdict is returned?

Answer

FilterVerdict.REFUTED at x = 1, where True holds, lhs evaluates to Indeterminate, and rhs evaluates to 2.

Next: 4.5 Natural numbers, induction, and primitive recursion.

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