IntegerComputeFloatControlFlow

Lean checked

Statement

This explanation was written alongside the formalization to describe the statement and its reasoning.

Lemma

For every signed exponent q, unsigned mantissa w, and biased pair fp, a normal return compute_float(q,w)=fp belongs to at least one of the following exhaustive classes:
  1. fp has negative biased exponent;
  2. w=0 and fp=(0,0);
  3. w>0, q<-342, and fp=(0,0);
  4. w>0, q>308, and fp=(0,2047);
  5. w>0, -342\le q\le308, and the biased exponent of fp is nonnegative.

Lean statement

Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.

import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64
import Tablet.ParserComputeFloatExponentRange
import Tablet.ParserExponentConstants
import Tablet.num_imp_dec2flt_common_BiasedFp_zero_pow2
import Tablet.num_imp_dec2flt_lemire_compute_float

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: IntegerComputeFloatControlFlow]
theorem IntegerComputeFloatControlFlow :
    ∀ (q : Std.I64) (w : Std.U64)
        (fp : num.imp.dec2flt.common.BiasedFp),
      num.imp.dec2flt.lemire.compute_float
          F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 q w = ok fp →
      fp.p_biased.val < 0 ∨
        (w.val = 0 ∧ fp = { m := 0#u64, p_biased := 0#i32 }) ∨
        (0 < w.val ∧ q.val < -342 ∧
          fp = { m := 0#u64, p_biased := 0#i32 }) ∨
        (0 < w.val ∧ 308 < q.val ∧
          fp = { m := 0#u64, p_biased := 2047#i32 }) ∨
        (0 < w.val ∧ -342 ≤ q.val ∧ q.val ≤ 308 ∧
          0 ≤ fp.p_biased.val) := by
Proof

Natural-language proof

The status above describes the Lean proof. Its explanatory prose may remain a sketch after that proof closes.

Explanatory prose status

Prose retained as authored. The explanatory prose is preserved as it stood when captured.

Proof

The binary64 constants in ParserExponentConstants show that the smallest and largest admitted decimal exponents are -342 and 308, and that the infinite biased exponent is 2047. Unfold num imp dec2flt lemire compute float. Each of its first three monadic calls is an invocation of num imp dec2flt common BiasedFp zero pow2; unfolding that definition gives, without any possible failure,
fp_0=(0,0),\qquad fp_\infty=(0,2047),\qquad fp_{err}=(0,-1). \tag{1}
The first conditional tests w=0. On its true branch the assumed return equation and (1) identify fp with fp_0, giving class 2. On the false branch, unsignedness gives w>0. The next trait call returns -342, its cast to signed 64 bits is exact, and the next conditional tests q<-342. Its true branch returns fp_0, giving class 3. On its false branch one has -342\le q. Similarly, the largest-power trait call returns 308, its cast is exact, and the next conditional tests q>308. Its true branch returns fp_\infty, giving class 4; its false branch leaves -342\le q\le308.
It remains only to classify a normal return from the duplicated arithmetic blocks after normalization and product approximation. The principal statement of ParserComputeFloatExponentRange applies to exactly the assumed compute_float return and says that fp.p_{biased} is -1 or is in [0,2047]. In the first alternative it is negative, giving class 1. In the second it is nonnegative, and the inequalities already obtained for w and q give class 5. This argument does not assume that intermediate checked operations succeed: on any path producing the assumed ok result, every earlier result-monad bind must already have returned ok. Failure and divergence therefore contribute no omitted normal return. The five classes exhaust the three immediate tests and both copies of the continuing block.

Lean proof


  intro q w fp h
  have hrange := ParserComputeFloatExponentRange q w fp h
  rcases ParserExponentConstants with
    ⟨hbits, htotal, hsig, hexpbits, hexpsat, hinf, hbias, hmin,
      hsmall, hlarge, hroundmin, hroundmax⟩
  have hzero : num.imp.dec2flt.common.BiasedFp.zero_pow2 0#i32 =
      ok { m := 0#u64, p_biased := 0#i32 } := by rfl
  have hinfFp : num.imp.dec2flt.common.BiasedFp.zero_pow2 2047#i32 =
      ok { m := 0#u64, p_biased := 2047#i32 } := by rfl
  have herr : num.imp.dec2flt.common.BiasedFp.zero_pow2 (-1)#i32 =
      ok { m := 0#u64, p_biased := (-1)#i32 } := by rfl
  have hcastSmall : lift (IScalar.cast .I64 (-342)#i32) =
      ok (-342)#i64 := by rfl
  have hcastLarge : lift (IScalar.cast .I64 308#i32) =
      ok 308#i64 := by rfl
  unfold num.imp.dec2flt.lemire.compute_float at h
  rw [hzero, hinf] at h
  simp only [bind_tc_ok] at h
  rw [hinfFp, herr] at h
  simp only [bind_tc_ok] at h
  by_cases hw : w = 0#u64
  · simp only [hw, if_true] at h
    cases h
    right
    left
    exact ⟨UScalar.val_eq_of_eq hw, by norm_num⟩
  simp only [hw, if_false] at h
  rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SMALLEST_POWER_OF_TEN.eq_def] at h
  rw [hcastSmall] at h
  simp only [bind_tc_ok] at h
  by_cases hqsmall : q < (-342)#i64
  · simp only [hqsmall, if_true] at h
    cases h
    right
    right
    left
    have hwval : 0 < w.val := by
      have hne : w.val ≠ 0 := by
        intro hz
        apply hw
        exact UScalar.eq_of_val_eq hz
      omega
    have hqval : q.val < -342 := by
      simpa only [IScalar.lt_equiv] using hqsmall
    exact ⟨hwval, hqval, by norm_num⟩
  simp only [hqsmall, if_false] at h
  rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.LARGEST_POWER_OF_TEN.eq_def] at h
  rw [hcastLarge] at h
  simp only [bind_tc_ok] at h
  by_cases hqlarge : q > 308#i64
  · simp only [hqlarge, if_true] at h
    cases h
    right
    right
    right
    left
    have hwval : 0 < w.val := by
      have hne : w.val ≠ 0 := by
        intro hz
        apply hw
        exact UScalar.eq_of_val_eq hz
      omega
    have hqval : 308 < q.val := by
      simpa only [IScalar.lt_equiv] using hqlarge
    exact ⟨hwval, hqval, by norm_num⟩
  simp only [hqlarge, if_false] at h
  rcases hrange with hneg | hbound
  · left
    omega
  · right
    right
    right
    right
    constructor
    · change 0 < w.val
      have hwval : w.val ≠ 0 := by
        intro hz
        apply hw
        exact UScalar.eq_of_val_eq hz
      omega
    · constructor
      · have hqval : ¬ q.val < -342 := by
          simpa only [IScalar.lt_equiv] using hqsmall
        omega
      · constructor
        · have hqval : ¬ 308 < q.val := by
            simpa only [IScalar.lt_equiv] using hqlarge
          omega
        · exact hbound.1

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64
import Tablet.ParserComputeFloatExponentRange
import Tablet.ParserExponentConstants
import Tablet.num_imp_dec2flt_common_BiasedFp_zero_pow2
import Tablet.num_imp_dec2flt_lemire_compute_float

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: IntegerComputeFloatControlFlow]
theorem IntegerComputeFloatControlFlow :
    ∀ (q : Std.I64) (w : Std.U64)
        (fp : num.imp.dec2flt.common.BiasedFp),
      num.imp.dec2flt.lemire.compute_float
          F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 q w = ok fp →
      fp.p_biased.val < 0 ∨
        (w.val = 0 ∧ fp = { m := 0#u64, p_biased := 0#i32 }) ∨
        (0 < w.val ∧ q.val < -342 ∧
          fp = { m := 0#u64, p_biased := 0#i32 }) ∨
        (0 < w.val ∧ 308 < q.val ∧
          fp = { m := 0#u64, p_biased := 2047#i32 }) ∨
        (0 < w.val ∧ -342 ≤ q.val ∧ q.val ≤ 308 ∧
          0 ≤ fp.p_biased.val) := by
-- BODY
  intro q w fp h
  have hrange := ParserComputeFloatExponentRange q w fp h
  rcases ParserExponentConstants with
    ⟨hbits, htotal, hsig, hexpbits, hexpsat, hinf, hbias, hmin,
      hsmall, hlarge, hroundmin, hroundmax⟩
  have hzero : num.imp.dec2flt.common.BiasedFp.zero_pow2 0#i32 =
      ok { m := 0#u64, p_biased := 0#i32 } := by rfl
  have hinfFp : num.imp.dec2flt.common.BiasedFp.zero_pow2 2047#i32 =
      ok { m := 0#u64, p_biased := 2047#i32 } := by rfl
  have herr : num.imp.dec2flt.common.BiasedFp.zero_pow2 (-1)#i32 =
      ok { m := 0#u64, p_biased := (-1)#i32 } := by rfl
  have hcastSmall : lift (IScalar.cast .I64 (-342)#i32) =
      ok (-342)#i64 := by rfl
  have hcastLarge : lift (IScalar.cast .I64 308#i32) =
      ok 308#i64 := by rfl
  unfold num.imp.dec2flt.lemire.compute_float at h
  rw [hzero, hinf] at h
  simp only [bind_tc_ok] at h
  rw [hinfFp, herr] at h
  simp only [bind_tc_ok] at h
  by_cases hw : w = 0#u64
  · simp only [hw, if_true] at h
    cases h
    right
    left
    exact ⟨UScalar.val_eq_of_eq hw, by norm_num⟩
  simp only [hw, if_false] at h
  rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SMALLEST_POWER_OF_TEN.eq_def] at h
  rw [hcastSmall] at h
  simp only [bind_tc_ok] at h
  by_cases hqsmall : q < (-342)#i64
  · simp only [hqsmall, if_true] at h
    cases h
    right
    right
    left
    have hwval : 0 < w.val := by
      have hne : w.val ≠ 0 := by
        intro hz
        apply hw
        exact UScalar.eq_of_val_eq hz
      omega
    have hqval : q.val < -342 := by
      simpa only [IScalar.lt_equiv] using hqsmall
    exact ⟨hwval, hqval, by norm_num⟩
  simp only [hqsmall, if_false] at h
  rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.LARGEST_POWER_OF_TEN.eq_def] at h
  rw [hcastLarge] at h
  simp only [bind_tc_ok] at h
  by_cases hqlarge : q > 308#i64
  · simp only [hqlarge, if_true] at h
    cases h
    right
    right
    right
    left
    have hwval : 0 < w.val := by
      have hne : w.val ≠ 0 := by
        intro hz
        apply hw
        exact UScalar.eq_of_val_eq hz
      omega
    have hqval : 308 < q.val := by
      simpa only [IScalar.lt_equiv] using hqlarge
    exact ⟨hwval, hqval, by norm_num⟩
  simp only [hqlarge, if_false] at h
  rcases hrange with hneg | hbound
  · left
    omega
  · right
    right
    right
    right
    constructor
    · change 0 < w.val
      have hwval : w.val ≠ 0 := by
        intro hz
        apply hw
        exact UScalar.eq_of_val_eq hz
      omega
    · constructor
      · have hqval : ¬ q.val < -342 := by
          simpa only [IScalar.lt_equiv] using hqsmall
        omega
      · constructor
        · have hqval : ¬ 308 < q.val := by
            simpa only [IScalar.lt_equiv] using hqlarge
          omega
        · exact hbound.1

end dec2flt_model

Proof structure

Follow the declared dependencies to read the supporting statements and proofs.

Explore this dependency tree

Inspect related source (87)
More program dependencies (45)
Standard-library dependencies (39)

Related source and models come from the extraction records and declared dependencies.

Proof assumptions and review

Lean checks the formal proof. Trellis also reviews how the statement corresponds to the intended requirement.

Transitive assumptions

This list combines the assumptions recorded for this declaration and its dependencies. It may include assumptions used by auxiliary declarations in those modules.

Helper theorems included
All 84 entries
Technical checking records
Local closure summary
{
  "node": "IntegerComputeFloatControlFlow",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "789c6c1c85f0b1a71a909ef53d3bda5059f5f8b0f3c7306bf9893edf3812e855",
  "active_statement_hash": "6dc6cfc215ac71c3f66432955db8f70d77851ba0ec5e7dbd8b0773a4d7a123b8",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "ab64d13f256386ee0e494a5abcc6a5db49512cf3d90bbdfcabd6327793a31f95",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "sidecar-sc-20261003-040321-804d93-IntegerComputeFloatControlFlow-cycle-142",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_LARGEST_POWER_OF_TEN": "2532cbe447dc07c7",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SMALLEST_POWER_OF_TEN": "8a9e3dfa7e9aa5ec",
    "ParserComputeFloatExponentRange": "d9f781305664f993",
    "ParserExponentConstants": "2f730aeb581fecf5",
    "num_imp_dec2flt_lemire_compute_float": "1b73cde8bfea548e",
    "num_imp_traits_Float_EXP_BIAS_default": "39417be39e20ce5d",
    "num_imp_traits_Float_EXP_BITS_default": "9ab622d85ef04d94",
    "num_imp_traits_Float_EXP_MIN_default": "39417be39e20ce5d",
    "num_imp_traits_Float_EXP_SAT_default": "9ab622d85ef04d94",
    "num_imp_traits_Float_SIG_BITS_default": "9ab622d85ef04d94"
  },
  "strict_definition_deps": {
    "F64": "ec4e25fbaf4e11d4",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64": "94095c9bf79da164",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_BITS": "d9fd91641cb78c5e",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_LARGEST_POWER_OF_TEN": "e23324c94fa5d1b5",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_MAX_EXPONENT_ROUND_TO_EVEN": "3dbd3f15a918cb2c",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_MIN_EXPONENT_ROUND_TO_EVEN": "e508b987ade7bf71",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SIG_TOTAL_BITS": "887b52d84a3fc9c4",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SMALLEST_POWER_OF_TEN": "0c461c0c4f5aa880",
    "num_imp_dec2flt_common_BiasedFp": "c43614ff2149ecf2",
    "num_imp_dec2flt_common_BiasedFp_zero_pow2": "0f99ae12c6707137",
    "num_imp_dec2flt_lemire_compute_float": "39d89e3f0a8a9d4c",
    "num_imp_dec2flt_lemire_compute_product_approx": "af91b9cbef7779f3",
    "num_imp_dec2flt_lemire_power": "56b7c3bd69ca3c6a",
    "num_imp_traits_Float": "94ab5f784cdf5db8"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:72d2bde221a566d394e392c9463953f3495f3bee2f4df8289492666c3455678f",
  "current_fingerprint": "sound-v2:72d2bde221a566d394e392c9463953f3495f3bee2f4df8289492666c3455678f",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}