import Mathlib
import Optlib


open Set Real

open Set Real PNat

theorem extracted_formal_statement_6.{u_1} {E : Type u_1} [inst : NormedAddCommGroup E] [inst_1 : InnerProductSpace ℝ E]
  [inst_2 : CompleteSpace E] {x0 : E} {f h : E → ℝ} {f' : E → E} {alg : Nesterov_second f h f' x0} {xm : E}
  (minφ : IsMinOn (f + h) univ xm) :
  let φ := fun z => f z + h z;
  (∀ (z : E), φ z = f z + h z) →
    (∀ (k : ℕ+),
        Nesterov_second.γ f h f' x0 ↑k • (Nesterov_second.y f h f' x0 (↑k - 1) - Nesterov_second.y f h f' x0 ↑k) -
            Nesterov_second.t f h f' x0 ↑k • f' (Nesterov_second.z f h f' x0 k) ∈
          SubderivAt (Nesterov_second.t f h f' x0 ↑k • h) (Nesterov_second.y f h f' x0 ↑k)) →
      (∀ (w : E) (k : ℕ+),
          Nesterov_second.t f h f' x0 ↑k * h w ≥
            Nesterov_second.t f h f' x0 ↑k * h (Nesterov_second.y f h f' x0 ↑k) +
              inner
                (Nesterov_second.γ f h f' x0 ↑k •
                    (Nesterov_second.y f h f' x0 (↑k - 1) - Nesterov_second.y f h f' x0 ↑k) -
                  Nesterov_second.t f h f' x0 ↑k • f' (Nesterov_second.z f h f' x0 k))
                (w - Nesterov_second.y f h f' x0 ↑k)) →
        (∀ (k : ℕ+),
            h (Nesterov_second.x f h f' x0 ↑k) ≤
              (1 - Nesterov_second.γ f h f' x0 ↑k) * h (Nesterov_second.x f h f' x0 (↑k - 1)) +
                Nesterov_second.γ f h f' x0 ↑k * h (Nesterov_second.y f h f' x0 ↑k)) →
          (∀ (k : ℕ), Nesterov_second.t f h f' x0 k / Nesterov_second.γ f h f' x0 k > 0) →
            (∀ (w : E) (k : ℕ+),
                Nesterov_second.γ f h f' x0 ↑k * h (Nesterov_second.y f h f' x0 ↑k) ≤
                  Nesterov_second.γ f h f' x0 ↑k * h w -
                    Nesterov_second.γ f h f' x0 ↑k / Nesterov_second.t f h f' x0 ↑k *
                      inner
                        (Nesterov_second.γ f h f' x0 ↑k •
                            (Nesterov_second.y f h f' x0 (↑k - 1) - Nesterov_second.y f h f' x0 ↑k) -
                          Nesterov_second.t f h f' x0 ↑k • f' (Nesterov_second.z f h f' x0 k))
                        (w - Nesterov_second.y f h f' x0 ↑k)) →
              (∀ (w : E) (k : ℕ+),
                  h (Nesterov_second.x f h f' x0 ↑k) ≤
                    (1 - Nesterov_second.γ f h f' x0 ↑k) * h (Nesterov_second.x f h f' x0 (↑k - 1)) +
                        Nesterov_second.γ f h f' x0 ↑k * h w -
                      Nesterov_second.γ f h f' x0 ↑k / Nesterov_second.t f h f' x0 ↑k *
                        inner
                          (Nesterov_second.γ f h f' x0 ↑k •
                              (Nesterov_second.y f h f' x0 (↑k - 1) - Nesterov_second.y f h f' x0 ↑k) -
                            Nesterov_second.t f h f' x0 ↑k • f' (Nesterov_second.z f h f' x0 k))
                          (w - Nesterov_second.y f h f' x0 ↑k)) →
                (∀ (k : ℕ+),
                    f (Nesterov_second.x f h f' x0 ↑k) ≤
                      f (Nesterov_second.z f h f' x0 k) +
                          inner (f' (Nesterov_second.z f h f' x0 k))
                            (Nesterov_second.x f h f' x0 ↑k - Nesterov_second.z f h f' x0 k) +
                        ↑(Nesterov_second.l f h f' x0) / 2 *
                          ‖Nesterov_second.x f h f' x0 ↑k - Nesterov_second.z f h f' x0 k‖ ^ 2) →
                  ∀ (k : ℕ+),
                    f (Nesterov_second.x f h f' x0 ↑k) ≤
                      f (Nesterov_second.z f h f' x0 k) +
                          inner (f' (Nesterov_second.z f h f' x0 k))
                            (Nesterov_second.x f h f' x0 ↑k - Nesterov_second.z f h f' x0 k) +
                        1 / (2 * Nesterov_second.t f h f' x0 ↑k) *
                          ‖Nesterov_second.x f h f' x0 ↑k - Nesterov_second.z f h f' x0 k‖ ^ 2 := sorry

Tactic state before:
E : Type u_1
inst✝² : NormedAddCommGroup E
inst✝¹ : InnerProductSpace ℝ E
inst✝ : CompleteSpace E
x0 : E
f h : E → ℝ
f' : E → E
alg : Nesterov_second f h f' x0
xm : E
minφ : IsMinOn (f + h) univ xm
φ : E → ℝ := fun z => f z + h z
φdef : ∀ (z : E), φ z = f z + h z
h1 :
  ∀ (k : ℕ+),
    Nesterov_second.γ f h f' x0 ↑k • (Nesterov_second.y f h f' x0 (↑k - 1) - Nesterov_second.y f h f' x0 ↑k) -
        Nesterov_second.t f h f' x0 ↑k • f' (Nesterov_second.z f h f' x0 k) ∈
      SubderivAt (Nesterov_second.t f h f' x0 ↑k • h) (Nesterov_second.y f h f' x0 ↑k)
hieq1 :
  ∀ (w : E) (k : ℕ+),
    Nesterov_second.t f h f' x0 ↑k * h w ≥
      Nesterov_second.t f h f' x0 ↑k * h (Nesterov_second.y f h f' x0 ↑k) +
        inner
          (Nesterov_second.γ f h f' x0 ↑k • (Nesterov_second.y f h f' x0 (↑k - 1) - Nesterov_second.y f h f' x0 ↑k) -
            Nesterov_second.t f h f' x0 ↑k • f' (Nesterov_second.z f h f' x0 k))
          (w - Nesterov_second.y f h f' x0 ↑k)
cvxineq :
  ∀ (k : ℕ+),
    h (Nesterov_second.x f h f' x0 ↑k) ≤
      (1 - Nesterov_second.γ f h f' x0 ↑k) * h (Nesterov_second.x f h f' x0 (↑k - 1)) +
        Nesterov_second.γ f h f' x0 ↑k * h (Nesterov_second.y f h f' x0 ↑k)
bsc1 : ∀ (k : ℕ), Nesterov_second.t f h f' x0 k / Nesterov_second.γ f h f' x0 k > 0
hieq2 :
  ∀ (w : E) (k : ℕ+),
    Nesterov_second.γ f h f' x0 ↑k * h (Nesterov_second.y f h f' x0 ↑k) ≤
      Nesterov_second.γ f h f' x0 ↑k * h w -
        Nesterov_second.γ f h f' x0 ↑k / Nesterov_second.t f h f' x0 ↑k *
          inner
            (Nesterov_second.γ f h f' x0 ↑k • (Nesterov_second.y f h f' x0 (↑k - 1) - Nesterov_second.y f h f' x0 ↑k) -
              Nesterov_second.t f h f' x0 ↑k • f' (Nesterov_second.z f h f' x0 k))
            (w - Nesterov_second.y f h f' x0 ↑k)
hieq3 :
  ∀ (w : E) (k : ℕ+),
    h (Nesterov_second.x f h f' x0 ↑k) ≤
      (1 - Nesterov_second.γ f h f' x0 ↑k) * h (Nesterov_second.x f h f' x0 (↑k - 1)) +
          Nesterov_second.γ f h f' x0 ↑k * h w -
        Nesterov_second.γ f h f' x0 ↑k / Nesterov_second.t f h f' x0 ↑k *
          inner
            (Nesterov_second.γ f h f' x0 ↑k • (Nesterov_second.y f h f' x0 (↑k - 1) - Nesterov_second.y f h f' x0 ↑k) -
              Nesterov_second.t f h f' x0 ↑k • f' (Nesterov_second.z f h f' x0 k))
            (w - Nesterov_second.y f h f' x0 ↑k)
hieq4 :
  ∀ (k : ℕ+),
    f (Nesterov_second.x f h f' x0 ↑k) ≤
      f (Nesterov_second.z f h f' x0 k) +
          inner (f' (Nesterov_second.z f h f' x0 k)) (Nesterov_second.x f h f' x0 ↑k - Nesterov_second.z f h f' x0 k) +
        ↑(Nesterov_second.l f h f' x0) / 2 * ‖Nesterov_second.x f h f' x0 ↑k - Nesterov_second.z f h f' x0 k‖ ^ 2
k : ℕ+
⊢ f (Nesterov_second.x f h f' x0 ↑k) ≤
    f (Nesterov_second.z f h f' x0 k) +
        inner (f' (Nesterov_second.z f h f' x0 k)) (Nesterov_second.x f h f' x0 ↑k - Nesterov_second.z f h f' x0 k) +
      1 / (2 * Nesterov_second.t f h f' x0 ↑k) * ‖Nesterov_second.x f h f' x0 ↑k - Nesterov_second.z f h f' x0 k‖ ^ 2

Tactic:
apply le_trans (hieq4 k)
    simp only [add_le_add_iff_left]
    by_cases nm0 : ‖alg.x ↑k - alg.z k‖ ^ 2 = 0
    .
  rw [nm0]
  simp
    .
  push_neg at nm0
  have ax : ‖alg.x ↑k - alg.z k‖ ^ 2 > 0 := by
    apply lt_of_le_of_ne
    simp
    symm
    apply nm0
  to_theorem
    apply
      (mul_le_mul_right ax).mpr
        have lc2 : alg.l / 2 > (0 : ℝ) := by
      linarith [alg.hl]have tc2 : (2 * alg.t ↑k) > 0 := by
      linarith [(alg.tbound k).1]rw [one_div]apply (le_inv_comm₀ lc2 tc2).mpr
  rw [← one_div, ← div_mul, mul_comm]
  linarith [(alg.tbound k).2]

Tactic state after: