P804_T1_LeftLoopStep

Lean checked

Statement

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

Helper · Quantitative step of slow normalization loop one

Let the retained digits of d form a list of length d.\mathit{num\_digits}, all below ten, with positive leading digit, and with d.\mathit{num\_digits}\leq768. Write I for their base-ten fold and
V=I10^{p-d.\mathit{num\_digits}}, \qquad p=d.\mathit{decimal\_point}.
Suppose -324\leq p\leq0, e\geq-1260, and there are real numbers \eta,u such that
X=V+\eta,\qquad Y=V+u,\qquad 0\leq\eta<u,\qquad (d.\mathit{truncated}=\mathsf{true}\iff0<\eta). \tag{E}
One execution of the second extracted slow-loop body either returns the unchanged done state, with p=0 and leading digit at least five, or returns a continuation. In the continuation case there are a selected shift k\leq60, its extracted digit growth g, a returned record d', exponent e'=e-k, and leading digits z,z'\in[1,9] for the input and output retained words; the extracted growth and left-shift calls return g and d' exactly.
The output retained digits have length d'.\mathit{num\_digits}, are all below ten, and number at most 768. If I' is their base-ten fold and
V'=I'10^{p'-d'.\mathit{num\_digits}}, \qquad p'=d'.\mathit{decimal\_point},
then there are real numbers \eta',u' satisfying the corresponding continuation endpoint equations
2^kX=V'+\eta',\qquad 2^kY=V'+u',\qquad 0\leq\eta'<u',\qquad (d'.\mathit{truncated}=\mathsf{true}\iff0<\eta'). \tag{E'}
Quantitatively, p\le-19 implies p'=p+18 or p'=p+19, while -18\le p<0 implies p'\in\{-1,0\}. The correlated tail facts are: if p=p'=-1, then z'\ge7; if p=-1 and z\ge7, then p'=0 and z'\ge5; and if p=0, then p'=0, the input has z<5, an input z\le2 gives z'\ge3, and an input z\ge3 gives z'\ge5. These alternatives, the endpoint equations above, and all exact call equations are the conjunctions of the Lean statement.

Lean statement

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

import Tablet.DecimalLeftShiftLoop1Execution
import Tablet.DecimalSeqLeftShiftCanonical
import Tablet.P804_T1_LeftLeadingProgress
import Tablet.P804_T1_LeftShiftPointGrowth
import Tablet.P804_T1_PointMinusOneGrowth
import Tablet.P804_T1_PointZeroGrowth
import Tablet.P804_T1_SelectedLeftGrowth
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P804_T1_LeftLoopStep]
theorem P804_T1_LeftLoopStep
    (X Y : Real) (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (exp : Std.I32)
    (hpointLo : -324 <= d.decimal_point.val)
    (hpointHi : d.decimal_point.val <= 0)
    (hexpLo : -1260 <= exp.val)
    (hinput :
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun q => q.val)
      d.num_digits.val <= 768 /\
      (forall q, q ∈ d.digits.val.take d.num_digits.val -> q.val < 10) /\
      (exists z zs, digits = z :: zs /\ 1 <= z /\ z < 10 /\
        digits.length = d.num_digits.val))
    (hend :
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun q => q.val)
      let I : Nat := digits.foldl (fun n q => 10 * n + q) 0
      let V : Real := I * (10 : Real) ^
        (d.decimal_point.val - (d.num_digits.val : Int))
      exists eta u : Real, X = V + eta /\ Y = V + u /\
        0 <= eta /\ eta < u /\ (d.truncated = true <-> 0 < eta)) :
    (num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
        { m := 0#u64, p_biased := 2047#i32 }
        d.num_digits d.decimal_point d.truncated d.digits exp none =
          ok (done (d.num_digits, d.decimal_point, d.truncated,
            d.digits, exp, none)) /\
      d.decimal_point.val = 0 /\
      5 <= d.digits.val[0].val) \/
    exists (d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift growth : Std.Usize) (exp' : Std.I32) (z z' : Nat)
      (zs zs' : List Nat),
      num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
        { m := 0#u64, p_biased := 2047#i32 }
        d.num_digits d.decimal_point d.truncated d.digits exp none =
          ok (cont (d'.num_digits, d'.decimal_point, d'.truncated,
            d'.digits, exp', none)) /\
      num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d' /\
      num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
        d shift = ok growth /\
      shift.val <= 60 /\
      exp'.val = exp.val - (shift.val : Int) /\
      (d.digits.val.take d.num_digits.val).map (fun q => q.val) = z :: zs /\
      (d'.digits.val.take d'.num_digits.val).map (fun q => q.val) = z' :: zs' /\
      1 <= z /\ z < 10 /\ 1 <= z' /\ z' < 10 /\
      (d.decimal_point.val <= -19 ->
        d'.decimal_point.val = d.decimal_point.val + 18 \/
        d'.decimal_point.val = d.decimal_point.val + 19) /\
      (-18 <= d.decimal_point.val -> d.decimal_point.val < 0 ->
        d'.decimal_point.val = -1 \/ d'.decimal_point.val = 0) /\
      (d.decimal_point.val = -1 -> d'.decimal_point.val = -1 -> 7 <= z') /\
      (d.decimal_point.val = -1 -> 7 <= z ->
        d'.decimal_point.val = 0 /\ 5 <= z') /\
      (d.decimal_point.val = 0 -> z < 5) /\
      (d.decimal_point.val = 0 ->
        d'.decimal_point.val = 0 /\
        (z <= 2 -> 3 <= z') /\ (3 <= z -> 5 <= z')) /\
      (let digits' :=
        (d'.digits.val.take d'.num_digits.val).map (fun q => q.val)
       let I' : Nat := digits'.foldl (fun n q => 10 * n + q) 0
       let V' : Real := I' * (10 : Real) ^
         (d'.decimal_point.val - (d'.num_digits.val : Int))
       d'.num_digits.val <= 768 /\
       (forall q, q ∈ d'.digits.val.take d'.num_digits.val -> q.val < 10) /\
       digits'.length = d'.num_digits.val /\
       (exists eta' u' : Real,
         (2 : Real) ^ shift.val * X = V' + eta' /\
         (2 : Real) ^ shift.val * Y = V' + u' /\
         0 <= eta' /\ eta' < u' /\
         (d'.truncated = true <-> 0 < eta'))) := 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

Let the retained input word be z::z_s. Its stated length and the fixed array length make the read of cell zero legal, and the list equality identifies the read byte with z. If p=0 and z\ge5, unfold num imp dec2flt slow parse long mantissa: the loop-one body takes its immediate-exit branch, changes no field, and returns the asserted done state.
Assume that this exit test is false. If p<0, checked I32 negation gives -p\in[1,324], its Usize cast has that same natural value, and SlowShiftSelectorCertificate returns the selected k\le60 with the exact extracted closure-call equation. Applying SelectedLeftGrowth to n=-p supplies the exact growth call and gives
p\le-19\Rightarrow g\in\{18,19\},\qquad -18\le p<0\Rightarrow g\in\{-p-1,-p\}. \tag{1}
If instead p=0, the failed exit test gives z<5. The extracted selector chooses k=2 for z=1 and k=1 for z=2,3,4. PointZeroGrowth proves that the growth call returns g=0 in all four cases. Thus in every continuing branch the selector and growth calls appearing in the body have been constructed, with k\le60.
Apply DecimalLeftShiftLoop1Execution. It constructs the complete left-shift call, returns a nonempty positive-leading record d', and bounds its growth by nineteen. Then LeftShiftPointGrowth, using the same growth-call equation, identifies the point update exactly as
p'=p+g. \tag{2}
Equations (1)--(2) give the two large- and small-negative point alternatives. At point zero, g=0 gives p'=0.
It remains to correlate the possible tail states. On the p=-1 branch the literal selector chooses k=3. If it stalls at p'=-1, clause one of LeftLeadingProgress gives z'\ge7. Conversely, when the input already has z\ge7, PointMinusOneGrowth forces g=1; (2) gives p'=0, and clause four of LeftLeadingProgress gives z'\ge5. At point zero, the selector is (z,k)=(1,2) or has k=1. Clauses two and three of the same leading-progress lemma therefore give respectively z\le2\Rightarrow z'\ge3 and z\ge3\Rightarrow z'\ge5. This is the simultaneous three-state tail information in the statement, rather than independent worst-case estimates.
Apply DecimalSeqLeftShiftCanonical to the successful shift. Its nonzero alternative supplies the output capacity, decimal-cell, exact-length, positive-leading, sticky, and endpoint clauses, with endpoints 2^kX,2^kY. In particular it also gives p'\le19. The extracted reset constant is exactly 2047 by num imp dec2flt decimal seq DecimalSeq DECIMAL POINT RANGE; hence p'\le19 makes the post-shift reset comparison false.
Finally, the Usize-to-I32 cast of k is exact because k\le60. Before the update, e\ge-1260, so e-k\ge-1320, above the I32 minimum; its upper bound follows from the intrinsic I32 upper bound on e and k\ge0. Therefore the checked subtraction succeeds and returns the mathematical value e-k. Substitute the read, selector, growth, shift, reset-test, cast, and subtraction equations into the unfolded extracted body. This gives exactly the stated continuation and completes every output clause.

Lean proof


  dsimp only at hinput hend
  rcases hinput with ⟨hcap, hdigit, z, zs, hshape, hzLo, hzHi, hlen⟩
  have hnumPos : 0 < d.num_digits.val := by
    rw [← hlen]
    rw [hshape]
    simp
  have hnumNe : d.num_digits.val != 0 := by
    simp only [bne_iff_ne]
    omega
  have hlenCons : (z :: zs).length = d.num_digits.val := by
    rw [← hshape]
    exact hlen
  have hindexBound : (0 : Nat) < d.digits.val.length := by
    rw [d.digits.property]
    norm_num
  obtain ⟨head, hread, hreadVal⟩ := Aeneas.Std.WP.spec_imp_exists
    (Array.index_usize_spec d.digits 0#usize (by
      simpa [Array.length] using hindexBound))
  have hgetHead : d.digits.val[0]? = some head :=
    getElem?_eq_some_iff.mpr ⟨hindexBound, hreadVal.symm⟩
  have hheadVal : head.val = z := by
    have hh := congrArg List.head? hshape
    rw [List.head?_eq_getElem?] at hh
    have ht : (d.digits.val.take d.num_digits.val)[0]? = some head := by
      rw [List.getElem?_take, if_pos hnumPos]
      exact hgetHead
    simp only [List.getElem?_map, ht] at hh
    simpa using Option.some.inj hh
  have harrayHead : d.digits.val[0].val = z := by
    have hv : d.digits.val[0] = head := by
      exact getElem?_eq_some_iff.mp hgetHead |>.2
    rw [hv, hheadVal]
  let chooseShift (u : Std.U8) : Result Std.Usize :=
    match u with
    | 0#uscalar => ok 2#usize
    | 1#uscalar => ok 2#usize
    | _ => ok 1#usize
  have chooseShift_eq (u : Std.U8) :
      (match u with
        | 0#uscalar => ok 2#usize
        | 1#uscalar => ok 2#usize
        | _ => ok 1#usize) = chooseShift u := by
    rfl
  have shiftRule (u : Std.U8) (w : Nat)
      (hwLo : 1 <= w) (hwHi : w < 5) (hw : u.val = w) :
      chooseShift u = ok (if w = 1 then 2#usize else 1#usize) := by
    have hwCases : w = 1 \/ w = 2 \/ w = 3 \/ w = 4 := by omega
    rcases hwCases with rfl | rfl | rfl | rfl
    · have hu : u = 1#u8 := UScalar.eq_of_val_eq (by simpa using hw)
      subst u
      rfl
    · have hu : u = 2#u8 := UScalar.eq_of_val_eq (by simpa using hw)
      subst u
      rfl
    · have hu : u = 3#u8 := UScalar.eq_of_val_eq (by simpa using hw)
      subst u
      rfl
    · have hu : u = 4#u8 := UScalar.eq_of_val_eq (by simpa using hw)
      subst u
      rfl
  by_cases hexit : d.decimal_point.val = 0 /\ 5 <= z
  · left
    rcases hexit with ⟨hp0, hz5⟩
    refine ⟨?_, hp0, by simpa [harrayHead] using hz5⟩
    unfold num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
    have hple : d.decimal_point <= 0#i32 := by
      change d.decimal_point.val <= 0
      omega
    have hpeq : d.decimal_point = 0#i32 :=
      IScalar.eq_of_val_eq (by simpa using hp0)
    have hge : head >= 5#u8 := by
      change head.val >= 5
      omega
    have hnotlt : ¬ head.val < 5 := by omega
    simp [hpeq, hread, hnotlt]
  · right
    have hcontinue : d.decimal_point.val < 0 \/
        (d.decimal_point.val = 0 /\ z < 5) := by omega
    obtain ⟨shift, growth, hk, hgrowth, hcontrol, hlargeGrowth,
        hsmallGrowth, hzeroGrowth⟩ :
        exists (shift growth : Std.Usize),
          shift.val <= 60 /\
          num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
            d shift = ok growth /\
          ((d.decimal_point.val < 0 /\
              exists (negp : Std.I32) (n : Std.Usize),
                (-. d.decimal_point) = ok negp /\
                lift (IScalar.hcast .Usize negp) = ok n /\
                n.val = -d.decimal_point.val /\
                num.imp.dec2flt.slow.parse_long_mantissa.closure.Insts.CoreOpsFunctionFnTupleUsizeUsize.call
                  F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 () n = ok shift) \/
            (d.decimal_point.val = 0 /\ z < 5 /\
              shift = if z = 1 then 2#usize else 1#usize)) /\
          (d.decimal_point.val <= -19 -> growth.val = 18 \/ growth.val = 19) /\
          (-18 <= d.decimal_point.val -> d.decimal_point.val < 0 ->
            growth.val = (-d.decimal_point.val).toNat - 1 \/
            growth.val = (-d.decimal_point.val).toNat) /\
          (d.decimal_point.val = 0 -> growth.val = 0) := by
      rcases hcontinue with hpneg | ⟨hp0, hz5⟩
      · obtain ⟨negp, hneg, hnegVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.sub_spec (x := (0#i32 : Std.I32))
            (y := d.decimal_point) (by
              rw [IScalar.min_I32_eq]
              norm_num
              omega)
            (by
              rw [IScalar.max_I32_eq]
              norm_num
              omega))
        have hnegCall : (-. d.decimal_point) = ok negp := by
          change IScalar.neg d.decimal_point = ok negp
          unfold IScalar.neg
          change IScalar.sub (0#i32 : Std.I32) d.decimal_point = ok negp at hneg
          unfold IScalar.sub at hneg
          norm_num at hneg ⊢
          exact hneg
        have hnegVal' : negp.val = -d.decimal_point.val := by
          simpa using hnegVal
        obtain ⟨n, hnCall, hnVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.hcast_inBounds_spec .Usize negp (by
            rw [hnegVal']
            constructor <;> scalar_tac))
        have hnVal' : n.val = (-d.decimal_point.val).toNat := by
          have hnonneg : 0 <= -d.decimal_point.val := by omega
          have hnInt : (n.val : Int) = -d.decimal_point.val := by
            rw [hnegVal'] at hnVal
            simpa using hnVal
          have ht := congrArg Int.toNat hnInt
          simpa [Int.toNat_of_nonneg hnonneg] using ht
        obtain ⟨shift, growth, hselector, hk, hgrowth, hlarge, hsmall⟩ :=
          P804_T1_SelectedLeftGrowth d n (by rw [hnVal']; omega)
            (by rw [hnVal']; omega) ⟨hcap, hdigit, z, zs, hshape,
              hzLo, hzHi, hlen⟩
        refine ⟨shift, growth, hk, hgrowth, Or.inl ⟨hpneg, negp, n,
          hnegCall, hnCall, ?_, hselector⟩, ?_, ?_, ?_⟩
        · have hnInt : (n.val : Int) = -d.decimal_point.val := by
            rw [hnegVal'] at hnVal
            simpa using hnVal
          exact hnInt
        · intro hp19
          exact hlarge (by rw [hnVal']; omega)
        · intro hp18 hpneg'
          simpa [hnVal'] using hsmall (by rw [hnVal']; omega)
        · omega
      · obtain ⟨growth, hgrowth, hgrowth0⟩ :=
          P804_T1_PointZeroGrowth d z zs hcap hdigit hshape hzLo hz5
        let shift : Std.Usize := if z = 1 then 2#usize else 1#usize
        refine ⟨shift, growth, ?_, hgrowth, Or.inr ⟨hp0, hz5, rfl⟩,
          ?_, ?_, ?_⟩
        · by_cases hz1 : z = 1 <;> simp [shift, hz1]
        · omega
        · omega
        · exact fun _ => hgrowth0
    obtain ⟨d', q, hleft, hq, hqPoint, hp'LoWide, hp'HiWide, hnonzero⟩ :=
      DecimalLeftShiftLoop1Execution d shift hk hpointLo hpointHi
        ⟨hcap, hdigit, z, zs, hshape, hzLo, hzHi, hlenCons⟩
    have hpoint := P804_T1_LeftShiftPointGrowth d d' shift growth hnumNe
      hgrowth (by
        rcases hcontrol with ⟨hp, negp, n, hneg, hn, hnval, hsel⟩ |
          ⟨hp, hz5, hshift⟩
        · by_cases hn19 : 19 <= n.val
          · rcases hlargeGrowth (by omega) with h | h <;> omega
          · rcases hsmallGrowth (by omega) hp with h | h <;> omega
        · rw [hzeroGrowth hp]
          omega) hleft
    have hlargePoint : d.decimal_point.val <= -19 ->
        d'.decimal_point.val = d.decimal_point.val + 18 \/
        d'.decimal_point.val = d.decimal_point.val + 19 := by
      intro hp
      rcases hlargeGrowth hp with h | h <;> rw [hpoint, h] <;> omega
    have hsmallPoint : -18 <= d.decimal_point.val ->
        d.decimal_point.val < 0 ->
        d'.decimal_point.val = -1 \/ d'.decimal_point.val = 0 := by
      intro hp18 hpneg
      rcases hsmallGrowth hp18 hpneg with h | h
      · left
        rw [hpoint, h]
        have : ((-d.decimal_point.val).toNat : Int) =
            -d.decimal_point.val := Int.toNat_of_nonneg (by omega)
        omega
      · right
        rw [hpoint, h]
        have : ((-d.decimal_point.val).toNat : Int) =
            -d.decimal_point.val := Int.toNat_of_nonneg (by omega)
        omega
    have hzeroPoint : d.decimal_point.val = 0 ->
        d'.decimal_point.val = 0 := by
      intro hp0
      rw [hpoint, hzeroGrowth hp0]
      omega
    have hp'Hi : d'.decimal_point.val <= 0 := by
      by_cases hp19 : d.decimal_point.val <= -19
      · rcases hlargePoint hp19 with h | h <;> omega
      · by_cases hpneg : d.decimal_point.val < 0
        · rcases hsmallPoint (by omega) hpneg with h | h <;> omega
        · have hz := hzeroPoint (by omega)
          omega
    have hcanon := DecimalSeqLeftShiftCanonical d d' shift X Y
      ⟨hcap, hdigit, Or.inr ⟨z, zs, hshape, hzLo, hzHi, hlen⟩⟩
      hend hk hleft
    dsimp only at hcanon
    rcases hcanon with ⟨hcap', hdigit', hshapeOut, hend', q', hq', hpq'⟩
    rcases hshapeOut with hzero | hpositive
    · rcases hzero with ⟨hn0, hd0, ht0, hX0⟩
      simp [hn0] at hnonzero
    · rcases hpositive with
        ⟨z', zs', hshape', hz'Lo, hz'Hi, hlen', hlast'⟩
      have hlenCons' : (z' :: zs').length = d'.num_digits.val := by
        rw [← hshape']
        exact hlen'
      have hlead := P804_T1_LeftLeadingProgress d d' shift z z' zs zs'
        hk hshape hzLo hdigit hlenCons hshape' hz'Lo hdigit' hlenCons' hleft
      have hshiftMinusOne : d.decimal_point.val = -1 -> shift.val = 3 := by
        intro hp
        rcases hcontrol with ⟨hpneg, negp, n, hneg, hn, hnval, hsel⟩ |
            ⟨hp0, hz5, hshift⟩
        · have hn1 : n.val = 1 := by omega
          have hs := SlowShiftSelectorCertificate
            F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 n
          dsimp only at hs
          rcases hs with ⟨hsel', hk', hz0', hsmall', hlarge'⟩
          rw [hsel] at hsel'
          injection hsel' with heq
          have heq' : shift = 3#usize := by simpa [hn1] using heq
          simpa [heq']
        · omega
      have hstallLead : d.decimal_point.val = -1 ->
          d'.decimal_point.val = -1 -> 7 <= z' := by
        intro hp hp'
        exact hlead.1 hp hp' (hshiftMinusOne hp)
      have hminusRecovery : d.decimal_point.val = -1 -> 7 <= z ->
          d'.decimal_point.val = 0 /\ 5 <= z' := by
        intro hp hz7
        have hshift3 : shift = 3#usize := by
          apply UScalar.eq_of_val_eq
          simpa using hshiftMinusOne hp
        obtain ⟨growth1, hgrowth1, hgrowth1Val⟩ :=
          P804_T1_PointMinusOneGrowth d z zs hcap hdigit hshape hz7
        rw [hshift3] at hgrowth
        rw [hgrowth] at hgrowth1
        cases hgrowth1
        have hg1 : growth.val = 1 := hgrowth1Val
        have hp'0 : d'.decimal_point.val = 0 := by rw [hpoint, hp, hg1]; omega
        exact ⟨hp'0, hlead.2.2.2 hp hp'0 hz7 (by simpa [hshift3])⟩
      have hzeroProgress : d.decimal_point.val = 0 ->
          d'.decimal_point.val = 0 /\
          (z <= 2 -> 3 <= z') /\ (3 <= z -> 5 <= z') := by
        intro hp0
        have hp'0 := hzeroPoint hp0
        have hshiftCases : (z = 1 /\ shift.val = 2) \/
            (z = 2 /\ shift.val = 1) \/
            (3 <= z /\ shift.val = 1) := by
          rcases hcontrol with hneg | ⟨_, hz5, hshift⟩
          · omega
          · by_cases hz1 : z = 1
            · left
              exact ⟨hz1, by simp [hshift, hz1]⟩
            · by_cases hz2 : z = 2
              · right; left
                exact ⟨hz2, by simp [hshift, hz1]⟩
              · right; right
                exact ⟨by omega, by simp [hshift, hz1]⟩
        refine ⟨hp'0, ?_, ?_⟩
        · intro hz2
          rcases hshiftCases with h | h | h
          · exact hlead.2.1 hp0 hp'0 (Or.inl h)
          · exact hlead.2.1 hp0 hp'0 (Or.inr h)
          · omega
        · intro hz3
          rcases hshiftCases with h | h | h
          · omega
          · omega
          · exact hlead.2.2.1 hp0 hp'0 h.1 h.2
      have hshiftCast : (UScalar.hcast .I32 shift).val =
          (shift.val : Int) := by
        have hs := UScalar.hcast_inBounds_spec .I32 shift (by
          rw [IScalar.max_I32_eq]
          omega)
        simpa [lift, WP.spec_ok] using hs
      let shiftI : Std.I32 := UScalar.hcast .I32 shift
      have hshiftIVal : shiftI.val = (shift.val : Int) := by
        simpa [shiftI] using hshiftCast
      obtain ⟨exp', hexpSub, hexpVal⟩ := Aeneas.Std.WP.spec_imp_exists
        (IScalar.sub_spec (x := exp) (y := shiftI) (by
          rw [hshiftIVal, IScalar.min_I32_eq]
          omega) (by
          rw [hshiftIVal, IScalar.max_I32_eq]
          have := exp.bv.isLt
          scalar_tac))
      have hexpVal' : exp'.val = exp.val - (shift.val : Int) := by
        rw [hexpVal, hshiftIVal]
      have hnotReset : ¬ d'.decimal_point >
          num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE := by
        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
        change ¬ d'.decimal_point.val > 2047
        omega
      have hnotResetVal : ¬
          num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE.val <
            d'.decimal_point.val := by
        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
        change ¬ (2047 : Int) < d'.decimal_point.val
        omega
      have hzeroInput : d.decimal_point.val = 0 -> z < 5 := by
        intro hp0
        rcases hcontinue with hpneg | hzero
        · omega
        · exact hzero.2
      refine ⟨d', shift, growth, exp', z, z', zs, zs', ?_, hleft,
        hgrowth, hk, hexpVal', hshape, hshape', hzLo, hzHi, hz'Lo,
        hz'Hi, hlargePoint, hsmallPoint, hstallLead, hminusRecovery,
        hzeroInput, hzeroProgress, ?_⟩
      · unfold num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
        have hple : d.decimal_point <= 0#i32 := by
          change d.decimal_point.val <= 0
          omega
        simp only [hple, if_true, core.option.Option.is_none]
        rcases hcontrol with ⟨hpneg, negp, n, hneg, hn, hnval, hsel⟩ |
            ⟨hp0, hz5, hshift⟩
        · have hpne : ¬ d.decimal_point = 0#i32 := by
            intro h
            have := congrArg IScalar.val h
            simp at this
            omega
          simp only [hpne, if_false, hneg, bind_tc_ok,
            Bool.true_eq, if_true]
          rw [hn]
          simp only [bind_tc_ok]
          rw [hsel]
          simp only [bind_tc_ok]
          have hdEta :
              ({ num_digits := d.num_digits,
                 decimal_point := d.decimal_point,
                 truncated := d.truncated,
                 digits := d.digits } :
                num.imp.dec2flt.decimal_seq.DecimalSeq) = d := by
            cases d
            rfl
          rw [hdEta, hleft]
          simp only [bind_tc_ok, hnotReset, if_false, lift]
          rw [show UScalar.hcast .I32 shift = shiftI by rfl]
          simp only [hexpSub, bind_tc_ok]
          simp only [show (none : Option num.imp.dec2flt.common.BiasedFp).isNone =
            true by rfl, if_true]
        · have hpeq : d.decimal_point = 0#i32 :=
            IScalar.eq_of_val_eq (by simpa using hp0)
          have hlt : ¬ head >= 5#u8 := by
            change ¬ head.val >= 5
            omega
          have hshiftMatch := shiftRule head z hzLo hz5 hheadVal
          rw [← hshift] at hshiftMatch
          have hselector := (chooseShift_eq head).trans hshiftMatch
          simp only [hpeq, if_true, hread, hlt, if_false, bind_tc_ok]
          simp only [show (none : Option num.imp.dec2flt.common.BiasedFp).isNone =
            true by rfl, if_true]
          have hleadVal : d.digits.val[0].val = z := harrayHead
          split
          · change 0 = z at hheadVal
            have hz0 : z = 0 := hheadVal.symm
            omega
          · change 1 = z at hheadVal
            have hz1 : z = 1 := hheadVal.symm
            simp only [hz1, if_true] at hshift hleft
            subst shift
            have hdZeroEta :
                ({ num_digits := d.num_digits,
                   decimal_point := 0#i32,
                   truncated := d.truncated,
                   digits := d.digits } :
                num.imp.dec2flt.decimal_seq.DecimalSeq) = d := by
              rw [← hpeq]
            simp only [bind_tc_ok]
            rw [hdZeroEta, hleft]
            simp only [bind_tc_ok, hnotReset, if_false, lift]
            rw [show UScalar.hcast .I32 2#usize = shiftI by rfl]
            simp only [hexpSub, bind_tc_ok]
          · have hz1 : z ≠ 1 := by
              intro hz
              subst z
              have hu : head = 1#u8 := by
                apply UScalar.eq_of_val_eq
                scalar_tac
              subst head
              simp_all
            simp only [hz1, if_false] at hshift
            subst shift
            have hdZeroEta :
                ({ num_digits := d.num_digits,
                   decimal_point := 0#i32,
                   truncated := d.truncated,
                   digits := d.digits } :
                num.imp.dec2flt.decimal_seq.DecimalSeq) = d := by
              rw [← hpeq]
            simp only [bind_tc_ok]
            rw [hdZeroEta, hleft]
            simp only [bind_tc_ok, hnotReset, if_false, lift]
            rw [show UScalar.hcast .I32 1#usize = shiftI by rfl]
            simp only [hexpSub, bind_tc_ok]
      · exact ⟨hcap', hdigit', hlen', hend'⟩

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalLeftShiftLoop1Execution
import Tablet.DecimalSeqLeftShiftCanonical
import Tablet.P804_T1_LeftLeadingProgress
import Tablet.P804_T1_LeftShiftPointGrowth
import Tablet.P804_T1_PointMinusOneGrowth
import Tablet.P804_T1_PointZeroGrowth
import Tablet.P804_T1_SelectedLeftGrowth
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P804_T1_LeftLoopStep]
theorem P804_T1_LeftLoopStep
    (X Y : Real) (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (exp : Std.I32)
    (hpointLo : -324 <= d.decimal_point.val)
    (hpointHi : d.decimal_point.val <= 0)
    (hexpLo : -1260 <= exp.val)
    (hinput :
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun q => q.val)
      d.num_digits.val <= 768 /\
      (forall q, q ∈ d.digits.val.take d.num_digits.val -> q.val < 10) /\
      (exists z zs, digits = z :: zs /\ 1 <= z /\ z < 10 /\
        digits.length = d.num_digits.val))
    (hend :
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun q => q.val)
      let I : Nat := digits.foldl (fun n q => 10 * n + q) 0
      let V : Real := I * (10 : Real) ^
        (d.decimal_point.val - (d.num_digits.val : Int))
      exists eta u : Real, X = V + eta /\ Y = V + u /\
        0 <= eta /\ eta < u /\ (d.truncated = true <-> 0 < eta)) :
    (num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
        { m := 0#u64, p_biased := 2047#i32 }
        d.num_digits d.decimal_point d.truncated d.digits exp none =
          ok (done (d.num_digits, d.decimal_point, d.truncated,
            d.digits, exp, none)) /\
      d.decimal_point.val = 0 /\
      5 <= d.digits.val[0].val) \/
    exists (d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift growth : Std.Usize) (exp' : Std.I32) (z z' : Nat)
      (zs zs' : List Nat),
      num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
        { m := 0#u64, p_biased := 2047#i32 }
        d.num_digits d.decimal_point d.truncated d.digits exp none =
          ok (cont (d'.num_digits, d'.decimal_point, d'.truncated,
            d'.digits, exp', none)) /\
      num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d' /\
      num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
        d shift = ok growth /\
      shift.val <= 60 /\
      exp'.val = exp.val - (shift.val : Int) /\
      (d.digits.val.take d.num_digits.val).map (fun q => q.val) = z :: zs /\
      (d'.digits.val.take d'.num_digits.val).map (fun q => q.val) = z' :: zs' /\
      1 <= z /\ z < 10 /\ 1 <= z' /\ z' < 10 /\
      (d.decimal_point.val <= -19 ->
        d'.decimal_point.val = d.decimal_point.val + 18 \/
        d'.decimal_point.val = d.decimal_point.val + 19) /\
      (-18 <= d.decimal_point.val -> d.decimal_point.val < 0 ->
        d'.decimal_point.val = -1 \/ d'.decimal_point.val = 0) /\
      (d.decimal_point.val = -1 -> d'.decimal_point.val = -1 -> 7 <= z') /\
      (d.decimal_point.val = -1 -> 7 <= z ->
        d'.decimal_point.val = 0 /\ 5 <= z') /\
      (d.decimal_point.val = 0 -> z < 5) /\
      (d.decimal_point.val = 0 ->
        d'.decimal_point.val = 0 /\
        (z <= 2 -> 3 <= z') /\ (3 <= z -> 5 <= z')) /\
      (let digits' :=
        (d'.digits.val.take d'.num_digits.val).map (fun q => q.val)
       let I' : Nat := digits'.foldl (fun n q => 10 * n + q) 0
       let V' : Real := I' * (10 : Real) ^
         (d'.decimal_point.val - (d'.num_digits.val : Int))
       d'.num_digits.val <= 768 /\
       (forall q, q ∈ d'.digits.val.take d'.num_digits.val -> q.val < 10) /\
       digits'.length = d'.num_digits.val /\
       (exists eta' u' : Real,
         (2 : Real) ^ shift.val * X = V' + eta' /\
         (2 : Real) ^ shift.val * Y = V' + u' /\
         0 <= eta' /\ eta' < u' /\
         (d'.truncated = true <-> 0 < eta'))) := by
-- BODY
  dsimp only at hinput hend
  rcases hinput with ⟨hcap, hdigit, z, zs, hshape, hzLo, hzHi, hlen⟩
  have hnumPos : 0 < d.num_digits.val := by
    rw [← hlen]
    rw [hshape]
    simp
  have hnumNe : d.num_digits.val != 0 := by
    simp only [bne_iff_ne]
    omega
  have hlenCons : (z :: zs).length = d.num_digits.val := by
    rw [← hshape]
    exact hlen
  have hindexBound : (0 : Nat) < d.digits.val.length := by
    rw [d.digits.property]
    norm_num
  obtain ⟨head, hread, hreadVal⟩ := Aeneas.Std.WP.spec_imp_exists
    (Array.index_usize_spec d.digits 0#usize (by
      simpa [Array.length] using hindexBound))
  have hgetHead : d.digits.val[0]? = some head :=
    getElem?_eq_some_iff.mpr ⟨hindexBound, hreadVal.symm⟩
  have hheadVal : head.val = z := by
    have hh := congrArg List.head? hshape
    rw [List.head?_eq_getElem?] at hh
    have ht : (d.digits.val.take d.num_digits.val)[0]? = some head := by
      rw [List.getElem?_take, if_pos hnumPos]
      exact hgetHead
    simp only [List.getElem?_map, ht] at hh
    simpa using Option.some.inj hh
  have harrayHead : d.digits.val[0].val = z := by
    have hv : d.digits.val[0] = head := by
      exact getElem?_eq_some_iff.mp hgetHead |>.2
    rw [hv, hheadVal]
  let chooseShift (u : Std.U8) : Result Std.Usize :=
    match u with
    | 0#uscalar => ok 2#usize
    | 1#uscalar => ok 2#usize
    | _ => ok 1#usize
  have chooseShift_eq (u : Std.U8) :
      (match u with
        | 0#uscalar => ok 2#usize
        | 1#uscalar => ok 2#usize
        | _ => ok 1#usize) = chooseShift u := by
    rfl
  have shiftRule (u : Std.U8) (w : Nat)
      (hwLo : 1 <= w) (hwHi : w < 5) (hw : u.val = w) :
      chooseShift u = ok (if w = 1 then 2#usize else 1#usize) := by
    have hwCases : w = 1 \/ w = 2 \/ w = 3 \/ w = 4 := by omega
    rcases hwCases with rfl | rfl | rfl | rfl
    · have hu : u = 1#u8 := UScalar.eq_of_val_eq (by simpa using hw)
      subst u
      rfl
    · have hu : u = 2#u8 := UScalar.eq_of_val_eq (by simpa using hw)
      subst u
      rfl
    · have hu : u = 3#u8 := UScalar.eq_of_val_eq (by simpa using hw)
      subst u
      rfl
    · have hu : u = 4#u8 := UScalar.eq_of_val_eq (by simpa using hw)
      subst u
      rfl
  by_cases hexit : d.decimal_point.val = 0 /\ 5 <= z
  · left
    rcases hexit with ⟨hp0, hz5⟩
    refine ⟨?_, hp0, by simpa [harrayHead] using hz5⟩
    unfold num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
    have hple : d.decimal_point <= 0#i32 := by
      change d.decimal_point.val <= 0
      omega
    have hpeq : d.decimal_point = 0#i32 :=
      IScalar.eq_of_val_eq (by simpa using hp0)
    have hge : head >= 5#u8 := by
      change head.val >= 5
      omega
    have hnotlt : ¬ head.val < 5 := by omega
    simp [hpeq, hread, hnotlt]
  · right
    have hcontinue : d.decimal_point.val < 0 \/
        (d.decimal_point.val = 0 /\ z < 5) := by omega
    obtain ⟨shift, growth, hk, hgrowth, hcontrol, hlargeGrowth,
        hsmallGrowth, hzeroGrowth⟩ :
        exists (shift growth : Std.Usize),
          shift.val <= 60 /\
          num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
            d shift = ok growth /\
          ((d.decimal_point.val < 0 /\
              exists (negp : Std.I32) (n : Std.Usize),
                (-. d.decimal_point) = ok negp /\
                lift (IScalar.hcast .Usize negp) = ok n /\
                n.val = -d.decimal_point.val /\
                num.imp.dec2flt.slow.parse_long_mantissa.closure.Insts.CoreOpsFunctionFnTupleUsizeUsize.call
                  F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 () n = ok shift) \/
            (d.decimal_point.val = 0 /\ z < 5 /\
              shift = if z = 1 then 2#usize else 1#usize)) /\
          (d.decimal_point.val <= -19 -> growth.val = 18 \/ growth.val = 19) /\
          (-18 <= d.decimal_point.val -> d.decimal_point.val < 0 ->
            growth.val = (-d.decimal_point.val).toNat - 1 \/
            growth.val = (-d.decimal_point.val).toNat) /\
          (d.decimal_point.val = 0 -> growth.val = 0) := by
      rcases hcontinue with hpneg | ⟨hp0, hz5⟩
      · obtain ⟨negp, hneg, hnegVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.sub_spec (x := (0#i32 : Std.I32))
            (y := d.decimal_point) (by
              rw [IScalar.min_I32_eq]
              norm_num
              omega)
            (by
              rw [IScalar.max_I32_eq]
              norm_num
              omega))
        have hnegCall : (-. d.decimal_point) = ok negp := by
          change IScalar.neg d.decimal_point = ok negp
          unfold IScalar.neg
          change IScalar.sub (0#i32 : Std.I32) d.decimal_point = ok negp at hneg
          unfold IScalar.sub at hneg
          norm_num at hneg ⊢
          exact hneg
        have hnegVal' : negp.val = -d.decimal_point.val := by
          simpa using hnegVal
        obtain ⟨n, hnCall, hnVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.hcast_inBounds_spec .Usize negp (by
            rw [hnegVal']
            constructor <;> scalar_tac))
        have hnVal' : n.val = (-d.decimal_point.val).toNat := by
          have hnonneg : 0 <= -d.decimal_point.val := by omega
          have hnInt : (n.val : Int) = -d.decimal_point.val := by
            rw [hnegVal'] at hnVal
            simpa using hnVal
          have ht := congrArg Int.toNat hnInt
          simpa [Int.toNat_of_nonneg hnonneg] using ht
        obtain ⟨shift, growth, hselector, hk, hgrowth, hlarge, hsmall⟩ :=
          P804_T1_SelectedLeftGrowth d n (by rw [hnVal']; omega)
            (by rw [hnVal']; omega) ⟨hcap, hdigit, z, zs, hshape,
              hzLo, hzHi, hlen⟩
        refine ⟨shift, growth, hk, hgrowth, Or.inl ⟨hpneg, negp, n,
          hnegCall, hnCall, ?_, hselector⟩, ?_, ?_, ?_⟩
        · have hnInt : (n.val : Int) = -d.decimal_point.val := by
            rw [hnegVal'] at hnVal
            simpa using hnVal
          exact hnInt
        · intro hp19
          exact hlarge (by rw [hnVal']; omega)
        · intro hp18 hpneg'
          simpa [hnVal'] using hsmall (by rw [hnVal']; omega)
        · omega
      · obtain ⟨growth, hgrowth, hgrowth0⟩ :=
          P804_T1_PointZeroGrowth d z zs hcap hdigit hshape hzLo hz5
        let shift : Std.Usize := if z = 1 then 2#usize else 1#usize
        refine ⟨shift, growth, ?_, hgrowth, Or.inr ⟨hp0, hz5, rfl⟩,
          ?_, ?_, ?_⟩
        · by_cases hz1 : z = 1 <;> simp [shift, hz1]
        · omega
        · omega
        · exact fun _ => hgrowth0
    obtain ⟨d', q, hleft, hq, hqPoint, hp'LoWide, hp'HiWide, hnonzero⟩ :=
      DecimalLeftShiftLoop1Execution d shift hk hpointLo hpointHi
        ⟨hcap, hdigit, z, zs, hshape, hzLo, hzHi, hlenCons⟩
    have hpoint := P804_T1_LeftShiftPointGrowth d d' shift growth hnumNe
      hgrowth (by
        rcases hcontrol with ⟨hp, negp, n, hneg, hn, hnval, hsel⟩ |
          ⟨hp, hz5, hshift⟩
        · by_cases hn19 : 19 <= n.val
          · rcases hlargeGrowth (by omega) with h | h <;> omega
          · rcases hsmallGrowth (by omega) hp with h | h <;> omega
        · rw [hzeroGrowth hp]
          omega) hleft
    have hlargePoint : d.decimal_point.val <= -19 ->
        d'.decimal_point.val = d.decimal_point.val + 18 \/
        d'.decimal_point.val = d.decimal_point.val + 19 := by
      intro hp
      rcases hlargeGrowth hp with h | h <;> rw [hpoint, h] <;> omega
    have hsmallPoint : -18 <= d.decimal_point.val ->
        d.decimal_point.val < 0 ->
        d'.decimal_point.val = -1 \/ d'.decimal_point.val = 0 := by
      intro hp18 hpneg
      rcases hsmallGrowth hp18 hpneg with h | h
      · left
        rw [hpoint, h]
        have : ((-d.decimal_point.val).toNat : Int) =
            -d.decimal_point.val := Int.toNat_of_nonneg (by omega)
        omega
      · right
        rw [hpoint, h]
        have : ((-d.decimal_point.val).toNat : Int) =
            -d.decimal_point.val := Int.toNat_of_nonneg (by omega)
        omega
    have hzeroPoint : d.decimal_point.val = 0 ->
        d'.decimal_point.val = 0 := by
      intro hp0
      rw [hpoint, hzeroGrowth hp0]
      omega
    have hp'Hi : d'.decimal_point.val <= 0 := by
      by_cases hp19 : d.decimal_point.val <= -19
      · rcases hlargePoint hp19 with h | h <;> omega
      · by_cases hpneg : d.decimal_point.val < 0
        · rcases hsmallPoint (by omega) hpneg with h | h <;> omega
        · have hz := hzeroPoint (by omega)
          omega
    have hcanon := DecimalSeqLeftShiftCanonical d d' shift X Y
      ⟨hcap, hdigit, Or.inr ⟨z, zs, hshape, hzLo, hzHi, hlen⟩⟩
      hend hk hleft
    dsimp only at hcanon
    rcases hcanon with ⟨hcap', hdigit', hshapeOut, hend', q', hq', hpq'⟩
    rcases hshapeOut with hzero | hpositive
    · rcases hzero with ⟨hn0, hd0, ht0, hX0⟩
      simp [hn0] at hnonzero
    · rcases hpositive with
        ⟨z', zs', hshape', hz'Lo, hz'Hi, hlen', hlast'⟩
      have hlenCons' : (z' :: zs').length = d'.num_digits.val := by
        rw [← hshape']
        exact hlen'
      have hlead := P804_T1_LeftLeadingProgress d d' shift z z' zs zs'
        hk hshape hzLo hdigit hlenCons hshape' hz'Lo hdigit' hlenCons' hleft
      have hshiftMinusOne : d.decimal_point.val = -1 -> shift.val = 3 := by
        intro hp
        rcases hcontrol with ⟨hpneg, negp, n, hneg, hn, hnval, hsel⟩ |
            ⟨hp0, hz5, hshift⟩
        · have hn1 : n.val = 1 := by omega
          have hs := SlowShiftSelectorCertificate
            F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 n
          dsimp only at hs
          rcases hs with ⟨hsel', hk', hz0', hsmall', hlarge'⟩
          rw [hsel] at hsel'
          injection hsel' with heq
          have heq' : shift = 3#usize := by simpa [hn1] using heq
          simpa [heq']
        · omega
      have hstallLead : d.decimal_point.val = -1 ->
          d'.decimal_point.val = -1 -> 7 <= z' := by
        intro hp hp'
        exact hlead.1 hp hp' (hshiftMinusOne hp)
      have hminusRecovery : d.decimal_point.val = -1 -> 7 <= z ->
          d'.decimal_point.val = 0 /\ 5 <= z' := by
        intro hp hz7
        have hshift3 : shift = 3#usize := by
          apply UScalar.eq_of_val_eq
          simpa using hshiftMinusOne hp
        obtain ⟨growth1, hgrowth1, hgrowth1Val⟩ :=
          P804_T1_PointMinusOneGrowth d z zs hcap hdigit hshape hz7
        rw [hshift3] at hgrowth
        rw [hgrowth] at hgrowth1
        cases hgrowth1
        have hg1 : growth.val = 1 := hgrowth1Val
        have hp'0 : d'.decimal_point.val = 0 := by rw [hpoint, hp, hg1]; omega
        exact ⟨hp'0, hlead.2.2.2 hp hp'0 hz7 (by simpa [hshift3])⟩
      have hzeroProgress : d.decimal_point.val = 0 ->
          d'.decimal_point.val = 0 /\
          (z <= 2 -> 3 <= z') /\ (3 <= z -> 5 <= z') := by
        intro hp0
        have hp'0 := hzeroPoint hp0
        have hshiftCases : (z = 1 /\ shift.val = 2) \/
            (z = 2 /\ shift.val = 1) \/
            (3 <= z /\ shift.val = 1) := by
          rcases hcontrol with hneg | ⟨_, hz5, hshift⟩
          · omega
          · by_cases hz1 : z = 1
            · left
              exact ⟨hz1, by simp [hshift, hz1]⟩
            · by_cases hz2 : z = 2
              · right; left
                exact ⟨hz2, by simp [hshift, hz1]⟩
              · right; right
                exact ⟨by omega, by simp [hshift, hz1]⟩
        refine ⟨hp'0, ?_, ?_⟩
        · intro hz2
          rcases hshiftCases with h | h | h
          · exact hlead.2.1 hp0 hp'0 (Or.inl h)
          · exact hlead.2.1 hp0 hp'0 (Or.inr h)
          · omega
        · intro hz3
          rcases hshiftCases with h | h | h
          · omega
          · omega
          · exact hlead.2.2.1 hp0 hp'0 h.1 h.2
      have hshiftCast : (UScalar.hcast .I32 shift).val =
          (shift.val : Int) := by
        have hs := UScalar.hcast_inBounds_spec .I32 shift (by
          rw [IScalar.max_I32_eq]
          omega)
        simpa [lift, WP.spec_ok] using hs
      let shiftI : Std.I32 := UScalar.hcast .I32 shift
      have hshiftIVal : shiftI.val = (shift.val : Int) := by
        simpa [shiftI] using hshiftCast
      obtain ⟨exp', hexpSub, hexpVal⟩ := Aeneas.Std.WP.spec_imp_exists
        (IScalar.sub_spec (x := exp) (y := shiftI) (by
          rw [hshiftIVal, IScalar.min_I32_eq]
          omega) (by
          rw [hshiftIVal, IScalar.max_I32_eq]
          have := exp.bv.isLt
          scalar_tac))
      have hexpVal' : exp'.val = exp.val - (shift.val : Int) := by
        rw [hexpVal, hshiftIVal]
      have hnotReset : ¬ d'.decimal_point >
          num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE := by
        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
        change ¬ d'.decimal_point.val > 2047
        omega
      have hnotResetVal : ¬
          num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE.val <
            d'.decimal_point.val := by
        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
        change ¬ (2047 : Int) < d'.decimal_point.val
        omega
      have hzeroInput : d.decimal_point.val = 0 -> z < 5 := by
        intro hp0
        rcases hcontinue with hpneg | hzero
        · omega
        · exact hzero.2
      refine ⟨d', shift, growth, exp', z, z', zs, zs', ?_, hleft,
        hgrowth, hk, hexpVal', hshape, hshape', hzLo, hzHi, hz'Lo,
        hz'Hi, hlargePoint, hsmallPoint, hstallLead, hminusRecovery,
        hzeroInput, hzeroProgress, ?_⟩
      · unfold num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
        have hple : d.decimal_point <= 0#i32 := by
          change d.decimal_point.val <= 0
          omega
        simp only [hple, if_true, core.option.Option.is_none]
        rcases hcontrol with ⟨hpneg, negp, n, hneg, hn, hnval, hsel⟩ |
            ⟨hp0, hz5, hshift⟩
        · have hpne : ¬ d.decimal_point = 0#i32 := by
            intro h
            have := congrArg IScalar.val h
            simp at this
            omega
          simp only [hpne, if_false, hneg, bind_tc_ok,
            Bool.true_eq, if_true]
          rw [hn]
          simp only [bind_tc_ok]
          rw [hsel]
          simp only [bind_tc_ok]
          have hdEta :
              ({ num_digits := d.num_digits,
                 decimal_point := d.decimal_point,
                 truncated := d.truncated,
                 digits := d.digits } :
                num.imp.dec2flt.decimal_seq.DecimalSeq) = d := by
            cases d
            rfl
          rw [hdEta, hleft]
          simp only [bind_tc_ok, hnotReset, if_false, lift]
          rw [show UScalar.hcast .I32 shift = shiftI by rfl]
          simp only [hexpSub, bind_tc_ok]
          simp only [show (none : Option num.imp.dec2flt.common.BiasedFp).isNone =
            true by rfl, if_true]
        · have hpeq : d.decimal_point = 0#i32 :=
            IScalar.eq_of_val_eq (by simpa using hp0)
          have hlt : ¬ head >= 5#u8 := by
            change ¬ head.val >= 5
            omega
          have hshiftMatch := shiftRule head z hzLo hz5 hheadVal
          rw [← hshift] at hshiftMatch
          have hselector := (chooseShift_eq head).trans hshiftMatch
          simp only [hpeq, if_true, hread, hlt, if_false, bind_tc_ok]
          simp only [show (none : Option num.imp.dec2flt.common.BiasedFp).isNone =
            true by rfl, if_true]
          have hleadVal : d.digits.val[0].val = z := harrayHead
          split
          · change 0 = z at hheadVal
            have hz0 : z = 0 := hheadVal.symm
            omega
          · change 1 = z at hheadVal
            have hz1 : z = 1 := hheadVal.symm
            simp only [hz1, if_true] at hshift hleft
            subst shift
            have hdZeroEta :
                ({ num_digits := d.num_digits,
                   decimal_point := 0#i32,
                   truncated := d.truncated,
                   digits := d.digits } :
                num.imp.dec2flt.decimal_seq.DecimalSeq) = d := by
              rw [← hpeq]
            simp only [bind_tc_ok]
            rw [hdZeroEta, hleft]
            simp only [bind_tc_ok, hnotReset, if_false, lift]
            rw [show UScalar.hcast .I32 2#usize = shiftI by rfl]
            simp only [hexpSub, bind_tc_ok]
          · have hz1 : z ≠ 1 := by
              intro hz
              subst z
              have hu : head = 1#u8 := by
                apply UScalar.eq_of_val_eq
                scalar_tac
              subst head
              simp_all
            simp only [hz1, if_false] at hshift
            subst shift
            have hdZeroEta :
                ({ num_digits := d.num_digits,
                   decimal_point := 0#i32,
                   truncated := d.truncated,
                   digits := d.digits } :
                num.imp.dec2flt.decimal_seq.DecimalSeq) = d := by
              rw [← hpeq]
            simp only [bind_tc_ok]
            rw [hdZeroEta, hleft]
            simp only [bind_tc_ok, hnotReset, if_false, lift]
            rw [show UScalar.hcast .I32 1#usize = shiftI by rfl]
            simp only [hexpSub, bind_tc_ok]
      · exact ⟨hcap', hdigit', hlen', hend'⟩

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (151)
More program dependencies (109)
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 126 entries
Technical checking records
Local closure summary
{
  "node": "P804_T1_LeftLoopStep",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "ded94ac66ca9e83bcd5644f89cda2b4e63112db0c198af36bbfa89a8daad23ec",
  "active_statement_hash": "88d3a658c7d9eeeb77e0a25c6211fe2fd6ae04bcad2fed5349299d5f13c65c55",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "ab64d13f256386ee0e494a5abcc6a5db49512cf3d90bbdfcabd6327793a31f95",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-192",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalLeftShiftLoop1Execution": "c945a26c8d67077b",
    "DecimalSeqLeftShiftCanonical": "bcdb7c96b3a59e9d",
    "P804_T1_LeftLeadingProgress": "fb2f46fb0f6f403d",
    "P804_T1_LeftShiftPointGrowth": "6a9a6c7c27a2ac4e",
    "P804_T1_PointMinusOneGrowth": "431f2d39adc7b497",
    "P804_T1_PointZeroGrowth": "8e1fc1c4da087135",
    "P804_T1_SelectedLeftGrowth": "22f870b26a133ec7",
    "SlowShiftSelectorCertificate": "963b7a2e670856e8",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE": "01b84a7a35e5bcf4",
    "num_imp_dec2flt_slow_parse_long_mantissa": "adfe9a4161cd7fd6"
  },
  "strict_definition_deps": {
    "F64": "ec4e25fbaf4e11d4",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64": "94095c9bf79da164",
    "SlowShiftSelectorCertificate": "7ebc2b39d69850c4",
    "num_imp_dec2flt_common_BiasedFp": "63baac539d218a81",
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "eaf981d0a5d9f472",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE": "0cc5155758bc976a",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift": "fd298e37d5ae4f86",
    "num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift": "b3efbd0e6b3fe061",
    "num_imp_dec2flt_slow_parse_long_mantissa": "c03771a4216d102f",
    "num_imp_dec2flt_slow_parse_long_mantissa_closure_Insts_CoreOpsFunctionFnTupleUsizeUsize_call": "7df8a2c8e3717790"
  }
}
Prose assessment
{
  "label": "Lean proof closed; predictive prose review no longer required",
  "assessment_status": null,
  "fingerprint_relation": "unknown",
  "assessment_fingerprint": null,
  "current_fingerprint": "sound-v2:77ab2586467aa47274cfa124807948af1b97e4f82bdbd94d4412e6c1ad4ce7ac",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}