DecimalLeftShiftSecondLoopSemantics

Lean checked

Statement

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

Lemma · Exact second carry-loop semantics

Let a be a 768-cell byte array, let b be a Boolean, let q be a usize write cursor, and let c be a u64 carry satisfying c<10^q. Suppose that, whenever j<q, j<768, and c<10^{q-j-1}, cell a[j] is zero. If the extracted left_shift_loop1 call on (b,a,q,c) returns (b',a'), then there is a length-q list u of decimal digits such that
c=\operatorname{fold}_{10}(u).
For every j<q with j<768, cell a'[j] equals u_j; every physical cell at or beyond q is unchanged; and
b'=\mathrm{true}\quad\Longleftrightarrow\quad b=\mathrm{true}\ \text{or}\ \bigl(\exists j<q,\ 768\le j\ \text{and}\ u_j\ne0\bigr).
Thus the conclusion includes the leading-zero-padding consequence: a carry having fewer than q decimal digits is represented by zeroes in the untouched positions before the loop's final cursor.

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_left_shift

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalLeftShiftSecondLoopSemantics]
theorem DecimalLeftShiftSecondLoopSemantics
    (a a' : Array Std.U8 768#usize) (b b' : Bool)
    (q : Std.Usize) (carry : Std.U64)
    (hcarry : carry.val < 10 ^ q.val)
    (hzero : forall j, j < q.val -> j < 768 ->
      carry.val < 10 ^ (q.val - j - 1) -> a.val[j]? = some 0#u8)
    (hloop :
      num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
        b a q carry = ok (b', a')) :
    exists outDigits : List Nat,
      outDigits.length = q.val /\
      (forall z, z ∈ outDigits -> z < 10) /\
      carry.val = outDigits.foldl (fun acc z => 10 * acc + z) 0 /\
      (forall j, j < outDigits.length -> j < 768 ->
        (a'.val.map (fun z => z.val))[j]? = outDigits[j]?) /\
      (forall j, j < 768 -> q.val <= j -> a'.val[j]? = a.val[j]?) /\
      (b' = true <-> b = true \/
        exists j, j < outDigits.length /\ 768 <= j /\
          outDigits[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

Use ResultLoopPartial for the exact extracted body in num imp dec2flt decimal seq DecimalSeq left shift. At an intermediate state (b_s,a_s,w,c_s), let v be the list of remainders already produced, in most-significant-to-least-significant order. Maintain
w\le q,\qquad c_s<10^w,\qquad |v|=q-w, \qquad c=c_s10^{|v|}+\operatorname{fold}_{10}(v). \tag{1}
Every member of v is below ten. Maintain also that a_s[w+j]=v_j whenever that destination is physical, that cells outside [w,q) are unchanged, and that b_s is b OR the nonzeroness of exactly those members v_j whose destination w+j is at least 768. These clauses hold initially with w=q, c_s=c, and v empty.
Consider a continuation step. It has c_s>0. From c_s<10^w it follows that w>0, so the extracted usize subtraction w-1 is legal. Put
t=\lfloor c_s/10\rfloor, \qquad r=c_s-10t=c_s\bmod10.
The extracted u64 division is legal because its divisor is ten. The standard division bound 10t\le c_s<2^{64} discharges the checked u64 multiplication, and it also discharges the following checked subtraction. Euclidean division gives c_s=10t+r and r<10; hence the u8 cast of r is exact. Moreover, if w_1=w-1, then t<10^{w_1}, directly from c_s<10^w and 10^w=10\cdot10^{w_1}. Prepending r to v therefore preserves every arithmetic clause of (1), including the exact fold equation.
If w_1<768, the extracted cap test takes its write branch. The fixed array has length 768, so the update is in bounds; its update equation puts r at the new first destination and preserves every other cell. The array and Boolean invariants follow. If w_1\ge768, the body performs no array access. Its two remainder branches set the Boolean precisely when r is nonzero, which is exactly the newly omitted-digit disjunct. Thus both cap branches preserve the invariant. Division also strictly reduces a positive carry, so the partial-loop principle reaches the body's done branch; equivalently, the supplied normal loop result receives the stated postcondition.
At done, c_s=0. Equation (1) says c=\operatorname{fold}_{10}(v) and |v|=q-w. Prefix v by w zeroes to obtain u. Decimal-digit induction gives \operatorname{fold}_{10}(v)<10^{|v|}. Hence for each j<w,
c<10^{|v|}\le10^{q-j-1}.
The hypothesis says that the original physical cell a[j] is zero, and the unchanged-cell invariant says that a_s[j] is the same zero. These are exactly the padded leading entries of u. The suffix entries follow from the written-cell invariant. Prefixing zeroes creates no omitted nonzero digit, so reindexing the maintained Boolean equivalence yields the displayed equivalence for u. The unchanged suffix clause is already an invariant clause, completing the proof.

Lean proof


  let F := fun ys : List Nat =>
    ys.foldl (fun acc z => 10 * acc + z) 0
  have foldlFrom (ys : List Nat) (acc : Nat) :
      ys.foldl (fun n z => 10 * n + z) acc =
        acc * 10 ^ ys.length + F ys := by
    induction ys generalizing acc with
    | nil => simp [F]
    | cons y ys ih =>
      simp only [List.foldl_cons, List.length_cons]
      rw [ih, pow_succ]
      change (10 * acc + y) * 10 ^ ys.length + F ys =
        acc * (10 ^ ys.length * 10) +
          (y :: ys).foldl (fun n z => 10 * n + z) 0
      simp only [List.foldl_cons]
      rw [ih]
      ring
  have foldBound (ys : List Nat) (hd : forall z, z ∈ ys -> z < 10) :
      F ys < 10 ^ ys.length := by
    induction ys with
    | nil => simp [F]
    | cons y ys ih =>
      have hy : y < 10 := hd y (by simp)
      have hys : forall z, z ∈ ys -> z < 10 := by
        intro z hz
        exact hd z (by simp [hz])
      have hi := ih hys
      rw [show F (y :: ys) = y * 10 ^ ys.length + F ys by
        simpa [F] using foldlFrom ys y, List.length_cons, pow_succ]
      have hmul : y * 10 ^ ys.length <= 9 * 10 ^ ys.length :=
        Nat.mul_le_mul_right _ (by omega)
      calc
        y * 10 ^ ys.length + F ys <
            y * 10 ^ ys.length + 10 ^ ys.length := Nat.add_lt_add_left hi _
        _ <= 9 * 10 ^ ys.length + 10 ^ ys.length :=
          Nat.add_le_add_right hmul _
        _ = 10 ^ ys.length * 10 := by ring
  let Inv := fun (x : Bool × Array Std.U8 768#usize × Std.Usize × Std.U64) =>
    let (bc, ac, wi, c) := x
    wi.val <= q.val /\
    c.val < 10 ^ wi.val /\
    exists outs : List Nat,
      outs.length = q.val - wi.val /\
      (forall z, z ∈ outs -> z < 10) /\
      carry.val = c.val * 10 ^ outs.length + F outs /\
      (forall j, j < outs.length -> wi.val + j < 768 ->
        (ac.val.map (fun z : Std.U8 => z.val))[wi.val + j]? = outs[j]?) /\
      (forall j, j < 768 -> (j < wi.val \/ q.val <= j) ->
        ac.val[j]? = a.val[j]?) /\
      (bc = true <-> b = true \/
        exists j, j < outs.length /\ 768 <= wi.val + j /\
          outs[j]? != some 0)
  let Post := fun (x : Bool × Array Std.U8 768#usize) =>
    let (bc, ac) := x
    exists outDigits : List Nat,
      outDigits.length = q.val /\
      (forall z, z ∈ outDigits -> z < 10) /\
      carry.val = F outDigits /\
      (forall j, j < outDigits.length -> j < 768 ->
        (ac.val.map (fun z : Std.U8 => z.val))[j]? = outDigits[j]?) /\
      (forall j, j < 768 -> q.val <= j -> ac.val[j]? = a.val[j]?) /\
      (bc = true <-> b = true \/
        exists j, j < outDigits.length /\ 768 <= j /\
          outDigits[j]? != some 0)
  let Body := fun (x : Bool × Array Std.U8 768#usize × Std.Usize × Std.U64) =>
    let (bc, ac, wi, c) := x
    num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body bc ac wi c
  have hp : Post (b', a') := by
    refine ResultLoopPartial Inv Post Body ?_ (b, a, q, carry) (b', a') ?_ ?_
    · intro x hx r hr
      rcases x with ⟨bc, ac, wi, c⟩
      simp only [Inv] at hx
      rcases hx with ⟨hwiq, hcfit, outs, houtslen, houtsdigit, hnum,
        harr, houtside, hsticky⟩
      simp only [Body,
        num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body] at hr
      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 hr
        subst r
        simp only [Post]
        have hcarryOut : carry.val = F outs := by simpa [hc0] using hnum
        have houtsBound : carry.val < 10 ^ outs.length := by
          rw [hcarryOut]
          exact foldBound outs houtsdigit
        let zeros := List.replicate wi.val 0
        let final := zeros ++ outs
        refine ⟨final, ?_, ?_, ?_, ?_, ?_, ?_⟩
        · simp [final, zeros, houtslen]
          omega
        · intro z hz
          simp only [final, zeros, List.mem_append, List.mem_replicate] at hz
          rcases hz with ⟨_, rfl⟩ | hz
          · omega
          · exact houtsdigit z hz
        · rw [hcarryOut]
          have hzF : F final = F outs := by
            simp only [final, zeros, F]
            rw [List.foldl_append]
            have hrep :
                (List.replicate wi.val 0).foldl
                    (fun acc z => 10 * acc + z) 0 = 0 := by
              induction wi.val with
              | zero => rfl
              | succ n ih => simpa [List.replicate_succ, ih]
            rw [hrep]
          exact hzF.symm
        · intro j hj hphys
          by_cases hjwi : j < wi.val
          · have hbase := houtside j hphys (Or.inl hjwi)
            have hpowle : 10 ^ outs.length <= 10 ^ (q.val - j - 1) := by
              apply Nat.pow_le_pow_right (by omega)
              rw [houtslen]
              omega
            have hz := hzero j (by omega) hphys (lt_of_lt_of_le houtsBound hpowle)
            have hac0 : ac.val[j]? = some 0#u8 := by simpa [hbase] using hz
            rw [List.getElem?_map, hac0]
            rw [List.getElem?_append_left (by simpa [zeros] using hjwi)]
            rw [List.getElem?_replicate_of_lt (by simpa [zeros] using hjwi)]
            rfl
          · have hjoff : exists t, j = wi.val + t /\ t < outs.length := by
              refine ⟨j - wi.val, by omega, ?_⟩
              simp [final, zeros] at hj
              omega
            rcases hjoff with ⟨t, rfl, ht⟩
            have ha := harr t ht hphys
            rw [List.getElem?_append_right (by simp [zeros])]
            simpa [final, zeros] using ha
        · intro j hj hqj
          exact houtside j hj (Or.inr hqj)
        · constructor
          · intro htrue
            rcases hsticky.mp htrue with hb | ⟨j, hj, hjcap, hjnz⟩
            · exact Or.inl hb
            · right
              refine ⟨wi.val + j, ?_, hjcap, ?_⟩
              · simp [final, zeros, houtslen]
                omega
              · rw [List.getElem?_append_right (by simp [zeros])]
                simpa [zeros] using hjnz
          · intro h
            apply hsticky.mpr
            rcases h with hb | ⟨j, hj, hjcap, hjnz⟩
            · exact Or.inl hb
            · right
              by_cases hji : j < wi.val
              · have hzj : final[j]? = some 0 := by
                  rw [List.getElem?_append_left (by simpa [zeros] using hji)]
                  rw [List.getElem?_replicate_of_lt (by simpa [zeros] using hji)]
                rw [hzj] at hjnz
                simp at hjnz
              · refine ⟨j - wi.val, ?_, ?_, ?_⟩
                · simp [final, zeros] at hj
                  omega
                · omega
                · rw [List.getElem?_append_right (by simpa [zeros] using hji)] at hjnz
                  simpa [zeros] using hjnz
      · have hcpos : 0 < c.val := by omega
        have hgt : c > 0#u64 := by
          change 0 < c.val
          exact hcpos
        have hwipos : 0 < wi.val := by
          by_contra hn
          have hwi0 : wi.val = 0 := by omega
          rw [hwi0] at hcfit
          simp at hcfit
          omega
        obtain ⟨wi1, hwi1, hwi1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (UScalar.sub_spec (x := wi) (y := 1#usize) (by simpa using hwipos))
        norm_num at hwi1Val
        simp only [hgt, if_true] at hr
        rw [hwi1] at hr
        simp only [bind_tc_ok] at hr
        obtain ⟨quotient, hquotient, hquotientVal⟩ :=
          Aeneas.Std.WP.spec_imp_exists
            (U64.div_spec c (y := 10#u64) (by norm_num))
        rw [hquotient] at hr
        simp only [bind_tc_ok] at hr
        have hmulBound : 10 * quotient.val <= U64.max := by
          rw [hquotientVal]
          have hcLt : c.val < 2 ^ 64 := c.bv.isLt
          have hqle : 10 * (c.val / 10) <= c.val := Nat.mul_div_le c.val 10
          norm_num [U64.max, U64.numBits] at hcLt ⊢
          omega
        obtain ⟨tenq, htenq, htenqVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (U64.mul_spec (x := 10#u64) (y := quotient) (by
            simpa [Nat.mul_comm] using hmulBound))
        rw [htenq] at hr
        simp only [bind_tc_ok] at hr
        have htenqLe : tenq.val <= c.val := by
          rw [htenqVal, hquotientVal]
          exact Nat.mul_div_le c.val 10
        obtain ⟨rem, hrem, hremVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (U64.sub_spec (x := c) (y := tenq) htenqLe)
        rw [hrem] at hr
        simp only [bind_tc_ok] at hr
        have hremFormula : rem.val = c.val % 10 := by
          have htenqFormula : tenq.val = 10 * (c.val / 10) := by
            rw [htenqVal, hquotientVal]
            norm_num
          have heuc := Nat.mod_add_div c.val 10
          rw [hremVal.1, htenqFormula]
          omega
        have hremLt : rem.val < 10 := by
          rw [hremFormula]
          exact Nat.mod_lt _ (by omega)
        have hquotientFormula : quotient.val = c.val / 10 := by
          simpa using hquotientVal
        have hquotientFit : quotient.val < 10 ^ wi1.val := by
          rw [hquotientFormula]
          apply (Nat.div_lt_iff_lt_mul (by omega)).2
          rw [← pow_succ]
          have hwiSucc : wi1.val + 1 = wi.val := by omega
          rw [hwiSucc]
          exact hcfit
        let newOuts := rem.val :: outs
        have hnewlen : newOuts.length = q.val - wi1.val := by
          simp [newOuts, houtslen]
          omega
        have hnewdigit : forall z, z ∈ newOuts -> z < 10 := by
          intro z hz
          simp only [newOuts, List.mem_cons] at hz
          rcases hz with rfl | hz
          · exact hremLt
          · exact houtsdigit z hz
        have houtStep : F newOuts = rem.val * 10 ^ outs.length + F outs := by
          simpa [newOuts, F] using foldlFrom outs rem.val
        have hnumNew : carry.val =
            quotient.val * 10 ^ newOuts.length + F newOuts := by
          have heuc : c.val = 10 * quotient.val + rem.val := by
            rw [hquotientFormula, hremFormula]
            have hmoddiv := Nat.mod_add_div c.val 10
            omega
          have hlenSucc : q.val - wi1.val = outs.length + 1 := by
            rw [houtslen]
            omega
          calc
            carry.val = c.val * 10 ^ outs.length + F outs := hnum
            _ = (10 * quotient.val + rem.val) * 10 ^ outs.length + F outs := by rw [heuc]
            _ = quotient.val * 10 ^ newOuts.length + F newOuts := by
              rw [houtStep, hnewlen, hlenSucc, pow_succ]
              ring
        by_cases hcap : wi1.val < 768
        · have hcastRem : (UScalar.cast .U8 rem).val = rem.val := by
            simp [UScalar.cast_val_eq, Nat.mod_eq_of_lt (by omega : rem.val < 256)]
          have hwibound : wi1.val < ac.val.length := by
            have hacLen : ac.val.length = 768 := by simpa using ac.property
            rw [hacLen]
            exact hcap
          obtain ⟨ac1, hac1, hac1Val⟩ := Aeneas.Std.WP.spec_imp_exists
            (Array.update_spec ac wi1 (UScalar.cast .U8 rem)
              (by simpa [Array.length] using hwibound))
          have hcapWord : wi1 <
              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 hr
          simp only [lift] at hr
          simp only [bind_tc_ok] at hr
          rw [hac1] at hr
          simp only [bind_tc_ok] at hr
          cases hr
          simp only [Inv]
          refine ⟨by omega, hquotientFit, newOuts, hnewlen, hnewdigit,
            hnumNew, ?_, ?_, ?_⟩
          · intro j hj hpos
            rw [hac1Val]
            simp only [Aeneas.Std.Array.set, List.map_set, List.getElem?_set]
            cases j with
            | zero => simpa [newOuts, hcastRem] using hcap
            | succ j =>
                have hjold : j < outs.length := by
                  simp [newOuts] at hj
                  omega
                have hidx : wi1.val + (j + 1) = wi.val + j := by omega
                have hne : wi1.val ≠ wi.val + j := by omega
                simp [newOuts, hidx, hne]
                simpa using harr j hjold (by omega)
          · intro j hj houtsideNew
            rw [hac1Val]
            simp only [Aeneas.Std.Array.set, List.getElem?_set]
            have hne : wi1.val ≠ j := by omega
            simp [hne]
            apply houtside j hj
            omega
          · constructor
            · intro htrue
              rcases hsticky.mp htrue with hb | ⟨j, hj, hjcap, hjnz⟩
              · exact Or.inl hb
              · right
                refine ⟨j + 1, ?_, ?_, ?_⟩
                · simp [newOuts]
                  omega
                · omega
                · simpa [newOuts] using hjnz
            · intro h
              apply hsticky.mpr
              rcases h with hb | ⟨j, hj, hjcap, hjnz⟩
              · exact Or.inl hb
              · right
                cases j with
                | zero => omega
                | succ j =>
                    refine ⟨j, ?_, ?_, ?_⟩
                    · simp [newOuts] at hj
                      omega
                    · omega
                    · simpa [newOuts] using hjnz
        · have hcapWord : ¬ wi1 <
              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 hr
          by_cases hrem0 : rem.val = 0
          · have hremEq : rem = 0#u64 := UScalar.eq_of_val_eq (by simpa using hrem0)
            have hnotgt : ¬ rem > 0#u64 := by simp [hremEq]
            simp [hnotgt] at hr
            subst r
            simp only [Inv]
            refine ⟨by omega, hquotientFit, newOuts, hnewlen, hnewdigit,
              hnumNew, ?_, ?_, ?_⟩
            · intro j hj hpos
              cases j with
              | zero => omega
              | succ j =>
                  have hjold : j < outs.length := by
                    simp [newOuts] at hj
                    omega
                  have hidx : wi1.val + (j + 1) = wi.val + j := by omega
                  simpa [newOuts, hidx] using harr j hjold (by omega)
            · intro j hj houtsideNew
              apply houtside j hj
              omega
            · constructor
              · intro htrue
                rcases hsticky.mp htrue with hb | ⟨j, hj, hjcap, hjnz⟩
                · exact Or.inl hb
                · right
                  refine ⟨j + 1, ?_, ?_, ?_⟩
                  · simp [newOuts]
                    omega
                  · omega
                  · simpa [newOuts] using hjnz
              · intro h
                apply hsticky.mpr
                rcases h with hb | ⟨j, hj, hjcap, hjnz⟩
                · exact Or.inl hb
                · right
                  cases j with
                  | zero => simp [newOuts, hrem0] at hjnz
                  | succ j =>
                      refine ⟨j, ?_, ?_, ?_⟩
                      · simp [newOuts] at hj
                        omega
                      · omega
                      · simpa [newOuts] using hjnz
          · have hgtRem : rem > 0#u64 := by
              change 0 < rem.val
              omega
            simp [hgtRem] at hr
            subst r
            simp only [Inv]
            refine ⟨by omega, hquotientFit, newOuts, hnewlen, hnewdigit,
              hnumNew, ?_, ?_, ?_⟩
            · intro j hj hpos
              cases j with
              | zero => omega
              | succ j =>
                  have hjold : j < outs.length := by
                    simp [newOuts] at hj
                    omega
                  have hidx : wi1.val + (j + 1) = wi.val + j := by omega
                  simpa [newOuts, hidx] using harr j hjold (by omega)
            · intro j hj houtsideNew
              apply houtside j hj
              omega
            · constructor
              · intro _
                right
                refine ⟨0, by simp [newOuts], by omega, ?_⟩
                simp [newOuts, hrem0]
              · exact fun _ => True.intro
    · simp only [Inv]
      refine ⟨le_rfl, hcarry, [], by simp, ?_, ?_, ?_, ?_, ?_⟩
      · intro z hz
        simp at hz
      · simp [F]
      · intro j hj
        simp at hj
      · intro j hj houtside
        trivial
      · simp
    · simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1, Body]
        using hloop
  simpa [Post, 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_left_shift

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalLeftShiftSecondLoopSemantics]
theorem DecimalLeftShiftSecondLoopSemantics
    (a a' : Array Std.U8 768#usize) (b b' : Bool)
    (q : Std.Usize) (carry : Std.U64)
    (hcarry : carry.val < 10 ^ q.val)
    (hzero : forall j, j < q.val -> j < 768 ->
      carry.val < 10 ^ (q.val - j - 1) -> a.val[j]? = some 0#u8)
    (hloop :
      num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
        b a q carry = ok (b', a')) :
    exists outDigits : List Nat,
      outDigits.length = q.val /\
      (forall z, z ∈ outDigits -> z < 10) /\
      carry.val = outDigits.foldl (fun acc z => 10 * acc + z) 0 /\
      (forall j, j < outDigits.length -> j < 768 ->
        (a'.val.map (fun z => z.val))[j]? = outDigits[j]?) /\
      (forall j, j < 768 -> q.val <= j -> a'.val[j]? = a.val[j]?) /\
      (b' = true <-> b = true \/
        exists j, j < outDigits.length /\ 768 <= j /\
          outDigits[j]? != some 0) := by
-- BODY
  let F := fun ys : List Nat =>
    ys.foldl (fun acc z => 10 * acc + z) 0
  have foldlFrom (ys : List Nat) (acc : Nat) :
      ys.foldl (fun n z => 10 * n + z) acc =
        acc * 10 ^ ys.length + F ys := by
    induction ys generalizing acc with
    | nil => simp [F]
    | cons y ys ih =>
      simp only [List.foldl_cons, List.length_cons]
      rw [ih, pow_succ]
      change (10 * acc + y) * 10 ^ ys.length + F ys =
        acc * (10 ^ ys.length * 10) +
          (y :: ys).foldl (fun n z => 10 * n + z) 0
      simp only [List.foldl_cons]
      rw [ih]
      ring
  have foldBound (ys : List Nat) (hd : forall z, z ∈ ys -> z < 10) :
      F ys < 10 ^ ys.length := by
    induction ys with
    | nil => simp [F]
    | cons y ys ih =>
      have hy : y < 10 := hd y (by simp)
      have hys : forall z, z ∈ ys -> z < 10 := by
        intro z hz
        exact hd z (by simp [hz])
      have hi := ih hys
      rw [show F (y :: ys) = y * 10 ^ ys.length + F ys by
        simpa [F] using foldlFrom ys y, List.length_cons, pow_succ]
      have hmul : y * 10 ^ ys.length <= 9 * 10 ^ ys.length :=
        Nat.mul_le_mul_right _ (by omega)
      calc
        y * 10 ^ ys.length + F ys <
            y * 10 ^ ys.length + 10 ^ ys.length := Nat.add_lt_add_left hi _
        _ <= 9 * 10 ^ ys.length + 10 ^ ys.length :=
          Nat.add_le_add_right hmul _
        _ = 10 ^ ys.length * 10 := by ring
  let Inv := fun (x : Bool × Array Std.U8 768#usize × Std.Usize × Std.U64) =>
    let (bc, ac, wi, c) := x
    wi.val <= q.val /\
    c.val < 10 ^ wi.val /\
    exists outs : List Nat,
      outs.length = q.val - wi.val /\
      (forall z, z ∈ outs -> z < 10) /\
      carry.val = c.val * 10 ^ outs.length + F outs /\
      (forall j, j < outs.length -> wi.val + j < 768 ->
        (ac.val.map (fun z : Std.U8 => z.val))[wi.val + j]? = outs[j]?) /\
      (forall j, j < 768 -> (j < wi.val \/ q.val <= j) ->
        ac.val[j]? = a.val[j]?) /\
      (bc = true <-> b = true \/
        exists j, j < outs.length /\ 768 <= wi.val + j /\
          outs[j]? != some 0)
  let Post := fun (x : Bool × Array Std.U8 768#usize) =>
    let (bc, ac) := x
    exists outDigits : List Nat,
      outDigits.length = q.val /\
      (forall z, z ∈ outDigits -> z < 10) /\
      carry.val = F outDigits /\
      (forall j, j < outDigits.length -> j < 768 ->
        (ac.val.map (fun z : Std.U8 => z.val))[j]? = outDigits[j]?) /\
      (forall j, j < 768 -> q.val <= j -> ac.val[j]? = a.val[j]?) /\
      (bc = true <-> b = true \/
        exists j, j < outDigits.length /\ 768 <= j /\
          outDigits[j]? != some 0)
  let Body := fun (x : Bool × Array Std.U8 768#usize × Std.Usize × Std.U64) =>
    let (bc, ac, wi, c) := x
    num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body bc ac wi c
  have hp : Post (b', a') := by
    refine ResultLoopPartial Inv Post Body ?_ (b, a, q, carry) (b', a') ?_ ?_
    · intro x hx r hr
      rcases x with ⟨bc, ac, wi, c⟩
      simp only [Inv] at hx
      rcases hx with ⟨hwiq, hcfit, outs, houtslen, houtsdigit, hnum,
        harr, houtside, hsticky⟩
      simp only [Body,
        num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body] at hr
      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 hr
        subst r
        simp only [Post]
        have hcarryOut : carry.val = F outs := by simpa [hc0] using hnum
        have houtsBound : carry.val < 10 ^ outs.length := by
          rw [hcarryOut]
          exact foldBound outs houtsdigit
        let zeros := List.replicate wi.val 0
        let final := zeros ++ outs
        refine ⟨final, ?_, ?_, ?_, ?_, ?_, ?_⟩
        · simp [final, zeros, houtslen]
          omega
        · intro z hz
          simp only [final, zeros, List.mem_append, List.mem_replicate] at hz
          rcases hz with ⟨_, rfl⟩ | hz
          · omega
          · exact houtsdigit z hz
        · rw [hcarryOut]
          have hzF : F final = F outs := by
            simp only [final, zeros, F]
            rw [List.foldl_append]
            have hrep :
                (List.replicate wi.val 0).foldl
                    (fun acc z => 10 * acc + z) 0 = 0 := by
              induction wi.val with
              | zero => rfl
              | succ n ih => simpa [List.replicate_succ, ih]
            rw [hrep]
          exact hzF.symm
        · intro j hj hphys
          by_cases hjwi : j < wi.val
          · have hbase := houtside j hphys (Or.inl hjwi)
            have hpowle : 10 ^ outs.length <= 10 ^ (q.val - j - 1) := by
              apply Nat.pow_le_pow_right (by omega)
              rw [houtslen]
              omega
            have hz := hzero j (by omega) hphys (lt_of_lt_of_le houtsBound hpowle)
            have hac0 : ac.val[j]? = some 0#u8 := by simpa [hbase] using hz
            rw [List.getElem?_map, hac0]
            rw [List.getElem?_append_left (by simpa [zeros] using hjwi)]
            rw [List.getElem?_replicate_of_lt (by simpa [zeros] using hjwi)]
            rfl
          · have hjoff : exists t, j = wi.val + t /\ t < outs.length := by
              refine ⟨j - wi.val, by omega, ?_⟩
              simp [final, zeros] at hj
              omega
            rcases hjoff with ⟨t, rfl, ht⟩
            have ha := harr t ht hphys
            rw [List.getElem?_append_right (by simp [zeros])]
            simpa [final, zeros] using ha
        · intro j hj hqj
          exact houtside j hj (Or.inr hqj)
        · constructor
          · intro htrue
            rcases hsticky.mp htrue with hb | ⟨j, hj, hjcap, hjnz⟩
            · exact Or.inl hb
            · right
              refine ⟨wi.val + j, ?_, hjcap, ?_⟩
              · simp [final, zeros, houtslen]
                omega
              · rw [List.getElem?_append_right (by simp [zeros])]
                simpa [zeros] using hjnz
          · intro h
            apply hsticky.mpr
            rcases h with hb | ⟨j, hj, hjcap, hjnz⟩
            · exact Or.inl hb
            · right
              by_cases hji : j < wi.val
              · have hzj : final[j]? = some 0 := by
                  rw [List.getElem?_append_left (by simpa [zeros] using hji)]
                  rw [List.getElem?_replicate_of_lt (by simpa [zeros] using hji)]
                rw [hzj] at hjnz
                simp at hjnz
              · refine ⟨j - wi.val, ?_, ?_, ?_⟩
                · simp [final, zeros] at hj
                  omega
                · omega
                · rw [List.getElem?_append_right (by simpa [zeros] using hji)] at hjnz
                  simpa [zeros] using hjnz
      · have hcpos : 0 < c.val := by omega
        have hgt : c > 0#u64 := by
          change 0 < c.val
          exact hcpos
        have hwipos : 0 < wi.val := by
          by_contra hn
          have hwi0 : wi.val = 0 := by omega
          rw [hwi0] at hcfit
          simp at hcfit
          omega
        obtain ⟨wi1, hwi1, hwi1Val⟩ := Aeneas.Std.WP.spec_imp_exists
          (UScalar.sub_spec (x := wi) (y := 1#usize) (by simpa using hwipos))
        norm_num at hwi1Val
        simp only [hgt, if_true] at hr
        rw [hwi1] at hr
        simp only [bind_tc_ok] at hr
        obtain ⟨quotient, hquotient, hquotientVal⟩ :=
          Aeneas.Std.WP.spec_imp_exists
            (U64.div_spec c (y := 10#u64) (by norm_num))
        rw [hquotient] at hr
        simp only [bind_tc_ok] at hr
        have hmulBound : 10 * quotient.val <= U64.max := by
          rw [hquotientVal]
          have hcLt : c.val < 2 ^ 64 := c.bv.isLt
          have hqle : 10 * (c.val / 10) <= c.val := Nat.mul_div_le c.val 10
          norm_num [U64.max, U64.numBits] at hcLt ⊢
          omega
        obtain ⟨tenq, htenq, htenqVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (U64.mul_spec (x := 10#u64) (y := quotient) (by
            simpa [Nat.mul_comm] using hmulBound))
        rw [htenq] at hr
        simp only [bind_tc_ok] at hr
        have htenqLe : tenq.val <= c.val := by
          rw [htenqVal, hquotientVal]
          exact Nat.mul_div_le c.val 10
        obtain ⟨rem, hrem, hremVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (U64.sub_spec (x := c) (y := tenq) htenqLe)
        rw [hrem] at hr
        simp only [bind_tc_ok] at hr
        have hremFormula : rem.val = c.val % 10 := by
          have htenqFormula : tenq.val = 10 * (c.val / 10) := by
            rw [htenqVal, hquotientVal]
            norm_num
          have heuc := Nat.mod_add_div c.val 10
          rw [hremVal.1, htenqFormula]
          omega
        have hremLt : rem.val < 10 := by
          rw [hremFormula]
          exact Nat.mod_lt _ (by omega)
        have hquotientFormula : quotient.val = c.val / 10 := by
          simpa using hquotientVal
        have hquotientFit : quotient.val < 10 ^ wi1.val := by
          rw [hquotientFormula]
          apply (Nat.div_lt_iff_lt_mul (by omega)).2
          rw [← pow_succ]
          have hwiSucc : wi1.val + 1 = wi.val := by omega
          rw [hwiSucc]
          exact hcfit
        let newOuts := rem.val :: outs
        have hnewlen : newOuts.length = q.val - wi1.val := by
          simp [newOuts, houtslen]
          omega
        have hnewdigit : forall z, z ∈ newOuts -> z < 10 := by
          intro z hz
          simp only [newOuts, List.mem_cons] at hz
          rcases hz with rfl | hz
          · exact hremLt
          · exact houtsdigit z hz
        have houtStep : F newOuts = rem.val * 10 ^ outs.length + F outs := by
          simpa [newOuts, F] using foldlFrom outs rem.val
        have hnumNew : carry.val =
            quotient.val * 10 ^ newOuts.length + F newOuts := by
          have heuc : c.val = 10 * quotient.val + rem.val := by
            rw [hquotientFormula, hremFormula]
            have hmoddiv := Nat.mod_add_div c.val 10
            omega
          have hlenSucc : q.val - wi1.val = outs.length + 1 := by
            rw [houtslen]
            omega
          calc
            carry.val = c.val * 10 ^ outs.length + F outs := hnum
            _ = (10 * quotient.val + rem.val) * 10 ^ outs.length + F outs := by rw [heuc]
            _ = quotient.val * 10 ^ newOuts.length + F newOuts := by
              rw [houtStep, hnewlen, hlenSucc, pow_succ]
              ring
        by_cases hcap : wi1.val < 768
        · have hcastRem : (UScalar.cast .U8 rem).val = rem.val := by
            simp [UScalar.cast_val_eq, Nat.mod_eq_of_lt (by omega : rem.val < 256)]
          have hwibound : wi1.val < ac.val.length := by
            have hacLen : ac.val.length = 768 := by simpa using ac.property
            rw [hacLen]
            exact hcap
          obtain ⟨ac1, hac1, hac1Val⟩ := Aeneas.Std.WP.spec_imp_exists
            (Array.update_spec ac wi1 (UScalar.cast .U8 rem)
              (by simpa [Array.length] using hwibound))
          have hcapWord : wi1 <
              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 hr
          simp only [lift] at hr
          simp only [bind_tc_ok] at hr
          rw [hac1] at hr
          simp only [bind_tc_ok] at hr
          cases hr
          simp only [Inv]
          refine ⟨by omega, hquotientFit, newOuts, hnewlen, hnewdigit,
            hnumNew, ?_, ?_, ?_⟩
          · intro j hj hpos
            rw [hac1Val]
            simp only [Aeneas.Std.Array.set, List.map_set, List.getElem?_set]
            cases j with
            | zero => simpa [newOuts, hcastRem] using hcap
            | succ j =>
                have hjold : j < outs.length := by
                  simp [newOuts] at hj
                  omega
                have hidx : wi1.val + (j + 1) = wi.val + j := by omega
                have hne : wi1.val ≠ wi.val + j := by omega
                simp [newOuts, hidx, hne]
                simpa using harr j hjold (by omega)
          · intro j hj houtsideNew
            rw [hac1Val]
            simp only [Aeneas.Std.Array.set, List.getElem?_set]
            have hne : wi1.val ≠ j := by omega
            simp [hne]
            apply houtside j hj
            omega
          · constructor
            · intro htrue
              rcases hsticky.mp htrue with hb | ⟨j, hj, hjcap, hjnz⟩
              · exact Or.inl hb
              · right
                refine ⟨j + 1, ?_, ?_, ?_⟩
                · simp [newOuts]
                  omega
                · omega
                · simpa [newOuts] using hjnz
            · intro h
              apply hsticky.mpr
              rcases h with hb | ⟨j, hj, hjcap, hjnz⟩
              · exact Or.inl hb
              · right
                cases j with
                | zero => omega
                | succ j =>
                    refine ⟨j, ?_, ?_, ?_⟩
                    · simp [newOuts] at hj
                      omega
                    · omega
                    · simpa [newOuts] using hjnz
        · have hcapWord : ¬ wi1 <
              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 hr
          by_cases hrem0 : rem.val = 0
          · have hremEq : rem = 0#u64 := UScalar.eq_of_val_eq (by simpa using hrem0)
            have hnotgt : ¬ rem > 0#u64 := by simp [hremEq]
            simp [hnotgt] at hr
            subst r
            simp only [Inv]
            refine ⟨by omega, hquotientFit, newOuts, hnewlen, hnewdigit,
              hnumNew, ?_, ?_, ?_⟩
            · intro j hj hpos
              cases j with
              | zero => omega
              | succ j =>
                  have hjold : j < outs.length := by
                    simp [newOuts] at hj
                    omega
                  have hidx : wi1.val + (j + 1) = wi.val + j := by omega
                  simpa [newOuts, hidx] using harr j hjold (by omega)
            · intro j hj houtsideNew
              apply houtside j hj
              omega
            · constructor
              · intro htrue
                rcases hsticky.mp htrue with hb | ⟨j, hj, hjcap, hjnz⟩
                · exact Or.inl hb
                · right
                  refine ⟨j + 1, ?_, ?_, ?_⟩
                  · simp [newOuts]
                    omega
                  · omega
                  · simpa [newOuts] using hjnz
              · intro h
                apply hsticky.mpr
                rcases h with hb | ⟨j, hj, hjcap, hjnz⟩
                · exact Or.inl hb
                · right
                  cases j with
                  | zero => simp [newOuts, hrem0] at hjnz
                  | succ j =>
                      refine ⟨j, ?_, ?_, ?_⟩
                      · simp [newOuts] at hj
                        omega
                      · omega
                      · simpa [newOuts] using hjnz
          · have hgtRem : rem > 0#u64 := by
              change 0 < rem.val
              omega
            simp [hgtRem] at hr
            subst r
            simp only [Inv]
            refine ⟨by omega, hquotientFit, newOuts, hnewlen, hnewdigit,
              hnumNew, ?_, ?_, ?_⟩
            · intro j hj hpos
              cases j with
              | zero => omega
              | succ j =>
                  have hjold : j < outs.length := by
                    simp [newOuts] at hj
                    omega
                  have hidx : wi1.val + (j + 1) = wi.val + j := by omega
                  simpa [newOuts, hidx] using harr j hjold (by omega)
            · intro j hj houtsideNew
              apply houtside j hj
              omega
            · constructor
              · intro _
                right
                refine ⟨0, by simp [newOuts], by omega, ?_⟩
                simp [newOuts, hrem0]
              · exact fun _ => True.intro
    · simp only [Inv]
      refine ⟨le_rfl, hcarry, [], by simp, ?_, ?_, ?_, ?_, ?_⟩
      · intro z hz
        simp at hz
      · simp [F]
      · intro j hj
        simp at hj
      · intro j hj houtside
        trivial
      · simp
    · simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1, Body]
        using hloop
  simpa [Post, 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 9 entries
Technical checking records
Local closure summary
{
  "node": "DecimalLeftShiftSecondLoopSemantics",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "3489afd071ea72e1b9c73468c58d87dfcb3a85d3aa27932fb7e43f896cc8dd7a",
  "active_statement_hash": "85a1cbb42095415b0c0341aab50b16b892c6a123f810cb77df4fd06fb5c189b0",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "ab64d13f256386ee0e494a5abcc6a5db49512cf3d90bbdfcabd6327793a31f95",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-158",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "ResultLoopPartial": "566d2aeb76ed9100",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "0eb6fb2627d0ff5c",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift": "0eb6fb2627d0ff5c"
  },
  "strict_definition_deps": {
    "ResultLoopPartial": "1b58e96ad00b23c2",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "39f6cb231ef80388",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift": "4555009895b002bf"
  }
}
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:7c79cee66260284c868ec33f6c4bc57fb0ab84971dc88ab868efa091478cc30d",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}