import Tablet.ResultLoopPartial import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: DecimalLeftShiftFirstLoopSemantics] theorem DecimalLeftShiftFirstLoopSemantics (a a' : Array Std.U8 768#usize) (b b' : Bool) (shift read write q : Std.Usize) (carry : Std.U64) (hshift : shift.val <= 60) (hread : read.val <= 768) (hwrite : read.val <= write.val) (hdigit : forall z, z ∈ a.val.take read.val -> z.val < 10) (hloop : num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0 b a shift read write 0#u64 = ok (b', a', q, carry)) : let xs := (a.val.take read.val).map (fun z => z.val) let I := xs.foldl (fun acc z => 10 * acc + z) 0 exists outDigits : List Nat, outDigits.length = read.val /\ (forall z, z ∈ outDigits -> z < 10) /\ q.val + read.val = write.val /\ carry.val < 2 ^ shift.val /\ 2 ^ shift.val * I = carry.val * 10 ^ read.val + outDigits.foldl (fun acc z => 10 * acc + z) 0 /\ (forall j, j < outDigits.length -> q.val + j < 768 -> (a'.val.map (fun z => z.val))[q.val + j]? = outDigits[j]?) /\ (forall j, j < 768 -> (j < q.val \/ write.val <= j) -> a'.val[j]? = a.val[j]?) /\ (b' = true <-> b = true \/ exists j, j < outDigits.length /\ 768 <= q.val + j /\ outDigits[j]? ≠ some 0) := by -- BODY dsimp only let xs := (a.val.take read.val).map (fun z => z.val) let F := fun ys : List Nat => ys.foldl (fun acc z => 10 * acc + z) 0 let B := 2 ^ shift.val have hBpos : 0 < B := by simp [B] have hBle : B <= 2 ^ 60 := by exact Nat.pow_le_pow_right (by omega) hshift have hxslen : xs.length = read.val := by simp [xs, hread, a.property] have hxdigit : forall z, z ∈ xs -> z < 10 := by intro z hz simp only [xs, List.mem_map] at hz rcases hz with ⟨u, hu, rfl⟩ exact hdigit u hu have foldlFrom (ys : List Nat) (acc : Nat) : ys.foldl (fun n z => 10 * n + z) acc = acc * 10 ^ ys.length + F ys := by induction ys generalizing acc with | nil => simp [F] | cons y ys ih => simp only [List.foldl_cons, List.length_cons] rw [ih, pow_succ] change (10 * acc + y) * 10 ^ ys.length + F ys = acc * (10 ^ ys.length * 10) + (y :: ys).foldl (fun n z => 10 * n + z) 0 simp only [List.foldl_cons] rw [ih] ring let Inv := fun (x : Bool × Array Std.U8 768#usize × Std.Usize × Std.Usize × Std.U64) => let (bc, ac, ri, wi, c) := x ri.val <= read.val /\ wi.val + (read.val - ri.val) = write.val /\ c.val < B /\ exists outs : List Nat, outs.length = read.val - ri.val /\ (forall z, z ∈ outs -> z < 10) /\ B * F (xs.drop ri.val) = c.val * 10 ^ outs.length + F outs /\ (forall j, j < outs.length -> wi.val + j < 768 -> (ac.val.map (fun z : Std.U8 => z.val))[wi.val + j]? = outs[j]?) /\ (forall j, j < 768 -> (j < wi.val \/ write.val <= j) -> ac.val[j]? = a.val[j]?) /\ (bc = true <-> b = true \/ exists j, j < outs.length /\ 768 <= wi.val + j /\ outs[j]? ≠ some 0) let Post := fun (x : Bool × Array Std.U8 768#usize × Std.Usize × Std.U64) => let (bc, ac, wi, c) := x exists outs : List Nat, outs.length = read.val /\ (forall z, z ∈ outs -> z < 10) /\ wi.val + read.val = write.val /\ c.val < B /\ B * F xs = c.val * 10 ^ read.val + F outs /\ (forall j, j < outs.length -> wi.val + j < 768 -> (ac.val.map (fun z : Std.U8 => z.val))[wi.val + j]? = outs[j]?) /\ (forall j, j < 768 -> (j < wi.val \/ write.val <= j) -> ac.val[j]? = a.val[j]?) /\ (bc = true <-> b = true \/ exists j, j < outs.length /\ 768 <= wi.val + j /\ outs[j]? ≠ some 0) let Body := fun (x : Bool × Array Std.U8 768#usize × Std.Usize × Std.Usize × Std.U64) => let (bc, ac, ri, wi, c) := x num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body shift bc ac ri wi c have hp : Post (b', a', q, carry) := by refine ResultLoopPartial Inv Post Body ?_ (b, a, read, write, 0#u64) (b', a', q, carry) ?_ ?_ · intro x hx r hr rcases x with ⟨bc, ac, ri, wi, c⟩ simp only [Inv] at hx rcases hx with ⟨hri, hwi, hcB, outs, houtslen, houtsdigit, hnum, harr, houtside, hsticky⟩ simp only [Body, num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body] at hr by_cases hri0 : ri.val = 0 · have hriEq : ri = 0#usize := UScalar.eq_of_val_eq (by simpa using hri0) subst ri simp at hr subst r simp only [Post] refine ⟨outs, ?_, houtsdigit, ?_, hcB, ?_, harr, houtside, hsticky⟩ · omega · omega · simpa [xs, F, houtslen] using hnum · have hripos : 0 < ri.val := by omega have hrine : ri != 0#usize := by simp only [bne_iff_ne] intro h apply hri0 simpa [h] obtain ⟨ri1, hri1, hri1val⟩ := Aeneas.Std.WP.spec_imp_exists (UScalar.sub_spec (x := ri) (y := 1#usize) (by simpa using hripos)) norm_num at hri1val have hriwi : ri.val <= wi.val := by omega have hwipos : 0 < wi.val := by omega obtain ⟨wi1, hwi1, hwi1val⟩ := Aeneas.Std.WP.spec_imp_exists (UScalar.sub_spec (x := wi) (y := 1#usize) (by simpa using hwipos)) norm_num at hwi1val have hri1lt : ri1.val < ac.val.length := by rw [ac.property] norm_num omega obtain ⟨digit, hdigitRead, hdigitReadVal⟩ := Aeneas.Std.WP.spec_imp_exists (Array.index_usize_spec ac ri1 (by simpa [Array.length] using hri1lt)) rw [hri1, hwi1] at hr simp only [hrine, if_true, bind_tc_ok] at hr rw [hdigitRead] at hr have hri1ltRead : ri1.val < read.val := by omega have hxdrop := List.drop_eq_getElem_cons (l := xs) (i := ri1.val) (by omega) have hriSucc : ri1.val + 1 = ri.val := by omega rw [hriSucc] at hxdrop have hxin : xs[ri1.val] < 10 := hxdigit _ (List.getElem_mem _) have hdigitVal : digit.val = xs[ri1.val] := by have hacOrig : ac.val[ri1.val]? = a.val[ri1.val]? := by apply houtside ri1.val (by omega) left omega have hacGet : ac.val[ri1.val]? = some digit := getElem?_eq_some_iff.mpr ⟨hri1lt, hdigitReadVal.symm⟩ have haGet : a.val[ri1.val]? = some digit := by rw [← hacOrig] exact hacGet have haBound : ri1.val < a.val.length := by have haLen : a.val.length = 768 := by simpa using a.property rw [haLen] omega have haVal : a.val[ri1.val]'haBound = digit := by have hsome := List.getElem?_eq_getElem haBound rw [haGet] at hsome exact Option.some.inj hsome.symm have hxsVal : xs[ri1.val]'(by omega) = (a.val[ri1.val]'haBound).val := by simp [xs] rw [hxsVal, haVal] have hdigitLt : digit.val < 10 := by omega let digit64 : Std.U64 := UScalar.cast .U64 digit have hdigit64 : digit64.val = digit.val := by simp [digit64, UScalar.cast_val_eq] simp only [bind_tc_ok, lift] at hr rw [show UScalar.cast .U64 digit = digit64 by rfl] at hr have hshiftSpec := UScalar.ShiftLeft_spec digit64 shift (2 ^ 64) (by norm_num [U64.numBits]; omega) (by norm_num [U64.size_eq]) cases hshiftEq : digit64 <<< shift with | fail e => simp [hshiftEq] at hr | div => simp [hshiftEq] at hr | ok shifted => rw [hshiftEq] at hshiftSpec have hshiftVal : shifted.val = digit.val * B := by have hmulLt : digit.val * B < 2 ^ 64 := by calc digit.val * B <= 9 * 2 ^ 60 := Nat.mul_le_mul (by omega) hBle _ < 2 ^ 64 := by norm_num have hs := hshiftSpec.1 simp only [WP.spec_ok] at hs rw [Nat.shiftLeft_eq, hdigit64, Nat.mod_eq_of_lt (by simpa [B] using hmulLt)] at hs simpa [B] using hs simp only [hshiftEq, bind_tc_ok] at hr have haddBound : c.val + shifted.val <= U64.max := by rw [hshiftVal] have hdigitMul : digit.val * B <= 9 * B := Nat.mul_le_mul_right B (by omega) have : c.val + digit.val * B < 2 ^ 64 := by calc c.val + digit.val * B < B + 9 * B := by omega _ = 10 * B := by ring _ <= 10 * 2 ^ 60 := Nat.mul_le_mul_left 10 hBle _ < 2 ^ 64 := by norm_num norm_num [U64.max, U64.numBits] omega obtain ⟨total, htotal, htotalVal⟩ := Aeneas.Std.WP.spec_imp_exists (U64.add_spec (x := c) (y := shifted) haddBound) rw [htotal] at hr simp only [bind_tc_ok] at hr obtain ⟨quotient, hquotient, hquotientVal⟩ := Aeneas.Std.WP.spec_imp_exists (U64.div_spec total (y := 10#u64) (by norm_num)) rw [hquotient] at hr simp only [bind_tc_ok] at hr have hmulBound : 10 * quotient.val <= U64.max := by rw [hquotientVal] have htotalLt : total.val < 2 ^ 64 := total.bv.isLt have hqle : 10 * (total.val / 10) <= total.val := Nat.mul_div_le total.val 10 norm_num [U64.max, U64.numBits] at htotalLt ⊢ omega obtain ⟨tenq, htenq, htenqVal⟩ := Aeneas.Std.WP.spec_imp_exists (U64.mul_spec (x := 10#u64) (y := quotient) (by simpa [Nat.mul_comm] using hmulBound)) rw [htenq] at hr simp only [bind_tc_ok] at hr have htenqLe : tenq.val <= total.val := by rw [htenqVal, hquotientVal] exact Nat.mul_div_le total.val 10 obtain ⟨rem, hrem, hremVal⟩ := Aeneas.Std.WP.spec_imp_exists (U64.sub_spec (x := total) (y := tenq) htenqLe) rw [hrem] at hr simp only [bind_tc_ok] at hr have htotalFormula : total.val = c.val + digit.val * B := by rw [htotalVal, hshiftVal] have hquotientFormula : quotient.val = total.val / 10 := hquotientVal have hremFormula : rem.val = total.val % 10 := by have htenqFormula : tenq.val = 10 * (total.val / 10) := by rw [htenqVal, hquotientVal] norm_num have heuc := Nat.mod_add_div total.val 10 rw [hremVal.1, htenqFormula] omega have hremLt : rem.val < 10 := by rw [hremFormula] exact Nat.mod_lt _ (by omega) have hquotientB : quotient.val < B := by rw [hquotientFormula] apply (Nat.div_lt_iff_lt_mul (by omega)).2 rw [htotalFormula] have hdigitMul : digit.val * B <= 9 * B := Nat.mul_le_mul_right B (by omega) omega let newOuts := rem.val :: outs have hnewlen : newOuts.length = read.val - ri1.val := by simp [newOuts, houtslen] omega have hnewdigit : forall z, z ∈ newOuts -> z < 10 := by intro z hz simp only [newOuts, List.mem_cons] at hz rcases hz with rfl | hz · exact hremLt · exact houtsdigit z hz have hinputStep : F (xs.drop ri1.val) = digit.val * 10 ^ outs.length + F (xs.drop ri.val) := by have hdropLen : (xs.drop ri.val).length = outs.length := by simp [hxslen, houtslen] calc F (xs.drop ri1.val) = F (xs[ri1.val] :: xs.drop ri.val) := by rw [hxdrop] _ = (xs.drop ri.val).foldl (fun n z => 10 * n + z) xs[ri1.val] := by simp [F] _ = xs[ri1.val] * 10 ^ (xs.drop ri.val).length + F (xs.drop ri.val) := foldlFrom _ _ _ = digit.val * 10 ^ outs.length + F (xs.drop ri.val) := by rw [hdigitVal, hdropLen] have houtStep : F newOuts = rem.val * 10 ^ outs.length + F outs := by simpa [newOuts, F] using foldlFrom outs rem.val have hnumNew : B * F (xs.drop ri1.val) = quotient.val * 10 ^ newOuts.length + F newOuts := by have heuc : total.val = 10 * quotient.val + rem.val := by rw [hquotientFormula, hremFormula] have hmoddiv := Nat.mod_add_div total.val 10 omega have hlenSucc : read.val - ri1.val = outs.length + 1 := by rw [houtslen] omega calc B * F (xs.drop ri1.val) = (c.val + digit.val * B) * 10 ^ outs.length + F outs := by rw [hinputStep] calc B * (digit.val * 10 ^ outs.length + F (xs.drop ri.val)) = digit.val * B * 10 ^ outs.length + B * F (xs.drop ri.val) := by ring _ = (c.val + digit.val * B) * 10 ^ outs.length + F outs := by rw [hnum]; ring _ = total.val * 10 ^ outs.length + F outs := by rw [htotalFormula] _ = (10 * quotient.val + rem.val) * 10 ^ outs.length + F outs := by rw [heuc] _ = quotient.val * 10 ^ newOuts.length + F newOuts := by rw [houtStep, hnewlen, hlenSucc, pow_succ] ring by_cases hcap : wi1.val < 768 · have hcastRem : (UScalar.cast .U8 rem).val = rem.val := by simp [UScalar.cast_val_eq, Nat.mod_eq_of_lt (by omega : rem.val < 256)] have hwibound : wi1.val < ac.val.length := by have hacLen : ac.val.length = 768 := by simpa using ac.property rw [hacLen] exact hcap obtain ⟨ac1, hac1, hac1Val⟩ := Aeneas.Std.WP.spec_imp_exists (Array.update_spec ac wi1 (UScalar.cast .U8 rem) (by simpa [Array.length] using hwibound)) have hcapWord : wi1 < num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def, UScalar.lt_equiv] using hcap simp only [hcapWord, if_true] at hr rw [hac1] at hr simp only [bind_tc_ok] at hr cases hr simp only [Inv] refine ⟨?_, ?_, hquotientB, newOuts, hnewlen, hnewdigit, hnumNew, ?_, ?_, ?_⟩ · omega · omega · intro j hj hpos rw [hac1Val] simp only [Aeneas.Std.Array.set, List.map_set, List.getElem?_set] cases j with | zero => simpa [newOuts, hcastRem] using hcap | succ j => have hjold : j < outs.length := by simp [newOuts] at hj omega have hidx : wi1.val + (j + 1) = wi.val + j := by omega have hne : wi1.val ≠ wi.val + j := by omega simp [newOuts, hidx, hne] simpa using harr j hjold (by omega) · intro j hj houtsideNew rw [hac1Val] simp only [Aeneas.Std.Array.set, List.getElem?_set] have hne : wi1.val ≠ j := by omega simp [hne] apply houtside j hj omega · constructor · intro htrue have hold := hsticky.mp htrue rcases hold with hb | ⟨j, hj, hjcap, hjnz⟩ · exact Or.inl hb · right refine ⟨j + 1, ?_, ?_, ?_⟩ · simp [newOuts] omega · omega · simpa [newOuts] · intro h apply hsticky.mpr rcases h with hb | ⟨j, hj, hjcap, hjnz⟩ · exact Or.inl hb · right cases j with | zero => omega | succ j => refine ⟨j, ?_, ?_, ?_⟩ · simp [newOuts] at hj omega · omega · simpa [newOuts] using hjnz · have hcapWord : ¬ wi1 < num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def, UScalar.lt_equiv] using hcap simp only [hcapWord, if_false] at hr by_cases hrem0 : rem.val = 0 · have hremEq : rem = 0#u64 := UScalar.eq_of_val_eq (by simpa using hrem0) have hnotgt : ¬ rem > 0#u64 := by simp [hremEq] simp [hnotgt] at hr subst r simp only [Inv] refine ⟨?_, ?_, hquotientB, newOuts, hnewlen, hnewdigit, hnumNew, ?_, ?_, ?_⟩ · omega · omega · intro j hj hpos cases j with | zero => omega | succ j => have hjold : j < outs.length := by simp [newOuts] at hj omega have hidx : wi1.val + (j + 1) = wi.val + j := by omega simpa [newOuts, hidx] using harr j hjold (by omega) · intro j hj houtsideNew apply houtside j hj omega · constructor · intro htrue have hold := hsticky.mp htrue rcases hold with hb | ⟨j, hj, hjcap, hjnz⟩ · exact Or.inl hb · right refine ⟨j + 1, ?_, ?_, ?_⟩ · simp [newOuts] omega · omega · simpa [newOuts] · intro h apply hsticky.mpr rcases h with hb | ⟨j, hj, hjcap, hjnz⟩ · exact Or.inl hb · right cases j with | zero => simp [newOuts, hrem0] at hjnz | succ j => refine ⟨j, ?_, ?_, ?_⟩ · simp [newOuts] at hj omega · omega · simpa [newOuts] using hjnz · have hgt : rem > 0#u64 := by change 0 < rem.val omega simp [hgt] at hr subst r simp only [Inv] refine ⟨?_, ?_, hquotientB, newOuts, hnewlen, hnewdigit, hnumNew, ?_, ?_, ?_⟩ · omega · omega · intro j hj hpos cases j with | zero => omega | succ j => have hjold : j < outs.length := by simp [newOuts] at hj omega have hidx : wi1.val + (j + 1) = wi.val + j := by omega simpa [newOuts, hidx] using harr j hjold (by omega) · intro j hj houtsideNew apply houtside j hj omega · constructor · intro _ right refine ⟨0, by simp [newOuts], by omega, ?_⟩ simp [newOuts, hrem0] · exact fun _ => True.intro · simp only [Inv] refine ⟨le_rfl, by simp, ?_, [], by simp, ?_, ?_, ?_, ?_, ?_⟩ · simpa [B] using hBpos · intro z hz simp at hz · have hxdropAll : xs.drop read.val = [] := List.drop_eq_nil_of_le (by rw [hxslen]) simp [F, hxdropAll] · intro j hj simp at hj · intro j hj houtside trivial · simp · simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0, Body] using hloop simpa [Post, B, F, xs] using hp end dec2flt_model