TorchLean API

FloatLib.Floats.Formats.IEEE754.Native

Lean native floating-point interoperability #

Explicit conversions connect every configured binary format to Lean's runtime Float32 and Float types: toFloat, ofFloat, toFloat32, and ofFloat32 round once in a mode option when the two formats differ, and move the interchange word unchanged between Binary64 and Float and between Binary32 and Float32. Lean names its IEEE binary64 runtime type Float, and there is no core type named Float64.

At the conversion boundary:

The conversions themselves do not choose an arithmetic backend. Configured binary32 and binary64 arithmetic uses proved software kernels by default. Applications may opt into Configured.NativeFPU.Unchecked for guarded host arithmetic, but those functions are outside the proof-carrying planner and depend on the compiler and host runtime.

The companion Native.Integer module connects integer conversion to FloatLib's rounding specifications. Native.AddSub proves agreement for finite operands, and Native.Sqrt covers every input after NaN canonicalization. Import FloatLib.Floats.Formats.IEEE754 or FloatLib to include these proofs alongside the conversions.

References #

Exact encoded-word transport #

@[inline]

Construct configured IEEE binary32 from an exact 32-bit interchange word.

Instances For
    @[inline]

    Extract the exact 32-bit interchange word from configured IEEE binary32.

    Instances For
      @[simp]

      Extracting the word just used to construct binary32 returns that word.

      @[simp]

      Reconstructing binary32 from its exact interchange word returns the original value.

      @[inline]

      Construct configured IEEE binary64 from an exact 64-bit interchange word.

      Instances For
        @[inline]

        Extract the exact 64-bit interchange word from configured IEEE binary64.

        Instances For
          @[simp]

          Extracting the word just used to construct binary64 returns that word.

          @[simp]

          Reconstructing binary64 from its exact interchange word returns the original value.

          Lean runtime types #

          The general paths of the native conversions: a value of another format crosses through Model.cast to or from the native word. They call the compiled status kernel of the cast and keep its value, which is the value of Model.cast (Model.value_cast), so the conversions of every format share the code of the cast library instead of inlining a copy of it.

          @[inline]

          Convert a configured value to Lean's native binary64 Float, rounding once in mode (nearest-even by default) when the format has values that binary64 cannot hold.

          A binary64 value crosses as its interchange word, through Lean's public Float.ofBits. Every other format is cast to binary64 by Model.cast first, which is exact for binary32, binary16, and bfloat16. Both the logical model of Float.ofBits and its compiled primitive canonicalize a noncanonical NaN payload, so a NaN with a payload does not round-trip through this function. Use toBits64 for payload-preserving serialization inside FloatLib.

          Instances For
            @[inline]

            Convert Lean's native binary64 Float to a configured value, rounding once in mode (nearest-even by default) when the destination cannot hold every binary64 value. Lean calls its 64-bit IEEE runtime type Float.

            The expected type selects the destination, as in (ExecFloat.Binary.ofFloat 0.1 : BFloat16). A binary64 destination receives the word of Lean's public Float.toBits, and every other destination receives Model.cast of that word.

            Instances For
              @[inline]

              Convert a configured value to Lean's native Float32, rounding once in mode (nearest-even by default) when the format has values that binary32 cannot hold.

              A binary32 value crosses as its interchange word, through Lean's public Float32.ofBits. Every other format is cast to binary32 by Model.cast first, which is exact for binary16 and bfloat16. Both the logical model of Float32.ofBits and its compiled primitive canonicalize a noncanonical NaN payload, so a NaN with a payload does not round-trip through this function. Use toBits32 for payload-preserving serialization inside FloatLib.

              Instances For
                @[inline]

                Convert Lean's native binary32 Float32 to a configured value, rounding once in mode (nearest-even by default) when the destination cannot hold every binary32 value.

                The expected type selects the destination. A binary32 destination receives the word of Lean's public Float32.toBits, whose logical model and compiled primitive agree on every finite value and infinity. A NaN reaches this function already canonicalized by whichever operation produced it. Every other destination receives Model.cast of that word.

                Instances For
                  @[simp]

                  Converting a native Float32 keeps Lean's public interchange word.

                  @[simp]

                  The logical model of a converted binary32 value is constructed from the same word.

                  @[simp]

                  Converting a native Float keeps Lean's public interchange word.

                  @[simp]

                  The logical model of a converted binary64 value is constructed from the same word.

                  Exact source integration #

                  @[instance_reducible]

                  Lean's native binary32 participates as an exact signed-rational conversion source.

                  The decoder crosses the native bit boundary and then uses the configured binary32 decoder, so -0.0 decodes to SignedRat.negZero. The caller names the destination through ExecFloat.convert, and native types do not join an implicit promotion lattice.

                  @[instance_reducible]

                  Lean's native binary64 participates as an exact signed-rational conversion source.

                  @[simp]

                  Decoding a native Float32 source decodes its binary32 interchange value.

                  @[simp]

                  Decoding a native Float source decodes its binary64 interchange value.