import Tablet.ParserSlowLoop0 import Tablet.ParserSlowLoop1 import Tablet.ParserSlowLoop2 import Tablet.ParserSlowReachableNormalizationBounds import Tablet.P804_T1_DecimalValueBounds import Tablet.P856_T1_Loop0NegativeExit import Tablet.P856_T1_DecimalBinaryExponentBound import Mathlib.Tactic.FieldSimp open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P856_T1_ReachableCapacity] theorem P856_T1_ReachableCapacity (A B : Real) (d d0 d1 d2 : num.imp.dec2flt.decimal_seq.DecimalSeq) (exp0 exp1 expStart exp2 : Std.I32) (hpLo : -324 <= d.decimal_point.val) (hpHi : d.decimal_point.val < 310) (hB : 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)) B = value + cutoff) (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)) 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) /\ (exists eta : Real, A = value + eta /\ 0 <= eta /\ eta < cutoff /\ (d.truncated = true <-> 0 < eta))) (hloop0 : num.imp.dec2flt.slow.parse_long_mantissa_loop0 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 { m := 0#u64, p_biased := 0#i32 } d.num_digits d.decimal_point d.truncated d.digits 0#i32 none = ok (d0.num_digits, d0.decimal_point, d0.truncated, d0.digits, exp0, none)) (hloop1 : num.imp.dec2flt.slow.parse_long_mantissa_loop1 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 { m := 0#u64, p_biased := 2047#i32 } d0.num_digits d0.decimal_point d0.truncated d0.digits exp0 none = ok (d1.num_digits, d1.decimal_point, d1.truncated, d1.digits, exp1, none)) (hsub : exp1 - 1#i32 = ok expStart) (hloop2 : num.imp.dec2flt.slow.parse_long_mantissa_loop2 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 expStart = ok (d2, exp2)) : 0 <= exp2.val - exp0.val + 715 - d0.decimal_point.val := by -- BODY dsimp only at hB hcanonical let Digits := fun q : num.imp.dec2flt.decimal_seq.DecimalSeq => (q.digits.val.take q.num_digits.val).map (fun z => z.val) let Value := fun q : num.imp.dec2flt.decimal_seq.DecimalSeq => ((Digits q).foldl (fun n digit => 10 * n + digit) 0 : Nat) * (10 : Real) ^ (q.decimal_point.val - (q.num_digits.val : Int)) rcases hcanonical with ⟨hcap, hdigit, hpositive, hcanonicalA⟩ rcases hpositive with ⟨z, zs, hzshape, hzLo, hzHi, hzlen⟩ have hdne : d.num_digits.val != 0 := by have hpos : 0 < d.num_digits.val := by rw [← hzlen, hzshape] simp simpa [bne_iff_ne] using hpos.ne' have hcanonicalPack : d.num_digits.val <= 768 /\ (forall q, q ∈ d.digits.val.take d.num_digits.val -> q.val < 10) /\ (exists z zs, Digits d = z :: zs /\ 1 <= z /\ z < 10 /\ (Digits d).length = d.num_digits.val) /\ (exists eta : Real, A = Value d + eta /\ 0 <= eta /\ eta < (10 : Real) ^ (d.decimal_point.val - (768 : Int)) /\ (d.truncated = true <-> 0 < eta)) := ⟨hcap, hdigit, ⟨z, zs, hzshape, hzLo, hzHi, hzlen⟩, hcanonicalA⟩ have hspec0 := ParserSlowLoop0 A B d hdne hpLo hpHi hB (by simpa [Digits, Value] using hcanonicalPack) dsimp only at hspec0 rcases hspec0 with ⟨q0, e0, steps0, hcall0, hsteps0, he0Lo, he0Hi, hALower, hp0Lo, hp0Hi, hselector0, hshape0⟩ have htuple0 := Result.ok.inj (hcall0.symm.trans hloop0) have hq0 : q0 = d0 := by cases q0 cases d0 simp only [Prod.mk.injEq] at htuple0 simp only [num.imp.dec2flt.decimal_seq.DecimalSeq.mk.injEq] tauto have he0 : e0 = exp0 := congrArg (fun x : Std.Usize × Std.I32 × Bool × Array Std.U8 768#usize × Std.I32 × Option num.imp.dec2flt.common.BiasedFp => x.2.2.2.2.1) htuple0 subst q0 subst e0 rcases hshape0 with ⟨hcap0, hdigit0, hpositive0, hend0⟩ have hshape0Pack : d0.num_digits.val <= 768 /\ (forall q, q ∈ d0.digits.val.take d0.num_digits.val -> q.val < 10) /\ (exists z zs, Digits d0 = z :: zs /\ 1 <= z /\ z < 10 /\ (Digits d0).length = d0.num_digits.val) := ⟨hcap0, hdigit0, by simpa [Digits] using hpositive0⟩ have hend0Pack : exists eta u : Real, A / (2 : Real) ^ exp0.val = Value d0 + eta /\ B / (2 : Real) ^ exp0.val = Value d0 + u /\ 0 <= eta /\ eta < u /\ (d0.truncated = true <-> 0 < eta) := by simpa [Value, Digits] using hend0 have hspec1 := ParserSlowLoop1 A B d0 exp0 hp0Lo hp0Hi he0Lo ⟨hcap0, hdigit0, hpositive0, hend0⟩ dsimp only at hspec1 rcases hspec1 with ⟨q1, e1, steps1, hcall1, hsteps1, he1Hi, hp1, hnum1, hlead1, hlead1Hi, hshape1⟩ have htuple1 := Result.ok.inj (hcall1.symm.trans hloop1) have hq1 : q1 = d1 := by cases q1 cases d1 simp only [Prod.mk.injEq] at htuple1 simp only [num.imp.dec2flt.decimal_seq.DecimalSeq.mk.injEq] tauto have he1 : e1 = exp1 := congrArg (fun x : Std.Usize × Std.I32 × Bool × Array Std.U8 768#usize × Std.I32 × Option num.imp.dec2flt.common.BiasedFp => x.2.2.2.2.1) htuple1 subst q1 subst e1 rcases hshape1 with ⟨hcap1, hdigit1, hpositive1, hend1⟩ have hnorm := ParserSlowReachableNormalizationBounds A B d d0 d1 exp0 exp1 hpLo hpHi hB (by simpa [Digits, Value] using hcanonicalPack) hALower hloop0 hloop1 hp1 hnum1 hlead1 (by simpa [Digits, Value] using hend1) have hspec2 := ParserSlowLoop2 A B d1 exp1 hnorm.2 hp1 hnum1 hlead1 ⟨hcap1, hdigit1, hend1⟩ dsimp only at hspec2 rcases hspec2 with ⟨es, q2, e2, total, hsubS, hloop2S, htotal, hes, he2, htotalEq, hpoint2Lo, hnum2, hshape2⟩ have hesEq : es = expStart := Result.ok.inj (hsubS.symm.trans hsub) subst es have hout2 := Result.ok.inj (hloop2S.symm.trans hloop2) have he2Eq : e2 = exp2 := congrArg Prod.snd hout2 subst e2 have hexp2Floor : -1022 <= exp2.val := by rw [he2] exact le_max_right _ _ have hexp2Norm : exp1.val - 1 <= exp2.val := by rw [he2, hes] exact le_max_left _ _ by_cases hlow : d0.decimal_point.val <= -307 · have he0zero := P856_T1_Loop0NegativeExit A B d d0 0#i32 exp0 hpLo hpHi (by exact ⟨hcap, hdigit, ⟨z, zs, hzshape, hzLo, hzHi, hzlen⟩⟩) (by rcases hcanonicalA with ⟨eta, hAeq, heta0, hetaCut, hsticky⟩ refine ⟨eta, (10 : Real) ^ (d.decimal_point.val - 768), ?_, ?_, heta0, hetaCut, hsticky⟩ · simpa [Value, Digits] using hAeq · simpa [Value, Digits] using hB) hloop0 (by omega) have he0val : exp0.val = 0 := by rw [he0zero]; norm_num omega · have hp0Ord : -306 <= d0.decimal_point.val := by omega rcases hpositive0 with ⟨z0, zs0, hzshape0, hz0Lo, hz0Hi, hzlen0⟩ have hv0 := P804_T1_DecimalValueBounds (Digits d0) z0 zs0 (by simpa [Digits] using hzshape0) hz0Lo (by intro w hw simp only [Digits, List.mem_map] at hw rcases hw with ⟨w8, hw8, rfl⟩ exact hdigit0 w8 hw8) d0.decimal_point.val dsimp only at hv0 rw [hzlen0] at hv0 rcases hend0 with ⟨eta0, u0, hA0, hB0, heta0, heta0u0, hsticky0⟩ have hAB : A < B := by rcases hcanonicalA with ⟨eta, hAeq, hetaLo, hetaHi, hsticky⟩ have hcutPos : 0 < (10 : Real) ^ (d.decimal_point.val - 768) := zpow_pos (by norm_num) _ rw [hAeq, hB] linarith have hpow0Pos : 0 < (2 : Real) ^ exp0.val := zpow_pos (by norm_num) _ have hpow1Pos : 0 < (2 : Real) ^ exp1.val := zpow_pos (by norm_num) _ have hBpow : B <= (2 : Real) ^ exp1.val := by have hm := (div_le_iff₀ hpow1Pos).mp hnorm.1 simpa using hm have hv0A : Value d0 <= A / (2 : Real) ^ exp0.val := by rw [hA0] linarith have hAdivB : A / (2 : Real) ^ exp0.val < B / (2 : Real) ^ exp0.val := by exact (div_lt_div_iff_of_pos_right hpow0Pos).2 hAB have hBdivPow : B / (2 : Real) ^ exp0.val <= (2 : Real) ^ (exp1.val - exp0.val) := by calc B / (2 : Real) ^ exp0.val <= (2 : Real) ^ exp1.val / (2 : Real) ^ exp0.val := div_le_div_of_nonneg_right hBpow hpow0Pos.le _ = (2 : Real) ^ (exp1.val - exp0.val) := by rw [zpow_sub₀ (by norm_num : (2 : Real) ≠ 0)] have hdecimalBinary : (10 : Real) ^ (d0.decimal_point.val - 1) < (2 : Real) ^ (exp1.val - exp0.val) := by have hzReal : (1 : Real) <= z0 := by exact_mod_cast hz0Lo have hpow10Pos : 0 < (10 : Real) ^ (d0.decimal_point.val - 1) := zpow_pos (by norm_num) _ have hleadLower : (10 : Real) ^ (d0.decimal_point.val - 1) <= (z0 : Real) * (10 : Real) ^ (d0.decimal_point.val - 1) := by nlinarith have hv0Lower : (z0 : Real) * (10 : Real) ^ (d0.decimal_point.val - 1) <= Value d0 := by simpa [Value, Digits] using hv0.1 exact lt_of_le_of_lt (hleadLower.trans (hv0Lower.trans hv0A)) (hAdivB.trans_le hBdivPow) have hpBound := P856_T1_DecimalBinaryExponentBound d0.decimal_point.val (exp1.val - exp0.val) hp0Ord hp0Hi hdecimalBinary omega end dec2flt_model