NumberHelperSafeLengthSharpness

Lean checked

Statement

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

Lemma · Sharpness of the uniform safe-length bound

There are a source-valid unsigned numeric slice s and its valid exactly rendered lexeme x of length 9223372036854120459, one more than the uniform safe bound, for which the extracted parse_number returns fail integerOverflow. Concretely, its bytes are a point, then 9223372036854120450 copies of ASCII 1, then e-655359.

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.NumberHelperAcceptanceSafeLength
import Tablet.NumberHelperSharpFractionBytes
import Tablet.NumberHelperSharpParseFailure
import Tablet.NumberHelperScientificExponentBounds
import Tablet.SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from
import Tablet.SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_read_u64
import Tablet.SourceValidByteSlice
import Tablet.num_imp_dec2flt_common_is_8digits
import Tablet.num_imp_dec2flt_parse_parse_8digits
import Tablet.num_imp_dec2flt_parse_parse_8digits_MASK
import Tablet.num_imp_dec2flt_parse_parse_8digits_MUL1
import Tablet.num_imp_dec2flt_parse_parse_8digits_MUL2
import Tablet.num_imp_dec2flt_parse_parse_number
import Tablet.num_imp_dec2flt_parse_parse_partial_number
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: NumberHelperSafeLengthSharpness]
theorem NumberHelperSafeLengthSharpness :
    exists (s : Slice Std.U8) (x : DecimalLexeme),
      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] /\
      s.val.length = 9223372036854120459 /\
      x = {
        leadingSign := none
        integerDigits := []
        fractionDigits := some (List.replicate 9223372036854120450 1)
        exponent := some (false, some true, [6, 5, 5, 3, 5, 9])
      } /\
      SourceValidByteSlice s /\ s.val != [] /\ x.leadingSign = none /\
      DecimalGoalVocabularyDefault.validNumeric x /\
      DecimalGoalVocabularyDefault.bytes s =
        DecimalGoalVocabularyDefault.renderNumeric x /\
      num.imp.dec2flt.parse.parse_number 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
