P13_T1_ParserNonNanResults

Lean checked

Statement

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

Theorem

For every source-valid Rust string other than a complete NaN literal, every normally successful Rust Ok result of the actual specialized dec2flt is not NaN. This ranges over all successful inputs, not only numeric literals, and imposes no A or B bound. The complement of the optionally signed NaN grammar and the returned-word NaN predicate are DecimalGoalVocabularyDefault. The source specialization is ActualDispatchOperations.

Lean statement

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

import Tablet.DecimalGoalVocabularyDefault
import Tablet.P9_T1_ActualDispatchOperations
import Tablet.num_imp_dec2flt_dec2flt
import Tablet.SourceValidByteSlice
import Tablet.ModelStrAsBytes
import Tablet.ModelSliceFirst
import Tablet.ModelF64Neg
import Tablet.SpecialHelperRejection
import Tablet.SpecialHelperInfinity
import Tablet.P13_T1_NativeCorrectOrDefer
import Tablet.ParserSelectedExponent
import Tablet.ParserSelectedMantissaBound
import Tablet.ParserComputeFloatInfinityMantissa
import Tablet.ParserSlowInfinityMantissa
import Tablet.ParserAssembledPairNonNan
import Tablet.ParserSelectedMagnitudeSign
import Tablet.AssemblyCorrespondence

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P13_T1_ParserNonNanResults]
theorem P13_T1_ParserNonNanResults :
    forall (s : Str) (f : F64),
      DecimalGoalVocabularyDefault.sourceString s ->
      (¬ exists sign : Option Bool, DecimalGoalVocabularyDefault.specialLiteral
        (DecimalGoalVocabularyDefault.bytes s) sign true) ->
      num.imp.dec2flt.dec2flt F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64
        F64.Insts.CoreOpsArithDivF64F64 F64.Insts.CoreOpsArithMulF64F64
        F64.Insts.CoreOpsArithNegF64 s = ok (core.result.Result.Ok f) ->
      ¬ DecimalGoalVocabularyDefault.isNan f := 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 proof: SKETCH. The explanatory prose is preserved as it stood when captured.

Proof

SKETCH: Analyze every normal Rust Ok branch, excluding a special NaN literal and checking the native and assembled numeric words as well as special infinity returns.

