import Tablet.DecimalSeqRightShiftCanonical import Tablet.P804_T1_DecimalValueBounds import Tablet.P804_T1_RightLoopStep import Tablet.DecimalRightShiftLoop0Execution import Tablet.ParserSlowEarlyExits import Tablet.SlowShiftSelectorCertificate import Tablet.num_imp_dec2flt_slow_parse_long_mantissa import Tablet.num_imp_dec2flt_slow_parse_long_mantissa_MAX_SHIFT import Tablet.num_imp_dec2flt_slow_parse_long_mantissa_NUM_POWERS import Tablet.num_imp_dec2flt_slow_parse_long_mantissa_POWERS import Tablet.num_imp_dec2flt_slow_parse_long_mantissa_closure_Insts_CoreOpsFunctionFnTupleUsizeUsize_call open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: ParserSlowLoop0] theorem ParserSlowLoop0 : forall (A B : Real) (d : num.imp.dec2flt.decimal_seq.DecimalSeq), d.num_digits.val != 0 -> -324 <= d.decimal_point.val -> d.decimal_point.val < 310 -> 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 -> 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)) -> exists (d0 : num.imp.dec2flt.decimal_seq.DecimalSeq) (exp0 : Std.I32) (steps : Nat), 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) /\ steps <= 21 /\ 0 <= exp0.val /\ exp0.val <= (60 : Int) * steps /\ (10 : Real) ^ (-(325 : Int)) <= A /\ -324 <= d0.decimal_point.val /\ d0.decimal_point.val <= 0 /\ let selector : Nat -> Nat := fun n => match n with | 0 => 0 | 1 => 3 | 2 => 6 | 3 => 9 | 4 => 13 | 5 => 16 | 6 => 19 | 7 => 23 | 8 => 26 | 9 => 29 | 10 => 33 | 11 => 36 | 12 => 39 | 13 => 43 | 14 => 46 | 15 => 49 | 16 => 53 | 17 => 56 | 18 => 59 | _ => 60 (forall n, selector n <= 60) /\ let digits0 := (d0.digits.val.take d0.num_digits.val).map (fun z => z.val) let integer0 : Nat := digits0.foldl (fun n digit => 10 * n + digit) 0 let value0 : Real := integer0 * (10 : Real) ^ (d0.decimal_point.val - (d0.num_digits.val : Int)) d0.num_digits.val <= 768 /\ (forall z, z ∈ d0.digits.val.take d0.num_digits.val -> z.val < 10) /\ (exists z zs, digits0 = z :: zs /\ 1 <= z /\ z < 10 /\ digits0.length = d0.num_digits.val) /\ exists eta0 u0 : Real, A / (2 : Real) ^ exp0.val = value0 + eta0 /\ B / (2 : Real) ^ exp0.val = value0 + u0 /\ 0 <= eta0 /\ eta0 < u0 /\ (d0.truncated = true <-> 0 < eta0) := by -- BODY dsimp only intro A B d hdne hpLower hpUpper hB hcanonical classical let State := Std.Usize × Std.I32 × Bool × Array Std.U8 768#usize × Std.I32 × Option num.imp.dec2flt.common.BiasedFp let recordOf : State -> num.imp.dec2flt.decimal_seq.DecimalSeq := fun x => { num_digits := x.1, decimal_point := x.2.1, truncated := x.2.2.1, digits := x.2.2.2.1 } let exponentOf : State -> Std.I32 := fun x => x.2.2.2.2.1 let resultOf : State -> Option num.imp.dec2flt.common.BiasedFp := fun x => x.2.2.2.2.2 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)) let Shape := fun q : num.imp.dec2flt.decimal_seq.DecimalSeq => q.num_digits.val <= 768 /\ (forall z, z ∈ q.digits.val.take q.num_digits.val -> z.val < 10) /\ (exists z zs, Digits q = z :: zs /\ 1 <= z /\ z < 10 /\ (Digits q).length = q.num_digits.val) let Endpoints := fun (q : num.imp.dec2flt.decimal_seq.DecimalSeq) (e : Std.I32) => exists eta u : Real, A / (2 : Real) ^ e.val = Value q + eta /\ B / (2 : Real) ^ e.val = Value q + u /\ 0 <= eta /\ eta < u /\ (q.truncated = true <-> 0 < eta) let HeadOne := fun q : num.imp.dec2flt.decimal_seq.DecimalSeq => exists tail, Digits q = 1 :: tail let Budget := fun (q : num.imp.dec2flt.decimal_seq.DecimalSeq) (s : Nat) => (19 <= q.decimal_point.val -> (18 : Int) * s + q.decimal_point.val <= 309) /\ (2 <= q.decimal_point.val -> q.decimal_point.val <= 18 -> s <= 17) /\ (q.decimal_point.val = 1 -> HeadOne q -> s <= 19) /\ (q.decimal_point.val = 1 -> Not (HeadOne q) -> s <= 18) /\ (q.decimal_point.val <= 0 -> s <= 20) let Inv := fun x : State => resultOf x = none /\ -324 <= (recordOf x).decimal_point.val /\ (recordOf x).decimal_point.val < 310 /\ 0 <= (exponentOf x).val /\ exists s : Nat, (exponentOf x).val <= (60 : Int) * s /\ Budget (recordOf x) s /\ Shape (recordOf x) /\ Endpoints (recordOf x) (exponentOf x) let Post := fun x : State => Inv x /\ (recordOf x).decimal_point.val <= 0 let Measure := fun x : State => if 0 < (recordOf x).decimal_point.val then 2 * (recordOf x).decimal_point.val.toNat + (if HeadOne (recordOf x) then 0 else 1) else 0 let Body := fun x : State => num.imp.dec2flt.slow.parse_long_mantissa_loop0.body F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 { m := 0#u64, p_biased := 0#i32 } x.1 x.2.1 x.2.2.1 x.2.2.2.1 x.2.2.2.2.1 x.2.2.2.2.2 have hinitShape : Shape d := by exact ⟨hcanonical.1, hcanonical.2.1, hcanonical.2.2.1⟩ have hinitEndpoints : Endpoints d 0#i32 := by rcases hcanonical.2.2.2 with ⟨eta, hA, heta0, hetaCut, hsticky⟩ refine ⟨eta, (10 : Real) ^ (d.decimal_point.val - 768), ?_, ?_, heta0, hetaCut, hsticky⟩ · simpa [Endpoints, Value, Digits] using hA · simpa [Endpoints, Value, Digits] using hB have hinitBudget : Budget d 0 := by simp only [Budget] constructor · intro _ omega constructor <;> omega have hinit : Inv (d.num_digits, d.decimal_point, d.truncated, d.digits, 0#i32, none) := by simp only [Inv, recordOf, exponentOf, resultOf] refine ⟨by simp, hpLower, hpUpper, by norm_num, 0, by norm_num, hinitBudget, hinitShape, hinitEndpoints⟩ have hspec : 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 ⦃ Post ⦄ := by unfold num.imp.dec2flt.slow.parse_long_mantissa_loop0 apply Aeneas.Std.loop.spec_decr_nat Measure Inv Post Body (d.num_digits, d.decimal_point, d.truncated, d.digits, 0#i32, none) · intro x hx rcases x with ⟨i, p, t, a, e, lr⟩ simp only [Inv, recordOf, exponentOf, resultOf] at hx rcases hx with ⟨hlr, hpLo, hpHi, heLo, s, heHi, hbudget, hshape, hend⟩ simp only [Budget] at hbudget simp only [Shape, Digits] at hshape simp only [Endpoints, Value, Digits] at hend simp only [Body, Prod.fst, Prod.snd] rw [hlr] by_cases hp : 0 < p.val · have hstep := P804_T1_RightLoopStep (A / (2 : Real) ^ e.val) (B / (2 : Real) ^ e.val) { num_digits := i, decimal_point := p, truncated := t, digits := a } e hp (by omega) heLo (by have hs20 : s <= 20 := by by_cases hp19 : 19 <= p.val · have h := hbudget.1 hp19 omega · by_cases hp2 : 2 <= p.val · exact (hbudget.2.1 hp2 (by omega)).trans (by omega) · have hp1 : p.val = 1 := by omega by_cases hh : HeadOne { num_digits := i, decimal_point := p, truncated := t, digits := a } · exact (hbudget.2.2.1 hp1 hh).trans (by omega) · exact (hbudget.2.2.2.1 hp1 hh).trans (by omega) omega) hshape hend rcases hstep with ⟨d', shift, e', read, acc, hbody, hright, hk, he', hlarge, hsmall, hshapeEnd, hstall, hnoStall⟩ change (19 <= p.val -> d'.decimal_point.val = p.val - 18 \/ d'.decimal_point.val = p.val - 19) at hlarge change (p.val <= 18 -> d'.decimal_point.val = 0 \/ d'.decimal_point.val = 1) at hsmall rcases hshapeEnd with ⟨hcap', hdigit', hpositive', hend'⟩ have hshape' : Shape d' := by exact ⟨hcap', hdigit', hpositive'⟩ have he'Lo : 0 <= e'.val := by omega have he'Hi : e'.val <= (60 : Int) * (s + 1) := by rw [he'] norm_num only [Nat.cast_add, Nat.cast_one] nlinarith have hscaleA : (A / (2 : Real) ^ e.val) / (2 : Real) ^ shift.val = A / (2 : Real) ^ e'.val := by rw [he', zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] norm_num only [zpow_natCast] rw [div_div] have hscaleB : (B / (2 : Real) ^ e.val) / (2 : Real) ^ shift.val = B / (2 : Real) ^ e'.val := by rw [he', zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] norm_num only [zpow_natCast] rw [div_div] have hendNext : Endpoints d' e' := by rcases hend' with ⟨eta, u, hA, hB', heta, hetau, hsticky⟩ refine ⟨eta, u, ?_, ?_, heta, hetau, hsticky⟩ · rw [← hscaleA] exact hA · rw [← hscaleB] exact hB' have hp'Lo : -324 <= d'.decimal_point.val := by by_cases hp19 : 19 <= p.val · rcases hlarge hp19 with h | h <;> omega · rcases hsmall (by omega) with h | h <;> omega have hp'Hi : d'.decimal_point.val < 310 := by by_cases hp19 : 19 <= p.val · rcases hlarge hp19 with h | h <;> omega · rcases hsmall (by omega) with h | h <;> omega have hbudgetNext : Budget d' (s + 1) := by have hheadStall : p.val = 1 -> d'.decimal_point.val = 1 -> HeadOne d' := by intro hp1 hp1' exact hstall hp1 hp1' have hnostall : p.val = 1 -> HeadOne { num_digits := i, decimal_point := p, truncated := t, digits := a } -> d'.decimal_point.val = 0 := by exact hnoStall constructor · intro hp'19 have hp19 : 19 <= p.val := by by_contra h rcases hsmall (by omega) with h | h <;> omega have hold := hbudget.1 hp19 rcases hlarge hp19 with h | h <;> omega constructor · intro hp'2 hp'18 by_cases hp19 : 19 <= p.val · have hold := hbudget.1 hp19 rcases hlarge hp19 with h | h <;> omega · by_cases hp2 : 2 <= p.val · have hold := hbudget.2.1 hp2 (by omega) rcases hsmall (by omega) with h | h <;> omega · have hp1 : p.val = 1 := by omega rcases hsmall (by omega) with h | h <;> omega constructor · intro hp'1 hh' by_cases hp19 : 19 <= p.val · have hold := hbudget.1 hp19 rcases hlarge hp19 with h | h <;> omega · by_cases hp2 : 2 <= p.val · have hold := hbudget.2.1 hp2 (by omega) omega · have hp1 : p.val = 1 := by omega by_cases hh : HeadOne { num_digits := i, decimal_point := p, truncated := t, digits := a } · have hold := hbudget.2.2.1 hp1 hh have hz := hnostall hp1 hh omega · have hold := hbudget.2.2.2.1 hp1 hh omega constructor · intro hp'1 hh' by_cases hp19 : 19 <= p.val · have hold := hbudget.1 hp19 rcases hlarge hp19 with h | h <;> omega · by_cases hp2 : 2 <= p.val · have hold := hbudget.2.1 hp2 (by omega) omega · have hp1 : p.val = 1 := by omega have hs := hheadStall hp1 hp'1 exact False.elim (hh' hs) · intro hp'0 by_cases hp19 : 19 <= p.val · have hold := hbudget.1 hp19 rcases hlarge hp19 with h | h <;> omega · by_cases hp2 : 2 <= p.val · have hold := hbudget.2.1 hp2 (by omega) omega · have hp1 : p.val = 1 := by omega by_cases hh : HeadOne { num_digits := i, decimal_point := p, truncated := t, digits := a } · have hold := hbudget.2.2.1 hp1 hh omega · have hold := hbudget.2.2.2.1 hp1 hh omega rw [hbody] simp only [WP.spec_ok] constructor · simp only [Inv, recordOf, exponentOf, resultOf] exact ⟨by simp, hp'Lo, hp'Hi, he'Lo, s + 1, he'Hi, hbudgetNext, hshape', hendNext⟩ · simp only [Measure, recordOf] have hd'eta : ({ num_digits := d'.num_digits, decimal_point := d'.decimal_point, truncated := d'.truncated, digits := d'.digits } : num.imp.dec2flt.decimal_seq.DecimalSeq) = d' := by cases d' rfl suffices (if 0 < d'.decimal_point.val then 2 * d'.decimal_point.val.toNat + (if HeadOne d' then 0 else 1) else 0) < (if 0 < p.val then 2 * p.val.toNat + (if HeadOne { num_digits := i, decimal_point := p, truncated := t, digits := a } then 0 else 1) else 0) by simpa only [hd'eta] using this by_cases hp' : 0 < d'.decimal_point.val · simp only [hp, hp', if_true] have hpNat : (p.val.toNat : Int) = p.val := Int.toNat_of_nonneg (by omega) have hp'Nat : (d'.decimal_point.val.toNat : Int) = d'.decimal_point.val := Int.toNat_of_nonneg (by omega) have hflagOld : (if HeadOne { num_digits := i, decimal_point := p, truncated := t, digits := a } then 0 else 1) <= 1 := by split <;> omega have hflagNew : (if HeadOne d' then 0 else 1) <= 1 := by split <;> omega by_cases hp19 : 19 <= p.val · rcases hlarge hp19 with h | h <;> omega · by_cases hp2 : 2 <= p.val · rcases hsmall (by omega) with h | h <;> omega · have hp1 : p.val = 1 := by omega by_cases hh : HeadOne { num_digits := i, decimal_point := p, truncated := t, digits := a } · have hz := hnoStall hp1 hh omega · rcases hsmall (by omega) with hz | hz · omega · have hh' : HeadOne d' := hstall hp1 hz simp only [hh, hh', if_false, if_true] omega · simp only [hp, hp', if_true, if_false] omega · have hpnot : ¬ p > 0#i32 := by change ¬ p.val > 0 omega unfold num.imp.dec2flt.slow.parse_long_mantissa_loop0.body simp [hpnot] simp only [Post, Inv, recordOf, exponentOf, resultOf] exact ⟨⟨by simp, hpLo, hpHi, heLo, s, heHi, hbudget, hshape, hend⟩, by omega⟩ · exact hinit obtain ⟨out, hcall, hout⟩ := Aeneas.Std.WP.spec_imp_exists hspec rcases out with ⟨i0, p0, t0, a0, e0, lr0⟩ simp only [Post, Inv, recordOf, exponentOf, resultOf] at hout rcases hout with ⟨⟨hlr0, hp0Lo, hp0Hi, he0Lo, steps, he0Hi, hbudget0, hshape0, hend0⟩, hp0Upper⟩ have hsteps : steps <= 21 := (hbudget0.2.2.2.2 hp0Upper).trans (by omega) let d0 : num.imp.dec2flt.decimal_seq.DecimalSeq := { num_digits := i0, decimal_point := p0, truncated := t0, digits := a0 } have hAmin : (10 : Real) ^ (-(325 : Int)) <= A := by rcases hcanonical with ⟨hcap, hdigit, hshapePack, hcanonicalA⟩ rcases hshapePack with ⟨z, zs, hshape, hzLo, hzHi, hlen⟩ rcases hcanonicalA with ⟨eta, hA, heta0, hetaCut, hsticky⟩ have hv := P804_T1_DecimalValueBounds ((d.digits.val.take d.num_digits.val).map (fun q => q.val)) z zs hshape hzLo (by intro q hq simp only [List.mem_map] at hq rcases hq with ⟨u, hu, rfl⟩ exact hdigit u hu) d.decimal_point.val dsimp only at hv rw [hlen] at hv have hpw : (10 : Real) ^ (-(325 : Int)) <= (10 : Real) ^ (d.decimal_point.val - 1) := zpow_le_zpow_right₀ (by norm_num) (by omega) have hzreal : (1 : Real) <= z := by exact_mod_cast hzLo have htenpos : 0 <= (10 : Real) ^ (d.decimal_point.val - 1) := le_of_lt (zpow_pos (by norm_num) _) have hlead : (10 : Real) ^ (d.decimal_point.val - 1) <= (z : Real) * (10 : Real) ^ (d.decimal_point.val - 1) := by nlinarith rw [hA] exact le_trans hpw (le_trans hlead (le_trans hv.1 (by linarith))) refine ⟨d0, e0, steps, ?_, hsteps, he0Lo, he0Hi, hAmin, hp0Lo, hp0Upper, ?_⟩ · simpa [d0, hlr0] using hcall · constructor · intro n rcases lt_or_ge n 19 with hn | hn · interval_cases n <;> simp · simp [show n ≠ 0 by omega, show n ≠ 1 by omega, show n ≠ 2 by omega, show n ≠ 3 by omega, show n ≠ 4 by omega, show n ≠ 5 by omega, show n ≠ 6 by omega, show n ≠ 7 by omega, show n ≠ 8 by omega, show n ≠ 9 by omega, show n ≠ 10 by omega, show n ≠ 11 by omega, show n ≠ 12 by omega, show n ≠ 13 by omega, show n ≠ 14 by omega, show n ≠ 15 by omega, show n ≠ 16 by omega, show n ≠ 17 by omega, show n ≠ 18 by omega] · rcases hshape0 with ⟨hcap0, hdigit0, hpositive0⟩ refine ⟨?_, ?_, ?_, ?_⟩ · simpa [d0] using hcap0 · simpa [d0] using hdigit0 · simpa [Digits, d0] using hpositive0 · simpa [Endpoints, Value, Digits, d0] using hend0 end dec2flt_model