NumberHelperPartialAcceptanceSafeLength

Lean checked

Statement

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

Lemma · Safe complete partial-number parsing

Every source-valid nonempty Aeneas byte slice that is exactly the rendering of an unsigned valid numeric lexeme and has length at most 9223372036854120458 makes the extracted parse_partial_number return normally with a decimal record and a consumed length equal to the full slice length.

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.DecimalGoalVocabularyDefault
import Tablet.NumberHelperDigitScanSafeLength
import Tablet.ModelSliceSplitFirst
import Tablet.NumberHelperAdjustManySafeLength
import Tablet.NumberHelperFractionalManyDigitsSafeLength
import Tablet.NumberHelperIntegralAdjustment
import Tablet.NumberHelperIntegralManyDigitsSafeLength
import Tablet.NumberHelperOffsetSafeLength
import Tablet.NumberHelperScientificRenderedSuffixSafeLength
import Tablet.NumberHelperScientificSuffixSafeLength
import Tablet.SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from
import Tablet.SourceValidByteSlice
import Tablet.num_imp_dec2flt_parse_MIN_19DIGIT_INT
import Tablet.num_imp_dec2flt_parse_adjust_many_digit_count
import Tablet.num_imp_dec2flt_parse_parse_many_digits
import Tablet.num_imp_dec2flt_parse_parse_partial_number
import Tablet.num_imp_dec2flt_parse_parse_scientific
import Tablet.num_imp_dec2flt_parse_try_parse_19digits

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperPartialAcceptanceSafeLength]
theorem NumberHelperPartialAcceptanceSafeLength :
    forall (s : Slice Std.U8) (x : DecimalLexeme),
      SourceValidByteSlice s -> s.val != [] -> x.leadingSign = none ->
      DecimalGoalVocabularyDefault.validNumeric x ->
      DecimalGoalVocabularyDefault.bytes s =
        DecimalGoalVocabularyDefault.renderNumeric x ->
      s.val.length <= 9223372036854120458 ->
      exists d,
        num.imp.dec2flt.parse.parse_partial_number s =
          ok (some (d, Slice.len s)) := 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 M=2^{63}-1, C=9223372036854120458=M-655349, and L=|s|. The definitions in DecimalGoalVocabularyDefault, together with the validity, unsigned-sign, and rendering hypotheses, decompose the input into an integer digit list, an optional point and fractional digit list, and an optional exponent marker, sign, and nonempty exponent digit list. Every digit byte is in [48,57], and at least one significand digit is present. The separate premise SourceValidByteSlice is consistent with the stronger bound L\leq C<M. By Assumptions, the configured target has 64-bit usize, isize, and i64 ranges.
Apply NumberHelperDigitScanSafeLength first to the integer digit prefix, taking the following point, exponent marker, or end as its tail. The tail's first byte, if present, is not a digit, so the result is normal and its remainder is exactly that tail. The offset operation in SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice offset from casts the lengths of the original slice and this suffix to isize and subtracts. Both lengths are between zero and L<M, so the casts and nonnegative difference succeed and give the integer digit count I.
If a point is present, its removal is guarded by the preceding split. Apply NumberHelperDigitScanSafeLength again to the fractional digits; its exact remainder is the exponent marker or end. Its offset similarly gives a fractional count F\leq L. Checked negation of F is safe, its cast to i64 is exact on this 64-bit target, and the checked addition I+F is the total significand count D\leq L. Validity gives D>0, so the extracted zero-digit rejection in num imp dec2flt parse parse partial number is not taken.
If there is an exponent marker, the saved suffix is exactly an optional sign followed by its nonempty digit list. Applying NumberHelperScientificSuffixSafeLength yields a normal Some result E, empty remainder, and |E|\leq655359. Let q be the exponent digit count. The guarded decimal recurrence in num imp dec2flt parse parse scientific also gives |E|\leq10^q-1 when q\leq5. We verify the checked addition -F+E. For E\geq0 it lies between -L and 655359. For E<0,F=0 it is at least -655359. For E<0,F>0, the spelling includes the point, marker, mandatory minus, and q digits, hence F\leq L-(q+3). Therefore
F+|E|\leq \begin{cases} L-(q+3)+(10^q-1)\leq L+99991,&q\leq5,\\ L-(q+3)+655359\leq L+655350,&q\geq6. \end{cases}
Since C+655350=2^{63}, the sum is never below the allowed endpoint -2^{63}, and its upper side is below 2^{63}. The explicit-exponent addition therefore succeeds. With no marker, E=0 and no additional operation is required. In both cases the final suffix is empty, so its offset from the original input casts exactly to Slice.len s.
For D\leq19 the extracted function now constructs its decimal record directly. Suppose D>19. Its checked subtraction of 19 is safe. In num imp dec2flt parse adjust many digit count, every byte access is index-guarded and every index increment remains at most L. The constant subtraction is 48-1=47; saturating subtraction by 47 yields zero for a point and one for a zero digit, both exactly castable to isize. If z is the number of leading zero significand digits, ignoring the point, induction through this scan shows its final adjusted count is D-19-z and all intermediate counts are at least -19 and at most L. Thus all checked signed subtractions succeed. A positive result implies D-z\geq20, so a nonzero significand digit exists and at least twenty digits remain from it; a nonpositive result causes no retry.
In a retry, num imp dec2flt parse MIN 19DIGIT INT is exactly 10^{18}. The loop num imp dec2flt parse try parse 19digits updates only while its accumulator is below this threshold, so every checked update is at most 10(10^{18}-1)+9=10^{19}-1<2^{64}. All byte casts and first-byte splits are exact or guarded. If the first retry reaches the threshold, its offset to the saved integer end is nonnegative and at most L. If it returns below the threshold, it cannot have consumed nineteen digits beginning at the established nonzero digit, since their exact recurrence is then at least 10^{18}. It must therefore have stopped at the saved point. The one-byte slice past that point is in bounds, and the second retry consumes at most the F fractional digits. Its negated offset base is consequently in [-F,0].
It remains to audit the final additions in num imp dec2flt parse parse many digits. In its nonnegative integer-offset branch, no explicit exponent leaves a base at most L, and a negative exponent lowers it by at most 655359. For a positive q-digit exponent, nineteen digits plus the marker and exponent digits lie outside the remaining offset; the sum is at most L+99974 for q\leq5 and at most L+655333 for q\geq6, hence below 2^{63} at L\leq C. In the fractional branch the base is in [-F,0]; its upper side is at most 655359, and its lower side is protected by the already proved F+|E|\leq2^{63}. Every retry path therefore returns normally.
All assertions, checked arithmetic, casts, offsets, slices, and parser loops have now been discharged. The rendering occupies the whole input, so the exact digit-scan and scientific-suffix remainders show that the final suffix is empty. The partial parser consequently returns Some of its constructed decimal record with consumed length exactly Slice.len s, as claimed.

