P367_T1_NumberHelperRenderedAccountingA

Lean checked

Statement

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

Lemma · Exact retained and retry accounting for rendered lexemes

Let s:\texttt{Slice U8} satisfy |s|\le A=3458764513820540928. Let x:\texttt{DecimalLexeme} have no leading sign, be valid numeric, and render exactly the whole byte-value list of s, in the vocabulary of DecimalGoalVocabularyDefault. Suppose the actual specialized call in ActualDispatchOperations returns \texttt{ok (some }d\texttt{)}. Let S be the integer digits of x followed by its fractional digits (empty when absent), let f be its fractional digit count, and let E=\texttt{accumulatedExplicitExponent}(x). Then both of the following conditional conclusions hold:
d.\mathit{many\_digits}=\texttt{false}\ \Longrightarrow d.\mathit{mantissa}.\mathit{val}=\operatorname{foldl}(a,b\mapsto10a+b,0,S) \ \land\ d.\mathit{exponent}.\mathit{val}=E-f;
d.\mathit{many\_digits}=\texttt{true}\ \Longrightarrow \exists P,Q,\quad S=P\mathbin{++}Q,\quad d.\mathit{mantissa}.\mathit{val}=\operatorname{foldl}(a,b\mapsto10a+b,0,P), \quad d.\mathit{exponent}.\mathit{val}=E-f+|Q|.
All displayed folds are natural folds, and all exponent equations are integer equations. The explicit exponent has the source-defined pre-update 2^{59} cutoff, including its overshoot and independent sign; it is not the unrestricted mathematical exponent. The statement does not assert the size of the retained prefix or require the discarded suffix to be nonzero.

Lean statement

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

import Tablet.P435_T1_RenderedDigitBlocks
import Tablet.P435_T1_DigitDecoding
import Tablet.P435_T1_SignificandScanA
import Tablet.P367_T1_NumberHelperFractionalRetryA
import Tablet.P367_T1_NumberHelperIntegralRetryA
import Tablet.DecimalGoalVocabularyDefault
import Tablet.P9_T1_ActualDispatchOperations
import Tablet.num_imp_dec2flt_parse_parse_number
import Tablet.num_imp_dec2flt_parse_parse_partial_number
import Tablet.P789_T1_RenderedExponentContinuation
import Tablet.P789_T1_RenderedSignificantBounds
import Tablet.P789_T1_SignificandNonwrapping
import Tablet.P789_T1_PartialNumberBranches

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P367_T1_NumberHelperRenderedAccountingA]
theorem P367_T1_NumberHelperRenderedAccountingA
    (s : Slice Std.U8) (x : DecimalLexeme) (d : num.imp.dec2flt.decimal.Decimal)
    (hlen : s.val.length <= 3458764513820540928)
    (hsign : x.leadingSign = none)
    (hvalid : DecimalGoalVocabularyDefault.validNumeric x)
    (hrender : DecimalGoalVocabularyDefault.renderNumeric x =
      DecimalGoalVocabularyDefault.bytes s)
    (hparse : P9_T1_ActualDispatchOperations.parseNumber s = ok (some d)) :
    let digits := x.integerDigits ++
      (match x.fractionDigits with | none => [] | some ds => ds)
    let fractionCount := match x.fractionDigits with | none => 0 | some ds => ds.length
    let explicit := DecimalGoalVocabularyDefault.accumulatedExplicitExponent x
    (d.many_digits = false ->
      d.mantissa.val = digits.foldl (fun a b => 10 * a + b) 0 /\
      d.exponent.val = explicit - (fractionCount : Int)) /\
    (d.many_digits = true ->
      exists consumed remaining : List Nat,
        digits = consumed ++ remaining /\
        d.mantissa.val = consumed.foldl (fun a b => 10 * a + b) 0 /\
        d.exponent.val = explicit - (fractionCount : Int) +
          (remaining.length : Int)) := 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 K(a,b)=10a+(b.\mathit{val}-48) for the byte-digit step, H(a,d)=10a+d for the natural-digit step, and v(B) for the zero-start K fold on a byte list B.
