introvoyz041/TaoAnalysisBench huggingface dataset:
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

/--
Tao, Analysis I, Thm. 11.4.1(h) (laws of integration), rephrased in Mathlib terms.

If `f` is integrable on a closed, bounded interval `[a, c]` and `b` is an
intermediate point (`a ≤ b ≤ c`), then `f` is integrable on `[a, b]` and `[b, c]`,
and the integral over `[a, c]` is the sum of the integrals over the two subintervals.
Here we use the (Lebesgue) set integral with respect to `volume`.
-/
theorem IntegrableOn.join
  {a b c : ℝ} {f : ℝ → ℝ}
  (hab : a ≤ b) (hbc : b ≤ c)
  (h : MeasureTheory.IntegrableOn f (Icc a c)) :
  MeasureTheory.IntegrableOn f (Icc a b) ∧
  MeasureTheory.IntegrableOn f (Icc b c) ∧
  (∫ x in Icc a c, f x) =
    (∫ x in Icc a b, f x) + (∫ x in Icc b c, f x) := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

namespace Chapter11

/-- If `f` and `g` are integrable on a bounded real interval, then so is their pointwise minimum. -/
theorem IntegrableOn.min
  {a b : ℝ} {f g : ℝ → ℝ}
  (hf : MeasureTheory.IntegrableOn f (Set.Icc a b))
  (hg : MeasureTheory.IntegrableOn g (Set.Icc a b)) :
  MeasureTheory.IntegrableOn (f ⊓ g) (Set.Icc a b) := by
end Chapter11

end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

open MeasureTheory

namespace IntegrableOn
theorem mono {s : Set ℝ}
    (hs : ∃ a b : ℝ,
      s = Set.Ioo a b ∨ s = Set.Icc a b ∨ s = Set.Ioc a b ∨ s = Set.Ico a b)
    {f g : ℝ → ℝ}
    (hf : MeasureTheory.IntegrableOn f s)
    (hg : MeasureTheory.IntegrableOn g s)
    (h : ∀ x ∈ s, f x ≤ g x) :
    ∫ x in s, f x ≤ ∫ x in s, g x := by
end IntegrableOn

end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

namespace Chapter11

/-- Nonnegativity of the integral of a nonnegative integrable function on a bounded interval. -/
theorem IntegrableOn.nonneg {a b : ℝ} {f : ℝ → ℝ}
    (hf : MeasureTheory.IntegrableOn f (Set.Icc a b))
    (hf_nonneg : ∀ x ∈ Set.Icc a b, 0 ≤ f x) :
    0 ≤ ∫ x in Set.Icc a b, f x := by
end Chapter11

end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

open MeasureTheory

/-- Mathlib translation of Tao's Chapter11.IntegrableOn.of_extend.
If a real-valued function `f` is integrable on a bounded interval `I`,
then its extension by zero outside `I` is integrable on any larger
bounded interval `J` containing `I`.
Here `I` and `J` are sets known to be one of the four standard
bounded real intervals: `(a,b)`, `[a,b]`, `(a,b]`, or `[a,b)`.
This uses Mathlib's `IntegrableOn` (Lebesgue integrability) on sets. -/
theorem IntegrableOn.of_extend
  {I J : Set ℝ}
  (hI : ∃ a b : ℝ,
    I = Ioo a b ∨ I = Ioc a b ∨ I = Ico a b ∨ I = Icc a b)
  (hJ : ∃ a b : ℝ,
    J = Ioo a b ∨ J = Ioc a b ∨ J = Ico a b ∨ J = Icc a b)
  (hIJ : I ⊆ J)
  {f : ℝ → ℝ}
  (h : IntegrableOn f I) :
  IntegrableOn (I.indicator f) J := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

open MeasureTheory
theorem IntegrableOn.of_extend'
  {s t : Set ℝ} {f : ℝ → ℝ}
  (hst : s ⊆ t)
  (hf : MeasureTheory.IntegrableOn f s) :
  ∫ x in t, s.indicator f x = ∫ x in s, f x := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench
