import Tablet.DecimalSeqRightShiftRecordSemantics
import Tablet.P804_T1_DecimalFoldBounds

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P804_T1_DecimalValueBounds]
theorem P804_T1_DecimalValueBounds
    (xs : List Nat) (z : Nat) (zs : List Nat)
    (hxs : xs = z :: zs) (hz : 1 <= z)
    (hd : forall q, q ∈ xs -> q < 10) (p : Int) :
    let I : Nat := xs.foldl (fun n digit => 10 * n + digit) 0
    (z : Real) * (10 : Real) ^ (p - 1) <=
        (I : Real) * (10 : Real) ^ (p - (xs.length : Int)) /\
      (I : Real) * (10 : Real) ^ (p - (xs.length : Int)) <
        (10 : Real) ^ p := by
-- BODY
  dsimp only
  have hb := P804_T1_DecimalFoldBounds xs hd z zs hxs hz
  have hn : 1 <= xs.length := by simp [hxs]
  have hlowerNat : z * 10 ^ (xs.length - 1) <=
      xs.foldl (fun n digit => 10 * n + digit) 0 := by
    subst xs
    simp only [List.length_cons, Nat.add_sub_cancel, List.foldl_cons]
    let F := fun ys : List Nat =>
      ys.foldl (fun n digit => 10 * n + digit) 0
    have foldlFrom (ys : List Nat) (a : Nat) :
        ys.foldl (fun n digit => 10 * n + digit) a =
          a * 10 ^ ys.length + F ys := by
      induction ys generalizing a with
      | nil => simp [F]
      | cons y ys ih =>
        simp only [List.foldl_cons, List.length_cons]
        rw [ih, pow_succ]
        change (10 * a + y) * 10 ^ ys.length + F ys =
          a * (10 ^ ys.length * 10) +
            (y :: ys).foldl (fun n digit => 10 * n + digit) 0
        simp only [List.foldl_cons]
        rw [ih]
        ring
    change z * 10 ^ zs.length <= F (z :: zs)
    rw [show F (z :: zs) = z * 10 ^ zs.length + F zs by
      simpa [F] using foldlFrom zs z]
    omega
  have hlower : (z : Real) * (10 : Real) ^ (xs.length - 1) <=
      ((xs.foldl (fun n digit => 10 * n + digit) 0 : Nat) : Real) := by
    exact_mod_cast hlowerNat
  have hupper :
      ((xs.foldl (fun n digit => 10 * n + digit) 0 : Nat) : Real) <
        (10 : Real) ^ xs.length := by
    exact_mod_cast hb.2
  have hpowpos : 0 < (10 : Real) ^ (p - (xs.length : Int)) :=
    zpow_pos (by norm_num) _
  constructor
  · have hm := mul_le_mul_of_nonneg_right hlower (le_of_lt hpowpos)
    have hexp : p - 1 = ((xs.length - 1 : Nat) : Int) +
        (p - (xs.length : Int)) := by
      rw [Nat.cast_sub hn]
      omega
    calc
      (z : Real) * (10 : Real) ^ (p - 1) =
          ((z : Real) * (10 : Real) ^ (xs.length - 1)) *
            (10 : Real) ^ (p - (xs.length : Int)) := by
              rw [hexp, zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
              norm_num only [zpow_natCast]
              ring
      _ <= ((xs.foldl (fun n digit => 10 * n + digit) 0 : Nat) : Real) *
          (10 : Real) ^ (p - (xs.length : Int)) := hm
  · have hm := mul_lt_mul_of_pos_right hupper hpowpos
    calc
      ((xs.foldl (fun n digit => 10 * n + digit) 0 : Nat) : Real) *
          (10 : Real) ^ (p - (xs.length : Int)) <
          (10 : Real) ^ xs.length *
            (10 : Real) ^ (p - (xs.length : Int)) := hm
      _ = (10 : Real) ^ p := by
        rw [← zpow_natCast]
        rw [← zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
        congr 1
        omega

end dec2flt_model
