import Tablet.DecimalSeqDyadicShiftSemantics import Tablet.ParserExponentConstants import Tablet.ParserSlowReachableDecimalSeq import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_round import Tablet.num_imp_dec2flt_slow_parse_long_mantissa_MAX_SHIFT import Tablet.num_imp_dec2flt_slow_parse_long_mantissa_NUM_POWERS import Tablet.num_imp_dec2flt_slow_parse_long_mantissa_POWERS import Tablet.num_imp_dec2flt_slow_parse_long_mantissa_closure_Insts_CoreOpsFunctionFnTupleUsizeUsize_call import Tablet.ParserSlowInitialCanonicality import Tablet.DecimalSeqLeftShiftCanonical import Tablet.DecimalSeqRightShiftCanonical import Tablet.ParserSlowEarlyExits import Tablet.ParserSlowLoop0 import Tablet.ParserSlowLoop1 import Tablet.ParserSlowLoop2 import Tablet.DecimalSeqRoundNearestEven import Tablet.ParserSlowReachableNormalizationBounds import Tablet.ParserSlowFirstRoundSeparation import Tablet.P856_T1_OddIntegerSeparation import Tablet.ParserSlowCarryRoundSeparation import Tablet.ParserSlowFinalEncoding open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: ParserSlowNormalizationCertificate] theorem ParserSlowNormalizationCertificate : forall (s : Str) (x : DecimalLexeme) (f : F64) (tr : num.imp.dec2flt.Dec2FltAssemblyTrace F64), DecimalGoalVocabularyDefault.sourceString s -> DecimalGoalVocabularyDefault.validNumeric x -> DecimalGoalVocabularyDefault.bytes s = DecimalGoalVocabularyDefault.renderNumeric x -> num.imp.dec2flt.dec2flt_with_trace F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64 F64.Insts.CoreOpsArithDivF64F64 F64.Insts.CoreOpsArithMulF64F64 F64.Insts.CoreOpsArithNegF64 s = ok (core.result.Result.Ok f, num.imp.dec2flt.Dec2FltTrace.Assembled tr) -> tr.slow_path_guard = true -> let A := DecimalGoalVocabularyDefault.accumulatedMagnitude x let unit : Nat := 2 ^ 52 let infinityBits : Nat := 2047 * unit let threshold : Real := (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) let raw : Nat := Nat.lor tr.selected_pair.m.val (((tr.selected_pair.p_biased.val.emod (2 ^ 64)).toNat * unit) % (2 ^ 64)) 0 <= A /\ ((raw = infinityBits /\ threshold <= A) \/ (exists j : Nat, j <= 2 ^ 53 /\ raw = j /\ A <= (2 ^ 53 : Nat) * (2 : Real) ^ (-(1074 : Int)) /\ A <= (j : Real) * (2 : Real) ^ (-(1074 : Int)) + (2 : Real) ^ (-(1075 : Int)) /\ (j : Real) * (2 : Real) ^ (-(1074 : Int)) <= A + (2 : Real) ^ (-(1075 : Int)) /\ (A = ((j : Real) + (1 / 2 : Real)) * (2 : Real) ^ (-(1074 : Int)) \/ A = ((j : Real) - (1 / 2 : Real)) * (2 : Real) ^ (-(1074 : Int)) -> j % 2 = 0)) \/ (exists p j : Nat, 2 <= p /\ p <= 2046 /\ 2 ^ 52 <= j /\ j <= 2 ^ 53 /\ (p < 2046 \/ j < 2 ^ 53) /\ raw = p * unit + (j - unit) /\ let delta : Real := (2 : Real) ^ ((p : Int) - 1075) (unit : Real) * delta <= A /\ A <= (2 ^ 53 : Nat) * delta /\ A < threshold /\ A <= (j : Real) * delta + delta / 2 /\ (j : Real) * delta <= A + delta / 2 /\ (A = ((j : Real) + (1 / 2 : Real)) * delta \/ A = ((j : Real) - (1 / 2 : Real)) * delta -> j % 2 = 0))) := by -- BODY intro s x f tr hsource hvalid hbytes htrace hguard let A := DecimalGoalVocabularyDefault.accumulatedMagnitude x have hnonnegative : 0 <= A := by obtain ⟨d, _hparse, _hslow, _hassembled, hA, _hcanonical⟩ := ParserSlowInitialCanonicality s x f tr hsource hvalid hbytes htrace hguard exact hA dsimp only constructor · exact hnonnegative · have hearly := ParserSlowEarlyExits s x f tr hsource hvalid hbytes htrace hguard dsimp only at hearly rcases hearly with hzero | hunder | hinfinity | hregular · right left exact hzero.2.2 · right left exact hunder.2.2.2 · exact Or.inl hinfinity · rcases hregular with ⟨d, hparse, hslow, hassembled, hdne, hdpLower, hdpUpper, hcanonical⟩ let digits := (d.digits.val.take d.num_digits.val).map (fun z => z.val) let integer : Nat := digits.foldl (fun n digit => 10 * n + digit) 0 let value : Real := integer * (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) let cutoff : Real := (10 : Real) ^ (d.decimal_point.val - (768 : Int)) let B : Real := value + cutoff have hB : B = value + cutoff := rfl obtain ⟨d0, exp0, steps0, hloop0, _hsteps0, hexp0Lower, _hexp0Upper, hALower, hd0Lower, hd0Upper, _hselector, hstate0⟩ := ParserSlowLoop0 A B d hdne hdpLower hdpUpper hB hcanonical obtain ⟨d1, exp1, steps1, hloop1, _hsteps1, _hexp10, hd1Point, hd1ne, hd1Lead, _hd1Digit, hstate1⟩ := ParserSlowLoop1 A B d0 exp0 hd0Lower hd0Upper hexp0Lower hstate0 have hreach := ParserSlowReachableNormalizationBounds A B d d0 d1 exp0 exp1 hdpLower hdpUpper hB hcanonical hALower hloop0 hloop1 hd1Point hd1ne hd1Lead hstate1.2.2.2 obtain ⟨hBUpper, hexp1Lower⟩ := hreach obtain ⟨expStart, d2, exp2, totalShift, hsub, hloop2, hshiftBound, hstart, hexp2, hshiftEq, _hd2Point, _hd2ne, hstate2⟩ := ParserSlowLoop2 A B d1 exp1 hexp1Lower hd1Point hd1ne hd1Lead ⟨hstate1.1, hstate1.2.1, hstate1.2.2.2⟩ have hinitial : d.num_digits.val <= 768 /\ (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\ (exists z zs, digits = z :: zs /\ 1 <= z /\ z < 10 /\ digits.length = d.num_digits.val) /\ B = value + cutoff /\ (exists eta : Real, A = value + eta /\ 0 <= eta /\ eta < cutoff /\ (d.truncated = true <-> 0 < eta)) := by exact ⟨hcanonical.1, hcanonical.2.1, hcanonical.2.2.1, hB, hcanonical.2.2.2⟩ obtain ⟨d3, hleft53, hstate3⟩ := ParserSlowFirstRoundSeparation A B d d0 d1 d2 exp0 exp1 expStart exp2 hdpLower hdpUpper hinitial hloop0 hloop1 hsub hloop2 hstate2 have hodd := P856_T1_OddIntegerSeparation A B d d0 d1 d2 d3 exp0 exp1 expStart exp2 hdpLower hdpUpper hinitial hloop0 hloop1 hsub hloop2 hstate2 hleft53 dsimp only at hodd have hcarry : forall d4 : num.imp.dec2flt.decimal_seq.DecimalSeq, num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d3 1#usize = ok d4 -> d4.num_digits.val != 0 -> let digits4 := (d4.digits.val.take d4.num_digits.val).map (fun z => z.val) let integer4 : Nat := digits4.foldl (fun n digit => 10 * n + digit) 0 let value4 : Real := integer4 * (10 : Real) ^ (d4.decimal_point.val - (d4.num_digits.val : Int)) d4.num_digits.val <= 768 /\ (forall z, z ∈ d4.digits.val.take d4.num_digits.val -> z.val < 10) /\ (exists z zs, digits4 = z :: zs /\ 1 <= z /\ z < 10 /\ digits4.length = d4.num_digits.val /\ digits4.getLast? != some 0) /\ (exists eta4 u4 : Real, A / (2 : Real) ^ (exp2.val - 51) = value4 + eta4 /\ B / (2 : Real) ^ (exp2.val - 51) = value4 + u4 /\ 0 <= eta4 /\ eta4 < u4 /\ (d4.truncated = true <-> 0 < eta4)) /\ d4.decimal_point.val < 19 /\ 0 <= A / (2 : Real) ^ (exp2.val - 51) /\ A / (2 : Real) ^ (exp2.val - 51) < (2 : Real) ^ (64 : Nat) - (1 / 2 : Real) /\ forall n : Nat, (n : Real) + (1 / 2 : Real) <= value4 \/ B / (2 : Real) ^ (exp2.val - 51) <= (n : Real) + (1 / 2 : Real) := by intro d4 hright hne exact ParserSlowCarryRoundSeparation A B d3 d4 exp2 hstate3 hodd hright hne apply ParserSlowFinalEncoding x B tr d d0 d1 d2 d3 exp0 exp1 expStart exp2 hparse hdne hdpLower hdpUpper hslow hassembled hloop0 hloop1 hloop2 hsub hstart hd1Point hd1ne hd1Lead hexp1Lower hBUpper hexp2 · omega · exact hstate2 · exact hleft53 · exact hstate3 · exact hcarry end dec2flt_model