import Mathlib
import Optlib


open Set

theorem extracted_formal_statement_3.{u_1} {E : Type u_1} [inst : NormedAddCommGroup E] [inst_1 : InnerProductSpace ℝ E]
  [inst_2 : CompleteSpace E] {f : E → ℝ} {m : ℝ} {f' : E → E} {x y : E} {l : NNReal} (mp : m > 0)
  (hf : ∀ (x : E), HasGradientAt f (f' x) x) (h₂ : LipschitzWith l f') (hl : ↑l > 0) (cov : Convex ℝ univ)
  (hsc :
    ∀ ⦃x : E⦄,
      x ∈ univ →
        ∀ ⦃y : E⦄,
          y ∈ univ →
            ∀ ⦃a b : ℝ⦄,
              0 ≤ a → 0 ≤ b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y - a * b * (m / 2 * ‖x - y‖ ^ 2)) :
  let phi := fun x => ↑l / 2 * ‖x‖ ^ 2 - f x;
  ConvexOn ℝ univ phi →
    let g := fun x => f x - m / 2 * ‖x‖ ^ 2;
    let g' := fun x => f' x - m • x;
    let h := fun x => (↑l - m) / 2 * ‖x‖ ^ 2 - g x;
    (∀ (x : E), HasGradientAt g (g' x) x) →
      ConvexOn ℝ univ g →
        ConvexOn ℝ univ h →
          0 < ↑l - m →
            inner (g' x - g' y) (x - y) ≥ 1 / (↑l - m) * ‖g' x - g' y‖ ^ 2 →
              let alpha := f' x - f' y;
              let beta := x - y;
              g' x - g' y = alpha - m • beta →
                inner (f' x - f' y) (x - y) ≥ m * ↑l / (m + ↑l) * ‖x - y‖ ^ 2 + 1 / (m + ↑l) * ‖f' x - f' y‖ ^ 2 →
                  inner (alpha - m • beta) (x - y) ≥ 1 / (↑l - m) * ‖alpha - m • beta‖ ^ 2 := sorry

Tactic state before:
case pos
E : Type u_1
inst✝² : NormedAddCommGroup E
inst✝¹ : InnerProductSpace ℝ E
inst✝ : CompleteSpace E
f : E → ℝ
m : ℝ
f' : E → E
x y : E
l : NNReal
mp : m > 0
hf : ∀ (x : E), HasGradientAt f (f' x) x
h₂ : LipschitzWith l f'
hl : ↑l > 0
cov : Convex ℝ univ
hsc :
  ∀ ⦃x : E⦄,
    x ∈ univ →
      ∀ ⦃y : E⦄,
        y ∈ univ →
          ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y - a * b * (m / 2 * ‖x - y‖ ^ 2)
phi : E → ℝ := fun x => ↑l / 2 * ‖x‖ ^ 2 - f x
convphi : ConvexOn ℝ univ phi
g : E → ℝ := fun x => f x - m / 2 * ‖x‖ ^ 2
g' : E → E := fun x => f' x - m • x
h : E → ℝ := fun x => (↑l - m) / 2 * ‖x‖ ^ 2 - g x
gderiv : ∀ (x : E), HasGradientAt g (g' x) x
convg : ConvexOn ℝ univ g
convh : ConvexOn ℝ univ h
coef : 0 < ↑l - m
eq1 : inner (g' x - g' y) (x - y) ≥ 1 / (↑l - m) * ‖g' x - g' y‖ ^ 2
alpha : E := f' x - f' y
beta : E := x - y
eq2 : g' x - g' y = alpha - m • beta
⊢ inner (f' x - f' y) (x - y) ≥ m * ↑l / (m + ↑l) * ‖x - y‖ ^ 2 + 1 / (m + ↑l) * ‖f' x - f' y‖ ^ 2

Tactic:
rw [eq2] at eq1
    have eq3 (u v : E) :
  inner (u - m • v) v ≥ 1 / (l - m) * ‖u - m • v‖ ^ 2 → inner u v ≥ m * l / (m + l) * ‖v‖ ^ 2 + 1 / (m + l) * ‖u‖ ^ 2 :=
  by
  have : ‖u - m • v‖ ^ 2 = ‖u‖ ^ 2 + m ^ 2 * ‖v‖ ^ 2 - 2 * m * inner u v :=
    by
    rw [norm_sub_sq_real, inner_smul_right]; ring_nf; rw [norm_smul]; simp
    rw [mul_pow, sq_abs]
  rw [this]
  intro h0; rw [inner_sub_left, inner_smul_left] at h0; field_simp at h0
  rw [real_inner_self_eq_norm_sq, div_le_iff₀] at h0
  field_simp at h0; rw [sub_mul, sub_add_eq_add_sub, le_sub_iff_add_le] at h0
  rw [mul_right_comm, mul_sub] at h0; ring_nf at h0
  rw [mul_right_comm, ← add_mul] at h0
  have mlpos : 0 < m + l := by linarith
  rw [ge_iff_le]
  field_simp; rw [add_comm]
  calc
    _ ≤ ((m + l) * inner u v) / (m + l) := by rw [div_le_div_iff_of_pos_right]; apply h0; apply mlpos
    _ = inner u v := by field_simp
  apply coef
    show inner alpha beta ≥ m * l / (m + l) * ‖beta‖ ^ 2 + 1 / (m + l) * ‖alpha‖ ^ 2
    apply eq3
    show inner (alpha - m • beta) (x - y) ≥ 1 / (l - m) * ‖alpha - m • beta‖ ^ 2

Tactic state after:
[case pos.a
 E : Type u_1
 inst✝² : NormedAddCommGroup E
 inst✝¹ : InnerProductSpace ℝ E
 inst✝ : CompleteSpace E
 f : E → ℝ
 m : ℝ
 f' : E → E
 x y : E
 l : NNReal
 mp : m > 0
 hf : ∀ (x : E), HasGradientAt f (f' x) x
 h₂ : LipschitzWith l f'
 hl : ↑l > 0
 cov : Convex ℝ univ
 hsc :
   ∀ ⦃x : E⦄,
     x ∈ univ →
       ∀ ⦃y : E⦄,
         y ∈ univ →
           ∀ ⦃a b : ℝ⦄,
             0 ≤ a → 0 ≤ b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y - a * b * (m / 2 * ‖x - y‖ ^ 2)
 phi : E → ℝ := fun x => ↑l / 2 * ‖x‖ ^ 2 - f x
 convphi : ConvexOn ℝ univ phi
 g : E → ℝ := fun x => f x - m / 2 * ‖x‖ ^ 2
 g' : E → E := fun x => f' x - m • x
 h : E → ℝ := fun x => (↑l - m) / 2 * ‖x‖ ^ 2 - g x
 gderiv : ∀ (x : E), HasGradientAt g (g' x) x
 convg : ConvexOn ℝ univ g
 convh : ConvexOn ℝ univ h
 coef : 0 < ↑l - m
 alpha : E := f' x - f' y
 beta : E := x - y
 eq1 : inner (alpha - m • beta) (x - y) ≥ 1 / (↑l - m) * ‖alpha - m • beta‖ ^ 2
 eq2 : g' x - g' y = alpha - m • beta
 eq3 :
   ∀ (u v : E),
     inner (u - m • v) v ≥ 1 / (↑l - m) * ‖u - m • v‖ ^ 2 →
       inner u v ≥ m * ↑l / (m + ↑l) * ‖v‖ ^ 2 + 1 / (m + ↑l) * ‖u‖ ^ 2
 ⊢ inner (alpha - m • beta) (x - y) ≥ 1 / (↑l - m) * ‖alpha - m • beta‖ ^ 2]