import Mathlib
import FoML


open MeasureTheory Filter Finset Real

open scoped MeasureTheory ProbabilityTheory ENNReal NNReal

theorem extracted_formal_statement_3.{u_1} {Ω : Type u_1} {m : MeasurableSpace Ω} {μ : Measure Ω}
  [inst : IsFiniteMeasure μ] (t a b : ℝ) {X : Ω → ℝ} (hX : AEMeasurable X μ) (h : ∀ᵐ (ω : Ω) ∂μ, X ω ∈ Set.Icc a b) :
  let e := fun t ω => rexp (t * X ω) * X ω;
  let e' := fun t ω => rexp (t * X ω) * X ω ^ 2;
  Integrable (e' t) μ ∧ HasDerivAt (fun t => ∫ (x : Ω), e t x ∂μ) (∫ (x : Ω), e' t x ∂μ) t →
    ∀ᵐ (a : Ω) ∂μ, ∀ x ∈ Metric.ball t (|t| + 1), HasDerivAt (fun x => e x a) (e' x a) x := sorry

Tactic state before:
Ω : Type u_1
m : MeasurableSpace Ω
μ : Measure Ω
inst✝ : IsFiniteMeasure μ
t a b : ℝ
X : Ω → ℝ
hX : AEMeasurable X μ
h : ∀ᵐ (ω : Ω) ∂μ, X ω ∈ Set.Icc a b
c : ℝ := ‖a‖ ⊔ ‖b‖
e : ℝ → Ω → ℝ := fun t ω => rexp (t * X ω) * X ω
e' : ℝ → Ω → ℝ := fun t ω => rexp (t * X ω) * X ω ^ 2
⊢ Integrable (e' t) μ ∧ HasDerivAt (fun t => ∫ (x : Ω), e t x ∂μ) (∫ (x : Ω), e' t x ∂μ) t

Tactic:
apply hasDerivAt_integral_of_dominated_loc_of_deriv_le
    · exact LE.le.trans_lt (abs_nonneg t) (lt_add_one |t|)
    ·
  apply Filter.Eventually.of_forall
  intro t'
  dsimp only [e]
  rw [aestronglyMeasurable_iff_aemeasurable]
  apply AEMeasurable.mul _ hX
  exact Measurable.comp_aemeasurable' measurable_exp (hX.const_mul t')
    ·
  simp only [e]; apply MeasureTheory.Integrable.bdd_mul' (integrable_bounded a b hX h) (aemeasurable_expt t hX)
  · filter_upwards [h] with ω h
    change ‖rexp (t * X ω)‖ ≤ rexp (|t| * c)
    simp only [norm_eq_abs, abs_exp, exp_le_exp]
    calc
      _ ≤ |t * X ω| := le_abs_self (t * X ω)
      _ = |t| * |X ω| := (abs_mul t (X ω))
      _ ≤ |t| * c := by
        apply mul_le_mul_of_nonneg_left
        · rw [← (by dsimp only [c]; simp only [norm_eq_abs, abs_eq_self, le_max_iff, abs_nonneg, or_self] : |c| = c)]
          exact le_trans' (le_abs_self (max ‖a‖ ‖b‖)) (abs_le_max_abs_abs h.1 h.2)
        · exact abs_nonneg t
    ·
  dsimp only [e']
  rw [aestronglyMeasurable_iff_aemeasurable]
  apply AEMeasurable.mul
  · apply Measurable.comp_aemeasurable' _ _
    · exact measurable_exp
    · exact hX.const_mul t
  exact hX.pow_const 2

Tactic state after:
[case h_bound
 Ω : Type u_1
 m : MeasurableSpace Ω
 μ : Measure Ω
 inst✝ : IsFiniteMeasure μ
 t a b : ℝ
 X : Ω → ℝ
 hX : AEMeasurable X μ
 h : ∀ᵐ (ω : Ω) ∂μ, X ω ∈ Set.Icc a b
 c : ℝ := ‖a‖ ⊔ ‖b‖
 e : ℝ → Ω → ℝ := fun t ω => rexp (t * X ω) * X ω
 e' : ℝ → Ω → ℝ := fun t ω => rexp (t * X ω) * X ω ^ 2
 ⊢ ∀ᵐ (a : Ω) ∂μ, ∀ x ∈ Metric.ball t (|t| + 1), ‖e' x a‖ ≤ ?bound a,
 case bound_integrable
 Ω : Type u_1
 m : MeasurableSpace Ω
 μ : Measure Ω
 inst✝ : IsFiniteMeasure μ
 t a b : ℝ
 X : Ω → ℝ
 hX : AEMeasurable X μ
 h : ∀ᵐ (ω : Ω) ∂μ, X ω ∈ Set.Icc a b
 c : ℝ := ‖a‖ ⊔ ‖b‖
 e : ℝ → Ω → ℝ := fun t ω => rexp (t * X ω) * X ω
 e' : ℝ → Ω → ℝ := fun t ω => rexp (t * X ω) * X ω ^ 2
 ⊢ Integrable ?bound μ,
 case h_diff
 Ω : Type u_1
 m : MeasurableSpace Ω
 μ : Measure Ω
 inst✝ : IsFiniteMeasure μ
 t a b : ℝ
 X : Ω → ℝ
 hX : AEMeasurable X μ
 h : ∀ᵐ (ω : Ω) ∂μ, X ω ∈ Set.Icc a b
 c : ℝ := ‖a‖ ⊔ ‖b‖
 e : ℝ → Ω → ℝ := fun t ω => rexp (t * X ω) * X ω
 e' : ℝ → Ω → ℝ := fun t ω => rexp (t * X ω) * X ω ^ 2
 ⊢ ∀ᵐ (a : Ω) ∂μ, ∀ x ∈ Metric.ball t (|t| + 1), HasDerivAt (fun x => e x a) (e' x a) x,
 case bound
 Ω : Type u_1
 m : MeasurableSpace Ω
 μ : Measure Ω
 inst✝ : IsFiniteMeasure μ
 t a b : ℝ
 X : Ω → ℝ
 hX : AEMeasurable X μ
 h : ∀ᵐ (ω : Ω) ∂μ, X ω ∈ Set.Icc a b
 c : ℝ := ‖a‖ ⊔ ‖b‖
 e : ℝ → Ω → ℝ := fun t ω => rexp (t * X ω) * X ω
 e' : ℝ → Ω → ℝ := fun t ω => rexp (t * X ω) * X ω ^ 2
 ⊢ Ω → ℝ]