DecimalSeqRightShiftCanonical

Lean checked

Statement

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

Lemma · Fixed endpoints under a nonreset decimal right shift

Let a nonzero decimal record d have at most 768 decimal cells and positive leading retained digit. Put V=V(d) and suppose
X=V+\eta,\qquad B=V+u,\qquad0\leq\eta<u, \qquad(d.\mathit{truncated}\iff0<\eta).
If k\leq60, the extracted right shift returns d', and d' has nonzero count, then the reset branch was not taken, p'\geq-2047, and d' has at most 768 decimal cells, positive leading digit, and terminally trimmed word. There are \eta',u' with
X/2^k=V(d')+\eta',\qquad B/2^k=V(d')+u',\qquad 0\leq\eta'<u',\qquad(d'.\mathit{truncated}\iff0<\eta').
This is the let-bound conjunction stated in Lean.

Lean statement

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

import Tablet.DecimalSeqDyadicShiftSemantics
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE
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: DecimalSeqRightShiftCanonical]
theorem DecimalSeqRightShiftCanonical :
    forall (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift : Std.Usize) (X B : Real),
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun z => z.val)
      let integer : Nat := digits.foldl (fun n digit => 10 * n + digit) 0
      let value : Real := integer * (10 : Real) ^
        (d.decimal_point.val - (d.num_digits.val : Int))
      d.num_digits.val <= 768 /\
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
      (exists z zs, digits = z :: zs /\ 1 <= z /\ z < 10 /\
        digits.length = d.num_digits.val) ->
      (exists eta u : Real,
        X = value + eta /\ B = value + u /\
        0 <= eta /\ eta < u /\
        (d.truncated = true <-> 0 < eta)) ->
      shift.val <= 60 ->
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d' ->
      d'.num_digits.val != 0 ->
      let digits' :=
        (d'.digits.val.take d'.num_digits.val).map (fun z => z.val)
      let integer' : Nat := digits'.foldl (fun n digit => 10 * n + digit) 0
      let value' : Real := integer' * (10 : Real) ^
        (d'.decimal_point.val - (d'.num_digits.val : Int))
      d'.num_digits.val <= 768 /\
      (forall z, z ∈ d'.digits.val.take d'.num_digits.val -> z.val < 10) /\
      (exists z zs, digits' = z :: zs /\ 1 <= z /\ z < 10 /\
        digits'.length = d'.num_digits.val /\
        digits'.getLast? != some 0) /\
      (exists eta' u' : Real,
        X / (2 : Real) ^ shift.val = value' + eta' /\
        B / (2 : Real) ^ shift.val = value' + u' /\
        0 <= eta' /\ eta' < u' /\
        (d'.truncated = true <-> 0 < eta')) /\
      -2047 <= d'.decimal_point.val := by
Proof

Natural-language proof

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

Explanatory prose status

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

Proof

