P13_T1_NumberHelperRecordBounds

Lean checked

Statement

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

Theorem

For every nonempty source-valid byte slice, any normally returned record d from the specialized parse_number has d.\mathit{negative}=\texttt{false} and d.\mathit{mantissa}<10^{19}. Source validity is SourceValidByteSlice, without a UTF-8 condition. The actual call is ActualDispatchOperations. There is no numeric-grammar, A, B, reachability, or record-well-formedness premise; only normal Some return is required.

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.NumberHelperDigitScanExact
import Tablet.NumberHelperDigitScanSourceLength
import Tablet.NumberHelperTryParse19SafeLength
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
import Tablet.SourceValidByteSlice
import Tablet.P9_T1_ActualDispatchOperations

import Tablet.P364_T1_NumberHelperOffsetSourceLength
import Tablet.P364_T1_NumberHelperAdjustCountExact
import Tablet.P364_T1_NumberHelperManyDigitBound
import Tablet.P364_T1_NumberHelperDigitPrefix
import Tablet.NumberHelperDecimalPrefixInterval
import Tablet.DecimalDigitTrimDecomposition

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P13_T1_NumberHelperRecordBounds]
theorem P13_T1_NumberHelperRecordBounds :
    forall (s : Slice Std.U8) (d : num.imp.dec2flt.decimal.Decimal),
      SourceValidByteSlice s -> s.val ≠ [] ->
      P9_T1_ActualDispatchOperations.parseNumber 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

