DecimalRightShiftMainLoopSemantics

Lean checked

Statement

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

Helper · Exact semantics of the main decimal right-shift output loop

Let a be a 768-cell U8 array, let 0\le k\le60, put B=2^k, and let 1\le r\le i with r\le768. Suppose every entry in a[0:i] is below ten, and let a U64 accumulator c satisfy c<10B. Suppose also that the modeled U64 operations successfully compute
p=1\mathbin{\mathtt{<<}}k, \qquad m=p-1.
If the extracted right_shift_loop1, started with limit i, array a, shift k, read cursor r, write cursor zero, accumulator c, and mask m, returns array a', write cursor w', and accumulator c', then there is a list E of natural decimal digits such that
i\le768,\qquad |E|=i-r=w',\qquad (\forall z\in E,\ z<10),\qquad c'<10B. \tag{1}
Writing x for the list of natural values of all cells of a and F(L)=\operatorname{fold}_{10}(L), the exact long-division equation is
c10^{i-r}+F(x[r:i])=B\bigl(10F(E)\bigr)+c'. \tag{2}
For every j<|E|, cell j of a' has natural value E_j. Every physical cell j\ge w' is unchanged from a; in particular every cell j\ge i is unchanged from a.

Lean statement

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

import Tablet.DecimalRightShiftU64DyadicBridge
import Tablet.ResultLoopPartial
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: DecimalRightShiftMainLoopSemantics]
theorem DecimalRightShiftMainLoopSemantics
    (a a' : Array Std.U8 768#usize)
    (shift r i write' : Std.Usize) (pow mask acc acc' : Std.U64)
    (hshift : shift.val <= 60)
    (hrpos : 1 <= r.val)
    (hri : r.val <= i.val)
    (hrcap : r.val <= 768)
    (hacc : acc.val < 10 * 2 ^ shift.val)
    (hdigit : forall z, z ∈ a.val.take i.val -> z.val < 10)
    (hpow : 1#u64 <<< shift = ok pow)
    (hmask : pow - 1#u64 = ok mask)
    (hloop :
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
        i a shift r 0#usize acc mask = ok (a', write', acc')) :
    let xs := a.val.map (fun z => z.val)
    let F := fun ys : List Nat => ys.foldl (fun n z => 10 * n + z) 0
    let B := 2 ^ shift.val
    i.val <= 768 /\
    exists outs : List Nat,
      outs.length = i.val - r.val /\
      (forall z, z ∈ outs -> z < 10) /\
      write'.val = i.val - r.val /\
      acc'.val < 10 * B /\
      acc.val * 10 ^ (i.val - r.val) + F ((xs.take i.val).drop r.val) =
        B * (10 * F outs) + acc'.val /\
      (forall j, j < outs.length ->
        (a'.val.map (fun z : Std.U8 => z.val))[j]? = outs[j]?) /\
      (forall j, j < 768 -> write'.val <= j -> a'.val[j]? = a.val[j]?) /\
      (forall j, j < 768 -> i.val <= j -> a'.val[j]? = a.val[j]?) := 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

Unfold the main output loop and its body from num imp dec2flt decimal seq DecimalSeq right shift, and apply the result-loop induction principle ResultLoopPartial. Keep the initial array a, cursor r, and accumulator c immutable. For a current state (A,s,w,C) use the invariant that
r\le s\le i,\qquad s\le768,\qquad w=s-r,\qquad C<10B, \tag{3}
and that there is a decimal-digit list E_s with
|E_s|=w, \qquad c10^{s-r}+F(x[r:s])=B\bigl(10F(E_s)\bigr)+C. \tag{4}
For every j<|E_s|, cell j of A has natural value (E_s)_j, and every physical cell j\ge w still equals cell j of the immutable array a. Initially s=r, w=0, and C=c. Take E_r empty. The slice x[r:r] is empty, so (3)--(4) and both array clauses are immediate.
Suppose s<i. Since r\ge1 and w=s-r, one has w<s. A normal return of this body step forces its fixed-array read at s to succeed; if s\ge768, the extracted array-index operation fails and cannot equal the assumed normal body result. Hence s<768. The unchanged suffix clause therefore identifies the current array cell read at s with the immutable original cell a[s], and the digit hypothesis supplies
d=a[s]<10. \tag{5}
Apply DecimalRightShiftU64DyadicBridge to the successful right shift of C and the exact mask operation in this body step. If their outputs are q and e, respectively, the helper gives
q=\lfloor C/B\rfloor,\qquad e=C\bmod B,\qquad C=qB+e,qquad e<B, \tag{6}
and also 10e+d<2^{64}. From C<10B and B>0, q=C/B<10. Consequently the extracted U64-to-U8 cast of q is exact. The multiplication 10e and addition of the U64 cast of d are both within range by the last bound supplied by the bridge, and the U8-to-U64 cast of d is exact. Thus the new accumulator is exactly
C_1=10e+d. \tag{7}
In particular, using e<B and d<10,
C_1\le10(B-1)+9<10B, \tag{8}
so the strict accumulator bound required at the next iteration is preserved. The two checked cursor increments are valid because s<768 and w<s.
Set E_{s+1}=E_s\mathbin{+\!+}[q]. Its length is w+1, all its entries are below ten, and
F(E_{s+1})=10F(E_s)+q, \qquad F(x[r:s+1])=10F(x[r:s])+d. \tag{9}
Multiplying (4) by ten, adding d, and substituting (6)--(7) gives
\begin{aligned} c10^{s+1-r}+F(x[r:s+1]) &=10\left(B(10F(E_s))+C\right)+d\\ &=B\left(10(10F(E_s)+q)\right)+(10e+d)\\ &=B\bigl(10F(E_{s+1})\bigr)+C_1, \end{aligned}
which is (4) for the next state.
The array write is at w<s<768, hence is in range. It stores the exact U8 representation of q, establishing the new final cell of the emitted prefix. Earlier prefix cells have indices below w and are not changed. The update changes only cell w, so every cell at or above w+1 remains equal to its immutable value in a. Advancing both cursors also gives w+1=(s+1)-r. This proves every continuation clause of the invariant, including all read, write, cast, and U64 side conditions.
At a normal finish the body test is false. Together with s\le i, this gives s=i. The invariant bound s\le768 also proves i\le768. Thus (3)--(4) become (1)--(2), and the emitted-prefix and unchanged-suffix clauses have exactly the asserted form. Finally, w'=i-r\le i, so every j\ge i is also at or above w' and is unchanged. The conclusion follows from ResultLoopPartial.

Lean proof


  dsimp only
  let xs := a.val.map (fun z => z.val)
  let F := fun ys : List Nat => ys.foldl (fun n z => 10 * n + z) 0
  let B := 2 ^ shift.val
  have hBpos : 0 < B := by simp [B]
  have hxslen : xs.length = 768 := by simp [xs, a.property]
  have foldlFrom (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
  let Inv := fun (x : Array Std.U8 768#usize × Std.Usize × Std.Usize × Std.U64) =>
    let (ac, s, w, c) := x
    r.val <= s.val /\
    s.val <= i.val /\
    s.val <= 768 /\
    w.val = s.val - r.val /\
    c.val < 10 * B /\
    exists outs : List Nat,
      outs.length = w.val /\
      (forall z, z ∈ outs -> z < 10) /\
      acc.val * 10 ^ (s.val - r.val) + F ((xs.take s.val).drop r.val) =
        B * (10 * F outs) + c.val /\
      (forall j, j < outs.length ->
        (ac.val.map (fun z : Std.U8 => z.val))[j]? = outs[j]?) /\
      (forall j, j < 768 -> w.val <= j -> ac.val[j]? = a.val[j]?)
  let Post := fun (x : Array Std.U8 768#usize × Std.Usize × Std.U64) =>
    let (ac, w, c) := x
    i.val <= 768 /\
    exists outs : List Nat,
      outs.length = i.val - r.val /\
      (forall z, z ∈ outs -> z < 10) /\
      w.val = i.val - r.val /\
      c.val < 10 * B /\
      acc.val * 10 ^ (i.val - r.val) + F ((xs.take i.val).drop r.val) =
        B * (10 * F outs) + c.val /\
      (forall j, j < outs.length ->
        (ac.val.map (fun z : Std.U8 => z.val))[j]? = outs[j]?) /\
      (forall j, j < 768 -> w.val <= j -> ac.val[j]? = a.val[j]?) /\
      (forall j, j < 768 -> i.val <= j -> ac.val[j]? = a.val[j]?)
  let Body := fun (x : Array Std.U8 768#usize × Std.Usize × Std.Usize × Std.U64) =>
    let (ac, s, w, c) := x
    num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1.body
      i shift mask ac s w c
  have hp : Post (a', write', acc') := by
    refine ResultLoopPartial Inv Post Body ?_ (a, r, 0#usize, acc)
      (a', write', acc') ?_ ?_
    · intro x hx out hout
      rcases x with ⟨ac, s, w, c⟩
      simp only [Inv] at hx
      rcases hx with ⟨hrs, hsi, hscap, hws, hc10, outs, houtslen, houtsdigit,
        hnum, harr, houtside⟩
      simp only [Body,
        num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1.body] at hout
      by_cases hslt : s.val < i.val
      · have hsWord : s < i := by simpa [UScalar.lt_equiv] using hslt
        simp only [hsWord, if_true] at hout
        obtain ⟨q, hq, hqSpec⟩ := Aeneas.Std.WP.spec_imp_exists
          (UScalar.ShiftRight_spec c shift (by
            norm_num [U64.numBits]
            omega))
        rw [hq] at hout
        simp only [bind_tc_ok, lift] at hout
        let newDigit : Std.U8 := UScalar.cast .U8 q
        rw [show UScalar.cast .U8 q = newDigit by rfl] at hout
        let rem : Std.U64 := c &&& mask
        rw [show c &&& mask = rem by rfl] at hout
        have hbridge := DecimalRightShiftU64DyadicBridge shift c pow mask q rem
          hshift hpow hmask hq (by rfl)
        simp only [B] at hbridge
        rcases hbridge with ⟨_, _, hqVal, hremVal, heuc, hremB, _, hbound⟩
        have hq10 : q.val < 10 := by
          rw [hqVal]
          exact (Nat.div_lt_iff_lt_mul hBpos).2 (by
            simpa [Nat.mul_comm] using hc10)
        have hnewDigitVal : newDigit.val = q.val := by
          simp [newDigit, UScalar.cast_val_eq, Nat.mod_eq_of_lt (by omega : q.val < 256)]
        have hmulBound : 10 * rem.val <= U64.max := by
          have hlt := hbound 0 (by omega)
          norm_num [U64.max, U64.numBits] at hlt ⊢
          omega
        obtain ⟨tenrem, htenrem, htenremVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (U64.mul_spec (x := 10#u64) (y := rem) (by simpa using hmulBound))
        rw [htenrem] at hout
        simp only [bind_tc_ok] at hout
        have hsArray : s.val < ac.val.length := by
          by_contra hnot
          have hnone : ac.val[s.val]? = none := List.getElem?_eq_none (by omega)
          simp [Array.index_usize, Array.getElem?_Usize_eq, hnone] at hout
        have hs768 : s.val < 768 := by
          have hlen : ac.val.length = 768 := by simpa using ac.property
          omega
        obtain ⟨digit, hdigitRead, hdigitReadVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (Array.index_usize_spec ac s (by simpa [Array.length] using hsArray))
        rw [hdigitRead] at hout
        simp only [bind_tc_ok, lift] at hout
        let digit64 : Std.U64 := UScalar.cast .U64 digit
        rw [show UScalar.cast .U64 digit = digit64 by rfl] at hout
        have hwslt : w.val < s.val := by omega
        have hacOrig : ac.val[s.val]? = a.val[s.val]? :=
          houtside s.val hs768 (by omega)
        have hacGet : ac.val[s.val]? = some digit :=
          getElem?_eq_some_iff.mpr ⟨hsArray, hdigitReadVal.symm⟩
        have haGet : a.val[s.val]? = some digit := by rw [← hacOrig]; exact hacGet
        have haBound : s.val < a.val.length := by
          have hlen : a.val.length = 768 := by simpa using a.property
          omega
        have haVal : a.val[s.val]'haBound = digit := by
          have hsome := List.getElem?_eq_getElem haBound
          rw [haGet] at hsome
          exact Option.some.inj hsome.symm
        have htakeBound : s.val < (a.val.take i.val).length := by
          rw [List.length_take]
          exact lt_min hslt haBound
        have htakeVal : (a.val.take i.val)[s.val]'htakeBound = digit := by
          simpa using haVal
        have hdigitMem : digit ∈ a.val.take i.val := by
          rw [← htakeVal]
          exact List.getElem_mem _
        have hdigitLt : digit.val < 10 := hdigit digit hdigitMem
        have hdigit64 : digit64.val = digit.val := by
          simp [digit64, UScalar.cast_val_eq]
        have haddBound : tenrem.val + digit64.val <= U64.max := by
          have htenExact : tenrem.val = 10 * rem.val := by simpa using htenremVal
          have hlt := hbound digit.val hdigitLt
          rw [htenExact, hdigit64]
          norm_num [U64.max, U64.numBits] at hlt ⊢
          omega
        obtain ⟨c1, hc1, hc1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (U64.add_spec (x := tenrem) (y := digit64) haddBound)
        rw [hc1] at hout
        simp only [bind_tc_ok] at hout
        have hsBound : s.val + 1 <= Usize.max := by
          simp [Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits]
          omega
        obtain ⟨s1, hs1, hs1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (Usize.add_spec (x := s) (y := 1#usize) (by simpa using hsBound))
        rw [hs1] at hout
        simp only [bind_tc_ok] at hout
        have hwArray : w.val < ac.val.length := by rw [ac.property]; norm_num; omega
        obtain ⟨ac1, hac1, hac1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (Array.update_spec ac w newDigit (by simpa [Array.length] using hwArray))
        rw [hac1] at hout
        simp only [bind_tc_ok] at hout
        have hwBound : w.val + 1 <= Usize.max := by
          simp [Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits]
          omega
        obtain ⟨w1, hw1, hw1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (Usize.add_spec (x := w) (y := 1#usize) (by simpa using hwBound))
        rw [hw1] at hout
        simp only [bind_tc_ok] at hout
        cases hout
        simp only [Inv]
        have hs1Exact : s1.val = s.val + 1 := by simpa using hs1Val
        have hw1Exact : w1.val = w.val + 1 := by simpa using hw1Val
        have htenExact : tenrem.val = 10 * rem.val := by simpa using htenremVal
        have hc1Exact : c1.val = 10 * rem.val + digit.val := by
          rw [hc1Val, htenExact, hdigit64]
        have hc1ten : c1.val < 10 * B := by rw [hc1Exact]; omega
        let newOuts := outs ++ [q.val]
        have hnewLen : newOuts.length = w1.val := by
          simp [newOuts, houtslen, hw1Exact]
        have hnewDigit : forall z, z ∈ newOuts -> z < 10 := by
          intro z hz
          simp only [newOuts, List.mem_append, List.mem_singleton] at hz
          rcases hz with hz | rfl
          · exact houtsdigit z hz
          · exact hq10
        have hxsGet : xs[s.val]? = some digit.val := by
          simp only [xs, List.getElem?_map]
          rw [haGet]
          rfl
        have htakeStep : xs.take (s.val + 1) = xs.take s.val ++ [digit.val] := by
          rw [List.take_succ, hxsGet]
          rfl
        have hdropStep : (xs.take (s.val + 1)).drop r.val =
            (xs.take s.val).drop r.val ++ [digit.val] := by
          rw [htakeStep, List.drop_append_of_le_length]
          simp
          omega
        have hsegStep : F ((xs.take (s.val + 1)).drop r.val) =
            10 * F ((xs.take s.val).drop r.val) + digit.val := by
          rw [hdropStep]
          simp [F]
        have houtFold : F newOuts = 10 * F outs + q.val := by
          simp [newOuts, F]
        have hnumNew :
            acc.val * 10 ^ (s1.val - r.val) + F ((xs.take s1.val).drop r.val) =
              B * (10 * F newOuts) + c1.val := by
          rw [hs1Exact]
          have hsub : s.val + 1 - r.val = (s.val - r.val) + 1 := by omega
          rw [hsub, pow_succ, hsegStep]
          calc
            acc.val * (10 ^ (s.val - r.val) * 10) +
                (10 * F ((xs.take s.val).drop r.val) + digit.val) =
                10 * (acc.val * 10 ^ (s.val - r.val) +
                  F ((xs.take s.val).drop r.val)) + digit.val := by ring
            _ = 10 * (B * (10 * F outs) + c.val) + digit.val := by rw [hnum]
            _ = B * (10 * F newOuts) + c1.val := by
              rw [houtFold, hc1Exact, heuc]
              ring
        refine ⟨by omega, by omega, by omega, by omega, hc1ten, newOuts, hnewLen,
          hnewDigit, hnumNew, ?_, ?_⟩
        · intro j hj
          rw [hac1Val]
          simp only [Aeneas.Std.Array.set, List.map_set, List.getElem?_set]
          by_cases hjw : j = w.val
          · subst j
            have hw768 : w.val < 768 := by omega
            simp [newOuts, houtslen, hnewDigitVal, hw768]
          · have hjold : j < outs.length := by
              simp [newOuts] at hj
              omega
            have hwj : w.val ≠ j := Ne.symm hjw
            simp only [hwj, if_false]
            rw [List.getElem?_append_left (by simpa using hjold)]
            simpa [List.getElem?_map] using harr j hjold
        · intro j hj hw1j
          rw [hac1Val]
          simp only [Aeneas.Std.Array.set, List.getElem?_set]
          have hne : w.val ≠ j := by omega
          simp [hne]
          exact houtside j hj (by omega)
      · have hsNotWord : ¬ s < i := by simpa [UScalar.lt_equiv] using hslt
        simp only [hsNotWord, if_false] at hout
        cases hout
        simp only [Post]
        have hsiEq : s.val = i.val := by omega
        refine ⟨by omega, outs, ?_, houtsdigit, ?_, hc10, ?_, harr, houtside, ?_⟩
        · omega
        · omega
        · simpa [hsiEq] using hnum
        · intro j hj hij
          exact houtside j hj (by omega)
    · simp only [Inv]
      refine ⟨le_rfl, hri, hrcap, by simp, hacc, ?_⟩
      refine ⟨[], by simp, by simp, ?_, ?_, ?_⟩
      · have hdrop : (xs.take r.val).drop r.val = [] := by
          exact List.drop_eq_nil_of_le (by simp)
        simp [hdrop, F]
      · intro j hj
        simp at hj
      · intro j hj _
        trivial
    · exact hloop
  simpa [Post, F, B, xs] using hp

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalRightShiftU64DyadicBridge
import Tablet.ResultLoopPartial
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: DecimalRightShiftMainLoopSemantics]
theorem DecimalRightShiftMainLoopSemantics
    (a a' : Array Std.U8 768#usize)
    (shift r i write' : Std.Usize) (pow mask acc acc' : Std.U64)
    (hshift : shift.val <= 60)
    (hrpos : 1 <= r.val)
    (hri : r.val <= i.val)
    (hrcap : r.val <= 768)
    (hacc : acc.val < 10 * 2 ^ shift.val)
    (hdigit : forall z, z ∈ a.val.take i.val -> z.val < 10)
    (hpow : 1#u64 <<< shift = ok pow)
    (hmask : pow - 1#u64 = ok mask)
    (hloop :
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
        i a shift r 0#usize acc mask = ok (a', write', acc')) :
    let xs := a.val.map (fun z => z.val)
    let F := fun ys : List Nat => ys.foldl (fun n z => 10 * n + z) 0
    let B := 2 ^ shift.val
    i.val <= 768 /\
    exists outs : List Nat,
      outs.length = i.val - r.val /\
      (forall z, z ∈ outs -> z < 10) /\
      write'.val = i.val - r.val /\
      acc'.val < 10 * B /\
      acc.val * 10 ^ (i.val - r.val) + F ((xs.take i.val).drop r.val) =
        B * (10 * F outs) + acc'.val /\
      (forall j, j < outs.length ->
        (a'.val.map (fun z : Std.U8 => z.val))[j]? = outs[j]?) /\
      (forall j, j < 768 -> write'.val <= j -> a'.val[j]? = a.val[j]?) /\
      (forall j, j < 768 -> i.val <= j -> a'.val[j]? = a.val[j]?) := by
-- BODY
  dsimp only
  let xs := a.val.map (fun z => z.val)
  let F := fun ys : List Nat => ys.foldl (fun n z => 10 * n + z) 0
  let B := 2 ^ shift.val
  have hBpos : 0 < B := by simp [B]
  have hxslen : xs.length = 768 := by simp [xs, a.property]
  have foldlFrom (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
  let Inv := fun (x : Array Std.U8 768#usize × Std.Usize × Std.Usize × Std.U64) =>
    let (ac, s, w, c) := x
    r.val <= s.val /\
    s.val <= i.val /\
    s.val <= 768 /\
    w.val = s.val - r.val /\
    c.val < 10 * B /\
    exists outs : List Nat,
      outs.length = w.val /\
      (forall z, z ∈ outs -> z < 10) /\
      acc.val * 10 ^ (s.val - r.val) + F ((xs.take s.val).drop r.val) =
        B * (10 * F outs) + c.val /\
      (forall j, j < outs.length ->
        (ac.val.map (fun z : Std.U8 => z.val))[j]? = outs[j]?) /\
      (forall j, j < 768 -> w.val <= j -> ac.val[j]? = a.val[j]?)
  let Post := fun (x : Array Std.U8 768#usize × Std.Usize × Std.U64) =>
    let (ac, w, c) := x
    i.val <= 768 /\
    exists outs : List Nat,
      outs.length = i.val - r.val /\
      (forall z, z ∈ outs -> z < 10) /\
      w.val = i.val - r.val /\
      c.val < 10 * B /\
      acc.val * 10 ^ (i.val - r.val) + F ((xs.take i.val).drop r.val) =
        B * (10 * F outs) + c.val /\
      (forall j, j < outs.length ->
        (ac.val.map (fun z : Std.U8 => z.val))[j]? = outs[j]?) /\
      (forall j, j < 768 -> w.val <= j -> ac.val[j]? = a.val[j]?) /\
      (forall j, j < 768 -> i.val <= j -> ac.val[j]? = a.val[j]?)
  let Body := fun (x : Array Std.U8 768#usize × Std.Usize × Std.Usize × Std.U64) =>
    let (ac, s, w, c) := x
    num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1.body
      i shift mask ac s w c
  have hp : Post (a', write', acc') := by
    refine ResultLoopPartial Inv Post Body ?_ (a, r, 0#usize, acc)
      (a', write', acc') ?_ ?_
    · intro x hx out hout
      rcases x with ⟨ac, s, w, c⟩
      simp only [Inv] at hx
      rcases hx with ⟨hrs, hsi, hscap, hws, hc10, outs, houtslen, houtsdigit,
        hnum, harr, houtside⟩
      simp only [Body,
        num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1.body] at hout
      by_cases hslt : s.val < i.val
      · have hsWord : s < i := by simpa [UScalar.lt_equiv] using hslt
        simp only [hsWord, if_true] at hout
        obtain ⟨q, hq, hqSpec⟩ := Aeneas.Std.WP.spec_imp_exists
          (UScalar.ShiftRight_spec c shift (by
            norm_num [U64.numBits]
            omega))
        rw [hq] at hout
        simp only [bind_tc_ok, lift] at hout
        let newDigit : Std.U8 := UScalar.cast .U8 q
        rw [show UScalar.cast .U8 q = newDigit by rfl] at hout
        let rem : Std.U64 := c &&& mask
        rw [show c &&& mask = rem by rfl] at hout
        have hbridge := DecimalRightShiftU64DyadicBridge shift c pow mask q rem
          hshift hpow hmask hq (by rfl)
        simp only [B] at hbridge
        rcases hbridge with ⟨_, _, hqVal, hremVal, heuc, hremB, _, hbound⟩
        have hq10 : q.val < 10 := by
          rw [hqVal]
          exact (Nat.div_lt_iff_lt_mul hBpos).2 (by
            simpa [Nat.mul_comm] using hc10)
        have hnewDigitVal : newDigit.val = q.val := by
          simp [newDigit, UScalar.cast_val_eq, Nat.mod_eq_of_lt (by omega : q.val < 256)]
        have hmulBound : 10 * rem.val <= U64.max := by
          have hlt := hbound 0 (by omega)
          norm_num [U64.max, U64.numBits] at hlt ⊢
          omega
        obtain ⟨tenrem, htenrem, htenremVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (U64.mul_spec (x := 10#u64) (y := rem) (by simpa using hmulBound))
        rw [htenrem] at hout
        simp only [bind_tc_ok] at hout
        have hsArray : s.val < ac.val.length := by
          by_contra hnot
          have hnone : ac.val[s.val]? = none := List.getElem?_eq_none (by omega)
          simp [Array.index_usize, Array.getElem?_Usize_eq, hnone] at hout
        have hs768 : s.val < 768 := by
          have hlen : ac.val.length = 768 := by simpa using ac.property
          omega
        obtain ⟨digit, hdigitRead, hdigitReadVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (Array.index_usize_spec ac s (by simpa [Array.length] using hsArray))
        rw [hdigitRead] at hout
        simp only [bind_tc_ok, lift] at hout
        let digit64 : Std.U64 := UScalar.cast .U64 digit
        rw [show UScalar.cast .U64 digit = digit64 by rfl] at hout
        have hwslt : w.val < s.val := by omega
        have hacOrig : ac.val[s.val]? = a.val[s.val]? :=
          houtside s.val hs768 (by omega)
        have hacGet : ac.val[s.val]? = some digit :=
          getElem?_eq_some_iff.mpr ⟨hsArray, hdigitReadVal.symm⟩
        have haGet : a.val[s.val]? = some digit := by rw [← hacOrig]; exact hacGet
        have haBound : s.val < a.val.length := by
          have hlen : a.val.length = 768 := by simpa using a.property
          omega
        have haVal : a.val[s.val]'haBound = digit := by
          have hsome := List.getElem?_eq_getElem haBound
          rw [haGet] at hsome
          exact Option.some.inj hsome.symm
        have htakeBound : s.val < (a.val.take i.val).length := by
          rw [List.length_take]
          exact lt_min hslt haBound
        have htakeVal : (a.val.take i.val)[s.val]'htakeBound = digit := by
          simpa using haVal
        have hdigitMem : digit ∈ a.val.take i.val := by
          rw [← htakeVal]
          exact List.getElem_mem _
        have hdigitLt : digit.val < 10 := hdigit digit hdigitMem
        have hdigit64 : digit64.val = digit.val := by
          simp [digit64, UScalar.cast_val_eq]
        have haddBound : tenrem.val + digit64.val <= U64.max := by
          have htenExact : tenrem.val = 10 * rem.val := by simpa using htenremVal
          have hlt := hbound digit.val hdigitLt
          rw [htenExact, hdigit64]
          norm_num [U64.max, U64.numBits] at hlt ⊢
          omega
        obtain ⟨c1, hc1, hc1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (U64.add_spec (x := tenrem) (y := digit64) haddBound)
        rw [hc1] at hout
        simp only [bind_tc_ok] at hout
        have hsBound : s.val + 1 <= Usize.max := by
          simp [Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits]
          omega
        obtain ⟨s1, hs1, hs1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (Usize.add_spec (x := s) (y := 1#usize) (by simpa using hsBound))
        rw [hs1] at hout
        simp only [bind_tc_ok] at hout
        have hwArray : w.val < ac.val.length := by rw [ac.property]; norm_num; omega
        obtain ⟨ac1, hac1, hac1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (Array.update_spec ac w newDigit (by simpa [Array.length] using hwArray))
        rw [hac1] at hout
        simp only [bind_tc_ok] at hout
        have hwBound : w.val + 1 <= Usize.max := by
          simp [Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits]
          omega
        obtain ⟨w1, hw1, hw1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (Usize.add_spec (x := w) (y := 1#usize) (by simpa using hwBound))
        rw [hw1] at hout
        simp only [bind_tc_ok] at hout
        cases hout
        simp only [Inv]
        have hs1Exact : s1.val = s.val + 1 := by simpa using hs1Val
        have hw1Exact : w1.val = w.val + 1 := by simpa using hw1Val
        have htenExact : tenrem.val = 10 * rem.val := by simpa using htenremVal
        have hc1Exact : c1.val = 10 * rem.val + digit.val := by
          rw [hc1Val, htenExact, hdigit64]
        have hc1ten : c1.val < 10 * B := by rw [hc1Exact]; omega
        let newOuts := outs ++ [q.val]
        have hnewLen : newOuts.length = w1.val := by
          simp [newOuts, houtslen, hw1Exact]
        have hnewDigit : forall z, z ∈ newOuts -> z < 10 := by
          intro z hz
          simp only [newOuts, List.mem_append, List.mem_singleton] at hz
          rcases hz with hz | rfl
          · exact houtsdigit z hz
          · exact hq10
        have hxsGet : xs[s.val]? = some digit.val := by
          simp only [xs, List.getElem?_map]
          rw [haGet]
          rfl
        have htakeStep : xs.take (s.val + 1) = xs.take s.val ++ [digit.val] := by
          rw [List.take_succ, hxsGet]
          rfl
        have hdropStep : (xs.take (s.val + 1)).drop r.val =
            (xs.take s.val).drop r.val ++ [digit.val] := by
          rw [htakeStep, List.drop_append_of_le_length]
          simp
          omega
        have hsegStep : F ((xs.take (s.val + 1)).drop r.val) =
            10 * F ((xs.take s.val).drop r.val) + digit.val := by
          rw [hdropStep]
          simp [F]
        have houtFold : F newOuts = 10 * F outs + q.val := by
          simp [newOuts, F]
        have hnumNew :
            acc.val * 10 ^ (s1.val - r.val) + F ((xs.take s1.val).drop r.val) =
              B * (10 * F newOuts) + c1.val := by
          rw [hs1Exact]
          have hsub : s.val + 1 - r.val = (s.val - r.val) + 1 := by omega
          rw [hsub, pow_succ, hsegStep]
          calc
            acc.val * (10 ^ (s.val - r.val) * 10) +
                (10 * F ((xs.take s.val).drop r.val) + digit.val) =
                10 * (acc.val * 10 ^ (s.val - r.val) +
                  F ((xs.take s.val).drop r.val)) + digit.val := by ring
            _ = 10 * (B * (10 * F outs) + c.val) + digit.val := by rw [hnum]
            _ = B * (10 * F newOuts) + c1.val := by
              rw [houtFold, hc1Exact, heuc]
              ring
        refine ⟨by omega, by omega, by omega, by omega, hc1ten, newOuts, hnewLen,
          hnewDigit, hnumNew, ?_, ?_⟩
        · intro j hj
          rw [hac1Val]
          simp only [Aeneas.Std.Array.set, List.map_set, List.getElem?_set]
          by_cases hjw : j = w.val
          · subst j
            have hw768 : w.val < 768 := by omega
            simp [newOuts, houtslen, hnewDigitVal, hw768]
          · have hjold : j < outs.length := by
              simp [newOuts] at hj
              omega
            have hwj : w.val ≠ j := Ne.symm hjw
            simp only [hwj, if_false]
            rw [List.getElem?_append_left (by simpa using hjold)]
            simpa [List.getElem?_map] using harr j hjold
        · intro j hj hw1j
          rw [hac1Val]
          simp only [Aeneas.Std.Array.set, List.getElem?_set]
          have hne : w.val ≠ j := by omega
          simp [hne]
          exact houtside j hj (by omega)
      · have hsNotWord : ¬ s < i := by simpa [UScalar.lt_equiv] using hslt
        simp only [hsNotWord, if_false] at hout
        cases hout
        simp only [Post]
        have hsiEq : s.val = i.val := by omega
        refine ⟨by omega, outs, ?_, houtsdigit, ?_, hc10, ?_, harr, houtside, ?_⟩
        · omega
        · omega
        · simpa [hsiEq] using hnum
        · intro j hj hij
          exact houtside j hj (by omega)
    · simp only [Inv]
      refine ⟨le_rfl, hri, hrcap, by simp, hacc, ?_⟩
      refine ⟨[], by simp, by simp, ?_, ?_, ?_⟩
      · have hdrop : (xs.take r.val).drop r.val = [] := by
          exact List.drop_eq_nil_of_le (by simp)
        simp [hdrop, F]
      · intro j hj
        simp at hj
      · intro j hj _
        trivial
    · exact hloop
  simpa [Post, F, B, xs] using hp

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 8 entries
Technical checking records
Local closure summary
{
  "node": "DecimalRightShiftMainLoopSemantics",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "82ced332cb1306f42586dae6fdb06029597b58f32d90abb019ad2e05b1910e23",
  "active_statement_hash": "09ab025b323f2e809fa212d94560e30755a5364d2e2fc2968204cb366862604f",
  "kernel_axioms": [
    "Classical.choice",
    "Dec2Flt.configured_x86_64_numBits",
    "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-160",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalRightShiftU64DyadicBridge": "253d7e344ffc70a6",
    "ResultLoopPartial": "566d2aeb76ed9100",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "0eb6fb2627d0ff5c"
  },
  "strict_definition_deps": {
    "ResultLoopPartial": "1b58e96ad00b23c2",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "ac5cbad6def0e047"
  }
}
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:9c94b222f6a9bc497310fa5e60458be556a1cb60124a5dbb518d821a7334aa3d",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}