TorchLean API

FloatLib.Floats.Formats.BinaryInterchange.Model.Lean

Lean floating-point model bridge #

Model fmt retains the raw interchange bits, including noncanonical NaN payloads. Lean's Float.Model.UnpackedFloat interprets those bits using the conventional IEEE bias and exceptional encodings for their field widths. This module connects the two without selecting binary32 or any other fixed format. Custom descriptor biases and encoding policies require separate semantic agreement theorems.

The bridge is width preserving: FloatFormat.toModel has exactly fmt.bitWidth packed bits, so no padding, truncation, or host Float conversion occurs. Packing a model value canonicalizes NaNs, as Lean's model requires; unpacking arbitrary Model bits remains total.

References #

@[inline]

Translate the executable sign bit to Lean's logical floating-point sign.

Instances For
    @[inline]

    Translate Lean's logical floating-point sign back to an executable sign bit.

    Instances For
      @[simp]

      Translating a clear executable sign bit produces Lean's positive sign.

      @[simp]

      Translating a set executable sign bit produces Lean's negative sign.

      @[simp]

      Lean's negative logical sign translates to a set executable sign bit.

      @[simp]

      Lean's positive logical sign translates to a clear executable sign bit.

      @[simp]

      Translating an executable sign bit to the logical model and back is the identity.

      @[inline]

      View executable bits at the exactly equal width expected by Lean's model.

      Instances For
        @[inline]

        Wrap packed Lean-model bits as an executable value of the corresponding format.

        Instances For
          @[simp]

          Wrapping Lean-model bits and viewing them again preserves every bit.

          @[simp]

          Viewing an executable value as Lean-model bits and wrapping them again is the identity.

          Packing explicit fields agrees with Lean's sign/exponent/fraction concatenation. Inputs are reduced to their declared field widths by the corresponding BitVec.ofNat.

          Agreement of packed fields #

          These lemmas are the representation boundary for the generic bridge. They show that the manual field extractors used by the executable kernel and the extractors used by Lean's logical model read the same raw bit vector. Arithmetic refinement proofs can therefore work with one decoded sign, exponent, and mantissa rather than repeat bit-layout arguments for each format.

          @[simp]

          Packing and then extracting the one-bit sign field returns the original model sign.

          Lean's model and the executable decoder extract the same fraction field.

          The decoded fraction field fits in exactly the number of bits declared by its format.

          The all-ones exponent word of Lean's model is the executable all-ones exponent field.

          Lean's model and the executable decoder extract the same biased exponent field.

          The decoded biased exponent fits in exactly the number of bits declared by its format.

          An exponent field that is neither endpoint lies strictly inside the encoded range.

          Native normal-number kernels use this fact after checking zero and the all-ones pattern. Keeping the arithmetic here avoids repeating width-specific proofs for binary64, binary128, and future fixed-width backends.

          Lean's one-bit sign field is exactly the most significant executable bit.

          @[simp]
          theorem FloatLib.Floats.Formats.BinaryInterchange.Model.fracField_ofFields (fmt : FloatFormat) (sign : Bool) (exponent fraction : ) :
          (ofFields fmt sign exponent fraction).fracField = fraction % 2 ^ fmt.fracWidth

          Packing explicit fields retains the low fracWidth bits of the fraction.

          @[simp]
          theorem FloatLib.Floats.Formats.BinaryInterchange.Model.expField_ofFields (fmt : FloatFormat) (sign : Bool) (exponent fraction : ) :
          (ofFields fmt sign exponent fraction).expField = exponent % 2 ^ fmt.expWidth

          Packing explicit fields retains the low expWidth bits of the biased exponent.

          @[simp]
          theorem FloatLib.Floats.Formats.BinaryInterchange.Model.signBit_ofFields (fmt : FloatFormat) (sign : Bool) (exponent fraction : ) :
          (ofFields fmt sign exponent fraction).signBit = sign

          Packing explicit fields preserves the requested sign bit.

          Repacking the fields extracted from a Model reconstructs its exact bit pattern.

          @[simp]

          Packing three zero fields produces positive zero.

          @[simp]

          Packing only the sign bit produces the IEEE negative-zero bit pattern.

          theorem FloatLib.Floats.Formats.BinaryInterchange.Model.fracField_ofFields_of_lt (fmt : FloatFormat) (sign : Bool) (exponent fraction : ) (hfraction : fraction < 2 ^ fmt.fracWidth) :
          (ofFields fmt sign exponent fraction).fracField = fraction

          An in-range fraction is recovered exactly after field packing.

          theorem FloatLib.Floats.Formats.BinaryInterchange.Model.expField_ofFields_of_lt (fmt : FloatFormat) (sign : Bool) (exponent fraction : ) (hexponent : exponent < 2 ^ fmt.expWidth) :
          (ofFields fmt sign exponent fraction).expField = exponent

          An in-range biased exponent is recovered exactly after field packing.

          @[inline]

          Interpret an arbitrary bit pattern using Lean's width-parameterized IEEE model.

          Instances For
            @[inline]

            Encode a Lean logical value at fmt's field widths; NaNs use Lean's canonical payload.

            Instances For

              Whether the bits satisfy Lean's canonical-NaN invariant for packed model values.

              Instances For

                Every value produced by model packing satisfies the model's canonical-NaN invariant.

                @[inline]

                Repacking through Lean's model canonicalizes bit patterns it interprets as IEEE NaNs.

                Instances For

                  Canonicalizing through Lean's model always establishes its packed-value invariant.