import Tablet.ParserNumericTraceShape import Tablet.ParserSelectedMagnitudeSign import Tablet.AssemblyCorrespondence import Tablet.DecimalGoalVocabularyDefault import Tablet.F64_Insts_CoreOpsArithNegF64 import Tablet.ModelF64Neg import Tablet.num_imp_dec2flt_biased_fp_to_float import Tablet.num_imp_dec2flt_dec2flt_with_trace open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: ParserAssembledTraceSign] theorem ParserAssembledTraceSign : ∀ (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) -> DecimalGoalVocabularyDefault.signedMagnitude (DecimalGoalVocabularyDefault.lexicalNegative x) tr.assembly_result f := by -- BODY intro s x f tr hsource hvalid hbytes htrace 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 shape := ParserNumericTraceShape s x f (num.imp.dec2flt.Dec2FltTrace.Assembled tr) hsource hvalid hbytes htrace rcases shape with fast | assembled · rcases fast with ⟨body, negative, parsed, fastInput, fastResult, hEq, _, _, _, _, _⟩ cases hEq · rcases assembled with ⟨assemblyTrace, parsed, hEq, _, _, _, hnegative, _, _⟩ cases hEq have hlex : tr.negative = DecimalGoalVocabularyDefault.lexicalNegative x := hnegative have hmain := htrace unfold num.imp.dec2flt.dec2flt_with_trace at htrace obtain ⟨body, hbody, htrace⟩ := bind_ok_inv _ _ _ htrace obtain ⟨first, hfirst, htrace⟩ := bind_ok_inv _ _ _ htrace cases first with | none => obtain ⟨pfe, hpfe, htrace⟩ := bind_ok_inv _ _ _ htrace cases htrace | some c => obtain ⟨body1, hbody1, htrace⟩ := bind_ok_inv _ _ _ htrace obtain ⟨empty, hempty, htrace⟩ := bind_ok_inv _ _ _ htrace by_cases he : empty = true · simp only [he, if_true] at htrace obtain ⟨pfe, hpfe, htrace⟩ := bind_ok_inv _ _ _ htrace cases htrace · have hef : empty = false := Bool.eq_false_of_not_eq_true he simp only [hef, if_false] at htrace obtain ⟨parsedResult, hparsed, htrace⟩ := bind_ok_inv _ _ _ htrace cases parsedResult with | none => obtain ⟨special, hspecial, htrace⟩ := bind_ok_inv _ _ _ htrace cases special with | none => obtain ⟨pfe, hpfe, htrace⟩ := bind_ok_inv _ _ _ htrace cases htrace | some value => cases htrace | some r => obtain ⟨fastResult, hfast, htrace⟩ := bind_ok_inv _ _ _ htrace cases fastResult with | some value => cases htrace | none => obtain ⟨firstPair, hfirstPair, htrace⟩ := bind_ok_inv _ _ _ htrace obtain ⟨phase1, hphase1, htrace⟩ := bind_ok_inv _ _ _ htrace rcases phase1 with ⟨many1, many2, firstPair1, fp, guard⟩ obtain ⟨phase2, hphase2, htrace⟩ := bind_ok_inv _ _ _ htrace rcases phase2 with ⟨post, secondExp, secondMant, secondResult, secondEqual, forced⟩ obtain ⟨phase3, hphase3, htrace⟩ := bind_ok_inv _ _ _ htrace rcases phase3 with ⟨selected, slowGuard, slowResult⟩ obtain ⟨i, hi, htrace⟩ := bind_ok_inv _ _ _ htrace obtain ⟨i1, hi1, htrace⟩ := bind_ok_inv _ _ _ htrace obtain ⟨i2, hi2, htrace⟩ := bind_ok_inv _ _ _ htrace obtain ⟨word, hword, htrace⟩ := bind_ok_inv _ _ _ htrace obtain ⟨assembled, hassembled, htrace⟩ := bind_ok_inv _ _ _ htrace by_cases hneg : c = 45#u8 · simp only [hneg, if_true] at htrace obtain ⟨result, hresult, htrace⟩ := bind_ok_inv _ _ _ htrace have hselected := ParserSelectedMagnitudeSign s f tr hsource hmain cases htrace have hselected' : DecimalGoalVocabularyDefault.assembledWord selected word ∧ word.val < 2 ^ 63 := by simpa using hselected rcases hselected' with ⟨wordSpec, wordBound⟩ change word.val < 2 ^ 63 at 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 assembledBound : assembled.val < 2 ^ 63 := by omega rw [← hlex] simp only [hneg, decide_true] change ModelF64Neg assembled = ok f at hresult cases hresult dsimp only [DecimalGoalVocabularyDefault] refine ⟨assembledBound, ?_⟩ have hxor : assembled.val ^^^ (2 ^ 63) = (2 ^ 63) ||| assembled.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 assembledBound] decide · have hk' : 63 ≠ k := Ne.symm hk simp [hk, hk'] have hor : (2 ^ 63) ||| assembled.val = assembled.val + 2 ^ 63 := by have h := Nat.two_pow_add_eq_or_of_lt assembledBound 1 simpa [Nat.add_comm] using h.symm change (assembled.bv ^^^ BitVec.ofNat 64 (2 ^ 63)).toNat = assembled.val + 2 ^ 63 rw [BitVec.toNat_xor, BitVec.toNat_ofNat] norm_num exact hxor.trans hor · simp only [hneg, if_false] at htrace have hselected := ParserSelectedMagnitudeSign s f tr hsource hmain cases htrace have hselected' : DecimalGoalVocabularyDefault.assembledWord selected word ∧ word.val < 2 ^ 63 := by simpa using hselected rcases hselected' with ⟨wordSpec, wordBound⟩ change word.val < 2 ^ 63 at wordBound obtain ⟨canonical, hcanonical, canonicalSpec⟩ := AssemblyCorrespondence selected rw [hassembled] at hcanonical cases hcanonical have assembledVal : f.val = word.val := by dsimp only [DecimalGoalVocabularyDefault] at wordSpec canonicalSpec exact canonicalSpec.trans wordSpec.symm have assembledBound : f.val < 2 ^ 63 := by omega rw [← hlex] simp only [hneg, decide_false] dsimp only [DecimalGoalVocabularyDefault] exact ⟨assembledBound, by simp⟩ end dec2flt_model