import Tablet.DecimalDigitTrimDecomposition import Tablet.DecimalSeqDigitRunExact import Tablet.DecimalSeqDuplicateLoops import Tablet.DecimalSeqExponentSuffixExact import Tablet.DecimalSeqFinalizationExact import Tablet.DecimalSeqLeadingZeroLoopExact import Tablet.DecimalSeqScalarScanExact import Tablet.DecimalGoalVocabularyDefault import Tablet.SourceValidByteSlice open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false set_option maxHeartbeats 2000000 set_option maxRecDepth 10000 namespace dec2flt_model -- [TABLET NODE: DecimalSeqIntegerExponentState] theorem DecimalSeqIntegerExponentState (s : Slice Std.U8) (I : List Nat) (upper : Bool) (sign : Option Bool) (expDigits : List Nat) (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (hsource : SourceValidByteSlice s) (hIdigits : forall z, z ∈ I -> z < 10) (hExpNe : expDigits ≠ []) (hExpDigits : forall z, z ∈ expDigits -> z < 10) (hSigNe : I.dropWhile (fun digit => digit == 0) ≠ []) (hbytes : s.val.map (fun b => b.val) = I.map (fun z => 48 + z) ++ (if upper then 69 else 101) :: (match sign with | none => [] | some negative => [if negative then 45 else 43]) ++ expDigits.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 let E : Int := let n : Nat := expDigits.foldl (fun n digit => if n < 65536 then 10 * n + digit else n) 0 match sign with | some true => -(n : Int) | _ => (n : Int) (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) + E + 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 let tail := (if upper then 69 else 101) :: (match sign with | none => [] | some negative => [if negative then 45 else 43]) ++ expDigits.map (fun z => 48 + z) let E : Int := let n : Nat := expDigits.foldl (fun n digit => if n < 65536 then 10 * n + digit else n) 0 match sign with | some true => -(n : Int) | _ => (n : Int) change (_ /\ _ /\ _) /\ exists k : Int, d.decimal_point.val = (sig.length : Int) + E + k * 4294967296 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) ++ tail := by rw [hbytes, hI, List.map_append, hzmap] simp [tail, sig, List.append_assoc] have hleadstop : (sig.map (fun n => 48 + n) ++ tail)[0]? ≠ some 48 := by cases hsigeq : sig with | nil => exact (hSigNe (by simpa [sig] using hsigeq)).elim | cons a xs => intro heq have ha : a = 0 := by simp [hsigeq] at heq; omega subst a rcases hsigstop with h | h · simp [sig, hsigeq] at h · exact h (by simp [sig, hsigeq]) obtain ⟨s1, hlead, hs1⟩ := DecimalSeqLeadingZeroLoopExact s z (sig.map (fun n => 48 + n) ++ tail) (by simpa [List.append_assoc] using hslead) (Or.inr 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 hsource have hlen := congrArg List.length hslead simp at hlen 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 tail hs1 (fun n hn => hIdigits n (by rw [hI]; exact List.mem_append_right _ hn)) (by cases upper <;> simp [tail]) (by simpa [d0, Nat.zero_add] using hbound) have hrestTail : rest.val.map (fun b => b.val) = tail := hrest have hrestNe : rest.val ≠ [] := by intro hr have hh := congrArg List.length hrestTail simp [tail, hr] at hh have hscanDigits : (d1.digits.val.take (min sig.length 768)).map (fun b => b.val) = sig.take (min sig.length 768) := by apply List.ext_getElem? intro j by_cases hj : j < min sig.length 768 · rw [List.getElem?_map, List.getElem?_take, List.getElem?_take] simp only [hj, if_true] have hw := hd1written j (by simp [d0]) (by simpa [d0] using hj) simpa [d0] using hw · simp [List.getElem?_map, List.getElem?_take, hj] have hprefixBytes : s.val.map (fun b => b.val) = (List.replicate z 48 ++ trim.map (fun n => 48 + n)) ++ List.replicate q 48 ++ tail := by rw [hslead] have hs : sig = trim ++ List.replicate q 0 := by simpa [sig, trim] using hsig rw [hs, List.map_append] simp [List.append_assoc] let prefx := List.replicate z 48 ++ trim.map (fun n => 48 + n) let suffix := List.replicate q 48 have hprefx : prefx = [] ∨ exists pre b, prefx = pre ++ [b] /\ b ≠ 48 /\ b ≠ 46 := by right have htrimNe : trim ≠ [] := by intro ht have hs : sig = trim ++ List.replicate q 0 := by simpa [sig, trim] using hsig rw [ht] at hs simp at hs rcases hsigstop with h | h · exact hSigNe (by simpa [sig] using h) · cases q with | zero => exact hSigNe (by simpa [sig] using hs) | succ q => exact h (by simp [sig, hs, List.replicate_succ]) 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 · intro heq have hlast0 : last = 0 := by omega rcases htrimstop with h | h · exact htrimNe (by simpa [trim] using h) · apply h rw [List.getLast?_eq_getLast htrimNe] simp [last, hlast0] · omega have hsuffix : forall b, b ∈ suffix -> b = 48 ∨ b = 46 := by intro b hb left simpa [suffix] using (List.mem_replicate.mp hb).2 have hsuffixCount : suffix.count 48 = q := by simp [suffix] let suffixEval : Std.I32 -> Result Std.I32 := fun base => do let ch <- Slice.index_usize rest 0#usize if ch = 101#u8 then let s3 <- core.slice.index.Slice.index (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) rest { start := 1#usize } let b2 <- core.slice.Slice.is_empty s3 let (s4, exp_sign) <- if b2 then ok (s3, 1#i32) else do let ch1 <- Slice.index_usize s3 0#usize let exp_sign1 <- if ch1 = 45#u8 then ok (-1)#i32 else ok 1#i32 let s5 <- if ch1 = 45#u8 then core.slice.index.Slice.index (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s3 { start := 1#usize } else if ch1 = 43#u8 then core.slice.index.Slice.index (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s3 { start := 1#usize } else ok s3 ok (s5, exp_sign1) let exp_num <- num.imp.dec2flt.decimal_seq.parse_decimal_exponent s4 let signed <- exp_sign * exp_num base + signed else if ch = 69#u8 then let s3 <- core.slice.index.Slice.index (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) rest { start := 1#usize } let b2 <- core.slice.Slice.is_empty s3 let (s4, exp_sign) <- if b2 then ok (s3, 1#i32) else do let ch1 <- Slice.index_usize s3 0#usize let exp_sign1 <- if ch1 = 45#u8 then ok (-1)#i32 else ok 1#i32 let s5 <- if ch1 = 45#u8 then core.slice.index.Slice.index (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s3 { start := 1#usize } else if ch1 = 43#u8 then core.slice.index.Slice.index (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s3 { start := 1#usize } else ok s3 ok (s5, exp_sign1) let exp_num <- num.imp.dec2flt.decimal_seq.parse_decimal_exponent s4 let signed <- exp_sign * exp_num base + signed else ok base obtain ⟨signed, hsignedVal, hsuffixEval⟩ := DecimalSeqExponentSuffixExact rest upper sign expDigits (by unfold SourceValidByteSlice at hsource ⊢ have hlen := congrArg List.length hrestTail simp [tail] at hlen have hrestLe : rest.val.length <= s.val.length := by have hslen := congrArg List.length hslead simp [tail] at hslen omega omega) hExpNe hExpDigits (by simpa [tail] using hrestTail) have hsignedE : signed.val = E := by cases sign with | none => dsimp only [E] exact hsignedVal | some negative => cases negative <;> dsimp only [E] <;> exact hsignedVal have hsuffixValue : forall base result, suffixEval base = ok result -> result.val = base.val + E := by intro base result heval rw [show suffixEval base = base + signed by simpa [suffixEval] using hsuffixEval base] at heval have hadd := IScalar.add_equiv base signed rw [heval] at hadd rw [hadd.2.1, hsignedE] have hscanPoint : exists k : Int, d1.decimal_point.val + (q : Int) + (trim.length : Int) = (sig.length : Int) + k * 4294967296 := by refine ⟨0, ?_⟩ rw [hd1point] have hsig' : sig = trim ++ List.replicate q 0 := by simpa [sig, trim] using hsig have hlen : sig.length = trim.length + q := by have hh := congrArg List.length hsig' simpa using hh simp [d0] push_cast omega have hmarkerIndex : Slice.index_usize rest 0#usize = ok (if upper then 69#u8 else 101#u8) := by cases hr : rest.val with | nil => exact (hrestNe hr).elim | cons b bs => have hbval : b.val = if upper then 69 else 101 := by have hh := congrArg List.head? hrestTail simp [hr, tail] at hh exact hh have hb : b = if upper then 69#u8 else 101#u8 := by cases upper <;> apply UScalar.eq_of_val_eq <;> simpa using hbval simp [Slice.index_usize, hr, hb] have hloop7 := DecimalSeqDuplicateLoops.2.2.2.2 have hmarkerNe : (if upper then 69#u8 else 101#u8) ≠ 46#u8 := by cases upper <;> decide 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 only [bind_tc_ok] at hparse have hd1ne : d1.num_digits ≠ 0#usize := by intro heq have hv := congrArg UScalar.val heq simp [hd1count, d0, sig, hSigNe] at hv have hd1valNe : d1.num_digits.val ≠ 0 := by rw [hd1count] simp [d0, sig, hSigNe] have hcore : (do let consumed <- Slice.len s - Slice.len rest let trailing <- num.imp.dec2flt.decimal_seq.count_trailing_zeros s consumed let p1 <- d1.decimal_point + UScalar.hcast .I32 trailing let kept <- d1.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, d1.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 } d1.digits ok (num.imp.dec2flt.decimal_seq.DecimalSeq.mk nDigits finalPoint flag a)) = ok d := by cases upper <;> simp [core.slice.Slice.is_empty, hrestNe, hmarkerIndex, hmarkerNe, hd1ne, hd1valNe, hloop7, suffixEval, Aeneas.Std.lift] at hparse ⊢ all_goals exact hparse have hfinal := DecimalSeqFinalizationExact s rest d1 d sig trim prefx suffix tail q (sig.length : Int) E suffixEval (by simpa [prefx, suffix] using hprefixBytes) hrestTail hprefx hsuffix hsuffixCount (by simpa [d0] using hd1count) hscanPoint (by simpa [d0] using hd1trunc) (by simpa [sig, trim] using hsig) hscanDigits hsuffixValue hcore rcases hfinal with ⟨hcount, hdigits, htrunc, k, hk⟩ exact ⟨⟨by simpa [trim] using hcount, by simpa [trim] using hdigits, by simpa [trim] using htrunc⟩, k, hk⟩ end dec2flt_model