import Tablet.P529_T1_NormalizationBounds
import Tablet.P529_T1_RightHistoryBoundary
import Tablet.P856_T1_DecimalBinaryExponentBound
import Tablet.P495_T1_SlowLoop0Execution
import Tablet.P472_T1_DecimalValueBounds
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P529_T1_ReachableCapacity]
theorem P529_T1_ReachableCapacity (X Y : Real)
    (d d0 d1 : num.imp.dec2flt.decimal_seq.DecimalSeq) (exp0 exp1 exp2 : Std.I32)
    (hpointLo : -324 <= d.decimal_point.val)
    (hpointHi : d.decimal_point.val < 310)
    (hinput : d.num_digits.val <= 768 /\
      (forall b, b ∈ d.digits.val.take d.num_digits.val -> b.val < 10) /\
      (exists h tail,
        (d.digits.val.take d.num_digits.val).map (fun b => b.val) = h :: tail /\
        1 <= h /\ h < 10 /\ (h :: tail).length = d.num_digits.val))
    (hloop0 : num.imp.dec2flt.slow.parse_long_mantissa_loop0
      F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
      { m := 0#u64, p_biased := 0#i32 }
      d.num_digits d.decimal_point d.truncated d.digits 0#i32 none =
        ok (d0.num_digits, d0.decimal_point, d0.truncated, d0.digits, exp0, none))
    (hloop1 : num.imp.dec2flt.slow.parse_long_mantissa_loop1
      F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
      { m := 0#u64, p_biased := 2047#i32 }
      d0.num_digits d0.decimal_point d0.truncated d0.digits exp0 none =
        ok (d1.num_digits, d1.decimal_point, d1.truncated, d1.digits, exp1, none))
    (hexp2 : exp2.val = max (exp1.val - 1) (-1022)) :
    let V := fun a : num.imp.dec2flt.decimal_seq.DecimalSeq =>
      ((((a.digits.val.take a.num_digits.val).map (fun b => b.val)).foldl
        (fun n z => 10 * n + z) 0 : Nat) : Real) *
        (10 : Real) ^ (a.decimal_point.val - (a.num_digits.val : Int))
    Y = V d + (10 : Real) ^ (d.decimal_point.val - 768) ->
    (exists eta : Real, X = V d + eta /\ 0 <= eta /\
      eta < (10 : Real) ^ (d.decimal_point.val - 768) /\
      (d.truncated = true <-> 0 < eta)) ->
    0 <= exp2.val - exp0.val + 715 - d0.decimal_point.val := by
-- BODY
  dsimp only
  intro hY hX
  obtain ⟨d0', exp0', steps, hloop0', hsteps, hexp0lo, hexp0hi,
    hXlo, hp0lo, hp0hi, hcount0, hdigits0, hshape0, hlast0, herror0⟩ :=
    P495_T1_SlowLoop0Execution X Y d hpointLo hpointHi hinput hY hX
  have hloop0eq := hloop0'
  rw [hloop0] at hloop0eq
  injection hloop0eq with htuple
  have hd0eq : d0' = d0 := by
    cases d0'
    cases d0
    simp_all
  have hexp0eq : exp0' = exp0 := by
    simp_all
  subst d0'
  subst exp0'
  have hnorm := P529_T1_NormalizationBounds X Y d d0 d1 exp0 exp1
    hpointLo hpointHi hinput hloop0 hloop1 hY hX
  rcases hnorm with ⟨_, hXltY, hYnorm, _, _⟩
  have hboundary := P529_T1_RightHistoryBoundary X Y d d0 exp0
    hpointLo hpointHi hinput hloop0 hY hX
  by_cases hpneg : d0.decimal_point.val ≤ -307
  · have he0zero : exp0.val = 0 := hboundary.1 (by omega)
    rw [hexp2]
    omega
  · have hp0lo' : -306 ≤ d0.decimal_point.val := by omega
    have hdigits : ∀ q, q ∈
        (d0.digits.val.take d0.num_digits.val).map (fun b => b.val) → q < 10 := by
      intro q hq
      obtain ⟨b, hb, rfl⟩ := List.mem_map.mp hq
      exact hdigits0 b hb
    obtain ⟨head, tail, hshape, hheadlo, hheadhi, hlen⟩ := hshape0
    let xs : List Nat :=
      (d0.digits.val.take d0.num_digits.val).map (fun b => b.val)
    have hvalue := P472_T1_DecimalValueBounds xs head tail hshape
      hheadlo hdigits d0.decimal_point.val
    have hval : (10 : Real) ^ (d0.decimal_point.val - 1) ≤
        ((((d0.digits.val.take d0.num_digits.val).map (fun b => b.val)).foldl
          (fun n z => 10 * n + z) 0 : Nat) : Real) *
          (10 : Real) ^ (d0.decimal_point.val - (d0.num_digits.val : Int)) := by
      have hpowpos : 0 < (10 : Real) ^ (d0.decimal_point.val -
          (d0.num_digits.val : Int)) := zpow_pos (by norm_num) _
      have hone : (1 : Real) ≤ (head : Real) := by exact_mod_cast hheadlo
      calc
        (10 : Real) ^ (d0.decimal_point.val - 1) =
            (1 : Real) * (10 : Real) ^ (d0.decimal_point.val - 1) := by ring
        _ ≤ (head : Real) * (10 : Real) ^ (d0.decimal_point.val - 1) :=
          mul_le_mul_of_nonneg_right hone (le_of_lt (zpow_pos (by norm_num) _))
        _ ≤ ((xs.foldl (fun n z => 10 * n + z) 0 : Nat) : Real) *
            (10 : Real) ^ (d0.decimal_point.val - (xs.length : Int)) := hvalue.1
        _ = ((((d0.digits.val.take d0.num_digits.val).map
              (fun b => b.val)).foldl (fun n z => 10 * n + z) 0 : Nat) : Real) *
            (10 : Real) ^ (d0.decimal_point.val - (d0.num_digits.val : Int)) := by
          simp [xs, hshape, hlen]
    obtain ⟨eta0, u0, hXscaled, _, heta0, _, _⟩ := herror0
    have hVle :
        ((((d0.digits.val.take d0.num_digits.val).map (fun b => b.val)).foldl
          (fun n z => 10 * n + z) 0 : Nat) : Real) *
          (10 : Real) ^ (d0.decimal_point.val - (d0.num_digits.val : Int)) ≤
        X / (2 : Real) ^ exp0.val := by
      rw [hXscaled]
      exact le_add_of_nonneg_right heta0
    have hden0 : 0 < (2 : Real) ^ exp0.val :=
      zpow_pos (by norm_num : (0 : Real) < 2) _
    have hden1 : 0 < (2 : Real) ^ exp1.val :=
      zpow_pos (by norm_num : (0 : Real) < 2) _
    have hYle : Y ≤ (2 : Real) ^ exp1.val := by
      have htmp := (div_le_iff₀ hden1).mp hYnorm
      simpa using htmp
    have hYscaled : Y / (2 : Real) ^ exp0.val ≤
        (2 : Real) ^ (exp1.val - exp0.val) := by
      have hdiv := div_le_div_of_nonneg_right hYle (le_of_lt hden0)
      calc
        Y / (2 : Real) ^ exp0.val ≤
            (2 : Real) ^ exp1.val / (2 : Real) ^ exp0.val := hdiv
        _ = (2 : Real) ^ (exp1.val - exp0.val) := by
          rw [← zpow_sub₀ (by norm_num : (2 : Real) ≠ 0)]
    have hpow : (10 : Real) ^ (d0.decimal_point.val - 1) <
        (2 : Real) ^ (exp1.val - exp0.val) := by
      calc
        (10 : Real) ^ (d0.decimal_point.val - 1) ≤
            ((((d0.digits.val.take d0.num_digits.val).map (fun b => b.val)).foldl
              (fun n z => 10 * n + z) 0 : Nat) : Real) *
              (10 : Real) ^ (d0.decimal_point.val - (d0.num_digits.val : Int)) := hval
        _ ≤ X / (2 : Real) ^ exp0.val := hVle
        _ < Y / (2 : Real) ^ exp0.val :=
          div_lt_div_of_pos_right hXltY hden0
        _ ≤ (2 : Real) ^ (exp1.val - exp0.val) := hYscaled
    have hpbound := P856_T1_DecimalBinaryExponentBound
      d0.decimal_point.val (exp1.val - exp0.val) hp0lo' hp0hi hpow
    rw [hexp2]
    omega

end dec2flt_model
