ParserSlowEarlyExits

Lean checked

Statement

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

Lemma · Exact early exits of the reachable slow call

Under the source-valid successful slow-trace hypotheses, write A for the accumulated magnitude and r for the selected pair's assembled raw word. One of the proof-relevant alternatives stated in Lean is available: the zero exit has r=A=0 and the displayed j=0 low-grid certificate; the underflow exit has r=0 and 0\le A<2^{-1075} with the same certificate; the large-point exit has r=2047\,2^{52} and 2^{1024}-2^{970}\le A; or parsing returns a nonempty canonical sequence with point in [-324,310) and the full exact-error invariant.

Lean statement

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

import Tablet.ParserSlowInitialCanonicality
import Tablet.num_imp_dec2flt_common_BiasedFp_zero_pow2
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: ParserSlowEarlyExits]
theorem ParserSlowEarlyExits :
    forall (s : Str) (x : DecimalLexeme) (f : F64)
      (tr : num.imp.dec2flt.Dec2FltAssemblyTrace F64),
      DecimalGoalVocabularyDefault.sourceString s ->
      DecimalGoalVocabularyDefault.validNumeric x ->
      DecimalGoalVocabularyDefault.bytes s =
        DecimalGoalVocabularyDefault.renderNumeric x ->
      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) ->
      tr.slow_path_guard = true ->
      let A := DecimalGoalVocabularyDefault.accumulatedMagnitude x
      let unit : Nat := 2 ^ 52
      let infinityBits : Nat := 2047 * unit
      let threshold : Real := (2 : Real) ^ (1024 : Nat) -
        (2 : Real) ^ (970 : Nat)
      let raw : Nat := Nat.lor tr.selected_pair.m.val
        (((tr.selected_pair.p_biased.val.emod (2 ^ 64)).toNat * unit) %
          (2 ^ 64))
      (raw = 0 /\ A = 0 /\
        exists j : Nat,
          j <= 2 ^ 53 /\ raw = j /\
          A <= (2 ^ 53 : Nat) * (2 : Real) ^ (-(1074 : Int)) /\
          A <= (j : Real) * (2 : Real) ^ (-(1074 : Int)) +
            (2 : Real) ^ (-(1075 : Int)) /\
          (j : Real) * (2 : Real) ^ (-(1074 : Int)) <= A +
            (2 : Real) ^ (-(1075 : Int)) /\
          (A = ((j : Real) + (1 / 2 : Real)) *
                (2 : Real) ^ (-(1074 : Int)) \/
              A = ((j : Real) - (1 / 2 : Real)) *
                (2 : Real) ^ (-(1074 : Int)) -> j % 2 = 0)) \/
      (raw = 0 /\ 0 <= A /\ A < (2 : Real) ^ (-(1075 : Int)) /\
        exists j : Nat,
          j <= 2 ^ 53 /\ raw = j /\
          A <= (2 ^ 53 : Nat) * (2 : Real) ^ (-(1074 : Int)) /\
          A <= (j : Real) * (2 : Real) ^ (-(1074 : Int)) +
            (2 : Real) ^ (-(1075 : Int)) /\
          (j : Real) * (2 : Real) ^ (-(1074 : Int)) <= A +
            (2 : Real) ^ (-(1075 : Int)) /\
          (A = ((j : Real) + (1 / 2 : Real)) *
                (2 : Real) ^ (-(1074 : Int)) \/
              A = ((j : Real) - (1 / 2 : Real)) *
                (2 : Real) ^ (-(1074 : Int)) -> j % 2 = 0)) \/
      (raw = infinityBits /\ threshold <= A) \/
      exists d : num.imp.dec2flt.decimal_seq.DecimalSeq,
        num.imp.dec2flt.decimal_seq.parse_decimal_seq tr.body = ok d /\
        num.imp.dec2flt.slow.parse_long_mantissa
          F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 tr.body =
            ok tr.selected_pair /\
        DecimalGoalVocabularyDefault.assembledWord
          tr.selected_pair tr.assembly_result /\
        d.num_digits.val != 0 /\
        -324 <= d.decimal_point.val /\ d.decimal_point.val < 310 /\
        let digits :=
          (d.digits.val.take d.num_digits.val).map (fun z => z.val)
        let integer : Nat := digits.foldl (fun n digit => 10 * n + digit) 0
        let value : Real := integer * (10 : Real) ^
          (d.decimal_point.val - (d.num_digits.val : Int))
        let cutoff : Real := (10 : Real) ^
          (d.decimal_point.val - (768 : Int))
        d.num_digits.val <= 768 /\
        (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
        (exists z zs, digits = z :: zs /\ 1 <= z /\ z < 10 /\
          digits.length = d.num_digits.val) /\
        exists eta : Real, A = value + eta /\ 0 <= eta /\ eta < cutoff /\
          (d.truncated = true <-> 0 < eta) := 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

Obtain the parsed record, selected slow-call equation, assembly relation, and canonical invariant from ParserSlowInitialCanonicality. Unfold the same call using num imp dec2flt slow parse long mantissa. Its first three tests are, in order, zero digit count, point below -324, and point at least 310. Since the whole call returns the trace's selected pair, each taken branch identifies that pair with the branch's exact `zero_pow2' result. Unfolding num imp dec2flt common BiasedFp zero pow2 gives (m,p)=(0,0) for zero and (m,p)=(0,2047) for binary64 infinity. The assembly relation then gives raw words 0 and 2047\,2^{52} respectively; the shifts, multiplication, modulo, and OR are all within 64 bits at these two literal records.
In the zero-count branch the canonical alternative of ParserSlowInitialCanonicality gives A=0. Choose j=0. All three size and half-step inequalities reduce to positivity of powers of two; the two midpoint equalities are impossible, so the parity implication is true.
In the point-below--324 branch, nonzero canonicality gives a first digit at least one and all digits below ten. The entire infinite decimal represented by the record is therefore nonnegative and strictly below 10^{-324}. The integer inequality 2^{1075}<10^{324}, checked by repeated squaring, gives 10^{-324}<2^{-1075}. Hence 0\le A<2^{-1075}. Again choose j=0: the upper half-step inequality is this strict bound, the lower one follows from nonnegativity, and neither adjacent midpoint can be equal to A (the negative midpoint is negative and the positive one is the strict upper bound). Thus the complete low-grid certificate follows.
In the point-at-least-310 branch, the positive leading digit gives A\ge10^{309}. Direct integer-power comparison yields 10^{309}>2^{1024}-2^{970}, so the infinity branch has the required threshold bound. If none of the three tests is taken, their negations are precisely nonzero count and -324\le p<310. Append those facts to the canonical package from ParserSlowInitialCanonicality; this is the fourth alternative. The extracted tests are exhaustive, completing the proof.

Lean proof


  intro s x f tr hsource hvalid hbytes htrace hguard
  obtain ⟨d, hparse, hslow, hassembled, hnonnegative, hcanonical⟩ :=
    ParserSlowInitialCanonicality s x f tr hsource hvalid hbytes htrace hguard
  rcases hcanonical with ⟨hdcount, hdigit, hshape, heta⟩
  rcases ParserExponentConstants with
    ⟨hbits, htotal, hsig, hexpbits, hexpsat, hinf, hbias, hmin,
      hsmall, hlarge, hroundmin, hroundmax⟩
  have bind_ok_inv : forall {alpha beta : Type} (r : Result alpha)
      (g : alpha -> Result beta) y,
      Bind.bind r g = ok y -> exists z, r = ok z /\ g z = ok y := by
    intro alpha beta 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 z => exact ⟨z, rfl, hr⟩
  by_cases hempty : d.num_digits.val = 0
  · have hz : num.imp.dec2flt.common.BiasedFp.zero_pow2 0#i32 =
        ok { m := 0#u64, p_biased := 0#i32 } := by rfl
    have hi : num.imp.dec2flt.common.BiasedFp.zero_pow2 2047#i32 =
        ok { m := 0#u64, p_biased := 2047#i32 } := by rfl
    unfold num.imp.dec2flt.slow.parse_long_mantissa at hslow
    rw [hz, hinf] at hslow
    simp only [bind_tc_ok] at hslow
    simp only [hi, bind_tc_ok] at hslow
    obtain ⟨d0, hd0, hslow⟩ := bind_ok_inv _ _ _ hslow
    rw [hparse] at hd0
    have hd0eq : d0 = d := by cases hd0; rfl
    subst d0
    have hempty' : d.num_digits = 0#usize := by
      apply UScalar.eq_of_val_eq
      exact hempty
    simp only [hempty', if_true] at hslow
    have hpair : ({ m := 0#u64, p_biased := 0#i32 } :
        num.imp.dec2flt.common.BiasedFp) = tr.selected_pair := by
      injection hslow
    have hm : tr.selected_pair.m.val = 0 := by
      simpa using congrArg (fun q => q.m.val) hpair.symm
    have hp : tr.selected_pair.p_biased.val = 0 := by
      simpa using congrArg (fun q => q.p_biased.val) hpair.symm
    rcases hshape with hzero | hnonempty
    · rcases hzero with ⟨_, _, _, hA⟩
      left
      dsimp
      have hstep : 0 ≤ (2 : Real) ^ (-(1074 : Int)) :=
        zpow_nonneg (by norm_num) _
      have hhalf : 0 ≤ (2 : Real) ^ (-(1075 : Int)) :=
        zpow_nonneg (by norm_num) _
      refine ⟨?_, hA, 0, by norm_num, ?_, ?_, ?_, ?_, ?_⟩
      · rw [hm, hp]
        norm_num
      · rw [hm, hp]
        norm_num
      · rw [hA]
        exact mul_nonneg (by positivity) hstep
      · rw [hA]
        simpa using hhalf
      · rw [hA]
        simpa using hhalf
      · norm_num
    · exfalso
      rcases hnonempty with ⟨z, zs, hds, hz1, hz9, hlen⟩
      simp [hds] at hlen
      omega
  by_cases hlow : d.decimal_point.val < -324
  · have hz : num.imp.dec2flt.common.BiasedFp.zero_pow2 0#i32 =
        ok { m := 0#u64, p_biased := 0#i32 } := by rfl
    have hi : num.imp.dec2flt.common.BiasedFp.zero_pow2 2047#i32 =
        ok { m := 0#u64, p_biased := 2047#i32 } := by rfl
    unfold num.imp.dec2flt.slow.parse_long_mantissa at hslow
    rw [hz, hinf] at hslow
    simp only [bind_tc_ok] at hslow
    simp only [hi, bind_tc_ok] at hslow
    obtain ⟨d0, hd0, hslow⟩ := bind_ok_inv _ _ _ hslow
    rw [hparse] at hd0
    have hd0eq : d0 = d := by cases hd0; rfl
    subst d0
    have hempty' : d.num_digits ≠ 0#usize := by
      intro he
      apply hempty
      exact congrArg UScalar.val he
    simp only [hempty', if_false] at hslow
    have hlow' : d.decimal_point < (-324)#i32 := by
      simpa only [IScalar.lt_equiv] using hlow
    simp only [hlow', if_true] at hslow
    have hpair : ({ m := 0#u64, p_biased := 0#i32 } :
        num.imp.dec2flt.common.BiasedFp) = tr.selected_pair := by
      injection hslow
    have hm : tr.selected_pair.m.val = 0 := by
      simpa using congrArg (fun q => q.m.val) hpair.symm
    have hp : tr.selected_pair.p_biased.val = 0 := by
      simpa using congrArg (fun q => q.p_biased.val) hpair.symm
    apply Or.inr
    apply Or.inl
    dsimp
    have hstep : 0 ≤ (2 : Real) ^ (-(1074 : Int)) :=
      zpow_nonneg (by norm_num) _
    have hhalf : 0 ≤ (2 : Real) ^ (-(1075 : Int)) :=
      zpow_nonneg (by norm_num) _
    have hAupper : DecimalGoalVocabularyDefault.accumulatedMagnitude x <
        (2 : Real) ^ (-(1075 : Int)) := by
      obtain ⟨eta, hAe, heta0, hetaCut, hetaSticky, hetaLower, hetaUpper⟩ := heta
      have hnotzero : ¬ (d.num_digits.val = 0 ∧
          (d.digits.val.take d.num_digits.val).map (fun q => q.val) = [] ∧
          d.truncated = false ∧
          DecimalGoalVocabularyDefault.accumulatedMagnitude x = 0) := by
        intro hbad
        exact hempty hbad.1
      obtain ⟨z, zs, hdigitEq, hz1, hz9, hlen⟩ :=
        hshape.resolve_left hnotzero
      let digits := (d.digits.val.take d.num_digits.val).map (fun q => q.val)
      let integer := digits.foldl (fun n digit => 10 * n + digit) 0
      have hdigitsNat : ∀ q, q ∈ digits -> q < 10 := by
        intro q hq
        rcases List.mem_map.mp hq with ⟨u, hu, rfl⟩
        exact hdigit u hu
      have hprefix := NumberHelperDecimalPrefixInterval [] digits
        hdigitsNat (d.decimal_point.val - (d.num_digits.val : Int))
      have hlen' : digits.length = d.num_digits.val := by
        simpa [digits] using hlen
      have hvalue : (integer : Real) * (10 : Real) ^
          (d.decimal_point.val - (d.num_digits.val : Int)) <
          (10 : Real) ^ d.decimal_point.val := by
        have hbound := hprefix.2
        simp only [List.foldl, List.nil_append, Nat.cast_zero, Nat.cast_one,
          zero_mul, one_mul] at hbound
        rw [hlen'] at hbound
        have hexponent : d.decimal_point.val - (d.num_digits.val : Int) +
            (d.num_digits.val : Int) = d.decimal_point.val := by omega
        rw [hexponent] at hbound
        simpa [integer] using hbound
      have hpoint : d.decimal_point.val ≤ -325 := by omega
      have hpow : (10 : Real) ^ d.decimal_point.val ≤
          (10 : Real) ^ (-325 : Int) := by
        gcongr
        norm_num
      have hexp : d.decimal_point.val - 768 ≤ (-1093 : Int) := by
        omega
      have hcut : (10 : Real) ^ (d.decimal_point.val - 768) ≤
          (10 : Real) ^ (-1093 : Int) := by
        gcongr
        · norm_num
      have hsmall : (10 : Real) ^ (-325 : Int) +
          (10 : Real) ^ (-1093 : Int) <
          (2 : Real) ^ (-(1075 : Int)) := by
        have hpow53 : (2 : Nat) ^ 53 < 10 ^ 16 := by norm_num
        have hpow16 : (2 : Nat) ^ 16 < 10 ^ 5 := by norm_num
        have hpow10 : (2 : Nat) ^ 1076 < 10 ^ 325 := by
          calc
            (2 : Nat) ^ 1076 = ((2 : Nat) ^ 53) ^ 20 * 2 ^ 16 := by
              rw [show 1076 = 53 * 20 + 16 by norm_num, pow_add, ← pow_mul]
            _ < (10 ^ 16) ^ 20 * 10 ^ 5 := by gcongr
            _ = 10 ^ 325 := by
              rw [← pow_mul, ← pow_add]
        have hfirst : (10 : Real) ^ (-325 : Int) <
            (2 : Real) ^ (-1076 : Int) := by
          rw [zpow_neg, zpow_neg]
          field_simp
          change (2 : Real) ^ 1076 < (10 : Real) ^ 325
          exact_mod_cast hpow10
        have hsecond : (10 : Real) ^ (-1093 : Int) <
            (10 : Real) ^ (-325 : Int) := by
          gcongr <;> norm_num
        calc
          (10 : Real) ^ (-325 : Int) + (10 : Real) ^ (-1093 : Int) <
              (2 : Real) ^ (-1076 : Int) + (2 : Real) ^ (-1076 : Int) :=
            add_lt_add hfirst (hsecond.trans_le hfirst.le)
          _ = 2 * (2 : Real) ^ (-1076 : Int) := by ring
          _ = (2 : Real) ^ (-1075 : Int) := by
            calc
              2 * (2 : Real) ^ (-1076 : Int) =
                  (2 : Real) ^ (1 : Int) * (2 : Real) ^ (-1076 : Int) := by norm_num
              _ = (2 : Real) ^ (1 + (-1076 : Int)) := by
                rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
              _ = (2 : Real) ^ (-1075 : Int) := by norm_num
      have htotal := (add_lt_add hvalue hetaCut).trans_le
        (add_le_add hpow hcut)
      have htotal' := htotal.trans hsmall
      rw [hAe]
      simpa [digits, integer] using htotal'
    refine ⟨?_, hnonnegative, hAupper, 0, by norm_num, ?_, ?_, ?_, ?_, ?_⟩
    · rw [hm, hp]
      norm_num
    · rw [hm, hp]
      norm_num
    · have hmono : (2 : Real) ^ (-(1075 : Int)) ≤
          (2 : Real) ^ (-(1074 : Int)) := by
        gcongr <;> norm_num
      have hmul : (2 : Real) ^ (-(1074 : Int)) ≤
          (2 ^ 53 : Nat) * (2 : Real) ^ (-(1074 : Int)) := by
        have hone : (1 : Real) ≤ (2 ^ 53 : Nat) := by norm_num
        have hnonneg : 0 ≤ (2 : Real) ^ (-(1074 : Int)) :=
          zpow_nonneg (by norm_num) _
        have := mul_le_mul_of_nonneg_right hone hnonneg
        simpa using this
      exact (le_of_lt hAupper).trans (hmono.trans hmul)
    · simpa using (le_of_lt hAupper)
    · simpa using add_nonneg hnonnegative hhalf
    · intro hmid
      rcases hmid with hmid | hmid
      · have hmidplus : (((0 : Nat) : Real) + 1 / 2) *
            (2 : Real) ^ (-(1074 : Int)) =
            (2 : Real) ^ (-(1075 : Int)) := by
          calc
            (((0 : Nat) : Real) + 1 / 2) * (2 : Real) ^ (-(1074 : Int)) =
                (2 : Real) ^ (-1 : Int) * (2 : Real) ^ (-(1074 : Int)) := by
              norm_num [zpow_neg, div_eq_mul_inv]
            _ = (2 : Real) ^ (-1 + (-(1074 : Int))) := by
              rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
            _ = (2 : Real) ^ (-(1075 : Int)) := by norm_num
        have heq : DecimalGoalVocabularyDefault.accumulatedMagnitude x =
            (2 : Real) ^ (-(1075 : Int)) := hmid.trans hmidplus
        exact False.elim ((ne_of_lt hAupper) heq)
      · have hmidplus : ((0 : Real) + 1 / 2) *
            (2 : Real) ^ (-(1074 : Int)) =
            (2 : Real) ^ (-(1075 : Int)) := by
          calc
            ((0 : Real) + 1 / 2) * (2 : Real) ^ (-(1074 : Int)) =
                (2 : Real) ^ (-1 : Int) * (2 : Real) ^ (-(1074 : Int)) := by
              norm_num [zpow_neg, div_eq_mul_inv]
            _ = (2 : Real) ^ (-1 + (-(1074 : Int))) := by
              rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
            _ = (2 : Real) ^ (-(1075 : Int)) := by norm_num
        have hmidneg : (((0 : Nat) : Real) - 1 / 2) *
            (2 : Real) ^ (-(1074 : Int)) =
            -((2 : Real) ^ (-(1075 : Int))) := by
          calc
            (((0 : Nat) : Real) - 1 / 2) * (2 : Real) ^ (-(1074 : Int)) =
                -(((0 : Real) + 1 / 2) * (2 : Real) ^ (-(1074 : Int))) := by ring
            _ = -((2 : Real) ^ (-(1075 : Int))) := by rw [hmidplus]
        have heq : DecimalGoalVocabularyDefault.accumulatedMagnitude x =
            -((2 : Real) ^ (-(1075 : Int))) := hmid.trans hmidneg
        have hneg : DecimalGoalVocabularyDefault.accumulatedMagnitude x < 0 := by
          rw [heq]
          exact neg_lt_zero.mpr (zpow_pos (by norm_num) _)
        exact False.elim ((not_lt_of_ge hnonnegative) hneg)
  by_cases hhigh : 310 ≤ d.decimal_point.val
  · have hz : num.imp.dec2flt.common.BiasedFp.zero_pow2 0#i32 =
        ok { m := 0#u64, p_biased := 0#i32 } := by rfl
    have hi : num.imp.dec2flt.common.BiasedFp.zero_pow2 2047#i32 =
        ok { m := 0#u64, p_biased := 2047#i32 } := by rfl
    unfold num.imp.dec2flt.slow.parse_long_mantissa at hslow
    rw [hz, hinf] at hslow
    simp only [bind_tc_ok] at hslow
    simp only [hi, bind_tc_ok] at hslow
    obtain ⟨d0, hd0, hslow⟩ := bind_ok_inv _ _ _ hslow
    rw [hparse] at hd0
    have hd0eq : d0 = d := by cases hd0; rfl
    subst d0
    have hempty' : d.num_digits ≠ 0#usize := by
      intro he
      apply hempty
      exact congrArg UScalar.val he
    simp only [hempty', if_false] at hslow
    have hlow' : ¬ d.decimal_point < (-324)#i32 := by
      intro h
      apply hlow
      simpa only [IScalar.lt_equiv] using h
    simp only [hlow', if_false] at hslow
    have hhigh' : d.decimal_point ≥ 310#i32 := by
      simpa only [ge_iff_le, IScalar.le_equiv] using hhigh
    simp only [hhigh', if_true] at hslow
    have hpair : ({ m := 0#u64, p_biased := 2047#i32 } :
        num.imp.dec2flt.common.BiasedFp) = tr.selected_pair := by
      injection hslow
    have hm : tr.selected_pair.m.val = 0 := by
      simpa using congrArg (fun q => q.m.val) hpair.symm
    have hp : tr.selected_pair.p_biased.val = 2047 := by
      simpa using congrArg (fun q => q.p_biased.val) hpair.symm
    apply Or.inr
    apply Or.inr
    apply Or.inl
    dsimp
    constructor
    · rw [hm, hp]
      norm_num [Int.toNat]
    · obtain ⟨eta, hAe, heta0, hetaCut, hetaSticky, _, _⟩ := heta
      have hnotzero : ¬ (d.num_digits.val = 0 ∧
          (d.digits.val.take d.num_digits.val).map (fun q => q.val) = [] ∧
          d.truncated = false ∧
          DecimalGoalVocabularyDefault.accumulatedMagnitude x = 0) := by
        intro hbad
        exact hempty hbad.1
      obtain ⟨z, zs, hdigitEq, hz1, hz9, hlen⟩ :=
        hshape.resolve_left hnotzero
      let digits := (d.digits.val.take d.num_digits.val).map (fun q => q.val)
      let integer := digits.foldl (fun n digit => 10 * n + digit) 0
      have hdigitsNat : ∀ q, q ∈ zs -> q < 10 := by
        intro q hq
        have hqd : q ∈ digits := by
          dsimp [digits]
          rw [hdigitEq]
          exact List.mem_cons_of_mem z hq
        rcases List.mem_map.mp hqd with ⟨u, hu, rfl⟩
        exact hdigit u hu
      have hprefix := NumberHelperDecimalPrefixInterval [z] zs
        hdigitsNat (d.decimal_point.val - (d.num_digits.val : Int))
      have hlen' : digits.length = d.num_digits.val := by
        simpa [digits] using hlen
      have hzslen : zs.length + 1 = d.num_digits.val := by
        have htmp : (z :: zs).length = d.num_digits.val := by
          rw [← hdigitEq]
          exact hlen'
        simpa using htmp
      have hbound := hprefix.1
      simp only [List.foldl, List.nil_append, Nat.cast_zero, Nat.cast_one,
        zero_mul, one_mul] at hbound
      have hbound' : (z : Real) * (10 : Real) ^
          (d.decimal_point.val - (d.num_digits.val : Int) + (zs.length : Int)) ≤
          (integer : Real) * (10 : Real) ^
            (d.decimal_point.val - (d.num_digits.val : Int)) := by
        simpa [integer, digits, hdigitEq] using hbound
      have hbound'' : (10 : Real) ^
          (d.decimal_point.val - (d.num_digits.val : Int) + (zs.length : Int)) ≤
          (integer : Real) * (10 : Real) ^
            (d.decimal_point.val - (d.num_digits.val : Int)) := by
        have hzreal : (1 : Real) ≤ z := by exact_mod_cast hz1
        have hpowpos : 0 ≤ (10 : Real) ^
            (d.decimal_point.val - (d.num_digits.val : Int) + (zs.length : Int)) :=
          (zpow_pos (by norm_num) _).le
        calc
          (10 : Real) ^
              (d.decimal_point.val - (d.num_digits.val : Int) + (zs.length : Int)) =
              1 * (10 : Real) ^
                (d.decimal_point.val - (d.num_digits.val : Int) + (zs.length : Int)) := by ring
          _ ≤ (z : Real) * (10 : Real) ^
              (d.decimal_point.val - (d.num_digits.val : Int) + (zs.length : Int)) :=
            mul_le_mul_of_nonneg_right hzreal hpowpos
          _ ≤ _ := hbound'
      have hexponent : d.decimal_point.val - (d.num_digits.val : Int) +
          (zs.length : Int) = d.decimal_point.val - 1 := by omega
      rw [hexponent] at hbound''
      have hvalueLower : (10 : Real) ^ 309 ≤
          (integer : Real) * (10 : Real) ^
            (d.decimal_point.val - (d.num_digits.val : Int)) := by
        have hpoint : (309 : Int) ≤ d.decimal_point.val - 1 := by omega
        have hpowlower : (10 : Real) ^ (309 : Int) ≤
            (10 : Real) ^ (d.decimal_point.val - 1) := by
          gcongr <;> norm_num
        exact hpowlower.trans hbound''
      have hAvalue : (10 : Real) ^ 309 ≤
          DecimalGoalVocabularyDefault.accumulatedMagnitude x := by
        rw [hAe]
        have hnonnegvalue : 0 ≤ (integer : Real) * (10 : Real) ^
            (d.decimal_point.val - (d.num_digits.val : Int)) := by
          exact mul_nonneg (Nat.cast_nonneg _) (zpow_nonneg (by norm_num) _)
        linarith
      have hthreshold : (2 : Real) ^ (1024 : Nat) -
          (2 : Real) ^ (970 : Nat) ≤
          DecimalGoalVocabularyDefault.accumulatedMagnitude x := by
        have hpow : (2 : Real) ^ (1024 : Nat) < (10 : Real) ^ 309 := by
          set_option exponentiation.threshold 2048 in norm_num
        have hsub : (2 : Real) ^ (1024 : Nat) -
            (2 : Real) ^ (970 : Nat) < (10 : Real) ^ 309 := by
          have hpos : 0 < (2 : Real) ^ (970 : Nat) := by positivity
          exact (sub_lt_self _ hpos).trans hpow
        exact hsub.le.trans hAvalue
      exact hthreshold
  have hlow' : -324 ≤ d.decimal_point.val := by omega
  have hhigh' : d.decimal_point.val < 310 := by omega
  apply Or.inr
  apply Or.inr
  apply Or.inr
  rcases heta with ⟨eta, hAe, heta0, hetaCut, hetaSticky, _, _⟩
  refine ⟨d, hparse, hslow, hassembled, ?_, hlow', hhigh', hdcount, hdigit, ?_,
    ⟨eta, hAe, heta0, hetaCut, hetaSticky⟩⟩
  · simp [hempty]
  · exact hshape.resolve_left (by
      intro hbad
      exact hempty hbad.1)

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.ParserSlowInitialCanonicality
import Tablet.num_imp_dec2flt_common_BiasedFp_zero_pow2
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: ParserSlowEarlyExits]
theorem ParserSlowEarlyExits :
    forall (s : Str) (x : DecimalLexeme) (f : F64)
      (tr : num.imp.dec2flt.Dec2FltAssemblyTrace F64),
      DecimalGoalVocabularyDefault.sourceString s ->
      DecimalGoalVocabularyDefault.validNumeric x ->
      DecimalGoalVocabularyDefault.bytes s =
        DecimalGoalVocabularyDefault.renderNumeric x ->
      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) ->
      tr.slow_path_guard = true ->
      let A := DecimalGoalVocabularyDefault.accumulatedMagnitude x
      let unit : Nat := 2 ^ 52
      let infinityBits : Nat := 2047 * unit
      let threshold : Real := (2 : Real) ^ (1024 : Nat) -
        (2 : Real) ^ (970 : Nat)
      let raw : Nat := Nat.lor tr.selected_pair.m.val
        (((tr.selected_pair.p_biased.val.emod (2 ^ 64)).toNat * unit) %
          (2 ^ 64))
      (raw = 0 /\ A = 0 /\
        exists j : Nat,
          j <= 2 ^ 53 /\ raw = j /\
          A <= (2 ^ 53 : Nat) * (2 : Real) ^ (-(1074 : Int)) /\
          A <= (j : Real) * (2 : Real) ^ (-(1074 : Int)) +
            (2 : Real) ^ (-(1075 : Int)) /\
          (j : Real) * (2 : Real) ^ (-(1074 : Int)) <= A +
            (2 : Real) ^ (-(1075 : Int)) /\
          (A = ((j : Real) + (1 / 2 : Real)) *
                (2 : Real) ^ (-(1074 : Int)) \/
              A = ((j : Real) - (1 / 2 : Real)) *
                (2 : Real) ^ (-(1074 : Int)) -> j % 2 = 0)) \/
      (raw = 0 /\ 0 <= A /\ A < (2 : Real) ^ (-(1075 : Int)) /\
        exists j : Nat,
          j <= 2 ^ 53 /\ raw = j /\
          A <= (2 ^ 53 : Nat) * (2 : Real) ^ (-(1074 : Int)) /\
          A <= (j : Real) * (2 : Real) ^ (-(1074 : Int)) +
            (2 : Real) ^ (-(1075 : Int)) /\
          (j : Real) * (2 : Real) ^ (-(1074 : Int)) <= A +
            (2 : Real) ^ (-(1075 : Int)) /\
          (A = ((j : Real) + (1 / 2 : Real)) *
                (2 : Real) ^ (-(1074 : Int)) \/
              A = ((j : Real) - (1 / 2 : Real)) *
                (2 : Real) ^ (-(1074 : Int)) -> j % 2 = 0)) \/
      (raw = infinityBits /\ threshold <= A) \/
      exists d : num.imp.dec2flt.decimal_seq.DecimalSeq,
        num.imp.dec2flt.decimal_seq.parse_decimal_seq tr.body = ok d /\
        num.imp.dec2flt.slow.parse_long_mantissa
          F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 tr.body =
            ok tr.selected_pair /\
        DecimalGoalVocabularyDefault.assembledWord
          tr.selected_pair tr.assembly_result /\
        d.num_digits.val != 0 /\
        -324 <= d.decimal_point.val /\ d.decimal_point.val < 310 /\
        let digits :=
          (d.digits.val.take d.num_digits.val).map (fun z => z.val)
        let integer : Nat := digits.foldl (fun n digit => 10 * n + digit) 0
        let value : Real := integer * (10 : Real) ^
          (d.decimal_point.val - (d.num_digits.val : Int))
        let cutoff : Real := (10 : Real) ^
          (d.decimal_point.val - (768 : Int))
        d.num_digits.val <= 768 /\
        (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
        (exists z zs, digits = z :: zs /\ 1 <= z /\ z < 10 /\
          digits.length = d.num_digits.val) /\
        exists eta : Real, A = value + eta /\ 0 <= eta /\ eta < cutoff /\
          (d.truncated = true <-> 0 < eta) := by
-- BODY
  intro s x f tr hsource hvalid hbytes htrace hguard
  obtain ⟨d, hparse, hslow, hassembled, hnonnegative, hcanonical⟩ :=
    ParserSlowInitialCanonicality s x f tr hsource hvalid hbytes htrace hguard
  rcases hcanonical with ⟨hdcount, hdigit, hshape, heta⟩
  rcases ParserExponentConstants with
    ⟨hbits, htotal, hsig, hexpbits, hexpsat, hinf, hbias, hmin,
      hsmall, hlarge, hroundmin, hroundmax⟩
  have bind_ok_inv : forall {alpha beta : Type} (r : Result alpha)
      (g : alpha -> Result beta) y,
      Bind.bind r g = ok y -> exists z, r = ok z /\ g z = ok y := by
    intro alpha beta 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 z => exact ⟨z, rfl, hr⟩
  by_cases hempty : d.num_digits.val = 0
  · have hz : num.imp.dec2flt.common.BiasedFp.zero_pow2 0#i32 =
        ok { m := 0#u64, p_biased := 0#i32 } := by rfl
    have hi : num.imp.dec2flt.common.BiasedFp.zero_pow2 2047#i32 =
        ok { m := 0#u64, p_biased := 2047#i32 } := by rfl
    unfold num.imp.dec2flt.slow.parse_long_mantissa at hslow
    rw [hz, hinf] at hslow
    simp only [bind_tc_ok] at hslow
    simp only [hi, bind_tc_ok] at hslow
    obtain ⟨d0, hd0, hslow⟩ := bind_ok_inv _ _ _ hslow
    rw [hparse] at hd0
    have hd0eq : d0 = d := by cases hd0; rfl
    subst d0
    have hempty' : d.num_digits = 0#usize := by
      apply UScalar.eq_of_val_eq
      exact hempty
    simp only [hempty', if_true] at hslow
    have hpair : ({ m := 0#u64, p_biased := 0#i32 } :
        num.imp.dec2flt.common.BiasedFp) = tr.selected_pair := by
      injection hslow
    have hm : tr.selected_pair.m.val = 0 := by
      simpa using congrArg (fun q => q.m.val) hpair.symm
    have hp : tr.selected_pair.p_biased.val = 0 := by
      simpa using congrArg (fun q => q.p_biased.val) hpair.symm
    rcases hshape with hzero | hnonempty
    · rcases hzero with ⟨_, _, _, hA⟩
      left
      dsimp
      have hstep : 0 ≤ (2 : Real) ^ (-(1074 : Int)) :=
        zpow_nonneg (by norm_num) _
      have hhalf : 0 ≤ (2 : Real) ^ (-(1075 : Int)) :=
        zpow_nonneg (by norm_num) _
      refine ⟨?_, hA, 0, by norm_num, ?_, ?_, ?_, ?_, ?_⟩
      · rw [hm, hp]
        norm_num
      · rw [hm, hp]
        norm_num
      · rw [hA]
        exact mul_nonneg (by positivity) hstep
      · rw [hA]
        simpa using hhalf
      · rw [hA]
        simpa using hhalf
      · norm_num
    · exfalso
      rcases hnonempty with ⟨z, zs, hds, hz1, hz9, hlen⟩
      simp [hds] at hlen
      omega
  by_cases hlow : d.decimal_point.val < -324
  · have hz : num.imp.dec2flt.common.BiasedFp.zero_pow2 0#i32 =
        ok { m := 0#u64, p_biased := 0#i32 } := by rfl
    have hi : num.imp.dec2flt.common.BiasedFp.zero_pow2 2047#i32 =
        ok { m := 0#u64, p_biased := 2047#i32 } := by rfl
    unfold num.imp.dec2flt.slow.parse_long_mantissa at hslow
    rw [hz, hinf] at hslow
    simp only [bind_tc_ok] at hslow
    simp only [hi, bind_tc_ok] at hslow
    obtain ⟨d0, hd0, hslow⟩ := bind_ok_inv _ _ _ hslow
    rw [hparse] at hd0
    have hd0eq : d0 = d := by cases hd0; rfl
    subst d0
    have hempty' : d.num_digits ≠ 0#usize := by
      intro he
      apply hempty
      exact congrArg UScalar.val he
    simp only [hempty', if_false] at hslow
    have hlow' : d.decimal_point < (-324)#i32 := by
      simpa only [IScalar.lt_equiv] using hlow
    simp only [hlow', if_true] at hslow
    have hpair : ({ m := 0#u64, p_biased := 0#i32 } :
        num.imp.dec2flt.common.BiasedFp) = tr.selected_pair := by
      injection hslow
    have hm : tr.selected_pair.m.val = 0 := by
      simpa using congrArg (fun q => q.m.val) hpair.symm
    have hp : tr.selected_pair.p_biased.val = 0 := by
      simpa using congrArg (fun q => q.p_biased.val) hpair.symm
    apply Or.inr
    apply Or.inl
    dsimp
    have hstep : 0 ≤ (2 : Real) ^ (-(1074 : Int)) :=
      zpow_nonneg (by norm_num) _
    have hhalf : 0 ≤ (2 : Real) ^ (-(1075 : Int)) :=
      zpow_nonneg (by norm_num) _
    have hAupper : DecimalGoalVocabularyDefault.accumulatedMagnitude x <
        (2 : Real) ^ (-(1075 : Int)) := by
      obtain ⟨eta, hAe, heta0, hetaCut, hetaSticky, hetaLower, hetaUpper⟩ := heta
      have hnotzero : ¬ (d.num_digits.val = 0 ∧
          (d.digits.val.take d.num_digits.val).map (fun q => q.val) = [] ∧
          d.truncated = false ∧
          DecimalGoalVocabularyDefault.accumulatedMagnitude x = 0) := by
        intro hbad
        exact hempty hbad.1
      obtain ⟨z, zs, hdigitEq, hz1, hz9, hlen⟩ :=
        hshape.resolve_left hnotzero
      let digits := (d.digits.val.take d.num_digits.val).map (fun q => q.val)
      let integer := digits.foldl (fun n digit => 10 * n + digit) 0
      have hdigitsNat : ∀ q, q ∈ digits -> q < 10 := by
        intro q hq
        rcases List.mem_map.mp hq with ⟨u, hu, rfl⟩
        exact hdigit u hu
      have hprefix := NumberHelperDecimalPrefixInterval [] digits
        hdigitsNat (d.decimal_point.val - (d.num_digits.val : Int))
      have hlen' : digits.length = d.num_digits.val := by
        simpa [digits] using hlen
      have hvalue : (integer : Real) * (10 : Real) ^
          (d.decimal_point.val - (d.num_digits.val : Int)) <
          (10 : Real) ^ d.decimal_point.val := by
        have hbound := hprefix.2
        simp only [List.foldl, List.nil_append, Nat.cast_zero, Nat.cast_one,
          zero_mul, one_mul] at hbound
        rw [hlen'] at hbound
        have hexponent : d.decimal_point.val - (d.num_digits.val : Int) +
            (d.num_digits.val : Int) = d.decimal_point.val := by omega
        rw [hexponent] at hbound
        simpa [integer] using hbound
      have hpoint : d.decimal_point.val ≤ -325 := by omega
      have hpow : (10 : Real) ^ d.decimal_point.val ≤
          (10 : Real) ^ (-325 : Int) := by
        gcongr
        norm_num
      have hexp : d.decimal_point.val - 768 ≤ (-1093 : Int) := by
        omega
      have hcut : (10 : Real) ^ (d.decimal_point.val - 768) ≤
          (10 : Real) ^ (-1093 : Int) := by
        gcongr
        · norm_num
      have hsmall : (10 : Real) ^ (-325 : Int) +
          (10 : Real) ^ (-1093 : Int) <
          (2 : Real) ^ (-(1075 : Int)) := by
        have hpow53 : (2 : Nat) ^ 53 < 10 ^ 16 := by norm_num
        have hpow16 : (2 : Nat) ^ 16 < 10 ^ 5 := by norm_num
        have hpow10 : (2 : Nat) ^ 1076 < 10 ^ 325 := by
          calc
            (2 : Nat) ^ 1076 = ((2 : Nat) ^ 53) ^ 20 * 2 ^ 16 := by
              rw [show 1076 = 53 * 20 + 16 by norm_num, pow_add, ← pow_mul]
            _ < (10 ^ 16) ^ 20 * 10 ^ 5 := by gcongr
            _ = 10 ^ 325 := by
              rw [← pow_mul, ← pow_add]
        have hfirst : (10 : Real) ^ (-325 : Int) <
            (2 : Real) ^ (-1076 : Int) := by
          rw [zpow_neg, zpow_neg]
          field_simp
          change (2 : Real) ^ 1076 < (10 : Real) ^ 325
          exact_mod_cast hpow10
        have hsecond : (10 : Real) ^ (-1093 : Int) <
            (10 : Real) ^ (-325 : Int) := by
          gcongr <;> norm_num
        calc
          (10 : Real) ^ (-325 : Int) + (10 : Real) ^ (-1093 : Int) <
              (2 : Real) ^ (-1076 : Int) + (2 : Real) ^ (-1076 : Int) :=
            add_lt_add hfirst (hsecond.trans_le hfirst.le)
          _ = 2 * (2 : Real) ^ (-1076 : Int) := by ring
          _ = (2 : Real) ^ (-1075 : Int) := by
            calc
              2 * (2 : Real) ^ (-1076 : Int) =
                  (2 : Real) ^ (1 : Int) * (2 : Real) ^ (-1076 : Int) := by norm_num
              _ = (2 : Real) ^ (1 + (-1076 : Int)) := by
                rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
              _ = (2 : Real) ^ (-1075 : Int) := by norm_num
      have htotal := (add_lt_add hvalue hetaCut).trans_le
        (add_le_add hpow hcut)
      have htotal' := htotal.trans hsmall
      rw [hAe]
      simpa [digits, integer] using htotal'
    refine ⟨?_, hnonnegative, hAupper, 0, by norm_num, ?_, ?_, ?_, ?_, ?_⟩
    · rw [hm, hp]
      norm_num
    · rw [hm, hp]
      norm_num
    · have hmono : (2 : Real) ^ (-(1075 : Int)) ≤
          (2 : Real) ^ (-(1074 : Int)) := by
        gcongr <;> norm_num
      have hmul : (2 : Real) ^ (-(1074 : Int)) ≤
          (2 ^ 53 : Nat) * (2 : Real) ^ (-(1074 : Int)) := by
        have hone : (1 : Real) ≤ (2 ^ 53 : Nat) := by norm_num
        have hnonneg : 0 ≤ (2 : Real) ^ (-(1074 : Int)) :=
          zpow_nonneg (by norm_num) _
        have := mul_le_mul_of_nonneg_right hone hnonneg
        simpa using this
      exact (le_of_lt hAupper).trans (hmono.trans hmul)
    · simpa using (le_of_lt hAupper)
    · simpa using add_nonneg hnonnegative hhalf
    · intro hmid
      rcases hmid with hmid | hmid
      · have hmidplus : (((0 : Nat) : Real) + 1 / 2) *
            (2 : Real) ^ (-(1074 : Int)) =
            (2 : Real) ^ (-(1075 : Int)) := by
          calc
            (((0 : Nat) : Real) + 1 / 2) * (2 : Real) ^ (-(1074 : Int)) =
                (2 : Real) ^ (-1 : Int) * (2 : Real) ^ (-(1074 : Int)) := by
              norm_num [zpow_neg, div_eq_mul_inv]
            _ = (2 : Real) ^ (-1 + (-(1074 : Int))) := by
              rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
            _ = (2 : Real) ^ (-(1075 : Int)) := by norm_num
        have heq : DecimalGoalVocabularyDefault.accumulatedMagnitude x =
            (2 : Real) ^ (-(1075 : Int)) := hmid.trans hmidplus
        exact False.elim ((ne_of_lt hAupper) heq)
      · have hmidplus : ((0 : Real) + 1 / 2) *
            (2 : Real) ^ (-(1074 : Int)) =
            (2 : Real) ^ (-(1075 : Int)) := by
          calc
            ((0 : Real) + 1 / 2) * (2 : Real) ^ (-(1074 : Int)) =
                (2 : Real) ^ (-1 : Int) * (2 : Real) ^ (-(1074 : Int)) := by
              norm_num [zpow_neg, div_eq_mul_inv]
            _ = (2 : Real) ^ (-1 + (-(1074 : Int))) := by
              rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
            _ = (2 : Real) ^ (-(1075 : Int)) := by norm_num
        have hmidneg : (((0 : Nat) : Real) - 1 / 2) *
            (2 : Real) ^ (-(1074 : Int)) =
            -((2 : Real) ^ (-(1075 : Int))) := by
          calc
            (((0 : Nat) : Real) - 1 / 2) * (2 : Real) ^ (-(1074 : Int)) =
                -(((0 : Real) + 1 / 2) * (2 : Real) ^ (-(1074 : Int))) := by ring
            _ = -((2 : Real) ^ (-(1075 : Int))) := by rw [hmidplus]
        have heq : DecimalGoalVocabularyDefault.accumulatedMagnitude x =
            -((2 : Real) ^ (-(1075 : Int))) := hmid.trans hmidneg
        have hneg : DecimalGoalVocabularyDefault.accumulatedMagnitude x < 0 := by
          rw [heq]
          exact neg_lt_zero.mpr (zpow_pos (by norm_num) _)
        exact False.elim ((not_lt_of_ge hnonnegative) hneg)
  by_cases hhigh : 310 ≤ d.decimal_point.val
  · have hz : num.imp.dec2flt.common.BiasedFp.zero_pow2 0#i32 =
        ok { m := 0#u64, p_biased := 0#i32 } := by rfl
    have hi : num.imp.dec2flt.common.BiasedFp.zero_pow2 2047#i32 =
        ok { m := 0#u64, p_biased := 2047#i32 } := by rfl
    unfold num.imp.dec2flt.slow.parse_long_mantissa at hslow
    rw [hz, hinf] at hslow
    simp only [bind_tc_ok] at hslow
    simp only [hi, bind_tc_ok] at hslow
    obtain ⟨d0, hd0, hslow⟩ := bind_ok_inv _ _ _ hslow
    rw [hparse] at hd0
    have hd0eq : d0 = d := by cases hd0; rfl
    subst d0
    have hempty' : d.num_digits ≠ 0#usize := by
      intro he
      apply hempty
      exact congrArg UScalar.val he
    simp only [hempty', if_false] at hslow
    have hlow' : ¬ d.decimal_point < (-324)#i32 := by
      intro h
      apply hlow
      simpa only [IScalar.lt_equiv] using h
    simp only [hlow', if_false] at hslow
    have hhigh' : d.decimal_point ≥ 310#i32 := by
      simpa only [ge_iff_le, IScalar.le_equiv] using hhigh
    simp only [hhigh', if_true] at hslow
    have hpair : ({ m := 0#u64, p_biased := 2047#i32 } :
        num.imp.dec2flt.common.BiasedFp) = tr.selected_pair := by
      injection hslow
    have hm : tr.selected_pair.m.val = 0 := by
      simpa using congrArg (fun q => q.m.val) hpair.symm
    have hp : tr.selected_pair.p_biased.val = 2047 := by
      simpa using congrArg (fun q => q.p_biased.val) hpair.symm
    apply Or.inr
    apply Or.inr
    apply Or.inl
    dsimp
    constructor
    · rw [hm, hp]
      norm_num [Int.toNat]
    · obtain ⟨eta, hAe, heta0, hetaCut, hetaSticky, _, _⟩ := heta
      have hnotzero : ¬ (d.num_digits.val = 0 ∧
          (d.digits.val.take d.num_digits.val).map (fun q => q.val) = [] ∧
          d.truncated = false ∧
          DecimalGoalVocabularyDefault.accumulatedMagnitude x = 0) := by
        intro hbad
        exact hempty hbad.1
      obtain ⟨z, zs, hdigitEq, hz1, hz9, hlen⟩ :=
        hshape.resolve_left hnotzero
      let digits := (d.digits.val.take d.num_digits.val).map (fun q => q.val)
      let integer := digits.foldl (fun n digit => 10 * n + digit) 0
      have hdigitsNat : ∀ q, q ∈ zs -> q < 10 := by
        intro q hq
        have hqd : q ∈ digits := by
          dsimp [digits]
          rw [hdigitEq]
          exact List.mem_cons_of_mem z hq
        rcases List.mem_map.mp hqd with ⟨u, hu, rfl⟩
        exact hdigit u hu
      have hprefix := NumberHelperDecimalPrefixInterval [z] zs
        hdigitsNat (d.decimal_point.val - (d.num_digits.val : Int))
      have hlen' : digits.length = d.num_digits.val := by
        simpa [digits] using hlen
      have hzslen : zs.length + 1 = d.num_digits.val := by
        have htmp : (z :: zs).length = d.num_digits.val := by
          rw [← hdigitEq]
          exact hlen'
        simpa using htmp
      have hbound := hprefix.1
      simp only [List.foldl, List.nil_append, Nat.cast_zero, Nat.cast_one,
        zero_mul, one_mul] at hbound
      have hbound' : (z : Real) * (10 : Real) ^
          (d.decimal_point.val - (d.num_digits.val : Int) + (zs.length : Int)) ≤
          (integer : Real) * (10 : Real) ^
            (d.decimal_point.val - (d.num_digits.val : Int)) := by
        simpa [integer, digits, hdigitEq] using hbound
      have hbound'' : (10 : Real) ^
          (d.decimal_point.val - (d.num_digits.val : Int) + (zs.length : Int)) ≤
          (integer : Real) * (10 : Real) ^
            (d.decimal_point.val - (d.num_digits.val : Int)) := by
        have hzreal : (1 : Real) ≤ z := by exact_mod_cast hz1
        have hpowpos : 0 ≤ (10 : Real) ^
            (d.decimal_point.val - (d.num_digits.val : Int) + (zs.length : Int)) :=
          (zpow_pos (by norm_num) _).le
        calc
          (10 : Real) ^
              (d.decimal_point.val - (d.num_digits.val : Int) + (zs.length : Int)) =
              1 * (10 : Real) ^
                (d.decimal_point.val - (d.num_digits.val : Int) + (zs.length : Int)) := by ring
          _ ≤ (z : Real) * (10 : Real) ^
              (d.decimal_point.val - (d.num_digits.val : Int) + (zs.length : Int)) :=
            mul_le_mul_of_nonneg_right hzreal hpowpos
          _ ≤ _ := hbound'
      have hexponent : d.decimal_point.val - (d.num_digits.val : Int) +
          (zs.length : Int) = d.decimal_point.val - 1 := by omega
      rw [hexponent] at hbound''
      have hvalueLower : (10 : Real) ^ 309 ≤
          (integer : Real) * (10 : Real) ^
            (d.decimal_point.val - (d.num_digits.val : Int)) := by
        have hpoint : (309 : Int) ≤ d.decimal_point.val - 1 := by omega
        have hpowlower : (10 : Real) ^ (309 : Int) ≤
            (10 : Real) ^ (d.decimal_point.val - 1) := by
          gcongr <;> norm_num
        exact hpowlower.trans hbound''
      have hAvalue : (10 : Real) ^ 309 ≤
          DecimalGoalVocabularyDefault.accumulatedMagnitude x := by
        rw [hAe]
        have hnonnegvalue : 0 ≤ (integer : Real) * (10 : Real) ^
            (d.decimal_point.val - (d.num_digits.val : Int)) := by
          exact mul_nonneg (Nat.cast_nonneg _) (zpow_nonneg (by norm_num) _)
        linarith
      have hthreshold : (2 : Real) ^ (1024 : Nat) -
          (2 : Real) ^ (970 : Nat) ≤
          DecimalGoalVocabularyDefault.accumulatedMagnitude x := by
        have hpow : (2 : Real) ^ (1024 : Nat) < (10 : Real) ^ 309 := by
          set_option exponentiation.threshold 2048 in norm_num
        have hsub : (2 : Real) ^ (1024 : Nat) -
            (2 : Real) ^ (970 : Nat) < (10 : Real) ^ 309 := by
          have hpos : 0 < (2 : Real) ^ (970 : Nat) := by positivity
          exact (sub_lt_self _ hpos).trans hpow
        exact hsub.le.trans hAvalue
      exact hthreshold
  have hlow' : -324 ≤ d.decimal_point.val := by omega
  have hhigh' : d.decimal_point.val < 310 := by omega
  apply Or.inr
  apply Or.inr
  apply Or.inr
  rcases heta with ⟨eta, hAe, heta0, hetaCut, hetaSticky, _, _⟩
  refine ⟨d, hparse, hslow, hassembled, ?_, hlow', hhigh', hdcount, hdigit, ?_,
    ⟨eta, hAe, heta0, hetaCut, hetaSticky⟩⟩
  · simp [hempty]
  · exact hshape.resolve_left (by
      intro hbad
      exact hempty hbad.1)

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (201)
More program dependencies (159)
Standard-library dependencies (39)

Related source and models come from the extraction records and declared dependencies.

Proof assumptions and review

Lean checks the formal proof. Trellis also reviews how the statement corresponds to the intended requirement.

Transitive assumptions

This list combines the assumptions recorded for this declaration and its dependencies. It may include assumptions used by auxiliary declarations in those modules.

Helper theorems included
All 214 entries
Technical checking records
Local closure summary
{
  "node": "ParserSlowEarlyExits",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "8f792fbe9f0657e174a1a3b14eef9b092fdafc16f14cb51651a75b2604627de1",
  "active_statement_hash": "8b8bbc14955c6c39b3bd85cbc43a09b073b7c3d9bb0ad4193f0f7d5e4132ef97",
  "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": "sidecar-sc-20261005-024220-e6690c-ParserSlowEarlyExits-cycle-175",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "NumberHelperDecimalPrefixInterval": "5c32858414034b55",
    "ParserExponentConstants": "e2a30a7b0c5e9c91",
    "ParserSlowInitialCanonicality": "e5f61b97b4ad24f2",
    "num_imp_dec2flt_slow_parse_long_mantissa": "a4d13e23fad8f54e",
    "num_imp_traits_Float_EXP_BIAS_default": "39417be39e20ce5d",
    "num_imp_traits_Float_EXP_BITS_default": "9ab622d85ef04d94",
    "num_imp_traits_Float_EXP_MIN_default": "39417be39e20ce5d",
    "num_imp_traits_Float_EXP_SAT_default": "9ab622d85ef04d94",
    "num_imp_traits_Float_SIG_BITS_default": "9ab622d85ef04d94"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "65227461bf3cac06",
    "DecimalGoalVocabularyDefault": "512f8d0aea894250",
    "DecimalLexeme": "c43614ff2149ecf2",
    "F64": "ec4e25fbaf4e11d4",
    "F64_Insts_CoreOpsArithDivF64F64": "dc44e13a99fc6bed",
    "F64_Insts_CoreOpsArithMulF64F64": "1ae1a9021a654abd",
    "F64_Insts_CoreOpsArithNegF64": "2d9be310c546ea2e",
    "F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64": "30963b4ef656cc79",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64": "94095c9bf79da164",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_BITS": "d9fd91641cb78c5e",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SIG_TOTAL_BITS": "887b52d84a3fc9c4",
    "num_float_parse_ParseFloatError": "c43614ff2149ecf2",
    "num_imp_dec2flt_Dec2FltAssemblyTrace": "2a4d2e4074960bd4",
    "num_imp_dec2flt_Dec2FltTrace": "644abe66cec8ac98",
    "num_imp_dec2flt_common_BiasedFp": "c43614ff2149ecf2",
    "num_imp_dec2flt_common_BiasedFp_zero_pow2": "0f99ae12c6707137",
    "num_imp_dec2flt_dec2flt_with_trace": "ebe6a57c6dce0df6",
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "59bf1ff9fe88fa8d",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift": "fd298e37d5ae4f86",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "1aaf1bb11c0b6a49",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_round": "8ea3c058fea09978",
    "num_imp_dec2flt_decimal_seq_parse_decimal_seq": "244cf8318357caab",
    "num_imp_dec2flt_slow_parse_long_mantissa": "b1ccfebea6d0fad3",
    "num_imp_traits_Float": "5bbdcb7d46520e94"
  }
}
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:7b4358db77fc2ff6ccd7f98f78e631121f9e9d8c9c88d195fcd6b293fdbebd1b",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}