NumberHelperRecordBounds

Lean checked

Statement

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

Theorem

For every source-valid nonempty Rust byte slice in the sense of SourceValidByteSlice, if parse_number returns normally with Some record d, then d.\mathit{negative}=\texttt{false} and d.\mathit{mantissa}<10^{19}.

Lean statement

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

import Tablet.Assumptions
import Tablet.ModelOptionBranch
import Tablet.ModelOptionFromResidual
import Tablet.ModelSliceSplitFirst
import Tablet.NumberHelperAdjustManyExact
import Tablet.NumberHelperDigitScanExact
import Tablet.NumberHelperDigitScanSourceLength
import Tablet.NumberHelperOffsetSourceLength
import Tablet.NumberHelperTryParse19SafeLength
import Tablet.SourceValidByteSlice
import Tablet.U64_Insts_CoreOpsBitBitAndU64U64
import Tablet.U64_Insts_CoreOpsBitBitOrU64U64
import Tablet.num_imp_dec2flt_decimal_Decimal
import Tablet.num_imp_dec2flt_parse_parse_many_digits
import Tablet.num_imp_dec2flt_parse_parse_number
import Tablet.num_imp_dec2flt_parse_parse_partial_number
import Tablet.num_imp_dec2flt_parse_parse_scientific

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperRecordBounds]
theorem NumberHelperRecordBounds :
    ∀ (s : Slice Std.U8) d, SourceValidByteSlice s -> s.val ≠ [] ->
      num.imp.dec2flt.parse.parse_number s = ok (some d) ->
      d.negative = false ∧ d.mantissa.val < 10 ^ 19 := 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

The strict endpoint required by GOAL.md is 10^{19}. Put L=|s|. By SourceValidByteSlice, L\leq2^{63}-1, and Assumptions fixes both modeled pointer-sized widths at 64 bits; the nonemptiness premise also discharges the initial nonempty assertion in the partial parser. First unfold num imp dec2flt parse parse number. Its only route to ok (some d) is that num imp dec2flt parse parse partial number returned ok (some (d,\ell)) and that the final test established \ell=L. The former definition constructs d, and the latter definition only tests \ell and returns the same record. It therefore suffices to bound every record literal in parse_partial_number.
We first establish the exact invariant of the digit scanner num imp dec2flt parse try parse digits. This requires checking its eight-byte decoder rather than assuming that its wrapping accumulator denotes base-ten arithmetic.
Suppose the eight-byte loop is on its continuing branch. Its length guard is 8\leq|t|. Thus the prefix operation in SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice read u64 is in bounds, the source and destination of copy_from_slice both have length eight, and from_le_bytes returns
v=\sum_{j=0}^{7} b_j2^{8j}
for the next bytes b_0,\ldots,b_7 in source order. The loop later takes the suffix at index eight under the same guard, so it removes exactly these bytes.
The test num imp dec2flt common is 8digits has constants 0x4646464646464646, 0x3030303030303030, and 0x8080808080808080; its conjunction and disjunction have the bounded word meanings in U64 Insts CoreOpsBitBitAndU64U64, U64 Insts CoreOpsBitBitOrU64U64, ModelU64BitAnd, and ModelU64BitOr. To rule out false positives, let c_j be the carry into byte j of the wrapping addition by 0x46 in every byte, and let r_j be the borrow into byte j of the wrapping subtraction by 0x30 in every byte. Both initial values are zero. Acceptance says that every result byte of both words has zero high bit, because the high-bit mask of their OR is zero. Inducting from the low byte in the subtraction, if r_j=0 and b_j<48, its result is b_j-48+256\in[208,255], a contradiction. Hence b_j\geq48, the outgoing borrow is zero, and the zero high bit of b_j-48 also gives b_j\leq175. Inducting through the addition next, if c_j=0 and b_j\geq58, then the established upper bound gives b_j+70\in[128,245], again a contradiction. Thus b_j\leq57, and b_j+70\leq127 gives zero outgoing carry. Consequently the test returns true only if
b_j=48+d_j\quad\hbox{with}\quad0\leq d_j\leq9 \qquad(0\leq j<8).
Conversely these inequalities make all carries and borrows zero and both high-bit tests zero, so every eight-digit block is accepted.
Now unfold num imp dec2flt parse parse 8digits. Its checked subtraction of 3472328296227680304=\texttt{0x3030303030303030} has no borrow and gives v'=\sum_jd_j2^{8j}. In particular v'\leq\texttt{0x0909090909090909}<2^{60}. Hence the checked product 10v'<10\cdot2^{60}<2^{64}, the signed-count shift by eight is defined because 0\leq8<64, and the checked sum
q=10v'+(v'\mathbin{\mathtt{>>>}}8) <10\cdot2^{60}+2^{52}<2^{64}
succeeds. There is no byte carry in this displayed sum: byte j is 10d_j+d_{j+1} for 0\leq j<7, and byte seven is 10d_7; all are at most 99.
The three extracted constants are, by num imp dec2flt parse parse 8digits MASK, num imp dec2flt parse parse 8digits MUL1, and num imp dec2flt parse parse 8digits MUL2,
\begin{split} M&=1095216660735=\texttt{0x000000ff000000ff},\\ M_1&=4294967296000100=100+10^6 2^{32},\\ M_2&=42949672960001=1+10^4 2^{32}. \end{split}
The signed-count shift by sixteen is defined for the same reason as the shift by eight. Therefore the two masked words are
a=A+B2^{32},\quad b=C+D2^{32},
where
A=10d_0+d_1,\ B=10d_4+d_5,\quad C=10d_2+d_3,\ D=10d_6+d_7.
The next operations are deliberately wrapping. Reduction modulo 2^{64} of their complete products, including the discarded multiples of 2^{64}, gives
\begin{split} aM_1&\equiv100A+(10^6A+100B)2^{32}\pmod {2^{64}},\\ bM_2&\equiv C+(10^4C+D)2^{32}\pmod {2^{64}}. \end{split}
Here A,B,C,D\leq99. The low-word sum is 100A+C\leq9999<2^{32} and the high-word coefficient is at most 10^6\cdot99+100\cdot99+10^4\cdot99+99=99999999<2^{32}. Thus their wrapping addition has no further wrap. The signed-count shift by 32 is defined, and its result is exactly
10^6A+10^4C+100B+D =10^7d_0+10^6d_1+\cdots+10d_6+d_7<10^8.
This lies below 2^{32}, so the checked cast to u32 succeeds and the final widening cast to u64 preserves it. We have proved that parse_8digits returns the ordinary base-ten value of every guarded accepted block, with all checked products, sums, shifts, and casts accounted for.
It follows by induction on the two loops of num imp dec2flt parse try parse digits that, as long as the ordinary value obtained by appending all consumed digits is below 10^{19}, the returned accumulator is that ordinary value and the returned slice is the suffix beginning at the first nondigit. For the eight-byte step just proved, the new ideal value is 10^8x+B with 0\leq B<10^8; for the scalar step, wrapping subtraction classifies a byte as a digit exactly when it is 48+e, 0\leq e<10, the byte-to-u64 cast is exact, and the new ideal value is 10x+e. Every intermediate ideal prefix is no larger than the final nonnegative value. Since
10^{19}=10000000000000000000<2^{64}=18446744073709551616,
the wrapping multiplications and additions in both loop bodies coincide with these ordinary updates. The bulk suffix at eight and each scalar index and count increment are guarded by the current length; the final scalar suffix is taken at a count no greater than that length. This proves the invariant and all its slice obligations.
Apply that invariant to the two calls made by parse_partial_number. Let I be the consecutive initial digit sequence, let F be the consecutive sequence after byte 46 if that byte immediately follows I (and the empty sequence otherwise), and put D=|I|+|F|. Here the optional-point test uses the exact empty-versus-head/tail decomposition of ModelSliceSplitFirst. The two returned slices and SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice offset from show that n_digits1 is exactly D: both suffix lengths are at most L\leq2^{63}-1, and the 64-bit widths from Assumptions show that the usize-to-isize casts and their nonnegative differences are in range; the optional fractional negation and cast to i64 are also in range. The two digit runs are disjoint subranges of the input, so D\leq L and the checked addition forming n_digits1 also succeeds. If D\leq19, the concatenation IF has at most nineteen digit positions, so its ordinary value is at most 10^D-1\leq10^{19}-1. The scanner invariant proves mantissa1<10^{19} in every such branch.
Suppose instead that D>19. In num imp dec2flt parse adjust many digit count, start from D-19. While its scanning flag is true, byte 46 subtracts \operatorname{sat}(46-47)=0, byte 48 subtracts \operatorname{sat}(48-47)=1, and the scan index advances by one; the first other byte changes the flag to false without changing either the count or index, and the next loop step returns. Let z be the number of leading zero digits of IF, taking z=D if every digit is zero. Ignoring the optional point, if IF has a nonzero digit, the scan visits exactly those z zeros and the intervening optional point, then stops at that first nonzero; its result is exactly D-19-z. If every digit of IF is zero, the scanner has already accumulated the value zero. It may continue over later zero or point bytes that the partial parser did not include in IF, but each such step can only decrease the adjustment further and cannot alter mantissa1. All accesses and increments are guarded by L. Since D>19, the initial count is at least one and at most L-19; at most L zero bytes can be subtracted, so every count is at least 1-L\geq1-(2^{63}-1) and at most 2^{63}-1. By the 64-bit signed range supplied by Assumptions, every checked signed subtraction succeeds.
If this adjusted value is nonpositive and all digits of IF are zero, then mantissa1=0 immediately. Otherwise the exact formula above gives D-z\leq19. Before the first nonzero digit, every scalar or eight-byte update appends only zeros to accumulator zero. From that digit onward there are at most nineteen positions, including internal and trailing zeros. Hence the full ordinary value of IF is at most 10^{19}-1, and the same scanner invariant gives mantissa1<10^{19} even though D itself exceeds nineteen. Conversely, an adjusted-positive outcome is impossible in the all-zero case; in the other case its exact value D-19-z>0 gives D-z\geq20.
It remains to bound the adjusted-positive retry. By num imp dec2flt parse MIN 19DIGIT INT, the guard in num imp dec2flt parse try parse 19digits is exactly 10^{18}. At a loop head with x<10^{18}, ModelSliceSplitFirst says that splitting an empty slice yields no head and stops, while a nonempty slice is decomposed into exactly its head and tail; a nondigit stops after the total wrapping byte subtraction. For a digit e\in[0,9], the checked operations satisfy
x\cdot10\leq(10^{18}-1)10<2^{64},\qquad x\cdot10+e\leq10^{19}-1<2^{64}.
The byte cast is exact. After this update the next guard either permits another such step or stops because the value has reached 10^{18}. Thus a scan begun with any x<10^{18} returns a value below 10^{19}; all guarded splits, checked multiplications, additions, and casts in the loop have been discharged.
Unfold num imp dec2flt parse parse many digits. Its first nineteen-digit scan starts at zero, so it returns m<10^{19}. If m\geq10^{18}, the function retains m; its offset operation can affect only the exponent. Both involved suffix lengths are at most L, so their casts are in range by Assumptions, and their checked difference lies between -(2^{63}-1) and 2^{63}-1. If m<10^{18}, the function takes the suffix at index one and scans again from initial value m. As just proved, the positive-adjustment condition implies D-z\geq20. If the first scan returned below 10^{18}, it cannot have reached nineteen positions beginning with the first nonzero digit: after exactly nineteen such positions their decimal value is at least 10^{18}. Nor can it have reached the final nondigit or end before those positions, because D-z\geq20. It must therefore have stopped at the optional point; the saved integer-end slice begins with that point, so taking its suffix at one is in bounds and starts exactly at the fractional digits. The second scan begins with m<10^{18} and therefore returns m'<10^{19} by the preceding invariant. Its suffix offset is nonnegative and at most L, its negation lies in [-(2^{63}-1),0], and the isize-to-i64 cast is exact. The later checked addition of the explicit scientific exponent may fail on very long inputs, but a record-producing execution is by hypothesis successful; that addition changes only the exponent and cannot change m'. Consequently either return route of parse_many_digits has mantissa below 10^{19}, including the initial scan, the one-byte-skip retry, and every offset, slice, cast, and checked arithmetic operation relevant to reaching its result.
We can now exhaust the syntax of num imp dec2flt parse parse partial number. By the exact empty-versus-head/tail semantics of ModelSliceSplitFirst, both its optional-point handling and its later scientific-suffix inspection preserve the actual first byte and remaining suffix. Consequently, after the optional point handling, splitting the remaining suffix has exactly four cases: no byte; byte 101; byte 69; or a byte different from both. The middle two invoke num imp dec2flt parse parse scientific. At the ensuing try-control-flow step, ModelOptionBranch maps some val to successful Continue val, which can reach a record literal, whereas it maps none to successful Break none; then ModelOptionFromResidual converts that sole residual to successful none. Thus the Break route cannot produce the hypothesized d, and the continued value is used only in exponent arithmetic. In each of the four cases the definition then has exactly these three record-producing alternatives:
\begin{array}{c|c|c} \text{digit-count alternative}&\text{mantissa field}&\text{negative field}\\ \hline D\leq19&\texttt{mantissa1}&\texttt{false}\\ D>19,\ \text{adjusted count}>0&\texttt{parse\_many\_digits result}&\texttt{false}\\ D>19,\ \text{adjusted count}\leq0&\texttt{mantissa1}&\texttt{false}. \end{array}
This table applies separately to the no-suffix, lower-case scientific, upper-case scientific, and other-suffix branches, so it covers all twelve displayed record literals. The first and third mantissas are below 10^{19} by the scanner arguments; the middle mantissa is below 10^{19} by the retry argument. Every literal visibly assigns negative := false. By the extracted structure type num imp dec2flt decimal Decimal, each displayed literal's negative and mantissa fields are exactly the projections d.\mathit{negative} and d.\mathit{mantissa} in the theorem. Final consumed-length offsets and casts may affect only the paired length, and scientific parsing and exponent additions may affect only the exponent. Therefore the record d passed unchanged through parse_number satisfies both asserted conjuncts.

