import Tablet.Assumptions import Tablet.P364_T1_NumberHelperOffsetSourceLength 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: P835_T1_FinishOnNormalA] theorem P835_T1_FinishOnNormalA (s intEnd rest : Slice Std.U8) (pref : List Std.U8) (word : Std.U64) (total : Std.Isize) (exponent explicit : Std.I64) (result : Option (num.imp.dec2flt.decimal.Decimal × Std.Usize)) (hlen : s.val.length <= 3458764513820540928) (hsuffix : s.val = pref ++ rest.val) (hnormal : ( 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))) = ok result) : exists d consumed, result = some (d, consumed) /\ consumed.val = pref.length := by -- BODY have hrestLen : rest.val.length <= s.val.length := by rw [hsuffix] simp only [List.length_append] omega obtain ⟨offset, hoff, hoffval⟩ := P364_T1_NumberHelperOffsetSourceLength s rest (by omega) hrestLen have hbits := Dec2Flt.P340_T1_selected_target_numBits have hbnd : 0 <= offset.val /\ offset.val <= UScalar.max .Usize := by simp [UScalar.max, UScalarTy.numBits, hbits] rw [hoffval] omega obtain ⟨len, hcast, hval⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.hcast_inBounds_spec .Usize offset hbnd) have hcast' : lift (IScalar.hcast .Usize offset) = ok len := by simpa [WP.spec_ok] using hcast have hlenPrefix : len.val = pref.length := by have hslen := congrArg List.length hsuffix simp only [List.length_append] at hslen have heq : (len.val : Int) = (pref.length : Int) := by rw [hval, hoffval] omega exact_mod_cast heq rw [hoff] at hnormal simp only [bind_tc_ok, hcast'] at hnormal have final : forall d, ok (some (d, len)) = ok result -> exists d consumed, result = some (d, consumed) /\ consumed.val = pref.length := by intro d hd have heq : some (d, len) = result := Result.ok.inj hd exact ⟨d, len, heq.symm, hlenPrefix⟩ by_cases hsmall : total <= 19#isize · simp only [if_pos hsmall] at hnormal exact final _ hnormal · simp only [if_neg hsmall] at hnormal cases hm : total - 19#isize with | fail err => simp [hm] at hnormal | div => simp [hm] at hnormal | ok more => simp only [hm, bind_tc_ok] at hnormal cases ha : num.imp.dec2flt.parse.adjust_many_digit_count s more with | fail err => simp [ha] at hnormal | div => simp [ha] at hnormal | ok adjusted => simp only [ha, bind_tc_ok] at hnormal by_cases hp : adjusted > 0#isize · simp only [if_pos hp] at hnormal cases hm : num.imp.dec2flt.parse.parse_many_digits s intEnd explicit with | fail err => simp [hm] at hnormal | div => simp [hm] at hnormal | ok pair => rcases pair with ⟨manyWord, manyExp⟩ simp only [hm, bind_tc_ok] at hnormal exact final _ hnormal · simp only [if_neg hp] at hnormal exact final _ hnormal end dec2flt_model