import Tablet.ParserSlowLoop2 import Tablet.P856_T1_RightPrefixReadBound import Tablet.DecimalRightShiftLoop0Execution import Tablet.DecimalLeftShiftLoop1Execution import Tablet.DecimalSeqLeftShiftCanonical import Tablet.DecimalLeftShiftDigitGrowth import Tablet.P804_T1_LeftShiftPointGrowth import Tablet.ParserExponentConstants open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P856_T1_FinalLeftShiftExecution] theorem P856_T1_FinalLeftShiftExecution (A B : Real) (d1 d2 : num.imp.dec2flt.decimal_seq.DecimalSeq) (exp1 expStart exp2 : Std.I32) (hexpLower : -1079 <= exp1.val) (hpoint1 : d1.decimal_point.val = 0) (hnum1 : d1.num_digits.val != 0) (hlead1 : 5 <= d1.digits.val[0].val) (hinput : let digits1 := (d1.digits.val.take d1.num_digits.val).map (fun z => z.val) let integer1 : Nat := digits1.foldl (fun n digit => 10 * n + digit) 0 let value1 : Real := integer1 * (10 : Real) ^ (d1.decimal_point.val - (d1.num_digits.val : Int)) d1.num_digits.val <= 768 /\ (forall z, z ∈ d1.digits.val.take d1.num_digits.val -> z.val < 10) /\ (exists z zs, digits1 = z :: zs /\ 1 <= z /\ z < 10 /\ digits1.length = d1.num_digits.val) /\ (exists eta1 u1 : Real, A / (2 : Real) ^ exp1.val = value1 + eta1 /\ B / (2 : Real) ^ exp1.val = value1 + u1 /\ 0 <= eta1 /\ eta1 < u1 /\ (d1.truncated = true <-> 0 < eta1))) (hsub : exp1 - 1#i32 = ok expStart) (hloop : num.imp.dec2flt.slow.parse_long_mantissa_loop2 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 expStart = ok (d2, exp2)) : exists d3 : num.imp.dec2flt.decimal_seq.DecimalSeq, -17 <= d2.decimal_point.val /\ d2.decimal_point.val <= 0 /\ ((d2 = d1 /\ exp2.val + 1 = exp1.val) \/ (exists shift : Std.Usize, shift.val <= 58 /\ num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d1 shift = ok d2 /\ exp2.val + 1 = exp1.val + (shift.val : Int))) /\ num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d2 53#usize = ok d3 /\ (let digits3 := (d3.digits.val.take d3.num_digits.val).map (fun z => z.val) let integer3 : Nat := digits3.foldl (fun n digit => 10 * n + digit) 0 let value3 : Real := integer3 * (10 : Real) ^ (d3.decimal_point.val - (d3.num_digits.val : Int)) d3.num_digits.val <= 768 /\ (forall z, z ∈ d3.digits.val.take d3.num_digits.val -> z.val < 10) /\ (exists z zs, digits3 = z :: zs /\ 1 <= z /\ z < 10 /\ digits3.length = d3.num_digits.val /\ digits3.getLast? != some 0) /\ (exists eta3 u3 : Real, A / (2 : Real) ^ (exp2.val - 52) = value3 + eta3 /\ B / (2 : Real) ^ (exp2.val - 52) = value3 + u3 /\ 0 <= eta3 /\ eta3 < u3 /\ (d3.truncated = true <-> 0 < eta3)) /\ d3.decimal_point.val < 19) := by -- BODY dsimp only at hinput rcases hinput with ⟨hcap1, hdigit1, hpositive1, hend1⟩ have hspec := ParserSlowLoop2 A B d1 exp1 hexpLower hpoint1 hnum1 hlead1 ⟨hcap1, hdigit1, hend1⟩ dsimp only at hspec rcases hspec with ⟨es, q2, e2, total, hsubS, hloopS, htotal, hes, he2, htotalEq, hpoint2Lo, hnum2, hshape2⟩ have hesEq : es = expStart := Result.ok.inj (hsubS.symm.trans hsub) subst es have houtEq := Result.ok.inj (hloopS.symm.trans hloop) have hq2 : q2 = d2 := congrArg Prod.fst houtEq have he2Eq : e2 = exp2 := congrArg Prod.snd houtEq subst q2 subst e2 rcases hshape2 with ⟨hcap2, hdigit2, hpositive2, hend2⟩ have hstartVal : expStart.val = exp1.val - 1 := by have hs := IScalar.sub_equiv exp1 1#i32 rw [hsub] at hs simpa using hs.2.1 have hpoint2 : (-17 <= d2.decimal_point.val /\ d2.decimal_point.val <= 0) /\ ((d2 = d1 /\ exp2.val + 1 = exp1.val) \/ (exists shift : Std.Usize, shift.val <= 58 /\ num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d1 shift = ok d2 /\ exp2.val + 1 = exp1.val + (shift.val : Int))) := by rcases ParserExponentConstants with ⟨hbits, htotalBits, hsig, hexpbits, hexpsat, hinf, hbias, hmin, hsmall, hlarge, hroundmin, hroundmax⟩ by_cases hstartHigh : -1022 <= expStart.val · have hbody : num.imp.dec2flt.slow.parse_long_mantissa_loop2.body F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 expStart = ok (done (d1, expStart)) := by unfold num.imp.dec2flt.slow.parse_long_mantissa_loop2.body rw [hmin] simp only [bind_tc_ok] have hnot : ¬ ((-1022)#i32 : Std.I32) > expStart := by change ¬ (-1022 : Int) > expStart.val omega simp [hnot] have hconstructed : num.imp.dec2flt.slow.parse_long_mantissa_loop2 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 expStart = ok (d1, expStart) := by unfold num.imp.dec2flt.slow.parse_long_mantissa_loop2 rw [Aeneas.Std.loop.eq_1] simpa only [hbody] have heq := Result.ok.inj (hconstructed.symm.trans hloop) have hd : d1 = d2 := congrArg Prod.fst heq have he : expStart = exp2 := congrArg Prod.snd heq constructor · rw [← hd, hpoint1] omega · left exact ⟨hd.symm, by rw [← he]; omega⟩ · have hstartLow : expStart.val < -1022 := by omega obtain ⟨diff, hdiff, hdiffSpec⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.sub_spec (x := ((-1022)#i32 : Std.I32)) (y := expStart) (by rw [IScalar.min_I32_eq] norm_num omega) (by rw [IScalar.max_I32_eq] norm_num omega)) have hdiffVal : diff.val = -1022 - expStart.val := by simpa using hdiffSpec have hgapLo : 1 <= diff.val := by omega have hgapHi : diff.val <= 58 := by omega obtain ⟨shift, hshiftCast, hshiftSpec⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.hcast_inBounds_spec .Usize diff (by rw [hdiffVal] constructor <;> scalar_tac)) have hshiftVal : (shift.val : Int) = diff.val := by simpa using hshiftSpec have hshift58 : shift.val <= 58 := by omega obtain ⟨z1, zs1, hshape1, hz1Lo, hz1Hi, hlen1⟩ : exists z zs, (d1.digits.val.take d1.num_digits.val).map (fun q => q.val) = z :: zs /\ 1 <= z /\ z < 10 /\ ((d1.digits.val.take d1.num_digits.val).map (fun q => q.val)).length = d1.num_digits.val := hpositive1 have hlen1' : (z1 :: zs1).length = d1.num_digits.val := by rw [← hshape1] exact hlen1 have hpositive1Exec : exists z zs, (d1.digits.val.take d1.num_digits.val).map (fun a => a.val) = z :: zs /\ 1 <= z /\ z < 10 /\ (z :: zs).length = d1.num_digits.val := ⟨z1, zs1, hshape1, hz1Lo, hz1Hi, hlen1'⟩ have hleadEq : d1.digits.val[0].val = z1 := by have hnumPos : 0 < d1.num_digits.val := by rw [← hlen1, hshape1] simp have hindexBound : (0 : Nat) < d1.digits.val.length := by rw [d1.digits.property] norm_num have hget : d1.digits.val[0]? = some d1.digits.val[0] := getElem?_eq_some_iff.mpr ⟨hindexBound, rfl⟩ have hhead := congrArg List.head? hshape1 rw [List.head?_eq_getElem?] at hhead have htake : (d1.digits.val.take d1.num_digits.val)[0]? = some d1.digits.val[0] := by rw [List.getElem?_take, if_pos hnumPos] exact hget simp only [List.getElem?_map, htake] at hhead simpa using Option.some.inj hhead have hz1Five : 5 <= z1 := by omega obtain ⟨read, acc, q2, hpref, hreadLo, hreadHi, haccLo, haccHi, hprosLo, hprosHi, hright, hq2point, hq2nonzero⟩ := DecimalRightShiftLoop0Execution d1 shift (by omega) (by omega) (by omega) ⟨hcap1, hdigit1, hpositive1Exec⟩ have hread18 := P856_T1_RightPrefixReadBound d1 shift read acc hshift58 hcap1 hdigit1 z1 zs1 hshape1 hz1Five hlen1' hpref let shiftI : Std.I32 := UScalar.hcast .I32 shift have hshiftIVal : shiftI.val = (shift.val : Int) := by have hs := UScalar.hcast_inBounds_spec .I32 shift (by rw [IScalar.max_I32_eq] omega) simpa [shiftI, lift, WP.spec_ok] using hs obtain ⟨eout, hadd, haddSpec⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.add_spec (x := expStart) (y := shiftI) (by rw [hshiftIVal, IScalar.min_I32_eq] omega) (by rw [hshiftIVal, IScalar.max_I32_eq] omega)) have heout : eout.val = -1022 := by rw [haddSpec, hshiftIVal, hshiftVal, hdiffVal] omega have hmaxShift : num.imp.dec2flt.slow.parse_long_mantissa.MAX_SHIFT = 60#usize := by rw [num.imp.dec2flt.slow.parse_long_mantissa.MAX_SHIFT.eq_def] have hnotCap : ¬ shift > num.imp.dec2flt.slow.parse_long_mantissa.MAX_SHIFT := by rw [hmaxShift] change ¬ shift.val > 60 omega have hbodyCont : num.imp.dec2flt.slow.parse_long_mantissa_loop2.body F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 expStart = ok (cont (q2, eout)) := by unfold num.imp.dec2flt.slow.parse_long_mantissa_loop2.body rw [hmin] have hgt : ((-1022)#i32 : Std.I32) > expStart := by change (-1022 : Int) > expStart.val omega simp only [bind_tc_ok, hgt, if_true, hdiff] rw [hshiftCast] simp only [bind_tc_ok, hnotCap, if_false, hright, lift] rw [show UScalar.hcast .I32 shift = shiftI by rfl] simp only [bind_tc_ok, hadd] have hbodyDone : num.imp.dec2flt.slow.parse_long_mantissa_loop2.body F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 q2 eout = ok (done (q2, eout)) := by unfold num.imp.dec2flt.slow.parse_long_mantissa_loop2.body rw [hmin] simp only [bind_tc_ok] have hnot : ¬ ((-1022)#i32 : Std.I32) > eout := by change ¬ (-1022 : Int) > eout.val omega simp [hnot] have hconstructed : num.imp.dec2flt.slow.parse_long_mantissa_loop2 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 expStart = ok (q2, eout) := by unfold num.imp.dec2flt.slow.parse_long_mantissa_loop2 rw [Aeneas.Std.loop.eq_1] simp only [hbodyCont, bind_tc_ok] rw [Aeneas.Std.loop.eq_1] simpa only [hbodyDone] have heq := Result.ok.inj (hconstructed.symm.trans hloop) have hd : q2 = d2 := congrArg Prod.fst heq have he : eout = exp2 := congrArg Prod.snd heq have hp : d2.decimal_point.val = d1.decimal_point.val - (read.val : Int) + 1 := by rw [← hd, hq2point] rw [hpoint1] at hp constructor · omega · right refine ⟨shift, hshift58, ?_, ?_⟩ · rw [← hd] exact hright · omega obtain ⟨z2, zs2, hshape2', hz2Lo, hz2Hi, hlen2⟩ := hpositive2 have hpositive2Exec : exists z zs, (d2.digits.val.take d2.num_digits.val).map (fun a => a.val) = z :: zs /\ 1 <= z /\ z < 10 /\ (z :: zs).length = d2.num_digits.val := by exact ⟨z2, zs2, hshape2', hz2Lo, hz2Hi, by rw [← hshape2']; exact hlen2⟩ obtain ⟨d3, q, hleft, hq19, hpoint3Eq, hpoint3Lo, hpoint3Hi, hnum3⟩ := DecimalLeftShiftLoop1Execution d2 53#usize (by norm_num) (by omega) hpoint2.1.2 ⟨hcap2, hdigit2, hpositive2Exec⟩ obtain ⟨growth, hgrowth, hgrowthVal⟩ := DecimalLeftShiftDigitGrowth d2 53#usize 16 (by norm_num) (by norm_num) (by norm_num) hcap2 hdigit2 have hgrowth16 : growth.val <= 16 := by rw [hgrowthVal] split <;> omega have hpoint3 : d3.decimal_point.val = d2.decimal_point.val + (growth.val : Int) := P804_T1_LeftShiftPointGrowth d2 d3 53#usize growth hnum2 hgrowth (by omega) hleft have hcanonical := DecimalSeqLeftShiftCanonical d2 d3 53#usize (A / (2 : Real) ^ (exp2.val + 1)) (B / (2 : Real) ^ (exp2.val + 1)) ⟨hcap2, hdigit2, Or.inr ⟨z2, zs2, hshape2', hz2Lo, hz2Hi, hlen2⟩⟩ hend2 (by norm_num) hleft dsimp only at hcanonical rcases hcanonical with ⟨hcap3, hdigit3, hshape3, hend3, q3, hq3, hpoint3Canon⟩ rcases hshape3 with hzero3 | hpositive3 · exfalso have hn : d3.num_digits.val ≠ 0 := by simpa [bne_iff_ne] using hnum3 exact hn hzero3.1 · rcases hend3 with ⟨eta3, u3, hA3, hB3, heta3, heta3u3, hsticky3⟩ have hscaleA : (2 : Real) ^ (53 : Nat) * (A / (2 : Real) ^ (exp2.val + 1)) = A / (2 : Real) ^ (exp2.val - 52) := by rw [show exp2.val + 1 = (exp2.val - 52) + 53 by omega] rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] norm_num only [zpow_natCast] field_simp have hscaleB : (2 : Real) ^ (53 : Nat) * (B / (2 : Real) ^ (exp2.val + 1)) = B / (2 : Real) ^ (exp2.val - 52) := by rw [show exp2.val + 1 = (exp2.val - 52) + 53 by omega] rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] norm_num only [zpow_natCast] field_simp refine ⟨d3, hpoint2.1.1, hpoint2.1.2, hpoint2.2, hleft, ?_⟩ dsimp only refine ⟨hcap3, hdigit3, hpositive3, ⟨eta3, u3, ?_, ?_, heta3, heta3u3, hsticky3⟩, ?_⟩ · rw [← hscaleA] exact hA3 · rw [← hscaleB] exact hB3 · rw [hpoint3] omega end dec2flt_model