import Tablet.Assumptions import Tablet.ModelSliceSplitFirst import Tablet.ModelU8IsAsciiDigit import Tablet.num_imp_dec2flt_parse_parse_scientific open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: NumberHelperScientificPrefixSafeLength] theorem NumberHelperScientificPrefixSafeLength (s : Slice Std.U8) (sign : Option Bool) (digits tail : List Std.U8) (hne : digits != []) (hdigits : forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57) (htail : forall b rest, tail = b :: rest -> Not (48 <= b.val /\ b.val <= 57)) (hs : s.val = (match sign with | none => [] | some negative => [if negative then 45#u8 else 43#u8]) ++ digits ++ tail) : exists (exponent : Std.I64) (rest : Slice Std.U8), num.imp.dec2flt.parse.parse_scientific s = ok (some exponent, rest) /\ rest.val = tail /\ -655359 <= exponent.val /\ exponent.val <= 655359 /\ (digits.length <= 5 -> -99999 <= exponent.val /\ exponent.val <= 99999) /\ (sign ≠ some true -> 0 <= exponent.val) := by -- BODY have loop_prefix : forall (t : Slice Std.U8) (a : Std.I64) (k : Std.Usize), t.val = digits ++ tail -> k.val <= digits.length -> 0 <= a.val -> a.val <= 655359 -> (digits.length <= 5 -> a.val < 10 ^ k.val) -> num.imp.dec2flt.parse.parse_scientific_loop a t k ⦃ p => p.2.val = digits.length /\ 0 <= p.1.val /\ p.1.val <= 655359 /\ (digits.length <= 5 -> p.1.val <= 99999) ⦄ := by intro t a k ht hk ha hb hpow unfold num.imp.dec2flt.parse.parse_scientific_loop apply Aeneas.Std.loop.spec_decr_nat (fun p : Std.I64 × Std.Usize => digits.length - p.2.val) (fun p => p.2.val <= digits.length /\ 0 <= p.1.val /\ p.1.val <= 655359 /\ (digits.length <= 5 -> p.1.val < 10 ^ p.2.val)) (fun p => p.2.val = digits.length /\ 0 <= p.1.val /\ p.1.val <= 655359 /\ (digits.length <= 5 -> p.1.val <= 99999)) (fun p => num.imp.dec2flt.parse.parse_scientific_loop.body t p.1 p.2) (a, k) · intro p hp rcases p with ⟨acc, count⟩ rcases hp with ⟨hcount, hxlo, hxhi, hxpow⟩ simp only [Prod.fst, Prod.snd] at hcount hxlo hxhi hxpow ⊢ simp [num.imp.dec2flt.parse.parse_scientific_loop.body, WP.spec_ok] split · rename_i hinside step by_cases hdigitpos : count.val < digits.length · have hi_mem : List.Mem i1 digits := by have hi_eq : i1 = digits[count.val]'hdigitpos := by rw [i1_post] simp [ht, hdigitpos] rw [hi_eq] exact List.getElem_mem _ have hi_digit := hdigits i1 hi_mem step have hi2lt : i2.val < 10 := by rw [i2_post, core.num.U8.wrapping_sub_val_eq] simp only [UScalar.size_UScalarTyU8, U8.size_eq] norm_num omega simp only [if_pos hi2lt] step split · step step have hd : digit.val < 10 := by calc digit.val = i2.val := by simp [digit_post, i2_post] _ < 10 := hi2lt have hc : (UScalar.hcast IScalarTy.I64 digit).val = digit.val := by have q := UScalar.hcast_inBounds_spec IScalarTy.I64 digit (by grind) simpa [WP.spec_ok] using q have hx1 : x.val = digit.val := by rw [x_post, hc] have hmax : exponent1.val + x.val <= I64.max := by rw [I64.max_eq] omega step with IScalar.add_spec step have hnextpow : digits.length <= 5 -> exponent1.val < 10 ^ (count.val + 1) := by intro hshort rw [exponent1_post, hx1, pow_succ] omega norm_num at count1_post refine ⟨by omega, by omega, by omega, ?_, by omega⟩ rw [count1_post] exact hnextpow · step refine ⟨by omega, hxlo, hxhi, ?_, by omega⟩ intro hshort have hcount4 : count.val <= 4 := by omega have hpow4 : (10 : Int) ^ count.val <= 10000 := by have hn : 10 ^ count.val <= (10 : Nat) ^ 4 := Nat.pow_le_pow_right (by norm_num) hcount4 norm_num at hn exact_mod_cast hn have : acc.val < 10000 := (hxpow hshort).trans_le hpow4 omega · have hcountEq : count.val = digits.length := by omega have htailPos : 0 < tail.length := by have hlen := congrArg List.length ht simp only [List.length_append] at hlen omega obtain ⟨b, bs, htailEq⟩ := List.exists_cons_of_length_pos htailPos have hiEq : i1 = b := by rw [i1_post] simp [ht, htailEq, hcountEq] have hbnot := htail b bs htailEq step have hi2not : Not (i2.val < 10) := by rw [i2_post, hiEq, core.num.U8.wrapping_sub_val_eq] simp only [UScalar.size_UScalarTyU8, U8.size_eq] norm_num scalar_tac simp [hi2not, WP.spec_ok, hcountEq] refine ⟨hxlo, hxhi, ?_⟩ intro hshort have hpow5 : (10 : Int) ^ count.val <= 100000 := by have hn : 10 ^ count.val <= (10 : Nat) ^ 5 := Nat.pow_le_pow_right (by norm_num) (by omega) norm_num at hn exact_mod_cast hn have : acc.val < 100000 := (hxpow hshort).trans_le hpow5 omega · rename_i houtside have hlen := congrArg List.length ht simp only [List.length_append] at hlen have hcountEq : count.val = digits.length := by omega simp [WP.spec_ok, hcountEq] refine ⟨hxlo, hxhi, ?_⟩ intro hshort have hpow5 : (10 : Int) ^ count.val <= 100000 := by have hn : 10 ^ count.val <= (10 : Nat) ^ 5 := Nat.pow_le_pow_right (by norm_num) (by omega) norm_num at hn exact_mod_cast hn have : acc.val < 100000 := (hxpow hshort).trans_le hpow5 omega · exact ⟨hk, ha, hb, hpow⟩ cases hd : digits with | nil => simp_all | cons c cs => have hc := hdigits c (by rw [hd]; exact List.mem_cons_self) have htailProp : (c :: cs ++ tail).length <= Usize.max := by have hp := s.property rw [hs, hd] at hp cases sign with | none => simpa using hp | some neg => cases neg <;> simp at hp ⊢ <;> omega let t : Slice Std.U8 := ⟨c :: cs ++ tail, htailProp⟩ have htUnique : forall h : (c :: cs ++ tail).length <= Usize.max, (⟨c :: cs ++ tail, h⟩ : Slice Std.U8) = t := by intro h exact (Slice.eq_iff _ _).2 (by simp [t]) have htval : t.val = digits ++ tail := by simp [t, hd] have lp := loop_prefix t 0#i64 0#usize htval (by simp) (by norm_num) (by norm_num) (by norm_num) have hc45 : c ≠ 45#u8 := by scalar_tac have hc43 : c ≠ 43#u8 := by scalar_tac cases sign with | none => have hseq : s = t := (Slice.eq_iff _ _).2 (by simpa [t, hd] using hs) subst s simp [num.imp.dec2flt.parse.parse_scientific, core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst, t, hc45, hc43, core.num.U8.is_ascii_digit, dec2flt_model.ModelU8IsAsciiDigit, Slice.index_usize] simp only [if_pos hc] obtain ⟨p, hloop, hp⟩ := Aeneas.Std.WP.spec_imp_exists lp rcases p with ⟨e, count⟩ simp only [Prod.fst, Prod.snd] at hp have hloopAny : forall u : Slice Std.U8, u.val = c :: (cs ++ tail) -> num.imp.dec2flt.parse.parse_scientific_loop 0#i64 u 0#usize = ok (e, count) := by intro u hu have hut : u = t := (Slice.eq_iff _ _).2 (by simpa [t] using hu) simpa [hut] using hloop rw [hloopAny _ rfl] have hshort : cs.length < 5 -> e.val <= 99999 := by intro hcs exact hp.2.2.2 (by rw [hd]; simp; omega) simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index, Slice.drop, t, hp, hd, List.drop_append_of_le_length] omega | some negative => cases negative with | false => have hsval : s.val = 43#u8 :: t.val := by simpa [t, hd] using hs have hseq : s = (⟨43#u8 :: t.val, by simpa [hsval] using s.property⟩ : Slice Std.U8) := (Slice.eq_iff _ _).2 hsval rw [hseq] simp [num.imp.dec2flt.parse.parse_scientific, core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst, core.num.U8.is_ascii_digit, dec2flt_model.ModelU8IsAsciiDigit, Slice.index_usize, t] simp only [if_pos hc] obtain ⟨p, hloop, hp⟩ := Aeneas.Std.WP.spec_imp_exists lp rcases p with ⟨e, count⟩ simp only [Prod.fst, Prod.snd] at hp have hloopAny : forall u : Slice Std.U8, u.val = c :: (cs ++ tail) -> num.imp.dec2flt.parse.parse_scientific_loop 0#i64 u 0#usize = ok (e, count) := by intro u hu have hut : u = t := (Slice.eq_iff _ _).2 (by simpa [t] using hu) simpa [hut] using hloop rw [hloopAny _ rfl] have hshort : cs.length < 5 -> e.val <= 99999 := by intro hcs exact hp.2.2.2 (by rw [hd]; simp; omega) simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index, Slice.drop, t, hp, hd, List.drop_append_of_le_length] omega | true => have hsval : s.val = 45#u8 :: t.val := by simpa [t, hd] using hs have hseq : s = (⟨45#u8 :: t.val, by simpa [hsval] using s.property⟩ : Slice Std.U8) := (Slice.eq_iff _ _).2 hsval rw [hseq] simp [num.imp.dec2flt.parse.parse_scientific, core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst, core.num.U8.is_ascii_digit, dec2flt_model.ModelU8IsAsciiDigit, Slice.index_usize, t] simp only [if_pos hc] obtain ⟨p, hloop, hp⟩ := Aeneas.Std.WP.spec_imp_exists lp rcases p with ⟨e, count⟩ simp only [Prod.fst, Prod.snd] at hp have hnotmin : e.val ≠ IScalar.min IScalarTy.I64 := by rw [IScalar.min] norm_num [IScalarTy.numBits] omega obtain ⟨ne, hneg, hneVal⟩ := Aeneas.Std.WP.spec_imp_exists (HNeg.hNeg.step e hnotmin) have hloopAny : forall u : Slice Std.U8, u.val = c :: (cs ++ tail) -> num.imp.dec2flt.parse.parse_scientific_loop 0#i64 u 0#usize = ok (e, count) := by intro u hu have hut : u = t := (Slice.eq_iff _ _).2 (by simpa [t] using hu) simpa [hut] using hloop rw [hloopAny _ rfl] have hshort : cs.length < 5 -> e.val <= 99999 := by intro hcs exact hp.2.2.2 (by rw [hd]; simp; omega) simp [hneg, core.slice.index.SliceIndexRangeFromUsizeSlice.index, Slice.drop, t, hp, hd, List.drop_append_of_le_length] omega end dec2flt_model