import Tablet.P435_T1_RenderedDigitBlocks import Tablet.P435_T1_DigitDecoding import Tablet.P435_T1_SignificandScanA import Tablet.P367_T1_NumberHelperFractionalRetryA import Tablet.P367_T1_NumberHelperIntegralRetryA import Tablet.DecimalGoalVocabularyDefault import Tablet.P9_T1_ActualDispatchOperations import Tablet.num_imp_dec2flt_parse_parse_number import Tablet.num_imp_dec2flt_parse_parse_partial_number import Tablet.P789_T1_RenderedExponentContinuation import Tablet.P789_T1_RenderedSignificantBounds import Tablet.P789_T1_SignificandNonwrapping import Tablet.P789_T1_PartialNumberBranches open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P367_T1_NumberHelperRenderedAccountingA] theorem P367_T1_NumberHelperRenderedAccountingA (s : Slice Std.U8) (x : DecimalLexeme) (d : num.imp.dec2flt.decimal.Decimal) (hlen : s.val.length <= 3458764513820540928) (hsign : x.leadingSign = none) (hvalid : DecimalGoalVocabularyDefault.validNumeric x) (hrender : DecimalGoalVocabularyDefault.renderNumeric x = DecimalGoalVocabularyDefault.bytes s) (hparse : P9_T1_ActualDispatchOperations.parseNumber s = ok (some d)) : let digits := x.integerDigits ++ (match x.fractionDigits with | none => [] | some ds => ds) let fractionCount := match x.fractionDigits with | none => 0 | some ds => ds.length let explicit := DecimalGoalVocabularyDefault.accumulatedExplicitExponent x (d.many_digits = false -> d.mantissa.val = digits.foldl (fun a b => 10 * a + b) 0 /\ d.exponent.val = explicit - (fractionCount : Int)) /\ (d.many_digits = true -> exists consumed remaining : List Nat, digits = consumed ++ remaining /\ d.mantissa.val = consumed.foldl (fun a b => 10 * a + b) 0 /\ d.exponent.val = explicit - (fractionCount : Int) + (remaining.length : Int)) := by -- BODY have hpartial : exists consumed : Std.Usize, num.imp.dec2flt.parse.parse_partial_number s = ok (some (d, consumed)) := by change num.imp.dec2flt.parse.parse_number s = ok (some d) at hparse unfold num.imp.dec2flt.parse.parse_number at hparse cases hp : num.imp.dec2flt.parse.parse_partial_number s with | fail e => simp [hp] at hparse | div => simp [hp] at hparse | ok result => cases result with | none => simp [hp] at hparse | some pair => rcases pair with ⟨d', consumed⟩ by_cases hc : consumed = s.len · simp [hp, hc] at hparse subst d' exact ⟨consumed, rfl⟩ · simp [hp, hc] at hparse obtain ⟨consumed, hpartial⟩ := hpartial obtain ⟨integer, fraction, exponent, hblocks, hi, hf, he, hid, hfd, hed⟩ := P435_T1_RenderedDigitBlocks s.val [] x hsign hvalid (by change s.val.map (fun b => b.val) = DecimalGoalVocabularyDefault.renderNumeric x ++ [].map (fun b : Std.U8 => b.val) simpa using hrender.symm) let tail : List Std.U8 := match x.exponent with | none => [] | some (upper, sign, _) => (if upper then 69#u8 else 101#u8) :: (match sign with | none => [] | some neg => [if neg then 45#u8 else 43#u8]) ++ exponent let point : Bool := x.fractionDigits.isSome have hshape : s.val = integer ++ (if point then 46#u8 :: (fraction ++ tail) else tail) := by cases hx : x.fractionDigits <;> simpa [point, hx, tail, List.append_assoc] using hblocks have hnoFraction : point = false -> fraction = [] := by cases hx : x.fractionDigits with | none => intro _; simpa [hx] using hf | some ds => simp [point, hx] have htail : forall b rest, tail = b :: rest -> ¬ (48 <= b.val /\ b.val <= 57) := by intro b rest h cases hx : x.exponent with | none => simp [tail, hx] at h | some tuple => rcases tuple with ⟨upper, sign, ds⟩ have hb : b = (if upper then 69#u8 else 101#u8) := by simpa [tail, hx] using (congrArg List.head? h).symm rw [hb] cases upper <;> norm_num have hnoPoint : point = false -> forall b rest, tail = b :: rest -> b ≠ 46#u8 := by intro _ b rest h cases hx : x.exponent with | none => simp [tail, hx] at h | some tuple => rcases tuple with ⟨upper, sign, ds⟩ have hb : b = (if upper then 69#u8 else 101#u8) := by simpa [tail, hx] using (congrArg List.head? h).symm rw [hb] cases upper <;> norm_num obtain ⟨intEnd, sigEnd, intWord, word, intCount, fractionCount, total, base, hscan, hintEnd, hiOffset, _, hfront, hsigEnd, _, hbase, hadd, htotal⟩ := P435_T1_SignificandScanA s integer fraction tail point hshape hnoFraction hid hfd htail hnoPoint hlen obtain ⟨hdecodeI, hlengthI, _⟩ := (P435_T1_DigitDecoding integer).2 x.integerDigits hi obtain ⟨hdecodeF, hlengthF, _⟩ := (P435_T1_DigitDecoding fraction).2 (match x.fractionDigits with | none => [] | some ds => ds) hf have hbaseExact : base.val = -((match x.fractionDigits with | none => 0 | some ds => ds.length) : Int) := by rw [hbase, hlengthF] cases x.fractionDigits <;> rfl have htotalExact : total.val = ((x.integerDigits ++ (match x.fractionDigits with | none => [] | some ds => ds)).length : Int) := by rw [htotal, hlengthI, hlengthF, List.length_append] have hdecodeIF : (integer ++ fraction).map (fun b => b.val - 48) = x.integerDigits ++ (match x.fractionDigits with | none => [] | some ds => ds) := by simp only [List.map_append, hdecodeI, hdecodeF] have hfoldIF : (integer ++ fraction).foldl (fun a b => 10 * a + (b.val - 48)) 0 = (x.integerDigits ++ (match x.fractionDigits with | none => [] | some ds => ds)).foldl (fun a b => 10 * a + b) 0 := by rw [← List.foldl_map, hdecodeIF] have hcountBound : integer.length + fraction.length <= s.val.length := by rw [hshape] cases hp : point with | false => simp [hp, hnoFraction hp] | true => simp [hp]; omega have hpositive : 0 < total.val := by have hv : x.integerDigits ≠ [] ∨ match x.fractionDigits with | none => False | some ds => ds ≠ [] := by exact (show _ ∧ _ ∧ _ ∧ _ from hvalid).2.2.1 rw [htotalExact] rcases hv with hv | hv · have : 0 < x.integerDigits.length := List.length_pos_of_ne_nil hv simp only [List.length_append, Nat.cast_add] omega · cases hx : x.fractionDigits with | none => simp [hx] at hv | some ds => have : 0 < ds.length := List.length_pos_of_ne_nil (by simpa [hx] using hv) simp only [hx, List.length_append, Nat.cast_add] omega obtain ⟨explicit, hexact, hexpLo, hexpHi, hcontinuation⟩ := P789_T1_RenderedExponentContinuation x sigEnd exponent hvalid he hed hsigEnd have hstop : forall b rest, tail = b :: rest -> b ≠ 48#u8 /\ b ≠ 46#u8 := by intro b rest ht cases hx : x.exponent with | none => simp [tail, hx] at ht | some tuple => rcases tuple with ⟨upper, sign, ds⟩ have hb : b = (if upper then 69#u8 else 101#u8) := by simpa [tail, hx] using (congrArg List.head? ht).symm rw [hb] cases upper <;> norm_num obtain ⟨_, hretainedBound, hretryBound⟩ := P789_T1_RenderedSignificantBounds s integer fraction tail point hshape hnoFraction hid hfd hstop have hbaseLo : -3458764513820540928 <= base.val := by rw [hbase] omega have hbaseHi : base.val <= 3458764513820540928 := by rw [hbase]; omega have hcases := P789_T1_PartialNumberBranches s intEnd sigEnd intWord word intCount fractionCount total base explicit d consumed hlen hpositive (by rw [htotal]; exact_mod_cast hcountBound) hbaseLo hbaseHi hexpLo hexpHi hscan hiOffset hfront hadd hcontinuation hpartial constructor · intro hfalse rcases hcases with hkeep | hretry · rcases hkeep with ⟨_, hword, hexponent, hsize⟩ have hbound : (integer ++ fraction).foldl (fun a b => 10 * a + (b.val - 48)) 0 < 10^19 := hretainedBound (by simpa only [htotal] using hsize) have hvalue := P789_T1_SignificandNonwrapping s intEnd sigEnd intWord word fractionCount base integer fraction tail point hshape hintEnd hnoFraction hid hfd htail hnoPoint (by omega) (by rw [U64.max_eq]; norm_num at hbound ⊢; omega) hscan hfront exact ⟨by rw [hword, hvalue, hfoldIF], by rw [hexponent, hbaseExact, hexact]; omega⟩ · simp [hfalse] at hretry · intro htrue rcases hcases with hkeep | hretry · simp [htrue] at hkeep · rcases hretry with ⟨_, hpositiveRetry, hretry⟩ have hlarge : 10^19 <= (integer ++ fraction).foldl (fun a b => 10 * a + (b.val - 48)) 0 := hretryBound (by simpa only [htotal] using hpositiveRetry) have finish : forall (m : Std.U64) (e : Std.I64) (used rest : List Std.U8), num.imp.dec2flt.parse.parse_many_digits s intEnd explicit = ok (m, e) -> integer ++ fraction = used ++ rest -> m.val = used.foldl (fun a b => 10 * a + (b.val - 48)) 0 -> e.val = explicit.val - (fraction.length : Int) + (rest.length : Int) -> exists used rest : List Nat, x.integerDigits ++ (match x.fractionDigits with | none => [] | some ds => ds) = used ++ rest /\ d.mantissa.val = used.foldl (fun a b => 10 * a + b) 0 /\ d.exponent.val = DecimalGoalVocabularyDefault.accumulatedExplicitExponent x - ((match x.fractionDigits with | none => 0 | some ds => ds.length) : Int) + (rest.length : Int) := by intro m e used rest hp hpart hm he have heq : (m, e) = (d.mantissa, d.exponent) := Result.ok.inj (hp.symm.trans hretry) have hm' : m = d.mantissa := congrArg Prod.fst heq have he' : e = d.exponent := congrArg Prod.snd heq refine ⟨used.map (fun b => b.val - 48), rest.map (fun b => b.val - 48), ?_, ?_, ?_⟩ · simpa only [List.map_append, hdecodeIF] using congrArg (List.map (fun b : Std.U8 => b.val - 48)) hpart · simpa only [List.foldl_map, hm'] using hm · rw [he'] at he cases hx : x.fractionDigits <;> simpa only [List.length_map, hexact, hlengthF, hx, List.length_nil, Nat.cast_zero] using he cases hp : point with | true => obtain ⟨m, e, used, rest, hparseRetry, hpart, hm, _, he⟩ := P367_T1_NumberHelperFractionalRetryA s intEnd explicit integer fraction tail (by simpa [hp] using hshape) (by simpa [hp] using hintEnd) hid hfd htail hlen hexpLo hexpHi exact finish m e used rest hparseRetry hpart hm he | false => have hfEmpty := hnoFraction hp obtain ⟨m, e, used, rest, hparseRetry, hpart, hm, _, _, he⟩ := P367_T1_NumberHelperIntegralRetryA s intEnd explicit integer tail (by simpa [hp] using hshape) (by simpa [hp] using hintEnd) hid htail hlen (by simpa [hfEmpty] using (show 1000000000000000000 <= (integer ++ fraction).foldl (fun a b => 10 * a + (b.val - 48)) 0 by norm_num at hlarge; omega)) hexpLo hexpHi exact finish m e used rest hparseRetry (by simpa [hfEmpty] using hpart) hm (by simpa [hfEmpty] using he) end dec2flt_model