NumberHelperDigitScanExact

Lean checked

Statement

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

Lemma · Exact bounded digit scan

Let a byte slice decompose as s=DT, where every byte of D is an ASCII decimal digit and the first byte of T, if present, is not a decimal digit. Assume |s|\le 2^{63}-1. For an initial unsigned word a, define
F(D,a)=\operatorname{foldl}(v,b\mapsto 10v+(b-48),a,D).
If F(D,a)\le 2^{64}-1, then the extracted try_parse_digits returns the suffix T and an unsigned word whose natural value is exactly F(D,a).

Lean statement

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

import Tablet.NumberHelperEightDigitWord
import Tablet.NumberHelperParseEightDigitsExact
import Tablet.NumberHelperReadEightBytes
import Tablet.num_imp_dec2flt_parse_try_parse_digits

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperDigitScanExact]
theorem NumberHelperDigitScanExact :
    forall (s : Slice Std.U8) (initial : Std.U64)
      (digits tail : List Std.U8),
      s.val = digits ++ tail ->
      (forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57) ->
      (forall b rest, tail = b :: rest ->
        Not (48 <= b.val /\ b.val <= 57)) ->
      s.val.length <= 9223372036854775807 ->
      digits.foldl (fun a b => 10 * a + (b.val - 48)) initial.val <=
        U64.max ->
      exists (value : Std.U64) (rest : Slice Std.U8),
        num.imp.dec2flt.parse.try_parse_digits s initial = ok (rest, value) /\
        rest.val = tail /\
        value.val = digits.foldl
          (fun a b => 10 * a + (b.val - 48)) initial.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 num imp dec2flt parse try parse digits. For its eight-byte loop, maintain a partition D=PR, the live slice equation u=RT, and the accumulator equation x=F(P,a). The slice length remains at most 2^{63}-1. When at least eight bytes are available, the extracted read is described by NumberHelperReadEightBytes. If the eight-digit test succeeds, NumberHelperEightDigitWord shows that all eight bytes are digits. If fewer than eight bytes remain in R, one of those tested bytes would instead be the first byte of T, contradicting its nondigit hypothesis. Hence eight bytes of R may be consumed. By NumberHelperParseEightDigitsExact, the parsed block is its exact eight-digit decimal value, so the accumulator update is 10^8F(P,a)+F(R.take(8),0)=F(P(R.take(8)),a).
Every decimal-fold prefix is at most the complete fold: each step maps v to 10v+d\ge v. Thus the hypothesis F(D,a)\le2^{64}-1 bounds both the intermediate multiplication and its following addition, and the extracted wrapping operations agree with ordinary natural arithmetic. Dropping eight bytes strictly decreases the loop measure. If the test fails or fewer than eight bytes are available, the same partition and accumulator invariant are returned unchanged.
For the scalar loop, maintain that its count j is at most |R| and that the accumulator is F(R.take(j),F(P,a)). At j<|R|, the indexed byte is a digit in [48,57]; wrapping subtraction by 48 is therefore ordinary subtraction and yields a value below 10. The cast to 64 bits is exact. The same prefix bound shows that multiplication by 10 and addition of the digit do not wrap, while the checked count increment succeeds because j+1\le |R|\le |s|\le2^{63}-1. The invariant advances to j+1 and the measure |R|-j strictly decreases. At j=|R|, either the slice ends or the next indexed byte is the first byte of T; in the latter case wrapping subtraction is below 10 exactly for ASCII bytes 48 through 57, so the nondigit hypothesis forces the loop to stop without consuming it.
Consequently the scalar loop finishes at count |R| with accumulator F(R,F(P,a))=F(D,a). The final guarded slice drop removes exactly R and leaves T. All reads and drops are in bounds by the maintained partition and source-length bound, proving the stated returned pair.

