DecimalLeftShiftLoop1Execution

Lean checked

Statement

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

Lemma · Normal left shift on the loop-one domain

Let d have at most 768 retained cells, all retained cells be decimal digits, and let its retained word have length equal to the count and begin with a digit in [1,9]. Suppose -324\leq p\leq0, and let 0\leq k\leq60. Then the complete extracted decimal left shift returns normally. There is a natural digit growth q\leq19 such that its returned record d' is nonempty and
d'.p=p+q,\qquad -324\leq d'.p\leq19. \tag{1}
The Lean conclusion contains the exact successful-call equation and (1).

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_left_shift

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalLeftShiftLoop1Execution]
theorem DecimalLeftShiftLoop1Execution :
    forall (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (shift : Std.Usize),
      shift.val <= 60 ->
      -324 <= d.decimal_point.val -> d.decimal_point.val <= 0 ->
      d.num_digits.val <= 768 /\
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
      (exists z zs,
        (d.digits.val.take d.num_digits.val).map (fun a => a.val) = z :: zs /\
        1 <= z /\ z < 10 /\ (z :: zs).length = d.num_digits.val) ->
      exists (d' : num.imp.dec2flt.decimal_seq.DecimalSeq) (q : Nat),
        num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d' /\
        q <= 19 /\
        d'.decimal_point.val = d.decimal_point.val + (q : Int) /\
        -324 <= d'.decimal_point.val /\ d'.decimal_point.val <= 19 /\
        d'.num_digits.val != 0 := 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

Write x for the retained digit list, n=|x| for its count, I=\operatorname{fold}_{10}(x), p for the input point, and B=2^k. The supplied list equality says that x=z::z_s, where 1\leq z<10 and |x|=n. Hence 1\leq n\leq768. The usual induction on the tail of this decimal word, using that every member of x is below ten, gives the sharp input bounds
10^{n-1}\leq I<10^n. \tag{2}
We now construct, in order, every subcall appearing in the unfolded definition num imp dec2flt decimal seq DecimalSeq left shift.
First consider the digit-growth routine. If k=0, direct unfolding at the zero table entry gives the empty comparison slice and the normal result q=0. In this case IB=I, so (2) says
10^{n+q-1}\leq IB<10^{n+q}. \tag{3}
(Here n\geq1 makes the natural subtraction in the left exponent equal to n-1.) Suppose instead that k>0. The finite certificate DecimalLeftShiftTableCertificate supplies the table integer h and the bounds
10^{h-1}<B<10^h. \tag{4}
Applying the exact growth theorem DecimalLeftShiftDigitGrowth constructs the normal routine return and gives precisely the following alternatives:
\begin{array}{c|c} q=h & 10^{n+h-1}\leq IB,\\ q=h-1 & IB<10^{n+h-1}. \end{array} \tag{5}
In the first alternative, the upper halves of (2) and (4) give IB<10^n10^h=10^{n+q}. In the second, (5) is the required upper bound IB<10^{n+q}, while the lower halves of (2) and (4) give
IB>10^{n-1}10^{h-1}=10^{n+q-1}.
Thus (3) holds in both alternatives (with a strict, hence weak, lower bound in the second). Moreover 10^{h-1}<B\leq2^{60}<10^{19} implies h\leq19 by strict monotonicity of the natural powers of ten. Since (5) always has q\leq h, we have
q\leq19. \tag{6}
This also holds in the zero-shift case.
The first checked count addition succeeds and has natural value w=n+q\leq768+19=787, well below the Usize maximum. Construct the first carry loop by induction on its decreasing read cursor, starting with carry zero and cursors n,w. Maintain that the carry C is below B, and that only destinations between the current write cursor and w have changed. When the read cursor is positive, n\leq w and the equal decrement history of the two cursors show that the write cursor is positive too, so both Usize subtractions succeed. The new read index is below n\leq768 and below or equal to the new destination; all earlier destinations were larger, and the read precedes the possible write at the new destination. Thus the read still sees the corresponding original decimal digit a<10. The U64 cast is exact and the modeled shift and addition have the natural value
T=C+aB<10B\leq10\,2^{60}<2^{64}. \tag{7}
Division by ten is legal. Its quotient C'=\lfloor T/10\rfloor obeys C'<B, while 10C'\leq T<2^{64} makes the checked multiplication exact and the checked subtraction returns the remainder R=T-10C'<10. The U8 cast of R is therefore exact. A destination below 768 is a valid array index; a destination at least 768 takes the nonwriting sticky branch. Consequently the loop returns normally after exactly n steps, with final cursor w-n=q.
Apply DecimalLeftShiftFirstLoopSemantics to that constructed return, and call its returned array A_0. The theorem gives a length-n decimal list v, final carry c<B, and, with W=\operatorname{fold}_{10}(v),
IB=c10^n+W,\qquad 0\leq W<10^n. \tag{8}
It also says that first-loop cell q+j equals v_j whenever j<n and q+j<768. The strict bound on W follows directly by induction from the stated length and digit bounds on v.
We next derive the exact bounds needed to initialize the second loop. If c\geq10^q, (8) would give IB\geq c10^n\geq10^{n+q}, contradicting (3). Hence
c<10^q. \tag{9}
In particular, q=0 forces c=0. If q>0 and c<10^{q-1}, integrality gives c+1\leq10^{q-1}; combining this with W<10^n in (8) yields
IB=c10^n+W<(c+1)10^n\leq10^{q-1}10^n =10^{n+q-1},
contrary to (3). Therefore
q>0\quad\Longrightarrow\quad 10^{q-1}\leq c. \tag{10}
The zero-prefix premise of DecimalLeftShiftSecondLoopSemantics is now satisfied. It is vacuous when q=0. When q>0, take j<q (the additional requirement j<768 is immaterial). Then q-j-1\leq q-1, so monotonicity and (10) give 10^{q-j-1}\leq10^{q-1}\leq c. Thus the premise's antecedent c<10^{q-j-1} is impossible, and the required implication holds regardless of the current array cell.
For completeness, the second loop itself has a normal return. Starting from (9), maintain at cursor s a carry C<10^s. A positive C forces s>0, so the checked cursor decrement succeeds. Division by ten gives quotient C' and remainder R<10 with C'<10^{s-1}; as in (7), 10C'\leq C<2^{64} discharges the checked multiplication and subtraction, and the U8 cast is exact. The body either writes at the in-range destination s-1<768 or takes the nonwriting sticky branch. Each continuing step decreases both the cursor and the carry, so after at most q steps the carry is zero and the loop returns. Call its returned array A_1. Applying the cited second-loop theorem gives a length-q decimal list u such that
c=\operatorname{fold}_{10}(u). \tag{11}
Its cell clause puts u_j in final cell j whenever j<q and j<768; its unchanged-suffix clause preserves every first-loop cell with index at least q.
The two lists therefore splice without a gap: for every physical index, the final array contains the entries of u in positions [0,q) and the entries of v in positions [q,q+n). Algebraically, the standard concatenation identity for decimal folds, (11), and (8) give
\begin{aligned} \operatorname{fold}_{10}(u{+\!+}v) &=\operatorname{fold}_{10}(u)10^{|v|} +\operatorname{fold}_{10}(v)\\ &=c10^n+W=IB, \end{aligned} \tag{12}
and |u{+\!+}v|=q+n. We also need the promised positive physical first cell. If q>0, (10)--(11) imply 10^{q-1}\leq\operatorname{fold}_{10}(u). Were u_0=0, its remaining q-1 decimal digits would instead make that fold strictly below 10^{q-1}, a contradiction. Hence u_0>0, and the second-loop cell clause writes this value at physical index zero. If q=0, (9) gives c=0, so (3), (8), and (11) give 10^{n-1}\leq W=\operatorname{fold}_{10}(v). A zero head of the length-n list v would make its fold less than 10^{n-1}; thus v_0>0. The first-loop cell clause puts v_0 at index q=0, and the second-loop unchanged-suffix clause preserves it. In either case the final loop array has a strictly positive cell at index zero:
A_1[0]>0. \tag{13}
The second checked addition again has value n+q\leq787. Capping it at 768 produces a pre-trim count
m=\min(n+q,768).
Because n\geq1, both arguments of this minimum are positive, so m>0; in particular (13) lies inside the retained prefix. The Usize-to-I32 cast of q is exact by (6). The checked point addition is also exact, because
-324\leq p+q\leq19 \tag{14}
lies strictly inside the I32 range.
It remains only to construct and analyze trim on the record with digit array A_1. Its input count is m\leq768, the length of the fixed array. Induct on that count: at a positive count, decrementing it succeeds and the inspected index is below 768; a nonzero terminal digit returns immediately, while a zero digit continues at the smaller count. Thus the trim call returns normally after at most m inspections. Apply DecimalSeqTrimSemantics. It preserves the array and the point p+q, and its zero-suffix clause says that every cell from the returned count m' through m-1 is zero. If m'=0, then m>0 lets us instantiate that clause at index zero, contradicting the positive cell (13). Hence m'\ne0.
Substituting the constructed digit-growth, two loop, arithmetic, and trim returns into the unfolded left-shift definition proves its exact successful call equation. Equations (6), (14), trim's point preservation, and m'\ne0 give respectively every remaining clause of the conclusion and of (1).

Lean proof


  intro d shift hshift hpointLo hpointHi hinput
  have loop0Total :
      forall (b : Bool) (a : Array Std.U8 768#usize)
        (read write : Std.Usize) (carry : Std.U64),
        read.val <= write.val ->
        read.val <= 768 ->
        (forall j z, j < read.val -> a.val[j]? = some z -> z.val < 10) ->
        carry.val < 2 ^ shift.val ->
        exists (b' : Bool) (a' : Array Std.U8 768#usize)
          (write' : Std.Usize) (carry' : Std.U64),
          num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
            b a shift read write carry = ok (b', a', write', carry') := by
    let B := 2 ^ shift.val
    have hBle : B <= 2 ^ 60 :=
      Nat.pow_le_pow_right (by omega) hshift
    intro b a read
    generalize hn : read.val = n
    induction n using Nat.strong_induction_on generalizing read b a with
    | h n ih =>
      intro write carry hreadwrite hreadcap hdigit hcarry
      by_cases hread0 : read.val = 0
      · have hreadEq : read = 0#usize :=
          UScalar.eq_of_val_eq (by simpa using hread0)
        subst read
        refine ⟨b, a, write, carry, ?_⟩
        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
        rw [Aeneas.Std.loop.eq_def]
        simp [num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body]
      · have hreadpos : 0 < read.val := by omega
        have hreadne : read != 0#usize := by
          simp only [bne_iff_ne]
          intro h
          apply hread0
          simpa [h]
        obtain ⟨read1, hread1, hread1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (UScalar.sub_spec (x := read) (y := 1#usize) (by simpa using hreadpos))
        norm_num at hread1Val
        have hwritepos : 0 < write.val := by omega
        obtain ⟨write1, hwrite1, hwrite1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (UScalar.sub_spec (x := write) (y := 1#usize) (by simpa using hwritepos))
        norm_num at hwrite1Val
        have hread1lt : read1.val < a.val.length := by
          rw [a.property]
          norm_num
          omega
        obtain ⟨digit, hdigitRead, hdigitReadVal⟩ :=
          Aeneas.Std.WP.spec_imp_exists
            (Array.index_usize_spec a read1
              (by simpa [Array.length] using hread1lt))
        have hdigitGet : a.val[read1.val]? = some digit :=
          getElem?_eq_some_iff.mpr ⟨hread1lt, hdigitReadVal.symm⟩
        have hdigitLt : digit.val < 10 :=
          hdigit read1.val digit (by omega) hdigitGet
        let digit64 : Std.U64 := UScalar.cast .U64 digit
        have hdigit64 : digit64.val = digit.val := by
          simp [digit64, UScalar.cast_val_eq]
        have hshiftSpec := UScalar.ShiftLeft_spec digit64 shift (2 ^ 64)
          (by norm_num [U64.numBits]; omega) (by norm_num [U64.size_eq])
        cases hshiftEq : digit64 <<< shift with
        | fail e => simp [hshiftEq] at hshiftSpec
        | div => simp [hshiftEq] at hshiftSpec
        | ok shifted =>
          rw [hshiftEq] at hshiftSpec
          have hshiftVal : shifted.val = digit.val * B := by
            have hmulLt : digit.val * B < 2 ^ 64 := by
              calc
                digit.val * B <= 9 * 2 ^ 60 := Nat.mul_le_mul (by omega) hBle
                _ < 2 ^ 64 := by norm_num
            have hs := hshiftSpec.1
            simp only [WP.spec_ok] at hs
            rw [Nat.shiftLeft_eq, hdigit64, Nat.mod_eq_of_lt (by
              simpa [B] using hmulLt)] at hs
            simpa [B] using hs
          have haddBound : carry.val + shifted.val <= U64.max := by
            rw [hshiftVal]
            have hdigitMul : digit.val * B <= 9 * B :=
              Nat.mul_le_mul_right B (by omega)
            have : carry.val + digit.val * B < 2 ^ 64 := by
              calc
                carry.val + digit.val * B < B + 9 * B := by omega
                _ = 10 * B := by ring
                _ <= 10 * 2 ^ 60 := Nat.mul_le_mul_left 10 hBle
                _ < 2 ^ 64 := by norm_num
            norm_num [U64.max, U64.numBits]
            omega
          obtain ⟨total, htotal, htotalVal⟩ := Aeneas.Std.WP.spec_imp_exists
            (U64.add_spec (x := carry) (y := shifted) haddBound)
          obtain ⟨quotient, hquotient, hquotientVal⟩ :=
            Aeneas.Std.WP.spec_imp_exists
              (U64.div_spec total (y := 10#u64) (by norm_num))
          have hmulBound : 10 * quotient.val <= U64.max := by
            rw [hquotientVal]
            have htotalLt : total.val < 2 ^ 64 := total.bv.isLt
            have hqle : 10 * (total.val / 10) <= total.val :=
              Nat.mul_div_le total.val 10
            norm_num [U64.max, U64.numBits] at htotalLt ⊢
            omega
          obtain ⟨tenq, htenq, htenqVal⟩ := Aeneas.Std.WP.spec_imp_exists
            (U64.mul_spec (x := 10#u64) (y := quotient) (by
              simpa [Nat.mul_comm] using hmulBound))
          have htenqLe : tenq.val <= total.val := by
            rw [htenqVal, hquotientVal]
            exact Nat.mul_div_le total.val 10
          obtain ⟨rem, hrem, hremVal⟩ := Aeneas.Std.WP.spec_imp_exists
            (U64.sub_spec (x := total) (y := tenq) htenqLe)
          have htotalFormula : total.val = carry.val + digit.val * B := by
            rw [htotalVal, hshiftVal]
          have hquotientFormula : quotient.val = total.val / 10 := hquotientVal
          have hremFormula : rem.val = total.val % 10 := by
            have htenqFormula : tenq.val = 10 * (total.val / 10) := by
              rw [htenqVal, hquotientVal]
              norm_num
            have heuc := Nat.mod_add_div total.val 10
            rw [hremVal.1, htenqFormula]
            omega
          have hremLt : rem.val < 10 := by
            rw [hremFormula]
            exact Nat.mod_lt _ (by omega)
          have hquotientB : quotient.val < B := by
            rw [hquotientFormula]
            apply (Nat.div_lt_iff_lt_mul (by omega)).2
            rw [htotalFormula]
            have hdigitMul : digit.val * B <= 9 * B :=
              Nat.mul_le_mul_right B (by omega)
            omega
          by_cases hcap : write1.val < 768
          · have hwibound : write1.val < a.val.length := by
              rw [a.property]
              norm_num
              exact hcap
            obtain ⟨a1, ha1, ha1Val⟩ := Aeneas.Std.WP.spec_imp_exists
              (Array.update_spec a write1 (UScalar.cast .U8 rem)
                (by simpa [Array.length] using hwibound))
            have hreadwrite1 : read1.val <= write1.val := by omega
            have hcapWord : write1 <
                num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
              simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
                UScalar.lt_equiv] using hcap
            have hdigit1 : forall j z, j < read1.val ->
                a1.val[j]? = some z -> z.val < 10 := by
              intro j z hj hz
              rw [ha1Val] at hz
              simp only [Aeneas.Std.Array.set, List.getElem?_set] at hz
              have hne : write1.val ≠ j := by omega
              simp [hne] at hz
              exact hdigit j z (by omega) hz
            obtain ⟨b', a', write', carry', hrest⟩ :=
              ih read1.val (by omega) b a1 read1 (by rfl)
                write1 quotient (by omega) (by omega) hdigit1 hquotientB
            refine ⟨b', a', write', carry', ?_⟩
            unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
            rw [Aeneas.Std.loop.eq_def]
            simp only [
              num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body]
            simp only [hreadne, if_true, hread1, hwrite1, bind_tc_ok,
              hdigitRead, lift]
            rw [show UScalar.cast .U64 digit = digit64 by rfl]
            simp only [
              hshiftEq, bind_tc_ok, htotal, hquotient, htenq, hrem,
              hcapWord, ha1]
            exact hrest
          · have hcapWord : ¬ write1 <
                num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
              simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
                UScalar.lt_equiv] using hcap
            have hdigit1 : forall j z, j < read1.val ->
                a.val[j]? = some z -> z.val < 10 := by
              intro j z hj hz
              exact hdigit j z (by omega) hz
            by_cases hrem0 : rem.val = 0
            · have hremEq : rem = 0#u64 :=
                UScalar.eq_of_val_eq (by simpa using hrem0)
              have hnotgt : ¬ rem > 0#u64 := by simp [hremEq]
              obtain ⟨b', a', write', carry', hrest⟩ :=
                ih read1.val (by omega) b a read1 (by rfl)
                  write1 quotient (by omega) (by omega) hdigit1 hquotientB
              refine ⟨b', a', write', carry', ?_⟩
              unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
              rw [Aeneas.Std.loop.eq_def]
              simp only [
                num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body]
              simp only [hreadne, if_true, hread1, hwrite1, bind_tc_ok,
                hdigitRead, lift]
              rw [show UScalar.cast .U64 digit = digit64 by rfl]
              simp only [
                hshiftEq, bind_tc_ok, htotal, hquotient, htenq, hrem,
                hcapWord, if_false, hnotgt]
              exact hrest
            · have hgt : rem > 0#u64 := by
                change 0 < rem.val
                omega
              obtain ⟨b', a', write', carry', hrest⟩ :=
                ih read1.val (by omega) true a read1 (by rfl)
                  write1 quotient (by omega) (by omega) hdigit1 hquotientB
              refine ⟨b', a', write', carry', ?_⟩
              unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
              rw [Aeneas.Std.loop.eq_def]
              simp only [
                num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body]
              simp only [hreadne, if_true, hread1, hwrite1, bind_tc_ok,
                hdigitRead, lift]
              rw [show UScalar.cast .U64 digit = digit64 by rfl]
              simp only [
                hshiftEq, bind_tc_ok, htotal, hquotient, htenq, hrem,
                hcapWord, if_false, hgt]
              exact hrest
  have loop1Total :
      forall (b : Bool) (a : Array Std.U8 768#usize)
        (write : Std.Usize) (carry : Std.U64),
        carry.val < 10 ^ write.val ->
        exists (b' : Bool) (a' : Array Std.U8 768#usize),
          num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
            b a write carry = ok (b', a') := by
    intro b a write
    generalize hn : write.val = n
    induction n using Nat.strong_induction_on generalizing write b a with
    | h n ih =>
      intro carry hcarry
      have hcarryWrite : carry.val < 10 ^ write.val := by
        rw [hn]
        exact hcarry
      by_cases hcarry0 : carry.val = 0
      · have hcarryEq : carry = 0#u64 :=
          UScalar.eq_of_val_eq (by simpa using hcarry0)
        subst carry
        refine ⟨b, a, ?_⟩
        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
        rw [Aeneas.Std.loop.eq_def]
        simp [num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body]
      · have hcarrypos : 0 < carry.val := by omega
        have hgt : carry > 0#u64 := by
          change 0 < carry.val
          exact hcarrypos
        have hwritepos : 0 < write.val := by
          by_contra hnpos
          have hwrite0 : write.val = 0 := by omega
          rw [hwrite0] at hcarryWrite
          simp at hcarryWrite
          omega
        obtain ⟨write1, hwrite1, hwrite1Val⟩ :=
          Aeneas.Std.WP.spec_imp_exists
            (UScalar.sub_spec (x := write) (y := 1#usize)
              (by simpa using hwritepos))
        norm_num at hwrite1Val
        obtain ⟨quotient, hquotient, hquotientVal⟩ :=
          Aeneas.Std.WP.spec_imp_exists
            (U64.div_spec carry (y := 10#u64) (by norm_num))
        have hmulBound : 10 * quotient.val <= U64.max := by
          rw [hquotientVal]
          have hcarryLt : carry.val < 2 ^ 64 := carry.bv.isLt
          have hqle : 10 * (carry.val / 10) <= carry.val :=
            Nat.mul_div_le carry.val 10
          norm_num [U64.max, U64.numBits] at hcarryLt ⊢
          omega
        obtain ⟨tenq, htenq, htenqVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (U64.mul_spec (x := 10#u64) (y := quotient) (by
            simpa [Nat.mul_comm] using hmulBound))
        have htenqLe : tenq.val <= carry.val := by
          rw [htenqVal, hquotientVal]
          exact Nat.mul_div_le carry.val 10
        obtain ⟨rem, hrem, hremVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (U64.sub_spec (x := carry) (y := tenq) htenqLe)
        have hremFormula : rem.val = carry.val % 10 := by
          have htenqFormula : tenq.val = 10 * (carry.val / 10) := by
            rw [htenqVal, hquotientVal]
            norm_num
          have heuc := Nat.mod_add_div carry.val 10
          rw [hremVal.1, htenqFormula]
          omega
        have hremLt : rem.val < 10 := by
          rw [hremFormula]
          exact Nat.mod_lt _ (by omega)
        have hquotientFormula : quotient.val = carry.val / 10 := by
          simpa using hquotientVal
        have hquotientFit : quotient.val < 10 ^ write1.val := by
          rw [hquotientFormula]
          apply (Nat.div_lt_iff_lt_mul (by omega)).2
          rw [← pow_succ]
          have hwriteSucc : write1.val + 1 = write.val := by omega
          rw [hwriteSucc]
          exact hcarryWrite
        by_cases hcap : write1.val < 768
        · have hwibound : write1.val < a.val.length := by
            rw [a.property]
            norm_num
            exact hcap
          obtain ⟨a1, ha1, ha1Val⟩ := Aeneas.Std.WP.spec_imp_exists
            (Array.update_spec a write1 (UScalar.cast .U8 rem)
              (by simpa [Array.length] using hwibound))
          have hcapWord : write1 <
              num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
            simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
              UScalar.lt_equiv] using hcap
          obtain ⟨b', a', hrest⟩ :=
            ih write1.val (by omega) b a1 write1 (by rfl)
              quotient hquotientFit
          refine ⟨b', a', ?_⟩
          unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
          rw [Aeneas.Std.loop.eq_def]
          simp only [
            num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body,
            hgt, if_true, hwrite1, bind_tc_ok, hquotient, htenq, hrem,
            hcapWord, lift, ha1]
          exact hrest
        · have hcapWord : ¬ write1 <
              num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
            simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
              UScalar.lt_equiv] using hcap
          by_cases hrem0 : rem.val = 0
          · have hremEq : rem = 0#u64 :=
              UScalar.eq_of_val_eq (by simpa using hrem0)
            have hnotgt : ¬ rem > 0#u64 := by simp [hremEq]
            obtain ⟨b', a', hrest⟩ :=
              ih write1.val (by omega) b a write1 (by rfl)
                quotient hquotientFit
            refine ⟨b', a', ?_⟩
            unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
            rw [Aeneas.Std.loop.eq_def]
            simp only [
              num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body,
              hgt, if_true, hwrite1, bind_tc_ok, hquotient, htenq, hrem,
              hcapWord, if_false, hnotgt]
            exact hrest
          · have hgtRem : rem > 0#u64 := by
              change 0 < rem.val
              omega
            obtain ⟨b', a', hrest⟩ :=
              ih write1.val (by omega) true a write1 (by rfl)
                quotient hquotientFit
            refine ⟨b', a', ?_⟩
            unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
            rw [Aeneas.Std.loop.eq_def]
            simp only [
              num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body,
              hgt, if_true, hwrite1, bind_tc_ok, hquotient, htenq, hrem,
              hcapWord, if_false, hgtRem]
            exact hrest
  have zeroGrowthTotal (d0 : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (sh : Std.Usize) (hsh : sh.val = 0) :
      exists gr : Std.Usize,
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
          d0 sh = ok gr /\ gr.val = 0 := by
    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, hlenTable, _, hzeroTable, _⟩
    have hz := hzeroTable (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 q => q.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 hlenTable
    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
    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]))
    have hxaEq : xa = x := by
      apply UScalar.eq_of_val_eq
      simpa [x, table] using congrArg UScalar.val hxaVal
    subst xa
    obtain ⟨next, hnext, hnextVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.add_spec (x := 0#usize) (y := 1#usize) (by scalar_tac))
    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))
    have hybEq : yb = y := by
      apply UScalar.eq_of_val_eq
      simpa [y, table, hnextVal] using congrArg UScalar.val hybVal
    subst yb
    obtain ⟨high16, hhigh16, hhigh16Val⟩ := Aeneas.Std.WP.spec_imp_exists
      (U16.ShiftRight_IScalar_spec x 11#i32 (by norm_num) (by norm_num))
    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]
    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]
    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)
    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
    refine ⟨numNew, ?_, hnumNewVal⟩
    unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
    simp only [hmask, lift, Bind.bind, Aeneas.Std.bind, hxa, bind_tc_ok,
      hnext, hyb, hhigh16]
    rw [hxaEq, hybEq]
    simp only [hhigh16, bind_tc_ok]
    rw [show UScalar.cast .Usize (2047#u16 &&& x) = pow5a by rfl]
    rw [show UScalar.cast .Usize (2047#u16 &&& y) = pow5b by rfl]
    rw [show UScalar.cast .Usize high16 = numNew by rfl]
    rw [hslice]
    simp only [bind_tc_ok]
    unfold core.slice.Slice.iter
    simp only [bind_tc_ok]
    unfold core.slice.iter.IteratorSliceIter.enumerate
    simp only [bind_tc_ok, hspan]
    unfold core.iter.adapters.enumerate.IteratorEnumerate.take
    simp only [bind_tc_ok]
    unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift_loop
    rw [Aeneas.Std.loop.eq_def]
    unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift_loop.body
    unfold core.iter.adapters.take.IteratorTake.next
    simp
  have trimLoopTotal :
      forall (a : Array Std.U8 768#usize) (n : Std.Usize),
        n.val <= a.val.length ->
        exists j : Std.Usize,
          num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop n a = ok j := by
    intro a n
    generalize hn : n.val = k
    induction k using Nat.strong_induction_on generalizing n with
    | h k ih =>
      intro hbound
      by_cases hn0 : n.val = 0
      · have hnEq : n = 0#usize :=
          UScalar.eq_of_val_eq (by simpa using hn0)
        subst n
        refine ⟨0#usize, ?_⟩
        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
        rw [Aeneas.Std.loop.eq_def]
        simp [num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body]
      · have hnpos : 0 < n.val := by omega
        have hnne : n != 0#usize := by
          simp only [bne_iff_ne]
          intro heq
          apply hn0
          simpa [heq]
        obtain ⟨n1, hn1, hn1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (UScalar.sub_spec (x := n) (y := 1#usize) (by simpa using hnpos))
        norm_num at hn1Val
        have hn1lt : n1.val < a.val.length := by omega
        obtain ⟨digit, hdigit, hdigitVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (Array.index_usize_spec a n1
            (by simpa [Array.length] using hn1lt))
        by_cases hzero : digit = 0#u8
        · obtain ⟨j, hrest⟩ :=
            ih n1.val (by omega) n1 (by rfl) (by omega)
          refine ⟨j, ?_⟩
          unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
          rw [Aeneas.Std.loop.eq_def]
          simp only [num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body,
            hnne, if_false, hn1, bind_tc_ok, hdigit, hzero, if_true]
          exact hrest
        · refine ⟨n, ?_⟩
          unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
          rw [Aeneas.Std.loop.eq_def]
          simp only [num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body,
            hnne, if_false, hn1, bind_tc_ok, hdigit, hzero, if_false]
          simp
  have trimTotal
      (e : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (hbound : e.num_digits.val <= e.digits.val.length) :
      exists e' : num.imp.dec2flt.decimal_seq.DecimalSeq,
        num.imp.dec2flt.decimal_seq.DecimalSeq.trim e = ok e' := by
    have hle : e.num_digits <=
        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
      simpa [UScalar.le_equiv,
        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
        e.digits.property] using hbound
    obtain ⟨j, hj⟩ := trimLoopTotal e.digits e.num_digits hbound
    refine ⟨{ e with num_digits := j }, ?_⟩
    unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim
    simp only [hle, massert_True, bind_tc_ok, hj]
  rcases hinput with ⟨hcap, hdigit, z, zs, hdigits, hzlo, hzhi, hlen⟩
  let xs := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
  let F := fun ys : List Nat => ys.foldl (fun n z => 10 * n + z) 0
  let I := F xs
  let B := 2 ^ shift.val
  have hxdigit : forall q, q ∈ xs -> q < 10 := by
    intro q hq
    simp only [xs, List.mem_map] at hq
    rcases hq with ⟨a, ha, rfl⟩
    exact hdigit a ha
  have hxslen : xs.length = d.num_digits.val := by
    rw [show xs = z :: zs by simpa [xs] using hdigits]
    exact hlen
  have hnpos : 0 < d.num_digits.val := by
    rw [← hxslen]
    simpa [xs, hdigits]
  have hnne : d.num_digits ≠ 0#usize := by
    intro heq
    have : d.num_digits.val = 0 := by simpa [heq]
    omega
  have foldlFrom (ys : List Nat) (acc : Nat) :
      ys.foldl (fun n q => 10 * n + q) acc =
        acc * 10 ^ ys.length + F ys := by
    induction ys generalizing acc with
    | nil => simp [F]
    | cons q qs ih =>
      simp only [List.foldl_cons, List.length_cons]
      rw [ih, pow_succ]
      change (10 * acc + q) * 10 ^ qs.length + F qs =
        acc * (10 ^ qs.length * 10) +
          (q :: qs).foldl (fun n z => 10 * n + z) 0
      simp only [List.foldl_cons]
      rw [ih]
      ring
  have foldBound (ys : List Nat) (hy : forall q, q ∈ ys -> q < 10) :
      F ys < 10 ^ ys.length := by
    induction ys with
    | nil => simp [F]
    | cons q qs ih =>
      have hq : q < 10 := hy q (by simp)
      have hqs : forall a, a ∈ qs -> a < 10 := by
        intro a ha
        exact hy a (by simp [ha])
      have hi := ih hqs
      rw [show F (q :: qs) = q * 10 ^ qs.length + F qs by
        simpa [F] using foldlFrom qs q, List.length_cons, pow_succ]
      have hm : q * 10 ^ qs.length <= 9 * 10 ^ qs.length :=
        Nat.mul_le_mul_right _ (by omega)
      calc
        q * 10 ^ qs.length + F qs <
            q * 10 ^ qs.length + 10 ^ qs.length := Nat.add_lt_add_left hi _
        _ <= 9 * 10 ^ qs.length + 10 ^ qs.length := Nat.add_le_add_right hm _
        _ = 10 ^ qs.length * 10 := by ring
  have hIbound : I < 10 ^ d.num_digits.val := by
    simpa [hxslen] using foldBound xs hxdigit
  have hIlower : 10 ^ (d.num_digits.val - 1) <= I := by
    have hIform : I = z * 10 ^ zs.length + F zs := by
      simp only [I, xs, hdigits, List.foldl_cons]
      simpa [F] using foldlFrom zs z
    have hnlen : d.num_digits.val = zs.length + 1 := by
      rw [← hxslen]
      simp [xs, hdigits]
    rw [hnlen]
    simp only [Nat.add_sub_cancel]
    rw [hIform]
    have hm := Nat.mul_le_mul_right (10 ^ zs.length) hzlo
    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
  obtain ⟨growth, hgrowth, hgrowthLe, hproductLo, hproductHi⟩ :
      exists growth : Std.Usize,
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
          d shift = ok growth /\
        growth.val <= 19 /\
        10 ^ (d.num_digits.val + growth.val - 1) <= B * I /\
        B * I < 10 ^ (d.num_digits.val + growth.val) := by
    by_cases hk0 : shift.val = 0
    · obtain ⟨growth, hgrowth, hgrowthZero⟩ :=
        zeroGrowthTotal d shift hk0
      refine ⟨growth, hgrowth, by omega, ?_, ?_⟩
      · simpa [B, hk0, hgrowthZero] using hIlower
      · simpa [B, hk0, hgrowthZero] using hIbound
    · 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 ⟨_, _, _, _, _, hpositiveTable⟩
      have hp := hpositiveTable (by simpa [kfin] using hkpos)
      rcases hp with ⟨_, _, _, _, hlo, hhi⟩
      let table :=
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
      let tableEntry := table.val.get ⟨shift.val, by
        have htlen : table.val.length = 65 := by simp [table]
        omega⟩
      let h := tableEntry.val / 2048
      have hlo' : 10 ^ (h - 1) < B := by
        simpa [h, tableEntry, table, kfin, B] using hlo
      have hhi' : B < 10 ^ h := by
        simpa [h, tableEntry, 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 hp19 : 10 ^ 19 <= 10 ^ (h - 1) :=
          Nat.pow_le_pow_right (by omega) (by omega)
        have hBle : B <= 2 ^ 60 :=
          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' hcap hdigit
      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
      refine ⟨growth, hgrowth, hgrowthLe, ?_, ?_⟩
      · rw [hgrowthFormula]
        split
        next ht => simpa [Nat.mul_comm] using ht
        next ht =>
          have hmul1 : 10 ^ (h - 1) * 10 ^ (d.num_digits.val - 1) <
              B * 10 ^ (d.num_digits.val - 1) :=
            (Nat.mul_lt_mul_right (by positivity)).mpr hlo'
          have hmul2 : B * 10 ^ (d.num_digits.val - 1) <= B * I :=
            Nat.mul_le_mul_left B hIlower
          have he : d.num_digits.val + (h - 1) - 1 =
              (h - 1) + (d.num_digits.val - 1) := by omega
          rw [he, Nat.pow_add]
          exact Nat.le_of_lt (lt_of_lt_of_le hmul1 hmul2)
      · 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 hm := lt_trans hmul1 hmul2
          rw [Nat.mul_comm (10 ^ h), ← Nat.pow_add] at hm
          simpa using hm
        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 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)
  have hcursorInput : d.num_digits.val <= width.val := by
    rw [hwidthVal]
    omega
  have hdigitIndexed : forall j u, j < d.num_digits.val ->
      d.digits.val[j]? = some u -> u.val < 10 := by
    intro j u hj hu
    apply hdigit u
    rw [List.mem_take_iff_getElem]
    rcases getElem?_eq_some_iff.mp hu with ⟨hjlen, hju⟩
    exact ⟨j, by simp; omega, hju⟩
  obtain ⟨b0, a0, cursor, carry, hloop0⟩ :=
    loop0Total d.truncated d.digits d.num_digits
      width 0#u64 hcursorInput hcap hdigitIndexed (by simp [B])
  obtain ⟨lowDigits, hlowLen, hlowDigit, hcursorEq, hcarryBase,
      hproduct, hlowArray, houtside0, hsticky0⟩ :=
    DecimalLeftShiftFirstLoopSemantics d.digits a0 d.truncated b0
      shift d.num_digits width cursor carry hshift hcap hcursorInput
      hdigit hloop0
  have hcursorVal : cursor.val = growth.val := by
    rw [hwidthVal] at hcursorEq
    omega
  have hlowBound : F lowDigits < 10 ^ d.num_digits.val := by
    simpa [hlowLen] using foldBound lowDigits hlowDigit
  have hproduct' :
      B * I = carry.val * 10 ^ d.num_digits.val + F lowDigits := by
    simpa [B, I, F, xs, List.map_take] using hproduct
  have hcarryWidth : carry.val < 10 ^ cursor.val := by
    rw [hcursorVal]
    have hcle : carry.val * 10 ^ d.num_digits.val <= B * I := by
      rw [hproduct']
      omega
    have hpow : 10 ^ growth.val * 10 ^ d.num_digits.val =
        10 ^ (d.num_digits.val + growth.val) := by
      rw [Nat.mul_comm, ← Nat.pow_add]
    have hm : carry.val * 10 ^ d.num_digits.val <
        10 ^ growth.val * 10 ^ d.num_digits.val := by
      rw [hpow]
      exact lt_of_le_of_lt hcle hproductHi
    exact Nat.lt_of_mul_lt_mul_right hm
  have hcarryLower : 0 < growth.val ->
      10 ^ (growth.val - 1) <= carry.val := by
    intro hgpos
    by_contra hnot
    have hcsmall : carry.val < 10 ^ (growth.val - 1) := by omega
    have hu := decimalUpper carry.val (F lowDigits)
      (10 ^ (growth.val - 1)) d.num_digits.val hcsmall hlowBound
    rw [← hproduct'] at hu
    rw [Nat.mul_comm (10 ^ (growth.val - 1)), ← Nat.pow_add] at hu
    have he : d.num_digits.val + (growth.val - 1) =
        d.num_digits.val + growth.val - 1 := by omega
    rw [he] at hu
    exact (not_lt_of_ge hproductLo) hu
  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 hjcursor _ hjcarry
    have hgpos : 0 < growth.val := by omega
    have hlower := hcarryLower hgpos
    have hexp : cursor.val - j - 1 <= growth.val - 1 := by
      rw [hcursorVal]
      omega
    have hpow :
        10 ^ (cursor.val - j - 1) <= 10 ^ (growth.val - 1) :=
      Nat.pow_le_pow_right (by omega) hexp
    omega
  obtain ⟨b1, a1, hloop1⟩ :=
    loop1Total b0 a0 cursor carry hcarryWidth
  obtain ⟨highDigits, hhighLen, hhighDigit, hcarryDigits,
      hhighArray, houtside1, hsticky1⟩ :=
    DecimalLeftShiftSecondLoopSemantics a0 a1 b0 b1 cursor carry
      hcarryWidth hpad hloop1
  have headPositive (ys : List Nat) (hy : forall q, q ∈ ys -> q < 10)
      (hyspos : 0 < ys.length)
      (hlower : 10 ^ (ys.length - 1) <= F ys) :
      exists q qs, ys = q :: qs /\ 0 < q := by
    cases hys : ys with
    | nil =>
      rw [hys] at hyspos
      simp at hyspos
    | cons q qs =>
      refine ⟨q, qs, rfl, ?_⟩
      by_contra hq
      have hqzero : q = 0 := by omega
      have htailDigit : forall a, a ∈ qs -> a < 10 := by
        intro a ha
        apply hy a
        rw [hys]
        simp [ha]
      have htailBound := foldBound qs htailDigit
      have htailLower : 10 ^ qs.length <= F qs := by
        simpa [hys, F, hqzero] using hlower
      omega
  have hcellZero : a1.val[0]? != some 0#u8 := by
    simp only [bne_iff_ne]
    by_cases hgzero : growth.val = 0
    · have hcursorZero : cursor.val = 0 := by rw [hcursorVal, hgzero]
      have hcarryZero : carry.val = 0 := by
        rw [hcursorZero] at hcarryWidth
        simp at hcarryWidth
        omega
      have hlowLower : 10 ^ (lowDigits.length - 1) <= F lowDigits := by
        rw [hlowLen]
        rw [hgzero, Nat.add_zero] at hproductLo
        rw [hcarryZero] at hproduct'
        simp only [zero_mul, zero_add] at hproduct'
        rw [hproduct'] at hproductLo
        exact hproductLo
      obtain ⟨q, qs, hlowEq, hqpos⟩ :=
        headPositive lowDigits hlowDigit (by rw [hlowLen]; exact hnpos) hlowLower
      have hlowCell := hlowArray 0 (by rw [hlowEq]; simp) (by
        rw [hcursorZero]
        omega)
      have hout := houtside1 0 (by omega) (by omega)
      have hcell :
          (a1.val.map (fun z : Std.U8 => z.val))[0]? = some q := by
        rw [List.getElem?_map, hout]
        simpa [hcursorZero, hlowEq] using hlowCell
      intro hz
      have hzmap :
          (a1.val.map (fun z : Std.U8 => z.val))[0]? = some 0 := by
        rw [List.getElem?_map, hz]
        rfl
      rw [hcell] at hzmap
      have : q = 0 := Option.some.inj hzmap
      omega
    · have hgpos : 0 < growth.val := by omega
      have hhighLower :
          10 ^ (highDigits.length - 1) <= F highDigits := by
        rw [hhighLen, hcursorVal]
        simpa [F, hcarryDigits] using hcarryLower hgpos
      obtain ⟨q, qs, hhighEq, hqpos⟩ :=
        headPositive highDigits hhighDigit
          (by rw [hhighLen, hcursorVal]; exact hgpos) hhighLower
      have hcell := hhighArray 0 (by rw [hhighEq]; simp) (by omega)
      have hcell' :
          (a1.val.map (fun z : Std.U8 => z.val))[0]? = some q := by
        simpa [hhighEq] using hcell
      intro hz
      have hzmap :
          (a1.val.map (fun z : Std.U8 => z.val))[0]? = some 0 := by
        rw [List.getElem?_map, hz]
        rfl
      rw [hcell'] at hzmap
      have : q = 0 := Option.some.inj hzmap
      omega
  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]
  have hcappedPos : 0 < capped.val := by
    rw [hcappedVal, hwidthVal]
    simp only [lt_min_iff]
    omega
  let growthI : Std.I32 := UScalar.hcast .I32 growth
  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 hpointBounds :
      IScalar.min .I32 <= d.decimal_point.val + growthI.val /\
      d.decimal_point.val + growthI.val <= IScalar.max .I32 := by
    rw [hgrowthIVal]
    constructor
    · rw [IScalar.min_I32_eq]
      omega
    · rw [IScalar.max_I32_eq]
      omega
  obtain ⟨point, hpoint, hpointVal⟩ := Aeneas.Std.WP.spec_imp_exists
    (IScalar.add_spec hpointBounds.1 hpointBounds.2)
  have hpointVal' : point.val =
      d.decimal_point.val + (growth.val : Int) := by
    rw [hpointVal, hgrowthIVal]
  let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
    { num_digits := capped, decimal_point := point,
      truncated := b1, digits := a1 }
  have hpreBound : pre.num_digits.val <= pre.digits.val.length := by
    simp [pre, hcappedVal, a1.property]
  obtain ⟨d', htrim⟩ := trimTotal pre hpreBound
  have htrimSem := DecimalSeqTrimSemantics pre d' hpreBound htrim
  dsimp only at htrimSem
  rcases htrimSem with
    ⟨hdigitsEq, hpointEq, htruncEq, hnumLe, hzeros, hlast, hvalue⟩
  have hnumNonzero : d'.num_digits.val != 0 := by
    simp only [bne_iff_ne]
    intro hzero
    have hz := hzeros 0 (by omega) (by simpa [pre] using hcappedPos)
    have hz' : a1.val[0]? = some 0#u8 := by
      simpa [pre] using hz
    have hcellZero' : a1.val[0]? ≠ some 0#u8 := by
      simpa only [bne_iff_ne] using hcellZero
    exact hcellZero' hz'
  have hcall :
      num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d' := by
    unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift
    simp only [hnne, if_false, hgrowth, bind_tc_ok, hwidth, hloop0]
    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'
    rw [hloop1]
    simp only [bind_tc_ok]
    rw [hcapEval]
    simp only [bind_tc_ok, lift]
    rw [show UScalar.hcast .I32 growth = growthI by rfl]
    simp only [bind_tc_ok, hpoint]
    simpa [pre] using htrim
  have hpointOut : d'.decimal_point.val =
      d.decimal_point.val + (growth.val : Int) := by
    rw [hpointEq]
    simpa [pre] using hpointVal'
  refine ⟨d', growth.val, hcall, hgrowthLe, hpointOut, ?_, ?_,
    hnumNonzero⟩
  · rw [hpointOut]
    omega
  · rw [hpointOut]
    omega

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_left_shift

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalLeftShiftLoop1Execution]
theorem DecimalLeftShiftLoop1Execution :
    forall (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (shift : Std.Usize),
      shift.val <= 60 ->
      -324 <= d.decimal_point.val -> d.decimal_point.val <= 0 ->
      d.num_digits.val <= 768 /\
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
      (exists z zs,
        (d.digits.val.take d.num_digits.val).map (fun a => a.val) = z :: zs /\
        1 <= z /\ z < 10 /\ (z :: zs).length = d.num_digits.val) ->
      exists (d' : num.imp.dec2flt.decimal_seq.DecimalSeq) (q : Nat),
        num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d' /\
        q <= 19 /\
        d'.decimal_point.val = d.decimal_point.val + (q : Int) /\
        -324 <= d'.decimal_point.val /\ d'.decimal_point.val <= 19 /\
        d'.num_digits.val != 0 := by
-- BODY
  intro d shift hshift hpointLo hpointHi hinput
  have loop0Total :
      forall (b : Bool) (a : Array Std.U8 768#usize)
        (read write : Std.Usize) (carry : Std.U64),
        read.val <= write.val ->
        read.val <= 768 ->
        (forall j z, j < read.val -> a.val[j]? = some z -> z.val < 10) ->
        carry.val < 2 ^ shift.val ->
        exists (b' : Bool) (a' : Array Std.U8 768#usize)
          (write' : Std.Usize) (carry' : Std.U64),
          num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
            b a shift read write carry = ok (b', a', write', carry') := by
    let B := 2 ^ shift.val
    have hBle : B <= 2 ^ 60 :=
      Nat.pow_le_pow_right (by omega) hshift
    intro b a read
    generalize hn : read.val = n
    induction n using Nat.strong_induction_on generalizing read b a with
    | h n ih =>
      intro write carry hreadwrite hreadcap hdigit hcarry
      by_cases hread0 : read.val = 0
      · have hreadEq : read = 0#usize :=
          UScalar.eq_of_val_eq (by simpa using hread0)
        subst read
        refine ⟨b, a, write, carry, ?_⟩
        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
        rw [Aeneas.Std.loop.eq_def]
        simp [num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body]
      · have hreadpos : 0 < read.val := by omega
        have hreadne : read != 0#usize := by
          simp only [bne_iff_ne]
          intro h
          apply hread0
          simpa [h]
        obtain ⟨read1, hread1, hread1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (UScalar.sub_spec (x := read) (y := 1#usize) (by simpa using hreadpos))
        norm_num at hread1Val
        have hwritepos : 0 < write.val := by omega
        obtain ⟨write1, hwrite1, hwrite1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (UScalar.sub_spec (x := write) (y := 1#usize) (by simpa using hwritepos))
        norm_num at hwrite1Val
        have hread1lt : read1.val < a.val.length := by
          rw [a.property]
          norm_num
          omega
        obtain ⟨digit, hdigitRead, hdigitReadVal⟩ :=
          Aeneas.Std.WP.spec_imp_exists
            (Array.index_usize_spec a read1
              (by simpa [Array.length] using hread1lt))
        have hdigitGet : a.val[read1.val]? = some digit :=
          getElem?_eq_some_iff.mpr ⟨hread1lt, hdigitReadVal.symm⟩
        have hdigitLt : digit.val < 10 :=
          hdigit read1.val digit (by omega) hdigitGet
        let digit64 : Std.U64 := UScalar.cast .U64 digit
        have hdigit64 : digit64.val = digit.val := by
          simp [digit64, UScalar.cast_val_eq]
        have hshiftSpec := UScalar.ShiftLeft_spec digit64 shift (2 ^ 64)
          (by norm_num [U64.numBits]; omega) (by norm_num [U64.size_eq])
        cases hshiftEq : digit64 <<< shift with
        | fail e => simp [hshiftEq] at hshiftSpec
        | div => simp [hshiftEq] at hshiftSpec
        | ok shifted =>
          rw [hshiftEq] at hshiftSpec
          have hshiftVal : shifted.val = digit.val * B := by
            have hmulLt : digit.val * B < 2 ^ 64 := by
              calc
                digit.val * B <= 9 * 2 ^ 60 := Nat.mul_le_mul (by omega) hBle
                _ < 2 ^ 64 := by norm_num
            have hs := hshiftSpec.1
            simp only [WP.spec_ok] at hs
            rw [Nat.shiftLeft_eq, hdigit64, Nat.mod_eq_of_lt (by
              simpa [B] using hmulLt)] at hs
            simpa [B] using hs
          have haddBound : carry.val + shifted.val <= U64.max := by
            rw [hshiftVal]
            have hdigitMul : digit.val * B <= 9 * B :=
              Nat.mul_le_mul_right B (by omega)
            have : carry.val + digit.val * B < 2 ^ 64 := by
              calc
                carry.val + digit.val * B < B + 9 * B := by omega
                _ = 10 * B := by ring
                _ <= 10 * 2 ^ 60 := Nat.mul_le_mul_left 10 hBle
                _ < 2 ^ 64 := by norm_num
            norm_num [U64.max, U64.numBits]
            omega
          obtain ⟨total, htotal, htotalVal⟩ := Aeneas.Std.WP.spec_imp_exists
            (U64.add_spec (x := carry) (y := shifted) haddBound)
          obtain ⟨quotient, hquotient, hquotientVal⟩ :=
            Aeneas.Std.WP.spec_imp_exists
              (U64.div_spec total (y := 10#u64) (by norm_num))
          have hmulBound : 10 * quotient.val <= U64.max := by
            rw [hquotientVal]
            have htotalLt : total.val < 2 ^ 64 := total.bv.isLt
            have hqle : 10 * (total.val / 10) <= total.val :=
              Nat.mul_div_le total.val 10
            norm_num [U64.max, U64.numBits] at htotalLt ⊢
            omega
          obtain ⟨tenq, htenq, htenqVal⟩ := Aeneas.Std.WP.spec_imp_exists
            (U64.mul_spec (x := 10#u64) (y := quotient) (by
              simpa [Nat.mul_comm] using hmulBound))
          have htenqLe : tenq.val <= total.val := by
            rw [htenqVal, hquotientVal]
            exact Nat.mul_div_le total.val 10
          obtain ⟨rem, hrem, hremVal⟩ := Aeneas.Std.WP.spec_imp_exists
            (U64.sub_spec (x := total) (y := tenq) htenqLe)
          have htotalFormula : total.val = carry.val + digit.val * B := by
            rw [htotalVal, hshiftVal]
          have hquotientFormula : quotient.val = total.val / 10 := hquotientVal
          have hremFormula : rem.val = total.val % 10 := by
            have htenqFormula : tenq.val = 10 * (total.val / 10) := by
              rw [htenqVal, hquotientVal]
              norm_num
            have heuc := Nat.mod_add_div total.val 10
            rw [hremVal.1, htenqFormula]
            omega
          have hremLt : rem.val < 10 := by
            rw [hremFormula]
            exact Nat.mod_lt _ (by omega)
          have hquotientB : quotient.val < B := by
            rw [hquotientFormula]
            apply (Nat.div_lt_iff_lt_mul (by omega)).2
            rw [htotalFormula]
            have hdigitMul : digit.val * B <= 9 * B :=
              Nat.mul_le_mul_right B (by omega)
            omega
          by_cases hcap : write1.val < 768
          · have hwibound : write1.val < a.val.length := by
              rw [a.property]
              norm_num
              exact hcap
            obtain ⟨a1, ha1, ha1Val⟩ := Aeneas.Std.WP.spec_imp_exists
              (Array.update_spec a write1 (UScalar.cast .U8 rem)
                (by simpa [Array.length] using hwibound))
            have hreadwrite1 : read1.val <= write1.val := by omega
            have hcapWord : write1 <
                num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
              simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
                UScalar.lt_equiv] using hcap
            have hdigit1 : forall j z, j < read1.val ->
                a1.val[j]? = some z -> z.val < 10 := by
              intro j z hj hz
              rw [ha1Val] at hz
              simp only [Aeneas.Std.Array.set, List.getElem?_set] at hz
              have hne : write1.val ≠ j := by omega
              simp [hne] at hz
              exact hdigit j z (by omega) hz
            obtain ⟨b', a', write', carry', hrest⟩ :=
              ih read1.val (by omega) b a1 read1 (by rfl)
                write1 quotient (by omega) (by omega) hdigit1 hquotientB
            refine ⟨b', a', write', carry', ?_⟩
            unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
            rw [Aeneas.Std.loop.eq_def]
            simp only [
              num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body]
            simp only [hreadne, if_true, hread1, hwrite1, bind_tc_ok,
              hdigitRead, lift]
            rw [show UScalar.cast .U64 digit = digit64 by rfl]
            simp only [
              hshiftEq, bind_tc_ok, htotal, hquotient, htenq, hrem,
              hcapWord, ha1]
            exact hrest
          · have hcapWord : ¬ write1 <
                num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
              simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
                UScalar.lt_equiv] using hcap
            have hdigit1 : forall j z, j < read1.val ->
                a.val[j]? = some z -> z.val < 10 := by
              intro j z hj hz
              exact hdigit j z (by omega) hz
            by_cases hrem0 : rem.val = 0
            · have hremEq : rem = 0#u64 :=
                UScalar.eq_of_val_eq (by simpa using hrem0)
              have hnotgt : ¬ rem > 0#u64 := by simp [hremEq]
              obtain ⟨b', a', write', carry', hrest⟩ :=
                ih read1.val (by omega) b a read1 (by rfl)
                  write1 quotient (by omega) (by omega) hdigit1 hquotientB
              refine ⟨b', a', write', carry', ?_⟩
              unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
              rw [Aeneas.Std.loop.eq_def]
              simp only [
                num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body]
              simp only [hreadne, if_true, hread1, hwrite1, bind_tc_ok,
                hdigitRead, lift]
              rw [show UScalar.cast .U64 digit = digit64 by rfl]
              simp only [
                hshiftEq, bind_tc_ok, htotal, hquotient, htenq, hrem,
                hcapWord, if_false, hnotgt]
              exact hrest
            · have hgt : rem > 0#u64 := by
                change 0 < rem.val
                omega
              obtain ⟨b', a', write', carry', hrest⟩ :=
                ih read1.val (by omega) true a read1 (by rfl)
                  write1 quotient (by omega) (by omega) hdigit1 hquotientB
              refine ⟨b', a', write', carry', ?_⟩
              unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
              rw [Aeneas.Std.loop.eq_def]
              simp only [
                num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body]
              simp only [hreadne, if_true, hread1, hwrite1, bind_tc_ok,
                hdigitRead, lift]
              rw [show UScalar.cast .U64 digit = digit64 by rfl]
              simp only [
                hshiftEq, bind_tc_ok, htotal, hquotient, htenq, hrem,
                hcapWord, if_false, hgt]
              exact hrest
  have loop1Total :
      forall (b : Bool) (a : Array Std.U8 768#usize)
        (write : Std.Usize) (carry : Std.U64),
        carry.val < 10 ^ write.val ->
        exists (b' : Bool) (a' : Array Std.U8 768#usize),
          num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
            b a write carry = ok (b', a') := by
    intro b a write
    generalize hn : write.val = n
    induction n using Nat.strong_induction_on generalizing write b a with
    | h n ih =>
      intro carry hcarry
      have hcarryWrite : carry.val < 10 ^ write.val := by
        rw [hn]
        exact hcarry
      by_cases hcarry0 : carry.val = 0
      · have hcarryEq : carry = 0#u64 :=
          UScalar.eq_of_val_eq (by simpa using hcarry0)
        subst carry
        refine ⟨b, a, ?_⟩
        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
        rw [Aeneas.Std.loop.eq_def]
        simp [num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body]
      · have hcarrypos : 0 < carry.val := by omega
        have hgt : carry > 0#u64 := by
          change 0 < carry.val
          exact hcarrypos
        have hwritepos : 0 < write.val := by
          by_contra hnpos
          have hwrite0 : write.val = 0 := by omega
          rw [hwrite0] at hcarryWrite
          simp at hcarryWrite
          omega
        obtain ⟨write1, hwrite1, hwrite1Val⟩ :=
          Aeneas.Std.WP.spec_imp_exists
            (UScalar.sub_spec (x := write) (y := 1#usize)
              (by simpa using hwritepos))
        norm_num at hwrite1Val
        obtain ⟨quotient, hquotient, hquotientVal⟩ :=
          Aeneas.Std.WP.spec_imp_exists
            (U64.div_spec carry (y := 10#u64) (by norm_num))
        have hmulBound : 10 * quotient.val <= U64.max := by
          rw [hquotientVal]
          have hcarryLt : carry.val < 2 ^ 64 := carry.bv.isLt
          have hqle : 10 * (carry.val / 10) <= carry.val :=
            Nat.mul_div_le carry.val 10
          norm_num [U64.max, U64.numBits] at hcarryLt ⊢
          omega
        obtain ⟨tenq, htenq, htenqVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (U64.mul_spec (x := 10#u64) (y := quotient) (by
            simpa [Nat.mul_comm] using hmulBound))
        have htenqLe : tenq.val <= carry.val := by
          rw [htenqVal, hquotientVal]
          exact Nat.mul_div_le carry.val 10
        obtain ⟨rem, hrem, hremVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (U64.sub_spec (x := carry) (y := tenq) htenqLe)
        have hremFormula : rem.val = carry.val % 10 := by
          have htenqFormula : tenq.val = 10 * (carry.val / 10) := by
            rw [htenqVal, hquotientVal]
            norm_num
          have heuc := Nat.mod_add_div carry.val 10
          rw [hremVal.1, htenqFormula]
          omega
        have hremLt : rem.val < 10 := by
          rw [hremFormula]
          exact Nat.mod_lt _ (by omega)
        have hquotientFormula : quotient.val = carry.val / 10 := by
          simpa using hquotientVal
        have hquotientFit : quotient.val < 10 ^ write1.val := by
          rw [hquotientFormula]
          apply (Nat.div_lt_iff_lt_mul (by omega)).2
          rw [← pow_succ]
          have hwriteSucc : write1.val + 1 = write.val := by omega
          rw [hwriteSucc]
          exact hcarryWrite
        by_cases hcap : write1.val < 768
        · have hwibound : write1.val < a.val.length := by
            rw [a.property]
            norm_num
            exact hcap
          obtain ⟨a1, ha1, ha1Val⟩ := Aeneas.Std.WP.spec_imp_exists
            (Array.update_spec a write1 (UScalar.cast .U8 rem)
              (by simpa [Array.length] using hwibound))
          have hcapWord : write1 <
              num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
            simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
              UScalar.lt_equiv] using hcap
          obtain ⟨b', a', hrest⟩ :=
            ih write1.val (by omega) b a1 write1 (by rfl)
              quotient hquotientFit
          refine ⟨b', a', ?_⟩
          unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
          rw [Aeneas.Std.loop.eq_def]
          simp only [
            num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body,
            hgt, if_true, hwrite1, bind_tc_ok, hquotient, htenq, hrem,
            hcapWord, lift, ha1]
          exact hrest
        · have hcapWord : ¬ write1 <
              num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
            simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
              UScalar.lt_equiv] using hcap
          by_cases hrem0 : rem.val = 0
          · have hremEq : rem = 0#u64 :=
              UScalar.eq_of_val_eq (by simpa using hrem0)
            have hnotgt : ¬ rem > 0#u64 := by simp [hremEq]
            obtain ⟨b', a', hrest⟩ :=
              ih write1.val (by omega) b a write1 (by rfl)
                quotient hquotientFit
            refine ⟨b', a', ?_⟩
            unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
            rw [Aeneas.Std.loop.eq_def]
            simp only [
              num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body,
              hgt, if_true, hwrite1, bind_tc_ok, hquotient, htenq, hrem,
              hcapWord, if_false, hnotgt]
            exact hrest
          · have hgtRem : rem > 0#u64 := by
              change 0 < rem.val
              omega
            obtain ⟨b', a', hrest⟩ :=
              ih write1.val (by omega) true a write1 (by rfl)
                quotient hquotientFit
            refine ⟨b', a', ?_⟩
            unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
            rw [Aeneas.Std.loop.eq_def]
            simp only [
              num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body,
              hgt, if_true, hwrite1, bind_tc_ok, hquotient, htenq, hrem,
              hcapWord, if_false, hgtRem]
            exact hrest
  have zeroGrowthTotal (d0 : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (sh : Std.Usize) (hsh : sh.val = 0) :
      exists gr : Std.Usize,
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
          d0 sh = ok gr /\ gr.val = 0 := by
    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, hlenTable, _, hzeroTable, _⟩
    have hz := hzeroTable (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 q => q.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 hlenTable
    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
    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]))
    have hxaEq : xa = x := by
      apply UScalar.eq_of_val_eq
      simpa [x, table] using congrArg UScalar.val hxaVal
    subst xa
    obtain ⟨next, hnext, hnextVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.add_spec (x := 0#usize) (y := 1#usize) (by scalar_tac))
    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))
    have hybEq : yb = y := by
      apply UScalar.eq_of_val_eq
      simpa [y, table, hnextVal] using congrArg UScalar.val hybVal
    subst yb
    obtain ⟨high16, hhigh16, hhigh16Val⟩ := Aeneas.Std.WP.spec_imp_exists
      (U16.ShiftRight_IScalar_spec x 11#i32 (by norm_num) (by norm_num))
    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]
    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]
    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)
    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
    refine ⟨numNew, ?_, hnumNewVal⟩
    unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
    simp only [hmask, lift, Bind.bind, Aeneas.Std.bind, hxa, bind_tc_ok,
      hnext, hyb, hhigh16]
    rw [hxaEq, hybEq]
    simp only [hhigh16, bind_tc_ok]
    rw [show UScalar.cast .Usize (2047#u16 &&& x) = pow5a by rfl]
    rw [show UScalar.cast .Usize (2047#u16 &&& y) = pow5b by rfl]
    rw [show UScalar.cast .Usize high16 = numNew by rfl]
    rw [hslice]
    simp only [bind_tc_ok]
    unfold core.slice.Slice.iter
    simp only [bind_tc_ok]
    unfold core.slice.iter.IteratorSliceIter.enumerate
    simp only [bind_tc_ok, hspan]
    unfold core.iter.adapters.enumerate.IteratorEnumerate.take
    simp only [bind_tc_ok]
    unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift_loop
    rw [Aeneas.Std.loop.eq_def]
    unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift_loop.body
    unfold core.iter.adapters.take.IteratorTake.next
    simp
  have trimLoopTotal :
      forall (a : Array Std.U8 768#usize) (n : Std.Usize),
        n.val <= a.val.length ->
        exists j : Std.Usize,
          num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop n a = ok j := by
    intro a n
    generalize hn : n.val = k
    induction k using Nat.strong_induction_on generalizing n with
    | h k ih =>
      intro hbound
      by_cases hn0 : n.val = 0
      · have hnEq : n = 0#usize :=
          UScalar.eq_of_val_eq (by simpa using hn0)
        subst n
        refine ⟨0#usize, ?_⟩
        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
        rw [Aeneas.Std.loop.eq_def]
        simp [num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body]
      · have hnpos : 0 < n.val := by omega
        have hnne : n != 0#usize := by
          simp only [bne_iff_ne]
          intro heq
          apply hn0
          simpa [heq]
        obtain ⟨n1, hn1, hn1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (UScalar.sub_spec (x := n) (y := 1#usize) (by simpa using hnpos))
        norm_num at hn1Val
        have hn1lt : n1.val < a.val.length := by omega
        obtain ⟨digit, hdigit, hdigitVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (Array.index_usize_spec a n1
            (by simpa [Array.length] using hn1lt))
        by_cases hzero : digit = 0#u8
        · obtain ⟨j, hrest⟩ :=
            ih n1.val (by omega) n1 (by rfl) (by omega)
          refine ⟨j, ?_⟩
          unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
          rw [Aeneas.Std.loop.eq_def]
          simp only [num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body,
            hnne, if_false, hn1, bind_tc_ok, hdigit, hzero, if_true]
          exact hrest
        · refine ⟨n, ?_⟩
          unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
          rw [Aeneas.Std.loop.eq_def]
          simp only [num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body,
            hnne, if_false, hn1, bind_tc_ok, hdigit, hzero, if_false]
          simp
  have trimTotal
      (e : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (hbound : e.num_digits.val <= e.digits.val.length) :
      exists e' : num.imp.dec2flt.decimal_seq.DecimalSeq,
        num.imp.dec2flt.decimal_seq.DecimalSeq.trim e = ok e' := by
    have hle : e.num_digits <=
        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
      simpa [UScalar.le_equiv,
        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
        e.digits.property] using hbound
    obtain ⟨j, hj⟩ := trimLoopTotal e.digits e.num_digits hbound
    refine ⟨{ e with num_digits := j }, ?_⟩
    unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim
    simp only [hle, massert_True, bind_tc_ok, hj]
  rcases hinput with ⟨hcap, hdigit, z, zs, hdigits, hzlo, hzhi, hlen⟩
  let xs := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
  let F := fun ys : List Nat => ys.foldl (fun n z => 10 * n + z) 0
  let I := F xs
  let B := 2 ^ shift.val
  have hxdigit : forall q, q ∈ xs -> q < 10 := by
    intro q hq
    simp only [xs, List.mem_map] at hq
    rcases hq with ⟨a, ha, rfl⟩
    exact hdigit a ha
  have hxslen : xs.length = d.num_digits.val := by
    rw [show xs = z :: zs by simpa [xs] using hdigits]
    exact hlen
  have hnpos : 0 < d.num_digits.val := by
    rw [← hxslen]
    simpa [xs, hdigits]
  have hnne : d.num_digits ≠ 0#usize := by
    intro heq
    have : d.num_digits.val = 0 := by simpa [heq]
    omega
  have foldlFrom (ys : List Nat) (acc : Nat) :
      ys.foldl (fun n q => 10 * n + q) acc =
        acc * 10 ^ ys.length + F ys := by
    induction ys generalizing acc with
    | nil => simp [F]
    | cons q qs ih =>
      simp only [List.foldl_cons, List.length_cons]
      rw [ih, pow_succ]
      change (10 * acc + q) * 10 ^ qs.length + F qs =
        acc * (10 ^ qs.length * 10) +
          (q :: qs).foldl (fun n z => 10 * n + z) 0
      simp only [List.foldl_cons]
      rw [ih]
      ring
  have foldBound (ys : List Nat) (hy : forall q, q ∈ ys -> q < 10) :
      F ys < 10 ^ ys.length := by
    induction ys with
    | nil => simp [F]
    | cons q qs ih =>
      have hq : q < 10 := hy q (by simp)
      have hqs : forall a, a ∈ qs -> a < 10 := by
        intro a ha
        exact hy a (by simp [ha])
      have hi := ih hqs
      rw [show F (q :: qs) = q * 10 ^ qs.length + F qs by
        simpa [F] using foldlFrom qs q, List.length_cons, pow_succ]
      have hm : q * 10 ^ qs.length <= 9 * 10 ^ qs.length :=
        Nat.mul_le_mul_right _ (by omega)
      calc
        q * 10 ^ qs.length + F qs <
            q * 10 ^ qs.length + 10 ^ qs.length := Nat.add_lt_add_left hi _
        _ <= 9 * 10 ^ qs.length + 10 ^ qs.length := Nat.add_le_add_right hm _
        _ = 10 ^ qs.length * 10 := by ring
  have hIbound : I < 10 ^ d.num_digits.val := by
    simpa [hxslen] using foldBound xs hxdigit
  have hIlower : 10 ^ (d.num_digits.val - 1) <= I := by
    have hIform : I = z * 10 ^ zs.length + F zs := by
      simp only [I, xs, hdigits, List.foldl_cons]
      simpa [F] using foldlFrom zs z
    have hnlen : d.num_digits.val = zs.length + 1 := by
      rw [← hxslen]
      simp [xs, hdigits]
    rw [hnlen]
    simp only [Nat.add_sub_cancel]
    rw [hIform]
    have hm := Nat.mul_le_mul_right (10 ^ zs.length) hzlo
    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
  obtain ⟨growth, hgrowth, hgrowthLe, hproductLo, hproductHi⟩ :
      exists growth : Std.Usize,
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
          d shift = ok growth /\
        growth.val <= 19 /\
        10 ^ (d.num_digits.val + growth.val - 1) <= B * I /\
        B * I < 10 ^ (d.num_digits.val + growth.val) := by
    by_cases hk0 : shift.val = 0
    · obtain ⟨growth, hgrowth, hgrowthZero⟩ :=
        zeroGrowthTotal d shift hk0
      refine ⟨growth, hgrowth, by omega, ?_, ?_⟩
      · simpa [B, hk0, hgrowthZero] using hIlower
      · simpa [B, hk0, hgrowthZero] using hIbound
    · 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 ⟨_, _, _, _, _, hpositiveTable⟩
      have hp := hpositiveTable (by simpa [kfin] using hkpos)
      rcases hp with ⟨_, _, _, _, hlo, hhi⟩
      let table :=
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
      let tableEntry := table.val.get ⟨shift.val, by
        have htlen : table.val.length = 65 := by simp [table]
        omega⟩
      let h := tableEntry.val / 2048
      have hlo' : 10 ^ (h - 1) < B := by
        simpa [h, tableEntry, table, kfin, B] using hlo
      have hhi' : B < 10 ^ h := by
        simpa [h, tableEntry, 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 hp19 : 10 ^ 19 <= 10 ^ (h - 1) :=
          Nat.pow_le_pow_right (by omega) (by omega)
        have hBle : B <= 2 ^ 60 :=
          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' hcap hdigit
      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
      refine ⟨growth, hgrowth, hgrowthLe, ?_, ?_⟩
      · rw [hgrowthFormula]
        split
        next ht => simpa [Nat.mul_comm] using ht
        next ht =>
          have hmul1 : 10 ^ (h - 1) * 10 ^ (d.num_digits.val - 1) <
              B * 10 ^ (d.num_digits.val - 1) :=
            (Nat.mul_lt_mul_right (by positivity)).mpr hlo'
          have hmul2 : B * 10 ^ (d.num_digits.val - 1) <= B * I :=
            Nat.mul_le_mul_left B hIlower
          have he : d.num_digits.val + (h - 1) - 1 =
              (h - 1) + (d.num_digits.val - 1) := by omega
          rw [he, Nat.pow_add]
          exact Nat.le_of_lt (lt_of_lt_of_le hmul1 hmul2)
      · 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 hm := lt_trans hmul1 hmul2
          rw [Nat.mul_comm (10 ^ h), ← Nat.pow_add] at hm
          simpa using hm
        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 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)
  have hcursorInput : d.num_digits.val <= width.val := by
    rw [hwidthVal]
    omega
  have hdigitIndexed : forall j u, j < d.num_digits.val ->
      d.digits.val[j]? = some u -> u.val < 10 := by
    intro j u hj hu
    apply hdigit u
    rw [List.mem_take_iff_getElem]
    rcases getElem?_eq_some_iff.mp hu with ⟨hjlen, hju⟩
    exact ⟨j, by simp; omega, hju⟩
  obtain ⟨b0, a0, cursor, carry, hloop0⟩ :=
    loop0Total d.truncated d.digits d.num_digits
      width 0#u64 hcursorInput hcap hdigitIndexed (by simp [B])
  obtain ⟨lowDigits, hlowLen, hlowDigit, hcursorEq, hcarryBase,
      hproduct, hlowArray, houtside0, hsticky0⟩ :=
    DecimalLeftShiftFirstLoopSemantics d.digits a0 d.truncated b0
      shift d.num_digits width cursor carry hshift hcap hcursorInput
      hdigit hloop0
  have hcursorVal : cursor.val = growth.val := by
    rw [hwidthVal] at hcursorEq
    omega
  have hlowBound : F lowDigits < 10 ^ d.num_digits.val := by
    simpa [hlowLen] using foldBound lowDigits hlowDigit
  have hproduct' :
      B * I = carry.val * 10 ^ d.num_digits.val + F lowDigits := by
    simpa [B, I, F, xs, List.map_take] using hproduct
  have hcarryWidth : carry.val < 10 ^ cursor.val := by
    rw [hcursorVal]
    have hcle : carry.val * 10 ^ d.num_digits.val <= B * I := by
      rw [hproduct']
      omega
    have hpow : 10 ^ growth.val * 10 ^ d.num_digits.val =
        10 ^ (d.num_digits.val + growth.val) := by
      rw [Nat.mul_comm, ← Nat.pow_add]
    have hm : carry.val * 10 ^ d.num_digits.val <
        10 ^ growth.val * 10 ^ d.num_digits.val := by
      rw [hpow]
      exact lt_of_le_of_lt hcle hproductHi
    exact Nat.lt_of_mul_lt_mul_right hm
  have hcarryLower : 0 < growth.val ->
      10 ^ (growth.val - 1) <= carry.val := by
    intro hgpos
    by_contra hnot
    have hcsmall : carry.val < 10 ^ (growth.val - 1) := by omega
    have hu := decimalUpper carry.val (F lowDigits)
      (10 ^ (growth.val - 1)) d.num_digits.val hcsmall hlowBound
    rw [← hproduct'] at hu
    rw [Nat.mul_comm (10 ^ (growth.val - 1)), ← Nat.pow_add] at hu
    have he : d.num_digits.val + (growth.val - 1) =
        d.num_digits.val + growth.val - 1 := by omega
    rw [he] at hu
    exact (not_lt_of_ge hproductLo) hu
  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 hjcursor _ hjcarry
    have hgpos : 0 < growth.val := by omega
    have hlower := hcarryLower hgpos
    have hexp : cursor.val - j - 1 <= growth.val - 1 := by
      rw [hcursorVal]
      omega
    have hpow :
        10 ^ (cursor.val - j - 1) <= 10 ^ (growth.val - 1) :=
      Nat.pow_le_pow_right (by omega) hexp
    omega
  obtain ⟨b1, a1, hloop1⟩ :=
    loop1Total b0 a0 cursor carry hcarryWidth
  obtain ⟨highDigits, hhighLen, hhighDigit, hcarryDigits,
      hhighArray, houtside1, hsticky1⟩ :=
    DecimalLeftShiftSecondLoopSemantics a0 a1 b0 b1 cursor carry
      hcarryWidth hpad hloop1
  have headPositive (ys : List Nat) (hy : forall q, q ∈ ys -> q < 10)
      (hyspos : 0 < ys.length)
      (hlower : 10 ^ (ys.length - 1) <= F ys) :
      exists q qs, ys = q :: qs /\ 0 < q := by
    cases hys : ys with
    | nil =>
      rw [hys] at hyspos
      simp at hyspos
    | cons q qs =>
      refine ⟨q, qs, rfl, ?_⟩
      by_contra hq
      have hqzero : q = 0 := by omega
      have htailDigit : forall a, a ∈ qs -> a < 10 := by
        intro a ha
        apply hy a
        rw [hys]
        simp [ha]
      have htailBound := foldBound qs htailDigit
      have htailLower : 10 ^ qs.length <= F qs := by
        simpa [hys, F, hqzero] using hlower
      omega
  have hcellZero : a1.val[0]? != some 0#u8 := by
    simp only [bne_iff_ne]
    by_cases hgzero : growth.val = 0
    · have hcursorZero : cursor.val = 0 := by rw [hcursorVal, hgzero]
      have hcarryZero : carry.val = 0 := by
        rw [hcursorZero] at hcarryWidth
        simp at hcarryWidth
        omega
      have hlowLower : 10 ^ (lowDigits.length - 1) <= F lowDigits := by
        rw [hlowLen]
        rw [hgzero, Nat.add_zero] at hproductLo
        rw [hcarryZero] at hproduct'
        simp only [zero_mul, zero_add] at hproduct'
        rw [hproduct'] at hproductLo
        exact hproductLo
      obtain ⟨q, qs, hlowEq, hqpos⟩ :=
        headPositive lowDigits hlowDigit (by rw [hlowLen]; exact hnpos) hlowLower
      have hlowCell := hlowArray 0 (by rw [hlowEq]; simp) (by
        rw [hcursorZero]
        omega)
      have hout := houtside1 0 (by omega) (by omega)
      have hcell :
          (a1.val.map (fun z : Std.U8 => z.val))[0]? = some q := by
        rw [List.getElem?_map, hout]
        simpa [hcursorZero, hlowEq] using hlowCell
      intro hz
      have hzmap :
          (a1.val.map (fun z : Std.U8 => z.val))[0]? = some 0 := by
        rw [List.getElem?_map, hz]
        rfl
      rw [hcell] at hzmap
      have : q = 0 := Option.some.inj hzmap
      omega
    · have hgpos : 0 < growth.val := by omega
      have hhighLower :
          10 ^ (highDigits.length - 1) <= F highDigits := by
        rw [hhighLen, hcursorVal]
        simpa [F, hcarryDigits] using hcarryLower hgpos
      obtain ⟨q, qs, hhighEq, hqpos⟩ :=
        headPositive highDigits hhighDigit
          (by rw [hhighLen, hcursorVal]; exact hgpos) hhighLower
      have hcell := hhighArray 0 (by rw [hhighEq]; simp) (by omega)
      have hcell' :
          (a1.val.map (fun z : Std.U8 => z.val))[0]? = some q := by
        simpa [hhighEq] using hcell
      intro hz
      have hzmap :
          (a1.val.map (fun z : Std.U8 => z.val))[0]? = some 0 := by
        rw [List.getElem?_map, hz]
        rfl
      rw [hcell'] at hzmap
      have : q = 0 := Option.some.inj hzmap
      omega
  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]
  have hcappedPos : 0 < capped.val := by
    rw [hcappedVal, hwidthVal]
    simp only [lt_min_iff]
    omega
  let growthI : Std.I32 := UScalar.hcast .I32 growth
  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 hpointBounds :
      IScalar.min .I32 <= d.decimal_point.val + growthI.val /\
      d.decimal_point.val + growthI.val <= IScalar.max .I32 := by
    rw [hgrowthIVal]
    constructor
    · rw [IScalar.min_I32_eq]
      omega
    · rw [IScalar.max_I32_eq]
      omega
  obtain ⟨point, hpoint, hpointVal⟩ := Aeneas.Std.WP.spec_imp_exists
    (IScalar.add_spec hpointBounds.1 hpointBounds.2)
  have hpointVal' : point.val =
      d.decimal_point.val + (growth.val : Int) := by
    rw [hpointVal, hgrowthIVal]
  let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
    { num_digits := capped, decimal_point := point,
      truncated := b1, digits := a1 }
  have hpreBound : pre.num_digits.val <= pre.digits.val.length := by
    simp [pre, hcappedVal, a1.property]
  obtain ⟨d', htrim⟩ := trimTotal pre hpreBound
  have htrimSem := DecimalSeqTrimSemantics pre d' hpreBound htrim
  dsimp only at htrimSem
  rcases htrimSem with
    ⟨hdigitsEq, hpointEq, htruncEq, hnumLe, hzeros, hlast, hvalue⟩
  have hnumNonzero : d'.num_digits.val != 0 := by
    simp only [bne_iff_ne]
    intro hzero
    have hz := hzeros 0 (by omega) (by simpa [pre] using hcappedPos)
    have hz' : a1.val[0]? = some 0#u8 := by
      simpa [pre] using hz
    have hcellZero' : a1.val[0]? ≠ some 0#u8 := by
      simpa only [bne_iff_ne] using hcellZero
    exact hcellZero' hz'
  have hcall :
      num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d' := by
    unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift
    simp only [hnne, if_false, hgrowth, bind_tc_ok, hwidth, hloop0]
    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'
    rw [hloop1]
    simp only [bind_tc_ok]
    rw [hcapEval]
    simp only [bind_tc_ok, lift]
    rw [show UScalar.hcast .I32 growth = growthI by rfl]
    simp only [bind_tc_ok, hpoint]
    simpa [pre] using htrim
  have hpointOut : d'.decimal_point.val =
      d.decimal_point.val + (growth.val : Int) := by
    rw [hpointEq]
    simpa [pre] using hpointVal'
  refine ⟨d', growth.val, hcall, hgrowthLe, hpointOut, ?_, ?_,
    hnumNonzero⟩
  · rw [hpointOut]
    omega
  · rw [hpointOut]
    omega

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (117)
More program dependencies (84)
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": "DecimalLeftShiftLoop1Execution",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "89381916b4c8be60a99b4df09969d2397e4abe3a7f3fa96aae4f8778c23f5c64",
  "active_statement_hash": "88a05276c3f3123a43e3bd7a70414d42c7c1394f5480709bdca02ff2c64b68c8",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "576e6bf9480cfd32f53c309c7ac8fb115c1ba6e413f299ed5ff9fa7c647e6769",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-181",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalLeftShiftDigitGrowth": "6dcf8858df4713eb",
    "DecimalLeftShiftFirstLoopSemantics": "d126fe9b83dfc5f3",
    "DecimalLeftShiftSecondLoopSemantics": "ccbfee7a0aef7445",
    "DecimalLeftShiftTableCertificate": "2f00a3779f444f8c",
    "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_DecimalSeq_trim": "adfe9a4161cd7fd6",
    "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": "fd298e37d5ae4f86",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_trim": "faa9fd92b8c29c14",
    "num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift": "889854c120bac844",
    "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": "Lean proof closed; predictive prose review no longer required",
  "assessment_status": null,
  "fingerprint_relation": "unknown",
  "assessment_fingerprint": null,
  "current_fingerprint": "sound-v2:cabaa5aa5d3049bad7d1f056f9845ed5c35b54521b3c4f5728c612be40f1182b",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}