InvalidOverflowBoundaryWitness

Lean checked

Statement

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

Lemma · Invalid overflow immediately beyond the rejection bound

There is a source-valid Rust string and byte slice whose bytes are
[46]\mathbin{++}\operatorname{replicate}(9223372036854120450,49) \mathbin{++}[101,45,54,53,53,51,53,57,120].
It has length 9223372036854120460, is nonempty, belongs to neither the complete numeric grammar nor the complete special grammar, and hence is classified invalid. Nevertheless, the extracted parse_number and the public extracted f64 (binary64) specialization of dec2flt both return fail integerOverflow.

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.SourceValidByteSlice
import Tablet.F64_Insts_CoreOpsArithDivF64F64
import Tablet.F64_Insts_CoreOpsArithMulF64F64
import Tablet.F64_Insts_CoreOpsArithNegF64
import Tablet.F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64
import Tablet.ModelSliceFirst
import Tablet.ModelStrAsBytes
import Tablet.NumberHelperSharpFractionBytes
import Tablet.NumberHelperSharpTrailingParseFailure
import Tablet.SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from
import Tablet.SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_read_u64
import Tablet.num_imp_dec2flt_common_is_8digits
import Tablet.num_imp_dec2flt_dec2flt
import Tablet.num_imp_dec2flt_dec2flt_with_trace
import Tablet.num_imp_dec2flt_parse_parse_8digits
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.num_imp_dec2flt_parse_try_parse_digits

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: InvalidOverflowBoundaryWitness]
theorem InvalidOverflowBoundaryWitness :
    exists s : Str,
      s.val = [46#u8] ++ List.replicate 9223372036854120450 49#u8 ++
        [101#u8, 45#u8, 54#u8, 53#u8, 53#u8, 51#u8, 53#u8, 57#u8,
          120#u8] /\
      s.val.length = 9223372036854120460 /\
      SourceValidByteSlice s /\
      DecimalGoalVocabularyDefault.sourceString s /\
      s.val != [] /\
      (Not (exists x : DecimalLexeme,
        DecimalGoalVocabularyDefault.validNumeric x /\
        DecimalGoalVocabularyDefault.renderNumeric x =
          DecimalGoalVocabularyDefault.bytes s)) /\
      (Not (exists (sign : Option Bool) (nan : Bool),
        DecimalGoalVocabularyDefault.specialLiteral
          (DecimalGoalVocabularyDefault.bytes s) sign nan)) /\
      DecimalGoalVocabularyDefault.classifies
        (DecimalGoalVocabularyDefault.bytes s) none /\
      num.imp.dec2flt.parse.parse_number s = fail integerOverflow /\
      num.imp.dec2flt.dec2flt
        F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64
        F64.Insts.CoreOpsArithDivF64F64
        F64.Insts.CoreOpsArithMulF64F64
        F64.Insts.CoreOpsArithNegF64 s = fail integerOverflow := 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
K=9223372036854120450,\qquad q=1152921504606765056.
Thus K=8q+2. Let s be the displayed list, regarded as an Aeneas Str; its byte list is also the Slice U8 passed to the extracted parser. Its length is 1+K+9=9223372036854120460. This is below both 2^{63}-1=9223372036854775807 and the 64-bit Usize maximum supplied by Assumptions, so the carrier exists and satisfies SourceValidByteSlice. Every displayed byte is ASCII. Taking the Lean string consisting of a point, K copies of the character 1, and e-655359x gives exactly these UTF-8 bytes. The source-string clause of DecimalGoalVocabularyDefault therefore holds. The first byte also proves nonemptiness. Write
E=[101,45,54,53,53,51,53,57,120]
for the nine-byte suffix e-655359x.
Unfold the two complete grammars in DecimalGoalVocabularyDefault. A numeric rendering that begins with a point has no leading sign and no integer digit, so validity forces the run of bytes 49 following the point to be its nonempty fractional digit list. The following byte 101 can then only be the lower-case exponent marker, byte 45 its minus sign, and the next six bytes the exponent digits 655359. The remaining final byte 120 is neither an exponent digit nor any byte that the numeric renderer may append. Thus no valid numeric lexeme renders the complete list. A special rendering begins, after an optional sign, with an ASCII case variant of i or n; the first byte here is a point. Hence no special literal renders it either. These are exactly the two negated existentials in the none case of classifies, proving the stated invalid classification.
It remains to check that failure occurs before the unmatched x can be used for whole-input rejection. First execute the initial digit scan in num imp dec2flt parse try parse digits. Its input begins with [46,49,49,49,49,49,49,49]. The bulk-loop length guard is true, so the eight-byte range in SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice read u64 is in bounds. That definition copies those bytes and returns the little-endian word 0x313131313131312e. In num imp dec2flt common is 8digits, the fixed wrapping addition and subtraction give respectively 0x7777777777777774 and 0x01010101010100fe; after OR and masking by 0x8080808080808080, the result is 0x80, not zero. The bulk loop therefore stops without parsing or dropping a block. Its scalar loop reads the point at the guarded index zero; wrapping subtraction of 48 from 46 gives 254, which is not below ten, so the scalar count remains zero and slicing at index zero returns the whole input. Finally, SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice offset from casts the two equal lengths to isize and subtracts them. Both casts are exact because the common length is below 2^{63}-1, and the result is the integer-digit count zero.
The guarded split in num imp dec2flt parse parse partial number now removes the point and presents
T_0=\operatorname{replicate}(K,49)\mathbin{++}E
to the fractional digit scan. We prove the exact behavior of its bulk loop by induction on the number of completed iterations. For 0\le r\le q, let
T_r=\operatorname{replicate}(K-8r,49)\mathbin{++}E.
The induction assertion is that after r continuing iterations the residual slice is exactly T_r, exactly 8r fractional bytes have been dropped, the accumulator is a normally returned u64, and every operation so far has returned normally. The base case is the displayed T_0, with zero bytes dropped and the normally returned accumulator from the initial scan. Also 8r\le8q=K-2<K+9<2^{63}<2^{64}, so every implicit byte count in the assertion is a valid Usize.
For the induction step, assume r<q. Then
K-8r=8(q-r)+2\ge10.
Thus the first eight bytes of T_r are all 49, and |T_r|=(K-8r)+9\ge19. The length guard is consequently true and justifies both the eight-byte read in SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice read u64 and the later range slice dropping exactly eight bytes. The read returns the little-endian word
v=\texttt{0x3131313131313131}.
For the test in num imp dec2flt common is 8digits, the wrapping sum and difference are 0x7777777777777777 and 0x0101010101010101; their OR has zero intersection with 0x8080808080808080. Hence the test returns true.
Every checked operation in num imp dec2flt parse parse 8digits is also in range. Subtracting 0x3030303030303030 from v gives 0x0101010101010101. Multiplication by ten gives 0x0a0a0a0a0a0a0a0a, shifting the subtracted word right by eight gives 0x0001010101010101, and their checked sum is 0x0a0b0b0b0b0b0b0b<2^{64}$. The remaining masked multiplications and addition are wrapping. On this input the first mask gives 0x0000000b0000000b; the two wrapping products are 0x00a7dd0c0000044c and 0x0001adbb0000000b; and their wrapping sum is 0x00a98ac700000457. The fixed right shifts by 8, 16, and 32 are all below the 64-bit width. The last result is 0x0000000000a98ac7=11111111<2^{32}$, so the u32 cast succeeds and the helper returns 11111111. The enclosing accumulator multiplication by 100000000 and addition are explicitly wrapping and hence return some u64 for every prior accumulator. Dropping the first eight bytes is in bounds and leaves exactly
\operatorname{replicate}(K-8(r+1),49)\mathbin{++}E=T_{r+1}.
This proves the induction step, including every guarded read, test, decimal parse, cast, accumulator update, and slice operation in all q continuing iterations.
At the endpoint K-8q=2, so the exact residual is the eleven-byte slice
T_q=[49,49,101,45,54,53,53,51,53,57,120].
Its length guard is still true. The guarded read of its first eight bytes returns the little-endian word 0x333535362d653131. In the cited eight-digit test, the wrapping sum and difference are 0x797b7b7c73ab7777 and 0x03050505fd350101; their OR masked by 0x8080808080808080 is 0x0000000080800000, which is nonzero. The bulk loop therefore rejects this mixed block, calls neither parse_8digits nor the drop operation, and hands exactly T_q to the scalar loop.
That loop begins with count zero. At counts zero and one, the guard is true, the indexed byte is 49, wrapping subtraction by 48 gives digit 1, and the accumulator multiplication and addition are wrapping. The checked count increments yield one and two, both within Usize and below the eleven-byte slice length. At count two the guard is again true and the indexed byte is 101; wrapping subtraction gives 53, not below ten, so the loop stops without a third increment. Its final range slice at the in-bounds index two is precisely E. Thus the scan consumes exactly 8q+2=K ones and no byte of e-655359x.
The fractional call to the cited offset_from now casts the original fractional-scan length K+9=9223372036854120459 and the residual length 9 to isize, then computes their checked difference K. Both casts are exact because both lengths are below 2^{63}-1, and the nonnegative difference is in range. Its checked negation is -K, still above i64::MIN, and its cast to the 64-bit i64 is exact. The earlier integer-digit count is zero, so the checked significand-count addition is 0+K=K, in range and nonzero.
The exponent split then consumes byte 101 and passes [45,54,53,53,51,53,57,120], namely -655359x, to num imp dec2flt parse parse scientific. That function's guarded first split removes the minus sign, leaving the seven-byte slice [54,53,53,51,53,57,120]. Its initial slice is nonempty and byte 54 is an ASCII digit. At loop counts 0,1,2,3,4,5, all strictly below seven, the indexed bytes are the six digits and the accumulated values after their updates are successively 6,65,655,6553,65535,655359. Before every update the previous value is below 65536; each digit-to-i64 cast is exact; each multiplication, addition, and count increment is in range; and the largest checked arithmetic is 10\cdot65535+9=655359<2^{63}. At count six the guard is still true but the indexed byte is 120; subtracting 48 gives 72, not below ten, so the loop stops without incrementing or consuming it. Slicing at the in-bounds index six returns exactly [120], and checked negation of 655359 returns -655359. Thus the scientific parser returns that value with precisely the one-byte residual suffix x. The option branch continues, and the next operation in num imp dec2flt parse parse partial number is the checked addition
-K+(-655359) =-(9223372036854120450+655359) =-9223372036854775809=-2^{63}-1.
This is one below i64::MIN, so the extracted addition returns fail integerOverflow. In particular, the partial parser fails before its subsequent offset_from, consumed-length cast, many-digit branch, or return record, and hence before the trailing byte can cause ordinary whole-input rejection. The defining bind in num imp dec2flt parse parse number propagates exactly that failure.
Finally, ModelStrAsBytes gives the identical byte slice to the public dispatcher, and ModelSliceFirst returns its initial point. That byte is neither sign, so num imp dec2flt dec2flt with trace calls parse_number on the unchanged nonempty slice. Concretely, the tests against bytes 45 and 43 both fail, the body remains the entire nonempty slice, and the next bind is the numeric helper call. The failure just established propagates from that bind before the numeric-result match, special parsing, or construction of a source-level error; this prefix is independent of the later F64 operations, so it applies to exactly the four concrete instances in the Lean equation. The traced F64 call therefore has the same failure. The outer bind in num imp dec2flt dec2flt propagates it once more, which proves the asserted public failure.

Lean proof


  obtain ⟨fractionBytes, hfractionValue, hfractionLength⟩ :=
    NumberHelperSharpFractionBytes
  have hbound : (46#u8 :: fractionBytes ++ [120#u8]).length <= Usize.max := by
    simp only [List.length_cons, List.length_append, List.length_nil,
      hfractionLength]
    have h := Usize.max_succ_eq_pow
    rw [Dec2Flt.configured_x86_64_numBits] at h
    norm_num at h
    omega
  let s : Str := ⟨46#u8 :: fractionBytes ++ [120#u8], hbound⟩
  have hs : s.val = [46#u8] ++
      List.replicate 9223372036854120450 49#u8 ++
        [101#u8, 45#u8, 54#u8, 53#u8, 53#u8, 51#u8, 53#u8, 57#u8,
          120#u8] := by
    simp only [s, List.cons_append, List.nil_append]
    rw [hfractionValue, List.append_assoc]
    rfl
  have hslen : s.val.length = 9223372036854120460 := by
    simp only [s, List.length_cons, List.length_append, List.length_nil,
      hfractionLength]
  have hvalid : SourceValidByteSlice s := by
    unfold SourceValidByteSlice
    rw [hslen]
    norm_num
  have hsource : DecimalGoalVocabularyDefault.sourceString s := by
    change s.val.length <= 9223372036854775807 /\
      exists t : String,
        s.val.map (fun b : Std.U8 => b.val) =
          t.toUTF8.toList.map UInt8.toNat
    constructor
    · rw [hslen]
      norm_num
    · have htoData : forall bs : ByteArray,
          bs.toList = bs.data.toList := by
        intro bs
        have loop_eq : forall (i : Nat) (r : List UInt8), i <= bs.size ->
            ByteArray.toList.loop bs i r =
              r.reverse ++ bs.data.toList.drop i := by
          intro i r hi
          fun_induction ByteArray.toList.loop bs i r with
          | case1 i r h ih =>
              rw [ih (by omega)]
              rw [List.reverse_cons, List.append_assoc]
              have hd := List.drop_eq_getElem_cons
                (l := bs.data.toList) (i := i) (by simpa using h)
              rw [hd]
              simp only [List.cons_append]
              congr 2
              simp [ByteArray.get!, h]
              rfl
          | case2 i r h =>
              have hidrop : bs.data.toList.drop i = [] := by
                apply List.drop_eq_nil_of_le
                simpa using h
              simp [hidrop]
        unfold ByteArray.toList
        rw [loop_eq 0 [] (Nat.zero_le _)]
        simp
      have htoListAppend : forall a b : ByteArray,
          (a ++ b).toList = a.toList ++ b.toList := by
        intro a b
        rw [htoData, htoData, htoData]
        exact ByteArray.toList_data_append
      have hencPoint : String.utf8EncodeChar '.' = [46] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hencOne : String.utf8EncodeChar '1' = [49] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hencE : String.utf8EncodeChar 'e' = [101] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hencMinus : String.utf8EncodeChar '-' = [45] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hencSix : String.utf8EncodeChar '6' = [54] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hencFive : String.utf8EncodeChar '5' = [53] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hencThree : String.utf8EncodeChar '3' = [51] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hencNine : String.utf8EncodeChar '9' = [57] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hencX : String.utf8EncodeChar 'x' = [120] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hrep : forall n : Nat,
          (List.replicate n '1').utf8Encode.toList.map UInt8.toNat =
            List.replicate n 49 := by
        intro n
        induction n with
        | zero =>
            simp [List.utf8Encode_nil, ByteArray.toList_empty]
        | succ n ih =>
            rw [List.replicate_succ, List.utf8Encode_cons]
            rw [htoListAppend, List.map_append, ih]
            rw [htoData]
            rw [List.utf8Encode_singleton, hencOne,
              List.toList_data_toByteArray]
            rfl
      have hpoint :
          ['.'].utf8Encode.toList.map UInt8.toNat = [46] := by
        rw [htoData]
        simp [List.utf8Encode, hencPoint]
      have htail :
          ['e', '-', '6', '5', '5', '3', '5', '9', 'x'].utf8Encode.toList.map
              UInt8.toNat = [101, 45, 54, 53, 53, 51, 53, 57, 120] := by
        rw [htoData]
        simp [List.utf8Encode, hencE, hencMinus, hencSix, hencFive,
          hencThree, hencNine, hencX]
      have hascii : forall n : Nat,
          ([46#u8] ++ List.replicate n 49#u8 ++
            [101#u8, 45#u8, 54#u8, 53#u8, 53#u8, 51#u8, 53#u8,
              57#u8, 120#u8]).map (fun b : Std.U8 => b.val) =
          ((String.ofList
            (['.'] ++ List.replicate n '1' ++
              ['e', '-', '6', '5', '5', '3', '5', '9', 'x'])).toUTF8.toList.map
                UInt8.toNat) := by
        intro n
        simp only [String.toUTF8_eq_toByteArray, String.toByteArray_ofList,
          List.utf8Encode_append]
        rw [htoListAppend, htoListAppend]
        simp only [List.map_append, hpoint, hrep, htail,
          List.map_cons, List.map_nil, List.map_replicate]
        norm_num
      refine ⟨String.ofList
        (['.'] ++ List.replicate 9223372036854120450 '1' ++
          ['e', '-', '6', '5', '5', '3', '5', '9', 'x']), ?_⟩
      rw [hs]
      exact hascii 9223372036854120450
  have hne : s.val != [] := by
    change (46#u8 :: fractionBytes ++ [120#u8]) != []
    simp only [bne_iff_ne]
    intro h
    have q := congrArg List.length h
    simp at q
  have hbytes : DecimalGoalVocabularyDefault.bytes s =
      [46] ++ List.replicate 9223372036854120450 49 ++
        [101, 45, 54, 53, 53, 51, 53, 57, 120] := by
    change s.val.map (fun b : Std.U8 => b.val) = _
    rw [hs]
    have hmap : forall n : Nat,
        ([46#u8] ++ List.replicate n 49#u8 ++
          [101#u8, 45#u8, 54#u8, 53#u8, 53#u8, 51#u8, 53#u8,
            57#u8, 120#u8]).map (fun b : Std.U8 => b.val) =
        [46] ++ List.replicate n 49 ++
          [101, 45, 54, 53, 53, 51, 53, 57, 120] := by
      intro n
      simp only [List.map_append, List.map_cons, List.map_nil,
        List.map_replicate]
      norm_num
    exact hmap 9223372036854120450
  have hmem120 : 120 ∈ DecimalGoalVocabularyDefault.bytes s := by
    rw [hbytes]
    have hmem : forall n : Nat,
        120 ∈ [46] ++ List.replicate n 49 ++
          [101, 45, 54, 53, 53, 51, 53, 57, 120] := by
      intro n
      simp
    exact hmem 9223372036854120450
  have noNumeric : Not (exists x : DecimalLexeme,
      DecimalGoalVocabularyDefault.validNumeric x /\
      DecimalGoalVocabularyDefault.renderNumeric x =
        DecimalGoalVocabularyDefault.bytes s) := by
    rintro ⟨x, hxvalid, hxrender⟩
    have hrenderNo120 : 120 ∉
        DecimalGoalVocabularyDefault.renderNumeric x := by
      rcases x with ⟨leadingSign, integerDigits, fractionDigits, exponent⟩
      simp only [DecimalGoalVocabularyDefault] at hxvalid
      simp only [DecimalGoalVocabularyDefault]
      have hsign : 120 ∉
          (match leadingSign with
          | none => []
          | some negative => [if negative then 45 else 43]) := by
        cases leadingSign <;> simp
        split <;> simp
      have hint : 120 ∉ integerDigits.map (fun d => 48 + d) := by
        simp only [List.mem_map]
        rintro ⟨d, hd, heq⟩
        have hdlt := hxvalid.1 d hd
        omega
      have hfrac : 120 ∉
          (match fractionDigits with
          | none => []
          | some ds => 46 :: ds.map (fun d => 48 + d)) := by
        cases fractionDigits with
        | none => simp
        | some ds =>
            simp only [List.mem_cons, List.mem_map, not_or]
            constructor
            · norm_num
            rintro ⟨d, hd, heq⟩
            have hdlt := hxvalid.2.1 d hd
            omega
      have hexponent : 120 ∉
          (match exponent with
          | none => []
          | some (upper, sign, ds) =>
              (if upper then 69 else 101) ::
                (match sign with
                | none => []
                | some negative => [if negative then 45 else 43]) ++
                ds.map (fun d => 48 + d)) := by
        cases exponent with
        | none => simp
        | some p =>
            rcases p with ⟨upper, sign, ds⟩
            simp only [List.mem_cons, List.mem_append, List.mem_map,
              not_or]
            constructor
            · constructor
              · split <;> simp
              · cases sign <;> simp
                split <;> simp
            · rintro ⟨d, hd, heq⟩
              have hdlt := hxvalid.2.2.2.2 d hd
              omega
      simp only [List.mem_append, not_or]
      exact ⟨⟨⟨hsign, hint⟩, hfrac⟩, hexponent⟩
    exact hrenderNo120 (hxrender.symm ▸ hmem120)
  have noSpecial : Not (exists (sign : Option Bool) (nan : Bool),
      DecimalGoalVocabularyDefault.specialLiteral
        (DecimalGoalVocabularyDefault.bytes s) sign nan) := by
    rintro ⟨sign, nan, body, heq, hbody⟩
    have hfirst : (DecimalGoalVocabularyDefault.bytes s).head? = some 46 := by
      rw [hbytes]
      rfl
    cases sign with
    | some negative =>
        have heq' : ([if negative then 45 else 43] ++ body).head? =
            some 46 := by
          rw [← heq]
          exact hfirst
        simp only [List.head?_cons] at heq'
        split at heq' <;> simp at heq'
    | none =>
        simp only [List.nil_append] at heq
        have heq' : body.head? = some 46 := by
          rw [← heq]
          exact hfirst
        cases body with
        | nil => simp at heq'
        | cons b bs =>
            simp only [List.head?_cons, Option.some.injEq] at heq'
            subst b
            split at hbody
            · cases hbody with
              | cons hhead _ => omega
            · rcases hbody with hbody | hbody
              · cases hbody with
                | cons hhead _ => omega
              · cases hbody with
                | cons hhead _ => omega
  have hclassifies : DecimalGoalVocabularyDefault.classifies
      (DecimalGoalVocabularyDefault.bytes s) none := by
    simpa only [DecimalGoalVocabularyDefault] using
      And.intro noNumeric noSpecial
  have hcompact : s.val = 46#u8 :: fractionBytes ++ [120#u8] := by rfl
  have hparse : num.imp.dec2flt.parse.parse_number s =
      fail integerOverflow :=
    NumberHelperSharpTrailingParseFailure fractionBytes hfractionValue
      hfractionLength s hcompact
  have hpublic : num.imp.dec2flt.dec2flt
      F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64
      F64.Insts.CoreOpsArithDivF64F64
      F64.Insts.CoreOpsArithMulF64F64
      F64.Insts.CoreOpsArithNegF64 s = fail integerOverflow := by
    simp [num.imp.dec2flt.dec2flt, num.imp.dec2flt.dec2flt_with_trace,
      core.str.Str.as_bytes, core.slice.Slice.first, ModelStrAsBytes,
      ModelSliceFirst, hcompact, hparse]
  exact ⟨s, hs, hslen, hvalid, hsource, hne, noNumeric, noSpecial,
    hclassifies, hparse, hpublic⟩

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.Assumptions
import Tablet.DecimalGoalVocabularyDefault
import Tablet.SourceValidByteSlice
import Tablet.F64_Insts_CoreOpsArithDivF64F64
import Tablet.F64_Insts_CoreOpsArithMulF64F64
import Tablet.F64_Insts_CoreOpsArithNegF64
import Tablet.F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64
import Tablet.ModelSliceFirst
import Tablet.ModelStrAsBytes
import Tablet.NumberHelperSharpFractionBytes
import Tablet.NumberHelperSharpTrailingParseFailure
import Tablet.SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from
import Tablet.SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_read_u64
import Tablet.num_imp_dec2flt_common_is_8digits
import Tablet.num_imp_dec2flt_dec2flt
import Tablet.num_imp_dec2flt_dec2flt_with_trace
import Tablet.num_imp_dec2flt_parse_parse_8digits
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.num_imp_dec2flt_parse_try_parse_digits

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: InvalidOverflowBoundaryWitness]
theorem InvalidOverflowBoundaryWitness :
    exists s : Str,
      s.val = [46#u8] ++ List.replicate 9223372036854120450 49#u8 ++
        [101#u8, 45#u8, 54#u8, 53#u8, 53#u8, 51#u8, 53#u8, 57#u8,
          120#u8] /\
      s.val.length = 9223372036854120460 /\
      SourceValidByteSlice s /\
      DecimalGoalVocabularyDefault.sourceString s /\
      s.val != [] /\
      (Not (exists x : DecimalLexeme,
        DecimalGoalVocabularyDefault.validNumeric x /\
        DecimalGoalVocabularyDefault.renderNumeric x =
          DecimalGoalVocabularyDefault.bytes s)) /\
      (Not (exists (sign : Option Bool) (nan : Bool),
        DecimalGoalVocabularyDefault.specialLiteral
          (DecimalGoalVocabularyDefault.bytes s) sign nan)) /\
      DecimalGoalVocabularyDefault.classifies
        (DecimalGoalVocabularyDefault.bytes s) none /\
      num.imp.dec2flt.parse.parse_number s = fail integerOverflow /\
      num.imp.dec2flt.dec2flt
        F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64
        F64.Insts.CoreOpsArithDivF64F64
        F64.Insts.CoreOpsArithMulF64F64
        F64.Insts.CoreOpsArithNegF64 s = fail integerOverflow := by
-- BODY
  obtain ⟨fractionBytes, hfractionValue, hfractionLength⟩ :=
    NumberHelperSharpFractionBytes
  have hbound : (46#u8 :: fractionBytes ++ [120#u8]).length <= Usize.max := by
    simp only [List.length_cons, List.length_append, List.length_nil,
      hfractionLength]
    have h := Usize.max_succ_eq_pow
    rw [Dec2Flt.configured_x86_64_numBits] at h
    norm_num at h
    omega
  let s : Str := ⟨46#u8 :: fractionBytes ++ [120#u8], hbound⟩
  have hs : s.val = [46#u8] ++
      List.replicate 9223372036854120450 49#u8 ++
        [101#u8, 45#u8, 54#u8, 53#u8, 53#u8, 51#u8, 53#u8, 57#u8,
          120#u8] := by
    simp only [s, List.cons_append, List.nil_append]
    rw [hfractionValue, List.append_assoc]
    rfl
  have hslen : s.val.length = 9223372036854120460 := by
    simp only [s, List.length_cons, List.length_append, List.length_nil,
      hfractionLength]
  have hvalid : SourceValidByteSlice s := by
    unfold SourceValidByteSlice
    rw [hslen]
    norm_num
  have hsource : DecimalGoalVocabularyDefault.sourceString s := by
    change s.val.length <= 9223372036854775807 /\
      exists t : String,
        s.val.map (fun b : Std.U8 => b.val) =
          t.toUTF8.toList.map UInt8.toNat
    constructor
    · rw [hslen]
      norm_num
    · have htoData : forall bs : ByteArray,
          bs.toList = bs.data.toList := by
        intro bs
        have loop_eq : forall (i : Nat) (r : List UInt8), i <= bs.size ->
            ByteArray.toList.loop bs i r =
              r.reverse ++ bs.data.toList.drop i := by
          intro i r hi
          fun_induction ByteArray.toList.loop bs i r with
          | case1 i r h ih =>
              rw [ih (by omega)]
              rw [List.reverse_cons, List.append_assoc]
              have hd := List.drop_eq_getElem_cons
                (l := bs.data.toList) (i := i) (by simpa using h)
              rw [hd]
              simp only [List.cons_append]
              congr 2
              simp [ByteArray.get!, h]
              rfl
          | case2 i r h =>
              have hidrop : bs.data.toList.drop i = [] := by
                apply List.drop_eq_nil_of_le
                simpa using h
              simp [hidrop]
        unfold ByteArray.toList
        rw [loop_eq 0 [] (Nat.zero_le _)]
        simp
      have htoListAppend : forall a b : ByteArray,
          (a ++ b).toList = a.toList ++ b.toList := by
        intro a b
        rw [htoData, htoData, htoData]
        exact ByteArray.toList_data_append
      have hencPoint : String.utf8EncodeChar '.' = [46] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hencOne : String.utf8EncodeChar '1' = [49] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hencE : String.utf8EncodeChar 'e' = [101] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hencMinus : String.utf8EncodeChar '-' = [45] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hencSix : String.utf8EncodeChar '6' = [54] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hencFive : String.utf8EncodeChar '5' = [53] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hencThree : String.utf8EncodeChar '3' = [51] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hencNine : String.utf8EncodeChar '9' = [57] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hencX : String.utf8EncodeChar 'x' = [120] := by
        rw [String.utf8EncodeChar_eq_singleton (by decide)]
        rfl
      have hrep : forall n : Nat,
          (List.replicate n '1').utf8Encode.toList.map UInt8.toNat =
            List.replicate n 49 := by
        intro n
        induction n with
        | zero =>
            simp [List.utf8Encode_nil, ByteArray.toList_empty]
        | succ n ih =>
            rw [List.replicate_succ, List.utf8Encode_cons]
            rw [htoListAppend, List.map_append, ih]
            rw [htoData]
            rw [List.utf8Encode_singleton, hencOne,
              List.toList_data_toByteArray]
            rfl
      have hpoint :
          ['.'].utf8Encode.toList.map UInt8.toNat = [46] := by
        rw [htoData]
        simp [List.utf8Encode, hencPoint]
      have htail :
          ['e', '-', '6', '5', '5', '3', '5', '9', 'x'].utf8Encode.toList.map
              UInt8.toNat = [101, 45, 54, 53, 53, 51, 53, 57, 120] := by
        rw [htoData]
        simp [List.utf8Encode, hencE, hencMinus, hencSix, hencFive,
          hencThree, hencNine, hencX]
      have hascii : forall n : Nat,
          ([46#u8] ++ List.replicate n 49#u8 ++
            [101#u8, 45#u8, 54#u8, 53#u8, 53#u8, 51#u8, 53#u8,
              57#u8, 120#u8]).map (fun b : Std.U8 => b.val) =
          ((String.ofList
            (['.'] ++ List.replicate n '1' ++
              ['e', '-', '6', '5', '5', '3', '5', '9', 'x'])).toUTF8.toList.map
                UInt8.toNat) := by
        intro n
        simp only [String.toUTF8_eq_toByteArray, String.toByteArray_ofList,
          List.utf8Encode_append]
        rw [htoListAppend, htoListAppend]
        simp only [List.map_append, hpoint, hrep, htail,
          List.map_cons, List.map_nil, List.map_replicate]
        norm_num
      refine ⟨String.ofList
        (['.'] ++ List.replicate 9223372036854120450 '1' ++
          ['e', '-', '6', '5', '5', '3', '5', '9', 'x']), ?_⟩
      rw [hs]
      exact hascii 9223372036854120450
  have hne : s.val != [] := by
    change (46#u8 :: fractionBytes ++ [120#u8]) != []
    simp only [bne_iff_ne]
    intro h
    have q := congrArg List.length h
    simp at q
  have hbytes : DecimalGoalVocabularyDefault.bytes s =
      [46] ++ List.replicate 9223372036854120450 49 ++
        [101, 45, 54, 53, 53, 51, 53, 57, 120] := by
    change s.val.map (fun b : Std.U8 => b.val) = _
    rw [hs]
    have hmap : forall n : Nat,
        ([46#u8] ++ List.replicate n 49#u8 ++
          [101#u8, 45#u8, 54#u8, 53#u8, 53#u8, 51#u8, 53#u8,
            57#u8, 120#u8]).map (fun b : Std.U8 => b.val) =
        [46] ++ List.replicate n 49 ++
          [101, 45, 54, 53, 53, 51, 53, 57, 120] := by
      intro n
      simp only [List.map_append, List.map_cons, List.map_nil,
        List.map_replicate]
      norm_num
    exact hmap 9223372036854120450
  have hmem120 : 120 ∈ DecimalGoalVocabularyDefault.bytes s := by
    rw [hbytes]
    have hmem : forall n : Nat,
        120 ∈ [46] ++ List.replicate n 49 ++
          [101, 45, 54, 53, 53, 51, 53, 57, 120] := by
      intro n
      simp
    exact hmem 9223372036854120450
  have noNumeric : Not (exists x : DecimalLexeme,
      DecimalGoalVocabularyDefault.validNumeric x /\
      DecimalGoalVocabularyDefault.renderNumeric x =
        DecimalGoalVocabularyDefault.bytes s) := by
    rintro ⟨x, hxvalid, hxrender⟩
    have hrenderNo120 : 120 ∉
        DecimalGoalVocabularyDefault.renderNumeric x := by
      rcases x with ⟨leadingSign, integerDigits, fractionDigits, exponent⟩
      simp only [DecimalGoalVocabularyDefault] at hxvalid
      simp only [DecimalGoalVocabularyDefault]
      have hsign : 120 ∉
          (match leadingSign with
          | none => []
          | some negative => [if negative then 45 else 43]) := by
        cases leadingSign <;> simp
        split <;> simp
      have hint : 120 ∉ integerDigits.map (fun d => 48 + d) := by
        simp only [List.mem_map]
        rintro ⟨d, hd, heq⟩
        have hdlt := hxvalid.1 d hd
        omega
      have hfrac : 120 ∉
          (match fractionDigits with
          | none => []
          | some ds => 46 :: ds.map (fun d => 48 + d)) := by
        cases fractionDigits with
        | none => simp
        | some ds =>
            simp only [List.mem_cons, List.mem_map, not_or]
            constructor
            · norm_num
            rintro ⟨d, hd, heq⟩
            have hdlt := hxvalid.2.1 d hd
            omega
      have hexponent : 120 ∉
          (match exponent with
          | none => []
          | some (upper, sign, ds) =>
              (if upper then 69 else 101) ::
                (match sign with
                | none => []
                | some negative => [if negative then 45 else 43]) ++
                ds.map (fun d => 48 + d)) := by
        cases exponent with
        | none => simp
        | some p =>
            rcases p with ⟨upper, sign, ds⟩
            simp only [List.mem_cons, List.mem_append, List.mem_map,
              not_or]
            constructor
            · constructor
              · split <;> simp
              · cases sign <;> simp
                split <;> simp
            · rintro ⟨d, hd, heq⟩
              have hdlt := hxvalid.2.2.2.2 d hd
              omega
      simp only [List.mem_append, not_or]
      exact ⟨⟨⟨hsign, hint⟩, hfrac⟩, hexponent⟩
    exact hrenderNo120 (hxrender.symm ▸ hmem120)
  have noSpecial : Not (exists (sign : Option Bool) (nan : Bool),
      DecimalGoalVocabularyDefault.specialLiteral
        (DecimalGoalVocabularyDefault.bytes s) sign nan) := by
    rintro ⟨sign, nan, body, heq, hbody⟩
    have hfirst : (DecimalGoalVocabularyDefault.bytes s).head? = some 46 := by
      rw [hbytes]
      rfl
    cases sign with
    | some negative =>
        have heq' : ([if negative then 45 else 43] ++ body).head? =
            some 46 := by
          rw [← heq]
          exact hfirst
        simp only [List.head?_cons] at heq'
        split at heq' <;> simp at heq'
    | none =>
        simp only [List.nil_append] at heq
        have heq' : body.head? = some 46 := by
          rw [← heq]
          exact hfirst
        cases body with
        | nil => simp at heq'
        | cons b bs =>
            simp only [List.head?_cons, Option.some.injEq] at heq'
            subst b
            split at hbody
            · cases hbody with
              | cons hhead _ => omega
            · rcases hbody with hbody | hbody
              · cases hbody with
                | cons hhead _ => omega
              · cases hbody with
                | cons hhead _ => omega
  have hclassifies : DecimalGoalVocabularyDefault.classifies
      (DecimalGoalVocabularyDefault.bytes s) none := by
    simpa only [DecimalGoalVocabularyDefault] using
      And.intro noNumeric noSpecial
  have hcompact : s.val = 46#u8 :: fractionBytes ++ [120#u8] := by rfl
  have hparse : num.imp.dec2flt.parse.parse_number s =
      fail integerOverflow :=
    NumberHelperSharpTrailingParseFailure fractionBytes hfractionValue
      hfractionLength s hcompact
  have hpublic : num.imp.dec2flt.dec2flt
      F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64
      F64.Insts.CoreOpsArithDivF64F64
      F64.Insts.CoreOpsArithMulF64F64
      F64.Insts.CoreOpsArithNegF64 s = fail integerOverflow := by
    simp [num.imp.dec2flt.dec2flt, num.imp.dec2flt.dec2flt_with_trace,
      core.str.Str.as_bytes, core.slice.Slice.first, ModelStrAsBytes,
      ModelSliceFirst, hcompact, hparse]
  exact ⟨s, hs, hslen, hvalid, hsource, hne, noNumeric, noSpecial,
    hclassifies, hparse, hpublic⟩

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (201)
More program dependencies (159)
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 165 entries
Technical checking records
Local closure summary
{
  "node": "InvalidOverflowBoundaryWitness",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "08ec83d83f770c2ad0bac3c85d3304884f7b357d848543d3d7b6e88401378f05",
  "active_statement_hash": "da1cf9129b54a90c84285b79c9a51c3239616c7d9d1409ec95dcc3485995f285",
  "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-118",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalGoalVocabularyDefault": "94cb4c406f8efb78",
    "NumberHelperSharpFractionBytes": "7d84989e66ebc1cc",
    "NumberHelperSharpTrailingParseFailure": "c594c2c0e512dac5",
    "num_imp_dec2flt_dec2flt_with_trace": "faf0081b4c91301b",
    "num_imp_traits_Float_SIG_BITS_default": "9ab622d85ef04d94"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "c08dd4b4aea4abb4",
    "DecimalGoalVocabularyDefault": "b9386c8d0fd53e6e",
    "DecimalLexeme": "bf0c609978369054",
    "F64": "ec4e25fbaf4e11d4",
    "F64_Insts_CoreOpsArithDivF64F64": "dc44e13a99fc6bed",
    "F64_Insts_CoreOpsArithMulF64F64": "1ae1a9021a654abd",
    "F64_Insts_CoreOpsArithNegF64": "2d9be310c546ea2e",
    "F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64": "30963b4ef656cc79",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatExtU64": "60afe26c6a80cf0f",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64": "94095c9bf79da164",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SIG_TOTAL_BITS": "887b52d84a3fc9c4",
    "ModelSliceFirst": "a31394b3c1080f71",
    "ModelStrAsBytes": "024fe35b4dbfa9a3",
    "Preamble": "5575719dcb1443eb",
    "SourceValidByteSlice": "3dc42716a6378d26",
    "core_ops_arith_Div": "6e5df64b87169aa0",
    "core_ops_arith_Mul": "6e5df64b87169aa0",
    "core_ops_arith_Neg": "0ff07135a60bcbf9",
    "num_float_parse_ParseFloatError": "c43614ff2149ecf2",
    "num_imp_dec2flt_Dec2FltAssemblyTrace": "7bda5de02611ad76",
    "num_imp_dec2flt_Dec2FltTrace": "8fa376ebb5c489ee",
    "num_imp_dec2flt_Lemire": "1923f90c53d323e6",
    "num_imp_dec2flt_biased_fp_to_float": "acdde060e8d35ca7",
    "num_imp_dec2flt_common_BiasedFp": "31d81cf1cf22709f",
    "num_imp_dec2flt_common_BiasedFp_Insts_CoreCmpPartialEqBiasedFp_eq": "fbbf553e3c502a05",
    "num_imp_dec2flt_dec2flt": "f9f7d93449a25efa",
    "num_imp_dec2flt_dec2flt_with_trace": "ebe6a57c6dce0df6",
    "num_imp_dec2flt_decimal_Decimal": "4c41a56bf44cb31e",
    "num_imp_dec2flt_decimal_Decimal_try_fast_path": "2e4888f65a06b49f",
    "num_imp_dec2flt_lemire_compute_float": "39d89e3f0a8a9d4c",
    "num_imp_dec2flt_parse_parse_inf_nan": "85655c19a8791b54",
    "num_imp_dec2flt_parse_parse_number": "f73f8bc11faa6db3",
    "num_imp_dec2flt_pfe_empty": "89046d1da5ac1ce2",
    "num_imp_dec2flt_pfe_invalid": "21db875427d92c6d",
    "num_imp_dec2flt_slow_parse_long_mantissa": "cd8af64c3688b60f",
    "num_imp_traits_Float": "02acccb9db3e30f2",
    "num_imp_traits_FloatExt": "f996e5400649797d"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:881c62f2b1f905e86d10ab9ad40ba514e88cfc484078ad8c0a396292e3c4dddf",
  "current_fingerprint": "sound-v2:881c62f2b1f905e86d10ab9ad40ba514e88cfc484078ad8c0a396292e3c4dddf",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}