import Tablet.P495_T1_DecimalSeqReconstruction import Tablet.P472_T1_RetainedRegularPoint import Tablet.P472_T1_InitialPointCutoffRounding import Tablet.P472_T1_LexemeSignificantMagnitude import Tablet.P401_T1_ZeroRounding import Tablet.P9_T1_ActualDispatchOperations import Tablet.ParserExponentConstants open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P495_T1_SlowInitialCertificate] theorem P495_T1_SlowInitialCertificate (s : Slice Std.U8) (x : DecimalLexeme) (fp : num.imp.dec2flt.common.BiasedFp) (hsource : SourceValidByteSlice s) (hsign : x.leadingSign = none) (hvalid : DecimalGoalVocabularyDefault.validNumeric x) (hbytes : DecimalGoalVocabularyDefault.renderNumeric x = DecimalGoalVocabularyDefault.bytes s) (hslow : P9_T1_ActualDispatchOperations.slow s = ok fp) : (exists w : F64, DecimalGoalVocabularyDefault.assembledWord fp w /\ DecimalGoalVocabularyDefault.roundsMagnitude (DecimalGoalVocabularyDefault.accumulatedMagnitude x) w) \/ (exists (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (eta : Real) (head : Nat), num.imp.dec2flt.decimal_seq.parse_decimal_seq s = ok d /\ 0 < d.num_digits.val /\ d.num_digits.val <= 768 /\ -324 <= d.decimal_point.val /\ d.decimal_point.val < 310 /\ (forall b, b ∈ d.digits.val -> b.val < 10) /\ (d.digits.val[0]?).map (fun b => b.val) = some head /\ 1 <= head /\ head < 10 /\ DecimalGoalVocabularyDefault.accumulatedMagnitude x = ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl (fun a digit => 10 * a + digit) 0 : Nat) : Real) * (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) + eta /\ 0 <= eta /\ eta < (10 : Real) ^ (d.decimal_point.val - (768 : Int)) /\ (d.truncated = true <-> 0 < eta)) := by -- BODY obtain ⟨d, scan, n, f, e, hparse, hnScan, hnlen, hfLen, heVal, hnBound, hfBound, hSbound, hcall, hpoint, hnum, hnumBound, hstored, htrunc, hscanPoint, hscanTrunc, hscanStored, hscanTake, hscanDigits, hdigit, hCempty, hCzero, hhead⟩ := P495_T1_DecimalSeqReconstruction s x hsource hsign hvalid hbytes let S := x.integerDigits ++ match x.fractionDigits with | none => [] | some ds => ds let C := S.dropWhile (fun z => z == 0) let T := (C.reverse.dropWhile (fun z => z == 0)).reverse have hnCount : n.val = C.length := by simpa [S, C] using hnlen have bind_ok_inv : forall {alpha beta : Type} (r : Result alpha) (g : alpha -> Result beta) y, Bind.bind r g = ok y -> exists z, r = ok z /\ g z = ok y := by intro alpha beta r g y hr cases r with | fail err => simp [Bind.bind, Aeneas.Std.bind] at hr | div => simp [Bind.bind, Aeneas.Std.bind] at hr | ok z => exact ⟨z, rfl, hr⟩ rcases ParserExponentConstants with ⟨_, _, _, _, _, hinf, _, _, _, _, _, _⟩ have hzero : num.imp.dec2flt.common.BiasedFp.zero_pow2 0#i32 = ok { m := 0#u64, p_biased := 0#i32 } := by rfl have hinfFp : num.imp.dec2flt.common.BiasedFp.zero_pow2 2047#i32 = ok { m := 0#u64, p_biased := 2047#i32 } := by rfl change num.imp.dec2flt.slow.parse_long_mantissa F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 s = ok fp at hslow unfold num.imp.dec2flt.slow.parse_long_mantissa at hslow rw [hzero, hinf] at hslow simp only [bind_tc_ok] at hslow rw [hinfFp] at hslow simp only [bind_tc_ok] at hslow obtain ⟨d0, hdparse, hslow⟩ := bind_ok_inv _ _ _ hslow have hd0 : d0 = d := Result.ok.inj (hdparse.symm.trans hparse) subst d0 by_cases hcount0 : d.num_digits = 0#usize · simp only [hcount0, if_pos] at hslow have hdval0 : d.num_digits.val = 0 := by simpa using congrArg (fun z : Std.Usize => z.val) hcount0 have hCnil : C = [] := hCempty.mpr hdval0 have hfp0 : fp = { m := 0#u64, p_biased := 0#i32 } := (Result.ok.inj hslow).symm let w : F64 := ⟨BitVec.ofNat 64 0⟩ refine Or.inl ⟨w, ?_, ?_⟩ · rw [hfp0] dsimp [DecimalGoalVocabularyDefault, w] decide +kernel · have hround := P401_T1_ZeroRounding have hX0 : DecimalGoalVocabularyDefault.accumulatedMagnitude x = 0 := by have hformula := P472_T1_LexemeSignificantMagnitude x have hfoldC : C.foldl (fun a digit => 10 * a + digit) 0 = 0 := by simp [hCnil] change DecimalGoalVocabularyDefault.accumulatedMagnitude x = ((C.foldl (fun a digit => 10 * a + digit) 0 : Nat) : Real) * (10 : Real) ^ ((x.integerDigits.length : Int) - (S.length - C.length : Nat) + DecimalGoalVocabularyDefault.accumulatedExplicitExponent x - (C.length : Int)) at hformula rw [hfoldC] at hformula simp only [Nat.cast_zero, zero_mul] at hformula exact hformula simpa [hX0] using hround · have hcountPos : 0 < d.num_digits.val := by have hne : d.num_digits.val ≠ 0 := by intro hv apply hcount0 exact UScalar.eq_of_val_eq (by simpa using hv) simpa using Nat.pos_of_ne_zero hne have hnUpper : n.val <= 9223372036854775807 := by change s.val.length <= 9223372036854775807 at hsource omega have hfUpper : f.val <= 9223372036854775807 := by change s.val.length <= 9223372036854775807 at hsource omega have hCne : C ≠ [] := by intro hnil have hz := hCempty.mp hnil omega have hfCount : f.val = (match x.fractionDigits with | none => 0 | some ds => ds.length) := by cases hfrac : x.fractionDigits with | none => simpa [hfrac] using hfLen | some ds => simpa [hfrac] using hfLen have hcut := P472_T1_InitialPointCutoffRounding x n f e d.decimal_point hvalid hnUpper hfUpper hcall have hcut' := hcut hnCount hCne hfCount heVal by_cases hsmall : d.decimal_point < (-324)#i32 · simp only [hcount0, if_neg, hsmall, if_pos] at hslow have hsmallVal : d.decimal_point.val < -324 := by simpa only [IScalar.lt_equiv] using hsmall obtain ⟨w, hasm, hround⟩ := hcut'.1 hsmallVal have hfp0 : fp = { m := 0#u64, p_biased := 0#i32 } := (Result.ok.inj hslow).symm rw [hfp0] exact Or.inl ⟨w, hasm, hround⟩ · by_cases hlarge : (310#i32) <= d.decimal_point · simp only [hcount0, if_neg, hsmall, if_neg, hlarge, if_pos] at hslow have hlargeVal : 310 <= d.decimal_point.val := by simpa only [IScalar.le_equiv] using hlarge obtain ⟨w, hasm, hround⟩ := hcut'.2 hlargeVal have hfpInf : fp = { m := 0#u64, p_biased := 2047#i32 } := (Result.ok.inj hslow).symm rw [hfpInf] exact Or.inl ⟨w, hasm, hround⟩ · simp only [hcount0, if_neg, hsmall, if_neg, hlarge, if_neg] at hslow have hlo : -324 <= d.decimal_point.val := by have hnot : ¬ d.decimal_point.val < -324 := by simpa only [IScalar.lt_equiv] using hsmall omega have hhi : d.decimal_point.val < 310 := by have hnot : ¬ (310 : Int) <= d.decimal_point.val := by simpa only [IScalar.le_equiv] using hlarge omega obtain ⟨head, _, hheadLo, hheadHi, hheadFirst⟩ := hhead hCne have hCpos : 0 < C.length := Nat.pos_of_ne_zero (by intro hz exact hCne (List.length_eq_zero_iff.mp hz)) have hnPos : 0 < n.val := by rw [hnCount]; exact hCpos have hnz : n.val ≠ 0 := Nat.ne_of_gt hnPos have hregular := P472_T1_RetainedRegularPoint x d n f e hsign hvalid hnUpper hfUpper hnz hcall hlo hhi dsimp only at hregular obtain ⟨hcap, _, eta, hX, heta0, hetaCut, hsticky⟩ := hregular hnCount hfCount heVal hnum hstored htrunc exact Or.inr ⟨d, eta, head, hparse, hcountPos, hcap, hlo, hhi, hdigit, hheadFirst, hheadLo, hheadHi, hX, heta0, hetaCut, hsticky⟩ end dec2flt_model