import Mathlib
import Optlib


open Set

theorem extracted_formal_statement_8.{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 →
            let alpha := f' x - f' y;
            let beta := x - y;
            inner alpha beta ≥ m * ‖beta‖ ^ 2 →
              inner alpha beta ≥ 1 / ↑l * ‖alpha‖ ^ 2 →
                inner (f' x - f' y) (x - y) ≥ m * ↑l / (m + ↑l) * ‖x - y‖ ^ 2 + 1 / (m + ↑l) * ‖f' x - f' y‖ ^ 2 →
                  inner alpha beta ≥ m * ↑l / (m + ↑l) * ‖beta‖ ^ 2 + 1 / (m + ↑l) * ‖alpha‖ ^ 2 := sorry

Tactic state before:
case neg
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 beta ≥ m * ‖beta‖ ^ 2
eq2 : inner alpha beta ≥ 1 / ↑l * ‖alpha‖ ^ 2
⊢ inner (f' x - f' y) (x - y) ≥ m * ↑l / (m + ↑l) * ‖x - y‖ ^ 2 + 1 / (m + ↑l) * ‖f' x - f' y‖ ^ 2

Tactic:
rw [ge_iff_le] at eq1
    rw [ge_iff_le] at eq2
    have mlpos : 0 < m + l := by linarith
    have eq3 (u v : E) (h1 : m * ‖v‖ ^ 2 ≤ inner u v) (h2 : 1 / l * ‖u‖ ^ 2 ≤ inner u v) :
  inner u v ≥ m * l / (m + l) * ‖v‖ ^ 2 + 1 / (m + l) * ‖u‖ ^ 2 :=
  by
  field_simp; rw [div_le_iff₀ mlpos, mul_comm _ (m + l), add_mul]
  have eq4 : m * l * ‖v‖ ^ 2 ≤ m * inner u v := by
    calc
      _ ≤ m * m * ‖v‖ ^ 2 := by
        rw [mul_comm m l, mul_assoc, mul_assoc]
        have : 0 ≤ ‖v‖ ^ 2 := by simp
        apply mul_le_mul_of_nonneg_right
        simp at coef; apply coef
        rw [mul_nonneg_iff_right_nonneg_of_pos]; simp; apply mp
      _ ≤ m * inner u v := by rw [mul_assoc, mul_le_mul_left]; apply h1; apply mp
  have eq5 : ‖u‖ ^ 2 ≤ l * inner u v := by field_simp at h2; rw [mul_comm, ← div_le_iff₀]; apply h2; apply hl
  linarith
    show inner alpha beta ≥ m * l / (m + l) * ‖beta‖ ^ 2 + 1 / (m + l) * ‖alpha‖ ^ 2

Tactic state after:
[case neg
 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 : m * ‖beta‖ ^ 2 ≤ inner alpha beta
 eq2 : 1 / ↑l * ‖alpha‖ ^ 2 ≤ inner alpha beta
 mlpos : 0 < m + ↑l
 eq3 :
   ∀ (u v : E),
     m * ‖v‖ ^ 2 ≤ inner u v →
       1 / ↑l * ‖u‖ ^ 2 ≤ inner u v → inner u v ≥ m * ↑l / (m + ↑l) * ‖v‖ ^ 2 + 1 / (m + ↑l) * ‖u‖ ^ 2
 ⊢ inner alpha beta ≥ m * ↑l / (m + ↑l) * ‖beta‖ ^ 2 + 1 / (m + ↑l) * ‖alpha‖ ^ 2]