import Tablet.DecimalGoalVocabularyDefault import Tablet.DecimalDigitTrimDecomposition import Tablet.P472_T1_DecimalValueBounds import Tablet.P616_T1_CutoffFoldComparison import Tablet.P616_T1_BinaryDecimalSeparation import Tablet.P616_T1_ExtremeRounding import Tablet.ExponentValueAgreement import Tablet.RoundingDeterminism import Mathlib.Tactic.NormNum import Mathlib.Tactic.Positivity open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P616_T1_LexemeExactTransportB] theorem P616_T1_LexemeExactTransportB (x : DecimalLexeme) (hx : DecimalGoalVocabularyDefault.validNumeric x) (hB : (DecimalGoalVocabularyDefault.renderNumeric x).length <= 576460752303421440) : forall target : F64, DecimalGoalVocabularyDefault.roundsMagnitude (DecimalGoalVocabularyDefault.accumulatedMagnitude x) target ↔ DecimalGoalVocabularyDefault.roundsMagnitude (DecimalGoalVocabularyDefault.exactMagnitude x) target := by -- BODY let S := x.integerDigits ++ match x.fractionDigits with | none => [] | some ds => ds let f := match x.fractionDigits with | none => 0 | some ds => ds.length let C := S.dropWhile (fun d => d == 0) let M := C.foldl (fun n d => 10 * n + d) 0 have hSL : S.length <= (DecimalGoalVocabularyDefault.renderNumeric x).length := by cases hf : x.fractionDigits <;> simp only [S, DecimalGoalVocabularyDefault, hf, List.length_append, List.length_map, List.length_nil, List.length_cons] <;> omega have hCL : C.length <= 576460752303421440 := le_trans (List.length_dropWhile_le _ _) (le_trans hSL hB) have hfL : f <= 576460752303421440 := by have hsf : f <= S.length := by cases hf : x.fractionDigits <;> simp [f, S, hf] exact le_trans hsf (le_trans hSL hB) have hfold (ds : List Nat) : (ds.dropWhile (fun d => d == 0)).foldl (fun n d => 10 * n + d) 0 = ds.foldl (fun n d => 10 * n + d) 0 := by induction ds with | nil => rfl | cons d ds ih => by_cases hz : d = 0 · subst d; simpa using ih · simp [List.dropWhile, beq_eq_false_iff_ne.mpr hz] have hacc : DecimalGoalVocabularyDefault.accumulatedMagnitude x = (M : Real) * (10 : Real) ^ (DecimalGoalVocabularyDefault.accumulatedExplicitExponent x - (f : Int)) := by simp only [DecimalGoalVocabularyDefault] cases hf : x.fractionDigits <;> simp only [S, f, M, C, hf, hfold, List.append_nil, Nat.cast_zero] have hexact : DecimalGoalVocabularyDefault.exactMagnitude x = (M : Real) * (10 : Real) ^ (DecimalGoalVocabularyDefault.mathExplicitExponent x - (f : Int)) := by simp only [DecimalGoalVocabularyDefault] cases hf : x.fractionDigits <;> simp only [S, f, M, C, hf, hfold, List.append_nil, Nat.cast_zero] have hacc0 : 0 <= DecimalGoalVocabularyDefault.accumulatedMagnitude x := by rw [hacc] exact mul_nonneg (Nat.cast_nonneg _) (le_of_lt (zpow_pos (by norm_num) _)) have hexact0 : 0 <= DecimalGoalVocabularyDefault.exactMagnitude x := by rw [hexact] exact mul_nonneg (Nat.cast_nonneg _) (le_of_lt (zpow_pos (by norm_num) _)) have common (w : F64) (ha : DecimalGoalVocabularyDefault.roundsMagnitude (DecimalGoalVocabularyDefault.accumulatedMagnitude x) w) (hb : DecimalGoalVocabularyDefault.roundsMagnitude (DecimalGoalVocabularyDefault.exactMagnitude x) w) : forall target : F64, DecimalGoalVocabularyDefault.roundsMagnitude (DecimalGoalVocabularyDefault.accumulatedMagnitude x) target ↔ DecimalGoalVocabularyDefault.roundsMagnitude (DecimalGoalVocabularyDefault.exactMagnitude x) target := by intro target constructor · intro ht have heq := RoundingDeterminism _ target w hacc0 ht ha simpa [heq] using hb · intro ht have heq := RoundingDeterminism _ target w hexact0 ht hb simpa [heq] using ha by_cases hC : C = [] · have hM : M = 0 := by simp [M, hC] intro target rw [hacc, hexact, hM] simp only [Nat.cast_zero, zero_mul] have hdS : ∀ d ∈ S, d < 10 := by have hvalid := hx dsimp only [DecimalGoalVocabularyDefault] at hvalid intro d hd obtain hi | hf := List.mem_append.mp hd · exact hvalid.1 d hi · cases hfrac : x.fractionDigits with | none => simp [hfrac] at hf | some ds => have hd : ∀ d ∈ ds, d < 10 := by simpa only [hfrac] using hvalid.2.1 exact hd d (by simpa only [hfrac] using hf) have hdC : ∀ d ∈ C, d < 10 := fun d hd => hdS d (List.dropWhile_subset _ hd) obtain ⟨z, zs, hc⟩ := List.exists_cons_of_ne_nil hC have hz : 1 <= z := by have hs := List.head_dropWhile_not (fun d => d == 0) (l := S) hC have hh : C.head hC = z := by simp [hc] change (C.head hC == 0) = false at hs rw [hh] at hs have hz0 : z ≠ 0 := by simpa using hs omega have hbnd (p : Int) : (10 : Real) ^ (p - 1) <= (M : Real) * (10 : Real) ^ (p - (C.length : Int)) ∧ (M : Real) * (10 : Real) ^ (p - (C.length : Int)) < (10 : Real) ^ p := by have hh := P472_T1_DecimalValueBounds C z zs hc hz hdC p dsimp only at hh have hzR : (1 : Real) <= z := by exact_mod_cast hz have hpow := mul_le_mul_of_nonneg_right hzR (le_of_lt (zpow_pos (show (0 : Real) < 10 by norm_num) (p - 1))) refine ⟨?_, hh.2⟩ exact le_trans (by simpa only [one_mul] using hpow) hh.1 have hOver (p : Int) (hp : 2049 <= p) : (2 : Real)^1024 - (2 : Real)^970 <= (M : Real) * (10 : Real)^(p - (C.length : Int)) := by have hbase : (2 : Real)^(1024 : Nat) < (10 : Real)^(309 : Nat) := by exact_mod_cast P616_T1_BinaryDecimalSeparation.1 have hten : (10 : Real)^(309 : Nat) <= (10 : Real)^(p-1) := by have hh := zpow_le_zpow_right₀ (show (1 : Real) <= 10 by norm_num) (show (309 : Int) <= p - 1 by omega) simpa only [zpow_natCast] using hh exact le_trans (sub_le_self _ (by positivity)) (le_trans (le_of_lt hbase) (le_trans hten (hbnd p).1)) have hUnder (p : Int) (hp : p <= -2048) : (M : Real) * (10 : Real)^(p - (C.length : Int)) <= 1 / (2 : Real)^(1075 : Nat) := by have hbase : (2 : Real)^(1075 : Nat) < (10 : Real)^(325 : Nat) := by exact_mod_cast P616_T1_BinaryDecimalSeparation.2 have hten := zpow_le_zpow_right₀ (show (1 : Real) <= 10 by norm_num) (show p <= (-325 : Int) by omega) have hinv : (10 : Real)^(-325 : Int) = 1 / (10 : Real)^(325 : Nat) := by rw [zpow_neg, inv_eq_one_div] rfl have hrecip : 1 / (10 : Real)^(325 : Nat) < 1 / (2 : Real)^(1075 : Nat) := one_div_lt_one_div_of_lt (by positivity) hbase exact le_of_lt (lt_trans (lt_of_lt_of_le (hbnd p).2 hten) (by simpa only [hinv] using hrecip)) cases he : x.exponent with | none => have heq : DecimalGoalVocabularyDefault.accumulatedExplicitExponent x = DecimalGoalVocabularyDefault.mathExplicitExponent x := by simp [DecimalGoalVocabularyDefault, he] intro target exact (ExponentValueAgreement x hx heq target).symm | some exponent => obtain ⟨upper, sign, ds⟩ := exponent let a := ds.foldl (fun n d => if n < 576460752303423488 then 10 * n + d else n) 0 let b := ds.foldl (fun n d => 10 * n + d) 0 have heacc : DecimalGoalVocabularyDefault.accumulatedExplicitExponent x = if sign = some true then -(a : Int) else (a : Int) := by cases sign with | none => simp [DecimalGoalVocabularyDefault, he, a] | some neg => cases neg <;> simp [DecimalGoalVocabularyDefault, he, a] have hemath : DecimalGoalVocabularyDefault.mathExplicitExponent x = if sign = some true then -(b : Int) else (b : Int) := by cases sign with | none => simp [DecimalGoalVocabularyDefault, he, b] | some neg => cases neg <;> simp [DecimalGoalVocabularyDefault, he, b] have hcFold := P616_T1_CutoffFoldComparison 576460752303423488 ds change a <= b ∧ (a = b ∨ 576460752303423488 <= a) at hcFold obtain ⟨hab, heq | hcap⟩ := hcFold · have heqexp : DecimalGoalVocabularyDefault.accumulatedExplicitExponent x = DecimalGoalVocabularyDefault.mathExplicitExponent x := by rw [heacc, hemath, heq] intro target exact (ExponentValueAgreement x hx heqexp target).symm · by_cases hneg : sign = some true · let pa : Int := (C.length : Int) - f - a let pb : Int := (C.length : Int) - f - b have hpa : pa <= -2048 := by dsimp [pa]; omega have hpb : pb <= -2048 := by dsimp [pb]; omega have haccP : DecimalGoalVocabularyDefault.accumulatedMagnitude x = (M : Real) * (10 : Real)^(pa - (C.length : Int)) := by rw [hacc, heacc, if_pos hneg] congr 1 congr 1 dsimp [pa]; omega have hexactP : DecimalGoalVocabularyDefault.exactMagnitude x = (M : Real) * (10 : Real)^(pb - (C.length : Int)) := by rw [hexact, hemath, if_pos hneg] congr 1 congr 1 dsimp [pb]; omega apply common (0#u64) · apply P616_T1_ExtremeRounding.2 _ hacc0 rw [haccP] exact hUnder pa hpa · apply P616_T1_ExtremeRounding.2 _ hexact0 rw [hexactP] exact hUnder pb hpb · let pa : Int := (C.length : Int) - f + a let pb : Int := (C.length : Int) - f + b have hn : 1 <= C.length := by rw [hc]; simp have hpa : 2049 <= pa := by dsimp [pa]; omega have hpb : 2049 <= pb := by dsimp [pb]; omega have haccP : DecimalGoalVocabularyDefault.accumulatedMagnitude x = (M : Real) * (10 : Real)^(pa - (C.length : Int)) := by rw [hacc, heacc, if_neg hneg] congr 1 congr 1 dsimp [pa]; omega have hexactP : DecimalGoalVocabularyDefault.exactMagnitude x = (M : Real) * (10 : Real)^(pb - (C.length : Int)) := by rw [hexact, hemath, if_neg hneg] congr 1 congr 1 dsimp [pb]; omega apply common (9218868437227405312#u64) · apply P616_T1_ExtremeRounding.1 _ hacc0 rw [haccP] exact hOver pa hpa · apply P616_T1_ExtremeRounding.1 _ hexact0 rw [hexactP] exact hOver pb hpb end dec2flt_model