import Tablet.DecimalSeqTrailingZeroValues import Tablet.DecimalSeqFillPrefixPreserved import Tablet.DecimalSeqI32HCastCongruence import Tablet.DecimalSeqDuplicateLoops open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false set_option maxHeartbeats 2000000 namespace dec2flt_model -- [TABLET NODE: DecimalSeqFinalizationExact] theorem DecimalSeqFinalizationExact (start rest : Slice Std.U8) (scan out : num.imp.dec2flt.decimal_seq.DecimalSeq) (sig trim : List Nat) (prefx suffix tail : List Nat) (q : Nat) (P E : Int) (suffixEval : Std.I32 -> Result Std.I32) (hstart : start.val.map (fun b => b.val) = prefx ++ suffix ++ tail) (hrest : rest.val.map (fun b => b.val) = tail) (hprefx : prefx = [] ∨ exists pre b, prefx = pre ++ [b] /\ b ≠ 48 /\ b ≠ 46) (hsuffix : forall b, b ∈ suffix -> b = 48 ∨ b = 46) (hq : suffix.count 48 = q) (hscanCount : scan.num_digits.val = sig.length) (hscanPoint : exists k : Int, scan.decimal_point.val + (q : Int) + (trim.length : Int) = P + k * 4294967296) (hscanTrunc : scan.truncated = false) (hsig : sig = trim ++ List.replicate q 0) (hscanDigits : (scan.digits.val.take (min sig.length 768)).map (fun b => b.val) = sig.take (min sig.length 768)) (hsuffixValue : forall base result, suffixEval base = ok result -> result.val = base.val + E) (hcore : (do let consumed <- Slice.len start - Slice.len rest let trailing <- num.imp.dec2flt.decimal_seq.count_trailing_zeros start consumed let p1 <- scan.decimal_point + UScalar.hcast .I32 trailing let kept <- scan.num_digits - trailing let point <- p1 + UScalar.hcast .I32 kept let (nDigits, flag) <- if kept > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS then ok (num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS, true) else ok (kept, scan.truncated) let finalPoint <- suffixEval point let a <- num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1 { start := nDigits, «end» := num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW } scan.digits ok (num.imp.dec2flt.decimal_seq.DecimalSeq.mk nDigits finalPoint flag a)) = ok out) : out.num_digits.val = min trim.length 768 /\ (out.digits.val.take out.num_digits.val).map (fun z => z.val) = trim.take out.num_digits.val /\ (out.truncated = true <-> 768 < trim.length) /\ exists k : Int, out.decimal_point.val = P + E + k * 4294967296 := by -- BODY have hlenStart : start.val.length = prefx.length + suffix.length + tail.length := by have h := congrArg List.length hstart simpa [Nat.add_assoc] using h have hlenRest : rest.val.length = tail.length := by have h := congrArg List.length hrest simpa using h have hsubbound : (Slice.len rest).val <= (Slice.len start).val := by simp [Slice.len, hlenStart, hlenRest] obtain ⟨consumed, hconsumed, hconsumedVal, _⟩ := Aeneas.Std.WP.spec_imp_exists (UScalar.sub_spec (x := Slice.len start) (y := Slice.len rest) hsubbound) have hconsumedPrefix : consumed.val = (prefx ++ suffix).length := by simpa [Slice.len, hlenStart, hlenRest] using hconsumedVal have htrailBound : suffix.count 48 <= UScalar.max .Usize := by have hle : suffix.count 48 <= start.val.length := by rw [hlenStart] have hc : suffix.count 48 <= suffix.length := List.count_le_length omega have hsmax : start.val.length <= UScalar.max .Usize := by simpa [UScalar.max, Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits] using start.property exact le_trans hle hsmax obtain ⟨trailing, htrailLoop, htrailingVal⟩ := DecimalSeqTrailingZeroValues start prefx suffix tail 0#usize consumed hstart hprefx hsuffix hconsumedPrefix (by simpa using htrailBound) have htrailingQ : trailing.val = q := by simpa [hq] using htrailingVal have htrail : num.imp.dec2flt.decimal_seq.count_trailing_zeros start consumed = ok trailing := by unfold num.imp.dec2flt.decimal_seq.count_trailing_zeros simpa using htrailLoop rw [hconsumed] at hcore simp only [bind_tc_ok] at hcore rw [htrail] at hcore simp only [bind_tc_ok, Aeneas.Std.lift] at hcore have hqle : q <= sig.length := by have h := congrArg List.length hsig simp at h omega have htrailLe : trailing.val <= scan.num_digits.val := by rw [htrailingQ, hscanCount] exact hqle obtain ⟨kept, hkeep, hkeepVal, _⟩ := Aeneas.Std.WP.spec_imp_exists (UScalar.sub_spec (x := scan.num_digits) (y := trailing) htrailLe) have hkeepTrim : kept.val = trim.length := by rw [hkeepVal, htrailingQ, hscanCount] have h := congrArg List.length hsig simp at h omega cases ha : scan.decimal_point + UScalar.hcast .I32 trailing with | fail err => rw [ha] at hcore; simp at hcore | div => rw [ha] at hcore; simp at hcore | ok p1 => rw [ha] at hcore simp only [bind_tc_ok] at hcore rw [hkeep] at hcore simp only [bind_tc_ok, Aeneas.Std.lift] at hcore cases hp : p1 + UScalar.hcast .I32 kept with | fail err => rw [hp] at hcore; simp at hcore | div => rw [hp] at hcore; simp at hcore | ok point => rw [hp] at hcore simp only [bind_tc_ok] at hcore cases he : suffixEval point with | fail err => rw [he] at hcore split at hcore <;> simp_all | div => rw [he] at hcore split at hcore <;> simp_all | ok finalPoint => rw [he] at hcore simp only [bind_tc_ok] at hcore obtain ⟨kq, hkq⟩ := DecimalSeqI32HCastCongruence trailing obtain ⟨kt, hkt⟩ := DecimalSeqI32HCastCongruence kept have haVal : p1.val = scan.decimal_point.val + (UScalar.hcast .I32 trailing).val := by have hh := IScalar.add_equiv scan.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 heVal : finalPoint.val = point.val + E := hsuffixValue point finalPoint he rcases hscanPoint with ⟨kb, hkb⟩ have hpointCong : exists k : Int, finalPoint.val = P + E + k * 4294967296 := by refine ⟨kb + kq + kt, ?_⟩ rw [heVal, hpVal, haVal, hkq, hkt, htrailingQ, hkeepTrim] push_cast ring_nf at hkb ⊢ omega have hdigitPrefix : (scan.digits.val.take (min trim.length 768)).map (fun b => b.val) = trim.take (min trim.length 768) := by have htrimLe : trim.length <= sig.length := by rw [hsig] simp have hm : min trim.length 768 <= min sig.length 768 := by omega have hd := congrArg (List.take (min trim.length 768)) hscanDigits rw [← List.map_take] at hd simp only [List.take_take, min_eq_left hm] at hd rw [hsig] at hd simpa [List.take_append_of_le_length (Nat.min_le_left _ _)] using hd by_cases hcap : 768 < trim.length · obtain ⟨a', hfill, hpref⟩ := DecimalSeqFillPrefixPreserved num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS scan.digits simp [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def, hkeepTrim, hcap, hscanTrunc] at hcore 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 } scan.digits = ok a' := by simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def] using hfill rw [hfill768] at hcore simp at hcore subst out refine ⟨?_, ?_, ?_, hpointCong⟩ · change 768 = min trim.length 768 omega · change (a'.val.take 768).map (fun z => z.val) = trim.take 768 have hpref768 : a'.val.take 768 = scan.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] exact hcap · obtain ⟨a', hfill, hpref⟩ := DecimalSeqFillPrefixPreserved kept scan.digits have hle : trim.length <= 768 := by omega simp [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def, hkeepTrim, hcap, hscanTrunc] at hcore rw [hfill] at hcore simp at hcore subst out refine ⟨?_, ?_, ?_, hpointCong⟩ · change kept.val = min trim.length 768 rw [hkeepTrim, Nat.min_eq_left hle] · rw [hpref] simpa [hkeepTrim, Nat.min_eq_left hle] using hdigitPrefix · simp exact hle end dec2flt_model