theorem IntegrableOn.smul {a b : ℝ} (c : ℝ) {f : ℝ → ℝ}
    (hf : MeasureTheory.IntegrableOn f (Set.Icc a b)) :
  MeasureTheory.IntegrableOn (c • f) (Set.Icc a b) ∧
    (∫ x in Set.Icc a b, c • f x) =
      c * (∫ x in Set.Icc a b, f x) := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter
open scoped BigOperators

namespace TaoBench
theorem IntegrableOn.split
  {a b : ℝ} {f : ℝ → ℝ}
  (hf : MeasureTheory.IntegrableOn f (Set.Icc a b))
  (P : Finset (Set ℝ))
  (hpart :
    (∀ x ∈ Set.Icc a b, ∃! J, J ∈ P ∧ x ∈ J) ∧
    (∀ J ∈ P, J ⊆ Set.Icc a b) ∧
    (∀ J ∈ P, ∃ c d : ℝ, J = Set.Icc c d)) :
  ∫ x in Set.Icc a b, f x = Finset.sum P (fun J => ∫ x in J, f x) := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

/-- Theorem 11.4.1(c) / Exercise 11.4.1, translated to Mathlib:
If `f` and `g` are integrable on a bounded interval `I ⊆ ℝ`, then `f - g` is
integrable on `I`, and the integral of `f - g` over `I` is the difference of the
integrals of `f` and `g` over `I`. Here a \"bounded interval\" means a set of the
form `Icc a b`, `Ioc a b`, `Ico a b`, or `Ioo a b` for some real numbers
`a ≤ b`. -/
theorem IntegrableOn.sub
    {I : Set ℝ}
    (hI : ∃ a b : ℝ, a ≤ b ∧
      (I = Set.Icc a b ∨ I = Set.Ioc a b ∨ I = Set.Ico a b ∨ I = Set.Ioo a b))
    {f g : ℝ → ℝ}
    (hf : MeasureTheory.IntegrableOn f I)
    (hg : MeasureTheory.IntegrableOn g I) :
    MeasureTheory.IntegrableOn (f - g) I ∧
      (∫ x in I, (f - g) x) =
        (∫ x in I, f x) - (∫ x in I, g x) := by
end TaoBench
import Mathlib.Data.Real.Basic

namespace Chapter11

inductive BoundedInterval where
  | Ioo (a b:ℝ) : BoundedInterval
  | Icc (a b:ℝ) : BoundedInterval
  | Ioc (a b:ℝ) : BoundedInterval
  | Ico (a b:ℝ) : BoundedInterval

open BoundedInterval

/-- There is a technical issue in that this coercion is not injective: the empty set is represented by multiple bounded intervals.  This causes some of the statements in this section to be a little uglier than necessary.-/
@[coe]
def BoundedInterval.toSet (I: BoundedInterval) : Set ℝ := match I with
  | Ioo a b => .Ioo a b
  | Icc a b => .Icc a b
  | Ioc a b => .Ioc a b
  | Ico a b => .Ico a b

instance BoundedInterval.inst_coeSet : Coe BoundedInterval (Set ℝ) where
  coe := toSet

/-- Definition 11.3.1 (Majorization of functions) -/
abbrev MajorizesOn (g f:ℝ → ℝ) (I: BoundedInterval) : Prop := ∀ x ∈ (I:Set ℝ), f x ≤ g x

/-- Exercise 11.3.1 -/
theorem MajorizesOn.anti_symm {f g: ℝ → ℝ} {I: BoundedInterval}:
  ∀ x ∈ (I:Set ℝ), f x = g x ↔ MajorizesOn f g I ∧ MajorizesOn g f I := by
end Chapter11
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

/-- Exercise 11.3.1 (Mathlib version) -/
theorem MajorizesOn.anti_symm {f g : ℝ → ℝ} {I : Set ℝ} :
  ∀ x ∈ I, f x = g x ↔
    ((∀ x ∈ I, g x ≤ f x) ∧ (∀ x ∈ I, f x ≤ g x)) := by
end TaoBench
import Mathlib.Tactic
import Mathlib.Data.Real.Basic
import Mathlib.Data.Set.Intervals.Basic

/-!
Self-contained slice for Chapter 11.3 target theorem
-/

namespace Chapter11

