import Tablet.P435_T1_RenderedDigitBlocks import Tablet.P435_T1_DigitPrefixUnique import Tablet.P364_T1_NumberHelperDigitPrefix import Tablet.P835_T1_PartialContinuationA import Tablet.P835_T1_FinishOnNormalA import Tablet.NumberHelperScientificPrefixSafeLength import Tablet.ModelOptionBranch open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P835_T1_PartialRunReverseA] theorem P835_T1_PartialRunReverseA (s : Slice Std.U8) (hne : s.val ≠ []) (hlen : s.val.length <= 3458764513820540928) (result : Option (num.imp.dec2flt.decimal.Decimal × Std.Usize)) (hnormal : num.imp.dec2flt.parse.parse_partial_number s = ok result) (x : DecimalLexeme) (r : List Std.U8) (hsign : x.leadingSign = none) (hvalid : DecimalGoalVocabularyDefault.validNumeric x) (hrender : DecimalGoalVocabularyDefault.bytes s = DecimalGoalVocabularyDefault.renderNumeric x ++ r.map (fun b => b.val)) (hrt : forall b bs, r = b :: bs -> ¬ (48 <= b.val /\ b.val <= 57)) (hguards : x.exponent = none -> forall b bs, r = b :: bs -> b ≠ 69#u8 /\ b ≠ 101#u8 /\ (x.fractionDigits = none -> b ≠ 46#u8)) : exists d consumed, result = some (d, consumed) /\ consumed.val = (DecimalGoalVocabularyDefault.renderNumeric x).length := by -- BODY obtain ⟨integer, fraction, digits, hshape, him, hfm, hem, hd, hf, he⟩ := P435_T1_RenderedDigitBlocks s.val r x hsign hvalid hrender let point := x.fractionDigits.isSome let tail : List Std.U8 := (match (generalizing := false) x.exponent with | none => [] | some (upper, sign, _) => (if upper then 69#u8 else 101#u8) :: (match (generalizing := false) sign with | none => [] | some neg => [if neg then 45#u8 else 43#u8]) ++ digits) ++ r have hshape' : s.val = integer ++ (if point then 46#u8 :: (fraction ++ tail) else tail) := by rw [hshape] cases hx : x.fractionDigits <;> simp [point, tail, hx, List.append_assoc] all_goals cases x.exponent with | none => rfl | some p => rcases p with ⟨u, sg, ds⟩ cases sg <;> rfl have hnoFraction : point = false -> fraction = [] := by intro hp cases hx : x.fractionDigits with | none => simpa [hx] using hfm | some ds => simp [point, hx] at hp have ht : forall b bs, tail = b :: bs -> ¬ (48 <= b.val /\ b.val <= 57) := by cases hx : x.exponent with | none => simpa [tail, hx] using hrt | some p => rcases p with ⟨upper, sign, ds⟩ intro b bs hb have hhead : (if upper then 69#u8 else 101#u8) = b := by simpa [tail, hx] using congrArg List.head? hb rw [← hhead] cases upper <;> norm_num have hnoPoint : point = false -> forall b bs, tail = b :: bs -> b ≠ 46#u8 := by intro hp b bs hb cases hx : x.exponent with | none => have hr : r = b :: bs := by simpa [tail, hx] using hb have hfrac : x.fractionDigits = none := by cases hf : x.fractionDigits <;> simp_all [point] exact (hguards hx b bs hr).2.2 hfrac | some p => rcases p with ⟨upper, sign, ds⟩ have hhead : (if upper then 69#u8 else 101#u8) = b := by simpa [tail, hx] using congrArg List.head? hb rw [← hhead] cases upper <;> decide have hpos : 0 < integer.length + fraction.length := by have hiLen := congrArg List.length him have hfLen := congrArg List.length hfm simp only [List.length_map] at hiLen hfLen have hv := hvalid change _ ∧ _ ∧ _ ∧ _ at hv rcases hv.2.2.1 with hi | hfr · have hnonzero := List.length_pos_iff.mpr hi omega · cases hx : x.fractionDigits with | none => simp [hx] at hfr | some ds => have hnonzero : 0 < ds.length := List.length_pos_iff.mpr (by simpa [hx] using hfr) simp only [hx] at hfLen omega have htailInteger : forall b bs, (if point then 46#u8 :: (fraction ++ tail) else tail) = b :: bs -> ¬ (48 <= b.val /\ b.val <= 57) := by cases point with | false => exact ht | true => intro b bs hb; cases hb; norm_num obtain ⟨actualInteger, actualTail, hactual, haDigits, haTail⟩ := P364_T1_NumberHelperDigitPrefix s.val have hunique := P435_T1_DigitPrefixUnique actualInteger actualTail integer (if point then 46#u8 :: (fraction ++ tail) else tail) (hactual.symm.trans hshape') haDigits haTail hd htailInteger rcases hunique with ⟨hiUnique, htUnique⟩ subst actualInteger subst actualTail obtain ⟨actualFraction, actualFractionTail, hfActual, hfDigits, hfTail⟩ := P364_T1_NumberHelperDigitPrefix (fraction ++ tail) have hfUnique := P435_T1_DigitPrefixUnique actualFraction actualFractionTail fraction tail hfActual.symm hfDigits hfTail hf ht rcases hfUnique with ⟨hfUnique, htUnique⟩ subst actualFraction subst actualFractionTail obtain ⟨intEnd, sigEnd, word, total, base, hsigEnd, htotal, hparser⟩ := P835_T1_PartialContinuationA s integer fraction tail point hne hlen hshape' hnoFraction hd hf ht hnoPoint let finish := fun (exponent explicit : Std.I64) (rest : Slice Std.U8) => do let offset ← SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from rest s let len ← lift (IScalar.hcast .Usize offset) if total <= 19#isize then ok (some (num.imp.dec2flt.decimal.Decimal.mk exponent word false false, len)) else let more ← total - 19#isize let adjusted ← num.imp.dec2flt.parse.adjust_many_digit_count s more if adjusted > 0#isize then let (manyWord, manyExp) ← num.imp.dec2flt.parse.parse_many_digits s intEnd explicit ok (some (num.imp.dec2flt.decimal.Decimal.mk manyExp manyWord false true, len)) else ok (some (num.imp.dec2flt.decimal.Decimal.mk exponent word false false, len)) let scientific := fun (next : Slice Std.U8) => do let (value, rest) ← num.imp.dec2flt.parse.parse_scientific next let cf ← core.option.Option.Insts.CoreOpsTry_traitTry.branch value match cf with | core.ops.control_flow.ControlFlow.Continue explicit => do let exponent ← base + explicit finish exponent explicit rest | core.ops.control_flow.ControlFlow.Break residual => core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual (num.imp.dec2flt.decimal.Decimal × Std.Usize) residual let afterSignificand : Result (Option (num.imp.dec2flt.decimal.Decimal × Std.Usize)) := do let o ← core.slice.Slice.split_first sigEnd match o with | none => finish base 0#i64 sigEnd | some (c, next) => if c = 101#u8 then scientific next else if c = 69#u8 then scientific next else finish base 0#i64 sigEnd change num.imp.dec2flt.parse.parse_partial_number s = (if total = 0#isize then ok none else afterSignificand) at hparser have htne : total ≠ 0#isize := by intro hz have hzero : total.val = 0 := by rw [hz]; simp rw [htotal] at hzero omega have hafterNormal : afterSignificand = ok result := by rw [hparser] at hnormal simpa only [if_neg htne] using hnormal have finishDone : forall (exponent explicit : Std.I64) (rest : Slice Std.U8) (pref : List Std.U8), rest.val = r -> s.val = pref ++ rest.val -> finish exponent explicit rest = ok result -> exists d consumed, result = some (d, consumed) /\ consumed.val = (DecimalGoalVocabularyDefault.renderNumeric x).length := by intro exponent explicit rest pref hr hp hn obtain ⟨d, consumed, hres, hk⟩ := P835_T1_FinishOnNormalA s intEnd rest pref word total exponent explicit result hlen hp hn refine ⟨d, consumed, hres, ?_⟩ have hlength := congrArg List.length hrender change (s.val.map (fun b => b.val)).length = _ at hlength simp only [List.length_map, List.length_append] at hlength have hlength' := congrArg List.length hp simp only [List.length_append, hr] at hlength' rw [hk] omega cases hx : x.exponent with | none => have htail : tail = r := by simp [tail, hx] have hfinish : finish base 0#i64 sigEnd = ok result := by cases hr : r with | nil => have hs : sigEnd = (⟨[], by simp⟩ : Slice Std.U8) := (Slice.eq_iff _ _).2 (by rw [hsigEnd, htail, hr]) have hsplit : core.slice.Slice.split_first sigEnd = ok none := by rw [hs]; rfl simpa [afterSignificand, hsplit] using hafterNormal | cons b bs => let next : Slice Std.U8 := ⟨bs, by have hp := sigEnd.property rw [hsigEnd, htail, hr] at hp simp only [List.length_cons] at hp omega⟩ have hs : sigEnd = (⟨b :: bs, by rw [← hr, ← htail, ← hsigEnd] exact sigEnd.property⟩ : Slice Std.U8) := (Slice.eq_iff _ _).2 (hsigEnd.trans (htail.trans hr)) have hsplit : core.slice.Slice.split_first sigEnd = ok (some (b, next)) := by rw [hs]; rfl have hg := hguards hx b bs hr simpa [afterSignificand, hsplit, hg.1, hg.2.1] using hafterNormal apply finishDone base 0#i64 sigEnd (integer ++ (if point then 46#u8 :: fraction else [])) · exact hsigEnd.trans htail · rw [hshape', hsigEnd] cases point <;> simp [List.append_assoc] · exact hfinish | some p => rcases p with ⟨upper, sign, ds⟩ let signBytes : List Std.U8 := match (generalizing := false) sign with | none => [] | some neg => [if neg then 45#u8 else 43#u8] let marker : Std.U8 := if upper then 69#u8 else 101#u8 have htail : tail = marker :: (signBytes ++ digits ++ r) := by simp [tail, hx, marker, signBytes, List.append_assoc] let next : Slice Std.U8 := ⟨signBytes ++ digits ++ r, by have hp := sigEnd.property rw [hsigEnd, htail] at hp simp only [List.length_cons] at hp omega⟩ have hs : sigEnd = (⟨marker :: next.val, by change (marker :: (signBytes ++ digits ++ r)).length <= Usize.max rw [← htail, ← hsigEnd] exact sigEnd.property⟩ : Slice Std.U8) := (Slice.eq_iff _ _).2 (hsigEnd.trans htail) have hsplit : core.slice.Slice.split_first sigEnd = ok (some (marker, next)) := by rw [hs]; rfl have hscient : scientific next = ok result := by cases upper <;> simpa [afterSignificand, hsplit, marker] using hafterNormal have hdigitsNe : digits ≠ [] := by have hv := hvalid change _ ∧ _ ∧ _ ∧ _ at hv have hvexp : ds ≠ [] ∧ (forall d, d ∈ ds -> d < 10) := by simpa only [hx] using hv.2.2.2 have hds := hvexp.1 have hm : digits.map (fun b => b.val) = ds.map (fun d => 48 + d) := by simpa [hx] using hem intro hz simp [hz] at hm exact hds hm obtain ⟨explicit, rest, hparse, hrest, hlo, hhi, hshort, hpositive⟩ := NumberHelperScientificPrefixSafeLength next sign digits r (by simpa only [bne_iff_ne] using hdigitsNe) he hrt rfl cases haddExp : base + explicit with | fail err => simp [scientific, hparse, core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, haddExp] at hscient | div => simp [scientific, hparse, core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, haddExp] at hscient | ok exponent => have hfinish : finish exponent explicit rest = ok result := by simpa [scientific, hparse, core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, haddExp] using hscient apply finishDone exponent explicit rest (integer ++ (if point then 46#u8 :: fraction else []) ++ [marker] ++ signBytes ++ digits) hrest · rw [hshape', htail, hrest] cases point <;> simp [List.append_assoc] · exact hfinish end dec2flt_model