P529_T1_RegularSlowExecution

Lean checked

Statement

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

Lemma · Constructed regular slow execution and raw-word certificate

Let s be any modeled byte slice with an actual normal decimal-sequence parse to d. Suppose d has positive exact-length decimal shape, count at most 768 and -324\le p<310, and let
X=V(d)+\eta,\quad Y=V(d)+10^{p-768},\quad 0\le\eta<10^{p-768},\quad d.\mathit{truncated}\iff\eta>0 .
There is a biased pair fp such that the actual specialized slow call on s returns normally to fp. Put
r=\operatorname{lor}\left(fp.m, ((fp.p_{\rm biased}\bmod2^{64})\,2^{52})\bmod2^{64}\right),
where the signed remainder is nonnegative and converted to a natural. Then X\ge0 and at least one of these raw-word alternatives holds, with U=2^{52}, T=2^{53}, J=2047U and H=2^{1024}-2^{970}:
  1. r=J and H\le X;
  2. for some natural j\le T, r=j, X\le T2^{-1074}, X\le j2^{-1074}+2^{-1075}, j2^{-1074}\le X+2^{-1075}, and (X=(j+1/2)2^{-1074}\ \lor\ X=(j-1/2)2^{-1074}) \Longrightarrow j\bmod2=0;
  3. for some natural p,j with 2\le p\le2046, U\le j\le T, and p<2046 or j<T, put \Delta=2^{p-1075}. Then r=pU+(j-U), U\Delta\le X\le T\Delta, X<H, X\le j\Delta+\Delta/2, j\Delta\le X+\Delta/2, and (X=(j+1/2)\Delta\ \lor\ X=(j-1/2)\Delta) \Longrightarrow j\bmod2=0.
Moreover there is an F64 magnitude word assembled from fp that nearest-even rounds X. No enclosing slow or dispatcher-success hypothesis is assumed.

Lean statement

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