Put C=2^{63}-1 and M=10^{19}-1<2^{64}-1. By SourceValidByteSlice, |s|\leq C, and by the approved equation in Assumptions, pointer-sized scalars have width 64. The actual call in ActualDispatchOperations is the extracted num imp dec2flt parse parse number. Its normal Some result implies a normal Some result (d,\ell) of num imp dec2flt parse parse partial number: failure, divergence, or None propagates to a result different from the assumed one, and the wrapper's remaining-length equality check can only retain the very same record or return None. We prove the stronger field conclusion for this partial-parser result.
Throughout, inversion of a monadic bind with normal result supplies a normal result of its first operation and of its continuation. This follows by case analysis on ok, fail, and divergence, since the latter two propagate. In particular, later exponent and length operations need not succeed on every input: only their executed normal branches can contribute a record under the given hypothesis.
Split the bytes of s as IT using NumberHelperDigitPrefix. Here I consists of digits and T is empty or starts with a nondigit. By NumberHelperDigitScanSourceLength, the initial scan from zero returns normally with remainder T, even if its wrapping accumulator is not yet known to equal the ordinary decimal value. The suffix is a valid model slice because its length is at most the original length. By NumberHelperOffsetSourceLength, the next offset returns the exact integer |I|. The split-first operation has exactly the empty or head-and-tail behavior of ModelSliceSplitFirst. If its first byte is a dot, write the following slice as FR by NumberHelperDigitPrefix. This slice is no longer than s, so NumberHelperDigitScanSourceLength again applies, now starting from the integer scan's accumulator. Its remainder is R, and NumberHelperOffsetSourceLength gives the exact fractional count |F|. Negating this count is safe: it lies in [0,C], not at the signed minimum. Its negative lies in [-C,0], so the cast to i64 is exact and supplies exponent -|F|. Without a dot take F=[], retain the integer accumulator and remainder, and use exponent zero. In either case the checked sum of the counts is exactly N=|I|+|F|, with 0\leq N\leq |s|\leq C. Thus the signed addition is safe; in the no-dot case it is addition of zero. If N=0, the partial parser returns None, contradicting its assumed Some result.
Let D=IF and let V be the ordinary natural-number decimal fold of D from zero, with each byte b decoded as b-48. We use two elementary consequences of existing support. The upper endpoint in NumberHelperDecimalPrefixInterval, applied with empty head, the decoded digits as suffix, and exponent zero, gives V<10^{|D|}. Decoding preserves lengths and concatenation, and the mapped left fold is precisely the byte fold used here. Each decoded digit is below ten. Also each fold update a\mapsto10a+(b-48) is at least a, so every fold prefix is at most the whole fold; induction on the remaining digits proves this without a machine-arithmetic assumption. Whenever V\leq2^{64}-1, apply NumberHelperDigitScanExact to I and then F. The first fold is bounded by V, all slices have length at most C, and their stopping tails satisfy the required nondigit condition. Consequently both returned accumulators equal their ordinary folds, and the retained accumulator equals V. This is an application of the exact scan's bounded-value conclusion, not an assumption that a wrapping scan is always exact.
If N\leq19, then V<10^N\leq10^{19}, hence V\leq M. The preceding exactness argument therefore identifies the retained accumulator with V and proves its required bound.
Suppose N>19. The checked subtraction N-19 is within [0,C] and gives a word n with this exact value. To analyze the count adjustment, separate the leading zero digits of D from its significant suffix U. This decomposition can be obtained from DecimalDigitTrimDecomposition on the decoded digit list: its leading-zero decomposition and nonzero-head clause identify precisely the maximal initial zero prefix. On digit bytes, decoded value zero is equivalent to byte 48, so it lifts back to the byte list. Every all-zero byte prefix has decimal fold zero and its zero count equals its length, by induction on that prefix.
If D is all zero, its fold V=0 is in bounds, and the exact scans give retained accumulator zero. Retention leaves it bounded; any normal retry is bounded by NumberHelperManyDigitBound. Otherwise let z be the number of leading zero digits. In the raw slice the corresponding maximal prefix Z consists of those zero bytes, with the dot included if it precedes the first nonzero digit. More explicitly, if I contains a nonzero digit, Z is just its leading zeros. If all of I is zero and a nonzero digit occurs in F, then Z is I, the dot, and the leading zeros of F. Without a dot, it is simply the leading zeros of I. In every case the raw tail starts with a nonzero digit, different from both 48 and 46, and c_0(Z)=z. In the no-dot all-zero case the raw tail is either empty or starts with the original nondigit, which is neither zero nor dot. Thus this case too meets the adjustment lemma's tail clause.
The parameters for NumberHelperAdjustCountExact satisfy 0\leq n=N-19\leq |s|, while n-c_0(Z)=N-19-z\geq-19>-2^{63} because z\leq N. Its source-length and prefix hypotheses were just established. It therefore gives the exact adjusted count N-19-z. If this count is nonpositive, |U|=N-z\leq19. The zero prefix contributes zero to the decimal fold, so V=\operatorname{fold}_{10}(U,0)<10^{|U|}\leq10^{19} by NumberHelperDecimalPrefixInterval again. The exact scans now identify the retained accumulator with V\leq M, even though the original digit count exceeded nineteen. If the adjusted count is positive, the parser instead uses a normal result of the many-digit retry. Its mantissa is at most M by NumberHelperManyDigitBound, with no extra assumptions on the retry's slices or explicit exponent.
We finally enumerate the remaining record-producing continuations of num imp dec2flt parse parse partial number. An empty remainder computes the consumed offset and its unsigned length. A remainder starting with lowercase or uppercase exponent marker calls num imp dec2flt parse parse scientific. The option branch ModelOptionBranch continues only for a returned exponent; None follows the residual behavior ModelOptionFromResidual and cannot produce the assumed record. For a returned exponent, the checked addition to the fractional exponent must be normal, followed by the consumed offset and its length cast. A remainder with neither exponent marker takes the non-scientific continuation and computes its consumed offset and length directly. These cases cover both the dot and no-dot branches, including an input whose entire remainder is empty immediately after the integer scan. Every executed offset, cast, and exponent addition not already established safe above is forced normal by bind inversion. We claim neither unconditional exponent safety nor a scientific-grammar fact here.
Each such continuation uses exactly the three constructor cases already bounded: a short record when N\leq19, a retained record when the adjusted count is nonpositive, or the retry record when it is positive. The record constructors of num imp dec2flt decimal Decimal in the cited partial-parser definition set negative to false in all three cases. The first two use the retained accumulator, bounded above by M; the third uses the retry accumulator, also bounded by M. Injectivity of the normally returned option and pair identifies this constructor with d. Hence d.\mathit{negative}=\texttt{false} and d.\mathit{mantissa}\leq M<10^{19}, as required on the full stated domain.

