import Tablet.DecimalSeqLeftShiftRecordSemantics
import Tablet.P804_T1_DecimalLeadingInterval

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P804_T1_LeftLeadingProgress]
theorem P804_T1_LeftLeadingProgress
    (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (shift : Std.Usize) (z z' : Nat) (zs zs' : List Nat)
    (hk : shift.val <= 60)
    (hshape :
      (d.digits.val.take d.num_digits.val).map (fun q => q.val) = z :: zs)
    (hz : 1 <= z)
    (hdigit : forall q, q ∈ d.digits.val.take d.num_digits.val -> q.val < 10)
    (hlen : (z :: zs).length = d.num_digits.val)
    (hshape' :
      (d'.digits.val.take d'.num_digits.val).map (fun q => q.val) = z' :: zs')
    (hz' : 1 <= z')
    (hdigit' : forall q, q ∈ d'.digits.val.take d'.num_digits.val -> q.val < 10)
    (hlen' : (z' :: zs').length = d'.num_digits.val)
    (hcall : num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d') :
    (d.decimal_point.val = -1 -> d'.decimal_point.val = -1 ->
      shift.val = 3 -> 7 <= z') /\
    (d.decimal_point.val = 0 -> d'.decimal_point.val = 0 ->
      ((z = 1 /\ shift.val = 2) \/ (z = 2 /\ shift.val = 1)) ->
      3 <= z') /\
    (d.decimal_point.val = 0 -> d'.decimal_point.val = 0 ->
      3 <= z -> shift.val = 1 -> 5 <= z') /\
    (d.decimal_point.val = -1 -> d'.decimal_point.val = 0 ->
      7 <= z -> shift.val = 3 -> 5 <= z') := by
-- BODY
  let xs := (d.digits.val.take d.num_digits.val).map (fun q => q.val)
  let xs' := (d'.digits.val.take d'.num_digits.val).map (fun q => q.val)
  let V : Real := (xs.foldl (fun a q => 10 * a + q) 0 : Nat) *
    (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int))
  let V' : Real := (xs'.foldl (fun a q => 10 * a + q) 0 : Nat) *
    (10 : Real) ^ (d'.decimal_point.val - (d'.num_digits.val : Int))
  have hdin : forall q, q ∈ xs -> q < 10 := by
    intro q hq
    simp only [xs, List.mem_map] at hq
    rcases hq with ⟨u, hu, rfl⟩
    exact hdigit u hu
  have hdout : forall q, q ∈ xs' -> q < 10 := by
    intro q hq
    simp only [xs', List.mem_map] at hq
    rcases hq with ⟨u, hu, rfl⟩
    exact hdigit' u hu
  have hin := P804_T1_DecimalLeadingInterval xs z zs
    (by simpa [xs] using hshape) hz hdin d.decimal_point.val
  have hout := P804_T1_DecimalLeadingInterval xs' z' zs'
    (by simpa [xs'] using hshape') hz' hdout d'.decimal_point.val
  dsimp only at hin hout
  have hlenx : xs.length = d.num_digits.val := by
    simpa [xs, hshape] using hlen
  have hlenx' : xs'.length = d'.num_digits.val := by
    simpa [xs', hshape'] using hlen'
  rw [hlenx] at hin
  rw [hlenx'] at hout
  have hVpos : 0 < V := by
    have hzR : (0 : Real) < z := by exact_mod_cast (by omega : 0 < z)
    have hp : 0 < (10 : Real) ^ (d.decimal_point.val - 1) :=
      zpow_pos (by norm_num) _
    exact lt_of_lt_of_le (mul_pos hzR hp) (by simpa [V] using hin.1)
  obtain ⟨rho, hrho0, hrhoCut, hrecord, hsticky⟩ :=
    DecimalSeqLeftShiftRecordSemantics d d' shift hk
      (by simpa [V, xs, List.map_take] using hVpos)
      hdigit hcall
  change V' = (2 : Real) ^ shift.val * V - rho at hrecord
  have hinLow : (z : Real) * (10 : Real) ^ (d.decimal_point.val - 1) <= V := by
    simpa [V] using hin.1
  have houtHigh : V' < (z' + 1 : Nat) *
      (10 : Real) ^ (d'.decimal_point.val - 1) := by
    simpa [V'] using hout.2
  constructor
  · intro hp hp' hk3
    have hrho : rho < (10 : Real) ^ (-2 : Int) := by
      have hpow : (10 : Real) ^ (d'.decimal_point.val - 768) <
          (10 : Real) ^ (-2 : Int) :=
        zpow_lt_zpow_right₀ (by norm_num) (by omega)
      exact lt_trans hrhoCut hpow
    norm_num [hp, hp', hk3] at hinLow houtHigh hrecord hrho
    by_contra h
    have hz'R : (z' : Real) <= 6 := by exact_mod_cast (by omega : z' <= 6)
    have hzR : (1 : Real) <= z := by exact_mod_cast hz
    nlinarith
  constructor
  · intro hp hp' hcase
    have hrho : rho < (10 : Real) ^ (-1 : Int) := by
      have hpow : (10 : Real) ^ (d'.decimal_point.val - 768) <
          (10 : Real) ^ (-1 : Int) :=
        zpow_lt_zpow_right₀ (by norm_num) (by omega)
      exact lt_trans hrhoCut hpow
    rcases hcase with ⟨hz1, hk2⟩ | ⟨hz2, hk1⟩
    · norm_num [hp, hp', hk2, hz1] at hinLow houtHigh hrecord hrho
      by_contra h
      have hz'R : (z' : Real) <= 2 := by exact_mod_cast (by omega : z' <= 2)
      nlinarith
    · norm_num [hp, hp', hk1, hz2] at hinLow houtHigh hrecord hrho
      by_contra h
      have hz'R : (z' : Real) <= 2 := by exact_mod_cast (by omega : z' <= 2)
      nlinarith
  constructor
  · intro hp hp' hz3 hk1
    have hrho : rho < (10 : Real) ^ (-1 : Int) := by
      have hpow : (10 : Real) ^ (d'.decimal_point.val - 768) <
          (10 : Real) ^ (-1 : Int) :=
        zpow_lt_zpow_right₀ (by norm_num) (by omega)
      exact lt_trans hrhoCut hpow
    norm_num [hp, hp', hk1] at hinLow houtHigh hrecord hrho
    by_contra h
    have hz'R : (z' : Real) <= 4 := by exact_mod_cast (by omega : z' <= 4)
    have hzR : (3 : Real) <= z := by exact_mod_cast hz3
    nlinarith
  · intro hp hp' hz7 hk3
    have hrho : rho < (10 : Real) ^ (-2 : Int) := by
      have hpow : (10 : Real) ^ (d'.decimal_point.val - 768) <
          (10 : Real) ^ (-2 : Int) :=
        zpow_lt_zpow_right₀ (by norm_num) (by omega)
      exact lt_trans hrhoCut hpow
    norm_num [hp, hp', hk3] at hinLow houtHigh hrecord hrho
    by_contra h
    have hz'R : (z' : Real) <= 4 := by exact_mod_cast (by omega : z' <= 4)
    have hzR : (7 : Real) <= z := by exact_mod_cast hz7
    nlinarith

end dec2flt_model