Lean proof


  intro s x hsource hne hsign hvalid hbytes hlen
  rcases x with ⟨sign, ints, frac, expo⟩
  simp only at hsign
  subst sign
  simp [DecimalGoalVocabularyDefault] at hvalid hbytes
  have byte_digits : forall (bs : List Std.U8) (ds : List Nat),
      List.map (fun b : Std.U8 => b.val) bs =
        List.map (fun d : Nat => 48 + d) ds ->
      (forall d, List.Mem d ds -> d < 10) ->
      forall b, List.Mem b bs -> 48 <= b.val /\ b.val <= 57 := by
    intro bs ds hmap hds b hb
    have hbmap : b.val ∈ List.map (fun b : Std.U8 => b.val) bs :=
      List.mem_map_of_mem hb
    rw [hmap] at hbmap
    obtain ⟨d, hd, hdval⟩ := List.mem_map.mp hbmap
    have := hds d hd
    omega
  have offset_safe : forall (whole rest : Slice Std.U8),
      whole.val.length <= 9223372036854120458 ->
      rest.val.length <= whole.val.length ->
      exists z : Std.Isize,
        SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
          rest whole = ok z /\
        z.val = whole.val.length - rest.val.length := by
    intro whole rest hw hr
    have hbits := Dec2Flt.configured_x86_64_numBits
    have hwcast : (Slice.len whole).val <= IScalar.max .Isize := by
      simp [IScalar.max, IScalarTy.numBits, hbits]
      omega
    have hrcast : (Slice.len rest).val <= IScalar.max .Isize := by
      simp [IScalar.max, IScalarTy.numBits, hbits]
      omega
    have hwsp := UScalar.hcast_inBounds_spec .Isize (Slice.len whole) hwcast
    have hrsp := UScalar.hcast_inBounds_spec .Isize (Slice.len rest) hrcast
    obtain ⟨wi, hwi, hwival⟩ := Aeneas.Std.WP.spec_imp_exists hwsp
    obtain ⟨ri, hri, hrival⟩ := Aeneas.Std.WP.spec_imp_exists hrsp
    simp at hwival hrival
    have hsublo : IScalar.min .Isize <= wi.val - ri.val := by
      simp [IScalar.min, IScalarTy.numBits, hbits]
      omega
    have hsubhi : wi.val - ri.val <= IScalar.max .Isize := by
      simp [IScalar.max, IScalarTy.numBits, hbits]
      omega
    have hsub := IScalar.sub_spec hsublo hsubhi
    obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists hsub
    refine ⟨z, ?_, ?_⟩
    · simp [SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from,
        hwi, hri, hz]
    · omega
  cases frac with
  | none =>
    simp at hvalid hbytes
    rcases hvalid with ⟨hints, hintsne, hexp⟩
    cases expo with
    | none =>
      simp at hbytes
      have hsdigits := byte_digits s.val ints hbytes hints
      obtain ⟨mantissa, rest, hscan, hrest⟩ :=
        NumberHelperDigitScanSafeLength s 0#u64 s.val [] (by simp) hsdigits
          (by simp) hlen
      have hrestnil : rest.val = [] := by simpa using hrest
      let empty : Slice Std.U8 := ⟨[], by simp⟩
      have hresteq : rest = empty := (Slice.eq_iff _ _).2 hrestnil
      subst rest
      have hrestle : empty.val.length <= s.val.length := by simp [empty]
      obtain ⟨nDigits, hoff, hnval⟩ := offset_safe s empty hlen hrestle
      have hnpos : nDigits.val > 0 := by
        have hslen : s.val.length = ints.length := by
          have := congrArg List.length hbytes
          simpa using this
        have hintlen : 0 < ints.length := by
          cases ints <;> simp_all
        simp [empty] at hnval
        omega
      have hcastlen : lift (IScalar.hcast .Usize nDigits) = ok (Slice.len s) := by
        have hbound : 0 <= nDigits.val /\
            nDigits.val <= UScalar.max .Usize := by
          simp [empty] at hnval
          simp [UScalar.max, Dec2Flt.configured_x86_64_numBits]
          omega
        have hspec := IScalar.hcast_inBounds_spec .Usize nDigits hbound
        obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists hspec
        rw [hz]
        congr 2
        apply UScalar.eq_of_val_eq
        simp [empty] at hnval
        simpa [Slice.len_val] using (show z.val = s.val.length by omega)
      have haddzero : (nDigits + 0#isize) = ok nDigits := by
        have hspec := IScalar.add_spec (x := nDigits) (y := 0#isize)
          (by
            simp [IScalar.min, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits]
            omega)
          (by
            simp [IScalar.max, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits]
            omega)
        obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists hspec
        rw [hz]
        congr 2
        apply IScalar.eq_of_val_eq
        simpa using hzval
      have hnotempty : Not (s.val = []) := by simpa using hne
      unfold num.imp.dec2flt.parse.parse_partial_number
      simp [hnotempty, hscan, hoff, haddzero, hnpos, hcastlen, empty,
        core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst]
      split
      · rename_i hsmall
        exact ⟨{
          exponent := 0#i64
          mantissa := mantissa
          negative := false
          many_digits := false
        }, rfl⟩
      · rename_i hlarge
        have hsublo : IScalar.min .Isize <= nDigits.val - 19 := by
          simp [IScalar.min, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits]
          omega
        have hsubhi : nDigits.val - 19 <= IScalar.max .Isize := by
          simp [IScalar.max, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits]
          omega
        obtain ⟨nMore, hnMore, hnMoreVal⟩ :=
          Aeneas.Std.WP.spec_imp_exists
            (IScalar.sub_spec (x := nDigits) (y := 19#isize)
              (by simpa using hsublo) (by simpa using hsubhi))
        rw [hnMore]
        simp
        obtain ⟨adjusted, hadjusted, hadjustedPositive⟩ :=
          NumberHelperIntegralAdjustment s nMore s.val [] (by simp)
            hsdigits (by simp) (by simp) hlen (by
              simp [empty] at hnval
              norm_num at hnMoreVal
              omega)
        rw [hadjusted]
        simp
        split
        · rename_i hpositive
          obtain ⟨mantissa2, exponent2, hmany⟩ :=
            NumberHelperIntegralManyDigitsSafeLength s empty 0#i64
              nMore adjusted s.val [] (by simp) (by simp [empty])
              hsdigits (by simp) (by simp) hlen (by
                simp [empty] at hnval
                norm_num at hnMoreVal
                omega) hadjusted (by omega) (by
                rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq]
                norm_num
                norm_num at hnMoreVal
                simp [empty] at hnval
                omega)
          rw [hmany]
          exact ⟨{
            exponent := exponent2
            mantissa := mantissa2
            negative := false
            many_digits := true
          }, rfl⟩
        · exact ⟨{
            exponent := 0#i64
            mantissa := mantissa
            negative := false
            many_digits := false
          }, rfl⟩
    | some e =>
      simp at hbytes hexp
      rcases e with ⟨upper, sign, expds⟩
      simp only [Prod.fst, Prod.snd] at hbytes hexp
      obtain ⟨hexpne, hexpdigits⟩ := hexp
      obtain ⟨intBytes, afterInt, hsval, hintMap, hafterMap⟩ :=
        List.map_eq_append_iff.mp hbytes
      obtain ⟨marker, suffixBytes, hafterVal, hmarkerVal, hsuffixMap⟩ :=
        List.map_eq_cons_iff.mp hafterMap
      have hmarker : marker = if upper then 69#u8 else 101#u8 := by
        apply UScalar.eq_of_val_eq
        cases upper <;> simp_all
      subst marker
      have hsdecomp : s.val = intBytes ++
          (if upper then 69#u8 else 101#u8) :: suffixBytes := by
        rw [hsval, hafterVal]
      have hintDigits := byte_digits intBytes ints hintMap hints
      have hintTailProp :
          ((if upper then 69#u8 else 101#u8) :: suffixBytes).length <=
            Usize.max := by
        have := s.property
        rw [hsdecomp] at this
        simp only [List.length_append] at this
        omega
      let intTail : Slice Std.U8 :=
        ⟨(if upper then 69#u8 else 101#u8) :: suffixBytes, hintTailProp⟩
      obtain ⟨mantissa, intRest, hscan, hintRest⟩ :=
        NumberHelperDigitScanSafeLength s 0#u64 intBytes
          ((if upper then 69#u8 else 101#u8) :: suffixBytes)
          hsdecomp hintDigits (by
            intro b rest h
            simp only [List.cons.injEq] at h
            rcases h with ⟨rfl, _⟩
            cases upper <;> norm_num) hlen
      have hintRestVal : intRest.val =
          (if upper then 69#u8 else 101#u8) :: suffixBytes := by
        simpa using hintRest
      have hintRestEq : intRest = intTail := (Slice.eq_iff _ _).2 hintRestVal
      subst intRest
      obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ :=
        NumberHelperOffsetSafeLength s intTail hlen (by
          rw [hsdecomp]
          simp [intTail]
          )
      have hnDigitsEq : nDigits.val = intBytes.length := by
        rw [hnDigitsVal, hsdecomp]
        simp [intTail]
      have hsuffixProp : suffixBytes.length <= Usize.max := by
        have := intTail.property
        simp [intTail] at this
        omega
      let suffix : Slice Std.U8 := ⟨suffixBytes, hsuffixProp⟩
      have hsplitMarker : core.slice.Slice.split_first intTail =
          ok (some (if upper then 69#u8 else 101#u8, suffix)) := by
        simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
          intTail, suffix]
      have hsuffixLen : suffix.val.length <= 9223372036854120458 := by
        change suffixBytes.length <= 9223372036854120458
        rw [hsdecomp] at hlen
        simp only [List.length_append, List.length_cons] at hlen
        omega
      obtain ⟨parsedExp, empty, hscientific, hempty, hexplo, hexphi,
          hexpshort, _⟩ :=
        NumberHelperScientificRenderedSuffixSafeLength suffix sign expds
          (by simpa only [bne_iff_ne] using hexpne) hexpdigits
          (by simpa [suffix] using hsuffixMap) hsuffixLen
      have haddExp : (0#i64 + parsedExp) = ok parsedExp := by
        have hb : IScalar.min .I64 <= (0#i64).val + parsedExp.val /\
            (0#i64).val + parsedExp.val <= IScalar.max .I64 := by
          have hp := IScalar.hBounds parsedExp
          rw [IScalar.min_IScalarTy_I64_eq, I64.min_eq,
            IScalar.max_IScalarTy_I64_eq, I64.max_eq]
          norm_num at hp ⊢
          omega
        obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.add_spec hb.1 hb.2)
        rw [hz]
        congr 2
        apply IScalar.eq_of_val_eq
        norm_num at hzval
        exact hzval
      have hemptyLen : empty.val.length <= s.val.length := by
        simp [hempty]
      obtain ⟨fullOffset, hoffFull, hfullOffsetVal⟩ :=
        NumberHelperOffsetSafeLength s empty hlen hemptyLen
      have hfullCast : lift (IScalar.hcast .Usize fullOffset) =
          ok (Slice.len s) := by
        have hbound : 0 <= fullOffset.val /\
            fullOffset.val <= UScalar.max .Usize := by
          rw [hfullOffsetVal, hempty]
          simp [UScalar.max, Dec2Flt.configured_x86_64_numBits]
          omega
        obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.hcast_inBounds_spec .Usize fullOffset hbound)
        rw [hz]
        congr 2
        apply UScalar.eq_of_val_eq
        rw [hfullOffsetVal, hempty] at hzval
        simpa [Slice.len_val] using hzval
      have haddzero : (nDigits + 0#isize) = ok nDigits := by
        have hb : IScalar.min .Isize <= nDigits.val + (0#isize).val /\
            nDigits.val + (0#isize).val <= IScalar.max .Isize := by
          have hn := IScalar.hBounds nDigits
          simp [IScalar.min, IScalar.max, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits] at hn ⊢
          omega
        obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.add_spec hb.1 hb.2)
        rw [hz]
        congr 2
        apply IScalar.eq_of_val_eq
        norm_num at hzval
        exact hzval
      have hnpos : 0 < nDigits.val := by
        have : 0 < ints.length := List.length_pos_of_ne_nil hintsne
        have hlenEq := congrArg List.length hintMap
        simp only [List.length_map] at hlenEq
        omega
      have hstartIntTail : s.val = intBytes ++ intTail.val := by
        simpa [intTail] using hsdecomp
      have hintTailNotDigit : forall b rest, intTail.val = b :: rest ->
          Not (48 <= b.val /\ b.val <= 57) := by
        intro b rest h
        simp [intTail] at h
        rcases h with ⟨rfl, _⟩
        cases upper <;> norm_num
      have hintTailNotPoint : forall b rest, intTail.val = b :: rest ->
          b ≠ 46#u8 := by
        intro b rest h
        simp [intTail] at h
        rcases h with ⟨rfl, _⟩
        cases upper <;> norm_num
      have hnotempty : Not (s.val = []) := by simpa using hne
      unfold num.imp.dec2flt.parse.parse_partial_number
      simp [hnotempty, hscan, hoffInt]
      cases upper <;> simp [haddzero, hnpos, hsplitMarker, hscientific,
        haddExp, hoffFull, hfullCast, UScalar.ofNatCore,
        core.option.Option.Insts.CoreOpsTry_traitTry.branch,
        dec2flt_model.ModelOptionBranch]
      case false | true =>
        split
        · exact ⟨{
            exponent := parsedExp
            mantissa := mantissa
            negative := false
            many_digits := false
          }, rfl⟩
        · rename_i hlarge
          have hsublo : IScalar.min .Isize <= nDigits.val - 19 := by
            have hn := IScalar.hBounds nDigits
            simp [IScalar.min, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits] at hn ⊢
            omega
          have hsubhi : nDigits.val - 19 <= IScalar.max .Isize := by
            have hn := IScalar.hBounds nDigits
            simp [IScalar.max, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits] at hn ⊢
            omega
          obtain ⟨nMore, hnMore, hnMoreVal⟩ :=
            Aeneas.Std.WP.spec_imp_exists
              (IScalar.sub_spec (x := nDigits) (y := 19#isize)
                (by simpa using hsublo) (by simpa using hsubhi))
          rw [hnMore]
          simp
          obtain ⟨adjusted, hadjusted, hadjustedPositive⟩ :=
            NumberHelperIntegralAdjustment s nMore intBytes
              intTail.val hstartIntTail hintDigits hintTailNotDigit
              hintTailNotPoint hlen (by
                norm_num at hnMoreVal
                omega)
          rw [hadjusted]
          simp
          split
          · rename_i hpositive
            obtain ⟨mantissa2, exponent2, hmany⟩ :=
              NumberHelperIntegralManyDigitsSafeLength s intTail parsedExp
                nMore adjusted intBytes intTail.val hstartIntTail rfl
                hintDigits hintTailNotDigit hintTailNotPoint hlen (by
                  norm_num at hnMoreVal
                  omega) hadjusted hpositive (by
                  rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq]
                  rw [hsdecomp] at hlen
                  simp only [List.length_append, List.length_cons] at hlen
                  omega)
            rw [hmany]
            exact ⟨{
              exponent := exponent2
              mantissa := mantissa2
              negative := false
              many_digits := true
            }, rfl⟩
          · exact ⟨{
              exponent := parsedExp
              mantissa := mantissa
              negative := false
              many_digits := false
            }, rfl⟩
  | some fracs =>
    simp at hvalid hbytes
    rcases hvalid with ⟨hints, hfracs, hsome, hexp⟩
    cases expo with
    | none =>
      simp at hbytes
      obtain ⟨intBytes, afterInt, hsval, hintMap, hafterMap⟩ :=
        List.map_eq_append_iff.mp hbytes
      obtain ⟨point, fracBytes, hafterVal, hpointVal, hfracMap⟩ :=
        List.map_eq_cons_iff.mp hafterMap
      have hpoint : point = 46#u8 := by
        apply UScalar.eq_of_val_eq
        simpa using hpointVal
      subst point
      have hsdecomp : s.val = intBytes ++ 46#u8 :: fracBytes := by
        rw [hsval, hafterVal]
      have hintDigits := byte_digits intBytes ints hintMap hints
      have hfracDigits := byte_digits fracBytes fracs hfracMap hfracs
      have hintTailProp : (46#u8 :: fracBytes).length <= Usize.max := by
        have := s.property
        rw [hsdecomp] at this
        simp only [List.length_append] at this
        omega
      let intTail : Slice Std.U8 := ⟨46#u8 :: fracBytes, hintTailProp⟩
      obtain ⟨mantissa, intRest, hscanInt, hintRest⟩ :=
        NumberHelperDigitScanSafeLength s 0#u64 intBytes (46#u8 :: fracBytes)
          hsdecomp hintDigits (by
            intro b rest h
            simp only [List.cons.injEq] at h
            rcases h with ⟨rfl, _⟩
            norm_num) hlen
      have hintRestEq : intRest = intTail :=
        (Slice.eq_iff _ _).2 (by simpa [intTail] using hintRest)
      subst intRest
      obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ :=
        NumberHelperOffsetSafeLength s intTail hlen (by
          simp [intTail, hsdecomp])
      have hnDigitsEq : nDigits.val = intBytes.length := by
        rw [hnDigitsVal, hsdecomp]
        simp [intTail]
      have hfracProp : fracBytes.length <= Usize.max := by
        have := intTail.property
        simp [intTail] at this ⊢
        omega
      let fracSlice : Slice Std.U8 := ⟨fracBytes, hfracProp⟩
      have hsplitPoint : core.slice.Slice.split_first intTail =
          ok (some (46#u8, fracSlice)) := by
        simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
          intTail, fracSlice]
      obtain ⟨mantissa1, fracRest, hscanFrac, hfracRest⟩ :=
        NumberHelperDigitScanSafeLength fracSlice mantissa fracBytes []
          (by simp [fracSlice]) hfracDigits (by simp) (by
            simp [fracSlice]
            rw [hsdecomp] at hlen
            simp only [List.length_append, List.length_cons] at hlen
            omega)
      let empty : Slice Std.U8 := ⟨[], by simp⟩
      have hfracRestEq : fracRest = empty :=
        (Slice.eq_iff _ _).2 (by simpa [empty] using hfracRest)
      subst fracRest
      obtain ⟨nAfter, hoffFrac, hnAfterVal⟩ :=
        NumberHelperOffsetSafeLength fracSlice empty (by
          simp [fracSlice]
          rw [hsdecomp] at hlen
          simp only [List.length_append, List.length_cons] at hlen
          omega) (by simp [empty])
      have hnAfterEq : nAfter.val = fracBytes.length := by
        rw [hnAfterVal]
        simp [fracSlice, empty]
      have hnAfterNotMin : nAfter.val ≠ IScalar.min .Isize := by
        rw [hnAfterVal]
        simp [empty, IScalar.min, IScalarTy.numBits,
          Dec2Flt.configured_x86_64_numBits]
      obtain ⟨negAfter, hnegAfter, hnegAfterVal⟩ :=
        Aeneas.Std.WP.spec_imp_exists (HNeg.hNeg.step nAfter hnAfterNotMin)
      have hnegCastBounds : IScalar.min .I64 <= negAfter.val /\
          negAfter.val <= IScalar.max .I64 := by
        have hlenParts := hlen
        rw [hsdecomp] at hlenParts
        simp only [List.length_append, List.length_cons] at hlenParts
        rw [hnegAfterVal, hnAfterEq,
          IScalar.min_IScalarTy_I64_eq, I64.min_eq,
          IScalar.max_IScalarTy_I64_eq, I64.max_eq]
        constructor <;> omega
      obtain ⟨fracExponent, hfracExponent, hfracExponentVal⟩ :=
        Aeneas.Std.WP.spec_imp_exists
          (IScalar.cast_inBounds_spec .I64 negAfter hnegCastBounds)
      have htotalBounds : IScalar.min .Isize <= nDigits.val + nAfter.val /\
          nDigits.val + nAfter.val <= IScalar.max .Isize := by
        rw [hnDigitsEq, hnAfterEq]
        simp [IScalar.min, IScalar.max,
          IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits]
        rw [hsdecomp] 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 hintLen : intBytes.length = ints.length := by
        have := congrArg List.length hintMap
        simpa using this
      have hfracLen : fracBytes.length = fracs.length := by
        have := congrArg List.length hfracMap
        simpa using this
      have htotalPos : 0 < totalDigits.val := by
        rw [htotalVal, hnDigitsEq, hnAfterEq]
        rcases hsome with hneInt | hneFrac
        · have : 0 < ints.length := List.length_pos_of_ne_nil hneInt
          omega
        · have : 0 < fracs.length := List.length_pos_of_ne_nil hneFrac
          omega
      have htotalLen : totalDigits.val + 1 = s.val.length := by
        rw [htotalVal, hnDigitsEq, hnAfterEq, hsdecomp]
        simp
        omega
      obtain ⟨fullOffset, hoffFull, hfullOffsetVal⟩ :=
        NumberHelperOffsetSafeLength s empty hlen (by simp [empty])
      have hfullCast : lift (IScalar.hcast .Usize fullOffset) =
          ok (Slice.len s) := by
        have hbound : 0 <= fullOffset.val /\
            fullOffset.val <= UScalar.max .Usize := by
          rw [hfullOffsetVal]
          simp [empty, UScalar.max, Dec2Flt.configured_x86_64_numBits]
          omega
        obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.hcast_inBounds_spec .Usize fullOffset hbound)
        rw [hz]
        congr 2
        apply UScalar.eq_of_val_eq
        rw [hfullOffsetVal] at hzval
        simpa [empty, Slice.len_val] using hzval
      have hnotempty : Not (s.val = []) := by simpa using hne
      have hsplitEmpty : core.slice.Slice.split_first empty = ok none := by
        simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
          empty]
      have hpointMatch :
          (match (46#u8 : Std.U8) with
          | 46#uscalar => ok (empty, mantissa1, nAfter, fracExponent)
          | _ => ok (intTail, mantissa, 0#isize, 0#i64)) =
            ok (empty, mantissa1, nAfter, fracExponent) := by
        rfl
      unfold num.imp.dec2flt.parse.parse_partial_number
      simp [hnotempty, hscanInt, hoffInt, hsplitPoint, hscanFrac,
        hoffFrac, hnegAfter, hfracExponent, htotal, htotalPos, hsplitEmpty,
        hoffFull, hfullCast]
      norm_num
      simp only [UScalar.ofNat, UScalar.ofNatCore]
      simp [htotal, htotalPos, hsplitEmpty, hoffFull, hfullCast]
      split
      · exact ⟨{
          exponent := fracExponent
          mantissa := mantissa1
          negative := false
          many_digits := false
        }, rfl⟩
      · rename_i hlarge
        have hsublo : IScalar.min .Isize <= totalDigits.val - 19 := by
          have ht := IScalar.hBounds totalDigits
          simp [IScalar.min, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits] at ht ⊢
          omega
        have hsubhi : totalDigits.val - 19 <= IScalar.max .Isize := by
          have ht := IScalar.hBounds totalDigits
          simp [IScalar.max, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits] at ht ⊢
          omega
        obtain ⟨nMore, hnMore, hnMoreVal⟩ :=
          Aeneas.Std.WP.spec_imp_exists
            (IScalar.sub_spec (x := totalDigits) (y := 19#isize)
              (by simpa using hsublo) (by simpa using hsubhi))
        rw [hnMore]
        simp
        obtain ⟨adjusted, hadjusted⟩ :=
          NumberHelperAdjustManySafeLength s nMore hlen (by
            norm_num at hnMoreVal
            omega) (by
            norm_num at hnMoreVal
            rw [hnMoreVal]
            omega)
        rw [hadjusted]
        simp
        split
        · rename_i hpositive
          obtain ⟨mantissa2, exponent2, hmany⟩ :=
            NumberHelperFractionalManyDigitsSafeLength s intTail 0#i64
              intBytes fracBytes [] (by simpa [List.append_assoc] using hsdecomp)
              (by simp [intTail]) hintDigits hfracDigits (by simp) hlen
              (by
                rw [IScalar.min_IScalarTy_I64_eq, I64.min_eq]
                norm_num
                rw [hsdecomp] at hlen
                simp only [List.length_append, List.length_cons] at hlen
                omega) (by
                rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq]
                norm_num
                rw [hsdecomp] at hlen
                simp only [List.length_append, List.length_cons] at hlen
                omega)
          rw [hmany]
          exact ⟨{
            exponent := exponent2
            mantissa := mantissa2
            negative := false
            many_digits := true
          }, rfl⟩
        · exact ⟨{
            exponent := fracExponent
            mantissa := mantissa1
            negative := false
            many_digits := false
          }, rfl⟩
    | some e =>
      simp at hbytes hexp
      rcases e with ⟨upper, sign, expds⟩
      simp only [Prod.fst, Prod.snd] at hbytes hexp
      obtain ⟨hexpne, hexpdigits⟩ := hexp
      obtain ⟨intBytes, afterInt, hsval, hintMap, hafterMap⟩ :=
        List.map_eq_append_iff.mp hbytes
      obtain ⟨point, afterPoint, hafterVal, hpointVal, hafterPointMap⟩ :=
        List.map_eq_cons_iff.mp hafterMap
      have hpoint : point = 46#u8 := by
        apply UScalar.eq_of_val_eq
        simpa using hpointVal
      subst point
      obtain ⟨fracBytes, afterFrac, hafterPointVal, hfracMap,
          hafterFracMap⟩ := List.map_eq_append_iff.mp hafterPointMap
      obtain ⟨marker, suffixBytes, hafterFracVal, hmarkerVal, hsuffixMap⟩ :=
        List.map_eq_cons_iff.mp hafterFracMap
      have hmarker : marker = if upper then 69#u8 else 101#u8 := by
        apply UScalar.eq_of_val_eq
        cases upper <;> simp_all
      subst marker
      have hsdecomp : s.val = intBytes ++ 46#u8 ::
          (fracBytes ++ (if upper then 69#u8 else 101#u8) :: suffixBytes) := by
        rw [hsval, hafterVal, hafterPointVal, hafterFracVal]
      have hintDigits := byte_digits intBytes ints hintMap hints
      have hfracDigits := byte_digits fracBytes fracs hfracMap hfracs
      have hmarkerTailProp :
          ((if upper then 69#u8 else 101#u8) :: suffixBytes).length <=
            Usize.max := by
        have := s.property
        rw [hsdecomp] at this
        simp only [List.length_append, List.length_cons] at this ⊢
        omega
      let markerTail : Slice Std.U8 :=
        ⟨(if upper then 69#u8 else 101#u8) :: suffixBytes,
          hmarkerTailProp⟩
      have hmarkerTailNotDigit : forall b rest,
          markerTail.val = b :: rest ->
          Not (48 <= b.val /\ b.val <= 57) := by
        intro b rest h
        simp [markerTail] at h
        rcases h with ⟨rfl, _⟩
        cases upper <;> norm_num
      have hintTailProp :
          (46#u8 :: (fracBytes ++ markerTail.val)).length <= Usize.max := by
        have := s.property
        rw [hsdecomp] at this
        simp [markerTail] at this ⊢
        omega
      let intTail : Slice Std.U8 :=
        ⟨46#u8 :: (fracBytes ++ markerTail.val), hintTailProp⟩
      have hstart : s.val = intBytes ++ 46#u8 ::
          (fracBytes ++ markerTail.val) := by
        simpa [markerTail] using hsdecomp
      obtain ⟨mantissa, intRest, hscanInt, hintRest⟩ :=
        NumberHelperDigitScanSafeLength s 0#u64 intBytes intTail.val
          hstart hintDigits (by
            intro b rest h
            simp [intTail] at h
            rcases h with ⟨rfl, _⟩
            norm_num) hlen
      have hintRestEq : intRest = intTail :=
        (Slice.eq_iff _ _).2 (by simpa using hintRest)
      subst intRest
      obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ :=
        NumberHelperOffsetSafeLength s intTail hlen (by
          rw [hstart]
          simp [intTail])
      have hnDigitsEq : nDigits.val = intBytes.length := by
        rw [hnDigitsVal, hstart]
        simp [intTail]
      have hfracSliceProp : (fracBytes ++ markerTail.val).length <=
          Usize.max := by
        have := intTail.property
        simp [intTail] at this ⊢
        omega
      let fracSlice : Slice Std.U8 :=
        ⟨fracBytes ++ markerTail.val, hfracSliceProp⟩
      have hsplitPoint : core.slice.Slice.split_first intTail =
          ok (some (46#u8, fracSlice)) := by
        simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
          intTail, fracSlice]
      obtain ⟨mantissa1, fracRest, hscanFrac, hfracRest⟩ :=
        NumberHelperDigitScanSafeLength fracSlice mantissa fracBytes
          markerTail.val (by simp [fracSlice]) hfracDigits hmarkerTailNotDigit
          (by
            simp [fracSlice]
            rw [hstart] at hlen
            simp only [List.length_append, List.length_cons] at hlen
            omega)
      have hfracRestEq : fracRest = markerTail :=
        (Slice.eq_iff _ _).2 (by simpa using hfracRest)
      subst fracRest
      obtain ⟨nAfter, hoffFrac, hnAfterVal⟩ :=
        NumberHelperOffsetSafeLength fracSlice markerTail (by
          simp [fracSlice]
          rw [hstart] at hlen
          simp only [List.length_append, List.length_cons] at hlen
          omega) (by simp [fracSlice])
      have hnAfterEq : nAfter.val = fracBytes.length := by
        rw [hnAfterVal]
        simp [fracSlice]
      have hnAfterNotMin : nAfter.val ≠ IScalar.min .Isize := by
        rw [hnAfterEq, IScalar.min]
        simp [IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits]
      obtain ⟨negAfter, hnegAfter, hnegAfterVal⟩ :=
        Aeneas.Std.WP.spec_imp_exists (HNeg.hNeg.step nAfter hnAfterNotMin)
      have hnegCastBounds : IScalar.min .I64 <= negAfter.val /\
          negAfter.val <= IScalar.max .I64 := by
        have hparts := hlen
        rw [hstart] at hparts
        simp only [List.length_append, List.length_cons] at hparts
        rw [hnegAfterVal, hnAfterEq,
          IScalar.min_IScalarTy_I64_eq, I64.min_eq,
          IScalar.max_IScalarTy_I64_eq, I64.max_eq]
        constructor <;> omega
      obtain ⟨fracExponent, hfracExponent, hfracExponentVal⟩ :=
        Aeneas.Std.WP.spec_imp_exists
          (IScalar.cast_inBounds_spec .I64 negAfter hnegCastBounds)
      have htotalBounds : IScalar.min .Isize <= nDigits.val + nAfter.val /\
          nDigits.val + nAfter.val <= IScalar.max .Isize := by
        rw [hnDigitsEq, hnAfterEq]
        simp [IScalar.min, IScalar.max, IScalarTy.numBits,
          Dec2Flt.configured_x86_64_numBits]
        rw [hstart] 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 htotalPos : 0 < totalDigits.val := by
        rw [htotalVal, hnDigitsEq, hnAfterEq]
        have hintLen : intBytes.length = ints.length := by
          have := congrArg List.length hintMap
          simpa using this
        have hfracLen : fracBytes.length = fracs.length := by
          have := congrArg List.length hfracMap
          simpa using this
        rcases hsome with hneInt | hneFrac
        · have := List.length_pos_of_ne_nil hneInt
          omega
        · have := List.length_pos_of_ne_nil hneFrac
          omega
      have hsuffixProp : suffixBytes.length <= Usize.max := by
        have := markerTail.property
        simp [markerTail] at this
        omega
      let suffix : Slice Std.U8 := ⟨suffixBytes, hsuffixProp⟩
      have hsplitMarker : core.slice.Slice.split_first markerTail =
          ok (some (if upper then 69#u8 else 101#u8, suffix)) := by
        simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
          markerTail, suffix]
      have hsuffixLen : suffix.val.length <= 9223372036854120458 := by
        change suffixBytes.length <= 9223372036854120458
        rw [hstart] at hlen
        simp [markerTail, intTail] at hlen
        omega
      obtain ⟨parsedExp, empty, hscientific, hempty, hexplo, hexphi,
          hexpshort, hexpnonnegative⟩ :=
        NumberHelperScientificRenderedSuffixSafeLength suffix sign expds
          (by simpa only [bne_iff_ne] using hexpne) hexpdigits
          (by simpa [suffix] using hsuffixMap) hsuffixLen
      have hsuffixLenEq : suffixBytes.length =
          (match sign with
          | none => []
          | some negative => [if negative then 45 else 43]).length +
            expds.length := by
        have := congrArg List.length hsuffixMap
        simpa using this
      have hcombinedBounds : IScalar.min .I64 <=
            fracExponent.val + parsedExp.val /\
          fracExponent.val + parsedExp.val <= IScalar.max .I64 := by
        have hparts := hlen
        rw [hstart] at hparts
        simp [markerTail, intTail] at hparts
        rw [hfracExponentVal, hnegAfterVal, hnAfterEq,
          IScalar.min_IScalarTy_I64_eq, I64.min_eq,
          IScalar.max_IScalarTy_I64_eq, I64.max_eq]
        constructor
        · by_cases hshort : expds.length <= 5
          · have hsharp := hexpshort hshort
            omega
          · have hq : 6 <= expds.length := by omega
            by_cases hnegative : sign = some true
            · subst sign
              simp at hsuffixLenEq
              omega
            · have hnonnegative := hexpnonnegative hnegative
              omega
        · omega
      obtain ⟨combinedExponent, hcombinedExponent,
          hcombinedExponentVal⟩ := Aeneas.Std.WP.spec_imp_exists
        (IScalar.add_spec hcombinedBounds.1 hcombinedBounds.2)
      have h101Target : (101#u8 : Std.U8) = 101#uscalar := by
        apply UScalar.eq_of_val_eq
        rfl
      have h69Target : (69#u8 : Std.U8) = 69#uscalar := by
        apply UScalar.eq_of_val_eq
        rfl
      obtain ⟨fullOffset, hoffFull, hfullOffsetVal⟩ :=
        NumberHelperOffsetSafeLength s empty hlen (by simp [hempty])
      have hfullCast : lift (IScalar.hcast .Usize fullOffset) =
          ok (Slice.len s) := by
        have hbound : 0 <= fullOffset.val /\
            fullOffset.val <= UScalar.max .Usize := by
          rw [hfullOffsetVal, hempty]
          simp [UScalar.max, Dec2Flt.configured_x86_64_numBits]
          omega
        obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.hcast_inBounds_spec .Usize fullOffset hbound)
        rw [hz]
        congr 2
        apply UScalar.eq_of_val_eq
        rw [hfullOffsetVal, hempty] at hzval
        simpa [Slice.len_val] using hzval
      have hnotempty : Not (s.val = []) := by simpa using hne
      unfold num.imp.dec2flt.parse.parse_partial_number
      simp [hnotempty, hscanInt, hoffInt, hsplitPoint, hscanFrac,
        hoffFrac, hnegAfter, hfracExponent]
      norm_num
      simp only [UScalar.ofNat, UScalar.ofNatCore]
      cases upper <;> simp [htotal, htotalPos, hsplitMarker, hscientific,
        hcombinedExponent, hoffFull, hfullCast,
        core.option.Option.Insts.CoreOpsTry_traitTry.branch,
        dec2flt_model.ModelOptionBranch, UScalar.ofNatCore,
        h101Target, h69Target]
      case false | true =>
        by_cases hsmall : totalDigits.val <= 19
        · exact ⟨{
            exponent := combinedExponent
            mantissa := mantissa1
            negative := false
            many_digits := false
          }, by simp [hsmall]⟩
        · simp [hsmall]
          have hsublo : IScalar.min .Isize <= totalDigits.val - 19 := by
            have ht := IScalar.hBounds totalDigits
            simp [IScalar.min, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits] at ht ⊢
            omega
          have hsubhi : totalDigits.val - 19 <= IScalar.max .Isize := by
            have ht := IScalar.hBounds totalDigits
            simp [IScalar.max, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits] at ht ⊢
            omega
          obtain ⟨nMore, hnMore, hnMoreVal⟩ :=
            Aeneas.Std.WP.spec_imp_exists
              (IScalar.sub_spec (x := totalDigits) (y := 19#isize)
                (by simpa using hsublo) (by simpa using hsubhi))
          rw [hnMore]
          simp
          obtain ⟨adjusted, hadjusted⟩ :=
            NumberHelperAdjustManySafeLength s nMore hlen (by
              norm_num at hnMoreVal
              omega) (by
              norm_num at hnMoreVal
              rw [hnMoreVal]
              rw [htotalVal, hnDigitsEq, hnAfterEq]
              rw [hstart]
              simp
              omega)
          rw [hadjusted]
          by_cases hpositive : 0 < adjusted.val
          · simp [hpositive]
            obtain ⟨mantissa2, exponent2, hmany⟩ :=
              NumberHelperFractionalManyDigitsSafeLength s intTail parsedExp
                intBytes fracBytes markerTail.val hstart (by simp [intTail])
                hintDigits hfracDigits hmarkerTailNotDigit hlen (by
                  have hb := hcombinedBounds.1
                  rw [hfracExponentVal, hnegAfterVal, hnAfterEq] at hb
                  simpa [IScalar.min_IScalarTy_I64_eq, I64.min_eq] using hb) (by
                  rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq]
                  rw [hstart] at hlen
                  simp at hlen
                  omega)
            rw [hmany]
            exact ⟨{
              exponent := exponent2
              mantissa := mantissa2
              negative := false
              many_digits := true
            }, rfl⟩
          · simp [hpositive]

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.Assumptions
import Tablet.DecimalGoalVocabularyDefault
import Tablet.NumberHelperDigitScanSafeLength
import Tablet.ModelSliceSplitFirst
import Tablet.NumberHelperAdjustManySafeLength
import Tablet.NumberHelperFractionalManyDigitsSafeLength
import Tablet.NumberHelperIntegralAdjustment
import Tablet.NumberHelperIntegralManyDigitsSafeLength
import Tablet.NumberHelperOffsetSafeLength
import Tablet.NumberHelperScientificRenderedSuffixSafeLength
import Tablet.NumberHelperScientificSuffixSafeLength
import Tablet.SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from
import Tablet.SourceValidByteSlice
import Tablet.num_imp_dec2flt_parse_MIN_19DIGIT_INT
import Tablet.num_imp_dec2flt_parse_adjust_many_digit_count
import Tablet.num_imp_dec2flt_parse_parse_many_digits
import Tablet.num_imp_dec2flt_parse_parse_partial_number
import Tablet.num_imp_dec2flt_parse_parse_scientific
import Tablet.num_imp_dec2flt_parse_try_parse_19digits

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperPartialAcceptanceSafeLength]
theorem NumberHelperPartialAcceptanceSafeLength :
    forall (s : Slice Std.U8) (x : DecimalLexeme),
      SourceValidByteSlice s -> s.val != [] -> x.leadingSign = none ->
      DecimalGoalVocabularyDefault.validNumeric x ->
      DecimalGoalVocabularyDefault.bytes s =
        DecimalGoalVocabularyDefault.renderNumeric x ->
      s.val.length <= 9223372036854120458 ->
      exists d,
        num.imp.dec2flt.parse.parse_partial_number s =
          ok (some (d, Slice.len s)) := by
-- BODY
  intro s x hsource hne hsign hvalid hbytes hlen
  rcases x with ⟨sign, ints, frac, expo⟩
  simp only at hsign
  subst sign
  simp [DecimalGoalVocabularyDefault] at hvalid hbytes
  have byte_digits : forall (bs : List Std.U8) (ds : List Nat),
      List.map (fun b : Std.U8 => b.val) bs =
        List.map (fun d : Nat => 48 + d) ds ->
      (forall d, List.Mem d ds -> d < 10) ->
      forall b, List.Mem b bs -> 48 <= b.val /\ b.val <= 57 := by
    intro bs ds hmap hds b hb
    have hbmap : b.val ∈ List.map (fun b : Std.U8 => b.val) bs :=
      List.mem_map_of_mem hb
    rw [hmap] at hbmap
    obtain ⟨d, hd, hdval⟩ := List.mem_map.mp hbmap
    have := hds d hd
    omega
  have offset_safe : forall (whole rest : Slice Std.U8),
      whole.val.length <= 9223372036854120458 ->
      rest.val.length <= whole.val.length ->
      exists z : Std.Isize,
        SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
          rest whole = ok z /\
        z.val = whole.val.length - rest.val.length := by
    intro whole rest hw hr
    have hbits := Dec2Flt.configured_x86_64_numBits
    have hwcast : (Slice.len whole).val <= IScalar.max .Isize := by
      simp [IScalar.max, IScalarTy.numBits, hbits]
      omega
    have hrcast : (Slice.len rest).val <= IScalar.max .Isize := by
      simp [IScalar.max, IScalarTy.numBits, hbits]
      omega
    have hwsp := UScalar.hcast_inBounds_spec .Isize (Slice.len whole) hwcast
    have hrsp := UScalar.hcast_inBounds_spec .Isize (Slice.len rest) hrcast
    obtain ⟨wi, hwi, hwival⟩ := Aeneas.Std.WP.spec_imp_exists hwsp
    obtain ⟨ri, hri, hrival⟩ := Aeneas.Std.WP.spec_imp_exists hrsp
    simp at hwival hrival
    have hsublo : IScalar.min .Isize <= wi.val - ri.val := by
      simp [IScalar.min, IScalarTy.numBits, hbits]
      omega
    have hsubhi : wi.val - ri.val <= IScalar.max .Isize := by
      simp [IScalar.max, IScalarTy.numBits, hbits]
      omega
    have hsub := IScalar.sub_spec hsublo hsubhi
    obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists hsub
    refine ⟨z, ?_, ?_⟩
    · simp [SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from,
        hwi, hri, hz]
    · omega
  cases frac with
  | none =>
    simp at hvalid hbytes
    rcases hvalid with ⟨hints, hintsne, hexp⟩
    cases expo with
    | none =>
      simp at hbytes
      have hsdigits := byte_digits s.val ints hbytes hints
      obtain ⟨mantissa, rest, hscan, hrest⟩ :=
        NumberHelperDigitScanSafeLength s 0#u64 s.val [] (by simp) hsdigits
          (by simp) hlen
      have hrestnil : rest.val = [] := by simpa using hrest
      let empty : Slice Std.U8 := ⟨[], by simp⟩
      have hresteq : rest = empty := (Slice.eq_iff _ _).2 hrestnil
      subst rest
      have hrestle : empty.val.length <= s.val.length := by simp [empty]
      obtain ⟨nDigits, hoff, hnval⟩ := offset_safe s empty hlen hrestle
      have hnpos : nDigits.val > 0 := by
        have hslen : s.val.length = ints.length := by
          have := congrArg List.length hbytes
          simpa using this
        have hintlen : 0 < ints.length := by
          cases ints <;> simp_all
        simp [empty] at hnval
        omega
      have hcastlen : lift (IScalar.hcast .Usize nDigits) = ok (Slice.len s) := by
        have hbound : 0 <= nDigits.val /\
            nDigits.val <= UScalar.max .Usize := by
          simp [empty] at hnval
          simp [UScalar.max, Dec2Flt.configured_x86_64_numBits]
          omega
        have hspec := IScalar.hcast_inBounds_spec .Usize nDigits hbound
        obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists hspec
        rw [hz]
        congr 2
        apply UScalar.eq_of_val_eq
        simp [empty] at hnval
        simpa [Slice.len_val] using (show z.val = s.val.length by omega)
      have haddzero : (nDigits + 0#isize) = ok nDigits := by
        have hspec := IScalar.add_spec (x := nDigits) (y := 0#isize)
          (by
            simp [IScalar.min, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits]
            omega)
          (by
            simp [IScalar.max, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits]
            omega)
        obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists hspec
        rw [hz]
        congr 2
        apply IScalar.eq_of_val_eq
        simpa using hzval
      have hnotempty : Not (s.val = []) := by simpa using hne
      unfold num.imp.dec2flt.parse.parse_partial_number
      simp [hnotempty, hscan, hoff, haddzero, hnpos, hcastlen, empty,
        core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst]
      split
      · rename_i hsmall
        exact ⟨{
          exponent := 0#i64
          mantissa := mantissa
          negative := false
          many_digits := false
        }, rfl⟩
      · rename_i hlarge
        have hsublo : IScalar.min .Isize <= nDigits.val - 19 := by
          simp [IScalar.min, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits]
          omega
        have hsubhi : nDigits.val - 19 <= IScalar.max .Isize := by
          simp [IScalar.max, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits]
          omega
        obtain ⟨nMore, hnMore, hnMoreVal⟩ :=
          Aeneas.Std.WP.spec_imp_exists
            (IScalar.sub_spec (x := nDigits) (y := 19#isize)
              (by simpa using hsublo) (by simpa using hsubhi))
        rw [hnMore]
        simp
        obtain ⟨adjusted, hadjusted, hadjustedPositive⟩ :=
          NumberHelperIntegralAdjustment s nMore s.val [] (by simp)
            hsdigits (by simp) (by simp) hlen (by
              simp [empty] at hnval
              norm_num at hnMoreVal
              omega)
        rw [hadjusted]
        simp
        split
        · rename_i hpositive
          obtain ⟨mantissa2, exponent2, hmany⟩ :=
            NumberHelperIntegralManyDigitsSafeLength s empty 0#i64
              nMore adjusted s.val [] (by simp) (by simp [empty])
              hsdigits (by simp) (by simp) hlen (by
                simp [empty] at hnval
                norm_num at hnMoreVal
                omega) hadjusted (by omega) (by
                rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq]
                norm_num
                norm_num at hnMoreVal
                simp [empty] at hnval
                omega)
          rw [hmany]
          exact ⟨{
            exponent := exponent2
            mantissa := mantissa2
            negative := false
            many_digits := true
          }, rfl⟩
        · exact ⟨{
            exponent := 0#i64
            mantissa := mantissa
            negative := false
            many_digits := false
          }, rfl⟩
    | some e =>
      simp at hbytes hexp
      rcases e with ⟨upper, sign, expds⟩
      simp only [Prod.fst, Prod.snd] at hbytes hexp
      obtain ⟨hexpne, hexpdigits⟩ := hexp
      obtain ⟨intBytes, afterInt, hsval, hintMap, hafterMap⟩ :=
        List.map_eq_append_iff.mp hbytes
      obtain ⟨marker, suffixBytes, hafterVal, hmarkerVal, hsuffixMap⟩ :=
        List.map_eq_cons_iff.mp hafterMap
      have hmarker : marker = if upper then 69#u8 else 101#u8 := by
        apply UScalar.eq_of_val_eq
        cases upper <;> simp_all
      subst marker
      have hsdecomp : s.val = intBytes ++
          (if upper then 69#u8 else 101#u8) :: suffixBytes := by
        rw [hsval, hafterVal]
      have hintDigits := byte_digits intBytes ints hintMap hints
      have hintTailProp :
          ((if upper then 69#u8 else 101#u8) :: suffixBytes).length <=
            Usize.max := by
        have := s.property
        rw [hsdecomp] at this
        simp only [List.length_append] at this
        omega
      let intTail : Slice Std.U8 :=
        ⟨(if upper then 69#u8 else 101#u8) :: suffixBytes, hintTailProp⟩
      obtain ⟨mantissa, intRest, hscan, hintRest⟩ :=
        NumberHelperDigitScanSafeLength s 0#u64 intBytes
          ((if upper then 69#u8 else 101#u8) :: suffixBytes)
          hsdecomp hintDigits (by
            intro b rest h
            simp only [List.cons.injEq] at h
            rcases h with ⟨rfl, _⟩
            cases upper <;> norm_num) hlen
      have hintRestVal : intRest.val =
          (if upper then 69#u8 else 101#u8) :: suffixBytes := by
        simpa using hintRest
      have hintRestEq : intRest = intTail := (Slice.eq_iff _ _).2 hintRestVal
      subst intRest
      obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ :=
        NumberHelperOffsetSafeLength s intTail hlen (by
          rw [hsdecomp]
          simp [intTail]
          )
      have hnDigitsEq : nDigits.val = intBytes.length := by
        rw [hnDigitsVal, hsdecomp]
        simp [intTail]
      have hsuffixProp : suffixBytes.length <= Usize.max := by
        have := intTail.property
        simp [intTail] at this
        omega
      let suffix : Slice Std.U8 := ⟨suffixBytes, hsuffixProp⟩
      have hsplitMarker : core.slice.Slice.split_first intTail =
          ok (some (if upper then 69#u8 else 101#u8, suffix)) := by
        simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
          intTail, suffix]
      have hsuffixLen : suffix.val.length <= 9223372036854120458 := by
        change suffixBytes.length <= 9223372036854120458
        rw [hsdecomp] at hlen
        simp only [List.length_append, List.length_cons] at hlen
        omega
      obtain ⟨parsedExp, empty, hscientific, hempty, hexplo, hexphi,
          hexpshort, _⟩ :=
        NumberHelperScientificRenderedSuffixSafeLength suffix sign expds
          (by simpa only [bne_iff_ne] using hexpne) hexpdigits
          (by simpa [suffix] using hsuffixMap) hsuffixLen
      have haddExp : (0#i64 + parsedExp) = ok parsedExp := by
        have hb : IScalar.min .I64 <= (0#i64).val + parsedExp.val /\
            (0#i64).val + parsedExp.val <= IScalar.max .I64 := by
          have hp := IScalar.hBounds parsedExp
          rw [IScalar.min_IScalarTy_I64_eq, I64.min_eq,
            IScalar.max_IScalarTy_I64_eq, I64.max_eq]
          norm_num at hp ⊢
          omega
        obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.add_spec hb.1 hb.2)
        rw [hz]
        congr 2
        apply IScalar.eq_of_val_eq
        norm_num at hzval
        exact hzval
      have hemptyLen : empty.val.length <= s.val.length := by
        simp [hempty]
      obtain ⟨fullOffset, hoffFull, hfullOffsetVal⟩ :=
        NumberHelperOffsetSafeLength s empty hlen hemptyLen
      have hfullCast : lift (IScalar.hcast .Usize fullOffset) =
          ok (Slice.len s) := by
        have hbound : 0 <= fullOffset.val /\
            fullOffset.val <= UScalar.max .Usize := by
          rw [hfullOffsetVal, hempty]
          simp [UScalar.max, Dec2Flt.configured_x86_64_numBits]
          omega
        obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.hcast_inBounds_spec .Usize fullOffset hbound)
        rw [hz]
        congr 2
        apply UScalar.eq_of_val_eq
        rw [hfullOffsetVal, hempty] at hzval
        simpa [Slice.len_val] using hzval
      have haddzero : (nDigits + 0#isize) = ok nDigits := by
        have hb : IScalar.min .Isize <= nDigits.val + (0#isize).val /\
            nDigits.val + (0#isize).val <= IScalar.max .Isize := by
          have hn := IScalar.hBounds nDigits
          simp [IScalar.min, IScalar.max, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits] at hn ⊢
          omega
        obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.add_spec hb.1 hb.2)
        rw [hz]
        congr 2
        apply IScalar.eq_of_val_eq
        norm_num at hzval
        exact hzval
      have hnpos : 0 < nDigits.val := by
        have : 0 < ints.length := List.length_pos_of_ne_nil hintsne
        have hlenEq := congrArg List.length hintMap
        simp only [List.length_map] at hlenEq
        omega
      have hstartIntTail : s.val = intBytes ++ intTail.val := by
        simpa [intTail] using hsdecomp
      have hintTailNotDigit : forall b rest, intTail.val = b :: rest ->
          Not (48 <= b.val /\ b.val <= 57) := by
        intro b rest h
        simp [intTail] at h
        rcases h with ⟨rfl, _⟩
        cases upper <;> norm_num
      have hintTailNotPoint : forall b rest, intTail.val = b :: rest ->
          b ≠ 46#u8 := by
        intro b rest h
        simp [intTail] at h
        rcases h with ⟨rfl, _⟩
        cases upper <;> norm_num
      have hnotempty : Not (s.val = []) := by simpa using hne
      unfold num.imp.dec2flt.parse.parse_partial_number
      simp [hnotempty, hscan, hoffInt]
      cases upper <;> simp [haddzero, hnpos, hsplitMarker, hscientific,
        haddExp, hoffFull, hfullCast, UScalar.ofNatCore,
        core.option.Option.Insts.CoreOpsTry_traitTry.branch,
        dec2flt_model.ModelOptionBranch]
      case false | true =>
        split
        · exact ⟨{
            exponent := parsedExp
            mantissa := mantissa
            negative := false
            many_digits := false
          }, rfl⟩
        · rename_i hlarge
          have hsublo : IScalar.min .Isize <= nDigits.val - 19 := by
            have hn := IScalar.hBounds nDigits
            simp [IScalar.min, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits] at hn ⊢
            omega
          have hsubhi : nDigits.val - 19 <= IScalar.max .Isize := by
            have hn := IScalar.hBounds nDigits
            simp [IScalar.max, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits] at hn ⊢
            omega
          obtain ⟨nMore, hnMore, hnMoreVal⟩ :=
            Aeneas.Std.WP.spec_imp_exists
              (IScalar.sub_spec (x := nDigits) (y := 19#isize)
                (by simpa using hsublo) (by simpa using hsubhi))
          rw [hnMore]
          simp
          obtain ⟨adjusted, hadjusted, hadjustedPositive⟩ :=
            NumberHelperIntegralAdjustment s nMore intBytes
              intTail.val hstartIntTail hintDigits hintTailNotDigit
              hintTailNotPoint hlen (by
                norm_num at hnMoreVal
                omega)
          rw [hadjusted]
          simp
          split
          · rename_i hpositive
            obtain ⟨mantissa2, exponent2, hmany⟩ :=
              NumberHelperIntegralManyDigitsSafeLength s intTail parsedExp
                nMore adjusted intBytes intTail.val hstartIntTail rfl
                hintDigits hintTailNotDigit hintTailNotPoint hlen (by
                  norm_num at hnMoreVal
                  omega) hadjusted hpositive (by
                  rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq]
                  rw [hsdecomp] at hlen
                  simp only [List.length_append, List.length_cons] at hlen
                  omega)
            rw [hmany]
            exact ⟨{
              exponent := exponent2
              mantissa := mantissa2
              negative := false
              many_digits := true
            }, rfl⟩
          · exact ⟨{
              exponent := parsedExp
              mantissa := mantissa
              negative := false
              many_digits := false
            }, rfl⟩
  | some fracs =>
    simp at hvalid hbytes
    rcases hvalid with ⟨hints, hfracs, hsome, hexp⟩
    cases expo with
    | none =>
      simp at hbytes
      obtain ⟨intBytes, afterInt, hsval, hintMap, hafterMap⟩ :=
        List.map_eq_append_iff.mp hbytes
      obtain ⟨point, fracBytes, hafterVal, hpointVal, hfracMap⟩ :=
        List.map_eq_cons_iff.mp hafterMap
      have hpoint : point = 46#u8 := by
        apply UScalar.eq_of_val_eq
        simpa using hpointVal
      subst point
      have hsdecomp : s.val = intBytes ++ 46#u8 :: fracBytes := by
        rw [hsval, hafterVal]
      have hintDigits := byte_digits intBytes ints hintMap hints
      have hfracDigits := byte_digits fracBytes fracs hfracMap hfracs
      have hintTailProp : (46#u8 :: fracBytes).length <= Usize.max := by
        have := s.property
        rw [hsdecomp] at this
        simp only [List.length_append] at this
        omega
      let intTail : Slice Std.U8 := ⟨46#u8 :: fracBytes, hintTailProp⟩
      obtain ⟨mantissa, intRest, hscanInt, hintRest⟩ :=
        NumberHelperDigitScanSafeLength s 0#u64 intBytes (46#u8 :: fracBytes)
          hsdecomp hintDigits (by
            intro b rest h
            simp only [List.cons.injEq] at h
            rcases h with ⟨rfl, _⟩
            norm_num) hlen
      have hintRestEq : intRest = intTail :=
        (Slice.eq_iff _ _).2 (by simpa [intTail] using hintRest)
      subst intRest
      obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ :=
        NumberHelperOffsetSafeLength s intTail hlen (by
          simp [intTail, hsdecomp])
      have hnDigitsEq : nDigits.val = intBytes.length := by
        rw [hnDigitsVal, hsdecomp]
        simp [intTail]
      have hfracProp : fracBytes.length <= Usize.max := by
        have := intTail.property
        simp [intTail] at this ⊢
        omega
      let fracSlice : Slice Std.U8 := ⟨fracBytes, hfracProp⟩
      have hsplitPoint : core.slice.Slice.split_first intTail =
          ok (some (46#u8, fracSlice)) := by
        simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
          intTail, fracSlice]
      obtain ⟨mantissa1, fracRest, hscanFrac, hfracRest⟩ :=
        NumberHelperDigitScanSafeLength fracSlice mantissa fracBytes []
          (by simp [fracSlice]) hfracDigits (by simp) (by
            simp [fracSlice]
            rw [hsdecomp] at hlen
            simp only [List.length_append, List.length_cons] at hlen
            omega)
      let empty : Slice Std.U8 := ⟨[], by simp⟩
      have hfracRestEq : fracRest = empty :=
        (Slice.eq_iff _ _).2 (by simpa [empty] using hfracRest)
      subst fracRest
      obtain ⟨nAfter, hoffFrac, hnAfterVal⟩ :=
        NumberHelperOffsetSafeLength fracSlice empty (by
          simp [fracSlice]
          rw [hsdecomp] at hlen
          simp only [List.length_append, List.length_cons] at hlen
          omega) (by simp [empty])
      have hnAfterEq : nAfter.val = fracBytes.length := by
        rw [hnAfterVal]
        simp [fracSlice, empty]
      have hnAfterNotMin : nAfter.val ≠ IScalar.min .Isize := by
        rw [hnAfterVal]
        simp [empty, IScalar.min, IScalarTy.numBits,
          Dec2Flt.configured_x86_64_numBits]
      obtain ⟨negAfter, hnegAfter, hnegAfterVal⟩ :=
        Aeneas.Std.WP.spec_imp_exists (HNeg.hNeg.step nAfter hnAfterNotMin)
      have hnegCastBounds : IScalar.min .I64 <= negAfter.val /\
          negAfter.val <= IScalar.max .I64 := by
        have hlenParts := hlen
        rw [hsdecomp] at hlenParts
        simp only [List.length_append, List.length_cons] at hlenParts
        rw [hnegAfterVal, hnAfterEq,
          IScalar.min_IScalarTy_I64_eq, I64.min_eq,
          IScalar.max_IScalarTy_I64_eq, I64.max_eq]
        constructor <;> omega
      obtain ⟨fracExponent, hfracExponent, hfracExponentVal⟩ :=
        Aeneas.Std.WP.spec_imp_exists
          (IScalar.cast_inBounds_spec .I64 negAfter hnegCastBounds)
      have htotalBounds : IScalar.min .Isize <= nDigits.val + nAfter.val /\
          nDigits.val + nAfter.val <= IScalar.max .Isize := by
        rw [hnDigitsEq, hnAfterEq]
        simp [IScalar.min, IScalar.max,
          IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits]
        rw [hsdecomp] 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 hintLen : intBytes.length = ints.length := by
        have := congrArg List.length hintMap
        simpa using this
      have hfracLen : fracBytes.length = fracs.length := by
        have := congrArg List.length hfracMap
        simpa using this
      have htotalPos : 0 < totalDigits.val := by
        rw [htotalVal, hnDigitsEq, hnAfterEq]
        rcases hsome with hneInt | hneFrac
        · have : 0 < ints.length := List.length_pos_of_ne_nil hneInt
          omega
        · have : 0 < fracs.length := List.length_pos_of_ne_nil hneFrac
          omega
      have htotalLen : totalDigits.val + 1 = s.val.length := by
        rw [htotalVal, hnDigitsEq, hnAfterEq, hsdecomp]
        simp
        omega
      obtain ⟨fullOffset, hoffFull, hfullOffsetVal⟩ :=
        NumberHelperOffsetSafeLength s empty hlen (by simp [empty])
      have hfullCast : lift (IScalar.hcast .Usize fullOffset) =
          ok (Slice.len s) := by
        have hbound : 0 <= fullOffset.val /\
            fullOffset.val <= UScalar.max .Usize := by
          rw [hfullOffsetVal]
          simp [empty, UScalar.max, Dec2Flt.configured_x86_64_numBits]
          omega
        obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.hcast_inBounds_spec .Usize fullOffset hbound)
        rw [hz]
        congr 2
        apply UScalar.eq_of_val_eq
        rw [hfullOffsetVal] at hzval
        simpa [empty, Slice.len_val] using hzval
      have hnotempty : Not (s.val = []) := by simpa using hne
      have hsplitEmpty : core.slice.Slice.split_first empty = ok none := by
        simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
          empty]
      have hpointMatch :
          (match (46#u8 : Std.U8) with
          | 46#uscalar => ok (empty, mantissa1, nAfter, fracExponent)
          | _ => ok (intTail, mantissa, 0#isize, 0#i64)) =
            ok (empty, mantissa1, nAfter, fracExponent) := by
        rfl
      unfold num.imp.dec2flt.parse.parse_partial_number
      simp [hnotempty, hscanInt, hoffInt, hsplitPoint, hscanFrac,
        hoffFrac, hnegAfter, hfracExponent, htotal, htotalPos, hsplitEmpty,
        hoffFull, hfullCast]
      norm_num
      simp only [UScalar.ofNat, UScalar.ofNatCore]
      simp [htotal, htotalPos, hsplitEmpty, hoffFull, hfullCast]
      split
      · exact ⟨{
          exponent := fracExponent
          mantissa := mantissa1
          negative := false
          many_digits := false
        }, rfl⟩
      · rename_i hlarge
        have hsublo : IScalar.min .Isize <= totalDigits.val - 19 := by
          have ht := IScalar.hBounds totalDigits
          simp [IScalar.min, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits] at ht ⊢
          omega
        have hsubhi : totalDigits.val - 19 <= IScalar.max .Isize := by
          have ht := IScalar.hBounds totalDigits
          simp [IScalar.max, IScalarTy.numBits,
            Dec2Flt.configured_x86_64_numBits] at ht ⊢
          omega
        obtain ⟨nMore, hnMore, hnMoreVal⟩ :=
          Aeneas.Std.WP.spec_imp_exists
            (IScalar.sub_spec (x := totalDigits) (y := 19#isize)
              (by simpa using hsublo) (by simpa using hsubhi))
        rw [hnMore]
        simp
        obtain ⟨adjusted, hadjusted⟩ :=
          NumberHelperAdjustManySafeLength s nMore hlen (by
            norm_num at hnMoreVal
            omega) (by
            norm_num at hnMoreVal
            rw [hnMoreVal]
            omega)
        rw [hadjusted]
        simp
        split
        · rename_i hpositive
          obtain ⟨mantissa2, exponent2, hmany⟩ :=
            NumberHelperFractionalManyDigitsSafeLength s intTail 0#i64
              intBytes fracBytes [] (by simpa [List.append_assoc] using hsdecomp)
              (by simp [intTail]) hintDigits hfracDigits (by simp) hlen
              (by
                rw [IScalar.min_IScalarTy_I64_eq, I64.min_eq]
                norm_num
                rw [hsdecomp] at hlen
                simp only [List.length_append, List.length_cons] at hlen
                omega) (by
                rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq]
                norm_num
                rw [hsdecomp] at hlen
                simp only [List.length_append, List.length_cons] at hlen
                omega)
          rw [hmany]
          exact ⟨{
            exponent := exponent2
            mantissa := mantissa2
            negative := false
            many_digits := true
          }, rfl⟩
        · exact ⟨{
            exponent := fracExponent
            mantissa := mantissa1
            negative := false
            many_digits := false
          }, rfl⟩
    | some e =>
      simp at hbytes hexp
      rcases e with ⟨upper, sign, expds⟩
      simp only [Prod.fst, Prod.snd] at hbytes hexp
      obtain ⟨hexpne, hexpdigits⟩ := hexp
      obtain ⟨intBytes, afterInt, hsval, hintMap, hafterMap⟩ :=
        List.map_eq_append_iff.mp hbytes
      obtain ⟨point, afterPoint, hafterVal, hpointVal, hafterPointMap⟩ :=
        List.map_eq_cons_iff.mp hafterMap
      have hpoint : point = 46#u8 := by
        apply UScalar.eq_of_val_eq
        simpa using hpointVal
      subst point
      obtain ⟨fracBytes, afterFrac, hafterPointVal, hfracMap,
          hafterFracMap⟩ := List.map_eq_append_iff.mp hafterPointMap
      obtain ⟨marker, suffixBytes, hafterFracVal, hmarkerVal, hsuffixMap⟩ :=
        List.map_eq_cons_iff.mp hafterFracMap
      have hmarker : marker = if upper then 69#u8 else 101#u8 := by
        apply UScalar.eq_of_val_eq
        cases upper <;> simp_all
      subst marker
      have hsdecomp : s.val = intBytes ++ 46#u8 ::
          (fracBytes ++ (if upper then 69#u8 else 101#u8) :: suffixBytes) := by
        rw [hsval, hafterVal, hafterPointVal, hafterFracVal]
      have hintDigits := byte_digits intBytes ints hintMap hints
      have hfracDigits := byte_digits fracBytes fracs hfracMap hfracs
      have hmarkerTailProp :
          ((if upper then 69#u8 else 101#u8) :: suffixBytes).length <=
            Usize.max := by
        have := s.property
        rw [hsdecomp] at this
        simp only [List.length_append, List.length_cons] at this ⊢
        omega
      let markerTail : Slice Std.U8 :=
        ⟨(if upper then 69#u8 else 101#u8) :: suffixBytes,
          hmarkerTailProp⟩
      have hmarkerTailNotDigit : forall b rest,
          markerTail.val = b :: rest ->
          Not (48 <= b.val /\ b.val <= 57) := by
        intro b rest h
        simp [markerTail] at h
        rcases h with ⟨rfl, _⟩
        cases upper <;> norm_num
      have hintTailProp :
          (46#u8 :: (fracBytes ++ markerTail.val)).length <= Usize.max := by
        have := s.property
        rw [hsdecomp] at this
        simp [markerTail] at this ⊢
        omega
      let intTail : Slice Std.U8 :=
        ⟨46#u8 :: (fracBytes ++ markerTail.val), hintTailProp⟩
      have hstart : s.val = intBytes ++ 46#u8 ::
          (fracBytes ++ markerTail.val) := by
        simpa [markerTail] using hsdecomp
      obtain ⟨mantissa, intRest, hscanInt, hintRest⟩ :=
        NumberHelperDigitScanSafeLength s 0#u64 intBytes intTail.val
          hstart hintDigits (by
            intro b rest h
            simp [intTail] at h
            rcases h with ⟨rfl, _⟩
            norm_num) hlen
      have hintRestEq : intRest = intTail :=
        (Slice.eq_iff _ _).2 (by simpa using hintRest)
      subst intRest
      obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ :=
        NumberHelperOffsetSafeLength s intTail hlen (by
          rw [hstart]
          simp [intTail])
      have hnDigitsEq : nDigits.val = intBytes.length := by
        rw [hnDigitsVal, hstart]
        simp [intTail]
      have hfracSliceProp : (fracBytes ++ markerTail.val).length <=
          Usize.max := by
        have := intTail.property
        simp [intTail] at this ⊢
        omega
      let fracSlice : Slice Std.U8 :=
        ⟨fracBytes ++ markerTail.val, hfracSliceProp⟩
      have hsplitPoint : core.slice.Slice.split_first intTail =
          ok (some (46#u8, fracSlice)) := by
        simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
          intTail, fracSlice]
      obtain ⟨mantissa1, fracRest, hscanFrac, hfracRest⟩ :=
        NumberHelperDigitScanSafeLength fracSlice mantissa fracBytes
          markerTail.val (by simp [fracSlice]) hfracDigits hmarkerTailNotDigit
          (by
            simp [fracSlice]
            rw [hstart] at hlen
            simp only [List.length_append, List.length_cons] at hlen
            omega)
      have hfracRestEq : fracRest = markerTail :=
        (Slice.eq_iff _ _).2 (by simpa using hfracRest)
      subst fracRest
      obtain ⟨nAfter, hoffFrac, hnAfterVal⟩ :=
        NumberHelperOffsetSafeLength fracSlice markerTail (by
          simp [fracSlice]
          rw [hstart] at hlen
          simp only [List.length_append, List.length_cons] at hlen
          omega) (by simp [fracSlice])
      have hnAfterEq : nAfter.val = fracBytes.length := by
        rw [hnAfterVal]
        simp [fracSlice]
      have hnAfterNotMin : nAfter.val ≠ IScalar.min .Isize := by
        rw [hnAfterEq, IScalar.min]
        simp [IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits]
      obtain ⟨negAfter, hnegAfter, hnegAfterVal⟩ :=
        Aeneas.Std.WP.spec_imp_exists (HNeg.hNeg.step nAfter hnAfterNotMin)
      have hnegCastBounds : IScalar.min .I64 <= negAfter.val /\
          negAfter.val <= IScalar.max .I64 := by
        have hparts := hlen
        rw [hstart] at hparts
        simp only [List.length_append, List.length_cons] at hparts
        rw [hnegAfterVal, hnAfterEq,
          IScalar.min_IScalarTy_I64_eq, I64.min_eq,
          IScalar.max_IScalarTy_I64_eq, I64.max_eq]
        constructor <;> omega
      obtain ⟨fracExponent, hfracExponent, hfracExponentVal⟩ :=
        Aeneas.Std.WP.spec_imp_exists
          (IScalar.cast_inBounds_spec .I64 negAfter hnegCastBounds)
      have htotalBounds : IScalar.min .Isize <= nDigits.val + nAfter.val /\
          nDigits.val + nAfter.val <= IScalar.max .Isize := by
        rw [hnDigitsEq, hnAfterEq]
        simp [IScalar.min, IScalar.max, IScalarTy.numBits,
          Dec2Flt.configured_x86_64_numBits]
        rw [hstart] 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 htotalPos : 0 < totalDigits.val := by
        rw [htotalVal, hnDigitsEq, hnAfterEq]
        have hintLen : intBytes.length = ints.length := by
          have := congrArg List.length hintMap
          simpa using this
        have hfracLen : fracBytes.length = fracs.length := by
          have := congrArg List.length hfracMap
          simpa using this
        rcases hsome with hneInt | hneFrac
        · have := List.length_pos_of_ne_nil hneInt
          omega
        · have := List.length_pos_of_ne_nil hneFrac
          omega
      have hsuffixProp : suffixBytes.length <= Usize.max := by
        have := markerTail.property
        simp [markerTail] at this
        omega
      let suffix : Slice Std.U8 := ⟨suffixBytes, hsuffixProp⟩
      have hsplitMarker : core.slice.Slice.split_first markerTail =
          ok (some (if upper then 69#u8 else 101#u8, suffix)) := by
        simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
          markerTail, suffix]
      have hsuffixLen : suffix.val.length <= 9223372036854120458 := by
        change suffixBytes.length <= 9223372036854120458
        rw [hstart] at hlen
        simp [markerTail, intTail] at hlen
        omega
      obtain ⟨parsedExp, empty, hscientific, hempty, hexplo, hexphi,
          hexpshort, hexpnonnegative⟩ :=
        NumberHelperScientificRenderedSuffixSafeLength suffix sign expds
          (by simpa only [bne_iff_ne] using hexpne) hexpdigits
          (by simpa [suffix] using hsuffixMap) hsuffixLen
      have hsuffixLenEq : suffixBytes.length =
          (match sign with
          | none => []
          | some negative => [if negative then 45 else 43]).length +
            expds.length := by
        have := congrArg List.length hsuffixMap
        simpa using this
      have hcombinedBounds : IScalar.min .I64 <=
            fracExponent.val + parsedExp.val /\
          fracExponent.val + parsedExp.val <= IScalar.max .I64 := by
        have hparts := hlen
        rw [hstart] at hparts
        simp [markerTail, intTail] at hparts
        rw [hfracExponentVal, hnegAfterVal, hnAfterEq,
          IScalar.min_IScalarTy_I64_eq, I64.min_eq,
          IScalar.max_IScalarTy_I64_eq, I64.max_eq]
        constructor
        · by_cases hshort : expds.length <= 5
          · have hsharp := hexpshort hshort
            omega
          · have hq : 6 <= expds.length := by omega
            by_cases hnegative : sign = some true
            · subst sign
              simp at hsuffixLenEq
              omega
            · have hnonnegative := hexpnonnegative hnegative
              omega
        · omega
      obtain ⟨combinedExponent, hcombinedExponent,
          hcombinedExponentVal⟩ := Aeneas.Std.WP.spec_imp_exists
        (IScalar.add_spec hcombinedBounds.1 hcombinedBounds.2)
      have h101Target : (101#u8 : Std.U8) = 101#uscalar := by
        apply UScalar.eq_of_val_eq
        rfl
      have h69Target : (69#u8 : Std.U8) = 69#uscalar := by
        apply UScalar.eq_of_val_eq
        rfl
      obtain ⟨fullOffset, hoffFull, hfullOffsetVal⟩ :=
        NumberHelperOffsetSafeLength s empty hlen (by simp [hempty])
      have hfullCast : lift (IScalar.hcast .Usize fullOffset) =
          ok (Slice.len s) := by
        have hbound : 0 <= fullOffset.val /\
            fullOffset.val <= UScalar.max .Usize := by
          rw [hfullOffsetVal, hempty]
          simp [UScalar.max, Dec2Flt.configured_x86_64_numBits]
          omega
        obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.hcast_inBounds_spec .Usize fullOffset hbound)
        rw [hz]
        congr 2
        apply UScalar.eq_of_val_eq
        rw [hfullOffsetVal, hempty] at hzval
        simpa [Slice.len_val] using hzval
      have hnotempty : Not (s.val = []) := by simpa using hne
      unfold num.imp.dec2flt.parse.parse_partial_number
      simp [hnotempty, hscanInt, hoffInt, hsplitPoint, hscanFrac,
        hoffFrac, hnegAfter, hfracExponent]
      norm_num
      simp only [UScalar.ofNat, UScalar.ofNatCore]
      cases upper <;> simp [htotal, htotalPos, hsplitMarker, hscientific,
        hcombinedExponent, hoffFull, hfullCast,
        core.option.Option.Insts.CoreOpsTry_traitTry.branch,
        dec2flt_model.ModelOptionBranch, UScalar.ofNatCore,
        h101Target, h69Target]
      case false | true =>
        by_cases hsmall : totalDigits.val <= 19
        · exact ⟨{
            exponent := combinedExponent
            mantissa := mantissa1
            negative := false
            many_digits := false
          }, by simp [hsmall]⟩
        · simp [hsmall]
          have hsublo : IScalar.min .Isize <= totalDigits.val - 19 := by
            have ht := IScalar.hBounds totalDigits
            simp [IScalar.min, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits] at ht ⊢
            omega
          have hsubhi : totalDigits.val - 19 <= IScalar.max .Isize := by
            have ht := IScalar.hBounds totalDigits
            simp [IScalar.max, IScalarTy.numBits,
              Dec2Flt.configured_x86_64_numBits] at ht ⊢
            omega
          obtain ⟨nMore, hnMore, hnMoreVal⟩ :=
            Aeneas.Std.WP.spec_imp_exists
              (IScalar.sub_spec (x := totalDigits) (y := 19#isize)
                (by simpa using hsublo) (by simpa using hsubhi))
          rw [hnMore]
          simp
          obtain ⟨adjusted, hadjusted⟩ :=
            NumberHelperAdjustManySafeLength s nMore hlen (by
              norm_num at hnMoreVal
              omega) (by
              norm_num at hnMoreVal
              rw [hnMoreVal]
              rw [htotalVal, hnDigitsEq, hnAfterEq]
              rw [hstart]
              simp
              omega)
          rw [hadjusted]
          by_cases hpositive : 0 < adjusted.val
          · simp [hpositive]
            obtain ⟨mantissa2, exponent2, hmany⟩ :=
              NumberHelperFractionalManyDigitsSafeLength s intTail parsedExp
                intBytes fracBytes markerTail.val hstart (by simp [intTail])
                hintDigits hfracDigits hmarkerTailNotDigit hlen (by
                  have hb := hcombinedBounds.1
                  rw [hfracExponentVal, hnegAfterVal, hnAfterEq] at hb
                  simpa [IScalar.min_IScalarTy_I64_eq, I64.min_eq] using hb) (by
                  rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq]
                  rw [hstart] at hlen
                  simp at hlen
                  omega)
            rw [hmany]
            exact ⟨{
              exponent := exponent2
              mantissa := mantissa2
              negative := false
              many_digits := true
            }, rfl⟩
          · simp [hpositive]

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (68)
More program dependencies (35)
Standard-library dependencies (30)

Related source and models come from the extraction records and declared dependencies.

Proof assumptions and review

Lean checks the formal proof. Trellis also reviews how the statement corresponds to the intended requirement.

Transitive assumptions

This list combines the assumptions recorded for this declaration and its dependencies. It may include assumptions used by auxiliary declarations in those modules.

Helper theorems included
All 66 entries
Technical checking records
Local closure summary
{
  "node": "NumberHelperPartialAcceptanceSafeLength",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "166a6523f952970f3b6136dc7d0d7cd1399c2bf03b0aac7b75c35f7a9461a8c1",
  "active_statement_hash": "4b1862a7360198fb98b9c140294d0f02a9d91bb787f4775d6cce012b35df4dae",
  "kernel_axioms": [
    "Classical.choice",
    "Dec2Flt.configured_x86_64_numBits",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "ab64d13f256386ee0e494a5abcc6a5db49512cf3d90bbdfcabd6327793a31f95",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-110",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalGoalVocabularyDefault": "2bbf2be48b5a4a4b",
    "ModelSliceSplitFirst": "f617ee05dd2925a9",
    "NumberHelperAdjustManySafeLength": "95baec0cbdffaa61",
    "NumberHelperDigitScanSafeLength": "890b3a333811314c",
    "NumberHelperFractionalManyDigitsSafeLength": "a5629aa775e4c63f",
    "NumberHelperIntegralAdjustment": "87a0993d47acb7bb",
    "NumberHelperIntegralManyDigitsSafeLength": "189c7a8b78fa907f",
    "NumberHelperOffsetSafeLength": "20aaaa4dc95189e4",
    "NumberHelperScientificRenderedSuffixSafeLength": "ab2dc4e68839f3a1",
    "num_imp_dec2flt_parse_parse_partial_number": "0f3e5cc6a65d812e"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "c08dd4b4aea4abb4",
    "DecimalGoalVocabularyDefault": "4634372c8f7a7f44",
    "DecimalLexeme": "18938585e7684823",
    "F64": "ec4e25fbaf4e11d4",
    "ModelOptionBranch": "2b227898dbe15e59",
    "ModelSliceSplitFirst": "4ed9c51a5bb9fb58",
    "NumberHelperScientificRenderedSuffixSafeLength": "28ca0a8ac503ee01",
    "Preamble": "4adf4172ae751d7d",
    "SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from": "7e7284dc9bc88830",
    "SourceValidByteSlice": "3dc42716a6378d26",
    "num_imp_dec2flt_common_BiasedFp": "31d81cf1cf22709f",
    "num_imp_dec2flt_decimal_Decimal": "4c41a56bf44cb31e",
    "num_imp_dec2flt_parse_adjust_many_digit_count": "4e59e3776b6cfdaa",
    "num_imp_dec2flt_parse_parse_many_digits": "50c9ef13b3ed7fe8",
    "num_imp_dec2flt_parse_parse_partial_number": "359d30eefc709c2a",
    "num_imp_dec2flt_parse_parse_scientific": "447f722d5841d2c5",
    "num_imp_dec2flt_parse_try_parse_digits": "238b4a73cd46fc56"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:98f29485017e6821844f2cab9cb051ca15ecdd87d45203755b5d0954f2eebab7",
  "current_fingerprint": "sound-v2:98f29485017e6821844f2cab9cb051ca15ecdd87d45203755b5d0954f2eebab7",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}