Lean proof


  have hbits := Dec2Flt.P340_T1_selected_target_numBits
  intro s d hsource hne hparse
  change num.imp.dec2flt.parse.parse_number s = ok (some d) at 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 offset_exact := P364_T1_NumberHelperOffsetSourceLength
  have adjust_exact := P364_T1_NumberHelperAdjustCountExact
  have split_digits := P364_T1_NumberHelperDigitPrefix
  have parse_many_bound := P364_T1_NumberHelperManyDigitBound
  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 (digits : List Std.U8) (a : Nat)
      (hd : forall b, 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
    have hm : forall z, z ∈ digits.map (fun b => b.val - 48) -> z < 10 := by
      intro z hz
      obtain ⟨b, hb, rfl⟩ := List.mem_map.mp hz
      have := hd b hb
      omega
    let ds : List Nat := digits.map (fun b => b.val - 48)
    have hds : forall z, z ∈ ds -> z < 10 := hm
    have hp0 := NumberHelperDecimalPrefixInterval [a] ds hds (0 : Int)
    dsimp only at hp0
    obtain ⟨_, hp⟩ := hp0
    simp only [List.foldl_append, List.foldl_cons, List.foldl_nil,
      Nat.mul_zero, Nat.zero_add,
      Int.zero_add, zpow_zero, mul_one, zpow_natCast] at hp
    have hn : ds.foldl (fun x z => 10 * x + z) a <
        (a + 1) * 10 ^ ds.length := by exact_mod_cast hp
    simpa [ds, List.foldl_map] using hn
  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 (digits : List Std.U8)
      (hd : forall b, b ∈ digits -> 48 <= b.val /\ b.val <= 57) :
      exists zeros tail,
        digits = zeros ++ tail /\
        (forall b, b ∈ zeros -> b = 48#u8) /\
        (tail = [] \/ exists b rest, tail = b :: rest /\ b ≠ 48#u8) := by
    let decode : Std.U8 -> Nat := fun b => b.val - 48
    let zeros := digits.takeWhile (fun b => b == 48#u8)
    let tail := digits.dropWhile (fun b => b == 48#u8)
    have hmap : tail.map decode =
        (digits.map decode).dropWhile (fun z => z == 0) := by
      dsimp only [tail]
      induction digits with
      | nil => rfl
      | cons b bs ih =>
        have hb := hd b (by simp)
        have hbs : forall c, c ∈ bs -> 48 <= c.val /\ c.val <= 57 := by
          intro c hc
          exact hd c (by simp [hc])
        have hzero : decode b = 0 ↔ b = 48#u8 := by
          simp only [decode]
          constructor
          · intro h
            apply UScalar.eq_of_val_eq
            norm_num
            omega
          · intro h
            simp [h]
        simp only [List.map_cons, List.dropWhile_cons]
        by_cases hb0 : b = 48#u8
        · simp [hb0, decode, ih hbs]
        · simp [hb0, hzero, decode]
    obtain ⟨z, q, hz, hq, hsplit, htrim, hstop, hlast⟩ :=
      DecimalDigitTrimDecomposition (digits.map decode)
    rw [← hmap] at hstop
    refine ⟨zeros, tail, ?_, ?_, ?_⟩
    · exact (List.takeWhile_append_dropWhile).symm
    · intro b hb
      have := List.mem_takeWhile_imp hb
      simpa using this
    · cases ht : tail with
      | nil => exact Or.inl rfl
      | cons b bs =>
        right
        refine ⟨b, bs, rfl, ?_⟩
        rcases hstop with hn | hn
        · simp [ht] at hn
        · intro hb
          simp [ht, decode, hb] at hn
  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 hinteger
    rcases hintSig with hintNil | ⟨b, intRest, hintSigEq, hb⟩
    · obtain ⟨fracZeros, fracSig, hfracSplit, hfracZeros, hfracSig⟩ :=
        split_zeros fraction hfraction
      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⟩ :=
    offset_exact 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,
            hbits]
          omega) (by
          simp [IScalar.max, IScalarTy.numBits,
            hbits] 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,
            hbits] 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,
            hbits] 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 hintDigits
        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,
            hbits]
          omega
        obtain ⟨adjusted, hadjust, hadjustVal⟩ :=
          adjust_exact 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⟩ :=
        offset_exact 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, hbits]
      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,
          hbits]
        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,
              hbits]
            omega
          obtain ⟨adjusted2, hadjust2, hadjust2Val⟩ :=
            adjust_exact 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,
              hbits]
            omega) (by
            simp [IScalar.max, IScalarTy.numBits,
              hbits] 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 hintDigits
        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,
            hbits]
          omega
        obtain ⟨adjusted2, hadjust2, hadjust2Val⟩ :=
          adjust_exact 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.NumberHelperDigitScanExact
