NF: a rounded scalar type (rounding-on-ℝ) #
NeuralFloat (the record with a mantissa/exponent) is useful for talking about the grid and for
stating format predicates like FLT_format. In many places, though, we want something closer to a
“numeric scalar type” that we can plug into higher-level specs and examples.
NF β fexp rnd is that scalar carrier:
- it stores a semantic value
val : ℝ, - and every primitive arithmetic operation rounds back to the format using
neural_round.
The public constructor remains available because proof developments sometimes embed an arbitrary
real as a comparison value. Such a value need not be representable. NF.IsRepresentable records the
grid invariant when a theorem needs it; NF.ofReal and arithmetic results establish that invariant.
So when you write a + b in NF, what you get is:
val(a + b) = round( val(a) + val(b) )
This is the standard textbook model used for floating-point error analysis: compute in reals, then incur a rounding error at each step (Higham/Goldberg style).
Trust boundary:
NF/NeuralFloatare proof-relevant Lean models of rounded arithmetic (built onℝ).- Instantiating
NFwith IEEE single parameters + round-to-nearest-even models domain-valid, finite, no-overflow arithmetic with binary32's precision and gradual-underflow grid. The format has no upper exponent bound, and Mathlib's real division, square root, and logarithm are totalized; exceptional IEEE behavior belongs toIEEE32Exec. - Correspondence to hardware float32 / Lean's builtin
Floatis not proved in this file; that connection is an external assumption/interface boundary (or requires a separate verified kernel).
Rounded scalar value at a given radix/format/rounding mode.
β is the radix (typically 2), fexp selects the exponent grid, and rnd rounds the scaled
mantissa to an integer.
- val : ℝ
val.
Instances For
The rounding operator associated with the format: roundR x = neural_round … x.
Instances For
Inject a real into NF by rounding it onto the target grid.
Instances For
Forgetful projection (semantic view): treat an NF as a real number.
Instances For
The semantic value of an NF lies on its declared radix/exponent grid.
Instances For
toReal (ofReal x) is definitionally the rounded real roundR x.
A default inhabitant (rounded zero).
Coerce natural literals into NF by rounding (n : ℝ) onto the grid.
0 and 1 for NF are defined via ofReal, so they live on the target grid.
1 : NF is ofReal 1, i.e. the rounded real 1 on the target grid.
Arithmetic on NF is “compute in ℝ, then round”.
This is the key choice that makes many error bounds compositional: each primitive incurs at most
ulp/2 of rounding error (under round-to-nearest assumptions), so long compositions can be bounded
by accumulating per-op bounds.
Rounded multiplication: val(a * b) = roundR (val a * val b).
Checked rounded division. Unlike the totalized Div instance, this rejects a zero divisor.
Instances For
Checked division rejects exactly the zero-divisor case.
Boolean equality on NF values (semantic equality of reals).
This is not intended as a fast runtime check (it relies on classical decidability for ℝ), but
it is convenient for specs that want a BEq instance for logging or compact examples.
Min/max in the semantic order on ℝ, lifted to NF.
max on NF, defined by comparing the underlying real values.
Natural exponentiation evaluated in ℝ and rounded once onto the target grid.
Instances For
Natural powers have unambiguous real semantics for every base.
Positive-base real exponentiation, evaluated as exp (b * log a) and rounded once.
The positivity proof is part of the API so negative bases and 0^0 cannot silently acquire an
arbitrary totalized value.
Instances For
Checked real exponentiation.
Positive bases accept every real exponent. Negative bases accept integer exponents, including
negative integer exponents. Zero uses the usual natural-power convention for nonnegative integer
exponents, maps positive noninteger exponents to zero, and rejects negative exponents. Thus the
remaining rejected case is a negative base with a noninteger exponent. Natural powers can also use
powNat directly.
Instances For
On a positive base, checked exponentiation is the ordinary positive real-power formula.
Common math functions lifted to NF by “evaluate in ℝ, then round”.
This matches the same modeling decision as Add/Mul: the spec says what real function we intend,
and the rounding model accounts for discretization.
Checked rounded square root; negative inputs are rejected instead of using real totalization.
Instances For
Checked square root rejects exactly the negative inputs.
Checked rounded logarithm; zero and negative inputs are rejected.
Instances For
Checked logarithm rejects exactly the nonpositive inputs.
Numeric constants for NF via rounded reals.
Extract an approximate radix-β mantissa/exponent pair for debugging.
We compute:
e := cexp(x)from the format (fexp),m := rnd( scaled_mantissa(x) ),
so that x ≈ m · β^e (with the approximation coming from rounding).
This is meant for logs / human inspection; it is not used by the core proofs.
Instances For
Format an integer in base 10.
Instances For
Format an NF value as a radix-β scientific string "m * β^e".
Example (β = 2): "-123 * 2^7".
Instances For
Format an interval [lo, hi] for NF values using formatRadix.