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.SpecialHelperRejection import Tablet.P13_T1_NativeCorrectOrDefer import Tablet.ParserSelectedMagnitudeSign import Tablet.AssemblyCorrespondence import Tablet.Binary64ApplySign open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P13_T1_ParserNumericSign] theorem P13_T1_ParserNumericSign : forall (s : Str) (x : DecimalLexeme) (f : F64), DecimalGoalVocabularyDefault.sourceString s -> DecimalGoalVocabularyDefault.validNumeric x -> DecimalGoalVocabularyDefault.renderNumeric x = DecimalGoalVocabularyDefault.bytes s -> 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.isNegative f ↔ DecimalGoalVocabularyDefault.lexicalNegative x = true) := by -- BODY intro s x f hsource hvalid hrender 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 signed_negative : ∀ (negative : Bool) (magnitude result : F64), DecimalGoalVocabularyDefault.signedMagnitude negative magnitude result → (DecimalGoalVocabularyDefault.isNegative result ↔ negative = true) := by intro negative magnitude result hsigned dsimp only [DecimalGoalVocabularyDefault] at hsigned ⊢ rcases hsigned with ⟨hbound, hval⟩ cases negative <;> simp only [Bool.false_eq_true, if_false, if_true, add_zero] at hval ⊢ · rw [hval] exact ⟨fun h => (Nat.not_le_of_lt hbound h).elim, False.elim⟩ · rw [hval] exact ⟨fun _ => trivial, fun _ => Nat.le_add_left _ _⟩ let fractionBytes : List Nat := match x.fractionDigits with | none => [] | some ds => 46 :: ds.map (fun d => 48 + d) let exponentBytes : List Nat := match x.exponent with | none => [] | some (upper, sign, ds) => (if upper then 69 else 101) :: (match sign with | none => [] | some neg => [if neg then 45 else 43]) ++ ds.map (fun d => 48 + d) let numericBody : List Nat := x.integerDigits.map (fun d => 48 + d) ++ fractionBytes ++ exponentBytes have numericHead : ∃ head rest, numericBody = head :: rest ∧ (head = 46 ∨ (48 ≤ head ∧ head < 58)) := by have hv := hvalid dsimp only [DecimalGoalVocabularyDefault] at hv cases hi : x.integerDigits with | nil => cases hf : x.fractionDigits with | none => simp [hi, hf] at hv | some ds => refine ⟨46, ds.map (fun d => 48 + d) ++ exponentBytes, ?_, Or.inl rfl⟩ simp [numericBody, fractionBytes, hi, hf] | cons d ds => refine ⟨48 + d, ds.map (fun d => 48 + d) ++ fractionBytes ++ exponentBytes, ?_, Or.inr ?_⟩ · simp [numericBody, hi] · have hd : d < 10 := hv.1 d (by simp [hi]) omega obtain ⟨head, rest, hhead, hheadRange⟩ := numericHead have hrenderBody : (match x.leadingSign with | none => [] | some negative => [if negative then 45 else 43]) ++ (head :: rest) = DecimalGoalVocabularyDefault.bytes s := by rw [← hhead] simpa only [DecimalGoalVocabularyDefault, numericBody, fractionBytes, exponentBytes, List.append_assoc] using hrender have notSpecial : ∀ nan : Bool, ¬ DecimalGoalVocabularyDefault.specialLiteral (head :: rest) none nan := by intro nan hspecial dsimp only [DecimalGoalVocabularyDefault] at hspecial rcases hspecial with ⟨specialBody, hspecialBody, hcase⟩ simp only [List.nil_append] at hspecialBody rw [← hspecialBody] at hcase cases nan with | false => simp only [Bool.false_eq_true, if_false] at hcase rcases hcase with hcase | hcase <;> simp only [List.forall₂_cons] at hcase <;> omega | true => simp only [if_true, List.forall₂_cons] at hcase omega 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 hstrip : decide (first = 45#u8) = DecimalGoalVocabularyDefault.lexicalNegative x ∧ DecimalGoalVocabularyDefault.bytes body1 = head :: rest ∧ SourceValidByteSlice body1 := by have hslen := hsource.1 dsimp only [DecimalGoalVocabularyDefault] at hrenderBody simp only [List.map_cons] at hrenderBody cases hsign : x.leadingSign with | none => simp only [hsign, List.nil_append, List.cons.injEq] at hrenderBody have hminus : first ≠ 45#u8 := by intro heq have hv := congrArg UScalar.val heq norm_num at hv omega have hplus : first ≠ 43#u8 := by intro heq have hv := congrArg UScalar.val heq norm_num at hv omega simp only [hminus, hplus, if_false] at hbody1 cases hbody1 refine ⟨?_, ?_, hslen⟩ · simp [DecimalGoalVocabularyDefault, hsign, hminus] · dsimp only [DecimalGoalVocabularyDefault] simp only [List.map_cons] rw [← hrenderBody.1, ← hrenderBody.2] | some negative => cases negative with | false => simp only [hsign, Bool.false_eq_true, if_false, List.cons_append, List.nil_append, List.cons.injEq] at hrenderBody have hfirstPlus : first = 43#u8 := by apply UScalar.eq_of_val_eq norm_num exact hrenderBody.1.symm simp only [hfirstPlus, if_false, if_true] at hbody1 simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index, Slice.drop] at hbody1 cases hbody1 refine ⟨?_, ?_, ?_⟩ · simp [DecimalGoalVocabularyDefault, hsign, hfirstPlus] · simpa only [DecimalGoalVocabularyDefault] using hrenderBody.2.symm · unfold SourceValidByteSlice simp at hslen ⊢ omega | true => simp only [hsign, if_true, List.cons_append, List.nil_append, List.cons.injEq] at hrenderBody have hfirstMinus : first = 45#u8 := by apply UScalar.eq_of_val_eq norm_num exact hrenderBody.1.symm simp only [hfirstMinus, if_true] at hbody1 simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index, Slice.drop] at hbody1 cases hbody1 refine ⟨?_, ?_, ?_⟩ · simp [DecimalGoalVocabularyDefault, hsign, hfirstMinus] · simpa only [DecimalGoalVocabularyDefault] using hrenderBody.2.symm · unfold SourceValidByteSlice simp at hslen ⊢ omega 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 have hrejection := SpecialHelperRejection body1 (decide (first = 45#u8)) hstrip.2.2 (by rw [hstrip.2.1]; exact notSpecial false) (by rw [hstrip.2.1]; exact notSpecial true) rw [hrejection] at hspecial cases hspecial obtain ⟨pfe, hpfe, traced⟩ := bind_ok_inv _ _ _ traced cases traced | 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, _, _, hsigned⟩ · simp at hnone · cases hvalue cases traced have hnegative := signed_negative _ _ _ hsigned simpa only [hstrip.1] using hnegative | none => obtain ⟨firstPair, hfirstPair, traced⟩ := bind_ok_inv _ _ _ traced obtain ⟨phase1, hphase1, traced⟩ := bind_ok_inv _ _ _ traced rcases phase1 with ⟨many1, many2, firstPair1, fp, guard⟩ obtain ⟨phase2, hphase2, traced⟩ := bind_ok_inv _ _ _ traced rcases phase2 with ⟨post, secondExp, secondMant, secondResult, secondEqual, forced⟩ obtain ⟨phase3, hphase3, traced⟩ := bind_ok_inv _ _ _ traced rcases phase3 with ⟨selected, slowGuard, slowResult⟩ 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 change DecimalGoalVocabularyDefault.assembledWord selected word at wordSpec 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 assembledEq : assembled = word := UScalar.eq_of_val_eq assembledVal subst assembled change ModelF64Neg word = ok f at hresult obtain ⟨signed, hsign, hsigned⟩ := Binary64ApplySign true word wordBound simp only [if_true, hresult, bind_tc_ok] at hsign cases hsign have hnegative := signed_negative _ _ _ hsigned have hlexical : DecimalGoalVocabularyDefault.lexicalNegative x = true := by rw [← hstrip.1] simp [negative] simpa only [hlexical] using hnegative · simp only [negative, if_false] at traced cases traced obtain ⟨wordSpec, wordBound⟩ := ParserSelectedMagnitudeSign (⟨first :: tail, source_bound⟩ : Str) f _ hsource hout change DecimalGoalVocabularyDefault.assembledWord selected word at wordSpec change word.val < 2 ^ 63 at 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 have hlexical : DecimalGoalVocabularyDefault.lexicalNegative x = false := by rw [← hstrip.1] simp [negative] dsimp only [DecimalGoalVocabularyDefault] at wordBound ⊢ rw [resultVal] change 2 ^ 63 ≤ word.val ↔ DecimalGoalVocabularyDefault.lexicalNegative x = true rw [hlexical] simp only [Bool.false_eq_true, iff_false] omega end dec2flt_model