import Tablet.DecimalDigitTrimDecomposition import Tablet.DecimalSeqLeadingZeroLoopExact import Tablet.DecimalSeqScalarScanExact import Tablet.DecimalSeqTrailingZeroValues import Tablet.DecimalSeqFillPrefixPreserved import Tablet.DecimalGoalVocabularyDefault import Tablet.SourceValidByteSlice import Tablet.DecimalSeqI32HCastCongruence open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false set_option maxHeartbeats 2000000 namespace dec2flt_model -- [TABLET NODE: DecimalSeqIntegerNoExponentState] theorem DecimalSeqIntegerNoExponentState (s : Slice Std.U8) (I : List Nat) (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (hsrc : SourceValidByteSlice s) (hdig : forall z, z ∈ I -> z < 10) (hne : I ≠ []) (hs : s.val.map (fun b => b.val) = I.map (fun z => 48 + z)) (hparse : num.imp.dec2flt.decimal_seq.parse_decimal_seq s = ok d) : let significant := I.dropWhile (fun digit => digit == 0) let trimmed := (significant.reverse.dropWhile (fun digit => digit == 0)).reverse (d.num_digits.val = min trimmed.length 768 /\ (d.digits.val.take d.num_digits.val).map (fun z => z.val) = trimmed.take d.num_digits.val /\ (d.truncated = true <-> 768 < trimmed.length)) /\ exists k : Int, d.decimal_point.val = (significant.length : Int) + k * 4294967296 := by -- BODY dsimp only obtain ⟨z, q, hz, hq, hI, hsig, hsigstop, htrimstop⟩ := DecimalDigitTrimDecomposition I let sig := I.dropWhile (fun digit => digit == 0) let trim := (sig.reverse.dropWhile (fun digit => digit == 0)).reverse change (_ = min trim.length 768 /\ _ = trim.take _ /\ _) /\ _ have hzmap : (List.replicate z 0).map (fun n => 48 + n) = List.replicate z 48 := by simp have hslead : s.val.map (fun b => b.val) = List.replicate z 48 ++ sig.map (fun n => 48 + n) := by rw [hs, hI, List.map_append, hzmap] have hleadstop : sig.map (fun n => 48 + n) = [] \/ (sig.map (fun n => 48 + n))[0]? ≠ some 48 := by rcases hsigstop with h | h · left; simp [sig, h] · right intro heq apply h cases hsigeq : sig with | nil => simp [sig, hsigeq] at heq | cons a xs => have ha : a = 0 := by simp [hsigeq] at heq omega subst a simpa [sig, hsigeq] obtain ⟨s1, hlead, hs1⟩ := DecimalSeqLeadingZeroLoopExact s z (sig.map (fun n => 48 + n)) hslead hleadstop 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 hbound : sig.length <= UScalar.max .Usize := by unfold SourceValidByteSlice at hsrc have hlen := congrArg List.length hs simp at hlen have hsigle : sig.length <= I.length := by apply List.length_dropWhile_le simp [UScalar.max, Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits] omega obtain ⟨rest, d1, hscan, hrest, hd1count, hd1point, hd1trunc, hd1old, hd1written, hd1outside⟩ := DecimalSeqScalarScanExact s1 d0 sig [] (by simpa using hs1) (fun n hn => hdig n (by rw [hI] exact List.mem_append_right _ hn)) (by simp) (by simpa [d0] using hbound) have hrestnil : rest.val = [] := by apply List.eq_nil_of_length_eq_zero have h := congrArg List.length hrest simpa using h obtain ⟨afill, hfill, hafill⟩ := DecimalSeqFillPrefixPreserved d1.num_digits d1.digits by_cases hempty : sig = [] · have hd10 : d1.num_digits = 0#usize := by apply UScalar.eq_of_val_eq simp [hd1count, d0, hempty] have htrim : trim = [] := by simp [trim, hempty] unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq at hparse rw [show num.imp.dec2flt.decimal_seq.DecimalSeq.Insts.CoreDefaultDefault.default = ok d0 by rfl] at hparse simp only [bind_tc_ok] at hparse rw [hlead] at hparse simp only [bind_tc_ok] at hparse rw [hscan] at hparse simp [hrestnil, hd10] at hparse have hfill0 : num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1 { start := 0#usize, «end» := num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW } d1.digits = ok afill := by simpa [hd10] using hfill rw [hfill0] at hparse simp at hparse subst d constructor · constructor · change 0 = min trim.length 768 simp [htrim] constructor · change (afill.val.take 0).map (fun z => z.val) = trim.take 0 simp · change d1.truncated = true ↔ 768 < trim.length rw [hd1trunc] simp [d0, htrim] · refine ⟨0, ?_⟩ change d1.decimal_point.val = (sig.length : Int) simp [hd1point, d0, hempty] · have htrimne : trim ≠ [] := by have hsig' : sig = trim ++ List.replicate q 0 := by simpa [sig, trim] using hsig intro ht have hzsig : sig = List.replicate q 0 := by simpa [ht] using hsig' rcases hsigstop with hs0 | hs0 · exact hempty (by simpa [sig] using hs0) · cases q with | zero => apply hempty; simpa [hzsig] | succ q => apply hs0 simp [sig, hzsig, List.replicate_succ] have htrimlast : trim.getLast? ≠ some 0 := by rcases htrimstop with h | h · exact (htrimne (by simpa [trim] using h)).elim · simpa [trim] using h let prefx := List.replicate z 48 ++ trim.map (fun n => 48 + n) let suffix := List.replicate q 48 have hsig' : sig = trim ++ List.replicate q 0 := by simpa [sig, trim] using hsig have hstart : s.val.map (fun b => b.val) = prefx ++ suffix := by rw [hs, hI] change _ = _ rw [show I.dropWhile (fun digit => digit == 0) = sig by rfl, hsig', List.map_append, List.map_append] simp [prefx, suffix, sig, trim, List.append_assoc] have hprefx : prefx = [] \/ exists pre b, prefx = pre ++ [b] /\ b ≠ 48 /\ b ≠ 46 := by right let last := trim.getLast htrimne refine ⟨List.replicate z 48 ++ trim.dropLast.map (fun n => 48 + n), 48 + last, ?_, ?_, ?_⟩ · dsimp [prefx] rw [List.append_assoc] congr 1 have ht := congrArg (List.map (fun n => 48 + n)) (List.dropLast_append_getLast htrimne) simpa [last, List.map_append] using ht.symm · have hlast0 : last ≠ 0 := by intro h apply htrimlast have hget : trim.getLast? = some last := by rw [List.getLast?_eq_getLast htrimne] rw [hget, h] omega · omega have hsuffix : forall b, b ∈ suffix -> b = 48 \/ b = 46 := by intro b hb left have hb' := hb simp [suffix] at hb' exact hb'.2 have hlenS : s.val.length = I.length := by have := congrArg List.length hs simpa using this have hlenRest : rest.val.length = 0 := by simp [hrestnil] have hsubbound : (Slice.len rest).val <= (Slice.len s).val := by simp [Slice.len, hlenS, hlenRest] obtain ⟨consumed, hconsumed, hconsumedVal, hconsumedLe⟩ := Aeneas.Std.WP.spec_imp_exists (UScalar.sub_spec (x := Slice.len s) (y := Slice.len rest) hsubbound) have hconsumedI : consumed.val = I.length := by simpa [Slice.len, hlenS, hlenRest] using hconsumedVal have htrailBound : suffix.count 48 <= UScalar.max .Usize := by have hqle : q <= sig.length := by have hh := congrArg List.length hsig' simp at hh omega simpa [suffix] using le_trans hqle hbound obtain ⟨trailing, htrailLoop, htrailingVal⟩ := DecimalSeqTrailingZeroValues s prefx suffix [] 0#usize consumed (by simpa using hstart) hprefx hsuffix (by rw [hconsumedI] have hlen := congrArg List.length hstart simp at hlen simp only [List.length_append] omega) (by simpa using htrailBound) have htrailingQ : trailing.val = q := by simpa [suffix] using htrailingVal have htrail : num.imp.dec2flt.decimal_seq.count_trailing_zeros s consumed = ok trailing := by unfold num.imp.dec2flt.decimal_seq.count_trailing_zeros simpa using htrailLoop unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq at hparse rw [show num.imp.dec2flt.decimal_seq.DecimalSeq.Insts.CoreDefaultDefault.default = ok d0 by rfl] at hparse simp only [bind_tc_ok] at hparse rw [hlead] at hparse simp only [bind_tc_ok] at hparse rw [hscan] at hparse simp [hrestnil] at hparse rw [hconsumed] at hparse simp only [bind_tc_ok] at hparse rw [htrail] at hparse have htrailLe : trailing.val <= d1.num_digits.val := by rw [htrailingQ, hd1count] simp [d0] have hh := congrArg List.length hsig' simp at hh omega obtain ⟨kept, hkeep, hkeepVal, hkeepLe⟩ := Aeneas.Std.WP.spec_imp_exists (UScalar.sub_spec (x := d1.num_digits) (y := trailing) htrailLe) have hkeepTrim : kept.val = trim.length := by rw [hkeepVal, htrailingQ, hd1count] simp [d0] have hh := congrArg List.length hsig' simp at hh omega simp [hd1count, d0, hempty] at hparse simp only [Aeneas.Std.lift, bind_tc_ok] at hparse cases ha : d1.decimal_point + UScalar.hcast .I32 trailing with | fail e => rw [ha] at hparse; simp at hparse | div => rw [ha] at hparse; simp at hparse | ok p1 => rw [ha] at hparse simp only [bind_tc_ok] at hparse rw [hkeep] at hparse simp only [bind_tc_ok] at hparse cases hp : p1 + UScalar.hcast .I32 kept with | fail e => rw [hp] at hparse; simp at hparse | div => rw [hp] at hparse; simp at hparse | ok point => rw [hp] at hparse simp only [bind_tc_ok] at hparse have hdigitPrefix : (d1.digits.val.take (min trim.length 768)).map (fun b => b.val) = trim.take (min trim.length 768) := by apply List.ext_getElem? intro j by_cases hj : j < min trim.length 768 · rw [List.getElem?_map, List.getElem?_take, List.getElem?_take] simp only [hj, if_true] have hjsig : j < min (d0.num_digits.val + sig.length) 768 := by simp [d0] have hh := congrArg List.length hsig' simp at hh omega have hw := hd1written j (by simp [d0]) hjsig simpa [d0, hsig', List.getElem?_append, (Nat.lt_min.mp hj).1] using hw · simp [List.getElem?_map, List.getElem?_take, hj] obtain ⟨kq, hkq⟩ := DecimalSeqI32HCastCongruence trailing obtain ⟨kt, hkt⟩ := DecimalSeqI32HCastCongruence kept have haVal : p1.val = d1.decimal_point.val + (UScalar.hcast .I32 trailing).val := by have hh := IScalar.add_equiv d1.decimal_point (UScalar.hcast .I32 trailing) rw [ha] at hh exact hh.2.1 have hpVal : point.val = p1.val + (UScalar.hcast .I32 kept).val := by have hh := IScalar.add_equiv p1 (UScalar.hcast .I32 kept) rw [hp] at hh exact hh.2.1 have hpointCong : exists k : Int, point.val = (sig.length : Int) + k * 4294967296 := by refine ⟨kq + kt, ?_⟩ rw [hpVal, haVal, hd1point, hkq, hkt, htrailingQ, hkeepTrim] simp [d0] have hh := congrArg List.length hsig' simp at hh push_cast ring_nf omega by_cases hcap : 768 < trim.length · obtain ⟨a', hfill', hpref'⟩ := DecimalSeqFillPrefixPreserved num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS d1.digits simp [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def, hkeepTrim, hcap] at hparse have hfill768 : num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1 { start := 768#usize, «end» := num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW } d1.digits = ok a' := by simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def] using hfill' rw [hfill768] at hparse simp at hparse subst d constructor · constructor · change 768 = min trim.length 768 omega constructor · change (a'.val.take 768).map (fun z => z.val) = trim.take 768 have hpref768 : a'.val.take 768 = d1.digits.val.take 768 := by simpa [ num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def] using hpref' rw [hpref768] have hd := hdigitPrefix rw [Nat.min_eq_right (by omega)] at hd exact hd · simp only [Bool.true_eq, true_iff] simpa [trim, sig] using hcap · exact hpointCong · obtain ⟨a', hfill', hpref'⟩ := DecimalSeqFillPrefixPreserved kept d1.digits have hle : trim.length <= 768 := by omega simp [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def, hkeepTrim, hcap] at hparse rw [hfill'] at hparse simp at hparse subst d constructor · constructor · change kept.val = min trim.length 768 rw [hkeepTrim, Nat.min_eq_left hle] constructor · rw [hpref'] simpa [hkeepTrim, Nat.min_eq_left hle] using hdigitPrefix · simp [hd1trunc, d0] simpa [trim, sig] using hle · exact hpointCong end dec2flt_model