introvoyz041/TaoAnalysisBench huggingface dataset:
import Mathlib.Tactic
import Mathlib.Data.Real.Basic
import Mathlib.Data.Set.Basic
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.instEmpty : EmptyCollection BoundedInterval where
  emptyCollection := Ioo 0 0

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

open Classical in
noncomputable instance BoundedInterval.decidableEq : DecidableEq BoundedInterval := instDecidableEqOfLawfulBEq

@[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

/-- Definition 11.2.1 -/
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

theorem Constant.eq {X Y:Type} {f: X → Y} [Nonempty Y] (h: Constant f) (x:X) :
  f x = constant_value f := by simp [constant_value, h]; apply h.choose_spec

-- from Section_11_2
 theorem Constant.of_const {X Y:Type} {f:X → Y} {c:Y} (h: ∀ x, f x = c) :
  Constant f := by use c

abbrev ConstantOn (f: ℝ → ℝ) (X: Set ℝ) : Prop := Constant (fun x : X ↦ f ↑x)

noncomputable abbrev constant_value_on (f:ℝ → ℝ) (X: Set ℝ) : ℝ := constant_value (fun x : X ↦ f ↑x)

/-- Definition 11.2.3 (Piecewise constant functions I) -/
abbrev PiecewiseConstantWith (f:ℝ → ℝ) {I: BoundedInterval} (P: Partition I) : Prop := ∀ J ∈ P, ConstantOn f (J:Set ℝ)

/-- Definition 11.2.5 (Piecewise constant functions I) -/
abbrev PiecewiseConstantOn (f:ℝ → ℝ) (I: BoundedInterval) : Prop := ∃ P : Partition I, PiecewiseConstantWith f P

/-- Lemma 11.2.8 / Exercise 11.2.2 -/
 theorem PiecewiseConstantOn.add {f g: ℝ → ℝ} {I: BoundedInterval}
  (hf: PiecewiseConstantOn f I) (hg: PiecewiseConstantOn g I) : PiecewiseConstantOn (f + g) I := by
end Chapter11
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench
 theorem PiecewiseConstantOn.add
  {f g : ℝ → ℝ} {I : Set ℝ}
  (hI : ∃ a b : ℝ,
      I = Set.Ioo a b ∨ I = Set.Icc a b ∨ I = Set.Ioc a b ∨ I = Set.Ico a b)
  (hf :
    ∃ P : Finset (Set ℝ),
      (∀ J ∈ P, ∃ a b : ℝ,
        J = Set.Ioo a b ∨ J = Set.Icc a b ∨ J = Set.Ioc a b ∨ J = Set.Ico a b) ∧
      (∀ x, x ∈ I → ∃! J : Set ℝ, J ∈ P ∧ x ∈ J) ∧
      (∀ J ∈ P, J ⊆ I) ∧
      (∀ J ∈ P, ∃ c : ℝ, ∀ x ∈ J, f x = c))
  (hg :
    ∃ P : Finset (Set ℝ),
      (∀ J ∈ P, ∃ a b : ℝ,
        J = Set.Ioo a b ∨ J = Set.Icc a b ∨ J = Set.Ioc a b ∨ J = Set.Ico a b) ∧
      (∀ x, x ∈ I → ∃! J : Set ℝ, J ∈ P ∧ x ∈ J) ∧
      (∀ J ∈ P, J ⊆ I) ∧
      (∀ J ∈ P, ∃ c : ℝ, ∀ x ∈ J, g x = c)) :
  ∃ P : Finset (Set ℝ),
    (∀ J ∈ P, ∃ a b : ℝ,
      J = Set.Ioo a b ∨ J = Set.Icc a b ∨ J = Set.Ioc a b ∨ J = Set.Ico a b) ∧
    (∀ x, x ∈ I → ∃! J : Set ℝ, J ∈ P ∧ x ∈ J) ∧
    (∀ J ∈ P, J ⊆ I) ∧
    (∀ J ∈ P, ∃ c : ℝ, ∀ x ∈ J, f x + g x = c) := by
end TaoBench
import Mathlib.Tactic

/-!
This file extracts the minimal context needed for the target theorem
`Chapter11.PiecewiseConstantOn.div` from Sections 11.1 and 11.2.
It avoids importing any `Analysis.Section_*` modules.
-/

namespace Chapter11

-- Section 11.1: Bounded intervals and partitions (minimal slice)

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 subset of `ℝ`. -/
@[coe]
def BoundedInterval.toSet (I: BoundedInterval) : Set ℝ := match I with
  | Ioo a b => Set.Ioo a b
  | Icc a b => Set.Icc a b
  | Ioc a b => Set.Ioc a b
  | Ico a b => Set.Ico a b

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

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

/-- Subset relation on bounded intervals, via their underlying sets. -/
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


-- Section 11.2: Piecewise constant functions (minimal slice)

open BoundedInterval

/-- Definition 11.2.1 -/
abbrev Constant {X Y:Type} (f: X → Y) : Prop := ∃ c, ∀ x, f x = c

abbrev ConstantOn (f: ℝ → ℝ) (X: Set ℝ) : Prop := Constant (fun x : X ↦ f ↑x)

/-- Definition 11.2.3 (Piecewise constant functions I) -/
abbrev PiecewiseConstantWith (f:ℝ → ℝ) {I: BoundedInterval} (P: Partition I) : Prop :=
  ∀ J ∈ P, ConstantOn f (J:Set ℝ)

/-- Definition 11.2.5 (Piecewise constant functions I) -/
abbrev PiecewiseConstantOn (f:ℝ → ℝ) (I: BoundedInterval) : Prop :=
  ∃ P : Partition I, PiecewiseConstantWith f P

/-- Lemma 11.2.8 / Exercise 11.2.2.  I believe the hypothesis that `g` does not vanish is not needed. -/
theorem PiecewiseConstantOn.div {f g: ℝ → ℝ} {I: BoundedInterval}
  (hf: PiecewiseConstantOn f I) (hg: PiecewiseConstantOn f I) : PiecewiseConstantOn (f / g) I := by
end Chapter11
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench
theorem PiecewiseConstantOn.div
  {f g : ℝ → ℝ} {I : Set ℝ}
  (hf :
    ∃ P : Finset (Set ℝ),
      (∀ x, x ∈ I → ∃! (J : Set ℝ), J ∈ P ∧ x ∈ J) ∧
      (∀ J, J ∈ P → J ⊆ I) ∧
      (∀ J, J ∈ P → ∃ c, ∀ x ∈ J, f x = c))
  (hg :
    ∃ P : Finset (Set ℝ),
      (∀ x, x ∈ I → ∃! (J : Set ℝ), J ∈ P ∧ x ∈ J) ∧
      (∀ J, J ∈ P → J ⊆ I) ∧
      (∀ J, J ∈ P → ∃ c, ∀ x ∈ J, f x = c))
  :
    ∃ P : Finset (Set ℝ),
      (∀ x, x ∈ I → ∃! (J : Set ℝ), J ∈ P ∧ x ∈ J) ∧
      (∀ J, J ∈ P → J ⊆ I) ∧
      (∀ J, J ∈ P → ∃ c, ∀ x ∈ J, (f / g) x = c) := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

/--
Tao 11.2.16(a) / Exercise 11.2.4, translated to Mathlib primitives.

`hf` and `hg` say that `f` and `g` are piecewise constant on the bounded interval
`[a, b]` in the sense that there is a finite partition of `[a, b]` into subsets
(each contained in `[a, b]` and covering it, with each point belonging to a
unique part of the partition) on which the function is constant.

The conclusion uses Mathlib's interval integral on `[a, b]`.
-/
theorem PiecewiseConstantOn.integ_add
  {f g : ℝ → ℝ} {a b : ℝ}
  (hf :
    ∃ P : Finset (Set ℝ),
      (∀ x ∈ Set.Icc a b, ∃! s, s ∈ P ∧ x ∈ s) ∧
      (∀ s ∈ P, s ⊆ Set.Icc a b) ∧
      (∀ s ∈ P, ∃ c : ℝ, ∀ x ∈ s, f x = c))
  (hg :
    ∃ Q : Finset (Set ℝ),
      (∀ x ∈ Set.Icc a b, ∃! s, s ∈ Q ∧ x ∈ s) ∧
      (∀ s ∈ Q, s ⊆ Set.Icc a b) ∧
      (∀ s ∈ Q, ∃ c : ℝ, ∀ x ∈ s, g x = c)) :
  ∫ 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.Tactic
import Mathlib.Data.Real.Basic
import Mathlib.Algebra.BigOperators

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

abbrev BoundedInterval.length (I: BoundedInterval) : ℝ := max (I.b - I.a) 0

/-- Using ||ₗ subscript here to not override || -/
macro:max atomic("|" noWs) a:term noWs "|ₗ" : term => `(BoundedInterval.length $a)

@[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

/-- Definition 11.2.1 -/
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

abbrev ConstantOn (f: ℝ → ℝ) (X: Set ℝ) : Prop := Constant (fun x : X ↦ f ↑x)

noncomputable abbrev constant_value_on (f:ℝ → ℝ) (X: Set ℝ) : ℝ := constant_value (fun x : X ↦ f ↑x)

/-- Definition 11.2.3 (Piecewise constant functions I) -/
abbrev PiecewiseConstantWith (f:ℝ → ℝ) {I: BoundedInterval} (P: Partition I) : Prop := ∀ J ∈ P, ConstantOn f (J:Set ℝ)

/-- Definition 11.2.5 (Piecewise constant functions I) -/
abbrev PiecewiseConstantOn (f:ℝ → ℝ) (I: BoundedInterval) : Prop := ∃ P : Partition I, PiecewiseConstantWith f P

open scoped BigOperators

/-- Definition 11.2.9 (Piecewise constant integral I)-/
noncomputable abbrev PiecewiseConstantWith.integ (f:ℝ → ℝ) {I: BoundedInterval} (P: Partition I)  :
  ℝ := ∑ J ∈ P.intervals, constant_value_on f (J:Set ℝ) * |J|ₗ

open Classical in
/-- Definition 11.2.14 (Piecewise constant integral II)  -/
noncomputable abbrev PiecewiseConstantOn.integ (f:ℝ → ℝ) (I: BoundedInterval) :
  ℝ := if h: PiecewiseConstantOn f I then PiecewiseConstantWith.integ f h.choose else 0

/-- Theorem 11.2.16 (f) (Laws of integration) / Exercise 11.2.4 -/
theorem PiecewiseConstantOn.integ_const (c: ℝ) (I: BoundedInterval) :
  integ (fun _ ↦ c) I = c * |I|ₗ := by
end Chapter11
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench
theorem PiecewiseConstantOn.integ_const (c a b : ℝ) :
  MeasureTheory.integral
    (MeasureTheory.volume.restrict (Set.Icc a b))
    (fun _ : ℝ => c)
    = c * max (b - a) 0 := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench
theorem PiecewiseConstantOn.integ_const'
  {f : ℝ → ℝ} {I : Set ℝ} {a b : ℝ}
  (hI : I = Set.Ioo a b ∨ I = Set.Ioc a b ∨ I = Set.Ico a b ∨ I = Set.Icc a b)
  (h : ∃ c : ℝ, ∀ x ∈ I, f x = c) :
  ∫ x in I, f x = (Classical.choose h) * max (b - a) 0 := by
end TaoBench
import Mathlib.Tactic
import Mathlib.Data.Real.Basic
import Mathlib.Algebra.BigOperators.Basic

open scoped BigOperators

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

abbrev BoundedInterval.length (I: BoundedInterval) : ℝ := max (I.b - I.a) 0

macro:max atomic("|" noWs) a:term noWs "|ₗ" : term => `(BoundedInterval.length $a)

@[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

/-- Definition 11.2.1 -/
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

abbrev ConstantOn (f: ℝ → ℝ) (X: Set ℝ) : Prop := Constant (fun x : X ↦ f ↑x)

noncomputable abbrev constant_value_on (f:ℝ → ℝ) (X: Set ℝ) : ℝ := constant_value (fun x : X ↦ f ↑x)

/-- Definition 11.2.3 (Piecewise constant functions I) -/
abbrev PiecewiseConstantWith (f:ℝ → ℝ) {I: BoundedInterval} (P: Partition I) : Prop := ∀ J ∈ P, ConstantOn f (J:Set ℝ)

/-- Definition 11.2.5 (Piecewise constant functions I) -/
abbrev PiecewiseConstantOn (f:ℝ → ℝ) (I: BoundedInterval) : Prop := ∃ P : Partition I, PiecewiseConstantWith f P

/-- Definition 11.2.9 (Piecewise constant integral I)-/
noncomputable abbrev PiecewiseConstantWith.integ (f:ℝ → ℝ) {I: BoundedInterval} (P: Partition I)  :
  ℝ := ∑ J ∈ P.intervals, constant_value_on f (J:Set ℝ) * |J|ₗ

open Classical in
/-- Definition 11.2.14 (Piecewise constant integral II)  -/
noncomputable abbrev PiecewiseConstantOn.integ (f:ℝ → ℝ) (I: BoundedInterval) :
  ℝ := if h: PiecewiseConstantOn f I then PiecewiseConstantWith.integ f h.choose else 0

/-- Theorem 11.2.16 (e) (Laws of integration) / Exercise 11.2.4 -/
theorem PiecewiseConstantOn.integ_mono {f g: ℝ → ℝ} {I: BoundedInterval} (h: ∀ x ∈ I, f x ≤ g x)
  (hf: PiecewiseConstantOn f I) (hg: PiecewiseConstantOn g I) :
  integ f I ≤ integ g I := by
end Chapter11
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench
theorem PiecewiseConstantOn.integ_mono
  {f g : ℝ → ℝ} {a b : ℝ}
  (hfg : ∀ x ∈ Icc a b, f x ≤ g x)
  (hf : MeasureTheory.IntegrableOn f (Icc a b))
  (hg : MeasureTheory.IntegrableOn g (Icc a b)) :
  (∫ x in Icc a b, f x) ≤ (∫ x in Icc 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

open MeasureTheory
theorem PiecewiseConstantOn.integ_of_extend
  {I J : Set ℝ} [DecidablePred (· ∈ I)] (hIJ : I ⊆ J)
  {f : ℝ → ℝ}
  (hf : IntegrableOn f I) :
  ∫ x in J, (if x ∈ I then f x else 0) = ∫ x in I, f x := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench
theorem PiecewiseConstantOn.integ_of_join
  {a b c : ℝ} (hab : a ≤ b) (hbc : b ≤ c)
  {f : ℝ → ℝ} (hf : MeasureTheory.IntegrableOn f (Set.Icc a c)) :
  ∫ x in a..c, f x = ∫ x in a..b, f x + ∫ x in 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

/--
Tao's theorem 11.2.16(d) for piecewise-constant functions, translated to Mathlib:

- The bounded interval `I : BoundedInterval` is represented by a closed interval
  `Icc a b : Set ℝ`.
- “`f` is piecewise constant on `I` with respect to a finite partition” is
  expressed by saying that the restricted map `fun x : Icc a b ↦ f x` has
  finite range.
- Tao's combinatorial integral of a piecewise-constant function over `I`
  is identified with the Lebesgue integral of `f` with respect to the
  restriction of `volume` to `Icc a b`.
- The conclusion is that this integral is nonnegative whenever `f ≥ 0` on `I`.
-/
theorem PiecewiseConstantOn.integ_of_nonneg
  {f : ℝ → ℝ} {a b : ℝ}
  (h : ∀ x ∈ Icc a b, 0 ≤ f x)
  (hf : (Set.range fun x : Icc a b ↦ f x).Finite) :
  0 ≤
    MeasureTheory.integral
      (MeasureTheory.Measure.restrict
        (MeasureTheory.volume : MeasureTheory.Measure ℝ)
        (Icc a b))
      f := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

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

namespace TaoBench
theorem PiecewiseConstantOn.integ_sub
  {f g : ℝ → ℝ} {I : Set ℝ}
  (hI : ∃ a b : ℝ, I = Ioo a b ∨ I = Icc a b ∨ I = Ioc a b ∨ I = Ico a b)
  (hf : MeasureTheory.IntegrableOn f I)
  (hg : MeasureTheory.IntegrableOn g I) :
  ∫ x in I, (f x - 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

@[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

open Classical in
/-- This is to make Finsets of BoundedIntervals work properly -/
noncomputable instance BoundedInterval.decidableEq : DecidableEq BoundedInterval := instDecidableEqOfLawfulBEq

@[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

/-- Definition 11.2.1 -/
abbrev Constant {X Y:Type} (f: X → Y) : Prop := ∃ c, ∀ x, f x = c

abbrev ConstantOn (f: ℝ → ℝ) (X: Set ℝ) : Prop := Constant (fun x : X ↦ f ↑x)

/-- Definition 11.2.3 (Piecewise constant functions I) -/
abbrev PiecewiseConstantWith (f:ℝ → ℝ) {I: BoundedInterval} (P: Partition I) : Prop := ∀ J ∈ P, ConstantOn f (J:Set ℝ)

/-- Definition 11.2.5 (Piecewise constant functions I) -/
abbrev PiecewiseConstantOn (f:ℝ → ℝ) (I: BoundedInterval) : Prop := ∃ P : Partition I, PiecewiseConstantWith f P

/-- Lemma 11.2.8 / Exercise 11.2.2 -/
theorem PiecewiseConstantOn.max {f g: ℝ → ℝ} {I: BoundedInterval}
  (hf: PiecewiseConstantOn f I) (hg: PiecewiseConstantOn g I) : PiecewiseConstantOn (max f g) I := by
end Chapter11
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

/--
Lemma 11.2.8 / Exercise 11.2.2 (piecewise constant functions).

Here `I : Set ℝ` is assumed to be a bounded interval of one of the
four standard types `Ioo`, `Icc`, `Ioc`, `Ico`. A function `f : ℝ → ℝ`
is *piecewise constant on* `I` if there exists a finite family `P` of
bounded intervals which

* are all contained in `I`,
* partition `I` (each `x ∈ I` lies in exactly one member of `P`), and
* on each member of `P` the function `f` is constant.

The statement is that if `f` and `g` are piecewise constant on `I`
in this sense, then so is `x ↦ max (f x) (g x)`.
-/
theorem PiecewiseConstantOn.max
  {f g : ℝ → ℝ} {I : Set ℝ}
  (hI :
    ∃ a b : ℝ,
      I = Set.Ioo a b ∨ I = Set.Icc a b ∨ I = Set.Ioc a b ∨ I = Set.Ico a b)
  (hf :
    ∃ P : Finset (Set ℝ),
      (∀ J ∈ P,
        ∃ a b : ℝ,
          J = Set.Ioo a b ∨ J = Set.Icc a b ∨ J = Set.Ioc a b ∨ J = Set.Ico a b) ∧
      (∀ x ∈ I, ∃! J : Set ℝ, J ∈ P ∧ x ∈ J) ∧
      (∀ J ∈ P, J ⊆ I) ∧
      (∀ J ∈ P, ∃ c : ℝ, ∀ x ∈ J, f x = c))
  (hg :
    ∃ P : Finset (Set ℝ),
      (∀ J ∈ P,
        ∃ a b : ℝ,
          J = Set.Ioo a b ∨ J = Set.Icc a b ∨ J = Set.Ioc a b ∨ J = Set.Ico a b) ∧
      (∀ x ∈ I, ∃! J : Set ℝ, J ∈ P ∧ x ∈ J) ∧
      (∀ J ∈ P, J ⊆ I) ∧
      (∀ J ∈ P, ∃ c : ℝ, ∀ x ∈ J, g x = c)) :
  ∃ P : Finset (Set ℝ),
    (∀ J ∈ P,
      ∃ a b : ℝ,
        J = Set.Ioo a b ∨ J = Set.Icc a b ∨ J = Set.Ioc a b ∨ J = Set.Ico a b) ∧
    (∀ x ∈ I, ∃! J : Set ℝ, J ∈ P ∧ x ∈ J) ∧
    (∀ J ∈ P, J ⊆ I) ∧
    (∀ J ∈ P, ∃ c : ℝ, ∀ x ∈ J, max (f x) (g x) = c) :=
by
end TaoBench
import Mathlib.Tactic
import Mathlib.Data.Real.Basic
import Mathlib.Data.Set.Intervals.Basic
import Mathlib.Data.Finset.Basic

/-!
Self-contained slice for Chapter 11 Sections 11.1–11.2, sufficient for
`PiecewiseConstantOn.min`.
-/

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

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

@[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

/-- Definition 11.2.1 -/
abbrev Constant {X Y:Type} (f: X → Y) : Prop := ∃ c, ∀ x, f x = c

abbrev ConstantOn (f: ℝ → ℝ) (X: Set ℝ) : Prop := Constant (fun x : X ↦ f ↑x)

/-- Definition 11.2.3 (Piecewise constant functions I) -/
abbrev PiecewiseConstantWith (f:ℝ → ℝ) {I: BoundedInterval} (P: Partition I) : Prop := ∀ J ∈ P, ConstantOn f (J:Set ℝ)

/-- Definition 11.2.5 (Piecewise constant functions I) -/
abbrev PiecewiseConstantOn (f:ℝ → ℝ) (I: BoundedInterval) : Prop := ∃ P : Partition I, PiecewiseConstantWith f P

/-- Lemma 11.2.8 / Exercise 11.2.2 -/
theorem PiecewiseConstantOn.min {f g: ℝ → ℝ} {I: BoundedInterval}
  (hf: PiecewiseConstantOn f I) (hg: PiecewiseConstantOn g I) : PiecewiseConstantOn (min f g) I := by
end Chapter11