/-- Bounded intervals (from Section 11.1) -/
inductive BoundedInterval where
  | Ioo (a b:ℝ) : BoundedInterval
  | Icc (a b:ℝ) : BoundedInterval
  | Ioc (a b:ℝ) : BoundedInterval
  | Ico (a b:ℝ) : BoundedInterval

open BoundedInterval

/-- Coercion of a bounded interval to the corresponding set (from Section 11.1) -/
@[coe]
def BoundedInterval.toSet (I: BoundedInterval) : Set ℝ := match I with
  | Ioo a b => .Ioo a b
  | Icc a b => .Icc a b
  | Ioc a b => .Ioc a b
  | Ico a b => .Ico a b

instance BoundedInterval.inst_coeSet : Coe BoundedInterval (Set ℝ) where
  coe := toSet

/-- Definition 11.3.1 (Majorization of functions) -/
abbrev MajorizesOn (g f:ℝ → ℝ) (I: BoundedInterval) : Prop := ∀ x ∈ (I:Set ℝ), f x ≤ g x

/-- Exercise 11.3.2 -/
def MajorizesOn.of_add : Decidable ( ∀ (f g h:ℝ → ℝ) (I:BoundedInterval) (hfg: MajorizesOn f g I),
 MajorizesOn (f+h) (g+h) I) := by
end Chapter11
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

namespace Chapter11

/-- Exercise 11.3.2 (translated to Mathlib).

If `f` majorizes `g` on a bounded interval `I` (i.e. `g x ≤ f x` for all `x ∈ I`),
then `f + h` majorizes `g + h` on the same interval. Here a “bounded interval”
is any set of the form `Ioo a b`, `Icc a b`, `Ioc a b`, or `Ico a b`. -/
def MajorizesOn.of_add :
    Decidable
      (∀ (f g h : ℝ → ℝ) (I : Set ℝ),
        (∃ a b : ℝ,
          I = Set.Ioo a b ∨
            I = Set.Icc a b ∨
            I = Set.Ioc a b ∨
            I = Set.Ico a b) →
        (∀ x ∈ I, g x ≤ f x) →
        ∀ x ∈ I, g x + h x ≤ f x + h x) := by
end Chapter11

end TaoBench
import Mathlib.Data.Real.Basic
import Mathlib.Data.Set.Lattice

namespace Chapter11

inductive BoundedInterval where
  | Ioo (a b:ℝ) : BoundedInterval
  | Icc (a b:ℝ) : BoundedInterval
  | Ioc (a b:ℝ) : BoundedInterval
  | Ico (a b:ℝ) : BoundedInterval

open BoundedInterval

/-- There is a technical issue in that this coercion is not injective: the empty set is represented by multiple bounded intervals.  This causes some of the statements in this section to be a little uglier than necessary.-/
@[coe]
def BoundedInterval.toSet (I: BoundedInterval) : Set ℝ := match I with
  | Ioo a b => .Ioo a b
  | Icc a b => .Icc a b
  | Ioc a b => .Ioc a b
  | Ico a b => .Ico a b

instance BoundedInterval.inst_coeSet : Coe BoundedInterval (Set ℝ) where
  coe := toSet

/-- Definition 11.3.1 (Majorization of functions) -/
abbrev MajorizesOn (g f:ℝ → ℝ) (I: BoundedInterval) : Prop := ∀ x ∈ (I:Set ℝ), f x ≤ g x

/-- Exercise 11.3.1 -/
theorem MajorizesOn.trans {f g h: ℝ → ℝ} {I: BoundedInterval}
  (hfg: MajorizesOn f g I) (hgh: MajorizesOn g h I) : MajorizesOn f h I := by
end Chapter11
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench
theorem MajorizesOn.trans {f g h : ℝ → ℝ} {s : Set ℝ}
    (hs : ∃ a b : ℝ,
      s = Set.Ioo a b ∨ s = Set.Icc a b ∨ s = Set.Ioc a b ∨ s = Set.Ico a b)
    (hfg : ∀ x ∈ s, g x ≤ f x)
    (hgh : ∀ x ∈ s, h x ≤ g x) :
    ∀ x ∈ s, h x ≤ f x := by
end TaoBench
import Mathlib.Data.Real.Basic
import Mathlib.Order.Interval
import Mathlib.Data.Finset.Basic

namespace Chapter11

