DecimalLeftShiftDigitGrowth

Lean checked

Statement

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

Lemma · Exact decimal digit growth under a left shift

Let a decimal sequence contain n\le768 stored decimal digits, let I be their base-ten fold, and let 0\le k\le60. Suppose h satisfies
10^{h-1}<2^k<10^h.
Then the extracted decimal left-shift digit-count routine returns normally, and its result is
\begin{cases} h,&10^{n+h-1}\le I2^k,\\ h-1,&I2^k<10^{n+h-1}. \end{cases}
The Lean statement records the same choice by an if expression.

Lean statement

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

import Tablet.DecimalLeftShiftTableCertificate
import Tablet.DecimalDigitLexScale
import Tablet.DecimalLeftShiftLoopLex
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq
import Tablet.num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalLeftShiftDigitGrowth]
theorem DecimalLeftShiftDigitGrowth :
    forall (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift : Std.Usize) (h : Nat),
      shift.val <= 60 ->
      10 ^ (h - 1) < 2 ^ shift.val ->
      2 ^ shift.val < 10 ^ h ->
      d.num_digits.val <= 768 ->
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) ->
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun z => z.val)
      let n := d.num_digits.val
      let value := digits.foldl (fun a z => 10 * a + z) 0
      exists out : Std.Usize,
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
          d shift = ok out /\
        out.val = if 10 ^ (n + h - 1) <= value * 2 ^ shift.val
          then h else h - 1 := 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.

Earlier prose review

An earlier review questioned the prose argument. Lean subsequently checked the formal proof. Review of the statement’s intended meaning is a separate judgment.

Proof

The shift is at most 60, so masking it with 63 in num imp dec2flt decimal seq number of digits decimal left shift does not change it. Apply DecimalLeftShiftTableCertificate at the finite index k. For k=0 its table field is zero and its comparison slice is empty; the routine returns zero. Since 2^0=1 and an n-digit integer satisfies I<10^n, the displayed test is false and its right side is h-1=0.
Assume henceforth k>0. Write d_0\ldots d_{\ell-1} for the certified table slice. The certificate gives
D:=\sum_{i<\ell}d_i10^{\ell-i-1}=5^k,\qquad h+\ell=k+1, \tag{1}
and d_0 is nonzero. All delimiter calculations are ordered and within the 1308-cell table, so both table indexing operations, the slice construction, the iterator construction, and subtraction of its endpoints return normally. The encoded high field is h.
The loop compares the sequence digits with d_0,\ldots,d_{\ell-1} in order. At the first unequal position it returns h when the sequence digit is larger and h-1 when it is smaller. If all available sequence digits agree and the table slice still has a digit, the branch i\ge n returns h-1. Thus the loop is exactly a lexicographic comparison of the first \ell digits of the sequence after padding the sequence on the right with zeroes. No array access fails: an input digit is read only in the branch i<n, and n\le768 is the physical array length.
We now translate that comparison into the arithmetic test in the statement. If n\ge\ell, write
I=Q10^{n-\ell}+S,\qquad 0\le S<10^{n-\ell}.
The compared prefix is the \ell-digit integer Q, so the routine selects h exactly when Q\ge5^k. Equivalently,
I10^\ell\ge5^k10^n. \tag{2}
Indeed Q<5^k makes the left side strictly smaller after bounding S, while Q\ge5^k makes it at least the right side.
If n<\ell, the comparison is not automatically smaller. The available word may differ upward before it is exhausted. Its zero-padded comparison integer is exactly I10^{\ell-n}, so this branch selects h precisely when
I10^{\ell-n}\ge5^k. \tag{3}
This includes both possibilities omitted by the former argument: an earlier larger digit makes (3) true, whereas equality through all n positions and exhaustion makes it false because 5^k has nonzero leading digit. For example, k=3, n=2, and I=99 compares 990 with 125 and therefore selects the larger growth.
Multiplying (3) by 10^n, or using (2) directly, gives in both cases
I10^\ell\ge5^k10^n. \tag{4}
By (1), \ell=k+1-h. Multiply the desired threshold 10^{n+h-1}\le I2^k by the positive integer 5^k and use 2^k5^k=10^k; after cancelling the common positive power of ten, the result is exactly (4). Therefore the loop returns h exactly under the statement's test and h-1 otherwise, as required.

