SlowAccumulatedRounding__Refutation

Lean checked

Statement

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

Theorem

The following result is false: For every Rust byte slice that is an unsigned numeric literal, every normal result of parse_long_mantissa is a raw pair whose unrestricted assembled word is the nonnegative binary64 rounding target of the full-significand accumulated-exponent value.

Lean statement

Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.

import Tablet.DecimalGoalVocabularyDefault
import Tablet.SourceValidByteSlice
import Tablet.F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64
import Tablet.F64_Insts_CoreOpsArithDivF64F64
import Tablet.F64_Insts_CoreOpsArithMulF64F64
import Tablet.F64_Insts_CoreOpsArithNegF64
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatExtU64
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_BITS
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SIG_TOTAL_BITS
import Tablet.ModelF64MantissaDigits
import Tablet.num_imp_dec2flt_dec2flt
import Tablet.num_imp_dec2flt_dec2flt_with_trace
import Tablet.num_imp_dec2flt_parse_parse_number
import Tablet.num_imp_dec2flt_parse_parse_inf_nan
import Tablet.num_imp_dec2flt_decimal_Decimal_try_fast_path
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW
import Tablet.num_imp_dec2flt_lemire_compute_float
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa
import Tablet.num_imp_dec2flt_biased_fp_to_float
import Tablet.num_imp_traits_Float_EXP_BITS_default
import Tablet.num_imp_traits_Float_EXP_SAT_default
import Tablet.num_imp_traits_Float_INFINITE_POWER_default
import Tablet.num_imp_traits_Float_SIG_BITS_default
import Tablet.SlowAccumulatedRoundingWitness

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: SlowAccumulatedRounding__Refutation]
theorem SlowAccumulatedRounding__Refutation : ¬ (∀ (s : Slice Std.U8) (x : DecimalLexeme) fp, SourceValidByteSlice s -> x.leadingSign = none ->
      DecimalGoalVocabularyDefault.validNumeric x -> DecimalGoalVocabularyDefault.bytes s = DecimalGoalVocabularyDefault.renderNumeric x ->
      num.imp.dec2flt.slow.parse_long_mantissa
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 s = ok fp ->
      ∃ target : F64, DecimalGoalVocabularyDefault.assembledWord fp target ∧
        DecimalGoalVocabularyDefault.roundsMagnitude (DecimalGoalVocabularyDefault.accumulatedMagnitude x) target) := 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