inductive BoundedInterval where
  | Ioo (a b:ℝ) : BoundedInterval
  | Icc (a b:ℝ) : BoundedInterval
  | Ioc (a b:ℝ) : BoundedInterval
  | Ico (a b:ℝ) : BoundedInterval

open BoundedInterval

@[coe]
def BoundedInterval.toSet (I: BoundedInterval) : Set ℝ := match I with
  | Ioo a b => .Ioo a b
  | Icc a b => .Icc a b
  | Ioc a b => .Ioc a b
  | Ico a b => .Ico a b

instance BoundedInterval.inst_coeSet : Coe BoundedInterval (Set ℝ) where
  coe := toSet

instance BoundedInterval.instMembership : Membership ℝ BoundedInterval where
  mem I x := x ∈ (I:Set ℝ)

instance BoundedInterval.instSubset : HasSubset BoundedInterval where
  Subset I J := ∀ x, x ∈ I → x ∈ J

abbrev BoundedInterval.a (I: BoundedInterval) : ℝ := match I with
  | Ioo a _ => a
  | Icc a _ => a
  | Ioc a _ => a
  | Ico a _ => a

abbrev BoundedInterval.b (I: BoundedInterval) : ℝ := match I with
  | Ioo _ b => b
  | Icc _ b => b
  | Ioc _ b => b
  | Ico _ b => b

@[ext]
structure Partition (I: BoundedInterval) where
  intervals : Finset BoundedInterval
  exists_unique (x:ℝ) (hx : x ∈ I) : ∃! J, J ∈ intervals ∧ x ∈ J
  contains (J : BoundedInterval) (hJ : J ∈ intervals) : J ⊆ I

instance Partition.instMembership (I: BoundedInterval) : Membership BoundedInterval (Partition I) where
  mem P J := J ∈ P.intervals