import Tablet.NumberHelperDigitScanSourceLength
import Tablet.NumberHelperTryParse19SafeLength
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
import Tablet.SourceValidByteSlice
import Tablet.P9_T1_ActualDispatchOperations

import Tablet.P364_T1_NumberHelperOffsetSourceLength
import Tablet.P364_T1_NumberHelperAdjustCountExact
import Tablet.P364_T1_NumberHelperManyDigitBound
import Tablet.P364_T1_NumberHelperDigitPrefix
import Tablet.NumberHelperDecimalPrefixInterval
import Tablet.DecimalDigitTrimDecomposition

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P13_T1_NumberHelperRecordBounds]
theorem P13_T1_NumberHelperRecordBounds :
    forall (s : Slice Std.U8) (d : num.imp.dec2flt.decimal.Decimal),
      SourceValidByteSlice s -> s.val ≠ [] ->
      P9_T1_ActualDispatchOperations.parseNumber s = ok (some d) ->
      d.negative = false ∧ d.mantissa.val < 10 ^ 19 := by
-- BODY
  have hbits := Dec2Flt.P340_T1_selected_target_numBits
  intro s d hsource hne hparse
  change num.imp.dec2flt.parse.parse_number s = ok (some d) at 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 offset_exact := P364_T1_NumberHelperOffsetSourceLength
  have adjust_exact := P364_T1_NumberHelperAdjustCountExact
  have split_digits := P364_T1_NumberHelperDigitPrefix
  have parse_many_bound := P364_T1_NumberHelperManyDigitBound
  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 (digits : List Std.U8) (a : Nat)
      (hd : forall b, 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
    have hm : forall z, z ∈ digits.map (fun b => b.val - 48) -> z < 10 := by
      intro z hz
      obtain ⟨b, hb, rfl⟩ := List.mem_map.mp hz
      have := hd b hb
      omega
    let ds : List Nat := digits.map (fun b => b.val - 48)
    have hds : forall z, z ∈ ds -> z < 10 := hm
    have hp0 := NumberHelperDecimalPrefixInterval [a] ds hds (0 : Int)
    dsimp only at hp0
    obtain ⟨_, hp⟩ := hp0
    simp only [List.foldl_append, List.foldl_cons, List.foldl_nil,
      Nat.mul_zero, Nat.zero_add,
      Int.zero_add, zpow_zero, mul_one, zpow_natCast] at hp
    have hn : ds.foldl (fun x z => 10 * x + z) a <
        (a + 1) * 10 ^ ds.length := by exact_mod_cast hp
    simpa [ds, List.foldl_map] using hn
  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 (digits : List Std.U8)
      (hd : forall b, b ∈ digits -> 48 <= b.val /\ b.val <= 57) :
      exists zeros tail,
        digits = zeros ++ tail /\
        (forall b, b ∈ zeros -> b = 48#u8) /\
        (tail = [] \/ exists b rest, tail = b :: rest /\ b ≠ 48#u8) := by
    let decode : Std.U8 -> Nat := fun b => b.val - 48
    let zeros := digits.takeWhile (fun b => b == 48#u8)
    let tail := digits.dropWhile (fun b => b == 48#u8)
    have hmap : tail.map decode =
        (digits.map decode).dropWhile (fun z => z == 0) := by
      dsimp only [tail]
      induction digits with
      | nil => rfl
      | cons b bs ih =>
        have hb := hd b (by simp)
        have hbs : forall c, c ∈ bs -> 48 <= c.val /\ c.val <= 57 := by
          intro c hc
          exact hd c (by simp [hc])
        have hzero : decode b = 0 ↔ b = 48#u8 := by
          simp only [decode]
          constructor
          · intro h
            apply UScalar.eq_of_val_eq
            norm_num
            omega
          · intro h
            simp [h]
        simp only [List.map_cons, List.dropWhile_cons]
        by_cases hb0 : b = 48#u8
        · simp [hb0, decode, ih hbs]
        · simp [hb0, hzero, decode]
    obtain ⟨z, q, hz, hq, hsplit, htrim, hstop, hlast⟩ :=
      DecimalDigitTrimDecomposition (digits.map decode)
    rw [← hmap] at hstop
    refine ⟨zeros, tail, ?_, ?_, ?_⟩
    · exact (List.takeWhile_append_dropWhile).symm
    · intro b hb
      have := List.mem_takeWhile_imp hb
      simpa using this
    · cases ht : tail with
      | nil => exact Or.inl rfl
      | cons b bs =>
        right
        refine ⟨b, bs, rfl, ?_⟩
        rcases hstop with hn | hn
        · simp [ht] at hn
        · intro hb
          simp [ht, decode, hb] at hn
  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 hinteger
    rcases hintSig with hintNil | ⟨b, intRest, hintSigEq, hb⟩
    · obtain ⟨fracZeros, fracSig, hfracSplit, hfracZeros, hfracSig⟩ :=
        split_zeros fraction hfraction
      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⟩ :=
    offset_exact 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,
            hbits]
          omega) (by
          simp [IScalar.max, IScalarTy.numBits,
            hbits] 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,
            hbits] 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,
            hbits] 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 hintDigits
        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,
            hbits]
          omega
        obtain ⟨adjusted, hadjust, hadjustVal⟩ :=
          adjust_exact 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⟩ :=
        offset_exact 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, hbits]
      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,
          hbits]
        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,
              hbits]
            omega
          obtain ⟨adjusted2, hadjust2, hadjust2Val⟩ :=
            adjust_exact 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,
              hbits]
            omega) (by
            simp [IScalar.max, IScalarTy.numBits,
              hbits] 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 hintDigits
        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,
            hbits]
          omega
        obtain ⟨adjusted2, hadjust2, hadjust2Val⟩ :=
          adjust_exact 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 (198)
