NumberHelperAcceptanceSafeLength

Lean checked

Statement

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

Theorem · Uniform safe-length numeric acceptance

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_number return normally with Some of a decimal record.

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.NumberHelperPartialAcceptanceSafeLength
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_MIN_19DIGIT_INT
import Tablet.num_imp_dec2flt_parse_adjust_many_digit_count
import Tablet.num_imp_dec2flt_parse_parse_8digits
import Tablet.num_imp_dec2flt_parse_parse_many_digits
import Tablet.num_imp_dec2flt_parse_parse_number
import Tablet.num_imp_dec2flt_parse_parse_partial_number
import Tablet.num_imp_dec2flt_parse_parse_scientific
import Tablet.num_imp_dec2flt_parse_try_parse_19digits
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: NumberHelperAcceptanceSafeLength]
theorem NumberHelperAcceptanceSafeLength :
    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_number s = ok (some d) := 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

Let M=2^{63}-1, let C=9223372036854120458=M-655349, and write L for the input length. The rendering equality and DecimalGoalVocabularyDefault identify the bytes, in order, as an unsigned valid numeric grammar instance. In particular there is at least one significand digit, a present exponent has at least one digit, and every digit byte is in 48,\ldots,57. The premise L\leq C<M is stronger than the length part of SourceValidByteSlice and, by Assumptions, all these lengths lie in both the 64-bit usize and nonnegative isize ranges.
First audit num imp dec2flt parse try parse digits. Its eight-byte loop reads only under the guard that at least eight bytes remain. Thus the range copy in SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice read u64 is in bounds and returns those eight bytes as one little-endian word; the continuing branch later drops exactly the same eight bytes.
We need the general, rather than merely example-specific, implication supplied by the bit test in num imp dec2flt common is 8digits. Write the little-endian bytes of its argument v as b_0,\ldots,b_7. Its constants are respectively eight copies of 0x46, eight copies of 0x30, and eight copies of 0x80. Let c_i be the carry into byte i of the wrapping addition by 0x4646464646464646, and let d_i be the borrow into byte i of the wrapping subtraction by 0x3030303030303030; initially c_0=d_0=0. Acceptance says that in every byte the high bit of both resulting words is zero, since their bitwise OR has zero intersection with 0x8080808080808080.
Induct first through the subtraction bytes. If d_i=0 and b_i<48, the result byte is b_i-48+256\in[208,255], contrary to its zero high bit. Therefore b_i\geq48, there is no outgoing borrow, and the result byte is b_i-48. Its zero high bit also gives b_i-48<128, hence b_i\leq175. This establishes 48\leq b_i\leq175 and d_{i+1}=0 at every byte, including across all lower-byte boundaries. Now induct through the addition bytes. If c_i=0 and b_i\geq58, the just-proved upper bound makes b_i+70\in[128,245], again contradicting the zero high bit. Hence b_i\leq57; then b_i+70\leq127, so there is no outgoing carry. Starting from c_0=0, this proves c_i=0 and 48\leq b_i\leq57 for all eight bytes. Thus the test has no false positive despite possible cross-byte carries and borrows.
It remains to audit every checked operation in num imp dec2flt parse parse 8digits. For an accepted word, its checked subtraction of 0x3030303030303030 has no borrow and produces a word v' whose bytes are all in 0,\ldots,9. In particular v'<\texttt{0x1000000000000000}=2^{60}. The checked multiplication by ten is below 10\cdot2^{60}<2^{64}, and the checked right shift by eight is defined because 8<64 and gives a value below 2^{52}. Their checked sum is therefore below 10\cdot2^{60}+2^{52}<2^{64}. The later right shifts by 16 and 32 are likewise defined because both counts are below 64; the intervening masks, multiplications, and addition are explicitly wrapping. Whatever wrapped word w<2^{64} those operations produce, its final shift w\mathbin{\mathtt{>>}}32 is at most 2^{32}-1. The checked cast to u32 therefore succeeds, and the final widening back to u64 is exact.
The outer accumulator multiplication and addition are explicitly wrapping. The scalar loop indexes only when its count is below the remaining length, uses wrapping byte subtraction and wrapping accumulator operations, casts only a byte to u64, and increments the count to a value at most that length. Its final slice begins at that bounded count. Hence both digit-scan invocations terminate normally at the first nondigit. By SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice offset from, their offsets cast the lengths of a slice and one of its suffixes to isize and take the checked difference. Both casts are exact at length at most L, and each difference is a nonnegative value at most L.
If a point is present, removing it is guarded by split_first; the second digit scan has the same safety argument. Its fractional count is at most L, its checked negation lies in the signed range, and the cast to i64 is exact because isize and i64 are both 64-bit. The checked addition of integer and fractional digit counts is the number of significand digits and is at most L. Validity makes that number positive, so the extracted zero-digit rejection is not taken.
For a present exponent, the marker case of num imp dec2flt parse parse partial number removes exactly the e or E byte. Let q\geq1 be the number of exponent digits. The rendering premise says that the saved suffix is an optional sign followed by exactly those q digit bytes, with no further byte. We now establish the normal result and full consumption directly, rather than obtaining either from the conditional statement of NumberHelperScientificExponentBounds.
Unfold num imp dec2flt parse parse scientific on this saved suffix. Its first split_first either removes the unique optional plus or minus sign, or observes the first digit and retains the original suffix. In every case the resulting slice consists of exactly the q digits. It is nonempty, so the index at zero is in bounds, and its first byte passes is_ascii_digit. Induct on j=0,\ldots,q in the scientific loop. At the start of iteration j, the count is j, the first j exponent digits have been visited, and the accumulator a lies in [0,655359]. If j<q, the guarded index reads exponent digit j and wrapping subtraction by 48 gives its value d\in[0,9]. When a<65536, the checked operations compute 10a+d\leq10\cdot65535+9=655359<2^{63}; when a\geq65536, no arithmetic update is made. The checked count increment gives j+1\leq q\leq L<C<2^{63}, so it also lies in the configured usize range. Thus every one of the q digit iterations succeeds, including iterations after the accumulator has stopped changing. At count q the length guard is false. Slicing the digit-only suffix at the in-bounds index q leaves the empty slice. Finally, the optional checked negation is safe on [0,655359]. Hence parse_scientific returns normally with Some of a value E and an empty remainder, and the option-branch in the partial parser takes its Continue case.
Only now apply the first, conditional clause of NumberHelperScientificExponentBounds to that established return; it gives |E|\leq655359. We must still check the addition to the point exponent. For q\leq5, the just-described decimal accumulation gives |E|\leq10^q-1; for q\geq6, the guarded loop gives |E|\leq655359. If E\geq0, then -F+E lies between -F\geq-L>-2^{63} and E\leq655359, so both sides are safe. If E<0 and F=0, the sum is at least -655359. Finally suppose E<0 and F>0. A point is present and the exponent spelling contains the marker, its mandatory minus sign, and the q digits, so F\leq L-(q+3). Therefore
F+|E|\leq \begin{cases} L-(q+3)+(10^q-1),&q\leq5,\\ L-(q+3)+655359,&q\geq6. \end{cases}
The first expression is maximized at q=5 and is L+99991; the second is maximized at q=6 and is L+655350. Since C+655350=M+1=2^{63}, the sum -F+E is never below -2^{63}. Its positive side is at most 655359<M. Thus the checked i64 addition succeeds, including equality at i64::MIN. The offset of the remaining suffix from the original input is again between zero and L, and its cast through isize to usize succeeds.
If there are at most nineteen significand digits, all remaining checked operations have now been discharged and the extracted function returns its record. Otherwise subtracting nineteen from the positive digit count is safe. In num imp dec2flt parse adjust many digit count, each access is guarded by the scan index being below L, and each increment is at most L. The constant subtraction 48-1, saturating byte subtraction, and byte cast are safe. A point decrements the count by zero and a leading zero by one. If there are D significand digits, at most D such zeros can be removed from D-19, so every intermediate adjusted count is at least -19; there is no signed underflow.
It remains to audit the positive adjusted-count retry in num imp dec2flt parse parse many digits. Write D for the total number of significand digits and z for the number of its leading zero digits when the optional point is ignored (take z=D when every significand digit is zero). Starting from D-19, the scan in num imp dec2flt parse adjust many digit count visits precisely these z leading zero bytes and, if it occurs among them, the point byte: saturation makes the point decrement zero, while each zero decrement is one. It then stops at the first nonzero significand digit, or at the exponent marker or end when all D digits are zero. Thus its result is exactly D-19-z. All its intermediate values are at least -19, as noted above. The retry premise n_{\rm digits3}>0 therefore implies D-z\geq20 and in particular that a nonzero significand digit exists.
By num imp dec2flt parse MIN 19DIGIT INT, the retry guard constant is exactly 1000000000000000000=10^{18}. The loop num imp dec2flt parse try parse 19digits multiplies only while its accumulator is below this constant. Thus each checked update is at most 10\cdot(10^{18}-1)+9=10^{19}-1<2^{64}; byte-to-u64 casts and guarded splits are also safe. If the threshold is reached before a point, the retry base exponent is the nonnegative number of remaining integer digits. With no explicit exponent it is at most L-19. With q exponent digits it is at most L-q-20, since the spelling also contains the marker. Adding a nonnegative E is therefore at most L+99974 for q\leq5, and at most L+655333 for q\geq6; the latter maximum occurs at q=6. At L\leq C this is at most M-16, so the cast and checked addition succeed. Adding a negative E only lowers a nonnegative base by at most 655359 and cannot underflow.
It remains to justify the extracted one-byte slice in the other retry branch. Suppose the first retry scan returns an accumulator below the cited 10^{18} constant. It cannot have stopped at its threshold guard, so by the numeric grammar it stopped at the first nondigit, or at the end. If the spelling had no point, it would have consumed all D significand digits before reaching an exponent marker or the end. But D-z\geq20 says that, from the first nonzero digit onward, there are at least twenty digits. After the first nineteen of them, the exact recurrence a\mapsto10a+d has a nineteen-digit value and hence is at least 10^{18}, exactly the cited guard constant. The next guard must therefore force the threshold exit, contradicting the assumed low returned accumulator. A point must therefore be present. The initial full digit scan saved s1 (the int_end argument) as the suffix beginning with that point. The retry scan starts at the original slice, classifies the same integer digit prefix, and, since it did not reach the threshold, stops exactly at that same point. Its returned slice is consequently equal to int_end, is nonempty, and has point byte first. The extracted slice at index one is therefore in-bounds and is exactly the suffix immediately after the point.
The second scan starts on that fractional suffix. It may pass arbitrarily many leading fractional zeros before its accumulator reaches the threshold, but it cannot consume more than the full fractional count F. Its offset is therefore in [0,F], and its negated retry base is in [-F,0]. Adding E is safe by cases, without asserting a false uniform absolute-value bound. If E\geq0, the sum is at least -F\geq-L>-2^{63}, so it cannot underflow, and its positive side is at most E\leq655359. If E<0, its spelling contains the exponent marker, the mandatory minus sign, and its q digits in addition to the point and F fractional digits. Hence F\leq L-(q+3), and the same accumulator estimates give
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}
Thus the negative side is at least -(L+655350)\geq-(C+655350)=-2^{63}, including the allowed endpoint. This checks both retry branches, every offset, the offset negation, the isize-to-i64 cast, and the final checked addition.
Consequently num imp dec2flt parse parse partial number returns normally with Some of a record and with its consumed-length field equal to L: the rendering premise says the valid numeric spelling occupies the whole input, so its final suffix is empty. The length comparison in num imp dec2flt parse parse number therefore succeeds and that function returns the same record in Some, as required.

