DecimalRightShiftPrefixLoopSemantics

Lean checked

Statement

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

Helper · Prefix-search semantics for decimal right shift

Let d be a decimal-sequence record, put n=d.\mathit{num\_digits}, let x be the list of natural values of all 768 cells of d.\mathit{digits}, and put
I=\operatorname{fold}_{10}(x[0:n]),\qquad B=2^k.
Assume 0\le k\le60, I>0, and every cell in the stored prefix d.\mathit{digits}[0:n] is below ten. If the extracted right_shift_loop0, started on d with shift k, read index zero, zero accumulator, and false all-zero flag, returns (d',r,c,z), then
d'=d,\qquad z=\mathrm{false},\qquad r\ge1,qquad B\le c<10B. \tag{1}
Moreover exactly the applicable one of the following phase descriptions holds:
\begin{array}{ll} r\le n: & r\le768\quad\hbox{and}\quad c=\operatorname{fold}_{10}(x[0:r]);\tag{2}\\ n<r: & n\le768,\quad r\le n+k,\quad c=I10^{r-n}.\tag{3} \end{array}

Lean statement

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

import Tablet.ResultLoopPartial
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 800000

namespace dec2flt_model

-- [TABLET NODE: DecimalRightShiftPrefixLoopSemantics]
theorem DecimalRightShiftPrefixLoopSemantics
    (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (shift read : Std.Usize) (acc : Std.U64) (allZero : Bool)
    (hshift : shift.val <= 60)
    (hpositive : 0 <
      ((d.digits.val.take d.num_digits.val).map (fun z => z.val)).foldl
        (fun n z => 10 * n + z) 0)
    (hdigit : forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10)
    (hloop :
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0
        d shift 0#usize 0#u64 false = ok (d', read, acc, allZero)) :
    let xs := d.digits.val.map (fun z => z.val)
    let I := (xs.take d.num_digits.val).foldl (fun n z => 10 * n + z) 0
    let B := 2 ^ shift.val
    d' = d /\
    allZero = false /\
    1 <= read.val /\
    B <= acc.val /\
    acc.val < 10 * B /\
    ((read.val <= d.num_digits.val /\
        read.val <= 768 /\
        acc.val = (xs.take read.val).foldl (fun n z => 10 * n + z) 0) \/
      (d.num_digits.val < read.val /\
        d.num_digits.val <= 768 /\
        read.val <= d.num_digits.val + shift.val /\
        acc.val = I * 10 ^ (read.val - d.num_digits.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

Unfold both loops in num imp dec2flt decimal seq DecimalSeq right shift. We use the partial result-loop induction principle ResultLoopPartial; normal return is an explicit hypothesis, so each induction only has to preserve its invariant and analyze a finishing body result.
First analyze the nested zero-extension loop. Suppose it starts at cursor s\le768 with 0<c_0<B and c_0>0. At a state (u,C) use the invariant
s\le u\le s+k,qquad 0<C<10B,qquad C=c_0 10^{u-s}. \tag{4}
If the modeled right shift of C is nonzero, its natural value is \lfloor C/B\rfloor, so B\le C and the loop finishes. It cannot finish at u=s: there (4) gives C=c_0<B. Hence a finish satisfies s<u, as well as the other clauses of (4).
If that quotient is zero, Euclidean division by the positive B gives C<B. The body multiplies by ten and increments the cursor. The multiplication is checked and exact because
C\cdot10<B\cdot10\le10\cdot2^{60}<2^{64}. \tag{5}
To justify the cursor bound, put t=u-s. If t\ge k, then, since c_0\ge1 and 2^k\le10^k\le10^t, equation (4) would give C\ge B, contrary to the zero quotient. Thus t<k, so the incremented cursor is at most s+k; it is also at most 768+60, far below the Usize maximum. The new accumulator is 10C=c_0 10^{t+1}, is positive, and is below 10B, so (4) is preserved. Applying ResultLoopPartial proves that a normal nested-loop return has
s<u\le s+k,qquad B\le C<10B,qquad C=c_0 10^{u-s}. \tag{6}
Now apply the same principle to right_shift_loop0. At a state (u,C,\zeta) use
\zeta=\mathrm{false},qquad u\le n,qquad u\le768,qquad C=\operatorname{fold}_{10}(x[0:u]),qquad C<10B. \tag{7}
The initial state satisfies (7): the empty fold and accumulator are zero, and B>0.
The first operation in the body returns \lfloor C/B\rfloor. If it is nonzero, then B\le C<10B and the body finishes with the unchanged record, cursor, accumulator, and false flag. This cursor cannot be zero, because the empty prefix fold is zero. Thus (1) and (2) hold in this branch.
Suppose instead the quotient is zero, so C<B. The flag clause in (7) selects the false branch. If u<n, successful execution must make the fixed-array read at u succeed. Since the array has exactly 768 cells, this proves u<768. Let the read digit be a. It belongs to the assumed prefix, so a<10. The checked multiplication and addition are exact, because
10C+a<10B+10\le10\cdot2^{60}+10<2^{64}.
The checked Usize increment is safe since u<768. The new accumulator is 10C+a, which is below 10B, and the elementary fold recurrence
\operatorname{fold}_{10}(x[0:u+1]) =10\operatorname{fold}_{10}(x[0:u])+a
preserves (7) at u+1.
It remains to analyze u=n. Here (7) says C=I>0, so the branch that sets the all-zero flag true is impossible. The body calls the nested loop with s=n, c_0=I, and I<B. Reaching this point through successful fixed-array reads gives n\le768. Applying (6) gives precisely (1) and (3), while the outer body returns the original record and false flag unchanged. These cases exhaust every normally returning body result, so the outer application of ResultLoopPartial proves the claim.

Lean proof


  dsimp only
  let xs := d.digits.val.map (fun z => z.val)
  let F := fun r : Nat =>
    (xs.take r).foldl (fun n z => 10 * n + z) 0
  let I := F d.num_digits.val
  let B := 2 ^ shift.val
  have hBpos : 0 < B := by simp [B]
  have hBle : B <= 2 ^ 60 := Nat.pow_le_pow_right (by omega) hshift
  have hIpos : 0 < I := by
    simpa [I, F, xs] using hpositive
  have hlen : xs.length = 768 := by
    simp [xs, d.digits.property]
  have hshift64 : shift.val < 64 := by omega
  have shiftVal (x q : Std.U64) (hq : x >>> shift = ok q) :
      q.val = x.val / B := by
    have hs := UScalar.ShiftRight_spec x shift (by
      norm_num [U64.numBits]
      exact hshift64)
    rw [hq] at hs
    simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hs.1

  have nested (start : Std.Usize) (c0 : Std.U64)
      (hstart : start.val <= 768)
      (hc0pos : 0 < c0.val) (hc0B : c0.val < B)
      (ri : Std.Usize) (c : Std.U64)
      (hnested :
        num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0
          shift start c0 = ok (ri, c)) :
      start.val < ri.val /\
      ri.val <= start.val + shift.val /\
      B <= c.val /\
      c.val < 10 * B /\
      c.val = c0.val * 10 ^ (ri.val - start.val) := by
    let Inv := fun (x : Std.Usize × Std.U64) =>
      let (r, n) := x
      start.val <= r.val /\
      r.val <= start.val + shift.val /\
      0 < n.val /\
      n.val < 10 * B /\
      n.val = c0.val * 10 ^ (r.val - start.val)
    let Post := fun (x : Std.Usize × Std.U64) =>
      let (r, n) := x
      start.val < r.val /\
      r.val <= start.val + shift.val /\
      B <= n.val /\
      n.val < 10 * B /\
      n.val = c0.val * 10 ^ (r.val - start.val)
    let Body := fun (x : Std.Usize × Std.U64) =>
      let (r, n) := x
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0.body
        shift r n
    have hp : Post (ri, c) := by
      refine ResultLoopPartial Inv Post Body ?_ (start, c0) (ri, c) ?_ ?_
      · intro x hx out hout
        rcases x with ⟨r, n⟩
        simp only [Inv] at hx
        rcases hx with ⟨hsr, hrs, hnpos, hn10B, hnform⟩
        simp only [Body,
          num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0.body]
          at hout
        obtain ⟨q, hq, hqValSpec⟩ := Aeneas.Std.WP.spec_imp_exists
          (UScalar.ShiftRight_spec n shift (by
            norm_num [U64.numBits]
            exact hshift64))
        rw [hq] at hout
        simp only [bind_tc_ok] at hout
        have hqVal : q.val = n.val / B := by
          simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hqValSpec.1
        by_cases hq0 : q.val = 0
        · have hqEq : q = 0#u64 := UScalar.eq_of_val_eq (by simpa using hq0)
          have hnB : n.val < B := by
            rw [hqVal] at hq0
            exact (Nat.div_eq_zero_iff).mp hq0 |>.resolve_left (by omega)
          simp only [hqEq, if_true] at hout
          have htlt : r.val - start.val < shift.val := by
            by_contra hnot
            have hkle : shift.val <= r.val - start.val := by omega
            have hpowBase : 2 ^ shift.val <= 10 ^ shift.val := by
              exact Nat.pow_le_pow_left (by omega) _
            have hpowExp : 10 ^ shift.val <= 10 ^ (r.val - start.val) := by
              exact Nat.pow_le_pow_right (by omega) hkle
            have hc01 : 1 <= c0.val := by omega
            have hmul : 10 ^ (r.val - start.val) <=
                c0.val * 10 ^ (r.val - start.val) := by
              nlinarith
            rw [hnform] at hnB
            omega
          have hmulBound : n.val * 10 <= U64.max := by
            have hlt : n.val * 10 < 2 ^ 64 := by calc
              n.val * 10 < B * 10 := Nat.mul_lt_mul_of_pos_right hnB (by omega)
              _ <= 2 ^ 60 * 10 := Nat.mul_le_mul_right 10 hBle
              _ < 2 ^ 64 := by norm_num
            norm_num [U64.max, U64.numBits] at hlt ⊢
            omega
          obtain ⟨n1, hn1, hn1Val⟩ := Aeneas.Std.WP.spec_imp_exists
            (U64.mul_spec (x := n) (y := 10#u64) (by
              simpa using hmulBound))
          rw [hn1] at hout
          simp only [bind_tc_ok] at hout
          have hrBound : r.val + 1 <= Usize.max := by
            simp [Usize.max, Usize.numBits,
              Dec2Flt.configured_x86_64_numBits]
            omega
          obtain ⟨r1, hr1, hr1Val⟩ := Aeneas.Std.WP.spec_imp_exists
            (Usize.add_spec (x := r) (y := 1#usize) (by simpa using hrBound))
          rw [hr1] at hout
          simp only [bind_tc_ok] at hout
          cases hout
          simp only [Inv]
          have hr1Succ : r1.val = r.val + 1 := by simpa using hr1Val
          have hn1Exact : n1.val = n.val * 10 := by simpa using hn1Val
          refine ⟨by omega, by omega, by omega, ?_, ?_⟩
          · rw [hn1Exact]
            omega
          · rw [hn1Exact, hnform, hr1Succ]
            have hsub : r.val + 1 - start.val =
                (r.val - start.val) + 1 := by omega
            rw [hsub, pow_succ]
            ring
        · have hqne : ¬ q = 0#u64 := by
            intro heq
            apply hq0
            exact congrArg UScalar.val heq
          simp only [hqne, if_false] at hout
          cases hout
          simp only [Post]
          have hnB : B <= n.val := by
            rw [hqVal] at hq0
            exact (Nat.one_le_div_iff hBpos).mp (by omega)
          have hrpos : start.val < r.val := by
            by_contra hnot
            have hre : r.val = start.val := by omega
            rw [hre] at hnform
            simp at hnform
            rw [hnform] at hnB
            omega
          exact ⟨hrpos, hrs, hnB, hn10B, hnform⟩
      · simp only [Inv]
        refine ⟨by omega, ?_, hc0pos, ?_, ?_⟩
        · omega
        · omega
        · norm_num
      · exact hnested
    simpa [Post] using hp

  let Inv := fun (x : Std.Usize × Std.U64 × Bool) =>
    let (r, n, z) := x
    z = false /\
    r.val <= d.num_digits.val /\
    r.val <= 768 /\
    n.val = F r.val /\
    n.val < 10 * B
  let Post := fun (x :
      num.imp.dec2flt.decimal_seq.DecimalSeq × Std.Usize × Std.U64 × Bool) =>
    let (dc, r, n, z) := x
    dc = d /\
    z = false /\
    1 <= r.val /\
    B <= n.val /\
    n.val < 10 * B /\
    ((r.val <= d.num_digits.val /\
        r.val <= 768 /\
        n.val = F r.val) \/
      (d.num_digits.val < r.val /\
        d.num_digits.val <= 768 /\
        r.val <= d.num_digits.val + shift.val /\
        n.val = I * 10 ^ (r.val - d.num_digits.val)))
  let Body := fun (x : Std.Usize × Std.U64 × Bool) =>
    let (r, n, z) := x
    num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0.body
      d shift r n z
  have hp : Post (d', read, acc, allZero) := by
    refine ResultLoopPartial Inv Post Body ?_ (0#usize, 0#u64, false)
      (d', read, acc, allZero) ?_ ?_
    · intro x hx out hout
      rcases x with ⟨r, n, z⟩
      simp only [Inv] at hx
      rcases hx with ⟨hz, hrn, hr768, hnF, hn10B⟩
      subst z
      simp only [Body,
        num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0.body]
        at hout
      obtain ⟨q, hq, hqValSpec⟩ := Aeneas.Std.WP.spec_imp_exists
        (UScalar.ShiftRight_spec n shift (by
          norm_num [U64.numBits]
          exact hshift64))
      rw [hq] at hout
      simp only [bind_tc_ok] at hout
      have hqVal : q.val = n.val / B := by
        simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hqValSpec.1
      by_cases hq0 : q.val = 0
      · have hqEq : q = 0#u64 := UScalar.eq_of_val_eq (by simpa using hq0)
        have hnB : n.val < B := by
          rw [hqVal] at hq0
          exact (Nat.div_eq_zero_iff).mp hq0 |>.resolve_left (by omega)
        simp only [hqEq, if_true, Bool.false_eq_true, if_false] at hout
        by_cases hrlt : r.val < d.num_digits.val
        · have hrWord : r < d.num_digits := by
            simpa [UScalar.lt_equiv] using hrlt
          simp only [hrWord, if_true] at hout
          have hmulBound : 10 * n.val <= U64.max := by
            have hlt : 10 * n.val < 2 ^ 64 := by calc
              10 * n.val < 10 * B := Nat.mul_lt_mul_of_pos_left hnB (by omega)
              _ <= 10 * 2 ^ 60 := Nat.mul_le_mul_left 10 hBle
              _ < 2 ^ 64 := by norm_num
            norm_num [U64.max, U64.numBits] at hlt ⊢
            omega
          obtain ⟨tenn, htenn, htennVal⟩ := Aeneas.Std.WP.spec_imp_exists
            (U64.mul_spec (x := 10#u64) (y := n) (by simpa using hmulBound))
          rw [htenn] at hout
          simp only [bind_tc_ok] at hout
          have hrArray : r.val < d.digits.val.length := by
            by_contra hnot
            have hnone : d.digits.val[r.val]? = none :=
              List.getElem?_eq_none (by omega)
            simp [Array.index_usize, Array.getElem?_Usize_eq, hnone] at hout
          have harrayLen : d.digits.val.length = 768 := by
            simpa using d.digits.property
          have hr768lt : r.val < 768 := by omega
          obtain ⟨digit, hdigitRead, hdigitReadVal⟩ :=
            Aeneas.Std.WP.spec_imp_exists
              (Array.index_usize_spec d.digits r
                (by simpa [Array.length] using hrArray))
          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 hdigit64 : digit64.val = digit.val := by
            simp [digit64, UScalar.cast_val_eq]
          have htakeLen : r.val < (d.digits.val.take d.num_digits.val).length := by
            rw [List.length_take]
            exact lt_min hrlt hrArray
          have htakeVal :
              (d.digits.val.take d.num_digits.val)[r.val]'htakeLen = digit := by
            have horig : d.digits.val[r.val]'hrArray = digit := by
              exact hdigitReadVal.symm
            simpa using horig
          have hdigitMem : digit ∈ d.digits.val.take d.num_digits.val := by
            rw [← htakeVal]
            exact List.getElem_mem _
          have hdigitLt : digit.val < 10 := hdigit digit hdigitMem
          have haddBound : tenn.val + digit64.val <= U64.max := by
            have htennExact : tenn.val = 10 * n.val := by
              simpa using htennVal
            rw [htennExact, hdigit64]
            norm_num [U64.max, U64.numBits]
            omega
          obtain ⟨n1, hn1, hn1Val⟩ := Aeneas.Std.WP.spec_imp_exists
            (U64.add_spec (x := tenn) (y := digit64) haddBound)
          rw [hn1] at hout
          simp only [bind_tc_ok] at hout
          have hrBound : r.val + 1 <= Usize.max := by
            simp [Usize.max, Usize.numBits,
              Dec2Flt.configured_x86_64_numBits]
            omega
          obtain ⟨r1, hr1, hr1Val⟩ := Aeneas.Std.WP.spec_imp_exists
            (Usize.add_spec (x := r) (y := 1#usize) (by simpa using hrBound))
          rw [hr1] at hout
          simp only [bind_tc_ok] at hout
          cases hout
          simp only [Inv]
          have hr1Succ : r1.val = r.val + 1 := by simpa using hr1Val
          have htennExact : tenn.val = 10 * n.val := by simpa using htennVal
          have hn1Exact : n1.val = 10 * n.val + digit.val := by
            rw [hn1Val, htennExact, hdigit64]
          have hxsGet : xs[r.val]? = some digit.val := by
            simp only [xs, List.getElem?_map]
            have hsome : d.digits.val[r.val]? = some digit :=
              getElem?_eq_some_iff.mpr ⟨hrArray, hdigitReadVal.symm⟩
            rw [hsome]
            rfl
          have hFstep : F (r.val + 1) = 10 * F r.val + digit.val := by
            simp only [F]
            rw [List.take_succ, List.foldl_append]
            simp [hxsGet]
          refine ⟨by trivial, by omega, by omega, ?_, ?_⟩
          · rw [hr1Succ, hFstep, hn1Exact, hnF]
          · rw [hn1Exact]
            omega
        · have hrEq : r.val = d.num_digits.val := by omega
          have hrNotWord : ¬ r < d.num_digits := by
            simpa [UScalar.lt_equiv] using hrlt
          simp only [hrNotWord, if_false] at hout
          by_cases hn0 : n.val = 0
          · have hnEq : n = 0#u64 := UScalar.eq_of_val_eq (by simpa using hn0)
            simp only [hnEq, if_true] at hout
            exfalso
            apply (Nat.ne_of_gt hIpos)
            calc
              I = F r.val := by simp [I, hrEq]
              _ = n.val := hnF.symm
              _ = 0 := hn0
          · have hnNe : ¬ n = 0#u64 := by
              intro heq
              apply hn0
              exact congrArg UScalar.val heq
            simp only [hnNe, if_false] at hout
            cases hnestedCall :
                num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0
                  shift r n with
            | fail e => simp [hnestedCall] at hout
            | div => simp [hnestedCall] at hout
            | ok pair =>
              rcases pair with ⟨r1, n1⟩
              simp only [hnestedCall, bind_tc_ok] at hout
              cases hout
              have hnpos : 0 < n.val := by omega
              have hs := nested r n hr768 hnpos hnB r1 n1 hnestedCall
              rcases hs with ⟨hrr1, hr1bound, hBn1, hn1ten, hn1form⟩
              simp only [Post]
              refine ⟨by trivial, by trivial, by omega, hBn1, hn1ten, Or.inr ?_⟩
              refine ⟨by omega, ?_, ?_, ?_⟩
              · omega
              · simpa [hrEq] using hr1bound
              · simpa [I, hrEq, hnF] using hn1form
      · have hqne : ¬ q = 0#u64 := by
          intro heq
          apply hq0
          exact congrArg UScalar.val heq
        simp only [hqne, if_false] at hout
        cases hout
        simp only [Post]
        have hBn : B <= n.val := by
          rw [hqVal] at hq0
          exact (Nat.one_le_div_iff hBpos).mp (by omega)
        have hrpos : 1 <= r.val := by
          by_contra hnot
          have hr0 : r.val = 0 := by omega
          rw [hr0] at hnF
          simp [F] at hnF
          omega
        exact ⟨by trivial, by trivial, hrpos, hBn, hn10B,
          Or.inl ⟨hrn, hr768, hnF⟩⟩
    · simp [Inv, F]
      omega
    · exact hloop
  simpa [Post, I, F] using hp

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.ResultLoopPartial
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 800000

namespace dec2flt_model

-- [TABLET NODE: DecimalRightShiftPrefixLoopSemantics]
theorem DecimalRightShiftPrefixLoopSemantics
    (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (shift read : Std.Usize) (acc : Std.U64) (allZero : Bool)
    (hshift : shift.val <= 60)
    (hpositive : 0 <
      ((d.digits.val.take d.num_digits.val).map (fun z => z.val)).foldl
        (fun n z => 10 * n + z) 0)
    (hdigit : forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10)
    (hloop :
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0
        d shift 0#usize 0#u64 false = ok (d', read, acc, allZero)) :
    let xs := d.digits.val.map (fun z => z.val)
    let I := (xs.take d.num_digits.val).foldl (fun n z => 10 * n + z) 0
    let B := 2 ^ shift.val
    d' = d /\
    allZero = false /\
    1 <= read.val /\
    B <= acc.val /\
    acc.val < 10 * B /\
    ((read.val <= d.num_digits.val /\
        read.val <= 768 /\
        acc.val = (xs.take read.val).foldl (fun n z => 10 * n + z) 0) \/
      (d.num_digits.val < read.val /\
        d.num_digits.val <= 768 /\
        read.val <= d.num_digits.val + shift.val /\
        acc.val = I * 10 ^ (read.val - d.num_digits.val))) := by
-- BODY
  dsimp only
  let xs := d.digits.val.map (fun z => z.val)
  let F := fun r : Nat =>
    (xs.take r).foldl (fun n z => 10 * n + z) 0
  let I := F d.num_digits.val
  let B := 2 ^ shift.val
  have hBpos : 0 < B := by simp [B]
  have hBle : B <= 2 ^ 60 := Nat.pow_le_pow_right (by omega) hshift
  have hIpos : 0 < I := by
    simpa [I, F, xs] using hpositive
  have hlen : xs.length = 768 := by
    simp [xs, d.digits.property]
  have hshift64 : shift.val < 64 := by omega
  have shiftVal (x q : Std.U64) (hq : x >>> shift = ok q) :
      q.val = x.val / B := by
    have hs := UScalar.ShiftRight_spec x shift (by
      norm_num [U64.numBits]
      exact hshift64)
    rw [hq] at hs
    simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hs.1

  have nested (start : Std.Usize) (c0 : Std.U64)
      (hstart : start.val <= 768)
      (hc0pos : 0 < c0.val) (hc0B : c0.val < B)
      (ri : Std.Usize) (c : Std.U64)
      (hnested :
        num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0
          shift start c0 = ok (ri, c)) :
      start.val < ri.val /\
      ri.val <= start.val + shift.val /\
      B <= c.val /\
      c.val < 10 * B /\
      c.val = c0.val * 10 ^ (ri.val - start.val) := by
    let Inv := fun (x : Std.Usize × Std.U64) =>
      let (r, n) := x
      start.val <= r.val /\
      r.val <= start.val + shift.val /\
      0 < n.val /\
      n.val < 10 * B /\
      n.val = c0.val * 10 ^ (r.val - start.val)
    let Post := fun (x : Std.Usize × Std.U64) =>
      let (r, n) := x
      start.val < r.val /\
      r.val <= start.val + shift.val /\
      B <= n.val /\
      n.val < 10 * B /\
      n.val = c0.val * 10 ^ (r.val - start.val)
    let Body := fun (x : Std.Usize × Std.U64) =>
      let (r, n) := x
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0.body
        shift r n
    have hp : Post (ri, c) := by
      refine ResultLoopPartial Inv Post Body ?_ (start, c0) (ri, c) ?_ ?_
      · intro x hx out hout
        rcases x with ⟨r, n⟩
        simp only [Inv] at hx
        rcases hx with ⟨hsr, hrs, hnpos, hn10B, hnform⟩
        simp only [Body,
          num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0.body]
          at hout
        obtain ⟨q, hq, hqValSpec⟩ := Aeneas.Std.WP.spec_imp_exists
          (UScalar.ShiftRight_spec n shift (by
            norm_num [U64.numBits]
            exact hshift64))
        rw [hq] at hout
        simp only [bind_tc_ok] at hout
        have hqVal : q.val = n.val / B := by
          simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hqValSpec.1
        by_cases hq0 : q.val = 0
        · have hqEq : q = 0#u64 := UScalar.eq_of_val_eq (by simpa using hq0)
          have hnB : n.val < B := by
            rw [hqVal] at hq0
            exact (Nat.div_eq_zero_iff).mp hq0 |>.resolve_left (by omega)
          simp only [hqEq, if_true] at hout
          have htlt : r.val - start.val < shift.val := by
            by_contra hnot
            have hkle : shift.val <= r.val - start.val := by omega
            have hpowBase : 2 ^ shift.val <= 10 ^ shift.val := by
              exact Nat.pow_le_pow_left (by omega) _
            have hpowExp : 10 ^ shift.val <= 10 ^ (r.val - start.val) := by
              exact Nat.pow_le_pow_right (by omega) hkle
            have hc01 : 1 <= c0.val := by omega
            have hmul : 10 ^ (r.val - start.val) <=
                c0.val * 10 ^ (r.val - start.val) := by
              nlinarith
            rw [hnform] at hnB
            omega
          have hmulBound : n.val * 10 <= U64.max := by
            have hlt : n.val * 10 < 2 ^ 64 := by calc
              n.val * 10 < B * 10 := Nat.mul_lt_mul_of_pos_right hnB (by omega)
              _ <= 2 ^ 60 * 10 := Nat.mul_le_mul_right 10 hBle
              _ < 2 ^ 64 := by norm_num
            norm_num [U64.max, U64.numBits] at hlt ⊢
            omega
          obtain ⟨n1, hn1, hn1Val⟩ := Aeneas.Std.WP.spec_imp_exists
            (U64.mul_spec (x := n) (y := 10#u64) (by
              simpa using hmulBound))
          rw [hn1] at hout
          simp only [bind_tc_ok] at hout
          have hrBound : r.val + 1 <= Usize.max := by
            simp [Usize.max, Usize.numBits,
              Dec2Flt.configured_x86_64_numBits]
            omega
          obtain ⟨r1, hr1, hr1Val⟩ := Aeneas.Std.WP.spec_imp_exists
            (Usize.add_spec (x := r) (y := 1#usize) (by simpa using hrBound))
          rw [hr1] at hout
          simp only [bind_tc_ok] at hout
          cases hout
          simp only [Inv]
          have hr1Succ : r1.val = r.val + 1 := by simpa using hr1Val
          have hn1Exact : n1.val = n.val * 10 := by simpa using hn1Val
          refine ⟨by omega, by omega, by omega, ?_, ?_⟩
          · rw [hn1Exact]
            omega
          · rw [hn1Exact, hnform, hr1Succ]
            have hsub : r.val + 1 - start.val =
                (r.val - start.val) + 1 := by omega
            rw [hsub, pow_succ]
            ring
        · have hqne : ¬ q = 0#u64 := by
            intro heq
            apply hq0
            exact congrArg UScalar.val heq
          simp only [hqne, if_false] at hout
          cases hout
          simp only [Post]
          have hnB : B <= n.val := by
            rw [hqVal] at hq0
            exact (Nat.one_le_div_iff hBpos).mp (by omega)
          have hrpos : start.val < r.val := by
            by_contra hnot
            have hre : r.val = start.val := by omega
            rw [hre] at hnform
            simp at hnform
            rw [hnform] at hnB
            omega
          exact ⟨hrpos, hrs, hnB, hn10B, hnform⟩
      · simp only [Inv]
        refine ⟨by omega, ?_, hc0pos, ?_, ?_⟩
        · omega
        · omega
        · norm_num
      · exact hnested
    simpa [Post] using hp

  let Inv := fun (x : Std.Usize × Std.U64 × Bool) =>
    let (r, n, z) := x
    z = false /\
    r.val <= d.num_digits.val /\
    r.val <= 768 /\
    n.val = F r.val /\
    n.val < 10 * B
  let Post := fun (x :
      num.imp.dec2flt.decimal_seq.DecimalSeq × Std.Usize × Std.U64 × Bool) =>
    let (dc, r, n, z) := x
    dc = d /\
    z = false /\
    1 <= r.val /\
    B <= n.val /\
    n.val < 10 * B /\
    ((r.val <= d.num_digits.val /\
        r.val <= 768 /\
        n.val = F r.val) \/
      (d.num_digits.val < r.val /\
        d.num_digits.val <= 768 /\
        r.val <= d.num_digits.val + shift.val /\
        n.val = I * 10 ^ (r.val - d.num_digits.val)))
  let Body := fun (x : Std.Usize × Std.U64 × Bool) =>
    let (r, n, z) := x
    num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0.body
      d shift r n z
  have hp : Post (d', read, acc, allZero) := by
    refine ResultLoopPartial Inv Post Body ?_ (0#usize, 0#u64, false)
      (d', read, acc, allZero) ?_ ?_
    · intro x hx out hout
      rcases x with ⟨r, n, z⟩
      simp only [Inv] at hx
      rcases hx with ⟨hz, hrn, hr768, hnF, hn10B⟩
      subst z
      simp only [Body,
        num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0.body]
        at hout
      obtain ⟨q, hq, hqValSpec⟩ := Aeneas.Std.WP.spec_imp_exists
        (UScalar.ShiftRight_spec n shift (by
          norm_num [U64.numBits]
          exact hshift64))
      rw [hq] at hout
      simp only [bind_tc_ok] at hout
      have hqVal : q.val = n.val / B := by
        simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hqValSpec.1
      by_cases hq0 : q.val = 0
      · have hqEq : q = 0#u64 := UScalar.eq_of_val_eq (by simpa using hq0)
        have hnB : n.val < B := by
          rw [hqVal] at hq0
          exact (Nat.div_eq_zero_iff).mp hq0 |>.resolve_left (by omega)
        simp only [hqEq, if_true, Bool.false_eq_true, if_false] at hout
        by_cases hrlt : r.val < d.num_digits.val
        · have hrWord : r < d.num_digits := by
            simpa [UScalar.lt_equiv] using hrlt
          simp only [hrWord, if_true] at hout
          have hmulBound : 10 * n.val <= U64.max := by
            have hlt : 10 * n.val < 2 ^ 64 := by calc
              10 * n.val < 10 * B := Nat.mul_lt_mul_of_pos_left hnB (by omega)
              _ <= 10 * 2 ^ 60 := Nat.mul_le_mul_left 10 hBle
              _ < 2 ^ 64 := by norm_num
            norm_num [U64.max, U64.numBits] at hlt ⊢
            omega
          obtain ⟨tenn, htenn, htennVal⟩ := Aeneas.Std.WP.spec_imp_exists
            (U64.mul_spec (x := 10#u64) (y := n) (by simpa using hmulBound))
          rw [htenn] at hout
          simp only [bind_tc_ok] at hout
          have hrArray : r.val < d.digits.val.length := by
            by_contra hnot
            have hnone : d.digits.val[r.val]? = none :=
              List.getElem?_eq_none (by omega)
            simp [Array.index_usize, Array.getElem?_Usize_eq, hnone] at hout
          have harrayLen : d.digits.val.length = 768 := by
            simpa using d.digits.property
          have hr768lt : r.val < 768 := by omega
          obtain ⟨digit, hdigitRead, hdigitReadVal⟩ :=
            Aeneas.Std.WP.spec_imp_exists
              (Array.index_usize_spec d.digits r
                (by simpa [Array.length] using hrArray))
          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 hdigit64 : digit64.val = digit.val := by
            simp [digit64, UScalar.cast_val_eq]
          have htakeLen : r.val < (d.digits.val.take d.num_digits.val).length := by
            rw [List.length_take]
            exact lt_min hrlt hrArray
          have htakeVal :
              (d.digits.val.take d.num_digits.val)[r.val]'htakeLen = digit := by
            have horig : d.digits.val[r.val]'hrArray = digit := by
              exact hdigitReadVal.symm
            simpa using horig
          have hdigitMem : digit ∈ d.digits.val.take d.num_digits.val := by
            rw [← htakeVal]
            exact List.getElem_mem _
          have hdigitLt : digit.val < 10 := hdigit digit hdigitMem
          have haddBound : tenn.val + digit64.val <= U64.max := by
            have htennExact : tenn.val = 10 * n.val := by
              simpa using htennVal
            rw [htennExact, hdigit64]
            norm_num [U64.max, U64.numBits]
            omega
          obtain ⟨n1, hn1, hn1Val⟩ := Aeneas.Std.WP.spec_imp_exists
            (U64.add_spec (x := tenn) (y := digit64) haddBound)
          rw [hn1] at hout
          simp only [bind_tc_ok] at hout
          have hrBound : r.val + 1 <= Usize.max := by
            simp [Usize.max, Usize.numBits,
              Dec2Flt.configured_x86_64_numBits]
            omega
          obtain ⟨r1, hr1, hr1Val⟩ := Aeneas.Std.WP.spec_imp_exists
            (Usize.add_spec (x := r) (y := 1#usize) (by simpa using hrBound))
          rw [hr1] at hout
          simp only [bind_tc_ok] at hout
          cases hout
          simp only [Inv]
          have hr1Succ : r1.val = r.val + 1 := by simpa using hr1Val
          have htennExact : tenn.val = 10 * n.val := by simpa using htennVal
          have hn1Exact : n1.val = 10 * n.val + digit.val := by
            rw [hn1Val, htennExact, hdigit64]
          have hxsGet : xs[r.val]? = some digit.val := by
            simp only [xs, List.getElem?_map]
            have hsome : d.digits.val[r.val]? = some digit :=
              getElem?_eq_some_iff.mpr ⟨hrArray, hdigitReadVal.symm⟩
            rw [hsome]
            rfl
          have hFstep : F (r.val + 1) = 10 * F r.val + digit.val := by
            simp only [F]
            rw [List.take_succ, List.foldl_append]
            simp [hxsGet]
          refine ⟨by trivial, by omega, by omega, ?_, ?_⟩
          · rw [hr1Succ, hFstep, hn1Exact, hnF]
          · rw [hn1Exact]
            omega
        · have hrEq : r.val = d.num_digits.val := by omega
          have hrNotWord : ¬ r < d.num_digits := by
            simpa [UScalar.lt_equiv] using hrlt
          simp only [hrNotWord, if_false] at hout
          by_cases hn0 : n.val = 0
          · have hnEq : n = 0#u64 := UScalar.eq_of_val_eq (by simpa using hn0)
            simp only [hnEq, if_true] at hout
            exfalso
            apply (Nat.ne_of_gt hIpos)
            calc
              I = F r.val := by simp [I, hrEq]
              _ = n.val := hnF.symm
              _ = 0 := hn0
          · have hnNe : ¬ n = 0#u64 := by
              intro heq
              apply hn0
              exact congrArg UScalar.val heq
            simp only [hnNe, if_false] at hout
            cases hnestedCall :
                num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0
                  shift r n with
            | fail e => simp [hnestedCall] at hout
            | div => simp [hnestedCall] at hout
            | ok pair =>
              rcases pair with ⟨r1, n1⟩
              simp only [hnestedCall, bind_tc_ok] at hout
              cases hout
              have hnpos : 0 < n.val := by omega
              have hs := nested r n hr768 hnpos hnB r1 n1 hnestedCall
              rcases hs with ⟨hrr1, hr1bound, hBn1, hn1ten, hn1form⟩
              simp only [Post]
              refine ⟨by trivial, by trivial, by omega, hBn1, hn1ten, Or.inr ?_⟩
              refine ⟨by omega, ?_, ?_, ?_⟩
              · omega
              · simpa [hrEq] using hr1bound
              · simpa [I, hrEq, hnF] using hn1form
      · have hqne : ¬ q = 0#u64 := by
          intro heq
          apply hq0
          exact congrArg UScalar.val heq
        simp only [hqne, if_false] at hout
        cases hout
        simp only [Post]
        have hBn : B <= n.val := by
          rw [hqVal] at hq0
          exact (Nat.one_le_div_iff hBpos).mp (by omega)
        have hrpos : 1 <= r.val := by
          by_contra hnot
          have hr0 : r.val = 0 := by omega
          rw [hr0] at hnF
          simp [F] at hnF
          omega
        exact ⟨by trivial, by trivial, hrpos, hBn, hn10B,
          Or.inl ⟨hrn, hr768, hnF⟩⟩
    · simp [Inv, F]
      omega
    · exact hloop
  simpa [Post, I, F] 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 7 entries
Technical checking records
Local closure summary
{
  "node": "DecimalRightShiftPrefixLoopSemantics",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "7aa5803726be0a18fe87aeacebb80b02b38d61e4e8044176833d39699a12b75e",
  "active_statement_hash": "a8b5e59365838147449ccf5e4313513376e4b9d62634fc72806cf3017aad2f24",
  "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-159",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "ResultLoopPartial": "566d2aeb76ed9100",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "0eb6fb2627d0ff5c"
  },
  "strict_definition_deps": {
    "ResultLoopPartial": "1b58e96ad00b23c2",
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "c43614ff2149ecf2",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "1628b4985163d314"
  }
}
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:306bb996883d76c35dfe5e2e95646609dbd2b4528af19452eab4c07b4dcbae50",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}