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