import Tablet.NumberHelperDecimalPrefixInterval open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: DecimalSeqFractionalDigitInterval] theorem DecimalSeqFractionalDigitInterval (digits : List Nat) (q : Nat) (hq : q < digits.length) (hdigits : forall z, z ∈ digits -> z < 10) (hlast : digits.getLast? != some 0) : let E : List Nat -> Nat := fun xs => xs.foldl (fun n z => 10 * n + z) 0 let N := E (digits.take q) let V : Real := (E digits : Real) * (10 : Real) ^ ((q : Int) - (digits.length : Int)) let f := digits[q] (N : Real) <= V /\ V < (N : Real) + 1 /\ (f < 5 -> V < (N : Real) + 1 / 2) /\ (5 < f -> (N : Real) + 1 / 2 < V) /\ (f = 5 -> (q + 1 = digits.length -> V = (N : Real) + 1 / 2) /\ (q + 1 < digits.length -> (N : Real) + 1 / 2 < V)) := by -- BODY dsimp only simp only [bne_iff_ne] at hlast let E : List Nat -> Nat := fun xs => xs.foldl (fun n z => 10 * n + z) 0 let N := E (digits.take q) let V : Real := (E digits : Real) * (10 : Real) ^ ((q : Int) - (digits.length : Int)) let f := digits[q] change (N : Real) <= V /\ V < (N : Real) + 1 /\ (f < 5 -> V < (N : Real) + 1 / 2) /\ (5 < f -> (N : Real) + 1 / 2 < V) /\ (f = 5 -> (q + 1 = digits.length -> V = (N : Real) + 1 / 2) /\ (q + 1 < digits.length -> (N : Real) + 1 / 2 < V)) have hsuffixDigits : forall z, z ∈ digits.drop q -> z < 10 := by intro z hz exact hdigits z (List.mem_of_mem_drop hz) have hprefix := NumberHelperDecimalPrefixInterval (digits.take q) (digits.drop q) hsuffixDigits ((q : Int) - (digits.length : Int)) have hsplit : digits.take q ++ digits.drop q = digits := List.take_append_drop q digits have hdropLen : (digits.drop q).length = digits.length - q := by simp have hExp0 : (q : Int) - (digits.length : Int) + ((digits.drop q).length : Int) = 0 := by rw [hdropLen, Nat.cast_sub (Nat.le_of_lt hq)] omega have hinterval : (N : Real) <= V /\ V < (N : Real) + 1 := by dsimp only at hprefix rw [hsplit, hExp0] at hprefix simpa [E, N, V] using hprefix have htakeSucc : E (digits.take (q + 1)) = 10 * N + f := by dsimp only [E, N, f] rw [List.take_succ, List.foldl_append] simp [hq] have hsuffix1Digits : forall z, z ∈ digits.drop (q + 1) -> z < 10 := by intro z hz exact hdigits z (List.mem_of_mem_drop hz) have hprefix1 := NumberHelperDecimalPrefixInterval (digits.take (q + 1)) (digits.drop (q + 1)) hsuffix1Digits ((q : Int) - (digits.length : Int)) have hsplit1 : digits.take (q + 1) ++ digits.drop (q + 1) = digits := List.take_append_drop (q + 1) digits have hdropLen1 : (digits.drop (q + 1)).length = digits.length - (q + 1) := by simp have hExp1 : (q : Int) - (digits.length : Int) + ((digits.drop (q + 1)).length : Int) = -1 := by rw [hdropLen1, Nat.cast_sub (by omega : q + 1 <= digits.length)] omega have hinterval1 : ((10 * N + f : Nat) : Real) / 10 <= V /\ V < (((10 * N + f + 1 : Nat) : Real)) / 10 := by dsimp only at hprefix1 rw [hsplit1, hExp1] at hprefix1 change (E (digits.take (q + 1)) : Real) * (10 : Real) ^ (-1 : Int) <= V /\ V < ((E (digits.take (q + 1)) + 1 : Nat) : Real) * (10 : Real) ^ (-1 : Int) at hprefix1 rw [htakeSucc] at hprefix1 norm_num [zpow_neg] at hprefix1 push_cast at hprefix1 ⊢ constructor · nlinarith [hprefix1.1] · nlinarith [hprefix1.2] refine ⟨hinterval.1, hinterval.2, ?_, ?_, ?_⟩ · intro hf have hu := hinterval1.2 push_cast at hu change V < (N : Real) + 1 / 2 have hf' : (f : Real) <= 4 := by exact_mod_cast (by omega : f <= 4) nlinarith · intro hf have hl := hinterval1.1 push_cast at hl change (N : Real) + 1 / 2 < V have hf' : 6 <= (f : Real) := by exact_mod_cast (by omega : 6 <= f) nlinarith · intro hf have hf' : digits[q] = 5 := by simpa [f] using hf constructor · intro hterminal have hdropNil : digits.drop (q + 1) = [] := by apply List.eq_nil_of_length_eq_zero simp omega have hfull : E digits = 10 * N + 5 := by rw [← hsplit1, hdropNil] simpa [hf] using htakeSucc have hexp : (q : Int) - (digits.length : Int) = -1 := by omega dsimp only [V] rw [hfull, hexp] norm_num [zpow_neg] push_cast ring · intro hnonterminal let rest := digits.drop (q + 1) have hrestNe : rest ≠ [] := by intro he have hh := congrArg List.length he simp [rest] at hh omega have hrestLast : rest.getLast? ≠ some 0 := by intro he apply hlast rw [← hsplit1, List.getLast?_append_of_ne_nil _ hrestNe] exact he have foldFrom (ys : List Nat) (a : Nat) : ys.foldl (fun n z => 10 * n + z) a = a * 10 ^ ys.length + E ys := by induction ys generalizing a with | nil => simp [E] | cons y ys ih => simp only [List.foldl_cons, List.length_cons] rw [ih (10 * a + y)] have hEy : E (y :: ys) = y * 10 ^ ys.length + E ys := by dsimp only [E] simp only [List.foldl_cons, mul_zero, zero_add] exact ih y rw [hEy, pow_succ] ring have hrestPos : 0 < E rest := by have hdigitsRest : forall z, z ∈ rest -> z < 10 := by intro z hz exact hdigits z (List.mem_of_mem_drop hz) have hevalCons (a : Nat) (ys : List Nat) : E (a :: ys) = a * 10 ^ ys.length + E ys := by simp only [E, List.foldl_cons] simpa using foldFrom ys a have evalPos : forall ys : List Nat, (forall z, z ∈ ys -> z < 10) -> (forall hne : ys ≠ [], ys.getLast hne ≠ 0) -> ys ≠ [] -> 0 < E ys := by intro ys hz hlast' hne induction ys with | nil => contradiction | cons a tail ih => rw [hevalCons] by_cases has : tail = [] · subst tail simp only [List.length_nil, pow_zero, mul_one, E, List.foldl_nil, add_zero] have ha := hlast' (by simp) simpa using Nat.pos_of_ne_zero ha · have htailDigits : forall z, z ∈ tail -> z < 10 := by intro z hz' exact hz z (by simp [hz']) have htailLast : forall hne : tail ≠ [], tail.getLast hne ≠ 0 := by intro hne simpa [List.getLast_cons hne] using hlast' (by simp) have ht := ih htailDigits htailLast has omega apply evalPos rest hdigitsRest · intro hne heq apply hrestLast rw [List.getLast?_eq_getLast_of_ne_nil hne, heq] · exact hrestNe have hfull : E digits = (10 * N + 5) * 10 ^ rest.length + E rest := by calc E digits = E (digits.take (q + 1) ++ digits.drop (q + 1)) := by exact congrArg E hsplit1.symm _ = (digits.drop (q + 1)).foldl (fun n z => 10 * n + z) (E (digits.take (q + 1))) := by dsimp only [E] rw [List.foldl_append] _ = rest.foldl (fun n z => 10 * n + z) (10 * N + 5) := by rw [show E (digits.take (q + 1)) = 10 * N + 5 by simpa [hf] using htakeSucc] _ = (10 * N + 5) * 10 ^ rest.length + E rest := foldFrom rest (10 * N + 5) have hexp : (q : Int) - (digits.length : Int) + (rest.length : Int) = -1 := by simpa [rest] using hExp1 have hpowPos : 0 < (10 : Real) ^ ((q : Int) - (digits.length : Int)) := zpow_pos (by norm_num) _ have hdecomp : V = ((10 * N + 5 : Nat) : Real) / 10 + (E rest : Real) * (10 : Real) ^ ((q : Int) - (digits.length : Int)) := by dsimp only [V] rw [hfull] push_cast rw [add_mul] have hp : (10 : Real) ^ (rest.length : Int) * (10 : Real) ^ ((q : Int) - (digits.length : Int)) = 1 / 10 := by rw [← zpow_add₀ (by norm_num : (10 : Real) ≠ 0)] rw [add_comm, hexp] norm_num [zpow_neg] have hp' : (10 : Real) ^ rest.length * (10 : Real) ^ ((q : Int) - (digits.length : Int)) = 1 / 10 := by simpa [zpow_natCast] using hp rw [mul_assoc, hp'] ring rw [hdecomp] push_cast have hrestReal : 0 < (E rest : Real) := by exact_mod_cast hrestPos nlinarith [mul_pos hrestReal hpowPos] end dec2flt_model