import Tablet.ParserSuccessfulTrace import Tablet.DecimalGoalVocabularyDefault import Tablet.SourceValidByteSlice import Tablet.SpecialHelperRejection import Tablet.ModelStrAsBytes import Tablet.ModelSliceFirst import Tablet.F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64 import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatExtU64 import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64 import Tablet.num_imp_dec2flt_Dec2FltAssemblyTrace import Tablet.num_imp_dec2flt_Dec2FltTrace import Tablet.num_imp_dec2flt_dec2flt_with_trace import Tablet.num_imp_dec2flt_parse_parse_number import Tablet.num_imp_dec2flt_parse_parse_inf_nan open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: ParserNumericTraceShape] theorem ParserNumericTraceShape : ∀ (s : Str) (x : DecimalLexeme) (f : F64) (tr : num.imp.dec2flt.Dec2FltTrace 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, tr) -> (∃ (body : Slice Std.U8) (negative : Bool) (parsed fastInput : num.imp.dec2flt.decimal.Decimal) (fastResult : Option F64), tr = num.imp.dec2flt.Dec2FltTrace.FastPath body negative (some parsed) fastInput fastResult f ∧ body.val ≠ [] ∧ SourceValidByteSlice body ∧ DecimalGoalVocabularyDefault.bytes body = DecimalGoalVocabularyDefault.renderNumeric { x with leadingSign := none } ∧ negative = DecimalGoalVocabularyDefault.lexicalNegative x ∧ num.imp.dec2flt.parse.parse_number body = ok (some parsed)) ∨ ∃ (assemblyTrace : num.imp.dec2flt.Dec2FltAssemblyTrace F64) (parsed : num.imp.dec2flt.decimal.Decimal), tr = num.imp.dec2flt.Dec2FltTrace.Assembled assemblyTrace ∧ assemblyTrace.body.val ≠ [] ∧ SourceValidByteSlice assemblyTrace.body ∧ DecimalGoalVocabularyDefault.bytes assemblyTrace.body = DecimalGoalVocabularyDefault.renderNumeric { x with leadingSign := none } ∧ assemblyTrace.negative = DecimalGoalVocabularyDefault.lexicalNegative x ∧ assemblyTrace.parse_number_result = some parsed ∧ num.imp.dec2flt.parse.parse_number assemblyTrace.body = ok (some parsed) := 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 numeric_head : ∀ (y : DecimalLexeme), y.leadingSign = none -> DecimalGoalVocabularyDefault.validNumeric y -> ∃ n rest, DecimalGoalVocabularyDefault.renderNumeric y = n :: rest ∧ (n = 46 ∨ (48 ≤ n ∧ n ≤ 57)) := by intro y hsign hval rcases y with ⟨sign, ints, frac, exp⟩ simp only at hsign subst sign dsimp only [DecimalGoalVocabularyDefault] at hval ⊢ rcases hval with ⟨hints, hfrac, hnonempty, hexp⟩ cases ints with | nil => simp only [List.map_nil, List.nil_append] at hnonempty ⊢ cases frac with | none => simp at hnonempty | some ds => cases ds with | nil => simp at hnonempty | cons d ds => exact ⟨46, _, rfl, Or.inl rfl⟩ | cons d ds => have hd : d < 10 := hints d (by simp) exact ⟨48 + d, _, rfl, Or.inr (by omega)⟩ have numeric_not_special : ∀ (y : DecimalLexeme) (nan : Bool), y.leadingSign = none -> DecimalGoalVocabularyDefault.validNumeric y -> ¬ DecimalGoalVocabularyDefault.specialLiteral (DecimalGoalVocabularyDefault.renderNumeric y) none nan := by intro y nan hsign hval hspecial obtain ⟨n, rest, hrender, hn⟩ := numeric_head y hsign hval rcases hspecial with ⟨body, hbody, hcase⟩ change DecimalGoalVocabularyDefault.renderNumeric y = body at hbody rw [hrender] at hbody cases hbody dsimp only [DecimalGoalVocabularyDefault] at hcase cases nan with | false => simp only [Bool.false_eq_true, if_false] at hcase rcases hcase with hcase | hcase · cases hcase rename_i hfirst _ rcases hfirst with hfirst | hfirst <;> omega · cases hcase rename_i hfirst _ rcases hfirst with hfirst | hfirst <;> omega | true => simp only [if_true] at hcase cases hcase rename_i hfirst _ rcases hfirst with hfirst | hfirst <;> omega let unsigned : DecimalLexeme := { x with leadingSign := none } have unsigned_sign : unsigned.leadingSign = none := rfl have unsigned_valid : DecimalGoalVocabularyDefault.validNumeric unsigned := by simpa [DecimalGoalVocabularyDefault, unsigned] using hvalid obtain ⟨head, rest, unsigned_render, head_range⟩ := numeric_head unsigned unsigned_sign unsigned_valid unfold num.imp.dec2flt.dec2flt_with_trace at htrace obtain ⟨body, hbody, htrace⟩ := bind_ok_inv _ _ _ htrace rcases s with ⟨source, source_bound⟩ cases source with | nil => dsimp only [DecimalGoalVocabularyDefault] at hbytes cases hs : x.leadingSign with | none => have : DecimalGoalVocabularyDefault.renderNumeric x = [] := hbytes.symm obtain ⟨n, tail, hn, _⟩ := numeric_head x hs hvalid rw [hn] at this simp at this | some sign => cases sign <;> simp [DecimalGoalVocabularyDefault, hs] at hbytes | cons first tail => 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, 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 have lexical : body1.val ≠ [] ∧ SourceValidByteSlice body1 ∧ DecimalGoalVocabularyDefault.bytes body1 = DecimalGoalVocabularyDefault.renderNumeric unsigned ∧ (decide (first = 45#u8) : Bool) = DecimalGoalVocabularyDefault.lexicalNegative x := by cases hsign : x.leadingSign with | none => have full : first.val :: tail.map (fun b => b.val) = head :: rest := by calc first.val :: tail.map (fun b => b.val) = DecimalGoalVocabularyDefault.renderNumeric x := hbytes _ = DecimalGoalVocabularyDefault.renderNumeric unsigned := by simp [DecimalGoalVocabularyDefault, unsigned, hsign] _ = head :: rest := unsigned_render have firstVal : first.val = head := (List.cons.inj full).1 have hminus : first ≠ 45#u8 := by intro heq subst first simp at firstVal rcases head_range with hr | hr <;> omega have hplus : first ≠ 43#u8 := by intro heq subst first simp at firstVal rcases head_range with hr | hr <;> omega simp only [hminus, hplus, if_false] at hbody1 cases hbody1 refine ⟨by simp, ?_, ?_, ?_⟩ · exact hsource.1 · simpa [DecimalGoalVocabularyDefault, unsigned, hsign] using hbytes · simp [DecimalGoalVocabularyDefault, hsign, hminus] | some sign => cases sign with | false => have full : first.val :: tail.map (fun b => b.val) = 43 :: DecimalGoalVocabularyDefault.renderNumeric unsigned := by calc first.val :: tail.map (fun b => b.val) = DecimalGoalVocabularyDefault.renderNumeric x := hbytes _ = 43 :: DecimalGoalVocabularyDefault.renderNumeric unsigned := by simp [DecimalGoalVocabularyDefault, unsigned, hsign] have firstVal : first.val = 43 := (List.cons.inj full).1 have firstEq : first = 43#u8 := UScalar.eq_of_val_eq firstVal have tailRender : tail.map (fun b => b.val) = DecimalGoalVocabularyDefault.renderNumeric unsigned := (List.cons.inj full).2 simp only [firstEq, reduceCtorEq, if_false, if_true] at hbody1 simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index, Slice.drop] at hbody1 cases hbody1 refine ⟨?_, ?_, ?_, ?_⟩ · intro hempty have tailEmpty : tail = [] := by simpa using hempty rw [tailEmpty] at tailRender simp at tailRender rw [unsigned_render] at tailRender simp at tailRender · unfold SourceValidByteSlice have hslen := hsource.1 simp at hslen ⊢ omega · exact tailRender · simp [DecimalGoalVocabularyDefault, hsign, firstEq] | true => have full : first.val :: tail.map (fun b => b.val) = 45 :: DecimalGoalVocabularyDefault.renderNumeric unsigned := by calc first.val :: tail.map (fun b => b.val) = DecimalGoalVocabularyDefault.renderNumeric x := hbytes _ = 45 :: DecimalGoalVocabularyDefault.renderNumeric unsigned := by simp [DecimalGoalVocabularyDefault, unsigned, hsign] have firstVal : first.val = 45 := (List.cons.inj full).1 have firstEq : first = 45#u8 := UScalar.eq_of_val_eq firstVal have tailRender : tail.map (fun b => b.val) = DecimalGoalVocabularyDefault.renderNumeric unsigned := (List.cons.inj full).2 simp only [firstEq, if_true] at hbody1 simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index, Slice.drop] at hbody1 cases hbody1 refine ⟨?_, ?_, ?_, ?_⟩ · intro hempty have tailEmpty : tail = [] := by simpa using hempty rw [tailEmpty] at tailRender simp at tailRender rw [unsigned_render] at tailRender simp at tailRender · unfold SourceValidByteSlice have hslen := hsource.1 simp at hslen ⊢ omega · exact tailRender · simp [DecimalGoalVocabularyDefault, hsign, firstEq] rcases lexical with ⟨bodyNonempty, bodyValid, bodyRender, signEq⟩ have noInf : ¬ DecimalGoalVocabularyDefault.specialLiteral (DecimalGoalVocabularyDefault.bytes body1) none false := by rw [bodyRender] exact numeric_not_special unsigned false unsigned_sign unsigned_valid have noNan : ¬ DecimalGoalVocabularyDefault.specialLiteral (DecimalGoalVocabularyDefault.bytes body1) none true := by rw [bodyRender] exact numeric_not_special unsigned true unsigned_sign unsigned_valid have specialReject := SpecialHelperRejection body1 (decide (first = 45#u8)) bodyValid noInf noNan obtain ⟨empty, hempty, htrace⟩ := bind_ok_inv _ _ _ htrace by_cases emptyTrue : empty = true · simp only [emptyTrue, if_true] at htrace obtain ⟨pfe, hpfe, htrace⟩ := bind_ok_inv _ _ _ htrace cases htrace · have emptyFalse : empty = false := Bool.eq_false_of_not_eq_true emptyTrue simp only [emptyFalse, if_false] at htrace obtain ⟨parsed, hparsed, htrace⟩ := bind_ok_inv _ _ _ htrace cases parsed with | none => obtain ⟨special, hspecial, htrace⟩ := bind_ok_inv _ _ _ htrace rw [specialReject] at hspecial cases hspecial obtain ⟨pfe, hpfe, htrace⟩ := bind_ok_inv _ _ _ htrace cases htrace | some r => obtain ⟨fast, hfast, htrace⟩ := bind_ok_inv _ _ _ htrace cases fast with | some value => simp only at htrace injection htrace with hpair injection hpair with hresult htr injection hresult with hvalue cases hvalue left refine ⟨body1, decide (first = 45#u8), r, { r with negative := decide (first = 45#u8) }, some f, ?_, bodyNonempty, bodyValid, bodyRender, signEq, hparsed⟩ exact htr.symm | 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⟩ have many_eq : { r with many_digits := many1 } = r := by cases r with | mk exponent mantissa rnegative rmany => cases hm : rmany · simp [hm] at hphase1 rcases hphase1 with ⟨hmany, hrest⟩ simp [hm, hmany] · simp [hm] at hphase1 rcases hphase1 with ⟨hmany, hrest⟩ simp [hm, hmany] 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 negative : first = 45#u8 · simp only [negative, if_true] at htrace obtain ⟨result, hresult, htrace⟩ := bind_ok_inv _ _ _ htrace cases htrace right let assemblyTrace := num.imp.dec2flt.Dec2FltAssemblyTrace.mk body1 true (some { r with many_digits := many1 }) { r with negative := true, many_digits := many2 } none r.exponent r.mantissa firstPair1 guard secondExp secondMant secondResult secondEqual forced post slowGuard slowResult selected word assembled true f refine ⟨assemblyTrace, r, ?_, bodyNonempty, bodyValid, bodyRender, ?_, ?_, ?_⟩ · rfl · dsimp [assemblyTrace] simpa [negative] using signEq · dsimp [assemblyTrace] rw [many_eq] · exact hparsed · simp only [negative, if_false] at htrace cases htrace right let assemblyTrace := num.imp.dec2flt.Dec2FltAssemblyTrace.mk body1 false (some { r with many_digits := many1 }) { r with negative := false, many_digits := many2 } none r.exponent r.mantissa firstPair1 guard secondExp secondMant secondResult secondEqual forced post slowGuard slowResult selected word f false f refine ⟨assemblyTrace, r, ?_, bodyNonempty, bodyValid, bodyRender, ?_, ?_, ?_⟩ · rfl · dsimp [assemblyTrace] simpa [negative] using signEq · dsimp [assemblyTrace] rw [many_eq] · exact hparsed end dec2flt_model