DecimalRightShiftFinalLoopSemantics

Lean checked

Statement

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

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

Let a be a 768-cell U8 array, let 0\le k\le60, put B=2^k, and let an initial write cursor w\le768 and a U64 accumulator c<10B be given. Suppose the modeled U64 operations successfully compute
p=1\mathbin{\mathtt{<<}}k,\qquad m=p-1.
If the extracted right_shift_loop2, started with Boolean b, array a, shift k, cursor w, accumulator c, and mask m, returns Boolean b', array a', and cursor w', then there is a finite list Z of natural decimal digits. Writing F(L)=\operatorname{fold}_{10}(L), it satisfies
(\forall z\in Z,\ z<10),\qquad c10^{|Z|}=B\bigl(10F(Z)\bigr),\qquad w'=\min(w+|Z|,768). \tag{1}
For every j<|Z| whose destination w+j is below 768, cell w+j of a' has natural value Z_j. Every cell below the initial cursor w is unchanged, and every cell at or above the final cursor w' is unchanged. Finally,
b'=\mathrm{true}\quad\Longleftrightarrow\quad b=\mathrm{true}\ \text{or}\ \exists j<|Z|,\ 768\le w+j\ \text{and}\ Z_j\ne0. \tag{2}

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_MAX_DIGITS
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalRightShiftFinalLoopSemantics]
theorem DecimalRightShiftFinalLoopSemantics
    (a a' : Array Std.U8 768#usize) (b b' : Bool)
    (shift write write' : Std.Usize) (pow mask acc : Std.U64)
    (hshift : shift.val <= 60)
    (hwrite : write.val <= 768)
    (hacc : acc.val < 10 * 2 ^ shift.val)
    (hpow : 1#u64 <<< shift = ok pow)
    (hmask : pow - 1#u64 = ok mask)
    (hloop :
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
        b a shift write acc mask = ok (b', a', write')) :
    let F := fun ys : List Nat => ys.foldl (fun n z => 10 * n + z) 0
    let B := 2 ^ shift.val
    exists zs : List Nat,
      (forall z, z ∈ zs -> z < 10) /\
      acc.val * 10 ^ zs.length = B * (10 * F zs) /\
      write'.val = min (write.val + zs.length) 768 /\
      (forall j, j < zs.length -> write.val + j < 768 ->
        (a'.val.map (fun z : Std.U8 => z.val))[write.val + j]? = zs[j]?) /\
      (forall j, j < 768 -> j < write.val -> a'.val[j]? = a.val[j]?) /\
      (forall j, j < 768 -> write'.val <= j -> a'.val[j]? = a.val[j]?) /\
      (b' = true <-> b = true \/
        exists j, j < zs.length /\ 768 <= write.val + j /\
          zs[j]? != some 0) := by
Proof

Natural-language proof

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

Explanatory prose status

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

Proof

Unfold the final loop and its body from num imp dec2flt decimal seq DecimalSeq right shift, and apply ResultLoopPartial. Keep a,b,w,c fixed. For a current state (b_t,a_t,w_t,c_t), use an emitted list Z_t and maintain
c_t<10B,\qquad c10^{|Z_t|}=B\bigl(10F(Z_t)\bigr)+c_t,\qquad w_t=\min(w+|Z_t|,768). \tag{3}
Maintain also the three array clauses in the statement with Z_t,a_t,w_t in place of Z,a',w', and
b_t=\mathrm{true}\quad\Longleftrightarrow\quad b=\mathrm{true}\ \text{or}\ \exists j<|Z_t|,\ 768\le w+j\ \text{and}\ (Z_t)_j\ne0. \tag{4}
Initially Z_t is empty. Equation (3), the cursor equation, both unchanged-region claims, and (4) are then immediate from w\le768.
Consider a continuation, so the body test gives c_t>0. Apply DecimalRightShiftU64DyadicBridge to the successful power, mask, right-shift, and bit-and operations. If q is the quotient and e the masked remainder, the bridge gives
q=c_t/B,\quad e=c_t\bmod B,\quad c_t=qB+e,\quad e<B, \quad 10e<2^{64}. \tag{5}
Since B>0 and c_t<10B, one has q<10. Thus the U64-to-U8 cast is exact. The last bound in (5), supplied by the bridge with appended digit zero, validates the checked multiplication, and the next accumulator is c_{t+1}=10e<10B.
Append q to Z_t. The fold recurrence F(Z_t{+\!+}[q])=10F(Z_t)+q, (3), and (5) give
\begin{aligned} c10^{|Z_t|+1} &=10\left(B(10F(Z_t))+c_t\right)\\ &=B\left(10(10F(Z_t)+q)\right)+10e, \end{aligned}
which is (3) for the continuation.
By num imp dec2flt decimal seq DecimalSeq MAX DIGITS, the symbolic cap tested by the body is exactly 768. If w_t<768, (3) for the cursor implies w_t=w+|Z_t|. The body writes the exact U8 digit q there and successfully increments the cursor. Hence w_{t+1}=w_t+1=\min(w+|Z_t|+1,768); the update establishes the new final stored digit and changes neither cells below w nor cells at or above the new cursor. The new destination is retained, so it contributes nothing to the omitted-digit disjunct in (4).
If w_t\not<768, the cursor equation and w\le768 give w_t=768 and 768\le w+|Z_t|. The body leaves the array and cursor fixed, and changes the Boolean to true exactly when q>0. Thus the new last digit is outside the array precisely at a destination at least 768, and the two Boolean branches give exactly (4) for Z_t{+\!+}[q]. All array clauses are unchanged in this case. This proves preservation for every normal continuation, including every cast, multiplication, update, and cursor side condition.
At a normal finish the body test gives c_t=0. Equation (3) therefore becomes the terminal identity in (1), and all other invariant clauses are already the claimed conclusions. The supplied successful loop result is a finite execution; equivalently, its quotient/remainder recurrence also has an explicit arithmetic bound: after one step the residual is 10e, and after at most k further appended zeroes its residual is (e10^k)\bmod 2^k=0, since 2^k\mid10^k. Thus no infinite residual case is hidden in the successful call. The conclusion follows from ResultLoopPartial.

Lean proof


  dsimp only
  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]
  let Inv := fun (x : Bool × Array Std.U8 768#usize × Std.Usize × Std.U64) =>
    let (bc, ac, w, c) := x
    c.val < 10 * B /\
    exists zs : List Nat,
      (forall z, z ∈ zs -> z < 10) /\
      acc.val * 10 ^ zs.length = B * (10 * F zs) + c.val /\
      w.val = min (write.val + zs.length) 768 /\
      (forall j, j < zs.length -> write.val + j < 768 ->
        (ac.val.map (fun z : Std.U8 => z.val))[write.val + j]? = zs[j]?) /\
      (forall j, j < 768 -> j < write.val -> ac.val[j]? = a.val[j]?) /\
      (forall j, j < 768 -> w.val <= j -> ac.val[j]? = a.val[j]?) /\
      (bc = true <-> b = true \/
        exists j, j < zs.length /\ 768 <= write.val + j /\
          zs[j]? != some 0)
  let Post := fun (x : Bool × Array Std.U8 768#usize × Std.Usize) =>
    let (bc, ac, w) := x
    exists zs : List Nat,
      (forall z, z ∈ zs -> z < 10) /\
      acc.val * 10 ^ zs.length = B * (10 * F zs) /\
      w.val = min (write.val + zs.length) 768 /\
      (forall j, j < zs.length -> write.val + j < 768 ->
        (ac.val.map (fun z : Std.U8 => z.val))[write.val + j]? = zs[j]?) /\
      (forall j, j < 768 -> j < write.val -> ac.val[j]? = a.val[j]?) /\
      (forall j, j < 768 -> w.val <= j -> ac.val[j]? = a.val[j]?) /\
      (bc = true <-> b = true \/
        exists j, j < zs.length /\ 768 <= write.val + j /\
          zs[j]? != some 0)
  let Body := fun (x : Bool × Array Std.U8 768#usize × Std.Usize × Std.U64) =>
    let (bc, ac, w, c) := x
    num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2.body
      shift mask bc ac w c
  have hp : Post (b', a', write') := by
    refine ResultLoopPartial Inv Post Body ?_ (b, a, write, acc)
      (b', a', write') ?_ ?_
    · intro x hx out hout
      rcases x with ⟨bc, ac, w, c⟩
      simp only [Inv] at hx
      rcases hx with ⟨hc10, zs, hzsdigit, hnum, hw, harr, hbelow, houtside,
        hsticky⟩
      simp only [Body,
        num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2.body] at hout
      by_cases hc0 : c.val = 0
      · have hcEq : c = 0#u64 := UScalar.eq_of_val_eq (by simpa using hc0)
        have hnotgt : ¬ c > 0#u64 := by simp [hcEq]
        simp [hnotgt] at hout
        subst out
        simp only [Post]
        refine ⟨zs, hzsdigit, ?_, hw, harr, hbelow, houtside, hsticky⟩
        simpa [hc0] using hnum
      · have hcpos : 0 < c.val := by omega
        have hgt : c > 0#u64 := by
          change 0 < c.val
          exact hcpos
        simp only [hgt, 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 ⟨c1, hc1, hc1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (U64.mul_spec (x := 10#u64) (y := rem) (by simpa using hmulBound))
        rw [hc1] at hout
        simp only [bind_tc_ok] at hout
        have hc1Exact : c1.val = 10 * rem.val := by simpa using hc1Val
        have hc1ten : c1.val < 10 * B := by rw [hc1Exact]; omega
        let newZs := zs ++ [q.val]
        have hnewLen : newZs.length = zs.length + 1 := by simp [newZs]
        have hnewDigit : forall z, z ∈ newZs -> z < 10 := by
          intro z hz
          simp only [newZs, List.mem_append, List.mem_singleton] at hz
          rcases hz with hz | rfl
          · exact hzsdigit z hz
          · exact hq10
        have hnewFold : F newZs = 10 * F zs + q.val := by
          simp [newZs, F]
        have hnumNew :
            acc.val * 10 ^ newZs.length =
              B * (10 * F newZs) + c1.val := by
          rw [hnewLen, pow_succ, hnewFold, hc1Exact]
          calc
            acc.val * (10 ^ zs.length * 10) =
                (acc.val * 10 ^ zs.length) * 10 := by ring
            _ = (B * (10 * F zs) + c.val) * 10 := by rw [hnum]
            _ = B * (10 * (10 * F zs + q.val)) + 10 * rem.val := by
              rw [heuc]
              ring
        by_cases hcap : w.val < 768
        · have hcapWord : w <
              num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
            simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
              UScalar.lt_equiv] using hcap
          simp only [hcapWord, if_true] at hout
          have hwArray : w.val < ac.val.length := by
            rw [ac.property]
            exact hcap
          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 hwExact : w.val = write.val + zs.length := by
            rw [hw] at hcap
            rw [Nat.min_eq_left (by omega : write.val + zs.length <= 768)] at hw
            exact hw
          have hw1Exact : w1.val = w.val + 1 := by simpa using hw1Val
          have hcursor : w1.val = min (write.val + newZs.length) 768 := by
            rw [hw1Exact, hwExact, hnewLen]
            rw [Nat.min_eq_left (by omega)]
            omega
          refine ⟨hc1ten, newZs, hnewDigit, hnumNew, hcursor, ?_, ?_, ?_, ?_⟩
          · intro j hj hjphys
            rw [hac1Val]
            simp only [Aeneas.Std.Array.set, List.map_set, List.getElem?_set]
            by_cases hjold : j < zs.length
            · have hne : w.val ≠ write.val + j := by omega
              simp only [hne, if_false]
              rw [List.getElem?_append_left (by simpa using hjold)]
              exact harr j hjold hjphys
            · have hjlast : j = zs.length := by
                simp [newZs] at hj
                omega
              subst j
              simpa [hwExact, newZs, hnewDigitVal] using hjphys
          · intro j hj hjwrite
            rw [hac1Val]
            simp only [Aeneas.Std.Array.set, List.getElem?_set]
            have hne : w.val ≠ j := by omega
            simp [hne]
            exact hbelow j hj hjwrite
          · 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)
          · constructor
            · intro htrue
              rcases hsticky.mp htrue with hbtrue | ⟨j, hj, hjcap, hjnz⟩
              · exact Or.inl hbtrue
              · right
                refine ⟨j, by simpa [newZs] using Nat.lt_succ_of_lt hj, hjcap, ?_⟩
                rw [List.getElem?_append_left (by simpa using hj)]
                exact hjnz
            · intro h
              apply hsticky.mpr
              rcases h with hbtrue | ⟨j, hj, hjcap, hjnz⟩
              · exact Or.inl hbtrue
              · right
                have hjold : j < zs.length := by
                  by_contra hn
                  have : j = zs.length := by simp [newZs] at hj; omega
                  subst j
                  omega
                refine ⟨j, hjold, hjcap, ?_⟩
                rw [List.getElem?_append_left (by simpa using hjold)] at hjnz
                exact hjnz
        · have hcapWord : ¬ w <
              num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
            simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
              UScalar.lt_equiv] using hcap
          simp only [hcapWord, if_false] at hout
          have hwCap : w.val = 768 := by
            rw [hw]
            exact Nat.min_eq_right (by omega)
          have hdestCap : 768 <= write.val + zs.length := by
            rw [hw] at hwCap
            omega
          have hcursor : w.val = min (write.val + newZs.length) 768 := by
            rw [hwCap, Nat.min_eq_right]
            rw [hnewLen]
            omega
          by_cases hq0 : q.val = 0
          · have hnewDigitEq : newDigit = 0#u8 :=
              UScalar.eq_of_val_eq (by simpa [hnewDigitVal] using hq0)
            have hnotgt : ¬ newDigit > 0#u8 := by simp [hnewDigitEq]
            simp [hnotgt] at hout
            subst out
            simp only [Inv]
            refine ⟨hc1ten, newZs, hnewDigit, hnumNew, hcursor, ?_, hbelow,
              ?_, ?_⟩
            · intro j hj hjphys
              have hjold : j < zs.length := by
                by_contra hn
                have : j = zs.length := by simp [newZs] at hj; omega
                subst j
                omega
              rw [List.getElem?_append_left (by simpa using hjold)]
              exact harr j hjold hjphys
            · intro j hj hwj
              exact houtside j hj hwj
            · constructor
              · intro htrue
                rcases hsticky.mp htrue with hbtrue | ⟨j, hj, hjcap, hjnz⟩
                · exact Or.inl hbtrue
                · right
                  refine ⟨j, by simpa [newZs] using Nat.lt_succ_of_lt hj, hjcap, ?_⟩
                  rw [List.getElem?_append_left (by simpa using hj)]
                  exact hjnz
              · intro h
                apply hsticky.mpr
                rcases h with hbtrue | ⟨j, hj, hjcap, hjnz⟩
                · exact Or.inl hbtrue
                · right
                  have hjold : j < zs.length := by
                    by_contra hn
                    have : j = zs.length := by simp [newZs] at hj; omega
                    subst j
                    simp [newZs, hq0] at hjnz
                  refine ⟨j, hjold, hjcap, ?_⟩
                  rw [List.getElem?_append_left (by simpa using hjold)] at hjnz
                  exact hjnz
          · have hgtDigit : newDigit > 0#u8 := by
              change 0 < newDigit.val
              rw [hnewDigitVal]
              omega
            simp [hgtDigit] at hout
            subst out
            simp only [Inv]
            refine ⟨hc1ten, newZs, hnewDigit, hnumNew, hcursor, ?_, hbelow,
              ?_, ?_⟩
            · intro j hj hjphys
              have hjold : j < zs.length := by
                by_contra hn
                have : j = zs.length := by simp [newZs] at hj; omega
                subst j
                omega
              rw [List.getElem?_append_left (by simpa using hjold)]
              exact harr j hjold hjphys
            · intro j hj hwj
              exact houtside j hj hwj
            · constructor
              · intro _
                right
                refine ⟨zs.length, by simp [newZs], hdestCap, ?_⟩
                simp [newZs, hq0]
              · intro _
                trivial
    · simp only [Inv]
      refine ⟨hacc, [], ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
      · simp
      · simp [F]
      · simp [Nat.min_eq_left hwrite]
      · intro j hj
        simp at hj
      · intro j hj hjwrite
        trivial
      · intro j hj hwj
        trivial
      · simp
    · exact hloop
  simpa [Post, F, B] 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_MAX_DIGITS
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalRightShiftFinalLoopSemantics]
theorem DecimalRightShiftFinalLoopSemantics
    (a a' : Array Std.U8 768#usize) (b b' : Bool)
    (shift write write' : Std.Usize) (pow mask acc : Std.U64)
    (hshift : shift.val <= 60)
    (hwrite : write.val <= 768)
    (hacc : acc.val < 10 * 2 ^ shift.val)
    (hpow : 1#u64 <<< shift = ok pow)
    (hmask : pow - 1#u64 = ok mask)
    (hloop :
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
        b a shift write acc mask = ok (b', a', write')) :
    let F := fun ys : List Nat => ys.foldl (fun n z => 10 * n + z) 0
    let B := 2 ^ shift.val
    exists zs : List Nat,
      (forall z, z ∈ zs -> z < 10) /\
      acc.val * 10 ^ zs.length = B * (10 * F zs) /\
      write'.val = min (write.val + zs.length) 768 /\
      (forall j, j < zs.length -> write.val + j < 768 ->
        (a'.val.map (fun z : Std.U8 => z.val))[write.val + j]? = zs[j]?) /\
      (forall j, j < 768 -> j < write.val -> a'.val[j]? = a.val[j]?) /\
      (forall j, j < 768 -> write'.val <= j -> a'.val[j]? = a.val[j]?) /\
      (b' = true <-> b = true \/
        exists j, j < zs.length /\ 768 <= write.val + j /\
          zs[j]? != some 0) := by
-- BODY
  dsimp only
  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]
  let Inv := fun (x : Bool × Array Std.U8 768#usize × Std.Usize × Std.U64) =>
    let (bc, ac, w, c) := x
    c.val < 10 * B /\
    exists zs : List Nat,
      (forall z, z ∈ zs -> z < 10) /\
      acc.val * 10 ^ zs.length = B * (10 * F zs) + c.val /\
      w.val = min (write.val + zs.length) 768 /\
      (forall j, j < zs.length -> write.val + j < 768 ->
        (ac.val.map (fun z : Std.U8 => z.val))[write.val + j]? = zs[j]?) /\
      (forall j, j < 768 -> j < write.val -> ac.val[j]? = a.val[j]?) /\
      (forall j, j < 768 -> w.val <= j -> ac.val[j]? = a.val[j]?) /\
      (bc = true <-> b = true \/
        exists j, j < zs.length /\ 768 <= write.val + j /\
          zs[j]? != some 0)
  let Post := fun (x : Bool × Array Std.U8 768#usize × Std.Usize) =>
    let (bc, ac, w) := x
    exists zs : List Nat,
      (forall z, z ∈ zs -> z < 10) /\
      acc.val * 10 ^ zs.length = B * (10 * F zs) /\
      w.val = min (write.val + zs.length) 768 /\
      (forall j, j < zs.length -> write.val + j < 768 ->
        (ac.val.map (fun z : Std.U8 => z.val))[write.val + j]? = zs[j]?) /\
      (forall j, j < 768 -> j < write.val -> ac.val[j]? = a.val[j]?) /\
      (forall j, j < 768 -> w.val <= j -> ac.val[j]? = a.val[j]?) /\
      (bc = true <-> b = true \/
        exists j, j < zs.length /\ 768 <= write.val + j /\
          zs[j]? != some 0)
  let Body := fun (x : Bool × Array Std.U8 768#usize × Std.Usize × Std.U64) =>
    let (bc, ac, w, c) := x
    num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2.body
      shift mask bc ac w c
  have hp : Post (b', a', write') := by
    refine ResultLoopPartial Inv Post Body ?_ (b, a, write, acc)
      (b', a', write') ?_ ?_
    · intro x hx out hout
      rcases x with ⟨bc, ac, w, c⟩
      simp only [Inv] at hx
      rcases hx with ⟨hc10, zs, hzsdigit, hnum, hw, harr, hbelow, houtside,
        hsticky⟩
      simp only [Body,
        num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2.body] at hout
      by_cases hc0 : c.val = 0
      · have hcEq : c = 0#u64 := UScalar.eq_of_val_eq (by simpa using hc0)
        have hnotgt : ¬ c > 0#u64 := by simp [hcEq]
        simp [hnotgt] at hout
        subst out
        simp only [Post]
        refine ⟨zs, hzsdigit, ?_, hw, harr, hbelow, houtside, hsticky⟩
        simpa [hc0] using hnum
      · have hcpos : 0 < c.val := by omega
        have hgt : c > 0#u64 := by
          change 0 < c.val
          exact hcpos
        simp only [hgt, 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 ⟨c1, hc1, hc1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (U64.mul_spec (x := 10#u64) (y := rem) (by simpa using hmulBound))
        rw [hc1] at hout
        simp only [bind_tc_ok] at hout
        have hc1Exact : c1.val = 10 * rem.val := by simpa using hc1Val
        have hc1ten : c1.val < 10 * B := by rw [hc1Exact]; omega
        let newZs := zs ++ [q.val]
        have hnewLen : newZs.length = zs.length + 1 := by simp [newZs]
        have hnewDigit : forall z, z ∈ newZs -> z < 10 := by
          intro z hz
          simp only [newZs, List.mem_append, List.mem_singleton] at hz
          rcases hz with hz | rfl
          · exact hzsdigit z hz
          · exact hq10
        have hnewFold : F newZs = 10 * F zs + q.val := by
          simp [newZs, F]
        have hnumNew :
            acc.val * 10 ^ newZs.length =
              B * (10 * F newZs) + c1.val := by
          rw [hnewLen, pow_succ, hnewFold, hc1Exact]
          calc
            acc.val * (10 ^ zs.length * 10) =
                (acc.val * 10 ^ zs.length) * 10 := by ring
            _ = (B * (10 * F zs) + c.val) * 10 := by rw [hnum]
            _ = B * (10 * (10 * F zs + q.val)) + 10 * rem.val := by
              rw [heuc]
              ring
        by_cases hcap : w.val < 768
        · have hcapWord : w <
              num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
            simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
              UScalar.lt_equiv] using hcap
          simp only [hcapWord, if_true] at hout
          have hwArray : w.val < ac.val.length := by
            rw [ac.property]
            exact hcap
          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 hwExact : w.val = write.val + zs.length := by
            rw [hw] at hcap
            rw [Nat.min_eq_left (by omega : write.val + zs.length <= 768)] at hw
            exact hw
          have hw1Exact : w1.val = w.val + 1 := by simpa using hw1Val
          have hcursor : w1.val = min (write.val + newZs.length) 768 := by
            rw [hw1Exact, hwExact, hnewLen]
            rw [Nat.min_eq_left (by omega)]
            omega
          refine ⟨hc1ten, newZs, hnewDigit, hnumNew, hcursor, ?_, ?_, ?_, ?_⟩
          · intro j hj hjphys
            rw [hac1Val]
            simp only [Aeneas.Std.Array.set, List.map_set, List.getElem?_set]
            by_cases hjold : j < zs.length
            · have hne : w.val ≠ write.val + j := by omega
              simp only [hne, if_false]
              rw [List.getElem?_append_left (by simpa using hjold)]
              exact harr j hjold hjphys
            · have hjlast : j = zs.length := by
                simp [newZs] at hj
                omega
              subst j
              simpa [hwExact, newZs, hnewDigitVal] using hjphys
          · intro j hj hjwrite
            rw [hac1Val]
            simp only [Aeneas.Std.Array.set, List.getElem?_set]
            have hne : w.val ≠ j := by omega
            simp [hne]
            exact hbelow j hj hjwrite
          · 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)
          · constructor
            · intro htrue
              rcases hsticky.mp htrue with hbtrue | ⟨j, hj, hjcap, hjnz⟩
              · exact Or.inl hbtrue
              · right
                refine ⟨j, by simpa [newZs] using Nat.lt_succ_of_lt hj, hjcap, ?_⟩
                rw [List.getElem?_append_left (by simpa using hj)]
                exact hjnz
            · intro h
              apply hsticky.mpr
              rcases h with hbtrue | ⟨j, hj, hjcap, hjnz⟩
              · exact Or.inl hbtrue
              · right
                have hjold : j < zs.length := by
                  by_contra hn
                  have : j = zs.length := by simp [newZs] at hj; omega
                  subst j
                  omega
                refine ⟨j, hjold, hjcap, ?_⟩
                rw [List.getElem?_append_left (by simpa using hjold)] at hjnz
                exact hjnz
        · have hcapWord : ¬ w <
              num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
            simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
              UScalar.lt_equiv] using hcap
          simp only [hcapWord, if_false] at hout
          have hwCap : w.val = 768 := by
            rw [hw]
            exact Nat.min_eq_right (by omega)
          have hdestCap : 768 <= write.val + zs.length := by
            rw [hw] at hwCap
            omega
          have hcursor : w.val = min (write.val + newZs.length) 768 := by
            rw [hwCap, Nat.min_eq_right]
            rw [hnewLen]
            omega
          by_cases hq0 : q.val = 0
          · have hnewDigitEq : newDigit = 0#u8 :=
              UScalar.eq_of_val_eq (by simpa [hnewDigitVal] using hq0)
            have hnotgt : ¬ newDigit > 0#u8 := by simp [hnewDigitEq]
            simp [hnotgt] at hout
            subst out
            simp only [Inv]
            refine ⟨hc1ten, newZs, hnewDigit, hnumNew, hcursor, ?_, hbelow,
              ?_, ?_⟩
            · intro j hj hjphys
              have hjold : j < zs.length := by
                by_contra hn
                have : j = zs.length := by simp [newZs] at hj; omega
                subst j
                omega
              rw [List.getElem?_append_left (by simpa using hjold)]
              exact harr j hjold hjphys
            · intro j hj hwj
              exact houtside j hj hwj
            · constructor
              · intro htrue
                rcases hsticky.mp htrue with hbtrue | ⟨j, hj, hjcap, hjnz⟩
                · exact Or.inl hbtrue
                · right
                  refine ⟨j, by simpa [newZs] using Nat.lt_succ_of_lt hj, hjcap, ?_⟩
                  rw [List.getElem?_append_left (by simpa using hj)]
                  exact hjnz
              · intro h
                apply hsticky.mpr
                rcases h with hbtrue | ⟨j, hj, hjcap, hjnz⟩
                · exact Or.inl hbtrue
                · right
                  have hjold : j < zs.length := by
                    by_contra hn
                    have : j = zs.length := by simp [newZs] at hj; omega
                    subst j
                    simp [newZs, hq0] at hjnz
                  refine ⟨j, hjold, hjcap, ?_⟩
                  rw [List.getElem?_append_left (by simpa using hjold)] at hjnz
                  exact hjnz
          · have hgtDigit : newDigit > 0#u8 := by
              change 0 < newDigit.val
              rw [hnewDigitVal]
              omega
            simp [hgtDigit] at hout
            subst out
            simp only [Inv]
            refine ⟨hc1ten, newZs, hnewDigit, hnumNew, hcursor, ?_, hbelow,
              ?_, ?_⟩
            · intro j hj hjphys
              have hjold : j < zs.length := by
                by_contra hn
                have : j = zs.length := by simp [newZs] at hj; omega
                subst j
                omega
              rw [List.getElem?_append_left (by simpa using hjold)]
              exact harr j hjold hjphys
            · intro j hj hwj
              exact houtside j hj hwj
            · constructor
              · intro _
                right
                refine ⟨zs.length, by simp [newZs], hdestCap, ?_⟩
                simp [newZs, hq0]
              · intro _
                trivial
    · simp only [Inv]
      refine ⟨hacc, [], ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
      · simp
      · simp [F]
      · simp [Nat.min_eq_left hwrite]
      · intro j hj
        simp at hj
      · intro j hj hjwrite
        trivial
      · intro j hj hwj
        trivial
      · simp
    · exact hloop
  simpa [Post, F, B] 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": "DecimalRightShiftFinalLoopSemantics",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "5365b01d8facef4e1d1315b6cec7be11c2dc983cef4acdecbc2b31d93026c07c",
  "active_statement_hash": "5f52a75102c14cf4603d5ae3fcef00a05523e78b48ea785b997bb6a0882d7444",
  "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-161",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalRightShiftU64DyadicBridge": "253d7e344ffc70a6",
    "ResultLoopPartial": "566d2aeb76ed9100",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "0eb6fb2627d0ff5c",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "0f3e5cc6a65d812e"
  },
  "strict_definition_deps": {
    "ResultLoopPartial": "1b58e96ad00b23c2",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "39f6cb231ef80388",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "caa3dd29328f3731"
  }
}
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:251ca0468efe2b54613a0c2e9feeeef225aa93540933a6395a11f9e17007c45c",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}