import Tablet.DecimalRightShiftPrefixLoopSemantics open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P866_T1_RightShiftPointNonincrease] theorem P866_T1_RightShiftPointNonincrease : forall (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq) (shift : Std.Usize), shift.val <= 60 -> let digits := (d.digits.val.take d.num_digits.val).map (fun z => z.val) 0 < digits.foldl (fun n digit => 10 * n + digit) 0 -> (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 <= d.decimal_point.val := by -- BODY dsimp only intro d d' shift hshift hpositive hdigit hcall hnonzero 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 := DecimalRightShiftPrefixLoopSemantics d d0 shift read acc allZero hshift hpositive hdigit hloop0 dsimp only at hpref rcases hpref with ⟨hd0, hzero, hreadpos, _, _, _⟩ subst d0 subst allZero change (do let i ← lift (UScalar.hcast .I32 read) let i1 ← i - 1#i32 let i2 ← d.decimal_point - i1 let i3 ← -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE if i2 < i3 then ok { d with num_digits := 0#usize, decimal_point := 0#i32, truncated := false } else let i4 ← 1#u64 <<< shift let mask ← i4 - 1#u64 let (a, writeIndex, n1) ← num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1 d.num_digits d.digits shift read 0#usize acc mask let (b, a1, writeIndex1) ← num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2 d.truncated a shift writeIndex n1 mask num.imp.dec2flt.decimal_seq.DecimalSeq.trim { num_digits := writeIndex1, decimal_point := i2, truncated := b, digits := a1 }) = 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 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 : d.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 = d.decimal_point.val - (read.val : Int) + 1 := by have hs := IScalar.sub_equiv d.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' = { d with num_digits := 0#usize, decimal_point := 0#i32, truncated := false } := by simpa using hcall.symm subst d' simp at hnonzero · 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 d.num_digits d.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 cases hloop2 : num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2 d.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 (b, a, writeIndex) ← num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2 d.truncated a1 shift write1 acc1 mask num.imp.dec2flt.decimal_seq.DecimalSeq.trim { num_digits := writeIndex, decimal_point := point, truncated := b, digits := a }) = ok d' at hcall rw [hloop2] at hcall simp only [bind_tc_ok] at hcall 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 unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim at htrim by_cases hle : pre.num_digits <= num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS · simp only [hle, massert_True, bind_tc_ok] at htrim cases htrimLoop : num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop pre.num_digits pre.digits with | fail e => simp [htrimLoop] at htrim | div => simp [htrimLoop] at htrim | ok j => simp only [htrimLoop, bind_tc_ok] at htrim cases htrim simp only [pre] omega · simp [hle] at htrim end dec2flt_model