P529_T1_Loop2Execution

Lean checked

Statement

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

Lemma · Checked decrement and constructed minimum-exponent adjustment

Let d_1 have point zero, decimal retained cells, count at most 768, and exact-length retained word headed by a digit in [5,9]. Suppose e_1\ge-1079 and
X/2^{e_1}=V(d_1)+\eta_1,\quad Y/2^{e_1}=V(d_1)+u_1,\quad 0\le\eta_1<u_1,\quad d_1.\mathit{truncated}\iff\eta_1>0 .
The checked decrement returns e_s=e_1-1\ge-1080, and the actual specialized loop-two call returns normally to (d_2,e_2) with
e_2=\max(e_s,-1022),\qquad k=e_2-e_s\in[0,58].
If e_s\ge-1022, then k=0,d_2=d_1,e_2=e_s. Otherwise 1\le k\le58, the actual right shift of d_1 by k returns d_2, and e_2=-1022. The output has -18\le p_2\le0, positive exact-length decimal shape and count at most 768; its last retained cell is nonzero whenever k>0. Its endpoint equations are
X/2^{e_2+1}=V(d_2)+\eta_2,\quad Y/2^{e_2+1}=V(d_2)+u_2,\quad 0\le\eta_2<u_2,\quad d_2.\mathit{truncated}\iff\eta_2>0 .
The Lean conclusion includes all normal calls and the exhaustive alternatives.

Lean statement

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

