import Tablet.P367_T1_NumberHelperAdjustmentA import Tablet.P367_T1_NumberHelperExponentAddA import Tablet.ModelSliceSplitFirst import Tablet.ModelOptionBranch import Tablet.num_imp_dec2flt_parse_parse_partial_number open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P789_T1_PartialNumberBranches] theorem P789_T1_PartialNumberBranches (s intEnd sigEnd : Slice Std.U8) (intWord word : Std.U64) (intCount fractionCount total : Std.Isize) (base explicit : Std.I64) (d : num.imp.dec2flt.decimal.Decimal) (consumed : Std.Usize) (hlen : s.val.length <= 3458764513820540928) (htotalLo : 0 < total.val) (htotalHi : total.val <= s.val.length) (hbaseLo : -3458764513820540928 <= base.val) (hbaseHi : base.val <= 3458764513820540928) (hexpLo : -5764607523034234879 <= explicit.val) (hexpHi : explicit.val <= 5764607523034234879) (hscan : num.imp.dec2flt.parse.try_parse_digits s 0#u64 = ok (intEnd, intWord)) (hoffset : SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from intEnd s = ok intCount) (hfront : (do let o ← core.slice.Slice.split_first intEnd match o with | none => ok (intEnd, intWord, 0#isize, 0#i64) | some (b, next) => match b with | 46#uscalar => do let (rest, value) ← num.imp.dec2flt.parse.try_parse_digits next intWord let count ← SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from rest next let neg ← -. count let exponent ← lift (IScalar.cast .I64 neg) ok (rest, value, count, exponent) | _ => ok (intEnd, intWord, 0#isize, 0#i64)) = ok (sigEnd, word, fractionCount, base)) (hadd : intCount + fractionCount = ok total) (hcontinuation : (sigEnd.val = [] /\ explicit = 0#i64) \/ exists (marker : Std.U8) (next rest : Slice Std.U8), core.slice.Slice.split_first sigEnd = ok (some (marker, next)) /\ (marker = 101#u8 \/ marker = 69#u8) /\ num.imp.dec2flt.parse.parse_scientific next = ok (some explicit, rest) /\ rest.val = []) (hpartial : num.imp.dec2flt.parse.parse_partial_number s = ok (some (d, consumed))) : let zeroCount := (s.val.takeWhile (fun b => b == 48#u8 || b == 46#u8)).count 48#u8 (d.many_digits = false /\ d.mantissa = word /\ d.exponent.val = base.val + explicit.val /\ (total.val <= 19 \/ total.val - 19 - (zeroCount : Int) <= 0)) \/ (d.many_digits = true /\ 0 < total.val - 19 - (zeroCount : Int) /\ num.imp.dec2flt.parse.parse_many_digits s intEnd explicit = ok (d.mantissa, d.exponent)) := by -- BODY let zeroCount := (s.val.takeWhile (fun b => b == 48#u8 || b == 46#u8)).count 48#u8 let account : Prop := (d.many_digits = false /\ d.mantissa = word /\ d.exponent.val = base.val + explicit.val /\ (total.val <= 19 \/ total.val - 19 - (zeroCount : Int) <= 0)) \/ (d.many_digits = true /\ 0 < total.val - 19 - (zeroCount : Int) /\ num.imp.dec2flt.parse.parse_many_digits s intEnd explicit = ok (d.mantissa, d.exponent)) change account have hempty : core.slice.Slice.is_empty s = ok false := by have hs : s.val ≠ [] := by intro he; simp [he] at htotalHi; omega simp [core.slice.Slice.is_empty, hs] have hnonzero : total ≠ 0#isize := by intro he; simp [he] at htotalLo 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)) have hfinish : forall (exponent : Std.I64) (rest : Slice Std.U8), exponent.val = base.val + explicit.val -> finish exponent explicit rest = ok (some (d, consumed)) -> account := by intro exponent rest hexponent hp dsimp only [finish] at hp cases ho : SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from rest s with | fail err => simp [ho] at hp | div => simp [ho] at hp | ok offset => cases hc : lift (IScalar.hcast .Usize offset) with | fail err => simp [ho, hc] at hp | div => simp [ho, hc] at hp | ok len => simp only [ho, hc, bind_tc_ok] at hp by_cases hsmall : total.val <= 19 · simp [hsmall] at hp rcases hp with ⟨rfl, rfl⟩ exact Or.inl ⟨rfl, rfl, hexponent, Or.inl hsmall⟩ · obtain ⟨more, hmore, hmoreVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.sub_spec (x := total) (y := 19#isize) (by simp [IScalar.min, IScalarTy.numBits, Dec2Flt.P340_T1_selected_target_numBits]; omega) (by simp [IScalar.max, IScalarTy.numBits, Dec2Flt.P340_T1_selected_target_numBits]; omega)) norm_num at hmoreVal obtain ⟨adjusted, hadjust, hadjustVal⟩ := P367_T1_NumberHelperAdjustmentA s more hlen (by rw [hmoreVal]; omega) (by rw [hmoreVal]; omega) have hval : adjusted.val = total.val - 19 - (zeroCount : Int) := by rw [hadjustVal, hmoreVal] by_cases hpositive : 0 < adjusted.val · cases hr : num.imp.dec2flt.parse.parse_many_digits s intEnd explicit with | fail err => simp [hsmall, hmore, hadjust, hpositive, hr] at hp | div => simp [hsmall, hmore, hadjust, hpositive, hr] at hp | ok pair => rcases pair with ⟨manyWord, manyExp⟩ simp [hsmall, hmore, hadjust, hpositive, hr] at hp rcases hp with ⟨rfl, rfl⟩ exact Or.inr ⟨rfl, by omega, hr⟩ · simp [hsmall, hmore, hadjust, hpositive] at hp rcases hp with ⟨rfl, rfl⟩ exact Or.inl ⟨rfl, rfl, hexponent, Or.inr (by omega)⟩ 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 have hparser : num.imp.dec2flt.parse.parse_partial_number s = afterSignificand := by have hfront' : (do let o ← core.slice.Slice.split_first intEnd match o with | none => ok (intEnd, intWord, 0#isize, 0#i64) | some p => let (b, next) := p match b with | 46#uscalar => do let (rest, value) ← num.imp.dec2flt.parse.try_parse_digits next intWord let count ← SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from rest next let neg ← -. count let exponent ← lift (IScalar.cast .I64 neg) ok (rest, value, count, exponent) | _ => ok (intEnd, intWord, 0#isize, 0#i64)) = ok (sigEnd, word, fractionCount, base) := by convert hfront using 1 apply congrArg (fun k => Bind.bind (core.slice.Slice.split_first intEnd) k) funext o cases o with | none => rfl | some p => cases p; rfl unfold num.imp.dec2flt.parse.parse_partial_number simp only [hempty, bind_tc_ok, Bool.false_eq_true, not_false_eq_true, massert, if_true, hscan] dsimp simp only [hoffset, bind_tc_ok] simp only [Aeneas.Std.uncurry, ← bind_assoc_eq] at hfront' ⊢ erw [hfront'] simp only [bind_tc_ok, Aeneas.Std.uncurry_apply_pair] simp only [hadd, bind_tc_ok, hnonzero, if_false] simp only [afterSignificand, scientific, finish, Aeneas.Std.uncurry, bind_assoc_eq] apply congrArg (fun k => Bind.bind (core.slice.Slice.split_first sigEnd) k) funext o cases o with | none => rfl | some p => cases p; rfl rw [hparser] at hpartial rcases hcontinuation with ⟨hend, hezero⟩ | ⟨marker, next, rest, hsplit, hmarker, hscientific, _⟩ · have hsplit : core.slice.Slice.split_first sigEnd = ok none := by let empty : Slice Std.U8 := ⟨[], by simp⟩ have he : sigEnd = empty := (Slice.eq_iff _ _).2 hend rw [he] rfl have hp : finish base explicit sigEnd = ok (some (d, consumed)) := by simpa [afterSignificand, hsplit, hezero] using hpartial exact hfinish base sigEnd (by simp [hezero]) hp · obtain ⟨exponent, hexponent, hvalue⟩ := P367_T1_NumberHelperExponentAddA base explicit hbaseLo hbaseHi hexpLo hexpHi have hp : scientific next = ok (some (d, consumed)) := by rcases hmarker with rfl | rfl <;> simpa [afterSignificand, hsplit] using hpartial have hp' : finish exponent explicit rest = ok (some (d, consumed)) := by simpa [scientific, hscientific, core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, hexponent] using hp exact hfinish exponent rest hvalue hp' end dec2flt_model