import Tablet.P495_T1_SlowLoop0Execution
import Tablet.P529_T1_Loop1Execution
import Tablet.P529_T1_NormalizationBounds
import Tablet.P529_T1_Loop2Execution
import Tablet.P529_T1_FinalShiftCertificate
import Tablet.P529_T1_SeparationPackage
import Tablet.P529_T1_RoundArbitraryEndpoint
import Tablet.P529_T1_CarryRoundExecution
import Tablet.P529_T1_EncodingArithmetic
import Tablet.ParserExponentConstants
import Tablet.Binary64NearestEvenAssembly
import Tablet.ModelU64BitAnd
import Tablet.ModelU64BitOr
import Tablet.ModelU64Shl
import Tablet.P9_T1_ActualDispatchOperations
import Tablet.num_imp_dec2flt_decimal_seq_parse_decimal_seq
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P529_T1_RegularSlowExecution]
theorem P529_T1_RegularSlowExecution (s : Slice Std.U8) (X Y : Real)
    (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (hparse : num.imp.dec2flt.decimal_seq.parse_decimal_seq s = ok d)
    (hpointLo : -324 <= d.decimal_point.val)
    (hpointHi : d.decimal_point.val < 310)
    (hinput : d.num_digits.val <= 768 /\
      (forall b, b ∈ d.digits.val.take d.num_digits.val -> b.val < 10) /\
      (exists h tail,
        (d.digits.val.take d.num_digits.val).map (fun b => b.val) = h :: tail /\
        1 <= h /\ h < 10 /\ (h :: tail).length = d.num_digits.val)) :
    let V := fun a : num.imp.dec2flt.decimal_seq.DecimalSeq =>
      ((((a.digits.val.take a.num_digits.val).map (fun b => b.val)).foldl
        (fun n z => 10 * n + z) 0 : Nat) : Real) *
        (10 : Real) ^ (a.decimal_point.val - (a.num_digits.val : Int))
    Y = V d + (10 : Real) ^ (d.decimal_point.val - 768) ->
    (exists eta : Real, X = V d + eta /\ 0 <= eta /\
      eta < (10 : Real) ^ (d.decimal_point.val - 768) /\
      (d.truncated = true <-> 0 < eta)) ->
    exists fp : num.imp.dec2flt.common.BiasedFp,
      P9_T1_ActualDispatchOperations.slow s = ok fp /\
      (let raw : Nat := Nat.lor fp.m.val
        (((fp.p_biased.val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64))
       0 <= X /\ ((raw = 2047 * (2 ^ 52) /\
        (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) <= X) \/
      (exists j : Nat,
        j <= 2 ^ 53 /\ raw = j /\
        X <= (2 ^ 53 : Nat) * (2 : Real) ^ (-1074 : Int) /\
        X <= (j : Real) * (2 : Real) ^ (-1074 : Int) + (2 : Real) ^ (-1075 : Int) /\
        (j : Real) * (2 : Real) ^ (-1074 : Int) <= X + (2 : Real) ^ (-1075 : Int) /\
        (X = ((j : Real) + 1 / 2) * (2 : Real) ^ (-1074 : Int) \/
          X = ((j : Real) - 1 / 2) * (2 : Real) ^ (-1074 : Int) -> j % 2 = 0)) \/
      (exists p j : Nat,
        2 <= p /\ p <= 2046 /\ 2 ^ 52 <= j /\ j <= 2 ^ 53 /\
        (p < 2046 \/ j < 2 ^ 53) /\
        raw = p * (2 ^ 52) + (j - 2 ^ 52) /\
        let delta : Real := (2 : Real) ^ ((p : Int) - 1075)
        (2 ^ 52 : Nat) * delta <= X /\ X <= (2 ^ 53 : Nat) * delta /\
        X < (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) /\
        X <= (j : Real) * delta + delta / 2 /\
        (j : Real) * delta <= X + delta / 2 /\
        (X = ((j : Real) + 1 / 2) * delta \/
          X = ((j : Real) - 1 / 2) * delta -> j % 2 = 0)))) /\
      (exists magnitude : F64,
        DecimalGoalVocabularyDefault.assembledWord fp magnitude /\
        DecimalGoalVocabularyDefault.roundsMagnitude X magnitude) := 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

Unfold the actual specialized call using ActualDispatchOperations and the pinned body num imp dec2flt slow parse long mantissa. The trait instance in the actual dispatch record reduces to the same binary64 Float instance used in all the loop equations below. ParserExponentConstants supplies normal constant computations with infinite power 2047, minimum exponent -1022 and significand width 52. The zero-pair constructor returns (0,0) and the infinity constructor returns (0,2047) directly. Rewrite the parse by the given call. Positive shape makes the count nonzero, and the point bounds make both initial cutoff tests false.
Apply SlowLoop0Execution to the original adjacent-cell package. It constructs a normal loop-zero return with None, shape, point in [-324,0], 0\le e_0\le1260 and fixed endpoints. Apply Loop1Execution there: it constructs a normal loop-one return with None, point zero, head in [5,9], shape, endpoints and e_1\le e_0. Their actual equations permit NormalizationBounds, which gives e_1\ge-1079 and Y/2^{e_1}\le1. Apply Loop2Execution to construct the checked decrement e_s=e_1-1 and normal loop-two return. It gives e_2=\max(e_1-1,-1022), -18\le p_2\le0, shape, endpoint equations at e_2+1, and either the no-shift identity or one actual right shift. In particular
-1022\le e_2\le1259,\qquad e_1\le e_2+1 .
All constructed normal loop returns have None, so the two early-result matches take their None arms.
The first checked bias computation follows the source order: e_2-(-1022) lies in [0,2281] and its successor b=e_2+1023 in [1,2282]; both fit I32 exactly. The first saturation test is therefore b\ge2047, equivalently e_2\ge1024. Before completing the two source branches, construct the mathematical rounding certificates, independently of which branch is reached. The original X<Y and endpoint bound give 0<X/2^{e_2+1}<Y/2^{e_2+1}\le1. FinalShiftCertificate constructs the actual normal 53-bit shift, positive trimmed d_3, p_3\le16, and endpoints
t=X/2^{e_2-52}=V(d_3)+\eta_3,\quad Y_3=Y/2^{e_2-52}=V(d_3)+u_3,\quad 0<t<2^{53}.
SeparationPackage applies to the constructed exact loop, decrement and final-shift equations. It gives both half-integer and odd-integer separation at d_3. The safe range t<2^{53}<2^{64}-1/2, point and trim clauses allow RoundArbitraryEndpoint to construct its normal round m with the two nearest inequalities and even-tie parity. Thus m\le2^{53} by integrality. If e_2>-1022, its maximum equation forces e_s=e_2>-1022, so loop two is the identity and d_2=d_1. DecimalValueBounds on the point-zero head at least five gives V(d_1)\ge1/2. Hence X/2^{e_2+1}\ge1/2 and t\ge2^{52}, the normal-binade lower bound required for encoding.
If m\ge2^{53}, apply CarryRoundExecution with the odd-integer separation, actual first round and endpoint witnesses. It constructs the actual nonreset one-bit right shift and normal reround m_4, with endpoints at e_2-51 and independent nearest/parity laws for t/2. If no carry occurs, set a purely mathematical unused witness m_4=0; no reround is claimed in that case. The conditional reround premise is then vacuous. EncodingArithmetic applies to X,e_2,m,m_4 and gives the explicit raw-word alternatives in the statement. We now verify that this arithmetic raw word is exactly the source result, not merely a nearby encoder.
If the FIRST test saturates, the source returns (0,2047) immediately. The unused constructed shift and round certificates above are not asserted to be reached in this branch. Their arithmetic lemma's first branch returns 2047\,2^{52}, agreeing exactly.
Otherwise the source computes significand width 52, casts it to Usize, adds one and shifts the decimal record by 53. The cast and addition are exact under the ratified width in Assumptions; 53 is far below the Usize maximum. The actual shift and round coincide with the constructed d_3,m. The U32 addition 52+1=53 is in bounds; the U64 shift of one by 53 is legal and equals 2^{53}, so the carry guard is exactly m\ge2^{53}.
In a carry branch the source first performs the constructed normal right shift, then checked exponent increment e_2+1 and then the independently constructed reround. The increment lies in [-1021,1024] because the first saturation test was false, so it is safe. Its checked bias subtraction and successor yield e_2+1024 in [2,2047]. The SECOND saturation test is exactly e_2\ge1023. If true, the source returns (0,2047), agreeing with the second branch of the arithmetic raw word. If false, the repeated checked bias calculation gives e_2+1024 in [2,2046]. In a noncarry branch the repeated checked calculations give e_2+1023 in [1,2046]. In either nonsaturating branch the shift of one by 52 equals U=2^{52}, and the checked subtraction U-1 is safe. The conditional biased-exponent subtraction is by at most one from a positive bias and therefore remains in [0,2046]. The mask returns the chosen round word modulo U: each bit below position 52 is preserved and all higher bits are zero. These are the precise bounded operations in ModelU64Shl and ModelU64BitAnd, also used for the trait's corresponding built-in scalar operations in the pinned body. All scalar updates have thus been constructed in source order.
Call the masked word a<U and biased field b'\in[0,2046]. Its assembly shift is b'U<2^{64}, and a occupies only the lower 52 bits while b'U occupies none of them. Bit-by-bit, their OR is b'U+a; there is no carry across the disjoint bit ranges. This agrees with ModelU64BitOr and the formula in DecimalGoalVocabularyDefault. The signed exponent remainder and shifted-word remainder are identities because 0\le b'<2^{64} and b'U<2^{64}. Thus the actual pair's raw word equals precisely the arithmetic raw word in every branch. Rewriting all the constructed subcalls into the pinned body proves an actual normal slow return fp and its certificate.
It remains to construct an assembled magnitude, rather than assuming assembly already succeeded. Each raw alternative implies 0\le r\le2047U<2^{64}: the low grid has r\le2U; the normal grid has r\le2046U+U, with the top equality excluded by its range clause; infinity has equality with 2047U. The bounded U64 with natural value r therefore exists and is an F64 word; choose it as magnitude w. Its value equation is exactly the assembled-word definition. Apply Binary64NearestEvenAssembly with the established X\ge0 and the SAME explicit raw-word alternatives. It gives nearest-even magnitude rounding of X. This proves both operational existence and the unrestricted certificate, including the zero/subnormal low grid and the top-overflow tie.

Lean proof


  exact (set_option maxHeartbeats 2000000 in by
    let pack (e : Std.I32) (m : Std.U64) : Result num.imp.dec2flt.common.BiasedFp := do
      let b ← e - (-1022)#i32
      let p ← b + 1#i32
      let u ← 1#u64 <<< 52#u32
      let p' ← if m < u then p - 1#i32 else ok p
      let u' ← 1#u64 <<< 52#u32
      let mask ← u' - 1#u64
      let a ← lift (m &&& mask)
      ok { m := a, p_biased := p' }

    let suffix (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (e : Std.I32) :
        Result num.imp.dec2flt.common.BiasedFp := do
      let b ← e - (-1022)#i32
      let p ← b + 1#i32
      if p >= 2047#i32 then ok { m := 0#u64, p_biased := 2047#i32 }
      else
        let d3 ← num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d 53#usize
        let m ← num.imp.dec2flt.decimal_seq.DecimalSeq.round d3
        if m >= 9007199254740992#u64 then
          let d4 ← num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d3 1#usize
          let e' ← e + 1#i32
          let m4 ← num.imp.dec2flt.decimal_seq.DecimalSeq.round d4
          let b' ← e' - (-1022)#i32
          let p' ← b' + 1#i32
          if p' >= 2047#i32 then ok { m := 0#u64, p_biased := 2047#i32 }
          else pack e' m4
        else pack e m

    let rawOf (fp : num.imp.dec2flt.common.BiasedFp) : Nat :=
      Nat.lor fp.m.val (((fp.p_biased.val.emod (2^64)).toNat * 2^52) % 2^64)

    have biasOps (e : Std.I32) (hlo : -1022 <= e.val) (hhi : e.val <= 1261) :
        ∃ b p : Std.I32, e - (-1022)#i32 = ok b ∧ b + 1#i32 = ok p ∧
          p.val = e.val + 1023 := by
      obtain ⟨b, hb, hbval⟩ := WP.spec_imp_exists
        (IScalar.sub_spec (x := e) (y := (-1022)#i32)
          (by rw [IScalar.min_I32_eq]; norm_num; omega)
          (by rw [IScalar.max_I32_eq]; norm_num; omega))
      obtain ⟨p, hp, hpval⟩ := WP.spec_imp_exists
        (IScalar.add_spec (x := b) (y := 1#i32)
          (by rw [IScalar.min_I32_eq]; norm_num at hbval ⊢; omega)
          (by rw [IScalar.max_I32_eq]; norm_num at hbval ⊢; omega))
      exact ⟨b,p,hb,hp,by norm_num at hbval hpval; omega⟩

    have mask52 (z : Std.U64) : (z &&& 4503599627370495#u64).val = z.val % 2^52 := by
      change (z.bv &&& 4503599627370495#64).toNat = z.bv.toNat % 2^52
      rw [show 4503599627370495#64 = 2#64^52 - 1#64 by decide,
        BitVec.and_two_pow_sub_one_eq_mod]
      simp

    have fieldOr (a ef : Nat) (ha : a < 2^52)
        (hef : ef * 2^52 < 2^64) : a ||| (ef * 2^52) = a + ef * 2^52 := by
      have hland : a &&& (ef * 2^52) = 0 := by
        apply Nat.eq_of_testBit_eq
        intro j
        rw [Nat.testBit_land, Nat.testBit_mul_two_pow]
        by_cases hj : j < 52
        · simp [hj]
        · have haj : a.testBit j = false := by
            apply Nat.testBit_eq_false_of_lt
            exact lt_of_lt_of_le ha (Nat.pow_le_pow_right (by omega) (by omega))
          simp [haj]
      let av : BitVec 64 := BitVec.ofNat 64 a
      let ev : BitVec 64 := BitVec.ofNat 64 (ef * 2^52)
      have hlandBV : av &&& ev = 0#64 := by
        apply BitVec.eq_of_toNat_eq
        simp only [av, ev, BitVec.toNat_and, BitVec.toNat_ofNat]
        rw [Nat.mod_eq_of_lt (ha.trans (by norm_num)), Nat.mod_eq_of_lt hef, hland]
      calc
        a ||| (ef * 2^52) = (av ||| ev).toNat := by
          simp only [av, ev, BitVec.toNat_or, BitVec.toNat_ofNat]
          rw [Nat.mod_eq_of_lt (ha.trans (by norm_num)), Nat.mod_eq_of_lt hef]
        _ = (av + ev).toNat := by rw [BitVec.add_eq_or_of_and_eq_zero av ev hlandBV]
        _ = a + ef * 2^52 := by
          rw [BitVec.toNat_add_of_and_eq_zero hlandBV]
          simp only [av, ev, BitVec.toNat_ofNat]
          rw [Nat.mod_eq_of_lt (ha.trans (by norm_num)), Nat.mod_eq_of_lt hef]

    have packExec (e : Std.I32) (m : Std.U64)
        (hlo : -1022 <= e.val) (hhi : e.val <= 1023) :
        ∃ fp : num.imp.dec2flt.common.BiasedFp, pack e m = ok fp ∧
          rawOf fp = (e.val + 1023 - (if m.val < 2^52 then 1 else 0)).toNat * 2^52 +
            m.val % 2^52 := by
      obtain ⟨b,p,hb,hp,hpval⟩ := biasOps e hlo (by omega)
      have hpbound : 1 <= p.val ∧ p.val <= 2046 := by omega
      have hpower : ∃ p' : Std.I32,
          (if m < 4503599627370496#u64 then p - 1#i32 else ok p) = ok p' ∧
          p'.val = e.val + 1023 - (if m.val < 2^52 then 1 else 0) ∧
          0 <= p'.val ∧ p'.val <= 2046 := by
        by_cases hm : m.val < 2^52
        · have hm' : m < 4503599627370496#u64 := by
            change m.val < 4503599627370496
            norm_num at hm
            exact hm
          obtain ⟨p',hp',hv⟩ := WP.spec_imp_exists
            (IScalar.sub_spec (x := p) (y := 1#i32)
              (by rw [IScalar.min_I32_eq]; norm_num; omega)
              (by rw [IScalar.max_I32_eq]; norm_num; omega))
          refine ⟨p', by rw [if_pos hm']; exact hp', ?_, ?_, ?_⟩
          all_goals norm_num at hv; try simp only [if_pos hm]
          all_goals omega
        · have hm' : ¬m < 4503599627370496#u64 := by
            change ¬m.val < 4503599627370496
            norm_num at hm
            omega
          refine ⟨p, by rw [if_neg hm'], ?_, by omega, by omega⟩
          simp only [if_neg hm, Int.sub_zero]
          exact hpval
      obtain ⟨p',hp',hp'val,hp'lo,hp'hi⟩ := hpower
      let fp : num.imp.dec2flt.common.BiasedFp :=
        { m := m &&& 4503599627370495#u64, p_biased := p' }
      refine ⟨fp, ?_, ?_⟩
      · unfold pack
        simp only [hb, hp, bind_tc_ok,
          show (1#u64 <<< 52#u32) = ok 4503599627370496#u64 by rfl,
          hp', show (4503599627370496#u64 - 1#u64) = ok 4503599627370495#u64 by rfl]
        rfl
      · have hef : p'.val.toNat * 2^52 < 2^64 := by
          have : p'.val.toNat <= 2046 := by omega
          norm_num
          omega
        unfold rawOf
        change Nat.lor (m &&& 4503599627370495#u64).val
          (((p'.val.emod (2^64)).toNat * 2^52) % 2^64) = _
        rw [mask52]
        have hmod : p'.val.emod (2^64) = p'.val := by
          exact Int.emod_eq_of_lt hp'lo (by norm_num; omega)
        rw [hmod, Nat.mod_eq_of_lt hef]
        change (m.val % 2^52) ||| (p'.val.toNat * 2^52) = _
        rw [fieldOr (m.val % 2^52) p'.val.toNat (Nat.mod_lt _ (by positivity)) hef, hp'val]
        omega
    intro Vfun hY hX
    classical
    have h0pkg := P495_T1_SlowLoop0Execution X Y d hpointLo hpointHi hinput
      (by simpa [Vfun] using hY) (by simpa [Vfun] using hX)
    rcases h0pkg with ⟨d0, e0, steps, h0, hsteps, he0lo, he0hi, hXlo,
      hp0lo, hp0hi, hn0, hd0, hshape0, htrim0, hends0⟩
    -- The source call starts from exactly the parsed record and the certified loop zero.
    have hinput0 : d0.num_digits.val <= 768 /\
        (forall b, b ∈ d0.digits.val.take d0.num_digits.val -> b.val < 10) /\
        (exists h tail,
          (d0.digits.val.take d0.num_digits.val).map (fun b => b.val) = h :: tail /\
          1 <= h /\ h < 10 /\ (h :: tail).length = d0.num_digits.val) :=
      ⟨hn0, hd0, hshape0⟩
    have h1pkg := P529_T1_Loop1Execution X Y d0 e0 hp0lo hp0hi he0lo
      hinput0 (by simpa [Vfun] using hends0)
    rcases h1pkg with ⟨d1, e1, bound, h1, hbound, hboundlo, hboundhi,
      hp1, hn1, hheadlo, hheadhi, hn1cap, hd1, hshape1, hends1⟩
    have hnorm := P529_T1_NormalizationBounds X Y d d0 d1 e0 e1
      hpointLo hpointHi hinput h0 h1 (by simpa [Vfun] using hY)
      (by simpa [Vfun] using hX)
    rcases hnorm with ⟨_, hXY, hYbound, he1lo, he1hi⟩
    have h2pkg := P529_T1_Loop2Execution X Y d1 e1 he1lo hp1
      ⟨hn1cap, hd1, hshape1⟩ (by simpa [Vfun] using hends1)
    rcases h2pkg with ⟨es, e2, d2, sh, hdec, hesval, heslo, h2, he2val,
      hshval, hshle, h2case, hp2lo, hp2hi, hn2, hd2, hshape2, htrim2, hends2⟩
    have he2range : -1022 <= e2.val /\ e2.val <= 1260 := by
      rw [he2val]
      omega
    have he1le : e1.val <= e2.val + 1 := by rw [he2val, hesval]; omega
    have hYbound2 : Y / (2 : Real) ^ (e2.val + 1) <= 1 := by
      have hYle : Y <= (2 : Real) ^ e1.val :=
        (div_le_one (by positivity)).mp hYbound
      have hpow : (2 : Real) ^ e1.val <= (2 : Real) ^ (e2.val + 1) :=
        zpow_le_zpow_right₀ (by norm_num) he1le
      exact (div_le_one (by positivity)).2 (hYle.trans hpow)
    have h3pkg := P529_T1_FinalShiftCertificate X Y d2 e2 hp2lo hp2hi
      ⟨hn2, hd2, hshape2⟩ hYbound2 (by simpa using hends2)
    rcases h3pkg with ⟨d3, h3, hp3lo, hp3hi, hn3, hd3, hshape3, htrim3,
      hXpos3, hXhi3, hends3⟩
    have hsepPkg := P529_T1_SeparationPackage X Y d d0 d1 d2 d3 e0 e1 es e2
      hpointLo hpointHi hinput h0 h1 hdec h2 h3
      (by simpa [Vfun] using hY) (by simpa [Vfun] using hX)
    rcases hsepPkg with ⟨hhalfSep, hoddSep⟩
    have hroundPkg := P529_T1_RoundArbitraryEndpoint d3
      (X / (2 : Real) ^ (e2.val - 52)) (Y / (2 : Real) ^ (e2.val - 52))
      hn3 hd3 (Or.inr ⟨hshape3, htrim3⟩) (by omega) (by positivity)
      (by
        have hbig : (2 : Real) ^ (53 : Nat) < (2 : Real) ^ (64 : Nat) - 1 / 2 := by norm_num
        exact lt_trans hXhi3 hbig)
      (by simpa using hends3)
      (by
        intro n
        simpa using hhalfSep n)
    rcases hroundPkg with ⟨m, hround, hmUpper, hmLower, hmTie⟩
    have hnumNe : d.num_digits ≠ 0#usize := by
      rcases hinput.2.2 with ⟨head, tail, hmap, hhead, hheadHi, hlen⟩
      intro heq
      have hv := congrArg (fun n : Std.Usize => n.val) heq
      simp at hv
      rw [← hlen] at hv
      simp at hv
    have hsmall : ¬ d.decimal_point < (-324)#i32 := by
      intro hs
      have hs' : d.decimal_point.val < -324 := by
        simpa only [IScalar.lt_equiv] using hs
      omega
    have hlarge : ¬ d.decimal_point >= 310#i32 := by
      intro hl
      have hl' : (310 : Int) <= d.decimal_point.val := by
        simpa only [IScalar.le_equiv] using hl
      omega
    have hsource : P9_T1_ActualDispatchOperations.slow s = suffix d2 e2 := by
      have h0bind {R : Type}
          (k : Std.Usize → Std.I32 → Bool → Array Std.U8 768#usize → Std.I32 →
            Option num.imp.dec2flt.common.BiasedFp → Result R) :
          (do
            let (n,p,tr,a,e,r) ← num.imp.dec2flt.slow.parse_long_mantissa_loop0
              F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
              {m := 0#u64,p_biased := 0#i32}
              d.num_digits d.decimal_point d.truncated d.digits 0#i32 none
            k n p tr a e r) = k d0.num_digits d0.decimal_point d0.truncated
                d0.digits e0 none := by
        simp only [h0, bind_tc_ok]
        rfl
      have h1bind {R : Type}
          (k : Std.Usize → Std.I32 → Bool → Array Std.U8 768#usize → Std.I32 →
            Option num.imp.dec2flt.common.BiasedFp → Result R) :
          (do
            let (n,p,tr,a,e,r) ← num.imp.dec2flt.slow.parse_long_mantissa_loop1
              F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
              {m := 0#u64,p_biased := 2047#i32}
              d0.num_digits d0.decimal_point d0.truncated d0.digits e0 none
            k n p tr a e r) = k d1.num_digits d1.decimal_point d1.truncated
                d1.digits e1 none := by
        simp only [h1, bind_tc_ok]
        rfl
      have h2bind {R : Type}
          (k : num.imp.dec2flt.decimal_seq.DecimalSeq → Std.I32 → Result R) :
          (do
            let (a,e) ← num.imp.dec2flt.slow.parse_long_mantissa_loop2
              F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 es
            k a e) = k d2 e2 := by
        simp only [h2, bind_tc_ok]
        rfl
      change num.imp.dec2flt.slow.parse_long_mantissa
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 s = _
      unfold num.imp.dec2flt.slow.parse_long_mantissa
      rcases ParserExponentConstants with ⟨_, _, hsig, _, _, hinf, _, hmin, _⟩
      change (do
        let i ← F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_TOTAL_BITS
        i - 1#u32) = ok 52#u32 at hsig
      change (do
        let i ← (do
          let i ← (do
            let i ← (do
              let i1 ← (do
                let i ← F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_TOTAL_BITS
                i - 1#u32)
              let i2 ← F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.BITS - i1
              i2 - 1#u32)
            let i1 ← 1#u32 <<< i
            i1 - 1#u32)
          i >>> 1#i32)
        let i1 ← lift (UScalar.hcast .I32 i)
        let i2 ← i1 - 1#i32
        -.i2) = ok (-1022)#i32 at hmin
      rw [hinf]
      simp only [num.imp.dec2flt.common.BiasedFp.zero_pow2, bind_tc_ok]
      rw [hparse]
      simp only [bind_tc_ok, if_neg hnumNe, if_neg hsmall, if_neg hlarge]
      rw [h0bind]
      rw [h1bind]
      rw [hdec]
      simp only [bind_tc_ok]
      rw [h2bind]
      rw [hmin]
      simp only [bind_tc_ok]
      rw [hsig]
      simp only [bind_tc_ok]
      have hcast : lift (UScalar.cast .Usize 52#u32) = ok 52#usize := by
        apply congrArg ok
        apply UScalar.eq_of_val_eq
        simp
      have hadd : (52#usize + 1#usize) = ok 53#usize := by
        obtain ⟨w, hw, hv⟩ := WP.spec_imp_exists
          (UScalar.add_spec (x := 52#usize) (y := 1#usize) (by scalar_tac))
        have heq : w = 53#usize := UScalar.eq_of_val_eq (by simpa using hv)
        simpa only [heq] using hw
      simp only [hcast, hadd, show (52#u32 + 1#u32) = ok 53#u32 by rfl,
        show (1#u64 <<< 53#u32) = ok 9007199254740992#u64 by rfl, bind_tc_ok]
      rfl
    have hnormal : -1022 < e2.val -> (2 ^ 52 : Nat) <=
        X / (2 : Real) ^ (e2.val - 52) := by
      intro hnormalExp
      have hd2eq : d2 = d1 := by
        rcases h2case with hno | hyes
        · exact hno.2.2.1
        · have : e2.val = -1022 := hyes.2.2.2
          omega
      rcases hshape1 with ⟨head, tail, hmap, hheadlo, hheadhi, hlen⟩
      have hval := P472_T1_DecimalValueBounds
        ((d1.digits.val.take d1.num_digits.val).map (fun b => b.val))
        head tail hmap (by omega)
        (by
          intro q hq
          rcases List.mem_map.mp hq with ⟨b, hb, rfl⟩
          exact hd1 b hb)
        d1.decimal_point.val
      have hlen1 : ((d1.digits.val.take d1.num_digits.val).map
          (fun b => b.val)).length = d1.num_digits.val := by
        rw [hmap]
        simpa using hlen
      have hVlower : (1 / 2 : Real) <=
          ((((d1.digits.val.take d1.num_digits.val).map
            (fun b => b.val)).foldl (fun n z => 10 * n + z) 0 : Nat) : Real) *
            (10 : Real) ^ (d1.decimal_point.val - (d1.num_digits.val : Int)) := by
        have hfirst := hval.1
        rw [hlen1, hp1] at hfirst
        norm_num at hfirst
        have hfirst' : (head : Real) / 10 <=
          ((((d1.digits.val.take d1.num_digits.val).map
            (fun b => b.val)).foldl (fun n z => 10 * n + z) 0 : Nat) : Real) *
            (10 : Real) ^ (d1.decimal_point.val - (d1.num_digits.val : Int)) := by
          rw [hp1, show (0 : Int) - (d1.num_digits.val : Int) =
            -(d1.num_digits.val : Int) by omega, zpow_neg, zpow_natCast]
          simpa using hfirst
        have hh : (5 : Real) <= head := by exact_mod_cast hheadlo
        have hhalf : (1 / 2 : Real) <= (head : Real) / 10 := by nlinarith
        exact hhalf.trans hfirst'
      have hXbase : (1 / 2 : Real) <=
          X / (2 : Real) ^ (e2.val + 1) := by
        rcases hends2 with ⟨eta, u, hXeq, hYeq, heta, hlt, htr⟩
        rw [hXeq, hd2eq]
        have hlower' : (1 / 2 : Real) <=
            (fun a : num.imp.dec2flt.decimal_seq.DecimalSeq =>
              ((((a.digits.val.take a.num_digits.val).map (fun b => b.val)).foldl
                (fun n z => 10 * n + z) 0 : Nat) : Real) *
                (10 : Real) ^ (a.decimal_point.val - (a.num_digits.val : Int))) d1 := by
          simpa [Vfun] using hVlower
        linarith
      have hscale : (2 : Real) ^ (53 : Nat) *
          (X / (2 : Real) ^ (e2.val + 1)) =
          X / (2 : Real) ^ (e2.val - 52) := by
        rw [div_eq_mul_inv, div_eq_mul_inv, ← zpow_neg, ← zpow_neg]
        calc
          (2 : Real) ^ (53 : Nat) * (X * (2 : Real) ^ (-(e2.val + 1))) =
              ((2 : Real) ^ (53 : Nat) * (2 : Real) ^ (-(e2.val + 1))) * X := by ring
          _ = (2 : Real) ^ (-(e2.val - 52)) * X := by
            rw [← zpow_natCast (2 : Real) 53, ← zpow_add₀
              (by norm_num : (2 : Real) ≠ 0)]
            congr 1
            norm_num
            omega
          _ = X * (2 : Real) ^ (-(e2.val - 52)) := by ring
      have hmul := mul_le_mul_of_nonneg_left hXbase (by positivity : (0 : Real) <= 2 ^ (53 : Nat))
      rw [hscale] at hmul
      have hcast : (2 ^ 52 : Nat) = ((2 : Real) ^ (52 : Nat) : Real) := by norm_num
      rw [hcast]
      set_option maxHeartbeats 800000 in nlinarith
    have hcarryWitness : ∃ (d4 : num.imp.dec2flt.decimal_seq.DecimalSeq) (m4 : Std.U64),
        (2^53 <= m.val ->
          num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d3 1#usize = ok d4 ∧
          num.imp.dec2flt.decimal_seq.DecimalSeq.round d4 = ok m4) ∧
        (2^53 <= m.val ->
          (X / (2 : Real) ^ (e2.val - 52)) / 2 <= (m4.val : Real) + 1/2 ∧
          (m4.val : Real) <= (X / (2 : Real) ^ (e2.val - 52)) / 2 + 1/2 ∧
          ((X / (2 : Real) ^ (e2.val - 52)) / 2 = (m4.val : Real) + 1/2 ∨
            (X / (2 : Real) ^ (e2.val - 52)) / 2 = (m4.val : Real) - 1/2 ->
            m4.val % 2 = 0)) := by
      by_cases hc : 2^53 <= m.val
      · obtain ⟨d4, m4, hs4, _, _, _, _, _, _, _, _, hr4, hu4, hl4, ht4⟩ :=
          P529_T1_CarryRoundExecution
            (X / (2 : Real) ^ (e2.val - 52))
            (Y / (2 : Real) ^ (e2.val - 52)) d3 m hn3 hd3 hshape3 htrim3
            (by omega) (le_of_lt hXpos3) hXhi3 hround hc hends3 hoddSep
        exact ⟨d4, m4, fun _ => ⟨hs4,hr4⟩, fun _ => ⟨hu4,hl4,ht4⟩⟩
      · exact ⟨d3, 0#u64, fun h => (hc h).elim, fun h => (hc h).elim⟩
    obtain ⟨d4,m4,hcarryCall,hcarryNear⟩ := hcarryWitness
    let raw : Nat :=
      if 1024 <= e2.val then 2047 * 2^52
      else if 2^53 <= m.val then
        if 1023 <= e2.val then 2047 * 2^52
        else (e2.val + 1024 - (if m4.val < 2^52 then 1 else 0)).toNat * 2^52 +
          m4.val % 2^52
      else (e2.val + 1023 - (if m.val < 2^52 then 1 else 0)).toNat * 2^52 +
        m.val % 2^52
    let Cert (r : Nat) : Prop :=
      0 <= X ∧ ((r = 2047 * 2^52 ∧
        (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) <= X) ∨
      (∃ j : Nat, j <= 2^53 ∧ r = j ∧
        X <= (2^53 : Nat) * (2 : Real) ^ (-1074 : Int) ∧
        X <= (j : Real) * (2 : Real) ^ (-1074 : Int) + (2 : Real) ^ (-1075 : Int) ∧
        (j : Real) * (2 : Real) ^ (-1074 : Int) <= X + (2 : Real) ^ (-1075 : Int) ∧
        (X = ((j : Real) + 1/2) * (2 : Real) ^ (-1074 : Int) ∨
          X = ((j : Real) - 1/2) * (2 : Real) ^ (-1074 : Int) -> j % 2 = 0)) ∨
      (∃ p j : Nat, 2 <= p ∧ p <= 2046 ∧ 2^52 <= j ∧ j <= 2^53 ∧
        (p < 2046 ∨ j < 2^53) ∧ r = p * 2^52 + (j - 2^52) ∧
        let delta : Real := (2 : Real) ^ ((p : Int) - 1075)
        (2^52 : Nat) * delta <= X ∧ X <= (2^53 : Nat) * delta ∧
        X < (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) ∧
        X <= (j : Real) * delta + delta/2 ∧ (j : Real) * delta <= X + delta/2 ∧
        (X = ((j : Real) + 1/2) * delta ∨ X = ((j : Real) - 1/2) * delta ->
          j % 2 = 0)))
    have henc : Cert raw := P529_T1_EncodingArithmetic X e2.val m.val m4.val
      he2range.1 he2range.2 hXpos3 hXhi3 hnormal hmUpper hmLower hmTie hcarryNear
    have hrun : ∃ fp : num.imp.dec2flt.common.BiasedFp,
        P9_T1_ActualDispatchOperations.slow s = ok fp ∧ rawOf fp = raw := by
      obtain ⟨b,p,hb,hp,hpval⟩ := biasOps e2 he2range.1 (by omega)
      have hfirst : (p >= 2047#i32) ↔ 1024 <= e2.val := by
        change (2047 : Int) <= p.val ↔ 1024 <= e2.val
        omega
      by_cases he : 1024 <= e2.val
      · refine ⟨{m := 0#u64,p_biased := 2047#i32}, ?_, ?_⟩
        · rw [hsource]
          dsimp only [suffix]
          rw [hb]
          simp only [bind_tc_ok, hp, if_pos (hfirst.mpr he)]
        · simp only [raw, if_pos he]
          change Nat.lor 0 (((2047 : Int).emod (2^64)).toNat * 2^52 % 2^64) = _
          norm_num [Nat.lor_eq]
          rfl
      · have hpf : ¬p >= 2047#i32 := fun h => he (hfirst.mp h)
        have hcguard : (m >= 9007199254740992#u64) ↔ 2^53 <= m.val := by
          change 9007199254740992 <= m.val ↔ 2^53 <= m.val
          norm_num
        have hresume : P9_T1_ActualDispatchOperations.slow s =
            (if m >= 9007199254740992#u64 then do
              let d4 ← num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d3 1#usize
              let e' ← e2 + 1#i32
              let m4 ← num.imp.dec2flt.decimal_seq.DecimalSeq.round d4
              let b' ← e' - (-1022)#i32
              let p' ← b' + 1#i32
              if p' >= 2047#i32 then ok {m := 0#u64,p_biased := 2047#i32}
              else pack e' m4
            else pack e2 m) := by
          rw [hsource]
          dsimp only [suffix]
          rw [hb]
          simp only [bind_tc_ok, hp, if_neg hpf, h3, hround]
        by_cases hc : 2^53 <= m.val
        · obtain ⟨hs4,hr4⟩ := hcarryCall hc
          obtain ⟨e',he',he'val⟩ := WP.spec_imp_exists
            (IScalar.add_spec (x := e2) (y := 1#i32)
              (by rw [IScalar.min_I32_eq]; norm_num; omega)
              (by rw [IScalar.max_I32_eq]; norm_num; omega))
          have hev : e'.val = e2.val + 1 := by simpa using he'val
          obtain ⟨b',p',hb',hp',hp'val⟩ := biasOps e' (by omega) (by omega)
          have hsecond : (p' >= 2047#i32) ↔ 1023 <= e2.val := by
            change (2047 : Int) <= p'.val ↔ 1023 <= e2.val
            omega
          have hresume4 : P9_T1_ActualDispatchOperations.slow s =
              (if p' >= 2047#i32 then ok {m := 0#u64,p_biased := 2047#i32}
                else pack e' m4) := by
            rw [hresume, if_pos (hcguard.mpr hc), hs4]
            simp only [bind_tc_ok, he', hr4, hb', hp']
          by_cases hec : 1023 <= e2.val
          · refine ⟨{m := 0#u64,p_biased := 2047#i32}, ?_, ?_⟩
            · rw [hresume4, if_pos (hsecond.mpr hec)]
            · simp only [raw, if_neg he, if_pos hc, if_pos hec]
              change Nat.lor 0 (((2047 : Int).emod (2^64)).toNat * 2^52 % 2^64) = _
              norm_num [Nat.lor_eq]
              rfl
          · obtain ⟨fp,hpack,hraw⟩ := packExec e' m4 (by omega) (by omega)
            refine ⟨fp, ?_, ?_⟩
            · rw [hresume4, if_neg (fun h => hec (hsecond.mp h)), hpack]
            · simp only [raw, if_neg he, if_pos hc, if_neg hec]
              rw [hraw, hev]
              congr 3 <;> omega
        · obtain ⟨fp,hpack,hraw⟩ := packExec e2 m he2range.1 (by omega)
          refine ⟨fp, ?_, ?_⟩
          · rw [hresume, if_neg (fun h => hc (hcguard.mp h)), hpack]
          · simpa only [raw, if_neg he, if_neg hc] using hraw
    obtain ⟨fp,hcall,hraw⟩ := hrun
    have hcert : Cert (rawOf fp) := by rw [hraw]; exact henc
    refine ⟨fp,hcall,hcert,?_⟩
    have hbound : rawOf fp <= 2047 * 2^52 := by
      rcases hcert.2 with htop | hlow | hnormal
      · omega
      · obtain ⟨j,hj,hr,_⟩ := hlow
        rw [hr]
        norm_num at hj ⊢
        omega
      · obtain ⟨p,j,_,hp,_,hj,_,hr,_⟩ := hnormal
        rw [hr]
        norm_num at hp hj ⊢
        omega
    have hrawlt : rawOf fp < 2^64 := lt_of_le_of_lt hbound (by norm_num)
    let magnitude : F64 := U64.ofNatCore (rawOf fp) hrawlt
    have hasm : DecimalGoalVocabularyDefault.assembledWord fp magnitude := by
      change magnitude.val = rawOf fp
      exact U64.ofNatCore_val_eq hrawlt
    exact ⟨magnitude,hasm,Binary64NearestEvenAssembly X fp magnitude hcert hasm⟩
  )

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P495_T1_SlowLoop0Execution
import Tablet.P529_T1_Loop1Execution
import Tablet.P529_T1_NormalizationBounds
import Tablet.P529_T1_Loop2Execution
import Tablet.P529_T1_FinalShiftCertificate
import Tablet.P529_T1_SeparationPackage
import Tablet.P529_T1_RoundArbitraryEndpoint
import Tablet.P529_T1_CarryRoundExecution
import Tablet.P529_T1_EncodingArithmetic
import Tablet.ParserExponentConstants
import Tablet.Binary64NearestEvenAssembly
import Tablet.ModelU64BitAnd
import Tablet.ModelU64BitOr
import Tablet.ModelU64Shl
import Tablet.P9_T1_ActualDispatchOperations
import Tablet.num_imp_dec2flt_decimal_seq_parse_decimal_seq
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P529_T1_RegularSlowExecution]
theorem P529_T1_RegularSlowExecution (s : Slice Std.U8) (X Y : Real)
    (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (hparse : num.imp.dec2flt.decimal_seq.parse_decimal_seq s = ok d)
    (hpointLo : -324 <= d.decimal_point.val)
    (hpointHi : d.decimal_point.val < 310)
    (hinput : d.num_digits.val <= 768 /\
      (forall b, b ∈ d.digits.val.take d.num_digits.val -> b.val < 10) /\
      (exists h tail,
        (d.digits.val.take d.num_digits.val).map (fun b => b.val) = h :: tail /\
        1 <= h /\ h < 10 /\ (h :: tail).length = d.num_digits.val)) :
    let V := fun a : num.imp.dec2flt.decimal_seq.DecimalSeq =>
      ((((a.digits.val.take a.num_digits.val).map (fun b => b.val)).foldl
        (fun n z => 10 * n + z) 0 : Nat) : Real) *
        (10 : Real) ^ (a.decimal_point.val - (a.num_digits.val : Int))
    Y = V d + (10 : Real) ^ (d.decimal_point.val - 768) ->
    (exists eta : Real, X = V d + eta /\ 0 <= eta /\
      eta < (10 : Real) ^ (d.decimal_point.val - 768) /\
      (d.truncated = true <-> 0 < eta)) ->
    exists fp : num.imp.dec2flt.common.BiasedFp,
      P9_T1_ActualDispatchOperations.slow s = ok fp /\
      (let raw : Nat := Nat.lor fp.m.val
        (((fp.p_biased.val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64))
       0 <= X /\ ((raw = 2047 * (2 ^ 52) /\
        (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) <= X) \/
      (exists j : Nat,
        j <= 2 ^ 53 /\ raw = j /\
        X <= (2 ^ 53 : Nat) * (2 : Real) ^ (-1074 : Int) /\
        X <= (j : Real) * (2 : Real) ^ (-1074 : Int) + (2 : Real) ^ (-1075 : Int) /\
        (j : Real) * (2 : Real) ^ (-1074 : Int) <= X + (2 : Real) ^ (-1075 : Int) /\
        (X = ((j : Real) + 1 / 2) * (2 : Real) ^ (-1074 : Int) \/
          X = ((j : Real) - 1 / 2) * (2 : Real) ^ (-1074 : Int) -> j % 2 = 0)) \/
      (exists p j : Nat,
        2 <= p /\ p <= 2046 /\ 2 ^ 52 <= j /\ j <= 2 ^ 53 /\
        (p < 2046 \/ j < 2 ^ 53) /\
        raw = p * (2 ^ 52) + (j - 2 ^ 52) /\
        let delta : Real := (2 : Real) ^ ((p : Int) - 1075)
        (2 ^ 52 : Nat) * delta <= X /\ X <= (2 ^ 53 : Nat) * delta /\
        X < (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) /\
        X <= (j : Real) * delta + delta / 2 /\
        (j : Real) * delta <= X + delta / 2 /\
        (X = ((j : Real) + 1 / 2) * delta \/
          X = ((j : Real) - 1 / 2) * delta -> j % 2 = 0)))) /\
      (exists magnitude : F64,
        DecimalGoalVocabularyDefault.assembledWord fp magnitude /\
        DecimalGoalVocabularyDefault.roundsMagnitude X magnitude) := by
-- BODY
  exact (set_option maxHeartbeats 2000000 in by
    let pack (e : Std.I32) (m : Std.U64) : Result num.imp.dec2flt.common.BiasedFp := do
      let b ← e - (-1022)#i32
      let p ← b + 1#i32
      let u ← 1#u64 <<< 52#u32
      let p' ← if m < u then p - 1#i32 else ok p
      let u' ← 1#u64 <<< 52#u32
      let mask ← u' - 1#u64
      let a ← lift (m &&& mask)
      ok { m := a, p_biased := p' }

    let suffix (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (e : Std.I32) :
        Result num.imp.dec2flt.common.BiasedFp := do
      let b ← e - (-1022)#i32
      let p ← b + 1#i32
      if p >= 2047#i32 then ok { m := 0#u64, p_biased := 2047#i32 }
      else
        let d3 ← num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d 53#usize
        let m ← num.imp.dec2flt.decimal_seq.DecimalSeq.round d3
        if m >= 9007199254740992#u64 then
          let d4 ← num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d3 1#usize
          let e' ← e + 1#i32
          let m4 ← num.imp.dec2flt.decimal_seq.DecimalSeq.round d4
          let b' ← e' - (-1022)#i32
          let p' ← b' + 1#i32
          if p' >= 2047#i32 then ok { m := 0#u64, p_biased := 2047#i32 }
          else pack e' m4
        else pack e m

    let rawOf (fp : num.imp.dec2flt.common.BiasedFp) : Nat :=
      Nat.lor fp.m.val (((fp.p_biased.val.emod (2^64)).toNat * 2^52) % 2^64)

    have biasOps (e : Std.I32) (hlo : -1022 <= e.val) (hhi : e.val <= 1261) :
        ∃ b p : Std.I32, e - (-1022)#i32 = ok b ∧ b + 1#i32 = ok p ∧
          p.val = e.val + 1023 := by
      obtain ⟨b, hb, hbval⟩ := WP.spec_imp_exists
        (IScalar.sub_spec (x := e) (y := (-1022)#i32)
          (by rw [IScalar.min_I32_eq]; norm_num; omega)
          (by rw [IScalar.max_I32_eq]; norm_num; omega))
      obtain ⟨p, hp, hpval⟩ := WP.spec_imp_exists
        (IScalar.add_spec (x := b) (y := 1#i32)
          (by rw [IScalar.min_I32_eq]; norm_num at hbval ⊢; omega)
          (by rw [IScalar.max_I32_eq]; norm_num at hbval ⊢; omega))
      exact ⟨b,p,hb,hp,by norm_num at hbval hpval; omega⟩

    have mask52 (z : Std.U64) : (z &&& 4503599627370495#u64).val = z.val % 2^52 := by
      change (z.bv &&& 4503599627370495#64).toNat = z.bv.toNat % 2^52
      rw [show 4503599627370495#64 = 2#64^52 - 1#64 by decide,
        BitVec.and_two_pow_sub_one_eq_mod]
      simp

    have fieldOr (a ef : Nat) (ha : a < 2^52)
        (hef : ef * 2^52 < 2^64) : a ||| (ef * 2^52) = a + ef * 2^52 := by
      have hland : a &&& (ef * 2^52) = 0 := by
        apply Nat.eq_of_testBit_eq
        intro j
        rw [Nat.testBit_land, Nat.testBit_mul_two_pow]
        by_cases hj : j < 52
        · simp [hj]
        · have haj : a.testBit j = false := by
            apply Nat.testBit_eq_false_of_lt
            exact lt_of_lt_of_le ha (Nat.pow_le_pow_right (by omega) (by omega))
          simp [haj]
      let av : BitVec 64 := BitVec.ofNat 64 a
      let ev : BitVec 64 := BitVec.ofNat 64 (ef * 2^52)
      have hlandBV : av &&& ev = 0#64 := by
        apply BitVec.eq_of_toNat_eq
        simp only [av, ev, BitVec.toNat_and, BitVec.toNat_ofNat]
        rw [Nat.mod_eq_of_lt (ha.trans (by norm_num)), Nat.mod_eq_of_lt hef, hland]
      calc
        a ||| (ef * 2^52) = (av ||| ev).toNat := by
          simp only [av, ev, BitVec.toNat_or, BitVec.toNat_ofNat]
          rw [Nat.mod_eq_of_lt (ha.trans (by norm_num)), Nat.mod_eq_of_lt hef]
        _ = (av + ev).toNat := by rw [BitVec.add_eq_or_of_and_eq_zero av ev hlandBV]
        _ = a + ef * 2^52 := by
          rw [BitVec.toNat_add_of_and_eq_zero hlandBV]
          simp only [av, ev, BitVec.toNat_ofNat]
          rw [Nat.mod_eq_of_lt (ha.trans (by norm_num)), Nat.mod_eq_of_lt hef]

    have packExec (e : Std.I32) (m : Std.U64)
        (hlo : -1022 <= e.val) (hhi : e.val <= 1023) :
        ∃ fp : num.imp.dec2flt.common.BiasedFp, pack e m = ok fp ∧
          rawOf fp = (e.val + 1023 - (if m.val < 2^52 then 1 else 0)).toNat * 2^52 +
            m.val % 2^52 := by
      obtain ⟨b,p,hb,hp,hpval⟩ := biasOps e hlo (by omega)
      have hpbound : 1 <= p.val ∧ p.val <= 2046 := by omega
      have hpower : ∃ p' : Std.I32,
          (if m < 4503599627370496#u64 then p - 1#i32 else ok p) = ok p' ∧
          p'.val = e.val + 1023 - (if m.val < 2^52 then 1 else 0) ∧
          0 <= p'.val ∧ p'.val <= 2046 := by
        by_cases hm : m.val < 2^52
        · have hm' : m < 4503599627370496#u64 := by
            change m.val < 4503599627370496
            norm_num at hm
            exact hm
          obtain ⟨p',hp',hv⟩ := WP.spec_imp_exists
            (IScalar.sub_spec (x := p) (y := 1#i32)
              (by rw [IScalar.min_I32_eq]; norm_num; omega)
              (by rw [IScalar.max_I32_eq]; norm_num; omega))
          refine ⟨p', by rw [if_pos hm']; exact hp', ?_, ?_, ?_⟩
          all_goals norm_num at hv; try simp only [if_pos hm]
          all_goals omega
        · have hm' : ¬m < 4503599627370496#u64 := by
            change ¬m.val < 4503599627370496
            norm_num at hm
            omega
          refine ⟨p, by rw [if_neg hm'], ?_, by omega, by omega⟩
          simp only [if_neg hm, Int.sub_zero]
          exact hpval
      obtain ⟨p',hp',hp'val,hp'lo,hp'hi⟩ := hpower
      let fp : num.imp.dec2flt.common.BiasedFp :=
        { m := m &&& 4503599627370495#u64, p_biased := p' }
      refine ⟨fp, ?_, ?_⟩
      · unfold pack
        simp only [hb, hp, bind_tc_ok,
          show (1#u64 <<< 52#u32) = ok 4503599627370496#u64 by rfl,
          hp', show (4503599627370496#u64 - 1#u64) = ok 4503599627370495#u64 by rfl]
        rfl
      · have hef : p'.val.toNat * 2^52 < 2^64 := by
          have : p'.val.toNat <= 2046 := by omega
          norm_num
          omega
        unfold rawOf
        change Nat.lor (m &&& 4503599627370495#u64).val
          (((p'.val.emod (2^64)).toNat * 2^52) % 2^64) = _
        rw [mask52]
        have hmod : p'.val.emod (2^64) = p'.val := by
          exact Int.emod_eq_of_lt hp'lo (by norm_num; omega)
        rw [hmod, Nat.mod_eq_of_lt hef]
        change (m.val % 2^52) ||| (p'.val.toNat * 2^52) = _
        rw [fieldOr (m.val % 2^52) p'.val.toNat (Nat.mod_lt _ (by positivity)) hef, hp'val]
        omega
    intro Vfun hY hX
    classical
    have h0pkg := P495_T1_SlowLoop0Execution X Y d hpointLo hpointHi hinput
      (by simpa [Vfun] using hY) (by simpa [Vfun] using hX)
    rcases h0pkg with ⟨d0, e0, steps, h0, hsteps, he0lo, he0hi, hXlo,
      hp0lo, hp0hi, hn0, hd0, hshape0, htrim0, hends0⟩
    -- The source call starts from exactly the parsed record and the certified loop zero.
    have hinput0 : d0.num_digits.val <= 768 /\
        (forall b, b ∈ d0.digits.val.take d0.num_digits.val -> b.val < 10) /\
        (exists h tail,
          (d0.digits.val.take d0.num_digits.val).map (fun b => b.val) = h :: tail /\
          1 <= h /\ h < 10 /\ (h :: tail).length = d0.num_digits.val) :=
      ⟨hn0, hd0, hshape0⟩
    have h1pkg := P529_T1_Loop1Execution X Y d0 e0 hp0lo hp0hi he0lo
      hinput0 (by simpa [Vfun] using hends0)
    rcases h1pkg with ⟨d1, e1, bound, h1, hbound, hboundlo, hboundhi,
      hp1, hn1, hheadlo, hheadhi, hn1cap, hd1, hshape1, hends1⟩
    have hnorm := P529_T1_NormalizationBounds X Y d d0 d1 e0 e1
      hpointLo hpointHi hinput h0 h1 (by simpa [Vfun] using hY)
      (by simpa [Vfun] using hX)
    rcases hnorm with ⟨_, hXY, hYbound, he1lo, he1hi⟩
    have h2pkg := P529_T1_Loop2Execution X Y d1 e1 he1lo hp1
      ⟨hn1cap, hd1, hshape1⟩ (by simpa [Vfun] using hends1)
    rcases h2pkg with ⟨es, e2, d2, sh, hdec, hesval, heslo, h2, he2val,
      hshval, hshle, h2case, hp2lo, hp2hi, hn2, hd2, hshape2, htrim2, hends2⟩
    have he2range : -1022 <= e2.val /\ e2.val <= 1260 := by
      rw [he2val]
      omega
    have he1le : e1.val <= e2.val + 1 := by rw [he2val, hesval]; omega
    have hYbound2 : Y / (2 : Real) ^ (e2.val + 1) <= 1 := by
      have hYle : Y <= (2 : Real) ^ e1.val :=
        (div_le_one (by positivity)).mp hYbound
      have hpow : (2 : Real) ^ e1.val <= (2 : Real) ^ (e2.val + 1) :=
        zpow_le_zpow_right₀ (by norm_num) he1le
      exact (div_le_one (by positivity)).2 (hYle.trans hpow)
    have h3pkg := P529_T1_FinalShiftCertificate X Y d2 e2 hp2lo hp2hi
      ⟨hn2, hd2, hshape2⟩ hYbound2 (by simpa using hends2)
    rcases h3pkg with ⟨d3, h3, hp3lo, hp3hi, hn3, hd3, hshape3, htrim3,
      hXpos3, hXhi3, hends3⟩
    have hsepPkg := P529_T1_SeparationPackage X Y d d0 d1 d2 d3 e0 e1 es e2
      hpointLo hpointHi hinput h0 h1 hdec h2 h3
      (by simpa [Vfun] using hY) (by simpa [Vfun] using hX)
    rcases hsepPkg with ⟨hhalfSep, hoddSep⟩
    have hroundPkg := P529_T1_RoundArbitraryEndpoint d3
      (X / (2 : Real) ^ (e2.val - 52)) (Y / (2 : Real) ^ (e2.val - 52))
      hn3 hd3 (Or.inr ⟨hshape3, htrim3⟩) (by omega) (by positivity)
      (by
        have hbig : (2 : Real) ^ (53 : Nat) < (2 : Real) ^ (64 : Nat) - 1 / 2 := by norm_num
        exact lt_trans hXhi3 hbig)
      (by simpa using hends3)
      (by
        intro n
        simpa using hhalfSep n)
    rcases hroundPkg with ⟨m, hround, hmUpper, hmLower, hmTie⟩
    have hnumNe : d.num_digits ≠ 0#usize := by
      rcases hinput.2.2 with ⟨head, tail, hmap, hhead, hheadHi, hlen⟩
      intro heq
      have hv := congrArg (fun n : Std.Usize => n.val) heq
      simp at hv
      rw [← hlen] at hv
      simp at hv
    have hsmall : ¬ d.decimal_point < (-324)#i32 := by
      intro hs
      have hs' : d.decimal_point.val < -324 := by
        simpa only [IScalar.lt_equiv] using hs
      omega
    have hlarge : ¬ d.decimal_point >= 310#i32 := by
      intro hl
      have hl' : (310 : Int) <= d.decimal_point.val := by
        simpa only [IScalar.le_equiv] using hl
      omega
    have hsource : P9_T1_ActualDispatchOperations.slow s = suffix d2 e2 := by
      have h0bind {R : Type}
          (k : Std.Usize → Std.I32 → Bool → Array Std.U8 768#usize → Std.I32 →
            Option num.imp.dec2flt.common.BiasedFp → Result R) :
          (do
            let (n,p,tr,a,e,r) ← num.imp.dec2flt.slow.parse_long_mantissa_loop0
              F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
              {m := 0#u64,p_biased := 0#i32}
              d.num_digits d.decimal_point d.truncated d.digits 0#i32 none
            k n p tr a e r) = k d0.num_digits d0.decimal_point d0.truncated
                d0.digits e0 none := by
        simp only [h0, bind_tc_ok]
        rfl
      have h1bind {R : Type}
          (k : Std.Usize → Std.I32 → Bool → Array Std.U8 768#usize → Std.I32 →
            Option num.imp.dec2flt.common.BiasedFp → Result R) :
          (do
            let (n,p,tr,a,e,r) ← num.imp.dec2flt.slow.parse_long_mantissa_loop1
              F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
              {m := 0#u64,p_biased := 2047#i32}
              d0.num_digits d0.decimal_point d0.truncated d0.digits e0 none
            k n p tr a e r) = k d1.num_digits d1.decimal_point d1.truncated
                d1.digits e1 none := by
        simp only [h1, bind_tc_ok]
        rfl
      have h2bind {R : Type}
          (k : num.imp.dec2flt.decimal_seq.DecimalSeq → Std.I32 → Result R) :
          (do
            let (a,e) ← num.imp.dec2flt.slow.parse_long_mantissa_loop2
              F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 es
            k a e) = k d2 e2 := by
        simp only [h2, bind_tc_ok]
        rfl
      change num.imp.dec2flt.slow.parse_long_mantissa
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 s = _
      unfold num.imp.dec2flt.slow.parse_long_mantissa
      rcases ParserExponentConstants with ⟨_, _, hsig, _, _, hinf, _, hmin, _⟩
      change (do
        let i ← F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_TOTAL_BITS
        i - 1#u32) = ok 52#u32 at hsig
      change (do
        let i ← (do
          let i ← (do
            let i ← (do
              let i1 ← (do
                let i ← F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_TOTAL_BITS
                i - 1#u32)
              let i2 ← F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.BITS - i1
              i2 - 1#u32)
            let i1 ← 1#u32 <<< i
            i1 - 1#u32)
          i >>> 1#i32)
        let i1 ← lift (UScalar.hcast .I32 i)
        let i2 ← i1 - 1#i32
        -.i2) = ok (-1022)#i32 at hmin
      rw [hinf]
      simp only [num.imp.dec2flt.common.BiasedFp.zero_pow2, bind_tc_ok]
      rw [hparse]
      simp only [bind_tc_ok, if_neg hnumNe, if_neg hsmall, if_neg hlarge]
      rw [h0bind]
      rw [h1bind]
      rw [hdec]
      simp only [bind_tc_ok]
      rw [h2bind]
      rw [hmin]
      simp only [bind_tc_ok]
      rw [hsig]
      simp only [bind_tc_ok]
      have hcast : lift (UScalar.cast .Usize 52#u32) = ok 52#usize := by
        apply congrArg ok
        apply UScalar.eq_of_val_eq
        simp
      have hadd : (52#usize + 1#usize) = ok 53#usize := by
        obtain ⟨w, hw, hv⟩ := WP.spec_imp_exists
          (UScalar.add_spec (x := 52#usize) (y := 1#usize) (by scalar_tac))
        have heq : w = 53#usize := UScalar.eq_of_val_eq (by simpa using hv)
        simpa only [heq] using hw
      simp only [hcast, hadd, show (52#u32 + 1#u32) = ok 53#u32 by rfl,
        show (1#u64 <<< 53#u32) = ok 9007199254740992#u64 by rfl, bind_tc_ok]
      rfl
    have hnormal : -1022 < e2.val -> (2 ^ 52 : Nat) <=
        X / (2 : Real) ^ (e2.val - 52) := by
      intro hnormalExp
      have hd2eq : d2 = d1 := by
        rcases h2case with hno | hyes
        · exact hno.2.2.1
        · have : e2.val = -1022 := hyes.2.2.2
          omega
      rcases hshape1 with ⟨head, tail, hmap, hheadlo, hheadhi, hlen⟩
      have hval := P472_T1_DecimalValueBounds
        ((d1.digits.val.take d1.num_digits.val).map (fun b => b.val))
        head tail hmap (by omega)
        (by
          intro q hq
          rcases List.mem_map.mp hq with ⟨b, hb, rfl⟩
          exact hd1 b hb)
        d1.decimal_point.val
      have hlen1 : ((d1.digits.val.take d1.num_digits.val).map
          (fun b => b.val)).length = d1.num_digits.val := by
        rw [hmap]
        simpa using hlen
      have hVlower : (1 / 2 : Real) <=
          ((((d1.digits.val.take d1.num_digits.val).map
            (fun b => b.val)).foldl (fun n z => 10 * n + z) 0 : Nat) : Real) *
            (10 : Real) ^ (d1.decimal_point.val - (d1.num_digits.val : Int)) := by
        have hfirst := hval.1
        rw [hlen1, hp1] at hfirst
        norm_num at hfirst
        have hfirst' : (head : Real) / 10 <=
          ((((d1.digits.val.take d1.num_digits.val).map
            (fun b => b.val)).foldl (fun n z => 10 * n + z) 0 : Nat) : Real) *
            (10 : Real) ^ (d1.decimal_point.val - (d1.num_digits.val : Int)) := by
          rw [hp1, show (0 : Int) - (d1.num_digits.val : Int) =
            -(d1.num_digits.val : Int) by omega, zpow_neg, zpow_natCast]
          simpa using hfirst
        have hh : (5 : Real) <= head := by exact_mod_cast hheadlo
        have hhalf : (1 / 2 : Real) <= (head : Real) / 10 := by nlinarith
        exact hhalf.trans hfirst'
      have hXbase : (1 / 2 : Real) <=
          X / (2 : Real) ^ (e2.val + 1) := by
        rcases hends2 with ⟨eta, u, hXeq, hYeq, heta, hlt, htr⟩
        rw [hXeq, hd2eq]
        have hlower' : (1 / 2 : Real) <=
            (fun a : num.imp.dec2flt.decimal_seq.DecimalSeq =>
              ((((a.digits.val.take a.num_digits.val).map (fun b => b.val)).foldl
                (fun n z => 10 * n + z) 0 : Nat) : Real) *
                (10 : Real) ^ (a.decimal_point.val - (a.num_digits.val : Int))) d1 := by
          simpa [Vfun] using hVlower
        linarith
      have hscale : (2 : Real) ^ (53 : Nat) *
          (X / (2 : Real) ^ (e2.val + 1)) =
          X / (2 : Real) ^ (e2.val - 52) := by
        rw [div_eq_mul_inv, div_eq_mul_inv, ← zpow_neg, ← zpow_neg]
        calc
          (2 : Real) ^ (53 : Nat) * (X * (2 : Real) ^ (-(e2.val + 1))) =
              ((2 : Real) ^ (53 : Nat) * (2 : Real) ^ (-(e2.val + 1))) * X := by ring
          _ = (2 : Real) ^ (-(e2.val - 52)) * X := by
            rw [← zpow_natCast (2 : Real) 53, ← zpow_add₀
              (by norm_num : (2 : Real) ≠ 0)]
            congr 1
            norm_num
            omega
          _ = X * (2 : Real) ^ (-(e2.val - 52)) := by ring
      have hmul := mul_le_mul_of_nonneg_left hXbase (by positivity : (0 : Real) <= 2 ^ (53 : Nat))
      rw [hscale] at hmul
      have hcast : (2 ^ 52 : Nat) = ((2 : Real) ^ (52 : Nat) : Real) := by norm_num
      rw [hcast]
      set_option maxHeartbeats 800000 in nlinarith
    have hcarryWitness : ∃ (d4 : num.imp.dec2flt.decimal_seq.DecimalSeq) (m4 : Std.U64),
        (2^53 <= m.val ->
          num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d3 1#usize = ok d4 ∧
          num.imp.dec2flt.decimal_seq.DecimalSeq.round d4 = ok m4) ∧
        (2^53 <= m.val ->
          (X / (2 : Real) ^ (e2.val - 52)) / 2 <= (m4.val : Real) + 1/2 ∧
          (m4.val : Real) <= (X / (2 : Real) ^ (e2.val - 52)) / 2 + 1/2 ∧
          ((X / (2 : Real) ^ (e2.val - 52)) / 2 = (m4.val : Real) + 1/2 ∨
            (X / (2 : Real) ^ (e2.val - 52)) / 2 = (m4.val : Real) - 1/2 ->
            m4.val % 2 = 0)) := by
      by_cases hc : 2^53 <= m.val
      · obtain ⟨d4, m4, hs4, _, _, _, _, _, _, _, _, hr4, hu4, hl4, ht4⟩ :=
          P529_T1_CarryRoundExecution
            (X / (2 : Real) ^ (e2.val - 52))
            (Y / (2 : Real) ^ (e2.val - 52)) d3 m hn3 hd3 hshape3 htrim3
            (by omega) (le_of_lt hXpos3) hXhi3 hround hc hends3 hoddSep
        exact ⟨d4, m4, fun _ => ⟨hs4,hr4⟩, fun _ => ⟨hu4,hl4,ht4⟩⟩
      · exact ⟨d3, 0#u64, fun h => (hc h).elim, fun h => (hc h).elim⟩
    obtain ⟨d4,m4,hcarryCall,hcarryNear⟩ := hcarryWitness
    let raw : Nat :=
      if 1024 <= e2.val then 2047 * 2^52
      else if 2^53 <= m.val then
        if 1023 <= e2.val then 2047 * 2^52
        else (e2.val + 1024 - (if m4.val < 2^52 then 1 else 0)).toNat * 2^52 +
          m4.val % 2^52
      else (e2.val + 1023 - (if m.val < 2^52 then 1 else 0)).toNat * 2^52 +
        m.val % 2^52
    let Cert (r : Nat) : Prop :=
      0 <= X ∧ ((r = 2047 * 2^52 ∧
        (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) <= X) ∨
      (∃ j : Nat, j <= 2^53 ∧ r = j ∧
        X <= (2^53 : Nat) * (2 : Real) ^ (-1074 : Int) ∧
        X <= (j : Real) * (2 : Real) ^ (-1074 : Int) + (2 : Real) ^ (-1075 : Int) ∧
        (j : Real) * (2 : Real) ^ (-1074 : Int) <= X + (2 : Real) ^ (-1075 : Int) ∧
        (X = ((j : Real) + 1/2) * (2 : Real) ^ (-1074 : Int) ∨
          X = ((j : Real) - 1/2) * (2 : Real) ^ (-1074 : Int) -> j % 2 = 0)) ∨
      (∃ p j : Nat, 2 <= p ∧ p <= 2046 ∧ 2^52 <= j ∧ j <= 2^53 ∧
        (p < 2046 ∨ j < 2^53) ∧ r = p * 2^52 + (j - 2^52) ∧
        let delta : Real := (2 : Real) ^ ((p : Int) - 1075)
        (2^52 : Nat) * delta <= X ∧ X <= (2^53 : Nat) * delta ∧
        X < (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) ∧
        X <= (j : Real) * delta + delta/2 ∧ (j : Real) * delta <= X + delta/2 ∧
        (X = ((j : Real) + 1/2) * delta ∨ X = ((j : Real) - 1/2) * delta ->
          j % 2 = 0)))
    have henc : Cert raw := P529_T1_EncodingArithmetic X e2.val m.val m4.val
      he2range.1 he2range.2 hXpos3 hXhi3 hnormal hmUpper hmLower hmTie hcarryNear
    have hrun : ∃ fp : num.imp.dec2flt.common.BiasedFp,
        P9_T1_ActualDispatchOperations.slow s = ok fp ∧ rawOf fp = raw := by
      obtain ⟨b,p,hb,hp,hpval⟩ := biasOps e2 he2range.1 (by omega)
      have hfirst : (p >= 2047#i32) ↔ 1024 <= e2.val := by
        change (2047 : Int) <= p.val ↔ 1024 <= e2.val
        omega
      by_cases he : 1024 <= e2.val
      · refine ⟨{m := 0#u64,p_biased := 2047#i32}, ?_, ?_⟩
        · rw [hsource]
          dsimp only [suffix]
          rw [hb]
          simp only [bind_tc_ok, hp, if_pos (hfirst.mpr he)]
        · simp only [raw, if_pos he]
          change Nat.lor 0 (((2047 : Int).emod (2^64)).toNat * 2^52 % 2^64) = _
          norm_num [Nat.lor_eq]
          rfl
      · have hpf : ¬p >= 2047#i32 := fun h => he (hfirst.mp h)
        have hcguard : (m >= 9007199254740992#u64) ↔ 2^53 <= m.val := by
          change 9007199254740992 <= m.val ↔ 2^53 <= m.val
          norm_num
        have hresume : P9_T1_ActualDispatchOperations.slow s =
            (if m >= 9007199254740992#u64 then do
              let d4 ← num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d3 1#usize
              let e' ← e2 + 1#i32
              let m4 ← num.imp.dec2flt.decimal_seq.DecimalSeq.round d4
              let b' ← e' - (-1022)#i32
              let p' ← b' + 1#i32
              if p' >= 2047#i32 then ok {m := 0#u64,p_biased := 2047#i32}
              else pack e' m4
            else pack e2 m) := by
          rw [hsource]
          dsimp only [suffix]
          rw [hb]
          simp only [bind_tc_ok, hp, if_neg hpf, h3, hround]
        by_cases hc : 2^53 <= m.val
        · obtain ⟨hs4,hr4⟩ := hcarryCall hc
          obtain ⟨e',he',he'val⟩ := WP.spec_imp_exists
            (IScalar.add_spec (x := e2) (y := 1#i32)
              (by rw [IScalar.min_I32_eq]; norm_num; omega)
              (by rw [IScalar.max_I32_eq]; norm_num; omega))
          have hev : e'.val = e2.val + 1 := by simpa using he'val
          obtain ⟨b',p',hb',hp',hp'val⟩ := biasOps e' (by omega) (by omega)
          have hsecond : (p' >= 2047#i32) ↔ 1023 <= e2.val := by
            change (2047 : Int) <= p'.val ↔ 1023 <= e2.val
            omega
          have hresume4 : P9_T1_ActualDispatchOperations.slow s =
              (if p' >= 2047#i32 then ok {m := 0#u64,p_biased := 2047#i32}
                else pack e' m4) := by
            rw [hresume, if_pos (hcguard.mpr hc), hs4]
            simp only [bind_tc_ok, he', hr4, hb', hp']
          by_cases hec : 1023 <= e2.val
          · refine ⟨{m := 0#u64,p_biased := 2047#i32}, ?_, ?_⟩
            · rw [hresume4, if_pos (hsecond.mpr hec)]
            · simp only [raw, if_neg he, if_pos hc, if_pos hec]
              change Nat.lor 0 (((2047 : Int).emod (2^64)).toNat * 2^52 % 2^64) = _
              norm_num [Nat.lor_eq]
              rfl
          · obtain ⟨fp,hpack,hraw⟩ := packExec e' m4 (by omega) (by omega)
            refine ⟨fp, ?_, ?_⟩
            · rw [hresume4, if_neg (fun h => hec (hsecond.mp h)), hpack]
            · simp only [raw, if_neg he, if_pos hc, if_neg hec]
              rw [hraw, hev]
              congr 3 <;> omega
        · obtain ⟨fp,hpack,hraw⟩ := packExec e2 m he2range.1 (by omega)
          refine ⟨fp, ?_, ?_⟩
          · rw [hresume, if_neg (fun h => hc (hcguard.mp h)), hpack]
          · simpa only [raw, if_neg he, if_neg hc] using hraw
    obtain ⟨fp,hcall,hraw⟩ := hrun
    have hcert : Cert (rawOf fp) := by rw [hraw]; exact henc
    refine ⟨fp,hcall,hcert,?_⟩
    have hbound : rawOf fp <= 2047 * 2^52 := by
      rcases hcert.2 with htop | hlow | hnormal
      · omega
      · obtain ⟨j,hj,hr,_⟩ := hlow
        rw [hr]
        norm_num at hj ⊢
        omega
      · obtain ⟨p,j,_,hp,_,hj,_,hr,_⟩ := hnormal
        rw [hr]
        norm_num at hp hj ⊢
        omega
    have hrawlt : rawOf fp < 2^64 := lt_of_le_of_lt hbound (by norm_num)
    let magnitude : F64 := U64.ofNatCore (rawOf fp) hrawlt
    have hasm : DecimalGoalVocabularyDefault.assembledWord fp magnitude := by
      change magnitude.val = rawOf fp
      exact U64.ofNatCore_val_eq hrawlt
    exact ⟨magnitude,hasm,Binary64NearestEvenAssembly X fp magnitude hcert hasm⟩
  )

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (198)
More program dependencies (156)
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 208 entries
Technical checking records
Local closure summary
{
  "node": "P529_T1_RegularSlowExecution",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "ef3c493c67a53e10afa8396a9e55049c2ea144e8e7419bc9dcaeb412aac68682",
  "active_statement_hash": "aca95b66de9a5d15f61f854865ff0090daf273031e4447fe5303f60fc478f2bc",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "576e6bf9480cfd32f53c309c7ac8fb115c1ba6e413f299ed5ff9fa7c647e6769",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-200",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "Binary64NearestEvenAssembly": "028531442bb8a66c",
    "P472_T1_DecimalValueBounds": "4b5c924b5d52bc9d",
    "P495_T1_SlowLoop0Execution": "5a1e49d921a1825a",
    "P529_T1_CarryRoundExecution": "026983daf0ccba18",
    "P529_T1_EncodingArithmetic": "f35d99c23b5ee50f",
    "P529_T1_FinalShiftCertificate": "6a5bd46c366c9837",
    "P529_T1_Loop1Execution": "da66766413255a0f",
    "P529_T1_Loop2Execution": "e8eeb6e19acff10b",
    "P529_T1_NormalizationBounds": "deb4bf7513f18e80",
    "P529_T1_RoundArbitraryEndpoint": "c9f717a9d35879a8",
    "P529_T1_SeparationPackage": "b45bf4832efd5912",
    "ParserExponentConstants": "e2a30a7b0c5e9c91",
    "num_imp_dec2flt_common_BiasedFp_zero_pow2": "0f3e5cc6a65d812e",
    "num_imp_dec2flt_slow_parse_long_mantissa": "0eb6fb2627d0ff5c",
    "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": {
    "DecimalGoalVocabulary": "d8d2f621d2f6aa27",
    "DecimalGoalVocabularyDefault": "71a18a6c119a3cdc",
    "F64": "ec4e25fbaf4e11d4",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64": "94095c9bf79da164",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_BITS": "d9fd91641cb78c5e",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SIG_TOTAL_BITS": "887b52d84a3fc9c4",
    "P9_T1_ActualDispatchOperations": "d8bf51ebbbf153ac",
    "P9_T1_DispatchOperations": "9a657a317bca572b",
    "num_imp_dec2flt_common_BiasedFp": "63baac539d218a81",
    "num_imp_dec2flt_common_BiasedFp_zero_pow2": "0f99ae12c6707137",
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "b0286c42454b158d",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift": "fd298e37d5ae4f86",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "1aaf1bb11c0b6a49",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_round": "8ea3c058fea09978",
    "num_imp_dec2flt_decimal_seq_parse_decimal_seq": "f7442f2664cfd5f2",
    "num_imp_dec2flt_slow_parse_long_mantissa": "f4b1825aa46c08b9",
    "num_imp_traits_Float": "426bf608517697dc"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:cc8aa4c826df66e85d7530a87bed05135ee4b7fdc84ae7f07bfef775e479e6da",
  "current_fingerprint": "sound-v2:cc8aa4c826df66e85d7530a87bed05135ee4b7fdc84ae7f07bfef775e479e6da",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}