import Tablet.DecimalGoalVocabularyDefault

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NearestEvenOrderedGrid]
theorem NearestEvenOrderedGrid (value : Nat → Real) (top raw : Nat)
    (A y dl du : Real)
    (hmono : ∀ p q : Nat, p < q → q < top → value p < value q)
    (hrtop : raw < top) (hval : value raw = y) (hdl : 0 < dl) (hdu : 0 < du)
    (hlo : y ≤ A + dl / 2) (hhi : A ≤ y + du / 2)
    (hprev : raw = 0 ∨ value (raw - 1) = y - dl)
    (hnext : raw + 1 = top ∨ value (raw + 1) = y + du)
    (htieLow : A = y - dl / 2 → raw % 2 = 0)
    (htieHigh : A = y + du / 2 → raw % 2 = 0) :
    ∀ v : Nat, v < top → v ≠ raw →
      abs (A - value raw) ≤ abs (A - value v) ∧
      (abs (A - value raw) = abs (A - value v) → raw % 2 = 0) := by
-- BODY
  intro v hv hne
  rw [hval]
  have lower_near : abs (A - y) ≤ abs (A - (y - dl)) := by
    by_cases hay : A ≤ y
    · rw [abs_of_nonpos (sub_nonpos.mpr hay),
          abs_of_nonneg (by linarith : 0 ≤ A - (y - dl))]
      linarith
    · have hya : y ≤ A := le_of_not_ge hay
      rw [abs_of_nonneg (sub_nonneg.mpr hya),
          abs_of_nonneg (by linarith : 0 ≤ A - (y - dl))]
      linarith
  have upper_near : abs (A - y) ≤ abs (A - (y + du)) := by
    by_cases hay : A ≤ y
    · rw [abs_of_nonpos (sub_nonpos.mpr hay),
          abs_of_nonpos (by linarith : A - (y + du) ≤ 0)]
      linarith
    · have hya : y ≤ A := le_of_not_ge hay
      rw [abs_of_nonneg (sub_nonneg.mpr hya),
          abs_of_nonpos (by linarith : A - (y + du) ≤ 0)]
      linarith
  rcases lt_or_gt_of_ne hne with hlt | hgt
  · have hr0 : raw ≠ 0 := by omega
    have hp := hprev.resolve_left hr0
    have hle : v ≤ raw - 1 := by omega
    rcases hle.eq_or_lt with heq | hvlt
    · subst v
      rw [hp]
      refine ⟨lower_near, ?_⟩
      intro heqdist
      apply htieLow
      have hs := congrArg (fun z : Real => z ^ 2) heqdist
      dsimp only at hs
      rw [sq_abs, sq_abs] at hs
      nlinarith
    · have hvalue : value v < y - dl := by
        rw [← hp]
        exact hmono v (raw - 1) hvlt (by omega)
      have hcomp : abs (A - (y - dl)) < abs (A - value v) := by
        have hva : value v < A := by linarith
        have hpa : y - dl ≤ A := by linarith
        rw [abs_of_nonneg (sub_nonneg.mpr hpa),
          abs_of_nonneg (sub_nonneg.mpr hva.le)]
        linarith
      refine ⟨lower_near.trans hcomp.le, ?_⟩
      intro heqdist
      exact (ne_of_lt (lower_near.trans_lt hcomp) heqdist).elim
  · have hsucctop : raw + 1 ≠ top := by omega
    have hn := hnext.resolve_left hsucctop
    have hle : raw + 1 ≤ v := by omega
    rcases hle.eq_or_lt with heq | hvlt
    · subst v
      rw [hn]
      refine ⟨upper_near, ?_⟩
      intro heqdist
      apply htieHigh
      have hs := congrArg (fun z : Real => z ^ 2) heqdist
      dsimp only at hs
      rw [sq_abs, sq_abs] at hs
      nlinarith
    · have hvalue : y + du < value v := by
        rw [← hn]
        exact hmono (raw + 1) v hvlt hv
      have hcomp : abs (A - (y + du)) < abs (A - value v) := by
        have hva : A < value v := by linarith
        have hpa : A ≤ y + du := by linarith
        rw [abs_of_nonpos (sub_nonpos.mpr hpa),
          abs_of_nonpos (sub_nonpos.mpr hva.le)]
        linarith
      refine ⟨upper_near.trans hcomp.le, ?_⟩
      intro heqdist
      exact (ne_of_lt (upper_near.trans_lt hcomp) heqdist).elim

end dec2flt_model
