import Mathlib
import Optlib


open Set Real Matrix Finset

open Set Real Matrix Finset NNReal

theorem extracted_formal_statement_15 {n : ℕ+} {t μ : ℝ} {h : EuclideanSpace ℝ (Fin ↑n) → ℝ}
  (lasso : h = fun y => μ • ∑ i, ‖y i‖) (x xm : EuclideanSpace ℝ (Fin ↑n)) (tpos : 0 < t) (μpos : 0 < μ)
  (minpoint : ∀ (i : Fin ↑n), xm i = sign (x i) * ((|x i| - t * μ) ⊔ 0)) :
  let g := (t * μ) • fun x => ∑ i, ‖x i‖;
  g = t • h →
    0 < t * μ →
      ∀ (y : EuclideanSpace ℝ (Fin ↑n)),
        ∀ i ∈ Finset.univ,
          ¬xm i = 0 →
            SubderivAt abs (xm i) = {sign (xm i)} →
              let sgnxm := sign (xm i);
              |y i| ≥ |xm i| + inner sgnxm (y i - xm i) →
                x i ≠ 0 →
                  t * μ < |x i| →
                    (|x i| - t * μ) ⊔ 0 = |x i| - t * μ →
                      ¬0 < x i →
                        x i < 0 →
                          (sign (x i) * (|x i| - t * μ)).sign = -1 →
                            (y i - sign (x i) * (|x i| - t * μ)) * (sign (x i) * (|x i| - t * μ)).sign =
                                μ⁻¹ * t⁻¹ * (x i - sign (x i) * (|x i| - t * μ)) * y i -
                                  μ⁻¹ * t⁻¹ * (x i - sign (x i) * (|x i| - t * μ)) * (sign (x i) * (|x i| - t * μ)) →
                              μ⁻¹ * t⁻¹ * (x i - -1 * (-x i - t * μ)) * y i -
                                  μ⁻¹ * t⁻¹ * (x i - -1 * (-x i - t * μ)) * (-1 * (-x i - t * μ)) =
                                (y i - -1 * (-x i - t * μ)) * -1 := sorry

Tactic state before:
case neg
n : ℕ+
t μ : ℝ
h : EuclideanSpace ℝ (Fin ↑n) → ℝ
lasso : h = fun y => μ • ∑ i, ‖y i‖
x xm : EuclideanSpace ℝ (Fin ↑n)
tpos : 0 < t
μpos : 0 < μ
minpoint : ∀ (i : Fin ↑n), xm i = sign (x i) * ((|x i| - t * μ) ⊔ 0)
g : EuclideanSpace ℝ (Fin ↑n) → ℝ := (t * μ) • fun x => ∑ i, ‖x i‖
geqth : g = t • h
tμpos : 0 < t * μ
y : EuclideanSpace ℝ (Fin ↑n)
i : Fin ↑n
a✝ : i ∈ Finset.univ
left✝ : ¬xm i = 0
abs_subg : SubderivAt abs (xm i) = {sign (xm i)}
sgnxm : ℝ := sign (xm i)
aux : |y i| ≥ |xm i| + inner sgnxm (y i - xm i)
xiieq0 : x i ≠ 0
ieq : t * μ < |x i|
eq1 : (|x i| - t * μ) ⊔ 0 = |x i| - t * μ
hx : ¬0 < x i
xneg : x i < 0
eq2 : (sign (x i) * (|x i| - t * μ)).sign = -1
⊢ (y i - sign (x i) * (|x i| - t * μ)) * (sign (x i) * (|x i| - t * μ)).sign =
    μ⁻¹ * t⁻¹ * (x i - sign (x i) * (|x i| - t * μ)) * y i -
      μ⁻¹ * t⁻¹ * (x i - sign (x i) * (|x i| - t * μ)) * (sign (x i) * (|x i| - t * μ))

Tactic:
rw [eq2]
    symm
    have eq_negxi : |x i| = -x i := by apply abs_eq_neg_self.mpr; linarith
    have eq_signx : sign (x i) = -1 := by apply Real.sign_of_neg xneg
    repeat rw [eq_signx, eq_negxi]

Tactic state after:
[case neg
 n : ℕ+
 t μ : ℝ
 h : EuclideanSpace ℝ (Fin ↑n) → ℝ
 lasso : h = fun y => μ • ∑ i, ‖y i‖
 x xm : EuclideanSpace ℝ (Fin ↑n)
 tpos : 0 < t
 μpos : 0 < μ
 minpoint : ∀ (i : Fin ↑n), xm i = sign (x i) * ((|x i| - t * μ) ⊔ 0)
 g : EuclideanSpace ℝ (Fin ↑n) → ℝ := (t * μ) • fun x => ∑ i, ‖x i‖
 geqth : g = t • h
 tμpos : 0 < t * μ
 y : EuclideanSpace ℝ (Fin ↑n)
 i : Fin ↑n
 a✝ : i ∈ Finset.univ
 left✝ : ¬xm i = 0
 abs_subg : SubderivAt abs (xm i) = {sign (xm i)}
 sgnxm : ℝ := sign (xm i)
 aux : |y i| ≥ |xm i| + inner sgnxm (y i - xm i)
 xiieq0 : x i ≠ 0
 ieq : t * μ < |x i|
 eq1 : (|x i| - t * μ) ⊔ 0 = |x i| - t * μ
 hx : ¬0 < x i
 xneg : x i < 0
 eq2 : (sign (x i) * (|x i| - t * μ)).sign = -1
 eq_negxi : |x i| = -x i
 eq_signx : sign (x i) = -1
 ⊢ μ⁻¹ * t⁻¹ * (x i - -1 * (-x i - t * μ)) * y i - μ⁻¹ * t⁻¹ * (x i - -1 * (-x i - t * μ)) * (-1 * (-x i - t * μ)) =
     (y i - -1 * (-x i - t * μ)) * -1]