import Tablet.NumberHelperDigitScanSourceLength import Tablet.Assumptions import Tablet.P364_T1_NumberHelperOffsetSourceLength import Tablet.ModelSliceSplitFirst open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P435_T1_SignificandScanA] theorem P435_T1_SignificandScanA (s : Slice Std.U8) (integer fraction tail : List Std.U8) (point : Bool) (hshape : s.val = integer ++ (if point then 46#u8 :: (fraction ++ tail) else tail)) (hnoFraction : point = false -> fraction = []) (hinteger : forall b, b ∈ integer -> 48 <= b.val /\ b.val <= 57) (hfraction : forall b, b ∈ fraction -> 48 <= b.val /\ b.val <= 57) (htail : forall b rest, tail = b :: rest -> ¬ (48 <= b.val /\ b.val <= 57)) (hnoPoint : point = false -> forall b rest, tail = b :: rest -> b ≠ 46#u8) (hlen : s.val.length <= 3458764513820540928) : exists (intEnd sigEnd : Slice Std.U8) (intWord word : Std.U64) (intCount fractionCount total : Std.Isize) (base : Std.I64), num.imp.dec2flt.parse.try_parse_digits s 0#u64 = ok (intEnd, intWord) /\ intEnd.val = (if point then 46#u8 :: (fraction ++ tail) else tail) /\ SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from intEnd s = ok intCount /\ intCount.val = (integer.length : Int) /\ (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) /\ sigEnd.val = tail /\ fractionCount.val = (fraction.length : Int) /\ base.val = -(fraction.length : Int) /\ intCount + fractionCount = ok total /\ total.val = ((integer.length + fraction.length : Nat) : Int) := by -- BODY have hintTail : forall b rest, (if point then 46#u8 :: (fraction ++ tail) else tail) = b :: rest -> ¬ (48 <= b.val /\ b.val <= 57) := by cases point with | false => simpa using htail | true => intro b rest h; cases h; norm_num obtain ⟨intWord, intEnd, hscan, hend⟩ := NumberHelperDigitScanSourceLength s 0#u64 integer (if point then 46#u8 :: (fraction ++ tail) else tail) hshape hinteger hintTail (by omega) have hendLen : intEnd.val.length <= s.val.length := by rw [hend, hshape] simp only [List.length_append] omega obtain ⟨intCount, hoffset, hival⟩ := P364_T1_NumberHelperOffsetSourceLength s intEnd (by omega) hendLen have hi : intCount.val = (integer.length : Int) := by rw [hshape, hend] at hival simp only [List.length_append, Nat.cast_add] at hival omega cases hp : point with | false => have hf : fraction = [] := hnoFraction hp have he : intEnd.val = tail := by simpa [hp] using hend let tailSlice : Slice Std.U8 := ⟨tail, by rw [← he]; exact intEnd.property⟩ have hEnd : intEnd = tailSlice := (Slice.eq_iff _ _).2 he have 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 (intEnd, intWord, 0#isize, 0#i64) := by rw [hEnd] cases tail with | nil => simp [tailSlice, core.slice.Slice.split_first, ModelSliceSplitFirst] | cons b bs => have hb := hnoPoint hp b bs rfl simp [tailSlice, core.slice.Slice.split_first, ModelSliceSplitFirst] simp only [UScalar.ofNat, UScalar.ofNatCore] at hb ⊢ split · exact False.elim (hb rfl) · rfl obtain ⟨total, hadd, htotal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.add_spec (x := intCount) (y := 0#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] rw [hi]; rw [hshape, hp] at hlen; simp at hlen; omega)) exact ⟨intEnd, intEnd, intWord, intWord, intCount, 0#isize, total, 0#i64, hscan, by simpa [hp] using hend, hoffset, hi, hfront, he, by simp [hf], by simp [hf], hadd, by simpa [hf, hi] using htotal⟩ | true => have he : intEnd.val = 46#u8 :: (fraction ++ tail) := by simpa [hp] using hend have hfLen : (fraction ++ tail).length <= s.val.length := by rw [hshape, hp] simp only [if_true, List.length_append, List.length_cons] omega let fractional : Slice Std.U8 := ⟨fraction ++ tail, le_trans hfLen s.property⟩ have hsplit : core.slice.Slice.split_first intEnd = ok (some (46#u8, fractional)) := by let shaped : Slice Std.U8 := ⟨46#u8 :: (fraction ++ tail), by rw [← he]; exact intEnd.property⟩ have hEnd : intEnd = shaped := (Slice.eq_iff _ _).2 he rw [hEnd] rfl obtain ⟨word, sigEnd, hfracScan, hsigEnd⟩ := NumberHelperDigitScanSourceLength fractional intWord fraction tail rfl hfraction htail (by dsimp [fractional]; omega) have hsigLen : sigEnd.val.length <= fractional.val.length := by simp [hsigEnd, fractional] obtain ⟨fractionCount, hfracOffset, hfval⟩ := P364_T1_NumberHelperOffsetSourceLength fractional sigEnd (by dsimp [fractional]; omega) hsigLen have hf : fractionCount.val = (fraction.length : Int) := by simp only [fractional, hsigEnd, List.length_append, Nat.cast_add] at hfval omega have hnotmin : fractionCount.val ≠ IScalar.min .Isize := by simp [IScalar.min, IScalarTy.numBits, Dec2Flt.P340_T1_selected_target_numBits] omega obtain ⟨neg, hneg, hnegval⟩ := Aeneas.Std.WP.spec_imp_exists (HNeg.hNeg.step fractionCount hnotmin) have hcastBound : IScalar.min .I64 <= neg.val /\ neg.val <= IScalar.max .I64 := by simp only [IScalar.min_IScalarTy_I64_eq, I64.min_eq, IScalar.max_IScalarTy_I64_eq, I64.max_eq] rw [hnegval, hf] rw [hshape, hp] at hlen simp only [if_true, List.length_append, List.length_cons] at hlen omega obtain ⟨base, hcast, hbase⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.cast_inBounds_spec .I64 neg hcastBound) have htotalBound : IScalar.min .Isize <= intCount.val + fractionCount.val /\ intCount.val + fractionCount.val <= IScalar.max .Isize := by simp [IScalar.min, IScalar.max, IScalarTy.numBits, Dec2Flt.P340_T1_selected_target_numBits] rw [hi, hf] rw [hshape, hp] at hlen simp only [if_true, List.length_append, List.length_cons] at hlen omega obtain ⟨total, hadd, htotal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.add_spec (x := intCount) (y := fractionCount) htotalBound.1 htotalBound.2) refine ⟨intEnd, sigEnd, intWord, word, intCount, fractionCount, total, base, hscan, by simpa [hp] using hend, hoffset, hi, ?_, hsigEnd, hf, ?_, hadd, ?_⟩ · simp [hsplit, hfracScan, hfracOffset, hneg, hcast, UScalar.ofNat, UScalar.ofNatCore] · rw [hbase, hnegval, hf] · rw [htotal, hi, hf, Nat.cast_add] end dec2flt_model