Lean proof


  intro s d hsource hne hparse
  have hlen : s.val.length <= 9223372036854775807 := by
    simpa [SourceValidByteSlice] using hsource
  have bind_ok_inv : forall {alpha beta : Type} (r : Result alpha)
      (f : alpha -> Result beta) y,
      Bind.bind r f = ok y -> exists x, r = ok x /\ f x = ok y := by
    intro alpha beta r f y h
    cases r with
    | fail err => simp [Bind.bind, Aeneas.Std.bind] at h
    | div => simp [Bind.bind, Aeneas.Std.bind] at h
    | ok x => exact ⟨x, rfl, h⟩
  have split_digits : forall xs : List Std.U8,
      exists digits tail,
        xs = 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)) := by
    intro xs
    induction xs with
    | nil => exact ⟨[], [], rfl, (by intro b hb; cases hb), (by
        intro b rest h
        cases h)⟩
    | cons b bs ih =>
      by_cases hb : 48 <= b.val /\ b.val <= 57
      · obtain ⟨digits, tail, hsplit, hdigits, htail⟩ := ih
        refine ⟨b :: digits, tail, by simp [hsplit], ?_, htail⟩
        intro c hc
        rcases List.mem_cons.mp hc with rfl | hc
        · exact hb
        · exact hdigits c hc
      · exact ⟨[], b :: bs, rfl, (by intro c hc; cases hc), (by
          intro c rest h
          cases h
          exact hb)⟩
  have parse_many_bound : forall (start intEnd : Slice Std.U8)
      (exp outExp : Std.I64) (mantissa : Std.U64),
      num.imp.dec2flt.parse.parse_many_digits start intEnd exp =
        ok (mantissa, outExp) ->
      mantissa.val <= 9999999999999999999 := by
    intro start intEnd exp outExp mantissa hmany
    obtain ⟨digits, tail, hsplit, hdigits, htail⟩ := split_digits start.val
    obtain ⟨rest, first, remaining, consumed, hfirst, hpartition,
        hrest, hfirstValue, hremainingDigits, hremainingLen, hfirstBound,
        hfirstLow, hfirstSharp⟩ :=
      NumberHelperTryParse19SafeLength start 0#u64 digits tail hsplit
        hdigits htail (by norm_num)
    unfold num.imp.dec2flt.parse.parse_many_digits at hmany
    rw [hfirst] at hmany
    by_cases hthreshold : first.val < 1000000000000000000
    · have hnotHigh : ¬ 1000000000000000000 <= first.val := by omega
      simp [num.imp.dec2flt.parse.MIN_19DIGIT_INT.eq_def,
        hnotHigh] at hmany
      cases hdrop :
          core.slice.index.SliceIndexRangeFromUsizeSlice.index
            { start := 1#usize } rest with
      | fail err =>
        rw [hdrop] at hmany
        simp at hmany
      | div =>
        rw [hdrop] at hmany
        simp at hmany
      | ok afterDrop =>
        rw [hdrop] at hmany
        simp at hmany
        obtain ⟨digits2, tail2, hsplit2, hdigits2, htail2⟩ :=
          split_digits afterDrop.val
        obtain ⟨rest2, second, remaining2, consumed2, hsecond,
            hpartition2, hrest2, hsecondValue, hremainingDigits2,
            hremainingLen2, hsecondBound, hsecondLow, hsecondSharp⟩ :=
          NumberHelperTryParse19SafeLength afterDrop first digits2 tail2
            hsplit2 hdigits2 htail2 hfirstBound
        rw [hsecond] at hmany
        simp at hmany
        cases hoff :
            SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
              rest2 afterDrop with
        | fail err => simp [hoff] at hmany
        | div => simp [hoff] at hmany
        | ok off =>
          simp [hoff] at hmany
          cases hneg : HNeg.hNeg off with
          | fail err => simp [hneg] at hmany
          | div => simp [hneg] at hmany
          | ok negOff =>
            simp [hneg] at hmany
            cases hcast : lift (IScalar.cast .I64 negOff) with
            | fail err => simp [hcast] at hmany
            | div => simp [hcast] at hmany
            | ok castOff =>
              simp [hcast] at hmany
              cases hadd : castOff + exp with
              | fail err => simp [hadd] at hmany
              | div => simp [hadd] at hmany
              | ok finalExp =>
                simp [hadd] at hmany
                rcases hmany with ⟨rfl, rfl⟩
                exact hsecondBound
    · have hhigh : 1000000000000000000 <= first.val := by omega
      simp [num.imp.dec2flt.parse.MIN_19DIGIT_INT.eq_def,
        hhigh] at hmany
      cases hoff :
          SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
            intEnd rest with
      | fail err => simp [hoff] at hmany
      | div => simp [hoff] at hmany
      | ok off =>
        simp [hoff] at hmany
        cases hcast : lift (IScalar.cast .I64 off) with
        | fail err => simp [hcast] at hmany
        | div => simp [hcast] at hmany
        | ok castOff =>
          simp [hcast] at hmany
          cases hadd : castOff + exp with
          | fail err => simp [hadd] at hmany
          | div => simp [hadd] at hmany
          | ok finalExp =>
            simp [hadd] at hmany
            rcases hmany with ⟨rfl, rfl⟩
            exact hfirstBound
  have record_branches : forall (total : Std.Isize)
      (shortMantissa : Std.U64) (intEnd : Slice Std.U8)
      (shortExponent retryExponent : Std.I64) (len outLen : Std.Usize),
      (total.val <= 19 -> shortMantissa.val <= 9999999999999999999) ->
      (forall (nMore adjusted : Std.Isize),
        total - 19#isize = ok nMore ->
        num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted ->
        Not (total.val <= 19) ->
        Not (0 < adjusted.val) ->
        shortMantissa.val <= 9999999999999999999) ->
      (if total.val <= 19 then
          ok (some
            (num.imp.dec2flt.decimal.Decimal.mk shortExponent shortMantissa
              false false, len))
        else do
          let nMore <- total - 19#isize
          let adjusted <-
            num.imp.dec2flt.parse.adjust_many_digit_count s nMore
          if 0 < adjusted.val then
            let (manyMantissa, manyExponent) <-
              num.imp.dec2flt.parse.parse_many_digits s intEnd retryExponent
            ok (some
              (num.imp.dec2flt.decimal.Decimal.mk manyExponent manyMantissa
                false true, len))
          else
            ok (some
              (num.imp.dec2flt.decimal.Decimal.mk shortExponent shortMantissa
                false false, len))) = ok (some (d, outLen)) ->
      d.negative = false /\ d.mantissa.val < 10 ^ 19 := by
    intro total shortMantissa intEnd shortExponent retryExponent len outLen
      hshort hnonpositive hresult
    by_cases hsmall : total.val <= 19
    · simp [hsmall] at hresult
      rcases hresult with ⟨rfl, rfl⟩
      refine ⟨rfl, ?_⟩
      have hb := hshort hsmall
      norm_num
      omega
    · simp [hsmall] at hresult
      obtain ⟨nMore, hsub, hresult⟩ := bind_ok_inv _ _ _ hresult
      obtain ⟨adjusted, hadjust, hresult⟩ := bind_ok_inv _ _ _ hresult
      by_cases hpositive : 0 < adjusted.val
      · simp only [if_pos hpositive] at hresult
        obtain ⟨manyResult, hmany, hresult⟩ :=
          bind_ok_inv _ _ _ hresult
        rcases manyResult with ⟨manyMantissa, manyExponent⟩
        rcases hresult with ⟨rfl, rfl⟩
        refine ⟨rfl, ?_⟩
        have hb := parse_many_bound s intEnd retryExponent manyExponent
          manyMantissa hmany
        norm_num
        omega
      · simp only [if_neg hpositive] at hresult
        rcases hresult with ⟨rfl, rfl⟩
        refine ⟨rfl, ?_⟩
        have hb := hnonpositive nMore adjusted hsub hadjust hsmall hpositive
        norm_num
        omega
  have fold_upper : forall (digits : List Std.U8) (a : Nat),
      (forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57) ->
      digits.foldl (fun x b => 10 * x + (b.val - 48)) a <
        (a + 1) * 10 ^ digits.length := by
    intro digits
    induction digits with
    | nil => intro a h; simp
    | cons b bs ih =>
      intro a h
      have hb := h b List.mem_cons_self
      have hbs : forall c, List.Mem c bs ->
          48 <= c.val /\ c.val <= 57 := by
        intro c hc
        exact h c (List.mem_cons_of_mem b hc)
      simp only [List.foldl_cons, List.length_cons, Nat.pow_succ]
      have hrec := ih (10 * a + (b.val - 48)) hbs
      have hcoeff : 10 * a + (b.val - 48) + 1 <= 10 * (a + 1) := by
        omega
      have hmul := Nat.mul_le_mul_right (10 ^ bs.length) hcoeff
      exact hrec.trans_le (by
        simpa [Nat.mul_assoc, Nat.mul_comm, Nat.mul_left_comm] using hmul)
  have fold_ge_start : forall (digits : List Std.U8) (a : Nat),
      a <= digits.foldl (fun x b => 10 * x + (b.val - 48)) a := by
    intro digits
    induction digits with
    | nil => intro a; rfl
    | cons b bs ih =>
      intro a
      apply le_trans (b := 10 * a + (b.val - 48))
      · omega
      · exact ih _
  have zero_fold : forall digits : List Std.U8,
      (forall b, List.Mem b digits -> b = 48#u8) ->
      digits.foldl (fun x b => 10 * x + (b.val - 48)) 0 = 0 := by
    intro digits hzero
    induction digits with
    | nil => rfl
    | cons b bs ih =>
      have hb := hzero b List.mem_cons_self
      have hbs : forall c, List.Mem c bs -> c = 48#u8 := by
        intro c hc
        exact hzero c (List.mem_cons_of_mem b hc)
      simp [hb, ih hbs]
  have split_zeros : forall digits : List Std.U8,
      exists zeros tail,
        digits = zeros ++ tail /\
        (forall b, List.Mem b zeros -> b = 48#u8) /\
        (tail = [] \/ exists b rest, tail = b :: rest /\ b ≠ 48#u8) := by
    intro digits
    induction digits with
    | nil => exact ⟨[], [], rfl, (by intro b hb; cases hb), Or.inl rfl⟩
    | cons b bs ih =>
      by_cases hb : b = 48#u8
      · obtain ⟨zeros, tail, hsplit, hzeros, htail⟩ := ih
        refine ⟨b :: zeros, tail, by simp [hsplit], ?_, htail⟩
        intro c hc
        rcases List.mem_cons.mp hc with rfl | hc
        · exact hb
        · exact hzeros c hc
      · exact ⟨[], b :: bs, rfl, (by intro c hc; cases hc),
          Or.inr ⟨b, bs, rfl, hb⟩⟩
  have count_zeros : forall zeros : List Std.U8,
      (forall b, List.Mem b zeros -> b = 48#u8) ->
      zeros.count 48#u8 = zeros.length := by
    intro zeros hzeros
    induction zeros with
    | nil => rfl
    | cons b bs ih =>
      have hb := hzeros b List.mem_cons_self
      have hbs : forall c, List.Mem c bs -> c = 48#u8 := by
        intro c hc
        exact hzeros c (List.mem_cons_of_mem b hc)
      simp [hb, ih hbs]
  have split_significand_zeros : forall
      (integer fraction suffix : List Std.U8),
      (forall b, List.Mem b integer -> 48 <= b.val /\ b.val <= 57) ->
      (forall b, List.Mem b fraction -> 48 <= b.val /\ b.val <= 57) ->
      Not (forall b, List.Mem b (integer ++ fraction) -> b = 48#u8) ->
      exists rawZeros rawTail sigZeros sigTail,
        integer ++ 46#u8 :: (fraction ++ suffix) = rawZeros ++ rawTail /\
        (forall b, List.Mem b rawZeros -> b = 48#u8 \/ b = 46#u8) /\
        (exists b rest, rawTail = b :: rest /\
          b ≠ 48#u8 /\ b ≠ 46#u8) /\
        integer ++ fraction = sigZeros ++ sigTail /\
        (forall b, List.Mem b sigZeros -> b = 48#u8) /\
        (exists b rest, sigTail = b :: rest /\ b ≠ 48#u8) /\
        rawZeros.count 48#u8 = sigZeros.length := by
    intro integer fraction suffix hinteger hfraction hnonzero
    obtain ⟨intZeros, intSig, hintSplit, hintZeros, hintSig⟩ :=
      split_zeros integer
    rcases hintSig with hintNil | ⟨b, intRest, hintSigEq, hb⟩
    · obtain ⟨fracZeros, fracSig, hfracSplit, hfracZeros, hfracSig⟩ :=
        split_zeros fraction
      rcases hfracSig with hfracNil | ⟨c, fracRest, hfracSigEq, hc⟩
      · exfalso
        apply hnonzero
        intro x hx
        rcases List.mem_append.mp hx with hx | hx
        · rw [hintSplit, hintNil] at hx
          exact hintZeros x (by simpa using hx)
        · rw [hfracSplit, hfracNil] at hx
          exact hfracZeros x (by simpa using hx)
      · refine ⟨intZeros ++ 46#u8 :: fracZeros,
          fracSig ++ suffix, intZeros ++ fracZeros, fracSig,
          ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
        · rw [hintSplit, hintNil, hfracSplit]
          simp [List.append_assoc]
        · intro x hx
          rcases List.mem_append.mp hx with hx | hx
          · exact Or.inl (hintZeros x hx)
          · rcases List.mem_cons.mp hx with rfl | hx
            · exact Or.inr rfl
            · exact Or.inl (hfracZeros x hx)
        · refine ⟨c, fracRest ++ suffix, by simp [hfracSigEq], hc, ?_⟩
          have hcdigit := hfraction c (by
            rw [hfracSplit, hfracSigEq]
            exact List.mem_append_right fracZeros List.mem_cons_self)
          intro hpoint
          subst c
          norm_num at hcdigit
        · rw [hintSplit, hintNil, hfracSplit]
          simp [List.append_assoc]
        · intro x hx
          rcases List.mem_append.mp hx with hx | hx
          · exact hintZeros x hx
          · exact hfracZeros x hx
        · exact ⟨c, fracRest, hfracSigEq, hc⟩
        · rw [List.count_append, List.count_cons,
            count_zeros intZeros hintZeros,
            count_zeros fracZeros hfracZeros]
          norm_num
    · refine ⟨intZeros, intSig ++ 46#u8 :: (fraction ++ suffix),
        intZeros, intSig ++ fraction, ?_, ?_, ?_, ?_, hintZeros, ?_, ?_⟩
      · rw [hintSplit]
        simp [List.append_assoc]
      · intro x hx
        exact Or.inl (hintZeros x hx)
      · refine ⟨b, intRest ++ 46#u8 :: (fraction ++ suffix),
          by simp [hintSigEq], hb, ?_⟩
        have hbdigit := hinteger b (by
          rw [hintSplit, hintSigEq]
          exact List.mem_append_right intZeros List.mem_cons_self)
        intro hpoint
        subst b
        norm_num at hbdigit
      · rw [hintSplit]
        simp [List.append_assoc]
      · exact ⟨b, intRest ++ fraction, by simp [hintSigEq], hb⟩
      · exact count_zeros intZeros hintZeros
  obtain ⟨intDigits, intTail, hsval, hintDigits, hintTail⟩ :=
    split_digits s.val
  have hintTailProp : intTail.length <= Usize.max := by
    have hp := s.property
    rw [hsval] at hp
    simp only [List.length_append] at hp
    omega
  let intRest : Slice Std.U8 := ⟨intTail, hintTailProp⟩
  obtain ⟨intMantissa, scannedIntRest, hscanInt, hscannedIntRest⟩ :=
    NumberHelperDigitScanSourceLength s 0#u64 intDigits intTail hsval
      hintDigits hintTail hlen
  have hscannedIntRestEq : scannedIntRest = intRest :=
    (Slice.eq_iff _ _).2 (by simpa [intRest] using hscannedIntRest)
  subst scannedIntRest
  obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ :=
    NumberHelperOffsetSourceLength s intRest hlen (by
      rw [hsval]
      simp [intRest])
  have hnDigitsEq : nDigits.val = intDigits.length := by
    rw [hnDigitsVal, hsval]
    simp [intRest]
  have hpartial : exists len,
      num.imp.dec2flt.parse.parse_partial_number s = ok (some (d, len)) := by
    unfold num.imp.dec2flt.parse.parse_number at hparse
    cases hp : num.imp.dec2flt.parse.parse_partial_number s with
    | fail err => simp [hp] at hparse
    | div => simp [hp] at hparse
    | ok result =>
      cases result with
      | none => simp [hp] at hparse
      | some pair =>
        rcases pair with ⟨record, len⟩
        simp [hp] at hparse
        split at hparse
        · have hrecord : record = d := by simpa using hparse
          subst d
          exact ⟨len, rfl⟩
        · simp at hparse
  obtain ⟨parsedLen, hpartial⟩ := hpartial
  cases intTail with
  | nil =>
    have hsplitInt : core.slice.Slice.split_first intRest = ok none := by
      simp [core.slice.Slice.split_first, ModelSliceSplitFirst, intRest]
    have haddZero : nDigits + 0#isize = ok nDigits := by
      have hb := IScalar.hBounds nDigits
      obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
        (IScalar.add_spec (x := nDigits) (y := 0#isize) (by
          simp [IScalar.min, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits]
          omega) (by
          simp [IScalar.max, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits] at hb ⊢
          omega))
      rw [hz]
      congr 2
      apply IScalar.eq_of_val_eq
      simpa using hzval
    have hfullOffset :
        SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
          intRest s = ok nDigits := by
      simpa [intRest] using hoffInt
    have hcastLen : lift (IScalar.hcast .Usize nDigits) =
        ok (Slice.len s) := by
      have hbound : 0 <= nDigits.val /\
          nDigits.val <= UScalar.max .Usize := by
        rw [hnDigitsEq]
        constructor
        · omega
        · have hp := s.property
          rw [hsval] at hp
          simpa [UScalar.max, Usize.max, Usize.numBits,
            Dec2Flt.configured_x86_64_numBits] using hp
      obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
        (IScalar.hcast_inBounds_spec .Usize nDigits hbound)
      rw [hz]
      congr 2
      apply UScalar.eq_of_val_eq
      rw [hnDigitsEq] at hzval
      have hslen : s.val.length = intDigits.length := by simp [hsval]
      simpa [Slice.len_val, hslen] using hzval
    unfold num.imp.dec2flt.parse.parse_partial_number at hpartial
    simp [hne, hscanInt, hoffInt, hsplitInt, haddZero,
      hcastLen] at hpartial
    by_cases hnzero : nDigits = 0#isize
    · simp [hnzero] at hpartial
    · simp [hnzero] at hpartial
      by_cases hsmall : nDigits.val <= 19
      · simp [hsmall] at hpartial
        rcases hpartial with ⟨rfl, rfl⟩
        constructor
        · rfl
        · have hfoldLt := fold_upper intDigits 0 hintDigits
          norm_num at hfoldLt
          have hlen19 : intDigits.length <= 19 := by
            omega
          have hbound : intDigits.foldl
              (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
            rw [U64.max_eq]
            have hp := hfoldLt.trans_le
              (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hlen19)
            norm_num at hp ⊢
            omega
          obtain ⟨value, rest, hexact, hrest, hvalue⟩ :=
            NumberHelperDigitScanExact s 0#u64 intDigits []
              (by simpa using hsval) hintDigits (by
                intro b rest h
                cases h) hlen hbound
          rw [hscanInt] at hexact
          simp at hexact
          rcases hexact with ⟨rfl, rfl⟩
          rw [hvalue]
          norm_num
          exact hfoldLt.trans_le
            (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hlen19)
      · have hlarge : 19 < nDigits.val := by omega
        have hsubBounds : IScalar.min .Isize <= nDigits.val - 19 /\
            nDigits.val - 19 <= IScalar.max .Isize := by
          have hb := IScalar.hBounds nDigits
          simp [IScalar.min, IScalar.max, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits] at hb ⊢
          omega
        obtain ⟨nMore, hnMore, hnMoreVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.sub_spec (x := nDigits) (y := 19#isize)
            (by norm_num; omega) (by norm_num; omega))
        norm_num at hnMoreVal
        simp [hsmall, hnMore] at hpartial
        obtain ⟨zeros, nonzeroTail, hzeroSplit, hzeros, hzeroTail⟩ :=
          split_zeros intDigits
        have hcount := count_zeros zeros hzeros
        have hsAdjust : s.val = zeros ++ nonzeroTail := by
          simpa [hzeroSplit] using hsval
        have htailSharp : nonzeroTail = [] \/ exists b rest,
            nonzeroTail = b :: rest /\ b ≠ 48#u8 /\ b ≠ 46#u8 := by
          rcases hzeroTail with hnil | ⟨b, rest, htailEq, hb⟩
          · exact Or.inl hnil
          · right
            refine ⟨b, rest, htailEq, hb, ?_⟩
            have hbdigit := hintDigits b (by
              rw [hzeroSplit, htailEq]
              exact List.mem_append_right zeros List.mem_cons_self)
            intro heq
            subst b
            norm_num at hbdigit
        have hnMoreNonneg : 0 <= nMore.val := by
          rw [hnMoreVal]
          omega
        have hnMoreLe : nMore.val <= s.val.length := by
          rw [hnMoreVal, hnDigitsEq, hsval]
          simp only [List.length_append, List.length_nil, Nat.add_zero]
          omega
        have hadjustLower : IScalar.min .Isize <=
            nMore.val - (zeros.count 48#u8 : Int) := by
          rw [hnMoreVal, hcount, hnDigitsEq]
          have hzerosLen : zeros.length <= intDigits.length := by
            rw [hzeroSplit]
            simp
          simp [IScalar.min, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits]
          omega
        obtain ⟨adjusted, hadjust, hadjustVal⟩ :=
          NumberHelperAdjustManyExact s nMore zeros nonzeroTail hsAdjust
            (by intro b hb; exact Or.inl (hzeros b hb)) htailSharp hlen
            hnMoreNonneg hnMoreLe hadjustLower
        rw [hadjust] at hpartial
        by_cases hpositive : 0 < adjusted.val
        · simp [hpositive] at hpartial
          cases hmany :
              num.imp.dec2flt.parse.parse_many_digits s intRest 0#i64 with
          | fail err => simp [hmany] at hpartial
          | div => simp [hmany] at hpartial
          | ok result =>
            rcases result with ⟨manyMantissa, manyExponent⟩
            simp [hmany] at hpartial
            rcases hpartial with ⟨rfl, rfl⟩
            refine ⟨rfl, ?_⟩
            change manyMantissa.val < 10 ^ 19
            have hb := parse_many_bound s intRest 0#i64 manyExponent
              manyMantissa hmany
            omega
        · simp [hpositive] at hpartial
          have htailLen : nonzeroTail.length <= 19 := by
            rw [hadjustVal, hnMoreVal, hcount, hnDigitsEq] at hpositive
            have hlenEq := congrArg List.length hzeroSplit
            simp only [List.length_append] at hlenEq
            omega
          have htailDigits : forall b, List.Mem b nonzeroTail ->
              48 <= b.val /\ b.val <= 57 := by
            intro b hb
            apply hintDigits b
            rw [hzeroSplit]
            exact List.mem_append_right zeros hb
          have hzeroFold := zero_fold zeros hzeros
          have hfoldLt : intDigits.foldl
              (fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 := by
            rcases hzeroTail with hnil | hcons
            · subst nonzeroTail
              rw [hzeroSplit, List.append_nil, hzeroFold]
              norm_num
            · rw [hzeroSplit, List.foldl_append, hzeroFold]
              have htailLt := fold_upper nonzeroTail 0 htailDigits
              norm_num at htailLt
              exact htailLt.trans_le
                (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat))
                  htailLen)
          have hfoldBound : intDigits.foldl
              (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
            rw [U64.max_eq]
            omega
          obtain ⟨value, rest, hexact, hrest, hvalue⟩ :=
            NumberHelperDigitScanExact s 0#u64 intDigits []
              (by simpa using hsval) hintDigits (by
                intro b rest h
                cases h) hlen hfoldBound
          rw [hscanInt] at hexact
          simp at hexact
          rcases hexact with ⟨rfl, rfl⟩
          rcases hpartial with ⟨rfl, rfl⟩
          refine ⟨rfl, ?_⟩
          norm_num at hvalue ⊢
          rw [hvalue]
          exact hfoldLt
  | cons first tail =>
    have htailProp : tail.length <= Usize.max := by
      have hp := intRest.property
      simp [intRest] at hp ⊢
      omega
    let afterFirst : Slice Std.U8 := ⟨tail, htailProp⟩
    have hsplitInt : core.slice.Slice.split_first intRest =
        ok (some (first, afterFirst)) := by
      simp [core.slice.Slice.split_first, ModelSliceSplitFirst,
        intRest, afterFirst]
    by_cases hpoint : first = 46#u8
    · subst first
      obtain ⟨fracDigits, fracTail, hfracSplit, hfracDigits, hfracTail⟩ :=
        split_digits tail
      have hfracTailProp : fracTail.length <= Usize.max := by
        have hp := afterFirst.property
        simp [afterFirst, hfracSplit] at hp ⊢
        omega
      let fracRest : Slice Std.U8 := ⟨fracTail, hfracTailProp⟩
      have hlenAfter : afterFirst.val.length <= 9223372036854775807 := by
        simp [afterFirst]
        rw [hsval] at hlen
        simp only [List.length_append, List.length_cons] at hlen
        omega
      obtain ⟨fracMantissa, scannedFracRest, hscanFrac, hscannedFracRest⟩ :=
        NumberHelperDigitScanSourceLength afterFirst intMantissa fracDigits
          fracTail (by simp [afterFirst, hfracSplit]) hfracDigits hfracTail
            hlenAfter
      have hscannedFracRestEq : scannedFracRest = fracRest :=
        (Slice.eq_iff _ _).2 (by
          simpa [fracRest] using hscannedFracRest)
      subst scannedFracRest
      obtain ⟨nAfter, hoffFrac, hnAfterVal⟩ :=
        NumberHelperOffsetSourceLength afterFirst fracRest hlenAfter (by
          simp [afterFirst, fracRest, hfracSplit])
      have hnAfterEq : nAfter.val = fracDigits.length := by
        rw [hnAfterVal]
        simp [afterFirst, fracRest, hfracSplit]
      have hnAfterNotMin : nAfter.val ≠ IScalar.min .Isize := by
        rw [hnAfterEq, IScalar.min]
        simp [IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits]
      obtain ⟨negAfter, hnegAfter, hnegAfterVal⟩ :=
        Aeneas.Std.WP.spec_imp_exists
          (HNeg.hNeg.step nAfter hnAfterNotMin)
      have hnegCastBounds : IScalar.min .I64 <= negAfter.val /\
          negAfter.val <= IScalar.max .I64 := by
        rw [hnegAfterVal, hnAfterEq,
          IScalar.min_IScalarTy_I64_eq, I64.min_eq,
          IScalar.max_IScalarTy_I64_eq, I64.max_eq]
        constructor <;> omega
      obtain ⟨fracExponent, hfracExponent, hfracExponentVal⟩ :=
        Aeneas.Std.WP.spec_imp_exists
          (IScalar.cast_inBounds_spec .I64 negAfter hnegCastBounds)
      have htotalBounds : IScalar.min .Isize <=
          nDigits.val + nAfter.val /\
          nDigits.val + nAfter.val <= IScalar.max .Isize := by
        rw [hnDigitsEq, hnAfterEq]
        simp [IScalar.min, IScalar.max, IScalarTy.numBits,
          Dec2Flt.configured_x86_64_numBits]
        rw [hsval, hfracSplit] at hlen
        simp only [List.length_append, List.length_cons] at hlen
        constructor <;> omega
      obtain ⟨totalDigits, htotal, htotalVal⟩ :=
        Aeneas.Std.WP.spec_imp_exists
          (IScalar.add_spec htotalBounds.1 htotalBounds.2)
      have hsigDigits : forall b,
          List.Mem b (intDigits ++ fracDigits) ->
          48 <= b.val /\ b.val <= 57 := by
        intro b hb
        rcases List.mem_append.mp hb with hb | hb
        · exact hintDigits b hb
        · exact hfracDigits b hb
      have hexactScans :
          (intDigits ++ fracDigits).foldl
              (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max ->
          fracMantissa.val = (intDigits ++ fracDigits).foldl
            (fun x b => 10 * x + (b.val - 48)) 0 := by
        intro hfullBound
        have hprefixLe := fold_ge_start fracDigits
          (intDigits.foldl (fun x b => 10 * x + (b.val - 48)) 0)
        rw [← List.foldl_append] at hprefixLe
        have hintBound : intDigits.foldl
            (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max :=
          hprefixLe.trans hfullBound
        obtain ⟨intValue, intScanRest, hexactInt, hexactIntRest,
            hintValue⟩ :=
          NumberHelperDigitScanExact s 0#u64 intDigits
            (46#u8 :: tail) (by simpa using hsval) hintDigits hintTail
              hlen hintBound
        rw [hscanInt] at hexactInt
        simp at hexactInt
        rcases hexactInt with ⟨rfl, rfl⟩
        have hfracBound : fracDigits.foldl
            (fun x b => 10 * x + (b.val - 48)) intMantissa.val <=
              U64.max := by
          norm_num at hintValue
          rw [hintValue, ← List.foldl_append]
          exact hfullBound
        obtain ⟨fracValue, fracScanRest, hexactFrac, hexactFracRest,
            hfracValue⟩ :=
          NumberHelperDigitScanExact afterFirst intMantissa fracDigits
            fracTail (by simp [afterFirst, hfracSplit]) hfracDigits
              hfracTail hlenAfter hfracBound
        rw [hscanFrac] at hexactFrac
        simp at hexactFrac
        rcases hexactFrac with ⟨rfl, rfl⟩
        norm_num at hintValue hfracValue ⊢
        rw [hfracValue, hintValue]
      have hshort : totalDigits.val <= 19 ->
          fracMantissa.val <= 9999999999999999999 := by
        intro hsmall
        have hsigLen : intDigits.length + fracDigits.length <= 19 := by
          rw [htotalVal, hnDigitsEq, hnAfterEq] at hsmall
          omega
        have hfoldLt := fold_upper (intDigits ++ fracDigits) 0 hsigDigits
        norm_num at hfoldLt
        have hfold19 := hfoldLt.trans_le
          (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hsigLen)
        have hfoldBound : (intDigits ++ fracDigits).foldl
            (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
          rw [U64.max_eq]
          norm_num
          omega
        rw [hexactScans hfoldBound]
        norm_num
        omega
      have hnonpositive : forall (nMore adjusted : Std.Isize),
          totalDigits - 19#isize = ok nMore ->
          num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted ->
          Not (totalDigits.val <= 19) ->
          Not (0 < adjusted.val) ->
          fracMantissa.val <= 9999999999999999999 := by
        intro nMore adjusted hsub hadjust hlarge hnonpos
        by_cases hallzero : forall b,
            List.Mem b (intDigits ++ fracDigits) -> b = 48#u8
        · have hfoldZero := zero_fold (intDigits ++ fracDigits) hallzero
          have hfoldBound : (intDigits ++ fracDigits).foldl
              (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
            rw [hfoldZero]
            exact Nat.zero_le _
          rw [hexactScans hfoldBound, hfoldZero]
          norm_num
        · obtain ⟨rawZeros, rawTail, sigZeros, sigTail, hrawSplit,
              hrawZeros, hrawTail, hsigSplit, hsigZeros, hsigTail, hcount⟩ :=
            split_significand_zeros intDigits fracDigits fracTail
              hintDigits hfracDigits hallzero
          have hsAdjust : s.val = rawZeros ++ rawTail := by
            rw [hsval, hfracSplit]
            exact hrawSplit
          have hnMoreVal : nMore.val = totalDigits.val - 19 := by
            have heq := IScalar.sub_equiv totalDigits 19#isize
            rw [hsub] at heq
            have hval := heq.2.1
            norm_num at hval ⊢
            exact hval
          have hnMoreNonneg : 0 <= nMore.val := by
            rw [hnMoreVal]
            omega
          have hnMoreLe : nMore.val <= s.val.length := by
            rw [hnMoreVal, htotalVal, hnDigitsEq, hnAfterEq, hsval,
              hfracSplit]
            simp only [List.length_append, List.length_cons]
            omega
          have hadjustLower : IScalar.min .Isize <=
              nMore.val - (rawZeros.count 48#u8 : Int) := by
            rw [hnMoreVal, hcount, htotalVal, hnDigitsEq, hnAfterEq]
            have hzlen : sigZeros.length <=
                (intDigits ++ fracDigits).length := by
              rw [hsigSplit]
              simp
            have hsigSource : (intDigits ++ fracDigits).length <=
                s.val.length := by
              rw [hsval, hfracSplit]
              simp only [List.length_append, List.length_cons]
              omega
            have hzBound : sigZeros.length <= 9223372036854775807 :=
              hzlen.trans (hsigSource.trans hlen)
            simp [IScalar.min, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits]
            omega
          obtain ⟨adjusted2, hadjust2, hadjust2Val⟩ :=
            NumberHelperAdjustManyExact s nMore rawZeros rawTail hsAdjust
              hrawZeros (Or.inr hrawTail) hlen hnMoreNonneg hnMoreLe
                hadjustLower
          rw [hadjust] at hadjust2
          simp at hadjust2
          subst adjusted2
          have hsigTailLen : sigTail.length <= 19 := by
            rw [hadjust2Val, hnMoreVal, hcount, htotalVal, hnDigitsEq,
              hnAfterEq] at hnonpos
            have hlenEq := congrArg List.length hsigSplit
            simp only [List.length_append] at hlenEq
            omega
          have hsigTailDigits : forall b, List.Mem b sigTail ->
              48 <= b.val /\ b.val <= 57 := by
            intro b hb
            apply hsigDigits b
            rw [hsigSplit]
            exact List.mem_append_right sigZeros hb
          have hzeroFold := zero_fold sigZeros hsigZeros
          have htailLt := fold_upper sigTail 0 hsigTailDigits
          norm_num at htailLt
          have hfoldLt : (intDigits ++ fracDigits).foldl
              (fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 := by
            rw [hsigSplit, List.foldl_append, hzeroFold]
            exact htailLt.trans_le
              (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat))
                hsigTailLen)
          have hfoldBound : (intDigits ++ fracDigits).foldl
              (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
            exact (Nat.le_of_lt hfoldLt).trans (by
              norm_num [U64.max_eq])
          rw [hexactScans hfoldBound]
          norm_num at hfoldLt ⊢
          omega
      unfold num.imp.dec2flt.parse.parse_partial_number at hpartial
      simp [hne, hscanInt, hoffInt, hsplitInt, hscanFrac, hoffFrac,
        hnegAfter, hfracExponent] at hpartial
      simp only [UScalar.ofNatCore] at hpartial
      simp [htotal] at hpartial
      by_cases hzero : totalDigits = 0#isize
      · simp [hzero] at hpartial
      · simp [hzero] at hpartial
        cases fracTail with
        | nil =>
          have hsplitFrac : core.slice.Slice.split_first fracRest =
              ok none := by
            simp [core.slice.Slice.split_first, ModelSliceSplitFirst,
              fracRest]
          simp [hsplitFrac] at hpartial
          obtain ⟨off, hoff, hpartial⟩ := bind_ok_inv _ _ _ hpartial
          obtain ⟨len, hcast, hpartial⟩ := bind_ok_inv _ _ _ hpartial
          exact record_branches totalDigits fracMantissa intRest fracExponent
            0#i64 len parsedLen hshort hnonpositive hpartial
        | cons marker suffix =>
          have hsuffixProp : suffix.length <= Usize.max := by
            have hp := fracRest.property
            simp [fracRest] at hp ⊢
            omega
          let afterMarker : Slice Std.U8 := ⟨suffix, hsuffixProp⟩
          have hsplitFrac : core.slice.Slice.split_first fracRest =
              ok (some (marker, afterMarker)) := by
            simp [core.slice.Slice.split_first, ModelSliceSplitFirst,
              fracRest, afterMarker]
          simp [hsplitFrac] at hpartial
          by_cases hlower : marker = (⟨101#8⟩ : Std.U8)
          · subst marker
            obtain ⟨sciResult, hsci, hpartial⟩ :=
              bind_ok_inv _ _ _ hpartial
            rcases sciResult with ⟨optExp, sciRest⟩
            cases optExp with
            | none =>
              simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
                ModelOptionBranch,
                core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual,
                ModelOptionFromResidual] at hpartial
            | some parsedExp =>
              simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
                ModelOptionBranch] at hpartial
              obtain ⟨combined, hsum, hpartial⟩ :=
                bind_ok_inv _ _ _ hpartial
              obtain ⟨off, hoff, hpartial⟩ :=
                bind_ok_inv _ _ _ hpartial
              obtain ⟨len, hcast, hpartial⟩ :=
                bind_ok_inv _ _ _ hpartial
              exact record_branches totalDigits fracMantissa intRest combined
                parsedExp len parsedLen hshort hnonpositive hpartial
          · by_cases hupper : marker = (⟨69#8⟩ : Std.U8)
            · subst marker
              obtain ⟨sciResult, hsci, hpartial⟩ :=
                bind_ok_inv _ _ _ hpartial
              rcases sciResult with ⟨optExp, sciRest⟩
              cases optExp with
              | none =>
                simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
                  ModelOptionBranch,
                  core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual,
                  ModelOptionFromResidual] at hpartial
              | some parsedExp =>
                simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
                  ModelOptionBranch] at hpartial
                obtain ⟨combined, hsum, hpartial⟩ :=
                  bind_ok_inv _ _ _ hpartial
                obtain ⟨off, hoff, hpartial⟩ :=
                  bind_ok_inv _ _ _ hpartial
                obtain ⟨len, hcast, hpartial⟩ :=
                  bind_ok_inv _ _ _ hpartial
                exact record_branches totalDigits fracMantissa intRest combined
                  parsedExp len parsedLen hshort hnonpositive hpartial
            · simp [hlower, hupper] at hpartial
              obtain ⟨off, hoff, hpartial⟩ := bind_ok_inv _ _ _ hpartial
              obtain ⟨len, hcast, hpartial⟩ := bind_ok_inv _ _ _ hpartial
              exact record_branches totalDigits fracMantissa intRest
                fracExponent 0#i64 len parsedLen hshort hnonpositive hpartial
    · have haddZero : nDigits + 0#isize = ok nDigits := by
        have hb := IScalar.hBounds nDigits
        obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.add_spec (x := nDigits) (y := 0#isize) (by
            simp [IScalar.min, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits]
            omega) (by
            simp [IScalar.max, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits] at hb ⊢
            omega))
        rw [hz]
        congr 2
        apply IScalar.eq_of_val_eq
        simpa using hzval
      have hshort : nDigits.val <= 19 ->
          intMantissa.val <= 9999999999999999999 := by
        intro hsmall
        have hlen19 : intDigits.length <= 19 := by omega
        have hfoldLt := fold_upper intDigits 0 hintDigits
        norm_num at hfoldLt
        have hfold19 : intDigits.foldl
            (fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 :=
          hfoldLt.trans_le
            (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hlen19)
        have hfoldBound : intDigits.foldl
            (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
          rw [U64.max_eq]
          omega
        obtain ⟨value, rest, hexact, hrest, hvalue⟩ :=
          NumberHelperDigitScanExact s 0#u64 intDigits (first :: tail)
            (by simpa using hsval) hintDigits hintTail hlen hfoldBound
        rw [hscanInt] at hexact
        simp at hexact
        rcases hexact with ⟨rfl, rfl⟩
        norm_num at hvalue ⊢
        rw [hvalue]
        omega
      have hnonpositive : forall (nMore adjusted : Std.Isize),
          nDigits - 19#isize = ok nMore ->
          num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted ->
          Not (nDigits.val <= 19) ->
          Not (0 < adjusted.val) ->
          intMantissa.val <= 9999999999999999999 := by
        intro nMore adjusted hsub hadjust hlarge hnonpos
        have hnMoreVal : nMore.val = nDigits.val - 19 := by
          have heq := IScalar.sub_equiv nDigits 19#isize
          rw [hsub] at heq
          have hval := heq.2.1
          norm_num at hval ⊢
          exact hval
        obtain ⟨zeros, nzTail, hzeroSplit, hzeros, hnzTail⟩ :=
          split_zeros intDigits
        have hcount := count_zeros zeros hzeros
        let rawTail : List Std.U8 := nzTail ++ first :: tail
        have hsAdjust : s.val = zeros ++ rawTail := by
          rw [hsval, hzeroSplit]
          simp [rawTail, List.append_assoc]
        have hrawTail : rawTail = [] \/ exists b rest,
            rawTail = b :: rest /\ b ≠ 48#u8 /\ b ≠ 46#u8 := by
          right
          rcases hnzTail with hnil | ⟨b, rest, heq, hb⟩
          · subst nzTail
            refine ⟨first, tail, by simp [rawTail], ?_, hpoint⟩
            intro hzero
            subst first
            have hnot := hintTail 48#u8 tail rfl
            norm_num at hnot
          · refine ⟨b, rest ++ first :: tail, by simp [rawTail, heq], hb, ?_⟩
            have hbdigit := hintDigits b (by
              rw [hzeroSplit, heq]
              exact List.mem_append_right zeros List.mem_cons_self)
            intro heq46
            subst b
            norm_num at hbdigit
        have hnMoreNonneg : 0 <= nMore.val := by
          rw [hnMoreVal]
          omega
        have hnMoreLe : nMore.val <= s.val.length := by
          rw [hnMoreVal, hnDigitsEq, hsval]
          simp only [List.length_append, List.length_cons]
          omega
        have hadjustLower : IScalar.min .Isize <=
            nMore.val - (zeros.count 48#u8 : Int) := by
          rw [hnMoreVal, hcount, hnDigitsEq]
          have hzlen : zeros.length <= intDigits.length := by
            rw [hzeroSplit]
            simp
          simp [IScalar.min, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits]
          omega
        obtain ⟨adjusted2, hadjust2, hadjust2Val⟩ :=
          NumberHelperAdjustManyExact s nMore zeros rawTail hsAdjust
            (by intro b hb; exact Or.inl (hzeros b hb)) hrawTail hlen
              hnMoreNonneg hnMoreLe hadjustLower
        rw [hadjust] at hadjust2
        simp at hadjust2
        subst adjusted2
        have hnzLen : nzTail.length <= 19 := by
          rw [hadjust2Val, hnMoreVal, hcount, hnDigitsEq] at hnonpos
          have hlenEq := congrArg List.length hzeroSplit
          simp only [List.length_append] at hlenEq
          omega
        have hnzDigits : forall b, List.Mem b nzTail ->
            48 <= b.val /\ b.val <= 57 := by
          intro b hb
          apply hintDigits b
          rw [hzeroSplit]
          exact List.mem_append_right zeros hb
        have hzeroFold := zero_fold zeros hzeros
        have hfoldLt : intDigits.foldl
            (fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 := by
          rcases hnzTail with hnil | hcons
          · subst nzTail
            rw [hzeroSplit, List.append_nil, hzeroFold]
            norm_num
          · rw [hzeroSplit, List.foldl_append, hzeroFold]
            have htailLt := fold_upper nzTail 0 hnzDigits
            norm_num at htailLt
            exact htailLt.trans_le
              (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hnzLen)
        have hfoldBound : intDigits.foldl
            (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
          rw [U64.max_eq]
          omega
        obtain ⟨value, rest, hexact, hrest, hvalue⟩ :=
          NumberHelperDigitScanExact s 0#u64 intDigits (first :: tail)
            (by simpa using hsval) hintDigits hintTail hlen hfoldBound
        rw [hscanInt] at hexact
        simp at hexact
        rcases hexact with ⟨rfl, rfl⟩
        norm_num at hvalue ⊢
        rw [hvalue]
        omega
      have hpointVal : first.val ≠ 46 := by
        intro hval
        apply hpoint
        exact UScalar.eq_of_val_eq hval
      unfold num.imp.dec2flt.parse.parse_partial_number at hpartial
      simp [hne, hscanInt, hoffInt, hsplitInt] at hpartial
      split at hpartial
      all_goals try { exfalso; apply hpointVal; rfl }
      simp [haddZero] at hpartial
      by_cases hnzero : nDigits = 0#isize
      · simp [hnzero] at hpartial
      · simp [hnzero, hsplitInt] at hpartial
        by_cases hlower : first = 101#u8
        · subst first
          obtain ⟨sciResult, hsci, hpartial⟩ :=
            bind_ok_inv _ _ _ hpartial
          rcases sciResult with ⟨optExp, sciRest⟩
          cases optExp with
          | none =>
            simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
              ModelOptionBranch,
              core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual,
              ModelOptionFromResidual] at hpartial
          | some parsedExp =>
            simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
              ModelOptionBranch] at hpartial
            obtain ⟨combined, hsum, hpartial⟩ :=
              bind_ok_inv _ _ _ hpartial
            obtain ⟨off, hoff, hpartial⟩ :=
              bind_ok_inv _ _ _ hpartial
            obtain ⟨len, hcast, hpartial⟩ :=
              bind_ok_inv _ _ _ hpartial
            exact record_branches nDigits intMantissa intRest combined
              parsedExp len parsedLen hshort hnonpositive hpartial
        · by_cases hupper : first = 69#u8
          · simp [hlower] at hpartial
            subst first
            obtain ⟨sciResult, hsci, hpartial⟩ :=
              bind_ok_inv _ _ _ hpartial
            rcases sciResult with ⟨optExp, sciRest⟩
            cases optExp with
            | none =>
              simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
                ModelOptionBranch,
                core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual,
                ModelOptionFromResidual] at hpartial
            | some parsedExp =>
              simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
                ModelOptionBranch] at hpartial
              obtain ⟨combined, hsum, hpartial⟩ :=
                bind_ok_inv _ _ _ hpartial
              obtain ⟨off, hoff, hpartial⟩ :=
                bind_ok_inv _ _ _ hpartial
              obtain ⟨len, hcast, hpartial⟩ :=
                bind_ok_inv _ _ _ hpartial
              exact record_branches nDigits intMantissa intRest combined
                parsedExp len parsedLen hshort hnonpositive hpartial
          · simp [hlower, hupper] at hpartial
            rw [hoffInt] at hpartial
            simp at hpartial
            obtain ⟨len, hcast, hpartial⟩ :=
              bind_ok_inv _ _ _ hpartial
            exact record_branches nDigits intMantissa intRest 0#i64 0#i64
              len parsedLen hshort hnonpositive hpartial

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.Assumptions
import Tablet.ModelOptionBranch
import Tablet.ModelOptionFromResidual
import Tablet.ModelSliceSplitFirst
import Tablet.NumberHelperAdjustManyExact
import Tablet.NumberHelperDigitScanExact
import Tablet.NumberHelperDigitScanSourceLength
import Tablet.NumberHelperOffsetSourceLength
import Tablet.NumberHelperTryParse19SafeLength
import Tablet.SourceValidByteSlice
import Tablet.U64_Insts_CoreOpsBitBitAndU64U64
import Tablet.U64_Insts_CoreOpsBitBitOrU64U64
import Tablet.num_imp_dec2flt_decimal_Decimal
import Tablet.num_imp_dec2flt_parse_parse_many_digits
import Tablet.num_imp_dec2flt_parse_parse_number
import Tablet.num_imp_dec2flt_parse_parse_partial_number
import Tablet.num_imp_dec2flt_parse_parse_scientific

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperRecordBounds]
theorem NumberHelperRecordBounds :
    ∀ (s : Slice Std.U8) d, SourceValidByteSlice s -> s.val ≠ [] ->
      num.imp.dec2flt.parse.parse_number s = ok (some d) ->
      d.negative = false ∧ d.mantissa.val < 10 ^ 19 := by
-- BODY
  intro s d hsource hne hparse
  have hlen : s.val.length <= 9223372036854775807 := by
    simpa [SourceValidByteSlice] using hsource
  have bind_ok_inv : forall {alpha beta : Type} (r : Result alpha)
      (f : alpha -> Result beta) y,
      Bind.bind r f = ok y -> exists x, r = ok x /\ f x = ok y := by
    intro alpha beta r f y h
    cases r with
    | fail err => simp [Bind.bind, Aeneas.Std.bind] at h
    | div => simp [Bind.bind, Aeneas.Std.bind] at h
    | ok x => exact ⟨x, rfl, h⟩
  have split_digits : forall xs : List Std.U8,
      exists digits tail,
        xs = 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)) := by
    intro xs
    induction xs with
    | nil => exact ⟨[], [], rfl, (by intro b hb; cases hb), (by
        intro b rest h
        cases h)⟩
    | cons b bs ih =>
      by_cases hb : 48 <= b.val /\ b.val <= 57
      · obtain ⟨digits, tail, hsplit, hdigits, htail⟩ := ih
        refine ⟨b :: digits, tail, by simp [hsplit], ?_, htail⟩
        intro c hc
        rcases List.mem_cons.mp hc with rfl | hc
        · exact hb
        · exact hdigits c hc
      · exact ⟨[], b :: bs, rfl, (by intro c hc; cases hc), (by
          intro c rest h
          cases h
          exact hb)⟩
  have parse_many_bound : forall (start intEnd : Slice Std.U8)
      (exp outExp : Std.I64) (mantissa : Std.U64),
      num.imp.dec2flt.parse.parse_many_digits start intEnd exp =
        ok (mantissa, outExp) ->
      mantissa.val <= 9999999999999999999 := by
    intro start intEnd exp outExp mantissa hmany
    obtain ⟨digits, tail, hsplit, hdigits, htail⟩ := split_digits start.val
    obtain ⟨rest, first, remaining, consumed, hfirst, hpartition,
        hrest, hfirstValue, hremainingDigits, hremainingLen, hfirstBound,
        hfirstLow, hfirstSharp⟩ :=
      NumberHelperTryParse19SafeLength start 0#u64 digits tail hsplit
        hdigits htail (by norm_num)
    unfold num.imp.dec2flt.parse.parse_many_digits at hmany
    rw [hfirst] at hmany
    by_cases hthreshold : first.val < 1000000000000000000
    · have hnotHigh : ¬ 1000000000000000000 <= first.val := by omega
      simp [num.imp.dec2flt.parse.MIN_19DIGIT_INT.eq_def,
        hnotHigh] at hmany
      cases hdrop :
          core.slice.index.SliceIndexRangeFromUsizeSlice.index
            { start := 1#usize } rest with
      | fail err =>
        rw [hdrop] at hmany
        simp at hmany
      | div =>
        rw [hdrop] at hmany
        simp at hmany
      | ok afterDrop =>
        rw [hdrop] at hmany
        simp at hmany
        obtain ⟨digits2, tail2, hsplit2, hdigits2, htail2⟩ :=
          split_digits afterDrop.val
        obtain ⟨rest2, second, remaining2, consumed2, hsecond,
            hpartition2, hrest2, hsecondValue, hremainingDigits2,
            hremainingLen2, hsecondBound, hsecondLow, hsecondSharp⟩ :=
          NumberHelperTryParse19SafeLength afterDrop first digits2 tail2
            hsplit2 hdigits2 htail2 hfirstBound
        rw [hsecond] at hmany
        simp at hmany
        cases hoff :
            SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
              rest2 afterDrop with
        | fail err => simp [hoff] at hmany
        | div => simp [hoff] at hmany
        | ok off =>
          simp [hoff] at hmany
          cases hneg : HNeg.hNeg off with
          | fail err => simp [hneg] at hmany
          | div => simp [hneg] at hmany
          | ok negOff =>
            simp [hneg] at hmany
            cases hcast : lift (IScalar.cast .I64 negOff) with
            | fail err => simp [hcast] at hmany
            | div => simp [hcast] at hmany
            | ok castOff =>
              simp [hcast] at hmany
              cases hadd : castOff + exp with
              | fail err => simp [hadd] at hmany
              | div => simp [hadd] at hmany
              | ok finalExp =>
                simp [hadd] at hmany
                rcases hmany with ⟨rfl, rfl⟩
                exact hsecondBound
    · have hhigh : 1000000000000000000 <= first.val := by omega
      simp [num.imp.dec2flt.parse.MIN_19DIGIT_INT.eq_def,
        hhigh] at hmany
      cases hoff :
          SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
            intEnd rest with
      | fail err => simp [hoff] at hmany
      | div => simp [hoff] at hmany
      | ok off =>
        simp [hoff] at hmany
        cases hcast : lift (IScalar.cast .I64 off) with
        | fail err => simp [hcast] at hmany
        | div => simp [hcast] at hmany
        | ok castOff =>
          simp [hcast] at hmany
          cases hadd : castOff + exp with
          | fail err => simp [hadd] at hmany
          | div => simp [hadd] at hmany
          | ok finalExp =>
            simp [hadd] at hmany
            rcases hmany with ⟨rfl, rfl⟩
            exact hfirstBound
  have record_branches : forall (total : Std.Isize)
      (shortMantissa : Std.U64) (intEnd : Slice Std.U8)
      (shortExponent retryExponent : Std.I64) (len outLen : Std.Usize),
      (total.val <= 19 -> shortMantissa.val <= 9999999999999999999) ->
      (forall (nMore adjusted : Std.Isize),
        total - 19#isize = ok nMore ->
        num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted ->
        Not (total.val <= 19) ->
        Not (0 < adjusted.val) ->
        shortMantissa.val <= 9999999999999999999) ->
      (if total.val <= 19 then
          ok (some
            (num.imp.dec2flt.decimal.Decimal.mk shortExponent shortMantissa
              false false, len))
        else do
          let nMore <- total - 19#isize
          let adjusted <-
            num.imp.dec2flt.parse.adjust_many_digit_count s nMore
          if 0 < adjusted.val then
            let (manyMantissa, manyExponent) <-
              num.imp.dec2flt.parse.parse_many_digits s intEnd retryExponent
            ok (some
              (num.imp.dec2flt.decimal.Decimal.mk manyExponent manyMantissa
                false true, len))
          else
            ok (some
              (num.imp.dec2flt.decimal.Decimal.mk shortExponent shortMantissa
                false false, len))) = ok (some (d, outLen)) ->
      d.negative = false /\ d.mantissa.val < 10 ^ 19 := by
    intro total shortMantissa intEnd shortExponent retryExponent len outLen
      hshort hnonpositive hresult
    by_cases hsmall : total.val <= 19
    · simp [hsmall] at hresult
      rcases hresult with ⟨rfl, rfl⟩
      refine ⟨rfl, ?_⟩
      have hb := hshort hsmall
      norm_num
      omega
    · simp [hsmall] at hresult
      obtain ⟨nMore, hsub, hresult⟩ := bind_ok_inv _ _ _ hresult
      obtain ⟨adjusted, hadjust, hresult⟩ := bind_ok_inv _ _ _ hresult
      by_cases hpositive : 0 < adjusted.val
      · simp only [if_pos hpositive] at hresult
        obtain ⟨manyResult, hmany, hresult⟩ :=
          bind_ok_inv _ _ _ hresult
        rcases manyResult with ⟨manyMantissa, manyExponent⟩
        rcases hresult with ⟨rfl, rfl⟩
        refine ⟨rfl, ?_⟩
        have hb := parse_many_bound s intEnd retryExponent manyExponent
          manyMantissa hmany
        norm_num
        omega
      · simp only [if_neg hpositive] at hresult
        rcases hresult with ⟨rfl, rfl⟩
        refine ⟨rfl, ?_⟩
        have hb := hnonpositive nMore adjusted hsub hadjust hsmall hpositive
        norm_num
        omega
  have fold_upper : forall (digits : List Std.U8) (a : Nat),
      (forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57) ->
      digits.foldl (fun x b => 10 * x + (b.val - 48)) a <
        (a + 1) * 10 ^ digits.length := by
    intro digits
    induction digits with
    | nil => intro a h; simp
    | cons b bs ih =>
      intro a h
      have hb := h b List.mem_cons_self
      have hbs : forall c, List.Mem c bs ->
          48 <= c.val /\ c.val <= 57 := by
        intro c hc
        exact h c (List.mem_cons_of_mem b hc)
      simp only [List.foldl_cons, List.length_cons, Nat.pow_succ]
      have hrec := ih (10 * a + (b.val - 48)) hbs
      have hcoeff : 10 * a + (b.val - 48) + 1 <= 10 * (a + 1) := by
        omega
      have hmul := Nat.mul_le_mul_right (10 ^ bs.length) hcoeff
      exact hrec.trans_le (by
        simpa [Nat.mul_assoc, Nat.mul_comm, Nat.mul_left_comm] using hmul)
  have fold_ge_start : forall (digits : List Std.U8) (a : Nat),
      a <= digits.foldl (fun x b => 10 * x + (b.val - 48)) a := by
    intro digits
    induction digits with
    | nil => intro a; rfl
    | cons b bs ih =>
      intro a
      apply le_trans (b := 10 * a + (b.val - 48))
      · omega
      · exact ih _
  have zero_fold : forall digits : List Std.U8,
      (forall b, List.Mem b digits -> b = 48#u8) ->
      digits.foldl (fun x b => 10 * x + (b.val - 48)) 0 = 0 := by
    intro digits hzero
    induction digits with
    | nil => rfl
    | cons b bs ih =>
      have hb := hzero b List.mem_cons_self
      have hbs : forall c, List.Mem c bs -> c = 48#u8 := by
        intro c hc
        exact hzero c (List.mem_cons_of_mem b hc)
      simp [hb, ih hbs]
  have split_zeros : forall digits : List Std.U8,
      exists zeros tail,
        digits = zeros ++ tail /\
        (forall b, List.Mem b zeros -> b = 48#u8) /\
        (tail = [] \/ exists b rest, tail = b :: rest /\ b ≠ 48#u8) := by
    intro digits
    induction digits with
    | nil => exact ⟨[], [], rfl, (by intro b hb; cases hb), Or.inl rfl⟩
    | cons b bs ih =>
      by_cases hb : b = 48#u8
      · obtain ⟨zeros, tail, hsplit, hzeros, htail⟩ := ih
        refine ⟨b :: zeros, tail, by simp [hsplit], ?_, htail⟩
        intro c hc
        rcases List.mem_cons.mp hc with rfl | hc
        · exact hb
        · exact hzeros c hc
      · exact ⟨[], b :: bs, rfl, (by intro c hc; cases hc),
          Or.inr ⟨b, bs, rfl, hb⟩⟩
  have count_zeros : forall zeros : List Std.U8,
      (forall b, List.Mem b zeros -> b = 48#u8) ->
      zeros.count 48#u8 = zeros.length := by
    intro zeros hzeros
    induction zeros with
    | nil => rfl
    | cons b bs ih =>
      have hb := hzeros b List.mem_cons_self
      have hbs : forall c, List.Mem c bs -> c = 48#u8 := by
        intro c hc
        exact hzeros c (List.mem_cons_of_mem b hc)
      simp [hb, ih hbs]
  have split_significand_zeros : forall
      (integer fraction suffix : List Std.U8),
      (forall b, List.Mem b integer -> 48 <= b.val /\ b.val <= 57) ->
      (forall b, List.Mem b fraction -> 48 <= b.val /\ b.val <= 57) ->
      Not (forall b, List.Mem b (integer ++ fraction) -> b = 48#u8) ->
      exists rawZeros rawTail sigZeros sigTail,
        integer ++ 46#u8 :: (fraction ++ suffix) = rawZeros ++ rawTail /\
        (forall b, List.Mem b rawZeros -> b = 48#u8 \/ b = 46#u8) /\
        (exists b rest, rawTail = b :: rest /\
          b ≠ 48#u8 /\ b ≠ 46#u8) /\
        integer ++ fraction = sigZeros ++ sigTail /\
        (forall b, List.Mem b sigZeros -> b = 48#u8) /\
        (exists b rest, sigTail = b :: rest /\ b ≠ 48#u8) /\
        rawZeros.count 48#u8 = sigZeros.length := by
    intro integer fraction suffix hinteger hfraction hnonzero
    obtain ⟨intZeros, intSig, hintSplit, hintZeros, hintSig⟩ :=
      split_zeros integer
    rcases hintSig with hintNil | ⟨b, intRest, hintSigEq, hb⟩
    · obtain ⟨fracZeros, fracSig, hfracSplit, hfracZeros, hfracSig⟩ :=
        split_zeros fraction
      rcases hfracSig with hfracNil | ⟨c, fracRest, hfracSigEq, hc⟩
      · exfalso
        apply hnonzero
        intro x hx
        rcases List.mem_append.mp hx with hx | hx
        · rw [hintSplit, hintNil] at hx
          exact hintZeros x (by simpa using hx)
        · rw [hfracSplit, hfracNil] at hx
          exact hfracZeros x (by simpa using hx)
      · refine ⟨intZeros ++ 46#u8 :: fracZeros,
          fracSig ++ suffix, intZeros ++ fracZeros, fracSig,
          ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
        · rw [hintSplit, hintNil, hfracSplit]
          simp [List.append_assoc]
        · intro x hx
          rcases List.mem_append.mp hx with hx | hx
          · exact Or.inl (hintZeros x hx)
          · rcases List.mem_cons.mp hx with rfl | hx
            · exact Or.inr rfl
            · exact Or.inl (hfracZeros x hx)
        · refine ⟨c, fracRest ++ suffix, by simp [hfracSigEq], hc, ?_⟩
          have hcdigit := hfraction c (by
            rw [hfracSplit, hfracSigEq]
            exact List.mem_append_right fracZeros List.mem_cons_self)
          intro hpoint
          subst c
          norm_num at hcdigit
        · rw [hintSplit, hintNil, hfracSplit]
          simp [List.append_assoc]
        · intro x hx
          rcases List.mem_append.mp hx with hx | hx
          · exact hintZeros x hx
          · exact hfracZeros x hx
        · exact ⟨c, fracRest, hfracSigEq, hc⟩
        · rw [List.count_append, List.count_cons,
            count_zeros intZeros hintZeros,
            count_zeros fracZeros hfracZeros]
          norm_num
    · refine ⟨intZeros, intSig ++ 46#u8 :: (fraction ++ suffix),
        intZeros, intSig ++ fraction, ?_, ?_, ?_, ?_, hintZeros, ?_, ?_⟩
      · rw [hintSplit]
        simp [List.append_assoc]
      · intro x hx
        exact Or.inl (hintZeros x hx)
      · refine ⟨b, intRest ++ 46#u8 :: (fraction ++ suffix),
          by simp [hintSigEq], hb, ?_⟩
        have hbdigit := hinteger b (by
          rw [hintSplit, hintSigEq]
          exact List.mem_append_right intZeros List.mem_cons_self)
        intro hpoint
        subst b
        norm_num at hbdigit
      · rw [hintSplit]
        simp [List.append_assoc]
      · exact ⟨b, intRest ++ fraction, by simp [hintSigEq], hb⟩
      · exact count_zeros intZeros hintZeros
  obtain ⟨intDigits, intTail, hsval, hintDigits, hintTail⟩ :=
    split_digits s.val
  have hintTailProp : intTail.length <= Usize.max := by
    have hp := s.property
    rw [hsval] at hp
    simp only [List.length_append] at hp
    omega
  let intRest : Slice Std.U8 := ⟨intTail, hintTailProp⟩
  obtain ⟨intMantissa, scannedIntRest, hscanInt, hscannedIntRest⟩ :=
    NumberHelperDigitScanSourceLength s 0#u64 intDigits intTail hsval
      hintDigits hintTail hlen
  have hscannedIntRestEq : scannedIntRest = intRest :=
    (Slice.eq_iff _ _).2 (by simpa [intRest] using hscannedIntRest)
  subst scannedIntRest
  obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ :=
    NumberHelperOffsetSourceLength s intRest hlen (by
      rw [hsval]
      simp [intRest])
  have hnDigitsEq : nDigits.val = intDigits.length := by
    rw [hnDigitsVal, hsval]
    simp [intRest]
  have hpartial : exists len,
      num.imp.dec2flt.parse.parse_partial_number s = ok (some (d, len)) := by
    unfold num.imp.dec2flt.parse.parse_number at hparse
    cases hp : num.imp.dec2flt.parse.parse_partial_number s with
    | fail err => simp [hp] at hparse
    | div => simp [hp] at hparse
    | ok result =>
      cases result with
      | none => simp [hp] at hparse
      | some pair =>
        rcases pair with ⟨record, len⟩
        simp [hp] at hparse
        split at hparse
        · have hrecord : record = d := by simpa using hparse
          subst d
          exact ⟨len, rfl⟩
        · simp at hparse
  obtain ⟨parsedLen, hpartial⟩ := hpartial
  cases intTail with
  | nil =>
    have hsplitInt : core.slice.Slice.split_first intRest = ok none := by
      simp [core.slice.Slice.split_first, ModelSliceSplitFirst, intRest]
    have haddZero : nDigits + 0#isize = ok nDigits := by
      have hb := IScalar.hBounds nDigits
      obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
        (IScalar.add_spec (x := nDigits) (y := 0#isize) (by
          simp [IScalar.min, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits]
          omega) (by
          simp [IScalar.max, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits] at hb ⊢
          omega))
      rw [hz]
      congr 2
      apply IScalar.eq_of_val_eq
      simpa using hzval
    have hfullOffset :
        SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
          intRest s = ok nDigits := by
      simpa [intRest] using hoffInt
    have hcastLen : lift (IScalar.hcast .Usize nDigits) =
        ok (Slice.len s) := by
      have hbound : 0 <= nDigits.val /\
          nDigits.val <= UScalar.max .Usize := by
        rw [hnDigitsEq]
        constructor
        · omega
        · have hp := s.property
          rw [hsval] at hp
          simpa [UScalar.max, Usize.max, Usize.numBits,
            Dec2Flt.configured_x86_64_numBits] using hp
      obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
        (IScalar.hcast_inBounds_spec .Usize nDigits hbound)
      rw [hz]
      congr 2
      apply UScalar.eq_of_val_eq
      rw [hnDigitsEq] at hzval
      have hslen : s.val.length = intDigits.length := by simp [hsval]
      simpa [Slice.len_val, hslen] using hzval
    unfold num.imp.dec2flt.parse.parse_partial_number at hpartial
    simp [hne, hscanInt, hoffInt, hsplitInt, haddZero,
      hcastLen] at hpartial
    by_cases hnzero : nDigits = 0#isize
    · simp [hnzero] at hpartial
    · simp [hnzero] at hpartial
      by_cases hsmall : nDigits.val <= 19
      · simp [hsmall] at hpartial
        rcases hpartial with ⟨rfl, rfl⟩
        constructor
        · rfl
        · have hfoldLt := fold_upper intDigits 0 hintDigits
          norm_num at hfoldLt
          have hlen19 : intDigits.length <= 19 := by
            omega
          have hbound : intDigits.foldl
              (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
            rw [U64.max_eq]
            have hp := hfoldLt.trans_le
              (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hlen19)
            norm_num at hp ⊢
            omega
          obtain ⟨value, rest, hexact, hrest, hvalue⟩ :=
            NumberHelperDigitScanExact s 0#u64 intDigits []
              (by simpa using hsval) hintDigits (by
                intro b rest h
                cases h) hlen hbound
          rw [hscanInt] at hexact
          simp at hexact
          rcases hexact with ⟨rfl, rfl⟩
          rw [hvalue]
          norm_num
          exact hfoldLt.trans_le
            (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hlen19)
      · have hlarge : 19 < nDigits.val := by omega
        have hsubBounds : IScalar.min .Isize <= nDigits.val - 19 /\
            nDigits.val - 19 <= IScalar.max .Isize := by
          have hb := IScalar.hBounds nDigits
          simp [IScalar.min, IScalar.max, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits] at hb ⊢
          omega
        obtain ⟨nMore, hnMore, hnMoreVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.sub_spec (x := nDigits) (y := 19#isize)
            (by norm_num; omega) (by norm_num; omega))
        norm_num at hnMoreVal
        simp [hsmall, hnMore] at hpartial
        obtain ⟨zeros, nonzeroTail, hzeroSplit, hzeros, hzeroTail⟩ :=
          split_zeros intDigits
        have hcount := count_zeros zeros hzeros
        have hsAdjust : s.val = zeros ++ nonzeroTail := by
          simpa [hzeroSplit] using hsval
        have htailSharp : nonzeroTail = [] \/ exists b rest,
            nonzeroTail = b :: rest /\ b ≠ 48#u8 /\ b ≠ 46#u8 := by
          rcases hzeroTail with hnil | ⟨b, rest, htailEq, hb⟩
          · exact Or.inl hnil
          · right
            refine ⟨b, rest, htailEq, hb, ?_⟩
            have hbdigit := hintDigits b (by
              rw [hzeroSplit, htailEq]
              exact List.mem_append_right zeros List.mem_cons_self)
            intro heq
            subst b
            norm_num at hbdigit
        have hnMoreNonneg : 0 <= nMore.val := by
          rw [hnMoreVal]
          omega
        have hnMoreLe : nMore.val <= s.val.length := by
          rw [hnMoreVal, hnDigitsEq, hsval]
          simp only [List.length_append, List.length_nil, Nat.add_zero]
          omega
        have hadjustLower : IScalar.min .Isize <=
            nMore.val - (zeros.count 48#u8 : Int) := by
          rw [hnMoreVal, hcount, hnDigitsEq]
          have hzerosLen : zeros.length <= intDigits.length := by
            rw [hzeroSplit]
            simp
          simp [IScalar.min, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits]
          omega
        obtain ⟨adjusted, hadjust, hadjustVal⟩ :=
          NumberHelperAdjustManyExact s nMore zeros nonzeroTail hsAdjust
            (by intro b hb; exact Or.inl (hzeros b hb)) htailSharp hlen
            hnMoreNonneg hnMoreLe hadjustLower
        rw [hadjust] at hpartial
        by_cases hpositive : 0 < adjusted.val
        · simp [hpositive] at hpartial
          cases hmany :
              num.imp.dec2flt.parse.parse_many_digits s intRest 0#i64 with
          | fail err => simp [hmany] at hpartial
          | div => simp [hmany] at hpartial
          | ok result =>
            rcases result with ⟨manyMantissa, manyExponent⟩
            simp [hmany] at hpartial
            rcases hpartial with ⟨rfl, rfl⟩
            refine ⟨rfl, ?_⟩
            change manyMantissa.val < 10 ^ 19
            have hb := parse_many_bound s intRest 0#i64 manyExponent
              manyMantissa hmany
            omega
        · simp [hpositive] at hpartial
          have htailLen : nonzeroTail.length <= 19 := by
            rw [hadjustVal, hnMoreVal, hcount, hnDigitsEq] at hpositive
            have hlenEq := congrArg List.length hzeroSplit
            simp only [List.length_append] at hlenEq
            omega
          have htailDigits : forall b, List.Mem b nonzeroTail ->
              48 <= b.val /\ b.val <= 57 := by
            intro b hb
            apply hintDigits b
            rw [hzeroSplit]
            exact List.mem_append_right zeros hb
          have hzeroFold := zero_fold zeros hzeros
          have hfoldLt : intDigits.foldl
              (fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 := by
            rcases hzeroTail with hnil | hcons
            · subst nonzeroTail
              rw [hzeroSplit, List.append_nil, hzeroFold]
              norm_num
            · rw [hzeroSplit, List.foldl_append, hzeroFold]
              have htailLt := fold_upper nonzeroTail 0 htailDigits
              norm_num at htailLt
              exact htailLt.trans_le
                (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat))
                  htailLen)
          have hfoldBound : intDigits.foldl
              (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
            rw [U64.max_eq]
            omega
          obtain ⟨value, rest, hexact, hrest, hvalue⟩ :=
            NumberHelperDigitScanExact s 0#u64 intDigits []
              (by simpa using hsval) hintDigits (by
                intro b rest h
                cases h) hlen hfoldBound
          rw [hscanInt] at hexact
          simp at hexact
          rcases hexact with ⟨rfl, rfl⟩
          rcases hpartial with ⟨rfl, rfl⟩
          refine ⟨rfl, ?_⟩
          norm_num at hvalue ⊢
          rw [hvalue]
          exact hfoldLt
  | cons first tail =>
    have htailProp : tail.length <= Usize.max := by
      have hp := intRest.property
      simp [intRest] at hp ⊢
      omega
    let afterFirst : Slice Std.U8 := ⟨tail, htailProp⟩
    have hsplitInt : core.slice.Slice.split_first intRest =
        ok (some (first, afterFirst)) := by
      simp [core.slice.Slice.split_first, ModelSliceSplitFirst,
        intRest, afterFirst]
    by_cases hpoint : first = 46#u8
    · subst first
      obtain ⟨fracDigits, fracTail, hfracSplit, hfracDigits, hfracTail⟩ :=
        split_digits tail
      have hfracTailProp : fracTail.length <= Usize.max := by
        have hp := afterFirst.property
        simp [afterFirst, hfracSplit] at hp ⊢
        omega
      let fracRest : Slice Std.U8 := ⟨fracTail, hfracTailProp⟩
      have hlenAfter : afterFirst.val.length <= 9223372036854775807 := by
        simp [afterFirst]
        rw [hsval] at hlen
        simp only [List.length_append, List.length_cons] at hlen
        omega
      obtain ⟨fracMantissa, scannedFracRest, hscanFrac, hscannedFracRest⟩ :=
        NumberHelperDigitScanSourceLength afterFirst intMantissa fracDigits
          fracTail (by simp [afterFirst, hfracSplit]) hfracDigits hfracTail
            hlenAfter
      have hscannedFracRestEq : scannedFracRest = fracRest :=
        (Slice.eq_iff _ _).2 (by
          simpa [fracRest] using hscannedFracRest)
      subst scannedFracRest
      obtain ⟨nAfter, hoffFrac, hnAfterVal⟩ :=
        NumberHelperOffsetSourceLength afterFirst fracRest hlenAfter (by
          simp [afterFirst, fracRest, hfracSplit])
      have hnAfterEq : nAfter.val = fracDigits.length := by
        rw [hnAfterVal]
        simp [afterFirst, fracRest, hfracSplit]
      have hnAfterNotMin : nAfter.val ≠ IScalar.min .Isize := by
        rw [hnAfterEq, IScalar.min]
        simp [IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits]
      obtain ⟨negAfter, hnegAfter, hnegAfterVal⟩ :=
        Aeneas.Std.WP.spec_imp_exists
          (HNeg.hNeg.step nAfter hnAfterNotMin)
      have hnegCastBounds : IScalar.min .I64 <= negAfter.val /\
          negAfter.val <= IScalar.max .I64 := by
        rw [hnegAfterVal, hnAfterEq,
          IScalar.min_IScalarTy_I64_eq, I64.min_eq,
          IScalar.max_IScalarTy_I64_eq, I64.max_eq]
        constructor <;> omega
      obtain ⟨fracExponent, hfracExponent, hfracExponentVal⟩ :=
        Aeneas.Std.WP.spec_imp_exists
          (IScalar.cast_inBounds_spec .I64 negAfter hnegCastBounds)
      have htotalBounds : IScalar.min .Isize <=
          nDigits.val + nAfter.val /\
          nDigits.val + nAfter.val <= IScalar.max .Isize := by
        rw [hnDigitsEq, hnAfterEq]
        simp [IScalar.min, IScalar.max, IScalarTy.numBits,
          Dec2Flt.configured_x86_64_numBits]
        rw [hsval, hfracSplit] at hlen
        simp only [List.length_append, List.length_cons] at hlen
        constructor <;> omega
      obtain ⟨totalDigits, htotal, htotalVal⟩ :=
        Aeneas.Std.WP.spec_imp_exists
          (IScalar.add_spec htotalBounds.1 htotalBounds.2)
      have hsigDigits : forall b,
          List.Mem b (intDigits ++ fracDigits) ->
          48 <= b.val /\ b.val <= 57 := by
        intro b hb
        rcases List.mem_append.mp hb with hb | hb
        · exact hintDigits b hb
        · exact hfracDigits b hb
      have hexactScans :
          (intDigits ++ fracDigits).foldl
              (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max ->
          fracMantissa.val = (intDigits ++ fracDigits).foldl
            (fun x b => 10 * x + (b.val - 48)) 0 := by
        intro hfullBound
        have hprefixLe := fold_ge_start fracDigits
          (intDigits.foldl (fun x b => 10 * x + (b.val - 48)) 0)
        rw [← List.foldl_append] at hprefixLe
        have hintBound : intDigits.foldl
            (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max :=
          hprefixLe.trans hfullBound
        obtain ⟨intValue, intScanRest, hexactInt, hexactIntRest,
            hintValue⟩ :=
          NumberHelperDigitScanExact s 0#u64 intDigits
            (46#u8 :: tail) (by simpa using hsval) hintDigits hintTail
              hlen hintBound
        rw [hscanInt] at hexactInt
        simp at hexactInt
        rcases hexactInt with ⟨rfl, rfl⟩
        have hfracBound : fracDigits.foldl
            (fun x b => 10 * x + (b.val - 48)) intMantissa.val <=
              U64.max := by
          norm_num at hintValue
          rw [hintValue, ← List.foldl_append]
          exact hfullBound
        obtain ⟨fracValue, fracScanRest, hexactFrac, hexactFracRest,
            hfracValue⟩ :=
          NumberHelperDigitScanExact afterFirst intMantissa fracDigits
            fracTail (by simp [afterFirst, hfracSplit]) hfracDigits
              hfracTail hlenAfter hfracBound
        rw [hscanFrac] at hexactFrac
        simp at hexactFrac
        rcases hexactFrac with ⟨rfl, rfl⟩
        norm_num at hintValue hfracValue ⊢
        rw [hfracValue, hintValue]
      have hshort : totalDigits.val <= 19 ->
          fracMantissa.val <= 9999999999999999999 := by
        intro hsmall
        have hsigLen : intDigits.length + fracDigits.length <= 19 := by
          rw [htotalVal, hnDigitsEq, hnAfterEq] at hsmall
          omega
        have hfoldLt := fold_upper (intDigits ++ fracDigits) 0 hsigDigits
        norm_num at hfoldLt
        have hfold19 := hfoldLt.trans_le
          (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hsigLen)
        have hfoldBound : (intDigits ++ fracDigits).foldl
            (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
          rw [U64.max_eq]
          norm_num
          omega
        rw [hexactScans hfoldBound]
        norm_num
        omega
      have hnonpositive : forall (nMore adjusted : Std.Isize),
          totalDigits - 19#isize = ok nMore ->
          num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted ->
          Not (totalDigits.val <= 19) ->
          Not (0 < adjusted.val) ->
          fracMantissa.val <= 9999999999999999999 := by
        intro nMore adjusted hsub hadjust hlarge hnonpos
        by_cases hallzero : forall b,
            List.Mem b (intDigits ++ fracDigits) -> b = 48#u8
        · have hfoldZero := zero_fold (intDigits ++ fracDigits) hallzero
          have hfoldBound : (intDigits ++ fracDigits).foldl
              (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
            rw [hfoldZero]
            exact Nat.zero_le _
          rw [hexactScans hfoldBound, hfoldZero]
          norm_num
        · obtain ⟨rawZeros, rawTail, sigZeros, sigTail, hrawSplit,
              hrawZeros, hrawTail, hsigSplit, hsigZeros, hsigTail, hcount⟩ :=
            split_significand_zeros intDigits fracDigits fracTail
              hintDigits hfracDigits hallzero
          have hsAdjust : s.val = rawZeros ++ rawTail := by
            rw [hsval, hfracSplit]
            exact hrawSplit
          have hnMoreVal : nMore.val = totalDigits.val - 19 := by
            have heq := IScalar.sub_equiv totalDigits 19#isize
            rw [hsub] at heq
            have hval := heq.2.1
            norm_num at hval ⊢
            exact hval
          have hnMoreNonneg : 0 <= nMore.val := by
            rw [hnMoreVal]
            omega
          have hnMoreLe : nMore.val <= s.val.length := by
            rw [hnMoreVal, htotalVal, hnDigitsEq, hnAfterEq, hsval,
              hfracSplit]
            simp only [List.length_append, List.length_cons]
            omega
          have hadjustLower : IScalar.min .Isize <=
              nMore.val - (rawZeros.count 48#u8 : Int) := by
            rw [hnMoreVal, hcount, htotalVal, hnDigitsEq, hnAfterEq]
            have hzlen : sigZeros.length <=
                (intDigits ++ fracDigits).length := by
              rw [hsigSplit]
              simp
            have hsigSource : (intDigits ++ fracDigits).length <=
                s.val.length := by
              rw [hsval, hfracSplit]
              simp only [List.length_append, List.length_cons]
              omega
            have hzBound : sigZeros.length <= 9223372036854775807 :=
              hzlen.trans (hsigSource.trans hlen)
            simp [IScalar.min, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits]
            omega
          obtain ⟨adjusted2, hadjust2, hadjust2Val⟩ :=
            NumberHelperAdjustManyExact s nMore rawZeros rawTail hsAdjust
              hrawZeros (Or.inr hrawTail) hlen hnMoreNonneg hnMoreLe
                hadjustLower
          rw [hadjust] at hadjust2
          simp at hadjust2
          subst adjusted2
          have hsigTailLen : sigTail.length <= 19 := by
            rw [hadjust2Val, hnMoreVal, hcount, htotalVal, hnDigitsEq,
              hnAfterEq] at hnonpos
            have hlenEq := congrArg List.length hsigSplit
            simp only [List.length_append] at hlenEq
            omega
          have hsigTailDigits : forall b, List.Mem b sigTail ->
              48 <= b.val /\ b.val <= 57 := by
            intro b hb
            apply hsigDigits b
            rw [hsigSplit]
            exact List.mem_append_right sigZeros hb
          have hzeroFold := zero_fold sigZeros hsigZeros
          have htailLt := fold_upper sigTail 0 hsigTailDigits
          norm_num at htailLt
          have hfoldLt : (intDigits ++ fracDigits).foldl
              (fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 := by
            rw [hsigSplit, List.foldl_append, hzeroFold]
            exact htailLt.trans_le
              (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat))
                hsigTailLen)
          have hfoldBound : (intDigits ++ fracDigits).foldl
              (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
            exact (Nat.le_of_lt hfoldLt).trans (by
              norm_num [U64.max_eq])
          rw [hexactScans hfoldBound]
          norm_num at hfoldLt ⊢
          omega
      unfold num.imp.dec2flt.parse.parse_partial_number at hpartial
      simp [hne, hscanInt, hoffInt, hsplitInt, hscanFrac, hoffFrac,
        hnegAfter, hfracExponent] at hpartial
      simp only [UScalar.ofNatCore] at hpartial
      simp [htotal] at hpartial
      by_cases hzero : totalDigits = 0#isize
      · simp [hzero] at hpartial
      · simp [hzero] at hpartial
        cases fracTail with
        | nil =>
          have hsplitFrac : core.slice.Slice.split_first fracRest =
              ok none := by
            simp [core.slice.Slice.split_first, ModelSliceSplitFirst,
              fracRest]
          simp [hsplitFrac] at hpartial
          obtain ⟨off, hoff, hpartial⟩ := bind_ok_inv _ _ _ hpartial
          obtain ⟨len, hcast, hpartial⟩ := bind_ok_inv _ _ _ hpartial
          exact record_branches totalDigits fracMantissa intRest fracExponent
            0#i64 len parsedLen hshort hnonpositive hpartial
        | cons marker suffix =>
          have hsuffixProp : suffix.length <= Usize.max := by
            have hp := fracRest.property
            simp [fracRest] at hp ⊢
            omega
          let afterMarker : Slice Std.U8 := ⟨suffix, hsuffixProp⟩
          have hsplitFrac : core.slice.Slice.split_first fracRest =
              ok (some (marker, afterMarker)) := by
            simp [core.slice.Slice.split_first, ModelSliceSplitFirst,
              fracRest, afterMarker]
          simp [hsplitFrac] at hpartial
          by_cases hlower : marker = (⟨101#8⟩ : Std.U8)
          · subst marker
            obtain ⟨sciResult, hsci, hpartial⟩ :=
              bind_ok_inv _ _ _ hpartial
            rcases sciResult with ⟨optExp, sciRest⟩
            cases optExp with
            | none =>
              simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
                ModelOptionBranch,
                core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual,
                ModelOptionFromResidual] at hpartial
            | some parsedExp =>
              simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
                ModelOptionBranch] at hpartial
              obtain ⟨combined, hsum, hpartial⟩ :=
                bind_ok_inv _ _ _ hpartial
              obtain ⟨off, hoff, hpartial⟩ :=
                bind_ok_inv _ _ _ hpartial
              obtain ⟨len, hcast, hpartial⟩ :=
                bind_ok_inv _ _ _ hpartial
              exact record_branches totalDigits fracMantissa intRest combined
                parsedExp len parsedLen hshort hnonpositive hpartial
          · by_cases hupper : marker = (⟨69#8⟩ : Std.U8)
            · subst marker
              obtain ⟨sciResult, hsci, hpartial⟩ :=
                bind_ok_inv _ _ _ hpartial
              rcases sciResult with ⟨optExp, sciRest⟩
              cases optExp with
              | none =>
                simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
                  ModelOptionBranch,
                  core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual,
                  ModelOptionFromResidual] at hpartial
              | some parsedExp =>
                simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
                  ModelOptionBranch] at hpartial
                obtain ⟨combined, hsum, hpartial⟩ :=
                  bind_ok_inv _ _ _ hpartial
                obtain ⟨off, hoff, hpartial⟩ :=
                  bind_ok_inv _ _ _ hpartial
                obtain ⟨len, hcast, hpartial⟩ :=
                  bind_ok_inv _ _ _ hpartial
                exact record_branches totalDigits fracMantissa intRest combined
                  parsedExp len parsedLen hshort hnonpositive hpartial
            · simp [hlower, hupper] at hpartial
              obtain ⟨off, hoff, hpartial⟩ := bind_ok_inv _ _ _ hpartial
              obtain ⟨len, hcast, hpartial⟩ := bind_ok_inv _ _ _ hpartial
              exact record_branches totalDigits fracMantissa intRest
                fracExponent 0#i64 len parsedLen hshort hnonpositive hpartial
    · have haddZero : nDigits + 0#isize = ok nDigits := by
        have hb := IScalar.hBounds nDigits
        obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.add_spec (x := nDigits) (y := 0#isize) (by
            simp [IScalar.min, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits]
            omega) (by
            simp [IScalar.max, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits] at hb ⊢
            omega))
        rw [hz]
        congr 2
        apply IScalar.eq_of_val_eq
        simpa using hzval
      have hshort : nDigits.val <= 19 ->
          intMantissa.val <= 9999999999999999999 := by
        intro hsmall
        have hlen19 : intDigits.length <= 19 := by omega
        have hfoldLt := fold_upper intDigits 0 hintDigits
        norm_num at hfoldLt
        have hfold19 : intDigits.foldl
            (fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 :=
          hfoldLt.trans_le
            (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hlen19)
        have hfoldBound : intDigits.foldl
            (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
          rw [U64.max_eq]
          omega
        obtain ⟨value, rest, hexact, hrest, hvalue⟩ :=
          NumberHelperDigitScanExact s 0#u64 intDigits (first :: tail)
            (by simpa using hsval) hintDigits hintTail hlen hfoldBound
        rw [hscanInt] at hexact
        simp at hexact
        rcases hexact with ⟨rfl, rfl⟩
        norm_num at hvalue ⊢
        rw [hvalue]
        omega
      have hnonpositive : forall (nMore adjusted : Std.Isize),
          nDigits - 19#isize = ok nMore ->
          num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted ->
          Not (nDigits.val <= 19) ->
          Not (0 < adjusted.val) ->
          intMantissa.val <= 9999999999999999999 := by
        intro nMore adjusted hsub hadjust hlarge hnonpos
        have hnMoreVal : nMore.val = nDigits.val - 19 := by
          have heq := IScalar.sub_equiv nDigits 19#isize
          rw [hsub] at heq
          have hval := heq.2.1
          norm_num at hval ⊢
          exact hval
        obtain ⟨zeros, nzTail, hzeroSplit, hzeros, hnzTail⟩ :=
          split_zeros intDigits
        have hcount := count_zeros zeros hzeros
        let rawTail : List Std.U8 := nzTail ++ first :: tail
        have hsAdjust : s.val = zeros ++ rawTail := by
          rw [hsval, hzeroSplit]
          simp [rawTail, List.append_assoc]
        have hrawTail : rawTail = [] \/ exists b rest,
            rawTail = b :: rest /\ b ≠ 48#u8 /\ b ≠ 46#u8 := by
          right
          rcases hnzTail with hnil | ⟨b, rest, heq, hb⟩
          · subst nzTail
            refine ⟨first, tail, by simp [rawTail], ?_, hpoint⟩
            intro hzero
            subst first
            have hnot := hintTail 48#u8 tail rfl
            norm_num at hnot
          · refine ⟨b, rest ++ first :: tail, by simp [rawTail, heq], hb, ?_⟩
            have hbdigit := hintDigits b (by
              rw [hzeroSplit, heq]
              exact List.mem_append_right zeros List.mem_cons_self)
            intro heq46
            subst b
            norm_num at hbdigit
        have hnMoreNonneg : 0 <= nMore.val := by
          rw [hnMoreVal]
          omega
        have hnMoreLe : nMore.val <= s.val.length := by
          rw [hnMoreVal, hnDigitsEq, hsval]
          simp only [List.length_append, List.length_cons]
          omega
        have hadjustLower : IScalar.min .Isize <=
            nMore.val - (zeros.count 48#u8 : Int) := by
          rw [hnMoreVal, hcount, hnDigitsEq]
          have hzlen : zeros.length <= intDigits.length := by
            rw [hzeroSplit]
            simp
          simp [IScalar.min, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits]
          omega
        obtain ⟨adjusted2, hadjust2, hadjust2Val⟩ :=
          NumberHelperAdjustManyExact s nMore zeros rawTail hsAdjust
            (by intro b hb; exact Or.inl (hzeros b hb)) hrawTail hlen
              hnMoreNonneg hnMoreLe hadjustLower
        rw [hadjust] at hadjust2
        simp at hadjust2
        subst adjusted2
        have hnzLen : nzTail.length <= 19 := by
          rw [hadjust2Val, hnMoreVal, hcount, hnDigitsEq] at hnonpos
          have hlenEq := congrArg List.length hzeroSplit
          simp only [List.length_append] at hlenEq
          omega
        have hnzDigits : forall b, List.Mem b nzTail ->
            48 <= b.val /\ b.val <= 57 := by
          intro b hb
          apply hintDigits b
          rw [hzeroSplit]
          exact List.mem_append_right zeros hb
        have hzeroFold := zero_fold zeros hzeros
        have hfoldLt : intDigits.foldl
            (fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 := by
          rcases hnzTail with hnil | hcons
          · subst nzTail
            rw [hzeroSplit, List.append_nil, hzeroFold]
            norm_num
          · rw [hzeroSplit, List.foldl_append, hzeroFold]
            have htailLt := fold_upper nzTail 0 hnzDigits
            norm_num at htailLt
            exact htailLt.trans_le
              (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hnzLen)
        have hfoldBound : intDigits.foldl
            (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
          rw [U64.max_eq]
          omega
        obtain ⟨value, rest, hexact, hrest, hvalue⟩ :=
          NumberHelperDigitScanExact s 0#u64 intDigits (first :: tail)
            (by simpa using hsval) hintDigits hintTail hlen hfoldBound
        rw [hscanInt] at hexact
        simp at hexact
        rcases hexact with ⟨rfl, rfl⟩
        norm_num at hvalue ⊢
        rw [hvalue]
        omega
      have hpointVal : first.val ≠ 46 := by
        intro hval
        apply hpoint
        exact UScalar.eq_of_val_eq hval
      unfold num.imp.dec2flt.parse.parse_partial_number at hpartial
      simp [hne, hscanInt, hoffInt, hsplitInt] at hpartial
      split at hpartial
      all_goals try { exfalso; apply hpointVal; rfl }
      simp [haddZero] at hpartial
      by_cases hnzero : nDigits = 0#isize
      · simp [hnzero] at hpartial
      · simp [hnzero, hsplitInt] at hpartial
        by_cases hlower : first = 101#u8
        · subst first
          obtain ⟨sciResult, hsci, hpartial⟩ :=
            bind_ok_inv _ _ _ hpartial
          rcases sciResult with ⟨optExp, sciRest⟩
          cases optExp with
          | none =>
            simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
              ModelOptionBranch,
              core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual,
              ModelOptionFromResidual] at hpartial
          | some parsedExp =>
            simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
              ModelOptionBranch] at hpartial
            obtain ⟨combined, hsum, hpartial⟩ :=
              bind_ok_inv _ _ _ hpartial
            obtain ⟨off, hoff, hpartial⟩ :=
              bind_ok_inv _ _ _ hpartial
            obtain ⟨len, hcast, hpartial⟩ :=
              bind_ok_inv _ _ _ hpartial
            exact record_branches nDigits intMantissa intRest combined
              parsedExp len parsedLen hshort hnonpositive hpartial
        · by_cases hupper : first = 69#u8
          · simp [hlower] at hpartial
            subst first
            obtain ⟨sciResult, hsci, hpartial⟩ :=
              bind_ok_inv _ _ _ hpartial
            rcases sciResult with ⟨optExp, sciRest⟩
            cases optExp with
            | none =>
              simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
                ModelOptionBranch,
                core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual,
                ModelOptionFromResidual] at hpartial
            | some parsedExp =>
              simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
                ModelOptionBranch] at hpartial
              obtain ⟨combined, hsum, hpartial⟩ :=
                bind_ok_inv _ _ _ hpartial
              obtain ⟨off, hoff, hpartial⟩ :=
                bind_ok_inv _ _ _ hpartial
              obtain ⟨len, hcast, hpartial⟩ :=
                bind_ok_inv _ _ _ hpartial
              exact record_branches nDigits intMantissa intRest combined
                parsedExp len parsedLen hshort hnonpositive hpartial
          · simp [hlower, hupper] at hpartial
            rw [hoffInt] at hpartial
            simp at hpartial
            obtain ⟨len, hcast, hpartial⟩ :=
              bind_ok_inv _ _ _ hpartial
            exact record_branches nDigits intMantissa intRest 0#i64 0#i64
              len parsedLen hshort hnonpositive hpartial

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (71)
More program dependencies (36)
Standard-library dependencies (32)

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 56 entries
Technical checking records
Local closure summary
{
  "node": "NumberHelperRecordBounds",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "698d95ba07f02035f8d7078a273eb58fe7fcf975bbd30c3803a8957602efb8cb",
  "active_statement_hash": "273d7ce693720aade46ebfbdca0ec4027ee04613fd6ec39e8e6b91072393eeb7",
  "kernel_axioms": [
    "Classical.choice",
    "Dec2Flt.configured_x86_64_numBits",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "ab64d13f256386ee0e494a5abcc6a5db49512cf3d90bbdfcabd6327793a31f95",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-140",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "ModelSliceSplitFirst": "f617ee05dd2925a9",
    "NumberHelperAdjustManyExact": "8999fa99d4681219",
    "NumberHelperDigitScanExact": "6bcf20c61d10dea3",
    "NumberHelperDigitScanSourceLength": "8892b939bba0ecf9",
    "NumberHelperOffsetSourceLength": "de0168dfeba3b802",
    "NumberHelperTryParse19SafeLength": "dd334c60b1e032a8",
    "num_imp_dec2flt_parse_MIN_19DIGIT_INT": "8d99761093f40bc7",
    "num_imp_dec2flt_parse_parse_many_digits": "faf0081b4c91301b",
    "num_imp_dec2flt_parse_parse_partial_number": "004e715471c389b8"
  },
  "strict_definition_deps": {
    "ModelOptionBranch": "2b227898dbe15e59",
    "ModelOptionFromResidual": "99bee9493878565f",
    "Preamble": "7754ffaf671693bf",
    "SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from": "7e7284dc9bc88830",
    "SourceValidByteSlice": "3dc42716a6378d26",
    "num_imp_dec2flt_decimal_Decimal": "c43614ff2149ecf2",
    "num_imp_dec2flt_parse_MIN_19DIGIT_INT": "476edc5502ff7bfb",
    "num_imp_dec2flt_parse_adjust_many_digit_count": "4e59e3776b6cfdaa",
    "num_imp_dec2flt_parse_parse_many_digits": "50c9ef13b3ed7fe8",
    "num_imp_dec2flt_parse_parse_number": "b8693ec58f01c43c",
    "num_imp_dec2flt_parse_parse_partial_number": "359d30eefc709c2a",
    "num_imp_dec2flt_parse_parse_scientific": "447f722d5841d2c5",
    "num_imp_dec2flt_parse_try_parse_19digits": "c9c74554a359a9c4",
    "num_imp_dec2flt_parse_try_parse_digits": "238b4a73cd46fc56"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:b3ecdea1460097640039f56fe7799499544fbf26dee60f744e8a4b6e16337660",
  "current_fingerprint": "sound-v2:b3ecdea1460097640039f56fe7799499544fbf26dee60f744e8a4b6e16337660",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}