/-- Exercise 11.1.3.  The exercise only claims c ≤ b, but the stronger claim c < b is true and useful. -/
theorem Partition.exist_right {I: BoundedInterval} (hI: I.a < I.b) (hI': I.b ∉ I)
  {P: Partition I}
  : ∃ c ∈ Set.Ico I.a I.b, Ioo c I.b ∈ P ∨ Ico c I.b ∈ P := by
end Chapter11
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

namespace Partition

/-- Mathlib-only reformulation of `Chapter11.Partition.exist_right`. -/
theorem exist_right
  {a b : ℝ} (hI : a < b)
  {I : Set ℝ}
  (hIdef : I = Set.Ioo a b ∨ I = Set.Ico a b ∨ I = Set.Ioc a b ∨ I = Set.Icc a b)
  (hI' : b ∉ I)
  {ι : Type _} [Fintype ι]
  (P : ι → Set ℝ)
  (hcover : ∀ x, x ∈ I → ∃! i : ι, x ∈ P i)
  (hsubset : ∀ i, P i ⊆ I)
  (hshape :
    ∀ i, ∃ a' b' : ℝ,
      P i = Set.Ioo a' b' ∨
      P i = Set.Ioc a' b' ∨
      P i = Set.Ico a' b' ∨
      P i = Set.Icc a' b')
  : ∃ c ∈ Set.Ico a b,
      (∃ i, P i = Set.Ioo c b) ∨
      (∃ i, P i = Set.Ico c b) := by
end Partition

end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

open MeasureTheory
open scoped Interval
theorem PiecewiseConstantOn.RS_integ_add
  {f g : ℝ → ℝ} {μ : Measure ℝ} {a b : ℝ}
  (hf : IntervalIntegrable f μ a b)
  (hg : IntervalIntegrable g μ a b) :
  (∫ x in a..b, f x + g x ∂μ)
    = (∫ x in a..b, f x ∂μ) + (∫ x in a..b, g x ∂μ) := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench
theorem PiecewiseConstantOn.RS_integ_const
    (c : ℝ) (μ : MeasureTheory.Measure ℝ) [MeasureTheory.IsFiniteMeasure μ] (s : Set ℝ) :
    (∫ x in s, (fun _ ↦ c) x ∂μ) = c * (μ s).toReal := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

open MeasureTheory
theorem RS_integ_const'
  {α : StieltjesFunction} {f : ℝ → ℝ} {s : Set ℝ}
  (h_const : ∃ c : ℝ, ∀ x ∈ s, f x = c) :
  MeasureTheory.integral (α.measure.restrict s) f =
    (Classical.choose h_const) * (α.measure s).toReal := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

/--
A Mathlib-style reformulation of Tao's Theorem 11.8.8(e): monotonicity of the
piecewise-constant Riemann–Stieltjes integral.

Here we work with a fixed finite partition of a closed interval
`[a 0, a (Fin.last n)]`, given by a strictly increasing sequence of points
`a : Fin (n.succ) → ℝ`. On each subinterval
`[a (Fin.castSucc i), a (Fin.succ i)]`, the functions `f` and `g` are assumed
to be constant. The “Riemann–Stieltjes integral” along this partition is the
finite sum
`∑ i, f (a (Fin.castSucc i)) * (α (a (Fin.succ i)) - α (a (Fin.castSucc i)))`,
and similarly for `g`.

Under these hypotheses, if `α` is monotone and `f ≤ g` on the whole interval,
then the corresponding sums satisfy the same inequality.
-/
theorem PiecewiseConstantOn.RS_integ_mono
  {n : ℕ}
  {a : Fin (n.succ) → ℝ}
  {α f g : ℝ → ℝ}
  (hα : Monotone α)
  (ha_mono : StrictMono a)
  (hf :
    ∀ i : Fin n,
      ∀ x ∈ Set.Icc (a (Fin.castSucc i)) (a (Fin.succ i)),
      ∀ y ∈ Set.Icc (a (Fin.castSucc i)) (a (Fin.succ i)),
        f x = f y)
  (hg :
    ∀ i : Fin n,
      ∀ x ∈ Set.Icc (a (Fin.castSucc i)) (a (Fin.succ i)),
      ∀ y ∈ Set.Icc (a (Fin.castSucc i)) (a (Fin.succ i)),
        g x = g y)
  (h :
    ∀ x ∈ Set.Icc (a 0) (a (Fin.last n)), f x ≤ g x) :
  (∑ i : Fin n,
      f (a (Fin.castSucc i)) *
        (α (a (Fin.succ i)) - α (a (Fin.castSucc i))))
    ≤
  (∑ i : Fin n,
      g (a (Fin.castSucc i)) *
        (α (a (Fin.succ i)) - α (a (Fin.castSucc i)))) := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench
theorem PiecewiseConstantOn.RS_integ_of_comp
  {a b : ℝ} (hab : a < b) {phi f : ℝ → ℝ}
  (hphi_cont : Continuous phi) (hphi_mono : Monotone phi)
  (hf : ∃ n : ℕ,
    ∃ c : Fin n → ℝ,
    ∃ pts : Fin (n+1) → ℝ,
      StrictMono pts ∧
      pts 0 = phi a ∧
      pts (Fin.last n) = phi b ∧
      ∀ i : Fin n, ∀ x ∈ Set.Icc (pts (Fin.castSucc i)) (pts (Fin.succ i)),
        f x = c i) :
  (∃ n : ℕ,
    ∃ c : Fin n → ℝ,
    ∃ pts : Fin (n+1) → ℝ,
      StrictMono pts ∧
      pts 0 = a ∧
      pts (Fin.last n) = b ∧
      ∀ i : Fin n, ∀ x ∈ Set.Icc (pts (Fin.castSucc i)) (pts (Fin.succ i)),
        (f (phi x)) = c i) ∧
  (∫ x in a..b, f (phi x) ∂((  
      { toFun := phi,
        mono' := hphi_mono,
        right_continuous' := by
          intro x
          have hx : ContinuousAt phi x := hphi_cont.continuousAt
          simpa using hx.continuousWithinAt
      } : StieltjesFunction).measure) =
    ∫ y in (phi a)..(phi b), f y) := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench
theorem PiecewiseConstantOn.RS_integ_of_extend
  {μ : MeasureTheory.Measure ℝ} {I J : Set ℝ} (hIJ : I ⊆ J)
  (hI : MeasurableSet I) (hJ : MeasurableSet J)
  {f : ℝ → ℝ} (hf : MeasureTheory.IntegrableOn f I μ) :
  MeasureTheory.integral (μ.restrict J) (Set.indicator I f) =
    MeasureTheory.integral (μ.restrict I) f := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench
theorem PiecewiseConstantOn.RS_integ_of_join
  {α : StieltjesFunction}
  {I J K : Set ℝ}
  (hI : MeasurableSet I) (hJ : MeasurableSet J)
  (hdisj : Disjoint I J) (hK : K = I ∪ J)
  (f : MeasureTheory.SimpleFunc ℝ ℝ) :
  ∫ x in K, f x ∂α.measure
    = ∫ x in I, f x ∂α.measure + ∫ x in J, f x ∂α.measure := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter
open MeasureTheory

namespace TaoBench

/--
Tao 11.8.8(d), in Mathlib terms:

For a (Lebesgue–)Stieltjes measure on `ℝ` coming from a monotone function `α`,
if a real-valued function `f` is integrable and pointwise nonnegative on a
closed bounded interval `Icc a b`, then its integral over that interval is
nonnegative.
-/
theorem PiecewiseConstantOn.RS_integ_of_nonneg
  {α : StieltjesFunction} {f : ℝ → ℝ} {a b : ℝ}
  (hα : Monotone (α : ℝ → ℝ))
  (h_nonneg : ∀ x ∈ Icc a b, 0 ≤ f x)
  (hf : IntegrableOn f (Icc a b) α.measure) :
  0 ≤ ∫ x in Icc a b, f x ∂α.measure := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

open MeasureTheory
theorem PiecewiseConstantOn.RS_integ_smul
    {f : ℝ → ℝ} {a b : ℝ} (c : ℝ)
    (α : StieltjesFunction)
    (hf : IntervalIntegrable f α.measure a b) :
    ∫ x in a..b, (c • f) x ∂α.measure
      = c • ∫ x in a..b, f x ∂α.measure := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench
theorem PiecewiseConstantOn.RS_integ_sub
  {α : StieltjesFunction} {s : Set ℝ}
  {f g : ℝ → ℝ}
  -- `s` is a bounded interval of one of the four usual types
  (hs : ∃ a b, s = Set.Icc a b ∨ s = Set.Ioc a b ∨ s = Set.Ico a b ∨ s = Set.Ioo a b)
  -- `f` and `g` are piecewise constant on `s` in the sense of having finite range there
  (hf_pw : (f '' s).Finite)
  (hg_pw : (g '' s).Finite)
  -- integrability of `f` and `g` on `s` for the Stieltjes measure associated to `α`
  (hf : MeasureTheory.IntegrableOn f s α.measure)
  (hg : MeasureTheory.IntegrableOn g s α.measure) :
  (∫ x in s, (f x - g x) ∂α.measure)
    = (∫ x in s, f x ∂α.measure)
      - (∫ x in s, g x ∂α.measure) := by
end TaoBench
import Mathlib.Tactic
import Mathlib.Data.Real.Basic
import Mathlib.Order.Monotone

/-!
Self-contained slice for Chapter 11, Section 11.8 target theorem
-/

namespace Chapter11

/-- Bounded intervals (from Section 11.1). -/
inductive BoundedInterval where
  | Ioo (a b:ℝ) : BoundedInterval
  | Icc (a b:ℝ) : BoundedInterval
  | Ioc (a b:ℝ) : BoundedInterval
  | Ico (a b:ℝ) : BoundedInterval

open BoundedInterval

/-- Coercion to set (from Section 11.1). -/
@[coe]
def BoundedInterval.toSet (I: BoundedInterval) : Set ℝ := match I with
  | Ioo a b => .Ioo a b
  | Icc a b => .Icc a b
  | Ioc a b => .Ioc a b
  | Ico a b => .Ico a b

instance BoundedInterval.inst_coeSet : Coe BoundedInterval (Set ℝ) where
  coe := toSet

/-- Membership of a real in a bounded interval (from Section 11.1). -/
instance BoundedInterval.instMembership : Membership ℝ BoundedInterval where
  mem I x := x ∈ (I:Set ℝ)

/-- Subset relation on bounded intervals (from Section 11.1). -/
instance BoundedInterval.instSubset : HasSubset BoundedInterval where
  Subset I J := ∀ x, x ∈ I → x ∈ J

@[ext]
structure Partition (I: BoundedInterval) where
  intervals : Finset BoundedInterval
  exists_unique (x:ℝ) (hx : x ∈ I) : ∃! J, J ∈ intervals ∧ x ∈ J
  contains (J : BoundedInterval) (hJ : J ∈ intervals) : J ⊆ I

instance Partition.instMembership (I: BoundedInterval) :
    Membership BoundedInterval (Partition I) where
  mem P J := J ∈ P.intervals

/-- Constant functions (from Section 11.2). -/
abbrev Constant {X Y:Type} (f: X → Y) : Prop := ∃ c, ∀ x, f x = c

open Classical in
noncomputable abbrev constant_value {X Y:Type} [hY: Nonempty Y] (f:X → Y) : Y :=
  if h: Constant f then h.choose else hY.some

/-- Constant on a set (from Section 11.2). -/
abbrev ConstantOn (f: ℝ → ℝ) (X: Set ℝ) : Prop := Constant (fun x : X ↦ f ↑x)

/-- Piecewise-constant with respect to a given partition (from Section 11.2). -/
abbrev PiecewiseConstantWith (f:ℝ → ℝ) {I: BoundedInterval} (P: Partition I) : Prop :=
  ∀ J ∈ P, ConstantOn f (J:Set ℝ)

/-- Piecewise-constant on an interval (from Section 11.2). -/
abbrev PiecewiseConstantOn (f:ℝ → ℝ) (I: BoundedInterval) : Prop :=
  ∃ P : Partition I, PiecewiseConstantWith f P

open Classical in
/-- Theorem 11.8.8 (g) (Laws of RS integration) / Exercise 11.8.8 -/
theorem PiecewiseConstantOn.RS_of_extend {I J: BoundedInterval} (hIJ: I ⊆ J)
  {f: ℝ → ℝ} (h: PiecewiseConstantOn f I) {α:ℝ → ℝ} (hα: Monotone α):
  PiecewiseConstantOn (fun x ↦ if x ∈ I then f x else 0) J := by
end Chapter11
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

/-- Mathlib translation of `Chapter11.PiecewiseConstantOn.RS_of_extend`.

Here `I` and `J` are (bounded) real intervals, expressed using the
standard interval sets `Set.Ioo`, `Set.Icc`, `Set.Ioc`, `Set.Ico`.

`h` says that `f` is piecewise constant on `I` with respect to some
finite partition of `I` into bounded intervals (again of the four
standard types), which are pairwise disjoint and whose union is `I`.

The conclusion says that there is a similar finite interval partition of
`J` on which the extended function `I.indicator f` is piecewise
constant. -/
theorem PiecewiseConstantOn.RS_of_extend
  {I J : Set ℝ}
  (hI : ∃ a b : ℝ,
    I = Set.Ioo a b ∨ I = Set.Icc a b ∨ I = Set.Ioc a b ∨ I = Set.Ico a b)
  (hJ : ∃ a b : ℝ,
    J = Set.Ioo a b ∨ J = Set.Icc a b ∨ J = Set.Ioc a b ∨ J = Set.Ico a b)
  (hIJ : I ⊆ J)
  {f : ℝ → ℝ}
  (h : ∃ P : Finset (Set ℝ),
      (∀ K ∈ P,
        (∃ a b : ℝ,
          K = Set.Ioo a b ∨ K = Set.Icc a b ∨ K = Set.Ioc a b ∨ K = Set.Ico a b) ∧
        K ⊆ I) ∧
      (∀ x, x ∈ I → ∃! K, K ∈ P ∧ x ∈ K) ∧
      (∀ K ∈ P, ∃ c, ∀ x ∈ K, f x = c))
  {α : ℝ → ℝ} (hα : Monotone α) :
  ∃ Q : Finset (Set ℝ),
    (∀ K ∈ Q,
      (∃ a b : ℝ,
        K = Set.Ioo a b ∨ K = Set.Icc a b ∨ K = Set.Ioc a b ∨ K = Set.Ico a b) ∧
      K ⊆ J) ∧
    (∀ x, x ∈ J → ∃! K, K ∈ Q ∧ x ∈ K) ∧
    (∀ K ∈ Q, ∃ c, ∀ x ∈ K, I.indicator f x = c) := by
end TaoBench