NumberHelperManyRetryAccounting

Lean checked

Statement

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

Helper · Exact nineteen-position many-digit retry

Suppose a decimal significand has a maximal leading run of ASCII zero bytes Z and a remaining list S^+ whose first byte is nonzero, with |S^+|>19. For either of the two layouts accepted by parse_many_digits--an integral run followed by a nondigit tail, or an integral run, a point, a fractional run, and then a nondigit tail--assume the source length is at most 2^{63}-1 and the extracted retry returns mantissa m and exponent q from an incoming exponent e. Then there are lists A,B with
S^+=AB,\qquad |A|=19, \qquad m=\operatorname{foldl}(a,b\mapsto10a+(b-48),0,A),
and
10^{18}\le m<10^{19}, \qquad q=e+|I|-|Z|-19,
where I is the integral digit list. Thus the statement covers both an all-integral significant prefix and one that crosses the optional point.

Lean statement

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

import Tablet.NumberHelperFractionalManyExact
import Tablet.NumberHelperIntegralManyExact
import Tablet.NumberHelperTryParse19SafeLength
import Tablet.num_imp_dec2flt_parse_MIN_19DIGIT_INT
import Tablet.num_imp_dec2flt_parse_parse_many_digits

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 1000000

namespace dec2flt_model

-- [TABLET NODE: NumberHelperManyRetryAccounting]
theorem NumberHelperManyRetryAccounting :
    (forall (start intEnd : Slice Std.U8) (exp outExp : Std.I64)
      (mantissa : Std.U64) (integer tail zeros significant : List Std.U8),
      start.val = integer ++ tail ->
      intEnd.val = tail ->
      (forall b, List.Mem b integer -> 48 <= b.val /\ b.val <= 57) ->
      (forall b rest, tail = b :: rest ->
        Not (48 <= b.val /\ b.val <= 57)) ->
      start.val.length <= 9223372036854775807 ->
      integer = zeros ++ significant ->
      (forall b, List.Mem b zeros -> b = 48#u8) ->
      (exists b rest, significant = b :: rest /\ b ≠ 48#u8) ->
      19 < significant.length ->
      num.imp.dec2flt.parse.parse_many_digits start intEnd exp =
        ok (mantissa, outExp) ->
      exists headPart restPart,
        significant = headPart ++ restPart /\ headPart.length = 19 /\
        mantissa.val = headPart.foldl
          (fun a b => 10 * a + (b.val - 48)) 0 /\
        1000000000000000000 <= mantissa.val /\
        mantissa.val < 10000000000000000000 /\
        outExp.val = exp.val + integer.length - zeros.length - 19) /\
    (forall (start intEnd : Slice Std.U8) (exp outExp : Std.I64)
      (mantissa : Std.U64)
      (integer fraction tail zeros significant : List Std.U8),
      start.val = integer ++ 46#u8 :: (fraction ++ tail) ->
      intEnd.val = 46#u8 :: (fraction ++ tail) ->
      (forall b, List.Mem b integer -> 48 <= b.val /\ b.val <= 57) ->
      (forall b, List.Mem b fraction -> 48 <= b.val /\ b.val <= 57) ->
      (forall b rest, tail = b :: rest ->
        Not (48 <= b.val /\ b.val <= 57)) ->
      start.val.length <= 9223372036854775807 ->
      integer ++ fraction = zeros ++ significant ->
      (forall b, List.Mem b zeros -> b = 48#u8) ->
      (exists b rest, significant = b :: rest /\ b ≠ 48#u8) ->
      19 < significant.length ->
      num.imp.dec2flt.parse.parse_many_digits start intEnd exp =
        ok (mantissa, outExp) ->
      exists headPart restPart,
        significant = headPart ++ restPart /\ headPart.length = 19 /\
        mantissa.val = headPart.foldl
          (fun a b => 10 * a + (b.val - 48)) 0 /\
        1000000000000000000 <= mantissa.val /\
        mantissa.val < 10000000000000000000 /\
        outExp.val = exp.val + integer.length - zeros.length - 19) := 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 G(a,b)=10a+(b-48). We first record the elementary prefix calculation used in both layouts. If a decimal digit list is ZS^+, every byte of Z is 48, S^+ begins with a nonzero digit, and a prefix C has 10^{18}\le\operatorname{foldl}(G,0,C)<10^{19}, then C=ZA for a prefix A of S^+ of length exactly 19. Indeed, folding Z leaves zero. Fewer than nineteen digits after the first nonzero one give a value below 10^{18}, by induction from the bound 0\le b-48\le9. More than nineteen give at least 10^{19}: after the first digit the usual lower fold bound is (b-48)10^k, and b-48\ge1. Prefix cancellation therefore gives S^+=AB and the displayed fold formula.
For the integral layout, apply NumberHelperTryParse19SafeLength to the integral digit run with initial accumulator zero. If its result were below 10^{18}, its low-result clause would say that no digit remains, so the result would be the fold of the whole run. But the maximal-zero decomposition and |S^+|>19 make that whole fold at least 10^{19}, a contradiction. Hence the first retry result is at least 10^{18}; the same helper bounds it by 9{,}999{,}999{,}999{,}999{,}999{,}999<10^{19}. Unfolding num imp dec2flt parse parse many digits shows that its high branch retains this word as the returned mantissa. Apply the prefix calculation to obtain A,B and the exact fold. Next apply NumberHelperIntegralManyExact. Its partition has the same returned mantissa and hence satisfies the same prefix calculation, so its remaining list has length |S^+|-19. Its exponent equation is q=e+|B|=e+|I|-|Z|-19.
For the fractional layout, apply NumberHelperTryParse19SafeLength first to I. If this scan reaches 10^{18}, the extracted high branch returns that word, and the preceding argument applies unchanged to the prefix of IF. Otherwise the helper's low-result clause says it consumed all of I. The extracted range-from-one operation then removes the known point byte, and a second application of the same helper scans F starting from the exact fold of I. If this second result were below 10^{18}, its low-result clause would consume all of F and identify the word with the fold of IF, again contradicting the |S^+|>19 lower bound. Its upper bound is again 10^{19}-1. Unfolding the retry identifies that second word as the returned mantissa. Thus in either fractional subcase the prefix calculation gives exactly nineteen positions of S^+, irrespective of whether they lie before or after the point.
Finally apply NumberHelperFractionalManyExact. Its statement gives q=e-|F|+|R| for the unconsumed suffix R of IF. The prefix calculation gives |R|=|I|+|F|-|Z|-19, and substitution yields q=e+|I|-|Z|-19. All offset casts, negations, and checked additions used in these applications are already premises and conclusions of the cited exact retry theorems under the common 2^{63}-1 source-length bound; the assumed successful retry rules out their failure branches. This proves both conjuncts.

Lean proof


  let digitStep : Nat -> Std.U8 -> Nat :=
    fun a b => 10 * a + (b.val - 48)
  have zero_fold : forall (zeros : List Std.U8),
      (forall b, List.Mem b zeros -> b = 48#u8) ->
      forall a, zeros.foldl digitStep a = a * 10 ^ zeros.length := by
    intro zeros hzeros
    induction zeros with
    | nil => intro a; simp
    | cons b bs ih =>
      intro a
      have hb := hzeros b List.mem_cons_self
      have hbs : forall c, List.Mem c bs -> c = 48#u8 := by
        intro c hc
        exact hzeros c (List.mem_cons_of_mem b hc)
      simp [hb, digitStep, ih hbs, Nat.pow_succ]
      ring
  have fold_upper : forall (bs : List Std.U8) (a : Nat),
      (forall b, List.Mem b bs -> 48 <= b.val /\ b.val <= 57) ->
      bs.foldl digitStep a < (a + 1) * 10 ^ bs.length := by
    intro bs
    induction bs with
    | nil => intro a h; simp
    | cons b bs ih =>
      intro a h
      have hb := h b List.mem_cons_self
      have hbs : forall c, List.Mem c bs ->
          48 <= c.val /\ c.val <= 57 := by
        intro c hc
        exact h c (List.mem_cons_of_mem b hc)
      simp only [List.foldl_cons, List.length_cons, Nat.pow_succ]
      have hrec := ih (digitStep a b) hbs
      have hcoeff : digitStep a b + 1 <= 10 * (a + 1) := by
        simp only [digitStep]
        omega
      have hmul := Nat.mul_le_mul_right (10 ^ bs.length) hcoeff
      exact hrec.trans_le (by
        simpa [Nat.mul_assoc, Nat.mul_comm, Nat.mul_left_comm] using hmul)
  have fold_lower : forall (bs : List Std.U8) (a : Nat),
      a * 10 ^ bs.length <= bs.foldl digitStep a := by
    intro bs
    induction bs with
    | nil => intro a; simp
    | cons b bs ih =>
      intro a
      simp only [List.foldl_cons, List.length_cons, Nat.pow_succ]
      calc
        a * (10 ^ bs.length * 10) = (10 * a) * 10 ^ bs.length := by ring
        _ <= digitStep a b * 10 ^ bs.length := by
          apply Nat.mul_le_mul_right
          simp [digitStep]
        _ <= bs.foldl digitStep (digitStep a b) := ih _
  have partition_accounting : forall
      (all zeros significant consumed remaining : List Std.U8) (m : Nat),
      (forall b, List.Mem b all -> 48 <= b.val /\ b.val <= 57) ->
      all = zeros ++ significant ->
      (forall b, List.Mem b zeros -> b = 48#u8) ->
      (exists b rest, significant = b :: rest /\ b ≠ 48#u8) ->
      19 < significant.length ->
      all = consumed ++ remaining ->
      m = consumed.foldl digitStep 0 ->
      1000000000000000000 <= m ->
      m <= 9999999999999999999 ->
      exists headPart restPart,
        significant = headPart ++ restPart /\ headPart.length = 19 /\
        m = headPart.foldl digitStep 0 /\ remaining = restPart := by
    intro all zeros significant consumed remaining m hdigits hall hzeros
      hsignificant hsiglen hpartition hm hmLower hmUpper
    rcases hsignificant with ⟨first, sigRest, hsig, hfirst⟩
    have hfirstDigit : 48 <= first.val /\ first.val <= 57 := by
      apply hdigits first
      rw [hall, hsig]
      exact List.mem_append_right zeros List.mem_cons_self
    have hfirstPos : 1 <= first.val - 48 := by
      have hne : first.val ≠ 48 := by
        intro heq
        apply hfirst
        exact UScalar.eq_of_val_eq heq
      omega
    have hzerosLenLe : zeros.length <= consumed.length := by
      by_contra hnot
      have hconsumedTake : consumed = zeros.take consumed.length := by
        have ht := congrArg (List.take consumed.length) hpartition
        rw [List.take_left' (by omega)] at ht
        rw [hall, List.take_append_of_le_length (by omega)] at ht
        exact ht.symm
      have hconsumedZeros : forall b, List.Mem b consumed -> b = 48#u8 := by
        intro b hb
        rw [hconsumedTake] at hb
        exact hzeros b (List.mem_of_mem_take hb)
      have hzeroValue := zero_fold consumed hconsumedZeros 0
      rw [hm, hzeroValue] at hmLower
      norm_num at hmLower
    have hprefixExists : exists headPart,
        consumed = zeros ++ headPart /\ significant = headPart ++ remaining := by
      have ht := congrArg (List.drop zeros.length) hpartition
      rw [hall, List.drop_left] at ht
      have hconsumedSplit := (List.take_append_drop zeros.length consumed).symm
      refine ⟨consumed.drop zeros.length, ?_, ?_⟩
      · have hp : List.take zeros.length consumed = zeros := by
          have hp0 := congrArg (List.take zeros.length) hpartition
          rw [List.take_append_of_le_length hzerosLenLe, hall,
            List.take_left] at hp0
          exact hp0.symm
        calc
          consumed = List.take zeros.length consumed ++
              List.drop zeros.length consumed := hconsumedSplit
          _ = zeros ++ List.drop zeros.length consumed := by rw [hp]
      · simpa [List.drop_append_of_le_length hzerosLenLe] using ht
    obtain ⟨headPart, hconsumed, hsigPartition⟩ := hprefixExists
    have hprefixDigits : forall b, List.Mem b headPart ->
        48 <= b.val /\ b.val <= 57 := by
      intro b hb
      apply hdigits b
      rw [hall, hsigPartition]
      exact List.mem_append_right zeros (List.mem_append_left remaining hb)
    have hprefixValue : m = headPart.foldl digitStep 0 := by
      rw [hm, hconsumed, List.foldl_append, zero_fold zeros hzeros]
      norm_num
    have hprefixAtLeast : 19 <= headPart.length := by
      have hu := fold_upper headPart 0 hprefixDigits
      rw [← hprefixValue] at hu
      by_contra hnot
      have hp : 10 ^ headPart.length <= 10 ^ 18 :=
        Nat.pow_le_pow_right (by norm_num) (by omega)
      norm_num at hmLower
      omega
    have hprefixAtMost : headPart.length <= 19 := by
      by_contra hnot
      have hfirstPrefix : exists rest, headPart = first :: rest := by
        rw [hsig] at hsigPartition
        cases headPart with
        | nil => simp at hprefixAtLeast
        | cons b bs =>
          simp only [List.cons_append, List.cons.injEq] at hsigPartition
          exact ⟨bs, by rw [hsigPartition.1]⟩
      obtain ⟨prefixRest, hprefix⟩ := hfirstPrefix
      have hlower := fold_lower prefixRest (first.val - 48)
      rw [hprefix, List.foldl_cons] at hprefixValue
      have hrestLen : 19 <= prefixRest.length := by
        rw [hprefix, List.length_cons] at hnot
        omega
      have hp : 10000000000000000000 <= 10 ^ prefixRest.length := by
        have := Nat.pow_le_pow_right (by norm_num : 0 < 10) hrestLen
        norm_num at this ⊢
        exact this
      have hstep : 10 ^ prefixRest.length <=
          (first.val - 48) * 10 ^ prefixRest.length := by
        nlinarith
      have : 10000000000000000000 <= m := by
        rw [hprefixValue]
        exact hp.trans (hstep.trans (by
          simpa [digitStep] using hlower))
      omega
    exact ⟨headPart, remaining, hsigPartition, by omega, hprefixValue, rfl⟩
  constructor
  · intro start intEnd exp outExp mantissa integer tail zeros significant
      hstart hintEnd hinteger htail hlen hinter hzeros hsignificant hsiglen hrun
    have hfullThreshold : 1000000000000000000 <=
        integer.foldl digitStep 0 := by
      rcases hsignificant with ⟨first, sigRest, hsig, hfirst⟩
      have hfirstDigit := hinteger first (by
        rw [hinter, hsig]
        exact List.mem_append_right zeros List.mem_cons_self)
      have hfirstPos : 1 <= first.val - 48 := by
        have hne : first.val ≠ 48 := by
          intro heq
          apply hfirst
          exact UScalar.eq_of_val_eq heq
        omega
      have hlower := fold_lower sigRest (first.val - 48)
      have hpow : 1000000000000000000 <= 10 ^ sigRest.length := by
        have hrestLen : 18 <= sigRest.length := by
          rw [hsig, List.length_cons] at hsiglen
          omega
        have := Nat.pow_le_pow_right (by norm_num : 0 < 10) hrestLen
        norm_num at this ⊢
        exact this
      rw [hinter, List.foldl_append, zero_fold zeros hzeros, hsig,
        List.foldl_cons]
      norm_num
      change 1000000000000000000 <=
        sigRest.foldl digitStep (digitStep 0 first)
      have hstep : 10 ^ sigRest.length <=
          (first.val - 48) * 10 ^ sigRest.length := by
        nlinarith
      simpa [digitStep] using hpow.trans (hstep.trans hlower)
    obtain ⟨scanRest, parsed, scanRemaining, scanConsumed, hscan,
        hscanPartition, hscanRest, hscanValue, hscanDigits, hscanLen,
        hparsedUpper, hscanLow, hscanSharp⟩ :=
      NumberHelperTryParse19SafeLength start 0#u64 integer tail hstart
        hinteger htail (by norm_num)
    have hparsedLower : 1000000000000000000 <= parsed.val := by
      by_contra hnot
      have hlow : parsed.val < 1000000000000000000 := by omega
      have hremainingNil := hscanLow hlow
      have hconsumedEq : scanConsumed = integer := by
        rw [hremainingNil] at hscanPartition
        simpa using hscanPartition.symm
      rw [hscanValue, hconsumedEq] at hlow
      exact (not_lt_of_ge hfullThreshold) (by simpa [digitStep] using hlow)
    have hmantissaEq : mantissa = parsed := by
      unfold num.imp.dec2flt.parse.parse_many_digits at hrun
      rw [hscan] at hrun
      have hgeVal : num.imp.dec2flt.parse.MIN_19DIGIT_INT.val <= parsed.val := by
        simpa [num.imp.dec2flt.parse.MIN_19DIGIT_INT.eq_def] using hparsedLower
      simp [hgeVal] at hrun
      cases hoff :
          SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
            intEnd scanRest with
      | fail err => simp [hoff] at hrun
      | div => simp [hoff] at hrun
      | ok offset =>
        simp [hoff] at hrun
        cases hcast : lift (IScalar.cast .I64 offset) with
        | fail err => simp [hcast] at hrun
        | div => simp [hcast] at hrun
        | ok base =>
          simp [hcast] at hrun
          cases hadd : base + exp with
          | fail err => simp [hadd] at hrun
          | div => simp [hadd] at hrun
          | ok result =>
            simp [hadd] at hrun
            exact hrun.1.symm
    subst parsed
    obtain ⟨consumed, remaining, hpartition, hmValue, hout⟩ :=
      NumberHelperIntegralManyExact start intEnd exp outExp mantissa integer
        tail hstart hintEnd hinteger htail hlen (by
          simpa [digitStep] using hfullThreshold) hrun
    obtain ⟨headPart, restPart, hsigPartition, hprefixLen, hmPrefix,
        hrestEq⟩ :=
      partition_accounting integer zeros significant consumed remaining
        mantissa.val hinteger hinter hzeros hsignificant hsiglen hpartition
        (by simpa [digitStep] using hmValue) hparsedLower hparsedUpper
    refine ⟨headPart, restPart, hsigPartition, hprefixLen,
      by simpa [digitStep] using hmPrefix, hparsedLower, ?_, ?_⟩
    · omega
    · have hlength := congrArg List.length hsigPartition
      have hwhole := congrArg List.length hinter
      simp only [List.length_append] at hlength hwhole
      rw [hout, hrestEq]
      omega
  · intro start intEnd exp outExp mantissa integer fraction tail zeros
      significant hstart hintEnd hinteger hfraction htail hlen hinter hzeros
      hsignificant hsiglen hrun
    have hallDigits : forall b, List.Mem b (integer ++ fraction) ->
        48 <= b.val /\ b.val <= 57 := by
      intro b hb
      rcases List.mem_append.mp hb with hb | hb
      · exact hinteger b hb
      · exact hfraction b hb
    have hfullThreshold : 1000000000000000000 <=
        (integer ++ fraction).foldl digitStep 0 := by
      rcases hsignificant with ⟨first, sigRest, hsig, hfirst⟩
      have hfirstDigit := hallDigits first (by
        rw [hinter, hsig]
        exact List.mem_append_right zeros List.mem_cons_self)
      have hfirstPos : 1 <= first.val - 48 := by
        have hne : first.val ≠ 48 := by
          intro heq
          apply hfirst
          exact UScalar.eq_of_val_eq heq
        omega
      have hlower := fold_lower sigRest (first.val - 48)
      have hpow : 1000000000000000000 <= 10 ^ sigRest.length := by
        have hrestLen : 18 <= sigRest.length := by
          rw [hsig, List.length_cons] at hsiglen
          omega
        have := Nat.pow_le_pow_right (by norm_num : 0 < 10) hrestLen
        norm_num at this ⊢
        exact this
      rw [hinter, List.foldl_append, zero_fold zeros hzeros, hsig,
        List.foldl_cons]
      norm_num
      change 1000000000000000000 <=
        sigRest.foldl digitStep (digitStep 0 first)
      have hstep : 10 ^ sigRest.length <=
          (first.val - 48) * 10 ^ sigRest.length := by
        nlinarith
      simpa [digitStep] using hpow.trans (hstep.trans hlower)
    have hintTailNotDigit : forall b rest, intEnd.val = b :: rest ->
        Not (48 <= b.val /\ b.val <= 57) := by
      intro b rest hb
      rw [hintEnd] at hb
      simp only [List.cons.injEq] at hb
      rcases hb with ⟨rfl, _⟩
      norm_num
    have hstartInt : start.val = integer ++ intEnd.val := by
      rw [hstart, hintEnd]
    obtain ⟨scanRest1, parsed1, remaining1, consumed1, hscan1,
        hpartition1, hrest1, hvalue1, hremaining1, hremainingLen1,
        hparsed1Upper, hlow1, hsharp1⟩ :=
      NumberHelperTryParse19SafeLength start 0#u64 integer intEnd.val
        hstartInt hinteger hintTailNotDigit (by norm_num)
    by_cases hfirstHigh : 1000000000000000000 <= parsed1.val
    · have hmantissaEq : mantissa = parsed1 := by
        unfold num.imp.dec2flt.parse.parse_many_digits at hrun
        rw [hscan1] at hrun
        have hgeVal : num.imp.dec2flt.parse.MIN_19DIGIT_INT.val <= parsed1.val := by
          simpa [num.imp.dec2flt.parse.MIN_19DIGIT_INT.eq_def] using hfirstHigh
        simp [hgeVal] at hrun
        cases hoff :
            SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
              intEnd scanRest1 with
        | fail err => simp [hoff] at hrun
        | div => simp [hoff] at hrun
        | ok offset =>
          simp [hoff] at hrun
          cases hcast : lift (IScalar.cast .I64 offset) with
          | fail err => simp [hcast] at hrun
          | div => simp [hcast] at hrun
          | ok base =>
            simp [hcast] at hrun
            cases hadd : base + exp with
            | fail err => simp [hadd] at hrun
            | div => simp [hadd] at hrun
            | ok result =>
              simp [hadd] at hrun
              exact hrun.1.symm
      subst parsed1
      obtain ⟨consumed, remaining, hpartition, hmValue, hout⟩ :=
        NumberHelperFractionalManyExact start intEnd exp outExp mantissa
          integer fraction tail hstart hintEnd hinteger hfraction htail hlen
          (by simpa [digitStep] using hfullThreshold) hrun
      obtain ⟨headPart, restPart, hsigPartition, hprefixLen, hmPrefix,
          hrestEq⟩ :=
        partition_accounting (integer ++ fraction) zeros significant consumed
          remaining mantissa.val hallDigits hinter hzeros hsignificant hsiglen
          hpartition (by simpa [digitStep] using hmValue) hfirstHigh
          hparsed1Upper
      refine ⟨headPart, restPart, hsigPartition, hprefixLen,
        by simpa [digitStep] using hmPrefix, hfirstHigh, ?_, ?_⟩
      · omega
      · have hlength := congrArg List.length hsigPartition
        have hwhole := congrArg List.length hinter
        simp only [List.length_append] at hlength hwhole
        rw [hout, hrestEq]
        omega
    · have hfirstLow : parsed1.val < 1000000000000000000 := by omega
      have hremaining1Nil := hlow1 hfirstLow
      have hconsumed1Eq : consumed1 = integer := by
        rw [hremaining1Nil] at hpartition1
        simpa using hpartition1.symm
      have hscanRest1Val : scanRest1.val = intEnd.val := by
        simpa [hremaining1Nil] using hrest1
      have hscanRest1Eq : scanRest1 = intEnd :=
        (Slice.eq_iff _ _).2 hscanRest1Val
      have htailProp : (fraction ++ tail).length <= Usize.max := by
        have hp := intEnd.property
        rw [hintEnd] at hp
        simp only [List.length_cons] at hp
        omega
      let afterPoint : Slice Std.U8 := ⟨fraction ++ tail, htailProp⟩
      have hindex : core.slice.index.SliceIndexRangeFromUsizeSlice.index
          { start := 1#usize } intEnd = ok afterPoint := by
        simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
          Slice.drop, Slice.length, hintEnd, afterPoint]
      obtain ⟨scanRest2, parsed2, remaining2, consumed2, hscan2,
          hpartition2, hrest2, hvalue2, hremaining2, hremainingLen2,
          hparsed2Upper, hlow2, hsharp2⟩ :=
        NumberHelperTryParse19SafeLength afterPoint parsed1 fraction tail
          (by rfl) hfraction htail hparsed1Upper
      have hparsed1Value : parsed1.val = integer.foldl digitStep 0 := by
        rw [hvalue1, hconsumed1Eq]
        simp [digitStep]
      have hparsed2Lower : 1000000000000000000 <= parsed2.val := by
        by_contra hnot
        have hparsed2Low : parsed2.val < 1000000000000000000 := by omega
        have hremaining2Nil := hlow2 hparsed2Low
        have hconsumed2Eq : consumed2 = fraction := by
          rw [hremaining2Nil] at hpartition2
          simpa using hpartition2.symm
        have hfullValue : parsed2.val =
            (integer ++ fraction).foldl digitStep 0 := by
          rw [List.foldl_append, ← hparsed1Value, hvalue2, hconsumed2Eq]
        rw [hfullValue] at hparsed2Low
        exact (not_lt_of_ge hfullThreshold) hparsed2Low
      have hmantissaEq : mantissa = parsed2 := by
        unfold num.imp.dec2flt.parse.parse_many_digits at hrun
        rw [hscan1] at hrun
        have hlt : Not
            (num.imp.dec2flt.parse.MIN_19DIGIT_INT.val <= parsed1.val) := by
          simpa [num.imp.dec2flt.parse.MIN_19DIGIT_INT.eq_def]
            using hfirstLow
        rw [hscanRest1Eq] at hrun
        simp [hlt, hindex, hscan2] at hrun
        cases hoff :
            SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
              scanRest2 afterPoint with
        | fail err => simp [hoff] at hrun
        | div => simp [hoff] at hrun
        | ok offset =>
          simp [hoff] at hrun
          cases hneg : HNeg.hNeg offset with
          | fail err => simp [hneg] at hrun
          | div => simp [hneg] at hrun
          | ok negative =>
            simp [hneg] at hrun
            cases hcast : lift (IScalar.cast .I64 negative) with
            | fail err => simp [hcast] at hrun
            | div => simp [hcast] at hrun
            | ok base =>
              simp [hcast] at hrun
              cases hadd : base + exp with
              | fail err => simp [hadd] at hrun
              | div => simp [hadd] at hrun
              | ok result =>
                simp [hadd] at hrun
                exact hrun.1.symm
      subst parsed2
      obtain ⟨consumed, remaining, hpartition, hmValue, hout⟩ :=
        NumberHelperFractionalManyExact start intEnd exp outExp mantissa
          integer fraction tail hstart hintEnd hinteger hfraction htail hlen
          (by simpa [digitStep] using hfullThreshold) hrun
      obtain ⟨headPart, restPart, hsigPartition, hprefixLen, hmPrefix,
          hrestEq⟩ :=
        partition_accounting (integer ++ fraction) zeros significant consumed
          remaining mantissa.val hallDigits hinter hzeros hsignificant hsiglen
          hpartition (by simpa [digitStep] using hmValue) hparsed2Lower
          hparsed2Upper
      refine ⟨headPart, restPart, hsigPartition, hprefixLen,
        by simpa [digitStep] using hmPrefix, hparsed2Lower, ?_, ?_⟩
      · omega
      · have hlength := congrArg List.length hsigPartition
        have hwhole := congrArg List.length hinter
        simp only [List.length_append] at hlength hwhole
        rw [hout, hrestEq]
        omega

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.NumberHelperFractionalManyExact
import Tablet.NumberHelperIntegralManyExact
import Tablet.NumberHelperTryParse19SafeLength
import Tablet.num_imp_dec2flt_parse_MIN_19DIGIT_INT
import Tablet.num_imp_dec2flt_parse_parse_many_digits

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 1000000

namespace dec2flt_model

-- [TABLET NODE: NumberHelperManyRetryAccounting]
theorem NumberHelperManyRetryAccounting :
    (forall (start intEnd : Slice Std.U8) (exp outExp : Std.I64)
      (mantissa : Std.U64) (integer tail zeros significant : List Std.U8),
      start.val = integer ++ tail ->
      intEnd.val = tail ->
      (forall b, List.Mem b integer -> 48 <= b.val /\ b.val <= 57) ->
      (forall b rest, tail = b :: rest ->
        Not (48 <= b.val /\ b.val <= 57)) ->
      start.val.length <= 9223372036854775807 ->
      integer = zeros ++ significant ->
      (forall b, List.Mem b zeros -> b = 48#u8) ->
      (exists b rest, significant = b :: rest /\ b ≠ 48#u8) ->
      19 < significant.length ->
      num.imp.dec2flt.parse.parse_many_digits start intEnd exp =
        ok (mantissa, outExp) ->
      exists headPart restPart,
        significant = headPart ++ restPart /\ headPart.length = 19 /\
        mantissa.val = headPart.foldl
          (fun a b => 10 * a + (b.val - 48)) 0 /\
        1000000000000000000 <= mantissa.val /\
        mantissa.val < 10000000000000000000 /\
        outExp.val = exp.val + integer.length - zeros.length - 19) /\
    (forall (start intEnd : Slice Std.U8) (exp outExp : Std.I64)
      (mantissa : Std.U64)
      (integer fraction tail zeros significant : List Std.U8),
      start.val = integer ++ 46#u8 :: (fraction ++ tail) ->
      intEnd.val = 46#u8 :: (fraction ++ tail) ->
      (forall b, List.Mem b integer -> 48 <= b.val /\ b.val <= 57) ->
      (forall b, List.Mem b fraction -> 48 <= b.val /\ b.val <= 57) ->
      (forall b rest, tail = b :: rest ->
        Not (48 <= b.val /\ b.val <= 57)) ->
      start.val.length <= 9223372036854775807 ->
      integer ++ fraction = zeros ++ significant ->
      (forall b, List.Mem b zeros -> b = 48#u8) ->
      (exists b rest, significant = b :: rest /\ b ≠ 48#u8) ->
      19 < significant.length ->
      num.imp.dec2flt.parse.parse_many_digits start intEnd exp =
        ok (mantissa, outExp) ->
      exists headPart restPart,
        significant = headPart ++ restPart /\ headPart.length = 19 /\
        mantissa.val = headPart.foldl
          (fun a b => 10 * a + (b.val - 48)) 0 /\
        1000000000000000000 <= mantissa.val /\
        mantissa.val < 10000000000000000000 /\
        outExp.val = exp.val + integer.length - zeros.length - 19) := by
-- BODY
  let digitStep : Nat -> Std.U8 -> Nat :=
    fun a b => 10 * a + (b.val - 48)
  have zero_fold : forall (zeros : List Std.U8),
      (forall b, List.Mem b zeros -> b = 48#u8) ->
      forall a, zeros.foldl digitStep a = a * 10 ^ zeros.length := by
    intro zeros hzeros
    induction zeros with
    | nil => intro a; simp
    | cons b bs ih =>
      intro a
      have hb := hzeros b List.mem_cons_self
      have hbs : forall c, List.Mem c bs -> c = 48#u8 := by
        intro c hc
        exact hzeros c (List.mem_cons_of_mem b hc)
      simp [hb, digitStep, ih hbs, Nat.pow_succ]
      ring
  have fold_upper : forall (bs : List Std.U8) (a : Nat),
      (forall b, List.Mem b bs -> 48 <= b.val /\ b.val <= 57) ->
      bs.foldl digitStep a < (a + 1) * 10 ^ bs.length := by
    intro bs
    induction bs with
    | nil => intro a h; simp
    | cons b bs ih =>
      intro a h
      have hb := h b List.mem_cons_self
      have hbs : forall c, List.Mem c bs ->
          48 <= c.val /\ c.val <= 57 := by
        intro c hc
        exact h c (List.mem_cons_of_mem b hc)
      simp only [List.foldl_cons, List.length_cons, Nat.pow_succ]
      have hrec := ih (digitStep a b) hbs
      have hcoeff : digitStep a b + 1 <= 10 * (a + 1) := by
        simp only [digitStep]
        omega
      have hmul := Nat.mul_le_mul_right (10 ^ bs.length) hcoeff
      exact hrec.trans_le (by
        simpa [Nat.mul_assoc, Nat.mul_comm, Nat.mul_left_comm] using hmul)
  have fold_lower : forall (bs : List Std.U8) (a : Nat),
      a * 10 ^ bs.length <= bs.foldl digitStep a := by
    intro bs
    induction bs with
    | nil => intro a; simp
    | cons b bs ih =>
      intro a
      simp only [List.foldl_cons, List.length_cons, Nat.pow_succ]
      calc
        a * (10 ^ bs.length * 10) = (10 * a) * 10 ^ bs.length := by ring
        _ <= digitStep a b * 10 ^ bs.length := by
          apply Nat.mul_le_mul_right
          simp [digitStep]
        _ <= bs.foldl digitStep (digitStep a b) := ih _
  have partition_accounting : forall
      (all zeros significant consumed remaining : List Std.U8) (m : Nat),
      (forall b, List.Mem b all -> 48 <= b.val /\ b.val <= 57) ->
      all = zeros ++ significant ->
      (forall b, List.Mem b zeros -> b = 48#u8) ->
      (exists b rest, significant = b :: rest /\ b ≠ 48#u8) ->
      19 < significant.length ->
      all = consumed ++ remaining ->
      m = consumed.foldl digitStep 0 ->
      1000000000000000000 <= m ->
      m <= 9999999999999999999 ->
      exists headPart restPart,
        significant = headPart ++ restPart /\ headPart.length = 19 /\
        m = headPart.foldl digitStep 0 /\ remaining = restPart := by
    intro all zeros significant consumed remaining m hdigits hall hzeros
      hsignificant hsiglen hpartition hm hmLower hmUpper
    rcases hsignificant with ⟨first, sigRest, hsig, hfirst⟩
    have hfirstDigit : 48 <= first.val /\ first.val <= 57 := by
      apply hdigits first
      rw [hall, hsig]
      exact List.mem_append_right zeros List.mem_cons_self
    have hfirstPos : 1 <= first.val - 48 := by
      have hne : first.val ≠ 48 := by
        intro heq
        apply hfirst
        exact UScalar.eq_of_val_eq heq
      omega
    have hzerosLenLe : zeros.length <= consumed.length := by
      by_contra hnot
      have hconsumedTake : consumed = zeros.take consumed.length := by
        have ht := congrArg (List.take consumed.length) hpartition
        rw [List.take_left' (by omega)] at ht
        rw [hall, List.take_append_of_le_length (by omega)] at ht
        exact ht.symm
      have hconsumedZeros : forall b, List.Mem b consumed -> b = 48#u8 := by
        intro b hb
        rw [hconsumedTake] at hb
        exact hzeros b (List.mem_of_mem_take hb)
      have hzeroValue := zero_fold consumed hconsumedZeros 0
      rw [hm, hzeroValue] at hmLower
      norm_num at hmLower
    have hprefixExists : exists headPart,
        consumed = zeros ++ headPart /\ significant = headPart ++ remaining := by
      have ht := congrArg (List.drop zeros.length) hpartition
      rw [hall, List.drop_left] at ht
      have hconsumedSplit := (List.take_append_drop zeros.length consumed).symm
      refine ⟨consumed.drop zeros.length, ?_, ?_⟩
      · have hp : List.take zeros.length consumed = zeros := by
          have hp0 := congrArg (List.take zeros.length) hpartition
          rw [List.take_append_of_le_length hzerosLenLe, hall,
            List.take_left] at hp0
          exact hp0.symm
        calc
          consumed = List.take zeros.length consumed ++
              List.drop zeros.length consumed := hconsumedSplit
          _ = zeros ++ List.drop zeros.length consumed := by rw [hp]
      · simpa [List.drop_append_of_le_length hzerosLenLe] using ht
    obtain ⟨headPart, hconsumed, hsigPartition⟩ := hprefixExists
    have hprefixDigits : forall b, List.Mem b headPart ->
        48 <= b.val /\ b.val <= 57 := by
      intro b hb
      apply hdigits b
      rw [hall, hsigPartition]
      exact List.mem_append_right zeros (List.mem_append_left remaining hb)
    have hprefixValue : m = headPart.foldl digitStep 0 := by
      rw [hm, hconsumed, List.foldl_append, zero_fold zeros hzeros]
      norm_num
    have hprefixAtLeast : 19 <= headPart.length := by
      have hu := fold_upper headPart 0 hprefixDigits
      rw [← hprefixValue] at hu
      by_contra hnot
      have hp : 10 ^ headPart.length <= 10 ^ 18 :=
        Nat.pow_le_pow_right (by norm_num) (by omega)
      norm_num at hmLower
      omega
    have hprefixAtMost : headPart.length <= 19 := by
      by_contra hnot
      have hfirstPrefix : exists rest, headPart = first :: rest := by
        rw [hsig] at hsigPartition
        cases headPart with
        | nil => simp at hprefixAtLeast
        | cons b bs =>
          simp only [List.cons_append, List.cons.injEq] at hsigPartition
          exact ⟨bs, by rw [hsigPartition.1]⟩
      obtain ⟨prefixRest, hprefix⟩ := hfirstPrefix
      have hlower := fold_lower prefixRest (first.val - 48)
      rw [hprefix, List.foldl_cons] at hprefixValue
      have hrestLen : 19 <= prefixRest.length := by
        rw [hprefix, List.length_cons] at hnot
        omega
      have hp : 10000000000000000000 <= 10 ^ prefixRest.length := by
        have := Nat.pow_le_pow_right (by norm_num : 0 < 10) hrestLen
        norm_num at this ⊢
        exact this
      have hstep : 10 ^ prefixRest.length <=
          (first.val - 48) * 10 ^ prefixRest.length := by
        nlinarith
      have : 10000000000000000000 <= m := by
        rw [hprefixValue]
        exact hp.trans (hstep.trans (by
          simpa [digitStep] using hlower))
      omega
    exact ⟨headPart, remaining, hsigPartition, by omega, hprefixValue, rfl⟩
  constructor
  · intro start intEnd exp outExp mantissa integer tail zeros significant
      hstart hintEnd hinteger htail hlen hinter hzeros hsignificant hsiglen hrun
    have hfullThreshold : 1000000000000000000 <=
        integer.foldl digitStep 0 := by
      rcases hsignificant with ⟨first, sigRest, hsig, hfirst⟩
      have hfirstDigit := hinteger first (by
        rw [hinter, hsig]
        exact List.mem_append_right zeros List.mem_cons_self)
      have hfirstPos : 1 <= first.val - 48 := by
        have hne : first.val ≠ 48 := by
          intro heq
          apply hfirst
          exact UScalar.eq_of_val_eq heq
        omega
      have hlower := fold_lower sigRest (first.val - 48)
      have hpow : 1000000000000000000 <= 10 ^ sigRest.length := by
        have hrestLen : 18 <= sigRest.length := by
          rw [hsig, List.length_cons] at hsiglen
          omega
        have := Nat.pow_le_pow_right (by norm_num : 0 < 10) hrestLen
        norm_num at this ⊢
        exact this
      rw [hinter, List.foldl_append, zero_fold zeros hzeros, hsig,
        List.foldl_cons]
      norm_num
      change 1000000000000000000 <=
        sigRest.foldl digitStep (digitStep 0 first)
      have hstep : 10 ^ sigRest.length <=
          (first.val - 48) * 10 ^ sigRest.length := by
        nlinarith
      simpa [digitStep] using hpow.trans (hstep.trans hlower)
    obtain ⟨scanRest, parsed, scanRemaining, scanConsumed, hscan,
        hscanPartition, hscanRest, hscanValue, hscanDigits, hscanLen,
        hparsedUpper, hscanLow, hscanSharp⟩ :=
      NumberHelperTryParse19SafeLength start 0#u64 integer tail hstart
        hinteger htail (by norm_num)
    have hparsedLower : 1000000000000000000 <= parsed.val := by
      by_contra hnot
      have hlow : parsed.val < 1000000000000000000 := by omega
      have hremainingNil := hscanLow hlow
      have hconsumedEq : scanConsumed = integer := by
        rw [hremainingNil] at hscanPartition
        simpa using hscanPartition.symm
      rw [hscanValue, hconsumedEq] at hlow
      exact (not_lt_of_ge hfullThreshold) (by simpa [digitStep] using hlow)
    have hmantissaEq : mantissa = parsed := by
      unfold num.imp.dec2flt.parse.parse_many_digits at hrun
      rw [hscan] at hrun
      have hgeVal : num.imp.dec2flt.parse.MIN_19DIGIT_INT.val <= parsed.val := by
        simpa [num.imp.dec2flt.parse.MIN_19DIGIT_INT.eq_def] using hparsedLower
      simp [hgeVal] at hrun
      cases hoff :
          SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
            intEnd scanRest with
      | fail err => simp [hoff] at hrun
      | div => simp [hoff] at hrun
      | ok offset =>
        simp [hoff] at hrun
        cases hcast : lift (IScalar.cast .I64 offset) with
        | fail err => simp [hcast] at hrun
        | div => simp [hcast] at hrun
        | ok base =>
          simp [hcast] at hrun
          cases hadd : base + exp with
          | fail err => simp [hadd] at hrun
          | div => simp [hadd] at hrun
          | ok result =>
            simp [hadd] at hrun
            exact hrun.1.symm
    subst parsed
    obtain ⟨consumed, remaining, hpartition, hmValue, hout⟩ :=
      NumberHelperIntegralManyExact start intEnd exp outExp mantissa integer
        tail hstart hintEnd hinteger htail hlen (by
          simpa [digitStep] using hfullThreshold) hrun
    obtain ⟨headPart, restPart, hsigPartition, hprefixLen, hmPrefix,
        hrestEq⟩ :=
      partition_accounting integer zeros significant consumed remaining
        mantissa.val hinteger hinter hzeros hsignificant hsiglen hpartition
        (by simpa [digitStep] using hmValue) hparsedLower hparsedUpper
    refine ⟨headPart, restPart, hsigPartition, hprefixLen,
      by simpa [digitStep] using hmPrefix, hparsedLower, ?_, ?_⟩
    · omega
    · have hlength := congrArg List.length hsigPartition
      have hwhole := congrArg List.length hinter
      simp only [List.length_append] at hlength hwhole
      rw [hout, hrestEq]
      omega
  · intro start intEnd exp outExp mantissa integer fraction tail zeros
      significant hstart hintEnd hinteger hfraction htail hlen hinter hzeros
      hsignificant hsiglen hrun
    have hallDigits : forall b, List.Mem b (integer ++ fraction) ->
        48 <= b.val /\ b.val <= 57 := by
      intro b hb
      rcases List.mem_append.mp hb with hb | hb
      · exact hinteger b hb
      · exact hfraction b hb
    have hfullThreshold : 1000000000000000000 <=
        (integer ++ fraction).foldl digitStep 0 := by
      rcases hsignificant with ⟨first, sigRest, hsig, hfirst⟩
      have hfirstDigit := hallDigits first (by
        rw [hinter, hsig]
        exact List.mem_append_right zeros List.mem_cons_self)
      have hfirstPos : 1 <= first.val - 48 := by
        have hne : first.val ≠ 48 := by
          intro heq
          apply hfirst
          exact UScalar.eq_of_val_eq heq
        omega
      have hlower := fold_lower sigRest (first.val - 48)
      have hpow : 1000000000000000000 <= 10 ^ sigRest.length := by
        have hrestLen : 18 <= sigRest.length := by
          rw [hsig, List.length_cons] at hsiglen
          omega
        have := Nat.pow_le_pow_right (by norm_num : 0 < 10) hrestLen
        norm_num at this ⊢
        exact this
      rw [hinter, List.foldl_append, zero_fold zeros hzeros, hsig,
        List.foldl_cons]
      norm_num
      change 1000000000000000000 <=
        sigRest.foldl digitStep (digitStep 0 first)
      have hstep : 10 ^ sigRest.length <=
          (first.val - 48) * 10 ^ sigRest.length := by
        nlinarith
      simpa [digitStep] using hpow.trans (hstep.trans hlower)
    have hintTailNotDigit : forall b rest, intEnd.val = b :: rest ->
        Not (48 <= b.val /\ b.val <= 57) := by
      intro b rest hb
      rw [hintEnd] at hb
      simp only [List.cons.injEq] at hb
      rcases hb with ⟨rfl, _⟩
      norm_num
    have hstartInt : start.val = integer ++ intEnd.val := by
      rw [hstart, hintEnd]
    obtain ⟨scanRest1, parsed1, remaining1, consumed1, hscan1,
        hpartition1, hrest1, hvalue1, hremaining1, hremainingLen1,
        hparsed1Upper, hlow1, hsharp1⟩ :=
      NumberHelperTryParse19SafeLength start 0#u64 integer intEnd.val
        hstartInt hinteger hintTailNotDigit (by norm_num)
    by_cases hfirstHigh : 1000000000000000000 <= parsed1.val
    · have hmantissaEq : mantissa = parsed1 := by
        unfold num.imp.dec2flt.parse.parse_many_digits at hrun
        rw [hscan1] at hrun
        have hgeVal : num.imp.dec2flt.parse.MIN_19DIGIT_INT.val <= parsed1.val := by
          simpa [num.imp.dec2flt.parse.MIN_19DIGIT_INT.eq_def] using hfirstHigh
        simp [hgeVal] at hrun
        cases hoff :
            SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
              intEnd scanRest1 with
        | fail err => simp [hoff] at hrun
        | div => simp [hoff] at hrun
        | ok offset =>
          simp [hoff] at hrun
          cases hcast : lift (IScalar.cast .I64 offset) with
          | fail err => simp [hcast] at hrun
          | div => simp [hcast] at hrun
          | ok base =>
            simp [hcast] at hrun
            cases hadd : base + exp with
            | fail err => simp [hadd] at hrun
            | div => simp [hadd] at hrun
            | ok result =>
              simp [hadd] at hrun
              exact hrun.1.symm
      subst parsed1
      obtain ⟨consumed, remaining, hpartition, hmValue, hout⟩ :=
        NumberHelperFractionalManyExact start intEnd exp outExp mantissa
          integer fraction tail hstart hintEnd hinteger hfraction htail hlen
          (by simpa [digitStep] using hfullThreshold) hrun
      obtain ⟨headPart, restPart, hsigPartition, hprefixLen, hmPrefix,
          hrestEq⟩ :=
        partition_accounting (integer ++ fraction) zeros significant consumed
          remaining mantissa.val hallDigits hinter hzeros hsignificant hsiglen
          hpartition (by simpa [digitStep] using hmValue) hfirstHigh
          hparsed1Upper
      refine ⟨headPart, restPart, hsigPartition, hprefixLen,
        by simpa [digitStep] using hmPrefix, hfirstHigh, ?_, ?_⟩
      · omega
      · have hlength := congrArg List.length hsigPartition
        have hwhole := congrArg List.length hinter
        simp only [List.length_append] at hlength hwhole
        rw [hout, hrestEq]
        omega
    · have hfirstLow : parsed1.val < 1000000000000000000 := by omega
      have hremaining1Nil := hlow1 hfirstLow
      have hconsumed1Eq : consumed1 = integer := by
        rw [hremaining1Nil] at hpartition1
        simpa using hpartition1.symm
      have hscanRest1Val : scanRest1.val = intEnd.val := by
        simpa [hremaining1Nil] using hrest1
      have hscanRest1Eq : scanRest1 = intEnd :=
        (Slice.eq_iff _ _).2 hscanRest1Val
      have htailProp : (fraction ++ tail).length <= Usize.max := by
        have hp := intEnd.property
        rw [hintEnd] at hp
        simp only [List.length_cons] at hp
        omega
      let afterPoint : Slice Std.U8 := ⟨fraction ++ tail, htailProp⟩
      have hindex : core.slice.index.SliceIndexRangeFromUsizeSlice.index
          { start := 1#usize } intEnd = ok afterPoint := by
        simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
          Slice.drop, Slice.length, hintEnd, afterPoint]
      obtain ⟨scanRest2, parsed2, remaining2, consumed2, hscan2,
          hpartition2, hrest2, hvalue2, hremaining2, hremainingLen2,
          hparsed2Upper, hlow2, hsharp2⟩ :=
        NumberHelperTryParse19SafeLength afterPoint parsed1 fraction tail
          (by rfl) hfraction htail hparsed1Upper
      have hparsed1Value : parsed1.val = integer.foldl digitStep 0 := by
        rw [hvalue1, hconsumed1Eq]
        simp [digitStep]
      have hparsed2Lower : 1000000000000000000 <= parsed2.val := by
        by_contra hnot
        have hparsed2Low : parsed2.val < 1000000000000000000 := by omega
        have hremaining2Nil := hlow2 hparsed2Low
        have hconsumed2Eq : consumed2 = fraction := by
          rw [hremaining2Nil] at hpartition2
          simpa using hpartition2.symm
        have hfullValue : parsed2.val =
            (integer ++ fraction).foldl digitStep 0 := by
          rw [List.foldl_append, ← hparsed1Value, hvalue2, hconsumed2Eq]
        rw [hfullValue] at hparsed2Low
        exact (not_lt_of_ge hfullThreshold) hparsed2Low
      have hmantissaEq : mantissa = parsed2 := by
        unfold num.imp.dec2flt.parse.parse_many_digits at hrun
        rw [hscan1] at hrun
        have hlt : Not
            (num.imp.dec2flt.parse.MIN_19DIGIT_INT.val <= parsed1.val) := by
          simpa [num.imp.dec2flt.parse.MIN_19DIGIT_INT.eq_def]
            using hfirstLow
        rw [hscanRest1Eq] at hrun
        simp [hlt, hindex, hscan2] at hrun
        cases hoff :
            SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
              scanRest2 afterPoint with
        | fail err => simp [hoff] at hrun
        | div => simp [hoff] at hrun
        | ok offset =>
          simp [hoff] at hrun
          cases hneg : HNeg.hNeg offset with
          | fail err => simp [hneg] at hrun
          | div => simp [hneg] at hrun
          | ok negative =>
            simp [hneg] at hrun
            cases hcast : lift (IScalar.cast .I64 negative) with
            | fail err => simp [hcast] at hrun
            | div => simp [hcast] at hrun
            | ok base =>
              simp [hcast] at hrun
              cases hadd : base + exp with
              | fail err => simp [hadd] at hrun
              | div => simp [hadd] at hrun
              | ok result =>
                simp [hadd] at hrun
                exact hrun.1.symm
      subst parsed2
      obtain ⟨consumed, remaining, hpartition, hmValue, hout⟩ :=
        NumberHelperFractionalManyExact start intEnd exp outExp mantissa
          integer fraction tail hstart hintEnd hinteger hfraction htail hlen
          (by simpa [digitStep] using hfullThreshold) hrun
      obtain ⟨headPart, restPart, hsigPartition, hprefixLen, hmPrefix,
          hrestEq⟩ :=
        partition_accounting (integer ++ fraction) zeros significant consumed
          remaining mantissa.val hallDigits hinter hzeros hsignificant hsiglen
          hpartition (by simpa [digitStep] using hmValue) hparsed2Lower
          hparsed2Upper
      refine ⟨headPart, restPart, hsigPartition, hprefixLen,
        by simpa [digitStep] using hmPrefix, hparsed2Lower, ?_, ?_⟩
      · omega
      · have hlength := congrArg List.length hsigPartition
        have hwhole := congrArg List.length hinter
        simp only [List.length_append] at hlength hwhole
        rw [hout, hrestEq]
        omega

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (50)
More program dependencies (17)
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 38 entries
Technical checking records
Local closure summary
{
  "node": "NumberHelperManyRetryAccounting",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "9a82d3f4f9e95be60ea1228343cd7cf4a1e2413e082c6047389232c02c1b3123",
  "active_statement_hash": "cc2a13bfabe980357b6a69bc97335427153e7b19e4474b268379e78a890c37e9",
  "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-166",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "NumberHelperFractionalManyExact": "4f108f0f2477fa14",
    "NumberHelperIntegralManyExact": "4b76d387097e136b",
    "NumberHelperTryParse19SafeLength": "dd334c60b1e032a8",
    "num_imp_dec2flt_parse_MIN_19DIGIT_INT": "8d99761093f40bc7",
    "num_imp_dec2flt_parse_parse_many_digits": "faf0081b4c91301b"
  },
  "strict_definition_deps": {
    "SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from": "7e7284dc9bc88830",
    "num_imp_dec2flt_parse_MIN_19DIGIT_INT": "476edc5502ff7bfb",
    "num_imp_dec2flt_parse_parse_many_digits": "50c9ef13b3ed7fe8",
    "num_imp_dec2flt_parse_try_parse_19digits": "c9c74554a359a9c4"
  }
}
Prose assessment
{
  "label": "Lean proof closed; predictive prose review no longer required",
  "assessment_status": null,
  "fingerprint_relation": "unknown",
  "assessment_fingerprint": null,
  "current_fingerprint": "sound-v2:88d2653d24aab388a2683e0b371ed69db98a54222389877b616f5e15f287c1c3",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}