import Tablet.Assumptions import Tablet.DecimalSeqTrimSemantics import Tablet.P472_T1_DecimalRightShiftPrefixLoopSemantics import Tablet.P472_T1_DecimalRightShiftMainLoopSemantics import Tablet.DecimalRightShiftU64DyadicBridge import Tablet.P472_T1_DecimalRightShiftFinalLoopSemantics import Tablet.ResultLoopPartial import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P472_T1_DecimalSeqRightShiftRecordSemantics] theorem P472_T1_DecimalSeqRightShiftRecordSemantics : let value := fun d : num.imp.dec2flt.decimal_seq.DecimalSeq => let digits := (d.digits.val.take d.num_digits.val).map (fun z => z.val) let integer : Nat := digits.foldl (fun a z => 10 * a + z) 0 (integer : Real) * (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) forall (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq) (shift : Std.Usize), shift.val <= 60 -> 0 < value d -> (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) -> num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d' -> (d'.num_digits.val = 0 /\ d'.decimal_point.val = 0 /\ d'.truncated = false) \/ exists rho : Real, 0 <= rho /\ rho < (10 : Real) ^ (d'.decimal_point.val - 768) /\ value d' = value d / (2 : Real) ^ shift.val - rho /\ (d'.truncated = true <-> d.truncated = true \/ 0 < rho) := by -- BODY dsimp only intro d d' shift hshift hvalue hdigit hcall let xs := d.digits.val.map (fun z => z.val) let F := fun ys : List Nat => ys.foldl (fun n z => 10 * n + z) 0 let I := F (xs.take d.num_digits.val) let B := 2 ^ shift.val have hscale : 0 < (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := zpow_pos (by norm_num) _ have hIreal : 0 < (I : Real) := by have hv : 0 < (I : Real) * (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by simpa [I, F, xs, List.map_take] using hvalue nlinarith have hIpos : 0 < I := by exact_mod_cast hIreal unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift at hcall cases hloop0 : num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0 d shift 0#usize 0#u64 false with | fail e => simp [hloop0] at hcall | div => simp [hloop0] at hcall | ok out0 => rcases out0 with ⟨d0, read, acc, allZero⟩ simp only [hloop0, bind_tc_ok] at hcall have hpref := P472_T1_DecimalRightShiftPrefixLoopSemantics d d0 shift read acc allZero hshift (by simpa [I, F, xs, List.map_take] using hIpos) hdigit hloop0 dsimp only at hpref rcases hpref with ⟨rfl, rfl, hreadpos, hBacc, hacc10, hphase⟩ let reset : num.imp.dec2flt.decimal_seq.DecimalSeq := { num_digits := 0#usize, decimal_point := 0#i32, truncated := false, digits := d0.digits } change (do let i ← lift (UScalar.hcast .I32 read) let i1 ← i - 1#i32 let i2 ← d0.decimal_point - i1 let i3 ← -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE if i2 < i3 then ok reset else let i4 ← 1#u64 <<< shift let mask ← i4 - 1#u64 let (ac, wi, n1) ← num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1 d0.num_digits d0.digits shift read 0#usize acc mask let (bc, ac1, wi1) ← num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2 d0.truncated ac shift wi n1 mask num.imp.dec2flt.decimal_seq.DecimalSeq.trim { num_digits := wi1, decimal_point := i2, truncated := bc, digits := ac1 }) = ok d' at hcall let readI : Std.I32 := UScalar.hcast .I32 read rw [show UScalar.hcast .I32 read = readI by rfl] at hcall simp only [lift, bind_tc_ok] at hcall have hreadBound : read.val <= d0.num_digits.val + shift.val := by rcases hphase with hphase | hphase · omega · omega have hread828 : read.val <= 828 := by rcases hphase with hphase | hphase <;> omega have hreadIVal : readI.val = (read.val : Int) := by have hs := UScalar.hcast_inBounds_spec .I32 read (by rw [IScalar.max_I32_eq] omega) simp only [lift, WP.spec_ok] at hs exact hs cases hsub : readI - 1#i32 with | fail e => rw [hsub] at hcall simp at hcall | div => rw [hsub] at hcall simp at hcall | ok readMinus => rw [hsub] at hcall simp only [bind_tc_ok] at hcall have hsubVal : readMinus.val = readI.val - 1 := by have hs := IScalar.sub_equiv readI 1#i32 rw [hsub] at hs exact hs.2.1 cases hpoint : d0.decimal_point - readMinus with | fail e => simp [hpoint] at hcall | div => simp [hpoint] at hcall | ok point => rw [hpoint] at hcall simp only [bind_tc_ok] at hcall have hpointVal : point.val = d0.decimal_point.val - (read.val : Int) + 1 := by have hs := IScalar.sub_equiv d0.decimal_point readMinus rw [hpoint] at hs rw [hsubVal, hreadIVal] at hs omega cases hnegRange : -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE with | fail e => simp [hnegRange] at hcall | div => simp [hnegRange] at hcall | ok negRange => rw [hnegRange] at hcall simp only [bind_tc_ok] at hcall by_cases hreset : point < negRange · simp only [hreset, if_true] at hcall have hd' : d' = reset := by simpa using hcall.symm subst d' exact Or.inl ⟨by simp [reset], by simp [reset], by simp [reset]⟩ · simp only [hreset, if_false] at hcall cases hpow : 1#u64 <<< shift with | fail e => simp [hpow] at hcall | div => simp [hpow] at hcall | ok pow => rw [hpow] at hcall simp only [bind_tc_ok] at hcall cases hmask : pow - 1#u64 with | fail e => simp [hmask] at hcall | div => simp [hmask] at hcall | ok mask => rw [hmask] at hcall simp only [bind_tc_ok] at hcall cases hloop1 : num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1 d0.num_digits d0.digits shift read 0#usize acc mask with | fail e => simp [hloop1] at hcall | div => simp [hloop1] at hcall | ok out1 => rcases out1 with ⟨a1, write1, acc1⟩ rw [hloop1] at hcall simp only [bind_tc_ok] at hcall have hmain : d0.num_digits.val <= 768 /\ exists low : List Nat, low.length = d0.num_digits.val - read.val /\ (forall z, z ∈ low -> z < 10) /\ write1.val = d0.num_digits.val - read.val /\ acc1.val < 10 * B /\ acc.val * 10 ^ (d0.num_digits.val - read.val) + F ((xs.take d0.num_digits.val).drop read.val) = B * (10 * F low) + acc1.val /\ (forall j, j < low.length -> (a1.val.map (fun z : Std.U8 => z.val))[j]? = low[j]?) /\ (forall j, j < 768 -> write1.val <= j -> a1.val[j]? = d0.digits.val[j]?) /\ (forall j, j < 768 -> d0.num_digits.val <= j -> a1.val[j]? = d0.digits.val[j]?) := by by_cases hrn : read.val < d0.num_digits.val · have hm := P472_T1_DecimalRightShiftMainLoopSemantics d0.digits a1 shift read d0.num_digits write1 pow mask acc acc1 hshift hreadpos (by omega) (by omega) hacc10 hdigit hpow hmask hloop1 simpa [xs, F, B] using hm · have hreadGe : d0.num_digits.val <= read.val := by omega have hreadWord : ¬ read < d0.num_digits := by simpa [UScalar.lt_equiv] using hrn have hloop1Direct : num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1 d0.num_digits d0.digits shift read 0#usize acc mask = ok (d0.digits, 0#usize, acc) := by unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1 rw [Aeneas.Std.loop.eq_1] simp [num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1.body, hreadWord] rw [hloop1Direct] at hloop1 cases hloop1 have hnBound : d0.num_digits.val <= 768 := by rcases hphase with hp | hp <;> omega refine ⟨hnBound, [], by simp [Nat.sub_eq_zero_of_le hreadGe], by simp, by simp [Nat.sub_eq_zero_of_le hreadGe], hacc10, ?_, ?_, ?_, ?_⟩ · have hdrop : (xs.take d0.num_digits.val).drop read.val = [] := List.drop_eq_nil_of_le (by simp; omega) have hsubzero : d0.num_digits.val - read.val = 0 := Nat.sub_eq_zero_of_le hreadGe simp [F, hdrop, hsubzero] · intro j hj simp at hj · intro j hj _ rfl · intro j hj _ rfl rcases hmain with ⟨_, low, hlowLen, hlowDigit, hwrite1, hacc1, hmainNum, hlowArray, houtside1, hhigh1⟩ cases hloop2 : num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2 d0.truncated a1 shift write1 acc1 mask with | fail e => simp [hloop2] at hcall | div => simp [hloop2] at hcall | ok out2 => rcases out2 with ⟨b2, a2, write2⟩ change (do let (bc, ac, wi) ← num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2 d0.truncated a1 shift write1 acc1 mask num.imp.dec2flt.decimal_seq.DecimalSeq.trim { num_digits := wi, decimal_point := point, truncated := bc, digits := ac }) = ok d' at hcall rw [hloop2] at hcall simp only [bind_tc_ok] at hcall have hfinal := P472_T1_DecimalRightShiftFinalLoopSemantics a1 a2 d0.truncated b2 shift write1 write2 pow mask acc1 hshift (by omega) hacc1 hpow hmask hloop2 dsimp only at hfinal rcases hfinal with ⟨high, hhighDigit, hfinalNum, hwrite2, hhighArray, hbelow2, houtside2, hsticky2⟩ let all := low ++ high have hallDigit : forall z, z ∈ all -> z < 10 := by intro z hz simp only [all, List.mem_append] at hz rcases hz with hz | hz · exact hlowDigit z hz · exact hhighDigit z hz have hallLen : all.length = low.length + high.length := by simp [all] have hallFold : F all = F low * 10 ^ high.length + F high := by simp only [all, F, List.foldl_append] have foldFrom (ys : List Nat) (n : Nat) : ys.foldl (fun x z => 10 * x + z) n = n * 10 ^ ys.length + F ys := by induction ys generalizing n with | nil => simp [F] | cons y ys ih => simp only [List.foldl_cons, List.length_cons] rw [ih, pow_succ] change (10 * n + y) * 10 ^ ys.length + F ys = n * (10 ^ ys.length * 10) + (y :: ys).foldl (fun x z => 10 * x + z) 0 simp only [List.foldl_cons] rw [ih] ring simpa using foldFrom high (F low) have hcombined : acc.val * 10 ^ (d0.num_digits.val - read.val + high.length) + F ((xs.take d0.num_digits.val).drop read.val) * 10 ^ high.length = B * (10 * F all) := by rw [hallFold] calc acc.val * 10 ^ (d0.num_digits.val - read.val + high.length) + F ((xs.take d0.num_digits.val).drop read.val) * 10 ^ high.length = (acc.val * 10 ^ (d0.num_digits.val - read.val) + F ((xs.take d0.num_digits.val).drop read.val)) * 10 ^ high.length := by rw [pow_add]; ring _ = (B * (10 * F low) + acc1.val) * 10 ^ high.length := by rw [hmainNum] _ = B * (10 * F low) * 10 ^ high.length + acc1.val * 10 ^ high.length := by ring _ = B * (10 * (F low * 10 ^ high.length + F high)) := by rw [hfinalNum] ring let extra := read.val - d0.num_digits.val + high.length have foldFrom2 (ys : List Nat) (n : Nat) : ys.foldl (fun x z => 10 * x + z) n = n * 10 ^ ys.length + F ys := by induction ys generalizing n with | nil => simp [F] | cons y ys ih => simp only [List.foldl_cons, List.length_cons] rw [ih, pow_succ] change (10 * n + y) * 10 ^ ys.length + F ys = n * (10 ^ ys.length * 10) + (y :: ys).foldl (fun x z => 10 * x + z) 0 simp only [List.foldl_cons] rw [ih] ring have hterminal : I * 10 ^ extra = B * (10 * F all) := by rcases hphase with hp | hp · rcases hp with ⟨hrn, _, haccPrefix⟩ have htake : (xs.take d0.num_digits.val).take read.val = xs.take read.val := by rw [List.take_take] simp [Nat.min_eq_left hrn] have hsplit : I = acc.val * 10 ^ (d0.num_digits.val - read.val) + F ((xs.take d0.num_digits.val).drop read.val) := by have hdropLen : ((xs.take d0.num_digits.val).drop read.val).length = d0.num_digits.val - read.val := by simp [xs, d0.digits.property] omega change F (xs.take d0.num_digits.val) = _ conv_lhs => rw [← List.take_append_drop read.val (xs.take d0.num_digits.val)] simp only [F, List.foldl_append] rw [foldFrom2, htake, hdropLen] rw [← haccPrefix] have hrsub : read.val - d0.num_digits.val = 0 := Nat.sub_eq_zero_of_le hrn rw [show extra = high.length by simp [extra, hrsub]] calc I * 10 ^ high.length = (acc.val * 10 ^ (d0.num_digits.val - read.val) + F ((xs.take d0.num_digits.val).drop read.val)) * 10 ^ high.length := by rw [hsplit] _ = acc.val * 10 ^ (d0.num_digits.val - read.val + high.length) + F ((xs.take d0.num_digits.val).drop read.val) * 10 ^ high.length := by rw [pow_add]; ring _ = B * (10 * F all) := hcombined · rcases hp with ⟨hnr, _, _, haccExtended⟩ have hnsub : d0.num_digits.val - read.val = 0 := Nat.sub_eq_zero_of_le (by omega) have hdrop : (xs.take d0.num_digits.val).drop read.val = [] := List.drop_eq_nil_of_le (by simp; omega) calc I * 10 ^ extra = (I * 10 ^ (read.val - d0.num_digits.val)) * 10 ^ high.length := by rw [show extra = (read.val - d0.num_digits.val) + high.length by rfl, pow_add] ring _ = acc.val * 10 ^ high.length := by rw [haccExtended] _ = acc.val * 10 ^ (d0.num_digits.val - read.val + high.length) + F ((xs.take d0.num_digits.val).drop read.val) * 10 ^ high.length := by simp [hnsub, hdrop, F] _ = B * (10 * F all) := hcombined have hlowWrite : write1.val = low.length := by omega have hlowCap : low.length <= 768 := by omega have hwriteAll : write2.val = min all.length 768 := by rw [hwrite2, hwrite1, hallLen, hlowLen] have hwriteCap : write2.val <= 768 := by rw [hwriteAll] exact Nat.min_le_right _ _ have hwriteLen : write2.val <= all.length := by rw [hwriteAll] exact Nat.min_le_left _ _ let kept := all.take write2.val let tail := all.drop write2.val have hpreDigits : (a2.val.take write2.val).map (fun z => z.val) = kept := by simp only [kept, List.map_take] apply List.ext_getElem · simp [hwriteCap, hwriteLen, a2.property] · intro j hj1 hj2 have hjm : j < write2.val := by simpa [List.length_take, hwriteCap, a2.property] using hj1 have hj768 : j < 768 := by omega by_cases hjlow : j < low.length · have ha1 := hlowArray j hjlow have ha2raw := hbelow2 j hj768 (by omega) have ha2 : (a2.val.map (fun z : Std.U8 => z.val))[j]? = low[j]? := by rw [List.getElem?_map, ha2raw] simpa [List.getElem?_map] using ha1 have hall : all[j]? = low[j]? := by rw [List.getElem?_append_left (by simpa using hjlow)] have hmapBound : j < (a2.val.map (fun z : Std.U8 => z.val)).length := by simp [a2.property] exact hj768 have hallBound : j < all.length := by omega rw [show ((a2.val.map (fun z : Std.U8 => z.val)).take write2.val)[j] = (a2.val.map (fun z : Std.U8 => z.val))[j] by simp] rw [show (all.take write2.val)[j] = all[j] by simp] apply Option.some.inj calc some (a2.val.map (fun z : Std.U8 => z.val))[j] = (a2.val.map (fun z : Std.U8 => z.val))[j]? := (List.getElem?_eq_getElem hmapBound).symm _ = low[j]? := ha2 _ = all[j]? := hall.symm _ = some all[j] := List.getElem?_eq_getElem hallBound · let t := j - low.length have ht : t < high.length := by simp only [t] simp [all] at hj2 omega have hjEq : write1.val + t = j := by simp only [t] omega have ha2 := hhighArray t ht (by omega) have hall : all[j]? = high[t]? := by rw [List.getElem?_append_right (by simpa using hjlow)] have hmapBound : j < (a2.val.map (fun z : Std.U8 => z.val)).length := by simp [a2.property] exact hj768 have hallBound : j < all.length := by omega rw [show ((a2.val.map (fun z : Std.U8 => z.val)).take write2.val)[j] = (a2.val.map (fun z : Std.U8 => z.val))[j] by simp] rw [show (all.take write2.val)[j] = all[j] by simp] rw [hjEq] at ha2 apply Option.some.inj calc some (a2.val.map (fun z : Std.U8 => z.val))[j] = (a2.val.map (fun z : Std.U8 => z.val))[j]? := (List.getElem?_eq_getElem hmapBound).symm _ = high[t]? := ha2 _ = all[j]? := hall.symm _ = some all[j] := List.getElem?_eq_getElem hallBound have foldPos (ys : List Nat) : 0 < F ys <-> exists j, j < ys.length /\ ys[j]? != some 0 := by induction ys with | nil => simp [F] | cons y ys ih => have hform : F (y :: ys) = y * 10 ^ ys.length + F ys := by simpa [F] using foldFrom2 ys y rw [hform] constructor · intro hp by_cases hy : y = 0 · have ht : 0 < F ys := by simpa [hy] using hp rcases ih.mp ht with ⟨j, hj, hjnz⟩ exact ⟨j + 1, by simp; omega, by simpa using hjnz⟩ · exact ⟨0, by simp, by simp [hy]⟩ · rintro ⟨j, hj, hjnz⟩ cases j with | zero => have hyne : y ≠ 0 := by intro hy subst y simp at hjnz have hy : 0 < y := Nat.pos_of_ne_zero hyne have hpow : 0 < 10 ^ ys.length := by positivity positivity | succ j => have hjlt : j < ys.length := by simp at hj; omega have ht : 0 < F ys := ih.mpr ⟨j, hjlt, by simpa using hjnz⟩ omega have htailSticky : (exists t, t < tail.length /\ tail[t]? != some 0) <-> (exists j, j < high.length /\ 768 <= write1.val + j /\ high[j]? != some 0) := by constructor · rintro ⟨t, ht, htnz⟩ have ht' : t < all.length - write2.val := by simpa [tail] using ht have hwriteEq : write2.val = 768 := by rw [hwriteAll] omega let g := write2.val + t have hgall : g < all.length := by simp [g]; omega have hglow : low.length <= g := by simp [g, hwriteEq] omega let j := g - low.length have hj : j < high.length := by simp [all] at hgall simp [j] omega have hdest : write1.val + j = g := by rw [hlowWrite] simp [j] omega have htailGet : tail[t]? = all[g]? := by simp [tail, g, List.getElem?_drop] have hallHigh : all[g]? = high[j]? := by simp only [all, List.getElem?_append_right (by omega)] simp [j] refine ⟨j, hj, ?_, ?_⟩ · rw [hdest] simp [g, hwriteEq] · rw [htailGet, hallHigh] at htnz exact htnz · rintro ⟨j, hj, hjcap, hjnz⟩ let g := low.length + j have hgall : g < all.length := by simp [g, all, hj] have hdest : write1.val + j = g := by simp [g, hlowWrite] have hgcap : 768 <= g := by simpa [hdest] using hjcap have hwriteEq : write2.val = 768 := by rw [hwriteAll] omega let t := g - write2.val have ht : t < tail.length := by simp [tail, t] omega have htailGet : tail[t]? = all[g]? := by rw [List.getElem?_drop] simp [t] congr 1 omega have hallHigh : all[g]? = high[j]? := by simp only [all] rw [List.getElem?_append_right (by simp [g])] simp [g] refine ⟨t, ht, ?_⟩ rw [htailGet, hallHigh] exact hjnz have decimalScale (I0 Q0 B0 extra0 L0 n0 : Nat) (p0 point0 : Int) (hB0 : 0 < B0) (hnum0 : I0 * 10 ^ extra0 = B0 * (10 * Q0)) (hexp0 : point0 - (L0 : Int) + (extra0 : Int) = p0 - (n0 : Int) + 1) : (Q0 : Real) * (10 : Real) ^ (point0 - (L0 : Int)) = (I0 : Real) * (10 : Real) ^ (p0 - (n0 : Int)) / (B0 : Real) := by have hnumR : (I0 : Real) * (10 : Real) ^ extra0 = (B0 : Real) * (10 * (Q0 : Real)) := by exact_mod_cast hnum0 have h10 : (10 : Real) ≠ 0 := by norm_num have hBR : (B0 : Real) ≠ 0 := by positivity apply (eq_div_iff hBR).2 have hpows : (10 : Real) ^ (point0 - (L0 : Int)) * (10 : Real) ^ (extra0 : Int) = (10 : Real) ^ (p0 - (n0 : Int) + 1) := by rw [← zpow_add₀ h10, hexp0] have hpowExtra : (10 : Real) ^ (extra0 : Int) = (10 : Real) ^ extra0 := by norm_num only [zpow_natCast] have hp : (10 : Real) ^ (p0 - (n0 : Int) + 1) = (10 : Real) ^ (p0 - (n0 : Int)) * 10 := by rw [zpow_add₀ h10] norm_num calc (Q0 : Real) * (10 : Real) ^ (point0 - (L0 : Int)) * (B0 : Real) = ((B0 : Real) * (10 * (Q0 : Real))) / 10 * (10 : Real) ^ (point0 - (L0 : Int)) := by ring _ = ((I0 : Real) * (10 : Real) ^ extra0) / 10 * (10 : Real) ^ (point0 - (L0 : Int)) := by rw [hnumR] _ = (I0 : Real) * ((10 : Real) ^ (point0 - (L0 : Int)) * (10 : Real) ^ (extra0 : Int)) / 10 := by rw [hpowExtra] ring _ = (I0 : Real) * (10 : Real) ^ (p0 - (n0 : Int)) := by rw [hpows, hp] ring have hexp : point.val - (all.length : Int) + (extra : Int) = d0.decimal_point.val - (d0.num_digits.val : Int) + 1 := by rw [hpointVal, hallLen, hlowLen] simp only [extra, Nat.cast_add] rcases hphase with hp | hp · have hrsub : read.val - d0.num_digits.val = 0 := Nat.sub_eq_zero_of_le hp.1 rw [hrsub] simp rw [Nat.cast_sub hp.1] omega · have hnle : d0.num_digits.val <= read.val := by omega have hnsub : d0.num_digits.val - read.val = 0 := Nat.sub_eq_zero_of_le hnle rw [hnsub] simp rw [Nat.cast_sub hnle] omega have hBpos : 0 < B := by simp [B] have hfullValue : (F all : Real) * (10 : Real) ^ (point.val - (all.length : Int)) = (I : Real) * (10 : Real) ^ (d0.decimal_point.val - (d0.num_digits.val : Int)) / (B : Real) := decimalScale I (F all) B extra all.length d0.num_digits.val d0.decimal_point.val point.val hBpos hterminal hexp have capSplit (ys : List Nat) (m : Nat) (p0 : Int) (hm : m <= ys.length) : (F ys : Real) * (10 : Real) ^ (p0 - (ys.length : Int)) = (F (ys.take m) : Real) * (10 : Real) ^ (p0 - (m : Int)) + (F (ys.drop m) : Real) * (10 : Real) ^ (p0 - (ys.length : Int)) := by have hfold : F ys = F (ys.take m) * 10 ^ (ys.length - m) + F (ys.drop m) := by conv_lhs => rw [← List.take_append_drop m ys] simp only [F, List.foldl_append] rw [foldFrom2] congr 2 simp have hexp' : p0 - (m : Int) = (p0 - (ys.length : Int)) + ((ys.length - m : Nat) : Int) := by rw [Nat.cast_sub hm] omega have hpow : (10 : Real) ^ (p0 - (m : Int)) = (10 : Real) ^ (p0 - (ys.length : Int)) * (10 : Real) ^ (ys.length - m) := by rw [hexp', zpow_add₀ (by norm_num : (10 : Real) ≠ 0)] norm_num only [zpow_natCast] rw [show (F ys : Real) = (F (ys.take m) : Real) * (10 : Real) ^ (ys.length - m) + (F (ys.drop m) : Real) by exact_mod_cast hfold] rw [hpow] ring have hcapSplit : (F all : Real) * (10 : Real) ^ (point.val - (all.length : Int)) = (F kept : Real) * (10 : Real) ^ (point.val - (write2.val : Int)) + (F tail : Real) * (10 : Real) ^ (point.val - (all.length : Int)) := by simpa [kept, tail] using capSplit all write2.val point.val hwriteLen let rho : Real := (F tail : Real) * (10 : Real) ^ (point.val - (all.length : Int)) have hrho0 : 0 <= rho := by dsimp only [rho] exact mul_nonneg (Nat.cast_nonneg _) (le_of_lt (zpow_pos (by norm_num : (0 : Real) < 10) _)) have hrhoPos : 0 < rho <-> 0 < F tail := by dsimp only [rho] have hp : 0 < (10 : Real) ^ (point.val - (all.length : Int)) := zpow_pos (by norm_num) _ constructor · intro h have : 0 < (F tail : Real) := by nlinarith exact_mod_cast this · intro h have hc : 0 < (F tail : Real) := by exact_mod_cast h positivity have hrhoSticky : 0 < rho <-> exists j, j < high.length /\ 768 <= write1.val + j /\ high[j]? != some 0 := by rw [hrhoPos, foldPos, htailSticky] have foldBound (ys : List Nat) (hd : forall z, z ∈ ys -> z < 10) : F ys < 10 ^ ys.length := by induction ys with | nil => simp [F] | cons y ys ih => have hy : y < 10 := hd y (by simp) have hys : forall z, z ∈ ys -> z < 10 := by intro z hz exact hd z (by simp [hz]) have hi := ih hys rw [show F (y :: ys) = y * 10 ^ ys.length + F ys by simpa [F] using foldFrom2 ys y, List.length_cons, pow_succ] have hmul : y * 10 ^ ys.length <= 9 * 10 ^ ys.length := Nat.mul_le_mul_right _ (by omega) calc y * 10 ^ ys.length + F ys < y * 10 ^ ys.length + 10 ^ ys.length := Nat.add_lt_add_left hi _ _ <= 9 * 10 ^ ys.length + 10 ^ ys.length := Nat.add_le_add_right hmul _ _ = 10 ^ ys.length * 10 := by ring have htailDigit : forall z, z ∈ tail -> z < 10 := by intro z hz exact hallDigit z (List.mem_of_mem_drop hz) have htailBound : F tail < 10 ^ tail.length := foldBound tail htailDigit let pre : num.imp.dec2flt.decimal_seq.DecimalSeq := { num_digits := write2, decimal_point := point, truncated := b2, digits := a2 } have htrim : num.imp.dec2flt.decimal_seq.DecimalSeq.trim pre = ok d' := by simpa [pre] using hcall have hpreBound : pre.num_digits.val <= pre.digits.val.length := by simp [pre, a2.property] exact hwriteCap have htrimSem := DecimalSeqTrimSemantics pre d' hpreBound htrim dsimp only at htrimSem rcases htrimSem with ⟨_, hpointTrim, htrunc, _, _, _, htrimValue⟩ have hrhoCell : rho < (10 : Real) ^ (d'.decimal_point.val - 768) := by by_cases htailZero : F tail = 0 · simp only [rho, htailZero, Nat.cast_zero, zero_mul] exact zpow_pos (by norm_num) _ · have htailPos : 0 < F tail := Nat.pos_of_ne_zero htailZero have htailNonempty : 0 < tail.length := by by_contra hlen have : tail = [] := List.eq_nil_of_length_eq_zero (by omega) simp [this, F] at htailPos have hwriteEq : write2.val = 768 := by simp [tail] at htailNonempty rw [Nat.min_def] at hwriteAll split at hwriteAll · omega · omega have hexpCell : point.val - (all.length : Int) + (tail.length : Int) = d'.decimal_point.val - 768 := by rw [show tail.length = all.length - write2.val by simp [tail]] rw [Nat.cast_sub hwriteLen, hwriteEq] rw [hpointTrim] simp only [pre] omega have hcastBound : (F tail : Real) < (10 : Real) ^ tail.length := by exact_mod_cast htailBound have hscalePos : 0 < (10 : Real) ^ (point.val - (all.length : Int)) := zpow_pos (by norm_num) _ calc rho = (F tail : Real) * (10 : Real) ^ (point.val - (all.length : Int)) := rfl _ < (10 : Real) ^ tail.length * (10 : Real) ^ (point.val - (all.length : Int)) := mul_lt_mul_of_pos_right hcastBound hscalePos _ = (10 : Real) ^ (d'.decimal_point.val - 768) := by norm_num only [← zpow_natCast] rw [← zpow_add₀ (by norm_num : (10 : Real) ≠ 0)] rw [add_comm, hexpCell] right refine ⟨rho, hrho0, hrhoCell, ?_, ?_⟩ · rw [htrimValue] simp only [pre] rw [hpreDigits] have hOrigFold : ((d0.digits.val.take d0.num_digits.val).map (fun z => z.val)).foldl (fun n z => 10 * n + z) 0 = I := by simp [I, F, xs, List.map_take] rw [hOrigFold] change (F kept : Real) * (10 : Real) ^ (point.val - (write2.val : Int)) = (I : Real) * (10 : Real) ^ (d0.decimal_point.val - (d0.num_digits.val : Int)) / (2 : Real) ^ shift.val - rho have hBReal : (B : Real) = (2 : Real) ^ shift.val := by simp [B] rw [← hBReal, ← hfullValue] rw [hcapSplit] simp [rho] · rw [htrunc, hsticky2, hrhoSticky] end dec2flt_model