ParserAssembledDirectMagnitudeRounding

Lean checked

Statement

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

Lemma

Suppose a source-valid string s renders a valid numeric lexeme x, its successful traced result is an Assembled record t, and t.\mathit{slow\_path\_guard} is false. Then t.\mathit{assembly\_result} rounds the accumulated-exponent magnitude of x.

Lean statement

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

import Tablet.ParserNumericTraceShape
import Tablet.RoundsMagnitudeInterval
import Tablet.AssemblyCorrespondence
import Tablet.DecimalGoalVocabularyDefault
import Tablet.IntegerCorrectOrDefer
import Tablet.NumberHelperAccumulatedValue
import Tablet.num_imp_dec2flt_biased_fp_to_float
import Tablet.num_imp_dec2flt_common_BiasedFp_Insts_CoreCmpPartialEqBiasedFp_eq
import Tablet.num_imp_dec2flt_dec2flt_with_trace

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: ParserAssembledDirectMagnitudeRounding]
theorem ParserAssembledDirectMagnitudeRounding :
    ∀ (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 = false ->
      DecimalGoalVocabularyDefault.roundsMagnitude
        (DecimalGoalVocabularyDefault.accumulatedMagnitude x)
        tr.assembly_result := 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

Apply ParserNumericTraceShape to the successful trace. The fast alternative contradicts distinctness of the trace constructors. Constructor injectivity in the assembled alternative identifies its record with t and provides a parsed record d. With x_0 denoting x with only its leading sign cleared, the strengthened statement supplies
\begin{gathered} t.\mathit{body}.\mathit{val}\ne[],\qquad \texttt{SourceValidByteSlice}(t.\mathit{body}),\\ \operatorname{bytes}(t.\mathit{body}) =\operatorname{renderNumeric}(x_0),\qquad t.\mathit{parse\_number\_result}=\texttt{some }d,\\ \texttt{parse\_number}(t.\mathit{body}) =\texttt{ok (some }d\texttt{)}. \tag{1} \end{gathered}
Unfold DecimalGoalVocabularyDefault. Clearing leadingSign preserves validNumeric, gives x_0.\mathit{leadingSign}=\texttt{none}, and changes neither the significand nor either exponent field. Hence
\operatorname{accumulatedMagnitude}(x_0) =\operatorname{accumulatedMagnitude}(x). \tag{2}
Apply NumberHelperAccumulatedValue to (1), the validity facts just proved, and then rewrite with (2). Put
L=d.\mathit{mantissa}.\mathit{val}\,10^{d.\mathit{exponent}.\mathit{val}}, \quad U=(d.\mathit{mantissa}.\mathit{val}+1)10^{d.\mathit{exponent}.\mathit{val}}, \quad A=\operatorname{accumulatedMagnitude}(x).
We obtain
(d.\mathit{many\_digits}=\mathrm{false}\ \wedge\ L=A) \quad\text{or}\quad (d.\mathit{many\_digits}=\mathrm{true}\ \wedge\ L\le A<U). \tag{3}
Invert the local assembled-trace equality through num imp dec2flt dec2flt with trace, using the exact body and parse equations in (1). Let p_0=t.\mathit{first\_compute\_result}. The trace fields and the extracted binds give
\texttt{compute\_float}(d.\mathit{exponent},d.\mathit{mantissa}) =\texttt{ok }p_0, \tag{4}
and identify all the recorded second-computation, comparison, post-comparison, selected-pair, and assembly fields with the corresponding local values in that body. Because the recorded slow guard is false, its guard test did not find a negative biased exponent: the selected pair is the post-comparison pair p and
0\le p.\mathit{p\_biased}.\mathit{val}. \tag{5}
First take the false-many_digits arm of (3). The extracted branch sets its second-computation guard false and p=p_0. Apply IntegerCorrectOrDefer to (4). Its negative-exponent alternative contradicts (5); therefore it supplies a binary64 word m which assembles from p and rounds L=A.
Now take the true-many_digits arm. If p_0 had negative biased exponent, the extracted second-computation guard would be false, the post-comparison pair would remain p_0, and the slow guard would be true, contrary to the hypothesis. Thus p_0 is nonnegative and the second guard is true. Normal return of the whole trace forces the checked increment to succeed with the U64 value d.\mathit{mantissa}.\mathit{val}+1 and forces a second normal equation
\texttt{compute\_float}(d.\mathit{exponent}, d.\mathit{mantissa}+1)=\texttt{ok }p_1. \tag{6}
The next extracted call compares p_0 and p_1. A false result replaces the post-comparison biased exponent by -1 and hence makes the slow guard true. It is therefore impossible here, so the trace records
\texttt{BiasedFp.eq}(p_0,p_1)=\texttt{ok true}. \tag{7}
Unfold precisely num imp dec2flt common BiasedFp Insts CoreCmpPartialEqBiasedFp eq in (7). Its outer test first forces p_0.m=p_1.m; otherwise the return is ok false. In that true arm, its returned Boolean is the equality of the two p_biased fields, so (7) also forces p_0.\mathit{p\_biased}=p_1.\mathit{p\_biased}. The two-field structure is therefore equal componentwise: p_0=p_1. This is the required fieldwise pair equality; it is not inferred merely from an opaque Boolean comparison. The post-comparison and selected pair are consequently the common pair p=p_0=p_1, which is nonnegative by (5).
Apply IntegerCorrectOrDefer separately to (4) and (6). Their negative alternatives contradict nonnegativity of the common pair. The remaining alternatives supply words assembling from the same p and rounding L and U, respectively. Unfolding the explicit assembledWord field of DecimalGoalVocabularyDefault shows that an assembled raw word is a function of the two fields of p; hence the two supplied words are the same word m. Since (3) gives L\le A<U, in particular L\le A\le U, and RoundsMagnitudeInterval proves that this common m rounds A.
It remains in either arm to identify m with the trace's assembly result. The successful extracted continuation calls num imp dec2flt biased fp to float on the selected pair p and records its returned word as t.\mathit{assembly\_result}. AssemblyCorrespondence gives a normal return of that same call and the same explicit assembledWord equation. Result injectivity and the explicit raw-word equality therefore identify the word assembled from p with t.\mathit{assembly\_result} (an Aeneas U64 carrier is determined by its raw value). Rewriting the rounding conclusion for m proves that t.\mathit{assembly\_result} rounds A, as required.

Lean proof


  set_option maxHeartbeats 800000 in
  intro s x f tr hsource hvalid hbytes htrace hslow
  have bind_ok_inv : ∀ {α β : Type} (r : Result α) (g : α → Result β) y,
      Bind.bind r g = ok y → ∃ z, r = ok z ∧ g z = ok y := by
    intro α β r g y hr
    cases r with
    | fail err => simp [Bind.bind, Aeneas.Std.bind] at hr
    | div => simp [Bind.bind, Aeneas.Std.bind] at hr
    | ok z => exact ⟨z, rfl, hr⟩
  have biased_pair_ext : ∀ (a b : num.imp.dec2flt.common.BiasedFp),
      a.m = b.m -> a.p_biased = b.p_biased -> a = b := by
    intro a b hm hp
    cases a with
    | mk am ap =>
        cases b with
        | mk bm bp => simp_all
  obtain hshape := ParserNumericTraceShape s x f
    (num.imp.dec2flt.Dec2FltTrace.Assembled tr)
    hsource hvalid hbytes htrace
  rcases hshape with hfast | hassembled
  · rcases hfast with ⟨body, negative, parsed, fastInput, fastResult,
      heq, hrest⟩
    cases heq
  · rcases hassembled with ⟨assembly, parsed, htraceeq, hne, hbody,
      hrender, hnegative, hparsed, hparse⟩
    cases htraceeq
    let unsigned : DecimalLexeme := { x with leadingSign := none }
    have unsigned_valid : DecimalGoalVocabularyDefault.validNumeric unsigned := by
      simpa [DecimalGoalVocabularyDefault, unsigned] using hvalid
    have magnitude_facts := NumberHelperAccumulatedValue
      tr.body unsigned parsed hbody hne rfl unsigned_valid hrender hparse
    have unsigned_magnitude :
        DecimalGoalVocabularyDefault.accumulatedMagnitude unsigned =
          DecimalGoalVocabularyDefault.accumulatedMagnitude x := by
      simp [DecimalGoalVocabularyDefault, unsigned]
    rw [unsigned_magnitude] at magnitude_facts
    unfold num.imp.dec2flt.dec2flt_with_trace at htrace
    obtain ⟨body0, hbody0, htrace⟩ := bind_ok_inv _ _ _ htrace
    rcases s with ⟨source, source_bound⟩
    cases source with
    | nil => cases htrace
    | cons first tail =>
      have hbodyEq : body0 = (⟨first :: tail, source_bound⟩ : Slice Std.U8) := by
        simpa [core.str.Str.as_bytes, ModelStrAsBytes] using hbody0.symm
      subst body0
      obtain ⟨firstOption, hfirst, htrace⟩ := bind_ok_inv _ _ _ htrace
      have hfirstEq : firstOption = some first := by
        simpa [core.slice.Slice.first, ModelSliceFirst] using hfirst.symm
      subst firstOption
      obtain ⟨body1, hbody1, htrace⟩ := bind_ok_inv _ _ _ htrace
      obtain ⟨empty, hempty, htrace⟩ := bind_ok_inv _ _ _ htrace
      by_cases emptyTrue : empty = true
      · simp only [emptyTrue, if_true] at htrace
        cases htrace
      · have emptyFalse : empty = false := Bool.eq_false_of_not_eq_true emptyTrue
        simp only [emptyFalse, if_false] at htrace
        obtain ⟨parsedOption, hparsedOption, htrace⟩ := bind_ok_inv _ _ _ htrace
        cases parsedOption with
        | none =>
            obtain ⟨specialOption, hspecialOption, htrace⟩ :=
              bind_ok_inv _ _ _ htrace
            cases specialOption with
            | none =>
                obtain ⟨pfe, hpfe, htrace⟩ := bind_ok_inv _ _ _ htrace
                cases htrace
            | some value => cases htrace
        | some r =>
            obtain ⟨fastPath, hfastPath, htrace⟩ := bind_ok_inv _ _ _ htrace
            cases fastPath with
            | some value => cases htrace
            | none =>
                obtain ⟨firstCompute, hfirstCompute, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                obtain ⟨phaseOne, hphaseOne, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                rcases phaseOne with
                  ⟨manyOne, manyTwo, firstPairOne, fp, secondGuard⟩
                obtain ⟨phaseTwo, hphaseTwo, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                rcases phaseTwo with
                  ⟨postPair, secondExponent, secondMantissa, secondResult,
                    secondEqual, forced⟩
                obtain ⟨phaseThree, hphaseThree, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                rcases phaseThree with ⟨selected, slowGuard, slowResult⟩
                obtain ⟨indexValue, hindexValue, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                obtain ⟨indexShift, hindexShift, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                obtain ⟨shifted, hshifted, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                obtain ⟨assembledWord, hassembledWord, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                obtain ⟨assemblyResult, hassemblyResult, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                have first_eq : fp = firstCompute := by
                  by_cases many : r.many_digits = true
                  · simp only [many, if_true] at hphaseOne
                    cases hphaseOne
                    rfl
                  · simp only [many, if_false] at hphaseOne
                    cases hphaseOne
                    rfl
                have first_compute :
                    num.imp.dec2flt.lemire.compute_float
                      F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
                      r.exponent r.mantissa = ok fp := by
                  simpa [first_eq] using hfirstCompute
                by_cases negative : first = 45#u8
                · simp only [negative, if_true] at htrace
                  obtain ⟨result, hresult, htrace⟩ :=
                    bind_ok_inv _ _ _ htrace
                  cases htrace
                  cases hparsed
                  have guard_false : slowGuard = false := by
                    simpa using hslow
                  change DecimalGoalVocabularyDefault.roundsMagnitude
                    (DecimalGoalVocabularyDefault.accumulatedMagnitude x) assemblyResult
                  have finish : ∀ (w : F64),
                      DecimalGoalVocabularyDefault.assembledWord selected w ->
                      DecimalGoalVocabularyDefault.roundsMagnitude
                        (DecimalGoalVocabularyDefault.accumulatedMagnitude x) w ->
                      DecimalGoalVocabularyDefault.roundsMagnitude
                        (DecimalGoalVocabularyDefault.accumulatedMagnitude x) assemblyResult := by
                    intro w hw hround
                    obtain ⟨canonical, hcanonical, hcanonicalWord⟩ :=
                      AssemblyCorrespondence selected
                    have hcall :
                        num.imp.dec2flt.biased_fp_to_float
                          F64.Insts.Dec2flt_modelNumImpTraitsFloatExtU64 selected = ok assemblyResult := by
                      simpa using hassemblyResult
                    have hresult : ok canonical = ok assemblyResult :=
                      hcanonical.symm.trans hcall
                    have ceq : canonical = assemblyResult := by
                      cases hresult
                      rfl
                    have hval : w.val = canonical.val := by
                      dsimp only [DecimalGoalVocabularyDefault] at hw hcanonicalWord
                      exact hw.trans hcanonicalWord.symm
                    have weq : w = canonical := UScalar.eq_of_val_eq hval
                    cases weq
                    cases ceq
                    exact hround
                  have post_nonnegative : ¬ postPair.p_biased < 0#i32 := by
                    intro hpost
                    simp only [hpost, if_true] at hphaseThree
                    obtain ⟨replacement, hreplacement, hphaseThree⟩ :=
                      bind_ok_inv _ _ _ hphaseThree
                    cases hphaseThree
                    simp_all
                  simp only [post_nonnegative, if_false] at hphaseThree
                  cases hphaseThree
                  by_cases manyFalse : r.many_digits = false
                  · have manyNotTrue : ¬ r.many_digits = true := by
                      intro hmany
                      have contradiction : false = true := manyFalse.symm.trans hmany
                      cases contradiction
                    simp only [manyNotTrue, if_false] at hphaseOne
                    cases hphaseOne
                    have phase_two_flat :
                        ok (firstCompute, none, none, none, none, false) =
                          ok (postPair, secondExponent, secondMantissa,
                            secondResult, secondEqual, forced) := by
                      simpa only [Bool.false_eq_true, if_false] using hphaseTwo
                    have phase_two_data := Result.ok.inj phase_two_flat
                    have post_eq : firstCompute = postPair :=
                      congrArg (fun z => z.1) phase_two_data
                    cases post_eq
                    have nonnegative : 0 ≤ firstCompute.p_biased.val := by
                      by_contra hnon
                      have pneg : firstCompute.p_biased < 0#i32 :=
                        lt_of_not_ge hnon
                      exact post_nonnegative pneg
                    rcases IntegerCorrectOrDefer r.exponent r.mantissa firstCompute
                      first_compute with hbad | ⟨target, hword, hround⟩
                    · omega
                    · rcases magnitude_facts with ⟨hmany, hmag⟩ | ⟨hmany, hlo, hhi⟩
                      · have hround' := hround
                        have htargetRound :
                            DecimalGoalVocabularyDefault.roundsMagnitude
                              (DecimalGoalVocabularyDefault.accumulatedMagnitude x) target := by
                          rw [← hmag]
                          exact hround'
                        exact finish target hword htargetRound
                      · simp at hmany
                  · have manyTrue : r.many_digits = true := by
                      cases hmany : r.many_digits with
                      | false => exact False.elim (manyFalse hmany)
                      | true => rfl
                    simp only [manyTrue, if_true] at hphaseOne
                    cases hphaseOne
                    have pair_nonnegative : 0 ≤ firstCompute.p_biased.val := by
                      by_contra hpair
                      have pair_negative : firstCompute.p_biased < 0#i32 :=
                        lt_of_not_ge hpair
                      have dec_false :
                          decide (firstCompute.p_biased ≥ 0#i32) = false := by
                        apply decide_eq_false_iff_not.mpr
                        exact not_le_of_gt pair_negative
                      simp only [dec_false, if_false] at hphaseTwo
                      have phase_two_flat :
                          ok (firstCompute, none, none, none, none, false) =
                            ok (postPair, secondExponent, secondMantissa,
                              secondResult, secondEqual, forced) := by
                        simpa using hphaseTwo
                      have phase_two_data := Result.ok.inj phase_two_flat
                      have post_eq : firstCompute = postPair :=
                        congrArg (fun z => z.1) phase_two_data
                      cases post_eq
                      exact post_nonnegative pair_negative
                    have dec_true :
                        decide (firstCompute.p_biased ≥ 0#i32) = true := by
                      simp [pair_nonnegative]
                    simp only [dec_true, if_true] at hphaseTwo
                    obtain ⟨incremented, hincremented, hphaseTwo⟩ :=
                      bind_ok_inv _ _ _ hphaseTwo
                    obtain ⟨secondPair, hsecondPair, hphaseTwo⟩ :=
                      bind_ok_inv _ _ _ hphaseTwo
                    obtain ⟨equal, hequal, hphaseTwo⟩ :=
                      bind_ok_inv _ _ _ hphaseTwo
                    obtain ⟨chosen, hchosen, hphaseTwo⟩ :=
                      bind_ok_inv _ _ _ hphaseTwo
                    rcases chosen with ⟨chosenPair, forcedDeferral⟩
                    have equal_true : equal = true := by
                      by_contra heq
                      have equal_false : equal = false :=
                        Bool.eq_false_of_not_eq_true heq
                      simp only [equal_false, if_false] at hchosen
                      cases hchosen
                      have phase_two_flat :
                          ok ({ m := firstCompute.m, p_biased := (-1)#i32 },
                            some r.exponent, some incremented, some secondPair,
                            some equal, true) =
                            ok (postPair, secondExponent, secondMantissa,
                              secondResult, secondEqual, forced) := by
                        simpa using hphaseTwo
                      have phase_two_data := Result.ok.inj phase_two_flat
                      have post_eq :
                          ({ m := firstCompute.m, p_biased := (-1)#i32 } :
                            num.imp.dec2flt.common.BiasedFp) = postPair :=
                        congrArg (fun z => z.1) phase_two_data
                      cases post_eq
                      apply post_nonnegative
                      norm_num
                    simp only [equal_true, if_true] at hchosen
                    cases hchosen
                    have phase_two_flat :
                        ok (firstCompute, some r.exponent, some incremented,
                          some secondPair, some equal, false) =
                          ok (postPair, secondExponent, secondMantissa,
                            secondResult, secondEqual, forced) := by
                      simpa using hphaseTwo
                    have phase_two_data := Result.ok.inj phase_two_flat
                    have post_eq : firstCompute = postPair :=
                      congrArg (fun z => z.1) phase_two_data
                    cases post_eq
                    have incremented_value :
                        incremented.val = r.mantissa.val + 1 := by
                      have hadd := UScalar.add_equiv r.mantissa (1#u64)
                      rw [hincremented] at hadd
                      simp at hadd
                      omega
                    have second_compute :
                        num.imp.dec2flt.lemire.compute_float
                          F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
                          r.exponent incremented = ok secondPair := by
                      simpa using hsecondPair
                    have pair_eq : firstCompute = secondPair := by
                      have hm : firstCompute.m = secondPair.m := by
                        by_contra hne
                        have equal_false : equal = false := by
                          simpa [num.imp.dec2flt.common.BiasedFp.Insts.CoreCmpPartialEqBiasedFp.eq,
                            hne] using hequal
                        cases equal_false
                        cases equal_true
                      have hp : firstCompute.p_biased = secondPair.p_biased := by
                        have hh := hequal
                        simp [num.imp.dec2flt.common.BiasedFp.Insts.CoreCmpPartialEqBiasedFp.eq,
                          hm, equal_true] at hh
                        exact hh
                      exact biased_pair_ext firstCompute secondPair hm hp
                    subst secondPair
                    have nonnegative : 0 ≤ firstCompute.p_biased.val := by omega
                    rcases IntegerCorrectOrDefer r.exponent r.mantissa firstCompute
                      first_compute with hbad | ⟨targetLower, hwordLower, hroundLower⟩
                    · omega
                    rcases IntegerCorrectOrDefer r.exponent incremented
                      firstCompute second_compute with hbad | ⟨targetUpper, hwordUpper, hroundUpper⟩
                    · omega
                    · rcases magnitude_facts with ⟨hmany, hmag⟩ | ⟨hmany, hlo, hhi⟩
                      · simp at hmany
                      · have lower_le :
                            ((r.mantissa.val : Real) * (10 : Real) ^ r.exponent.val) ≤
                              DecimalGoalVocabularyDefault.accumulatedMagnitude x := by
                          change ((r.mantissa.val : Real) * (10 : Real) ^ r.exponent.val) ≤
                            DecimalGoalVocabularyDefault.accumulatedMagnitude x at hlo
                          exact hlo
                        have upper_gt :
                            DecimalGoalVocabularyDefault.accumulatedMagnitude x <
                              ((incremented.val : Nat) : Real) *
                                (10 : Real) ^ r.exponent.val := by
                          change DecimalGoalVocabularyDefault.accumulatedMagnitude x <
                            ((r.mantissa.val + 1 : Nat) : Real) *
                              (10 : Real) ^ r.exponent.val at hhi
                          simpa [incremented_value] using hhi
                        have lower_round :
                            DecimalGoalVocabularyDefault.roundsMagnitude
                              ((r.mantissa.val : Real) * (10 : Real) ^ r.exponent.val)
                              targetLower := by
                          exact hroundLower
                        have upper_round :
                            DecimalGoalVocabularyDefault.roundsMagnitude
                              (((incremented.val : Nat) : Real) *
                                (10 : Real) ^ r.exponent.val) targetUpper := by
                          exact hroundUpper
                        have target_eq : targetLower = targetUpper := by
                          have hval : targetLower.val = targetUpper.val := by
                            dsimp only [DecimalGoalVocabularyDefault] at hwordLower hwordUpper
                            exact hwordLower.trans hwordUpper.symm
                          exact UScalar.eq_of_val_eq hval
                        cases target_eq
                        have interval := RoundsMagnitudeInterval
                          ((r.mantissa.val : Real) * (10 : Real) ^ r.exponent.val)
                          (DecimalGoalVocabularyDefault.accumulatedMagnitude x)
                          (((incremented.val : Nat) : Real) *
                            (10 : Real) ^ r.exponent.val)
                          targetLower
                          lower_le (le_of_lt upper_gt)
                          lower_round upper_round
                        exact finish targetLower hwordLower interval
                · simp only [negative, if_false] at htrace
                  cases htrace
                  cases hparsed
                  have guard_false : slowGuard = false := by
                    simpa using hslow
                  change DecimalGoalVocabularyDefault.roundsMagnitude
                    (DecimalGoalVocabularyDefault.accumulatedMagnitude x) f
                  have finish : ∀ (w : F64),
                      DecimalGoalVocabularyDefault.assembledWord selected w ->
                      DecimalGoalVocabularyDefault.roundsMagnitude
                        (DecimalGoalVocabularyDefault.accumulatedMagnitude x) w ->
                      DecimalGoalVocabularyDefault.roundsMagnitude
                        (DecimalGoalVocabularyDefault.accumulatedMagnitude x) f := by
                    intro w hw hround
                    obtain ⟨canonical, hcanonical, hcanonicalWord⟩ :=
                      AssemblyCorrespondence selected
                    have hcall :
                        num.imp.dec2flt.biased_fp_to_float
                          F64.Insts.Dec2flt_modelNumImpTraitsFloatExtU64 selected = ok f := by
                      simpa using hassemblyResult
                    have hresult : ok canonical = ok f :=
                      hcanonical.symm.trans hcall
                    have ceq : canonical = f := by
                      cases hresult
                      rfl
                    have hval : w.val = canonical.val := by
                      dsimp only [DecimalGoalVocabularyDefault] at hw hcanonicalWord
                      exact hw.trans hcanonicalWord.symm
                    have weq : w = canonical := UScalar.eq_of_val_eq hval
                    cases weq
                    cases ceq
                    exact hround
                  have post_nonnegative : ¬ postPair.p_biased < 0#i32 := by
                    intro hpost
                    simp only [hpost, if_true] at hphaseThree
                    obtain ⟨replacement, hreplacement, hphaseThree⟩ :=
                      bind_ok_inv _ _ _ hphaseThree
                    cases hphaseThree
                    simp_all
                  simp only [post_nonnegative, if_false] at hphaseThree
                  cases hphaseThree
                  by_cases manyFalse : r.many_digits = false
                  · have manyNotTrue : ¬ r.many_digits = true := by
                      intro hmany
                      have contradiction : false = true := manyFalse.symm.trans hmany
                      cases contradiction
                    simp only [manyNotTrue, if_false] at hphaseOne
                    cases hphaseOne
                    have phase_two_flat :
                        ok (firstCompute, none, none, none, none, false) =
                          ok (postPair, secondExponent, secondMantissa,
                            secondResult, secondEqual, forced) := by
                      simpa only [Bool.false_eq_true, if_false] using hphaseTwo
                    have phase_two_data := Result.ok.inj phase_two_flat
                    have post_eq : firstCompute = postPair :=
                      congrArg (fun z => z.1) phase_two_data
                    cases post_eq
                    have nonnegative : 0 ≤ firstCompute.p_biased.val := by
                      by_contra hnon
                      have pneg : firstCompute.p_biased < 0#i32 :=
                        lt_of_not_ge hnon
                      exact post_nonnegative pneg
                    rcases IntegerCorrectOrDefer r.exponent r.mantissa firstCompute
                      first_compute with hbad | ⟨target, hword, hround⟩
                    · omega
                    · rcases magnitude_facts with ⟨hmany, hmag⟩ | ⟨hmany, hlo, hhi⟩
                      · have hround' := hround
                        have htargetRound :
                            DecimalGoalVocabularyDefault.roundsMagnitude
                              (DecimalGoalVocabularyDefault.accumulatedMagnitude x) target := by
                          rw [← hmag]
                          exact hround'
                        exact finish target hword htargetRound
                      · simp at hmany
                  · have manyTrue : r.many_digits = true := by
                      cases hmany : r.many_digits with
                      | false => exact False.elim (manyFalse hmany)
                      | true => rfl
                    simp only [manyTrue, if_true] at hphaseOne
                    cases hphaseOne
                    have pair_nonnegative : 0 ≤ firstCompute.p_biased.val := by
                      by_contra hpair
                      have pair_negative : firstCompute.p_biased < 0#i32 :=
                        lt_of_not_ge hpair
                      have dec_false :
                          decide (firstCompute.p_biased ≥ 0#i32) = false := by
                        apply decide_eq_false_iff_not.mpr
                        exact not_le_of_gt pair_negative
                      simp only [dec_false, if_false] at hphaseTwo
                      have phase_two_flat :
                          ok (firstCompute, none, none, none, none, false) =
                            ok (postPair, secondExponent, secondMantissa,
                              secondResult, secondEqual, forced) := by
                        simpa using hphaseTwo
                      have phase_two_data := Result.ok.inj phase_two_flat
                      have post_eq : firstCompute = postPair :=
                        congrArg (fun z => z.1) phase_two_data
                      cases post_eq
                      exact post_nonnegative pair_negative
                    have dec_true :
                        decide (firstCompute.p_biased ≥ 0#i32) = true := by
                      simp [pair_nonnegative]
                    simp only [dec_true, if_true] at hphaseTwo
                    obtain ⟨incremented, hincremented, hphaseTwo⟩ :=
                      bind_ok_inv _ _ _ hphaseTwo
                    obtain ⟨secondPair, hsecondPair, hphaseTwo⟩ :=
                      bind_ok_inv _ _ _ hphaseTwo
                    obtain ⟨equal, hequal, hphaseTwo⟩ :=
                      bind_ok_inv _ _ _ hphaseTwo
                    obtain ⟨chosen, hchosen, hphaseTwo⟩ :=
                      bind_ok_inv _ _ _ hphaseTwo
                    rcases chosen with ⟨chosenPair, forcedDeferral⟩
                    have equal_true : equal = true := by
                      by_contra heq
                      have equal_false : equal = false :=
                        Bool.eq_false_of_not_eq_true heq
                      simp only [equal_false, if_false] at hchosen
                      cases hchosen
                      have phase_two_flat :
                          ok ({ m := firstCompute.m, p_biased := (-1)#i32 },
                            some r.exponent, some incremented, some secondPair,
                            some equal, true) =
                            ok (postPair, secondExponent, secondMantissa,
                              secondResult, secondEqual, forced) := by
                        simpa using hphaseTwo
                      have phase_two_data := Result.ok.inj phase_two_flat
                      have post_eq :
                          ({ m := firstCompute.m, p_biased := (-1)#i32 } :
                            num.imp.dec2flt.common.BiasedFp) = postPair :=
                        congrArg (fun z => z.1) phase_two_data
                      cases post_eq
                      apply post_nonnegative
                      norm_num
                    simp only [equal_true, if_true] at hchosen
                    cases hchosen
                    have phase_two_flat :
                        ok (firstCompute, some r.exponent, some incremented,
                          some secondPair, some equal, false) =
                          ok (postPair, secondExponent, secondMantissa,
                            secondResult, secondEqual, forced) := by
                      simpa using hphaseTwo
                    have phase_two_data := Result.ok.inj phase_two_flat
                    have post_eq : firstCompute = postPair :=
                      congrArg (fun z => z.1) phase_two_data
                    cases post_eq
                    have incremented_value :
                        incremented.val = r.mantissa.val + 1 := by
                      have hadd := UScalar.add_equiv r.mantissa (1#u64)
                      rw [hincremented] at hadd
                      simp at hadd
                      omega
                    have second_compute :
                        num.imp.dec2flt.lemire.compute_float
                          F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
                          r.exponent incremented = ok secondPair := by
                      simpa using hsecondPair
                    have pair_eq : firstCompute = secondPair := by
                      have hm : firstCompute.m = secondPair.m := by
                        by_contra hne
                        have equal_false : equal = false := by
                          simpa [num.imp.dec2flt.common.BiasedFp.Insts.CoreCmpPartialEqBiasedFp.eq,
                            hne] using hequal
                        cases equal_false
                        cases equal_true
                      have hp : firstCompute.p_biased = secondPair.p_biased := by
                        have hh := hequal
                        simp [num.imp.dec2flt.common.BiasedFp.Insts.CoreCmpPartialEqBiasedFp.eq,
                          hm, equal_true] at hh
                        exact hh
                      exact biased_pair_ext firstCompute secondPair hm hp
                    subst secondPair
                    have nonnegative : 0 ≤ firstCompute.p_biased.val := by
                      omega
                    rcases IntegerCorrectOrDefer r.exponent r.mantissa firstCompute
                      first_compute with hbad | ⟨targetLower, hwordLower, hroundLower⟩
                    · omega
                    rcases IntegerCorrectOrDefer r.exponent incremented
                      firstCompute second_compute with hbad | ⟨targetUpper, hwordUpper, hroundUpper⟩
                    · omega
                    · rcases magnitude_facts with ⟨hmany, hmag⟩ | ⟨hmany, hlo, hhi⟩
                      · simp at hmany
                      · have lower_le :
                            ((r.mantissa.val : Real) * (10 : Real) ^ r.exponent.val) ≤
                              DecimalGoalVocabularyDefault.accumulatedMagnitude x := by
                          change ((r.mantissa.val : Real) * (10 : Real) ^ r.exponent.val) ≤
                            DecimalGoalVocabularyDefault.accumulatedMagnitude x at hlo
                          exact hlo
                        have upper_gt :
                            DecimalGoalVocabularyDefault.accumulatedMagnitude x <
                              ((incremented.val : Nat) : Real) *
                                (10 : Real) ^ r.exponent.val := by
                          change DecimalGoalVocabularyDefault.accumulatedMagnitude x <
                            ((r.mantissa.val + 1 : Nat) : Real) *
                              (10 : Real) ^ r.exponent.val at hhi
                          simpa [incremented_value] using hhi
                        have lower_round :
                            DecimalGoalVocabularyDefault.roundsMagnitude
                              ((r.mantissa.val : Real) * (10 : Real) ^ r.exponent.val)
                              targetLower := by
                          exact hroundLower
                        have upper_round :
                            DecimalGoalVocabularyDefault.roundsMagnitude
                              (((incremented.val : Nat) : Real) *
                                (10 : Real) ^ r.exponent.val) targetUpper := by
                          exact hroundUpper
                        have target_eq : targetLower = targetUpper := by
                          have hval : targetLower.val = targetUpper.val := by
                            dsimp only [DecimalGoalVocabularyDefault] at hwordLower hwordUpper
                            exact hwordLower.trans hwordUpper.symm
                          exact UScalar.eq_of_val_eq hval
                        cases target_eq
                        have interval := RoundsMagnitudeInterval
                          ((r.mantissa.val : Real) * (10 : Real) ^ r.exponent.val)
                          (DecimalGoalVocabularyDefault.accumulatedMagnitude x)
                          (((incremented.val : Nat) : Real) *
                            (10 : Real) ^ r.exponent.val)
                          targetLower
                          lower_le (le_of_lt upper_gt)
                          lower_round upper_round
                        exact finish targetLower hwordLower interval

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.ParserNumericTraceShape
import Tablet.RoundsMagnitudeInterval
import Tablet.AssemblyCorrespondence
import Tablet.DecimalGoalVocabularyDefault
import Tablet.IntegerCorrectOrDefer
import Tablet.NumberHelperAccumulatedValue
import Tablet.num_imp_dec2flt_biased_fp_to_float
import Tablet.num_imp_dec2flt_common_BiasedFp_Insts_CoreCmpPartialEqBiasedFp_eq
import Tablet.num_imp_dec2flt_dec2flt_with_trace

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: ParserAssembledDirectMagnitudeRounding]
theorem ParserAssembledDirectMagnitudeRounding :
    ∀ (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 = false ->
      DecimalGoalVocabularyDefault.roundsMagnitude
        (DecimalGoalVocabularyDefault.accumulatedMagnitude x)
        tr.assembly_result := by
-- BODY
  set_option maxHeartbeats 800000 in
  intro s x f tr hsource hvalid hbytes htrace hslow
  have bind_ok_inv : ∀ {α β : Type} (r : Result α) (g : α → Result β) y,
      Bind.bind r g = ok y → ∃ z, r = ok z ∧ g z = ok y := by
    intro α β r g y hr
    cases r with
    | fail err => simp [Bind.bind, Aeneas.Std.bind] at hr
    | div => simp [Bind.bind, Aeneas.Std.bind] at hr
    | ok z => exact ⟨z, rfl, hr⟩
  have biased_pair_ext : ∀ (a b : num.imp.dec2flt.common.BiasedFp),
      a.m = b.m -> a.p_biased = b.p_biased -> a = b := by
    intro a b hm hp
    cases a with
    | mk am ap =>
        cases b with
        | mk bm bp => simp_all
  obtain hshape := ParserNumericTraceShape s x f
    (num.imp.dec2flt.Dec2FltTrace.Assembled tr)
    hsource hvalid hbytes htrace
  rcases hshape with hfast | hassembled
  · rcases hfast with ⟨body, negative, parsed, fastInput, fastResult,
      heq, hrest⟩
    cases heq
  · rcases hassembled with ⟨assembly, parsed, htraceeq, hne, hbody,
      hrender, hnegative, hparsed, hparse⟩
    cases htraceeq
    let unsigned : DecimalLexeme := { x with leadingSign := none }
    have unsigned_valid : DecimalGoalVocabularyDefault.validNumeric unsigned := by
      simpa [DecimalGoalVocabularyDefault, unsigned] using hvalid
    have magnitude_facts := NumberHelperAccumulatedValue
      tr.body unsigned parsed hbody hne rfl unsigned_valid hrender hparse
    have unsigned_magnitude :
        DecimalGoalVocabularyDefault.accumulatedMagnitude unsigned =
          DecimalGoalVocabularyDefault.accumulatedMagnitude x := by
      simp [DecimalGoalVocabularyDefault, unsigned]
    rw [unsigned_magnitude] at magnitude_facts
    unfold num.imp.dec2flt.dec2flt_with_trace at htrace
    obtain ⟨body0, hbody0, htrace⟩ := bind_ok_inv _ _ _ htrace
    rcases s with ⟨source, source_bound⟩
    cases source with
    | nil => cases htrace
    | cons first tail =>
      have hbodyEq : body0 = (⟨first :: tail, source_bound⟩ : Slice Std.U8) := by
        simpa [core.str.Str.as_bytes, ModelStrAsBytes] using hbody0.symm
      subst body0
      obtain ⟨firstOption, hfirst, htrace⟩ := bind_ok_inv _ _ _ htrace
      have hfirstEq : firstOption = some first := by
        simpa [core.slice.Slice.first, ModelSliceFirst] using hfirst.symm
      subst firstOption
      obtain ⟨body1, hbody1, htrace⟩ := bind_ok_inv _ _ _ htrace
      obtain ⟨empty, hempty, htrace⟩ := bind_ok_inv _ _ _ htrace
      by_cases emptyTrue : empty = true
      · simp only [emptyTrue, if_true] at htrace
        cases htrace
      · have emptyFalse : empty = false := Bool.eq_false_of_not_eq_true emptyTrue
        simp only [emptyFalse, if_false] at htrace
        obtain ⟨parsedOption, hparsedOption, htrace⟩ := bind_ok_inv _ _ _ htrace
        cases parsedOption with
        | none =>
            obtain ⟨specialOption, hspecialOption, htrace⟩ :=
              bind_ok_inv _ _ _ htrace
            cases specialOption with
            | none =>
                obtain ⟨pfe, hpfe, htrace⟩ := bind_ok_inv _ _ _ htrace
                cases htrace
            | some value => cases htrace
        | some r =>
            obtain ⟨fastPath, hfastPath, htrace⟩ := bind_ok_inv _ _ _ htrace
            cases fastPath with
            | some value => cases htrace
            | none =>
                obtain ⟨firstCompute, hfirstCompute, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                obtain ⟨phaseOne, hphaseOne, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                rcases phaseOne with
                  ⟨manyOne, manyTwo, firstPairOne, fp, secondGuard⟩
                obtain ⟨phaseTwo, hphaseTwo, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                rcases phaseTwo with
                  ⟨postPair, secondExponent, secondMantissa, secondResult,
                    secondEqual, forced⟩
                obtain ⟨phaseThree, hphaseThree, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                rcases phaseThree with ⟨selected, slowGuard, slowResult⟩
                obtain ⟨indexValue, hindexValue, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                obtain ⟨indexShift, hindexShift, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                obtain ⟨shifted, hshifted, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                obtain ⟨assembledWord, hassembledWord, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                obtain ⟨assemblyResult, hassemblyResult, htrace⟩ :=
                  bind_ok_inv _ _ _ htrace
                have first_eq : fp = firstCompute := by
                  by_cases many : r.many_digits = true
                  · simp only [many, if_true] at hphaseOne
                    cases hphaseOne
                    rfl
                  · simp only [many, if_false] at hphaseOne
                    cases hphaseOne
                    rfl
                have first_compute :
                    num.imp.dec2flt.lemire.compute_float
                      F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
                      r.exponent r.mantissa = ok fp := by
                  simpa [first_eq] using hfirstCompute
                by_cases negative : first = 45#u8
                · simp only [negative, if_true] at htrace
                  obtain ⟨result, hresult, htrace⟩ :=
                    bind_ok_inv _ _ _ htrace
                  cases htrace
                  cases hparsed
                  have guard_false : slowGuard = false := by
                    simpa using hslow
                  change DecimalGoalVocabularyDefault.roundsMagnitude
                    (DecimalGoalVocabularyDefault.accumulatedMagnitude x) assemblyResult
                  have finish : ∀ (w : F64),
                      DecimalGoalVocabularyDefault.assembledWord selected w ->
                      DecimalGoalVocabularyDefault.roundsMagnitude
                        (DecimalGoalVocabularyDefault.accumulatedMagnitude x) w ->
                      DecimalGoalVocabularyDefault.roundsMagnitude
                        (DecimalGoalVocabularyDefault.accumulatedMagnitude x) assemblyResult := by
                    intro w hw hround
                    obtain ⟨canonical, hcanonical, hcanonicalWord⟩ :=
                      AssemblyCorrespondence selected
                    have hcall :
                        num.imp.dec2flt.biased_fp_to_float
                          F64.Insts.Dec2flt_modelNumImpTraitsFloatExtU64 selected = ok assemblyResult := by
                      simpa using hassemblyResult
                    have hresult : ok canonical = ok assemblyResult :=
                      hcanonical.symm.trans hcall
                    have ceq : canonical = assemblyResult := by
                      cases hresult
                      rfl
                    have hval : w.val = canonical.val := by
                      dsimp only [DecimalGoalVocabularyDefault] at hw hcanonicalWord
                      exact hw.trans hcanonicalWord.symm
                    have weq : w = canonical := UScalar.eq_of_val_eq hval
                    cases weq
                    cases ceq
                    exact hround
                  have post_nonnegative : ¬ postPair.p_biased < 0#i32 := by
                    intro hpost
                    simp only [hpost, if_true] at hphaseThree
                    obtain ⟨replacement, hreplacement, hphaseThree⟩ :=
                      bind_ok_inv _ _ _ hphaseThree
                    cases hphaseThree
                    simp_all
                  simp only [post_nonnegative, if_false] at hphaseThree
                  cases hphaseThree
                  by_cases manyFalse : r.many_digits = false
                  · have manyNotTrue : ¬ r.many_digits = true := by
                      intro hmany
                      have contradiction : false = true := manyFalse.symm.trans hmany
                      cases contradiction
                    simp only [manyNotTrue, if_false] at hphaseOne
                    cases hphaseOne
                    have phase_two_flat :
                        ok (firstCompute, none, none, none, none, false) =
                          ok (postPair, secondExponent, secondMantissa,
                            secondResult, secondEqual, forced) := by
                      simpa only [Bool.false_eq_true, if_false] using hphaseTwo
                    have phase_two_data := Result.ok.inj phase_two_flat
                    have post_eq : firstCompute = postPair :=
                      congrArg (fun z => z.1) phase_two_data
                    cases post_eq
                    have nonnegative : 0 ≤ firstCompute.p_biased.val := by
                      by_contra hnon
                      have pneg : firstCompute.p_biased < 0#i32 :=
                        lt_of_not_ge hnon
                      exact post_nonnegative pneg
                    rcases IntegerCorrectOrDefer r.exponent r.mantissa firstCompute
                      first_compute with hbad | ⟨target, hword, hround⟩
                    · omega
                    · rcases magnitude_facts with ⟨hmany, hmag⟩ | ⟨hmany, hlo, hhi⟩
                      · have hround' := hround
                        have htargetRound :
                            DecimalGoalVocabularyDefault.roundsMagnitude
                              (DecimalGoalVocabularyDefault.accumulatedMagnitude x) target := by
                          rw [← hmag]
                          exact hround'
                        exact finish target hword htargetRound
                      · simp at hmany
                  · have manyTrue : r.many_digits = true := by
                      cases hmany : r.many_digits with
                      | false => exact False.elim (manyFalse hmany)
                      | true => rfl
                    simp only [manyTrue, if_true] at hphaseOne
                    cases hphaseOne
                    have pair_nonnegative : 0 ≤ firstCompute.p_biased.val := by
                      by_contra hpair
                      have pair_negative : firstCompute.p_biased < 0#i32 :=
                        lt_of_not_ge hpair
                      have dec_false :
                          decide (firstCompute.p_biased ≥ 0#i32) = false := by
                        apply decide_eq_false_iff_not.mpr
                        exact not_le_of_gt pair_negative
                      simp only [dec_false, if_false] at hphaseTwo
                      have phase_two_flat :
                          ok (firstCompute, none, none, none, none, false) =
                            ok (postPair, secondExponent, secondMantissa,
                              secondResult, secondEqual, forced) := by
                        simpa using hphaseTwo
                      have phase_two_data := Result.ok.inj phase_two_flat
                      have post_eq : firstCompute = postPair :=
                        congrArg (fun z => z.1) phase_two_data
                      cases post_eq
                      exact post_nonnegative pair_negative
                    have dec_true :
                        decide (firstCompute.p_biased ≥ 0#i32) = true := by
                      simp [pair_nonnegative]
                    simp only [dec_true, if_true] at hphaseTwo
                    obtain ⟨incremented, hincremented, hphaseTwo⟩ :=
                      bind_ok_inv _ _ _ hphaseTwo
                    obtain ⟨secondPair, hsecondPair, hphaseTwo⟩ :=
                      bind_ok_inv _ _ _ hphaseTwo
                    obtain ⟨equal, hequal, hphaseTwo⟩ :=
                      bind_ok_inv _ _ _ hphaseTwo
                    obtain ⟨chosen, hchosen, hphaseTwo⟩ :=
                      bind_ok_inv _ _ _ hphaseTwo
                    rcases chosen with ⟨chosenPair, forcedDeferral⟩
                    have equal_true : equal = true := by
                      by_contra heq
                      have equal_false : equal = false :=
                        Bool.eq_false_of_not_eq_true heq
                      simp only [equal_false, if_false] at hchosen
                      cases hchosen
                      have phase_two_flat :
                          ok ({ m := firstCompute.m, p_biased := (-1)#i32 },
                            some r.exponent, some incremented, some secondPair,
                            some equal, true) =
                            ok (postPair, secondExponent, secondMantissa,
                              secondResult, secondEqual, forced) := by
                        simpa using hphaseTwo
                      have phase_two_data := Result.ok.inj phase_two_flat
                      have post_eq :
                          ({ m := firstCompute.m, p_biased := (-1)#i32 } :
                            num.imp.dec2flt.common.BiasedFp) = postPair :=
                        congrArg (fun z => z.1) phase_two_data
                      cases post_eq
                      apply post_nonnegative
                      norm_num
                    simp only [equal_true, if_true] at hchosen
                    cases hchosen
                    have phase_two_flat :
                        ok (firstCompute, some r.exponent, some incremented,
                          some secondPair, some equal, false) =
                          ok (postPair, secondExponent, secondMantissa,
                            secondResult, secondEqual, forced) := by
                      simpa using hphaseTwo
                    have phase_two_data := Result.ok.inj phase_two_flat
                    have post_eq : firstCompute = postPair :=
                      congrArg (fun z => z.1) phase_two_data
                    cases post_eq
                    have incremented_value :
                        incremented.val = r.mantissa.val + 1 := by
                      have hadd := UScalar.add_equiv r.mantissa (1#u64)
                      rw [hincremented] at hadd
                      simp at hadd
                      omega
                    have second_compute :
                        num.imp.dec2flt.lemire.compute_float
                          F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
                          r.exponent incremented = ok secondPair := by
                      simpa using hsecondPair
                    have pair_eq : firstCompute = secondPair := by
                      have hm : firstCompute.m = secondPair.m := by
                        by_contra hne
                        have equal_false : equal = false := by
                          simpa [num.imp.dec2flt.common.BiasedFp.Insts.CoreCmpPartialEqBiasedFp.eq,
                            hne] using hequal
                        cases equal_false
                        cases equal_true
                      have hp : firstCompute.p_biased = secondPair.p_biased := by
                        have hh := hequal
                        simp [num.imp.dec2flt.common.BiasedFp.Insts.CoreCmpPartialEqBiasedFp.eq,
                          hm, equal_true] at hh
                        exact hh
                      exact biased_pair_ext firstCompute secondPair hm hp
                    subst secondPair
                    have nonnegative : 0 ≤ firstCompute.p_biased.val := by omega
                    rcases IntegerCorrectOrDefer r.exponent r.mantissa firstCompute
                      first_compute with hbad | ⟨targetLower, hwordLower, hroundLower⟩
                    · omega
                    rcases IntegerCorrectOrDefer r.exponent incremented
                      firstCompute second_compute with hbad | ⟨targetUpper, hwordUpper, hroundUpper⟩
                    · omega
                    · rcases magnitude_facts with ⟨hmany, hmag⟩ | ⟨hmany, hlo, hhi⟩
                      · simp at hmany
                      · have lower_le :
                            ((r.mantissa.val : Real) * (10 : Real) ^ r.exponent.val) ≤
                              DecimalGoalVocabularyDefault.accumulatedMagnitude x := by
                          change ((r.mantissa.val : Real) * (10 : Real) ^ r.exponent.val) ≤
                            DecimalGoalVocabularyDefault.accumulatedMagnitude x at hlo
                          exact hlo
                        have upper_gt :
                            DecimalGoalVocabularyDefault.accumulatedMagnitude x <
                              ((incremented.val : Nat) : Real) *
                                (10 : Real) ^ r.exponent.val := by
                          change DecimalGoalVocabularyDefault.accumulatedMagnitude x <
                            ((r.mantissa.val + 1 : Nat) : Real) *
                              (10 : Real) ^ r.exponent.val at hhi
                          simpa [incremented_value] using hhi
                        have lower_round :
                            DecimalGoalVocabularyDefault.roundsMagnitude
                              ((r.mantissa.val : Real) * (10 : Real) ^ r.exponent.val)
                              targetLower := by
                          exact hroundLower
                        have upper_round :
                            DecimalGoalVocabularyDefault.roundsMagnitude
                              (((incremented.val : Nat) : Real) *
                                (10 : Real) ^ r.exponent.val) targetUpper := by
                          exact hroundUpper
                        have target_eq : targetLower = targetUpper := by
                          have hval : targetLower.val = targetUpper.val := by
                            dsimp only [DecimalGoalVocabularyDefault] at hwordLower hwordUpper
                            exact hwordLower.trans hwordUpper.symm
                          exact UScalar.eq_of_val_eq hval
                        cases target_eq
                        have interval := RoundsMagnitudeInterval
                          ((r.mantissa.val : Real) * (10 : Real) ^ r.exponent.val)
                          (DecimalGoalVocabularyDefault.accumulatedMagnitude x)
                          (((incremented.val : Nat) : Real) *
                            (10 : Real) ^ r.exponent.val)
                          targetLower
                          lower_le (le_of_lt upper_gt)
                          lower_round upper_round
                        exact finish targetLower hwordLower interval
                · simp only [negative, if_false] at htrace
                  cases htrace
                  cases hparsed
                  have guard_false : slowGuard = false := by
                    simpa using hslow
                  change DecimalGoalVocabularyDefault.roundsMagnitude
                    (DecimalGoalVocabularyDefault.accumulatedMagnitude x) f
                  have finish : ∀ (w : F64),
                      DecimalGoalVocabularyDefault.assembledWord selected w ->
                      DecimalGoalVocabularyDefault.roundsMagnitude
                        (DecimalGoalVocabularyDefault.accumulatedMagnitude x) w ->
                      DecimalGoalVocabularyDefault.roundsMagnitude
                        (DecimalGoalVocabularyDefault.accumulatedMagnitude x) f := by
                    intro w hw hround
                    obtain ⟨canonical, hcanonical, hcanonicalWord⟩ :=
                      AssemblyCorrespondence selected
                    have hcall :
                        num.imp.dec2flt.biased_fp_to_float
                          F64.Insts.Dec2flt_modelNumImpTraitsFloatExtU64 selected = ok f := by
                      simpa using hassemblyResult
                    have hresult : ok canonical = ok f :=
                      hcanonical.symm.trans hcall
                    have ceq : canonical = f := by
                      cases hresult
                      rfl
                    have hval : w.val = canonical.val := by
                      dsimp only [DecimalGoalVocabularyDefault] at hw hcanonicalWord
                      exact hw.trans hcanonicalWord.symm
                    have weq : w = canonical := UScalar.eq_of_val_eq hval
                    cases weq
                    cases ceq
                    exact hround
                  have post_nonnegative : ¬ postPair.p_biased < 0#i32 := by
                    intro hpost
                    simp only [hpost, if_true] at hphaseThree
                    obtain ⟨replacement, hreplacement, hphaseThree⟩ :=
                      bind_ok_inv _ _ _ hphaseThree
                    cases hphaseThree
                    simp_all
                  simp only [post_nonnegative, if_false] at hphaseThree
                  cases hphaseThree
                  by_cases manyFalse : r.many_digits = false
                  · have manyNotTrue : ¬ r.many_digits = true := by
                      intro hmany
                      have contradiction : false = true := manyFalse.symm.trans hmany
                      cases contradiction
                    simp only [manyNotTrue, if_false] at hphaseOne
                    cases hphaseOne
                    have phase_two_flat :
                        ok (firstCompute, none, none, none, none, false) =
                          ok (postPair, secondExponent, secondMantissa,
                            secondResult, secondEqual, forced) := by
                      simpa only [Bool.false_eq_true, if_false] using hphaseTwo
                    have phase_two_data := Result.ok.inj phase_two_flat
                    have post_eq : firstCompute = postPair :=
                      congrArg (fun z => z.1) phase_two_data
                    cases post_eq
                    have nonnegative : 0 ≤ firstCompute.p_biased.val := by
                      by_contra hnon
                      have pneg : firstCompute.p_biased < 0#i32 :=
                        lt_of_not_ge hnon
                      exact post_nonnegative pneg
                    rcases IntegerCorrectOrDefer r.exponent r.mantissa firstCompute
                      first_compute with hbad | ⟨target, hword, hround⟩
                    · omega
                    · rcases magnitude_facts with ⟨hmany, hmag⟩ | ⟨hmany, hlo, hhi⟩
                      · have hround' := hround
                        have htargetRound :
                            DecimalGoalVocabularyDefault.roundsMagnitude
                              (DecimalGoalVocabularyDefault.accumulatedMagnitude x) target := by
                          rw [← hmag]
                          exact hround'
                        exact finish target hword htargetRound
                      · simp at hmany
                  · have manyTrue : r.many_digits = true := by
                      cases hmany : r.many_digits with
                      | false => exact False.elim (manyFalse hmany)
                      | true => rfl
                    simp only [manyTrue, if_true] at hphaseOne
                    cases hphaseOne
                    have pair_nonnegative : 0 ≤ firstCompute.p_biased.val := by
                      by_contra hpair
                      have pair_negative : firstCompute.p_biased < 0#i32 :=
                        lt_of_not_ge hpair
                      have dec_false :
                          decide (firstCompute.p_biased ≥ 0#i32) = false := by
                        apply decide_eq_false_iff_not.mpr
                        exact not_le_of_gt pair_negative
                      simp only [dec_false, if_false] at hphaseTwo
                      have phase_two_flat :
                          ok (firstCompute, none, none, none, none, false) =
                            ok (postPair, secondExponent, secondMantissa,
                              secondResult, secondEqual, forced) := by
                        simpa using hphaseTwo
                      have phase_two_data := Result.ok.inj phase_two_flat
                      have post_eq : firstCompute = postPair :=
                        congrArg (fun z => z.1) phase_two_data
                      cases post_eq
                      exact post_nonnegative pair_negative
                    have dec_true :
                        decide (firstCompute.p_biased ≥ 0#i32) = true := by
                      simp [pair_nonnegative]
                    simp only [dec_true, if_true] at hphaseTwo
                    obtain ⟨incremented, hincremented, hphaseTwo⟩ :=
                      bind_ok_inv _ _ _ hphaseTwo
                    obtain ⟨secondPair, hsecondPair, hphaseTwo⟩ :=
                      bind_ok_inv _ _ _ hphaseTwo
                    obtain ⟨equal, hequal, hphaseTwo⟩ :=
                      bind_ok_inv _ _ _ hphaseTwo
                    obtain ⟨chosen, hchosen, hphaseTwo⟩ :=
                      bind_ok_inv _ _ _ hphaseTwo
                    rcases chosen with ⟨chosenPair, forcedDeferral⟩
                    have equal_true : equal = true := by
                      by_contra heq
                      have equal_false : equal = false :=
                        Bool.eq_false_of_not_eq_true heq
                      simp only [equal_false, if_false] at hchosen
                      cases hchosen
                      have phase_two_flat :
                          ok ({ m := firstCompute.m, p_biased := (-1)#i32 },
                            some r.exponent, some incremented, some secondPair,
                            some equal, true) =
                            ok (postPair, secondExponent, secondMantissa,
                              secondResult, secondEqual, forced) := by
                        simpa using hphaseTwo
                      have phase_two_data := Result.ok.inj phase_two_flat
                      have post_eq :
                          ({ m := firstCompute.m, p_biased := (-1)#i32 } :
                            num.imp.dec2flt.common.BiasedFp) = postPair :=
                        congrArg (fun z => z.1) phase_two_data
                      cases post_eq
                      apply post_nonnegative
                      norm_num
                    simp only [equal_true, if_true] at hchosen
                    cases hchosen
                    have phase_two_flat :
                        ok (firstCompute, some r.exponent, some incremented,
                          some secondPair, some equal, false) =
                          ok (postPair, secondExponent, secondMantissa,
                            secondResult, secondEqual, forced) := by
                      simpa using hphaseTwo
                    have phase_two_data := Result.ok.inj phase_two_flat
                    have post_eq : firstCompute = postPair :=
                      congrArg (fun z => z.1) phase_two_data
                    cases post_eq
                    have incremented_value :
                        incremented.val = r.mantissa.val + 1 := by
                      have hadd := UScalar.add_equiv r.mantissa (1#u64)
                      rw [hincremented] at hadd
                      simp at hadd
                      omega
                    have second_compute :
                        num.imp.dec2flt.lemire.compute_float
                          F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
                          r.exponent incremented = ok secondPair := by
                      simpa using hsecondPair
                    have pair_eq : firstCompute = secondPair := by
                      have hm : firstCompute.m = secondPair.m := by
                        by_contra hne
                        have equal_false : equal = false := by
                          simpa [num.imp.dec2flt.common.BiasedFp.Insts.CoreCmpPartialEqBiasedFp.eq,
                            hne] using hequal
                        cases equal_false
                        cases equal_true
                      have hp : firstCompute.p_biased = secondPair.p_biased := by
                        have hh := hequal
                        simp [num.imp.dec2flt.common.BiasedFp.Insts.CoreCmpPartialEqBiasedFp.eq,
                          hm, equal_true] at hh
                        exact hh
                      exact biased_pair_ext firstCompute secondPair hm hp
                    subst secondPair
                    have nonnegative : 0 ≤ firstCompute.p_biased.val := by
                      omega
                    rcases IntegerCorrectOrDefer r.exponent r.mantissa firstCompute
                      first_compute with hbad | ⟨targetLower, hwordLower, hroundLower⟩
                    · omega
                    rcases IntegerCorrectOrDefer r.exponent incremented
                      firstCompute second_compute with hbad | ⟨targetUpper, hwordUpper, hroundUpper⟩
                    · omega
                    · rcases magnitude_facts with ⟨hmany, hmag⟩ | ⟨hmany, hlo, hhi⟩
                      · simp at hmany
                      · have lower_le :
                            ((r.mantissa.val : Real) * (10 : Real) ^ r.exponent.val) ≤
                              DecimalGoalVocabularyDefault.accumulatedMagnitude x := by
                          change ((r.mantissa.val : Real) * (10 : Real) ^ r.exponent.val) ≤
                            DecimalGoalVocabularyDefault.accumulatedMagnitude x at hlo
                          exact hlo
                        have upper_gt :
                            DecimalGoalVocabularyDefault.accumulatedMagnitude x <
                              ((incremented.val : Nat) : Real) *
                                (10 : Real) ^ r.exponent.val := by
                          change DecimalGoalVocabularyDefault.accumulatedMagnitude x <
                            ((r.mantissa.val + 1 : Nat) : Real) *
                              (10 : Real) ^ r.exponent.val at hhi
                          simpa [incremented_value] using hhi
                        have lower_round :
                            DecimalGoalVocabularyDefault.roundsMagnitude
                              ((r.mantissa.val : Real) * (10 : Real) ^ r.exponent.val)
                              targetLower := by
                          exact hroundLower
                        have upper_round :
                            DecimalGoalVocabularyDefault.roundsMagnitude
                              (((incremented.val : Nat) : Real) *
                                (10 : Real) ^ r.exponent.val) targetUpper := by
                          exact hroundUpper
                        have target_eq : targetLower = targetUpper := by
                          have hval : targetLower.val = targetUpper.val := by
                            dsimp only [DecimalGoalVocabularyDefault] at hwordLower hwordUpper
                            exact hwordLower.trans hwordUpper.symm
                          exact UScalar.eq_of_val_eq hval
                        cases target_eq
                        have interval := RoundsMagnitudeInterval
                          ((r.mantissa.val : Real) * (10 : Real) ^ r.exponent.val)
                          (DecimalGoalVocabularyDefault.accumulatedMagnitude x)
                          (((incremented.val : Nat) : Real) *
                            (10 : Real) ^ r.exponent.val)
                          targetLower
                          lower_le (le_of_lt upper_gt)
                          lower_round upper_round
                        exact finish targetLower hwordLower interval

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 182 entries
Technical checking records
Local closure summary
{
  "node": "ParserAssembledDirectMagnitudeRounding",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "5af6a6ae56fef34b942441ffa354980549614a61ffc553e346515c0828d6868b",
  "active_statement_hash": "79fa1954535b2a50588a5def26b2ce2b91ef6f1f28cab4a5683ba6e1f96cf917",
  "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": "revalidate-cycle-161",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "AssemblyCorrespondence": "e3495a6647394e20",
    "DecimalGoalVocabularyDefault": "94cb4c406f8efb78",
    "IntegerCorrectOrDefer": "f60d8456c59b0974",
    "NumberHelperAccumulatedValue": "6e36048c354bb051",
    "ParserNumericTraceShape": "5cc80ccbad3dbbfc",
    "ParserSpecialRejectionSafeLength": "4dbd872ba02a631e",
    "RoundsMagnitudeInterval": "5b497d52e6e4411d",
    "num_imp_dec2flt_dec2flt_with_trace": "f3e7d4ffbf9b4d68",
    "num_imp_traits_Float_SIG_BITS_default": "9ab622d85ef04d94"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "986725aca5ee5702",
    "DecimalGoalVocabularyDefault": "512f8d0aea894250",
    "DecimalLexeme": "8127b3eaeae00dd2",
    "F64": "ec4e25fbaf4e11d4",
    "F64_Insts_CoreOpsArithDivF64F64": "dc44e13a99fc6bed",
    "F64_Insts_CoreOpsArithMulF64F64": "1ae1a9021a654abd",
    "F64_Insts_CoreOpsArithNegF64": "2d9be310c546ea2e",
    "F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64": "30963b4ef656cc79",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatExtU64": "60afe26c6a80cf0f",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64": "94095c9bf79da164",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SIG_TOTAL_BITS": "887b52d84a3fc9c4",
    "Preamble": "86dc391ab250c79b",
    "SourceValidByteSlice": "3dc42716a6378d26",
    "core_ops_arith_Neg": "0ff07135a60bcbf9",
    "num_float_parse_FloatErrorKind": "20698e72f41a7333",
    "num_float_parse_ParseFloatError": "c43614ff2149ecf2",
    "num_imp_dec2flt_Dec2FltAssemblyTrace": "f2e3172d150bce83",
    "num_imp_dec2flt_Dec2FltTrace": "27322b4608129f59",
    "num_imp_dec2flt_Lemire": "1923f90c53d323e6",
    "num_imp_dec2flt_biased_fp_to_float": "acdde060e8d35ca7",
    "num_imp_dec2flt_common_BiasedFp": "63baac539d218a81",
    "num_imp_dec2flt_common_BiasedFp_Insts_CoreCmpPartialEqBiasedFp_eq": "fbbf553e3c502a05",
    "num_imp_dec2flt_dec2flt_with_trace": "d34666730435cdb9",
    "num_imp_dec2flt_decimal_Decimal": "63fd2f021dd0ef41",
    "num_imp_dec2flt_decimal_Decimal_try_fast_path": "2e4888f65a06b49f",
    "num_imp_dec2flt_lemire_compute_float": "39d89e3f0a8a9d4c",
    "num_imp_dec2flt_parse_parse_inf_nan": "85655c19a8791b54",
    "num_imp_dec2flt_parse_parse_number": "f73f8bc11faa6db3",
    "num_imp_dec2flt_pfe_empty": "89046d1da5ac1ce2",
    "num_imp_dec2flt_pfe_invalid": "21db875427d92c6d",
    "num_imp_dec2flt_slow_parse_long_mantissa": "cd8af64c3688b60f",
    "num_imp_traits_Float": "02acccb9db3e30f2",
    "num_imp_traits_FloatExt": "f996e5400649797d"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:0e31e68c1bd168f1ab6bade2144acf200e6390f12e16b832a12430597fdfe69f",
  "current_fingerprint": "sound-v2:0e31e68c1bd168f1ab6bade2144acf200e6390f12e16b832a12430597fdfe69f",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}