import Tablet.DecimalGoalVocabularyDefault open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: NumberHelperDecimalPrefixInterval] theorem NumberHelperDecimalPrefixInterval (headDigits suffix : List Nat) (hdigits : forall d, List.Mem d suffix -> d < 10) (e : Int) : let step : Nat -> Nat -> Nat := fun a d => 10 * a + d let m := headDigits.foldl step 0 let z := (headDigits ++ suffix).foldl step 0 (m : Real) * (10 : Real) ^ (e + (suffix.length : Int)) <= (z : Real) * (10 : Real) ^ e /\ (z : Real) * (10 : Real) ^ e < ((m + 1 : Nat) : Real) * (10 : Real) ^ (e + (suffix.length : Int)) := by -- BODY dsimp only let step : Nat -> Nat -> Nat := fun a d => 10 * a + d have fold_shift : forall (ds : List Nat) (a : Nat), ds.foldl step a = a * 10 ^ ds.length + ds.foldl step 0 := by intro ds induction ds with | nil => intro a; simp | cons d ds ih => intro a simp only [List.foldl_cons, List.length_cons] rw [ih (step a d), show step 0 d = d by simp [step], ih d] simp only [step, Nat.pow_succ] ring have suffix_upper : forall (ds : List Nat) (a : Nat), (forall d, List.Mem d ds -> d < 10) -> ds.foldl step a < (a + 1) * 10 ^ ds.length := by intro ds induction ds with | nil => intro a h; simp | cons d ds ih => intro a h have hd := h d List.mem_cons_self have hds : forall c, List.Mem c ds -> c < 10 := by intro c hc exact h c (List.mem_cons_of_mem d hc) simp only [List.foldl_cons, List.length_cons, Nat.pow_succ] have hrec := ih (step a d) hds have hcoeff : step a d + 1 <= 10 * (a + 1) := by simp only [step] omega have hmul := Nat.mul_le_mul_right (10 ^ ds.length) hcoeff exact hrec.trans_le (by simpa [Nat.mul_assoc, Nat.mul_comm, Nat.mul_left_comm] using hmul) let m := headDigits.foldl step 0 let r := suffix.foldl step 0 let z := (headDigits ++ suffix).foldl step 0 have hz : z = m * 10 ^ suffix.length + r := by simp only [z, m, r, List.foldl_append] exact fold_shift suffix (headDigits.foldl step 0) have hrUpper : r < 10 ^ suffix.length := by have := suffix_upper suffix 0 hdigits simpa [r] using this have hNatLower : m * 10 ^ suffix.length <= z := by omega have hNatUpper : z < (m + 1) * 10 ^ suffix.length := by rw [hz] calc m * 10 ^ suffix.length + r < m * 10 ^ suffix.length + 10 ^ suffix.length := Nat.add_lt_add_left hrUpper _ _ = (m + 1) * 10 ^ suffix.length := by ring have hpowPos : 0 < (10 : Real) ^ e := zpow_pos (by norm_num) e constructor · calc (m : Real) * (10 : Real) ^ (e + (suffix.length : Int)) = ((m * 10 ^ suffix.length : Nat) : Real) * (10 : Real) ^ e := by rw [zpow_add₀ (by norm_num : (10 : Real) ≠ 0), zpow_natCast] norm_num ring _ <= (z : Real) * (10 : Real) ^ e := by apply mul_le_mul_of_nonneg_right · exact_mod_cast hNatLower · exact hpowPos.le · calc (z : Real) * (10 : Real) ^ e < (((m + 1) * 10 ^ suffix.length : Nat) : Real) * (10 : Real) ^ e := by apply mul_lt_mul_of_pos_right exact_mod_cast hNatUpper exact hpowPos _ = ((m + 1 : Nat) : Real) * (10 : Real) ^ (e + (suffix.length : Int)) := by rw [zpow_add₀ (by norm_num : (10 : Real) ≠ 0), zpow_natCast] norm_num ring end dec2flt_model