More program dependencies (156)
Standard-library dependencies (39)

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 154 entries
Technical checking records
Local closure summary
{
  "node": "P13_T1_NumberHelperRecordBounds",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "8a423b0d2fe383c6b8a78883cd43eb117e967ea1d8ad516f312464ee6598fb0c",
  "active_statement_hash": "827d50dd04d3a61f3b17d397d59c4fa41a56ce9cdabdb989801cc752c9b154ce",
  "kernel_axioms": [
    "Classical.choice",
    "Dec2Flt.P340_T1_selected_target_numBits",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "576e6bf9480cfd32f53c309c7ac8fb115c1ba6e413f299ed5ff9fa7c647e6769",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-181",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalDigitTrimDecomposition": "74d5424585de54f0",
    "ModelSliceSplitFirst": "f617ee05dd2925a9",
    "NumberHelperDecimalPrefixInterval": "5c32858414034b55",
    "NumberHelperDigitScanExact": "6bcf20c61d10dea3",
    "NumberHelperDigitScanSourceLength": "8892b939bba0ecf9",
    "P364_T1_NumberHelperAdjustCountExact": "8999fa99d4681219",
    "P364_T1_NumberHelperDigitPrefix": "3426b4efc0d98c81",
    "P364_T1_NumberHelperManyDigitBound": "07a8916e7242c7bb",
    "P364_T1_NumberHelperOffsetSourceLength": "de0168dfeba3b802",
    "num_imp_dec2flt_parse_parse_partial_number": "854af4cc12136a58"
  },
  "strict_definition_deps": {
    "ModelOptionBranch": "2b227898dbe15e59",
    "ModelOptionFromResidual": "6d48ecfbc617744f",
    "P9_T1_ActualDispatchOperations": "d8bf51ebbbf153ac",
    "P9_T1_DispatchOperations": "9b177bb1b830a655",
    "Preamble": "4adf4172ae751d7d",
    "SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from": "7e7284dc9bc88830",
    "SourceValidByteSlice": "3dc42716a6378d26",
    "num_imp_dec2flt_decimal_Decimal": "b702e549017701b8",
    "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": "94ed7b2ff71604ef",
    "num_imp_dec2flt_parse_parse_scientific": "447f722d5841d2c5",
    "num_imp_dec2flt_parse_try_parse_digits": "238b4a73cd46fc56"
  }
}
Prose assessment
{
  "label": "Lean proof closed; predictive prose review no longer required",
  "assessment_status": null,
  "fingerprint_relation": "unknown",
  "assessment_fingerprint": null,
  "current_fingerprint": "sound-v2:b904ecdacab60dc1eaf5482f8a2cb5bcccb302c4120ef3426cc2482b0ac2b12c",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}