import Tablet.NumberHelperDigitScanExact import Tablet.ModelSliceSplitFirst import Tablet.SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P789_T1_SignificandNonwrapping] theorem P789_T1_SignificandNonwrapping (s intEnd sigEnd : Slice Std.U8) (intWord word : Std.U64) (fractionCount : Std.Isize) (base : Std.I64) (integer fraction tail : List Std.U8) (point : Bool) (hshape : s.val = integer ++ (if point then 46#u8 :: (fraction ++ tail) else tail)) (hend : intEnd.val = if point then 46#u8 :: (fraction ++ tail) else tail) (hnoFraction : point = false -> fraction = []) (hi : forall b, b ∈ integer -> 48 <= b.val /\ b.val <= 57) (hf : forall b, b ∈ fraction -> 48 <= b.val /\ b.val <= 57) (ht : 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 <= 9223372036854775807) (hbound : (integer ++ fraction).foldl (fun a b => 10 * a + (b.val - 48)) 0 <= U64.max) (hscan : num.imp.dec2flt.parse.try_parse_digits s 0#u64 = ok (intEnd, intWord)) (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)) : word.val = (integer ++ fraction).foldl (fun a b => 10 * a + (b.val - 48)) 0 := by -- BODY have fold_ge (bs : List Std.U8) (a : Nat) : a <= bs.foldl (fun a b => 10 * a + (b.val - 48)) a := by induction bs generalizing a with | nil => rfl | cons b bs ih => simp only [List.foldl_cons] exact le_trans (by omega) (ih _) have hIntBound : integer.foldl (fun a b => 10 * a + (b.val - 48)) 0 <= U64.max := by have hg := fold_ge fraction (integer.foldl (fun a b => 10 * a + (b.val - 48)) 0) rw [← List.foldl_append] at hg exact le_trans hg hbound 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 => exact ht | true => intro b rest he; cases he; norm_num obtain ⟨value, rest, hExact, _, hvalue⟩ := NumberHelperDigitScanExact s 0#u64 integer (if point then 46#u8 :: (fraction ++ tail) else tail) hshape hi hIntTail hlen (by simpa using hIntBound) have hWord : value = intWord := congrArg Prod.snd (Result.ok.inj (hExact.symm.trans hscan)) have hiValue : intWord.val = integer.foldl (fun a b => 10 * a + (b.val - 48)) 0 := by simpa [hWord] using hvalue cases hp : point with | false => have hfEmpty := hnoFraction hp let tailSlice : Slice Std.U8 := ⟨tail, by rw [← show intEnd.val = tail by simpa [hp] using hend] exact intEnd.property⟩ have hEnd : intEnd = tailSlice := (Slice.eq_iff _ _).2 (by simpa [hp] using hend) rw [hEnd] at hfront have hUnchanged : (do let o ← core.slice.Slice.split_first tailSlice match o with | none => ok (tailSlice, 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 (tailSlice, intWord, 0#isize, 0#i64)) = ok (tailSlice, intWord, 0#isize, 0#i64) := by cases tail with | nil => rfl | 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 rw [hUnchanged] at hfront have hword : intWord = word := congrArg (fun p => p.2.1) (Result.ok.inj hfront) simpa [hfEmpty, ← hword] using hiValue | true => have hfracLen : (fraction ++ tail).length <= s.val.length := by rw [hshape, hp] simp only [if_true, List.length_append, List.length_cons] omega let next : Slice Std.U8 := ⟨fraction ++ tail, le_trans hfracLen s.property⟩ have hsplit : core.slice.Slice.split_first intEnd = ok (some (46#u8, next)) := by let shaped : Slice Std.U8 := ⟨46#u8 :: (fraction ++ tail), by rw [← show intEnd.val = 46#u8 :: (fraction ++ tail) by simpa [hp] using hend] exact intEnd.property⟩ have he : intEnd = shaped := (Slice.eq_iff _ _).2 (by simpa [hp] using hend) rw [he] rfl obtain ⟨value, rest, hExact, _, hvalue⟩ := NumberHelperDigitScanExact next intWord fraction tail rfl hf ht (by dsimp [next]; omega) (by rw [hiValue, ← List.foldl_append]; exact hbound) simp [hsplit, hExact, UScalar.ofNat, UScalar.ofNatCore] at hfront cases hoff : SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from rest next with | fail err => simp [hoff] at hfront | div => simp [hoff] at hfront | ok count => cases hneg : -. count with | fail err => simp [hoff, hneg] at hfront | div => simp [hoff, hneg] at hfront | ok neg => cases hcast : lift (IScalar.cast .I64 neg) with | fail err => simp [hoff, hneg, hcast] at hfront | div => simp [hoff, hneg, hcast] at hfront | ok exponent => simp [hoff, hneg, hcast] at hfront rcases hfront with ⟨_, hword, _, _⟩ rw [hiValue, ← List.foldl_append] at hvalue simpa [hword] using hvalue end dec2flt_model