import Tablet.Assumptions import Tablet.ModelOptionBranch import Tablet.ModelOptionFromResidual import Tablet.ModelSliceSplitFirst import Tablet.NumberHelperAdjustManyExact import Tablet.NumberHelperDigitScanExact import Tablet.NumberHelperDigitScanSourceLength import Tablet.NumberHelperOffsetSourceLength import Tablet.NumberHelperTryParse19SafeLength import Tablet.SourceValidByteSlice import Tablet.U64_Insts_CoreOpsBitBitAndU64U64 import Tablet.U64_Insts_CoreOpsBitBitOrU64U64 import Tablet.num_imp_dec2flt_decimal_Decimal import Tablet.num_imp_dec2flt_parse_parse_many_digits import Tablet.num_imp_dec2flt_parse_parse_number import Tablet.num_imp_dec2flt_parse_parse_partial_number import Tablet.num_imp_dec2flt_parse_parse_scientific open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: NumberHelperRecordBounds] theorem NumberHelperRecordBounds : ∀ (s : Slice Std.U8) d, SourceValidByteSlice s -> s.val ≠ [] -> num.imp.dec2flt.parse.parse_number s = ok (some d) -> d.negative = false ∧ d.mantissa.val < 10 ^ 19 := by -- BODY intro s d hsource hne hparse have hlen : s.val.length <= 9223372036854775807 := by simpa [SourceValidByteSlice] using hsource have bind_ok_inv : forall {alpha beta : Type} (r : Result alpha) (f : alpha -> Result beta) y, Bind.bind r f = ok y -> exists x, r = ok x /\ f x = ok y := by intro alpha beta r f y h cases r with | fail err => simp [Bind.bind, Aeneas.Std.bind] at h | div => simp [Bind.bind, Aeneas.Std.bind] at h | ok x => exact ⟨x, rfl, h⟩ have split_digits : forall xs : List Std.U8, exists digits tail, xs = digits ++ tail /\ (forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57) /\ (forall b rest, tail = b :: rest -> Not (48 <= b.val /\ b.val <= 57)) := by intro xs induction xs with | nil => exact ⟨[], [], rfl, (by intro b hb; cases hb), (by intro b rest h cases h)⟩ | cons b bs ih => by_cases hb : 48 <= b.val /\ b.val <= 57 · obtain ⟨digits, tail, hsplit, hdigits, htail⟩ := ih refine ⟨b :: digits, tail, by simp [hsplit], ?_, htail⟩ intro c hc rcases List.mem_cons.mp hc with rfl | hc · exact hb · exact hdigits c hc · exact ⟨[], b :: bs, rfl, (by intro c hc; cases hc), (by intro c rest h cases h exact hb)⟩ have parse_many_bound : forall (start intEnd : Slice Std.U8) (exp outExp : Std.I64) (mantissa : Std.U64), num.imp.dec2flt.parse.parse_many_digits start intEnd exp = ok (mantissa, outExp) -> mantissa.val <= 9999999999999999999 := by intro start intEnd exp outExp mantissa hmany obtain ⟨digits, tail, hsplit, hdigits, htail⟩ := split_digits start.val obtain ⟨rest, first, remaining, consumed, hfirst, hpartition, hrest, hfirstValue, hremainingDigits, hremainingLen, hfirstBound, hfirstLow, hfirstSharp⟩ := NumberHelperTryParse19SafeLength start 0#u64 digits tail hsplit hdigits htail (by norm_num) unfold num.imp.dec2flt.parse.parse_many_digits at hmany rw [hfirst] at hmany by_cases hthreshold : first.val < 1000000000000000000 · have hnotHigh : ¬ 1000000000000000000 <= first.val := by omega simp [num.imp.dec2flt.parse.MIN_19DIGIT_INT.eq_def, hnotHigh] at hmany cases hdrop : core.slice.index.SliceIndexRangeFromUsizeSlice.index { start := 1#usize } rest with | fail err => rw [hdrop] at hmany simp at hmany | div => rw [hdrop] at hmany simp at hmany | ok afterDrop => rw [hdrop] at hmany simp at hmany obtain ⟨digits2, tail2, hsplit2, hdigits2, htail2⟩ := split_digits afterDrop.val obtain ⟨rest2, second, remaining2, consumed2, hsecond, hpartition2, hrest2, hsecondValue, hremainingDigits2, hremainingLen2, hsecondBound, hsecondLow, hsecondSharp⟩ := NumberHelperTryParse19SafeLength afterDrop first digits2 tail2 hsplit2 hdigits2 htail2 hfirstBound rw [hsecond] at hmany simp at hmany cases hoff : SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from rest2 afterDrop with | fail err => simp [hoff] at hmany | div => simp [hoff] at hmany | ok off => simp [hoff] at hmany cases hneg : HNeg.hNeg off with | fail err => simp [hneg] at hmany | div => simp [hneg] at hmany | ok negOff => simp [hneg] at hmany cases hcast : lift (IScalar.cast .I64 negOff) with | fail err => simp [hcast] at hmany | div => simp [hcast] at hmany | ok castOff => simp [hcast] at hmany cases hadd : castOff + exp with | fail err => simp [hadd] at hmany | div => simp [hadd] at hmany | ok finalExp => simp [hadd] at hmany rcases hmany with ⟨rfl, rfl⟩ exact hsecondBound · have hhigh : 1000000000000000000 <= first.val := by omega simp [num.imp.dec2flt.parse.MIN_19DIGIT_INT.eq_def, hhigh] at hmany cases hoff : SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from intEnd rest with | fail err => simp [hoff] at hmany | div => simp [hoff] at hmany | ok off => simp [hoff] at hmany cases hcast : lift (IScalar.cast .I64 off) with | fail err => simp [hcast] at hmany | div => simp [hcast] at hmany | ok castOff => simp [hcast] at hmany cases hadd : castOff + exp with | fail err => simp [hadd] at hmany | div => simp [hadd] at hmany | ok finalExp => simp [hadd] at hmany rcases hmany with ⟨rfl, rfl⟩ exact hfirstBound have record_branches : forall (total : Std.Isize) (shortMantissa : Std.U64) (intEnd : Slice Std.U8) (shortExponent retryExponent : Std.I64) (len outLen : Std.Usize), (total.val <= 19 -> shortMantissa.val <= 9999999999999999999) -> (forall (nMore adjusted : Std.Isize), total - 19#isize = ok nMore -> num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted -> Not (total.val <= 19) -> Not (0 < adjusted.val) -> shortMantissa.val <= 9999999999999999999) -> (if total.val <= 19 then ok (some (num.imp.dec2flt.decimal.Decimal.mk shortExponent shortMantissa false false, len)) else do let nMore <- total - 19#isize let adjusted <- num.imp.dec2flt.parse.adjust_many_digit_count s nMore if 0 < adjusted.val then let (manyMantissa, manyExponent) <- num.imp.dec2flt.parse.parse_many_digits s intEnd retryExponent ok (some (num.imp.dec2flt.decimal.Decimal.mk manyExponent manyMantissa false true, len)) else ok (some (num.imp.dec2flt.decimal.Decimal.mk shortExponent shortMantissa false false, len))) = ok (some (d, outLen)) -> d.negative = false /\ d.mantissa.val < 10 ^ 19 := by intro total shortMantissa intEnd shortExponent retryExponent len outLen hshort hnonpositive hresult by_cases hsmall : total.val <= 19 · simp [hsmall] at hresult rcases hresult with ⟨rfl, rfl⟩ refine ⟨rfl, ?_⟩ have hb := hshort hsmall norm_num omega · simp [hsmall] at hresult obtain ⟨nMore, hsub, hresult⟩ := bind_ok_inv _ _ _ hresult obtain ⟨adjusted, hadjust, hresult⟩ := bind_ok_inv _ _ _ hresult by_cases hpositive : 0 < adjusted.val · simp only [if_pos hpositive] at hresult obtain ⟨manyResult, hmany, hresult⟩ := bind_ok_inv _ _ _ hresult rcases manyResult with ⟨manyMantissa, manyExponent⟩ rcases hresult with ⟨rfl, rfl⟩ refine ⟨rfl, ?_⟩ have hb := parse_many_bound s intEnd retryExponent manyExponent manyMantissa hmany norm_num omega · simp only [if_neg hpositive] at hresult rcases hresult with ⟨rfl, rfl⟩ refine ⟨rfl, ?_⟩ have hb := hnonpositive nMore adjusted hsub hadjust hsmall hpositive norm_num omega have fold_upper : forall (digits : List Std.U8) (a : Nat), (forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57) -> digits.foldl (fun x b => 10 * x + (b.val - 48)) a < (a + 1) * 10 ^ digits.length := by intro digits induction digits with | nil => intro a h; simp | cons b bs ih => intro a h have hb := h b List.mem_cons_self have hbs : forall c, List.Mem c bs -> 48 <= c.val /\ c.val <= 57 := by intro c hc exact h c (List.mem_cons_of_mem b hc) simp only [List.foldl_cons, List.length_cons, Nat.pow_succ] have hrec := ih (10 * a + (b.val - 48)) hbs have hcoeff : 10 * a + (b.val - 48) + 1 <= 10 * (a + 1) := by omega have hmul := Nat.mul_le_mul_right (10 ^ bs.length) hcoeff exact hrec.trans_le (by simpa [Nat.mul_assoc, Nat.mul_comm, Nat.mul_left_comm] using hmul) have fold_ge_start : forall (digits : List Std.U8) (a : Nat), a <= digits.foldl (fun x b => 10 * x + (b.val - 48)) a := by intro digits induction digits with | nil => intro a; rfl | cons b bs ih => intro a apply le_trans (b := 10 * a + (b.val - 48)) · omega · exact ih _ have zero_fold : forall digits : List Std.U8, (forall b, List.Mem b digits -> b = 48#u8) -> digits.foldl (fun x b => 10 * x + (b.val - 48)) 0 = 0 := by intro digits hzero induction digits with | nil => rfl | cons b bs ih => have hb := hzero b List.mem_cons_self have hbs : forall c, List.Mem c bs -> c = 48#u8 := by intro c hc exact hzero c (List.mem_cons_of_mem b hc) simp [hb, ih hbs] have split_zeros : forall digits : List Std.U8, exists zeros tail, digits = zeros ++ tail /\ (forall b, List.Mem b zeros -> b = 48#u8) /\ (tail = [] \/ exists b rest, tail = b :: rest /\ b ≠ 48#u8) := by intro digits induction digits with | nil => exact ⟨[], [], rfl, (by intro b hb; cases hb), Or.inl rfl⟩ | cons b bs ih => by_cases hb : b = 48#u8 · obtain ⟨zeros, tail, hsplit, hzeros, htail⟩ := ih refine ⟨b :: zeros, tail, by simp [hsplit], ?_, htail⟩ intro c hc rcases List.mem_cons.mp hc with rfl | hc · exact hb · exact hzeros c hc · exact ⟨[], b :: bs, rfl, (by intro c hc; cases hc), Or.inr ⟨b, bs, rfl, hb⟩⟩ have count_zeros : forall zeros : List Std.U8, (forall b, List.Mem b zeros -> b = 48#u8) -> zeros.count 48#u8 = zeros.length := by intro zeros hzeros induction zeros with | nil => rfl | cons b bs ih => have hb := hzeros b List.mem_cons_self have hbs : forall c, List.Mem c bs -> c = 48#u8 := by intro c hc exact hzeros c (List.mem_cons_of_mem b hc) simp [hb, ih hbs] have split_significand_zeros : forall (integer fraction suffix : List Std.U8), (forall b, List.Mem b integer -> 48 <= b.val /\ b.val <= 57) -> (forall b, List.Mem b fraction -> 48 <= b.val /\ b.val <= 57) -> Not (forall b, List.Mem b (integer ++ fraction) -> b = 48#u8) -> exists rawZeros rawTail sigZeros sigTail, integer ++ 46#u8 :: (fraction ++ suffix) = rawZeros ++ rawTail /\ (forall b, List.Mem b rawZeros -> b = 48#u8 \/ b = 46#u8) /\ (exists b rest, rawTail = b :: rest /\ b ≠ 48#u8 /\ b ≠ 46#u8) /\ integer ++ fraction = sigZeros ++ sigTail /\ (forall b, List.Mem b sigZeros -> b = 48#u8) /\ (exists b rest, sigTail = b :: rest /\ b ≠ 48#u8) /\ rawZeros.count 48#u8 = sigZeros.length := by intro integer fraction suffix hinteger hfraction hnonzero obtain ⟨intZeros, intSig, hintSplit, hintZeros, hintSig⟩ := split_zeros integer rcases hintSig with hintNil | ⟨b, intRest, hintSigEq, hb⟩ · obtain ⟨fracZeros, fracSig, hfracSplit, hfracZeros, hfracSig⟩ := split_zeros fraction rcases hfracSig with hfracNil | ⟨c, fracRest, hfracSigEq, hc⟩ · exfalso apply hnonzero intro x hx rcases List.mem_append.mp hx with hx | hx · rw [hintSplit, hintNil] at hx exact hintZeros x (by simpa using hx) · rw [hfracSplit, hfracNil] at hx exact hfracZeros x (by simpa using hx) · refine ⟨intZeros ++ 46#u8 :: fracZeros, fracSig ++ suffix, intZeros ++ fracZeros, fracSig, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ · rw [hintSplit, hintNil, hfracSplit] simp [List.append_assoc] · intro x hx rcases List.mem_append.mp hx with hx | hx · exact Or.inl (hintZeros x hx) · rcases List.mem_cons.mp hx with rfl | hx · exact Or.inr rfl · exact Or.inl (hfracZeros x hx) · refine ⟨c, fracRest ++ suffix, by simp [hfracSigEq], hc, ?_⟩ have hcdigit := hfraction c (by rw [hfracSplit, hfracSigEq] exact List.mem_append_right fracZeros List.mem_cons_self) intro hpoint subst c norm_num at hcdigit · rw [hintSplit, hintNil, hfracSplit] simp [List.append_assoc] · intro x hx rcases List.mem_append.mp hx with hx | hx · exact hintZeros x hx · exact hfracZeros x hx · exact ⟨c, fracRest, hfracSigEq, hc⟩ · rw [List.count_append, List.count_cons, count_zeros intZeros hintZeros, count_zeros fracZeros hfracZeros] norm_num · refine ⟨intZeros, intSig ++ 46#u8 :: (fraction ++ suffix), intZeros, intSig ++ fraction, ?_, ?_, ?_, ?_, hintZeros, ?_, ?_⟩ · rw [hintSplit] simp [List.append_assoc] · intro x hx exact Or.inl (hintZeros x hx) · refine ⟨b, intRest ++ 46#u8 :: (fraction ++ suffix), by simp [hintSigEq], hb, ?_⟩ have hbdigit := hinteger b (by rw [hintSplit, hintSigEq] exact List.mem_append_right intZeros List.mem_cons_self) intro hpoint subst b norm_num at hbdigit · rw [hintSplit] simp [List.append_assoc] · exact ⟨b, intRest ++ fraction, by simp [hintSigEq], hb⟩ · exact count_zeros intZeros hintZeros obtain ⟨intDigits, intTail, hsval, hintDigits, hintTail⟩ := split_digits s.val have hintTailProp : intTail.length <= Usize.max := by have hp := s.property rw [hsval] at hp simp only [List.length_append] at hp omega let intRest : Slice Std.U8 := ⟨intTail, hintTailProp⟩ obtain ⟨intMantissa, scannedIntRest, hscanInt, hscannedIntRest⟩ := NumberHelperDigitScanSourceLength s 0#u64 intDigits intTail hsval hintDigits hintTail hlen have hscannedIntRestEq : scannedIntRest = intRest := (Slice.eq_iff _ _).2 (by simpa [intRest] using hscannedIntRest) subst scannedIntRest obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ := NumberHelperOffsetSourceLength s intRest hlen (by rw [hsval] simp [intRest]) have hnDigitsEq : nDigits.val = intDigits.length := by rw [hnDigitsVal, hsval] simp [intRest] have hpartial : exists len, num.imp.dec2flt.parse.parse_partial_number s = ok (some (d, len)) := by unfold num.imp.dec2flt.parse.parse_number at hparse cases hp : num.imp.dec2flt.parse.parse_partial_number s with | fail err => simp [hp] at hparse | div => simp [hp] at hparse | ok result => cases result with | none => simp [hp] at hparse | some pair => rcases pair with ⟨record, len⟩ simp [hp] at hparse split at hparse · have hrecord : record = d := by simpa using hparse subst d exact ⟨len, rfl⟩ · simp at hparse obtain ⟨parsedLen, hpartial⟩ := hpartial cases intTail with | nil => have hsplitInt : core.slice.Slice.split_first intRest = ok none := by simp [core.slice.Slice.split_first, ModelSliceSplitFirst, intRest] have haddZero : nDigits + 0#isize = ok nDigits := by have hb := IScalar.hBounds nDigits obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.add_spec (x := nDigits) (y := 0#isize) (by simp [IScalar.min, IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] omega) (by simp [IScalar.max, IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] at hb ⊢ omega)) rw [hz] congr 2 apply IScalar.eq_of_val_eq simpa using hzval have hfullOffset : SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from intRest s = ok nDigits := by simpa [intRest] using hoffInt have hcastLen : lift (IScalar.hcast .Usize nDigits) = ok (Slice.len s) := by have hbound : 0 <= nDigits.val /\ nDigits.val <= UScalar.max .Usize := by rw [hnDigitsEq] constructor · omega · have hp := s.property rw [hsval] at hp simpa [UScalar.max, Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits] using hp obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.hcast_inBounds_spec .Usize nDigits hbound) rw [hz] congr 2 apply UScalar.eq_of_val_eq rw [hnDigitsEq] at hzval have hslen : s.val.length = intDigits.length := by simp [hsval] simpa [Slice.len_val, hslen] using hzval unfold num.imp.dec2flt.parse.parse_partial_number at hpartial simp [hne, hscanInt, hoffInt, hsplitInt, haddZero, hcastLen] at hpartial by_cases hnzero : nDigits = 0#isize · simp [hnzero] at hpartial · simp [hnzero] at hpartial by_cases hsmall : nDigits.val <= 19 · simp [hsmall] at hpartial rcases hpartial with ⟨rfl, rfl⟩ constructor · rfl · have hfoldLt := fold_upper intDigits 0 hintDigits norm_num at hfoldLt have hlen19 : intDigits.length <= 19 := by omega have hbound : intDigits.foldl (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by rw [U64.max_eq] have hp := hfoldLt.trans_le (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hlen19) norm_num at hp ⊢ omega obtain ⟨value, rest, hexact, hrest, hvalue⟩ := NumberHelperDigitScanExact s 0#u64 intDigits [] (by simpa using hsval) hintDigits (by intro b rest h cases h) hlen hbound rw [hscanInt] at hexact simp at hexact rcases hexact with ⟨rfl, rfl⟩ rw [hvalue] norm_num exact hfoldLt.trans_le (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hlen19) · have hlarge : 19 < nDigits.val := by omega have hsubBounds : IScalar.min .Isize <= nDigits.val - 19 /\ nDigits.val - 19 <= IScalar.max .Isize := by have hb := IScalar.hBounds nDigits simp [IScalar.min, IScalar.max, IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] at hb ⊢ omega obtain ⟨nMore, hnMore, hnMoreVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.sub_spec (x := nDigits) (y := 19#isize) (by norm_num; omega) (by norm_num; omega)) norm_num at hnMoreVal simp [hsmall, hnMore] at hpartial obtain ⟨zeros, nonzeroTail, hzeroSplit, hzeros, hzeroTail⟩ := split_zeros intDigits have hcount := count_zeros zeros hzeros have hsAdjust : s.val = zeros ++ nonzeroTail := by simpa [hzeroSplit] using hsval have htailSharp : nonzeroTail = [] \/ exists b rest, nonzeroTail = b :: rest /\ b ≠ 48#u8 /\ b ≠ 46#u8 := by rcases hzeroTail with hnil | ⟨b, rest, htailEq, hb⟩ · exact Or.inl hnil · right refine ⟨b, rest, htailEq, hb, ?_⟩ have hbdigit := hintDigits b (by rw [hzeroSplit, htailEq] exact List.mem_append_right zeros List.mem_cons_self) intro heq subst b norm_num at hbdigit have hnMoreNonneg : 0 <= nMore.val := by rw [hnMoreVal] omega have hnMoreLe : nMore.val <= s.val.length := by rw [hnMoreVal, hnDigitsEq, hsval] simp only [List.length_append, List.length_nil, Nat.add_zero] omega have hadjustLower : IScalar.min .Isize <= nMore.val - (zeros.count 48#u8 : Int) := by rw [hnMoreVal, hcount, hnDigitsEq] have hzerosLen : zeros.length <= intDigits.length := by rw [hzeroSplit] simp simp [IScalar.min, IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] omega obtain ⟨adjusted, hadjust, hadjustVal⟩ := NumberHelperAdjustManyExact s nMore zeros nonzeroTail hsAdjust (by intro b hb; exact Or.inl (hzeros b hb)) htailSharp hlen hnMoreNonneg hnMoreLe hadjustLower rw [hadjust] at hpartial by_cases hpositive : 0 < adjusted.val · simp [hpositive] at hpartial cases hmany : num.imp.dec2flt.parse.parse_many_digits s intRest 0#i64 with | fail err => simp [hmany] at hpartial | div => simp [hmany] at hpartial | ok result => rcases result with ⟨manyMantissa, manyExponent⟩ simp [hmany] at hpartial rcases hpartial with ⟨rfl, rfl⟩ refine ⟨rfl, ?_⟩ change manyMantissa.val < 10 ^ 19 have hb := parse_many_bound s intRest 0#i64 manyExponent manyMantissa hmany omega · simp [hpositive] at hpartial have htailLen : nonzeroTail.length <= 19 := by rw [hadjustVal, hnMoreVal, hcount, hnDigitsEq] at hpositive have hlenEq := congrArg List.length hzeroSplit simp only [List.length_append] at hlenEq omega have htailDigits : forall b, List.Mem b nonzeroTail -> 48 <= b.val /\ b.val <= 57 := by intro b hb apply hintDigits b rw [hzeroSplit] exact List.mem_append_right zeros hb have hzeroFold := zero_fold zeros hzeros have hfoldLt : intDigits.foldl (fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 := by rcases hzeroTail with hnil | hcons · subst nonzeroTail rw [hzeroSplit, List.append_nil, hzeroFold] norm_num · rw [hzeroSplit, List.foldl_append, hzeroFold] have htailLt := fold_upper nonzeroTail 0 htailDigits norm_num at htailLt exact htailLt.trans_le (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) htailLen) have hfoldBound : intDigits.foldl (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by rw [U64.max_eq] omega obtain ⟨value, rest, hexact, hrest, hvalue⟩ := NumberHelperDigitScanExact s 0#u64 intDigits [] (by simpa using hsval) hintDigits (by intro b rest h cases h) hlen hfoldBound rw [hscanInt] at hexact simp at hexact rcases hexact with ⟨rfl, rfl⟩ rcases hpartial with ⟨rfl, rfl⟩ refine ⟨rfl, ?_⟩ norm_num at hvalue ⊢ rw [hvalue] exact hfoldLt | cons first tail => have htailProp : tail.length <= Usize.max := by have hp := intRest.property simp [intRest] at hp ⊢ omega let afterFirst : Slice Std.U8 := ⟨tail, htailProp⟩ have hsplitInt : core.slice.Slice.split_first intRest = ok (some (first, afterFirst)) := by simp [core.slice.Slice.split_first, ModelSliceSplitFirst, intRest, afterFirst] by_cases hpoint : first = 46#u8 · subst first obtain ⟨fracDigits, fracTail, hfracSplit, hfracDigits, hfracTail⟩ := split_digits tail have hfracTailProp : fracTail.length <= Usize.max := by have hp := afterFirst.property simp [afterFirst, hfracSplit] at hp ⊢ omega let fracRest : Slice Std.U8 := ⟨fracTail, hfracTailProp⟩ have hlenAfter : afterFirst.val.length <= 9223372036854775807 := by simp [afterFirst] rw [hsval] at hlen simp only [List.length_append, List.length_cons] at hlen omega obtain ⟨fracMantissa, scannedFracRest, hscanFrac, hscannedFracRest⟩ := NumberHelperDigitScanSourceLength afterFirst intMantissa fracDigits fracTail (by simp [afterFirst, hfracSplit]) hfracDigits hfracTail hlenAfter have hscannedFracRestEq : scannedFracRest = fracRest := (Slice.eq_iff _ _).2 (by simpa [fracRest] using hscannedFracRest) subst scannedFracRest obtain ⟨nAfter, hoffFrac, hnAfterVal⟩ := NumberHelperOffsetSourceLength afterFirst fracRest hlenAfter (by simp [afterFirst, fracRest, hfracSplit]) have hnAfterEq : nAfter.val = fracDigits.length := by rw [hnAfterVal] simp [afterFirst, fracRest, hfracSplit] have hnAfterNotMin : nAfter.val ≠ IScalar.min .Isize := by rw [hnAfterEq, IScalar.min] simp [IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] obtain ⟨negAfter, hnegAfter, hnegAfterVal⟩ := Aeneas.Std.WP.spec_imp_exists (HNeg.hNeg.step nAfter hnAfterNotMin) have hnegCastBounds : IScalar.min .I64 <= negAfter.val /\ negAfter.val <= IScalar.max .I64 := by rw [hnegAfterVal, hnAfterEq, IScalar.min_IScalarTy_I64_eq, I64.min_eq, IScalar.max_IScalarTy_I64_eq, I64.max_eq] constructor <;> omega obtain ⟨fracExponent, hfracExponent, hfracExponentVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.cast_inBounds_spec .I64 negAfter hnegCastBounds) have htotalBounds : IScalar.min .Isize <= nDigits.val + nAfter.val /\ nDigits.val + nAfter.val <= IScalar.max .Isize := by rw [hnDigitsEq, hnAfterEq] simp [IScalar.min, IScalar.max, IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] rw [hsval, hfracSplit] at hlen simp only [List.length_append, List.length_cons] at hlen constructor <;> omega obtain ⟨totalDigits, htotal, htotalVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.add_spec htotalBounds.1 htotalBounds.2) have hsigDigits : forall b, List.Mem b (intDigits ++ fracDigits) -> 48 <= b.val /\ b.val <= 57 := by intro b hb rcases List.mem_append.mp hb with hb | hb · exact hintDigits b hb · exact hfracDigits b hb have hexactScans : (intDigits ++ fracDigits).foldl (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max -> fracMantissa.val = (intDigits ++ fracDigits).foldl (fun x b => 10 * x + (b.val - 48)) 0 := by intro hfullBound have hprefixLe := fold_ge_start fracDigits (intDigits.foldl (fun x b => 10 * x + (b.val - 48)) 0) rw [← List.foldl_append] at hprefixLe have hintBound : intDigits.foldl (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := hprefixLe.trans hfullBound obtain ⟨intValue, intScanRest, hexactInt, hexactIntRest, hintValue⟩ := NumberHelperDigitScanExact s 0#u64 intDigits (46#u8 :: tail) (by simpa using hsval) hintDigits hintTail hlen hintBound rw [hscanInt] at hexactInt simp at hexactInt rcases hexactInt with ⟨rfl, rfl⟩ have hfracBound : fracDigits.foldl (fun x b => 10 * x + (b.val - 48)) intMantissa.val <= U64.max := by norm_num at hintValue rw [hintValue, ← List.foldl_append] exact hfullBound obtain ⟨fracValue, fracScanRest, hexactFrac, hexactFracRest, hfracValue⟩ := NumberHelperDigitScanExact afterFirst intMantissa fracDigits fracTail (by simp [afterFirst, hfracSplit]) hfracDigits hfracTail hlenAfter hfracBound rw [hscanFrac] at hexactFrac simp at hexactFrac rcases hexactFrac with ⟨rfl, rfl⟩ norm_num at hintValue hfracValue ⊢ rw [hfracValue, hintValue] have hshort : totalDigits.val <= 19 -> fracMantissa.val <= 9999999999999999999 := by intro hsmall have hsigLen : intDigits.length + fracDigits.length <= 19 := by rw [htotalVal, hnDigitsEq, hnAfterEq] at hsmall omega have hfoldLt := fold_upper (intDigits ++ fracDigits) 0 hsigDigits norm_num at hfoldLt have hfold19 := hfoldLt.trans_le (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hsigLen) have hfoldBound : (intDigits ++ fracDigits).foldl (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by rw [U64.max_eq] norm_num omega rw [hexactScans hfoldBound] norm_num omega have hnonpositive : forall (nMore adjusted : Std.Isize), totalDigits - 19#isize = ok nMore -> num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted -> Not (totalDigits.val <= 19) -> Not (0 < adjusted.val) -> fracMantissa.val <= 9999999999999999999 := by intro nMore adjusted hsub hadjust hlarge hnonpos by_cases hallzero : forall b, List.Mem b (intDigits ++ fracDigits) -> b = 48#u8 · have hfoldZero := zero_fold (intDigits ++ fracDigits) hallzero have hfoldBound : (intDigits ++ fracDigits).foldl (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by rw [hfoldZero] exact Nat.zero_le _ rw [hexactScans hfoldBound, hfoldZero] norm_num · obtain ⟨rawZeros, rawTail, sigZeros, sigTail, hrawSplit, hrawZeros, hrawTail, hsigSplit, hsigZeros, hsigTail, hcount⟩ := split_significand_zeros intDigits fracDigits fracTail hintDigits hfracDigits hallzero have hsAdjust : s.val = rawZeros ++ rawTail := by rw [hsval, hfracSplit] exact hrawSplit have hnMoreVal : nMore.val = totalDigits.val - 19 := by have heq := IScalar.sub_equiv totalDigits 19#isize rw [hsub] at heq have hval := heq.2.1 norm_num at hval ⊢ exact hval have hnMoreNonneg : 0 <= nMore.val := by rw [hnMoreVal] omega have hnMoreLe : nMore.val <= s.val.length := by rw [hnMoreVal, htotalVal, hnDigitsEq, hnAfterEq, hsval, hfracSplit] simp only [List.length_append, List.length_cons] omega have hadjustLower : IScalar.min .Isize <= nMore.val - (rawZeros.count 48#u8 : Int) := by rw [hnMoreVal, hcount, htotalVal, hnDigitsEq, hnAfterEq] have hzlen : sigZeros.length <= (intDigits ++ fracDigits).length := by rw [hsigSplit] simp have hsigSource : (intDigits ++ fracDigits).length <= s.val.length := by rw [hsval, hfracSplit] simp only [List.length_append, List.length_cons] omega have hzBound : sigZeros.length <= 9223372036854775807 := hzlen.trans (hsigSource.trans hlen) simp [IScalar.min, IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] omega obtain ⟨adjusted2, hadjust2, hadjust2Val⟩ := NumberHelperAdjustManyExact s nMore rawZeros rawTail hsAdjust hrawZeros (Or.inr hrawTail) hlen hnMoreNonneg hnMoreLe hadjustLower rw [hadjust] at hadjust2 simp at hadjust2 subst adjusted2 have hsigTailLen : sigTail.length <= 19 := by rw [hadjust2Val, hnMoreVal, hcount, htotalVal, hnDigitsEq, hnAfterEq] at hnonpos have hlenEq := congrArg List.length hsigSplit simp only [List.length_append] at hlenEq omega have hsigTailDigits : forall b, List.Mem b sigTail -> 48 <= b.val /\ b.val <= 57 := by intro b hb apply hsigDigits b rw [hsigSplit] exact List.mem_append_right sigZeros hb have hzeroFold := zero_fold sigZeros hsigZeros have htailLt := fold_upper sigTail 0 hsigTailDigits norm_num at htailLt have hfoldLt : (intDigits ++ fracDigits).foldl (fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 := by rw [hsigSplit, List.foldl_append, hzeroFold] exact htailLt.trans_le (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hsigTailLen) have hfoldBound : (intDigits ++ fracDigits).foldl (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by exact (Nat.le_of_lt hfoldLt).trans (by norm_num [U64.max_eq]) rw [hexactScans hfoldBound] norm_num at hfoldLt ⊢ omega unfold num.imp.dec2flt.parse.parse_partial_number at hpartial simp [hne, hscanInt, hoffInt, hsplitInt, hscanFrac, hoffFrac, hnegAfter, hfracExponent] at hpartial simp only [UScalar.ofNatCore] at hpartial simp [htotal] at hpartial by_cases hzero : totalDigits = 0#isize · simp [hzero] at hpartial · simp [hzero] at hpartial cases fracTail with | nil => have hsplitFrac : core.slice.Slice.split_first fracRest = ok none := by simp [core.slice.Slice.split_first, ModelSliceSplitFirst, fracRest] simp [hsplitFrac] at hpartial obtain ⟨off, hoff, hpartial⟩ := bind_ok_inv _ _ _ hpartial obtain ⟨len, hcast, hpartial⟩ := bind_ok_inv _ _ _ hpartial exact record_branches totalDigits fracMantissa intRest fracExponent 0#i64 len parsedLen hshort hnonpositive hpartial | cons marker suffix => have hsuffixProp : suffix.length <= Usize.max := by have hp := fracRest.property simp [fracRest] at hp ⊢ omega let afterMarker : Slice Std.U8 := ⟨suffix, hsuffixProp⟩ have hsplitFrac : core.slice.Slice.split_first fracRest = ok (some (marker, afterMarker)) := by simp [core.slice.Slice.split_first, ModelSliceSplitFirst, fracRest, afterMarker] simp [hsplitFrac] at hpartial by_cases hlower : marker = (⟨101#8⟩ : Std.U8) · subst marker obtain ⟨sciResult, hsci, hpartial⟩ := bind_ok_inv _ _ _ hpartial rcases sciResult with ⟨optExp, sciRest⟩ cases optExp with | none => simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual, ModelOptionFromResidual] at hpartial | some parsedExp => simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch] at hpartial obtain ⟨combined, hsum, hpartial⟩ := bind_ok_inv _ _ _ hpartial obtain ⟨off, hoff, hpartial⟩ := bind_ok_inv _ _ _ hpartial obtain ⟨len, hcast, hpartial⟩ := bind_ok_inv _ _ _ hpartial exact record_branches totalDigits fracMantissa intRest combined parsedExp len parsedLen hshort hnonpositive hpartial · by_cases hupper : marker = (⟨69#8⟩ : Std.U8) · subst marker obtain ⟨sciResult, hsci, hpartial⟩ := bind_ok_inv _ _ _ hpartial rcases sciResult with ⟨optExp, sciRest⟩ cases optExp with | none => simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual, ModelOptionFromResidual] at hpartial | some parsedExp => simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch] at hpartial obtain ⟨combined, hsum, hpartial⟩ := bind_ok_inv _ _ _ hpartial obtain ⟨off, hoff, hpartial⟩ := bind_ok_inv _ _ _ hpartial obtain ⟨len, hcast, hpartial⟩ := bind_ok_inv _ _ _ hpartial exact record_branches totalDigits fracMantissa intRest combined parsedExp len parsedLen hshort hnonpositive hpartial · simp [hlower, hupper] at hpartial obtain ⟨off, hoff, hpartial⟩ := bind_ok_inv _ _ _ hpartial obtain ⟨len, hcast, hpartial⟩ := bind_ok_inv _ _ _ hpartial exact record_branches totalDigits fracMantissa intRest fracExponent 0#i64 len parsedLen hshort hnonpositive hpartial · have haddZero : nDigits + 0#isize = ok nDigits := by have hb := IScalar.hBounds nDigits obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.add_spec (x := nDigits) (y := 0#isize) (by simp [IScalar.min, IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] omega) (by simp [IScalar.max, IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] at hb ⊢ omega)) rw [hz] congr 2 apply IScalar.eq_of_val_eq simpa using hzval have hshort : nDigits.val <= 19 -> intMantissa.val <= 9999999999999999999 := by intro hsmall have hlen19 : intDigits.length <= 19 := by omega have hfoldLt := fold_upper intDigits 0 hintDigits norm_num at hfoldLt have hfold19 : intDigits.foldl (fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 := hfoldLt.trans_le (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hlen19) have hfoldBound : intDigits.foldl (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by rw [U64.max_eq] omega obtain ⟨value, rest, hexact, hrest, hvalue⟩ := NumberHelperDigitScanExact s 0#u64 intDigits (first :: tail) (by simpa using hsval) hintDigits hintTail hlen hfoldBound rw [hscanInt] at hexact simp at hexact rcases hexact with ⟨rfl, rfl⟩ norm_num at hvalue ⊢ rw [hvalue] omega have hnonpositive : forall (nMore adjusted : Std.Isize), nDigits - 19#isize = ok nMore -> num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted -> Not (nDigits.val <= 19) -> Not (0 < adjusted.val) -> intMantissa.val <= 9999999999999999999 := by intro nMore adjusted hsub hadjust hlarge hnonpos have hnMoreVal : nMore.val = nDigits.val - 19 := by have heq := IScalar.sub_equiv nDigits 19#isize rw [hsub] at heq have hval := heq.2.1 norm_num at hval ⊢ exact hval obtain ⟨zeros, nzTail, hzeroSplit, hzeros, hnzTail⟩ := split_zeros intDigits have hcount := count_zeros zeros hzeros let rawTail : List Std.U8 := nzTail ++ first :: tail have hsAdjust : s.val = zeros ++ rawTail := by rw [hsval, hzeroSplit] simp [rawTail, List.append_assoc] have hrawTail : rawTail = [] \/ exists b rest, rawTail = b :: rest /\ b ≠ 48#u8 /\ b ≠ 46#u8 := by right rcases hnzTail with hnil | ⟨b, rest, heq, hb⟩ · subst nzTail refine ⟨first, tail, by simp [rawTail], ?_, hpoint⟩ intro hzero subst first have hnot := hintTail 48#u8 tail rfl norm_num at hnot · refine ⟨b, rest ++ first :: tail, by simp [rawTail, heq], hb, ?_⟩ have hbdigit := hintDigits b (by rw [hzeroSplit, heq] exact List.mem_append_right zeros List.mem_cons_self) intro heq46 subst b norm_num at hbdigit have hnMoreNonneg : 0 <= nMore.val := by rw [hnMoreVal] omega have hnMoreLe : nMore.val <= s.val.length := by rw [hnMoreVal, hnDigitsEq, hsval] simp only [List.length_append, List.length_cons] omega have hadjustLower : IScalar.min .Isize <= nMore.val - (zeros.count 48#u8 : Int) := by rw [hnMoreVal, hcount, hnDigitsEq] have hzlen : zeros.length <= intDigits.length := by rw [hzeroSplit] simp simp [IScalar.min, IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] omega obtain ⟨adjusted2, hadjust2, hadjust2Val⟩ := NumberHelperAdjustManyExact s nMore zeros rawTail hsAdjust (by intro b hb; exact Or.inl (hzeros b hb)) hrawTail hlen hnMoreNonneg hnMoreLe hadjustLower rw [hadjust] at hadjust2 simp at hadjust2 subst adjusted2 have hnzLen : nzTail.length <= 19 := by rw [hadjust2Val, hnMoreVal, hcount, hnDigitsEq] at hnonpos have hlenEq := congrArg List.length hzeroSplit simp only [List.length_append] at hlenEq omega have hnzDigits : forall b, List.Mem b nzTail -> 48 <= b.val /\ b.val <= 57 := by intro b hb apply hintDigits b rw [hzeroSplit] exact List.mem_append_right zeros hb have hzeroFold := zero_fold zeros hzeros have hfoldLt : intDigits.foldl (fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 := by rcases hnzTail with hnil | hcons · subst nzTail rw [hzeroSplit, List.append_nil, hzeroFold] norm_num · rw [hzeroSplit, List.foldl_append, hzeroFold] have htailLt := fold_upper nzTail 0 hnzDigits norm_num at htailLt exact htailLt.trans_le (Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hnzLen) have hfoldBound : intDigits.foldl (fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by rw [U64.max_eq] omega obtain ⟨value, rest, hexact, hrest, hvalue⟩ := NumberHelperDigitScanExact s 0#u64 intDigits (first :: tail) (by simpa using hsval) hintDigits hintTail hlen hfoldBound rw [hscanInt] at hexact simp at hexact rcases hexact with ⟨rfl, rfl⟩ norm_num at hvalue ⊢ rw [hvalue] omega have hpointVal : first.val ≠ 46 := by intro hval apply hpoint exact UScalar.eq_of_val_eq hval unfold num.imp.dec2flt.parse.parse_partial_number at hpartial simp [hne, hscanInt, hoffInt, hsplitInt] at hpartial split at hpartial all_goals try { exfalso; apply hpointVal; rfl } simp [haddZero] at hpartial by_cases hnzero : nDigits = 0#isize · simp [hnzero] at hpartial · simp [hnzero, hsplitInt] at hpartial by_cases hlower : first = 101#u8 · subst first obtain ⟨sciResult, hsci, hpartial⟩ := bind_ok_inv _ _ _ hpartial rcases sciResult with ⟨optExp, sciRest⟩ cases optExp with | none => simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual, ModelOptionFromResidual] at hpartial | some parsedExp => simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch] at hpartial obtain ⟨combined, hsum, hpartial⟩ := bind_ok_inv _ _ _ hpartial obtain ⟨off, hoff, hpartial⟩ := bind_ok_inv _ _ _ hpartial obtain ⟨len, hcast, hpartial⟩ := bind_ok_inv _ _ _ hpartial exact record_branches nDigits intMantissa intRest combined parsedExp len parsedLen hshort hnonpositive hpartial · by_cases hupper : first = 69#u8 · simp [hlower] at hpartial subst first obtain ⟨sciResult, hsci, hpartial⟩ := bind_ok_inv _ _ _ hpartial rcases sciResult with ⟨optExp, sciRest⟩ cases optExp with | none => simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual, ModelOptionFromResidual] at hpartial | some parsedExp => simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch] at hpartial obtain ⟨combined, hsum, hpartial⟩ := bind_ok_inv _ _ _ hpartial obtain ⟨off, hoff, hpartial⟩ := bind_ok_inv _ _ _ hpartial obtain ⟨len, hcast, hpartial⟩ := bind_ok_inv _ _ _ hpartial exact record_branches nDigits intMantissa intRest combined parsedExp len parsedLen hshort hnonpositive hpartial · simp [hlower, hupper] at hpartial rw [hoffInt] at hpartial simp at hpartial obtain ⟨len, hcast, hpartial⟩ := bind_ok_inv _ _ _ hpartial exact record_branches nDigits intMantissa intRest 0#i64 0#i64 len parsedLen hshort hnonpositive hpartial end dec2flt_model