import Tablet.P529_T1_RoundArbitraryEndpoint import Tablet.P529_T1_RightBoundaryTransport import Tablet.P495_T1_RightShiftCanonical import Tablet.P472_T1_DecimalRightShiftLoop0Execution import Tablet.P856_T1_CoefficientGridAlignment open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P529_T1_CarryRoundExecution] theorem P529_T1_CarryRoundExecution (X Y : Real) (d3 : num.imp.dec2flt.decimal_seq.DecimalSeq) (m : Std.U64) (hcap : d3.num_digits.val <= 768) (hdigits : forall b, b ∈ d3.digits.val.take d3.num_digits.val -> b.val < 10) (hshape : exists h tail, (d3.digits.val.take d3.num_digits.val).map (fun b => b.val) = h :: tail /\ 1 <= h /\ h < 10 /\ (h :: tail).length = d3.num_digits.val) (htrim : d3.digits.val[d3.num_digits.val - 1]? ≠ some 0#u8) (hpoint : d3.decimal_point.val < 19) (hXlo : 0 <= X) (hXhi : X < (2 : Real) ^ (53 : Nat)) (hround : num.imp.dec2flt.decimal_seq.DecimalSeq.round d3 = ok m) (hcarry : 2 ^ 53 <= m.val) : 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)) (exists eta u : Real, X = V d3 + eta /\ Y = V d3 + u /\ 0 <= eta /\ eta < u /\ (d3.truncated = true <-> 0 < eta)) -> (forall n : Nat, (2 * n + 1 : Nat) <= V d3 \/ Y <= (2 * n + 1 : Nat)) -> exists (d4 : num.imp.dec2flt.decimal_seq.DecimalSeq) (m4 : Std.U64), num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d3 1#usize = ok d4 /\ d4.decimal_point.val <= d3.decimal_point.val /\ d4.decimal_point.val < 19 /\ d4.num_digits.val <= 768 /\ (forall b, b ∈ d4.digits.val.take d4.num_digits.val -> b.val < 10) /\ (exists h tail, (d4.digits.val.take d4.num_digits.val).map (fun b => b.val) = h :: tail /\ 1 <= h /\ h < 10 /\ (h :: tail).length = d4.num_digits.val) /\ d4.digits.val[d4.num_digits.val - 1]? ≠ some 0#u8 /\ (exists eta4 u4 : Real, X / 2 = V d4 + eta4 /\ Y / 2 = V d4 + u4 /\ 0 <= eta4 /\ eta4 < u4 /\ (d4.truncated = true <-> 0 < eta4)) /\ (forall n : Nat, (n : Real) + 1 / 2 <= V d4 \/ Y / 2 <= (n : Real) + 1 / 2) /\ num.imp.dec2flt.decimal_seq.DecimalSeq.round d4 = ok m4 /\ X / 2 <= (m4.val : Real) + 1 / 2 /\ (m4.val : Real) <= X / 2 + 1 / 2 /\ (X / 2 = (m4.val : Real) + 1 / 2 \/ X / 2 = (m4.val : Real) - 1 / 2 -> m4.val % 2 = 0) := by -- BODY intro hends hendpoints hsep have hpointLo : 0 <= d3.decimal_point.val := by by_contra hneg have hnegval : d3.decimal_point.val < 0 := by omega have hneg' : d3.decimal_point < 0#i32 := by simpa [UScalar.lt_equiv] using hnegval have hmzero : m.val = 0 := by have hm : 0#u64 = m := by simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.round, hneg'] using hround simpa using congrArg UScalar.val hm omega have hinput : d3.num_digits.val <= 768 /\ (forall z, z ∈ d3.digits.val.take d3.num_digits.val -> z.val < 10) /\ (exists z zs, (d3.digits.val.take d3.num_digits.val).map (fun a => a.val) = z :: zs /\ 1 <= z /\ z < 10 /\ (z :: zs).length = d3.num_digits.val) := by exact ⟨hcap, hdigits, hshape⟩ obtain ⟨read, acc, d4, hloop, hreadLo, hreadHi, haccLo, haccHi, hprospectiveLo, hprospectiveHi, hshift, hp4, hnonzero⟩ := P472_T1_DecimalRightShiftLoop0Execution d3 1#usize (by decide) hpointLo (by omega) hinput have hpointMono : d4.decimal_point.val <= d3.decimal_point.val := by omega have hnum4 : d4.num_digits.val ≠ 0 := by simpa using hnonzero have hshapeOut := P495_T1_RightShiftCanonical X Y d3 d4 1#usize (by decide) hcap hdigits hshape hshift (by exact hnum4) hendpoints dsimp only at hshapeOut rcases hshapeOut with ⟨_, hcap4, hdigits4, hshape4, htrim4, hends4⟩ have hpoint4 : d4.decimal_point.val < 19 := by omega have hshiftNonreset : ¬ (d4.num_digits.val = 0 /\ d4.decimal_point.val = 0 /\ d4.truncated = false) := by intro h exact hnum4 h.1 have hvalue : 0 < hends d3 := by obtain ⟨head, tail, hmap, hhead, hheadlt, hlen⟩ := hshape have hfoldpos : 0 < (head :: tail).foldl (fun n z => 10 * n + z) 0 := by have htail (ys : List Nat) (a : Nat) : a ≤ ys.foldl (fun n z => 10 * n + z) a := by induction ys generalizing a with | nil => simp | cons b ys ih => simp only [List.foldl_cons] have hi := ih (10 * a + b) omega have hh := htail tail head simp only [List.foldl_cons, Nat.zero_mul, Nat.zero_add] have hp : 0 < head := by omega exact lt_of_lt_of_le hp (by simpa using hh) have hvalpos : 0 < (((head :: tail).foldl (fun n z => 10 * n + z) 0 : Nat) : Real) := by exact_mod_cast hfoldpos have hfold : (((d3.digits.val.take d3.num_digits.val).map (fun b => b.val)).foldl (fun n z => 10 * n + z) 0 : Nat) = (head :: tail).foldl (fun n z => 10 * n + z) 0 := by rw [hmap] change 0 < (((d3.digits.val.take d3.num_digits.val).map (fun b => b.val)).foldl (fun n z => 10 * n + z) 0 : Nat) * (10 : Real) ^ _ rw [hfold] exact mul_pos hvalpos (zpow_pos (by norm_num) _) have hOdd : forall n : Nat, (n : Real) + 1 / 2 <= ((((d4.digits.val.take d4.num_digits.val).map (fun b => b.val)).foldl (fun n z => 10 * n + z) 0 : Nat) : Real) * (10 : Real) ^ (d4.decimal_point.val - (d4.num_digits.val : Int)) \/ Y / 2 <= (n : Real) + 1 / 2 := by intro n have hsource := hsep n rcases hsource with hleft | hright · have halign := P856_T1_CoefficientGridAlignment (2 * n + 1) (-1) d4.decimal_point.val (by omega) (by omega) rcases halign with ⟨b, hb⟩ have hbval : (b : Real) * (10 : Real) ^ (d4.decimal_point.val - 768) = (2 * n + 1 : Real) / 2 := by rw [← hb] norm_num [div_eq_mul_inv] have hbound : (b : Real) * (10 : Real) ^ (d4.decimal_point.val - 768) <= hends d3 / (2 : Real) ^ 1 := by rw [hbval] norm_num exact (div_le_div_of_nonneg_right (by exact_mod_cast hleft) (by norm_num)) have htrans := P529_T1_RightBoundaryTransport d3 d4 1#usize (by decide) hdigits hshift hshiftNonreset hcap4 hvalue b (by simpa [hends] using hbound) left rw [hbval] at htrans norm_num at htrans ⊢ nlinarith [htrans] · right have : (Y : Real) <= (2 * n + 1 : Nat) := hright have hdiv : Y / 2 <= ((2 * n + 1 : Nat) : Real) / 2 := by exact (div_le_div_of_nonneg_right (by exact_mod_cast this) (by norm_num)) have hhalf : ((2 * n + 1 : Nat) : Real) / 2 = (n : Real) + 1 / 2 := by push_cast ring rw [hhalf] at hdiv norm_num at hdiv ⊢ exact hdiv have hendpoints4 : ∃ eta4 u4 : Real, X / 2 = hends d4 + eta4 /\ Y / 2 = hends d4 + u4 /\ 0 <= eta4 /\ eta4 < u4 /\ (d4.truncated = true <-> 0 < eta4) := by simpa [hends] using hends4 have hround4 := P529_T1_RoundArbitraryEndpoint d4 (X / 2) (Y / 2) hcap4 hdigits4 (Or.inr ⟨hshape4, htrim4⟩) hpoint4 (by nlinarith [hXlo]) (by norm_num; nlinarith [hXhi]) hendpoints4 hOdd rcases hround4 with ⟨m4, hroundCall4, hnearLo, hnearHi, hparity⟩ refine ⟨d4, m4, hshift, hpointMono, hpoint4, hcap4, hdigits4, hshape4, htrim4, ?_, hOdd, hroundCall4, hnearLo, hnearHi, hparity⟩ rcases hendpoints4 with ⟨eta4, u4, hX4, hY4, heta0, hetalt, hsticky4⟩ exact ⟨eta4, u4, hX4, hY4, heta0, hetalt, hsticky4⟩ end dec2flt_model