import Tablet.P529_T1_ReachableCapacity import Tablet.P495_T1_SlowLoop0Execution import Tablet.P529_T1_Loop1Execution import Tablet.P529_T1_NormalizationBounds import Tablet.P529_T1_Loop2Execution import Tablet.P529_T1_FinalShiftCertificate import Tablet.P856_T1_CoefficientGridAlignment import Tablet.P472_T1_DecimalValueBounds import Tablet.P529_T1_LeftHistoryBoundary import Tablet.P529_T1_RightHistoryBoundary import Tablet.P529_T1_RightBoundaryTransport import Tablet.P798_T1_LeftBoundaryTransport import Tablet.P798_T1_GridAlignment open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P529_T1_SeparationPackage] theorem P529_T1_SeparationPackage (X Y : Real) (d d0 d1 d2 d3 : num.imp.dec2flt.decimal_seq.DecimalSeq) (exp0 exp1 exps exp2 : Std.I32) (hpointLo : -324 <= d.decimal_point.val) (hpointHi : d.decimal_point.val < 310) (hinput : d.num_digits.val <= 768 /\ (forall b, b ∈ d.digits.val.take d.num_digits.val -> b.val < 10) /\ (exists h tail, (d.digits.val.take d.num_digits.val).map (fun b => b.val) = h :: tail /\ 1 <= h /\ h < 10 /\ (h :: tail).length = d.num_digits.val)) (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)) (hdecrement : exp1 - 1#i32 = ok exps) (hloop2 : num.imp.dec2flt.slow.parse_long_mantissa_loop2 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 exps = ok (d2, exp2)) (hshift : num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d2 53#usize = ok d3) : let V := fun a : num.imp.dec2flt.decimal_seq.DecimalSeq => ((((a.digits.val.take a.num_digits.val).map (fun b => b.val)).foldl (fun n z => 10 * n + z) 0 : Nat) : Real) * (10 : Real) ^ (a.decimal_point.val - (a.num_digits.val : Int)) Y = V d + (10 : Real) ^ (d.decimal_point.val - 768) -> (exists eta : Real, X = V d + eta /\ 0 <= eta /\ eta < (10 : Real) ^ (d.decimal_point.val - 768) /\ (d.truncated = true <-> 0 < eta)) -> (forall n : Nat, (n : Real) + 1 / 2 <= V d3 \/ Y / (2 : Real) ^ (exp2.val - 52) <= (n : Real) + 1 / 2) /\ (forall n : Nat, (2 * n + 1 : Nat) <= V d3 \/ Y / (2 : Real) ^ (exp2.val - 52) <= (2 * n + 1 : Nat)) := by -- BODY intro V hY hX have h0pkg := P495_T1_SlowLoop0Execution X Y d hpointLo hpointHi hinput hY hX rcases h0pkg with ⟨d0', e0', steps, h0', hsteps, hstepsLo, hstepsHi, hXlow, hp0lo, hp0hi, hn0, hd0, hshape0, htrim0, herr0⟩ have h0eq := h0'.symm.trans hloop0 injection h0eq with h0tuple simp only [Prod.mk.injEq] at h0tuple rcases h0tuple with ⟨h0num, h0point, h0trunc, h0digits, h0exp, _⟩ have h0record : d0' = d0 := by cases d0' with | mk n p t ds => cases d0 with | mk n' p' t' ds' => simp_all cases h0record cases h0exp have h1pkg := P529_T1_Loop1Execution X Y d0 exp0 hp0lo hp0hi hstepsLo (by exact ⟨hn0, hd0, hshape0⟩) herr0 rcases h1pkg with ⟨d1', e1', bound, h1', hbound, hboundlo, hboundhi, hp1, hn1, hhead1lo, hhead1hi, hn1cap, hd1, hshape1, herr1⟩ have h1eq := h1'.symm.trans hloop1 injection h1eq with h1tuple simp only [Prod.mk.injEq] at h1tuple rcases h1tuple with ⟨h1num, h1point, h1trunc, h1digits, h1exp, _⟩ have h1record : d1' = d1 := by cases d1' with | mk n p t ds => cases d1 with | mk n' p' t' ds' => simp_all cases h1record cases h1exp have hnorm := P529_T1_NormalizationBounds X Y d d0 d1 exp0 exp1 hpointLo hpointHi hinput hloop0 hloop1 hY hX rcases hnorm with ⟨hXlow', hXY, hYbound1, hexp1lo, hexp1hi⟩ have h2pkg := P529_T1_Loop2Execution X Y d1 exp1 hexp1lo hp1 (by exact ⟨hn1cap, hd1, hshape1⟩) herr1 rcases h2pkg with ⟨exps', exp2', d2', shift, hdec', hexpsval, hexpslo, h2', hexp2val, hshiftval, hshiftle, h2case, hp2lo, hp2hi, hn2, hd2, hshape2, hnonreset, herr2⟩ have hdeceq := hdec'.symm.trans hdecrement injection hdeceq with hexps cases hexps have h2eq := h2'.symm.trans hloop2 injection h2eq with h2tuple simp only [Prod.mk.injEq] at h2tuple rcases h2tuple with ⟨h2record, h2exp⟩ have h2record' : d2' = d2 := by cases d2' with | mk n p t ds => cases d2 with | mk n' p' t' ds' => simp_all cases h2record' cases h2exp have hexp2 : exp2.val = max (exp1.val - 1) (-1022) := by rw [← hexpsval] exact hexp2val have hcap0 := P529_T1_ReachableCapacity X Y d d0 d1 exp0 exp1 exp2 hpointLo hpointHi hinput hloop0 hloop1 hexp2 hY hX have he2lower : exp1.val ≤ exp2.val + 1 := by rw [hexp2] omega have hYle : Y ≤ (2 : Real) ^ exp1.val := (div_le_one (by positivity)).mp hYbound1 have hpowmon : (2 : Real) ^ exp1.val ≤ (2 : Real) ^ (exp2.val + 1) := zpow_le_zpow_right₀ (by norm_num) he2lower have hYupper : Y / (2 : Real) ^ (exp2.val + 1) ≤ 1 := (div_le_one (by positivity)).2 (le_trans hYle hpowmon) have h3pkg := P529_T1_FinalShiftCertificate X Y d2 exp2 hp2lo hp2hi (by exact ⟨hn2, hd2, hshape2⟩) hYupper herr2 rcases h3pkg with ⟨d3', h3', hp3lo, hp3hi, hn3, hd3, hshape3, htrim3, hXpos3, hXupper3, herr3⟩ have h3eq := h3'.symm.trans hshift injection h3eq with h3 have h3record : d3' = d3 := by cases d3' with | mk n p t ds => cases d3 with | mk n' p' t' ds' => simp_all cases h3record have hleftHist := P529_T1_LeftHistoryBoundary X Y d0 d1 exp0 exp1 hp0lo hp0hi hstepsLo ⟨hn0, hd0, hshape0⟩ hloop1 herr0 rcases hleftHist with ⟨hbasecap, hprogress, hleftBoundary⟩ have hrightHist := P529_T1_RightHistoryBoundary X Y d d0 exp0 hpointLo hpointHi hinput hloop0 hY hX rcases hrightHist with ⟨hrightZero, hrightBoundary⟩ rcases hshape1 with ⟨z1, zs1, hxs1, hz1, hz1lt, hlen1⟩ have hdmap1 : ∀ q, q ∈ (d1.digits.val.take d1.num_digits.val).map (fun b => b.val) -> q < 10 := by intro q hq rcases List.mem_map.mp hq with ⟨b, hb, rfl⟩ exact hd1 b hb have hvbounds1 := P472_T1_DecimalValueBounds ((d1.digits.val.take d1.num_digits.val).map (fun b => b.val)) z1 zs1 hxs1 (by omega) hdmap1 d1.decimal_point.val have htake1 : (d1.digits.val.take d1.num_digits.val).map (fun b => b.val) = (d1.digits.val.map (fun b => b.val)).take d1.num_digits.val := by simp have hv1 : 0 < V d1 := by have hz1pos : (0 : Real) < z1 := by exact_mod_cast (show 0 < z1 by omega) have hpow : (0 : Real) < (10 : Real) ^ (d1.decimal_point.val - 1) := zpow_pos (by norm_num) _ have hlow : (z1 : Real) * (10 : Real) ^ (d1.decimal_point.val - 1) ≤ V d1 := by simpa [V, ← htake1, hxs1, hlen1] using hvbounds1.1 exact lt_of_lt_of_le (mul_pos hz1pos hpow) hlow rcases hshape2 with ⟨z2, zs2, hxs2, hz2, hz2lt, hlen2⟩ have hdmap2 : ∀ q, q ∈ (d2.digits.val.take d2.num_digits.val).map (fun b => b.val) -> q < 10 := by intro q hq rcases List.mem_map.mp hq with ⟨b, hb, rfl⟩ exact hd2 b hb have hvbounds2 := P472_T1_DecimalValueBounds ((d2.digits.val.take d2.num_digits.val).map (fun b => b.val)) z2 zs2 hxs2 (by omega) hdmap2 d2.decimal_point.val have htake2 : (d2.digits.val.take d2.num_digits.val).map (fun b => b.val) = (d2.digits.val.map (fun b => b.val)).take d2.num_digits.val := by simp have hv2 : 0 < V d2 := by have hz2pos : (0 : Real) < z2 := by exact_mod_cast (show 0 < z2 by omega) have hpow : (0 : Real) < (10 : Real) ^ (d2.decimal_point.val - 1) := zpow_pos (by norm_num) _ have hlow : (z2 : Real) * (10 : Real) ^ (d2.decimal_point.val - 1) ≤ V d2 := by simpa [V, ← htake2, hxs2, hlen2] using hvbounds2.1 exact lt_of_lt_of_le (mul_pos hz2pos hpow) hlow have hcapacity0 (R : Int) (hR : R = exp2.val - 53 ∨ R = exp2.val - 52) : 0 ≤ R - exp0.val + 768 - d0.decimal_point.val := by have h := hcap0 rcases hR with hR | hR <;> rw [hR] <;> omega have hcapacity2 (R : Int) (hR : R = exp2.val - 53 ∨ R = exp2.val - 52) : 0 ≤ R - (exp2.val + 1) + 768 - d2.decimal_point.val := by rcases hR with hR | hR <;> rw [hR] <;> omega have hcapacity3 (R : Int) (hR : R = exp2.val - 53 ∨ R = exp2.val - 52) : 0 ≤ R - (exp2.val - 52) + 768 - d3.decimal_point.val := by rcases hR with hR | hR <;> rw [hR] <;> omega have hnoncross (c : Nat) (R : Int) (hR : R = exp2.val - 53 ∨ R = exp2.val - 52) : V d3 < (c : Real) * (2 : Real) ^ (R - (exp2.val - 52)) -> (c : Real) * (2 : Real) ^ R < Y -> False := by intro hcross3 hcrossY have hcap2 := hcapacity2 R hR have hcap3 := hcapacity3 R hR have halign2 := P856_T1_CoefficientGridAlignment c (R - (exp2.val + 1)) d2.decimal_point.val (by omega) hcap2 rcases halign2 with ⟨b2, hb2⟩ have halign3 := P856_T1_CoefficientGridAlignment c (R - (exp2.val - 52)) d3.decimal_point.val (by omega) hcap3 rcases halign3 with ⟨b3, hb3⟩ have hpow53 : (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (R - (exp2.val + 1)) = (2 : Real) ^ (R - (exp2.val - 52)) := by have hnat : (2 : Real) ^ (53 : Nat) = (2 : Real) ^ (53 : Int) := by norm_num rw [hnat, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] congr 1 omega have hcoefPower : (c : Real) * (2 : Real) ^ (R - (exp2.val - 52)) = (2 : Real) ^ (53 : Nat) * ((c : Real) * (2 : Real) ^ (R - (exp2.val + 1))) := by have hnatpow : (2 : Real) ^ (53 : Nat) = (2 : Real) ^ (53 : Int) := by norm_num rw [hnatpow] rw [show R - (exp2.val - 52) = (53 : Int) + (R - (exp2.val + 1)) by omega] rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] ring have hshifttransport := P798_T1_LeftBoundaryTransport d2 d3 53#usize b3 (by norm_num) hv2 hd2 hshift hn3 have hcross2 : V d2 < (c : Real) * (2 : Real) ^ (R - (exp2.val + 1)) := by by_contra hnot have hle : (c : Real) * (2 : Real) ^ (R - (exp2.val + 1)) ≤ V d2 := le_of_not_gt hnot have hmul := mul_le_mul_of_nonneg_left hle (le_of_lt (show (0 : Real) < (2 : Real) ^ (53 : Nat) by positivity)) have htransportIn : (b3 : Real) * (10 : Real) ^ (d3.decimal_point.val - 768) ≤ (2 : Real) ^ (53 : Nat) * V d2 := by calc (b3 : Real) * (10 : Real) ^ (d3.decimal_point.val - 768) = (c : Real) * (2 : Real) ^ (R - (exp2.val - 52)) := hb3.symm _ = (2 : Real) ^ (53 : Nat) * ((c : Real) * (2 : Real) ^ (R - (exp2.val + 1))) := by exact hcoefPower _ ≤ (2 : Real) ^ (53 : Nat) * V d2 := hmul have htransportOut := hshifttransport htransportIn rw [← hb3] at htransportOut exact (not_lt_of_ge htransportOut) hcross3 have hloop2Exponent : exp2.val + 1 = exp1.val + shift.val := by rcases h2case with hident | hright · rcases hident with ⟨hcut, hzero, hd2eq, he2eq⟩ have he2 : exp2.val = exps.val := congrArg (fun e : Std.I32 => e.val) he2eq omega · rcases hright with ⟨hcut, hpositive, hcall, hexp2reset⟩ omega have hcross1 : V d1 < (c : Real) * (2 : Real) ^ (R - exp1.val) := by by_contra hnot have hle : (c : Real) * (2 : Real) ^ (R - exp1.val) ≤ V d1 := le_of_not_gt hnot rcases h2case with hident | hright · rcases hident with ⟨hcut, hzero, hd2eq, he2eq⟩ have he2 : exp2.val = exps.val := congrArg (fun e : Std.I32 => e.val) he2eq have hexp : exp2.val + 1 = exp1.val := by omega rw [hd2eq] at hcross2 rw [show R - (exp2.val + 1) = R - exp1.val by omega] at hcross2 exact (not_lt_of_ge hle) hcross2 · rcases hright with ⟨hcut, hpositive, hcall, hexp2reset⟩ have hnonreset2 : ¬ (d2.num_digits.val = 0 ∧ d2.decimal_point.val = 0 ∧ d2.truncated = false) := by intro hreset have hn2pos : 0 < d2.num_digits.val := by rw [← hlen2] simp omega have hrighttransport := P529_T1_RightBoundaryTransport d1 d2 shift (by omega) hd1 hcall hnonreset2 hn2 hv1 have hshiftpowpos : (0 : Real) < (2 : Real) ^ shift.val := by positivity have hnatpow : (2 : Real) ^ shift.val = (2 : Real) ^ (shift.val : Int) := by norm_num [zpow_natCast] have hpowRel : (2 : Real) ^ (R - exp1.val) = (2 : Real) ^ (R - (exp2.val + 1)) * (2 : Real) ^ shift.val := by rw [show R - exp1.val = (R - (exp2.val + 1)) + (shift.val : Int) by omega] rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] rw [← hnatpow] have hmulBound : (c : Real) * (2 : Real) ^ (R - (exp2.val + 1)) * (2 : Real) ^ shift.val ≤ V d1 := by calc (c : Real) * (2 : Real) ^ (R - (exp2.val + 1)) * (2 : Real) ^ shift.val = (c : Real) * ((2 : Real) ^ (R - (exp2.val + 1)) * (2 : Real) ^ shift.val) := by ring _ = (c : Real) * (2 : Real) ^ (R - exp1.val) := by rw [← hpowRel] _ ≤ V d1 := hle have htransportIn : (b2 : Real) * (10 : Real) ^ (d2.decimal_point.val - 768) ≤ V d1 / (2 : Real) ^ shift.val := by rw [← hb2] exact (le_div_iff₀ hshiftpowpos).2 (by simpa [mul_assoc] using hmulBound) have htransportOut := hrighttransport b2 htransportIn rw [← hb2] at htransportOut exact (not_lt_of_ge htransportOut) hcross2 have hleftStep := hleftBoundary c R (hcapacity0 R hR) have hcross0 := hleftStep.2 hcross1 have hrightStep := hrightBoundary c R (hcapacity0 R hR) have hcrossd := hrightStep.2 hcross0 have hgrid := P798_T1_GridAlignment d hinput.1 rcases hgrid with ⟨a, ha⟩ have halign0 := P856_T1_CoefficientGridAlignment c R d.decimal_point.val (by omega) hrightStep.1 rcases halign0 with ⟨b, hb⟩ let q : Real := (10 : Real) ^ (d.decimal_point.val - 768) have htakeD : (d.digits.val.take d.num_digits.val).map (fun z => z.val) = (d.digits.val.map (fun z => z.val)).take d.num_digits.val := by simp have hq : 0 < q := by dsimp [q] exact zpow_pos (by norm_num) _ have ha' : V d = (a : Real) * q := by simpa only [V, q, htakeD] using ha have hb' : (c : Real) * (2 : Real) ^ R = (b : Real) * q := by simpa [q] using hb have hcrossd' : (a : Real) * q < (c : Real) * (2 : Real) ^ R := by rw [← ha'] exact hcrossd have habreal : (a : Real) * q < (b : Real) * q := by rw [← hb'] exact hcrossd' have hab : a < b := by exact_mod_cast (lt_of_mul_lt_mul_right habreal (le_of_lt hq)) have hbaReal : (b : Real) * q < ((a : Nat) + 1 : Real) * q := by calc (b : Real) * q = (c : Real) * (2 : Real) ^ R := hb'.symm _ < Y := hcrossY _ = V d + q := by simpa [q] using hY _ = (a : Real) * q + q := by rw [ha'] _ = ((a : Nat) + 1 : Real) * q := by push_cast; ring have hba : b < a + 1 := by exact_mod_cast (lt_of_mul_lt_mul_right hbaReal (le_of_lt hq)) omega have hpowFactor (r s : Int) : (2 : Real) ^ r = (2 : Real) ^ (r - s) * (2 : Real) ^ s := by calc (2 : Real) ^ r = (2 : Real) ^ ((r - s) + s) := by congr 1 <;> omega _ = (2 : Real) ^ (r - s) * (2 : Real) ^ s := zpow_add₀ (by norm_num : (2 : Real) ≠ 0) _ _ constructor · intro n by_contra hbad push_neg at hbad rcases hbad with ⟨hleft, hright⟩ let c : Nat := 2 * n + 1 let R : Int := exp2.val - 53 let s : Int := exp2.val - 52 have hboundary : (n : Real) + 1 / 2 = (c : Real) * (2 : Real) ^ (R - s) := by have hexp : R - s = -1 := by dsimp [R, s]; omega rw [hexp] have hpow : (2 : Real) ^ (-1 : Int) = (1 / 2 : Real) := by norm_num rw [hpow] dsimp [c] push_cast ring have hupperMul : ((n : Real) + 1 / 2) * (2 : Real) ^ s < Y := (lt_div_iff₀ (zpow_pos (by norm_num) s)).mp hright have hcrossY : (c : Real) * (2 : Real) ^ R < Y := by calc (c : Real) * (2 : Real) ^ R = ((c : Real) * (2 : Real) ^ (R - s)) * (2 : Real) ^ s := by conv_lhs => rw [hpowFactor R s] ring _ = ((n : Real) + 1 / 2) * (2 : Real) ^ s := by rw [← hboundary] _ < Y := hupperMul have hR : R = exp2.val - 53 ∨ R = exp2.val - 52 := Or.inl rfl have hcross3 : V d3 < (c : Real) * (2 : Real) ^ (R - s) := by rw [← hboundary] exact hleft exact hnoncross c R hR hcross3 hcrossY · intro n by_contra hbad push_neg at hbad rcases hbad with ⟨hleft, hright⟩ let c : Nat := 2 * n + 1 let R : Int := exp2.val - 52 let s : Int := exp2.val - 52 have hboundary : (2 * n + 1 : Nat) = (c : Real) * (2 : Real) ^ (R - s) := by have hexp : R - s = 0 := by dsimp [R, s]; omega rw [hexp] simp [c] have hupperMul : ((2 * n + 1 : Nat) : Real) * (2 : Real) ^ s < Y := (lt_div_iff₀ (zpow_pos (by norm_num) s)).mp hright have hcrossY : (c : Real) * (2 : Real) ^ R < Y := by calc (c : Real) * (2 : Real) ^ R = ((c : Real) * (2 : Real) ^ (R - s)) * (2 : Real) ^ s := by conv_lhs => rw [hpowFactor R s] ring _ = ((2 * n + 1 : Nat) : Real) * (2 : Real) ^ s := by rw [← hboundary] _ < Y := hupperMul have hR : R = exp2.val - 53 ∨ R = exp2.val - 52 := Or.inr rfl have hcross3 : V d3 < (c : Real) * (2 : Real) ^ (R - s) := by rw [← hboundary] exact hleft exact hnoncross c R hR hcross3 hcrossY end dec2flt_model