import Tablet.P495_T1_SlowLoop0Execution import Tablet.P495_T1_RightLoopStep import Tablet.P529_T1_RightBoundaryTransport import Tablet.P826_T1_ResultLoopBackward import Tablet.ResultLoopPartial import Tablet.P856_T1_CoefficientGridAlignment import Tablet.P472_T1_DecimalValueBounds open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P529_T1_RightHistoryBoundary] theorem P529_T1_RightHistoryBoundary (X Y : Real) (d d0 : num.imp.dec2flt.decimal_seq.DecimalSeq) (exp0 : 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)) (hcall : 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)) : 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)) -> (d0.decimal_point.val < 0 -> exp0.val = 0) /\ (forall (c : Nat) (R : Int), 0 <= R - exp0.val + 768 - d0.decimal_point.val -> 0 <= R + 768 - d.decimal_point.val /\ (V d0 < (c : Real) * (2 : Real) ^ (R - exp0.val) -> V d < (c : Real) * (2 : Real) ^ R)) := by -- BODY dsimp only intro hY hX classical rcases hX with ⟨eta, hxeq, hetaLo, hetaHi, hsticky⟩ 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)) rcases hinput with ⟨hcap, hdigit, hlead⟩ have hrun := P495_T1_SlowLoop0Execution X Y d hpointLo hpointHi ⟨hcap, hdigit, hlead⟩ hY ⟨eta, hxeq, hetaLo, hetaHi, hsticky⟩ rcases hrun with ⟨dF, eF, steps, hrun, hsteps, heFLo, heFHi, hXlower, hpFLo, hpFHi, hcapF, hdigitF, hleadF, hlastF, hendF⟩ have hfinalEq := Result.ok.inj (hrun.symm.trans hcall) have hn : dF.num_digits = d0.num_digits := congrArg (fun z => z.1) hfinalEq have hp : dF.decimal_point = d0.decimal_point := congrArg (fun z => z.2.1) hfinalEq have ht : dF.truncated = d0.truncated := congrArg (fun z => z.2.2.1) hfinalEq have ha : dF.digits = d0.digits := congrArg (fun z => z.2.2.2.1) hfinalEq have he : eF = exp0 := congrArg (fun z => z.2.2.2.2.1) hfinalEq have hterminal : -324 <= d0.decimal_point.val ∧ d0.decimal_point.val <= 0 ∧ d0.num_digits.val <= 768 ∧ (forall b, b ∈ d0.digits.val.take d0.num_digits.val -> b.val < 10) ∧ (exists h tail, (d0.digits.val.take d0.num_digits.val).map (fun b => b.val) = h :: tail ∧ 1 <= h ∧ h < 10 ∧ (h :: tail).length = d0.num_digits.val) := by exact ⟨by simpa [hp] using hpFLo, by simpa [hp] using hpFHi, by simpa [hn] using hcapF, by simpa [hn, ha] using hdigitF, by simpa [hn, ha] using hleadF⟩ have hendpoints : ∃ eta0 u0 : Real, X / (2 : Real) ^ exp0.val = V d0 + eta0 ∧ Y / (2 : Real) ^ exp0.val = V d0 + u0 ∧ 0 <= eta0 ∧ eta0 < u0 ∧ (d0.truncated = true ↔ 0 < eta0) := by simpa [V, he, hn, hp, ht, ha] using hendF have hterminalExp : 0 <= exp0.val ∧ exp0.val <= 1260 := by constructor · simpa [he] using heFLo · calc exp0.val <= 60 * steps := by simpa [he] using heFHi _ <= 60 * 21 := by omega _ = 1260 := by norm_num have bodyMono (a a' : num.imp.dec2flt.decimal_seq.DecimalSeq) (e e' : Std.I32) (hb : num.imp.dec2flt.slow.parse_long_mantissa_loop0.body F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 { m := 0#u64, p_biased := 0#i32 } a.num_digits a.decimal_point a.truncated a.digits e none = ok (cont (a'.num_digits, a'.decimal_point, a'.truncated, a'.digits, e', none))) : ∃ k : Std.Usize, k.val <= 60 ∧ e'.val = e.val + (k.val : Int) ∧ (19 <= a.decimal_point.val -> k.val = 60) ∧ (a.decimal_point.val <= 18 -> a.decimal_point.val <= (k.val : Int)) := by unfold num.imp.dec2flt.slow.parse_long_mantissa_loop0.body at hb by_cases houter : a.decimal_point > 0#i32 · simp only [if_pos houter, core.option.Option.is_none] at hb split at hb · obtain ⟨n, hncast, hnval⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.hcast_inBounds_spec .Usize a.decimal_point (by scalar_tac)) let traits := F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 let shift : Std.Usize := match n.val with | 0 => 0#usize | 1 => 3#usize | 2 => 6#usize | 3 => 9#usize | 4 => 13#usize | 5 => 16#usize | 6 => 19#usize | 7 => 23#usize | 8 => 26#usize | 9 => 29#usize | 10 => 33#usize | 11 => 36#usize | 12 => 39#usize | 13 => 43#usize | 14 => 46#usize | 15 => 49#usize | 16 => 53#usize | 17 => 56#usize | 18 => 59#usize | _ => 60#usize have hcert := SlowShiftSelectorCertificate traits n dsimp only at hcert rcases hcert with ⟨hselector, hshift, _, hsmall, hlarge⟩ have hs : num.imp.dec2flt.slow.parse_long_mantissa.closure.Insts.CoreOpsFunctionFnTupleUsizeUsize.call F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 () n = ok shift := by simpa [traits, shift] using hselector have hneg : -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE = ok (-2047)#i32 := by unfold num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE change IScalar.neg (2047#i32 : Std.I32) = _ unfold IScalar.neg IScalar.tryMk IScalar.tryMkOpt norm_num [IScalar.check_bounds, IScalarTy.numBits] simp only [Bind.bind, Aeneas.Std.bind, hncast, hs, hneg] at hb cases hr : num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift { num_digits := a.num_digits, decimal_point := a.decimal_point, truncated := a.truncated, digits := a.digits } shift <;> simp [hr] at hb split at hb · simp at hb · cases hc : lift (UScalar.hcast .I32 shift) with | fail err => simp [hc] at hb | div => simp [hc] at hb | ok i3 => simp [hc] at hb cases ha : e + i3 with | fail err => simp [ha] at hb | div => simp [ha] at hb | ok out => simp [ha] at hb have haddval := IScalar.add_equiv e i3 rw [ha] at haddval obtain ⟨_, houtval, _⟩ := haddval have hshiftI : (i3.val : Int) = (shift.val : Int) := by obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists (UScalar.hcast_inBounds_spec .I32 shift (by have hi : (shift.val : Int) <= 60 := by exact_mod_cast hshift rw [IScalar.max_I32_eq] norm_num [IScalarTy.numBits] omega)) have hz' : lift (UScalar.hcast .I32 shift) = ok z := by simpa [lift] using hz have hiz : i3 = z := Result.ok.inj (hc.symm.trans hz') subst z simpa [WP.spec_ok] using hzval have hout : out.val = e.val + (shift.val : Int) := by simpa [hshiftI] using houtval rcases hb with ⟨_, hpointEq, _, _, houtEq⟩ have hlargeK : 19 <= a.decimal_point.val -> shift.val = 60 := by intro hp have hnlarge : 19 <= n.val := by have hn : (n.val : Int) = a.decimal_point.val := by simpa using hnval exact_mod_cast (by omega : (19 : Int) <= (n.val : Int)) exact (hlarge hnlarge).1 have hsmallK : a.decimal_point.val <= 18 -> a.decimal_point.val <= (shift.val : Int) := by intro hp have hn : (n.val : Int) = a.decimal_point.val := by simpa using hnval have hnpos : 1 <= n.val := by have hpPos : (0 : Int) < a.decimal_point.val := by simpa only [gt_iff_lt, IScalar.lt_equiv] using houter have hnp : (1 : Int) <= a.decimal_point.val := by omega rw [← hn] at hnp exact_mod_cast hnp have hnsmall : n.val <= 18 := by have hnp : (n.val : Int) <= 18 := by rw [hn]; exact hp exact_mod_cast hnp have hkSmall : n.val <= shift.val := by have hpow := (hsmall (by omega) hnsmall).2 have h2 : 2 ^ n.val <= 10 ^ n.val := Nat.pow_le_pow_left (by norm_num) _ have hpow2 : 2 ^ n.val < 2 ^ (shift.val + 1) := lt_of_le_of_lt h2 hpow have hexp : n.val < shift.val + 1 := (Nat.pow_lt_pow_iff_right (by norm_num : 1 < (2 : Nat))).mp hpow2 omega have hkSmallI : (n.val : Int) <= (shift.val : Int) := by exact_mod_cast (show n.val <= shift.val from hkSmall) omega refine ⟨shift, hshift, ?_, hlargeK, hsmallK⟩ have hout' : e'.val = out.val := congrArg IScalar.val houtEq.symm omega · simp at hb · simp [houter] at hb let State := Std.Usize × Std.I32 × Bool × (Array Std.U8 768#usize) × Std.I32 × Option num.imp.dec2flt.common.BiasedFp let stateDec := fun s : State => num.imp.dec2flt.decimal_seq.DecimalSeq.mk s.1 s.2.1 s.2.2.1 s.2.2.2.1 let stateExp := fun s : State => s.2.2.2.2.1 let stateBody := fun s : State => num.imp.dec2flt.slow.parse_long_mantissa_loop0.body F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 { m := 0#u64, p_biased := 0#i32 } s.1 s.2.1 s.2.2.1 s.2.2.2.1 s.2.2.2.2.1 s.2.2.2.2.2 let sStart : State := (d.num_digits, d.decimal_point, d.truncated, d.digits, 0#i32, none) let sFinal : State := (d0.num_digits, d0.decimal_point, d0.truncated, d0.digits, exp0, none) have hloop : Aeneas.Std.loop stateBody sStart = ok sFinal := by simpa [stateBody, sStart, sFinal, num.imp.dec2flt.slow.parse_long_mantissa_loop0] using hcall let stateInv := fun s : State => let a := stateDec s; -324 <= a.decimal_point.val ∧ a.decimal_point.val < 310 ∧ a.num_digits.val <= 768 ∧ (forall b, b ∈ a.digits.val.take a.num_digits.val -> b.val < 10) ∧ (exists h tail, (a.digits.val.take a.num_digits.val).map (fun b => b.val) = h :: tail ∧ 1 <= h ∧ h < 10 ∧ (h :: tail).length = a.num_digits.val) ∧ 0 <= (stateExp s).val ∧ (stateExp s).val <= 1260 ∧ s.2.2.2.2.2 = none ∧ (exists etaS uS : Real, X / (2 : Real) ^ (stateExp s).val = V a + etaS ∧ Y / (2 : Real) ^ (stateExp s).val = V a + uS ∧ 0 <= etaS ∧ etaS < uS ∧ (a.truncated = true ↔ 0 < etaS)) have hstartInv : stateInv sStart := by dsimp [stateInv, stateDec, stateExp, sStart] refine ⟨hpointLo, hpointHi, hcap, hdigit, hlead, by norm_num, by norm_num, rfl, ?_⟩ refine ⟨eta, 10 ^ (d.decimal_point.val - 768), ?_, ?_, hetaLo, ?_, hsticky⟩ · simpa [V, List.map_take, zpow_zero, div_one] using hxeq · simpa [V, List.map_take] using hY · dsimp exact hetaHi have hfinalInv : stateInv sFinal := by dsimp [stateInv, stateDec, stateExp, sFinal] refine ⟨hterminal.1, ?_, hterminal.2.2.1, hterminal.2.2.2.1, hterminal.2.2.2.2, hterminalExp.1, hterminalExp.2, rfl, ?_⟩ · exact lt_of_le_of_lt hterminal.2.1 (by norm_num) · exact hendpoints have doneIdentity (s t : State) (hb : stateBody s = ok (done t)) : s = t := by by_cases hp : 0 < s.2.1.val · by_cases hn : s.2.2.2.2.2 = none · simp [stateBody, num.imp.dec2flt.slow.parse_long_mantissa_loop0.body, hp, hn] at hb cases hfirst : lift (IScalar.hcast .Usize s.2.1) with | fail err => simp [hfirst] at hb | div => simp [hfirst] at hb | ok n => simp only [Bind.bind, Aeneas.Std.bind, hfirst] at hb cases hs : num.imp.dec2flt.slow.parse_long_mantissa.closure.Insts.CoreOpsFunctionFnTupleUsizeUsize.call F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 () n with | fail err => simp [hs] at hb | div => simp [hs] at hb | ok shift => cases hr : num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift { num_digits := s.1, decimal_point := s.2.1, truncated := s.2.2.1, digits := s.2.2.2.1 } shift with | fail err => simp [hs, hr] at hb | div => simp [hs, hr] at hb | ok a => have hneg : -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE = ok (-2047)#i32 := by unfold num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE change IScalar.neg (2047#i32 : Std.I32) = _ unfold IScalar.neg IScalar.tryMk IScalar.tryMkOpt norm_num [IScalar.check_bounds, IScalarTy.numBits] simp only [hs, hr, hneg] at hb split at hb · simp at hb · cases hc : lift (UScalar.hcast .I32 shift) with | fail err => simp [hc] at hb | div => simp [hc] at hb | ok i3 => cases ha : s.2.2.2.2.1 + i3 with | fail err => simp [hc, ha] at hb | div => simp [hc, ha] at hb | ok out => simp [hc, ha] at hb · simp [stateBody, num.imp.dec2flt.slow.parse_long_mantissa_loop0.body, hp, hn] at hb exact hb · simp [stateBody, num.imp.dec2flt.slow.parse_long_mantissa_loop0.body, hp] at hb exact hb have hExpOrder : ∀ s t, Aeneas.Std.loop stateBody s = ok t → t.2.2.2.2.2 = none -> s.2.2.2.2.2 = none ∧ (stateExp s).val ≤ (stateExp t).val := by apply P826_T1_ResultLoopBackward (fun s t => t.2.2.2.2.2 = none -> s.2.2.2.2.2 = none ∧ (stateExp s).val ≤ (stateExp t).val) stateBody · intro s t hb intro htNone have hs := doneIdentity s t hb subst t exact ⟨htNone, le_rfl⟩ · intro s s' t hb ih intro htNone rcases ih htNone with ⟨hnextNone, horder⟩ have hcurNone : s.2.2.2.2.2 = none := by by_contra hn simp [stateBody, num.imp.dec2flt.slow.parse_long_mantissa_loop0.body, hn] at hb have hnextTuple : (s'.1, s'.2.1, s'.2.2.1, s'.2.2.2.1, s'.2.2.2.2.1, none) = s' := by cases s' with | mk n rest => cases rest with | mk p rest => cases rest with | mk b rest => cases rest with | mk arr rest => cases rest with | mk e opt => simp_all have hb' : num.imp.dec2flt.slow.parse_long_mantissa_loop0.body F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 { m := 0#u64, p_biased := 0#i32 } (stateDec s).num_digits (stateDec s).decimal_point (stateDec s).truncated (stateDec s).digits (stateExp s) none = ok (cont ((stateDec s').num_digits, (stateDec s').decimal_point, (stateDec s').truncated, (stateDec s').digits, stateExp s', none)) := by have hb0 := hb rw [← hnextTuple] at hb0 simpa [stateBody, stateDec, stateExp, hcurNone] using hb0 obtain ⟨k, _, hupdate, _, _⟩ := bodyMono (stateDec s) (stateDec s') (stateExp s) (stateExp s') hb' have hmono : (stateExp s).val <= (stateExp s').val := by have hk : (0 : Int) <= (k.val : Int) := Int.natCast_nonneg _ omega exact ⟨hcurNone, le_trans hmono horder⟩ let stateCapRel := fun s t : State => ∀ (c : Nat) (R : Int), 0 <= R - (stateExp t).val + 768 - (stateDec t).decimal_point.val -> 0 <= R - (stateExp s).val + 768 - (stateDec s).decimal_point.val ∧ (V (stateDec t) < (c : Real) * (2 : Real) ^ (R - (stateExp t).val) -> V (stateDec s) < (c : Real) * (2 : Real) ^ (R - (stateExp s).val)) let historyRel := fun s t : State => (t.2.2.2.2.2 = none -> s.2.2.2.2.2 = none ∧ (stateExp s).val <= (stateExp t).val) ∧ (stateInv t -> stateInv s -> ((stateDec s).decimal_point.val >= 0 -> (stateDec t).decimal_point.val >= 0) ∧ ((stateDec t).decimal_point.val < 0 -> (stateExp s).val = (stateExp t).val) ∧ stateCapRel s t) have hhistory : ∀ s t, Aeneas.Std.loop stateBody s = ok t -> historyRel s t := (set_option maxHeartbeats 1600000 in by apply P826_T1_ResultLoopBackward historyRel stateBody · intro s t hb have hst := doneIdentity s t hb subst t constructor · intro hnone exact ⟨hnone, le_rfl⟩ · intro htInv hsInv refine ⟨fun h => h, fun _ => rfl, ?_⟩ intro c R hcap exact ⟨hcap, fun hlt => hlt⟩ · intro s s' t hb ih constructor · intro htNone rcases ih.1 htNone with ⟨hnextNone, hexpOrder⟩ have hcurNone : s.2.2.2.2.2 = none := by by_contra hn simp [stateBody, num.imp.dec2flt.slow.parse_long_mantissa_loop0.body, hn] at hb have hnextTuple : (s'.1, s'.2.1, s'.2.2.1, s'.2.2.2.1, s'.2.2.2.2.1, none) = s' := by cases s' with | mk n rest => cases rest with | mk p rest => cases rest with | mk b rest => cases rest with | mk arr rest => cases rest with | mk e opt => simp_all have hb' : num.imp.dec2flt.slow.parse_long_mantissa_loop0.body F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 { m := 0#u64, p_biased := 0#i32 } (stateDec s).num_digits (stateDec s).decimal_point (stateDec s).truncated (stateDec s).digits (stateExp s) none = ok (cont ((stateDec s').num_digits, (stateDec s').decimal_point, (stateDec s').truncated, (stateDec s').digits, stateExp s', none)) := by have hb0 := hb rw [← hnextTuple] at hb0 simpa [stateBody, stateDec, stateExp, hcurNone] using hb0 obtain ⟨k, _, hupdate, _, _⟩ := bodyMono (stateDec s) (stateDec s') (stateExp s) (stateExp s') hb' have hkNonneg : (0 : Int) <= (↑↑k : Int) := Int.natCast_nonneg _ have hmono : (stateExp s).val <= (stateExp s').val := by omega exact ⟨hcurNone, le_trans hmono hexpOrder⟩ · intro htInv hsInv have htInvSaved := htInv dsimp only [stateInv, stateDec, stateExp] at htInv hsInv rcases htInv with ⟨hpLoT, hpHiT, hcountT, hdigitT, hshapeT, hexpLoT, hexpHiT, hsentT, hendT⟩ rcases hsInv with ⟨hpLoS, hpHiS, hcountS, hdigitS, hshapeS, hexpLoS, hexpHiS, hsentS, hendS⟩ rcases hshapeS with ⟨headS, tailS, hshapeS, hheadS, hhead10S, hlenS⟩ have hnextBound := ih.1 hsentT rcases hnextBound with ⟨hsentNext, hexpNextT⟩ have hpointPos : 1 <= (stateDec s).decimal_point.val := by change 1 <= s.2.1.val by_contra hn have hp : ¬ 0 < s.2.1.val := by omega simp [stateBody, num.imp.dec2flt.slow.parse_long_mantissa_loop0.body, hp] at hb rcases hendS with ⟨etaS, uS, hXS, hYS, hetaS, hetauS, hstickyS⟩ have hstep := P495_T1_RightLoopStep (X / (2 : Real) ^ (stateExp s).val) (Y / (2 : Real) ^ (stateExp s).val) (stateDec s) (stateExp s) hpointPos hpHiS hexpLoS hexpHiS ⟨hcountS, hdigitS, ⟨headS, tailS, hshapeS, hheadS, hhead10S, hlenS⟩⟩ ⟨etaS, uS, hXS, hYS, hetaS, hetauS, hstickyS⟩ rcases hstep with ⟨dn, shift, en, hbStep, hright, hshift, hexpStep, hpLarge, hpSmall, hcountN, hdigitN, hshapeN, htrimN, hendN, hstall, hrecover⟩ have hpLargeS : 19 <= s.2.1.val -> dn.decimal_point.val = s.2.1.val - 18 ∨ dn.decimal_point.val = s.2.1.val - 19 := by simpa [stateDec] using hpLarge have hpSmallS : s.2.1.val <= 18 -> dn.decimal_point.val = 0 ∨ dn.decimal_point.val = 1 := by simpa [stateDec] using hpSmall have hnextTuple : (dn.num_digits, dn.decimal_point, dn.truncated, dn.digits, en, none) = s' := by have hb0 := hb have hnextTuple0 : (s'.1, s'.2.1, s'.2.2.1, s'.2.2.2.1, s'.2.2.2.2.1, none) = s' := by cases s' with | mk n rest => cases rest with | mk p rest => cases rest with | mk b rest => cases rest with | mk arr rest => cases rest with | mk e opt => simp_all rw [← hnextTuple0] at hb0 have hb' : num.imp.dec2flt.slow.parse_long_mantissa_loop0.body F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 { m := 0#u64, p_biased := 0#i32 } (stateDec s).num_digits (stateDec s).decimal_point (stateDec s).truncated (stateDec s).digits (stateExp s) none = ok (cont ((stateDec s').num_digits, (stateDec s').decimal_point, (stateDec s').truncated, (stateDec s').digits, stateExp s', none)) := by simpa [stateBody, stateDec, stateExp, hsentS] using hb0 have hEq := Result.ok.inj (hbStep.symm.trans hb') injection hEq with htup rw [← hnextTuple0] simpa [stateDec, stateExp] using htup have hpointNLo : -324 <= dn.decimal_point.val := by by_cases hlarge : 19 <= s.2.1.val · rcases hpLargeS hlarge with hp | hp <;> omega · rcases hpSmallS (by omega) with hp | hp <;> omega have hpointNHi : dn.decimal_point.val < 310 := by by_cases hlarge : 19 <= s.2.1.val · rcases hpLargeS hlarge with hp | hp <;> omega · rcases hpSmallS (by omega) with hp | hp <;> omega have henEq : en = stateExp s' := by have hh := congrArg (fun z : State => z.2.2.2.2.1) hnextTuple simpa [stateExp] using hh rcases hendN with ⟨etaN, uN, hXN, hYN, hetaN, hetauN, hstickyN⟩ have hscaleX : X / (2 : Real) ^ ((stateExp s).val + (shift.val : Int)) = (X / (2 : Real) ^ (stateExp s).val) / (2 : Real) ^ (shift.val : Int) := by rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0) _ _] ring have hscaleY : Y / (2 : Real) ^ ((stateExp s).val + (shift.val : Int)) = (Y / (2 : Real) ^ (stateExp s).val) / (2 : Real) ^ (shift.val : Int) := by rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0) _ _] ring have hXNext : X / (2 : Real) ^ en.val = V dn + etaN := by rw [hexpStep, hscaleX] exact hXN have hYNext : Y / (2 : Real) ^ en.val = V dn + uN := by rw [hexpStep, hscaleY] exact hYN have hnextInv : stateInv s' := by rw [← hnextTuple] dsimp only [stateInv, stateDec, stateExp] refine ⟨hpointNLo, hpointNHi, hcountN, hdigitN, hshapeN, ?_, ?_, rfl, ?_⟩ · rw [hexpStep] have hshiftNonneg : (0 : Int) <= (↑↑shift : Int) := by exact Int.natCast_nonneg _ exact add_nonneg hexpLoS hshiftNonneg · have hupper : (stateExp s').val <= 1260 := le_trans hexpNextT hexpHiT simpa [henEq] using hupper · exact ⟨etaN, uN, hXNext, hYNext, hetaN, hetauN, hstickyN⟩ have hback := ih.2 htInvSaved hnextInv rcases hback with ⟨hpointBack, hnegativeBack, hcapBack⟩ have hpNextNonneg : 0 <= dn.decimal_point.val := by by_cases hlarge : 19 <= s.2.1.val · rcases hpLargeS hlarge with hp | hp <;> omega · rcases hpSmallS (by omega) with hp | hp <;> omega have hpNextState : 0 <= (stateDec s').decimal_point.val := by have hpointEq : dn.decimal_point = s'.2.1 := by have hh := congrArg (fun z : State => z.2.1) hnextTuple simpa using hh simpa [stateDec, hpointEq] using hpNextNonneg refine ⟨?_, ?_, ?_⟩ · intro _ exact hpointBack hpNextState · intro hpT have hpT' : 0 <= (stateDec t).decimal_point.val := hpointBack hpNextState omega · intro c R hcapT rcases hcapBack c R hcapT with ⟨hcapNext, hstrictNext⟩ have hbodyMono : num.imp.dec2flt.slow.parse_long_mantissa_loop0.body F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 { m := 0#u64, p_biased := 0#i32 } (stateDec s).num_digits (stateDec s).decimal_point (stateDec s).truncated (stateDec s).digits (stateExp s) none = ok (cont (dn.num_digits, dn.decimal_point, dn.truncated, dn.digits, en, none)) := by have hb0 := hb rw [← hnextTuple] at hb0 simpa [stateBody, stateDec, stateExp, hsentS] using hb0 obtain ⟨k, hkBound, hkUpdate, hkLarge, hkSmall⟩ := bodyMono (stateDec s) dn (stateExp s) en hbodyMono have hkEq : (k.val : Int) = (shift.val : Int) := by omega have hpointDiff : s.2.1.val - dn.decimal_point.val <= (shift.val : Int) := by by_cases hlarge : 19 <= s.2.1.val · have hp := hpLargeS hlarge rcases hp with hp | hp · rw [hp] have h60 : (shift.val : Int) = 60 := by have hkNat : k.val = 60 := hkLarge (by simpa [stateDec] using hlarge) omega omega · rw [hp] have h60 : (shift.val : Int) = 60 := by have hkNat : k.val = 60 := hkLarge (by simpa [stateDec] using hlarge) omega omega · have hpSmallS' : s.2.1.val <= 18 := by omega have hp := hpSmallS hpSmallS' rcases hp with hp | hp · rw [hp] have hkP := hkSmall (by simpa [stateDec] using hpSmallS') change s.2.1.val <= (k.val : Int) at hkP omega · rw [hp] have hkP := hkSmall (by simpa [stateDec] using hpSmallS') change s.2.1.val <= (k.val : Int) at hkP omega have hcapSource : 0 <= R - (stateExp s).val + 768 - (stateDec s).decimal_point.val := by have hpEq : dn.decimal_point = (stateDec s').decimal_point := by have hh := congrArg (fun z : State => z.2.1) hnextTuple simpa [stateDec] using hh have hcapNext' : 0 <= R - en.val + 768 - dn.decimal_point.val := by have heVal := congrArg IScalar.val henEq have hpVal : dn.decimal_point.val = (stateDec s').decimal_point.val := by simpa using congrArg IScalar.val hpEq rw [heVal, hpVal] exact hcapNext change 0 <= R - (stateExp s).val + 768 - s.2.1.val omega have hstrictSource : V (stateDec t) < (c : Real) * (2 : Real) ^ (R - (stateExp t).val) -> V (stateDec s) < (c : Real) * (2 : Real) ^ (R - (stateExp s).val) := (set_option maxHeartbeats 1600000 in by intro hstrictTerminal by_contra hnot have hle : (c : Real) * (2 : Real) ^ (R - (stateExp s).val) <= V (stateDec s) := le_of_not_gt hnot have hgrid := P856_T1_CoefficientGridAlignment c (R - en.val) dn.decimal_point.val (by omega) (by have hpEq : dn.decimal_point = (stateDec s').decimal_point := by have hh := congrArg (fun z : State => z.2.1) hnextTuple simpa [stateDec] using hh have heVal := congrArg IScalar.val henEq have hpVal : dn.decimal_point.val = (stateDec s').decimal_point.val := by simpa using congrArg IScalar.val hpEq rw [heVal, hpVal] exact hcapNext) rcases hgrid with ⟨b, hbgrid⟩ have hpowRel : (2 : Real) ^ (R - (stateExp s).val) = (2 : Real) ^ (R - en.val) * (2 : Real) ^ shift.val := by rw [show R - (stateExp s).val = (R - en.val) + (shift.val : Int) by omega] rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] norm_num [zpow_natCast] have hmulBound : (c : Real) * (2 : Real) ^ (R - en.val) * (2 : Real) ^ shift.val <= V (stateDec s) := by calc (c : Real) * (2 : Real) ^ (R - en.val) * (2 : Real) ^ shift.val = (c : Real) * ((2 : Real) ^ (R - en.val) * (2 : Real) ^ shift.val) := by ring _ = (c : Real) * (2 : Real) ^ (R - (stateExp s).val) := by rw [← hpowRel] _ <= V (stateDec s) := hle have hshiftPos : (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 hboundary : (b : Real) * (10 : Real) ^ (dn.decimal_point.val - 768) <= V (stateDec s) / (2 : Real) ^ shift.val := by rw [← hbgrid, hnatpow] exact (le_div_iff₀ hshiftPos).2 (by simpa [mul_assoc] using hmulBound) have hnonreset : ¬ (dn.num_digits.val = 0 ∧ dn.decimal_point.val = 0 ∧ dn.truncated = false) := by intro hreset rcases hshapeN with ⟨headN, tailN, _, hheadN, _, hlenN⟩ have hposN : 1 <= dn.num_digits.val := by rw [← hlenN] simp omega have hxs := hdigitS have hvbounds := P472_T1_DecimalValueBounds ((stateDec s).digits.val.take (stateDec s).num_digits.val |>.map (fun z => z.val)) headS tailS hshapeS hheadS (fun q hq => by rcases List.mem_map.mp hq with ⟨z, hz, hzq⟩ have hzd := hdigitS z hz simpa [hzq] using hzd) (stateDec s).decimal_point.val have hVpositive : 0 < V (stateDec s) := by have hlow := hvbounds.1 have hpowpos : (0 : Real) < (10 : Real) ^ ((stateDec s).decimal_point.val - 1) := by exact zpow_pos (by norm_num) _ have hheadReal : (1 : Real) <= (headS : Real) := by exact_mod_cast hheadS have hprod : 0 < (headS : Real) * (10 : Real) ^ ((stateDec s).decimal_point.val - 1) := mul_pos (by linarith) hpowpos have hlow' : (headS : Real) * (10 : Real) ^ ((stateDec s).decimal_point.val - 1) <= V (stateDec s) := by rw [hshapeS, hlenS] at hlow dsimp only [V] rw [hshapeS] simpa [V, List.map_take, List.foldl_cons] using hlow exact lt_of_lt_of_le hprod hlow' have htransport := P529_T1_RightBoundaryTransport (stateDec s) dn shift hshift hdigitS hright hnonreset hcountN hVpositive b hboundary have hstrictNext' : V dn < (c : Real) * (2 : Real) ^ (R - en.val) := by have hpEq : dn.decimal_point = (stateDec s').decimal_point := by have hh := congrArg (fun z : State => z.2.1) hnextTuple simpa [stateDec] using hh have hnEq : dn.num_digits = (stateDec s').num_digits := by have hh := congrArg (fun z : State => z.1) hnextTuple simpa [stateDec] using hh have hdEq : dn.digits = (stateDec s').digits := by have hh := congrArg (fun z : State => z.2.2.2.1) hnextTuple simpa [stateDec] using hh have hstrictNextInput := hstrictNext hstrictTerminal simpa [V, stateDec, hpEq, hnEq, hdEq, henEq] using hstrictNextInput have hstrictGrid : V dn < (b : Real) * (10 : Real) ^ (dn.decimal_point.val - 768) := by rw [← hbgrid] exact hstrictNext' exact (not_lt_of_ge htransport) hstrictGrid ) exact ⟨hcapSource, hstrictSource⟩ ) have hhist := hhistory sStart sFinal hloop rcases hhist with ⟨hfinalNone, hhistoryProp⟩ have hnegResult : d0.decimal_point.val < 0 -> exp0.val = 0 := by intro hneg have hsame := (hhistoryProp hfinalInv hstartInv).2.1 hneg simpa [sStart, sFinal, stateExp] using hsame.symm have hcapacityResult : ∀ (c : Nat) (R : Int), 0 <= R - exp0.val + 768 - d0.decimal_point.val -> 0 <= R + 768 - d.decimal_point.val ∧ (V d0 < (c : Real) * (2 : Real) ^ (R - exp0.val) -> V d < (c : Real) * (2 : Real) ^ R) := by intro c R hcap0 have hcap := (hhistoryProp hfinalInv hstartInv).2.2 c R (by simpa [sFinal, stateDec, stateExp] using hcap0) simpa [sStart, sFinal, stateDec, stateExp] using hcap exact ⟨hnegResult, hcapacityResult⟩ end dec2flt_model