Lean proof


  intro s f hsource hnotNanLiteral hpublic
  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 infinity_not_nan : ∀ (negative : Bool) (value : F64),
      DecimalGoalVocabularyDefault.isInfinity negative value ->
      ¬ DecimalGoalVocabularyDefault.isNan value := by
    intro negative value hinfinity hnan
    dsimp only [DecimalGoalVocabularyDefault] at hinfinity hnan
    cases negative <;>
      simp only [Bool.false_eq_true, if_false, if_true] at hinfinity
    · rw [hinfinity] at hnan
      norm_num at hnan
    · rw [hinfinity] at hnan
      norm_num at hnan
  have signed_bounded_not_nan : ∀ (negative : Bool) (magnitude result : F64),
      magnitude.val ≤ 2047 * 2 ^ 52 ->
      DecimalGoalVocabularyDefault.signedMagnitude negative magnitude result ->
      ¬ DecimalGoalVocabularyDefault.isNan result := by
    intro negative magnitude result hbound hsigned hnan
    dsimp only [DecimalGoalVocabularyDefault] at hsigned hnan
    rcases hsigned with ⟨hmag, hresult⟩
    rcases hnan with ⟨hexponent, hfraction⟩
    have hmodSign : result.val % (2 ^ 63) = magnitude.val := by
      cases negative with
      | false =>
        simp only [Bool.false_eq_true, if_false, add_zero] at hresult
        rw [hresult, Nat.mod_eq_of_lt hmag]
      | true =>
        simp only [if_true] at hresult
        rw [hresult, Nat.add_mod]
        have hm := Nat.mod_eq_of_lt hmag
        rw [hm, Nat.mod_self, Nat.add_zero, hm]
    have hmodFraction :
        result.val % (2 ^ 52) = magnitude.val % (2 ^ 52) := by
      cases negative with
      | false =>
        simp only [Bool.false_eq_true, if_false, add_zero] at hresult
        rw [hresult]
      | true =>
        simp only [if_true] at hresult
        rw [hresult, Nat.add_mod]
        norm_num
    rw [hmodSign] at hexponent
    have hdecomp := Nat.mod_add_div magnitude.val (2 ^ 52)
    rw [hexponent] at hdecomp
    have hmagExact : magnitude.val = 2047 * 2 ^ 52 := by
      norm_num at hdecomp ⊢
      omega
    rw [hmodFraction, hmagExact] at hfraction
    norm_num at hfraction
  unfold num.imp.dec2flt.dec2flt at hpublic
  obtain ⟨out, hout, hpublic⟩ := bind_ok_inv _ _ _ hpublic
  rcases out with ⟨publicResult, trace⟩
  cases hpublic
  have traced :
      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, trace) := hout
  rcases s with ⟨source, source_bound⟩
  cases source with
  | nil =>
      unfold num.imp.dec2flt.dec2flt_with_trace at traced
      obtain ⟨body, hbody, traced⟩ := bind_ok_inv _ _ _ traced
      have hbodyEq : body = (⟨[], source_bound⟩ : Slice Std.U8) := by
        simpa [core.str.Str.as_bytes, ModelStrAsBytes] using hbody.symm
      subst body
      obtain ⟨firstOption, hfirst, traced⟩ := bind_ok_inv _ _ _ traced
      have hfirstEq : firstOption = none := by
        simpa [core.slice.Slice.first, ModelSliceFirst] using hfirst.symm
      subst firstOption
      obtain ⟨pfe, hpfe, traced⟩ := bind_ok_inv _ _ _ traced
      cases traced
  | cons first tail =>
      unfold num.imp.dec2flt.dec2flt_with_trace at traced
      obtain ⟨body, hbody, traced⟩ := bind_ok_inv _ _ _ traced
      have hbodyEq : body = (⟨first :: tail, source_bound⟩ : Slice Std.U8) := by
        simpa [core.str.Str.as_bytes, ModelStrAsBytes] using hbody.symm
      subst body
      obtain ⟨firstOption, hfirst, traced⟩ := bind_ok_inv _ _ _ traced
      have hfirstEq : firstOption = some first := by
        simpa [core.slice.Slice.first, ModelSliceFirst] using hfirst.symm
      subst firstOption
      obtain ⟨body1, hbody1, traced⟩ := bind_ok_inv _ _ _ traced
      have body1Valid : SourceValidByteSlice body1 := by
        unfold SourceValidByteSlice
        have hslen := hsource.1
        by_cases hminus : first = 45#u8
        · simp only [hminus, if_true] at hbody1
          simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
            Slice.drop] at hbody1
          cases hbody1
          simp at hslen ⊢
          omega
        · simp only [hminus, if_false] at hbody1
          by_cases hplus : first = 43#u8
          · simp only [hplus, if_true] at hbody1
            simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
              Slice.drop] at hbody1
            cases hbody1
            simp at hslen ⊢
            omega
          · simp only [hplus, if_false] at hbody1
            cases hbody1
            exact hslen
      have noNanBody : ¬ DecimalGoalVocabularyDefault.specialLiteral
          (DecimalGoalVocabularyDefault.bytes body1) none true := by
        intro hspecial
        apply hnotNanLiteral
        by_cases hminus : first = 45#u8
        · simp only [hminus, if_true] at hbody1
          simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
            Slice.drop] at hbody1
          cases hbody1
          refine ⟨some true, ?_⟩
          dsimp only [DecimalGoalVocabularyDefault] at hspecial ⊢
          rcases hspecial with ⟨specialBody, hspecialBody, hcase⟩
          refine ⟨specialBody, ?_, hcase⟩
          subst first
          simpa using hspecialBody
        · simp only [hminus, if_false] at hbody1
          by_cases hplus : first = 43#u8
          · simp only [hplus, if_true] at hbody1
            simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
              Slice.drop] at hbody1
            cases hbody1
            refine ⟨some false, ?_⟩
            dsimp only [DecimalGoalVocabularyDefault] at hspecial ⊢
            rcases hspecial with ⟨specialBody, hspecialBody, hcase⟩
            refine ⟨specialBody, ?_, hcase⟩
            subst first
            simpa using hspecialBody
          · simp only [hplus, if_false] at hbody1
            cases hbody1
            exact ⟨none, hspecial⟩
      obtain ⟨empty, hempty, traced⟩ := bind_ok_inv _ _ _ traced
      by_cases emptyTrue : empty = true
      · simp only [emptyTrue, if_true] at traced
        obtain ⟨pfe, hpfe, traced⟩ := bind_ok_inv _ _ _ traced
        cases traced
      · have emptyFalse : empty = false := Bool.eq_false_of_not_eq_true emptyTrue
        simp only [emptyFalse, if_false] at traced
        obtain ⟨parsed, hparsed, traced⟩ := bind_ok_inv _ _ _ traced
        cases parsed with
        | none =>
          obtain ⟨special, hspecial, traced⟩ := bind_ok_inv _ _ _ traced
          cases special with
          | none =>
            obtain ⟨pfe, hpfe, traced⟩ := bind_ok_inv _ _ _ traced
            cases traced
          | some value =>
            cases traced
            by_cases hInf : DecimalGoalVocabularyDefault.specialLiteral
                (DecimalGoalVocabularyDefault.bytes body1) none false
            · obtain ⟨infinity, hinfinityEq, hinfinity⟩ :=
                SpecialHelperInfinity body1 (decide (first = 45#u8)) hInf
              rw [hspecial] at hinfinityEq
              cases hinfinityEq
              exact infinity_not_nan _ _ hinfinity
            · have hrejection := SpecialHelperRejection body1
                (decide (first = 45#u8)) body1Valid hInf noNanBody
              rw [hspecial] at hrejection
              cases hrejection
        | some r =>
          obtain ⟨fast, hfast, traced⟩ := bind_ok_inv _ _ _ traced
          cases fast with
          | some value =>
            have hnative := P13_T1_NativeCorrectOrDefer
              ({ r with negative := decide (first = 45#u8) })
              (some value) hfast
            rcases hnative with hnone |
              ⟨value', magnitude, hvalue, _, hrounds, hsigned⟩
            · simp at hnone
            · cases hvalue
              cases traced
              dsimp only [DecimalGoalVocabularyDefault] at hrounds
              exact signed_bounded_not_nan _ _ _ hrounds.2.1 hsigned
          | none =>
            obtain ⟨firstPair, hfirstPair, traced⟩ := bind_ok_inv _ _ _ traced
            have hfirstInfinity :=
              ParserComputeFloatInfinityMantissa _ _ _ hfirstPair
            obtain ⟨phase1, hphase1, traced⟩ := bind_ok_inv _ _ _ traced
            rcases phase1 with ⟨many1, many2, firstPair1, fp, guard⟩
            have hfp : fp = firstPair := by
              by_cases hm : r.many_digits
              · simp only [hm, if_true] at hphase1
                cases hphase1
                rfl
              · simp only [hm, if_false] at hphase1
                cases hphase1
                rfl
            subst fp
            obtain ⟨phase2, hphase2, traced⟩ := bind_ok_inv _ _ _ traced
            rcases phase2 with
              ⟨post, secondExp, secondMant, secondResult, secondEqual, forced⟩
            have hpostInfinity :
                post.p_biased.val = 2047 -> post.m.val = 0 := by
              cases guard with
              | false =>
                simp only [Bool.false_eq_true, if_false] at hphase2
                cases hphase2
                exact hfirstInfinity
              | true =>
                simp only [if_true] at hphase2
                obtain ⟨mantissa, hmantissa, hphase2⟩ :=
                  bind_ok_inv _ _ _ hphase2
                obtain ⟨second, hsecond, hphase2⟩ :=
                  bind_ok_inv _ _ _ hphase2
                obtain ⟨equal, hequal, hphase2⟩ :=
                  bind_ok_inv _ _ _ hphase2
                obtain ⟨chosen, hchosen, hphase2⟩ :=
                  bind_ok_inv _ _ _ hphase2
                rcases chosen with ⟨bf, wasForced⟩
                have hbf : bf.p_biased.val = 2047 -> bf.m.val = 0 := by
                  by_cases heq : equal = true
                  · simp only [heq, if_true] at hchosen
                    cases hchosen
                    exact hfirstInfinity
                  · have heqf : equal = false :=
                      Bool.eq_false_of_not_eq_true heq
                    simp only [heqf, if_false] at hchosen
                    cases hchosen
                    intro hp
                    norm_num at hp
                cases hphase2
                exact hbf
            obtain ⟨phase3, hphase3, traced⟩ := bind_ok_inv _ _ _ traced
            rcases phase3 with ⟨selected, slowGuard, slowResult⟩
            have hselectedInfinity :
                selected.p_biased.val = 2047 -> selected.m.val = 0 := by
              by_cases hneg : post.p_biased < 0#i32
              · simp only [hneg, if_true] at hphase3
                obtain ⟨replacement, hreplacement, hphase3⟩ :=
                  bind_ok_inv _ _ _ hphase3
                have hreplacement :=
                  ParserSlowInfinityMantissa _ _ hreplacement
                cases hphase3
                exact hreplacement
              · simp only [hneg, if_false] at hphase3
                cases hphase3
                exact hpostInfinity
            obtain ⟨i, hi, traced⟩ := bind_ok_inv _ _ _ traced
            obtain ⟨i1, hi1, traced⟩ := bind_ok_inv _ _ _ traced
            obtain ⟨i2, hi2, traced⟩ := bind_ok_inv _ _ _ traced
            obtain ⟨word, hword, traced⟩ := bind_ok_inv _ _ _ traced
            obtain ⟨assembled, hassembled, traced⟩ := bind_ok_inv _ _ _ traced
            by_cases negative : first = 45#u8
            · simp only [negative, if_true] at traced
              obtain ⟨result, hresult, traced⟩ := bind_ok_inv _ _ _ traced
              cases traced
              obtain ⟨wordSpec, wordBound⟩ :=
                ParserSelectedMagnitudeSign
                  (⟨first :: tail, source_bound⟩ : Str) f _ hsource hout
              have exponentBound := ParserSelectedExponent
                (⟨first :: tail, source_bound⟩ : Str) f _ hsource hout
              have mantissaBound := ParserSelectedMantissaBound
                (⟨first :: tail, source_bound⟩ : Str) f _ hsource hout
              change DecimalGoalVocabularyDefault.assembledWord selected word at wordSpec
              change word.val < 2 ^ 63 at wordBound
              have hwordNonNan := ParserAssembledPairNonNan selected word
                exponentBound.1 exponentBound.2 mantissaBound
                hselectedInfinity wordSpec wordBound
              obtain ⟨canonical, hcanonical, canonicalSpec⟩ :=
                AssemblyCorrespondence selected
              rw [hassembled] at hcanonical
              cases hcanonical
              have assembledVal : assembled.val = word.val := by
                dsimp only [DecimalGoalVocabularyDefault] at wordSpec canonicalSpec
                exact canonicalSpec.trans wordSpec.symm
              have assembledEq : assembled = word :=
                UScalar.eq_of_val_eq assembledVal
              subst assembled
              change ModelF64Neg word = ok f at hresult
              cases hresult
              dsimp only [DecimalGoalVocabularyDefault] at hwordNonNan ⊢
              intro hnan
              apply hwordNonNan
              rcases hnan with ⟨hexponent, hfraction⟩
              refine ⟨?_, ?_⟩
              · change (((word.bv ^^^ BitVec.ofNat 64 (2 ^ 63)).toNat) %
                    2 ^ 63) / 2 ^ 52 = 2047 at hexponent
                rw [BitVec.toNat_xor] at hexponent
                have hbit : (BitVec.ofNat 64 (2 ^ 63)).toNat = 2 ^ 63 := by
                  norm_num
                rw [hbit] at hexponent
                change (word.val ^^^ (2 ^ 63)) % (2 ^ 63) /
                    (2 ^ 52) = 2047 at hexponent
                have hxor : word.val ^^^ (2 ^ 63) = word.val + 2 ^ 63 := by
                  have hor := Nat.two_pow_add_eq_or_of_lt wordBound 1
                  have hx : word.val ^^^ (2 ^ 63) =
                      (2 ^ 63) ||| word.val := by
                    apply Nat.eq_of_testBit_eq
                    intro k
                    rw [Nat.testBit_xor, Nat.testBit_or, Nat.testBit_two_pow]
                    by_cases hk : k = 63
                    · subst k
                      rw [Nat.testBit_lt_two_pow wordBound]
                      decide
                    · have hk' : 63 ≠ k := Ne.symm hk
                      simp [hk, hk']
                  exact hx.trans (by simpa [Nat.add_comm] using hor.symm)
                rw [hxor, Nat.add_mod] at hexponent
                norm_num at hexponent
                exact hexponent
              · change (((word.bv ^^^ BitVec.ofNat 64 (2 ^ 63)).toNat) %
                    2 ^ 52) ≠ 0 at hfraction
                rw [BitVec.toNat_xor] at hfraction
                have hbit : (BitVec.ofNat 64 (2 ^ 63)).toNat = 2 ^ 63 := by
                  norm_num
                rw [hbit] at hfraction
                change (word.val ^^^ (2 ^ 63)) % (2 ^ 52) ≠ 0 at hfraction
                have hxor : word.val ^^^ (2 ^ 63) = word.val + 2 ^ 63 := by
                  have hor := Nat.two_pow_add_eq_or_of_lt wordBound 1
                  have hx : word.val ^^^ (2 ^ 63) =
                      (2 ^ 63) ||| word.val := by
                    apply Nat.eq_of_testBit_eq
                    intro k
                    rw [Nat.testBit_xor, Nat.testBit_or, Nat.testBit_two_pow]
                    by_cases hk : k = 63
                    · subst k
                      rw [Nat.testBit_lt_two_pow wordBound]
                      decide
                    · have hk' : 63 ≠ k := Ne.symm hk
                      simp [hk, hk']
                  exact hx.trans (by simpa [Nat.add_comm] using hor.symm)
                rw [hxor, Nat.add_mod] at hfraction
                norm_num at hfraction
                exact hfraction
            · simp only [negative, if_false] at traced
              cases traced
              obtain ⟨wordSpec, wordBound⟩ :=
                ParserSelectedMagnitudeSign
                  (⟨first :: tail, source_bound⟩ : Str) f _ hsource hout
              have exponentBound := ParserSelectedExponent
                (⟨first :: tail, source_bound⟩ : Str) f _ hsource hout
              have mantissaBound := ParserSelectedMantissaBound
                (⟨first :: tail, source_bound⟩ : Str) f _ hsource hout
              change DecimalGoalVocabularyDefault.assembledWord selected word at wordSpec
              change word.val < 2 ^ 63 at wordBound
              have hwordNonNan := ParserAssembledPairNonNan selected word
                exponentBound.1 exponentBound.2 mantissaBound
                hselectedInfinity wordSpec wordBound
              obtain ⟨canonical, hcanonical, canonicalSpec⟩ :=
                AssemblyCorrespondence selected
              rw [hassembled] at hcanonical
              cases hcanonical
              have resultVal : f.val = word.val := by
                dsimp only [DecimalGoalVocabularyDefault] at wordSpec canonicalSpec
                exact canonicalSpec.trans wordSpec.symm
              dsimp only [DecimalGoalVocabularyDefault] at hwordNonNan ⊢
              simpa [resultVal] using hwordNonNan

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalGoalVocabularyDefault
import Tablet.P9_T1_ActualDispatchOperations
import Tablet.num_imp_dec2flt_dec2flt
import Tablet.SourceValidByteSlice
import Tablet.ModelStrAsBytes
import Tablet.ModelSliceFirst
import Tablet.ModelF64Neg
import Tablet.SpecialHelperRejection
import Tablet.SpecialHelperInfinity
import Tablet.P13_T1_NativeCorrectOrDefer
import Tablet.ParserSelectedExponent
import Tablet.ParserSelectedMantissaBound
import Tablet.ParserComputeFloatInfinityMantissa
import Tablet.ParserSlowInfinityMantissa
import Tablet.ParserAssembledPairNonNan
import Tablet.ParserSelectedMagnitudeSign
import Tablet.AssemblyCorrespondence

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P13_T1_ParserNonNanResults]
theorem P13_T1_ParserNonNanResults :
    forall (s : Str) (f : F64),
      DecimalGoalVocabularyDefault.sourceString s ->
      (¬ exists sign : Option Bool, DecimalGoalVocabularyDefault.specialLiteral
        (DecimalGoalVocabularyDefault.bytes s) sign true) ->
      num.imp.dec2flt.dec2flt F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64
        F64.Insts.CoreOpsArithDivF64F64 F64.Insts.CoreOpsArithMulF64F64
        F64.Insts.CoreOpsArithNegF64 s = ok (core.result.Result.Ok f) ->
      ¬ DecimalGoalVocabularyDefault.isNan f := by
-- BODY
  intro s f hsource hnotNanLiteral hpublic
  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 infinity_not_nan : ∀ (negative : Bool) (value : F64),
      DecimalGoalVocabularyDefault.isInfinity negative value ->
      ¬ DecimalGoalVocabularyDefault.isNan value := by
    intro negative value hinfinity hnan
    dsimp only [DecimalGoalVocabularyDefault] at hinfinity hnan
    cases negative <;>
      simp only [Bool.false_eq_true, if_false, if_true] at hinfinity
    · rw [hinfinity] at hnan
      norm_num at hnan
    · rw [hinfinity] at hnan
      norm_num at hnan
  have signed_bounded_not_nan : ∀ (negative : Bool) (magnitude result : F64),
      magnitude.val ≤ 2047 * 2 ^ 52 ->
      DecimalGoalVocabularyDefault.signedMagnitude negative magnitude result ->
      ¬ DecimalGoalVocabularyDefault.isNan result := by
    intro negative magnitude result hbound hsigned hnan
    dsimp only [DecimalGoalVocabularyDefault] at hsigned hnan
    rcases hsigned with ⟨hmag, hresult⟩
    rcases hnan with ⟨hexponent, hfraction⟩
    have hmodSign : result.val % (2 ^ 63) = magnitude.val := by
      cases negative with
      | false =>
        simp only [Bool.false_eq_true, if_false, add_zero] at hresult
        rw [hresult, Nat.mod_eq_of_lt hmag]
      | true =>
        simp only [if_true] at hresult
        rw [hresult, Nat.add_mod]
        have hm := Nat.mod_eq_of_lt hmag
        rw [hm, Nat.mod_self, Nat.add_zero, hm]
    have hmodFraction :
        result.val % (2 ^ 52) = magnitude.val % (2 ^ 52) := by
      cases negative with
      | false =>
        simp only [Bool.false_eq_true, if_false, add_zero] at hresult
        rw [hresult]
      | true =>
        simp only [if_true] at hresult
        rw [hresult, Nat.add_mod]
        norm_num
    rw [hmodSign] at hexponent
    have hdecomp := Nat.mod_add_div magnitude.val (2 ^ 52)
    rw [hexponent] at hdecomp
    have hmagExact : magnitude.val = 2047 * 2 ^ 52 := by
      norm_num at hdecomp ⊢
      omega
    rw [hmodFraction, hmagExact] at hfraction
    norm_num at hfraction
  unfold num.imp.dec2flt.dec2flt at hpublic
  obtain ⟨out, hout, hpublic⟩ := bind_ok_inv _ _ _ hpublic
  rcases out with ⟨publicResult, trace⟩
  cases hpublic
  have traced :
      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, trace) := hout
  rcases s with ⟨source, source_bound⟩
  cases source with
  | nil =>
      unfold num.imp.dec2flt.dec2flt_with_trace at traced
      obtain ⟨body, hbody, traced⟩ := bind_ok_inv _ _ _ traced
      have hbodyEq : body = (⟨[], source_bound⟩ : Slice Std.U8) := by
        simpa [core.str.Str.as_bytes, ModelStrAsBytes] using hbody.symm
      subst body
      obtain ⟨firstOption, hfirst, traced⟩ := bind_ok_inv _ _ _ traced
      have hfirstEq : firstOption = none := by
        simpa [core.slice.Slice.first, ModelSliceFirst] using hfirst.symm
      subst firstOption
      obtain ⟨pfe, hpfe, traced⟩ := bind_ok_inv _ _ _ traced
      cases traced
  | cons first tail =>
      unfold num.imp.dec2flt.dec2flt_with_trace at traced
      obtain ⟨body, hbody, traced⟩ := bind_ok_inv _ _ _ traced
      have hbodyEq : body = (⟨first :: tail, source_bound⟩ : Slice Std.U8) := by
        simpa [core.str.Str.as_bytes, ModelStrAsBytes] using hbody.symm
      subst body
      obtain ⟨firstOption, hfirst, traced⟩ := bind_ok_inv _ _ _ traced
      have hfirstEq : firstOption = some first := by
        simpa [core.slice.Slice.first, ModelSliceFirst] using hfirst.symm
      subst firstOption
      obtain ⟨body1, hbody1, traced⟩ := bind_ok_inv _ _ _ traced
      have body1Valid : SourceValidByteSlice body1 := by
        unfold SourceValidByteSlice
        have hslen := hsource.1
        by_cases hminus : first = 45#u8
        · simp only [hminus, if_true] at hbody1
          simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
            Slice.drop] at hbody1
          cases hbody1
          simp at hslen ⊢
          omega
        · simp only [hminus, if_false] at hbody1
          by_cases hplus : first = 43#u8
          · simp only [hplus, if_true] at hbody1
            simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
              Slice.drop] at hbody1
            cases hbody1
            simp at hslen ⊢
            omega
          · simp only [hplus, if_false] at hbody1
            cases hbody1
            exact hslen
      have noNanBody : ¬ DecimalGoalVocabularyDefault.specialLiteral
          (DecimalGoalVocabularyDefault.bytes body1) none true := by
        intro hspecial
        apply hnotNanLiteral
        by_cases hminus : first = 45#u8
        · simp only [hminus, if_true] at hbody1
          simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
            Slice.drop] at hbody1
          cases hbody1
          refine ⟨some true, ?_⟩
          dsimp only [DecimalGoalVocabularyDefault] at hspecial ⊢
          rcases hspecial with ⟨specialBody, hspecialBody, hcase⟩
          refine ⟨specialBody, ?_, hcase⟩
          subst first
          simpa using hspecialBody
        · simp only [hminus, if_false] at hbody1
          by_cases hplus : first = 43#u8
          · simp only [hplus, if_true] at hbody1
            simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
              Slice.drop] at hbody1
            cases hbody1
            refine ⟨some false, ?_⟩
            dsimp only [DecimalGoalVocabularyDefault] at hspecial ⊢
            rcases hspecial with ⟨specialBody, hspecialBody, hcase⟩
            refine ⟨specialBody, ?_, hcase⟩
            subst first
            simpa using hspecialBody
          · simp only [hplus, if_false] at hbody1
            cases hbody1
            exact ⟨none, hspecial⟩
      obtain ⟨empty, hempty, traced⟩ := bind_ok_inv _ _ _ traced
      by_cases emptyTrue : empty = true
      · simp only [emptyTrue, if_true] at traced
        obtain ⟨pfe, hpfe, traced⟩ := bind_ok_inv _ _ _ traced
        cases traced
      · have emptyFalse : empty = false := Bool.eq_false_of_not_eq_true emptyTrue
        simp only [emptyFalse, if_false] at traced
        obtain ⟨parsed, hparsed, traced⟩ := bind_ok_inv _ _ _ traced
        cases parsed with
        | none =>
          obtain ⟨special, hspecial, traced⟩ := bind_ok_inv _ _ _ traced
          cases special with
          | none =>
            obtain ⟨pfe, hpfe, traced⟩ := bind_ok_inv _ _ _ traced
            cases traced
          | some value =>
            cases traced
            by_cases hInf : DecimalGoalVocabularyDefault.specialLiteral
                (DecimalGoalVocabularyDefault.bytes body1) none false
            · obtain ⟨infinity, hinfinityEq, hinfinity⟩ :=
                SpecialHelperInfinity body1 (decide (first = 45#u8)) hInf
              rw [hspecial] at hinfinityEq
              cases hinfinityEq
              exact infinity_not_nan _ _ hinfinity
            · have hrejection := SpecialHelperRejection body1
                (decide (first = 45#u8)) body1Valid hInf noNanBody
              rw [hspecial] at hrejection
              cases hrejection
        | some r =>
          obtain ⟨fast, hfast, traced⟩ := bind_ok_inv _ _ _ traced
          cases fast with
          | some value =>
            have hnative := P13_T1_NativeCorrectOrDefer
              ({ r with negative := decide (first = 45#u8) })
              (some value) hfast
            rcases hnative with hnone |
              ⟨value', magnitude, hvalue, _, hrounds, hsigned⟩
            · simp at hnone
            · cases hvalue
              cases traced
              dsimp only [DecimalGoalVocabularyDefault] at hrounds
              exact signed_bounded_not_nan _ _ _ hrounds.2.1 hsigned
          | none =>
            obtain ⟨firstPair, hfirstPair, traced⟩ := bind_ok_inv _ _ _ traced
            have hfirstInfinity :=
              ParserComputeFloatInfinityMantissa _ _ _ hfirstPair
            obtain ⟨phase1, hphase1, traced⟩ := bind_ok_inv _ _ _ traced
            rcases phase1 with ⟨many1, many2, firstPair1, fp, guard⟩
            have hfp : fp = firstPair := by
              by_cases hm : r.many_digits
              · simp only [hm, if_true] at hphase1
                cases hphase1
                rfl
              · simp only [hm, if_false] at hphase1
                cases hphase1
                rfl
            subst fp
            obtain ⟨phase2, hphase2, traced⟩ := bind_ok_inv _ _ _ traced
            rcases phase2 with
              ⟨post, secondExp, secondMant, secondResult, secondEqual, forced⟩
            have hpostInfinity :
                post.p_biased.val = 2047 -> post.m.val = 0 := by
              cases guard with
              | false =>
                simp only [Bool.false_eq_true, if_false] at hphase2
                cases hphase2
                exact hfirstInfinity
              | true =>
                simp only [if_true] at hphase2
                obtain ⟨mantissa, hmantissa, hphase2⟩ :=
                  bind_ok_inv _ _ _ hphase2
                obtain ⟨second, hsecond, hphase2⟩ :=
                  bind_ok_inv _ _ _ hphase2
                obtain ⟨equal, hequal, hphase2⟩ :=
                  bind_ok_inv _ _ _ hphase2
                obtain ⟨chosen, hchosen, hphase2⟩ :=
                  bind_ok_inv _ _ _ hphase2
                rcases chosen with ⟨bf, wasForced⟩
                have hbf : bf.p_biased.val = 2047 -> bf.m.val = 0 := by
                  by_cases heq : equal = true
                  · simp only [heq, if_true] at hchosen
                    cases hchosen
                    exact hfirstInfinity
                  · have heqf : equal = false :=
                      Bool.eq_false_of_not_eq_true heq
                    simp only [heqf, if_false] at hchosen
                    cases hchosen
                    intro hp
                    norm_num at hp
                cases hphase2
                exact hbf
            obtain ⟨phase3, hphase3, traced⟩ := bind_ok_inv _ _ _ traced
            rcases phase3 with ⟨selected, slowGuard, slowResult⟩
            have hselectedInfinity :
                selected.p_biased.val = 2047 -> selected.m.val = 0 := by
              by_cases hneg : post.p_biased < 0#i32
              · simp only [hneg, if_true] at hphase3
                obtain ⟨replacement, hreplacement, hphase3⟩ :=
                  bind_ok_inv _ _ _ hphase3
                have hreplacement :=
                  ParserSlowInfinityMantissa _ _ hreplacement
                cases hphase3
                exact hreplacement
              · simp only [hneg, if_false] at hphase3
                cases hphase3
                exact hpostInfinity
            obtain ⟨i, hi, traced⟩ := bind_ok_inv _ _ _ traced
            obtain ⟨i1, hi1, traced⟩ := bind_ok_inv _ _ _ traced
            obtain ⟨i2, hi2, traced⟩ := bind_ok_inv _ _ _ traced
            obtain ⟨word, hword, traced⟩ := bind_ok_inv _ _ _ traced
            obtain ⟨assembled, hassembled, traced⟩ := bind_ok_inv _ _ _ traced
            by_cases negative : first = 45#u8
            · simp only [negative, if_true] at traced
              obtain ⟨result, hresult, traced⟩ := bind_ok_inv _ _ _ traced
              cases traced
              obtain ⟨wordSpec, wordBound⟩ :=
                ParserSelectedMagnitudeSign
                  (⟨first :: tail, source_bound⟩ : Str) f _ hsource hout
              have exponentBound := ParserSelectedExponent
                (⟨first :: tail, source_bound⟩ : Str) f _ hsource hout
              have mantissaBound := ParserSelectedMantissaBound
                (⟨first :: tail, source_bound⟩ : Str) f _ hsource hout
              change DecimalGoalVocabularyDefault.assembledWord selected word at wordSpec
              change word.val < 2 ^ 63 at wordBound
              have hwordNonNan := ParserAssembledPairNonNan selected word
                exponentBound.1 exponentBound.2 mantissaBound
                hselectedInfinity wordSpec wordBound
              obtain ⟨canonical, hcanonical, canonicalSpec⟩ :=
                AssemblyCorrespondence selected
              rw [hassembled] at hcanonical
              cases hcanonical
              have assembledVal : assembled.val = word.val := by
                dsimp only [DecimalGoalVocabularyDefault] at wordSpec canonicalSpec
                exact canonicalSpec.trans wordSpec.symm
              have assembledEq : assembled = word :=
                UScalar.eq_of_val_eq assembledVal
              subst assembled
              change ModelF64Neg word = ok f at hresult
              cases hresult
              dsimp only [DecimalGoalVocabularyDefault] at hwordNonNan ⊢
              intro hnan
              apply hwordNonNan
              rcases hnan with ⟨hexponent, hfraction⟩
              refine ⟨?_, ?_⟩
              · change (((word.bv ^^^ BitVec.ofNat 64 (2 ^ 63)).toNat) %
                    2 ^ 63) / 2 ^ 52 = 2047 at hexponent
                rw [BitVec.toNat_xor] at hexponent
                have hbit : (BitVec.ofNat 64 (2 ^ 63)).toNat = 2 ^ 63 := by
                  norm_num
                rw [hbit] at hexponent
                change (word.val ^^^ (2 ^ 63)) % (2 ^ 63) /
                    (2 ^ 52) = 2047 at hexponent
                have hxor : word.val ^^^ (2 ^ 63) = word.val + 2 ^ 63 := by
                  have hor := Nat.two_pow_add_eq_or_of_lt wordBound 1
                  have hx : word.val ^^^ (2 ^ 63) =
                      (2 ^ 63) ||| word.val := by
                    apply Nat.eq_of_testBit_eq
                    intro k
                    rw [Nat.testBit_xor, Nat.testBit_or, Nat.testBit_two_pow]
                    by_cases hk : k = 63
                    · subst k
                      rw [Nat.testBit_lt_two_pow wordBound]
                      decide
                    · have hk' : 63 ≠ k := Ne.symm hk
                      simp [hk, hk']
                  exact hx.trans (by simpa [Nat.add_comm] using hor.symm)
                rw [hxor, Nat.add_mod] at hexponent
                norm_num at hexponent
                exact hexponent
              · change (((word.bv ^^^ BitVec.ofNat 64 (2 ^ 63)).toNat) %
                    2 ^ 52) ≠ 0 at hfraction
                rw [BitVec.toNat_xor] at hfraction
                have hbit : (BitVec.ofNat 64 (2 ^ 63)).toNat = 2 ^ 63 := by
                  norm_num
                rw [hbit] at hfraction
                change (word.val ^^^ (2 ^ 63)) % (2 ^ 52) ≠ 0 at hfraction
                have hxor : word.val ^^^ (2 ^ 63) = word.val + 2 ^ 63 := by
                  have hor := Nat.two_pow_add_eq_or_of_lt wordBound 1
                  have hx : word.val ^^^ (2 ^ 63) =
                      (2 ^ 63) ||| word.val := by
                    apply Nat.eq_of_testBit_eq
                    intro k
                    rw [Nat.testBit_xor, Nat.testBit_or, Nat.testBit_two_pow]
                    by_cases hk : k = 63
                    · subst k
                      rw [Nat.testBit_lt_two_pow wordBound]
                      decide
                    · have hk' : 63 ≠ k := Ne.symm hk
                      simp [hk, hk']
                  exact hx.trans (by simpa [Nat.add_comm] using hor.symm)
                rw [hxor, Nat.add_mod] at hfraction
                norm_num at hfraction
                exact hfraction
            · simp only [negative, if_false] at traced
              cases traced
              obtain ⟨wordSpec, wordBound⟩ :=
                ParserSelectedMagnitudeSign
                  (⟨first :: tail, source_bound⟩ : Str) f _ hsource hout
              have exponentBound := ParserSelectedExponent
                (⟨first :: tail, source_bound⟩ : Str) f _ hsource hout
              have mantissaBound := ParserSelectedMantissaBound
                (⟨first :: tail, source_bound⟩ : Str) f _ hsource hout
              change DecimalGoalVocabularyDefault.assembledWord selected word at wordSpec
              change word.val < 2 ^ 63 at wordBound
              have hwordNonNan := ParserAssembledPairNonNan selected word
                exponentBound.1 exponentBound.2 mantissaBound
                hselectedInfinity wordSpec wordBound
              obtain ⟨canonical, hcanonical, canonicalSpec⟩ :=
                AssemblyCorrespondence selected
              rw [hassembled] at hcanonical
              cases hcanonical
              have resultVal : f.val = word.val := by
                dsimp only [DecimalGoalVocabularyDefault] at wordSpec canonicalSpec
                exact canonicalSpec.trans wordSpec.symm
              dsimp only [DecimalGoalVocabularyDefault] at hwordNonNan ⊢
              simpa [resultVal] using hwordNonNan

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (199)
More program dependencies (157)
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 190 entries
Technical checking records
Local closure summary
{
  "node": "P13_T1_ParserNonNanResults",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "ae4958f04f0192f1ae6d0981f765e66d68aee0d99fae2b98a57a7479bae123d7",
  "active_statement_hash": "8de75c94d937f9489fbdd16d6243cdfb5a39698ed3c2347411611f61bf500f27",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "576e6bf9480cfd32f53c309c7ac8fb115c1ba6e413f299ed5ff9fa7c647e6769",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-199",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "AssemblyCorrespondence": "e3495a6647394e20",
    "DecimalGoalVocabularyDefault": "94cb4c406f8efb78",
    "P13_T1_NativeCorrectOrDefer": "bc7b08c927fd6202",
    "ParserAssembledPairNonNan": "5be7fbb4c365e47d",
    "ParserComputeFloatInfinityMantissa": "3d5b3a671765d393",
    "ParserSelectedExponent": "2d4e5f8c99bf68c7",
    "ParserSelectedMagnitudeSign": "70c47baac228615a",
    "ParserSelectedMantissaBound": "8c0433452b441220",
    "ParserSlowInfinityMantissa": "35810a563a0faf15",
    "SpecialHelperInfinity": "4124e26dd85ce114",
    "SpecialHelperRejection": "e1e229c5dd3ecf3c",
    "num_imp_dec2flt_dec2flt_with_trace": "f3e7d4ffbf9b4d68",
    "num_imp_traits_Float_SIG_BITS_default": "9ab622d85ef04d94"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "91e42e6d78c02c8e",
    "DecimalGoalVocabularyDefault": "71a18a6c119a3cdc",
    "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",
    "ModelF64Neg": "78a021de99eb373b",
    "ModelSliceFirst": "a31394b3c1080f71",
    "Preamble": "86dc391ab250c79b",
    "SourceValidByteSlice": "3dc42716a6378d26",
    "core_ops_arith_Neg": "0ff07135a60bcbf9",
    "num_float_parse_ParseFloatError": "c43614ff2149ecf2",
    "num_imp_dec2flt_Dec2FltAssemblyTrace": "7bda5de02611ad76",
    "num_imp_dec2flt_Dec2FltTrace": "644abe66cec8ac98",
    "num_imp_dec2flt_Lemire": "1923f90c53d323e6",
    "num_imp_dec2flt_biased_fp_to_float": "acdde060e8d35ca7",
    "num_imp_dec2flt_common_BiasedFp": "31d81cf1cf22709f",
    "num_imp_dec2flt_common_BiasedFp_Insts_CoreCmpPartialEqBiasedFp_eq": "fbbf553e3c502a05",
    "num_imp_dec2flt_dec2flt": "f9f7d93449a25efa",
    "num_imp_dec2flt_dec2flt_with_trace": "5665189e0304a27e",
    "num_imp_dec2flt_decimal_Decimal": "fe99f3f976739f2d",
    "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": "Lean proof closed; predictive prose review no longer required",
  "assessment_status": null,
  "fingerprint_relation": "unknown",
  "assessment_fingerprint": null,
  "current_fingerprint": "sound-v2:45ab2e860f621dc7fc4e481fa6de970dc02b8b01d577d7492e5897ef8c707dcf",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}