ParserSelectedExponent

Lean checked

Statement

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

Theorem

For every normally successful numeric execution whose trace selects a raw pair for final assembly, the selected pair's signed biased-exponent field lies between 0 and 2047 inclusive.

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.Assumptions
import Tablet.ModelF64MantissaDigits
import Tablet.ModelU64BitAnd
import Tablet.ModelU64BitOr
import Tablet.ModelU64Shl
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
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_BITS
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_LARGEST_POWER_OF_TEN
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_MAX_EXPONENT_ROUND_TO_EVEN
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_MIN_EXPONENT_ROUND_TO_EVEN
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SIG_TOTAL_BITS
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SMALLEST_POWER_OF_TEN
import Tablet.num_imp_dec2flt_Dec2FltAssemblyTrace
import Tablet.num_imp_dec2flt_Dec2FltTrace
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_common_BiasedFp
import Tablet.num_imp_dec2flt_common_BiasedFp_Insts_CoreCmpPartialEqBiasedFp_eq
import Tablet.num_imp_dec2flt_common_BiasedFp_zero_pow2
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_round
import Tablet.num_imp_dec2flt_decimal_seq_parse_decimal_seq
import Tablet.num_imp_dec2flt_lemire_compute_float
import Tablet.num_imp_dec2flt_lemire_compute_product_approx
import Tablet.num_imp_dec2flt_lemire_full_multiplication
import Tablet.num_imp_dec2flt_lemire_power
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa_MAX_SHIFT
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa_closure_Insts_CoreOpsFunctionFnTupleUsizeUsize_call
import Tablet.num_imp_dec2flt_table_LARGEST_POWER_OF_FIVE
import Tablet.num_imp_dec2flt_table_POWER_OF_FIVE_128
import Tablet.num_imp_dec2flt_table_SMALLEST_POWER_OF_FIVE
import Tablet.num_imp_traits_Float_EXP_BIAS_default
import Tablet.num_imp_traits_Float_EXP_BITS_default
import Tablet.num_imp_traits_Float_EXP_MIN_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.num_imp_dec2flt_biased_fp_to_float
import Tablet.ParserComputeFloatExponentRange
import Tablet.ParserSlowExponentRange

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: ParserSelectedExponent]
theorem ParserSelectedExponent :
    ∀ (s : Str) (f : F64) (tr : num.imp.dec2flt.Dec2FltAssemblyTrace F64),
      DecimalGoalVocabularyDefault.sourceString s ->
      num.imp.dec2flt.dec2flt_with_trace
        F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64
        F64.Insts.CoreOpsArithDivF64F64 F64.Insts.CoreOpsArithMulF64F64
        F64.Insts.CoreOpsArithNegF64 s =
        ok (core.result.Result.Ok f, num.imp.dec2flt.Dec2FltTrace.Assembled tr) ->
      0 <= tr.selected_pair.p_biased.val ∧ tr.selected_pair.p_biased.val <= 2047 := 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

