import Tablet.DecimalDigitTrimDecomposition import Tablet.DecimalSeqDigitRunExact import Tablet.DecimalSeqDuplicateLoops import Tablet.DecimalSeqExponentSuffixExact import Tablet.DecimalSeqFinalizationExact import Tablet.DecimalSeqFractionalIntegerState import Tablet.DecimalSeqFractionalLeadingState import Tablet.DecimalSeqFractionalZeroState import Tablet.DecimalSeqIntegerNoExponentState import Tablet.DecimalSeqIntegerExponentState import Tablet.DecimalSeqIntegerExponentZeroState import Tablet.DecimalSeqLeadingZeroLoopExact import Tablet.DecimalGoalVocabularyDefault import Tablet.SourceValidByteSlice open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false set_option maxHeartbeats 2000000 namespace dec2flt_model -- [TABLET NODE: DecimalSeqLexicalState] theorem DecimalSeqLexicalState : forall (s : Slice Std.U8) (x : DecimalLexeme) (d : num.imp.dec2flt.decimal_seq.DecimalSeq), SourceValidByteSlice s -> x.leadingSign = none -> DecimalGoalVocabularyDefault.validNumeric x -> DecimalGoalVocabularyDefault.bytes s = DecimalGoalVocabularyDefault.renderNumeric x -> num.imp.dec2flt.decimal_seq.parse_decimal_seq s = ok d -> let significand := x.integerDigits ++ match x.fractionDigits with | none => [] | some ds => ds let significant := significand.dropWhile (fun digit => digit == 0) let trimmed := (significant.reverse.dropWhile (fun digit => digit == 0)).reverse let leadingZeros := significand.length - significant.length let P : Int := (x.integerDigits.length : Int) - leadingZeros let E : Int := DecimalGoalVocabularyDefault.accumulatedExplicitExponent x (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 intro s x d hsource hsign hvalid hbytes hparse rcases x with ⟨leading, integer, fraction, exponent⟩ simp only at hsign subst leading simp [DecimalGoalVocabularyDefault] at hvalid hbytes ⊢ rcases hvalid with ⟨hinteger, hfraction, hnonempty, hexponent⟩ cases fraction with | none => simp only [List.append_nil, List.length_nil, Nat.add_zero] at * have hintegerNe : integer ≠ [] := hnonempty.resolve_right id cases exponent with | none => simp only [List.append_nil] at hbytes have hstate := DecimalSeqIntegerNoExponentState s integer d hsource hinteger hintegerNe hbytes hparse rcases hstate with ⟨⟨hcount, hdigits, htrunc⟩, k, hk⟩ refine ⟨by simpa using hcount, ?_, ?_, k, ?_⟩ · simpa [List.map_take] using hdigits · simpa using htrunc rw [hk] have hle := List.length_dropWhile_le (l := integer) (p := fun digit => digit == 0) push_cast omega | some exp => rcases exp with ⟨upper, sign, expDigits⟩ rcases hexponent with ⟨hExpNe, hExpDigits⟩ by_cases hSigNe : integer.dropWhile (fun digit => digit == 0) = [] · have hstate := DecimalSeqIntegerExponentZeroState s integer upper sign expDigits d hsource hinteger hExpNe hExpDigits hSigNe (by simpa [List.append_assoc] using hbytes) hparse rcases hstate with ⟨⟨hcount, hdigits, htrunc⟩, k, hk⟩ refine ⟨by simpa using hcount, ?_, ?_, k, ?_⟩ · simpa [List.map_take] using hdigits · simpa using htrunc · rcases sign with _ | (_ | _) <;> simp_all all_goals have hle := List.length_dropWhile_le (l := integer) (p := fun digit => digit == 0) push_cast omega · have hstate := DecimalSeqIntegerExponentState s integer upper sign expDigits d hsource hinteger hExpNe hExpDigits hSigNe (by simpa [List.append_assoc] using hbytes) hparse rcases hstate with ⟨⟨hcount, hdigits, htrunc⟩, k, hk⟩ refine ⟨by simpa using hcount, ?_, ?_, k, ?_⟩ · simpa [List.map_take] using hdigits · simpa using htrunc · rcases sign with _ | (_ | _) <;> simp_all all_goals have hle := List.length_dropWhile_le (l := integer) (p := fun digit => digit == 0) push_cast omega | some fractional => cases exponent with | none => have hExpNe : forall upper sign digits, (none : Option (Bool × Option Bool × List Nat)) = some (upper, sign, digits) -> digits ≠ [] := by simp have hExpDigits : forall upper sign digits, (none : Option (Bool × Option Bool × List Nat)) = some (upper, sign, digits) -> forall z, z ∈ digits -> z < 10 := by simp have hbytes' : s.val.map (fun b => b.val) = integer.map (fun z => 48 + z) ++ 46 :: fractional.map (fun z => 48 + z) ++ [] := by simpa [List.append_assoc] using hbytes by_cases hSigInt : integer.dropWhile (fun digit => digit == 0) = [] · by_cases hSigFrac : fractional.dropWhile (fun digit => digit == 0) = [] · have hstate := DecimalSeqFractionalZeroState s integer fractional [] 0 none d hsource hinteger hfraction hExpNe hExpDigits (by simp) (by simp) (by simp) (by simp) hSigInt hSigFrac hbytes' hparse simpa [List.map_take] using hstate · have hstate := DecimalSeqFractionalLeadingState s integer fractional [] 0 none d hsource hinteger hfraction hExpNe hExpDigits (by simp) (by simp) (by simp) (by simp) hSigInt hSigFrac hbytes' hparse simpa [List.map_take] using hstate · have hstate := DecimalSeqFractionalIntegerState s integer fractional [] 0 none d hsource hinteger hfraction hExpNe hExpDigits (by simp) (by simp) (by simp) (by simp) hSigInt hbytes' hparse simpa [List.map_take] using hstate | some exp => rcases exp with ⟨upper, sign, expDigits⟩ rcases hexponent with ⟨hexpNe, hexpDigits⟩ let expTail : List Nat := (if upper then 69 else 101) :: (match sign with | none => [] | some negative => [if negative then 45 else 43]) ++ expDigits.map (fun z => 48 + z) have hExpNe : forall u sg digits, (some (upper, sign, expDigits) : Option (Bool × Option Bool × List Nat)) = some (u, sg, digits) -> digits ≠ [] := by intro u sg digits he cases he exact hexpNe have hExpDigits : forall u sg digits, (some (upper, sign, expDigits) : Option (Bool × Option Bool × List Nat)) = some (u, sg, digits) -> forall z, z ∈ digits -> z < 10 := by intro u sg digits he cases he exact hexpDigits have htailSome : forall u sg digits, (some (upper, sign, expDigits) : Option (Bool × Option Bool × List Nat)) = some (u, sg, digits) -> expTail = (if u then 69 else 101) :: (match sg with | none => [] | some negative => [if negative then 45 else 43]) ++ digits.map (fun z => 48 + z) := by intro u sg digits he cases he rfl have hESome : forall u sg digits, (some (upper, sign, expDigits) : Option (Bool × Option Bool × List Nat)) = some (u, sg, digits) -> (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)) = let n : Nat := digits.foldl (fun n digit => if n < 65536 then 10 * n + digit else n) 0 match sg with | some true => -(n : Int) | _ => (n : Int) := by intro u sg digits he simp only [Option.some.injEq, Prod.mk.injEq] at he rcases he with ⟨rfl, rfl, rfl⟩ rfl have hbytes' : s.val.map (fun b => b.val) = integer.map (fun z => 48 + z) ++ 46 :: fractional.map (fun z => 48 + z) ++ expTail := by simpa [expTail, List.append_assoc] using hbytes by_cases hSigInt : integer.dropWhile (fun digit => digit == 0) = [] · by_cases hSigFrac : fractional.dropWhile (fun digit => digit == 0) = [] · have hstate := DecimalSeqFractionalZeroState s integer fractional expTail (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)) (some (upper, sign, expDigits)) d hsource hinteger hfraction hExpNe hExpDigits (by simp) htailSome (by simp) hESome hSigInt hSigFrac hbytes' hparse simpa [expTail, List.map_take] using hstate · have hstate := DecimalSeqFractionalLeadingState s integer fractional expTail (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)) (some (upper, sign, expDigits)) d hsource hinteger hfraction hExpNe hExpDigits (by simp) htailSome (by simp) hESome hSigInt hSigFrac hbytes' hparse simpa [expTail, List.map_take] using hstate · have hstate := DecimalSeqFractionalIntegerState s integer fractional expTail (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)) (some (upper, sign, expDigits)) d hsource hinteger hfraction hExpNe hExpDigits (by simp) htailSome (by simp) hESome hSigInt hbytes' hparse simpa [expTail, List.map_take] using hstate end dec2flt_model