TorchLean API

NN.Floats.IEEEExec.DirectedRoundingSoundness

Directed rounding soundness for IEEE32Exec #

NN/Floats/IEEEExec/Exec32.lean defines executable directed rounding on the IEEE-754 binary32 grid:

This file proves (one core piece of) the enclosure direction (“golden theorem” style): the directed rounding down kernel for positive dyadics is a sound lower bound for the exact real value.

Scope (what is proved here):

Non-goals (not proved here yet):

References:

Basic helpers #

@[reducible, inline]
Instances For

    bpow e = 2^e is strictly positive.

    bpow e = 2^e is nonnegative.

    Exponent law for bpow: bpow (e₁+e₂) = bpow e₁ * bpow e₂.

    pow2 is monotone in the exponent, in the simple form pow2 k ≤ pow2 (k+1).

    We use this for small exponent-range arguments (e.g. 2^23 ≤ 2^24) without importing heavier monotonicity machinery.

    Strict growth of pow2: pow2 k < pow2 (k+1).

    Commuted form of pow2_add: pow2 a * pow2 b = pow2 (a + b).

    Interpret bpow at a natural exponent in terms of pow2.

    Interpret bpow at a negative successor exponent as an inverse power of two.

    theorem TorchLean.Floats.IEEE754.IEEE32Exec.dyadicToReal_pos (mant : ) (exp : ) :
    dyadicToReal { sign := false, mant := mant, exp := exp } = mant * bpow exp

    Real semantics of a nonnegative dyadic (mant, exp) (sign bit false).

    theorem TorchLean.Floats.IEEE754.IEEE32Exec.dyadicToReal_signTrue (mant : ) (exp : ) :
    dyadicToReal { sign := true, mant := mant, exp := exp } = -(mant * bpow exp)

    Real semantics of a negative dyadic (mant, exp) (sign bit true).

    A few float32 constants as reals #

    Shift helpers (floor/ceil division by powers of two) #

    shiftRightCeilPow2 is also controlled from above by the corresponding floor quotient: ceil-division can overshoot by at most 1.

    Soundness: directed rounding on positive dyadics #

    theorem TorchLean.Floats.IEEE754.IEEE32Exec.toReal_roundDyadicPosDown_le (mant : ) (exp : ) (hm : mant 0) :
    (roundDyadicPosDown mant exp).toReal mant * bpow exp

    roundDyadicPosDown is a real lower bound for a positive dyadic.

    theorem TorchLean.Floats.IEEE754.IEEE32Exec.toEReal_roundDyadicPosUp_ge (mant : ) (exp : ) (hm : mant 0) :
    mant * (bpow exp) (roundDyadicPosUp mant exp).toEReal

    roundDyadicPosUp mant exp is an EReal upper bound for the positive dyadic (mant : ℝ) * 2^exp.

    This is the “ceil” counterpart to toReal_roundDyadicPosDown_le.

    toEReal respects negation on the finite/dyadic branch.

    We phrase this lemma using the dyadic decode witness toDyadic? x = some d, which guarantees:

    • x is finite (hence toEReal agrees with toReal), and
    • toReal (neg x) = -toReal x (via toReal_neg_eq_neg).

    Negation / sign-bit flip helpers #

    Negation preserves “not NaN” #

    If x is not a NaN then neg x is not a NaN.

    We only state the direction we actually need in the directed-rounding development: negative directed rounding is implemented as neg of a positive kernel, so we need to know this cannot introduce a NaN when starting from a non-NaN value.

    toEReal commutes with neg as long as we are not in the NaN case.

    This is a small but important glue lemma: it lets us lift the positive rounding kernels to the signed directed-rounding functions (roundDyadicDown / roundDyadicUp) without introducing new floating-point corner cases.

    Soundness of roundDyadicDown: the result is ≤ the exact dyadic real value (in EReal).

    This is the key lemma needed to justify addDown/mulDown as interval lower endpoints.

    roundDyadicPosDown (directed rounding toward -∞ for positive dyadics) never produces a NaN/Inf value.

    We package this as an existence statement: the output always has a dyadic decode. This is the bridge we use later to turn toEReal into toReal (as an EReal) without unfolding all bit-level definitions at call sites.

    Soundness of roundDyadicDown: it produces an EReal lower bound for the exact dyadic value.

    Informal: rounding down is monotone towards -∞, so the rounded result is always the exact real meaning.

    Soundness of roundDyadicUp: the result is ≥ the exact dyadic real value (in EReal).

    This is the key lemma needed to justify addUp/mulUp as interval upper endpoints.

    Lower-endpoint soundness for addDown on finite inputs: the result is ≤ the exact real sum (in EReal), even in overflow-to--∞ scenarios.

    Upper-endpoint soundness for addUp on finite inputs: the exact real sum is ≤ the result (in EReal), with overflow rounding to +∞.

    Lower-endpoint soundness for mulDown on finite inputs.

    Upper-endpoint soundness for mulUp on finite inputs.