import Tablet.NumberHelperScientificSuffixSharpSafeLength import Tablet.NumberHelperScientificSignedSuffixSafeLength import Tablet.NumberHelperSplitExponentSuffix open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: NumberHelperScientificRenderedSuffixSafeLength] theorem NumberHelperScientificRenderedSuffixSafeLength (s : Slice Std.U8) (sign : Option Bool) (digits : List Nat) (hne : digits != []) (hdigits : forall d, List.Mem d digits -> d < 10) (hs : List.map (fun b : Std.U8 => b.val) s.val = (match sign with | none => [] | some negative => [if negative then 45 else 43]) ++ List.map (fun d : Nat => 48 + d) digits) (hlen : s.val.length <= 9223372036854120458) : exists (exponent : Std.I64) (rest : Slice Std.U8), num.imp.dec2flt.parse.parse_scientific s = ok (some exponent, rest) /\ rest.val = [] /\ -655359 <= exponent.val /\ exponent.val <= 655359 /\ (digits.length <= 5 -> -99999 <= exponent.val /\ exponent.val <= 99999) /\ (sign ≠ some true -> 0 <= exponent.val) := by -- BODY obtain ⟨digitBytes, hsval, hmap⟩ := NumberHelperSplitExponentSuffix sign s.val digits hs have hbyteDigits : forall b, List.Mem b digitBytes -> 48 <= b.val /\ b.val <= 57 := by intro b hb have hbmap : b.val ∈ List.map (fun c : Std.U8 => c.val) digitBytes := List.mem_map_of_mem hb rw [hmap] at hbmap obtain ⟨d, hd, hdval⟩ := List.mem_map.mp hbmap have hdlt := hdigits d hd omega have hbytesNe : digitBytes != [] := by simp only [bne_iff_ne] intro hnil subst digitBytes cases digits with | nil => simp at hne | cons d ds => simp at hmap have hlength : digitBytes.length = digits.length := by have := congrArg List.length hmap simpa using this have finish : forall (sgn : Option Bool), s.val = (match sgn with | none => [] | some negative => [if negative then 45#u8 else 43#u8]) ++ digitBytes -> exists (exponent : Std.I64) (rest : Slice Std.U8), num.imp.dec2flt.parse.parse_scientific s = ok (some exponent, rest) /\ rest.val = [] /\ -655359 <= exponent.val /\ exponent.val <= 655359 /\ (digits.length <= 5 -> -99999 <= exponent.val /\ exponent.val <= 99999) /\ (sgn ≠ some true -> 0 <= exponent.val) := by intro sgn hval obtain ⟨exponent, rest, hparse, hrest, hlo, hhi, hshort⟩ := NumberHelperScientificSuffixSharpSafeLength s sgn digitBytes hbytesNe hbyteDigits hval hlen obtain ⟨signedExponent, signedRest, hsigned, _, _, _, hnonnegative⟩ := NumberHelperScientificSignedSuffixSafeLength s sgn digitBytes hbytesNe hbyteDigits hval hlen rw [hparse] at hsigned have he : signedExponent = exponent := by injection hsigned with hp injection hp with ho _ injection ho with hx exact hx.symm subst signedExponent exact ⟨exponent, rest, hparse, hrest, hlo, hhi, fun h => hshort (by omega), hnonnegative⟩ cases sign with | none => apply finish none simpa using hsval | some negative => cases negative · apply finish (some false) simpa using hsval · apply finish (some true) simpa using hsval end dec2flt_model