import Tablet.DecimalDigitTrimDecomposition import Tablet.DecimalSeqDigitRunAccumulate import Tablet.DecimalSeqDuplicateLoops import Tablet.DecimalSeqExponentResidualPointwise import Tablet.DecimalSeqFinalizationExact import Tablet.DecimalSeqFractionalFinish import Tablet.DecimalSeqFractionalLeadingParserCore import Tablet.DecimalSeqI32HCastCongruence import Tablet.DecimalSeqLeadingZeroLoopExact import Tablet.DecimalSeqRenderedSplit import Tablet.DecimalSeqScalarScanExact import Tablet.SourceValidByteSlice open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false set_option maxHeartbeats 3000000 set_option maxRecDepth 100000 namespace dec2flt_model -- [TABLET NODE: DecimalSeqFractionalLeadingState] theorem DecimalSeqFractionalLeadingState (s : Slice Std.U8) (I F tail : List Nat) (E : Int) (exponent : Option (Bool × Option Bool × List Nat)) (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (hsource : SourceValidByteSlice s) (hIdigits : forall z, z ∈ I -> z < 10) (hFdigits : forall z, z ∈ F -> z < 10) (hExpNe : forall upper sign digits, exponent = some (upper, sign, digits) -> digits ≠ []) (hExpDigits : forall upper sign digits, exponent = some (upper, sign, digits) -> forall z, z ∈ digits -> z < 10) (htailNone : exponent = none -> tail = []) (htailSome : forall upper sign digits, exponent = some (upper, sign, digits) -> tail = (if upper then 69 else 101) :: (match sign with | none => [] | some negative => [if negative then 45 else 43]) ++ digits.map (fun z => 48 + z)) (hENone : exponent = none -> E = 0) (hESome : forall upper sign digits, exponent = some (upper, sign, digits) -> E = let n : Nat := digits.foldl (fun n digit => if n < 65536 then 10 * n + digit else n) 0 match sign with | some true => -(n : Int) | _ => (n : Int)) (hSigI : I.dropWhile (fun digit => digit == 0) = []) (hSigF : F.dropWhile (fun digit => digit == 0) ≠ []) (hbytes : s.val.map (fun b => b.val) = I.map (fun z => 48 + z) ++ 46 :: F.map (fun z => 48 + z) ++ tail) (hparse : num.imp.dec2flt.decimal_seq.parse_decimal_seq s = ok d) : let significant := (I ++ F).dropWhile (fun digit => digit == 0) let trimmed := (significant.reverse.dropWhile (fun digit => digit == 0)).reverse let z := (I ++ F).length - significant.length let P : Int := (I.length : Int) - z (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 = P + E + k * 4294967296 := by -- BODY dsimp only let sig := (I ++ F).dropWhile (fun digit => digit == 0) let trim := (sig.reverse.dropWhile (fun digit => digit == 0)).reverse suffices hgoal : (d.num_digits.val = min trim.length 768 /\ (d.digits.val.take d.num_digits.val).map (fun z => z.val) = trim.take d.num_digits.val /\ (d.truncated = true <-> 768 < trim.length)) /\ exists k : Int, d.decimal_point.val = (I.length : Int) - ((I ++ F).length - sig.length : Nat) + E + k * 4294967296 by simpa [sig, trim] using hgoal obtain ⟨z, q, hz, hq, hIF, hsigtrim, hsigstop, htrimstop⟩ := DecimalDigitTrimDecomposition (I ++ F) obtain ⟨zi, qi, hzi, hqi, hI, hIsigTail, hIsigstop, hItrimstop⟩ := DecimalDigitTrimDecomposition I obtain ⟨L, qf, hL, hqf, hF, hFsigTail, hFsigstop, hFtrimstop⟩ := DecimalDigitTrimDecomposition F let fsig := F.dropWhile (fun digit => digit == 0) have hIzeros : I = List.replicate zi 0 := by simpa [hSigI] using hI have hziLen : zi = I.length := by have hh := congrArg List.length hIzeros simpa using hh.symm have hsig : sig = fsig := by simp [sig, fsig, List.dropWhile_append, hSigI] have hzTotal : z = I.length + L := by have hIFfsig : I ++ F = List.replicate z 0 ++ fsig := by calc I ++ F = List.replicate z 0 ++ sig := by simpa [sig] using hIF _ = List.replicate z 0 ++ fsig := by rw [hsig] have hFfsig : F = List.replicate L 0 ++ fsig := by simpa [fsig] using hF have hlenIF := congrArg List.length hIFfsig have hlenF := congrArg List.length hFfsig simp at hlenIF hlenF omega have hIbytes : I.map (fun n => 48 + n) = List.replicate zi 48 := by rw [hIzeros] simp have hslead : s.val.map (fun b => b.val) = List.replicate zi 48 ++ 46 :: F.map (fun n => 48 + n) ++ tail := by rw [hbytes, hIbytes] obtain ⟨s1, hlead, hs1⟩ := DecimalSeqLeadingZeroLoopExact s zi (46 :: F.map (fun n => 48 + n) ++ tail) (by simpa [List.append_assoc] using hslead) (by simp) 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 } obtain ⟨pointRest, d1, hscan1, hpointRest, hd1count, hd1point, hd1trunc, hd1old, hd1written, hd1outside⟩ := DecimalSeqScalarScanExact s1 d0 [] (46 :: F.map (fun n => 48 + n) ++ tail) (by simpa using hs1) (by simp) (by simp) (by simp [d0]) have hd1zero : d1.num_digits = 0#usize := by apply UScalar.eq_of_val_eq simpa [d0] using hd1count have hpointRestNe : pointRest.val ≠ [] := by intro he have hh := congrArg List.length hpointRest simp [he] at hh have hpointIndex : Slice.index_usize pointRest 0#usize = ok 46#u8 := by cases hr : pointRest.val with | nil => exact (hpointRestNe hr).elim | cons b bs => have hbval : b.val = 46 := by have hh := congrArg List.head? hpointRest simp [hr] at hh exact hh have hb : b = 46#u8 := UScalar.eq_of_val_eq hbval simp [Slice.index_usize, hr, hb] let afterPoint := Slice.drop pointRest 1#usize have hdropPoint : core.slice.index.SliceIndexRangeFromUsizeSlice.index { start := 1#usize } pointRest = ok afterPoint := by simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index, Slice.length, afterPoint, hpointRestNe] have hafterPoint : afterPoint.val.map (fun b => b.val) = F.map (fun n => 48 + n) ++ tail := by simp [afterPoint, Slice.drop, List.map_drop, hpointRest] have hafterLead : afterPoint.val.map (fun b => b.val) = List.replicate L 48 ++ fsig.map (fun n => 48 + n) ++ tail := by rw [hafterPoint, hF, List.map_append] simp [fsig, List.append_assoc] have hfracStop : (fsig.map (fun n => 48 + n) ++ tail)[0]? ≠ some 48 := by rcases hFsigstop with hempty | hstop · exact (hSigF (by simpa [fsig] using hempty)).elim · intro heq apply hstop cases hfs : fsig with | nil => exact (hSigF (by simpa [fsig] using hfs)).elim | cons a restF => have ha : a = 0 := by simp [hfs] at heq; omega subst a change fsig.head? = some 0 simp [hfs] obtain ⟨sigStart, hfracLead, hsigStart⟩ := DecimalSeqLeadingZeroLoopExact afterPoint L (fsig.map (fun n => 48 + n) ++ tail) (by simpa [List.append_assoc] using hafterLead) (Or.inr hfracStop) have hloop2 : num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop2 afterPoint = ok sigStart := by rw [DecimalSeqDuplicateLoops.1] exact hfracLead have htailForm : tail = [] \/ exists marker rest, tail = marker :: rest /\ (marker = 69 \/ marker = 101) := by cases he : exponent with | none => left; exact htailNone he | some data => rcases data with ⟨upper, sign, digits⟩ right refine ⟨if upper then 69 else 101, (match sign with | none => [] | some negative => [if negative then 45 else 43]) ++ digits.map (fun z => 48 + z), ?_, ?_⟩ · exact htailSome upper sign digits he · cases upper <;> simp have hbound2 : d1.num_digits.val + fsig.length + tail.length <= UScalar.max .Usize := by have hsmax : s.val.length <= UScalar.max .Usize := by unfold SourceValidByteSlice at hsource simp [UScalar.max, Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits] omega have hlen := congrArg List.length hbytes simp only [List.length_map, List.length_append, List.length_cons] at hlen have hfsle : fsig.length <= F.length := by simpa [fsig] using (List.length_dropWhile_le (l := F) (p := fun digit => digit == 0)) simp [hd1zero] omega have hd1prefix : (d1.digits.val.take (min (([] : List Nat).length) 768)).map (fun b => b.val) = ([] : List Nat).take (min (([] : List Nat).length) 768) := by simp obtain ⟨bulkRest, rest, bulkCount, bulkArray, d2, hbulk, hscan2, hrest, hd2count, hd2point, hd2trunc, hd2prefix⟩ := DecimalSeqDigitRunAccumulate sigStart d1 ([] : List Nat) fsig tail hsigStart (fun n hn => hFdigits n (by rw [hF]; exact List.mem_append_right _ hn)) htailForm hbound2 (by simpa [hd1zero]) hd1prefix have hloop3 : num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop3 sigStart d1.num_digits d1.digits = ok (bulkRest, bulkCount, bulkArray) := hbulk have hd2sigCount : d2.num_digits.val = sig.length := by rw [hd2count, hsig] simp [hd1zero] have hd2sigPrefix : (d2.digits.val.take (min sig.length 768)).map (fun b => b.val) = sig.take (min sig.length 768) := by simpa [hsig] using hd2prefix have hrestTail : rest.val.map (fun b => b.val) = tail := hrest have hsigNe : sig ≠ [] := by simpa [hsig, fsig] using hSigF have hsigTrim : sig = trim ++ List.replicate q 0 := by simpa [sig, trim] using hsigtrim have htrimNe : trim ≠ [] := by intro ht have hszeros : sig = List.replicate q 0 := by simpa [ht] using hsigTrim rcases hsigstop with hs0 | hs0 · exact hsigNe (by simpa [sig] using hs0) · cases q with | zero => exact hsigNe (by simpa using hszeros) | succ q => apply hs0; simp [sig, hszeros, List.replicate_succ] have htrimLast : trim.getLast? ≠ some 0 := by rcases htrimstop with ht | ht · exact (htrimNe (by simpa [trim] using ht)).elim · simpa [trim] using ht let A := List.replicate z 0 ++ trim have hIFA : I ++ F = A ++ List.replicate q 0 := by calc I ++ F = List.replicate z 0 ++ sig := by simpa [sig] using hIF _ = List.replicate z 0 ++ (trim ++ List.replicate q 0) := by rw [hsigTrim] _ = (List.replicate z 0 ++ trim) ++ List.replicate q 0 := by simp [List.append_assoc] _ = A ++ List.replicate q 0 := by rfl have hAne : A ≠ [] := by simp [A, htrimNe] have hAlast : A.getLast? ≠ some 0 := by simpa [A, htrimNe] using htrimLast obtain ⟨prefx, suffix, hrend, hprefx', hsuffix, hsuffixCount⟩ := DecimalSeqRenderedSplit I F A q hIFA hAne hAlast have hprefx : prefx = [] \/ exists pre b, prefx = pre ++ [b] /\ b ≠ 48 /\ b ≠ 46 := Or.inr hprefx' have hstart : s.val.map (fun b => b.val) = prefx ++ suffix ++ tail := by rw [hbytes, hrend] have hrestSource : SourceValidByteSlice rest := by unfold SourceValidByteSlice at hsource ⊢ have hslen := congrArg List.length hbytes have hrlen := congrArg List.length hrestTail simp at hslen hrlen omega let suffixEval : Std.I32 -> Result Std.I32 := DecimalSeqExponentEval rest have hsuffixValue : forall base result, suffixEval base = ok result -> result.val = base.val + E := by as_aux_lemma => intro base result heval apply DecimalSeqExponentResidualPointwise rest exponent E hrestSource hExpNe hExpDigits (fun he => by simpa using hrestTail.trans (htailNone he)) (fun upper sign digits he => by simpa using hrestTail.trans (htailSome upper sign digits he)) hENone hESome base result simpa [suffixEval] using heval have hd2valNe : d2.num_digits.val ≠ 0 := by rw [hd2sigCount] simp [hsigNe] obtain ⟨base, hb, hcore0⟩ := DecimalSeqFractionalLeadingParserCore s s1 pointRest afterPoint sigStart bulkRest rest d0 d1 d2 d bulkCount bulkArray (by rfl) hlead hscan1 hpointRestNe hpointIndex hdropPoint hd1zero hloop2 hloop3 hscan2 hd2valNe hparse let dbase : num.imp.dec2flt.decimal_seq.DecimalSeq := { d2 with decimal_point := base } obtain ⟨kr, hkr⟩ := DecimalSeqI32HCastCongruence (Slice.len rest) obtain ⟨kf, hkf⟩ := DecimalSeqI32HCastCongruence (Slice.len afterPoint) have hbaseVal : base.val = (UScalar.hcast .I32 (Slice.len rest)).val - (UScalar.hcast .I32 (Slice.len afterPoint)).val := by have hh := IScalar.sub_equiv (UScalar.hcast .I32 (Slice.len rest) : Std.I32) (UScalar.hcast .I32 (Slice.len afterPoint) : Std.I32) rw [hb] at hh exact hh.2.1 have hlenRest : (Slice.len rest).val = tail.length := by have hh := congrArg List.length hrestTail simpa [Slice.len] using hh have hlenAfter : (Slice.len afterPoint).val = F.length + tail.length := by have hh := congrArg List.length hafterPoint simpa [Slice.len] using hh have hscanPoint : exists k : Int, dbase.decimal_point.val + (q : Int) + (trim.length : Int) = ((I.length : Int) - ((I ++ F).length - sig.length : Nat)) + k * 4294967296 := by refine ⟨kr - kf, ?_⟩ simp only [dbase] rw [hbaseVal, hkr, hkf, hlenRest, hlenAfter] have hst := congrArg List.length hsigTrim simp at hst have hFlen := congrArg List.length hF have hsiglen := congrArg List.length hsig simp [fsig] at hFlen hsiglen have hz' : (I ++ F).length - sig.length = z := by simpa [sig] using hz.symm rw [hz', hzTotal] push_cast ring_nf omega 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 <- dbase.decimal_point + UScalar.hcast .I32 trailing let kept <- dbase.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, dbase.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 } dbase.digits ok (num.imp.dec2flt.decimal_seq.DecimalSeq.mk nDigits finalPoint flag a)) = ok d := by simpa only [DecimalSeqFractionalFinish, dbase, suffixEval] using hcore0 have hfinal := DecimalSeqFinalizationExact s rest dbase d sig trim prefx suffix tail q ((I.length : Int) - ((I ++ F).length - sig.length : Nat)) E suffixEval hstart hrestTail hprefx hsuffix hsuffixCount (by simpa [dbase] using hd2sigCount) hscanPoint (by simpa [dbase, hd2trunc, hd1trunc, d0]) hsigTrim (by simpa [dbase] using hd2sigPrefix) hsuffixValue hcore rcases hfinal with ⟨hcount, hdigits, htrunc, k, hk⟩ exact ⟨⟨hcount, hdigits, htrunc⟩, k, hk⟩ end dec2flt_model