The positive leading digit gives V(d)>0. Apply the right-shift half of DecimalSeqDyadicShiftSemantics to X,\eta,u. Its reset alternative has zero digit count, so the explicit nonzero-count hypothesis excludes it. The nonreset alternative supplies \eta',u' with
X/2^k=V(d')+\eta',\quad0\leq\eta'<u',\quad V(d')+u'=(V(d)+u)/2^k
and exact sticky equivalence. Substitution of B=V(d)+u gives the second endpoint equation. Again the conclusion transports B itself and does not replace it by a successor power-of-ten endpoint.
For the record facts, unfold num imp dec2flt decimal seq DecimalSeq right shift. Its prefix loop reads cells until its accumulator c reaches 2^k. Thereafter long division writes \lfloor c/2^k\rfloor<10, keeps remainder r<2^k, and forms the next accumulator 10r+a with a<10. This is below 10\,2^k\leq10\,2^{60}<2^{64}, so every multiplication, addition, quotient, cast, and write on the successful execution has its mathematical value. The positive leading input rules out the unchanged all-zero path. Thus every written cell lies in [0,9], and a nonempty quotient begins in [1,9]. The 768-cell cap gives the count and exact-list-length clauses, and the final trim makes the last retained digit nonzero.
If r_0 positions precede the first quotient digit, the checked point update is p'=p-(r_0-1). The comparison constant is 2047 by num imp dec2flt decimal seq DecimalSeq DECIMAL POINT RANGE; the extracted code resets exactly when this prospective point is below -2047. Since a reset result has zero count, the hypothesis on d' forces the other branch and hence p'\geq-2047. Combining these record facts with the two transported endpoint equations proves the conclusion.

Lean proof


  dsimp only
  intro d d' shift X B hinput hend hshift hcall hnonzero
  rcases hinput with ⟨hcap, hdigit, hshape⟩
  rcases hend with ⟨eta, u, hX, hB, heta0, hetau, hsticky⟩
  rcases hshape with ⟨z, zs, hdigits, hzlo, hzhi, hlen⟩
  let integer : Nat :=
    ((d.digits.val.take d.num_digits.val).map (fun z => z.val)).foldl
      (fun n digit => 10 * n + digit) 0
  have hvalue : 0 < (integer : Real) *
        (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
    have hfold : 0 <
        ((d.digits.val.take d.num_digits.val).map (fun z => z.val)).foldl
          (fun n digit => 10 * n + digit) 0 := by
      rw [hdigits]
      simp only [List.foldl_cons]
      have hrest (ys : List Nat) (a : Nat) :
          a ≤ ys.foldl (fun n digit => 10 * n + digit) a := by
        induction ys generalizing a with
        | nil => simp
        | cons b bs ih =>
            simp only [List.foldl_cons]
            have hi := ih (10 * a + b)
            omega
      have hmono := hrest zs (10 * 0 + z)
      omega
    have hfoldR : 0 < (integer : Real) := by
      exact_mod_cast hfold
    have hpow : 0 < (10 : Real) ^
        (d.decimal_point.val - (d.num_digits.val : Int)) :=
      zpow_pos (by norm_num) _
    exact mul_pos (by simpa [integer] using hfoldR) hpow
  have hdyadic := DecimalSeqDyadicShiftSemantics
  dsimp only at hdyadic
  have htransport := hdyadic.2 d d' shift X eta u hshift hvalue hdigit hcall
      (by simpa using hX) heta0 hetau hsticky
  rcases htransport with hreset | ⟨eta', u', hscaled, heta'0, heta'u', htrunc,
    hendpoint, hcomparison⟩
  · have hne : d'.num_digits.val ≠ 0 := by simpa using hnonzero
    omega
  clear hdyadic
  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 base : Nat := 2 ^ shift.val
  have hpowpos : 0 < (10 : Real) ^
      (d.decimal_point.val - (d.num_digits.val : Int)) :=
    zpow_pos (by norm_num) _
  have hIreal : 0 < (integer : Real) := by
    nlinarith [hvalue, hpowpos]
  have hIpos : 0 < integer := 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 [integer, 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 hp | hp <;> 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
            have hne : reset.num_digits.val ≠ 0 := by simpa [hd'] using hnonzero
            subst d'
            simp [reset] at hne
          · 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 * base /\
                        acc.val * 10 ^ (d0.num_digits.val - read.val) +
                            F ((xs.take d0.num_digits.val).drop read.val) =
                          base * (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, base] 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 hlowWrite : write1.val = low.length := by omega
                    have hwriteAll : write2.val = min all.length 768 := by
                      rw [hwrite2, hlowWrite]
                      simp [all]
                    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
                    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 [all] at hj2
                            omega
                          have hjEq : write1.val + t = j := by
                            simp [t, hlowWrite]
                            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
                    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
                      ⟨hdigEq, hpointEq, htruncEq, hnumLe, hzeros, hlast, hvalueEq⟩
                    have hdigEq' : d'.digits = a2 := by simpa [pre] using hdigEq
                    have hnumLe' : d'.num_digits.val <= write2.val := by
                      simpa [pre] using hnumLe
                    have hnumCap : d'.num_digits.val <= 768 := by omega
                    have hdigitsOut :
                        ((d'.digits.val.take d'.num_digits.val).map
                            (fun z => z.val)) = all.take d'.num_digits.val := by
                      rw [hdigEq']
                      have ht := congrArg (List.take d'.num_digits.val) hpreDigits
                      simpa [kept, List.take_take, min_eq_left hnumLe'] using ht
                    have hdigitOut :
                        forall q, q ∈ all.take d'.num_digits.val -> q < 10 := by
                      intro q hq
                      have hqall := List.mem_of_mem_take hq
                      simp only [all, List.mem_append] at hqall
                      rcases hqall with hqall | hqall
                      · exact hlowDigit q hqall
                      · exact hhighDigit q hqall
                    have hdigitOut' :
                        forall q, q ∈ d'.digits.val.take d'.num_digits.val -> q.val < 10 := by
                      intro q hq
                      apply hdigitOut
                      rw [← hdigitsOut]
                      exact List.mem_map.mpr ⟨q, hq, rfl⟩
                    have hlenOut :
                        ((d'.digits.val.take d'.num_digits.val).map
                            (fun z => z.val)).length = d'.num_digits.val := by
                      rw [hdigitsOut]
                      have hnumAll : d'.num_digits.val <= all.length :=
                        le_trans hnumLe' hwriteLen
                      simp [hnumAll, hnonzero]
                    have hne : d'.num_digits.val ≠ 0 := by simpa using hnonzero
                    have hlastOut :
                        ((d'.digits.val.take d'.num_digits.val).map
                            (fun z => z.val)).getLast? != some 0 := by
                      have hl := hlast.resolve_left hne
                      have hl' : ¬ (a2.val)[d'.num_digits.val - 1]? = some 0#u8 := by
                        simpa [pre, hdigEq'] using hl
                      rw [hdigEq']
                      simp only [bne_iff_ne]
                      intro hh
                      apply hl'
                      have hhs : 0 < d'.num_digits.val ∧
                          ∃ a, (a2.val)[d'.num_digits.val - 1]? = some a ∧ a.val = 0 := by
                        simpa [List.getLast?_eq_getElem?, List.getElem?_map,
                          List.getElem?_take, hlenOut, hnumCap, hnumLe'] using hh
                      rcases hhs with ⟨_, a, ha, ha0⟩
                      have haeq : a = 0#u8 := UScalar.eq_of_val_eq (by simpa using ha0)
                      simpa [haeq] using ha
                    have hnegVal : negRange.val = -2047 := by
                      have hc :
                          num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE =
                            2047#i32 := by
                        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
                        rfl
                      rw [hc] at hnegRange
                      change ok ((-2047)#i32) = ok negRange at hnegRange
                      cases hnegRange
                      rfl
                    have hpointLower : -2047 ≤ d'.decimal_point.val := by
                      have hpnt : d'.decimal_point = point := by simpa [pre] using hpointEq
                      have hnot : ¬ point.val < negRange.val := by
                        simpa [UScalar.lt_equiv] using hreset
                      rw [hpnt]
                      omega
                    let value' : Real :=
                      (List.foldl (fun n digit => 10 * n + digit) 0
                        ((d'.digits.val.take d'.num_digits.val).map (fun z => z.val)) : Nat) *
                        (10 : Real) ^ (d'.decimal_point.val - (d'.num_digits.val : Int))
                    have hp2 : 0 < (2 : Real) ^ shift.val := by positivity
                    have hdivX : X / (2 : Real) ^ shift.val = value' + eta' := by
                      simpa [value'] using hscaled
                    have hdivB : B / (2 : Real) ^ shift.val = value' + u' := by
                      rw [hB]
                      simpa [value'] using hendpoint.symm
                    set_option maxHeartbeats 800000 in
                    have hfirst : exists q qs,
                        (d'.digits.val.take d'.num_digits.val).map (fun z => z.val) = q :: qs /\
                          1 <= q /\ q < 10 := by
                      have foldFrom (ys : List Nat) (a : Nat) :
                          ys.foldl (fun n z => 10 * n + z) a =
                            a * 10 ^ ys.length + F ys := by
                        induction ys generalizing a with
                        | nil => simp [F]
                        | cons y ys ih =>
                          simp only [List.foldl_cons, List.length_cons]
                          rw [ih, pow_succ]
                          change (10 * a + y) * 10 ^ ys.length + F ys =
                            a * (10 ^ ys.length * 10) +
                              (y :: ys).foldl (fun n z => 10 * n + z) 0
                          simp only [List.foldl_cons]
                          rw [ih]
                          ring
                      have foldLt (ys : List Nat)
                          (hd : forall q, q ∈ ys -> q < 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 htail : forall q, q ∈ ys -> q < 10 := by
                            intro q hq
                            exact hd q (by simp [hq])
                          have hi := ih htail
                          have hf : F (y :: ys) = y * 10 ^ ys.length + F ys := by
                            simpa [F] using foldFrom ys y
                          rw [hf]
                          simp only [List.length_cons]
                          rw [pow_succ]
                          have hp : 0 < 10 ^ ys.length := by positivity
                          have h1 : y * 10 ^ ys.length < 10 * 10 ^ ys.length :=
                            Nat.mul_lt_mul_of_pos_right hy hp
                          have h2 : F ys < 10 ^ ys.length := hi
                          have h2' : F ys + 1 <= 10 ^ ys.length := by omega
                          have hy' : y <= 9 := by omega
                          have hsum : y * 10 ^ ys.length + F ys + 1 <=
                              10 * 10 ^ ys.length := by
                            calc
                              y * 10 ^ ys.length + F ys + 1 <=
                                  y * 10 ^ ys.length + 10 ^ ys.length := by omega
                              _ <= 9 * 10 ^ ys.length + 10 ^ ys.length := by
                                exact Nat.add_le_add_right
                                  (Nat.mul_le_mul_right _ hy') _
                              _ = 10 * 10 ^ ys.length := by ring
                          simpa [Nat.mul_comm] using Nat.lt_of_succ_le hsum
                      have headPos (q : Nat) (tail : List Nat)
                          (A S R : Nat) (hd : forall x, x ∈ tail -> x < 10)
                          (heq : A * 10 ^ (tail.length + 1) + S =
                            base * (10 * F (q :: tail)) + R)
                          (hA : base <= A) (hR : R < 10 * base) : 1 <= q := by
                        by_contra hq
                        have hq0 : q = 0 := by omega
                        have ht := foldLt tail hd
                        have htp : F tail + 1 <= 10 ^ tail.length := by omega
                        have hten : 10 * F tail + 10 <=
                            10 ^ (tail.length + 1) := by
                          have hh := Nat.mul_le_mul_left 10 htp
                          simpa [pow_succ, Nat.mul_comm, Nat.mul_left_comm,
                            Nat.mul_add] using hh
                        have hplus : R + 1 <= 10 * base := by omega
                        have hright : base * (10 * F tail) + R <
                            base * 10 ^ (tail.length + 1) := by
                          have hmul : base * (10 * F tail) + R + 1 <=
                              base * 10 ^ (tail.length + 1) := by
                            calc
                              base * (10 * F tail) + R + 1 <=
                                  base * (10 * F tail) + 10 * base :=
                                Nat.add_le_add_left hplus _
                              _ = base * (10 * F tail + 10) := by ring
                              _ <= base * 10 ^ (tail.length + 1) :=
                                Nat.mul_le_mul_left base hten
                          exact Nat.lt_of_succ_le hmul
                        have hleft : base * 10 ^ (tail.length + 1) <=
                            A * 10 ^ (tail.length + 1) + S := by
                          have hp := Nat.mul_le_mul_right (10 ^ (tail.length + 1)) hA
                          omega
                        have heq0 : A * 10 ^ (tail.length + 1) + S =
                            base * (10 * F tail) + R := by
                          simpa [hq0, F] using heq
                        omega
                      cases low with
                      | nil =>
                        cases high with
                        | nil =>
                          have hz : d'.num_digits.val = 0 := by
                            have hw : write2.val = 0 := by simp [hwriteAll, all]
                            omega
                          omega
                        | cons q tail =>
                          have hh := hlowLen
                          simp at hh
                          have hreadGe : d0.num_digits.val <= read.val := by omega
                          have hsubzero : d0.num_digits.val - read.val = 0 :=
                            Nat.sub_eq_zero_of_le hreadGe
                          have hdrop :
                              (xs.take d0.num_digits.val).drop read.val = [] :=
                            List.drop_eq_nil_of_le (by simp; omega)
                          have haccEq : acc.val = acc1.val := by
                            have hm := hmainNum
                            simp [hsubzero, hdrop, F] at hm
                            omega
                          have hqlo : 1 <= q := by
                            apply headPos q tail acc1.val 0 0
                            · intro x hx
                              exact hhighDigit x (by simp [hx])
                            · simpa [base, F] using hfinalNum
                            · rw [← haccEq]
                              exact hBacc
                            · simp [base]
                          have hqhi : q < 10 := hhighDigit q (by simp)
                          refine ⟨q, (tail.take (d'.num_digits.val - 1)), ?_, hqlo, hqhi⟩
                          rw [hdigitsOut]
                          simp [all, Nat.pos_of_ne_zero hne]
                      | cons q tail =>
                        have hlenEq : d0.num_digits.val - read.val = tail.length + 1 := by
                          have hh := hlowLen
                          simp at hh
                          omega
                        have hqlo : 1 <= q := by
                          apply headPos q tail acc.val
                            (F ((xs.take d0.num_digits.val).drop read.val)) acc1.val
                          · intro x hx
                            exact hlowDigit x (by simp [hx])
                          · rw [← hlenEq]
                            simpa [F] using hmainNum
                          · exact hBacc
                          · exact hacc1
                        have hqhi : q < 10 := hlowDigit q (by simp)
                        refine ⟨q, (tail ++ high).take (d'.num_digits.val - 1), ?_, hqlo, hqhi⟩
                        rw [hdigitsOut]
                        simp [all, Nat.pos_of_ne_zero hne]
                    refine ⟨hnumCap, hdigitOut', ?_, ?_, hpointLower⟩
                    · rcases hfirst with ⟨q, qs, hq, hqlo, hqhi⟩
                      exact ⟨q, qs, hq, hqlo, hqhi, hlenOut, hlastOut⟩
                    · refine ⟨eta', u', ?_, ?_, heta'0, heta'u', htrunc⟩
                      · simpa [value'] using hdivX
                      · simpa [value'] using hdivB

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalSeqDyadicShiftSemantics
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE
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: DecimalSeqRightShiftCanonical]
theorem DecimalSeqRightShiftCanonical :
    forall (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift : Std.Usize) (X B : Real),
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun z => z.val)
      let integer : Nat := digits.foldl (fun n digit => 10 * n + digit) 0
      let value : Real := integer * (10 : Real) ^
        (d.decimal_point.val - (d.num_digits.val : Int))
      d.num_digits.val <= 768 /\
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
      (exists z zs, digits = z :: zs /\ 1 <= z /\ z < 10 /\
        digits.length = d.num_digits.val) ->
      (exists eta u : Real,
        X = value + eta /\ B = value + u /\
        0 <= eta /\ eta < u /\
        (d.truncated = true <-> 0 < eta)) ->
      shift.val <= 60 ->
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d' ->
      d'.num_digits.val != 0 ->
      let digits' :=
        (d'.digits.val.take d'.num_digits.val).map (fun z => z.val)
      let integer' : Nat := digits'.foldl (fun n digit => 10 * n + digit) 0
      let value' : Real := integer' * (10 : Real) ^
        (d'.decimal_point.val - (d'.num_digits.val : Int))
      d'.num_digits.val <= 768 /\
      (forall z, z ∈ d'.digits.val.take d'.num_digits.val -> z.val < 10) /\
      (exists z zs, digits' = z :: zs /\ 1 <= z /\ z < 10 /\
        digits'.length = d'.num_digits.val /\
        digits'.getLast? != some 0) /\
      (exists eta' u' : Real,
        X / (2 : Real) ^ shift.val = value' + eta' /\
        B / (2 : Real) ^ shift.val = value' + u' /\
        0 <= eta' /\ eta' < u' /\
        (d'.truncated = true <-> 0 < eta')) /\
      -2047 <= d'.decimal_point.val := by
-- BODY
  dsimp only
  intro d d' shift X B hinput hend hshift hcall hnonzero
  rcases hinput with ⟨hcap, hdigit, hshape⟩
  rcases hend with ⟨eta, u, hX, hB, heta0, hetau, hsticky⟩
  rcases hshape with ⟨z, zs, hdigits, hzlo, hzhi, hlen⟩
  let integer : Nat :=
    ((d.digits.val.take d.num_digits.val).map (fun z => z.val)).foldl
      (fun n digit => 10 * n + digit) 0
  have hvalue : 0 < (integer : Real) *
        (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
    have hfold : 0 <
        ((d.digits.val.take d.num_digits.val).map (fun z => z.val)).foldl
          (fun n digit => 10 * n + digit) 0 := by
      rw [hdigits]
      simp only [List.foldl_cons]
      have hrest (ys : List Nat) (a : Nat) :
          a ≤ ys.foldl (fun n digit => 10 * n + digit) a := by
        induction ys generalizing a with
        | nil => simp
        | cons b bs ih =>
            simp only [List.foldl_cons]
            have hi := ih (10 * a + b)
            omega
      have hmono := hrest zs (10 * 0 + z)
      omega
    have hfoldR : 0 < (integer : Real) := by
      exact_mod_cast hfold
    have hpow : 0 < (10 : Real) ^
        (d.decimal_point.val - (d.num_digits.val : Int)) :=
      zpow_pos (by norm_num) _
    exact mul_pos (by simpa [integer] using hfoldR) hpow
  have hdyadic := DecimalSeqDyadicShiftSemantics
  dsimp only at hdyadic
  have htransport := hdyadic.2 d d' shift X eta u hshift hvalue hdigit hcall
      (by simpa using hX) heta0 hetau hsticky
  rcases htransport with hreset | ⟨eta', u', hscaled, heta'0, heta'u', htrunc,
    hendpoint, hcomparison⟩
  · have hne : d'.num_digits.val ≠ 0 := by simpa using hnonzero
    omega
  clear hdyadic
  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 base : Nat := 2 ^ shift.val
  have hpowpos : 0 < (10 : Real) ^
      (d.decimal_point.val - (d.num_digits.val : Int)) :=
    zpow_pos (by norm_num) _
  have hIreal : 0 < (integer : Real) := by
    nlinarith [hvalue, hpowpos]
  have hIpos : 0 < integer := 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 [integer, 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 hp | hp <;> 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
            have hne : reset.num_digits.val ≠ 0 := by simpa [hd'] using hnonzero
            subst d'
            simp [reset] at hne
          · 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 * base /\
                        acc.val * 10 ^ (d0.num_digits.val - read.val) +
                            F ((xs.take d0.num_digits.val).drop read.val) =
                          base * (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, base] 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 hlowWrite : write1.val = low.length := by omega
                    have hwriteAll : write2.val = min all.length 768 := by
                      rw [hwrite2, hlowWrite]
                      simp [all]
                    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
                    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 [all] at hj2
                            omega
                          have hjEq : write1.val + t = j := by
                            simp [t, hlowWrite]
                            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
                    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
                      ⟨hdigEq, hpointEq, htruncEq, hnumLe, hzeros, hlast, hvalueEq⟩
                    have hdigEq' : d'.digits = a2 := by simpa [pre] using hdigEq
                    have hnumLe' : d'.num_digits.val <= write2.val := by
                      simpa [pre] using hnumLe
                    have hnumCap : d'.num_digits.val <= 768 := by omega
                    have hdigitsOut :
                        ((d'.digits.val.take d'.num_digits.val).map
                            (fun z => z.val)) = all.take d'.num_digits.val := by
                      rw [hdigEq']
                      have ht := congrArg (List.take d'.num_digits.val) hpreDigits
                      simpa [kept, List.take_take, min_eq_left hnumLe'] using ht
                    have hdigitOut :
                        forall q, q ∈ all.take d'.num_digits.val -> q < 10 := by
                      intro q hq
                      have hqall := List.mem_of_mem_take hq
                      simp only [all, List.mem_append] at hqall
                      rcases hqall with hqall | hqall
                      · exact hlowDigit q hqall
                      · exact hhighDigit q hqall
                    have hdigitOut' :
                        forall q, q ∈ d'.digits.val.take d'.num_digits.val -> q.val < 10 := by
                      intro q hq
                      apply hdigitOut
                      rw [← hdigitsOut]
                      exact List.mem_map.mpr ⟨q, hq, rfl⟩
                    have hlenOut :
                        ((d'.digits.val.take d'.num_digits.val).map
                            (fun z => z.val)).length = d'.num_digits.val := by
                      rw [hdigitsOut]
                      have hnumAll : d'.num_digits.val <= all.length :=
                        le_trans hnumLe' hwriteLen
                      simp [hnumAll, hnonzero]
                    have hne : d'.num_digits.val ≠ 0 := by simpa using hnonzero
                    have hlastOut :
                        ((d'.digits.val.take d'.num_digits.val).map
                            (fun z => z.val)).getLast? != some 0 := by
                      have hl := hlast.resolve_left hne
                      have hl' : ¬ (a2.val)[d'.num_digits.val - 1]? = some 0#u8 := by
                        simpa [pre, hdigEq'] using hl
                      rw [hdigEq']
                      simp only [bne_iff_ne]
                      intro hh
                      apply hl'
                      have hhs : 0 < d'.num_digits.val ∧
                          ∃ a, (a2.val)[d'.num_digits.val - 1]? = some a ∧ a.val = 0 := by
                        simpa [List.getLast?_eq_getElem?, List.getElem?_map,
                          List.getElem?_take, hlenOut, hnumCap, hnumLe'] using hh
                      rcases hhs with ⟨_, a, ha, ha0⟩
                      have haeq : a = 0#u8 := UScalar.eq_of_val_eq (by simpa using ha0)
                      simpa [haeq] using ha
                    have hnegVal : negRange.val = -2047 := by
                      have hc :
                          num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE =
                            2047#i32 := by
                        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
                        rfl
                      rw [hc] at hnegRange
                      change ok ((-2047)#i32) = ok negRange at hnegRange
                      cases hnegRange
                      rfl
                    have hpointLower : -2047 ≤ d'.decimal_point.val := by
                      have hpnt : d'.decimal_point = point := by simpa [pre] using hpointEq
                      have hnot : ¬ point.val < negRange.val := by
                        simpa [UScalar.lt_equiv] using hreset
                      rw [hpnt]
                      omega
                    let value' : Real :=
                      (List.foldl (fun n digit => 10 * n + digit) 0
                        ((d'.digits.val.take d'.num_digits.val).map (fun z => z.val)) : Nat) *
                        (10 : Real) ^ (d'.decimal_point.val - (d'.num_digits.val : Int))
                    have hp2 : 0 < (2 : Real) ^ shift.val := by positivity
                    have hdivX : X / (2 : Real) ^ shift.val = value' + eta' := by
                      simpa [value'] using hscaled
                    have hdivB : B / (2 : Real) ^ shift.val = value' + u' := by
                      rw [hB]
                      simpa [value'] using hendpoint.symm
                    set_option maxHeartbeats 800000 in
                    have hfirst : exists q qs,
                        (d'.digits.val.take d'.num_digits.val).map (fun z => z.val) = q :: qs /\
                          1 <= q /\ q < 10 := by
                      have foldFrom (ys : List Nat) (a : Nat) :
                          ys.foldl (fun n z => 10 * n + z) a =
                            a * 10 ^ ys.length + F ys := by
                        induction ys generalizing a with
                        | nil => simp [F]
                        | cons y ys ih =>
                          simp only [List.foldl_cons, List.length_cons]
                          rw [ih, pow_succ]
                          change (10 * a + y) * 10 ^ ys.length + F ys =
                            a * (10 ^ ys.length * 10) +
                              (y :: ys).foldl (fun n z => 10 * n + z) 0
                          simp only [List.foldl_cons]
                          rw [ih]
                          ring
                      have foldLt (ys : List Nat)
                          (hd : forall q, q ∈ ys -> q < 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 htail : forall q, q ∈ ys -> q < 10 := by
                            intro q hq
                            exact hd q (by simp [hq])
                          have hi := ih htail
                          have hf : F (y :: ys) = y * 10 ^ ys.length + F ys := by
                            simpa [F] using foldFrom ys y
                          rw [hf]
                          simp only [List.length_cons]
                          rw [pow_succ]
                          have hp : 0 < 10 ^ ys.length := by positivity
                          have h1 : y * 10 ^ ys.length < 10 * 10 ^ ys.length :=
                            Nat.mul_lt_mul_of_pos_right hy hp
                          have h2 : F ys < 10 ^ ys.length := hi
                          have h2' : F ys + 1 <= 10 ^ ys.length := by omega
                          have hy' : y <= 9 := by omega
                          have hsum : y * 10 ^ ys.length + F ys + 1 <=
                              10 * 10 ^ ys.length := by
                            calc
                              y * 10 ^ ys.length + F ys + 1 <=
                                  y * 10 ^ ys.length + 10 ^ ys.length := by omega
                              _ <= 9 * 10 ^ ys.length + 10 ^ ys.length := by
                                exact Nat.add_le_add_right
                                  (Nat.mul_le_mul_right _ hy') _
                              _ = 10 * 10 ^ ys.length := by ring
                          simpa [Nat.mul_comm] using Nat.lt_of_succ_le hsum
                      have headPos (q : Nat) (tail : List Nat)
                          (A S R : Nat) (hd : forall x, x ∈ tail -> x < 10)
                          (heq : A * 10 ^ (tail.length + 1) + S =
                            base * (10 * F (q :: tail)) + R)
                          (hA : base <= A) (hR : R < 10 * base) : 1 <= q := by
                        by_contra hq
                        have hq0 : q = 0 := by omega
                        have ht := foldLt tail hd
                        have htp : F tail + 1 <= 10 ^ tail.length := by omega
                        have hten : 10 * F tail + 10 <=
                            10 ^ (tail.length + 1) := by
                          have hh := Nat.mul_le_mul_left 10 htp
                          simpa [pow_succ, Nat.mul_comm, Nat.mul_left_comm,
                            Nat.mul_add] using hh
                        have hplus : R + 1 <= 10 * base := by omega
                        have hright : base * (10 * F tail) + R <
                            base * 10 ^ (tail.length + 1) := by
                          have hmul : base * (10 * F tail) + R + 1 <=
                              base * 10 ^ (tail.length + 1) := by
                            calc
                              base * (10 * F tail) + R + 1 <=
                                  base * (10 * F tail) + 10 * base :=
                                Nat.add_le_add_left hplus _
                              _ = base * (10 * F tail + 10) := by ring
                              _ <= base * 10 ^ (tail.length + 1) :=
                                Nat.mul_le_mul_left base hten
                          exact Nat.lt_of_succ_le hmul
                        have hleft : base * 10 ^ (tail.length + 1) <=
                            A * 10 ^ (tail.length + 1) + S := by
                          have hp := Nat.mul_le_mul_right (10 ^ (tail.length + 1)) hA
                          omega
                        have heq0 : A * 10 ^ (tail.length + 1) + S =
                            base * (10 * F tail) + R := by
                          simpa [hq0, F] using heq
                        omega
                      cases low with
                      | nil =>
                        cases high with
                        | nil =>
                          have hz : d'.num_digits.val = 0 := by
                            have hw : write2.val = 0 := by simp [hwriteAll, all]
                            omega
                          omega
                        | cons q tail =>
                          have hh := hlowLen
                          simp at hh
                          have hreadGe : d0.num_digits.val <= read.val := by omega
                          have hsubzero : d0.num_digits.val - read.val = 0 :=
                            Nat.sub_eq_zero_of_le hreadGe
                          have hdrop :
                              (xs.take d0.num_digits.val).drop read.val = [] :=
                            List.drop_eq_nil_of_le (by simp; omega)
                          have haccEq : acc.val = acc1.val := by
                            have hm := hmainNum
                            simp [hsubzero, hdrop, F] at hm
                            omega
                          have hqlo : 1 <= q := by
                            apply headPos q tail acc1.val 0 0
                            · intro x hx
                              exact hhighDigit x (by simp [hx])
                            · simpa [base, F] using hfinalNum
                            · rw [← haccEq]
                              exact hBacc
                            · simp [base]
                          have hqhi : q < 10 := hhighDigit q (by simp)
                          refine ⟨q, (tail.take (d'.num_digits.val - 1)), ?_, hqlo, hqhi⟩
                          rw [hdigitsOut]
                          simp [all, Nat.pos_of_ne_zero hne]
                      | cons q tail =>
                        have hlenEq : d0.num_digits.val - read.val = tail.length + 1 := by
                          have hh := hlowLen
                          simp at hh
                          omega
                        have hqlo : 1 <= q := by
                          apply headPos q tail acc.val
                            (F ((xs.take d0.num_digits.val).drop read.val)) acc1.val
                          · intro x hx
                            exact hlowDigit x (by simp [hx])
                          · rw [← hlenEq]
                            simpa [F] using hmainNum
                          · exact hBacc
                          · exact hacc1
                        have hqhi : q < 10 := hlowDigit q (by simp)
                        refine ⟨q, (tail ++ high).take (d'.num_digits.val - 1), ?_, hqlo, hqhi⟩
                        rw [hdigitsOut]
                        simp [all, Nat.pos_of_ne_zero hne]
                    refine ⟨hnumCap, hdigitOut', ?_, ?_, hpointLower⟩
                    · rcases hfirst with ⟨q, qs, hq, hqlo, hqhi⟩
                      exact ⟨q, qs, hq, hqlo, hqhi, hlenOut, hlastOut⟩
                    · refine ⟨eta', u', ?_, ?_, heta'0, heta'u', htrunc⟩
                      · simpa [value'] using hdivX
                      · simpa [value'] using hdivB

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 26 entries
Technical checking records
Local closure summary
{
  "node": "DecimalSeqRightShiftCanonical",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "82570ea1c5c4d11ce44f5e48e0ffdf019e26a20c53a8fcb7e87ed3b45356448b",
  "active_statement_hash": "af855019bf3f3ebf3a6477f2a8a03a1f64269dcff415b7eb7c4bc110cc407820",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "ab64d13f256386ee0e494a5abcc6a5db49512cf3d90bbdfcabd6327793a31f95",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-188",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalRightShiftFinalLoopSemantics": "79a214a5ee500eac",
    "DecimalRightShiftMainLoopSemantics": "647cdcc98ff47e21",
    "DecimalRightShiftPrefixLoopSemantics": "0d5cb25f18eedbfb",
    "DecimalSeqDyadicShiftSemantics": "9d844d655b9f8a27",
    "DecimalSeqTrimSemantics": "e4b2cbb62f7171af",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE": "01b84a7a35e5bcf4",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "5f4488c177675662"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "b0286c42454b158d",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE": "0cc5155758bc976a",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift": "fd298e37d5ae4f86",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "4db7c37f5da1c6e0",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_trim": "faa9fd92b8c29c14"
  }
}
Prose assessment
{
  "label": "Lean proof closed; predictive prose review no longer required",
  "assessment_status": null,
  "fingerprint_relation": "unknown",
  "assessment_fingerprint": null,
  "current_fingerprint": "sound-v2:f60ff94c53fe7576169606c7d1a82c8fa8d7d64f85f33155f8a88da1b46727d9",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}