Lean proof


  intro s x hsource hne hsign hvalid hbytes hlen
  obtain ⟨d, hd⟩ := NumberHelperPartialAcceptanceSafeLength s x hsource hne
    hsign hvalid hbytes hlen
  refine ⟨d, ?_⟩
  simp [num.imp.dec2flt.parse.parse_number, hd]

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.Assumptions
import Tablet.DecimalGoalVocabularyDefault
import Tablet.NumberHelperPartialAcceptanceSafeLength
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_MIN_19DIGIT_INT
import Tablet.num_imp_dec2flt_parse_adjust_many_digit_count
import Tablet.num_imp_dec2flt_parse_parse_8digits
import Tablet.num_imp_dec2flt_parse_parse_many_digits
import Tablet.num_imp_dec2flt_parse_parse_number
import Tablet.num_imp_dec2flt_parse_parse_partial_number
import Tablet.num_imp_dec2flt_parse_parse_scientific
import Tablet.num_imp_dec2flt_parse_try_parse_19digits
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: NumberHelperAcceptanceSafeLength]
theorem NumberHelperAcceptanceSafeLength :
    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_number s = ok (some d) := by
-- BODY
  intro s x hsource hne hsign hvalid hbytes hlen
  obtain ⟨d, hd⟩ := NumberHelperPartialAcceptanceSafeLength s x hsource hne
    hsign hvalid hbytes hlen
  refine ⟨d, ?_⟩
  simp [num.imp.dec2flt.parse.parse_number, hd]

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 68 entries
Technical checking records
Local closure summary
{
  "node": "NumberHelperAcceptanceSafeLength",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "37ce1c73397564da8ffda50bef17f1683df26ee4d6a1934836ea5c96fffa44fd",
  "active_statement_hash": "f10e30fc768033e84fdcaa72d4abb65f95ffa76fe2c15839182c3a8745b8901a",
  "kernel_axioms": [
    "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-109",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "NumberHelperPartialAcceptanceSafeLength": "8932d60be6df1417"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "4da7f295a9b85b50",
    "DecimalGoalVocabularyDefault": "512f8d0aea894250",
    "DecimalLexeme": "c43614ff2149ecf2",
    "SourceValidByteSlice": "3dc42716a6378d26",
    "num_imp_dec2flt_decimal_Decimal": "c43614ff2149ecf2",
    "num_imp_dec2flt_parse_parse_number": "b8693ec58f01c43c",
    "num_imp_dec2flt_parse_parse_partial_number": "94ed7b2ff71604ef"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:7b67986355e19604b5dcd2bde32859af164ca9ce9312e667dd670c8cdecd6384",
  "current_fingerprint": "sound-v2:7b67986355e19604b5dcd2bde32859af164ca9ce9312e667dd670c8cdecd6384",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}