import Tablet.Preamble open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: DecimalDigitLexScale] theorem DecimalDigitLexScale (xs ys : List Nat) (hxs : forall x, x ∈ xs -> x < 10) (hys : forall y, y ∈ ys -> y < 10) (hylast : forall hne : ys ≠ [], ys.getLast hne ≠ 0) : List.Lex (fun a b : Nat => a < b) (xs.take ys.length) ys ↔ xs.foldl (fun a z => 10 * a + z) 0 * 10 ^ ys.length < ys.foldl (fun a z => 10 * a + z) 0 * 10 ^ xs.length := by -- BODY let eval := fun zs : List Nat => zs.foldl (fun a z => 10 * a + z) 0 have hacc : forall (zs : List Nat) (a : Nat), zs.foldl (fun n z => 10 * n + z) a = a * 10 ^ zs.length + eval zs := by intro zs induction zs with | nil => simp [eval] | cons z zs ih => intro a simp only [List.foldl_cons, List.length_cons] rw [ih (10 * a + z)] dsimp only [eval] simp only [List.foldl_cons] simp only [mul_zero, zero_add] rw [ih z] simp only [Nat.pow_succ] simp only [eval] ring have heval_cons : forall (a : Nat) (zs : List Nat), eval (a :: zs) = a * 10 ^ zs.length + eval zs := by intro a zs simp only [eval, List.foldl_cons] simpa using hacc zs a have hbound : forall (zs : List Nat), (forall z, z ∈ zs -> z < 10) -> eval zs < 10 ^ zs.length := by intro zs hz induction zs with | nil => simp [eval] | cons z zs ih => rw [heval_cons] have hz0 := hz z (by simp) have hzt : forall w, w ∈ zs -> w < 10 := by intro w hw exact hz w (by simp [hw]) have hit := ih hzt simp only [List.length_cons, Nat.pow_succ] nlinarith have hpos : forall (zs : List Nat), (forall z, z ∈ zs -> z < 10) -> (forall hne : zs ≠ [], zs.getLast hne ≠ 0) -> zs ≠ [] -> 0 < eval zs := by intro zs hz hlast hne induction zs with | nil => contradiction | cons z zs ih => rw [heval_cons] by_cases ht : zs = [] · subst zs simp only [List.length_nil, pow_zero, mul_one, eval, List.foldl_nil, add_zero] have hzlast := hlast (by simp) simpa using Nat.pos_of_ne_zero hzlast · have hzt : forall w, w ∈ zs -> w < 10 := by intro w hw exact hz w (by simp [hw]) have hlastt : forall hne : zs ≠ [], zs.getLast hne ≠ 0 := by intro hne simpa [List.getLast_cons hne] using hlast (by simp) have := ih hzt hlastt ht omega change List.Lex (fun a b : Nat => a < b) (xs.take ys.length) ys ↔ eval xs * 10 ^ ys.length < eval ys * 10 ^ xs.length induction ys generalizing xs with | nil => simp [eval] | cons b bs ih => cases xs with | nil => simp only [List.length_cons, List.take_nil, List.length_nil, pow_zero, mul_one] simp only [eval, List.foldl_nil, zero_mul] constructor · intro _ exact hpos (b :: bs) hys hylast (by simp) · intro _ exact List.Lex.nil | cons a xsTail => have has : forall x, x ∈ xsTail -> x < 10 := by intro x hx exact hxs x (by simp [hx]) have hbs : forall y, y ∈ bs -> y < 10 := by intro y hy exact hys y (by simp [hy]) have habound := hbound xsTail has have hbbound := hbound bs hbs rw [heval_cons, heval_cons] simp only [List.length_cons, List.take_succ_cons, Nat.pow_succ] rcases lt_trichotomy a b with hab | hab | hab · constructor · intro _ have hp : 0 < 10 ^ xsTail.length * 10 ^ bs.length * 10 := by positivity calc (a * 10 ^ xsTail.length + eval xsTail) * (10 ^ bs.length * 10) < (a + 1) * (10 ^ xsTail.length * 10 ^ bs.length * 10) := by nlinarith _ ≤ b * (10 ^ xsTail.length * 10 ^ bs.length * 10) := by exact Nat.mul_le_mul_right _ hab _ ≤ (b * 10 ^ bs.length + eval bs) * (10 ^ xsTail.length * 10) := by nlinarith · intro _ exact List.Lex.rel hab · subst b have hlastbs : forall hne : bs ≠ [], bs.getLast hne ≠ 0 := by intro hne simpa [List.getLast_cons hne] using hylast (by simp) rw [List.lex_cons_iff] have hi := ih xsTail has hbs hlastbs rw [hi] constructor <;> intro ht <;> nlinarith · constructor · intro hlex cases hlex with | rel h => omega | cons h => have hlastbs : forall hne : bs ≠ [], bs.getLast hne ≠ 0 := by intro hne simpa [List.getLast_cons hne] using hylast (by simp) have htail := (ih xsTail has hbs hlastbs).mp h nlinarith · intro hnum have hp : 0 < 10 ^ xsTail.length * 10 ^ bs.length * 10 := by positivity exfalso nlinarith end dec2flt_model