DecimalSeqLeftShiftRecordSemantics

Lean checked

Statement

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

Lemma · Returned-record semantics of decimal left shift

For a decimal sequence d, write
V(d)=\operatorname{fold}_{10} (d.\mathit{digits}[0:d.\mathit{num\_digits}]) 10^{d.\mathit{decimal\_point}-d.\mathit{num\_digits}}.
Let 0\le k\le60, suppose V(d)>0, and suppose every cell in the stored prefix is a decimal digit. If the extracted left shift by k returns d', then there is a real number \rho such that
0\le \rho<10^{d'.\mathit{decimal\_point}-768},
V(d')=2^kV(d)-\rho
and
d'.\mathit{truncated}=\mathrm{true} \quad\Longleftrightarrow\quad d.\mathit{truncated}=\mathrm{true}\ \text{or}\ \rho>0.

Lean statement

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

import Tablet.DecimalLeftShiftDigitGrowth
import Tablet.DecimalLeftShiftFirstLoopSemantics
import Tablet.DecimalLeftShiftSecondLoopSemantics
import Tablet.DecimalLeftShiftTableCertificate
import Tablet.DecimalSeqTrimSemantics
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift
import Tablet.num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 800000

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqLeftShiftRecordSemantics]
theorem DecimalSeqLeftShiftRecordSemantics :
    let value := fun d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun z => z.val)
      let integer : Nat := digits.foldl (fun a z => 10 * a + z) 0
      (integer : Real) * (10 : Real) ^
        (d.decimal_point.val - (d.num_digits.val : Int))
    forall (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift : Std.Usize),
      shift.val <= 60 ->
      0 < value d ->
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) ->
      num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d' ->
      exists rho : Real,
        0 <= rho /\
        rho < (10 : Real) ^ (d'.decimal_point.val - 768) /\
        value d' = (2 : Real) ^ shift.val * value d - rho /\
        (d'.truncated = true <->
          d.truncated = true \/ 0 < rho) := 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

Put n=d.\mathit{num\_digits} and p=d.\mathit{decimal\_point}, and first use only the list that occurs in the definition of V:
x=d.\mathit{digits}[0:n],\qquad I=\operatorname{fold}_{10}(x).
If n=0, the actual take-zero list x is empty, its base-ten fold is zero, and therefore V(d)=0\cdot10^p=0. This contradicts V(d)>0, so n>0. Consequently the zero-count return in the unfolded definition num imp dec2flt decimal seq DecimalSeq left shift is impossible.
Follow the successful nonzero execution through the digit-growth call and the checked width addition to its first call of left_shift_loop0. Its first body step decrements the positive read index n and then reads cell n-1. A failing decrement or array read cannot lie on an execution whose enclosing call returns normally. The digit array has exactly 768 cells by num imp dec2flt decimal seq DecimalSeq, and the cap has value 768 by num imp dec2flt decimal seq DecimalSeq MAX DIGITS; hence the successful read gives n-1<768, and therefore n\le768. This also covers an unrestricted Aeneas state whose count initially exceeds its physical array: such a state cannot meet the successful-call hypothesis.
Only now name the physical cells a_0,\ldots,a_{n-1} and put
I_r=\sum_{j=n-r}^{n-1}a_j10^{n-1-j}.
The established bound makes all these cells physical, the definition of the base-ten fold gives I=I_n, and hence V(d)=I10^{p-n}. In particular I>0, since the scale is positive and V(d)>0.
Handle k=0 separately. The digit-growth routine in the unfolded left shift returns q=0: its two table reads and empty comparison slice at index zero are certified by DecimalLeftShiftTableCertificate, and direct unfolding of num imp dec2flt decimal seq number of digits decimal left shift then takes the zero high field. In the first reverse loop, a shift by zero changes a cell a into a, the incoming carry is initially zero, division by ten returns quotient zero because a<10, and the remainder is a. The read and write indices therefore decrease together from n. Equivalently, DecimalLeftShiftFirstLoopSemantics gives cursor and carry zero, the original length-n digit list, and an unchanged Boolean because every destination is below 768. Apply DecimalLeftShiftSecondLoopSemantics with cursor and carry zero; its zero-padding premise is vacuous, its output list is empty, and it leaves both the array and Boolean unchanged. The cap comparison selects n, the cast of zero is zero, the point addition returns p, and the terminal trim preserves the value and truncation Boolean by DecimalSeqTrimSemantics. Taking \rho=0 proves both displayed claims in this case. The additional strict bound also holds because every real power of the positive base 10 is positive, so 0<10^{d'.\mathit{decimal\_point}-768}.
Assume now k>0, and set B=2^k. Let h be the unique positive integer with
10^{h-1}<B<10^h. \tag{1}
Such an h exists with 1\le h\le19: powers of ten are strictly increasing, 1<B, and B\le2^{60}<10^{19}. Apply DecimalLeftShiftDigitGrowth using (1), the just-proved n\le768, and the decimal-cell hypothesis. It says that the extracted digit-growth call returns a number q equal to h or h-1 according as IB does or does not reach 10^{n+h-1}. Consequently
IB<10^{n+q}. \tag{2}
Indeed, in the q=h branch the decimal-digit bound I<10^n and B<10^h give the result; in the q=h-1 branch it is exactly the negation of the threshold test. Thus L:=n+q is a fixed output width for IB, possibly with leading zeroes when the input itself has leading zeroes. In particular 0\le q\le19, so the checked addition n+q is far below the maximum machine-size value and returns L.
Apply DecimalLeftShiftFirstLoopSemantics to the first reverse carry loop with input array d.\mathit{digits}, read index n, write index L=n+q, zero carry, and the original truncation Boolean. Its hypotheses are exactly k\le60, the just-proved n\le768, n\le L, and the stated decimal-digit condition. Unfolding the successful outer call supplies the required normal return of this loop. The helper returns a digit list e=(e_0,\ldots,e_{n-1}), a carry c_n<B, and a write index w_1 with w_1+n=L. Thus w_1=q, and, if W_n is the base-ten fold of e,
BI=c_n10^n+W_n. \tag{8}
Every position q+j<768 visited by this loop contains e_j, every cell outside [q,L) is unchanged, and its returned Boolean is the input Boolean OR nonzeroness of exactly those e_j whose destination q+j is at least 768. These are the array and sticky premises needed below.
The second reverse loop expands c_n into the remaining q leading product digits. First, (2) and (8) give c_n10^n\le BI<10^{n+q}, hence c_n<10^q. We also establish the zero-padding premise of DecimalLeftShiftSecondLoopSemantics. Fix j<q with j<768 and suppose c_n<10^{q-j-1}. The first-loop unchanged-region clause identifies its cell at j with the original input cell. If the growth comparison selected q=h, combining this carry bound with W_n<10^n in (8) would give BI<10^{n+h-1}, contradicting the selected threshold. If it selected q=h-1, first j<n: otherwise the same upper bound, together with I\ge1 and 10^q<B, would give BI<B\le BI. Were the original cell at j nonzero, positional base-ten bounds would give I\ge10^{n-j-1}; multiplying by B>10^q would then contradict the strict upper bound on BI obtained from (8), the assumed carry bound, and W_n<10^n. Thus the cell is zero in either growth branch.
Apply DecimalLeftShiftSecondLoopSemantics with these two premises and the normal loop return supplied by the successful outer call. It yields a length-q decimal list whose fold is c_n, stores that list in every physical position below q, preserves all cells at or beyond q, and sets the Boolean precisely for its nonzero digits with destinations at least 768. The helper's padded-prefix conclusion covers the case in which the carry has fewer than q decimal digits: all unwritten positions before its final cursor are the just-proved original leading zeroes. Concatenating this list with e and using (8) therefore gives the complete fixed-width base-ten expansion of BI, while combining the helpers' two Boolean equivalences accounts for exactly every omitted nonzero digit.
Combining the two loop invariants, let b_0,\ldots,b_{L-1} be the width-L base-ten expansion of IB, padded by leading zeroes when necessary. Before trimming, the returned array stores b_j at every j<\min(L,768), its count is m=\min(L,768), its point is p+q, and its Boolean is
d.\mathit{truncated}\ \mathsf{or}\ (\exists j,\ 768\le j<L\ \text{and}\ b_j>0). \tag{9}
The point addition is checked safely: q\le19, and a normally returned outer call proves the required I32 range side condition. The count cap is exactly the branch selecting 768 when L>768.
Define the omitted integer suffix
E=\sum_{j=768}^{L-1} b_j10^{L-1-j},
with E=0 when L\le768, and define
\rho=E10^{p+q-L}. \tag{10}
All summands and the scale in (10) are nonnegative, so \rho\ge0, and \rho>0 holds exactly when some omitted digit is positive. Splitting the base-ten expansion of IB after the first m digits shows that the pre-trim record value is
(IB)10^{p-n}-\rho=2^kV(d)-\rho; \tag{11}
here p+q-L=p-n because L=n+q. Its count is at most the 768-cell array length, so DecimalSeqTrimSemantics applies to the terminal trim. That lemma preserves the record value and truncation Boolean. Equations (9)--(11) therefore hold for the actual returned d'; it also preserves the point, so d'.\mathit{decimal\_point}=p+q.
It remains to check the strict loss bound. If E=0, then \rho=0 and the bound follows from positivity of a power of 10. If E>0, the omitted suffix is nonempty, so L>m. Since m=\min(L,768), this forces m=768. Every omitted cell is one of the decimal product digits and is therefore below 10. Induction over these L-768 cells gives the strict base-ten fold estimate
E<10^{L-768}.
Multiplying by the positive scale in (10) and using the preserved point gives
\rho < 10^{L-768}10^{p+q-L} =10^{p+q-768} =10^{d'.\mathit{decimal\_point}-768}.
This is a bound on the suffix discarded by this one shift, not on any error accumulated across several shifts. Since the second disjunct of (9) is equivalent to \rho>0, they give exactly the asserted strict bound, value equation, and Boolean equivalence.

Lean proof


  dsimp only
  intro d d' shift hshift hvalue hdigit hcall
  let xs := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
  let F := fun ys : List Nat => ys.foldl (fun acc z => 10 * acc + z) 0
  let I := F xs
  have hxdigit : forall z, z ∈ xs -> z < 10 := by
    intro z hz
    simp only [xs, List.mem_map] at hz
    rcases hz with ⟨u, hu, rfl⟩
    exact hdigit u hu
  have foldlFrom (ys : List Nat) (acc : Nat) :
      ys.foldl (fun n z => 10 * n + z) acc =
        acc * 10 ^ ys.length + F ys := by
    induction ys generalizing acc with
    | nil => simp [F]
    | cons y ys ih =>
      simp only [List.foldl_cons, List.length_cons]
      rw [ih, pow_succ]
      change (10 * acc + y) * 10 ^ ys.length + F ys =
        acc * (10 ^ ys.length * 10) +
          (y :: ys).foldl (fun n z => 10 * n + z) 0
      simp only [List.foldl_cons]
      rw [ih]
      ring
  have foldBound (ys : List Nat) (hd : forall z, z ∈ ys -> z < 10) :
      F ys < 10 ^ ys.length := by
    induction ys with
    | nil => simp [F]
    | cons y ys ih =>
      have hy : y < 10 := hd y (by simp)
      have hys : forall z, z ∈ ys -> z < 10 := by
        intro z hz
        exact hd z (by simp [hz])
      have hi := ih hys
      rw [show F (y :: ys) = y * 10 ^ ys.length + F ys by
        simpa [F] using foldlFrom ys y, List.length_cons, pow_succ]
      have hmul : y * 10 ^ ys.length <= 9 * 10 ^ ys.length :=
        Nat.mul_le_mul_right _ (by omega)
      calc
        y * 10 ^ ys.length + F ys <
            y * 10 ^ ys.length + 10 ^ ys.length := Nat.add_lt_add_left hi _
        _ <= 9 * 10 ^ ys.length + 10 ^ ys.length :=
          Nat.add_le_add_right hmul _
        _ = 10 ^ ys.length * 10 := by ring
  have foldLowerAt (ys : List Nat) (j : Nat) (hj : j < ys.length)
      (hjpos : 0 < ys[j]) : 10 ^ (ys.length - j - 1) <= F ys := by
    have hprefix : ys[j] <= F (ys.take (j + 1)) := by
      simp only [F]
      rw [List.take_succ, List.foldl_append]
      have hget : ys[j]? = some ys[j] :=
        getElem?_eq_some_iff.mpr ⟨hj, rfl⟩
      simp [hget]
    have hdecomp : F ys =
        F (ys.take (j + 1)) * 10 ^ (ys.length - j - 1) +
          F (ys.drop (j + 1)) := by
      conv_lhs => rw [← List.take_append_drop (j + 1) ys]
      simp only [F, List.foldl_append]
      rw [foldlFrom]
      congr 2
      simp
      omega
    rw [hdecomp]
    have hm := Nat.mul_le_mul_right (10 ^ (ys.length - j - 1)) hprefix
    have hone : 1 <= ys[j] := by omega
    have hp := Nat.mul_le_mul_right (10 ^ (ys.length - j - 1)) hone
    omega
  have decimalUpper (c w t n : Nat) (hc : c < t) (hw : w < 10 ^ n) :
      c * 10 ^ n + w < t * 10 ^ n := by
    have hc1 : c + 1 <= t := by omega
    have hm := Nat.mul_le_mul_right (10 ^ n) hc1
    nlinarith
  have foldPos (ys : List Nat) :
      0 < F ys <-> exists j, exists hj : j < ys.length, 0 < ys[j] := by
    induction ys with
    | nil => simp [F]
    | cons y ys ih =>
      have hform : F (y :: ys) = y * 10 ^ ys.length + F ys := by
        simpa [F] using foldlFrom ys y
      rw [hform]
      constructor
      · intro hp
        by_cases hy : 0 < y
        · exact ⟨0, by simp, hy⟩
        · have htail : 0 < F ys := by
            have : y = 0 := by omega
            simpa [this] using hp
          rcases ih.mp htail with ⟨j, hj, hjpos⟩
          exact ⟨j + 1, by simp; omega, by simpa using hjpos⟩
      · rintro ⟨j, hj, hjpos⟩
        cases j with
        | zero =>
          simp only [List.getElem_cons_zero] at hjpos
          have hpow : 0 < 10 ^ ys.length := by positivity
          nlinarith
        | succ j =>
          have hjlt : j < ys.length := by simp at hj; omega
          have hp : 0 < F ys := ih.mpr ⟨j, hjlt, by simpa using hjpos⟩
          omega
  have zeroGrowth (d0 : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (sh gr : Std.Usize) (hsh : sh.val = 0)
      (hc : num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
        d0 sh = ok gr) : gr.val = 0 := by
    as_aux_lemma =>
    have hseq : sh = 0#usize := UScalar.eq_of_val_eq (by simpa using hsh)
    subst sh
    let kfin : Fin 61 := ⟨0, by omega⟩
    have cert := DecimalLeftShiftTableCertificate kfin
    dsimp only at cert
    rcases cert with ⟨hloidx, hhiidx, hlen, _, hzero, _⟩
    have hz := hzero (by simp [kfin])
    rcases hz with ⟨hhiZero, hdsZero⟩
    let table :=
      num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
    let pow5Table :=
      num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE_POW5
    let x := table.val.get ⟨0, by simp [table]⟩
    let y := table.val.get ⟨1, by simp [table]⟩
    let hi := x.val / 2048
    let lo := x.val % 2048
    let hiNext := y.val % 2048
    let dsU := pow5Table.val.extract lo hiNext
    let ds := dsU.map (fun z => z.val)
    have hloidx' : lo <= hiNext := by
      simpa [lo, hiNext, x, y, table, kfin] using hloidx
    have hhiidx' : hiNext <= 1308 := by
      simpa [hiNext, y, table, kfin] using hhiidx
    have hlen' : ds.length = hiNext - lo := by
      simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table, kfin] using hlen
    have hhiZero' : hi = 0 := by
      simpa [hi, x, table, kfin] using hhiZero
    have hdsZero' : ds = [] := by
      simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table, kfin] using hdsZero
    have hloEq : lo = hiNext := by
      rw [hdsZero'] at hlen'
      simp at hlen'
      omega
    have hmask : (0#usize &&& 63#usize) = 0#usize := by decide
    unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift at hc
    simp only [hmask, lift, Bind.bind, Aeneas.Std.bind] at hc
    obtain ⟨xa, hxa, hxaVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (Array.index_usize_spec
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
        0#usize (by simp [Array.length]))
    rw [hxa] at hc
    simp only [bind_tc_ok] at hc
    have hxaEq : xa = x := by
      apply UScalar.eq_of_val_eq
      simpa [x, table] using congrArg UScalar.val hxaVal
    subst xa
    rw [hxaEq] at hc
    obtain ⟨next, hnext, hnextVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.add_spec (x := 0#usize) (y := 1#usize) (by scalar_tac))
    rw [hnext] at hc
    simp only [bind_tc_ok] at hc
    norm_num at hnextVal
    obtain ⟨yb, hyb, hybVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (Array.index_usize_spec
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
        next (by simp [Array.length]; omega))
    rw [hyb] at hc
    simp only [bind_tc_ok] at hc
    have hybEq : yb = y := by
      apply UScalar.eq_of_val_eq
      simpa [y, table, hnextVal] using congrArg UScalar.val hybVal
    subst yb
    rw [hybEq] at hc
    obtain ⟨high16, hhigh16, hhigh16Val⟩ := Aeneas.Std.WP.spec_imp_exists
      (U16.ShiftRight_IScalar_spec x 11#i32 (by norm_num) (by norm_num))
    rw [hhigh16] at hc
    simp only [bind_tc_ok] at hc
    have hhigh16Eq : high16.val = hi := by
      rw [hhigh16Val.1, Nat.shiftRight_eq_div_pow]
      norm_num [hi, Int.toNat]
    let numNew : Std.Usize := UScalar.cast .Usize high16
    have hnumNewVal : numNew.val = 0 := by
      simp [numNew, hhigh16Eq, hhiZero']
    let low16 := 2047#u16 &&& x
    let pow5a : Std.Usize := UScalar.cast .Usize low16
    have hpow5aVal : pow5a.val = lo := by
      simp [pow5a, low16, lo, UScalar.val_and]
      rw [show 2047 = 2 ^ 11 - 1 by decide, Nat.and_comm,
        Nat.and_two_pow_sub_one_eq_mod]
    let next16 := 2047#u16 &&& y
    let pow5b : Std.Usize := UScalar.cast .Usize next16
    have hpow5bVal : pow5b.val = hiNext := by
      simp [pow5b, next16, hiNext, UScalar.val_and]
      rw [show 2047 = 2 ^ 11 - 1 by decide, Nat.and_comm,
        Nat.and_two_pow_sub_one_eq_mod]
    rw [show UScalar.cast .Usize (2047#u16 &&& x) = pow5a by rfl] at hc
    rw [show UScalar.cast .Usize (2047#u16 &&& y) = pow5b by rfl] at hc
    rw [show UScalar.cast .Usize high16 = numNew by rfl] at hc
    have hlo1308 : pow5a.val <= 1308 := by
      rw [hpow5aVal]
      exact hloidx'.trans hhiidx'
    let slice : Slice Std.U8 := pow5Table.to_slice.drop pow5a
    have hslice :
        core.array.Array.index
          (core.ops.index.IndexSlice
            (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8))
          pow5Table { start := pow5a } = ok slice := by
      rw [Array.index_SliceIndexRangeFromUsizeSlice]
      unfold core.slice.index.SliceIndexRangeFromUsizeSlice.index
      simp [hlo1308, slice]
    rw [hslice] at hc
    simp only [bind_tc_ok] at hc
    unfold core.slice.Slice.iter at hc
    simp only [bind_tc_ok] at hc
    unfold core.slice.iter.IteratorSliceIter.enumerate at hc
    simp only [bind_tc_ok] at hc
    have hsubBound : pow5a.val <= pow5b.val := by
      rw [hpow5aVal, hpow5bVal]
      exact hloidx'
    obtain ⟨span, hspan, hspanVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.sub_spec (x := pow5b) (y := pow5a) hsubBound)
    rw [hspan] at hc
    simp only [bind_tc_ok] at hc
    unfold core.iter.adapters.enumerate.IteratorEnumerate.take at hc
    simp only [bind_tc_ok] at hc
    have hspanZero : span.val = 0 := by
      rw [hspanVal.1, hpow5aVal, hpow5bVal, ← hloEq]
      simp
    have hspanEq : span = 0#usize :=
      UScalar.eq_of_val_eq (by simpa using hspanZero)
    subst span
    unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift_loop at hc
    rw [Aeneas.Std.loop.eq_def] at hc
    unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift_loop.body at hc
    unfold core.iter.adapters.take.IteratorTake.next at hc
    simp at hc
    have hgrEq : gr = numNew := hc.symm
    rw [hgrEq]
    exact hnumNewVal
  have hIpos : 0 < I := by
    by_contra hn
    have hIz : I = 0 := by omega
    simp only [xs, I, F] at hIz
    rw [hIz] at hvalue
    simp at hvalue
  have hnpos : 0 < d.num_digits.val := by
    by_contra hn
    have hnz : d.num_digits.val = 0 := by omega
    simp [xs, I, F, hnz] at hIpos
  have hnne : d.num_digits ≠ 0#usize := by
    intro h
    have : d.num_digits.val = 0 := by simpa [h]
    omega
  unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift at hcall
  simp only [hnne, if_false] at hcall
  cases hgrowth :
      num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift d shift with
  | fail e => simp [hgrowth] at hcall
  | div => simp [hgrowth] at hcall
  | ok growth =>
    simp only [hgrowth, bind_tc_ok] at hcall
    cases hadd : d.num_digits + growth with
    | fail e => simp [hadd] at hcall
    | div => simp [hadd] at hcall
    | ok width =>
      simp only [hadd, bind_tc_ok] at hcall
      cases hloop0 :
          num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
            d.truncated d.digits shift d.num_digits width 0#u64 with
      | fail e => simp [hloop0] at hcall
      | div => simp [hloop0] at hcall
      | ok out0 =>
        rcases out0 with ⟨b0, a0, cursor, carry⟩
        simp only [hloop0, bind_tc_ok] at hcall
        have hnle : d.num_digits.val <= 768 := by
          by_contra hnle
          have hbad : 768 < d.num_digits.val := by omega
          unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0 at hloop0
          simp only [Bind.bind, Aeneas.Std.bind] at hloop0
          rw [Aeneas.Std.loop.eq_def] at hloop0
          unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body at hloop0
          have hrne : d.num_digits != 0#usize := by
            simp only [bne_iff_ne]
            exact hnne
          simp only [hrne, if_true] at hloop0
          obtain ⟨read1, hread1, hread1Val⟩ := Aeneas.Std.WP.spec_imp_exists
            (UScalar.sub_spec (x := d.num_digits) (y := 1#usize)
              (by simpa using hnpos))
          norm_num at hread1Val
          rw [hread1] at hloop0
          simp only [bind_tc_ok] at hloop0
          cases hwrite : width - 1#usize with
          | fail e => simp [hwrite] at hloop0
          | div => simp [hwrite] at hloop0
          | ok write1 =>
            rw [hwrite] at hloop0
            simp only [bind_tc_ok] at hloop0
            have hread1bad : d.digits.val.length <= read1.val := by
              have halen : d.digits.val.length = 768 := by
                simpa using d.digits.property
              rw [halen, hread1Val.1]
              omega
            have hnone : d.digits.val[read1.val]? = none :=
              List.getElem?_eq_none hread1bad
            unfold Array.index_usize at hloop0
            simp [hnone] at hloop0
        have hxslen : xs.length = d.num_digits.val := by
          simp [xs, hnle, d.digits.property]
        have hIbound : I < 10 ^ d.num_digits.val := by
          simpa [I, hxslen] using foldBound xs hxdigit
        cases hloop1 :
            num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
              b0 a0 cursor carry with
        | fail e => simp [hloop1] at hcall
        | div => simp [hloop1] at hcall
        | ok out1 =>
          rcases out1 with ⟨b1, a1⟩
          let B := 2 ^ shift.val
          by_cases hk0 : shift.val = 0
          · have hgrowthZero : growth.val = 0 :=
              zeroGrowth d shift growth hk0 hgrowth
            have haddBound : d.num_digits.val + growth.val <=
                UScalar.max .Usize := by
              scalar_tac
            obtain ⟨width', hwidth', hwidthVal⟩ :=
              Aeneas.Std.WP.spec_imp_exists
                (UScalar.add_spec (x := d.num_digits) (y := growth) haddBound)
            rw [hadd] at hwidth'
            cases hwidth'
            have hwidthEq : width.val = d.num_digits.val := by
              rw [hwidthVal, hgrowthZero]
              simp
            have hcursorInput : d.num_digits.val <= width.val := by
              rw [hwidthEq]
            obtain ⟨lowDigits, hlowLen, hlowDigit, hcursorEq, hcarryB,
                hproduct, hlowArray, houtside0, hsticky0⟩ :=
              DecimalLeftShiftFirstLoopSemantics d.digits a0 d.truncated b0
                shift d.num_digits width cursor carry hshift hnle hcursorInput
                hdigit hloop0
            have hcursorZero : cursor.val = 0 := by
              rw [hwidthEq] at hcursorEq
              omega
            have hcarryZero : carry.val = 0 := by
              rw [hk0] at hcarryB
              norm_num at hcarryB
              omega
            have hcarryWidth : carry.val < 10 ^ cursor.val := by
              rw [hcarryZero, hcursorZero]
              norm_num
            have hpad : forall j, j < cursor.val -> j < 768 ->
                carry.val < 10 ^ (cursor.val - j - 1) ->
                  a0.val[j]? = some 0#u8 := by
              intro j hj
              rw [hcursorZero] at hj
              omega
            obtain ⟨highDigits, hhighLen, hhighDigit, hcarryDigits,
                hhighArray, houtside1, hsticky1⟩ :=
              DecimalLeftShiftSecondLoopSemantics a0 a1 b0 b1 cursor carry
                hcarryWidth hpad hloop1
            have hhighNil : highDigits = [] := by
              apply List.eq_nil_of_length_eq_zero
              rw [hhighLen, hcursorZero]
            have hb10 : b1 = true <-> b0 = true := by
              rw [hsticky1]
              constructor
              · intro h
                rcases h with h | ⟨j, hj, _, _⟩
                · exact h
                · rw [hhighNil] at hj
                  simp at hj
              · exact Or.inl
            have hb0 : b0 = true <-> d.truncated = true := by
              rw [hsticky0]
              constructor
              · intro h
                rcases h with h | ⟨j, hj, hjcap, _⟩
                · exact h
                · rw [hlowLen] at hj
                  rw [hcursorZero] at hjcap
                  omega
              · exact Or.inl
            have hsticky : b1 = true <-> d.truncated = true := by
              rw [hb10, hb0]
            have hlowFold : F lowDigits = I := by
              simpa [hk0, hcarryZero, F, I, xs] using hproduct.symm
            have hlowArray1 : forall j, j < lowDigits.length -> j < 768 ->
                (a1.val.map (fun z : Std.U8 => z.val))[j]? = lowDigits[j]? := by
              intro j hj hjphys
              have hout := houtside1 j hjphys (by
                rw [hcursorZero]
                exact Nat.zero_le j)
              have hlo := hlowArray j hj (by
                rw [hcursorZero]
                simpa using hjphys)
              rw [List.getElem?_map, hout]
              simpa [hcursorZero] using hlo
            have hwidthLe : width.val <= 768 := by
              rw [hwidthEq]
              exact hnle
            have hcapFalse : ¬ width >
                num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
              simpa [gt_iff_lt, UScalar.lt_equiv,
                num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]
                using (show ¬ 768 < width.val by omega)
            have hcapEval :
                (if width > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                  then ok num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                  else ok width) = ok width := by
              simp [hcapFalse]
            change (do
              let (bc, ac) ←
                num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
                  b0 a0 cursor carry
              let count ←
                if width > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                then ok num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                else ok width
              let delta ← lift (UScalar.hcast .I32 growth)
              let point ← d.decimal_point + delta
              num.imp.dec2flt.decimal_seq.DecimalSeq.trim
                { num_digits := count, decimal_point := point,
                  truncated := bc, digits := ac }) = ok d' at hcall
            rw [hloop1] at hcall
            simp only [bind_tc_ok] at hcall
            rw [hcapEval] at hcall
            simp only [bind_tc_ok, lift] at hcall
            let growthI : Std.I32 := UScalar.hcast .I32 growth
            rw [show UScalar.hcast .I32 growth = growthI by rfl] at hcall
            have hgrowthIVal : growthI.val = (growth.val : Int) := by
              have hs := UScalar.hcast_inBounds_spec .I32 growth (by
                rw [IScalar.max_I32_eq]
                omega)
              simp only [lift, WP.spec_ok] at hs
              exact hs
            have hgrowthIZero : growthI.val = 0 := by
              rw [hgrowthIVal, hgrowthZero]
              norm_num
            cases hpoint : d.decimal_point + growthI with
            | fail e => simp [hpoint] at hcall
            | div => simp [hpoint] at hcall
            | ok point =>
              rw [hpoint] at hcall
              simp only [bind_tc_ok] at hcall
              let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
                { num_digits := width, decimal_point := point,
                  truncated := b1, digits := a1 }
              have htrim :
                  num.imp.dec2flt.decimal_seq.DecimalSeq.trim pre = ok d' := by
                simpa [pre] using hcall
              have hpointVal : point.val = d.decimal_point.val := by
                have hs := IScalar.add_equiv d.decimal_point growthI
                rw [hpoint] at hs
                rw [hgrowthIZero] at hs
                omega
              have hpreBound : pre.num_digits.val <= pre.digits.val.length := by
                simp [pre, a1.property]
                exact hwidthLe
              have htrimSem := DecimalSeqTrimSemantics pre d' hpreBound htrim
              dsimp only at htrimSem
              rcases htrimSem with
                ⟨hdigitsEq, hpointEq, htruncEq, _, _, _, hvalueTrim⟩
              have hpreDigits :
                  (a1.val.take width.val).map (fun z => z.val) = lowDigits := by
                apply List.ext_getElem
                · simp [hwidthEq, hlowLen, hnle, a1.property]
                · intro i hleft hright
                  have hi : i < lowDigits.length := hright
                  have hi768 : i < 768 := by
                    rw [hlowLen] at hi
                    omega
                  have ha := hlowArray1 i hi hi768
                  have haSome :
                      (a1.val.map (fun z : Std.U8 => z.val))[i]? =
                        some lowDigits[i] := by
                    rw [ha, List.getElem?_eq_getElem hi]
                  have hmapBound :
                      i < (a1.val.map (fun z : Std.U8 => z.val)).length := by
                    simp [a1.property]
                    exact hi768
                  rw [show ((a1.val.take width.val).map (fun z => z.val))[i] =
                      (a1.val.map (fun z => z.val))[i] by simp]
                  have hsome := List.getElem?_eq_getElem hmapBound
                  rw [haSome] at hsome
                  exact Option.some.inj hsome.symm
              refine ⟨0, by norm_num, zpow_pos (by norm_num) _, ?_, ?_⟩
              · rw [hvalueTrim]
                simp only [pre, hpointVal]
                rw [hpreDigits, hwidthEq]
                change (F lowDigits : Real) * (10 : Real) ^
                    (d.decimal_point.val - (d.num_digits.val : Int)) =
                  (2 : Real) ^ shift.val *
                    ((I : Real) * (10 : Real) ^
                      (d.decimal_point.val - (d.num_digits.val : Int))) - 0
                rw [hlowFold, hk0]
                norm_num
              · rw [htruncEq]
                simpa using hsticky
          · have hkpos : 0 < shift.val := by omega
            let kfin : Fin 61 := ⟨shift.val, by omega⟩
            have cert := DecimalLeftShiftTableCertificate kfin
            dsimp only at cert
            rcases cert with ⟨_, _, _, _, _, hpositive⟩
            have hp := hpositive (by simpa [kfin] using hkpos)
            rcases hp with ⟨_, _, _, hsum, hlo, hhi⟩
            let table :=
              num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
            let x := table.val.get ⟨shift.val, by
              have hlen : table.val.length = 65 := by simp [table]
              omega⟩
            let h := x.val / 2048
            have hlo' : 10 ^ (h - 1) < B := by
              simpa [h, x, table, kfin, B] using hlo
            have hhi' : B < 10 ^ h := by
              simpa [h, x, table, kfin, B] using hhi
            have hhpos : 0 < h := by
              by_contra hz
              have : h = 0 := by omega
              have hBpos : 0 < B := by simp [B]
              rw [this] at hhi'
              norm_num at hhi'
              omega
            have hhle : h <= 19 := by
              by_contra hn
              have h20 : 20 <= h := by omega
              have hp19 : 10 ^ 19 <= 10 ^ (h - 1) :=
                Nat.pow_le_pow_right (by omega) (by omega)
              have hBle : B <= 2 ^ 60 := by
                exact Nat.pow_le_pow_right (by omega) hshift
              have hnum : 2 ^ 60 < 10 ^ 19 := by norm_num
              exact (not_lt_of_ge hp19)
                (lt_of_lt_of_le hlo' (le_trans hBle (Nat.le_of_lt hnum)))
            obtain ⟨growth', hgrowth', hgrowthVal⟩ :=
              DecimalLeftShiftDigitGrowth d shift h hshift hlo' hhi' hnle hdigit
            rw [hgrowth] at hgrowth'
            cases hgrowth'
            have hgrowthFormula : growth.val =
                if 10 ^ (d.num_digits.val + h - 1) <= I * B
                then h else h - 1 := by
              simpa [xs, I, F, B] using hgrowthVal
            have hgrowthLe : growth.val <= 19 := by
              rw [hgrowthFormula]
              split <;> omega
            have haddBound : d.num_digits.val + growth.val <=
                UScalar.max .Usize := by
              scalar_tac
            obtain ⟨width', hwidth', hwidthVal⟩ := Aeneas.Std.WP.spec_imp_exists
              (UScalar.add_spec (x := d.num_digits) (y := growth) haddBound)
            rw [hadd] at hwidth'
            cases hwidth'
            have hcursorInput : d.num_digits.val <= width.val := by
              rw [hwidthVal]
              omega
            obtain ⟨lowDigits, hlowLen, hlowDigit, hcursorEq, hcarryB,
                hproduct, hlowArray, houtside0, hsticky0⟩ :=
              DecimalLeftShiftFirstLoopSemantics d.digits a0 d.truncated b0
                shift d.num_digits width cursor carry hshift hnle hcursorInput
                hdigit hloop0
            have hcursorVal : cursor.val = growth.val := by
              rw [hwidthVal] at hcursorEq
              omega
            have hprodWidth : B * I <
                10 ^ (d.num_digits.val + growth.val) := by
              rw [hgrowthFormula]
              split
              next ht =>
                have hBpos : 0 < B := by simp [B]
                have hpowpos : 0 < 10 ^ d.num_digits.val := by positivity
                have hmul1 : B * I < B * 10 ^ d.num_digits.val :=
                  (Nat.mul_lt_mul_left hBpos).mpr hIbound
                have hmul2 : B * 10 ^ d.num_digits.val <
                    10 ^ h * 10 ^ d.num_digits.val :=
                  (Nat.mul_lt_mul_right hpowpos).mpr hhi'
                have hmul := lt_trans hmul1 hmul2
                rw [Nat.mul_comm (10 ^ h), ← Nat.pow_add] at hmul
                simpa using hmul
              next ht =>
                have hlt : I * B < 10 ^ (d.num_digits.val + h - 1) := by
                  omega
                rw [Nat.mul_comm]
                simpa only [show d.num_digits.val + h - 1 =
                  d.num_digits.val + (h - 1) by omega] using hlt
            have hcarryWidth : carry.val < 10 ^ cursor.val := by
              rw [hcursorVal]
              have hmul : carry.val * 10 ^ d.num_digits.val <
                  10 ^ growth.val * 10 ^ d.num_digits.val := by
                have hcle : carry.val * 10 ^ d.num_digits.val <= B * I := by
                  rw [hproduct]
                  omega
                rw [Nat.mul_comm (10 ^ growth.val) (10 ^ d.num_digits.val),
                  ← Nat.pow_add]
                exact lt_of_le_of_lt hcle hprodWidth
              exact Nat.lt_of_mul_lt_mul_right hmul
            have hpad : forall j, j < cursor.val -> j < 768 ->
                carry.val < 10 ^ (cursor.val - j - 1) ->
                  a0.val[j]? = some 0#u8 := by
              as_aux_lemma =>
              intro j hjcursor hjphys hjcarry
              have hbase : a0.val[j]? = d.digits.val[j]? :=
                houtside0 j hjphys (Or.inl hjcursor)
              rw [hgrowthFormula] at hcursorVal
              split at hcursorVal
              next ht =>
                have hexp : cursor.val - j - 1 <= h - 1 := by omega
                have hpow : 10 ^ (cursor.val - j - 1) <= 10 ^ (h - 1) :=
                  Nat.pow_le_pow_right (by omega) hexp
                have hcTop : carry.val < 10 ^ (h - 1) :=
                  lt_of_lt_of_le hjcarry hpow
                have hu := decimalUpper carry.val (F lowDigits)
                  (10 ^ (h - 1)) d.num_digits.val hcTop (by
                    simpa [hlowLen] using foldBound lowDigits hlowDigit)
                rw [← hproduct] at hu
                rw [Nat.mul_comm (10 ^ (h - 1)), ← Nat.pow_add] at hu
                change B * I < 10 ^ (d.num_digits.val + (h - 1)) at hu
                have hthreshold : 10 ^ (d.num_digits.val + h - 1) <= B * I := by
                  simpa [Nat.mul_comm] using ht
                have he : d.num_digits.val + (h - 1) =
                    d.num_digits.val + h - 1 := by omega
                rw [he] at hu
                exact False.elim ((not_lt_of_ge hthreshold) hu)
              next ht =>
                have hcursorGrowth : cursor.val = h - 1 := hcursorVal
                have hlowBound : F lowDigits < 10 ^ d.num_digits.val := by
                  simpa [hlowLen] using foldBound lowDigits hlowDigit
                have hu := decimalUpper carry.val (F lowDigits)
                  (10 ^ (cursor.val - j - 1)) d.num_digits.val
                  hjcarry hlowBound
                rw [← hproduct] at hu
                rw [Nat.mul_comm (10 ^ (cursor.val - j - 1)),
                  ← Nat.pow_add] at hu
                change B * I <
                  10 ^ (d.num_digits.val + (cursor.val - j - 1)) at hu
                have hjN : j < d.num_digits.val := by
                  by_contra hjN
                  have hNle : d.num_digits.val <= j := by omega
                  have hexp : d.num_digits.val + (cursor.val - j - 1) <=
                      cursor.val - 1 := by omega
                  have hpow : 10 ^ (d.num_digits.val + (cursor.val - j - 1)) <=
                      10 ^ (cursor.val - 1) :=
                    Nat.pow_le_pow_right (by omega) hexp
                  have hcursorPos : 0 < cursor.val := by omega
                  have hpowStep : 10 ^ (cursor.val - 1) < 10 ^ cursor.val := by
                    apply Nat.pow_lt_pow_right (by omega)
                    omega
                  have hBLower : 10 ^ cursor.val < B := by
                    simpa [hcursorGrowth] using hlo'
                  have hBI : B <= B * I := by
                    have hm := Nat.mul_le_mul_left B (show 1 <= I by omega)
                    simpa using hm
                  exact (not_lt_of_ge hBI)
                    (lt_of_lt_of_le hu (le_trans hpow
                      (le_trans (Nat.le_of_lt hpowStep) (Nat.le_of_lt hBLower))))
                have hcellBound : j < d.digits.val.length := by
                  have hlen : d.digits.val.length = 768 := by
                    simpa using d.digits.property
                  rw [hlen]
                  exact hjphys
                have hxval : xs[j] = (d.digits.val[j]'hcellBound).val := by
                  simp [xs, hjN]
                have hxzero : xs[j] = 0 := by
                  by_contra hxzero
                  have hxpos : 0 < xs[j] := by omega
                  have hIlower := foldLowerAt xs j (by simpa [hxslen] using hjN) hxpos
                  have hIlower' : 10 ^ (d.num_digits.val - j - 1) <= I := by
                    simpa [hxslen, I] using hIlower
                  have hBLower : 10 ^ cursor.val < B := by
                    simpa [hcursorGrowth] using hlo'
                  have hmul1 : 10 ^ cursor.val *
                        10 ^ (d.num_digits.val - j - 1) <
                      B * 10 ^ (d.num_digits.val - j - 1) :=
                    (Nat.mul_lt_mul_right (by positivity)).mpr hBLower
                  have hmul2 : B * 10 ^ (d.num_digits.val - j - 1) <= B * I :=
                    Nat.mul_le_mul_left B hIlower'
                  have hlower : 10 ^ (cursor.val +
                        (d.num_digits.val - j - 1)) < B * I := by
                    rw [Nat.pow_add]
                    exact lt_of_lt_of_le hmul1 hmul2
                  have he : cursor.val + (d.num_digits.val - j - 1) =
                      d.num_digits.val + (cursor.val - j - 1) := by omega
                  rw [he] at hlower
                  exact False.elim ((not_lt_of_ge (Nat.le_of_lt hlower)) hu)
                have hdigitZero : d.digits.val[j]'hcellBound = 0#u8 := by
                  apply UScalar.eq_of_val_eq
                  simpa [← hxval, hxzero]
                have hbaseZero : d.digits.val[j]? = some 0#u8 := by
                  apply getElem?_eq_some_iff.mpr
                  exact ⟨hcellBound, hdigitZero⟩
                simpa [hbase] using hbaseZero
            obtain ⟨highDigits, hhighLen, hhighDigit, hcarryDigits,
                hhighArray, houtside1, hsticky1⟩ :=
              DecimalLeftShiftSecondLoopSemantics a0 a1 b0 b1 cursor carry
                hcarryWidth hpad hloop1
            let fullDigits := highDigits ++ lowDigits
            have hfullLen : fullDigits.length = width.val := by
              simp [fullDigits, hhighLen, hlowLen, hcursorEq]
            have hfullDigit : forall z, z ∈ fullDigits -> z < 10 := by
              intro z hz
              simp only [fullDigits, List.mem_append] at hz
              exact hz.elim (hhighDigit z) (hlowDigit z)
            have hfullFold : F fullDigits = B * I := by
              have happ : F fullDigits =
                  carry.val * 10 ^ lowDigits.length + F lowDigits := by
                simp only [fullDigits, F, List.foldl_append]
                rw [foldlFrom, hcarryDigits]
              rw [happ, hlowLen]
              simpa [B, I, F, xs] using hproduct.symm
            have hfullArray : forall j, j < fullDigits.length -> j < 768 ->
                (a1.val.map (fun z : Std.U8 => z.val))[j]? = fullDigits[j]? := by
              intro j hj hjphys
              by_cases hjhigh : j < cursor.val
              · have hh := hhighArray j (by simpa [hhighLen] using hjhigh) hjphys
                rw [List.getElem?_append_left (by simpa [fullDigits, hhighLen] using hjhigh)]
                simpa [fullDigits] using hh
              · have hcj : cursor.val <= j := by omega
                let t := j - cursor.val
                have ht : t < lowDigits.length := by
                  simp [fullDigits, hhighLen, hlowLen, hfullLen] at hj
                  simp [t]
                  omega
                have hidx : cursor.val + t = j := by simp [t, hcj]
                have hout := houtside1 j hjphys hcj
                have hlo := hlowArray t ht (by omega)
                rw [List.getElem?_map, hout]
                rw [List.getElem?_append_right (by
                  simpa [fullDigits, hhighLen] using hjhigh)]
                simpa [fullDigits, hhighLen, t, hidx] using hlo
            have hb10 : b1 = true <-> b0 = true := by
              rw [hsticky1]
              constructor
              · intro h
                rcases h with h | ⟨j, hj, hjcap, _⟩
                · exact h
                · rw [hhighLen, hcursorVal] at hj
                  omega
              · exact Or.inl
            have hstickyFull : b1 = true <-> d.truncated = true \/
                exists j, j < fullDigits.length /\ 768 <= j /\
                  fullDigits[j]? != some 0 := by
              rw [hb10, hsticky0]
              constructor
              · intro h
                rcases h with hd | ⟨j, hj, hjcap, hjnz⟩
                · exact Or.inl hd
                · right
                  refine ⟨cursor.val + j, ?_, hjcap, ?_⟩
                  · simp [fullDigits, hhighLen, hlowLen]
                    omega
                  · rw [List.getElem?_append_right (by simp [hhighLen])]
                    simpa [hhighLen] using hjnz
              · intro h
                rcases h with hd | ⟨j, hj, hjcap, hjnz⟩
                · exact Or.inl hd
                · right
                  have hcj : cursor.val <= j := by
                    by_contra hcj
                    rw [List.getElem?_append_left (by
                      simpa [fullDigits, hhighLen] using hcj)] at hjnz
                    have hjlt : j < highDigits.length := by simpa [hhighLen] using hcj
                    have hjcap' : j < 768 := by omega
                    have hz := hhighDigit highDigits[j] (List.getElem_mem _)
                    omega
                  refine ⟨j - cursor.val, ?_, ?_, ?_⟩
                  · simp [fullDigits, hhighLen] at hj
                    omega
                  · omega
                  · rw [List.getElem?_append_right (by
                      simpa [fullDigits, hhighLen] using hcj)] at hjnz
                    simpa [hhighLen] using hjnz
            let capped : Std.Usize :=
              if width.val > 768 then
                num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS else width
            have hcappedVal : capped.val = min width.val 768 := by
              simp only [capped]
              split
              · simp [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]
                omega
              · simp
                omega
            have hcapEval :
                (if width > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                  then ok num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                  else ok width) = ok capped := by
              simp only [capped]
              by_cases hc : width.val > 768
              · have hc' : width >
                    num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
                  simpa [gt_iff_lt, UScalar.lt_equiv,
                    num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]
                    using hc
                simp [hc', hc]
              · have hc' : ¬ width >
                    num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
                  simpa [gt_iff_lt, UScalar.lt_equiv,
                    num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]
                    using hc
                simp [hc', hc]
            change (do
              let (bc, ac) ←
                num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
                  b0 a0 cursor carry
              let count ←
                if width > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                then ok num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                else ok width
              let delta ← lift (UScalar.hcast .I32 growth)
              let point ← d.decimal_point + delta
              num.imp.dec2flt.decimal_seq.DecimalSeq.trim
                { num_digits := count, decimal_point := point,
                  truncated := bc, digits := ac }) = ok d' at hcall
            rw [hloop1] at hcall
            simp only [bind_tc_ok] at hcall
            rw [hcapEval] at hcall
            simp only [bind_tc_ok, lift] at hcall
            let growthI : Std.I32 := UScalar.hcast .I32 growth
            rw [show UScalar.hcast .I32 growth = growthI by rfl] at hcall
            have hgrowthI : growthI.val = (growth.val : Int) := by
              have hs := UScalar.hcast_inBounds_spec .I32 growth (by
                rw [IScalar.max_I32_eq]
                omega)
              simp only [lift, WP.spec_ok] at hs
              exact hs
            cases hpoint : d.decimal_point + growthI with
            | fail e => simp [hpoint] at hcall
            | div => simp [hpoint] at hcall
            | ok point =>
              rw [hpoint] at hcall
              simp only [bind_tc_ok] at hcall
              let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
                { num_digits := capped, decimal_point := point,
                  truncated := b1, digits := a1 }
              have htrim :
                  num.imp.dec2flt.decimal_seq.DecimalSeq.trim pre = ok d' := by
                simpa [pre] using hcall
              have hpointVal : point.val = d.decimal_point.val + growth.val := by
                have hs := IScalar.add_equiv d.decimal_point growthI
                rw [hpoint] at hs
                rw [hgrowthI] at hs
                omega
              have hpreBound : pre.num_digits.val <= pre.digits.val.length := by
                simp [pre, hcappedVal, a1.property]
              have htrimSem := DecimalSeqTrimSemantics pre d' hpreBound htrim
              dsimp only at htrimSem
              rcases htrimSem with
                ⟨hdigitsEq, hpointEq, htruncEq, _, _, _, hvalueTrim⟩
              let m := capped.val
              let pref := fullDigits.take m
              let suff := fullDigits.drop m
              let E := F suff
              have hmWidth : m <= fullDigits.length := by
                simp [m, hcappedVal, hfullLen]
              have hm768 : m <= 768 := by
                simp [m, hcappedVal]
              have hprefixDigits :
                  (a1.val.take m).map (fun z => z.val) = pref := by
                apply List.ext_getElem
                · simp [pref, hmWidth, m, a1.property, hm768]
                · intro i hleft hright
                  have him : i < m := by
                    simpa [m, a1.property, hm768] using hleft
                  have hifull : i < fullDigits.length := lt_of_lt_of_le him hmWidth
                  have hi768 : i < 768 := lt_of_lt_of_le him hm768
                  have ha := hfullArray i hifull hi768
                  have haSome :
                      (a1.val.map (fun z : Std.U8 => z.val))[i]? =
                        some fullDigits[i] := by
                    rw [ha, List.getElem?_eq_getElem hifull]
                  have hmapBound : i < (a1.val.map (fun z : Std.U8 => z.val)).length := by
                    simp [a1.property]
                    exact hi768
                  rw [show ((a1.val.take m).map (fun z => z.val))[i] =
                      (a1.val.map (fun z => z.val))[i] by simp]
                  rw [show pref[i] = fullDigits[i] by simp [pref]]
                  have hsome := List.getElem?_eq_getElem hmapBound
                  rw [haSome] at hsome
                  exact Option.some.inj hsome.symm
              have hsuffixLen : suff.length = fullDigits.length - m := by
                simp [suff]
              have hfoldSplit : F fullDigits =
                  F pref * 10 ^ suff.length + E := by
                conv_lhs => rw [← List.take_append_drop m fullDigits]
                simp only [pref, suff, E, F, List.foldl_append]
                rw [foldlFrom]
              have hEpos : 0 < E <->
                  exists j, j < fullDigits.length /\ 768 <= j /\
                    fullDigits[j]? != some 0 := by
                constructor
                · intro hE
                  rcases (foldPos suff).mp hE with ⟨t, ht, htpos⟩
                  have hmLt : m < fullDigits.length := by
                    simp [suff] at ht
                    omega
                  have hmEq : m = 768 := by
                    simp [m, hcappedVal] at hmLt ⊢
                    omega
                  refine ⟨m + t, ?_, by omega, ?_⟩
                  · simp [suff] at ht
                    omega
                  · have hidx : suff[t] = fullDigits[m + t] := by
                      exact List.getElem_drop
                    have hbound : m + t < fullDigits.length := by
                      simp [suff] at ht
                      omega
                    have hsome := List.getElem?_eq_getElem hbound
                    rw [hsome]
                    simp only [bne_iff_ne]
                    intro hz
                    have hzval : fullDigits[m + t] = 0 := Option.some.inj hz
                    rw [← hidx] at hzval
                    omega
                · rintro ⟨j, hj, hjcap, hjnz⟩
                  have hmEq : m = 768 := by
                    simp [m, hcappedVal, hfullLen]
                    omega
                  have ht : j - m < suff.length := by
                    simp [suff]
                    omega
                  apply (foldPos suff).mpr
                  refine ⟨j - m, ht, ?_⟩
                  have hidx : suff[j - m] = fullDigits[j] := by
                    rw [List.getElem_drop]
                    congr
                    omega
                  rw [hidx]
                  have hsome := List.getElem?_eq_getElem hj
                  by_contra hz
                  have hz' : fullDigits[j] = 0 := by omega
                  rw [hsome, hz'] at hjnz
                  simp at hjnz
              let rho : Real := (E : Real) * (10 : Real) ^
                (d.decimal_point.val - (d.num_digits.val : Int))
              have hrho0 : 0 <= rho := by
                dsimp only [rho]
                exact mul_nonneg (Nat.cast_nonneg E)
                  (le_of_lt (zpow_pos (by norm_num : (0 : Real) < 10) _))
              have hrhopos : 0 < rho <-> 0 < E := by
                dsimp only [rho]
                have hscale : 0 < (10 : Real) ^
                    (d.decimal_point.val - (d.num_digits.val : Int)) :=
                  zpow_pos (by norm_num) _
                constructor
                · intro hr
                  have hcast : 0 < (E : Real) := by nlinarith
                  exact_mod_cast hcast
                · intro hE
                  have hcast : 0 < (E : Real) := by exact_mod_cast hE
                  exact mul_pos hcast hscale
              have hsuffDigit : forall z, z ∈ suff -> z < 10 := by
                intro z hz
                exact hfullDigit z (List.mem_of_mem_drop hz)
              have hEbound : E < 10 ^ suff.length := by
                simpa [E] using foldBound suff hsuffDigit
              have hrhoCell :
                  rho < (10 : Real) ^ (d'.decimal_point.val - 768) := by
                by_cases hEz : E = 0
                · simp only [rho, hEz, Nat.cast_zero, zero_mul]
                  exact zpow_pos (by norm_num) _
                · have hEposNat : 0 < E := Nat.pos_of_ne_zero hEz
                  rcases (foldPos suff).mp hEposNat with ⟨t, ht, _⟩
                  have hmLt : m < fullDigits.length := by
                    simp [suff] at ht
                    omega
                  have hmEq : m = 768 := by
                    simp [m, hcappedVal, hfullLen] at hmLt ⊢
                    omega
                  have hexpCell :
                      d.decimal_point.val - (d.num_digits.val : Int) +
                          (suff.length : Int) =
                        d'.decimal_point.val - 768 := by
                    rw [hpointEq]
                    simp only [pre]
                    rw [hpointVal, hsuffixLen, hfullLen, hwidthVal]
                    have hmBound : m <= d.num_digits.val + growth.val := by
                      rw [← hwidthVal, ← hfullLen]
                      exact hmWidth
                    rw [Nat.cast_sub hmBound, hmEq]
                    omega
                  have hcastBound :
                      (E : Real) < (10 : Real) ^ suff.length := by
                    exact_mod_cast hEbound
                  have hscalePos : 0 < (10 : Real) ^
                      (d.decimal_point.val - (d.num_digits.val : Int)) :=
                    zpow_pos (by norm_num) _
                  calc
                    rho = (E : Real) * (10 : Real) ^
                        (d.decimal_point.val - (d.num_digits.val : Int)) := rfl
                    _ < (10 : Real) ^ suff.length * (10 : Real) ^
                        (d.decimal_point.val - (d.num_digits.val : Int)) :=
                      mul_lt_mul_of_pos_right hcastBound hscalePos
                    _ = (10 : Real) ^ (d'.decimal_point.val - 768) := by
                      norm_num only [← zpow_natCast]
                      rw [← zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
                      rw [add_comm, hexpCell]
              refine ⟨rho, hrho0, hrhoCell, ?_, ?_⟩
              · rw [hvalueTrim]
                simp only [pre, hpointVal, m] at hprefixDigits ⊢
                rw [hprefixDigits]
                change (F pref : Real) * (10 : Real) ^
                    (d.decimal_point.val + (growth.val : Int) -
                      (capped.val : Int)) = _
                have hexp : d.decimal_point.val + (growth.val : Int) -
                      (m : Int) =
                    (d.decimal_point.val - (d.num_digits.val : Int)) +
                      (suff.length : Int) := by
                  rw [hsuffixLen, hfullLen, hwidthVal]
                  have hmbound : m <= d.num_digits.val + growth.val := by
                    rw [← hwidthVal, ← hfullLen]
                    exact hmWidth
                  rw [Nat.cast_sub hmbound]
                  omega
                rw [hexp, zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
                norm_num only [zpow_natCast]
                have hcastSplit : (B * I : Real) =
                    (F pref : Real) * (10 : Real) ^ suff.length + E := by
                  norm_cast
                  rw [← hfullFold]
                  exact hfoldSplit
                have hBcast : (2 : Real) ^ shift.val = (B : Real) := by
                  norm_cast
                have hINat :
                    List.foldl (fun acc z => 10 * acc + z) 0
                      (List.map (fun z : Std.U8 => z.val)
                        (List.take d.num_digits.val d.digits.val)) = I := by
                  rfl
                rw [hBcast, hINat]
                change (F pref : Real) *
                    ((10 : Real) ^
                      (d.decimal_point.val - (d.num_digits.val : Int)) *
                      (10 : Real) ^ suff.length) =
                  (B : Real) * ((I : Real) *
                    (10 : Real) ^
                      (d.decimal_point.val - (d.num_digits.val : Int))) - rho
                let scale : Real := (10 : Real) ^
                  (d.decimal_point.val - (d.num_digits.val : Int))
                calc
                  (F pref : Real) * (scale * (10 : Real) ^ suff.length) =
                      ((F pref : Real) * (10 : Real) ^ suff.length + E) *
                        scale - (E : Real) * scale := by ring
                  _ = ((B : Real) * I) * scale - (E : Real) * scale := by
                    rw [hcastSplit]
                  _ = (B : Real) * ((I : Real) * scale) - rho := by
                    dsimp only [rho, scale]
                    ring
              · rw [htruncEq]
                change b1 = true <-> _
                rw [hstickyFull, hrhopos, hEpos]


end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalLeftShiftDigitGrowth
import Tablet.DecimalLeftShiftFirstLoopSemantics
import Tablet.DecimalLeftShiftSecondLoopSemantics
import Tablet.DecimalLeftShiftTableCertificate
import Tablet.DecimalSeqTrimSemantics
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift
import Tablet.num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 800000

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqLeftShiftRecordSemantics]
theorem DecimalSeqLeftShiftRecordSemantics :
    let value := fun d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun z => z.val)
      let integer : Nat := digits.foldl (fun a z => 10 * a + z) 0
      (integer : Real) * (10 : Real) ^
        (d.decimal_point.val - (d.num_digits.val : Int))
    forall (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift : Std.Usize),
      shift.val <= 60 ->
      0 < value d ->
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) ->
      num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d' ->
      exists rho : Real,
        0 <= rho /\
        rho < (10 : Real) ^ (d'.decimal_point.val - 768) /\
        value d' = (2 : Real) ^ shift.val * value d - rho /\
        (d'.truncated = true <->
          d.truncated = true \/ 0 < rho) := by
-- BODY
  dsimp only
  intro d d' shift hshift hvalue hdigit hcall
  let xs := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
  let F := fun ys : List Nat => ys.foldl (fun acc z => 10 * acc + z) 0
  let I := F xs
  have hxdigit : forall z, z ∈ xs -> z < 10 := by
    intro z hz
    simp only [xs, List.mem_map] at hz
    rcases hz with ⟨u, hu, rfl⟩
    exact hdigit u hu
  have foldlFrom (ys : List Nat) (acc : Nat) :
      ys.foldl (fun n z => 10 * n + z) acc =
        acc * 10 ^ ys.length + F ys := by
    induction ys generalizing acc with
    | nil => simp [F]
    | cons y ys ih =>
      simp only [List.foldl_cons, List.length_cons]
      rw [ih, pow_succ]
      change (10 * acc + y) * 10 ^ ys.length + F ys =
        acc * (10 ^ ys.length * 10) +
          (y :: ys).foldl (fun n z => 10 * n + z) 0
      simp only [List.foldl_cons]
      rw [ih]
      ring
  have foldBound (ys : List Nat) (hd : forall z, z ∈ ys -> z < 10) :
      F ys < 10 ^ ys.length := by
    induction ys with
    | nil => simp [F]
    | cons y ys ih =>
      have hy : y < 10 := hd y (by simp)
      have hys : forall z, z ∈ ys -> z < 10 := by
        intro z hz
        exact hd z (by simp [hz])
      have hi := ih hys
      rw [show F (y :: ys) = y * 10 ^ ys.length + F ys by
        simpa [F] using foldlFrom ys y, List.length_cons, pow_succ]
      have hmul : y * 10 ^ ys.length <= 9 * 10 ^ ys.length :=
        Nat.mul_le_mul_right _ (by omega)
      calc
        y * 10 ^ ys.length + F ys <
            y * 10 ^ ys.length + 10 ^ ys.length := Nat.add_lt_add_left hi _
        _ <= 9 * 10 ^ ys.length + 10 ^ ys.length :=
          Nat.add_le_add_right hmul _
        _ = 10 ^ ys.length * 10 := by ring
  have foldLowerAt (ys : List Nat) (j : Nat) (hj : j < ys.length)
      (hjpos : 0 < ys[j]) : 10 ^ (ys.length - j - 1) <= F ys := by
    have hprefix : ys[j] <= F (ys.take (j + 1)) := by
      simp only [F]
      rw [List.take_succ, List.foldl_append]
      have hget : ys[j]? = some ys[j] :=
        getElem?_eq_some_iff.mpr ⟨hj, rfl⟩
      simp [hget]
    have hdecomp : F ys =
        F (ys.take (j + 1)) * 10 ^ (ys.length - j - 1) +
          F (ys.drop (j + 1)) := by
      conv_lhs => rw [← List.take_append_drop (j + 1) ys]
      simp only [F, List.foldl_append]
      rw [foldlFrom]
      congr 2
      simp
      omega
    rw [hdecomp]
    have hm := Nat.mul_le_mul_right (10 ^ (ys.length - j - 1)) hprefix
    have hone : 1 <= ys[j] := by omega
    have hp := Nat.mul_le_mul_right (10 ^ (ys.length - j - 1)) hone
    omega
  have decimalUpper (c w t n : Nat) (hc : c < t) (hw : w < 10 ^ n) :
      c * 10 ^ n + w < t * 10 ^ n := by
    have hc1 : c + 1 <= t := by omega
    have hm := Nat.mul_le_mul_right (10 ^ n) hc1
    nlinarith
  have foldPos (ys : List Nat) :
      0 < F ys <-> exists j, exists hj : j < ys.length, 0 < ys[j] := by
    induction ys with
    | nil => simp [F]
    | cons y ys ih =>
      have hform : F (y :: ys) = y * 10 ^ ys.length + F ys := by
        simpa [F] using foldlFrom ys y
      rw [hform]
      constructor
      · intro hp
        by_cases hy : 0 < y
        · exact ⟨0, by simp, hy⟩
        · have htail : 0 < F ys := by
            have : y = 0 := by omega
            simpa [this] using hp
          rcases ih.mp htail with ⟨j, hj, hjpos⟩
          exact ⟨j + 1, by simp; omega, by simpa using hjpos⟩
      · rintro ⟨j, hj, hjpos⟩
        cases j with
        | zero =>
          simp only [List.getElem_cons_zero] at hjpos
          have hpow : 0 < 10 ^ ys.length := by positivity
          nlinarith
        | succ j =>
          have hjlt : j < ys.length := by simp at hj; omega
          have hp : 0 < F ys := ih.mpr ⟨j, hjlt, by simpa using hjpos⟩
          omega
  have zeroGrowth (d0 : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (sh gr : Std.Usize) (hsh : sh.val = 0)
      (hc : num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
        d0 sh = ok gr) : gr.val = 0 := by
    as_aux_lemma =>
    have hseq : sh = 0#usize := UScalar.eq_of_val_eq (by simpa using hsh)
    subst sh
    let kfin : Fin 61 := ⟨0, by omega⟩
    have cert := DecimalLeftShiftTableCertificate kfin
    dsimp only at cert
    rcases cert with ⟨hloidx, hhiidx, hlen, _, hzero, _⟩
    have hz := hzero (by simp [kfin])
    rcases hz with ⟨hhiZero, hdsZero⟩
    let table :=
      num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
    let pow5Table :=
      num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE_POW5
    let x := table.val.get ⟨0, by simp [table]⟩
    let y := table.val.get ⟨1, by simp [table]⟩
    let hi := x.val / 2048
    let lo := x.val % 2048
    let hiNext := y.val % 2048
    let dsU := pow5Table.val.extract lo hiNext
    let ds := dsU.map (fun z => z.val)
    have hloidx' : lo <= hiNext := by
      simpa [lo, hiNext, x, y, table, kfin] using hloidx
    have hhiidx' : hiNext <= 1308 := by
      simpa [hiNext, y, table, kfin] using hhiidx
    have hlen' : ds.length = hiNext - lo := by
      simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table, kfin] using hlen
    have hhiZero' : hi = 0 := by
      simpa [hi, x, table, kfin] using hhiZero
    have hdsZero' : ds = [] := by
      simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table, kfin] using hdsZero
    have hloEq : lo = hiNext := by
      rw [hdsZero'] at hlen'
      simp at hlen'
      omega
    have hmask : (0#usize &&& 63#usize) = 0#usize := by decide
    unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift at hc
    simp only [hmask, lift, Bind.bind, Aeneas.Std.bind] at hc
    obtain ⟨xa, hxa, hxaVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (Array.index_usize_spec
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
        0#usize (by simp [Array.length]))
    rw [hxa] at hc
    simp only [bind_tc_ok] at hc
    have hxaEq : xa = x := by
      apply UScalar.eq_of_val_eq
      simpa [x, table] using congrArg UScalar.val hxaVal
    subst xa
    rw [hxaEq] at hc
    obtain ⟨next, hnext, hnextVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.add_spec (x := 0#usize) (y := 1#usize) (by scalar_tac))
    rw [hnext] at hc
    simp only [bind_tc_ok] at hc
    norm_num at hnextVal
    obtain ⟨yb, hyb, hybVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (Array.index_usize_spec
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
        next (by simp [Array.length]; omega))
    rw [hyb] at hc
    simp only [bind_tc_ok] at hc
    have hybEq : yb = y := by
      apply UScalar.eq_of_val_eq
      simpa [y, table, hnextVal] using congrArg UScalar.val hybVal
    subst yb
    rw [hybEq] at hc
    obtain ⟨high16, hhigh16, hhigh16Val⟩ := Aeneas.Std.WP.spec_imp_exists
      (U16.ShiftRight_IScalar_spec x 11#i32 (by norm_num) (by norm_num))
    rw [hhigh16] at hc
    simp only [bind_tc_ok] at hc
    have hhigh16Eq : high16.val = hi := by
      rw [hhigh16Val.1, Nat.shiftRight_eq_div_pow]
      norm_num [hi, Int.toNat]
    let numNew : Std.Usize := UScalar.cast .Usize high16
    have hnumNewVal : numNew.val = 0 := by
      simp [numNew, hhigh16Eq, hhiZero']
    let low16 := 2047#u16 &&& x
    let pow5a : Std.Usize := UScalar.cast .Usize low16
    have hpow5aVal : pow5a.val = lo := by
      simp [pow5a, low16, lo, UScalar.val_and]
      rw [show 2047 = 2 ^ 11 - 1 by decide, Nat.and_comm,
        Nat.and_two_pow_sub_one_eq_mod]
    let next16 := 2047#u16 &&& y
    let pow5b : Std.Usize := UScalar.cast .Usize next16
    have hpow5bVal : pow5b.val = hiNext := by
      simp [pow5b, next16, hiNext, UScalar.val_and]
      rw [show 2047 = 2 ^ 11 - 1 by decide, Nat.and_comm,
        Nat.and_two_pow_sub_one_eq_mod]
    rw [show UScalar.cast .Usize (2047#u16 &&& x) = pow5a by rfl] at hc
    rw [show UScalar.cast .Usize (2047#u16 &&& y) = pow5b by rfl] at hc
    rw [show UScalar.cast .Usize high16 = numNew by rfl] at hc
    have hlo1308 : pow5a.val <= 1308 := by
      rw [hpow5aVal]
      exact hloidx'.trans hhiidx'
    let slice : Slice Std.U8 := pow5Table.to_slice.drop pow5a
    have hslice :
        core.array.Array.index
          (core.ops.index.IndexSlice
            (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8))
          pow5Table { start := pow5a } = ok slice := by
      rw [Array.index_SliceIndexRangeFromUsizeSlice]
      unfold core.slice.index.SliceIndexRangeFromUsizeSlice.index
      simp [hlo1308, slice]
    rw [hslice] at hc
    simp only [bind_tc_ok] at hc
    unfold core.slice.Slice.iter at hc
    simp only [bind_tc_ok] at hc
    unfold core.slice.iter.IteratorSliceIter.enumerate at hc
    simp only [bind_tc_ok] at hc
    have hsubBound : pow5a.val <= pow5b.val := by
      rw [hpow5aVal, hpow5bVal]
      exact hloidx'
    obtain ⟨span, hspan, hspanVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.sub_spec (x := pow5b) (y := pow5a) hsubBound)
    rw [hspan] at hc
    simp only [bind_tc_ok] at hc
    unfold core.iter.adapters.enumerate.IteratorEnumerate.take at hc
    simp only [bind_tc_ok] at hc
    have hspanZero : span.val = 0 := by
      rw [hspanVal.1, hpow5aVal, hpow5bVal, ← hloEq]
      simp
    have hspanEq : span = 0#usize :=
      UScalar.eq_of_val_eq (by simpa using hspanZero)
    subst span
    unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift_loop at hc
    rw [Aeneas.Std.loop.eq_def] at hc
    unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift_loop.body at hc
    unfold core.iter.adapters.take.IteratorTake.next at hc
    simp at hc
    have hgrEq : gr = numNew := hc.symm
    rw [hgrEq]
    exact hnumNewVal
  have hIpos : 0 < I := by
    by_contra hn
    have hIz : I = 0 := by omega
    simp only [xs, I, F] at hIz
    rw [hIz] at hvalue
    simp at hvalue
  have hnpos : 0 < d.num_digits.val := by
    by_contra hn
    have hnz : d.num_digits.val = 0 := by omega
    simp [xs, I, F, hnz] at hIpos
  have hnne : d.num_digits ≠ 0#usize := by
    intro h
    have : d.num_digits.val = 0 := by simpa [h]
    omega
  unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift at hcall
  simp only [hnne, if_false] at hcall
  cases hgrowth :
      num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift d shift with
  | fail e => simp [hgrowth] at hcall
  | div => simp [hgrowth] at hcall
  | ok growth =>
    simp only [hgrowth, bind_tc_ok] at hcall
    cases hadd : d.num_digits + growth with
    | fail e => simp [hadd] at hcall
    | div => simp [hadd] at hcall
    | ok width =>
      simp only [hadd, bind_tc_ok] at hcall
      cases hloop0 :
          num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
            d.truncated d.digits shift d.num_digits width 0#u64 with
      | fail e => simp [hloop0] at hcall
      | div => simp [hloop0] at hcall
      | ok out0 =>
        rcases out0 with ⟨b0, a0, cursor, carry⟩
        simp only [hloop0, bind_tc_ok] at hcall
        have hnle : d.num_digits.val <= 768 := by
          by_contra hnle
          have hbad : 768 < d.num_digits.val := by omega
          unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0 at hloop0
          simp only [Bind.bind, Aeneas.Std.bind] at hloop0
          rw [Aeneas.Std.loop.eq_def] at hloop0
          unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body at hloop0
          have hrne : d.num_digits != 0#usize := by
            simp only [bne_iff_ne]
            exact hnne
          simp only [hrne, if_true] at hloop0
          obtain ⟨read1, hread1, hread1Val⟩ := Aeneas.Std.WP.spec_imp_exists
            (UScalar.sub_spec (x := d.num_digits) (y := 1#usize)
              (by simpa using hnpos))
          norm_num at hread1Val
          rw [hread1] at hloop0
          simp only [bind_tc_ok] at hloop0
          cases hwrite : width - 1#usize with
          | fail e => simp [hwrite] at hloop0
          | div => simp [hwrite] at hloop0
          | ok write1 =>
            rw [hwrite] at hloop0
            simp only [bind_tc_ok] at hloop0
            have hread1bad : d.digits.val.length <= read1.val := by
              have halen : d.digits.val.length = 768 := by
                simpa using d.digits.property
              rw [halen, hread1Val.1]
              omega
            have hnone : d.digits.val[read1.val]? = none :=
              List.getElem?_eq_none hread1bad
            unfold Array.index_usize at hloop0
            simp [hnone] at hloop0
        have hxslen : xs.length = d.num_digits.val := by
          simp [xs, hnle, d.digits.property]
        have hIbound : I < 10 ^ d.num_digits.val := by
          simpa [I, hxslen] using foldBound xs hxdigit
        cases hloop1 :
            num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
              b0 a0 cursor carry with
        | fail e => simp [hloop1] at hcall
        | div => simp [hloop1] at hcall
        | ok out1 =>
          rcases out1 with ⟨b1, a1⟩
          let B := 2 ^ shift.val
          by_cases hk0 : shift.val = 0
          · have hgrowthZero : growth.val = 0 :=
              zeroGrowth d shift growth hk0 hgrowth
            have haddBound : d.num_digits.val + growth.val <=
                UScalar.max .Usize := by
              scalar_tac
            obtain ⟨width', hwidth', hwidthVal⟩ :=
              Aeneas.Std.WP.spec_imp_exists
                (UScalar.add_spec (x := d.num_digits) (y := growth) haddBound)
            rw [hadd] at hwidth'
            cases hwidth'
            have hwidthEq : width.val = d.num_digits.val := by
              rw [hwidthVal, hgrowthZero]
              simp
            have hcursorInput : d.num_digits.val <= width.val := by
              rw [hwidthEq]
            obtain ⟨lowDigits, hlowLen, hlowDigit, hcursorEq, hcarryB,
                hproduct, hlowArray, houtside0, hsticky0⟩ :=
              DecimalLeftShiftFirstLoopSemantics d.digits a0 d.truncated b0
                shift d.num_digits width cursor carry hshift hnle hcursorInput
                hdigit hloop0
            have hcursorZero : cursor.val = 0 := by
              rw [hwidthEq] at hcursorEq
              omega
            have hcarryZero : carry.val = 0 := by
              rw [hk0] at hcarryB
              norm_num at hcarryB
              omega
            have hcarryWidth : carry.val < 10 ^ cursor.val := by
              rw [hcarryZero, hcursorZero]
              norm_num
            have hpad : forall j, j < cursor.val -> j < 768 ->
                carry.val < 10 ^ (cursor.val - j - 1) ->
                  a0.val[j]? = some 0#u8 := by
              intro j hj
              rw [hcursorZero] at hj
              omega
            obtain ⟨highDigits, hhighLen, hhighDigit, hcarryDigits,
                hhighArray, houtside1, hsticky1⟩ :=
              DecimalLeftShiftSecondLoopSemantics a0 a1 b0 b1 cursor carry
                hcarryWidth hpad hloop1
            have hhighNil : highDigits = [] := by
              apply List.eq_nil_of_length_eq_zero
              rw [hhighLen, hcursorZero]
            have hb10 : b1 = true <-> b0 = true := by
              rw [hsticky1]
              constructor
              · intro h
                rcases h with h | ⟨j, hj, _, _⟩
                · exact h
                · rw [hhighNil] at hj
                  simp at hj
              · exact Or.inl
            have hb0 : b0 = true <-> d.truncated = true := by
              rw [hsticky0]
              constructor
              · intro h
                rcases h with h | ⟨j, hj, hjcap, _⟩
                · exact h
                · rw [hlowLen] at hj
                  rw [hcursorZero] at hjcap
                  omega
              · exact Or.inl
            have hsticky : b1 = true <-> d.truncated = true := by
              rw [hb10, hb0]
            have hlowFold : F lowDigits = I := by
              simpa [hk0, hcarryZero, F, I, xs] using hproduct.symm
            have hlowArray1 : forall j, j < lowDigits.length -> j < 768 ->
                (a1.val.map (fun z : Std.U8 => z.val))[j]? = lowDigits[j]? := by
              intro j hj hjphys
              have hout := houtside1 j hjphys (by
                rw [hcursorZero]
                exact Nat.zero_le j)
              have hlo := hlowArray j hj (by
                rw [hcursorZero]
                simpa using hjphys)
              rw [List.getElem?_map, hout]
              simpa [hcursorZero] using hlo
            have hwidthLe : width.val <= 768 := by
              rw [hwidthEq]
              exact hnle
            have hcapFalse : ¬ width >
                num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
              simpa [gt_iff_lt, UScalar.lt_equiv,
                num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]
                using (show ¬ 768 < width.val by omega)
            have hcapEval :
                (if width > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                  then ok num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                  else ok width) = ok width := by
              simp [hcapFalse]
            change (do
              let (bc, ac) ←
                num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
                  b0 a0 cursor carry
              let count ←
                if width > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                then ok num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                else ok width
              let delta ← lift (UScalar.hcast .I32 growth)
              let point ← d.decimal_point + delta
              num.imp.dec2flt.decimal_seq.DecimalSeq.trim
                { num_digits := count, decimal_point := point,
                  truncated := bc, digits := ac }) = ok d' at hcall
            rw [hloop1] at hcall
            simp only [bind_tc_ok] at hcall
            rw [hcapEval] at hcall
            simp only [bind_tc_ok, lift] at hcall
            let growthI : Std.I32 := UScalar.hcast .I32 growth
            rw [show UScalar.hcast .I32 growth = growthI by rfl] at hcall
            have hgrowthIVal : growthI.val = (growth.val : Int) := by
              have hs := UScalar.hcast_inBounds_spec .I32 growth (by
                rw [IScalar.max_I32_eq]
                omega)
              simp only [lift, WP.spec_ok] at hs
              exact hs
            have hgrowthIZero : growthI.val = 0 := by
              rw [hgrowthIVal, hgrowthZero]
              norm_num
            cases hpoint : d.decimal_point + growthI with
            | fail e => simp [hpoint] at hcall
            | div => simp [hpoint] at hcall
            | ok point =>
              rw [hpoint] at hcall
              simp only [bind_tc_ok] at hcall
              let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
                { num_digits := width, decimal_point := point,
                  truncated := b1, digits := a1 }
              have htrim :
                  num.imp.dec2flt.decimal_seq.DecimalSeq.trim pre = ok d' := by
                simpa [pre] using hcall
              have hpointVal : point.val = d.decimal_point.val := by
                have hs := IScalar.add_equiv d.decimal_point growthI
                rw [hpoint] at hs
                rw [hgrowthIZero] at hs
                omega
              have hpreBound : pre.num_digits.val <= pre.digits.val.length := by
                simp [pre, a1.property]
                exact hwidthLe
              have htrimSem := DecimalSeqTrimSemantics pre d' hpreBound htrim
              dsimp only at htrimSem
              rcases htrimSem with
                ⟨hdigitsEq, hpointEq, htruncEq, _, _, _, hvalueTrim⟩
              have hpreDigits :
                  (a1.val.take width.val).map (fun z => z.val) = lowDigits := by
                apply List.ext_getElem
                · simp [hwidthEq, hlowLen, hnle, a1.property]
                · intro i hleft hright
                  have hi : i < lowDigits.length := hright
                  have hi768 : i < 768 := by
                    rw [hlowLen] at hi
                    omega
                  have ha := hlowArray1 i hi hi768
                  have haSome :
                      (a1.val.map (fun z : Std.U8 => z.val))[i]? =
                        some lowDigits[i] := by
                    rw [ha, List.getElem?_eq_getElem hi]
                  have hmapBound :
                      i < (a1.val.map (fun z : Std.U8 => z.val)).length := by
                    simp [a1.property]
                    exact hi768
                  rw [show ((a1.val.take width.val).map (fun z => z.val))[i] =
                      (a1.val.map (fun z => z.val))[i] by simp]
                  have hsome := List.getElem?_eq_getElem hmapBound
                  rw [haSome] at hsome
                  exact Option.some.inj hsome.symm
              refine ⟨0, by norm_num, zpow_pos (by norm_num) _, ?_, ?_⟩
              · rw [hvalueTrim]
                simp only [pre, hpointVal]
                rw [hpreDigits, hwidthEq]
                change (F lowDigits : Real) * (10 : Real) ^
                    (d.decimal_point.val - (d.num_digits.val : Int)) =
                  (2 : Real) ^ shift.val *
                    ((I : Real) * (10 : Real) ^
                      (d.decimal_point.val - (d.num_digits.val : Int))) - 0
                rw [hlowFold, hk0]
                norm_num
              · rw [htruncEq]
                simpa using hsticky
          · have hkpos : 0 < shift.val := by omega
            let kfin : Fin 61 := ⟨shift.val, by omega⟩
            have cert := DecimalLeftShiftTableCertificate kfin
            dsimp only at cert
            rcases cert with ⟨_, _, _, _, _, hpositive⟩
            have hp := hpositive (by simpa [kfin] using hkpos)
            rcases hp with ⟨_, _, _, hsum, hlo, hhi⟩
            let table :=
              num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
            let x := table.val.get ⟨shift.val, by
              have hlen : table.val.length = 65 := by simp [table]
              omega⟩
            let h := x.val / 2048
            have hlo' : 10 ^ (h - 1) < B := by
              simpa [h, x, table, kfin, B] using hlo
            have hhi' : B < 10 ^ h := by
              simpa [h, x, table, kfin, B] using hhi
            have hhpos : 0 < h := by
              by_contra hz
              have : h = 0 := by omega
              have hBpos : 0 < B := by simp [B]
              rw [this] at hhi'
              norm_num at hhi'
              omega
            have hhle : h <= 19 := by
              by_contra hn
              have h20 : 20 <= h := by omega
              have hp19 : 10 ^ 19 <= 10 ^ (h - 1) :=
                Nat.pow_le_pow_right (by omega) (by omega)
              have hBle : B <= 2 ^ 60 := by
                exact Nat.pow_le_pow_right (by omega) hshift
              have hnum : 2 ^ 60 < 10 ^ 19 := by norm_num
              exact (not_lt_of_ge hp19)
                (lt_of_lt_of_le hlo' (le_trans hBle (Nat.le_of_lt hnum)))
            obtain ⟨growth', hgrowth', hgrowthVal⟩ :=
              DecimalLeftShiftDigitGrowth d shift h hshift hlo' hhi' hnle hdigit
            rw [hgrowth] at hgrowth'
            cases hgrowth'
            have hgrowthFormula : growth.val =
                if 10 ^ (d.num_digits.val + h - 1) <= I * B
                then h else h - 1 := by
              simpa [xs, I, F, B] using hgrowthVal
            have hgrowthLe : growth.val <= 19 := by
              rw [hgrowthFormula]
              split <;> omega
            have haddBound : d.num_digits.val + growth.val <=
                UScalar.max .Usize := by
              scalar_tac
            obtain ⟨width', hwidth', hwidthVal⟩ := Aeneas.Std.WP.spec_imp_exists
              (UScalar.add_spec (x := d.num_digits) (y := growth) haddBound)
            rw [hadd] at hwidth'
            cases hwidth'
            have hcursorInput : d.num_digits.val <= width.val := by
              rw [hwidthVal]
              omega
            obtain ⟨lowDigits, hlowLen, hlowDigit, hcursorEq, hcarryB,
                hproduct, hlowArray, houtside0, hsticky0⟩ :=
              DecimalLeftShiftFirstLoopSemantics d.digits a0 d.truncated b0
                shift d.num_digits width cursor carry hshift hnle hcursorInput
                hdigit hloop0
            have hcursorVal : cursor.val = growth.val := by
              rw [hwidthVal] at hcursorEq
              omega
            have hprodWidth : B * I <
                10 ^ (d.num_digits.val + growth.val) := by
              rw [hgrowthFormula]
              split
              next ht =>
                have hBpos : 0 < B := by simp [B]
                have hpowpos : 0 < 10 ^ d.num_digits.val := by positivity
                have hmul1 : B * I < B * 10 ^ d.num_digits.val :=
                  (Nat.mul_lt_mul_left hBpos).mpr hIbound
                have hmul2 : B * 10 ^ d.num_digits.val <
                    10 ^ h * 10 ^ d.num_digits.val :=
                  (Nat.mul_lt_mul_right hpowpos).mpr hhi'
                have hmul := lt_trans hmul1 hmul2
                rw [Nat.mul_comm (10 ^ h), ← Nat.pow_add] at hmul
                simpa using hmul
              next ht =>
                have hlt : I * B < 10 ^ (d.num_digits.val + h - 1) := by
                  omega
                rw [Nat.mul_comm]
                simpa only [show d.num_digits.val + h - 1 =
                  d.num_digits.val + (h - 1) by omega] using hlt
            have hcarryWidth : carry.val < 10 ^ cursor.val := by
              rw [hcursorVal]
              have hmul : carry.val * 10 ^ d.num_digits.val <
                  10 ^ growth.val * 10 ^ d.num_digits.val := by
                have hcle : carry.val * 10 ^ d.num_digits.val <= B * I := by
                  rw [hproduct]
                  omega
                rw [Nat.mul_comm (10 ^ growth.val) (10 ^ d.num_digits.val),
                  ← Nat.pow_add]
                exact lt_of_le_of_lt hcle hprodWidth
              exact Nat.lt_of_mul_lt_mul_right hmul
            have hpad : forall j, j < cursor.val -> j < 768 ->
                carry.val < 10 ^ (cursor.val - j - 1) ->
                  a0.val[j]? = some 0#u8 := by
              as_aux_lemma =>
              intro j hjcursor hjphys hjcarry
              have hbase : a0.val[j]? = d.digits.val[j]? :=
                houtside0 j hjphys (Or.inl hjcursor)
              rw [hgrowthFormula] at hcursorVal
              split at hcursorVal
              next ht =>
                have hexp : cursor.val - j - 1 <= h - 1 := by omega
                have hpow : 10 ^ (cursor.val - j - 1) <= 10 ^ (h - 1) :=
                  Nat.pow_le_pow_right (by omega) hexp
                have hcTop : carry.val < 10 ^ (h - 1) :=
                  lt_of_lt_of_le hjcarry hpow
                have hu := decimalUpper carry.val (F lowDigits)
                  (10 ^ (h - 1)) d.num_digits.val hcTop (by
                    simpa [hlowLen] using foldBound lowDigits hlowDigit)
                rw [← hproduct] at hu
                rw [Nat.mul_comm (10 ^ (h - 1)), ← Nat.pow_add] at hu
                change B * I < 10 ^ (d.num_digits.val + (h - 1)) at hu
                have hthreshold : 10 ^ (d.num_digits.val + h - 1) <= B * I := by
                  simpa [Nat.mul_comm] using ht
                have he : d.num_digits.val + (h - 1) =
                    d.num_digits.val + h - 1 := by omega
                rw [he] at hu
                exact False.elim ((not_lt_of_ge hthreshold) hu)
              next ht =>
                have hcursorGrowth : cursor.val = h - 1 := hcursorVal
                have hlowBound : F lowDigits < 10 ^ d.num_digits.val := by
                  simpa [hlowLen] using foldBound lowDigits hlowDigit
                have hu := decimalUpper carry.val (F lowDigits)
                  (10 ^ (cursor.val - j - 1)) d.num_digits.val
                  hjcarry hlowBound
                rw [← hproduct] at hu
                rw [Nat.mul_comm (10 ^ (cursor.val - j - 1)),
                  ← Nat.pow_add] at hu
                change B * I <
                  10 ^ (d.num_digits.val + (cursor.val - j - 1)) at hu
                have hjN : j < d.num_digits.val := by
                  by_contra hjN
                  have hNle : d.num_digits.val <= j := by omega
                  have hexp : d.num_digits.val + (cursor.val - j - 1) <=
                      cursor.val - 1 := by omega
                  have hpow : 10 ^ (d.num_digits.val + (cursor.val - j - 1)) <=
                      10 ^ (cursor.val - 1) :=
                    Nat.pow_le_pow_right (by omega) hexp
                  have hcursorPos : 0 < cursor.val := by omega
                  have hpowStep : 10 ^ (cursor.val - 1) < 10 ^ cursor.val := by
                    apply Nat.pow_lt_pow_right (by omega)
                    omega
                  have hBLower : 10 ^ cursor.val < B := by
                    simpa [hcursorGrowth] using hlo'
                  have hBI : B <= B * I := by
                    have hm := Nat.mul_le_mul_left B (show 1 <= I by omega)
                    simpa using hm
                  exact (not_lt_of_ge hBI)
                    (lt_of_lt_of_le hu (le_trans hpow
                      (le_trans (Nat.le_of_lt hpowStep) (Nat.le_of_lt hBLower))))
                have hcellBound : j < d.digits.val.length := by
                  have hlen : d.digits.val.length = 768 := by
                    simpa using d.digits.property
                  rw [hlen]
                  exact hjphys
                have hxval : xs[j] = (d.digits.val[j]'hcellBound).val := by
                  simp [xs, hjN]
                have hxzero : xs[j] = 0 := by
                  by_contra hxzero
                  have hxpos : 0 < xs[j] := by omega
                  have hIlower := foldLowerAt xs j (by simpa [hxslen] using hjN) hxpos
                  have hIlower' : 10 ^ (d.num_digits.val - j - 1) <= I := by
                    simpa [hxslen, I] using hIlower
                  have hBLower : 10 ^ cursor.val < B := by
                    simpa [hcursorGrowth] using hlo'
                  have hmul1 : 10 ^ cursor.val *
                        10 ^ (d.num_digits.val - j - 1) <
                      B * 10 ^ (d.num_digits.val - j - 1) :=
                    (Nat.mul_lt_mul_right (by positivity)).mpr hBLower
                  have hmul2 : B * 10 ^ (d.num_digits.val - j - 1) <= B * I :=
                    Nat.mul_le_mul_left B hIlower'
                  have hlower : 10 ^ (cursor.val +
                        (d.num_digits.val - j - 1)) < B * I := by
                    rw [Nat.pow_add]
                    exact lt_of_lt_of_le hmul1 hmul2
                  have he : cursor.val + (d.num_digits.val - j - 1) =
                      d.num_digits.val + (cursor.val - j - 1) := by omega
                  rw [he] at hlower
                  exact False.elim ((not_lt_of_ge (Nat.le_of_lt hlower)) hu)
                have hdigitZero : d.digits.val[j]'hcellBound = 0#u8 := by
                  apply UScalar.eq_of_val_eq
                  simpa [← hxval, hxzero]
                have hbaseZero : d.digits.val[j]? = some 0#u8 := by
                  apply getElem?_eq_some_iff.mpr
                  exact ⟨hcellBound, hdigitZero⟩
                simpa [hbase] using hbaseZero
            obtain ⟨highDigits, hhighLen, hhighDigit, hcarryDigits,
                hhighArray, houtside1, hsticky1⟩ :=
              DecimalLeftShiftSecondLoopSemantics a0 a1 b0 b1 cursor carry
                hcarryWidth hpad hloop1
            let fullDigits := highDigits ++ lowDigits
            have hfullLen : fullDigits.length = width.val := by
              simp [fullDigits, hhighLen, hlowLen, hcursorEq]
            have hfullDigit : forall z, z ∈ fullDigits -> z < 10 := by
              intro z hz
              simp only [fullDigits, List.mem_append] at hz
              exact hz.elim (hhighDigit z) (hlowDigit z)
            have hfullFold : F fullDigits = B * I := by
              have happ : F fullDigits =
                  carry.val * 10 ^ lowDigits.length + F lowDigits := by
                simp only [fullDigits, F, List.foldl_append]
                rw [foldlFrom, hcarryDigits]
              rw [happ, hlowLen]
              simpa [B, I, F, xs] using hproduct.symm
            have hfullArray : forall j, j < fullDigits.length -> j < 768 ->
                (a1.val.map (fun z : Std.U8 => z.val))[j]? = fullDigits[j]? := by
              intro j hj hjphys
              by_cases hjhigh : j < cursor.val
              · have hh := hhighArray j (by simpa [hhighLen] using hjhigh) hjphys
                rw [List.getElem?_append_left (by simpa [fullDigits, hhighLen] using hjhigh)]
                simpa [fullDigits] using hh
              · have hcj : cursor.val <= j := by omega
                let t := j - cursor.val
                have ht : t < lowDigits.length := by
                  simp [fullDigits, hhighLen, hlowLen, hfullLen] at hj
                  simp [t]
                  omega
                have hidx : cursor.val + t = j := by simp [t, hcj]
                have hout := houtside1 j hjphys hcj
                have hlo := hlowArray t ht (by omega)
                rw [List.getElem?_map, hout]
                rw [List.getElem?_append_right (by
                  simpa [fullDigits, hhighLen] using hjhigh)]
                simpa [fullDigits, hhighLen, t, hidx] using hlo
            have hb10 : b1 = true <-> b0 = true := by
              rw [hsticky1]
              constructor
              · intro h
                rcases h with h | ⟨j, hj, hjcap, _⟩
                · exact h
                · rw [hhighLen, hcursorVal] at hj
                  omega
              · exact Or.inl
            have hstickyFull : b1 = true <-> d.truncated = true \/
                exists j, j < fullDigits.length /\ 768 <= j /\
                  fullDigits[j]? != some 0 := by
              rw [hb10, hsticky0]
              constructor
              · intro h
                rcases h with hd | ⟨j, hj, hjcap, hjnz⟩
                · exact Or.inl hd
                · right
                  refine ⟨cursor.val + j, ?_, hjcap, ?_⟩
                  · simp [fullDigits, hhighLen, hlowLen]
                    omega
                  · rw [List.getElem?_append_right (by simp [hhighLen])]
                    simpa [hhighLen] using hjnz
              · intro h
                rcases h with hd | ⟨j, hj, hjcap, hjnz⟩
                · exact Or.inl hd
                · right
                  have hcj : cursor.val <= j := by
                    by_contra hcj
                    rw [List.getElem?_append_left (by
                      simpa [fullDigits, hhighLen] using hcj)] at hjnz
                    have hjlt : j < highDigits.length := by simpa [hhighLen] using hcj
                    have hjcap' : j < 768 := by omega
                    have hz := hhighDigit highDigits[j] (List.getElem_mem _)
                    omega
                  refine ⟨j - cursor.val, ?_, ?_, ?_⟩
                  · simp [fullDigits, hhighLen] at hj
                    omega
                  · omega
                  · rw [List.getElem?_append_right (by
                      simpa [fullDigits, hhighLen] using hcj)] at hjnz
                    simpa [hhighLen] using hjnz
            let capped : Std.Usize :=
              if width.val > 768 then
                num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS else width
            have hcappedVal : capped.val = min width.val 768 := by
              simp only [capped]
              split
              · simp [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]
                omega
              · simp
                omega
            have hcapEval :
                (if width > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                  then ok num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                  else ok width) = ok capped := by
              simp only [capped]
              by_cases hc : width.val > 768
              · have hc' : width >
                    num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
                  simpa [gt_iff_lt, UScalar.lt_equiv,
                    num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]
                    using hc
                simp [hc', hc]
              · have hc' : ¬ width >
                    num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
                  simpa [gt_iff_lt, UScalar.lt_equiv,
                    num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]
                    using hc
                simp [hc', hc]
            change (do
              let (bc, ac) ←
                num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
                  b0 a0 cursor carry
              let count ←
                if width > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                then ok num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                else ok width
              let delta ← lift (UScalar.hcast .I32 growth)
              let point ← d.decimal_point + delta
              num.imp.dec2flt.decimal_seq.DecimalSeq.trim
                { num_digits := count, decimal_point := point,
                  truncated := bc, digits := ac }) = ok d' at hcall
            rw [hloop1] at hcall
            simp only [bind_tc_ok] at hcall
            rw [hcapEval] at hcall
            simp only [bind_tc_ok, lift] at hcall
            let growthI : Std.I32 := UScalar.hcast .I32 growth
            rw [show UScalar.hcast .I32 growth = growthI by rfl] at hcall
            have hgrowthI : growthI.val = (growth.val : Int) := by
              have hs := UScalar.hcast_inBounds_spec .I32 growth (by
                rw [IScalar.max_I32_eq]
                omega)
              simp only [lift, WP.spec_ok] at hs
              exact hs
            cases hpoint : d.decimal_point + growthI with
            | fail e => simp [hpoint] at hcall
            | div => simp [hpoint] at hcall
            | ok point =>
              rw [hpoint] at hcall
              simp only [bind_tc_ok] at hcall
              let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
                { num_digits := capped, decimal_point := point,
                  truncated := b1, digits := a1 }
              have htrim :
                  num.imp.dec2flt.decimal_seq.DecimalSeq.trim pre = ok d' := by
                simpa [pre] using hcall
              have hpointVal : point.val = d.decimal_point.val + growth.val := by
                have hs := IScalar.add_equiv d.decimal_point growthI
                rw [hpoint] at hs
                rw [hgrowthI] at hs
                omega
              have hpreBound : pre.num_digits.val <= pre.digits.val.length := by
                simp [pre, hcappedVal, a1.property]
              have htrimSem := DecimalSeqTrimSemantics pre d' hpreBound htrim
              dsimp only at htrimSem
              rcases htrimSem with
                ⟨hdigitsEq, hpointEq, htruncEq, _, _, _, hvalueTrim⟩
              let m := capped.val
              let pref := fullDigits.take m
              let suff := fullDigits.drop m
              let E := F suff
              have hmWidth : m <= fullDigits.length := by
                simp [m, hcappedVal, hfullLen]
              have hm768 : m <= 768 := by
                simp [m, hcappedVal]
              have hprefixDigits :
                  (a1.val.take m).map (fun z => z.val) = pref := by
                apply List.ext_getElem
                · simp [pref, hmWidth, m, a1.property, hm768]
                · intro i hleft hright
                  have him : i < m := by
                    simpa [m, a1.property, hm768] using hleft
                  have hifull : i < fullDigits.length := lt_of_lt_of_le him hmWidth
                  have hi768 : i < 768 := lt_of_lt_of_le him hm768
                  have ha := hfullArray i hifull hi768
                  have haSome :
                      (a1.val.map (fun z : Std.U8 => z.val))[i]? =
                        some fullDigits[i] := by
                    rw [ha, List.getElem?_eq_getElem hifull]
                  have hmapBound : i < (a1.val.map (fun z : Std.U8 => z.val)).length := by
                    simp [a1.property]
                    exact hi768
                  rw [show ((a1.val.take m).map (fun z => z.val))[i] =
                      (a1.val.map (fun z => z.val))[i] by simp]
                  rw [show pref[i] = fullDigits[i] by simp [pref]]
                  have hsome := List.getElem?_eq_getElem hmapBound
                  rw [haSome] at hsome
                  exact Option.some.inj hsome.symm
              have hsuffixLen : suff.length = fullDigits.length - m := by
                simp [suff]
              have hfoldSplit : F fullDigits =
                  F pref * 10 ^ suff.length + E := by
                conv_lhs => rw [← List.take_append_drop m fullDigits]
                simp only [pref, suff, E, F, List.foldl_append]
                rw [foldlFrom]
              have hEpos : 0 < E <->
                  exists j, j < fullDigits.length /\ 768 <= j /\
                    fullDigits[j]? != some 0 := by
                constructor
                · intro hE
                  rcases (foldPos suff).mp hE with ⟨t, ht, htpos⟩
                  have hmLt : m < fullDigits.length := by
                    simp [suff] at ht
                    omega
                  have hmEq : m = 768 := by
                    simp [m, hcappedVal] at hmLt ⊢
                    omega
                  refine ⟨m + t, ?_, by omega, ?_⟩
                  · simp [suff] at ht
                    omega
                  · have hidx : suff[t] = fullDigits[m + t] := by
                      exact List.getElem_drop
                    have hbound : m + t < fullDigits.length := by
                      simp [suff] at ht
                      omega
                    have hsome := List.getElem?_eq_getElem hbound
                    rw [hsome]
                    simp only [bne_iff_ne]
                    intro hz
                    have hzval : fullDigits[m + t] = 0 := Option.some.inj hz
                    rw [← hidx] at hzval
                    omega
                · rintro ⟨j, hj, hjcap, hjnz⟩
                  have hmEq : m = 768 := by
                    simp [m, hcappedVal, hfullLen]
                    omega
                  have ht : j - m < suff.length := by
                    simp [suff]
                    omega
                  apply (foldPos suff).mpr
                  refine ⟨j - m, ht, ?_⟩
                  have hidx : suff[j - m] = fullDigits[j] := by
                    rw [List.getElem_drop]
                    congr
                    omega
                  rw [hidx]
                  have hsome := List.getElem?_eq_getElem hj
                  by_contra hz
                  have hz' : fullDigits[j] = 0 := by omega
                  rw [hsome, hz'] at hjnz
                  simp at hjnz
              let rho : Real := (E : Real) * (10 : Real) ^
                (d.decimal_point.val - (d.num_digits.val : Int))
              have hrho0 : 0 <= rho := by
                dsimp only [rho]
                exact mul_nonneg (Nat.cast_nonneg E)
                  (le_of_lt (zpow_pos (by norm_num : (0 : Real) < 10) _))
              have hrhopos : 0 < rho <-> 0 < E := by
                dsimp only [rho]
                have hscale : 0 < (10 : Real) ^
                    (d.decimal_point.val - (d.num_digits.val : Int)) :=
                  zpow_pos (by norm_num) _
                constructor
                · intro hr
                  have hcast : 0 < (E : Real) := by nlinarith
                  exact_mod_cast hcast
                · intro hE
                  have hcast : 0 < (E : Real) := by exact_mod_cast hE
                  exact mul_pos hcast hscale
              have hsuffDigit : forall z, z ∈ suff -> z < 10 := by
                intro z hz
                exact hfullDigit z (List.mem_of_mem_drop hz)
              have hEbound : E < 10 ^ suff.length := by
                simpa [E] using foldBound suff hsuffDigit
              have hrhoCell :
                  rho < (10 : Real) ^ (d'.decimal_point.val - 768) := by
                by_cases hEz : E = 0
                · simp only [rho, hEz, Nat.cast_zero, zero_mul]
                  exact zpow_pos (by norm_num) _
                · have hEposNat : 0 < E := Nat.pos_of_ne_zero hEz
                  rcases (foldPos suff).mp hEposNat with ⟨t, ht, _⟩
                  have hmLt : m < fullDigits.length := by
                    simp [suff] at ht
                    omega
                  have hmEq : m = 768 := by
                    simp [m, hcappedVal, hfullLen] at hmLt ⊢
                    omega
                  have hexpCell :
                      d.decimal_point.val - (d.num_digits.val : Int) +
                          (suff.length : Int) =
                        d'.decimal_point.val - 768 := by
                    rw [hpointEq]
                    simp only [pre]
                    rw [hpointVal, hsuffixLen, hfullLen, hwidthVal]
                    have hmBound : m <= d.num_digits.val + growth.val := by
                      rw [← hwidthVal, ← hfullLen]
                      exact hmWidth
                    rw [Nat.cast_sub hmBound, hmEq]
                    omega
                  have hcastBound :
                      (E : Real) < (10 : Real) ^ suff.length := by
                    exact_mod_cast hEbound
                  have hscalePos : 0 < (10 : Real) ^
                      (d.decimal_point.val - (d.num_digits.val : Int)) :=
                    zpow_pos (by norm_num) _
                  calc
                    rho = (E : Real) * (10 : Real) ^
                        (d.decimal_point.val - (d.num_digits.val : Int)) := rfl
                    _ < (10 : Real) ^ suff.length * (10 : Real) ^
                        (d.decimal_point.val - (d.num_digits.val : Int)) :=
                      mul_lt_mul_of_pos_right hcastBound hscalePos
                    _ = (10 : Real) ^ (d'.decimal_point.val - 768) := by
                      norm_num only [← zpow_natCast]
                      rw [← zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
                      rw [add_comm, hexpCell]
              refine ⟨rho, hrho0, hrhoCell, ?_, ?_⟩
              · rw [hvalueTrim]
                simp only [pre, hpointVal, m] at hprefixDigits ⊢
                rw [hprefixDigits]
                change (F pref : Real) * (10 : Real) ^
                    (d.decimal_point.val + (growth.val : Int) -
                      (capped.val : Int)) = _
                have hexp : d.decimal_point.val + (growth.val : Int) -
                      (m : Int) =
                    (d.decimal_point.val - (d.num_digits.val : Int)) +
                      (suff.length : Int) := by
                  rw [hsuffixLen, hfullLen, hwidthVal]
                  have hmbound : m <= d.num_digits.val + growth.val := by
                    rw [← hwidthVal, ← hfullLen]
                    exact hmWidth
                  rw [Nat.cast_sub hmbound]
                  omega
                rw [hexp, zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
                norm_num only [zpow_natCast]
                have hcastSplit : (B * I : Real) =
                    (F pref : Real) * (10 : Real) ^ suff.length + E := by
                  norm_cast
                  rw [← hfullFold]
                  exact hfoldSplit
                have hBcast : (2 : Real) ^ shift.val = (B : Real) := by
                  norm_cast
                have hINat :
                    List.foldl (fun acc z => 10 * acc + z) 0
                      (List.map (fun z : Std.U8 => z.val)
                        (List.take d.num_digits.val d.digits.val)) = I := by
                  rfl
                rw [hBcast, hINat]
                change (F pref : Real) *
                    ((10 : Real) ^
                      (d.decimal_point.val - (d.num_digits.val : Int)) *
                      (10 : Real) ^ suff.length) =
                  (B : Real) * ((I : Real) *
                    (10 : Real) ^
                      (d.decimal_point.val - (d.num_digits.val : Int))) - rho
                let scale : Real := (10 : Real) ^
                  (d.decimal_point.val - (d.num_digits.val : Int))
                calc
                  (F pref : Real) * (scale * (10 : Real) ^ suff.length) =
                      ((F pref : Real) * (10 : Real) ^ suff.length + E) *
                        scale - (E : Real) * scale := by ring
                  _ = ((B : Real) * I) * scale - (E : Real) * scale := by
                    rw [hcastSplit]
                  _ = (B : Real) * ((I : Real) * scale) - rho := by
                    dsimp only [rho, scale]
                    ring
              · rw [htruncEq]
                change b1 = true <-> _
                rw [hstickyFull, hrhopos, hEpos]


end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (116)
More program dependencies (83)
Standard-library dependencies (30)

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 16 entries
Technical checking records
Local closure summary
{
  "node": "DecimalSeqLeftShiftRecordSemantics",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "1f6315c09e604970d1b604e64fdeac8313f80c9b7f7b5b3779b57bc3bc99b5f9",
  "active_statement_hash": "defee60c1ef0b5a2e318b78cb75d4781cf348a6fa5df296307c10f6fcc6b0cc4",
  "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": "revalidate-cycle-188",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalLeftShiftDigitGrowth": "6dcf8858df4713eb",
    "DecimalLeftShiftFirstLoopSemantics": "d126fe9b83dfc5f3",
    "DecimalLeftShiftSecondLoopSemantics": "ccbfee7a0aef7445",
    "DecimalLeftShiftTableCertificate": "3c85f76c60cebc6c",
    "DecimalSeqTrimSemantics": "e4b2cbb62f7171af",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "0eb6fb2627d0ff5c",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift": "0f3e5cc6a65d812e",
    "num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift": "3703c132ca68c95c"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "b0286c42454b158d",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "39f6cb231ef80388",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift": "bab51f6bda8660d2",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_trim": "faa9fd92b8c29c14",
    "num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift": "b3efbd0e6b3fe061",
    "num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift_TABLE": "e68bec96fec99700",
    "num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift_TABLE_POW5": "4dda92c1af5f2c9e"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "differs from current fingerprint",
  "assessment_fingerprint": "sound-v2:f98e9e7e3f4420d74ca64b8fe01332bccc3246a862930fcb9a68025c96cfd58f",
  "current_fingerprint": "sound-v2:2bac68c5d923d2769ce7860e7a3445f63fc800805e0a9f57a5d44f68f59d3976",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}