Lean proof


  intro s initial digits tail hs hdigits htail hlen hfinal
  let digitStep : Nat -> Std.U8 -> Nat :=
    fun a b => 10 * a + (b.val - 48)
  have fold_ge : forall (l : List Std.U8) (a : Nat),
      a <= l.foldl digitStep a := by
    intro l
    induction l with
    | nil => intro a; simp
    | cons b bs ih =>
      intro a
      simp only [List.foldl_cons]
      exact le_trans (by simp [digitStep]; omega) (ih _)
  have fast : forall (t : Slice Std.U8) (a : Std.U64)
      (consumed rem : List Std.U8),
      digits = consumed ++ rem ->
      t.val = rem ++ tail ->
      a.val = consumed.foldl digitStep initial.val ->
      (forall b, List.Mem b rem -> 48 <= b.val /\ b.val <= 57) ->
      t.val.length <= 9223372036854775807 ->
      num.imp.dec2flt.parse.try_parse_digits_loop0 t a
        ⦃ p => exists used remaining : List Std.U8,
          digits = used ++ remaining /\
          p.1.val = remaining ++ tail /\
          p.2.val = used.foldl digitStep initial.val /\
          (forall b, List.Mem b remaining ->
            48 <= b.val /\ b.val <= 57) /\
          p.1.val.length <= 9223372036854775807 ⦄ := by
    intro t a consumed rem hpartition ht hvalue hrem htlen
    unfold num.imp.dec2flt.parse.try_parse_digits_loop0
    apply Aeneas.Std.loop.spec_decr_nat
      (fun p : Slice Std.U8 × Std.U64 => p.1.length)
      (fun p => exists used remaining : List Std.U8,
        digits = used ++ remaining /\
        p.1.val = remaining ++ tail /\
        p.2.val = used.foldl digitStep initial.val /\
        (forall b, List.Mem b remaining ->
          48 <= b.val /\ b.val <= 57) /\
        p.1.val.length <= 9223372036854775807)
      (fun p => exists used remaining : List Std.U8,
        digits = used ++ remaining /\
        p.1.val = remaining ++ tail /\
        p.2.val = used.foldl digitStep initial.val /\
        (forall b, List.Mem b remaining ->
          48 <= b.val /\ b.val <= 57) /\
        p.1.val.length <= 9223372036854775807)
      (fun p => num.imp.dec2flt.parse.try_parse_digits_loop0.body p.1 p.2)
      (t, a)
    · intro p hp
      rcases p with ⟨u, x⟩
      rcases hp with
        ⟨used, remaining, hpart, hurem, hx, hremaining, hulen⟩
      change u.val = remaining ++ tail at hurem
      change u.val.length <= 9223372036854775807 at hulen
      simp [num.imp.dec2flt.parse.try_parse_digits_loop0.body, WP.spec_ok,
        Slice.length]
      split
      · rename_i h8
        obtain ⟨chunk, hread, hbyte⟩ := NumberHelperReadEightBytes u h8
        rw [hread]
        simp [num.imp.dec2flt.common.is_8digits, Aeneas.Std.lift]
        split
        · rename_i hacceptedRaw
          have haccepted :
              num.imp.dec2flt.common.is_8digits chunk = ok true := by
            simp [num.imp.dec2flt.common.is_8digits, Aeneas.Std.lift,
              hacceptedRaw]
          have hrem8 : 8 <= remaining.length := by
            by_contra hn
            have hrem_lt : remaining.length < 8 := by omega
            have htail_pos : 0 < tail.length := by
              by_contra hzero
              have ht0 : tail = [] := List.eq_nil_of_length_eq_zero (by omega)
              have heq := congrArg List.length hurem
              simp [ht0] at heq
              omega
            obtain ⟨b, rest, htail_eq⟩ :=
              List.exists_cons_of_length_pos htail_pos
            have hindex : remaining.length < u.val.length := by omega
            have hbyte_eq := hbyte remaining.length hrem_lt
            have hword_digit :=
              NumberHelperEightDigitWord chunk haccepted remaining.length
                hrem_lt
            have hu_digit :
                48 <= (u.val[remaining.length]'hindex).val /\
                  (u.val[remaining.length]'hindex).val <= 57 := by
              omega
            have hu_eq : (u.val[remaining.length]'hindex) = b := by
              simp [hurem, htail_eq]
            rw [hu_eq] at hu_digit
            exact (htail b rest htail_eq) hu_digit
          obtain ⟨z, hparse, hz⟩ :=
            NumberHelperParseEightDigitsExact chunk haccepted
          have hprefixShape : exists b0 b1 b2 b3 b4 b5 b6 b7 rest,
              remaining = b0 :: b1 :: b2 :: b3 :: b4 :: b5 :: b6 ::
                b7 :: rest := by
            cases remaining with
            | nil => simp at hrem8
            | cons b0 r0 =>
              cases r0 with
              | nil => simp at hrem8
              | cons b1 r1 =>
                cases r1 with
                | nil => simp at hrem8
                | cons b2 r2 =>
                  cases r2 with
                  | nil => simp at hrem8
                  | cons b3 r3 =>
                    cases r3 with
                    | nil => simp at hrem8
                    | cons b4 r4 =>
                      cases r4 with
                      | nil => simp at hrem8
                      | cons b5 r5 =>
                        cases r5 with
                        | nil => simp at hrem8
                        | cons b6 r6 =>
                          cases r6 with
                          | nil => simp at hrem8
                          | cons b7 rest =>
                            exact ⟨b0, b1, b2, b3, b4, b5, b6, b7,
                              rest, rfl⟩
          obtain ⟨b0, b1, b2, b3, b4, b5, b6, b7, more, hshape⟩ :=
            hprefixShape
          have hb0 := hbyte 0 (by omega)
          have hb1 := hbyte 1 (by omega)
          have hb2 := hbyte 2 (by omega)
          have hb3 := hbyte 3 (by omega)
          have hb4 := hbyte 4 (by omega)
          have hb5 := hbyte 5 (by omega)
          have hb6 := hbyte 6 (by omega)
          have hb7 := hbyte 7 (by omega)
          rw [hshape] at hurem
          simp only [hurem, List.append_assoc, List.cons_append,
            List.getElem_cons_zero, List.getElem_cons_succ] at hb0 hb1 hb2 hb3 hb4 hb5 hb6 hb7
          have hblock :
              ([b0, b1, b2, b3, b4, b5, b6, b7] : List Std.U8).foldl
                  digitStep x.val =
                100000000 * x.val + z.val := by
            rw [hz]
            simp only [hb0, hb1, hb2, hb3, hb4, hb5, hb6, hb7]
            simp [digitStep]
            omega
          let used' := used ++ remaining.take 8
          let remaining' := remaining.drop 8
          have hpart' : digits = used' ++ remaining' := by
            simp only [used', remaining', hpart, List.append_assoc]
            rw [List.take_append_drop]
          have husedValue : used'.foldl digitStep initial.val =
              100000000 * x.val + z.val := by
            simp only [used', List.foldl_append]
            rw [← hx]
            rw [hshape]
            norm_num [hblock]
          have hprefixBound : used'.foldl digitStep initial.val <= U64.max := by
            have hrestGe := fold_ge remaining'
              (used'.foldl digitStep initial.val)
            rw [← List.foldl_append, ← hpart'] at hrestGe
            exact le_trans hrestGe hfinal
          let next := core.num.U64.wrapping_add
            (core.num.U64.wrapping_mul x 100000000#u64) z
          have hnext : next.val = used'.foldl digitStep initial.val := by
            rw [husedValue]
            simp only [next, core.num.U64.wrapping_add_val_eq,
              core.num.U64.wrapping_mul_val_eq]
            simp [UScalar.size, U64.size_eq]
            have hsum : x.val * 100000000 + z.val <
                18446744073709551616 := by
              rw [husedValue] at hprefixBound
              rw [U64.max_eq] at hprefixBound
              omega
            rw [Nat.mod_eq_of_lt hsum]
            omega
          rw [hparse]
          simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
            Slice.drop, Slice.length, WP.spec_ok, h8, next]
          constructor
          · refine ⟨used', remaining', hpart', ?_, ?_, ?_, ?_⟩
            · simp [remaining', hurem, hshape]
            · simpa [next, core.num.U64.wrapping_add_val_eq,
                core.num.U64.wrapping_mul_val_eq, U64.size_eq] using hnext
            · intro b hb
              exact hremaining b (List.mem_of_mem_drop hb)
            · omega
          · omega
        · simpa [WP.spec_ok] using
            (show exists used remaining : List Std.U8,
              digits = used ++ remaining /\
              u.val = remaining ++ tail /\
              x.val = used.foldl digitStep initial.val /\
              (forall b, List.Mem b remaining ->
                48 <= b.val /\ b.val <= 57) /\
              u.val.length <= 9223372036854775807 from
                ⟨used, remaining, hpart, hurem, hx, hremaining, hulen⟩)
      · simpa [WP.spec_ok] using
          (show exists used remaining : List Std.U8,
            digits = used ++ remaining /\
            u.val = remaining ++ tail /\
            x.val = used.foldl digitStep initial.val /\
            (forall b, List.Mem b remaining ->
              48 <= b.val /\ b.val <= 57) /\
            u.val.length <= 9223372036854775807 from
              ⟨used, remaining, hpart, hurem, hx, hremaining, hulen⟩)
    · simpa using
        (show exists used remaining : List Std.U8,
          digits = used ++ remaining /\
          t.val = remaining ++ tail /\
          a.val = used.foldl digitStep initial.val /\
          (forall b, List.Mem b remaining ->
            48 <= b.val /\ b.val <= 57) /\
          t.val.length <= 9223372036854775807 from
            ⟨consumed, rem, hpartition, ht, hvalue, hrem, htlen⟩)
  have scalar : forall (t : Slice Std.U8) (a : Std.U64)
      (used rem : List Std.U8),
      digits = used ++ rem ->
      t.val = rem ++ tail ->
      a.val = used.foldl digitStep initial.val ->
      (forall b, List.Mem b rem -> 48 <= b.val /\ b.val <= 57) ->
      t.val.length <= 9223372036854775807 ->
      num.imp.dec2flt.parse.try_parse_digits_loop1 t a 0#usize
        ⦃ p => p.2.val = rem.length /\
          p.1.val = digits.foldl digitStep initial.val ⦄ := by
    intro t a used rem hpart ht ha hrem htlen
    unfold num.imp.dec2flt.parse.try_parse_digits_loop1
    apply Aeneas.Std.loop.spec_decr_nat
      (fun p : Std.U64 × Std.Usize => rem.length - p.2.val)
      (fun p => p.2.val <= rem.length /\
        p.1.val = (rem.take p.2.val).foldl digitStep a.val)
      (fun p => p.2.val = rem.length /\
        p.1.val = digits.foldl digitStep initial.val)
      (fun p =>
        num.imp.dec2flt.parse.try_parse_digits_loop1.body t p.1 p.2)
      (a, 0#usize)
    · intro p hp
      rcases p with ⟨x, count⟩
      rcases hp with ⟨hcount, hx⟩
      change count.val <= rem.length at hcount
      simp [num.imp.dec2flt.parse.try_parse_digits_loop1.body, WP.spec_ok,
        Slice.length]
      split
      · rename_i hcount_t
        step
        by_cases hinside : count.val < rem.length
        · have hi_eq : i1 = rem[count.val]'hinside := by
            rw [i1_post]
            simp [ht, hinside]
          have hi_mem : List.Mem i1 rem := by
            rw [hi_eq]
            exact List.getElem_mem _
          have hi_digit := hrem i1 hi_mem
          step
          have hi2_val : i2.val = i1.val - 48 := by
            rw [i2_post]
            rw [core.num.U8.wrapping_sub_val_eq]
            simp only [UScalar.size_UScalarTyU8, U8.size_eq]
            norm_num
            omega
          have hi2_lt : i2.val < 10 := by omega
          simp [hi2_lt]
          step
          step
          step
          step
          step
          have hnextCount : count1.val = count.val + 1 := by
            simpa using count1_post
          have hdigitVal : digit.val = i1.val - 48 := by
            rw [digit_post]
            rw [core.num.U8.wrapping_sub_val_eq]
            simp only [UScalar.size_UScalarTyU8, U8.size_eq]
            norm_num
            omega
          have hi4Val : i4.val = i1.val - 48 := by
            have h := congrArg UScalar.val i4_post
            simpa [hdigitVal] using h
          have hprefix :
              (rem.take (count.val + 1)).foldl digitStep a.val =
                10 * x.val + (i1.val - 48) := by
            rw [List.take_succ_eq_append_getElem hinside, List.foldl_append,
              List.foldl_cons, List.foldl_nil, ← hi_eq, hx]
          have hprefixBound :
              (rem.take (count.val + 1)).foldl digitStep a.val <=
                U64.max := by
            have hdropGe := fold_ge (rem.drop (count.val + 1))
              ((rem.take (count.val + 1)).foldl digitStep a.val)
            rw [← List.foldl_append, List.take_append_drop] at hdropGe
            have hfull : rem.foldl digitStep a.val =
                digits.foldl digitStep initial.val := by
              rw [hpart, List.foldl_append, ← ha]
            rw [hfull] at hdropGe
            exact le_trans hdropGe hfinal
          have hi3Val : i3.val = 10 * x.val := by
            rw [i3_post, core.num.U64.wrapping_mul_val_eq]
            simp [UScalar.size, U64.size_eq]
            rw [hprefix] at hprefixBound
            rw [U64.max_eq] at hprefixBound
            rw [Nat.mod_eq_of_lt (by omega)]
            omega
          have hx1Val : x1.val =
              (rem.take (count.val + 1)).foldl digitStep a.val := by
            rw [x1_post, core.num.U64.wrapping_add_val_eq]
            simp [UScalar.size, U64.size_eq, hi3Val, hi4Val]
            rw [hprefix] at hprefixBound ⊢
            rw [U64.max_eq] at hprefixBound
            rw [Nat.mod_eq_of_lt (by omega)]
          constructor
          · rw [hnextCount]
            omega
          · constructor
            · rw [hnextCount, hx1Val]
            · rw [hnextCount]
              omega
        · have hcount_eq : count.val = rem.length := by omega
          have htail_pos : 0 < tail.length := by
            have hlen_eq := congrArg List.length ht
            simp at hlen_eq
            omega
          obtain ⟨b, rest, htail_eq⟩ :=
            List.exists_cons_of_length_pos htail_pos
          have hi_eq : i1 = b := by
            rw [i1_post]
            simp [ht, htail_eq, hcount_eq]
          have hb_not := htail b rest htail_eq
          step
          have hi2_not : Not (i2.val < 10) := by
            rw [i2_post, hi_eq]
            rw [core.num.U8.wrapping_sub_val_eq]
            simp only [UScalar.size_UScalarTyU8, U8.size_eq]
            norm_num
            scalar_tac
          simp [hi2_not, WP.spec_ok, hcount_eq]
          rw [hpart, List.foldl_append, ← ha]
          simpa [hcount_eq] using hx
      · rename_i hcount_not
        simp [WP.spec_ok]
        have hlen_eq := congrArg List.length ht
        simp at hlen_eq
        have hcount_eq : count.val = rem.length := by omega
        constructor
        · exact hcount_eq
        · rw [hpart, List.foldl_append, ← ha]
          simpa [hcount_eq] using hx
    · simp
  unfold num.imp.dec2flt.parse.try_parse_digits
  have hf := fast s initial [] digits (by simp) hs (by simp)
    hdigits hlen
  obtain ⟨p, hfast, hfp⟩ := Aeneas.Std.WP.spec_imp_exists hf
  rcases p with ⟨s1, x1⟩
  rcases hfp with
    ⟨used, rem, hpart, hs1, hx1, hrem, hs1len⟩
  change s1.val = rem ++ tail at hs1
  change s1.val.length <= 9223372036854775807 at hs1len
  have hsc := scalar s1 x1 used rem hpart hs1 hx1 hrem hs1len
  obtain ⟨q, hscalar, hq⟩ := Aeneas.Std.WP.spec_imp_exists hsc
  rcases q with ⟨x2, count⟩
  rcases hq with ⟨hcount, hx2⟩
  change count.val = rem.length at hcount
  have hcount_le : count.val <= s1.val.length := by
    rw [hcount, hs1]
    simp
  simp [hfast, hscalar]
  simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
    Slice.drop, Slice.length, hcount_le]
  constructor
  · rw [hcount, hs1, List.drop_append_of_le_length (Nat.le_refl _)]
    simp
  · exact hx2


end dec2flt_model


Download Lean source · Download TeX source

Complete Lean module
import Tablet.NumberHelperEightDigitWord
import Tablet.NumberHelperParseEightDigitsExact
import Tablet.NumberHelperReadEightBytes
import Tablet.num_imp_dec2flt_parse_try_parse_digits

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperDigitScanExact]
theorem NumberHelperDigitScanExact :
    forall (s : Slice Std.U8) (initial : Std.U64)
      (digits tail : List Std.U8),
      s.val = digits ++ tail ->
      (forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57) ->
      (forall b rest, tail = b :: rest ->
        Not (48 <= b.val /\ b.val <= 57)) ->
      s.val.length <= 9223372036854775807 ->
      digits.foldl (fun a b => 10 * a + (b.val - 48)) initial.val <=
        U64.max ->
      exists (value : Std.U64) (rest : Slice Std.U8),
        num.imp.dec2flt.parse.try_parse_digits s initial = ok (rest, value) /\
        rest.val = tail /\
        value.val = digits.foldl
          (fun a b => 10 * a + (b.val - 48)) initial.val := by
-- BODY
  intro s initial digits tail hs hdigits htail hlen hfinal
  let digitStep : Nat -> Std.U8 -> Nat :=
    fun a b => 10 * a + (b.val - 48)
  have fold_ge : forall (l : List Std.U8) (a : Nat),
      a <= l.foldl digitStep a := by
    intro l
    induction l with
    | nil => intro a; simp
    | cons b bs ih =>
      intro a
      simp only [List.foldl_cons]
      exact le_trans (by simp [digitStep]; omega) (ih _)
  have fast : forall (t : Slice Std.U8) (a : Std.U64)
      (consumed rem : List Std.U8),
      digits = consumed ++ rem ->
      t.val = rem ++ tail ->
      a.val = consumed.foldl digitStep initial.val ->
      (forall b, List.Mem b rem -> 48 <= b.val /\ b.val <= 57) ->
      t.val.length <= 9223372036854775807 ->
      num.imp.dec2flt.parse.try_parse_digits_loop0 t a
        ⦃ p => exists used remaining : List Std.U8,
          digits = used ++ remaining /\
          p.1.val = remaining ++ tail /\
          p.2.val = used.foldl digitStep initial.val /\
          (forall b, List.Mem b remaining ->
            48 <= b.val /\ b.val <= 57) /\
          p.1.val.length <= 9223372036854775807 ⦄ := by
    intro t a consumed rem hpartition ht hvalue hrem htlen
    unfold num.imp.dec2flt.parse.try_parse_digits_loop0
    apply Aeneas.Std.loop.spec_decr_nat
      (fun p : Slice Std.U8 × Std.U64 => p.1.length)
      (fun p => exists used remaining : List Std.U8,
        digits = used ++ remaining /\
        p.1.val = remaining ++ tail /\
        p.2.val = used.foldl digitStep initial.val /\
        (forall b, List.Mem b remaining ->
          48 <= b.val /\ b.val <= 57) /\
        p.1.val.length <= 9223372036854775807)
      (fun p => exists used remaining : List Std.U8,
        digits = used ++ remaining /\
        p.1.val = remaining ++ tail /\
        p.2.val = used.foldl digitStep initial.val /\
        (forall b, List.Mem b remaining ->
          48 <= b.val /\ b.val <= 57) /\
        p.1.val.length <= 9223372036854775807)
      (fun p => num.imp.dec2flt.parse.try_parse_digits_loop0.body p.1 p.2)
      (t, a)
    · intro p hp
      rcases p with ⟨u, x⟩
      rcases hp with
        ⟨used, remaining, hpart, hurem, hx, hremaining, hulen⟩
      change u.val = remaining ++ tail at hurem
      change u.val.length <= 9223372036854775807 at hulen
      simp [num.imp.dec2flt.parse.try_parse_digits_loop0.body, WP.spec_ok,
        Slice.length]
      split
      · rename_i h8
        obtain ⟨chunk, hread, hbyte⟩ := NumberHelperReadEightBytes u h8
        rw [hread]
        simp [num.imp.dec2flt.common.is_8digits, Aeneas.Std.lift]
        split
        · rename_i hacceptedRaw
          have haccepted :
              num.imp.dec2flt.common.is_8digits chunk = ok true := by
            simp [num.imp.dec2flt.common.is_8digits, Aeneas.Std.lift,
              hacceptedRaw]
          have hrem8 : 8 <= remaining.length := by
            by_contra hn
            have hrem_lt : remaining.length < 8 := by omega
            have htail_pos : 0 < tail.length := by
              by_contra hzero
              have ht0 : tail = [] := List.eq_nil_of_length_eq_zero (by omega)
              have heq := congrArg List.length hurem
              simp [ht0] at heq
              omega
            obtain ⟨b, rest, htail_eq⟩ :=
              List.exists_cons_of_length_pos htail_pos
            have hindex : remaining.length < u.val.length := by omega
            have hbyte_eq := hbyte remaining.length hrem_lt
            have hword_digit :=
              NumberHelperEightDigitWord chunk haccepted remaining.length
                hrem_lt
            have hu_digit :
                48 <= (u.val[remaining.length]'hindex).val /\
                  (u.val[remaining.length]'hindex).val <= 57 := by
              omega
            have hu_eq : (u.val[remaining.length]'hindex) = b := by
              simp [hurem, htail_eq]
            rw [hu_eq] at hu_digit
            exact (htail b rest htail_eq) hu_digit
          obtain ⟨z, hparse, hz⟩ :=
            NumberHelperParseEightDigitsExact chunk haccepted
          have hprefixShape : exists b0 b1 b2 b3 b4 b5 b6 b7 rest,
              remaining = b0 :: b1 :: b2 :: b3 :: b4 :: b5 :: b6 ::
                b7 :: rest := by
            cases remaining with
            | nil => simp at hrem8
            | cons b0 r0 =>
              cases r0 with
              | nil => simp at hrem8
              | cons b1 r1 =>
                cases r1 with
                | nil => simp at hrem8
                | cons b2 r2 =>
                  cases r2 with
                  | nil => simp at hrem8
                  | cons b3 r3 =>
                    cases r3 with
                    | nil => simp at hrem8
                    | cons b4 r4 =>
                      cases r4 with
                      | nil => simp at hrem8
                      | cons b5 r5 =>
                        cases r5 with
                        | nil => simp at hrem8
                        | cons b6 r6 =>
                          cases r6 with
                          | nil => simp at hrem8
                          | cons b7 rest =>
                            exact ⟨b0, b1, b2, b3, b4, b5, b6, b7,
                              rest, rfl⟩
          obtain ⟨b0, b1, b2, b3, b4, b5, b6, b7, more, hshape⟩ :=
            hprefixShape
          have hb0 := hbyte 0 (by omega)
          have hb1 := hbyte 1 (by omega)
          have hb2 := hbyte 2 (by omega)
          have hb3 := hbyte 3 (by omega)
          have hb4 := hbyte 4 (by omega)
          have hb5 := hbyte 5 (by omega)
          have hb6 := hbyte 6 (by omega)
          have hb7 := hbyte 7 (by omega)
          rw [hshape] at hurem
          simp only [hurem, List.append_assoc, List.cons_append,
            List.getElem_cons_zero, List.getElem_cons_succ] at hb0 hb1 hb2 hb3 hb4 hb5 hb6 hb7
          have hblock :
              ([b0, b1, b2, b3, b4, b5, b6, b7] : List Std.U8).foldl
                  digitStep x.val =
                100000000 * x.val + z.val := by
            rw [hz]
            simp only [hb0, hb1, hb2, hb3, hb4, hb5, hb6, hb7]
            simp [digitStep]
            omega
          let used' := used ++ remaining.take 8
          let remaining' := remaining.drop 8
          have hpart' : digits = used' ++ remaining' := by
            simp only [used', remaining', hpart, List.append_assoc]
            rw [List.take_append_drop]
          have husedValue : used'.foldl digitStep initial.val =
              100000000 * x.val + z.val := by
            simp only [used', List.foldl_append]
            rw [← hx]
            rw [hshape]
            norm_num [hblock]
          have hprefixBound : used'.foldl digitStep initial.val <= U64.max := by
            have hrestGe := fold_ge remaining'
              (used'.foldl digitStep initial.val)
            rw [← List.foldl_append, ← hpart'] at hrestGe
            exact le_trans hrestGe hfinal
          let next := core.num.U64.wrapping_add
            (core.num.U64.wrapping_mul x 100000000#u64) z
          have hnext : next.val = used'.foldl digitStep initial.val := by
            rw [husedValue]
            simp only [next, core.num.U64.wrapping_add_val_eq,
              core.num.U64.wrapping_mul_val_eq]
            simp [UScalar.size, U64.size_eq]
            have hsum : x.val * 100000000 + z.val <
                18446744073709551616 := by
              rw [husedValue] at hprefixBound
              rw [U64.max_eq] at hprefixBound
              omega
            rw [Nat.mod_eq_of_lt hsum]
            omega
          rw [hparse]
          simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
            Slice.drop, Slice.length, WP.spec_ok, h8, next]
          constructor
          · refine ⟨used', remaining', hpart', ?_, ?_, ?_, ?_⟩
            · simp [remaining', hurem, hshape]
            · simpa [next, core.num.U64.wrapping_add_val_eq,
                core.num.U64.wrapping_mul_val_eq, U64.size_eq] using hnext
            · intro b hb
              exact hremaining b (List.mem_of_mem_drop hb)
            · omega
          · omega
        · simpa [WP.spec_ok] using
            (show exists used remaining : List Std.U8,
              digits = used ++ remaining /\
              u.val = remaining ++ tail /\
              x.val = used.foldl digitStep initial.val /\
              (forall b, List.Mem b remaining ->
                48 <= b.val /\ b.val <= 57) /\
              u.val.length <= 9223372036854775807 from
                ⟨used, remaining, hpart, hurem, hx, hremaining, hulen⟩)
      · simpa [WP.spec_ok] using
          (show exists used remaining : List Std.U8,
            digits = used ++ remaining /\
            u.val = remaining ++ tail /\
            x.val = used.foldl digitStep initial.val /\
            (forall b, List.Mem b remaining ->
              48 <= b.val /\ b.val <= 57) /\
            u.val.length <= 9223372036854775807 from
              ⟨used, remaining, hpart, hurem, hx, hremaining, hulen⟩)
    · simpa using
        (show exists used remaining : List Std.U8,
          digits = used ++ remaining /\
          t.val = remaining ++ tail /\
          a.val = used.foldl digitStep initial.val /\
          (forall b, List.Mem b remaining ->
            48 <= b.val /\ b.val <= 57) /\
          t.val.length <= 9223372036854775807 from
            ⟨consumed, rem, hpartition, ht, hvalue, hrem, htlen⟩)
  have scalar : forall (t : Slice Std.U8) (a : Std.U64)
      (used rem : List Std.U8),
      digits = used ++ rem ->
      t.val = rem ++ tail ->
      a.val = used.foldl digitStep initial.val ->
      (forall b, List.Mem b rem -> 48 <= b.val /\ b.val <= 57) ->
      t.val.length <= 9223372036854775807 ->
      num.imp.dec2flt.parse.try_parse_digits_loop1 t a 0#usize
        ⦃ p => p.2.val = rem.length /\
          p.1.val = digits.foldl digitStep initial.val ⦄ := by
    intro t a used rem hpart ht ha hrem htlen
    unfold num.imp.dec2flt.parse.try_parse_digits_loop1
    apply Aeneas.Std.loop.spec_decr_nat
      (fun p : Std.U64 × Std.Usize => rem.length - p.2.val)
      (fun p => p.2.val <= rem.length /\
        p.1.val = (rem.take p.2.val).foldl digitStep a.val)
      (fun p => p.2.val = rem.length /\
        p.1.val = digits.foldl digitStep initial.val)
      (fun p =>
        num.imp.dec2flt.parse.try_parse_digits_loop1.body t p.1 p.2)
      (a, 0#usize)
    · intro p hp
      rcases p with ⟨x, count⟩
      rcases hp with ⟨hcount, hx⟩
      change count.val <= rem.length at hcount
      simp [num.imp.dec2flt.parse.try_parse_digits_loop1.body, WP.spec_ok,
        Slice.length]
      split
      · rename_i hcount_t
        step
        by_cases hinside : count.val < rem.length
        · have hi_eq : i1 = rem[count.val]'hinside := by
            rw [i1_post]
            simp [ht, hinside]
          have hi_mem : List.Mem i1 rem := by
            rw [hi_eq]
            exact List.getElem_mem _
          have hi_digit := hrem i1 hi_mem
          step
          have hi2_val : i2.val = i1.val - 48 := by
            rw [i2_post]
            rw [core.num.U8.wrapping_sub_val_eq]
            simp only [UScalar.size_UScalarTyU8, U8.size_eq]
            norm_num
            omega
          have hi2_lt : i2.val < 10 := by omega
          simp [hi2_lt]
          step
          step
          step
          step
          step
          have hnextCount : count1.val = count.val + 1 := by
            simpa using count1_post
          have hdigitVal : digit.val = i1.val - 48 := by
            rw [digit_post]
            rw [core.num.U8.wrapping_sub_val_eq]
            simp only [UScalar.size_UScalarTyU8, U8.size_eq]
            norm_num
            omega
          have hi4Val : i4.val = i1.val - 48 := by
            have h := congrArg UScalar.val i4_post
            simpa [hdigitVal] using h
          have hprefix :
              (rem.take (count.val + 1)).foldl digitStep a.val =
                10 * x.val + (i1.val - 48) := by
            rw [List.take_succ_eq_append_getElem hinside, List.foldl_append,
              List.foldl_cons, List.foldl_nil, ← hi_eq, hx]
          have hprefixBound :
              (rem.take (count.val + 1)).foldl digitStep a.val <=
                U64.max := by
            have hdropGe := fold_ge (rem.drop (count.val + 1))
              ((rem.take (count.val + 1)).foldl digitStep a.val)
            rw [← List.foldl_append, List.take_append_drop] at hdropGe
            have hfull : rem.foldl digitStep a.val =
                digits.foldl digitStep initial.val := by
              rw [hpart, List.foldl_append, ← ha]
            rw [hfull] at hdropGe
            exact le_trans hdropGe hfinal
          have hi3Val : i3.val = 10 * x.val := by
            rw [i3_post, core.num.U64.wrapping_mul_val_eq]
            simp [UScalar.size, U64.size_eq]
            rw [hprefix] at hprefixBound
            rw [U64.max_eq] at hprefixBound
            rw [Nat.mod_eq_of_lt (by omega)]
            omega
          have hx1Val : x1.val =
              (rem.take (count.val + 1)).foldl digitStep a.val := by
            rw [x1_post, core.num.U64.wrapping_add_val_eq]
            simp [UScalar.size, U64.size_eq, hi3Val, hi4Val]
            rw [hprefix] at hprefixBound ⊢
            rw [U64.max_eq] at hprefixBound
            rw [Nat.mod_eq_of_lt (by omega)]
          constructor
          · rw [hnextCount]
            omega
          · constructor
            · rw [hnextCount, hx1Val]
            · rw [hnextCount]
              omega
        · have hcount_eq : count.val = rem.length := by omega
          have htail_pos : 0 < tail.length := by
            have hlen_eq := congrArg List.length ht
            simp at hlen_eq
            omega
          obtain ⟨b, rest, htail_eq⟩ :=
            List.exists_cons_of_length_pos htail_pos
          have hi_eq : i1 = b := by
            rw [i1_post]
            simp [ht, htail_eq, hcount_eq]
          have hb_not := htail b rest htail_eq
          step
          have hi2_not : Not (i2.val < 10) := by
            rw [i2_post, hi_eq]
            rw [core.num.U8.wrapping_sub_val_eq]
            simp only [UScalar.size_UScalarTyU8, U8.size_eq]
            norm_num
            scalar_tac
          simp [hi2_not, WP.spec_ok, hcount_eq]
          rw [hpart, List.foldl_append, ← ha]
          simpa [hcount_eq] using hx
      · rename_i hcount_not
        simp [WP.spec_ok]
        have hlen_eq := congrArg List.length ht
        simp at hlen_eq
        have hcount_eq : count.val = rem.length := by omega
        constructor
        · exact hcount_eq
        · rw [hpart, List.foldl_append, ← ha]
          simpa [hcount_eq] using hx
    · simp
  unfold num.imp.dec2flt.parse.try_parse_digits
  have hf := fast s initial [] digits (by simp) hs (by simp)
    hdigits hlen
  obtain ⟨p, hfast, hfp⟩ := Aeneas.Std.WP.spec_imp_exists hf
  rcases p with ⟨s1, x1⟩
  rcases hfp with
    ⟨used, rem, hpart, hs1, hx1, hrem, hs1len⟩
  change s1.val = rem ++ tail at hs1
  change s1.val.length <= 9223372036854775807 at hs1len
  have hsc := scalar s1 x1 used rem hpart hs1 hx1 hrem hs1len
  obtain ⟨q, hscalar, hq⟩ := Aeneas.Std.WP.spec_imp_exists hsc
  rcases q with ⟨x2, count⟩
  rcases hq with ⟨hcount, hx2⟩
  change count.val = rem.length at hcount
  have hcount_le : count.val <= s1.val.length := by
    rw [hcount, hs1]
    simp
  simp [hfast, hscalar]
  simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
    Slice.drop, Slice.length, hcount_le]
  constructor
  · rw [hcount, hs1, List.drop_append_of_le_length (Nat.le_refl _)]
    simp
  · exact hx2


end dec2flt_model


Proof structure

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

Explore this dependency tree

Inspect related source (55)
More program dependencies (22)
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 11 entries
Technical checking records
Local closure summary
{
  "node": "NumberHelperDigitScanExact",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "b4813791708985eccea8591b89e1d33c74c4af940c7b32171bd1ce70c3f56687",
  "active_statement_hash": "0b154f5da52ff3e4e9122a17b9b02427e7af2852e11472bc4b0c38f56f85729a",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "d5c04ffa333d9b33b238a527f5beaa2be82e021d3e9c0bbe01b1310280f7f55a",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-68",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "NumberHelperEightDigitWord": "a61cba9606484373",
    "NumberHelperParseEightDigitsExact": "4b20c98a383aae52",
    "NumberHelperReadEightBytes": "8738e3b807b9b2d9",
    "num_imp_dec2flt_common_is_8digits": "effb8a26907859a5",
    "num_imp_dec2flt_parse_try_parse_digits": "bcad6df678c024ae"
  },
  "strict_definition_deps": {
    "SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_read_u64": "279b0c1cd8ec9e39",
    "num_imp_dec2flt_common_is_8digits": "53e9de5212d69bf5",
    "num_imp_dec2flt_parse_parse_8digits": "ae610d45d88851c6",
    "num_imp_dec2flt_parse_try_parse_digits": "2b12615e16e91640"
  }
}
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:1a3d82431b686de977e09a0e26efdc294b8bbc49ed7aa96c15031f47db5b7ffb",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}