DecimalSeqRightShiftRecordSemantics

Lean checked

Statement

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

Lemma · Returned-record semantics of decimal right shift

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

Lean statement

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

import Tablet.DecimalSeqTrimSemantics
import Tablet.DecimalRightShiftPrefixLoopSemantics
import Tablet.DecimalRightShiftMainLoopSemantics
import Tablet.DecimalRightShiftU64DyadicBridge
import Tablet.DecimalRightShiftFinalLoopSemantics
import Tablet.ResultLoopPartial
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

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

Natural-language proof

The status above describes the Lean proof. Its explanatory prose may remain a sketch after that proof closes.

Explanatory prose status

Prose retained as authored. The explanatory prose is preserved as it stood when captured.

Proof

Put n=d.\mathit{num\_digits}, p=d.\mathit{decimal\_point}, B=2^k, and let I be the base-ten fold of the stored prefix. Since 10^{p-n}>0, the hypothesis V(d)>0 gives I>0. In particular the prefix contains a nonzero decimal cell.
Unfold num imp dec2flt decimal seq DecimalSeq right shift. The first result loop and its nested zero-extension loop have exactly the postcondition proved by DecimalRightShiftPrefixLoopSemantics. Apply that helper to the successful call from read index zero, accumulator zero, and false all-zero flag. Mapping all 768 cells and then taking n commutes with taking the U8 cells first and then mapping their natural values. The helper therefore returns the original record, keeps the all-zero flag false, and supplies an index r\ge1 and accumulator c satisfying
B\le c<10B. \tag{1}
If r\le n, it also gives r\le768 and says that c is exactly the fold of the first r stored digits. If r>n, it gives n\le768, r\le n+k, and c=I10^{r-n}. Thus in either phase r\le828, so the extracted cast of r to I32, the subtraction of one, and the subsequent decimal-point subtraction have precisely their ordinary integer meanings. In particular, they compute the prospective output point
p'=p-(r-1). \tag{2}
The constant in num imp dec2flt decimal seq DecimalSeq DECIMAL POINT RANGE is 2047. If p'<-2047, the next branch returns exactly the record (R): it explicitly sets count and point to zero and the Boolean to false. This establishes the first alternative of the lemma without making any claim about the discarded mathematical value.
Assume henceforth that this reset test is false. The function forms 1\ll k and subtracts one. Apply DecimalRightShiftU64DyadicBridge directly to these successful extracted Usize-count operations and to each successful quotient and mask operation in the two output loops. It gives B=2^k, the mask M=B-1, and, for every U64 accumulator x occurring in those applications,
x\mathbin{\mathtt{>>>}}k=\lfloor x/B\rfloor,\qquad x\mathbin{\mathtt{\&}}M=x\bmod B. \tag{3}
The same bridge supplies the Euclidean package
x=(x/B)B+(x\bmod B),\qquad 0\le x\bmod B<B, \tag{4}
as well as the uniform checked-arithmetic bound 10(x\bmod B)+a<2^{64} for every decimal digit a<10.
For the positional account, let a_0 be the immutable original digit array, extend its stored word by an infinite zero tail, and let A_s be the fold of the first s extended digits. Thus A_s=I10^{s-n} for s\ge n, and DecimalRightShiftPrefixLoopSemantics gives c=A_r, r\ge1, c<10B, and, in the case r\le n, also r\le768.
If r<n, apply DecimalRightShiftMainLoopSemantics with limit n, array a_0, initial read cursor r, initial write cursor zero, and accumulator c. Its hypotheses are precisely the prefix facts just listed, the original prefix digit hypothesis, and the successful power, mask, and main-loop calls obtained by unfolding the right shift. The helper proves n\le768 and returns a list E of exactly n-r decimal quotient digits, write count w=n-r, terminal accumulator c_* with c_*<10B, the entire emitted prefix in the output array, and preservation of every cell at or above w, in particular the original suffix at and above n. Its exact long-division equation is
c10^{n-r}+\operatorname{fold}_{10}(a_0[r:n]) =B\bigl(10\operatorname{fold}_{10}(E)\bigr)+c_* . \tag{5}
Because c=A_r, the left side is A_n=I, so (5) is the required terminal positional invariant. The relevant main-loop invariant carries c_s<10B, the cursor equation w=s-r, the emitted prefix, and equality of the unread suffix with a_0. Since r\ge1, it gives w<s, so the body reads the immutable original digit a_0[s]<10, not a mutable-array surrogate. With (3)--(4), its recurrence is
c_s=Bq_s+e_s,\qquad c_{s+1}=10e_s+a_0[s] \le10(B-1)+9<10B \tag{6}
both validates every quotient cast and preserves the strict bound.
If r\ge n, the main loop has no body step and returns immediately with w=0 and accumulator c=A_r. This is the same terminal positional invariant, with an empty emitted list. If r=n, the prefix helper's first case gives n\le768; if r>n, its second case gives that bound. Both cases also give c<10B. Thus in every case the state entering the final zero-extension loop has an emitted decimal list, the exact long-division identity, a write count at most 768, and an accumulator strictly below 10B.
There may remain a nonzero remainder accumulator after the input is exhausted. Apply DecimalRightShiftFinalLoopSemantics to the successful final zero-extension call, using the already established w\le768, c<10B, and the same successful power and mask operations. It supplies a finite decimal-digit list Z, terminal array and cursor, and the exact equation
c10^{|Z|}=B\bigl(10F(Z)\bigr). \tag{11}
Its cursor clause is w'=\min(w+|Z|,768). Its array clauses say that every emitted destination below 768 contains the corresponding digit, that cells below the initial cursor are preserved, and that cells at or above the final cursor are preserved. Finally its Boolean clause says exactly that the final Boolean is the incoming Boolean or some nonzero digit of Z had destination at least 768. The helper proves the U64 bounds, quotient cast, exact cap constant from num imp dec2flt decimal seq DecimalSeq MAX DIGITS, and finite dyadic termination from the extracted loop body and DecimalRightShiftU64DyadicBridge; hence these conclusions cover the whole successful final-loop execution.
At termination the accumulator in (5) is zero. If L digits have been emitted, the extended index is r+L, so
A_{r+L}=I10^{r+L-n}=B(10Q_L).
It follows that
I/B=Q_L10^{n-r-L+1}. \tag{12}
This exponent is exactly the adjustment needed by the prospective point p'=p-r+1.
Let b_0,\ldots,b_{L-1} be the finite decimal expansion emitted by the two output loops before the cap, let m=\min(L,768), and let J be the fold of its first m digits. Equations (2), (5), (6), and (11) are exactly base-ten long division, so the uncapped expansion represents V(d)/B at point p'. Define the omitted suffix value
\rho=\left(\sum_{j=768}^{L-1}b_j10^{L-1-j}\right) 10^{p'-L}, \tag{13}
using zero when L\le768. Then \rho\ge0, and it is positive exactly when at least one omitted output digit is positive. Splitting the expansion at index m gives the pre-trim identity
J10^{p'-m}=V(d)/B-\rho. \tag{14}
The pre-trim count is at most 768 by the cap invariant. Therefore DecimalSeqTrimSemantics applies to the terminal trim and preserves both (14) and the truncation Boolean; it also preserves the prospective point p', so d'.\mathit{decimal\_point}=p'.
For the strict loss bound, first suppose the omitted suffix fold is zero. Then \rho=0<10^{p'-768} because the base 10 is positive. Otherwise the suffix is nonempty. Hence L>m, and m=\min(L,768) forces m=768. Every emitted cell is a decimal digit by the two loop postconditions. A direct induction on the L-768 omitted cells, using 10A+z<10\cdot10^s whenever A<10^s and z<10, gives
\operatorname{fold}_{10}(b_{768},\ldots,b_{L-1})<10^{L-768}.
Multiplication by the positive scale 10^{p'-L} now yields
\rho<10^{L-768}10^{p'-L}=10^{p'-768} =10^{d'.\mathit{decimal\_point}-768}.
This estimate concerns only the suffix lost in this right shift. It makes no claim about a sum of losses from distinct shifts. The final Boolean analysis above is
d'.\mathit{truncated} \Longleftrightarrow d.\mathit{truncated}\ \text{or}\ \rho>0.
Together with B=2^k, (14), and the strict estimate, this is precisely the nonreset alternative of the statement.

Lean proof


  dsimp only
  intro d d' shift hshift hvalue hdigit hcall
  let xs := d.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.take d.num_digits.val)
  let B := 2 ^ shift.val
  have hscale : 0 < (10 : Real) ^
      (d.decimal_point.val - (d.num_digits.val : Int)) :=
    zpow_pos (by norm_num) _
  have hIreal : 0 < (I : Real) := by
    have hv : 0 < (I : Real) * (10 : Real) ^
        (d.decimal_point.val - (d.num_digits.val : Int)) := by
      simpa [I, F, xs, List.map_take] using hvalue
    nlinarith
  have hIpos : 0 < I := by exact_mod_cast hIreal
  unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift at hcall
  cases hloop0 :
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0
        d shift 0#usize 0#u64 false with
  | fail e => simp [hloop0] at hcall
  | div => simp [hloop0] at hcall
  | ok out0 =>
    rcases out0 with ⟨d0, read, acc, allZero⟩
    simp only [hloop0, bind_tc_ok] at hcall
    have hpref := DecimalRightShiftPrefixLoopSemantics d d0 shift read acc
      allZero hshift (by simpa [I, F, xs, List.map_take] using hIpos) hdigit hloop0
    dsimp only at hpref
    rcases hpref with ⟨rfl, rfl, hreadpos, hBacc, hacc10, hphase⟩
    let reset : num.imp.dec2flt.decimal_seq.DecimalSeq :=
      { num_digits := 0#usize,
        decimal_point := 0#i32,
        truncated := false,
        digits := d0.digits }
    change (do
      let i ← lift (UScalar.hcast .I32 read)
      let i1 ← i - 1#i32
      let i2 ← d0.decimal_point - i1
      let i3 ← -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
      if i2 < i3 then
        ok reset
      else
        let i4 ← 1#u64 <<< shift
        let mask ← i4 - 1#u64
        let (ac, wi, n1) ←
          num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
            d0.num_digits d0.digits shift read 0#usize acc mask
        let (bc, ac1, wi1) ←
          num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
            d0.truncated ac shift wi n1 mask
        num.imp.dec2flt.decimal_seq.DecimalSeq.trim
          { num_digits := wi1, decimal_point := i2, truncated := bc,
            digits := ac1 }) = ok d' at hcall
    let readI : Std.I32 := UScalar.hcast .I32 read
    rw [show UScalar.hcast .I32 read = readI by rfl] at hcall
    simp only [lift, bind_tc_ok] at hcall
    have hreadBound : read.val <= d0.num_digits.val + shift.val := by
      rcases hphase with hphase | hphase
      · omega
      · omega
    have hread828 : read.val <= 828 := by
      rcases hphase with hphase | hphase <;> omega
    have hreadIVal : readI.val = (read.val : Int) := by
      have hs := UScalar.hcast_inBounds_spec .I32 read (by
        rw [IScalar.max_I32_eq]
        omega)
      simp only [lift, WP.spec_ok] at hs
      exact hs
    cases hsub : readI - 1#i32 with
    | fail e =>
      rw [hsub] at hcall
      simp at hcall
    | div =>
      rw [hsub] at hcall
      simp at hcall
    | ok readMinus =>
      rw [hsub] at hcall
      simp only [bind_tc_ok] at hcall
      have hsubVal : readMinus.val = readI.val - 1 := by
        have hs := IScalar.sub_equiv readI 1#i32
        rw [hsub] at hs
        exact hs.2.1
      cases hpoint : d0.decimal_point - readMinus with
      | fail e => simp [hpoint] at hcall
      | div => simp [hpoint] at hcall
      | ok point =>
        rw [hpoint] at hcall
        simp only [bind_tc_ok] at hcall
        have hpointVal : point.val = d0.decimal_point.val - (read.val : Int) + 1 := by
          have hs := IScalar.sub_equiv d0.decimal_point readMinus
          rw [hpoint] at hs
          rw [hsubVal, hreadIVal] at hs
          omega
        cases hnegRange : -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE with
        | fail e => simp [hnegRange] at hcall
        | div => simp [hnegRange] at hcall
        | ok negRange =>
          rw [hnegRange] at hcall
          simp only [bind_tc_ok] at hcall
          by_cases hreset : point < negRange
          · simp only [hreset, if_true] at hcall
            have hd' : d' = reset := by simpa using hcall.symm
            subst d'
            exact Or.inl ⟨by simp [reset], by simp [reset], by simp [reset]⟩
          · simp only [hreset, if_false] at hcall
            cases hpow : 1#u64 <<< shift with
            | fail e => simp [hpow] at hcall
            | div => simp [hpow] at hcall
            | ok pow =>
              rw [hpow] at hcall
              simp only [bind_tc_ok] at hcall
              cases hmask : pow - 1#u64 with
              | fail e => simp [hmask] at hcall
              | div => simp [hmask] at hcall
              | ok mask =>
                rw [hmask] at hcall
                simp only [bind_tc_ok] at hcall
                cases hloop1 :
                    num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
                      d0.num_digits d0.digits shift read 0#usize acc mask with
                | fail e => simp [hloop1] at hcall
                | div => simp [hloop1] at hcall
                | ok out1 =>
                  rcases out1 with ⟨a1, write1, acc1⟩
                  rw [hloop1] at hcall
                  simp only [bind_tc_ok] at hcall
                  have hmain : d0.num_digits.val <= 768 /\
                      exists low : List Nat,
                        low.length = d0.num_digits.val - read.val /\
                        (forall z, z ∈ low -> z < 10) /\
                        write1.val = d0.num_digits.val - read.val /\
                        acc1.val < 10 * B /\
                        acc.val * 10 ^ (d0.num_digits.val - read.val) +
                            F ((xs.take d0.num_digits.val).drop read.val) =
                          B * (10 * F low) + acc1.val /\
                        (forall j, j < low.length ->
                          (a1.val.map (fun z : Std.U8 => z.val))[j]? = low[j]?) /\
                        (forall j, j < 768 -> write1.val <= j ->
                          a1.val[j]? = d0.digits.val[j]?) /\
                        (forall j, j < 768 -> d0.num_digits.val <= j ->
                          a1.val[j]? = d0.digits.val[j]?) := by
                    by_cases hrn : read.val < d0.num_digits.val
                    · have hm := DecimalRightShiftMainLoopSemantics
                        d0.digits a1 shift read d0.num_digits write1 pow mask acc acc1
                        hshift hreadpos (by omega) (by omega) hacc10 hdigit hpow hmask
                        hloop1
                      simpa [xs, F, B] using hm
                    · have hreadGe : d0.num_digits.val <= read.val := by omega
                      have hreadWord : ¬ read < d0.num_digits := by
                        simpa [UScalar.lt_equiv] using hrn
                      have hloop1Direct :
                          num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
                            d0.num_digits d0.digits shift read 0#usize acc mask =
                            ok (d0.digits, 0#usize, acc) := by
                        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
                        rw [Aeneas.Std.loop.eq_1]
                        simp [num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1.body,
                          hreadWord]
                      rw [hloop1Direct] at hloop1
                      cases hloop1
                      have hnBound : d0.num_digits.val <= 768 := by
                        rcases hphase with hp | hp <;> omega
                      refine ⟨hnBound, [], by simp [Nat.sub_eq_zero_of_le hreadGe],
                        by simp, by simp [Nat.sub_eq_zero_of_le hreadGe], hacc10,
                        ?_, ?_, ?_, ?_⟩
                      · have hdrop : (xs.take d0.num_digits.val).drop read.val = [] :=
                          List.drop_eq_nil_of_le (by simp; omega)
                        have hsubzero : d0.num_digits.val - read.val = 0 :=
                          Nat.sub_eq_zero_of_le hreadGe
                        simp [F, hdrop, hsubzero]
                      · intro j hj
                        simp at hj
                      · intro j hj _
                        rfl
                      · intro j hj _
                        rfl
                  rcases hmain with ⟨_, low, hlowLen, hlowDigit, hwrite1,
                    hacc1, hmainNum, hlowArray, houtside1, hhigh1⟩
                  cases hloop2 :
                      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
                        d0.truncated a1 shift write1 acc1 mask with
                  | fail e => simp [hloop2] at hcall
                  | div => simp [hloop2] at hcall
                  | ok out2 =>
                    rcases out2 with ⟨b2, a2, write2⟩
                    change (do
                      let (bc, ac, wi) ←
                        num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
                          d0.truncated a1 shift write1 acc1 mask
                      num.imp.dec2flt.decimal_seq.DecimalSeq.trim
                        { num_digits := wi, decimal_point := point,
                          truncated := bc, digits := ac }) = ok d' at hcall
                    rw [hloop2] at hcall
                    simp only [bind_tc_ok] at hcall
                    have hfinal := DecimalRightShiftFinalLoopSemantics
                      a1 a2 d0.truncated b2 shift write1 write2 pow mask acc1
                      hshift (by omega) hacc1 hpow hmask hloop2
                    dsimp only at hfinal
                    rcases hfinal with ⟨high, hhighDigit, hfinalNum, hwrite2,
                      hhighArray, hbelow2, houtside2, hsticky2⟩
                    let all := low ++ high
                    have hallDigit : forall z, z ∈ all -> z < 10 := by
                      intro z hz
                      simp only [all, List.mem_append] at hz
                      rcases hz with hz | hz
                      · exact hlowDigit z hz
                      · exact hhighDigit z hz
                    have hallLen : all.length = low.length + high.length := by simp [all]
                    have hallFold : F all = F low * 10 ^ high.length + F high := by
                      simp only [all, F, List.foldl_append]
                      have foldFrom (ys : List Nat) (n : Nat) :
                          ys.foldl (fun x z => 10 * x + z) n =
                            n * 10 ^ ys.length + F ys := by
                        induction ys generalizing n with
                        | nil => simp [F]
                        | cons y ys ih =>
                          simp only [List.foldl_cons, List.length_cons]
                          rw [ih, pow_succ]
                          change (10 * n + y) * 10 ^ ys.length + F ys =
                            n * (10 ^ ys.length * 10) +
                              (y :: ys).foldl (fun x z => 10 * x + z) 0
                          simp only [List.foldl_cons]
                          rw [ih]
                          ring
                      simpa using foldFrom high (F low)
                    have hcombined :
                        acc.val * 10 ^ (d0.num_digits.val - read.val + high.length) +
                            F ((xs.take d0.num_digits.val).drop read.val) *
                              10 ^ high.length =
                          B * (10 * F all) := by
                      rw [hallFold]
                      calc
                        acc.val * 10 ^ (d0.num_digits.val - read.val + high.length) +
                            F ((xs.take d0.num_digits.val).drop read.val) *
                              10 ^ high.length =
                            (acc.val * 10 ^ (d0.num_digits.val - read.val) +
                              F ((xs.take d0.num_digits.val).drop read.val)) *
                                10 ^ high.length := by rw [pow_add]; ring
                        _ = (B * (10 * F low) + acc1.val) *
                              10 ^ high.length := by rw [hmainNum]
                        _ = B * (10 * F low) * 10 ^ high.length +
                              acc1.val * 10 ^ high.length := by ring
                        _ = B * (10 * (F low * 10 ^ high.length + F high)) := by
                          rw [hfinalNum]
                          ring
                    let extra := read.val - d0.num_digits.val + high.length
                    have foldFrom2 (ys : List Nat) (n : Nat) :
                        ys.foldl (fun x z => 10 * x + z) n =
                          n * 10 ^ ys.length + F ys := by
                      induction ys generalizing n with
                      | nil => simp [F]
                      | cons y ys ih =>
                        simp only [List.foldl_cons, List.length_cons]
                        rw [ih, pow_succ]
                        change (10 * n + y) * 10 ^ ys.length + F ys =
                          n * (10 ^ ys.length * 10) +
                            (y :: ys).foldl (fun x z => 10 * x + z) 0
                        simp only [List.foldl_cons]
                        rw [ih]
                        ring
                    have hterminal : I * 10 ^ extra = B * (10 * F all) := by
                      rcases hphase with hp | hp
                      · rcases hp with ⟨hrn, _, haccPrefix⟩
                        have htake : (xs.take d0.num_digits.val).take read.val =
                            xs.take read.val := by
                          rw [List.take_take]
                          simp [Nat.min_eq_left hrn]
                        have hsplit : I =
                            acc.val * 10 ^ (d0.num_digits.val - read.val) +
                              F ((xs.take d0.num_digits.val).drop read.val) := by
                          have hdropLen :
                              ((xs.take d0.num_digits.val).drop read.val).length =
                                d0.num_digits.val - read.val := by
                            simp [xs, d0.digits.property]
                            omega
                          change F (xs.take d0.num_digits.val) = _
                          conv_lhs =>
                            rw [← List.take_append_drop read.val
                              (xs.take d0.num_digits.val)]
                          simp only [F, List.foldl_append]
                          rw [foldFrom2, htake, hdropLen]
                          rw [← haccPrefix]
                        have hrsub : read.val - d0.num_digits.val = 0 :=
                          Nat.sub_eq_zero_of_le hrn
                        rw [show extra = high.length by simp [extra, hrsub]]
                        calc
                          I * 10 ^ high.length =
                              (acc.val * 10 ^ (d0.num_digits.val - read.val) +
                                F ((xs.take d0.num_digits.val).drop read.val)) *
                                  10 ^ high.length := by rw [hsplit]
                          _ = acc.val * 10 ^
                                  (d0.num_digits.val - read.val + high.length) +
                                F ((xs.take d0.num_digits.val).drop read.val) *
                                  10 ^ high.length := by rw [pow_add]; ring
                          _ = B * (10 * F all) := hcombined
                      · rcases hp with ⟨hnr, _, _, haccExtended⟩
                        have hnsub : d0.num_digits.val - read.val = 0 :=
                          Nat.sub_eq_zero_of_le (by omega)
                        have hdrop :
                            (xs.take d0.num_digits.val).drop read.val = [] :=
                          List.drop_eq_nil_of_le (by simp; omega)
                        calc
                          I * 10 ^ extra =
                              (I * 10 ^ (read.val - d0.num_digits.val)) *
                                10 ^ high.length := by
                                  rw [show extra =
                                    (read.val - d0.num_digits.val) + high.length by rfl,
                                    pow_add]
                                  ring
                          _ = acc.val * 10 ^ high.length := by rw [haccExtended]
                          _ = acc.val * 10 ^
                                  (d0.num_digits.val - read.val + high.length) +
                                F ((xs.take d0.num_digits.val).drop read.val) *
                                  10 ^ high.length := by simp [hnsub, hdrop, F]
                          _ = B * (10 * F all) := hcombined
                    have hlowWrite : write1.val = low.length := by omega
                    have hlowCap : low.length <= 768 := by omega
                    have hwriteAll : write2.val = min all.length 768 := by
                      rw [hwrite2, hwrite1, hallLen, hlowLen]
                    have hwriteCap : write2.val <= 768 := by
                      rw [hwriteAll]
                      exact Nat.min_le_right _ _
                    have hwriteLen : write2.val <= all.length := by
                      rw [hwriteAll]
                      exact Nat.min_le_left _ _
                    let kept := all.take write2.val
                    let tail := all.drop write2.val
                    have hpreDigits :
                        (a2.val.take write2.val).map (fun z => z.val) = kept := by
                      simp only [kept, List.map_take]
                      apply List.ext_getElem
                      · simp [hwriteCap, hwriteLen, a2.property]
                      · intro j hj1 hj2
                        have hjm : j < write2.val := by
                          simpa [List.length_take, hwriteCap, a2.property] using hj1
                        have hj768 : j < 768 := by omega
                        by_cases hjlow : j < low.length
                        · have ha1 := hlowArray j hjlow
                          have ha2raw := hbelow2 j hj768 (by omega)
                          have ha2 :
                              (a2.val.map (fun z : Std.U8 => z.val))[j]? = low[j]? := by
                            rw [List.getElem?_map, ha2raw]
                            simpa [List.getElem?_map] using ha1
                          have hall : all[j]? = low[j]? := by
                            rw [List.getElem?_append_left (by simpa using hjlow)]
                          have hmapBound :
                              j < (a2.val.map (fun z : Std.U8 => z.val)).length := by
                            simp [a2.property]
                            exact hj768
                          have hallBound : j < all.length := by omega
                          rw [show ((a2.val.map (fun z : Std.U8 => z.val)).take
                              write2.val)[j] =
                              (a2.val.map (fun z : Std.U8 => z.val))[j] by simp]
                          rw [show (all.take write2.val)[j] = all[j] by simp]
                          apply Option.some.inj
                          calc
                            some (a2.val.map (fun z : Std.U8 => z.val))[j] =
                                (a2.val.map (fun z : Std.U8 => z.val))[j]? :=
                              (List.getElem?_eq_getElem hmapBound).symm
                            _ = low[j]? := ha2
                            _ = all[j]? := hall.symm
                            _ = some all[j] := List.getElem?_eq_getElem hallBound
                        · let t := j - low.length
                          have ht : t < high.length := by
                            simp only [t]
                            simp [all] at hj2
                            omega
                          have hjEq : write1.val + t = j := by
                            simp only [t]
                            omega
                          have ha2 := hhighArray t ht (by omega)
                          have hall : all[j]? = high[t]? := by
                            rw [List.getElem?_append_right (by simpa using hjlow)]
                          have hmapBound :
                              j < (a2.val.map (fun z : Std.U8 => z.val)).length := by
                            simp [a2.property]
                            exact hj768
                          have hallBound : j < all.length := by omega
                          rw [show ((a2.val.map (fun z : Std.U8 => z.val)).take
                              write2.val)[j] =
                              (a2.val.map (fun z : Std.U8 => z.val))[j] by simp]
                          rw [show (all.take write2.val)[j] = all[j] by simp]
                          rw [hjEq] at ha2
                          apply Option.some.inj
                          calc
                            some (a2.val.map (fun z : Std.U8 => z.val))[j] =
                                (a2.val.map (fun z : Std.U8 => z.val))[j]? :=
                              (List.getElem?_eq_getElem hmapBound).symm
                            _ = high[t]? := ha2
                            _ = all[j]? := hall.symm
                            _ = some all[j] := List.getElem?_eq_getElem hallBound
                    have foldPos (ys : List Nat) :
                        0 < F ys <->
                          exists j, j < ys.length /\ ys[j]? != some 0 := by
                      induction ys with
                      | nil => simp [F]
                      | cons y ys ih =>
                        have hform : F (y :: ys) =
                            y * 10 ^ ys.length + F ys := by
                          simpa [F] using foldFrom2 ys y
                        rw [hform]
                        constructor
                        · intro hp
                          by_cases hy : y = 0
                          · have ht : 0 < F ys := by simpa [hy] using hp
                            rcases ih.mp ht with ⟨j, hj, hjnz⟩
                            exact ⟨j + 1, by simp; omega, by simpa using hjnz⟩
                          · exact ⟨0, by simp, by simp [hy]⟩
                        · rintro ⟨j, hj, hjnz⟩
                          cases j with
                          | zero =>
                            have hyne : y ≠ 0 := by
                              intro hy
                              subst y
                              simp at hjnz
                            have hy : 0 < y := Nat.pos_of_ne_zero hyne
                            have hpow : 0 < 10 ^ ys.length := by positivity
                            positivity
                          | succ j =>
                            have hjlt : j < ys.length := by simp at hj; omega
                            have ht : 0 < F ys :=
                              ih.mpr ⟨j, hjlt, by simpa using hjnz⟩
                            omega
                    have htailSticky :
                        (exists t, t < tail.length /\ tail[t]? != some 0) <->
                        (exists j, j < high.length /\
                          768 <= write1.val + j /\ high[j]? != some 0) := by
                      constructor
                      · rintro ⟨t, ht, htnz⟩
                        have ht' : t < all.length - write2.val := by
                          simpa [tail] using ht
                        have hwriteEq : write2.val = 768 := by
                          rw [hwriteAll]
                          omega
                        let g := write2.val + t
                        have hgall : g < all.length := by simp [g]; omega
                        have hglow : low.length <= g := by
                          simp [g, hwriteEq]
                          omega
                        let j := g - low.length
                        have hj : j < high.length := by
                          simp [all] at hgall
                          simp [j]
                          omega
                        have hdest : write1.val + j = g := by
                          rw [hlowWrite]
                          simp [j]
                          omega
                        have htailGet : tail[t]? = all[g]? := by
                          simp [tail, g, List.getElem?_drop]
                        have hallHigh : all[g]? = high[j]? := by
                          simp only [all, List.getElem?_append_right (by omega)]
                          simp [j]
                        refine ⟨j, hj, ?_, ?_⟩
                        · rw [hdest]
                          simp [g, hwriteEq]
                        · rw [htailGet, hallHigh] at htnz
                          exact htnz
                      · rintro ⟨j, hj, hjcap, hjnz⟩
                        let g := low.length + j
                        have hgall : g < all.length := by simp [g, all, hj]
                        have hdest : write1.val + j = g := by simp [g, hlowWrite]
                        have hgcap : 768 <= g := by simpa [hdest] using hjcap
                        have hwriteEq : write2.val = 768 := by
                          rw [hwriteAll]
                          omega
                        let t := g - write2.val
                        have ht : t < tail.length := by
                          simp [tail, t]
                          omega
                        have htailGet : tail[t]? = all[g]? := by
                          rw [List.getElem?_drop]
                          simp [t]
                          congr 1
                          omega
                        have hallHigh : all[g]? = high[j]? := by
                          simp only [all]
                          rw [List.getElem?_append_right (by simp [g])]
                          simp [g]
                        refine ⟨t, ht, ?_⟩
                        rw [htailGet, hallHigh]
                        exact hjnz
                    have decimalScale (I0 Q0 B0 extra0 L0 n0 : Nat)
                        (p0 point0 : Int) (hB0 : 0 < B0)
                        (hnum0 : I0 * 10 ^ extra0 = B0 * (10 * Q0))
                        (hexp0 : point0 - (L0 : Int) + (extra0 : Int) =
                          p0 - (n0 : Int) + 1) :
                        (Q0 : Real) * (10 : Real) ^ (point0 - (L0 : Int)) =
                          (I0 : Real) * (10 : Real) ^ (p0 - (n0 : Int)) /
                            (B0 : Real) := by
                      have hnumR : (I0 : Real) * (10 : Real) ^ extra0 =
                          (B0 : Real) * (10 * (Q0 : Real)) := by
                        exact_mod_cast hnum0
                      have h10 : (10 : Real) ≠ 0 := by norm_num
                      have hBR : (B0 : Real) ≠ 0 := by positivity
                      apply (eq_div_iff hBR).2
                      have hpows :
                          (10 : Real) ^ (point0 - (L0 : Int)) *
                              (10 : Real) ^ (extra0 : Int) =
                            (10 : Real) ^ (p0 - (n0 : Int) + 1) := by
                        rw [← zpow_add₀ h10, hexp0]
                      have hpowExtra : (10 : Real) ^ (extra0 : Int) =
                          (10 : Real) ^ extra0 := by
                        norm_num only [zpow_natCast]
                      have hp : (10 : Real) ^ (p0 - (n0 : Int) + 1) =
                          (10 : Real) ^ (p0 - (n0 : Int)) * 10 := by
                        rw [zpow_add₀ h10]
                        norm_num
                      calc
                        (Q0 : Real) * (10 : Real) ^ (point0 - (L0 : Int)) *
                            (B0 : Real) =
                            ((B0 : Real) * (10 * (Q0 : Real))) / 10 *
                              (10 : Real) ^ (point0 - (L0 : Int)) := by ring
                        _ = ((I0 : Real) * (10 : Real) ^ extra0) / 10 *
                              (10 : Real) ^ (point0 - (L0 : Int)) := by
                          rw [hnumR]
                        _ = (I0 : Real) *
                              ((10 : Real) ^ (point0 - (L0 : Int)) *
                                (10 : Real) ^ (extra0 : Int)) / 10 := by
                          rw [hpowExtra]
                          ring
                        _ = (I0 : Real) *
                              (10 : Real) ^ (p0 - (n0 : Int)) := by
                          rw [hpows, hp]
                          ring
                    have hexp : point.val - (all.length : Int) + (extra : Int) =
                        d0.decimal_point.val - (d0.num_digits.val : Int) + 1 := by
                      rw [hpointVal, hallLen, hlowLen]
                      simp only [extra, Nat.cast_add]
                      rcases hphase with hp | hp
                      · have hrsub : read.val - d0.num_digits.val = 0 :=
                          Nat.sub_eq_zero_of_le hp.1
                        rw [hrsub]
                        simp
                        rw [Nat.cast_sub hp.1]
                        omega
                      · have hnle : d0.num_digits.val <= read.val := by omega
                        have hnsub : d0.num_digits.val - read.val = 0 :=
                          Nat.sub_eq_zero_of_le hnle
                        rw [hnsub]
                        simp
                        rw [Nat.cast_sub hnle]
                        omega
                    have hBpos : 0 < B := by simp [B]
                    have hfullValue :
                        (F all : Real) *
                            (10 : Real) ^ (point.val - (all.length : Int)) =
                          (I : Real) * (10 : Real) ^
                              (d0.decimal_point.val -
                                (d0.num_digits.val : Int)) / (B : Real) :=
                      decimalScale I (F all) B extra all.length
                        d0.num_digits.val d0.decimal_point.val point.val hBpos
                        hterminal hexp
                    have capSplit (ys : List Nat) (m : Nat) (p0 : Int)
                        (hm : m <= ys.length) :
                        (F ys : Real) *
                            (10 : Real) ^ (p0 - (ys.length : Int)) =
                          (F (ys.take m) : Real) *
                              (10 : Real) ^ (p0 - (m : Int)) +
                            (F (ys.drop m) : Real) *
                              (10 : Real) ^ (p0 - (ys.length : Int)) := by
                      have hfold : F ys = F (ys.take m) * 10 ^ (ys.length - m) +
                          F (ys.drop m) := by
                        conv_lhs => rw [← List.take_append_drop m ys]
                        simp only [F, List.foldl_append]
                        rw [foldFrom2]
                        congr 2
                        simp
                      have hexp' : p0 - (m : Int) =
                          (p0 - (ys.length : Int)) +
                            ((ys.length - m : Nat) : Int) := by
                        rw [Nat.cast_sub hm]
                        omega
                      have hpow : (10 : Real) ^ (p0 - (m : Int)) =
                          (10 : Real) ^ (p0 - (ys.length : Int)) *
                            (10 : Real) ^ (ys.length - m) := by
                        rw [hexp', zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
                        norm_num only [zpow_natCast]
                      rw [show (F ys : Real) =
                          (F (ys.take m) : Real) *
                              (10 : Real) ^ (ys.length - m) +
                            (F (ys.drop m) : Real) by exact_mod_cast hfold]
                      rw [hpow]
                      ring
                    have hcapSplit :
                        (F all : Real) *
                            (10 : Real) ^ (point.val - (all.length : Int)) =
                          (F kept : Real) *
                              (10 : Real) ^ (point.val - (write2.val : Int)) +
                            (F tail : Real) *
                              (10 : Real) ^ (point.val - (all.length : Int)) := by
                      simpa [kept, tail] using
                        capSplit all write2.val point.val hwriteLen
                    let rho : Real := (F tail : Real) *
                      (10 : Real) ^ (point.val - (all.length : Int))
                    have hrho0 : 0 <= rho := by
                      dsimp only [rho]
                      exact mul_nonneg (Nat.cast_nonneg _) (le_of_lt
                        (zpow_pos (by norm_num : (0 : Real) < 10) _))
                    have hrhoPos : 0 < rho <-> 0 < F tail := by
                      dsimp only [rho]
                      have hp : 0 < (10 : Real) ^
                          (point.val - (all.length : Int)) :=
                        zpow_pos (by norm_num) _
                      constructor
                      · intro h
                        have : 0 < (F tail : Real) := by nlinarith
                        exact_mod_cast this
                      · intro h
                        have hc : 0 < (F tail : Real) := by exact_mod_cast h
                        positivity
                    have hrhoSticky :
                        0 < rho <->
                          exists j, j < high.length /\
                            768 <= write1.val + j /\ high[j]? != some 0 := by
                      rw [hrhoPos, foldPos, htailSticky]
                    have foldBound (ys : List Nat)
                        (hd : forall z, z ∈ ys -> z < 10) :
                        F ys < 10 ^ ys.length := by
                      induction ys with
                      | nil => simp [F]
                      | cons y ys ih =>
                        have hy : y < 10 := hd y (by simp)
                        have hys : forall z, z ∈ ys -> z < 10 := by
                          intro z hz
                          exact hd z (by simp [hz])
                        have hi := ih hys
                        rw [show F (y :: ys) =
                            y * 10 ^ ys.length + F ys by
                          simpa [F] using foldFrom2 ys y,
                          List.length_cons, pow_succ]
                        have hmul : y * 10 ^ ys.length <=
                            9 * 10 ^ ys.length :=
                          Nat.mul_le_mul_right _ (by omega)
                        calc
                          y * 10 ^ ys.length + F ys <
                              y * 10 ^ ys.length + 10 ^ ys.length :=
                            Nat.add_lt_add_left hi _
                          _ <= 9 * 10 ^ ys.length + 10 ^ ys.length :=
                            Nat.add_le_add_right hmul _
                          _ = 10 ^ ys.length * 10 := by ring
                    have htailDigit : forall z, z ∈ tail -> z < 10 := by
                      intro z hz
                      exact hallDigit z (List.mem_of_mem_drop hz)
                    have htailBound : F tail < 10 ^ tail.length :=
                      foldBound tail htailDigit
                    let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
                      { num_digits := write2, decimal_point := point,
                        truncated := b2, digits := a2 }
                    have htrim :
                        num.imp.dec2flt.decimal_seq.DecimalSeq.trim pre = ok d' := by
                      simpa [pre] using hcall
                    have hpreBound : pre.num_digits.val <= pre.digits.val.length := by
                      simp [pre, a2.property]
                      exact hwriteCap
                    have htrimSem := DecimalSeqTrimSemantics pre d' hpreBound htrim
                    dsimp only at htrimSem
                    rcases htrimSem with
                      ⟨_, hpointTrim, htrunc, _, _, _, htrimValue⟩
                    have hrhoCell :
                        rho < (10 : Real) ^ (d'.decimal_point.val - 768) := by
                      by_cases htailZero : F tail = 0
                      · simp only [rho, htailZero, Nat.cast_zero, zero_mul]
                        exact zpow_pos (by norm_num) _
                      · have htailPos : 0 < F tail := Nat.pos_of_ne_zero htailZero
                        have htailNonempty : 0 < tail.length := by
                          by_contra hlen
                          have : tail = [] := List.eq_nil_of_length_eq_zero (by omega)
                          simp [this, F] at htailPos
                        have hwriteEq : write2.val = 768 := by
                          simp [tail] at htailNonempty
                          rw [Nat.min_def] at hwriteAll
                          split at hwriteAll
                          · omega
                          · omega
                        have hexpCell :
                            point.val - (all.length : Int) +
                                (tail.length : Int) =
                              d'.decimal_point.val - 768 := by
                          rw [show tail.length = all.length - write2.val by
                            simp [tail]]
                          rw [Nat.cast_sub hwriteLen, hwriteEq]
                          rw [hpointTrim]
                          simp only [pre]
                          omega
                        have hcastBound :
                            (F tail : Real) < (10 : Real) ^ tail.length := by
                          exact_mod_cast htailBound
                        have hscalePos : 0 < (10 : Real) ^
                            (point.val - (all.length : Int)) :=
                          zpow_pos (by norm_num) _
                        calc
                          rho = (F tail : Real) * (10 : Real) ^
                              (point.val - (all.length : Int)) := rfl
                          _ < (10 : Real) ^ tail.length * (10 : Real) ^
                              (point.val - (all.length : Int)) :=
                            mul_lt_mul_of_pos_right hcastBound hscalePos
                          _ = (10 : Real) ^
                              (d'.decimal_point.val - 768) := by
                            norm_num only [← zpow_natCast]
                            rw [← zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
                            rw [add_comm, hexpCell]
                    right
                    refine ⟨rho, hrho0, hrhoCell, ?_, ?_⟩
                    · rw [htrimValue]
                      simp only [pre]
                      rw [hpreDigits]
                      have hOrigFold :
                          ((d0.digits.val.take d0.num_digits.val).map
                              (fun z => z.val)).foldl
                                (fun n z => 10 * n + z) 0 = I := by
                        simp [I, F, xs, List.map_take]
                      rw [hOrigFold]
                      change (F kept : Real) *
                          (10 : Real) ^ (point.val - (write2.val : Int)) =
                        (I : Real) * (10 : Real) ^
                            (d0.decimal_point.val -
                              (d0.num_digits.val : Int)) /
                            (2 : Real) ^ shift.val - rho
                      have hBReal : (B : Real) = (2 : Real) ^ shift.val := by
                        simp [B]
                      rw [← hBReal, ← hfullValue]
                      rw [hcapSplit]
                      simp [rho]
                    · rw [htrunc, hsticky2, hrhoSticky]

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalSeqTrimSemantics
import Tablet.DecimalRightShiftPrefixLoopSemantics
import Tablet.DecimalRightShiftMainLoopSemantics
import Tablet.DecimalRightShiftU64DyadicBridge
import Tablet.DecimalRightShiftFinalLoopSemantics
import Tablet.ResultLoopPartial
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqRightShiftRecordSemantics]
theorem DecimalSeqRightShiftRecordSemantics :
    let value := fun d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun z => z.val)
      let integer : Nat := digits.foldl (fun a z => 10 * a + z) 0
      (integer : Real) * (10 : Real) ^
        (d.decimal_point.val - (d.num_digits.val : Int))
    forall (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift : Std.Usize),
      shift.val <= 60 ->
      0 < value d ->
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) ->
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d' ->
      (d'.num_digits.val = 0 /\ d'.decimal_point.val = 0 /\
        d'.truncated = false) \/
      exists rho : Real,
        0 <= rho /\
        rho < (10 : Real) ^ (d'.decimal_point.val - 768) /\
        value d' = value d / (2 : Real) ^ shift.val - rho /\
        (d'.truncated = true <->
          d.truncated = true \/ 0 < rho) := by
-- BODY
  dsimp only
  intro d d' shift hshift hvalue hdigit hcall
  let xs := d.digits.val.map (fun z => z.val)
  let F := fun ys : List Nat => ys.foldl (fun n z => 10 * n + z) 0
  let I := F (xs.take d.num_digits.val)
  let B := 2 ^ shift.val
  have hscale : 0 < (10 : Real) ^
      (d.decimal_point.val - (d.num_digits.val : Int)) :=
    zpow_pos (by norm_num) _
  have hIreal : 0 < (I : Real) := by
    have hv : 0 < (I : Real) * (10 : Real) ^
        (d.decimal_point.val - (d.num_digits.val : Int)) := by
      simpa [I, F, xs, List.map_take] using hvalue
    nlinarith
  have hIpos : 0 < I := by exact_mod_cast hIreal
  unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift at hcall
  cases hloop0 :
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0
        d shift 0#usize 0#u64 false with
  | fail e => simp [hloop0] at hcall
  | div => simp [hloop0] at hcall
  | ok out0 =>
    rcases out0 with ⟨d0, read, acc, allZero⟩
    simp only [hloop0, bind_tc_ok] at hcall
    have hpref := DecimalRightShiftPrefixLoopSemantics d d0 shift read acc
      allZero hshift (by simpa [I, F, xs, List.map_take] using hIpos) hdigit hloop0
    dsimp only at hpref
    rcases hpref with ⟨rfl, rfl, hreadpos, hBacc, hacc10, hphase⟩
    let reset : num.imp.dec2flt.decimal_seq.DecimalSeq :=
      { num_digits := 0#usize,
        decimal_point := 0#i32,
        truncated := false,
        digits := d0.digits }
    change (do
      let i ← lift (UScalar.hcast .I32 read)
      let i1 ← i - 1#i32
      let i2 ← d0.decimal_point - i1
      let i3 ← -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
      if i2 < i3 then
        ok reset
      else
        let i4 ← 1#u64 <<< shift
        let mask ← i4 - 1#u64
        let (ac, wi, n1) ←
          num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
            d0.num_digits d0.digits shift read 0#usize acc mask
        let (bc, ac1, wi1) ←
          num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
            d0.truncated ac shift wi n1 mask
        num.imp.dec2flt.decimal_seq.DecimalSeq.trim
          { num_digits := wi1, decimal_point := i2, truncated := bc,
            digits := ac1 }) = ok d' at hcall
    let readI : Std.I32 := UScalar.hcast .I32 read
    rw [show UScalar.hcast .I32 read = readI by rfl] at hcall
    simp only [lift, bind_tc_ok] at hcall
    have hreadBound : read.val <= d0.num_digits.val + shift.val := by
      rcases hphase with hphase | hphase
      · omega
      · omega
    have hread828 : read.val <= 828 := by
      rcases hphase with hphase | hphase <;> omega
    have hreadIVal : readI.val = (read.val : Int) := by
      have hs := UScalar.hcast_inBounds_spec .I32 read (by
        rw [IScalar.max_I32_eq]
        omega)
      simp only [lift, WP.spec_ok] at hs
      exact hs
    cases hsub : readI - 1#i32 with
    | fail e =>
      rw [hsub] at hcall
      simp at hcall
    | div =>
      rw [hsub] at hcall
      simp at hcall
    | ok readMinus =>
      rw [hsub] at hcall
      simp only [bind_tc_ok] at hcall
      have hsubVal : readMinus.val = readI.val - 1 := by
        have hs := IScalar.sub_equiv readI 1#i32
        rw [hsub] at hs
        exact hs.2.1
      cases hpoint : d0.decimal_point - readMinus with
      | fail e => simp [hpoint] at hcall
      | div => simp [hpoint] at hcall
      | ok point =>
        rw [hpoint] at hcall
        simp only [bind_tc_ok] at hcall
        have hpointVal : point.val = d0.decimal_point.val - (read.val : Int) + 1 := by
          have hs := IScalar.sub_equiv d0.decimal_point readMinus
          rw [hpoint] at hs
          rw [hsubVal, hreadIVal] at hs
          omega
        cases hnegRange : -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE with
        | fail e => simp [hnegRange] at hcall
        | div => simp [hnegRange] at hcall
        | ok negRange =>
          rw [hnegRange] at hcall
          simp only [bind_tc_ok] at hcall
          by_cases hreset : point < negRange
          · simp only [hreset, if_true] at hcall
            have hd' : d' = reset := by simpa using hcall.symm
            subst d'
            exact Or.inl ⟨by simp [reset], by simp [reset], by simp [reset]⟩
          · simp only [hreset, if_false] at hcall
            cases hpow : 1#u64 <<< shift with
            | fail e => simp [hpow] at hcall
            | div => simp [hpow] at hcall
            | ok pow =>
              rw [hpow] at hcall
              simp only [bind_tc_ok] at hcall
              cases hmask : pow - 1#u64 with
              | fail e => simp [hmask] at hcall
              | div => simp [hmask] at hcall
              | ok mask =>
                rw [hmask] at hcall
                simp only [bind_tc_ok] at hcall
                cases hloop1 :
                    num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
                      d0.num_digits d0.digits shift read 0#usize acc mask with
                | fail e => simp [hloop1] at hcall
                | div => simp [hloop1] at hcall
                | ok out1 =>
                  rcases out1 with ⟨a1, write1, acc1⟩
                  rw [hloop1] at hcall
                  simp only [bind_tc_ok] at hcall
                  have hmain : d0.num_digits.val <= 768 /\
                      exists low : List Nat,
                        low.length = d0.num_digits.val - read.val /\
                        (forall z, z ∈ low -> z < 10) /\
                        write1.val = d0.num_digits.val - read.val /\
                        acc1.val < 10 * B /\
                        acc.val * 10 ^ (d0.num_digits.val - read.val) +
                            F ((xs.take d0.num_digits.val).drop read.val) =
                          B * (10 * F low) + acc1.val /\
                        (forall j, j < low.length ->
                          (a1.val.map (fun z : Std.U8 => z.val))[j]? = low[j]?) /\
                        (forall j, j < 768 -> write1.val <= j ->
                          a1.val[j]? = d0.digits.val[j]?) /\
                        (forall j, j < 768 -> d0.num_digits.val <= j ->
                          a1.val[j]? = d0.digits.val[j]?) := by
                    by_cases hrn : read.val < d0.num_digits.val
                    · have hm := DecimalRightShiftMainLoopSemantics
                        d0.digits a1 shift read d0.num_digits write1 pow mask acc acc1
                        hshift hreadpos (by omega) (by omega) hacc10 hdigit hpow hmask
                        hloop1
                      simpa [xs, F, B] using hm
                    · have hreadGe : d0.num_digits.val <= read.val := by omega
                      have hreadWord : ¬ read < d0.num_digits := by
                        simpa [UScalar.lt_equiv] using hrn
                      have hloop1Direct :
                          num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
                            d0.num_digits d0.digits shift read 0#usize acc mask =
                            ok (d0.digits, 0#usize, acc) := by
                        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
                        rw [Aeneas.Std.loop.eq_1]
                        simp [num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1.body,
                          hreadWord]
                      rw [hloop1Direct] at hloop1
                      cases hloop1
                      have hnBound : d0.num_digits.val <= 768 := by
                        rcases hphase with hp | hp <;> omega
                      refine ⟨hnBound, [], by simp [Nat.sub_eq_zero_of_le hreadGe],
                        by simp, by simp [Nat.sub_eq_zero_of_le hreadGe], hacc10,
                        ?_, ?_, ?_, ?_⟩
                      · have hdrop : (xs.take d0.num_digits.val).drop read.val = [] :=
                          List.drop_eq_nil_of_le (by simp; omega)
                        have hsubzero : d0.num_digits.val - read.val = 0 :=
                          Nat.sub_eq_zero_of_le hreadGe
                        simp [F, hdrop, hsubzero]
                      · intro j hj
                        simp at hj
                      · intro j hj _
                        rfl
                      · intro j hj _
                        rfl
                  rcases hmain with ⟨_, low, hlowLen, hlowDigit, hwrite1,
                    hacc1, hmainNum, hlowArray, houtside1, hhigh1⟩
                  cases hloop2 :
                      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
                        d0.truncated a1 shift write1 acc1 mask with
                  | fail e => simp [hloop2] at hcall
                  | div => simp [hloop2] at hcall
                  | ok out2 =>
                    rcases out2 with ⟨b2, a2, write2⟩
                    change (do
                      let (bc, ac, wi) ←
                        num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
                          d0.truncated a1 shift write1 acc1 mask
                      num.imp.dec2flt.decimal_seq.DecimalSeq.trim
                        { num_digits := wi, decimal_point := point,
                          truncated := bc, digits := ac }) = ok d' at hcall
                    rw [hloop2] at hcall
                    simp only [bind_tc_ok] at hcall
                    have hfinal := DecimalRightShiftFinalLoopSemantics
                      a1 a2 d0.truncated b2 shift write1 write2 pow mask acc1
                      hshift (by omega) hacc1 hpow hmask hloop2
                    dsimp only at hfinal
                    rcases hfinal with ⟨high, hhighDigit, hfinalNum, hwrite2,
                      hhighArray, hbelow2, houtside2, hsticky2⟩
                    let all := low ++ high
                    have hallDigit : forall z, z ∈ all -> z < 10 := by
                      intro z hz
                      simp only [all, List.mem_append] at hz
                      rcases hz with hz | hz
                      · exact hlowDigit z hz
                      · exact hhighDigit z hz
                    have hallLen : all.length = low.length + high.length := by simp [all]
                    have hallFold : F all = F low * 10 ^ high.length + F high := by
                      simp only [all, F, List.foldl_append]
                      have foldFrom (ys : List Nat) (n : Nat) :
                          ys.foldl (fun x z => 10 * x + z) n =
                            n * 10 ^ ys.length + F ys := by
                        induction ys generalizing n with
                        | nil => simp [F]
                        | cons y ys ih =>
                          simp only [List.foldl_cons, List.length_cons]
                          rw [ih, pow_succ]
                          change (10 * n + y) * 10 ^ ys.length + F ys =
                            n * (10 ^ ys.length * 10) +
                              (y :: ys).foldl (fun x z => 10 * x + z) 0
                          simp only [List.foldl_cons]
                          rw [ih]
                          ring
                      simpa using foldFrom high (F low)
                    have hcombined :
                        acc.val * 10 ^ (d0.num_digits.val - read.val + high.length) +
                            F ((xs.take d0.num_digits.val).drop read.val) *
                              10 ^ high.length =
                          B * (10 * F all) := by
                      rw [hallFold]
                      calc
                        acc.val * 10 ^ (d0.num_digits.val - read.val + high.length) +
                            F ((xs.take d0.num_digits.val).drop read.val) *
                              10 ^ high.length =
                            (acc.val * 10 ^ (d0.num_digits.val - read.val) +
                              F ((xs.take d0.num_digits.val).drop read.val)) *
                                10 ^ high.length := by rw [pow_add]; ring
                        _ = (B * (10 * F low) + acc1.val) *
                              10 ^ high.length := by rw [hmainNum]
                        _ = B * (10 * F low) * 10 ^ high.length +
                              acc1.val * 10 ^ high.length := by ring
                        _ = B * (10 * (F low * 10 ^ high.length + F high)) := by
                          rw [hfinalNum]
                          ring
                    let extra := read.val - d0.num_digits.val + high.length
                    have foldFrom2 (ys : List Nat) (n : Nat) :
                        ys.foldl (fun x z => 10 * x + z) n =
                          n * 10 ^ ys.length + F ys := by
                      induction ys generalizing n with
                      | nil => simp [F]
                      | cons y ys ih =>
                        simp only [List.foldl_cons, List.length_cons]
                        rw [ih, pow_succ]
                        change (10 * n + y) * 10 ^ ys.length + F ys =
                          n * (10 ^ ys.length * 10) +
                            (y :: ys).foldl (fun x z => 10 * x + z) 0
                        simp only [List.foldl_cons]
                        rw [ih]
                        ring
                    have hterminal : I * 10 ^ extra = B * (10 * F all) := by
                      rcases hphase with hp | hp
                      · rcases hp with ⟨hrn, _, haccPrefix⟩
                        have htake : (xs.take d0.num_digits.val).take read.val =
                            xs.take read.val := by
                          rw [List.take_take]
                          simp [Nat.min_eq_left hrn]
                        have hsplit : I =
                            acc.val * 10 ^ (d0.num_digits.val - read.val) +
                              F ((xs.take d0.num_digits.val).drop read.val) := by
                          have hdropLen :
                              ((xs.take d0.num_digits.val).drop read.val).length =
                                d0.num_digits.val - read.val := by
                            simp [xs, d0.digits.property]
                            omega
                          change F (xs.take d0.num_digits.val) = _
                          conv_lhs =>
                            rw [← List.take_append_drop read.val
                              (xs.take d0.num_digits.val)]
                          simp only [F, List.foldl_append]
                          rw [foldFrom2, htake, hdropLen]
                          rw [← haccPrefix]
                        have hrsub : read.val - d0.num_digits.val = 0 :=
                          Nat.sub_eq_zero_of_le hrn
                        rw [show extra = high.length by simp [extra, hrsub]]
                        calc
                          I * 10 ^ high.length =
                              (acc.val * 10 ^ (d0.num_digits.val - read.val) +
                                F ((xs.take d0.num_digits.val).drop read.val)) *
                                  10 ^ high.length := by rw [hsplit]
                          _ = acc.val * 10 ^
                                  (d0.num_digits.val - read.val + high.length) +
                                F ((xs.take d0.num_digits.val).drop read.val) *
                                  10 ^ high.length := by rw [pow_add]; ring
                          _ = B * (10 * F all) := hcombined
                      · rcases hp with ⟨hnr, _, _, haccExtended⟩
                        have hnsub : d0.num_digits.val - read.val = 0 :=
                          Nat.sub_eq_zero_of_le (by omega)
                        have hdrop :
                            (xs.take d0.num_digits.val).drop read.val = [] :=
                          List.drop_eq_nil_of_le (by simp; omega)
                        calc
                          I * 10 ^ extra =
                              (I * 10 ^ (read.val - d0.num_digits.val)) *
                                10 ^ high.length := by
                                  rw [show extra =
                                    (read.val - d0.num_digits.val) + high.length by rfl,
                                    pow_add]
                                  ring
                          _ = acc.val * 10 ^ high.length := by rw [haccExtended]
                          _ = acc.val * 10 ^
                                  (d0.num_digits.val - read.val + high.length) +
                                F ((xs.take d0.num_digits.val).drop read.val) *
                                  10 ^ high.length := by simp [hnsub, hdrop, F]
                          _ = B * (10 * F all) := hcombined
                    have hlowWrite : write1.val = low.length := by omega
                    have hlowCap : low.length <= 768 := by omega
                    have hwriteAll : write2.val = min all.length 768 := by
                      rw [hwrite2, hwrite1, hallLen, hlowLen]
                    have hwriteCap : write2.val <= 768 := by
                      rw [hwriteAll]
                      exact Nat.min_le_right _ _
                    have hwriteLen : write2.val <= all.length := by
                      rw [hwriteAll]
                      exact Nat.min_le_left _ _
                    let kept := all.take write2.val
                    let tail := all.drop write2.val
                    have hpreDigits :
                        (a2.val.take write2.val).map (fun z => z.val) = kept := by
                      simp only [kept, List.map_take]
                      apply List.ext_getElem
                      · simp [hwriteCap, hwriteLen, a2.property]
                      · intro j hj1 hj2
                        have hjm : j < write2.val := by
                          simpa [List.length_take, hwriteCap, a2.property] using hj1
                        have hj768 : j < 768 := by omega
                        by_cases hjlow : j < low.length
                        · have ha1 := hlowArray j hjlow
                          have ha2raw := hbelow2 j hj768 (by omega)
                          have ha2 :
                              (a2.val.map (fun z : Std.U8 => z.val))[j]? = low[j]? := by
                            rw [List.getElem?_map, ha2raw]
                            simpa [List.getElem?_map] using ha1
                          have hall : all[j]? = low[j]? := by
                            rw [List.getElem?_append_left (by simpa using hjlow)]
                          have hmapBound :
                              j < (a2.val.map (fun z : Std.U8 => z.val)).length := by
                            simp [a2.property]
                            exact hj768
                          have hallBound : j < all.length := by omega
                          rw [show ((a2.val.map (fun z : Std.U8 => z.val)).take
                              write2.val)[j] =
                              (a2.val.map (fun z : Std.U8 => z.val))[j] by simp]
                          rw [show (all.take write2.val)[j] = all[j] by simp]
                          apply Option.some.inj
                          calc
                            some (a2.val.map (fun z : Std.U8 => z.val))[j] =
                                (a2.val.map (fun z : Std.U8 => z.val))[j]? :=
                              (List.getElem?_eq_getElem hmapBound).symm
                            _ = low[j]? := ha2
                            _ = all[j]? := hall.symm
                            _ = some all[j] := List.getElem?_eq_getElem hallBound
                        · let t := j - low.length
                          have ht : t < high.length := by
                            simp only [t]
                            simp [all] at hj2
                            omega
                          have hjEq : write1.val + t = j := by
                            simp only [t]
                            omega
                          have ha2 := hhighArray t ht (by omega)
                          have hall : all[j]? = high[t]? := by
                            rw [List.getElem?_append_right (by simpa using hjlow)]
                          have hmapBound :
                              j < (a2.val.map (fun z : Std.U8 => z.val)).length := by
                            simp [a2.property]
                            exact hj768
                          have hallBound : j < all.length := by omega
                          rw [show ((a2.val.map (fun z : Std.U8 => z.val)).take
                              write2.val)[j] =
                              (a2.val.map (fun z : Std.U8 => z.val))[j] by simp]
                          rw [show (all.take write2.val)[j] = all[j] by simp]
                          rw [hjEq] at ha2
                          apply Option.some.inj
                          calc
                            some (a2.val.map (fun z : Std.U8 => z.val))[j] =
                                (a2.val.map (fun z : Std.U8 => z.val))[j]? :=
                              (List.getElem?_eq_getElem hmapBound).symm
                            _ = high[t]? := ha2
                            _ = all[j]? := hall.symm
                            _ = some all[j] := List.getElem?_eq_getElem hallBound
                    have foldPos (ys : List Nat) :
                        0 < F ys <->
                          exists j, j < ys.length /\ ys[j]? != some 0 := by
                      induction ys with
                      | nil => simp [F]
                      | cons y ys ih =>
                        have hform : F (y :: ys) =
                            y * 10 ^ ys.length + F ys := by
                          simpa [F] using foldFrom2 ys y
                        rw [hform]
                        constructor
                        · intro hp
                          by_cases hy : y = 0
                          · have ht : 0 < F ys := by simpa [hy] using hp
                            rcases ih.mp ht with ⟨j, hj, hjnz⟩
                            exact ⟨j + 1, by simp; omega, by simpa using hjnz⟩
                          · exact ⟨0, by simp, by simp [hy]⟩
                        · rintro ⟨j, hj, hjnz⟩
                          cases j with
                          | zero =>
                            have hyne : y ≠ 0 := by
                              intro hy
                              subst y
                              simp at hjnz
                            have hy : 0 < y := Nat.pos_of_ne_zero hyne
                            have hpow : 0 < 10 ^ ys.length := by positivity
                            positivity
                          | succ j =>
                            have hjlt : j < ys.length := by simp at hj; omega
                            have ht : 0 < F ys :=
                              ih.mpr ⟨j, hjlt, by simpa using hjnz⟩
                            omega
                    have htailSticky :
                        (exists t, t < tail.length /\ tail[t]? != some 0) <->
                        (exists j, j < high.length /\
                          768 <= write1.val + j /\ high[j]? != some 0) := by
                      constructor
                      · rintro ⟨t, ht, htnz⟩
                        have ht' : t < all.length - write2.val := by
                          simpa [tail] using ht
                        have hwriteEq : write2.val = 768 := by
                          rw [hwriteAll]
                          omega
                        let g := write2.val + t
                        have hgall : g < all.length := by simp [g]; omega
                        have hglow : low.length <= g := by
                          simp [g, hwriteEq]
                          omega
                        let j := g - low.length
                        have hj : j < high.length := by
                          simp [all] at hgall
                          simp [j]
                          omega
                        have hdest : write1.val + j = g := by
                          rw [hlowWrite]
                          simp [j]
                          omega
                        have htailGet : tail[t]? = all[g]? := by
                          simp [tail, g, List.getElem?_drop]
                        have hallHigh : all[g]? = high[j]? := by
                          simp only [all, List.getElem?_append_right (by omega)]
                          simp [j]
                        refine ⟨j, hj, ?_, ?_⟩
                        · rw [hdest]
                          simp [g, hwriteEq]
                        · rw [htailGet, hallHigh] at htnz
                          exact htnz
                      · rintro ⟨j, hj, hjcap, hjnz⟩
                        let g := low.length + j
                        have hgall : g < all.length := by simp [g, all, hj]
                        have hdest : write1.val + j = g := by simp [g, hlowWrite]
                        have hgcap : 768 <= g := by simpa [hdest] using hjcap
                        have hwriteEq : write2.val = 768 := by
                          rw [hwriteAll]
                          omega
                        let t := g - write2.val
                        have ht : t < tail.length := by
                          simp [tail, t]
                          omega
                        have htailGet : tail[t]? = all[g]? := by
                          rw [List.getElem?_drop]
                          simp [t]
                          congr 1
                          omega
                        have hallHigh : all[g]? = high[j]? := by
                          simp only [all]
                          rw [List.getElem?_append_right (by simp [g])]
                          simp [g]
                        refine ⟨t, ht, ?_⟩
                        rw [htailGet, hallHigh]
                        exact hjnz
                    have decimalScale (I0 Q0 B0 extra0 L0 n0 : Nat)
                        (p0 point0 : Int) (hB0 : 0 < B0)
                        (hnum0 : I0 * 10 ^ extra0 = B0 * (10 * Q0))
                        (hexp0 : point0 - (L0 : Int) + (extra0 : Int) =
                          p0 - (n0 : Int) + 1) :
                        (Q0 : Real) * (10 : Real) ^ (point0 - (L0 : Int)) =
                          (I0 : Real) * (10 : Real) ^ (p0 - (n0 : Int)) /
                            (B0 : Real) := by
                      have hnumR : (I0 : Real) * (10 : Real) ^ extra0 =
                          (B0 : Real) * (10 * (Q0 : Real)) := by
                        exact_mod_cast hnum0
                      have h10 : (10 : Real) ≠ 0 := by norm_num
                      have hBR : (B0 : Real) ≠ 0 := by positivity
                      apply (eq_div_iff hBR).2
                      have hpows :
                          (10 : Real) ^ (point0 - (L0 : Int)) *
                              (10 : Real) ^ (extra0 : Int) =
                            (10 : Real) ^ (p0 - (n0 : Int) + 1) := by
                        rw [← zpow_add₀ h10, hexp0]
                      have hpowExtra : (10 : Real) ^ (extra0 : Int) =
                          (10 : Real) ^ extra0 := by
                        norm_num only [zpow_natCast]
                      have hp : (10 : Real) ^ (p0 - (n0 : Int) + 1) =
                          (10 : Real) ^ (p0 - (n0 : Int)) * 10 := by
                        rw [zpow_add₀ h10]
                        norm_num
                      calc
                        (Q0 : Real) * (10 : Real) ^ (point0 - (L0 : Int)) *
                            (B0 : Real) =
                            ((B0 : Real) * (10 * (Q0 : Real))) / 10 *
                              (10 : Real) ^ (point0 - (L0 : Int)) := by ring
                        _ = ((I0 : Real) * (10 : Real) ^ extra0) / 10 *
                              (10 : Real) ^ (point0 - (L0 : Int)) := by
                          rw [hnumR]
                        _ = (I0 : Real) *
                              ((10 : Real) ^ (point0 - (L0 : Int)) *
                                (10 : Real) ^ (extra0 : Int)) / 10 := by
                          rw [hpowExtra]
                          ring
                        _ = (I0 : Real) *
                              (10 : Real) ^ (p0 - (n0 : Int)) := by
                          rw [hpows, hp]
                          ring
                    have hexp : point.val - (all.length : Int) + (extra : Int) =
                        d0.decimal_point.val - (d0.num_digits.val : Int) + 1 := by
                      rw [hpointVal, hallLen, hlowLen]
                      simp only [extra, Nat.cast_add]
                      rcases hphase with hp | hp
                      · have hrsub : read.val - d0.num_digits.val = 0 :=
                          Nat.sub_eq_zero_of_le hp.1
                        rw [hrsub]
                        simp
                        rw [Nat.cast_sub hp.1]
                        omega
                      · have hnle : d0.num_digits.val <= read.val := by omega
                        have hnsub : d0.num_digits.val - read.val = 0 :=
                          Nat.sub_eq_zero_of_le hnle
                        rw [hnsub]
                        simp
                        rw [Nat.cast_sub hnle]
                        omega
                    have hBpos : 0 < B := by simp [B]
                    have hfullValue :
                        (F all : Real) *
                            (10 : Real) ^ (point.val - (all.length : Int)) =
                          (I : Real) * (10 : Real) ^
                              (d0.decimal_point.val -
                                (d0.num_digits.val : Int)) / (B : Real) :=
                      decimalScale I (F all) B extra all.length
                        d0.num_digits.val d0.decimal_point.val point.val hBpos
                        hterminal hexp
                    have capSplit (ys : List Nat) (m : Nat) (p0 : Int)
                        (hm : m <= ys.length) :
                        (F ys : Real) *
                            (10 : Real) ^ (p0 - (ys.length : Int)) =
                          (F (ys.take m) : Real) *
                              (10 : Real) ^ (p0 - (m : Int)) +
                            (F (ys.drop m) : Real) *
                              (10 : Real) ^ (p0 - (ys.length : Int)) := by
                      have hfold : F ys = F (ys.take m) * 10 ^ (ys.length - m) +
                          F (ys.drop m) := by
                        conv_lhs => rw [← List.take_append_drop m ys]
                        simp only [F, List.foldl_append]
                        rw [foldFrom2]
                        congr 2
                        simp
                      have hexp' : p0 - (m : Int) =
                          (p0 - (ys.length : Int)) +
                            ((ys.length - m : Nat) : Int) := by
                        rw [Nat.cast_sub hm]
                        omega
                      have hpow : (10 : Real) ^ (p0 - (m : Int)) =
                          (10 : Real) ^ (p0 - (ys.length : Int)) *
                            (10 : Real) ^ (ys.length - m) := by
                        rw [hexp', zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
                        norm_num only [zpow_natCast]
                      rw [show (F ys : Real) =
                          (F (ys.take m) : Real) *
                              (10 : Real) ^ (ys.length - m) +
                            (F (ys.drop m) : Real) by exact_mod_cast hfold]
                      rw [hpow]
                      ring
                    have hcapSplit :
                        (F all : Real) *
                            (10 : Real) ^ (point.val - (all.length : Int)) =
                          (F kept : Real) *
                              (10 : Real) ^ (point.val - (write2.val : Int)) +
                            (F tail : Real) *
                              (10 : Real) ^ (point.val - (all.length : Int)) := by
                      simpa [kept, tail] using
                        capSplit all write2.val point.val hwriteLen
                    let rho : Real := (F tail : Real) *
                      (10 : Real) ^ (point.val - (all.length : Int))
                    have hrho0 : 0 <= rho := by
                      dsimp only [rho]
                      exact mul_nonneg (Nat.cast_nonneg _) (le_of_lt
                        (zpow_pos (by norm_num : (0 : Real) < 10) _))
                    have hrhoPos : 0 < rho <-> 0 < F tail := by
                      dsimp only [rho]
                      have hp : 0 < (10 : Real) ^
                          (point.val - (all.length : Int)) :=
                        zpow_pos (by norm_num) _
                      constructor
                      · intro h
                        have : 0 < (F tail : Real) := by nlinarith
                        exact_mod_cast this
                      · intro h
                        have hc : 0 < (F tail : Real) := by exact_mod_cast h
                        positivity
                    have hrhoSticky :
                        0 < rho <->
                          exists j, j < high.length /\
                            768 <= write1.val + j /\ high[j]? != some 0 := by
                      rw [hrhoPos, foldPos, htailSticky]
                    have foldBound (ys : List Nat)
                        (hd : forall z, z ∈ ys -> z < 10) :
                        F ys < 10 ^ ys.length := by
                      induction ys with
                      | nil => simp [F]
                      | cons y ys ih =>
                        have hy : y < 10 := hd y (by simp)
                        have hys : forall z, z ∈ ys -> z < 10 := by
                          intro z hz
                          exact hd z (by simp [hz])
                        have hi := ih hys
                        rw [show F (y :: ys) =
                            y * 10 ^ ys.length + F ys by
                          simpa [F] using foldFrom2 ys y,
                          List.length_cons, pow_succ]
                        have hmul : y * 10 ^ ys.length <=
                            9 * 10 ^ ys.length :=
                          Nat.mul_le_mul_right _ (by omega)
                        calc
                          y * 10 ^ ys.length + F ys <
                              y * 10 ^ ys.length + 10 ^ ys.length :=
                            Nat.add_lt_add_left hi _
                          _ <= 9 * 10 ^ ys.length + 10 ^ ys.length :=
                            Nat.add_le_add_right hmul _
                          _ = 10 ^ ys.length * 10 := by ring
                    have htailDigit : forall z, z ∈ tail -> z < 10 := by
                      intro z hz
                      exact hallDigit z (List.mem_of_mem_drop hz)
                    have htailBound : F tail < 10 ^ tail.length :=
                      foldBound tail htailDigit
                    let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
                      { num_digits := write2, decimal_point := point,
                        truncated := b2, digits := a2 }
                    have htrim :
                        num.imp.dec2flt.decimal_seq.DecimalSeq.trim pre = ok d' := by
                      simpa [pre] using hcall
                    have hpreBound : pre.num_digits.val <= pre.digits.val.length := by
                      simp [pre, a2.property]
                      exact hwriteCap
                    have htrimSem := DecimalSeqTrimSemantics pre d' hpreBound htrim
                    dsimp only at htrimSem
                    rcases htrimSem with
                      ⟨_, hpointTrim, htrunc, _, _, _, htrimValue⟩
                    have hrhoCell :
                        rho < (10 : Real) ^ (d'.decimal_point.val - 768) := by
                      by_cases htailZero : F tail = 0
                      · simp only [rho, htailZero, Nat.cast_zero, zero_mul]
                        exact zpow_pos (by norm_num) _
                      · have htailPos : 0 < F tail := Nat.pos_of_ne_zero htailZero
                        have htailNonempty : 0 < tail.length := by
                          by_contra hlen
                          have : tail = [] := List.eq_nil_of_length_eq_zero (by omega)
                          simp [this, F] at htailPos
                        have hwriteEq : write2.val = 768 := by
                          simp [tail] at htailNonempty
                          rw [Nat.min_def] at hwriteAll
                          split at hwriteAll
                          · omega
                          · omega
                        have hexpCell :
                            point.val - (all.length : Int) +
                                (tail.length : Int) =
                              d'.decimal_point.val - 768 := by
                          rw [show tail.length = all.length - write2.val by
                            simp [tail]]
                          rw [Nat.cast_sub hwriteLen, hwriteEq]
                          rw [hpointTrim]
                          simp only [pre]
                          omega
                        have hcastBound :
                            (F tail : Real) < (10 : Real) ^ tail.length := by
                          exact_mod_cast htailBound
                        have hscalePos : 0 < (10 : Real) ^
                            (point.val - (all.length : Int)) :=
                          zpow_pos (by norm_num) _
                        calc
                          rho = (F tail : Real) * (10 : Real) ^
                              (point.val - (all.length : Int)) := rfl
                          _ < (10 : Real) ^ tail.length * (10 : Real) ^
                              (point.val - (all.length : Int)) :=
                            mul_lt_mul_of_pos_right hcastBound hscalePos
                          _ = (10 : Real) ^
                              (d'.decimal_point.val - 768) := by
                            norm_num only [← zpow_natCast]
                            rw [← zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
                            rw [add_comm, hexpCell]
                    right
                    refine ⟨rho, hrho0, hrhoCell, ?_, ?_⟩
                    · rw [htrimValue]
                      simp only [pre]
                      rw [hpreDigits]
                      have hOrigFold :
                          ((d0.digits.val.take d0.num_digits.val).map
                              (fun z => z.val)).foldl
                                (fun n z => 10 * n + z) 0 = I := by
                        simp [I, F, xs, List.map_take]
                      rw [hOrigFold]
                      change (F kept : Real) *
                          (10 : Real) ^ (point.val - (write2.val : Int)) =
                        (I : Real) * (10 : Real) ^
                            (d0.decimal_point.val -
                              (d0.num_digits.val : Int)) /
                            (2 : Real) ^ shift.val - rho
                      have hBReal : (B : Real) = (2 : Real) ^ shift.val := by
                        simp [B]
                      rw [← hBReal, ← hfullValue]
                      rw [hcapSplit]
                      simp [rho]
                    · rw [htrunc, hsticky2, hrhoSticky]

end dec2flt_model

Proof structure

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

Explore this dependency tree

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

Related source and models come from the extraction records and declared dependencies.

Proof assumptions and review

Lean checks the formal proof. Trellis also reviews how the statement corresponds to the intended requirement.

Transitive assumptions

This list combines the assumptions recorded for this declaration and its dependencies. It may include assumptions used by auxiliary declarations in those modules.

Helper theorems included
All 12 entries
Technical checking records
Local closure summary
{
  "node": "DecimalSeqRightShiftRecordSemantics",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "b4817e2753d0bdeb8e231eef8e5ad2a8f217c46ee88fba87ed8d76ab986950e8",
  "active_statement_hash": "142a2960d3006c1adca23ea0e34c1c25b546c5154a9c2ecfc299b21b15c7218f",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "ab64d13f256386ee0e494a5abcc6a5db49512cf3d90bbdfcabd6327793a31f95",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "revalidate-cycle-188",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalRightShiftFinalLoopSemantics": "79a214a5ee500eac",
    "DecimalRightShiftMainLoopSemantics": "647cdcc98ff47e21",
    "DecimalRightShiftPrefixLoopSemantics": "0d5cb25f18eedbfb",
    "DecimalSeqTrimSemantics": "e4b2cbb62f7171af",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "0eb6fb2627d0ff5c"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "6799fe6525ddb761",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE": "0cc5155758bc976a",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "c3a2fd15a88a3d38",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_trim": "faa9fd92b8c29c14"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "differs from current fingerprint",
  "assessment_fingerprint": "sound-v2:489005e88e2eb403dffac908f15b1a78af8ed7c00754c1e30055e4d8da93e10c",
  "current_fingerprint": "sound-v2:d717858529d457e10eba5e1e5611b591e34d460ba72e88023479af7f54b122b9",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}