import Tablet.DecimalLeftShiftDigitGrowth import Tablet.DecimalSeqTrimSemantics 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: P804_T1_LeftShiftPointGrowth] theorem P804_T1_LeftShiftPointGrowth (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq) (shift growth : Std.Usize) (hne : d.num_digits.val != 0) (hgrowth : num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift d shift = ok growth) (hgrowthBound : growth.val <= 19) (hcall : num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d') : d'.decimal_point.val = d.decimal_point.val + (growth.val : Int) := by -- BODY have hneWord : d.num_digits ≠ 0#usize := by intro h have hv := congrArg UScalar.val h simp at hv exact (by simpa [hv] using hne) unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift at hcall simp only [hneWord, if_false, hgrowth, bind_tc_ok] at hcall cases hadd : d.num_digits + growth with | fail e => simp [hadd] at hcall | div => simp [hadd] at hcall | ok width => simp only [hadd, bind_tc_ok] at hcall cases hloop0 : num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0 d.truncated d.digits shift d.num_digits width 0#u64 with | fail e => simp [hloop0] at hcall | div => simp [hloop0] at hcall | ok out0 => rcases out0 with ⟨b0, a0, cursor, carry⟩ simp only [hloop0, bind_tc_ok] at hcall change (do let (bc, ac) ← num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1 b0 a0 cursor carry let count ← if width > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS then ok num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS else ok width let delta ← lift (UScalar.hcast .I32 growth) let point ← d.decimal_point + delta num.imp.dec2flt.decimal_seq.DecimalSeq.trim { num_digits := count, decimal_point := point, truncated := bc, digits := ac }) = ok d' at hcall cases hloop1 : num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1 b0 a0 cursor carry with | fail e => simp [hloop1] at hcall | div => simp [hloop1] at hcall | ok out1 => rcases out1 with ⟨b1, a1⟩ rw [hloop1] at hcall simp only [bind_tc_ok] at hcall let count : Std.Usize := if width > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS then num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS else width have hcount : (if width > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS then ok num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS else ok width) = ok count := by unfold count split <;> rfl rw [hcount] at hcall let delta : Std.I32 := UScalar.hcast .I32 growth rw [show UScalar.hcast .I32 growth = delta by rfl] at hcall simp only [lift, bind_tc_ok] at hcall cases hpoint : d.decimal_point + delta 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 let pre : num.imp.dec2flt.decimal_seq.DecimalSeq := { num_digits := count, decimal_point := point, truncated := b1, digits := a1 } have hcountBound : count.val <= a1.val.length := by rw [a1.property] by_cases hgt : width > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS · rw [show count = num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS by simp [count, hgt]] simp [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def] · rw [show count = width by simp [count, hgt]] have hw : width.val <= 768 := by simpa [gt_iff_lt, UScalar.lt_equiv, num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def] using hgt exact hw have htrim := DecimalSeqTrimSemantics pre d' (by simpa [pre] using hcountBound) (by simpa [pre] using hcall) dsimp only at htrim have hpointEq := htrim.2.1 have hdelta : delta.val = (growth.val : Int) := by have hs := UScalar.hcast_inBounds_spec .I32 growth (by rw [IScalar.max_I32_eq] omega) simpa [delta, lift, WP.spec_ok] using hs have hpointVal : point.val = d.decimal_point.val + delta.val := by have hs := IScalar.add_equiv d.decimal_point delta rw [hpoint] at hs exact hs.2.1 rw [hpointEq] simpa [pre, hdelta] using hpointVal end dec2flt_model