import Tablet.SlowAccumulatedRoundingCount import Tablet.num_imp_dec2flt_decimal_seq_parse_decimal_seq open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false set_option maxHeartbeats 2000000 namespace dec2flt_model -- [TABLET NODE: SlowAccumulatedRoundingDecimal] theorem SlowAccumulatedRoundingDecimal (s : Slice Std.U8) (hs : s.val = List.replicate 2147483648 49#u8) : exists a : Array Std.U8 768#usize, num.imp.dec2flt.decimal_seq.parse_decimal_seq s = ok { num_digits := 768#usize decimal_point := (-2147483648)#i32 truncated := true digits := a } := by -- BODY let d0 : num.imp.dec2flt.decimal_seq.DecimalSeq := { num_digits := 0#usize decimal_point := 0#i32 truncated := false digits := Array.repeat 768#usize 0#u8 } have hdefault : num.imp.dec2flt.decimal_seq.DecimalSeq.Insts.CoreDefaultDefault.default = ok d0 := by rfl have hsLen : s.val.length = 2147483648 := by rw [hs] exact List.length_replicate have hnonempty : 0 < s.val.length := by omega have hidx0 : Slice.index_usize s 0#usize = ok 49#u8 := by have hget : s.val[0]'(by omega) = 49#u8 := by simpa only [hs, List.getElem_replicate] obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists (Slice.index_usize_spec s 0#usize (by simpa [Slice.length] using hnonempty)) rw [hz] congr exact hzval.trans hget have hleading : num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0 s = ok s := by unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0 rw [Aeneas.Std.loop.eq_1] simp [num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0.body, core.slice.Slice.is_empty, hnonempty, hidx0] obtain ⟨empty, d1, hscan, hempty, hdcount, hdpoint, hdtrunc⟩ := SlowAccumulatedRoundingCount s d0 hs rfl rfl rfl have hemptyLen : empty.val.length = 0 := by rw [hempty]; rfl have hemptyTest : core.slice.Slice.is_empty empty = ok true := by simp [core.slice.Slice.is_empty, hempty] have hsUsize : Slice.len s = 2147483648#usize := by apply UScalar.eq_of_val_eq simpa [Slice.len_val] using hsLen have hemptyUsize : Slice.len empty = 0#usize := by apply UScalar.eq_of_val_eq simpa [Slice.len_val] using hemptyLen have hconsumed : (2147483648#usize : Std.Usize) - 0#usize = ok 2147483648#usize := by obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists (UScalar.sub_spec (x := 2147483648#usize) (y := 0#usize) (by norm_num)) rw [hz] congr 2 apply UScalar.eq_of_val_eq simpa using hzval have hpred : (2147483648#usize : Std.Usize) - 1#usize = ok 2147483647#usize := by obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists (UScalar.sub_spec (x := 2147483648#usize) (y := 1#usize) (by norm_num)) rw [hz] congr 2 apply UScalar.eq_of_val_eq simpa using hzval have hlast : Slice.index_usize s 2147483647#usize = ok 49#u8 := by have hget : s.val[2147483647]'(by omega) = 49#u8 := by simpa only [hs, List.getElem_replicate] obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists (Slice.index_usize_spec s 2147483647#usize (by simp [Slice.length, hsLen])) rw [hz] congr exact hzval.trans hget have hzeros : num.imp.dec2flt.decimal_seq.count_trailing_zeros s 2147483648#usize = ok 0#usize := by unfold num.imp.dec2flt.decimal_seq.count_trailing_zeros unfold num.imp.dec2flt.decimal_seq.count_trailing_zeros_loop rw [Aeneas.Std.loop.eq_1] simp [num.imp.dec2flt.decimal_seq.count_trailing_zeros_loop.body, hpred, hlast] rw [Aeneas.Std.loop.eq_1] simp [num.imp.dec2flt.decimal_seq.count_trailing_zeros_loop.body] have hcastZero : UScalar.hcast .I32 (0#usize : Std.Usize) = 0#i32 := by rw [IScalar.eq_equiv_bv_eq] apply BitVec.eq_of_toNat_eq simp only [UScalar.hcast, BitVec.zeroExtend_eq_setWidth, BitVec.toNat_setWidth, UScalar.bv_toNat, I32.ofInt_bv, BitVec.toNat_ofInt] norm_num have hcastN : UScalar.hcast .I32 (2147483648#usize : Std.Usize) = (-2147483648)#i32 := by rw [IScalar.eq_equiv_bv_eq] apply BitVec.eq_of_toNat_eq simp only [UScalar.hcast, BitVec.zeroExtend_eq_setWidth, BitVec.toNat_setWidth, UScalar.bv_toNat, I32.ofInt_bv, BitVec.toNat_ofInt] norm_num rfl have haddZero : ((0#i32 : Std.I32) + 0#i32) = ok 0#i32 := by rfl have hsubZero : ((2147483648#usize : Std.Usize) - 0#usize) = ok 2147483648#usize := hconsumed have haddMin : ((0#i32 : Std.I32) + (-2147483648)#i32) = ok (-2147483648)#i32 := by rfl have hlarge : (2147483648#usize : Std.Usize) > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by rw [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def] norm_num have hcleanup (a : Array Std.U8 768#usize) : num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1 { start := 768#usize, «end» := num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW } a = ok a := by have hlt : core.iter.range.StepUsize.partialOrdInst.lt 768#usize 19#usize = ok false := by rfl unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1 rw [Aeneas.Std.loop.eq_1] simp [num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1.body, core.iter.range.IteratorRange.next, num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW.eq_def, hlt] refine ⟨d1.digits, ?_⟩ unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq simp [hdefault, hleading, hscan, hempty, hemptyTest, hdcount, hsUsize, hemptyUsize, hconsumed, hzeros, hcastZero, hdpoint, haddZero, hsubZero, hcastN, haddMin, hlarge, hcleanup, hdtrunc, Aeneas.Std.lift, num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def] end dec2flt_model