import Tablet.ParserExponentConstants
import Tablet.P472_T1_DecimalRightShiftLoop0Execution
import Tablet.P495_T1_RightPrefixLengthBounds
import Tablet.P495_T1_RightShiftCanonical
import Tablet.P529_T1_NormalizationBounds
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P529_T1_Loop2Execution]
theorem P529_T1_Loop2Execution (X Y : Real)
    (d1 : num.imp.dec2flt.decimal_seq.DecimalSeq) (exp1 : Std.I32)
    (hexp : -1079 <= exp1.val)
    (hpoint : d1.decimal_point.val = 0)
    (hinput : d1.num_digits.val <= 768 /\
      (forall b, b ∈ d1.digits.val.take d1.num_digits.val -> b.val < 10) /\
      (exists h tail,
        (d1.digits.val.take d1.num_digits.val).map (fun b => b.val) = h :: tail /\
        5 <= h /\ h < 10 /\ (h :: tail).length = d1.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))
    (exists eta1 u1 : Real,
      X / (2 : Real) ^ exp1.val = V d1 + eta1 /\
      Y / (2 : Real) ^ exp1.val = V d1 + u1 /\
      0 <= eta1 /\ eta1 < u1 /\ (d1.truncated = true <-> 0 < eta1)) ->
    exists (exps exp2 : Std.I32) (d2 : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift : Std.Usize),
      exp1 - 1#i32 = ok exps /\ exps.val = exp1.val - 1 /\
      -1080 <= exps.val /\
      num.imp.dec2flt.slow.parse_long_mantissa_loop2
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 exps = ok (d2, exp2) /\
      exp2.val = max exps.val (-1022) /\
      (shift.val : Int) = exp2.val - exps.val /\ shift.val <= 58 /\
      ((-1022 <= exps.val /\ shift.val = 0 /\ d2 = d1 /\ exp2 = exps) \/
        (exps.val < -1022 /\ 1 <= shift.val /\
          num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d1 shift = ok d2 /\
          exp2.val = -1022)) /\
      -18 <= d2.decimal_point.val /\ d2.decimal_point.val <= 0 /\
      d2.num_digits.val <= 768 /\
      (forall b, b ∈ d2.digits.val.take d2.num_digits.val -> b.val < 10) /\
      (exists h tail,
        (d2.digits.val.take d2.num_digits.val).map (fun b => b.val) = h :: tail /\
        1 <= h /\ h < 10 /\ (h :: tail).length = d2.num_digits.val) /\
      (shift.val = 0 \/ d2.digits.val[d2.num_digits.val - 1]? ≠ some 0#u8) /\
      (exists eta2 u2 : Real,
        X / (2 : Real) ^ (exp2.val + 1) = V d2 + eta2 /\
        Y / (2 : Real) ^ (exp2.val + 1) = V d2 + u2 /\
        0 <= eta2 /\ eta2 < u2 /\ (d2.truncated = true <-> 0 < eta2)) := 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 checked subtraction of one is safe since e_1-1\ge-1080>-2^{31} and e_1-1\le2^{31}-2 by the intrinsic I32 upper bound. Its exact value is e_s=e_1-1. By ParserExponentConstants the minimum binary64 exponent is -1022. Unfold loop two in num imp dec2flt slow parse long mantissa.
If e_s\ge-1022, the first guard is false and the body returns done unchanged. Take k=0. The point and shape facts are inherited and the terminal-cell disjunction holds by its zero alternative. Since e_s+1=e_1, both endpoint equations are inherited exactly.
If e_s<-1022, put k=-1022-e_s. Then 1\le k\le58. The checked gap subtraction lies in I32 bounds, its Usize cast is exact, and its comparison to the cap 60 of num imp dec2flt slow parse long mantissa MAX SHIFT is false. The Usize cast bound uses the width in Assumptions. DecimalRightShiftLoop0Execution applies with input point zero and shift k\le58\le60; the head is positive and all count, digit and length hypotheses are given. It constructs the actual normal right shift and an actual prefix return with read cursor r\ge1 and accumulator c, satisfying
2^k\le c<10\,2^k,\qquad p_2=1-r,\qquad d_2.n\ne0 .
Apply RightPrefixLengthBounds to THAT prefix call: 10^{r-1}\le c<10^r. Since 2^{58}<10^{18}, we have c<10^{19}. Thus r\le19 (otherwise 10^{r-1}\ge10^{19}); r\ge1 gives -18\le1-r\le0. In particular no reset can occur: the prospective point is greater than -2047, and the execution certificate's output count is nonzero.
The Usize-to-I32 cast of k\le58 is exact. The checked addition has value e_s+k=-1022, within I32 bounds. Thus the first body returns the normal continuation (d_2,-1022), and the next body has false guard and returns done unchanged. The defining loop equation therefore constructs the stated normal loop return after this one shift, with e_2=\max(e_s,-1022).
Apply RightShiftCanonical to the same right call and its nonzero output, with endpoints X/2^{e_1},Y/2^{e_1}. Its input shape and ordered-error hypotheses are precisely the stated ones. It supplies positive decimal shape, count, terminal trimming, and the two endpoints divided by 2^k, with exact sticky equivalence. Since e_2+1=e_1+k, these are exactly the displayed output endpoints. Both branches construct their operations independently of any enclosing slow or public success premise. The sharp exponent input supplied for the regular slow path by NormalizationBounds is the history-specific bound, not a bound on an arbitrary upper endpoint.

Lean proof


  exact (set_option maxHeartbeats 1600000 in by
    classical
    dsimp only
    intro hend
    rcases hend with ⟨eta1, u1, hX1, hY1, heta10, heta1u, hsticky1⟩
    rcases hinput with ⟨hcap1, hdigit1, hshape1⟩
    rcases hshape1 with ⟨head1, tail1, hmap1, hheadlo1, hheadhi1, hlen1⟩
    have hexphi : exp1.val ≤ IScalar.max .I32 := by
      change exp1.bv.toInt ≤ IScalar.max .I32
      rw [IScalar.max_I32_eq]
      exact exp1.bv.toInt_le
    have hexpsublo : IScalar.min .I32 ≤ exp1.val - 1 := by
      rw [IScalar.min_I32_eq]
      change -(2 ^ 31 : Int) ≤ exp1.val - 1
      omega
    have hexpsubhi : exp1.val - 1 ≤ IScalar.max .I32 := by
      exact le_trans (by omega : exp1.val - 1 ≤ exp1.val) hexphi
    obtain ⟨exps, hsub, hexpsval⟩ := Aeneas.Std.WP.spec_imp_exists
      (IScalar.sub_spec (x := exp1) (y := (1#i32)) hexpsublo hexpsubhi)
    have hexpsval' : exps.val = exp1.val - 1 := by
      simpa using hexpsval
    have hexpslo : -1080 ≤ exps.val := by omega
    rcases ParserExponentConstants with
      ⟨_, _, _, _, _, _, _, hmin, _, _, _, _⟩
    -- The normalized shift is determined entirely by the checked exponent.
    by_cases hnormal : -1022 ≤ exps.val
    · refine ⟨exps, exps, d1, 0#usize, hsub, hexpsval', hexpslo, ?_⟩
      have hbody :
          num.imp.dec2flt.slow.parse_long_mantissa_loop2.body
            F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 exps =
          ok (done (d1, exps)) := by
        unfold num.imp.dec2flt.slow.parse_long_mantissa_loop2.body
        rw [hmin]
        simp only [bind_tc_ok]
        by_cases hgt : (-1022)#i32 > exps
        · change -1022 > exps.val at hgt
          omega
        · simp [hgt]
      have hloop :
          num.imp.dec2flt.slow.parse_long_mantissa_loop2
            F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 exps =
          ok (d1, exps) := by
        unfold num.imp.dec2flt.slow.parse_long_mantissa_loop2
        rw [Aeneas.Std.loop.eq_1]
        simp only [hbody]
      refine ⟨hloop, ?_⟩
      constructor
      · change exps.val = max exps.val (-1022)
        exact (max_eq_left hnormal).symm
      · constructor
        · norm_num
        · constructor
          · norm_num
          · constructor
            · exact Or.inl ⟨hnormal, by simp, rfl, rfl⟩
            · constructor
              · change -18 ≤ d1.decimal_point.val
                rw [hpoint]
                norm_num
              · constructor
                · change d1.decimal_point.val ≤ 0
                  rw [hpoint]
                · constructor
                  · exact hcap1
                  · constructor
                    · exact hdigit1
                    · constructor
                      · exact ⟨head1, tail1, by simpa [List.map_take] using hmap1,
                          by omega, hheadhi1, by simpa using hlen1⟩
                      · constructor
                        · simp
                        · refine ⟨eta1, u1, ?_, ?_, heta10, heta1u, hsticky1⟩
                          · have hexpEq : exps.val + 1 = exp1.val := by omega
                            rw [hexpEq]
                            exact hX1
                          · have hexpEq : exps.val + 1 = exp1.val := by omega
                            rw [hexpEq]
                            exact hY1
    · have hgap : 1 ≤ -1022 - exps.val ∧ -1022 - exps.val ≤ 58 := by omega
      have hgapLo : IScalar.min .I32 ≤ ((-1022)#i32).val - exps.val := by
        rw [IScalar.min_I32_eq]
        norm_num [IScalarTy.numBits, I32.numBits]
        omega
      have hgapHi : ((-1022)#i32).val - exps.val ≤ IScalar.max .I32 := by
        rw [IScalar.max_I32_eq]
        norm_num [IScalarTy.numBits, I32.numBits]
        omega
      obtain ⟨gapI, hgapI, hgapIval⟩ := Aeneas.Std.WP.spec_imp_exists
        (IScalar.sub_spec (x := (-1022)#i32) (y := exps) hgapLo hgapHi)
      have hgapIval' : gapI.val = -1022 - exps.val := by
        simpa using hgapIval
      have hmaxSize : (58 : Int) ≤ UScalar.max .Usize := by
        norm_num [UScalar.max, UScalarTy.numBits,
          Dec2Flt.P340_T1_selected_target_numBits]
      have hgapRange : 0 ≤ gapI.val ∧ gapI.val ≤ UScalar.max .Usize := by
        rw [hgapIval']
        constructor
        · omega
        · exact le_trans (by omega) hmaxSize
      obtain ⟨shift, hcastShift, hshiftVal⟩ := Aeneas.Std.WP.spec_imp_exists
        (IScalar.hcast_inBounds_spec .Usize gapI hgapRange)
      have hshiftVal' : shift.val = gapI.val := by
        simpa [WP.spec_ok] using hshiftVal
      have hshiftInt : (shift.val : Int) = -1022 - exps.val := by
        simpa [hgapIval'] using hshiftVal
      have hshiftBound : shift.val ≤ 58 := by
        have h : (shift.val : Int) ≤ 58 := by omega
        exact_mod_cast h
      have hshiftPos : 1 ≤ shift.val := by
        have h : (1 : Int) ≤ shift.val := by omega
        exact_mod_cast h
      have hshiftCert := P472_T1_DecimalRightShiftLoop0Execution d1 shift
        (by omega) (by rw [hpoint]) (by rw [hpoint]; norm_num)
        ⟨hcap1, hdigit1,
          ⟨head1, tail1, hmap1, by omega, by omega, hlen1⟩⟩
      rcases hshiftCert with
        ⟨read, acc, d2, hprefixCall, hreadLo, hreadHi, haccLo, haccHi,
          hprosLo, hprosHi, hrightCall, hnonzero⟩
      have hprefixLen := P495_T1_RightPrefixLengthBounds d1 d1 shift read acc
        false (by omega) hcap1 hdigit1 head1 tail1 hmap1 (by omega) hlen1
        hprefixCall
      have hpowLe : 2 ^ shift.val ≤ 2 ^ 58 :=
        Nat.pow_le_pow_right (by omega) hshiftBound
      have hacc19 : acc.val < 10 ^ 19 := by
        calc
          acc.val < 10 * 2 ^ shift.val := haccHi
          _ ≤ 10 * 2 ^ 58 := Nat.mul_le_mul_left 10 hpowLe
          _ < 10 ^ 19 := by norm_num
      have hreadBound : read.val ≤ 19 := by
        by_contra hn
        have hr : 20 ≤ read.val := by omega
        have hpowlower : 10 ^ 19 ≤ 10 ^ (read.val - 1) :=
          Nat.pow_le_pow_right (by norm_num) (by omega)
        have : 10 ^ 19 ≤ acc.val := le_trans hpowlower hprefixLen.1
        omega
      have hpointOut : -18 ≤ d2.decimal_point.val ∧
          d2.decimal_point.val ≤ 0 := by
        have hpEq : d2.decimal_point.val = 1 - read.val := by
          have := hprosLo
          omega
        rw [hpEq]
        omega
      have hsource : ∃ h tail,
            (d1.digits.val.take d1.num_digits.val).map (fun b => b.val) = h :: tail ∧
            1 ≤ h ∧ h < 10 ∧ (h :: tail).length = d1.num_digits.val := by
        exact ⟨head1, tail1, hmap1, by omega, by omega, hlen1⟩
      have hnumpos : d2.num_digits.val ≠ 0 := by simpa using hnonzero.2
      have hcanonical := P495_T1_RightShiftCanonical (X / (2 : Real) ^ exp1.val)
        (Y / (2 : Real) ^ exp1.val) d1 d2 shift (by omega) hcap1 hdigit1
        hsource hrightCall hnumpos
        ⟨eta1, u1, hX1, hY1, heta10, heta1u, hsticky1⟩
      rcases hcanonical with
        ⟨hpointLo2, hcount2, hdigits2, hshape2, hterminal2,
          ⟨eta2, u2, hX2raw, hY2raw, heta20, heta2u, hsticky2⟩⟩
      let Value := 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))
      have hX2 : X / (2 : Real) ^ exp1.val / (2 : Real) ^ shift.val =
          Value d2 + eta2 := by simpa [Value] using hX2raw
      have hY2 : Y / (2 : Real) ^ exp1.val / (2 : Real) ^ shift.val =
          Value d2 + u2 := by simpa [Value] using hY2raw
      let shiftI : Std.I32 := UScalar.hcast .I32 shift
      have hshiftIVal : shiftI.val = (shift.val : Int) := by
        have hspec := UScalar.hcast_inBounds_spec .I32 shift (by
          rw [IScalar.max_I32_eq]
          norm_num [IScalarTy.numBits, I32.numBits]
          omega)
        simpa [shiftI, WP.spec_ok] using hspec
      have hcastLift : lift (UScalar.hcast .I32 shift) = ok shiftI := by
        obtain ⟨v, hv, hvval⟩ := Aeneas.Std.WP.spec_imp_exists
          (UScalar.hcast_inBounds_spec .I32 shift (by
            rw [IScalar.max_I32_eq]
            norm_num [IScalarTy.numBits, I32.numBits]
            omega))
        rw [hv]
        congr 1
        apply IScalar.eq_of_val_eq
        change v.val = shiftI.val
        have hvval' : v.val = (shift.val : Int) := by
          simpa [WP.spec_ok] using hvval
        rw [hshiftIVal]
        exact hvval'
      have haddLo : IScalar.min .I32 ≤ exps.val + shiftI.val := by
        rw [IScalar.min_I32_eq, hshiftIVal, hshiftInt]
        norm_num [IScalarTy.numBits, I32.numBits]
      have haddHi : exps.val + shiftI.val ≤ IScalar.max .I32 := by
        rw [IScalar.max_I32_eq, hshiftIVal, hshiftInt]
        norm_num [IScalarTy.numBits, I32.numBits]
      obtain ⟨exp2, hadd, haddVal⟩ := Aeneas.Std.WP.spec_imp_exists
        (IScalar.add_spec haddLo haddHi)
      have hexp2Val : exp2.val = -1022 := by
        rw [haddVal, hshiftIVal, hshiftInt]
        omega
      have hmaxShift : num.imp.dec2flt.slow.parse_long_mantissa.MAX_SHIFT = 60#usize := by
        rw [num.imp.dec2flt.slow.parse_long_mantissa.MAX_SHIFT.eq_def]
      have hshiftBelowCap : ¬ shift > num.imp.dec2flt.slow.parse_long_mantissa.MAX_SHIFT := by
        rw [hmaxShift]
        change ¬ shift.val > 60
        omega
      have hbodyShift :
          num.imp.dec2flt.slow.parse_long_mantissa_loop2.body
            F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 exps =
          ok (cont (d2, exp2)) := by
        unfold num.imp.dec2flt.slow.parse_long_mantissa_loop2.body
        rw [hmin]
        simp only [bind_tc_ok]
        have hgt : (-1022)#i32 > exps := by
          change -1022 > exps.val
          omega
        simp only [hgt, if_true, hgapI, bind_tc_ok, hcastShift,
          hshiftBelowCap, if_false, hrightCall, bind_tc_ok, hcastLift,
          hadd, bind_tc_ok]
      have hbodyDone :
          num.imp.dec2flt.slow.parse_long_mantissa_loop2.body
            F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d2 exp2 =
          ok (done (d2, exp2)) := by
        unfold num.imp.dec2flt.slow.parse_long_mantissa_loop2.body
        rw [hmin]
        simp only [bind_tc_ok]
        have hgt : ¬ ((-1022)#i32 > exp2) := by
          change ¬ (-1022 > exp2.val)
          rw [hexp2Val]
          omega
        simp [hgt]
      have hloop :
          num.imp.dec2flt.slow.parse_long_mantissa_loop2
            F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 exps =
          ok (d2, exp2) := by
        unfold num.imp.dec2flt.slow.parse_long_mantissa_loop2
        rw [Aeneas.Std.loop.eq_1]
        simp only [hbodyShift]
        rw [Aeneas.Std.loop.eq_1]
        simp only [hbodyDone]
      refine ⟨exps, exp2, d2, shift, hsub, hexpsval', hexpslo,
        hloop, ?_⟩
      constructor
      · change exp2.val = max exps.val (-1022)
        rw [hexp2Val]
        have hcut : exps.val < -1022 := by omega
        simp [max_eq_right (le_of_lt hcut)]
      · constructor
        · rw [hexp2Val, hshiftInt]
        · constructor
          · exact hshiftBound
          · constructor
            · refine Or.inr ⟨by omega, hshiftPos, hrightCall, hexp2Val⟩
            · constructor
              · exact hpointOut.1
              · constructor
                · exact hpointOut.2
                · constructor
                  · exact hcount2
                  · constructor
                    · exact hdigits2
                    · constructor
                      · exact hshape2
                      · constructor
                        · exact Or.inr hterminal2
                        · refine ⟨eta2, u2, ?_, ?_, heta20, heta2u, hsticky2⟩
                          · have hexpShift : exp1.val + shift.val = exp2.val + 1 := by
                              rw [hexp2Val, hshiftInt]
                              omega
                            have hdenom : (2 : Real) ^ exp1.val *
                                (2 : Real) ^ shift.val =
                                (2 : Real) ^ (exp2.val + 1) := by
                              rw [← zpow_natCast (2 : Real) shift.val]
                              rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
                              congr 1
                            calc
                              X / (2 : Real) ^ (exp2.val + 1) =
                                  X / ((2 : Real) ^ exp1.val *
                                    (2 : Real) ^ shift.val) := by rw [hdenom]
                              _ = (X / (2 : Real) ^ exp1.val) /
                                    (2 : Real) ^ shift.val := by rw [div_div]
                              _ = Value d2 + eta2 := hX2
                          · have hexpShift : exp1.val + shift.val = exp2.val + 1 := by
                              rw [hexp2Val, hshiftInt]
                              omega
                            have hdenom : (2 : Real) ^ exp1.val *
                                (2 : Real) ^ shift.val =
                                (2 : Real) ^ (exp2.val + 1) := by
                              rw [← zpow_natCast (2 : Real) shift.val]
                              rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
                              congr 1
                            calc
                              Y / (2 : Real) ^ (exp2.val + 1) =
                                  Y / ((2 : Real) ^ exp1.val *
                                    (2 : Real) ^ shift.val) := by rw [hdenom]
                              _ = (Y / (2 : Real) ^ exp1.val) /
                                    (2 : Real) ^ shift.val := by rw [div_div]
                              _ = Value d2 + u2 := hY2)

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.ParserExponentConstants
import Tablet.P472_T1_DecimalRightShiftLoop0Execution
import Tablet.P495_T1_RightPrefixLengthBounds
import Tablet.P495_T1_RightShiftCanonical
import Tablet.P529_T1_NormalizationBounds
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P529_T1_Loop2Execution]
theorem P529_T1_Loop2Execution (X Y : Real)
    (d1 : num.imp.dec2flt.decimal_seq.DecimalSeq) (exp1 : Std.I32)
    (hexp : -1079 <= exp1.val)
    (hpoint : d1.decimal_point.val = 0)
    (hinput : d1.num_digits.val <= 768 /\
      (forall b, b ∈ d1.digits.val.take d1.num_digits.val -> b.val < 10) /\
      (exists h tail,
        (d1.digits.val.take d1.num_digits.val).map (fun b => b.val) = h :: tail /\
        5 <= h /\ h < 10 /\ (h :: tail).length = d1.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))
    (exists eta1 u1 : Real,
      X / (2 : Real) ^ exp1.val = V d1 + eta1 /\
      Y / (2 : Real) ^ exp1.val = V d1 + u1 /\
      0 <= eta1 /\ eta1 < u1 /\ (d1.truncated = true <-> 0 < eta1)) ->
    exists (exps exp2 : Std.I32) (d2 : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift : Std.Usize),
      exp1 - 1#i32 = ok exps /\ exps.val = exp1.val - 1 /\
      -1080 <= exps.val /\
      num.imp.dec2flt.slow.parse_long_mantissa_loop2
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 exps = ok (d2, exp2) /\
      exp2.val = max exps.val (-1022) /\
      (shift.val : Int) = exp2.val - exps.val /\ shift.val <= 58 /\
      ((-1022 <= exps.val /\ shift.val = 0 /\ d2 = d1 /\ exp2 = exps) \/
        (exps.val < -1022 /\ 1 <= shift.val /\
          num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d1 shift = ok d2 /\
          exp2.val = -1022)) /\
      -18 <= d2.decimal_point.val /\ d2.decimal_point.val <= 0 /\
      d2.num_digits.val <= 768 /\
      (forall b, b ∈ d2.digits.val.take d2.num_digits.val -> b.val < 10) /\
      (exists h tail,
        (d2.digits.val.take d2.num_digits.val).map (fun b => b.val) = h :: tail /\
        1 <= h /\ h < 10 /\ (h :: tail).length = d2.num_digits.val) /\
      (shift.val = 0 \/ d2.digits.val[d2.num_digits.val - 1]? ≠ some 0#u8) /\
      (exists eta2 u2 : Real,
        X / (2 : Real) ^ (exp2.val + 1) = V d2 + eta2 /\
        Y / (2 : Real) ^ (exp2.val + 1) = V d2 + u2 /\
        0 <= eta2 /\ eta2 < u2 /\ (d2.truncated = true <-> 0 < eta2)) := by
-- BODY
  exact (set_option maxHeartbeats 1600000 in by
    classical
    dsimp only
    intro hend
    rcases hend with ⟨eta1, u1, hX1, hY1, heta10, heta1u, hsticky1⟩
    rcases hinput with ⟨hcap1, hdigit1, hshape1⟩
    rcases hshape1 with ⟨head1, tail1, hmap1, hheadlo1, hheadhi1, hlen1⟩
    have hexphi : exp1.val ≤ IScalar.max .I32 := by
      change exp1.bv.toInt ≤ IScalar.max .I32
      rw [IScalar.max_I32_eq]
      exact exp1.bv.toInt_le
    have hexpsublo : IScalar.min .I32 ≤ exp1.val - 1 := by
      rw [IScalar.min_I32_eq]
      change -(2 ^ 31 : Int) ≤ exp1.val - 1
      omega
    have hexpsubhi : exp1.val - 1 ≤ IScalar.max .I32 := by
      exact le_trans (by omega : exp1.val - 1 ≤ exp1.val) hexphi
    obtain ⟨exps, hsub, hexpsval⟩ := Aeneas.Std.WP.spec_imp_exists
      (IScalar.sub_spec (x := exp1) (y := (1#i32)) hexpsublo hexpsubhi)
    have hexpsval' : exps.val = exp1.val - 1 := by
      simpa using hexpsval
    have hexpslo : -1080 ≤ exps.val := by omega
    rcases ParserExponentConstants with
      ⟨_, _, _, _, _, _, _, hmin, _, _, _, _⟩
    -- The normalized shift is determined entirely by the checked exponent.
    by_cases hnormal : -1022 ≤ exps.val
    · refine ⟨exps, exps, d1, 0#usize, hsub, hexpsval', hexpslo, ?_⟩
      have hbody :
          num.imp.dec2flt.slow.parse_long_mantissa_loop2.body
            F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 exps =
          ok (done (d1, exps)) := by
        unfold num.imp.dec2flt.slow.parse_long_mantissa_loop2.body
        rw [hmin]
        simp only [bind_tc_ok]
        by_cases hgt : (-1022)#i32 > exps
        · change -1022 > exps.val at hgt
          omega
        · simp [hgt]
      have hloop :
          num.imp.dec2flt.slow.parse_long_mantissa_loop2
            F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 exps =
          ok (d1, exps) := by
        unfold num.imp.dec2flt.slow.parse_long_mantissa_loop2
        rw [Aeneas.Std.loop.eq_1]
        simp only [hbody]
      refine ⟨hloop, ?_⟩
      constructor
      · change exps.val = max exps.val (-1022)
        exact (max_eq_left hnormal).symm
      · constructor
        · norm_num
        · constructor
          · norm_num
          · constructor
            · exact Or.inl ⟨hnormal, by simp, rfl, rfl⟩
            · constructor
              · change -18 ≤ d1.decimal_point.val
                rw [hpoint]
                norm_num
              · constructor
                · change d1.decimal_point.val ≤ 0
                  rw [hpoint]
                · constructor
                  · exact hcap1
                  · constructor
                    · exact hdigit1
                    · constructor
                      · exact ⟨head1, tail1, by simpa [List.map_take] using hmap1,
                          by omega, hheadhi1, by simpa using hlen1⟩
                      · constructor
                        · simp
                        · refine ⟨eta1, u1, ?_, ?_, heta10, heta1u, hsticky1⟩
                          · have hexpEq : exps.val + 1 = exp1.val := by omega
                            rw [hexpEq]
                            exact hX1
                          · have hexpEq : exps.val + 1 = exp1.val := by omega
                            rw [hexpEq]
                            exact hY1
    · have hgap : 1 ≤ -1022 - exps.val ∧ -1022 - exps.val ≤ 58 := by omega
      have hgapLo : IScalar.min .I32 ≤ ((-1022)#i32).val - exps.val := by
        rw [IScalar.min_I32_eq]
        norm_num [IScalarTy.numBits, I32.numBits]
        omega
      have hgapHi : ((-1022)#i32).val - exps.val ≤ IScalar.max .I32 := by
        rw [IScalar.max_I32_eq]
        norm_num [IScalarTy.numBits, I32.numBits]
        omega
      obtain ⟨gapI, hgapI, hgapIval⟩ := Aeneas.Std.WP.spec_imp_exists
        (IScalar.sub_spec (x := (-1022)#i32) (y := exps) hgapLo hgapHi)
      have hgapIval' : gapI.val = -1022 - exps.val := by
        simpa using hgapIval
      have hmaxSize : (58 : Int) ≤ UScalar.max .Usize := by
        norm_num [UScalar.max, UScalarTy.numBits,
          Dec2Flt.P340_T1_selected_target_numBits]
      have hgapRange : 0 ≤ gapI.val ∧ gapI.val ≤ UScalar.max .Usize := by
        rw [hgapIval']
        constructor
        · omega
        · exact le_trans (by omega) hmaxSize
      obtain ⟨shift, hcastShift, hshiftVal⟩ := Aeneas.Std.WP.spec_imp_exists
        (IScalar.hcast_inBounds_spec .Usize gapI hgapRange)
      have hshiftVal' : shift.val = gapI.val := by
        simpa [WP.spec_ok] using hshiftVal
      have hshiftInt : (shift.val : Int) = -1022 - exps.val := by
        simpa [hgapIval'] using hshiftVal
      have hshiftBound : shift.val ≤ 58 := by
        have h : (shift.val : Int) ≤ 58 := by omega
        exact_mod_cast h
      have hshiftPos : 1 ≤ shift.val := by
        have h : (1 : Int) ≤ shift.val := by omega
        exact_mod_cast h
      have hshiftCert := P472_T1_DecimalRightShiftLoop0Execution d1 shift
        (by omega) (by rw [hpoint]) (by rw [hpoint]; norm_num)
        ⟨hcap1, hdigit1,
          ⟨head1, tail1, hmap1, by omega, by omega, hlen1⟩⟩
      rcases hshiftCert with
        ⟨read, acc, d2, hprefixCall, hreadLo, hreadHi, haccLo, haccHi,
          hprosLo, hprosHi, hrightCall, hnonzero⟩
      have hprefixLen := P495_T1_RightPrefixLengthBounds d1 d1 shift read acc
        false (by omega) hcap1 hdigit1 head1 tail1 hmap1 (by omega) hlen1
        hprefixCall
      have hpowLe : 2 ^ shift.val ≤ 2 ^ 58 :=
        Nat.pow_le_pow_right (by omega) hshiftBound
      have hacc19 : acc.val < 10 ^ 19 := by
        calc
          acc.val < 10 * 2 ^ shift.val := haccHi
          _ ≤ 10 * 2 ^ 58 := Nat.mul_le_mul_left 10 hpowLe
          _ < 10 ^ 19 := by norm_num
      have hreadBound : read.val ≤ 19 := by
        by_contra hn
        have hr : 20 ≤ read.val := by omega
        have hpowlower : 10 ^ 19 ≤ 10 ^ (read.val - 1) :=
          Nat.pow_le_pow_right (by norm_num) (by omega)
        have : 10 ^ 19 ≤ acc.val := le_trans hpowlower hprefixLen.1
        omega
      have hpointOut : -18 ≤ d2.decimal_point.val ∧
          d2.decimal_point.val ≤ 0 := by
        have hpEq : d2.decimal_point.val = 1 - read.val := by
          have := hprosLo
          omega
        rw [hpEq]
        omega
      have hsource : ∃ h tail,
            (d1.digits.val.take d1.num_digits.val).map (fun b => b.val) = h :: tail ∧
            1 ≤ h ∧ h < 10 ∧ (h :: tail).length = d1.num_digits.val := by
        exact ⟨head1, tail1, hmap1, by omega, by omega, hlen1⟩
      have hnumpos : d2.num_digits.val ≠ 0 := by simpa using hnonzero.2
      have hcanonical := P495_T1_RightShiftCanonical (X / (2 : Real) ^ exp1.val)
        (Y / (2 : Real) ^ exp1.val) d1 d2 shift (by omega) hcap1 hdigit1
        hsource hrightCall hnumpos
        ⟨eta1, u1, hX1, hY1, heta10, heta1u, hsticky1⟩
      rcases hcanonical with
        ⟨hpointLo2, hcount2, hdigits2, hshape2, hterminal2,
          ⟨eta2, u2, hX2raw, hY2raw, heta20, heta2u, hsticky2⟩⟩
      let Value := 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))
      have hX2 : X / (2 : Real) ^ exp1.val / (2 : Real) ^ shift.val =
          Value d2 + eta2 := by simpa [Value] using hX2raw
      have hY2 : Y / (2 : Real) ^ exp1.val / (2 : Real) ^ shift.val =
          Value d2 + u2 := by simpa [Value] using hY2raw
      let shiftI : Std.I32 := UScalar.hcast .I32 shift
      have hshiftIVal : shiftI.val = (shift.val : Int) := by
        have hspec := UScalar.hcast_inBounds_spec .I32 shift (by
          rw [IScalar.max_I32_eq]
          norm_num [IScalarTy.numBits, I32.numBits]
          omega)
        simpa [shiftI, WP.spec_ok] using hspec
      have hcastLift : lift (UScalar.hcast .I32 shift) = ok shiftI := by
        obtain ⟨v, hv, hvval⟩ := Aeneas.Std.WP.spec_imp_exists
          (UScalar.hcast_inBounds_spec .I32 shift (by
            rw [IScalar.max_I32_eq]
            norm_num [IScalarTy.numBits, I32.numBits]
            omega))
        rw [hv]
        congr 1
        apply IScalar.eq_of_val_eq
        change v.val = shiftI.val
        have hvval' : v.val = (shift.val : Int) := by
          simpa [WP.spec_ok] using hvval
        rw [hshiftIVal]
        exact hvval'
      have haddLo : IScalar.min .I32 ≤ exps.val + shiftI.val := by
        rw [IScalar.min_I32_eq, hshiftIVal, hshiftInt]
        norm_num [IScalarTy.numBits, I32.numBits]
      have haddHi : exps.val + shiftI.val ≤ IScalar.max .I32 := by
        rw [IScalar.max_I32_eq, hshiftIVal, hshiftInt]
        norm_num [IScalarTy.numBits, I32.numBits]
      obtain ⟨exp2, hadd, haddVal⟩ := Aeneas.Std.WP.spec_imp_exists
        (IScalar.add_spec haddLo haddHi)
      have hexp2Val : exp2.val = -1022 := by
        rw [haddVal, hshiftIVal, hshiftInt]
        omega
      have hmaxShift : num.imp.dec2flt.slow.parse_long_mantissa.MAX_SHIFT = 60#usize := by
        rw [num.imp.dec2flt.slow.parse_long_mantissa.MAX_SHIFT.eq_def]
      have hshiftBelowCap : ¬ shift > num.imp.dec2flt.slow.parse_long_mantissa.MAX_SHIFT := by
        rw [hmaxShift]
        change ¬ shift.val > 60
        omega
      have hbodyShift :
          num.imp.dec2flt.slow.parse_long_mantissa_loop2.body
            F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 exps =
          ok (cont (d2, exp2)) := by
        unfold num.imp.dec2flt.slow.parse_long_mantissa_loop2.body
        rw [hmin]
        simp only [bind_tc_ok]
        have hgt : (-1022)#i32 > exps := by
          change -1022 > exps.val
          omega
        simp only [hgt, if_true, hgapI, bind_tc_ok, hcastShift,
          hshiftBelowCap, if_false, hrightCall, bind_tc_ok, hcastLift,
          hadd, bind_tc_ok]
      have hbodyDone :
          num.imp.dec2flt.slow.parse_long_mantissa_loop2.body
            F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d2 exp2 =
          ok (done (d2, exp2)) := by
        unfold num.imp.dec2flt.slow.parse_long_mantissa_loop2.body
        rw [hmin]
        simp only [bind_tc_ok]
        have hgt : ¬ ((-1022)#i32 > exp2) := by
          change ¬ (-1022 > exp2.val)
          rw [hexp2Val]
          omega
        simp [hgt]
      have hloop :
          num.imp.dec2flt.slow.parse_long_mantissa_loop2
            F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 exps =
          ok (d2, exp2) := by
        unfold num.imp.dec2flt.slow.parse_long_mantissa_loop2
        rw [Aeneas.Std.loop.eq_1]
        simp only [hbodyShift]
        rw [Aeneas.Std.loop.eq_1]
        simp only [hbodyDone]
      refine ⟨exps, exp2, d2, shift, hsub, hexpsval', hexpslo,
        hloop, ?_⟩
      constructor
      · change exp2.val = max exps.val (-1022)
        rw [hexp2Val]
        have hcut : exps.val < -1022 := by omega
        simp [max_eq_right (le_of_lt hcut)]
      · constructor
        · rw [hexp2Val, hshiftInt]
        · constructor
          · exact hshiftBound
          · constructor
            · refine Or.inr ⟨by omega, hshiftPos, hrightCall, hexp2Val⟩
            · constructor
              · exact hpointOut.1
              · constructor
                · exact hpointOut.2
                · constructor
                  · exact hcount2
                  · constructor
                    · exact hdigits2
                    · constructor
                      · exact hshape2
                      · constructor
                        · exact Or.inr hterminal2
                        · refine ⟨eta2, u2, ?_, ?_, heta20, heta2u, hsticky2⟩
                          · have hexpShift : exp1.val + shift.val = exp2.val + 1 := by
                              rw [hexp2Val, hshiftInt]
                              omega
                            have hdenom : (2 : Real) ^ exp1.val *
                                (2 : Real) ^ shift.val =
                                (2 : Real) ^ (exp2.val + 1) := by
                              rw [← zpow_natCast (2 : Real) shift.val]
                              rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
                              congr 1
                            calc
                              X / (2 : Real) ^ (exp2.val + 1) =
                                  X / ((2 : Real) ^ exp1.val *
                                    (2 : Real) ^ shift.val) := by rw [hdenom]
                              _ = (X / (2 : Real) ^ exp1.val) /
                                    (2 : Real) ^ shift.val := by rw [div_div]
                              _ = Value d2 + eta2 := hX2
                          · have hexpShift : exp1.val + shift.val = exp2.val + 1 := by
                              rw [hexp2Val, hshiftInt]
                              omega
                            have hdenom : (2 : Real) ^ exp1.val *
                                (2 : Real) ^ shift.val =
                                (2 : Real) ^ (exp2.val + 1) := by
                              rw [← zpow_natCast (2 : Real) shift.val]
                              rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
                              congr 1
                            calc
                              Y / (2 : Real) ^ (exp2.val + 1) =
                                  Y / ((2 : Real) ^ exp1.val *
                                    (2 : Real) ^ shift.val) := by rw [hdenom]
                              _ = (Y / (2 : Real) ^ exp1.val) /
                                    (2 : Real) ^ shift.val := by rw [div_div]
                              _ = Value d2 + u2 := hY2)

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (152)
More program dependencies (110)
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 124 entries
Technical checking records
Local closure summary
{
  "node": "P529_T1_Loop2Execution",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "7f26094c7fdf675e6cd3e688164eae23e49e9cebf1fe661e9c3cb44fa07a9a15",
  "active_statement_hash": "5ac429e052ad8259ccfb763fb950905fdc0cf56f3da2f5115a89a08cbee496f8",
  "kernel_axioms": [
    "Classical.choice",
    "Dec2Flt.P340_T1_selected_target_numBits",
    "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": {
    "P472_T1_DecimalRightShiftLoop0Execution": "e27252660de78d95",
    "P495_T1_RightPrefixLengthBounds": "1ef34b1c9650b938",
    "P495_T1_RightShiftCanonical": "fca19eef55f03341",
    "ParserExponentConstants": "e2a30a7b0c5e9c91",
    "SlowShiftSelectorCertificate": "78f0b22ceeb544a4",
    "num_imp_dec2flt_slow_parse_long_mantissa_MAX_SHIFT": "a858290d96a26cbd"
  },
  "strict_definition_deps": {
    "F64": "ec4e25fbaf4e11d4",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64": "94095c9bf79da164",
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "13e3cec939d02f51",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "1aaf1bb11c0b6a49",
    "num_imp_dec2flt_slow_parse_long_mantissa": "6301f3828274183b",
    "num_imp_dec2flt_slow_parse_long_mantissa_MAX_SHIFT": "32f403f33bee78a7",
    "num_imp_traits_Float": "996e90a556298539"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:ebcdbdc0f77a993e83aa7f9a2cfce812c3b9b36f84c2262e2e2d41d1c099bd71",
  "current_fingerprint": "sound-v2:ebcdbdc0f77a993e83aa7f9a2cfce812c3b9b36f84c2262e2e2d41d1c099bd71",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}