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:
ofBits32,toBits32,ofBits64, andtoBits64transport every interchange word unchanged inside FloatLib.- The native conversions use Lean's public
Float32.ofBits,Float32.toBits,Float.ofBits, andFloat.toBitsoperations. - Lean's logical model of
Float32.ofBitsandFloat.ofBitscanonicalizes NaN payloads, and so do the compiled runtime primitives (a compiled(Float32.ofBits 0x7f800001).toBitsis0x7fc00000). Payload-preserving transport therefore stays in theofBits32/toBits32andofBits64/toBits64APIs.
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 #
- IEEE 754-2019, Sections 3.4 and 3.6, defines binary interchange encodings.
- Lean's
Init.Data.Float.Float32andInit.Data.Float.Floatmodules define the runtime types, their logical models, and the model-based conversion boundary used here.
Exact encoded-word transport #
Construct configured IEEE binary32 from an exact 32-bit interchange word.
Instances For
Extract the exact 32-bit interchange word from configured IEEE binary32.
Instances For
Construct configured IEEE binary64 from an exact 64-bit interchange word.
Instances For
Extract the exact 64-bit interchange word from configured IEEE binary64.
Instances For
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.
The value of Model.cast src dst value (mode := mode).
Instances For
The binary64 interchange word of value cast to binary64 in mode.
Instances For
The binary32 interchange word of value cast to binary32 in mode.
Instances For
The binary64 interchange word bits cast to format in mode.
Instances For
The binary32 interchange word bits cast to format in mode.
Instances For
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
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
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
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
The logical model of a converted binary32 value is constructed from the same word.
A round trip of configured binary32 through Float32 is the canonical-NaN normalization of the
generic Lean-model bridge.
The logical model of a converted binary64 value is constructed from the same word.
A round trip of configured binary64 through Float is the canonical-NaN normalization of the
generic Lean-model bridge.
Exact source integration #
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.
Lean's native binary64 participates as an exact signed-rational conversion source.
Decoding a native Float32 source decodes its binary32 interchange value.
Decoding a native Float source decodes its binary64 interchange value.