C=9223372036854120458,\qquad K=9223372036854120450,\qquad q=1152921504606765056.
Thus K=C-8=8q+2. By Assumptions, the list
[46]\mathbin{++}\operatorname{replicate}(K,49)\mathbin{++} [101,45,54,53,53,51,53,57]
constructs an Aeneas slice: its length is 1+K+8=C+1<2^{63}-1<2^{64}-1. Take it for s. Its displayed length also proves SourceValidByteSlice and nonemptiness. Write
E=[101,45,54,53,53,51,53,57]
for the eight bytes of e-655359. Take x with no leading sign, no integer digits, K fractional digits all equal to one, and negative exponent digits [6,5,5,3,5,9]. Under DecimalGoalVocabularyDefault, all digits are below ten; the fraction and exponent lists are nonempty; and rendering emits exactly the point, the K one bytes, and e-655359. Thus all lexical premises in the statement hold.
We next execute the initial digit scan in num imp dec2flt parse try parse digits. Its input begins with the eight bytes [46,49,49,49,49,49,49,49]. The length guard is true, so the subslice in SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice read u64 is in bounds and that definition copies exactly those bytes and reads the little-endian word 0x313131313131312e. In num imp dec2flt common is 8digits, adding the fixed 0x4646464646464646, subtracting 0x3030303030303030, ORing, and masking by 0x8080808080808080 gives 0x80, not zero. The bulk loop therefore returns without dropping a byte. The scalar loop reads the point at its guarded index zero; wrapping subtraction of 48 from 46 is 254, which is not below 10, so its count remains zero. Slicing at zero returns the complete input. Finally, SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice offset from casts the two equal lengths and subtracts them, so the integer-digit count is exactly zero. The following guarded split removes the point and presents
T_0=\operatorname{replicate}(K,49)\mathbin{++}E
to the fractional digit scan.
We prove the exact behavior of that scan's 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, the accumulator is some u64 value, and every operation so far has returned normally. The base case is the displayed T_0; the initial accumulator is the normally returned accumulator from the integer scan.
For the step, assume r<q. Since
K-8r=8(q-r)+2\ge10,
the first eight bytes of T_r are all 49, and in particular |T_r|\ge18. Hence the bulk-loop length guard justifies both the eight-byte range read in SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice read u64 and the later drop of exactly eight bytes. The read returns the little-endian word
v=\texttt{0x3131313131313131}.
For the test in num imp dec2flt common is 8digits, its wrapping sum and difference are respectively 0x7777777777777777 and 0x0101010101010101; their OR has zero intersection with 0x8080808080808080. Thus the test returns true.
It remains to check the non-wrapping arithmetic of num imp dec2flt parse parse 8digits, rather than merely using the truth of the test. 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, still below 2^{64}. The remaining masked multiplications and addition in that definition are explicitly wrapping. By num imp dec2flt parse parse 8digits MASK, the mask is 1095216660735=\texttt{0x000000ff000000ff}. Thus masking the checked sum, and masking that sum after its right shift by 16, both give 0x0000000b0000000b. By num imp dec2flt parse parse 8digits MUL1, the first multiplier is 4294967296000100=\texttt{0x000f424000000064}, so its wrapping product with that masked word is 0x00a7dd0c0000044c. By num imp dec2flt parse parse 8digits MUL2, the second multiplier is 42949672960001=\texttt{0x0000271000000001}, so its wrapping product with the second masked word is 0x0001adbb0000000b. Their wrapping sum is 0x00a98ac700000457. All three right shifts use the fixed in-range amounts 8, 16, and 32. The last shift is therefore 0x0000000000a98ac7=11111111<2^{32}$, so the u32 cast succeeds and the helper returns 11111111. The enclosing accumulator multiplication by 100000000 and addition are also explicitly wrapping. Dropping the first eight bytes of T_r therefore returns normally with precisely
\operatorname{replicate}(K-8(r+1),49)\mathbin{++}E=T_{r+1}.
This proves the induction step, including the read, digit test, decimal parse, wrapping accumulator update, and slice operation of every one of the q continuing iterations.
At the endpoint, K-8q=2, so the exact residual is the ten-byte slice
T_q=[49,49,101,45,54,53,53,51,53,57].
Its length guard is still true. The guarded eight-byte read returns the little-endian word 0x333535362d653131, corresponding to [49,49,101,45,54,53,53,51]. In the cited eight-digit test, the same fixed wrapping sum and difference are 0x797b7b7c73ab7777 and 0x03050505fd350101; after OR and masking the result is 0x0000000080800000, which is nonzero. Thus the bulk loop rejects this mixed block, does not call parse_8digits, does not drop any byte, and hands exactly T_q to the scalar loop.
That loop starts with count zero. At counts zero and one, its guarded index reads 49, wrapping subtraction by 48 gives the digit 1, the accumulator update is wrapping, and the checked count increments give respectively one and two, both at most the ten-byte slice length. At count two, the guarded index reads 101; wrapping subtraction gives 53, not below ten, so the loop stops without a third increment. Its final slice at the in-bounds index two is exactly E. We have therefore accounted for 8q+2=K fractional digits and identified every residual suffix used by either loop.
The fractional call to SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice offset from now casts the original fractional-scan length K+8=C and the residual length 8 to isize, then computes their checked difference K. Both casts are exact because C<2^{63}-1, and the subtraction is nonnegative and in range. Consequently its checked negation is -K, its cast to i64 is exact, and adding the zero integer-digit count to K gives the positive total significand count K without overflow.
Splitting the exponent marker passes the suffix -655359 to the scientific parser. The concrete negative clause of NumberHelperScientificExponentBounds returns -655359 and an empty suffix. More explicitly, the guarded split of E in num imp dec2flt parse parse partial number reads 101, selects its e branch, and passes [45,54,53,53,51,53,57] to that cited scientific result. The option branch therefore continues with value -655359. The next operation in num imp dec2flt parse parse partial number is the checked addition of the point exponent and this explicit exponent. Its mathematical result is
-K-655359=-(9223372036854120450+655359) =-9223372036854775809=-2^{63}-1.
This is one below i64::MIN; hence the extracted signed addition returns fail integerOverflow. Monadic binding propagates the failure out of the partial parser and then, through num imp dec2flt parse parse number, out of parse_number. This failure occurs before any many-digit retry or consumed-length calculation can be reached.
Finally, NumberHelperAcceptanceSafeLength establishes independently that every source-valid, nonempty, exactly rendered unsigned valid numeric slice of length at most C makes parse_number return normally with Some. The witness just executed has length exactly C+1 and instead returns fail integerOverflow. Hence the uniform safe length C is sharp; this conclusion uses the cited uniform theorem, not an analogy with one boundary arithmetic calculation.