First recover an actual partial-parser result. Unfold the parseNumber field of ActualDispatchOperations and then num imp dec2flt parse parse number. A failure, divergence, or normal None from the partial parser cannot give the assumed normal Some d. A normal Some pair with a consumed length different from the whole slice length is also rejected. Thus for some Usize \ell, the actual partial parser returns normally Some (d,\ell), with the same record d. This uses inversion of the wrapper's Result, option and pair constructors, not a totality assumption.
Apply RenderedDigitBlocks to the unsigned rendering equality, with empty remainder. Obtain integer bytes I, fractional bytes F, exponent digit bytes J, and the exact suffix T. Let p be the presence of a fraction. Then s.\mathit{val}=I++[46]++F++T if p, and I++T otherwise; F=[] in the latter case. All three byte digit blocks lie in [48,57]. The suffix is empty or starts with 69 or 101. Consequently it has a nondigit head when nonempty, and its head is neither 48 nor 46. Apply DigitDecoding to I,F and their encoding equations. Decoding gives exactly the integer and fractional natural digit lists of x, preserves their lengths, and transports their folds. Mapping over append therefore identifies the decoding of I++F with S, its natural fold with v(I++F), and |F| with f.
Apply SignificandScanA to this decomposition. Its digit, nondigit-tail, absent-point and length hypotheses all hold by the preceding paragraph and the assumed A-bound. Obtain actual normal scan words m_0,m, saved integer-end slice s_1, significand-end slice s_2, Isize counts i,f_b,D, and I64 base b. The stated equations give s_1's exact point-headed or point-free tail, s_2.\mathit{val}=T, b.\mathit{val}=-|F|=-f, and D.\mathit{val}=|I|+|F|=|S|. The shape gives |I|+|F|\le|s|\le A and |F|\le A. Validity in DecimalGoalVocabularyDefault supplies a nonempty integer or fractional digit list. Hence D.\mathit{val}>0. In particular 0<D.\mathit{val}\le|s| and -A\le b.\mathit{val}\le A.
Apply RenderedExponentContinuation to x,s_2,J. Its value-map and byte-range premises are those of the rendered blocks, and its suffix premise is s_2.\mathit{val}=T. It gives an actual I64 explicit word e, with e.\mathit{val}=E and -C\le e.\mathit{val}\le C, C=5764607523034234879. It also gives either the empty suffix with e=0, or an actual marker split followed by a normal scientific Some e and empty residual. Thus it identifies the exact source continuation, including the accumulated exponent's cutoff and sign.
Apply PartialNumberBranches using these continuation facts, the normal scans, offset, fraction block and count sum, the bounds just established, and the actual partial-parser result. Put z=\texttt{count}_{48}(\texttt{takeWhile}_{48\lor46}(s.\mathit{val})). Its alternatives are: a retained record with many-digits false, mantissa m and exponent value b.\mathit{val}+e.\mathit{val}=E-f, satisfying D.\mathit{val}\le19 or D.\mathit{val}-19-z\le0; or a retry record with many-digits true, 0<D.\mathit{val}-19-z, and the actual retry call on (s,s_1,e) equal to \texttt{ok}(d.\mathit{mantissa},d.\mathit{exponent}).
For the size implications apply RenderedSignificantBounds to I,F,T,p and the exact source shape. Its hypotheses hold because the digit ranges are known and T is empty or marker-headed. This lemma identifies the raw source zero count with the natural leading-zero count even when that prefix crosses the point. It gives v(I++F)<10^{19} in the retained alternative and v(I++F)\ge10^{19} in the retry alternative. These are natural fold bounds established without using exactness of either wrapping scan.
If d.\mathit{many\_digits} is false, the retry alternative is impossible. In the retained alternative the bound just proved implies v(I++F)\le\texttt{U64.max}, since 10^{19}-1<2^{64}-1. Apply SignificandNonwrapping with the actual scan and fraction-block equations, saved integer-end shape and the same digit and stopping facts. Its source-length bound holds since A<2^{63}-1. It yields m.\mathit{val}=v(I++F). The retained record's mantissa is m, and decoding transports this value to the natural fold on S. Its exponent value is E-f, as already identified. This proves the first conditional conclusion.
If d.\mathit{many\_digits} is true, the retained alternative is impossible. In the retry alternative consider point presence. If p is true, the actual start and saved end have contents I++[46]++F++T and [46]++F++T. Apply NumberHelperFractionalRetryA with explicit word e. Its digit, tail-head and A-bound premises hold, and its exponent bounds were obtained above. It gives a normal retry pair (m',q) and byte lists P_b,Q_b with I++F=P_b++Q_b, m'.\mathit{val}=v(P_b) and q.\mathit{val}=E-|F|+|Q_b|.
If p is false, F=[], and the independent retry fold bound gives v(I)\ge10^{19}\ge10^{18}. The start and saved end now have contents I++T and T. Apply NumberHelperIntegralRetryA using this value bound and the same digit, tail, A-bound and explicit exponent bounds. It returns a normal retry pair (m',q) and I=P_b++Q_b, with m'.\mathit{val}=v(P_b) and q.\mathit{val}=E+|Q_b|=E-|F|+|Q_b|. Thus both point cases supply the same partition and accounting form.
In either case compare this normal retry result with the actual retry equation supplied by the branch lemma. Injectivity of Result and pairs identifies m'=d.\mathit{mantissa} and q=d.\mathit{exponent}. Take P,Q to be the decodings of P_b,Q_b. Mapping the partition over append gives S=P++Q; map preserves |Q_b|=|Q|, and the fold-map identity gives the natural fold of P as v(P_b). Since |F|=f, the exponent becomes exactly E-f+|Q|. This proves the second conditional conclusion.

Lean proof


  have hpartial : exists consumed : Std.Usize,
      num.imp.dec2flt.parse.parse_partial_number s = ok (some (d, consumed)) := by
    change num.imp.dec2flt.parse.parse_number s = ok (some d) at hparse
    unfold num.imp.dec2flt.parse.parse_number at hparse
    cases hp : num.imp.dec2flt.parse.parse_partial_number s with
    | fail e => simp [hp] at hparse
    | div => simp [hp] at hparse
    | ok result =>
      cases result with
      | none => simp [hp] at hparse
      | some pair =>
        rcases pair with ⟨d', consumed⟩
        by_cases hc : consumed = s.len
        · simp [hp, hc] at hparse
          subst d'
          exact ⟨consumed, rfl⟩
        · simp [hp, hc] at hparse
  obtain ⟨consumed, hpartial⟩ := hpartial
  obtain ⟨integer, fraction, exponent, hblocks, hi, hf, he, hid, hfd, hed⟩ :=
    P435_T1_RenderedDigitBlocks s.val [] x hsign hvalid (by
      change s.val.map (fun b => b.val) =
        DecimalGoalVocabularyDefault.renderNumeric x ++ [].map (fun b : Std.U8 => b.val)
      simpa using hrender.symm)
  let tail : List Std.U8 := match x.exponent with
    | none => []
    | some (upper, sign, _) => (if upper then 69#u8 else 101#u8) ::
      (match sign with | none => [] | some neg => [if neg then 45#u8 else 43#u8]) ++ exponent
  let point : Bool := x.fractionDigits.isSome
  have hshape : s.val = integer ++
      (if point then 46#u8 :: (fraction ++ tail) else tail) := by
    cases hx : x.fractionDigits <;>
      simpa [point, hx, tail, List.append_assoc] using hblocks
  have hnoFraction : point = false -> fraction = [] := by
    cases hx : x.fractionDigits with
    | none => intro _; simpa [hx] using hf
    | some ds => simp [point, hx]
  have htail : forall b rest, tail = b :: rest -> ¬ (48 <= b.val /\ b.val <= 57) := by
    intro b rest h
    cases hx : x.exponent with
    | none => simp [tail, hx] at h
    | some tuple =>
      rcases tuple with ⟨upper, sign, ds⟩
      have hb : b = (if upper then 69#u8 else 101#u8) := by
        simpa [tail, hx] using (congrArg List.head? h).symm
      rw [hb]
      cases upper <;> norm_num
  have hnoPoint : point = false -> forall b rest, tail = b :: rest -> b ≠ 46#u8 := by
    intro _ b rest h
    cases hx : x.exponent with
    | none => simp [tail, hx] at h
    | some tuple =>
      rcases tuple with ⟨upper, sign, ds⟩
      have hb : b = (if upper then 69#u8 else 101#u8) := by
        simpa [tail, hx] using (congrArg List.head? h).symm
      rw [hb]
      cases upper <;> norm_num
  obtain ⟨intEnd, sigEnd, intWord, word, intCount, fractionCount, total, base,
    hscan, hintEnd, hiOffset, _, hfront, hsigEnd, _, hbase, hadd, htotal⟩ :=
    P435_T1_SignificandScanA s integer fraction tail point hshape hnoFraction
      hid hfd htail hnoPoint hlen
  obtain ⟨hdecodeI, hlengthI, _⟩ :=
    (P435_T1_DigitDecoding integer).2 x.integerDigits hi
  obtain ⟨hdecodeF, hlengthF, _⟩ :=
    (P435_T1_DigitDecoding fraction).2
      (match x.fractionDigits with | none => [] | some ds => ds) hf
  have hbaseExact : base.val =
      -((match x.fractionDigits with | none => 0 | some ds => ds.length) : Int) := by
    rw [hbase, hlengthF]
    cases x.fractionDigits <;> rfl
  have htotalExact : total.val =
      ((x.integerDigits ++ (match x.fractionDigits with | none => [] | some ds => ds)).length : Int) := by
    rw [htotal, hlengthI, hlengthF, List.length_append]
  have hdecodeIF : (integer ++ fraction).map (fun b => b.val - 48) =
      x.integerDigits ++ (match x.fractionDigits with | none => [] | some ds => ds) := by
    simp only [List.map_append, hdecodeI, hdecodeF]
  have hfoldIF : (integer ++ fraction).foldl (fun a b => 10 * a + (b.val - 48)) 0 =
      (x.integerDigits ++ (match x.fractionDigits with | none => [] | some ds => ds)).foldl
        (fun a b => 10 * a + b) 0 := by
    rw [← List.foldl_map, hdecodeIF]
  have hcountBound : integer.length + fraction.length <= s.val.length := by
    rw [hshape]
    cases hp : point with
    | false => simp [hp, hnoFraction hp]
    | true => simp [hp]; omega
  have hpositive : 0 < total.val := by
    have hv : x.integerDigits ≠ [] ∨
        match x.fractionDigits with | none => False | some ds => ds ≠ [] := by
      exact (show _ ∧ _ ∧ _ ∧ _ from hvalid).2.2.1
    rw [htotalExact]
    rcases hv with hv | hv
    · have : 0 < x.integerDigits.length := List.length_pos_of_ne_nil hv
      simp only [List.length_append, Nat.cast_add]
      omega
    · cases hx : x.fractionDigits with
      | none => simp [hx] at hv
      | some ds =>
        have : 0 < ds.length := List.length_pos_of_ne_nil (by simpa [hx] using hv)
        simp only [hx, List.length_append, Nat.cast_add]
        omega
  obtain ⟨explicit, hexact, hexpLo, hexpHi, hcontinuation⟩ :=
    P789_T1_RenderedExponentContinuation x sigEnd exponent hvalid he hed hsigEnd
  have hstop : forall b rest, tail = b :: rest -> b ≠ 48#u8 /\ b ≠ 46#u8 := by
    intro b rest ht
    cases hx : x.exponent with
    | none => simp [tail, hx] at ht
    | some tuple =>
      rcases tuple with ⟨upper, sign, ds⟩
      have hb : b = (if upper then 69#u8 else 101#u8) := by
        simpa [tail, hx] using (congrArg List.head? ht).symm
      rw [hb]
      cases upper <;> norm_num
  obtain ⟨_, hretainedBound, hretryBound⟩ :=
    P789_T1_RenderedSignificantBounds s integer fraction tail point
      hshape hnoFraction hid hfd hstop
  have hbaseLo : -3458764513820540928 <= base.val := by
    rw [hbase]
    omega
  have hbaseHi : base.val <= 3458764513820540928 := by rw [hbase]; omega
  have hcases := P789_T1_PartialNumberBranches s intEnd sigEnd intWord word
    intCount fractionCount total base explicit d consumed hlen hpositive
    (by rw [htotal]; exact_mod_cast hcountBound) hbaseLo hbaseHi hexpLo hexpHi
    hscan hiOffset hfront hadd hcontinuation hpartial
  constructor
  · intro hfalse
    rcases hcases with hkeep | hretry
    · rcases hkeep with ⟨_, hword, hexponent, hsize⟩
      have hbound : (integer ++ fraction).foldl (fun a b => 10 * a + (b.val - 48)) 0
          < 10^19 := hretainedBound (by simpa only [htotal] using hsize)
      have hvalue := P789_T1_SignificandNonwrapping s intEnd sigEnd intWord word
        fractionCount base integer fraction tail point hshape hintEnd hnoFraction
        hid hfd htail hnoPoint (by omega)
        (by rw [U64.max_eq]; norm_num at hbound ⊢; omega) hscan hfront
      exact ⟨by rw [hword, hvalue, hfoldIF], by rw [hexponent, hbaseExact, hexact]; omega⟩
    · simp [hfalse] at hretry
  · intro htrue
    rcases hcases with hkeep | hretry
    · simp [htrue] at hkeep
    · rcases hretry with ⟨_, hpositiveRetry, hretry⟩
      have hlarge : 10^19 <=
          (integer ++ fraction).foldl (fun a b => 10 * a + (b.val - 48)) 0 :=
        hretryBound (by simpa only [htotal] using hpositiveRetry)
      have finish : forall (m : Std.U64) (e : Std.I64) (used rest : List Std.U8),
          num.imp.dec2flt.parse.parse_many_digits s intEnd explicit = ok (m, e) ->
          integer ++ fraction = used ++ rest ->
          m.val = used.foldl (fun a b => 10 * a + (b.val - 48)) 0 ->
          e.val = explicit.val - (fraction.length : Int) + (rest.length : Int) ->
          exists used rest : List Nat,
            x.integerDigits ++ (match x.fractionDigits with | none => [] | some ds => ds) =
              used ++ rest /\
            d.mantissa.val = used.foldl (fun a b => 10 * a + b) 0 /\
            d.exponent.val = DecimalGoalVocabularyDefault.accumulatedExplicitExponent x -
              ((match x.fractionDigits with | none => 0 | some ds => ds.length) : Int) +
              (rest.length : Int) := by
        intro m e used rest hp hpart hm he
        have heq : (m, e) = (d.mantissa, d.exponent) := Result.ok.inj (hp.symm.trans hretry)
        have hm' : m = d.mantissa := congrArg Prod.fst heq
        have he' : e = d.exponent := congrArg Prod.snd heq
        refine ⟨used.map (fun b => b.val - 48), rest.map (fun b => b.val - 48), ?_, ?_, ?_⟩
        · simpa only [List.map_append, hdecodeIF] using
            congrArg (List.map (fun b : Std.U8 => b.val - 48)) hpart
        · simpa only [List.foldl_map, hm'] using hm
        · rw [he'] at he
          cases hx : x.fractionDigits <;>
            simpa only [List.length_map, hexact, hlengthF, hx, List.length_nil, Nat.cast_zero] using he
      cases hp : point with
      | true =>
        obtain ⟨m, e, used, rest, hparseRetry, hpart, hm, _, he⟩ :=
          P367_T1_NumberHelperFractionalRetryA s intEnd explicit integer fraction tail
            (by simpa [hp] using hshape) (by simpa [hp] using hintEnd)
            hid hfd htail hlen hexpLo hexpHi
        exact finish m e used rest hparseRetry hpart hm he
      | false =>
        have hfEmpty := hnoFraction hp
        obtain ⟨m, e, used, rest, hparseRetry, hpart, hm, _, _, he⟩ :=
          P367_T1_NumberHelperIntegralRetryA s intEnd explicit integer tail
            (by simpa [hp] using hshape) (by simpa [hp] using hintEnd)
            hid htail hlen (by simpa [hfEmpty] using (show
              1000000000000000000 <= (integer ++ fraction).foldl
                (fun a b => 10 * a + (b.val - 48)) 0 by norm_num at hlarge; omega))
            hexpLo hexpHi
        exact finish m e used rest hparseRetry (by simpa [hfEmpty] using hpart) hm
          (by simpa [hfEmpty] using he)

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P435_T1_RenderedDigitBlocks
import Tablet.P435_T1_DigitDecoding
import Tablet.P435_T1_SignificandScanA
import Tablet.P367_T1_NumberHelperFractionalRetryA
import Tablet.P367_T1_NumberHelperIntegralRetryA
import Tablet.DecimalGoalVocabularyDefault
import Tablet.P9_T1_ActualDispatchOperations
import Tablet.num_imp_dec2flt_parse_parse_number
import Tablet.num_imp_dec2flt_parse_parse_partial_number
import Tablet.P789_T1_RenderedExponentContinuation
import Tablet.P789_T1_RenderedSignificantBounds
import Tablet.P789_T1_SignificandNonwrapping
import Tablet.P789_T1_PartialNumberBranches

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P367_T1_NumberHelperRenderedAccountingA]
theorem P367_T1_NumberHelperRenderedAccountingA
    (s : Slice Std.U8) (x : DecimalLexeme) (d : num.imp.dec2flt.decimal.Decimal)
    (hlen : s.val.length <= 3458764513820540928)
    (hsign : x.leadingSign = none)
    (hvalid : DecimalGoalVocabularyDefault.validNumeric x)
    (hrender : DecimalGoalVocabularyDefault.renderNumeric x =
      DecimalGoalVocabularyDefault.bytes s)
    (hparse : P9_T1_ActualDispatchOperations.parseNumber s = ok (some d)) :
    let digits := x.integerDigits ++
      (match x.fractionDigits with | none => [] | some ds => ds)
    let fractionCount := match x.fractionDigits with | none => 0 | some ds => ds.length
    let explicit := DecimalGoalVocabularyDefault.accumulatedExplicitExponent x
    (d.many_digits = false ->
      d.mantissa.val = digits.foldl (fun a b => 10 * a + b) 0 /\
      d.exponent.val = explicit - (fractionCount : Int)) /\
    (d.many_digits = true ->
      exists consumed remaining : List Nat,
        digits = consumed ++ remaining /\
        d.mantissa.val = consumed.foldl (fun a b => 10 * a + b) 0 /\
        d.exponent.val = explicit - (fractionCount : Int) +
          (remaining.length : Int)) := by
-- BODY
  have hpartial : exists consumed : Std.Usize,
      num.imp.dec2flt.parse.parse_partial_number s = ok (some (d, consumed)) := by
    change num.imp.dec2flt.parse.parse_number s = ok (some d) at hparse
    unfold num.imp.dec2flt.parse.parse_number at hparse
    cases hp : num.imp.dec2flt.parse.parse_partial_number s with
    | fail e => simp [hp] at hparse
    | div => simp [hp] at hparse
    | ok result =>
      cases result with
      | none => simp [hp] at hparse
      | some pair =>
        rcases pair with ⟨d', consumed⟩
        by_cases hc : consumed = s.len
        · simp [hp, hc] at hparse
          subst d'
          exact ⟨consumed, rfl⟩
        · simp [hp, hc] at hparse
  obtain ⟨consumed, hpartial⟩ := hpartial
  obtain ⟨integer, fraction, exponent, hblocks, hi, hf, he, hid, hfd, hed⟩ :=
    P435_T1_RenderedDigitBlocks s.val [] x hsign hvalid (by
      change s.val.map (fun b => b.val) =
        DecimalGoalVocabularyDefault.renderNumeric x ++ [].map (fun b : Std.U8 => b.val)
      simpa using hrender.symm)
  let tail : List Std.U8 := match x.exponent with
    | none => []
    | some (upper, sign, _) => (if upper then 69#u8 else 101#u8) ::
      (match sign with | none => [] | some neg => [if neg then 45#u8 else 43#u8]) ++ exponent
  let point : Bool := x.fractionDigits.isSome
  have hshape : s.val = integer ++
      (if point then 46#u8 :: (fraction ++ tail) else tail) := by
    cases hx : x.fractionDigits <;>
      simpa [point, hx, tail, List.append_assoc] using hblocks
  have hnoFraction : point = false -> fraction = [] := by
    cases hx : x.fractionDigits with
    | none => intro _; simpa [hx] using hf
    | some ds => simp [point, hx]
  have htail : forall b rest, tail = b :: rest -> ¬ (48 <= b.val /\ b.val <= 57) := by
    intro b rest h
    cases hx : x.exponent with
    | none => simp [tail, hx] at h
    | some tuple =>
      rcases tuple with ⟨upper, sign, ds⟩
      have hb : b = (if upper then 69#u8 else 101#u8) := by
        simpa [tail, hx] using (congrArg List.head? h).symm
      rw [hb]
      cases upper <;> norm_num
  have hnoPoint : point = false -> forall b rest, tail = b :: rest -> b ≠ 46#u8 := by
    intro _ b rest h
    cases hx : x.exponent with
    | none => simp [tail, hx] at h
    | some tuple =>
      rcases tuple with ⟨upper, sign, ds⟩
      have hb : b = (if upper then 69#u8 else 101#u8) := by
        simpa [tail, hx] using (congrArg List.head? h).symm
      rw [hb]
      cases upper <;> norm_num
  obtain ⟨intEnd, sigEnd, intWord, word, intCount, fractionCount, total, base,
    hscan, hintEnd, hiOffset, _, hfront, hsigEnd, _, hbase, hadd, htotal⟩ :=
    P435_T1_SignificandScanA s integer fraction tail point hshape hnoFraction
      hid hfd htail hnoPoint hlen
  obtain ⟨hdecodeI, hlengthI, _⟩ :=
    (P435_T1_DigitDecoding integer).2 x.integerDigits hi
  obtain ⟨hdecodeF, hlengthF, _⟩ :=
    (P435_T1_DigitDecoding fraction).2
      (match x.fractionDigits with | none => [] | some ds => ds) hf
  have hbaseExact : base.val =
      -((match x.fractionDigits with | none => 0 | some ds => ds.length) : Int) := by
    rw [hbase, hlengthF]
    cases x.fractionDigits <;> rfl
  have htotalExact : total.val =
      ((x.integerDigits ++ (match x.fractionDigits with | none => [] | some ds => ds)).length : Int) := by
    rw [htotal, hlengthI, hlengthF, List.length_append]
  have hdecodeIF : (integer ++ fraction).map (fun b => b.val - 48) =
      x.integerDigits ++ (match x.fractionDigits with | none => [] | some ds => ds) := by
    simp only [List.map_append, hdecodeI, hdecodeF]
  have hfoldIF : (integer ++ fraction).foldl (fun a b => 10 * a + (b.val - 48)) 0 =
      (x.integerDigits ++ (match x.fractionDigits with | none => [] | some ds => ds)).foldl
        (fun a b => 10 * a + b) 0 := by
    rw [← List.foldl_map, hdecodeIF]
  have hcountBound : integer.length + fraction.length <= s.val.length := by
    rw [hshape]
    cases hp : point with
    | false => simp [hp, hnoFraction hp]
    | true => simp [hp]; omega
  have hpositive : 0 < total.val := by
    have hv : x.integerDigits ≠ [] ∨
        match x.fractionDigits with | none => False | some ds => ds ≠ [] := by
      exact (show _ ∧ _ ∧ _ ∧ _ from hvalid).2.2.1
    rw [htotalExact]
    rcases hv with hv | hv
    · have : 0 < x.integerDigits.length := List.length_pos_of_ne_nil hv
      simp only [List.length_append, Nat.cast_add]
      omega
    · cases hx : x.fractionDigits with
      | none => simp [hx] at hv
      | some ds =>
        have : 0 < ds.length := List.length_pos_of_ne_nil (by simpa [hx] using hv)
        simp only [hx, List.length_append, Nat.cast_add]
        omega
  obtain ⟨explicit, hexact, hexpLo, hexpHi, hcontinuation⟩ :=
    P789_T1_RenderedExponentContinuation x sigEnd exponent hvalid he hed hsigEnd
  have hstop : forall b rest, tail = b :: rest -> b ≠ 48#u8 /\ b ≠ 46#u8 := by
    intro b rest ht
    cases hx : x.exponent with
    | none => simp [tail, hx] at ht
    | some tuple =>
      rcases tuple with ⟨upper, sign, ds⟩
      have hb : b = (if upper then 69#u8 else 101#u8) := by
        simpa [tail, hx] using (congrArg List.head? ht).symm
      rw [hb]
      cases upper <;> norm_num
  obtain ⟨_, hretainedBound, hretryBound⟩ :=
    P789_T1_RenderedSignificantBounds s integer fraction tail point
      hshape hnoFraction hid hfd hstop
  have hbaseLo : -3458764513820540928 <= base.val := by
    rw [hbase]
    omega
  have hbaseHi : base.val <= 3458764513820540928 := by rw [hbase]; omega
  have hcases := P789_T1_PartialNumberBranches s intEnd sigEnd intWord word
    intCount fractionCount total base explicit d consumed hlen hpositive
    (by rw [htotal]; exact_mod_cast hcountBound) hbaseLo hbaseHi hexpLo hexpHi
    hscan hiOffset hfront hadd hcontinuation hpartial
  constructor
  · intro hfalse
    rcases hcases with hkeep | hretry
    · rcases hkeep with ⟨_, hword, hexponent, hsize⟩
      have hbound : (integer ++ fraction).foldl (fun a b => 10 * a + (b.val - 48)) 0
          < 10^19 := hretainedBound (by simpa only [htotal] using hsize)
      have hvalue := P789_T1_SignificandNonwrapping s intEnd sigEnd intWord word
        fractionCount base integer fraction tail point hshape hintEnd hnoFraction
        hid hfd htail hnoPoint (by omega)
        (by rw [U64.max_eq]; norm_num at hbound ⊢; omega) hscan hfront
      exact ⟨by rw [hword, hvalue, hfoldIF], by rw [hexponent, hbaseExact, hexact]; omega⟩
    · simp [hfalse] at hretry
  · intro htrue
    rcases hcases with hkeep | hretry
    · simp [htrue] at hkeep
    · rcases hretry with ⟨_, hpositiveRetry, hretry⟩
      have hlarge : 10^19 <=
          (integer ++ fraction).foldl (fun a b => 10 * a + (b.val - 48)) 0 :=
        hretryBound (by simpa only [htotal] using hpositiveRetry)
      have finish : forall (m : Std.U64) (e : Std.I64) (used rest : List Std.U8),
          num.imp.dec2flt.parse.parse_many_digits s intEnd explicit = ok (m, e) ->
          integer ++ fraction = used ++ rest ->
          m.val = used.foldl (fun a b => 10 * a + (b.val - 48)) 0 ->
          e.val = explicit.val - (fraction.length : Int) + (rest.length : Int) ->
          exists used rest : List Nat,
            x.integerDigits ++ (match x.fractionDigits with | none => [] | some ds => ds) =
              used ++ rest /\
            d.mantissa.val = used.foldl (fun a b => 10 * a + b) 0 /\
            d.exponent.val = DecimalGoalVocabularyDefault.accumulatedExplicitExponent x -
              ((match x.fractionDigits with | none => 0 | some ds => ds.length) : Int) +
              (rest.length : Int) := by
        intro m e used rest hp hpart hm he
        have heq : (m, e) = (d.mantissa, d.exponent) := Result.ok.inj (hp.symm.trans hretry)
        have hm' : m = d.mantissa := congrArg Prod.fst heq
        have he' : e = d.exponent := congrArg Prod.snd heq
        refine ⟨used.map (fun b => b.val - 48), rest.map (fun b => b.val - 48), ?_, ?_, ?_⟩
        · simpa only [List.map_append, hdecodeIF] using
            congrArg (List.map (fun b : Std.U8 => b.val - 48)) hpart
        · simpa only [List.foldl_map, hm'] using hm
        · rw [he'] at he
          cases hx : x.fractionDigits <;>
            simpa only [List.length_map, hexact, hlengthF, hx, List.length_nil, Nat.cast_zero] using he
      cases hp : point with
      | true =>
        obtain ⟨m, e, used, rest, hparseRetry, hpart, hm, _, he⟩ :=
          P367_T1_NumberHelperFractionalRetryA s intEnd explicit integer fraction tail
            (by simpa [hp] using hshape) (by simpa [hp] using hintEnd)
            hid hfd htail hlen hexpLo hexpHi
        exact finish m e used rest hparseRetry hpart hm he
      | false =>
        have hfEmpty := hnoFraction hp
        obtain ⟨m, e, used, rest, hparseRetry, hpart, hm, _, _, he⟩ :=
          P367_T1_NumberHelperIntegralRetryA s intEnd explicit integer tail
            (by simpa [hp] using hshape) (by simpa [hp] using hintEnd)
            hid htail hlen (by simpa [hfEmpty] using (show
              1000000000000000000 <= (integer ++ fraction).foldl
                (fun a b => 10 * a + (b.val - 48)) 0 by norm_num at hlarge; omega))
            hexpLo hexpHi
        exact finish m e used rest hparseRetry (by simpa [hfEmpty] using hpart) hm
          (by simpa [hfEmpty] using he)

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (198)
More program dependencies (156)
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 172 entries
Technical checking records
Local closure summary
{
  "node": "P367_T1_NumberHelperRenderedAccountingA",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "21ba697f46cea933a9fa35a8f213946b7f602394d00340e9604b44152f99db5f",
  "active_statement_hash": "0daba9f1f12a27528f17d6ed3970deca5e18659300d9fe8089222a3bc81c38b7",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "576e6bf9480cfd32f53c309c7ac8fb115c1ba6e413f299ed5ff9fa7c647e6769",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-182",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "P367_T1_NumberHelperFractionalRetryA": "013dace800a7a0d8",
    "P367_T1_NumberHelperIntegralRetryA": "d66d59fc5c9acf83",
    "P435_T1_DigitDecoding": "0b69e5fe3215de46",
    "P435_T1_RenderedDigitBlocks": "53c602364b85e70c",
    "P435_T1_SignificandScanA": "4fa1dfb050285227",
    "P789_T1_PartialNumberBranches": "204c734c972508df",
    "P789_T1_RenderedExponentContinuation": "95b4daf7cd32efb8",
    "P789_T1_RenderedSignificantBounds": "9dd26ae2201fdad4",
    "P789_T1_SignificandNonwrapping": "309218f2d4f0999b"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "c08dd4b4aea4abb4",
    "DecimalGoalVocabularyDefault": "55eb6bce0e8a4131",
    "DecimalLexeme": "250be645520aec86",
    "P435_T1_RenderedDigitBlocks": "55eb6bce0e8a4131",
    "P435_T1_SignificandScanA": "a4e982c388531a6e",
    "P9_T1_ActualDispatchOperations": "d8bf51ebbbf153ac",
    "P9_T1_DispatchOperations": "9b177bb1b830a655",
    "Preamble": "965f15fc36f80938",
    "SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from": "7e7284dc9bc88830",
    "num_imp_dec2flt_decimal_Decimal": "c43614ff2149ecf2",
    "num_imp_dec2flt_parse_parse_many_digits": "50c9ef13b3ed7fe8",
    "num_imp_dec2flt_parse_parse_number": "b8693ec58f01c43c",
    "num_imp_dec2flt_parse_parse_partial_number": "94ed7b2ff71604ef",
    "num_imp_dec2flt_parse_parse_scientific": "447f722d5841d2c5",
    "num_imp_dec2flt_parse_try_parse_digits": "238b4a73cd46fc56"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "differs from current fingerprint",
  "assessment_fingerprint": "sound-v2:48a953df58901431112021670092c768655c9bc68d6da2508e46db596f6ed184",
  "current_fingerprint": "sound-v2:a879a33193539fc8d24921d0a6c18ab7a35218a0b4c8362f398944371ecda313",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}