Lean proof


  intro d shift h hshift hlo hhi hn hdigit
  simp only
  by_cases hk0 : shift.val = 0
  · simp [hk0] at hlo
  · have hkpos : 0 < shift.val := by omega
    let kfin : Fin 61 := ⟨shift.val, by omega⟩
    have cert := DecimalLeftShiftTableCertificate kfin
    dsimp only at cert
    rcases cert with ⟨hloidx, hhiidx, hlen, hdsdigit, _, hpositive⟩
    have hp := hpositive (by simpa [kfin] using hkpos)
    rcases hp with ⟨hdsne, hhead, hfold, hsum, hclo, hchi⟩
    simp only [kfin] at hclo hchi hfold hsum
    have hhpos : 0 < h := by
      by_contra hz
      have : h = 0 := by omega
      simp [this] at hhi
    let table :=
      num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
    let x := table.val.get ⟨shift.val, by
      have hlen : table.val.length = 65 := by simp [table]
      omega⟩
    let hi := x.val / 2048
    change 10 ^ (hi - 1) < 2 ^ shift.val at hclo
    change 2 ^ shift.val < 10 ^ hi at hchi
    have hhipos : 0 < hi := by
      by_contra hz
      have : hi = 0 := by omega
      rw [this] at hchi
      simp at hchi
    have heq : hi = h := by
      apply Nat.le_antisymm
      · by_contra hnle
        have hle : h <= hi - 1 := by omega
        have hpows : 10 ^ h <= 10 ^ (hi - 1) :=
          Nat.pow_le_pow_right (by omega) hle
        omega
      · by_contra hnle
        have hle : hi <= h - 1 := by omega
        have hpows : 10 ^ hi <= 10 ^ (h - 1) :=
          Nat.pow_le_pow_right (by omega) hle
        omega
    simp only [kfin] at hloidx hhiidx hlen hdsdigit hdsne hhead
    let pow5Table :=
      num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE_POW5
    let y := table.val.get ⟨shift.val + 1, by
      have hlen : table.val.length = 65 := by simp [table]
      omega⟩
    let lo := x.val % 2048
    let hiNext := y.val % 2048
    let dsU := pow5Table.val.extract lo hiNext
    let ds := dsU.map (fun z => z.val)
    have hloidx' : lo <= hiNext := by
      simpa [lo, hiNext, x, y, table] using hloidx
    have hhiidx' : hiNext <= 1308 := by
      simpa [hiNext, y, table] using hhiidx
    have hlen' : ds.length = hiNext - lo := by
      simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hlen
    have hdsdigit' : forall z, z ∈ ds -> z < 10 := by
      simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hdsdigit
    have hdsne' : 0 < ds.length := by
      simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hdsne
    have hhead' : 0 < ds.headD 0 := by
      simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hhead
    have hfold' : ds.foldl (fun n z => 10 * n + z) 0 = 5 ^ shift.val := by
      simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hfold
    have hsum' : hi + ds.length = shift.val + 1 := by
      simpa [hi, x, ds, dsU, pow5Table, lo, hiNext, y, table] using hsum
    have hmask : shift &&& 63#usize = shift := by
      apply UScalar.eq_of_val_eq
      rw [UScalar.val_and]
      norm_num
      rw [show 63 = 2 ^ 6 - 1 by decide,
        Nat.and_two_pow_sub_one_of_lt_two_pow (n := 6) (by omega)]
    unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
    simp only [hmask, lift]
    simp only [Bind.bind, Aeneas.Std.bind]
    have htableBound : shift.val <
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE.val.length := by
      simp
      omega
    obtain ⟨xa, hxa, hxaVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (Array.index_usize_spec
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
        shift (by simpa [Array.length] using htableBound))
    rw [hxa]
    simp only [Bind.bind, Aeneas.Std.bind]
    have hxaEq : xa = x := by
      apply UScalar.eq_of_val_eq
      simpa [x, table] using congrArg UScalar.val hxaVal
    subst xa
    rw [hxaEq]
    have hshiftAdd : shift.val + 1 <= UScalar.max .Usize := by scalar_tac
    obtain ⟨next, hnext, hnextVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.add_spec (x := shift) (y := 1#usize)
        (by simpa using hshiftAdd))
    rw [hnext]
    simp only [Bind.bind, Aeneas.Std.bind]
    norm_num at hnextVal
    have hnextBound : next.val <
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE.val.length := by
      simp
      omega
    obtain ⟨yb, hyb, hybVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (Array.index_usize_spec
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
        next (by simpa [Array.length] using hnextBound))
    rw [hyb]
    simp only [Bind.bind, Aeneas.Std.bind]
    have hybEq : yb = y := by
      apply UScalar.eq_of_val_eq
      simpa [y, table, hnextVal] using congrArg UScalar.val hybVal
    subst yb
    rw [hybEq]
    obtain ⟨high16, hhigh16, hhigh16Val⟩ := Aeneas.Std.WP.spec_imp_exists
      (U16.ShiftRight_IScalar_spec x 11#i32 (by norm_num) (by norm_num))
    rw [hhigh16]
    simp only [Bind.bind, Aeneas.Std.bind]
    have hhigh16Eq : high16.val = hi := by
      rw [hhigh16Val.1, Nat.shiftRight_eq_div_pow]
      norm_num [hi, Int.toNat]
    let numNew : Std.Usize := UScalar.cast .Usize high16
    have hnumNewVal : numNew.val = h := by
      simp [numNew, UScalar.cast_val_eq, hhigh16Eq, heq]
    have hnumNewPos : 0 < numNew.val := by omega
    let low16 := 2047#u16 &&& x
    let pow5a : Std.Usize := UScalar.cast .Usize low16
    have hpow5aVal : pow5a.val = lo := by
      simp [pow5a, low16, lo, UScalar.cast_val_eq, UScalar.val_and]
      rw [show 2047 = 2 ^ 11 - 1 by decide, Nat.and_comm,
        Nat.and_two_pow_sub_one_eq_mod]
    let next16 := 2047#u16 &&& y
    let pow5b : Std.Usize := UScalar.cast .Usize next16
    have hpow5bVal : pow5b.val = hiNext := by
      simp [pow5b, next16, hiNext, UScalar.cast_val_eq, UScalar.val_and]
      rw [show 2047 = 2 ^ 11 - 1 by decide, Nat.and_comm,
        Nat.and_two_pow_sub_one_eq_mod]
    rw [show UScalar.cast .Usize (2047#u16 &&& x) = pow5a by rfl]
    rw [show UScalar.cast .Usize (2047#u16 &&& y) = pow5b by rfl]
    rw [show UScalar.cast .Usize high16 = numNew by rfl]
    have hloTable : pow5a.val <= pow5Table.val.length := by
      simp [hpow5aVal, pow5Table]
      omega
    have hlo1308 : pow5a.val <= 1308 := by
      rw [hpow5aVal]
      omega
    let suffix : Slice Std.U8 := pow5Table.to_slice.drop pow5a
    have hsuffix :
        core.array.Array.index
          (core.ops.index.IndexSlice
            (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8))
          pow5Table { start := pow5a } = ok suffix := by
      rw [Array.index_SliceIndexRangeFromUsizeSlice]
      unfold core.slice.index.SliceIndexRangeFromUsizeSlice.index
      simp [hlo1308, suffix]
    rw [hsuffix]
    simp only [Bind.bind, Aeneas.Std.bind]
    unfold core.slice.Slice.iter
    simp only [Bind.bind, Aeneas.Std.bind]
    unfold core.slice.iter.IteratorSliceIter.enumerate
    simp only [Bind.bind, Aeneas.Std.bind]
    have hsubBound : pow5a.val <= pow5b.val := by
      rw [hpow5aVal, hpow5bVal]
      exact hloidx'
    obtain ⟨span, hspan, hspanVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.sub_spec (x := pow5b) (y := pow5a) hsubBound)
    rw [hspan]
    simp only [Bind.bind, Aeneas.Std.bind]
    unfold core.iter.adapters.enumerate.IteratorEnumerate.take
    simp only [Bind.bind, Aeneas.Std.bind]
    have hspanDs : span.val = ds.length := by
      rw [hspanVal.1, hpow5aVal, hpow5bVal]
      omega
    have hell : span.val <= suffix.val.length := by
      simp [suffix, Slice.drop, Array.to_slice, pow5Table, hspanVal.1,
        hpow5aVal, hpow5bVal, List.length_drop]
      omega
    have hdiffBound : hiNext - lo <= 1308 - lo :=
      Nat.sub_le_sub_right hhiidx' lo
    have hseq : (suffix.val.take span.val).map (fun z => z.val) = ds := by
      simp [suffix, Slice.drop, Array.to_slice, hspanDs, ds, dsU,
        pow5Table, hpow5aVal, Nat.min_eq_left hdiffBound]
    have hfive : 5 ^ shift.val % 10 = 5 := by
      have hs : forall q : Nat, 5 ^ (q + 1) % 10 = 5 := by
        intro q
        induction q with
        | zero => decide
        | succ q ih =>
          rw [Nat.pow_succ, Nat.mul_mod, ih]
      obtain ⟨q, hq⟩ := Nat.exists_eq_add_of_le hkpos
      rw [hq]
      simpa [Nat.add_comm] using hs q
    have hdslast : forall hne : ds ≠ [], ds.getLast hne ≠ 0 := by
      intro hne hz
      have hreconstruct := List.dropLast_append_getLast hne
      have hmod := congrArg (fun n : Nat => n % 10) hfold'
      change List.foldl (fun n z => 10 * n + z) 0 ds % 10 =
        5 ^ shift.val % 10 at hmod
      rw [hfive] at hmod
      rw [← hreconstruct] at hmod
      simp [List.foldl_append, hz] at hmod
    let digits := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
    have hdigits : forall z, z ∈ digits -> z < 10 := by
      intro z hz
      simp only [digits, List.mem_map] at hz
      rcases hz with ⟨u, hu, rfl⟩
      exact hdigit u hu
    have hlex := DecimalDigitLexScale digits ds hdigits hdsdigit' hdslast
    let value := digits.foldl (fun a z => 10 * a + z) 0
    have hdigitsLen : digits.length = d.num_digits.val := by
      simp [digits]
      have hdlen : d.digits.val.length = 768 := d.digits.property
      omega
    have hkSplit : shift.val = (h - 1) + ds.length := by
      omega
    have hpowN : 10 ^ (d.num_digits.val + h - 1) =
        10 ^ digits.length * 10 ^ (h - 1) := by
      rw [show d.num_digits.val + h - 1 =
        d.num_digits.val + (h - 1) by omega, Nat.pow_add, hdigitsLen]
    have hpowK : 10 ^ shift.val = 10 ^ (h - 1) * 10 ^ ds.length := by
      rw [hkSplit, Nat.pow_add]
    have htwofive : 2 ^ shift.val * 5 ^ shift.val = 10 ^ shift.val := by
      rw [← Nat.mul_pow]
    have hthreshold :
        10 ^ (d.num_digits.val + h - 1) <= value * 2 ^ shift.val ↔
          5 ^ shift.val * 10 ^ digits.length <= value * 10 ^ ds.length := by
      constructor
      · intro ht
        have hm := Nat.mul_le_mul_left (5 ^ shift.val) ht
        apply Nat.le_of_mul_le_mul_left (c := 10 ^ (h - 1)) ?_ (by positivity)
        calc
          10 ^ (h - 1) * (5 ^ shift.val * 10 ^ digits.length) =
              5 ^ shift.val * 10 ^ (d.num_digits.val + h - 1) := by
                rw [hpowN]
                ring
          _ <= 5 ^ shift.val * (value * 2 ^ shift.val) := hm
          _ = 10 ^ (h - 1) * (value * 10 ^ ds.length) := by
                rw [show 5 ^ shift.val * (value * 2 ^ shift.val) =
                  value * (2 ^ shift.val * 5 ^ shift.val) by ring,
                  htwofive, hpowK]
                ring
      · intro ht
        have hm := Nat.mul_le_mul_left (10 ^ (h - 1)) ht
        apply Nat.le_of_mul_le_mul_left (c := 5 ^ shift.val) ?_ (by positivity)
        calc
          5 ^ shift.val * 10 ^ (d.num_digits.val + h - 1) =
              10 ^ (h - 1) * (5 ^ shift.val * 10 ^ digits.length) := by
                rw [hpowN]
                ring
          _ <= 10 ^ (h - 1) * (value * 10 ^ ds.length) := hm
          _ = 5 ^ shift.val * (value * 2 ^ shift.val) := by
                rw [show 5 ^ shift.val * (value * 2 ^ shift.val) =
                  value * (2 ^ shift.val * 5 ^ shift.val) by ring,
                  htwofive, hpowK]
                ring
    have hlex' : List.Lex (fun a b : Nat => a < b) (digits.take ds.length) ds ↔
        value * 10 ^ ds.length < 5 ^ shift.val * 10 ^ digits.length := by
      simpa [value, hfold'] using hlex
    obtain ⟨out, hout, houtVal⟩ := DecimalLeftShiftLoopLex
      d suffix span numNew hell hnumNewPos hn
    refine ⟨out, hout, ?_⟩
    rw [hseq, hspanDs] at houtVal
    rw [houtVal]
    change (if List.Lex (fun a b : Nat => a < b) (digits.take ds.length) ds
      then numNew.val - 1 else numNew.val) =
      if 10 ^ (d.num_digits.val + h - 1) <= value * 2 ^ shift.val
      then h else h - 1
    by_cases ht : 10 ^ (d.num_digits.val + h - 1) <= value * 2 ^ shift.val
    · have hs := hthreshold.mp ht
      have hnlex : ¬ List.Lex (fun a b : Nat => a < b)
          (digits.take ds.length) ds := by
        intro hl
        have hlt := hlex'.mp hl
        omega
      simp [ht, hnlex, hnumNewVal]
    · have hns : ¬ 5 ^ shift.val * 10 ^ digits.length <=
          value * 10 ^ ds.length := by
        intro hs
        exact ht (hthreshold.mpr hs)
      have hlt : value * 10 ^ ds.length <
          5 ^ shift.val * 10 ^ digits.length := by omega
      have hl := hlex'.mpr hlt
      simp [ht, hl, hnumNewVal]

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalLeftShiftTableCertificate
import Tablet.DecimalDigitLexScale
import Tablet.DecimalLeftShiftLoopLex
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq
import Tablet.num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalLeftShiftDigitGrowth]
theorem DecimalLeftShiftDigitGrowth :
    forall (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift : Std.Usize) (h : Nat),
      shift.val <= 60 ->
      10 ^ (h - 1) < 2 ^ shift.val ->
      2 ^ shift.val < 10 ^ h ->
      d.num_digits.val <= 768 ->
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) ->
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun z => z.val)
      let n := d.num_digits.val
      let value := digits.foldl (fun a z => 10 * a + z) 0
      exists out : Std.Usize,
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
          d shift = ok out /\
        out.val = if 10 ^ (n + h - 1) <= value * 2 ^ shift.val
          then h else h - 1 := by
-- BODY
  intro d shift h hshift hlo hhi hn hdigit
  simp only
  by_cases hk0 : shift.val = 0
  · simp [hk0] at hlo
  · have hkpos : 0 < shift.val := by omega
    let kfin : Fin 61 := ⟨shift.val, by omega⟩
    have cert := DecimalLeftShiftTableCertificate kfin
    dsimp only at cert
    rcases cert with ⟨hloidx, hhiidx, hlen, hdsdigit, _, hpositive⟩
    have hp := hpositive (by simpa [kfin] using hkpos)
    rcases hp with ⟨hdsne, hhead, hfold, hsum, hclo, hchi⟩
    simp only [kfin] at hclo hchi hfold hsum
    have hhpos : 0 < h := by
      by_contra hz
      have : h = 0 := by omega
      simp [this] at hhi
    let table :=
      num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
    let x := table.val.get ⟨shift.val, by
      have hlen : table.val.length = 65 := by simp [table]
      omega⟩
    let hi := x.val / 2048
    change 10 ^ (hi - 1) < 2 ^ shift.val at hclo
    change 2 ^ shift.val < 10 ^ hi at hchi
    have hhipos : 0 < hi := by
      by_contra hz
      have : hi = 0 := by omega
      rw [this] at hchi
      simp at hchi
    have heq : hi = h := by
      apply Nat.le_antisymm
      · by_contra hnle
        have hle : h <= hi - 1 := by omega
        have hpows : 10 ^ h <= 10 ^ (hi - 1) :=
          Nat.pow_le_pow_right (by omega) hle
        omega
      · by_contra hnle
        have hle : hi <= h - 1 := by omega
        have hpows : 10 ^ hi <= 10 ^ (h - 1) :=
          Nat.pow_le_pow_right (by omega) hle
        omega
    simp only [kfin] at hloidx hhiidx hlen hdsdigit hdsne hhead
    let pow5Table :=
      num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE_POW5
    let y := table.val.get ⟨shift.val + 1, by
      have hlen : table.val.length = 65 := by simp [table]
      omega⟩
    let lo := x.val % 2048
    let hiNext := y.val % 2048
    let dsU := pow5Table.val.extract lo hiNext
    let ds := dsU.map (fun z => z.val)
    have hloidx' : lo <= hiNext := by
      simpa [lo, hiNext, x, y, table] using hloidx
    have hhiidx' : hiNext <= 1308 := by
      simpa [hiNext, y, table] using hhiidx
    have hlen' : ds.length = hiNext - lo := by
      simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hlen
    have hdsdigit' : forall z, z ∈ ds -> z < 10 := by
      simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hdsdigit
    have hdsne' : 0 < ds.length := by
      simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hdsne
    have hhead' : 0 < ds.headD 0 := by
      simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hhead
    have hfold' : ds.foldl (fun n z => 10 * n + z) 0 = 5 ^ shift.val := by
      simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hfold
    have hsum' : hi + ds.length = shift.val + 1 := by
      simpa [hi, x, ds, dsU, pow5Table, lo, hiNext, y, table] using hsum
    have hmask : shift &&& 63#usize = shift := by
      apply UScalar.eq_of_val_eq
      rw [UScalar.val_and]
      norm_num
      rw [show 63 = 2 ^ 6 - 1 by decide,
        Nat.and_two_pow_sub_one_of_lt_two_pow (n := 6) (by omega)]
    unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
    simp only [hmask, lift]
    simp only [Bind.bind, Aeneas.Std.bind]
    have htableBound : shift.val <
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE.val.length := by
      simp
      omega
    obtain ⟨xa, hxa, hxaVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (Array.index_usize_spec
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
        shift (by simpa [Array.length] using htableBound))
    rw [hxa]
    simp only [Bind.bind, Aeneas.Std.bind]
    have hxaEq : xa = x := by
      apply UScalar.eq_of_val_eq
      simpa [x, table] using congrArg UScalar.val hxaVal
    subst xa
    rw [hxaEq]
    have hshiftAdd : shift.val + 1 <= UScalar.max .Usize := by scalar_tac
    obtain ⟨next, hnext, hnextVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.add_spec (x := shift) (y := 1#usize)
        (by simpa using hshiftAdd))
    rw [hnext]
    simp only [Bind.bind, Aeneas.Std.bind]
    norm_num at hnextVal
    have hnextBound : next.val <
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE.val.length := by
      simp
      omega
    obtain ⟨yb, hyb, hybVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (Array.index_usize_spec
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
        next (by simpa [Array.length] using hnextBound))
    rw [hyb]
    simp only [Bind.bind, Aeneas.Std.bind]
    have hybEq : yb = y := by
      apply UScalar.eq_of_val_eq
      simpa [y, table, hnextVal] using congrArg UScalar.val hybVal
    subst yb
    rw [hybEq]
    obtain ⟨high16, hhigh16, hhigh16Val⟩ := Aeneas.Std.WP.spec_imp_exists
      (U16.ShiftRight_IScalar_spec x 11#i32 (by norm_num) (by norm_num))
    rw [hhigh16]
    simp only [Bind.bind, Aeneas.Std.bind]
    have hhigh16Eq : high16.val = hi := by
      rw [hhigh16Val.1, Nat.shiftRight_eq_div_pow]
      norm_num [hi, Int.toNat]
    let numNew : Std.Usize := UScalar.cast .Usize high16
    have hnumNewVal : numNew.val = h := by
      simp [numNew, UScalar.cast_val_eq, hhigh16Eq, heq]
    have hnumNewPos : 0 < numNew.val := by omega
    let low16 := 2047#u16 &&& x
    let pow5a : Std.Usize := UScalar.cast .Usize low16
    have hpow5aVal : pow5a.val = lo := by
      simp [pow5a, low16, lo, UScalar.cast_val_eq, UScalar.val_and]
      rw [show 2047 = 2 ^ 11 - 1 by decide, Nat.and_comm,
        Nat.and_two_pow_sub_one_eq_mod]
    let next16 := 2047#u16 &&& y
    let pow5b : Std.Usize := UScalar.cast .Usize next16
    have hpow5bVal : pow5b.val = hiNext := by
      simp [pow5b, next16, hiNext, UScalar.cast_val_eq, UScalar.val_and]
      rw [show 2047 = 2 ^ 11 - 1 by decide, Nat.and_comm,
        Nat.and_two_pow_sub_one_eq_mod]
    rw [show UScalar.cast .Usize (2047#u16 &&& x) = pow5a by rfl]
    rw [show UScalar.cast .Usize (2047#u16 &&& y) = pow5b by rfl]
    rw [show UScalar.cast .Usize high16 = numNew by rfl]
    have hloTable : pow5a.val <= pow5Table.val.length := by
      simp [hpow5aVal, pow5Table]
      omega
    have hlo1308 : pow5a.val <= 1308 := by
      rw [hpow5aVal]
      omega
    let suffix : Slice Std.U8 := pow5Table.to_slice.drop pow5a
    have hsuffix :
        core.array.Array.index
          (core.ops.index.IndexSlice
            (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8))
          pow5Table { start := pow5a } = ok suffix := by
      rw [Array.index_SliceIndexRangeFromUsizeSlice]
      unfold core.slice.index.SliceIndexRangeFromUsizeSlice.index
      simp [hlo1308, suffix]
    rw [hsuffix]
    simp only [Bind.bind, Aeneas.Std.bind]
    unfold core.slice.Slice.iter
    simp only [Bind.bind, Aeneas.Std.bind]
    unfold core.slice.iter.IteratorSliceIter.enumerate
    simp only [Bind.bind, Aeneas.Std.bind]
    have hsubBound : pow5a.val <= pow5b.val := by
      rw [hpow5aVal, hpow5bVal]
      exact hloidx'
    obtain ⟨span, hspan, hspanVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.sub_spec (x := pow5b) (y := pow5a) hsubBound)
    rw [hspan]
    simp only [Bind.bind, Aeneas.Std.bind]
    unfold core.iter.adapters.enumerate.IteratorEnumerate.take
    simp only [Bind.bind, Aeneas.Std.bind]
    have hspanDs : span.val = ds.length := by
      rw [hspanVal.1, hpow5aVal, hpow5bVal]
      omega
    have hell : span.val <= suffix.val.length := by
      simp [suffix, Slice.drop, Array.to_slice, pow5Table, hspanVal.1,
        hpow5aVal, hpow5bVal, List.length_drop]
      omega
    have hdiffBound : hiNext - lo <= 1308 - lo :=
      Nat.sub_le_sub_right hhiidx' lo
    have hseq : (suffix.val.take span.val).map (fun z => z.val) = ds := by
      simp [suffix, Slice.drop, Array.to_slice, hspanDs, ds, dsU,
        pow5Table, hpow5aVal, Nat.min_eq_left hdiffBound]
    have hfive : 5 ^ shift.val % 10 = 5 := by
      have hs : forall q : Nat, 5 ^ (q + 1) % 10 = 5 := by
        intro q
        induction q with
        | zero => decide
        | succ q ih =>
          rw [Nat.pow_succ, Nat.mul_mod, ih]
      obtain ⟨q, hq⟩ := Nat.exists_eq_add_of_le hkpos
      rw [hq]
      simpa [Nat.add_comm] using hs q
    have hdslast : forall hne : ds ≠ [], ds.getLast hne ≠ 0 := by
      intro hne hz
      have hreconstruct := List.dropLast_append_getLast hne
      have hmod := congrArg (fun n : Nat => n % 10) hfold'
      change List.foldl (fun n z => 10 * n + z) 0 ds % 10 =
        5 ^ shift.val % 10 at hmod
      rw [hfive] at hmod
      rw [← hreconstruct] at hmod
      simp [List.foldl_append, hz] at hmod
    let digits := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
    have hdigits : forall z, z ∈ digits -> z < 10 := by
      intro z hz
      simp only [digits, List.mem_map] at hz
      rcases hz with ⟨u, hu, rfl⟩
      exact hdigit u hu
    have hlex := DecimalDigitLexScale digits ds hdigits hdsdigit' hdslast
    let value := digits.foldl (fun a z => 10 * a + z) 0
    have hdigitsLen : digits.length = d.num_digits.val := by
      simp [digits]
      have hdlen : d.digits.val.length = 768 := d.digits.property
      omega
    have hkSplit : shift.val = (h - 1) + ds.length := by
      omega
    have hpowN : 10 ^ (d.num_digits.val + h - 1) =
        10 ^ digits.length * 10 ^ (h - 1) := by
      rw [show d.num_digits.val + h - 1 =
        d.num_digits.val + (h - 1) by omega, Nat.pow_add, hdigitsLen]
    have hpowK : 10 ^ shift.val = 10 ^ (h - 1) * 10 ^ ds.length := by
      rw [hkSplit, Nat.pow_add]
    have htwofive : 2 ^ shift.val * 5 ^ shift.val = 10 ^ shift.val := by
      rw [← Nat.mul_pow]
    have hthreshold :
        10 ^ (d.num_digits.val + h - 1) <= value * 2 ^ shift.val ↔
          5 ^ shift.val * 10 ^ digits.length <= value * 10 ^ ds.length := by
      constructor
      · intro ht
        have hm := Nat.mul_le_mul_left (5 ^ shift.val) ht
        apply Nat.le_of_mul_le_mul_left (c := 10 ^ (h - 1)) ?_ (by positivity)
        calc
          10 ^ (h - 1) * (5 ^ shift.val * 10 ^ digits.length) =
              5 ^ shift.val * 10 ^ (d.num_digits.val + h - 1) := by
                rw [hpowN]
                ring
          _ <= 5 ^ shift.val * (value * 2 ^ shift.val) := hm
          _ = 10 ^ (h - 1) * (value * 10 ^ ds.length) := by
                rw [show 5 ^ shift.val * (value * 2 ^ shift.val) =
                  value * (2 ^ shift.val * 5 ^ shift.val) by ring,
                  htwofive, hpowK]
                ring
      · intro ht
        have hm := Nat.mul_le_mul_left (10 ^ (h - 1)) ht
        apply Nat.le_of_mul_le_mul_left (c := 5 ^ shift.val) ?_ (by positivity)
        calc
          5 ^ shift.val * 10 ^ (d.num_digits.val + h - 1) =
              10 ^ (h - 1) * (5 ^ shift.val * 10 ^ digits.length) := by
                rw [hpowN]
                ring
          _ <= 10 ^ (h - 1) * (value * 10 ^ ds.length) := hm
          _ = 5 ^ shift.val * (value * 2 ^ shift.val) := by
                rw [show 5 ^ shift.val * (value * 2 ^ shift.val) =
                  value * (2 ^ shift.val * 5 ^ shift.val) by ring,
                  htwofive, hpowK]
                ring
    have hlex' : List.Lex (fun a b : Nat => a < b) (digits.take ds.length) ds ↔
        value * 10 ^ ds.length < 5 ^ shift.val * 10 ^ digits.length := by
      simpa [value, hfold'] using hlex
    obtain ⟨out, hout, houtVal⟩ := DecimalLeftShiftLoopLex
      d suffix span numNew hell hnumNewPos hn
    refine ⟨out, hout, ?_⟩
    rw [hseq, hspanDs] at houtVal
    rw [houtVal]
    change (if List.Lex (fun a b : Nat => a < b) (digits.take ds.length) ds
      then numNew.val - 1 else numNew.val) =
      if 10 ^ (d.num_digits.val + h - 1) <= value * 2 ^ shift.val
      then h else h - 1
    by_cases ht : 10 ^ (d.num_digits.val + h - 1) <= value * 2 ^ shift.val
    · have hs := hthreshold.mp ht
      have hnlex : ¬ List.Lex (fun a b : Nat => a < b)
          (digits.take ds.length) ds := by
        intro hl
        have hlt := hlex'.mp hl
        omega
      simp [ht, hnlex, hnumNewVal]
    · have hns : ¬ 5 ^ shift.val * 10 ^ digits.length <=
          value * 10 ^ ds.length := by
        intro hs
        exact ht (hthreshold.mpr hs)
      have hlt : value * 10 ^ ds.length <
          5 ^ shift.val * 10 ^ digits.length := by omega
      have hl := hlex'.mpr hlt
      simp [ht, hl, hnumNewVal]

end dec2flt_model

Proof structure

Follow the declared dependencies to read the supporting statements and proofs.

Explore this dependency tree

Inspect related source (49)
More program dependencies (16)
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": "DecimalLeftShiftDigitGrowth",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "7153166e21d5312372f8071d8ff40068f022258fbfd0e8c750040a53a7fd9906",
  "active_statement_hash": "372457485bbca360df9d83ca146a5a607beefa3e63fbd1e932a35b81552cfc9e",
  "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-154",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalDigitLexScale": "d2942644d8f7d862",
    "DecimalLeftShiftLoopLex": "e280c31921852652",
    "DecimalLeftShiftTableCertificate": "3c85f76c60cebc6c",
    "num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift": "3703c132ca68c95c"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "59bf1ff9fe88fa8d",
    "num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift": "b3efbd0e6b3fe061",
    "num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift_TABLE": "e68bec96fec99700",
    "num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift_TABLE_POW5": "4dda92c1af5f2c9e"
  }
}
Prose assessment
{
  "label": "Sound flag superseded by recorded Lean closure",
  "assessment_status": "ReviewerPinnedFail",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:1223999f4f99cd0b23d792560fa4861f949a1bb73e7ea5d800bd223f38795060",
  "current_fingerprint": "sound-v2:1223999f4f99cd0b23d792560fa4861f949a1bb73e7ea5d800bd223f38795060",
  "superseded_for_theorem_closure": true
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Fail",
  "substantiveness_status": "Pass"
}