Lean proof


  obtain ⟨fractionBytes, hfractionValue, hfractionLength⟩ :=
    NumberHelperSharpFractionBytes
  have hBound : 9223372036854120459 <= Usize.max := by
    have h := Usize.max_succ_eq_pow
    rw [Dec2Flt.configured_x86_64_numBits] at h
    norm_num at h
    omega
  let s : Slice Std.U8 :=
    ⟨46#u8 :: fractionBytes, by
      simp only [List.length_cons, hfractionLength]
      exact hBound⟩
  let x : DecimalLexeme := {
    leadingSign := none
    integerDigits := []
    fractionDigits := some (List.replicate 9223372036854120450 1)
    exponent := some (false, some true, [6, 5, 5, 3, 5, 9])
  }
  have hsTail : s.val = 46#u8 :: fractionBytes := by
    rfl
  have hsVal : 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] := by
    rw [hsTail, hfractionValue]
    simp only [List.cons_append, List.nil_append]
  have hsLen : s.val.length = 9223372036854120459 := by
    rw [hsTail, List.length_cons, hfractionLength]
  have hsource : SourceValidByteSlice s := by
    unfold SourceValidByteSlice
    rw [hsLen]
    norm_num
  have hnonempty : s.val != [] := by
    rw [hsTail]
    simp only [bne_iff_ne]
    exact List.cons_ne_nil _ _
  have hvalid : DecimalGoalVocabularyDefault.validNumeric x := by
    dsimp only [DecimalGoalVocabularyDefault, x]
    simp only [List.not_mem_nil, false_implies, implies_true,
      List.mem_replicate, and_imp, true_and]
    constructor
    · intro d _ hd
      omega
    constructor
    · right
      intro h
      have q := congrArg List.length h
      simp only [List.length_replicate, List.length_nil] at q
      omega
    · constructor
      · exact List.cons_ne_nil _ _
      · norm_num
  have hrenders : DecimalGoalVocabularyDefault.bytes s =
      DecimalGoalVocabularyDefault.renderNumeric x := by
    change s.val.map (fun b : Std.U8 => b.val) =
      [46] ++ (List.replicate 9223372036854120450 1).map
        (fun d => 48 + d) ++ [101, 45, 54, 53, 53, 51, 53, 57]
    rw [hsVal]
    simp only [List.map_append, List.map_cons, List.map_nil,
      List.map_replicate]
    norm_num
  have hparse : num.imp.dec2flt.parse.parse_number s =
      fail integerOverflow := NumberHelperSharpParseFailure s hsVal
  exact ⟨s, x, hsVal, hsLen, rfl, hsource, hnonempty, rfl, hvalid,
    hrenders, hparse⟩

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.Assumptions
import Tablet.DecimalGoalVocabularyDefault
import Tablet.NumberHelperAcceptanceSafeLength
import Tablet.NumberHelperSharpFractionBytes
import Tablet.NumberHelperSharpParseFailure
import Tablet.NumberHelperScientificExponentBounds
import Tablet.SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from
import Tablet.SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_read_u64
import Tablet.SourceValidByteSlice
import Tablet.num_imp_dec2flt_common_is_8digits
import Tablet.num_imp_dec2flt_parse_parse_8digits
import Tablet.num_imp_dec2flt_parse_parse_8digits_MASK
import Tablet.num_imp_dec2flt_parse_parse_8digits_MUL1
import Tablet.num_imp_dec2flt_parse_parse_8digits_MUL2
import Tablet.num_imp_dec2flt_parse_parse_number
import Tablet.num_imp_dec2flt_parse_parse_partial_number
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: NumberHelperSafeLengthSharpness]
theorem NumberHelperSafeLengthSharpness :
    exists (s : Slice Std.U8) (x : DecimalLexeme),
      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] /\
      s.val.length = 9223372036854120459 /\
      x = {
        leadingSign := none
        integerDigits := []
        fractionDigits := some (List.replicate 9223372036854120450 1)
        exponent := some (false, some true, [6, 5, 5, 3, 5, 9])
      } /\
      SourceValidByteSlice s /\ s.val != [] /\ x.leadingSign = none /\
      DecimalGoalVocabularyDefault.validNumeric x /\
      DecimalGoalVocabularyDefault.bytes s =
        DecimalGoalVocabularyDefault.renderNumeric x /\
      num.imp.dec2flt.parse.parse_number s = fail integerOverflow := by
