Skip to content

Missing inverse lemmas for Float.ofBits / Float.toBits #13369

@kjsdesigns

Description

@kjsdesigns

Summary

Float.ofBits and Float.toBits are declared as @[extern] opaque primitives in Init.Data.Float but have no accompanying lemmas establishing their inverse relationship:

@[extern "lean_float_of_bits"] opaque Float.ofBits : UInt64 → Float
@[extern "lean_float_to_bits"] opaque Float.toBits : Float → UInt64

There are currently zero theorems about these functions in the standard library.

Requested additions

axiom Float.ofBits_toBits (f : Float) : Float.ofBits f.toBits = f
axiom Float.toBits_ofBits (u : UInt64) : (Float.ofBits u).toBits = u

Both hold by the C implementation (lean_float_of_bits / lean_float_to_bits are memcpy-based reinterpret casts between double and uint64_t, which are exact inverses on all IEEE 754 platforms).

Motivating use case

I maintain a production Lean 4 codebase that uses a typed data model with a positional JSON codec. The codec serializes Float values via their exact IEEE-754 bit pattern (Float.toBitsUInt64NatJson.num) and decodes the reverse path. Proving the round-trip property (fromPositionalJson dm (toPositionalJson v) = .ok v) requires knowing that Float.ofBits f.toBits = f.

Without upstream support, I had to introduce a project-local axiom:

-- lean/Spec/Trusted/FloatBits.lean
axiom float_ofBits_toBits (f : Float) : Float.ofBits f.toBits = f

This is the only axiom in the project (714+ sorry-free theorems otherwise). Having this in the standard library would eliminate the need for downstream projects to each introduce their own trusted axiom for the same fact.

Implementation question

Would the Lean team prefer these as:

  1. axiom declarations in Init.Data.Float (simplest, matches the opaque status of the functions), or
  2. Some other mechanism (e.g. native_decide-backed, or @[implemented_by])?

Happy to open a PR once there's alignment on the approach.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions