import Tablet.num_imp_dec2flt_parse_adjust_many_digit_count open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: NumberHelperIntegralAdjustmentSourceLength] theorem NumberHelperIntegralAdjustmentSourceLength (start : Slice Std.U8) (n : Std.Isize) (digits tail : List Std.U8) (hstart : start.val = digits ++ tail) (hdigits : forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57) (htail : forall b rest, tail = b :: rest -> Not (48 <= b.val /\ b.val <= 57)) (htailpoint : forall b rest, tail = b :: rest -> b ≠ 46#u8) (hlen : start.val.length <= 9223372036854775807) (hn : n.val = (digits.length : Int) - 19) : exists (adjusted : Std.Isize), num.imp.dec2flt.parse.adjust_many_digit_count start n = ok adjusted /\ (adjusted.val > 0 -> exists (zeros : List Std.U8) (b : Std.U8) (remaining : List Std.U8), digits = zeros ++ b :: remaining /\ (forall c, List.Mem c zeros -> c = 48#u8) /\ 49 <= b.val /\ b.val <= 57 /\ adjusted.val = (digits.length : Int) - 19 - zeros.length) := by -- BODY have hloop : num.imp.dec2flt.parse.adjust_many_digit_count_loop start n 0#usize true ⦃ adjusted => adjusted.val > 0 -> exists (zeros : List Std.U8) (b : Std.U8) (remaining : List Std.U8), digits = zeros ++ b :: remaining /\ (forall c, List.Mem c zeros -> c = 48#u8) /\ 49 <= b.val /\ b.val <= 57 /\ adjusted.val = (digits.length : Int) - 19 - zeros.length ⦄ := by unfold num.imp.dec2flt.parse.adjust_many_digit_count_loop apply Aeneas.Std.loop.spec_decr_nat (fun p : Std.Isize × Std.Usize × Bool => 2 * (start.val.length - p.2.1.val) + if p.2.2 then 1 else 0) (fun p => (p.2.2 = true /\ exists (zeros remaining : List Std.U8), digits = zeros ++ remaining /\ (forall c, List.Mem c zeros -> c = 48#u8) /\ p.2.1.val = zeros.length /\ p.1.val = (digits.length : Int) - 19 - zeros.length) \/ (p.2.2 = false /\ (p.1.val <= 0 \/ exists (zeros : List Std.U8) (b : Std.U8) (remaining : List Std.U8), digits = zeros ++ b :: remaining /\ (forall c, List.Mem c zeros -> c = 48#u8) /\ 49 <= b.val /\ b.val <= 57 /\ p.1.val = (digits.length : Int) - 19 - zeros.length))) (fun adjusted => adjusted.val > 0 -> exists (zeros : List Std.U8) (b : Std.U8) (remaining : List Std.U8), digits = zeros ++ b :: remaining /\ (forall c, List.Mem c zeros -> c = 48#u8) /\ 49 <= b.val /\ b.val <= 57 /\ adjusted.val = (digits.length : Int) - 19 - zeros.length) (fun p => num.imp.dec2flt.parse.adjust_many_digit_count_loop.body start p.1 p.2.1 p.2.2) (n, 0#usize, true) · intro p hp rcases p with ⟨count, index, scanning⟩ rcases hp with hscan | hstop · rcases hscan with ⟨hscanning, zeros, remaining, hpartition, hzeros, hindex, hcount⟩ simp only [Prod.fst, Prod.snd] at hscanning hindex hcount ⊢ cases scanning <;> simp at hscanning cases remaining with | nil => have hdigitsEq : digits = zeros := by simpa using hpartition cases htailEq : tail with | nil => have hindexNot : Not (index.val < start.val.length) := by simp [hstart, hdigitsEq, htailEq, hindex] simp [num.imp.dec2flt.parse.adjust_many_digit_count_loop.body, WP.spec_ok, hindexNot] intro hpositive rw [hcount, hdigitsEq] at hpositive omega | cons c cs => have hcnotpoint := htailpoint c cs htailEq have hcnotdigit := htail c cs htailEq have hcnotzero : c ≠ 48#u8 := by intro hc subst c exact hcnotdigit (by norm_num) have hindexInside : index.val < start.val.length := by simp [hstart, hdigitsEq, htailEq, hindex] have hget : Slice.index_usize start index = ok c := by simp [Slice.index_usize, hstart, hdigitsEq, htailEq, hindex] simp [num.imp.dec2flt.parse.adjust_many_digit_count_loop.body, WP.spec_ok, hindexInside, hget, hcnotpoint, hcnotzero] exact Or.inl (by rw [hcount, hdigitsEq]; omega) | cons b bs => have hb := hdigits b (by rw [hpartition] exact List.mem_append_right _ List.mem_cons_self) have hbnotpoint : b ≠ 46#u8 := by scalar_tac have hindexInside : index.val < start.val.length := by simp [hstart, hpartition, hindex] have hget : Slice.index_usize start index = ok b := by simp [Slice.index_usize, hstart, hpartition, hindex] by_cases hbzero : b = 48#u8 · subst b have hi1 : (48#u8 - 1#u8) = ok 47#u8 := by rfl have hcast1 : UScalar.hcast .Isize (1#u8) = 1#isize := by apply IScalar.eq_of_val_eq have h := UScalar.hcast_inBounds_spec .Isize (1#u8) (by scalar_tac) simpa [Aeneas.Std.lift, WP.spec_ok] using h have hsaturate1 : core.num.U8.saturating_sub 48#u8 47#u8 = 1#u8 := by rfl simp [num.imp.dec2flt.parse.adjust_many_digit_count_loop.body, WP.spec_ok, hindexInside, hget, hi1, hsaturate1, Aeneas.Std.lift, hcast1] have hsublo : IScalar.min .Isize <= count.val - (1#isize).val := by have hbits := Dec2Flt.configured_x86_64_numBits simp [IScalar.min, IScalarTy.numBits, hbits] have hzeroslen : zeros.length <= digits.length := by rw [hpartition] simp rw [hcount] omega have hsubhi : count.val - (1#isize).val <= IScalar.max .Isize := by have hbits := Dec2Flt.configured_x86_64_numBits simp [IScalar.max, IScalarTy.numBits, hbits] have hdigitlen : digits.length <= start.val.length := by rw [hstart] simp rw [hcount] omega obtain ⟨count1, hcount1, hcount1val⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.sub_spec (x := count) (y := 1#isize) hsublo hsubhi) rw [hcount1] have haddbound : index.val + 1 <= UScalar.max .Usize := by have hbits := Dec2Flt.configured_x86_64_numBits simp [UScalar.max, hbits] omega obtain ⟨index1, hindex1, hindex1val⟩ := Aeneas.Std.WP.spec_imp_exists (UScalar.add_spec (x := index) (y := 1#usize) (by simpa using haddbound)) rw [hindex1] simp [WP.spec_ok] refine ⟨⟨zeros ++ [48#u8], ⟨bs, ?_⟩, ?_, ?_, ?_⟩, ?_⟩ · simpa [List.append_assoc] using hpartition · intro c hc rcases List.mem_append.mp hc with hc | hc · exact hzeros c hc · simpa using hc · norm_num at hindex1val simp omega · norm_num at hcount1val simp omega · norm_num at hindex1val omega · simp [num.imp.dec2flt.parse.adjust_many_digit_count_loop.body, WP.spec_ok, hindexInside, hget, hbnotpoint, hbzero] refine Or.inr ⟨zeros, b, ⟨bs, hpartition⟩, hzeros, ?_, hb.2, hcount⟩ have : b.val ≠ 48 := by intro h apply hbzero apply UScalar.eq_of_val_eq norm_num at h ⊢ exact h omega · rcases hstop with ⟨hscanning, hstop⟩ simp only [Prod.fst, Prod.snd] at hscanning hstop ⊢ cases scanning <;> simp at hscanning simp [num.imp.dec2flt.parse.adjust_many_digit_count_loop.body, WP.spec_ok] rcases hstop with hnonpos | hwitness · intro hpositive omega · intro _ rcases hwitness with ⟨zeros, b, remaining, hpartition, hzeros, hb1, hb2, hcount⟩ exact ⟨zeros, b, ⟨remaining, hpartition⟩, hzeros, hb1, hb2, hcount⟩ · refine Or.inl ⟨rfl, [], digits, by simp, ?_, by simp, ?_⟩ · intro c hc cases hc · simpa using hn obtain ⟨adjusted, hadjusted, hpost⟩ := Aeneas.Std.WP.spec_imp_exists hloop refine ⟨adjusted, ?_, hpost⟩ simpa [num.imp.dec2flt.parse.adjust_many_digit_count] using hadjusted end dec2flt_model