Suppose, for contradiction, that the universal statement being negated is true. Put
N=2^{31}=2147483648, \qquad s.\mathit{val}=\operatorname{replicate}(N,49\#\mathrm{u8}),
and let x be the decimal lexeme with no leading sign, integer-digit list \operatorname{replicate}(N,1), no decimal point, and no exponent. We first verify all four premises with which the supposed universal statement will be instantiated.
The list underlying s has length N. It is a legitimate Aeneas Slice U8, since N<2^{64}, and it satisfies SourceValidByteSlice, since N=2^{31}\leq2^{63}-1=9223372036854775807. The concrete meanings in DecimalGoalVocabularyDefault make x valid: every integer digit is 1<10, its integer list is nonempty because N>0, and the absent fraction and exponent impose no further digit obligations. The leading-sign field is definitionally none. Finally, mapping the digit renderer d\mapsto48+d over N copies of 1 gives N copies of 49; the absent sign, fraction, and exponent render as empty lists. Mapping unsigned-byte value over s.\mathit{val} also gives N copies of 49. The standard length and map equations for List.replicate therefore prove
\operatorname{bytes}(s)=\operatorname{renderNumeric}(x).
This is a symbolic list calculation and does not expand the N-element list.
The default decimal sequence has count and decimal point zero, false truncated, and an array of 768 zero bytes. The initial leading-zero loop is nonempty, reads the in-bounds zeroth byte 49, finds it unequal to 48, and returns the original slice without an iteration. For the following parse_digits_into_seq loop, define D_k, for 0\leq k\leq N, to be the record with
\begin{split} D_k.\mathit{num\_digits}&=k,\\ D_k.\mathit{decimal\_point}&=0,\\ D_k.\mathit{truncated}&=\mathrm{false},\\ D_k.\mathit{digits}[j]&= \begin{cases}1,&j<\min(k,768),\\0,&\min(k,768)\leq j<768. \end{cases} \end{split}
The loop invariant is that its state is (D_k,k) and the unconsumed suffix s[k\mathinner{..}] is \operatorname{replicate}(N-k,49\#\mathrm{u8}). It holds at k=0 by the default-record equation and the replicate/drop equations.
For the induction step, assume k<N. The length guard is true, and the bounds-checked access at k succeeds because k<N=\operatorname{len}(s); the replicate-index equation makes the returned byte 49. Both occurrences of wrapping subtraction compute 49-48=1, and 1<10, so both lifted pure computations return normally and the digit branch is taken. If k<768, try_add_digit performs its bounds-checked array update at k; this is in range and changes precisely that zero entry to one. If 768\leq k, its guard is false and it leaves the already-all-one array unchanged. In both cases the checked usize addition changes the record count from k to k+1. The loop's separate checked count addition does the same. Neither addition can overflow, because k+1\leq N=2^{31}<2^{64}. Thus the body returns ok (cont (D_{k+1},k+1)), and dropping the consumed byte changes the symbolic suffix from N-k copies to N-(k+1) copies. Unfolding Aeneas.Std.loop.eq_def once and applying the induction hypothesis proves the invariant step. Induction on the natural number N-k proves termination without reducing the enormous list. At k=N the length guard is false, so the body returns done; the subsequent range-from slice at start N is in bounds and returns the empty suffix. Consequently parse_digits_into_seq returns normally with empty remainder and D_N. In particular, its machine count is exactly N, while all 768 stored array entries are one.
The empty remainder makes the first is_empty branch of parse_decimal_seq true. Since N\ne0, it computes the consumed length as the checked usize subtraction N-0=N. The count_trailing_zeros loop starts at (\mathit{zeros},i,\mathit{scanning})=(0,N,\mathrm{true}). Because N>0, the checked subtraction N-1 succeeds and the bounds-checked access at that index succeeds; the replicate-index equation again returns 49. This byte is neither 48 nor 46, so the body returns (0,N,\mathrm{false}). One more use of Aeneas.Std.loop.eq_def sees the false scanning flag and returns ok 0. Thus the number of trailing zero digits is exactly zero, not merely bounded by zero.
Casting that zero to i32 gives zero, and the checked addition to the initial decimal point is 0+0=0. The checked subtraction of trailing zeros from the digit count is N-0=N. The following UScalar.hcast .I32 is the extracted narrowing cast, not a range-checked conversion. It retains the low 32 bits and interprets them as signed. Since N=2^{31}, its residue modulo 2^{32} is 2^{31} and hence its signed value is
2^{31}-2^{32}=-2^{31}=-2147483648.
The ensuing checked signed addition is therefore 0+(-2147483648)=-2147483648, which is exactly i32::MIN and is in range. Because N>768, the truncation branch replaces the exposed count by 768 and sets truncated to true. The remainder is empty, so no exponent-marker access or exponent arithmetic occurs. Finally the cleanup range starts at 768, while num imp dec2flt decimal seq DecimalSeq MAX DIGITS WITHOUT OVERFLOW states that its end is exactly 19. Since 768>19, the range 768\mathinner{..}19 is empty and performs no array access or update. We have proved the exact monadic equation
\texttt{parse\_decimal\_seq}(s)=\texttt{ok}\, \{\mathit{num\_digits}=768,\quad \mathit{decimal\_point}=-2147483648, \quad \mathit{truncated}=\mathrm{true}, \quad \mathit{digits}=\operatorname{repeat}(768,1)\}.
It remains only to unfold the beginning of the slow helper. Its zero_pow2(0) call returns normally with fp_0=\{m=0\#\mathrm{u64},p_{\rm biased}=0\#\mathrm{i32}\}. In the f64 trait, F64 Insts Dec2flt modelNumImpTraitsFloatU64 BITS states that BITS is 64, while F64 Insts Dec2flt modelNumImpTraitsFloatU64 SIG TOTAL BITS calls the mantissa-digits operation and ModelF64MantissaDigits states that this operation returns ok 53. Therefore num imp traits Float SIG BITS default performs the checked unsigned subtraction 53-1, which cannot underflow and returns ok 52. Next, num imp traits Float EXP BITS default performs its two checked unsigned subtractions: 64-52=12 and then 12-1=11; neither underflows, so EXP_BITS returns ok 11. By num imp traits Float EXP SAT default, the checked shift 1\#\mathrm{u32}\mathbin{\texttt{<<}}11 succeeds because 11<32 and gives 2048, after which the checked subtraction 2048-1 cannot underflow and gives EXP_SAT=2047. Finally, num imp traits Float INFINITE POWER default applies the extracted unsigned-to-signed hcast to 2047. Since 0\leq2047\leq2^{31}-1, this cast is exactly 2047\#\mathrm{i32}, so INFINITE_POWER returns ok 2047. Thus the eagerly constructed infinity pair also returns normally. The parser equation above then supplies the decimal record. Its count is not zero, but -2147483648<-324, so the second short-circuit branch returns fp_0 before any shifting loop or array access. Therefore
\texttt{parse\_long\_mantissa}(s)=\texttt{ok }fp_0. \tag{1}
We now bound the mathematical value of the witness. Because x has no exponent and no fraction, DecimalGoalVocabularyDefault reduces its accumulated explicit exponent and fraction count to zero, and hence
\operatorname{accumulatedMagnitude}(x) =\bigl(\operatorname{digitsValue}(\operatorname{replicate}(N,1)):\mathbb R\bigr).
Here is a symbolic lower bound for that fold. If A_k is the natural-number fold of k copies of one from zero, then A_1=1=10^0, and A_{k+1}=10A_k+1\geq10A_k. Induction gives A_k\geq10^{k-1} for every k\geq1. Also, a fold starting at a nonnegative accumulator never decreases when another digit one is appended, because 10a+1\geq a. Since
\operatorname{replicate}(N,1)= \operatorname{replicate}(1025,1)\mathbin{++} \operatorname{replicate}(N-1025,1)
and 1025<N, the fold-append equation and the two inductions give
10^{1024}\leq A_{1025}\leq A_N =\operatorname{digitsValue}(\operatorname{replicate}(N,1)).
Coercion from naturals to reals preserves this inequality, so
(10:\mathbb R)^{1024}\leq \operatorname{accumulatedMagnitude}(x). \tag{2}
Instantiate the assumed universal statement with s, x, fp_0, the source-validity, sign, validity, and rendering facts above, and equation (1). It supplies a target word w that both assembles from fp_0 and satisfies roundsMagnitude for the value in (2). Such a rounding target must be positive infinity. Indeed, writing T=2^{1024}-2^{970} for the overflow threshold,
T\leq2^{1024}\leq10^{1024} \leq\operatorname{accumulatedMagnitude}(x).
The first inequality uses 2^{970}\geq0, and the second follows from 2\leq10 by monotonicity of natural powers. If w.\mathit{val}\ne2047\cdot2^{52}, unfolding roundsMagnitude selects its finite branch and requires the strict opposite inequality \operatorname{accumulatedMagnitude}(x)<T, contradicting the displayed bound. Hence
w.\mathit{val}=2047\cdot2^{52}=9218868437227405312. \tag{3}
On the other hand, unfolding assembledWord at fp_0 gives
w.\mathit{val}=\operatorname{Nat.lor} \left(0, \bigl(((0\mathbin{\mathrm{emod}}2^{64}).\mathit{toNat})2^{52}\bigr) \bmod2^{64}\right)=\operatorname{Nat.lor}(0,0)=0. \tag{4}
Equations (3) and (4) are incompatible because 2047\cdot2^{52}>0. This contradiction disproves the universal statement and establishes its exact negation.

Lean proof


  obtain ⟨s, x, _, _, hwitness⟩ := SlowAccumulatedRoundingWitness
  intro hall
  apply hwitness
  intro hsource hsign hvalid hrender hslow
  exact hall s x { m := 0#u64, p_biased := 0#i32 }
    hsource hsign hvalid hrender hslow

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalGoalVocabularyDefault
import Tablet.SourceValidByteSlice
import Tablet.F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64
import Tablet.F64_Insts_CoreOpsArithDivF64F64
import Tablet.F64_Insts_CoreOpsArithMulF64F64
import Tablet.F64_Insts_CoreOpsArithNegF64
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatExtU64
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_BITS
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SIG_TOTAL_BITS
import Tablet.ModelF64MantissaDigits
import Tablet.num_imp_dec2flt_dec2flt
import Tablet.num_imp_dec2flt_dec2flt_with_trace
import Tablet.num_imp_dec2flt_parse_parse_number
import Tablet.num_imp_dec2flt_parse_parse_inf_nan
import Tablet.num_imp_dec2flt_decimal_Decimal_try_fast_path
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW
import Tablet.num_imp_dec2flt_lemire_compute_float
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa
import Tablet.num_imp_dec2flt_biased_fp_to_float
import Tablet.num_imp_traits_Float_EXP_BITS_default
import Tablet.num_imp_traits_Float_EXP_SAT_default
import Tablet.num_imp_traits_Float_INFINITE_POWER_default
import Tablet.num_imp_traits_Float_SIG_BITS_default
import Tablet.SlowAccumulatedRoundingWitness

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: SlowAccumulatedRounding__Refutation]
theorem SlowAccumulatedRounding__Refutation : ¬ (∀ (s : Slice Std.U8) (x : DecimalLexeme) fp, SourceValidByteSlice s -> x.leadingSign = none ->
      DecimalGoalVocabularyDefault.validNumeric x -> DecimalGoalVocabularyDefault.bytes s = DecimalGoalVocabularyDefault.renderNumeric x ->
      num.imp.dec2flt.slow.parse_long_mantissa
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 s = ok fp ->
      ∃ target : F64, DecimalGoalVocabularyDefault.assembledWord fp target ∧
        DecimalGoalVocabularyDefault.roundsMagnitude (DecimalGoalVocabularyDefault.accumulatedMagnitude x) target) := by
-- BODY
  obtain ⟨s, x, _, _, hwitness⟩ := SlowAccumulatedRoundingWitness
  intro hall
  apply hwitness
  intro hsource hsign hvalid hrender hslow
  exact hall s x { m := 0#u64, p_biased := 0#i32 }
    hsource hsign hvalid hrender hslow

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 114 entries
Technical checking records
Local closure summary
{
  "node": "SlowAccumulatedRounding__Refutation",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "7f8b8756c083889ad60e990b2e45048c0077ecd660510352fccb8c05f1f1e4d2",
  "active_statement_hash": "111ec774e4848bda71acc4752e85b371b295d88320a759f22910ecdfc5e5be0e",
  "kernel_axioms": [
    "Classical.choice",
    "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-130",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "SlowAccumulatedRoundingWitness": "968bf92c37f22b2d"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "c08dd4b4aea4abb4",
    "DecimalGoalVocabularyDefault": "512f8d0aea894250",
    "DecimalLexeme": "8ad754cf2c272e73",
    "F64": "ec4e25fbaf4e11d4",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64": "94095c9bf79da164",
    "SourceValidByteSlice": "3dc42716a6378d26",
    "num_imp_dec2flt_common_BiasedFp": "c43614ff2149ecf2",
    "num_imp_dec2flt_slow_parse_long_mantissa": "cd8af64c3688b60f"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:895dfb3d309e4b53ae21d821ae38b82c455f766c1b847e8dac8c684425247b98",
  "current_fingerprint": "sound-v2:895dfb3d309e4b53ae21d821ae38b82c455f766c1b847e8dac8c684425247b98",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}