-- BODY
  obtain ⟨fractionBytes, hfractionValue, hfractionLength⟩ :=
    NumberHelperSharpFractionBytes
  have hBound : 9223372036854120459 <= Usize.max := by
    have h := Usize.max_succ_eq_pow
    rw [Dec2Flt.configured_x86_64_numBits] at h
    norm_num at h
    omega
  let s : Slice Std.U8 :=
    ⟨46#u8 :: fractionBytes, by
      simp only [List.length_cons, hfractionLength]
      exact hBound⟩
  let x : DecimalLexeme := {
    leadingSign := none
    integerDigits := []
    fractionDigits := some (List.replicate 9223372036854120450 1)
    exponent := some (false, some true, [6, 5, 5, 3, 5, 9])
  }
  have hsTail : s.val = 46#u8 :: fractionBytes := by
    rfl
  have hsVal : 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] := by
    rw [hsTail, hfractionValue]
    simp only [List.cons_append, List.nil_append]
  have hsLen : s.val.length = 9223372036854120459 := by
    rw [hsTail, List.length_cons, hfractionLength]
  have hsource : SourceValidByteSlice s := by
    unfold SourceValidByteSlice
    rw [hsLen]
    norm_num
  have hnonempty : s.val != [] := by
    rw [hsTail]
    simp only [bne_iff_ne]
    exact List.cons_ne_nil _ _
  have hvalid : DecimalGoalVocabularyDefault.validNumeric x := by
    dsimp only [DecimalGoalVocabularyDefault, x]
    simp only [List.not_mem_nil, false_implies, implies_true,
      List.mem_replicate, and_imp, true_and]
    constructor
    · intro d _ hd
      omega
    constructor
    · right
      intro h
      have q := congrArg List.length h
      simp only [List.length_replicate, List.length_nil] at q
      omega
    · constructor
      · exact List.cons_ne_nil _ _
      · norm_num
  have hrenders : DecimalGoalVocabularyDefault.bytes s =
      DecimalGoalVocabularyDefault.renderNumeric x := by
    change s.val.map (fun b : Std.U8 => b.val) =
      [46] ++ (List.replicate 9223372036854120450 1).map
        (fun d => 48 + d) ++ [101, 45, 54, 53, 53, 51, 53, 57]
    rw [hsVal]
    simp only [List.map_append, List.map_cons, List.map_nil,
      List.map_replicate]
    norm_num
  have hparse : num.imp.dec2flt.parse.parse_number s =
      fail integerOverflow := NumberHelperSharpParseFailure s hsVal
  exact ⟨s, x, hsVal, hsLen, rfl, hsource, hnonempty, rfl, hvalid,
    hrenders, hparse⟩

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (69)
More program dependencies (36)
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 65 entries
Technical checking records
Local closure summary
{
  "node": "NumberHelperSafeLengthSharpness",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "f9dfc526e402d38c8eb0528b5b7f8ebaec7eb116f3ca05ab4be70019d68b1fd6",
  "active_statement_hash": "2b78e2a510bf5f55825e51b96b5f7cc11175ec519e4ef163ef727e899f35a654",
  "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-111",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "NumberHelperSharpFractionBytes": "7d84989e66ebc1cc",
    "NumberHelperSharpParseFailure": "b1e0d18782ad6037"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "65227461bf3cac06",
    "DecimalGoalVocabularyDefault": "512f8d0aea894250",
    "DecimalLexeme": "bf0c609978369054",
    "SourceValidByteSlice": "3dc42716a6378d26",
    "num_imp_dec2flt_decimal_Decimal": "c43614ff2149ecf2",
    "num_imp_dec2flt_parse_parse_number": "f73f8bc11faa6db3"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:d4f3f9094e8a0d84df0f8bebf7c531c56d07cc7fe8a9b4ec44f668659e5f499c",
  "current_fingerprint": "sound-v2:d4f3f9094e8a0d84df0f8bebf7c531c56d07cc7fe8a9b4ec44f668659e5f499c",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}