TorchLean API

NN.Floats.NeuralFloat.Scalar.NF

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:

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:

structure TorchLean.Floats.NF (β : NeuralRadix) (fexp : ) (rnd : ) :

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.

Instances For
    @[inline]
    noncomputable def TorchLean.Floats.NF.roundR {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (x : ) :

    The rounding operator associated with the format: roundR x = neural_round … x.

    Instances For
      @[inline]
      noncomputable def TorchLean.Floats.NF.ofReal {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (x : ) :
      NF β fexp rnd

      Inject a real into NF by rounding it onto the target grid.

      Instances For
        @[inline]
        noncomputable def TorchLean.Floats.NF.toReal {β : NeuralRadix} {fexp : } {rnd : } (x : NF β fexp rnd) :

        Forgetful projection (semantic view): treat an NF as a real number.

        Instances For
          def TorchLean.Floats.NF.IsRepresentable {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (x : NF β fexp rnd) :

          The semantic value of an NF lies on its declared radix/exponent grid.

          Instances For
            @[simp]
            theorem TorchLean.Floats.NF.toReal_ofReal {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (x : ) :

            toReal (ofReal x) is definitionally the rounded real roundR x.

            @[simp]
            theorem TorchLean.Floats.NF.val_ofReal {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (x : ) :

            The underlying val field of ofReal x is roundR x.

            @[implicit_reducible]
            noncomputable instance TorchLean.Floats.NF.instInhabited {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] :
            Inhabited (NF β fexp rnd)

            A default inhabitant (rounded zero).

            @[implicit_reducible]
            noncomputable instance TorchLean.Floats.NF.instCoeNat {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] :
            Coe (NF β fexp rnd)

            Coerce natural literals into NF by rounding (n : ℝ) onto the grid.

            @[implicit_reducible]
            noncomputable instance TorchLean.Floats.NF.instZero {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] :
            Zero (NF β fexp rnd)

            0 and 1 for NF are defined via ofReal, so they live on the target grid.

            @[implicit_reducible]
            noncomputable instance TorchLean.Floats.NF.instOne {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] :
            One (NF β fexp rnd)

            1 : NF is ofReal 1, i.e. the rounded real 1 on the target grid.

            @[implicit_reducible]
            noncomputable instance TorchLean.Floats.NF.instNeg {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] :
            Neg (NF β fexp rnd)

            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.

            @[implicit_reducible]
            noncomputable instance TorchLean.Floats.NF.instAdd {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] :
            Add (NF β fexp rnd)

            Rounded addition: val(a + b) = roundR (val a + val b).

            @[implicit_reducible]
            noncomputable instance TorchLean.Floats.NF.instSub {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] :
            Sub (NF β fexp rnd)

            Rounded subtraction: val(a - b) = roundR (val a - val b).

            @[implicit_reducible]
            noncomputable instance TorchLean.Floats.NF.instMul {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] :
            Mul (NF β fexp rnd)

            Rounded multiplication: val(a * b) = roundR (val a * val b).

            @[implicit_reducible]
            noncomputable instance TorchLean.Floats.NF.instDiv {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] :
            Div (NF β fexp rnd)

            Rounded division: val(a / b) = roundR (val a / val b).

            noncomputable def TorchLean.Floats.NF.checkedDiv {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (a b : NF β fexp rnd) :
            Option (NF β fexp rnd)

            Checked rounded division. Unlike the totalized Div instance, this rejects a zero divisor.

            Instances For
              @[simp]
              theorem TorchLean.Floats.NF.checkedDiv_eq_none_iff {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (a b : NF β fexp rnd) :

              Checked division rejects exactly the zero-divisor case.

              @[implicit_reducible]
              noncomputable instance TorchLean.Floats.NF.instBEq {β : NeuralRadix} {fexp : } {rnd : } :
              BEq (NF β fexp rnd)

              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.

              @[implicit_reducible]
              noncomputable instance TorchLean.Floats.NF.instLT {β : NeuralRadix} {fexp : } {rnd : } :
              LT (NF β fexp rnd)

              Strict order on NF induced by the strict order on via the val field.

              @[implicit_reducible]
              noncomputable instance TorchLean.Floats.NF.instLE {β : NeuralRadix} {fexp : } {rnd : } :
              LE (NF β fexp rnd)

              Non-strict order on NF induced by on via the val field.

              @[implicit_reducible]
              noncomputable instance TorchLean.Floats.NF.instMin {β : NeuralRadix} {fexp : } {rnd : } :
              Min (NF β fexp rnd)

              Min/max in the semantic order on , lifted to NF.

              @[implicit_reducible]
              noncomputable instance TorchLean.Floats.NF.instMax {β : NeuralRadix} {fexp : } {rnd : } :
              Max (NF β fexp rnd)

              max on NF, defined by comparing the underlying real values.

              noncomputable def TorchLean.Floats.NF.powNat {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (a : NF β fexp rnd) (n : ) :
              NF β fexp rnd

              Natural exponentiation evaluated in and rounded once onto the target grid.

              Instances For
                @[implicit_reducible]
                noncomputable instance TorchLean.Floats.NF.instPowNat {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] :
                Pow (NF β fexp rnd)

                Natural powers have unambiguous real semantics for every base.

                noncomputable def TorchLean.Floats.NF.positiveRealPow {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (a b : NF β fexp rnd) (_ha : 0 < a.val) :
                NF β fexp rnd

                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
                  noncomputable def TorchLean.Floats.NF.checkedRealPow {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (a b : NF β fexp rnd) :
                  Option (NF β fexp rnd)

                  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
                    theorem TorchLean.Floats.NF.checkedRealPow_of_pos {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (a b : NF β fexp rnd) (ha : 0 < a.val) :

                    On a positive base, checked exponentiation is the ordinary positive real-power formula.

                    theorem TorchLean.Floats.NF.checkedRealPow_zero_of_pos_not_int {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (a b : NF β fexp rnd) (ha : a.val = 0) (hb : 0 < b.val) (hni : ¬∃ (z : ), b.val = z) :

                    A positive noninteger exponent of zero uses the unambiguous real value zero.

                    @[implicit_reducible]
                    noncomputable instance TorchLean.Floats.NF.instMathFunctions {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] :
                    MathFunctions (NF β fexp rnd)

                    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.

                    noncomputable def TorchLean.Floats.NF.checkedSqrt {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (x : NF β fexp rnd) :
                    Option (NF β fexp rnd)

                    Checked rounded square root; negative inputs are rejected instead of using real totalization.

                    Instances For
                      @[simp]
                      theorem TorchLean.Floats.NF.checkedSqrt_eq_none_iff {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (x : NF β fexp rnd) :

                      Checked square root rejects exactly the negative inputs.

                      noncomputable def TorchLean.Floats.NF.checkedLog {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (x : NF β fexp rnd) :
                      Option (NF β fexp rnd)

                      Checked rounded logarithm; zero and negative inputs are rejected.

                      Instances For
                        @[simp]
                        theorem TorchLean.Floats.NF.checkedLog_eq_none_iff {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (x : NF β fexp rnd) :

                        Checked logarithm rejects exactly the nonpositive inputs.

                        @[implicit_reducible]
                        noncomputable instance TorchLean.Floats.NF.instNumbers {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] :
                        Numbers (NF β fexp rnd)

                        Numeric constants for NF via rounded reals.

                        noncomputable def TorchLean.Floats.NF.mantExp {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (x : NF β fexp rnd) :

                        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
                          @[inline]

                          Format an integer in base 10.

                          Instances For
                            noncomputable def TorchLean.Floats.NF.formatRadix {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (x : NF β fexp rnd) :

                            Format an NF value as a radix-β scientific string "m * β^e".

                            Example (β = 2): "-123 * 2^7".

                            Instances For
                              noncomputable def TorchLean.Floats.NF.formatIntervalRadix {β : NeuralRadix} {fexp : } {rnd : } [NeuralValidExp fexp] (lo hi : NF β fexp rnd) :

                              Format an interval [lo, hi] for NF values using formatRadix.

                              Instances For