Write F_0 for the Float component reached through the traitsFloatExtInst field of F64 Insts Dec2flt modelNumImpDec2fltLemireU64. By F64 Insts Dec2flt modelNumImpTraitsFloatExtU64 that component is exactly F64 Insts Dec2flt modelNumImpTraitsFloatU64. We first evaluate the f64 constants used below. By F64 Insts Dec2flt modelNumImpTraitsFloatU64 BITS, BITS=64. The declaration F64 Insts Dec2flt modelNumImpTraitsFloatU64 SIG TOTAL BITS calls the operation ModelF64MantissaDigits, which returns 53. Thus the checked subtraction in num imp traits Float SIG BITS default is 53-1=52, and SIG_BITS=52. The two checked subtractions in num imp traits Float EXP BITS default are 64-52=12 and 12-1=11, so EXP_BITS=11. In num imp traits Float EXP SAT default, the shift count 11 is below the 32-bit width, 1\mathbin{\mathtt{<<}}11=2048, and the following checked subtraction gives EXP_SAT=2047. Hence num imp traits Float INFINITE POWER default casts the in-range unsigned value 2047 to the signed value 2047, while num imp traits Float EXP BIAS default shifts 2047 right once and gives 1023. Finally num imp traits Float EXP MIN default casts 1023 exactly, subtracts one without overflow, and negates 1022, giving EXP_MIN=-1022. These computations also show that every monadic operation in the constant chain succeeds. The other f64 constants which are read in the integer converter are -342, 308, -4, and 23 by F64 Insts Dec2flt modelNumImpTraitsFloatU64 SMALLEST POWER OF TEN, F64 Insts Dec2flt modelNumImpTraitsFloatU64 LARGEST POWER OF TEN, F64 Insts Dec2flt modelNumImpTraitsFloatU64 MIN EXPONENT ROUND TO EVEN, and F64 Insts Dec2flt modelNumImpTraitsFloatU64 MAX EXPONENT ROUND TO EVEN.
We next prove the following range fact directly from num imp dec2flt lemire compute float:
\texttt{compute_float}(F_0,q,w)=\texttt{ok }b \quad\Longrightarrow\quad b.p_{\rm biased}=-1\quad\hbox{or}\quad 0\leq b.p_{\rm biased}\leq2047. \tag{1}
The pair carrier has the two extracted fields m and p_biased. The three eager calls to num imp dec2flt common BiasedFp zero pow2 produce, respectively, fp_0=(0,0), fp_\infty=(0,2047), and fp_{\rm error}=(0,-1); this accounts for the zero-mantissa return, the two decimal-cutoff returns, and the approximation-error return. If none of those returns is taken, then w\ne0 and -342\leq q\leq308. Consequently its leading-zero count is in 0,\ldots,63, normalization shifts by a legal count, the cast of SIG_BITS to usize is 52, and the checked precision addition is 52+3=55. The identical table bounds -342 and 308 in num imp dec2flt table SMALLEST POWER OF FIVE and num imp dec2flt table LARGEST POWER OF FIVE, together with the 651-entry declaration num imp dec2flt table POWER OF FIVE 128, show that num imp dec2flt lemire compute product approx receives a legal precision and a table index q-(-342) in 0,\ldots,650. Its mask shift is therefore legal. The two widening casts and 128-bit product in num imp dec2flt lemire full multiplication are exact because the product of two 64-bit words fits in 128 bits; its shift by 64 is legal. A possible checked increment of the returned high limb, and every operation in the product approximation which could nevertheless fail, is bound through the surrounding result monad: such a failure would make the assumed compute_float equation a failure rather than ok b. No range conclusion below is inferred from a value on such a failing branch.
There are two copies of the remaining calculation in the extracted definition: one below the lo = u64::MAX test when the additional -27\leq q\leq55 test succeeds, and one below its false branch. They are textually identical, so we check both by checking that common block. Shifting the high product limb right by 63 is legal and yields an upperbit in \{0,1\}. Its cast to i32 is exact; 64+\texttt{upperbit} is 64 or 65; and the checked subtractions of 52 and 3 give the legal mantissa shift 9 or 10. The cast of q from i64 to i32 is exact because -342\leq q\leq308. In num imp dec2flt lemire power, the checked constant addition is 152170+65536=217706, the product with q lies between -74455452 and 67053448 and hence the wrapping product equals the mathematical product, the shift by 16 is legal, and adding 63 gives a value between -1074 and 1086. Adding upperbit, casting the leading-zero count exactly, subtracting that count, subtracting EXP_MIN=-1022, and adding one therefore keep the intermediate power2 between -114 and 2110. All those signed additions and subtractions are consequently in range. This discharges the casts, shifts, and checked arithmetic through the power2 test in each duplicated block.
If power2 <= 0, its checked negation is at most 114 and cannot be the forbidden negation of i32::MIN. The following checked addition by one is at most 115. A value at least 64 returns fp_0; otherwise the repeated addition by one gives a shift count in 1,\ldots,63. The two right shifts, by that count and by one, are legal; the conjunction with one is total by ModelU64BitAnd; and if the intervening mantissa addition overflowed, the result monad would preclude the assumed successful return. The shift 1\mathbin{\mathtt{<<}}52 is legal by ModelU64Shl. The returned exponent is the signed cast of the Boolean comparison, hence exactly zero or one.
If power2 > 0, all round-to-even subbranches alter only the mantissa. Their casts of -4 and 23 to i64 are exact; the conjunctions are total; and their shifts use the already established count 9 or 10. The final low-bit conjunction gives zero or one. A failing checked mantissa increment again cannot occur on an ok path, and the following right shift by one is legal because 1<64. The threshold 2\mathbin{\mathtt{<<}}52 and replacement 1\mathbin{\mathtt{<<}}52 use the legal count 52. If rounding carries, the checked exponent increment is at most 2111; otherwise the exponent remains power2. Clearing the implicit bit uses one more legal shift by 52 and a total conjunction. The last test compares this final exponent p with 2047. If p\geq2047 the function returns fp_\infty; if not, it returns a record with 0<p<2047. This proves (1) in both duplicated branches, including every ordinary, underflow, and sentinel return.
We also need the stronger slow-converter fact obtained from num imp dec2flt slow parse long mantissa:
\texttt{parse_long_mantissa}(F_0,t)=\texttt{ok }b \quad\Longrightarrow\quad 0\leq b.p_{\rm biased}\leq2047. \tag{2}
After the same successfully evaluated zero and infinity sentinels, a failure of num imp dec2flt decimal seq parse decimal seq propagates and so is excluded by the displayed premise. A sequence with no digits or decimal point below -324 returns fp_0; one with decimal point at least 310 returns fp_\infty. It remains to inspect the three extracted loops.
For loop 0 use the invariant
\texttt{loop_result}=\texttt{none} \quad\hbox{or}\quad \texttt{loop_result}=\texttt{some }fp_0. \tag{3}
It holds initially. A body iteration either leaves that field unchanged, terminates when it is already some, or, after a call through num imp dec2flt slow parse long mantissa closure Insts CoreOpsFunctionFnTupleUsizeUsize call and num imp dec2flt decimal seq DecimalSeq right shift, stores exactly some fp_0 when the shifted decimal point is below the negative range constant. Thus (3) is inductive. Every narrowing cast, negation, exponent addition, shift-helper operation, array operation, or decimal shift which fails makes the loop and hence the outer call fail; on a successful transition the checked arithmetic has its displayed mathematical meaning. Therefore a returned some from loop 0 is exactly fp_0.
Loop 1 has the analogous invariant
\texttt{loop_result}=\texttt{none} \quad\hbox{or}\quad \texttt{loop_result}=\texttt{some }fp_\infty. \tag{4}
The field again starts as none; the only assignments to it occur after either left-shift branch and assign exactly some fp_\infty. The other iterations preserve it, and a body presented with some terminates. The zero-index array read, sign change and cast of the decimal point, closure call, casts of the shift, checked exponent subtraction, and the calls to num imp dec2flt decimal seq DecimalSeq left shift are all monadic predecessors of the loop result. A failure in any one is therefore incompatible with a successful outer return. Hence a returned some from loop 1 is exactly fp_\infty.
For loop 2 use the exit invariant
\text{if the loop returns \texttt{ok }(d',e), then }e\geq-1022. \tag{5}
Indeed, its body returns done only when the test EXP_MIN > exp2 is false, which is precisely \texttt{exp2}\geq-1022. In the true branch the positive checked difference -1022-\texttt{exp2} is cast to usize, capped by num imp dec2flt slow parse long mantissa MAX SHIFT at 60, passed to num imp dec2flt decimal seq DecimalSeq right shift, cast back exactly from the interval 0,\ldots,60, and added to exp2 before a cont. Any subtraction, cast, shift, or addition failure propagates, so a successful loop return must eventually be a done return and satisfies (5).
If (3) or (4) supplies a result, it is one of the two sentinels and satisfies (2). Otherwise the main path first computes the checked
x=e-(-1022)+1
from the loop-2 exponent e. By (5), x\geq1. If x\geq2047, the first guard returns fp_\infty; otherwise 1\leq x\leq2046. The cast SIG_BITS=52 to usize, the checked addition producing 53, the decimal left shift by 53, and the call to num imp dec2flt decimal seq DecimalSeq round must all succeed on the assumed normal path. The shifts 1\mathbin{\mathtt{<<}}53 and 1\mathbin{\mathtt{<<}}52 are legal. Without a rounding carry, the returned exponent is x or the successful checked value x-1, hence belongs to [0,2046]. With a carry, the checked e'=e+1 is followed by a second guard on x'=e'-(-1022)+1=x+1. If x'\geq2047 it returns fp_\infty; otherwise 2\leq x'\leq2046, and the returned exponent is x' or x'-1, again in [0,2046]. The mask subtractions from 2^{52} cannot underflow and the final conjunctions are total. Any failing checked e+1, subtraction, addition, decimal shift/round, scalar shift, or cast would propagate instead of yielding the assumed ok b. This proves (2) with all direct, loop-result, carrying, and noncarrying exits covered.
Now unfold num imp dec2flt dec2flt with trace under the theorem's assumed result. The six trace constructors appearing in that extracted definition are disjoint. The empty-input and sign-only branches return EmptyInput and SignOnly; numeric failure followed by special failure returns Invalid; numeric failure followed by special success returns Special; and successful try_fast_path returns FastPath. None can equal the assumed Assembled tr. Hence num imp dec2flt parse parse number returned some r and num imp dec2flt decimal Decimal try fast path returned none; the call to num imp dec2flt parse parse inf nan is not made on this branch.
The first integer call returns a pair satisfying (1). When many_digits is false, or its first exponent is negative, the second guard is false and the post-comparison pair is the first pair. Otherwise the checked addition r.\texttt{mantissa}+1 and the second integer call must both succeed, since either failure would propagate and contradict the assumed outer ok. The second result also satisfies (1). The comparison is the total field comparison in num imp dec2flt common BiasedFp Insts CoreCmpPartialEqBiasedFp eq. If it reports equality the code retains the first pair; if it reports inequality the record update retains the first mantissa and writes exponent -1. Thus in every case the post-comparison exponent is -1 or is in [0,2047].
If that exponent is negative, the dispatcher selects the successfully returned slow pair, which satisfies (2). If it is not negative, the dispatcher selects the post-comparison pair itself, whose exponent is then in [0,2047]. Therefore, before assembly in either branch, the local selected_pair has exponent in [0,2047]. The next IScalar.hcast .U64 is a total sign-extending bit-vector cast, rather than a range-checking cast; on the range just proved it nevertheless agrees with the ordinary nonnegative conversion. The following shift uses the already computed count 52, the OR is total by ModelU64BitOr, and any failure in those assembly operations, num imp dec2flt biased fp to float, or final negation would contradict the assumed successful result.
Finally, the two sign branches in that same extracted definition build Dec2FltTrace.Assembled records, placing that same local pair in the selected_pair field. Constructor injectivity applied to the assumed equality identifies the constructed record with tr, and hence identifies its pair with tr.\texttt{selected_pair}. Transferring the range proved above gives 0\leq tr.\texttt{selected_pair}.p_{\rm biased}\leq2047, as required. The sourceString premise from DecimalGoalVocabularyDefault is needed to state the supplied source-domain goal faithfully but no additional lexical property is needed for this model-internal range argument.

Lean proof


  intro s f tr hs h
  have bind_ok_inv : ∀ {α β : Type} (r : Result α) (g : α → Result β) y,
      Bind.bind r g = ok y → ∃ x, r = ok x ∧ g x = ok y := by
    intro α β r g y hr
    cases r with
    | fail err => simp [Bind.bind, Aeneas.Std.bind] at hr
    | div => simp [Bind.bind, Aeneas.Std.bind] at hr
    | ok x => exact ⟨x, rfl, hr⟩
  let Good := fun (x : num.imp.dec2flt.common.BiasedFp) =>
    x.p_biased.val = -1 ∨
      (0 ≤ x.p_biased.val ∧ x.p_biased.val ≤ 2047)
  unfold num.imp.dec2flt.dec2flt_with_trace at h
  obtain ⟨body, hbody, h⟩ := bind_ok_inv _ _ _ h
  obtain ⟨first, hfirst, h⟩ := bind_ok_inv _ _ _ h
  cases first with
  | none =>
    obtain ⟨pfe, hpfe, h⟩ := bind_ok_inv _ _ _ h
    cases h
  | some c =>
    obtain ⟨body1, hbody1, h⟩ := bind_ok_inv _ _ _ h
    obtain ⟨empty, hempty, h⟩ := bind_ok_inv _ _ _ h
    by_cases he : empty = true
    · simp only [he, if_true] at h
      obtain ⟨pfe, hpfe, h⟩ := bind_ok_inv _ _ _ h
      cases h
    · have hef : empty = false := Bool.eq_false_of_not_eq_true he
      simp only [hef, if_false] at h
      obtain ⟨parsed, hparsed, h⟩ := bind_ok_inv _ _ _ h
      cases parsed with
      | none =>
        obtain ⟨special, hspecial, h⟩ := bind_ok_inv _ _ _ h
        cases special with
        | none =>
          obtain ⟨pfe, hpfe, h⟩ := bind_ok_inv _ _ _ h
          cases h
        | some value => cases h
      | some r =>
        obtain ⟨fast, hfast, h⟩ := bind_ok_inv _ _ _ h
        cases fast with
        | some value => cases h
        | none =>
          obtain ⟨firstPair, hfirstPair, h⟩ := bind_ok_inv _ _ _ h
          have hfirstGood : Good firstPair :=
            ParserComputeFloatExponentRange _ _ _ hfirstPair
          obtain ⟨phase1, hphase1, h⟩ := bind_ok_inv _ _ _ h
          rcases phase1 with ⟨many1, many2, firstPair1, fp, guard⟩
          have hfp : fp = firstPair := by
            by_cases hm : r.many_digits
            · simp only [hm, if_true] at hphase1
              cases hphase1
              rfl
            · simp only [hm, if_false] at hphase1
              cases hphase1
              rfl
          subst fp
          obtain ⟨phase2, hphase2, h⟩ := bind_ok_inv _ _ _ h
          rcases phase2 with
            ⟨post, secondExp, secondMant, secondResult, secondEqual, forced⟩
          have hpost : Good post := by
            cases guard with
            | false =>
              simp only [Bool.false_eq_true, if_false] at hphase2
              cases hphase2
              exact hfirstGood
            | true =>
              simp only [if_true] at hphase2
              obtain ⟨mantissa, hmantissa, hphase2⟩ :=
                bind_ok_inv _ _ _ hphase2
              obtain ⟨second, hsecond, hphase2⟩ :=
                bind_ok_inv _ _ _ hphase2
              have hsecondGood : Good second :=
                ParserComputeFloatExponentRange _ _ _ hsecond
              obtain ⟨equal, hequal, hphase2⟩ :=
                bind_ok_inv _ _ _ hphase2
              obtain ⟨chosen, hchosen, hphase2⟩ :=
                bind_ok_inv _ _ _ hphase2
              rcases chosen with ⟨bf, wasForced⟩
              have hbf : Good bf := by
                by_cases heq : equal = true
                · simp only [heq, if_true] at hchosen
                  cases hchosen
                  exact hfirstGood
                · have heqf : equal = false := Bool.eq_false_of_not_eq_true heq
                  simp only [heqf, if_false] at hchosen
                  cases hchosen
                  left
                  norm_num
              cases hphase2
              exact hbf
          obtain ⟨phase3, hphase3, h⟩ := bind_ok_inv _ _ _ h
          rcases phase3 with ⟨selected, slowGuard, slowResult⟩
          have hselected : 0 ≤ selected.p_biased.val ∧
              selected.p_biased.val ≤ 2047 := by
            by_cases hneg : post.p_biased < 0#i32
            · simp only [hneg, if_true] at hphase3
              obtain ⟨replacement, hreplacement, hphase3⟩ :=
                bind_ok_inv _ _ _ hphase3
              have hrange := ParserSlowExponentRange _ _ hreplacement
              cases hphase3
              exact hrange
            · simp only [hneg, if_false] at hphase3
              cases hphase3
              rcases hpost with hminus | hrange
              · change ¬ post.p_biased.val < 0 at hneg
                omega
              · exact hrange
          obtain ⟨i, hi, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i1, hi1, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i2, hi2, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨word, hword, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨assembled, hassembled, h⟩ := bind_ok_inv _ _ _ h
          by_cases hnegative : c = 45#u8
          · simp only [hnegative, if_true] at h
            obtain ⟨result, hresult, h⟩ := bind_ok_inv _ _ _ h
            cases h
            exact hselected
          · simp only [hnegative, if_false] at h
            cases h
            exact hselected

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalGoalVocabularyDefault
import Tablet.Assumptions
import Tablet.ModelF64MantissaDigits
import Tablet.ModelU64BitAnd
import Tablet.ModelU64BitOr
import Tablet.ModelU64Shl
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
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_BITS
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_LARGEST_POWER_OF_TEN
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_MAX_EXPONENT_ROUND_TO_EVEN
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_MIN_EXPONENT_ROUND_TO_EVEN
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SIG_TOTAL_BITS
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SMALLEST_POWER_OF_TEN
import Tablet.num_imp_dec2flt_Dec2FltAssemblyTrace
import Tablet.num_imp_dec2flt_Dec2FltTrace
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_common_BiasedFp
import Tablet.num_imp_dec2flt_common_BiasedFp_Insts_CoreCmpPartialEqBiasedFp_eq
import Tablet.num_imp_dec2flt_common_BiasedFp_zero_pow2
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_round
import Tablet.num_imp_dec2flt_decimal_seq_parse_decimal_seq
import Tablet.num_imp_dec2flt_lemire_compute_float
import Tablet.num_imp_dec2flt_lemire_compute_product_approx
import Tablet.num_imp_dec2flt_lemire_full_multiplication
import Tablet.num_imp_dec2flt_lemire_power
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa_MAX_SHIFT
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa_closure_Insts_CoreOpsFunctionFnTupleUsizeUsize_call
import Tablet.num_imp_dec2flt_table_LARGEST_POWER_OF_FIVE
import Tablet.num_imp_dec2flt_table_POWER_OF_FIVE_128
import Tablet.num_imp_dec2flt_table_SMALLEST_POWER_OF_FIVE
import Tablet.num_imp_traits_Float_EXP_BIAS_default
import Tablet.num_imp_traits_Float_EXP_BITS_default
import Tablet.num_imp_traits_Float_EXP_MIN_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.num_imp_dec2flt_biased_fp_to_float
import Tablet.ParserComputeFloatExponentRange
import Tablet.ParserSlowExponentRange

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: ParserSelectedExponent]
theorem ParserSelectedExponent :
    ∀ (s : Str) (f : F64) (tr : num.imp.dec2flt.Dec2FltAssemblyTrace F64),
      DecimalGoalVocabularyDefault.sourceString s ->
      num.imp.dec2flt.dec2flt_with_trace
        F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64
        F64.Insts.CoreOpsArithDivF64F64 F64.Insts.CoreOpsArithMulF64F64
        F64.Insts.CoreOpsArithNegF64 s =
        ok (core.result.Result.Ok f, num.imp.dec2flt.Dec2FltTrace.Assembled tr) ->
      0 <= tr.selected_pair.p_biased.val ∧ tr.selected_pair.p_biased.val <= 2047 := by
-- BODY
  intro s f tr hs h
  have bind_ok_inv : ∀ {α β : Type} (r : Result α) (g : α → Result β) y,
      Bind.bind r g = ok y → ∃ x, r = ok x ∧ g x = ok y := by
    intro α β r g y hr
    cases r with
    | fail err => simp [Bind.bind, Aeneas.Std.bind] at hr
    | div => simp [Bind.bind, Aeneas.Std.bind] at hr
    | ok x => exact ⟨x, rfl, hr⟩
  let Good := fun (x : num.imp.dec2flt.common.BiasedFp) =>
    x.p_biased.val = -1 ∨
      (0 ≤ x.p_biased.val ∧ x.p_biased.val ≤ 2047)
  unfold num.imp.dec2flt.dec2flt_with_trace at h
  obtain ⟨body, hbody, h⟩ := bind_ok_inv _ _ _ h
  obtain ⟨first, hfirst, h⟩ := bind_ok_inv _ _ _ h
  cases first with
  | none =>
    obtain ⟨pfe, hpfe, h⟩ := bind_ok_inv _ _ _ h
    cases h
  | some c =>
    obtain ⟨body1, hbody1, h⟩ := bind_ok_inv _ _ _ h
    obtain ⟨empty, hempty, h⟩ := bind_ok_inv _ _ _ h
    by_cases he : empty = true
    · simp only [he, if_true] at h
      obtain ⟨pfe, hpfe, h⟩ := bind_ok_inv _ _ _ h
      cases h
    · have hef : empty = false := Bool.eq_false_of_not_eq_true he
      simp only [hef, if_false] at h
      obtain ⟨parsed, hparsed, h⟩ := bind_ok_inv _ _ _ h
      cases parsed with
      | none =>
        obtain ⟨special, hspecial, h⟩ := bind_ok_inv _ _ _ h
        cases special with
        | none =>
          obtain ⟨pfe, hpfe, h⟩ := bind_ok_inv _ _ _ h
          cases h
        | some value => cases h
      | some r =>
        obtain ⟨fast, hfast, h⟩ := bind_ok_inv _ _ _ h
        cases fast with
        | some value => cases h
        | none =>
          obtain ⟨firstPair, hfirstPair, h⟩ := bind_ok_inv _ _ _ h
          have hfirstGood : Good firstPair :=
            ParserComputeFloatExponentRange _ _ _ hfirstPair
          obtain ⟨phase1, hphase1, h⟩ := bind_ok_inv _ _ _ h
          rcases phase1 with ⟨many1, many2, firstPair1, fp, guard⟩
          have hfp : fp = firstPair := by
            by_cases hm : r.many_digits
            · simp only [hm, if_true] at hphase1
              cases hphase1
              rfl
            · simp only [hm, if_false] at hphase1
              cases hphase1
              rfl
          subst fp
          obtain ⟨phase2, hphase2, h⟩ := bind_ok_inv _ _ _ h
          rcases phase2 with
            ⟨post, secondExp, secondMant, secondResult, secondEqual, forced⟩
          have hpost : Good post := by
            cases guard with
            | false =>
              simp only [Bool.false_eq_true, if_false] at hphase2
              cases hphase2
              exact hfirstGood
            | true =>
              simp only [if_true] at hphase2
              obtain ⟨mantissa, hmantissa, hphase2⟩ :=
                bind_ok_inv _ _ _ hphase2
              obtain ⟨second, hsecond, hphase2⟩ :=
                bind_ok_inv _ _ _ hphase2
              have hsecondGood : Good second :=
                ParserComputeFloatExponentRange _ _ _ hsecond
              obtain ⟨equal, hequal, hphase2⟩ :=
                bind_ok_inv _ _ _ hphase2
              obtain ⟨chosen, hchosen, hphase2⟩ :=
                bind_ok_inv _ _ _ hphase2
              rcases chosen with ⟨bf, wasForced⟩
              have hbf : Good bf := by
                by_cases heq : equal = true
                · simp only [heq, if_true] at hchosen
                  cases hchosen
                  exact hfirstGood
                · have heqf : equal = false := Bool.eq_false_of_not_eq_true heq
                  simp only [heqf, if_false] at hchosen
                  cases hchosen
                  left
                  norm_num
              cases hphase2
              exact hbf
          obtain ⟨phase3, hphase3, h⟩ := bind_ok_inv _ _ _ h
          rcases phase3 with ⟨selected, slowGuard, slowResult⟩
          have hselected : 0 ≤ selected.p_biased.val ∧
              selected.p_biased.val ≤ 2047 := by
            by_cases hneg : post.p_biased < 0#i32
            · simp only [hneg, if_true] at hphase3
              obtain ⟨replacement, hreplacement, hphase3⟩ :=
                bind_ok_inv _ _ _ hphase3
              have hrange := ParserSlowExponentRange _ _ hreplacement
              cases hphase3
              exact hrange
            · simp only [hneg, if_false] at hphase3
              cases hphase3
              rcases hpost with hminus | hrange
              · change ¬ post.p_biased.val < 0 at hneg
                omega
              · exact hrange
          obtain ⟨i, hi, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i1, hi1, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i2, hi2, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨word, hword, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨assembled, hassembled, h⟩ := bind_ok_inv _ _ _ h
          by_cases hnegative : c = 45#u8
          · simp only [hnegative, if_true] at h
            obtain ⟨result, hresult, h⟩ := bind_ok_inv _ _ _ h
            cases h
            exact hselected
          · simp only [hnegative, if_false] at h
            cases h
            exact hselected

end dec2flt_model

Proof structure

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

Uses

All 51 entries

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 156 entries
Technical checking records
Local closure summary
{
  "node": "ParserSelectedExponent",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "e4ca753538e42d6b08e4b20d5b34caaff3d19fbf8120f7d53f6290f7183a94c0",
  "active_statement_hash": "84f4fe10d0fa7a701708a1bb001629bdd15eea3daefe51ba878a31370f1879da",
  "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-132",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "ParserComputeFloatExponentRange": "d9f781305664f993",
    "ParserSlowExponentRange": "4b9c15f786857b92",
    "num_imp_dec2flt_dec2flt_with_trace": "8f54814ba9c190ea",
    "num_imp_traits_Float_SIG_BITS_default": "9ab622d85ef04d94"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "1f42f752b5d22513",
    "DecimalGoalVocabularyDefault": "512f8d0aea894250",
    "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",
    "Preamble": "86dc391ab250c79b",
    "core_ops_arith_Neg": "0ff07135a60bcbf9",
    "num_float_parse_ParseFloatError": "c43614ff2149ecf2",
    "num_imp_dec2flt_Dec2FltAssemblyTrace": "598dfa7d7b371110",
    "num_imp_dec2flt_Dec2FltTrace": "27322b4608129f59",
    "num_imp_dec2flt_Lemire": "1923f90c53d323e6",
    "num_imp_dec2flt_biased_fp_to_float": "acdde060e8d35ca7",
    "num_imp_dec2flt_common_BiasedFp": "c43614ff2149ecf2",
    "num_imp_dec2flt_common_BiasedFp_Insts_CoreCmpPartialEqBiasedFp_eq": "fbbf553e3c502a05",
    "num_imp_dec2flt_dec2flt_with_trace": "d34666730435cdb9",
    "num_imp_dec2flt_decimal_Decimal": "4da4ccbe640ef8e6",
    "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:0e34d41c6265b97107c5e35a9c79ef70b4a75ff48c375465ce47ef3913e1117a",
  "current_fingerprint": "sound-v2:0e34d41c6265b97107c5e35a9c79ef70b4a75ff48c375465ce47ef3913e1117a",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}