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

namespace TaoBench
theorem PiecewiseConstantOn.min
  {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, ∃ c : ℝ, ∀ x ∈ J, f x = c) ∧
      (∀ J ∈ P, J ⊆ I) ∧
      (∀ x ∈ I, ∃! J, J ∈ P ∧ x ∈ J) ∧
      (∀ J ∈ P,
        ∃ a b : ℝ,
          J = Set.Ioo a b ∨ J = Set.Icc a b ∨ J = Set.Ioc a b ∨ J = Set.Ico a b))
  (hg :
    ∃ Q : Finset (Set ℝ),
      (∀ J ∈ Q, ∃ c : ℝ, ∀ x ∈ J, g x = c) ∧
      (∀ J ∈ Q, J ⊆ I) ∧
      (∀ x ∈ I, ∃! J, J ∈ Q ∧ x ∈ J) ∧
      (∀ J ∈ Q,
        ∃ a b : ℝ,
          J = Set.Ioo a b ∨ J = Set.Icc a b ∨ J = Set.Ioc a b ∨ J = Set.Ico a b)) :
  ∃ R : Finset (Set ℝ),
      (∀ J ∈ R, ∃ c : ℝ, ∀ x ∈ J, min (f x) (g x) = c) ∧
      (∀ J ∈ R, J ⊆ I) ∧
      (∀ x ∈ I, ∃! J, J ∈ R ∧ x ∈ J) ∧
      (∀ J ∈ R,
        ∃ a b : ℝ,
          J = Set.Ioo a b ∨ J = Set.Icc a b ∨ J = Set.Ioc a b ∨ J = Set.Ico a b) := by
end TaoBench
import Mathlib.Tactic
import Mathlib.Data.Real.Basic

/-!
Self-contained slice for Chapter 11, Sections 11.1–11.2, sufficient to typecheck
PiecewiseConstantOn.mul. We avoid importing any Analysis.Section_* modules.
-/

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.instEmpty : EmptyCollection BoundedInterval where
  emptyCollection := Ioo 0 0

@[simp]
theorem BoundedInterval.coe_empty : ((∅ : BoundedInterval):Set ℝ) = ∅ := by
  simp [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.mul {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.mul
  {f g : ℝ → ℝ} {I : Set ℝ}
  (hI : ∃ a b : ℝ,
    I = {x : ℝ | a < x ∧ x < b} ∨
        I = {x : ℝ | a ≤ x ∧ x ≤ b} ∨
        I = {x : ℝ | a < x ∧ x ≤ b} ∨
        I = {x : ℝ | a ≤ x ∧ x < b})
  (hf :
    ∃ P : List (Set ℝ),
      (∀ J ∈ P, J ⊆ I) ∧
      (∀ x ∈ I, ∃! J : Set ℝ, J ∈ P ∧ x ∈ J) ∧
      (∀ J ∈ P, ∃ a b : ℝ,
        J = {x : ℝ | a < x ∧ x < b} ∨
            J = {x : ℝ | a ≤ x ∧ x ≤ b} ∨
            J = {x : ℝ | a < x ∧ x ≤ b} ∨
            J = {x : ℝ | a ≤ x ∧ x < b}) ∧
      (∀ J ∈ P, ∃ c : ℝ, ∀ x ∈ J, f x = c))
  (hg :
    ∃ P : List (Set ℝ),
      (∀ J ∈ P, J ⊆ I) ∧
      (∀ x ∈ I, ∃! J : Set ℝ, J ∈ P ∧ x ∈ J) ∧
      (∀ J ∈ P, ∃ a b : ℝ,
        J = {x : ℝ | a < x ∧ x < b} ∨
            J = {x : ℝ | a ≤ x ∧ x ≤ b} ∨
            J = {x : ℝ | a < x ∧ x ≤ b} ∨
            J = {x : ℝ | a ≤ x ∧ x < b}) ∧
      (∀ J ∈ P, ∃ c : ℝ, ∀ x ∈ J, g x = c)) :
  ∃ P : List (Set ℝ),
    (∀ J ∈ P, J ⊆ I) ∧
    (∀ x ∈ I, ∃! J : Set ℝ, J ∈ P ∧ x ∈ J) ∧
    (∀ J ∈ P, ∃ a b : ℝ,
      J = {x : ℝ | a < x ∧ x < b} ∨
          J = {x : ℝ | a ≤ x ∧ x ≤ b} ∨
          J = {x : ℝ | a < x ∧ x ≤ b} ∨
          J = {x : ℝ | a ≤ x ∧ x < b}) ∧
    (∀ J ∈ P, ∃ c : ℝ, ∀ x ∈ J, (f * g) x = c) := by
end TaoBench
import Mathlib.Data.Real.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

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

open Classical in
/-- Theorem 11.2.16 (g) (Laws of integration) / Exercise 11.2.4 -/
theorem PiecewiseConstantOn.of_extend {I J: BoundedInterval} (hIJ: I ⊆ J)
  {f: ℝ → ℝ} (h: PiecewiseConstantOn f I) :
  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

noncomputable section
open Classical
theorem PiecewiseConstantOn.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 :
    ∃ intervals : Finset (Set ℝ),
      (∀ x ∈ I, ∃! K : Set ℝ, K ∈ intervals ∧ x ∈ K) ∧
      (∀ K ∈ intervals, K ⊆ I) ∧
      (∀ K ∈ intervals,
        (∃ a b : ℝ, K = Set.Ioo a b ∨ K = Set.Icc a b ∨ K = Set.Ioc a b ∨ K = Set.Ico a b)) ∧
      (∀ K ∈ intervals, ∃ c : ℝ, ∀ x ∈ K, f x = c)) :
  ∃ intervals : Finset (Set ℝ),
    (∀ x ∈ J, ∃! K : Set ℝ, K ∈ intervals ∧ x ∈ K) ∧
    (∀ K ∈ intervals, K ⊆ J) ∧
    (∀ K ∈ intervals,
      (∃ a b : ℝ, K = Set.Ioo a b ∨ K = Set.Icc a b ∨ K = Set.Ioc a b ∨ K = Set.Ico a b)) ∧
    (∀ K ∈ intervals, ∃ c : ℝ, ∀ x ∈ K, (if x ∈ I then f x else 0) = c) := by
end

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

/-!
Self-contained slice for Chapter 11 Sections 11.1–11.2 minimal dependencies
for the target theorem `PiecewiseConstantOn.of_join`.
-/

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

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)

abbrev BoundedInterval.joins (K I J: BoundedInterval) : Prop := (I:Set ℝ) ∩ (J:Set ℝ) = ∅
  ∧ (K:Set ℝ) = (I:Set ℝ) ∪ (J:Set ℝ) ∧ |K|ₗ = |I|ₗ + |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

/-- Theorem 11.2.16 (h) (Laws of integration) / Exercise 11.2.4 -/
theorem PiecewiseConstantOn.of_join {I J K: BoundedInterval} (hIJK: K.joins I J)
  (f: ℝ → ℝ) : PiecewiseConstantOn f K ↔ PiecewiseConstantOn f I ∧ PiecewiseConstantOn f J := by
end Chapter11
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench
theorem PiecewiseConstantOn.of_join
  {I J K : Set ℝ}
  (hIJK : I ∩ J = (∅ : Set ℝ) ∧ K = I ∪ J)
  (f : ℝ → ℝ) :
  (∃ P : Finset (Set ℝ),
      (∀ L, L ∈ P → ∃ c : ℝ, ∀ x, x ∈ L → f x = c) ∧
      (∀ x, x ∈ K → ∃! L : Set ℝ, L ∈ P ∧ x ∈ L) ∧
      (∀ L, L ∈ P → L ⊆ K)) ↔
  ((∃ PI : Finset (Set ℝ),
      (∀ L, L ∈ PI → ∃ c : ℝ, ∀ x, x ∈ L → f x = c) ∧
      (∀ x, x ∈ I → ∃! L : Set ℝ, L ∈ PI ∧ x ∈ L) ∧
      (∀ L, L ∈ PI → L ⊆ I))
   ∧
   (∃ PJ : Finset (Set ℝ),
      (∀ L, L ∈ PJ → ∃ c : ℝ, ∀ x, x ∈ L → f x = c) ∧
      (∀ x, x ∈ J → ∃! L : Set ℝ, L ∈ PJ ∧ x ∈ L) ∧
      (∀ L, L ∈ PJ → L ⊆ J))) := 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

@[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.smul {f: ℝ → ℝ} {I: BoundedInterval}
  (c:ℝ) (hf: PiecewiseConstantOn f I) : PiecewiseConstantOn (c • f) I := by
end Chapter11
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench
theorem PiecewiseConstantOn.smul
  {f : ℝ → ℝ} {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)
  (c : ℝ)
  (hf :
    ∃ intervals : Finset (Set ℝ),
      (∀ x ∈ I, ∃! (J : Set ℝ), J ∈ intervals ∧ x ∈ J) ∧
      (∀ J ∈ intervals, J ⊆ I) ∧
      (∀ J ∈ intervals,
        (∃ a b : ℝ,
          J = Set.Ioo a b ∨ J = Set.Icc a b ∨ J = Set.Ioc a b ∨ J = Set.Ico a b) ∧
        (∃ k : ℝ, ∀ x ∈ J, f x = k))) :
  ∃ intervals : Finset (Set ℝ),
    (∀ x ∈ I, ∃! (J : Set ℝ), J ∈ intervals ∧ x ∈ J) ∧
    (∀ J ∈ intervals, J ⊆ I) ∧
    (∀ J ∈ intervals,
      (∃ a b : ℝ,
        J = Set.Ioo a b ∨ J = Set.Icc a b ∨ J = Set.Ioc a b ∨ J = Set.Ico a b) ∧
      (∃ k : ℝ, ∀ x ∈ J, (c • f) x = k)) := by
end TaoBench
import Mathlib.Data.Real.Basic
import Mathlib.Tactic

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

end Chapter11

namespace Chapter11
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 -/
theorem PiecewiseConstantOn.sub {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.sub
  {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
import Mathlib.Topology.Instances.Real

namespace Chapter11

-- Bounded intervals as a datatype
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
  | BoundedInterval.Ioo a b => Set.Ioo a b
  | BoundedInterval.Icc a b => Set.Icc a b
  | BoundedInterval.Ioc a b => Set.Ioc a b
  | BoundedInterval.Ico a b => Set.Ico a b

-- Provide a coercion from BoundedInterval to Set ℝ
instance : CoeTC BoundedInterval (Set ℝ) := ⟨BoundedInterval.toSet⟩

namespace BoundedInterval

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

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

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

end BoundedInterval

notation3:max "|" I "|ₗ" => BoundedInterval.length I

-- Partitions of a bounded interval
@[ext] structure Partition (I : BoundedInterval) where
  intervals : Finset BoundedInterval
  exists_unique (x : ℝ) (hx : x ∈ (I : Set ℝ)) : ∃! J, J ∈ intervals ∧ x ∈ (J : Set ℝ)
  contains (J : BoundedInterval) (hJ : J ∈ intervals) : (J : Set ℝ) ⊆ (I : Set ℝ)

-- Constant functions and their chosen value
abbrev Constant {X Y : Type} (f : X → Y) : Prop :=
  ∃ c, ∀ x, f x = c

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

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

-- One-sided limits and α-length
noncomputable abbrev right_lim (f : ℝ → ℝ) (x₀ : ℝ) : ℝ :=
  lim ((nhdsWithin x₀ (.Ioi x₀)).map f)

noncomputable abbrev left_lim (f : ℝ → ℝ) (x₀ : ℝ) : ℝ :=
  lim ((nhdsWithin x₀ (.Iio x₀)).map f)

noncomputable abbrev α_length (α : ℝ → ℝ) (I : BoundedInterval) : ℝ :=
  match I with
  | BoundedInterval.Icc a b => if a ≤ b then (right_lim α b) - (left_lim α a) else 0
  | BoundedInterval.Ico a b => if a ≤ b then (left_lim α b) - (left_lim α a) else 0
  | BoundedInterval.Ioc a b => if a ≤ b then (right_lim α b) - (right_lim α a) else 0
  | BoundedInterval.Ioo a b => if a < b then (left_lim α b) - (right_lim α a) else 0

notation3:max α "[" I "]ₗ" => α_length α I

-- Piecewise-constant integrals on a partition
noncomputable abbrev PiecewiseConstantWith.integ (f : ℝ → ℝ) {I : BoundedInterval}
    (P : Partition I) : ℝ :=
  ∑ J ∈ P.intervals, constant_value_on f (J : Set ℝ) * |J|ₗ

noncomputable abbrev PiecewiseConstantWith.RS_integ (f : ℝ → ℝ) {I : BoundedInterval}
    (P : Partition I) (α : ℝ → ℝ) : ℝ :=
  ∑ J ∈ P.intervals, constant_value_on f (J : Set ℝ) * α[J]ₗ

open PiecewiseConstantWith
theorem PiecewiseConstantWith.RS_integ_eq_integ {f : ℝ → ℝ} {I : BoundedInterval}
    (P : Partition I) : RS_integ f P (fun x ↦ x) = integ f P := by
end Chapter11
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench
theorem PiecewiseConstantWith.RS_integ_eq_integ
  {ι : Type*} (s : Finset ι) (a b v : ι → ℝ) :
    Finset.sum (s := s) (fun i =>
      v i * (if a i ≤ b i
        then ((fun x : ℝ ↦ x) (b i) - (fun x : ℝ ↦ x) (a i))
        else 0)) =
    Finset.sum (s := s) (fun i =>
      v i * (if a i ≤ b i then b i - a i else 0)) := by
end TaoBench
import Mathlib.Data.Real.Basic
import Mathlib.Algebra.BigOperators

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

namespace BoundedInterval

@[coe]
def 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 inst_coeSet : Coe BoundedInterval (Set ℝ) where
  coe := toSet

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

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

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

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

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

abbrev 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)

end BoundedInterval

open BoundedInterval

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

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|ₗ

/-- Proposition 11.2.13 (Piecewise constant integral is independent of partition) / Exercise 11.2.3 -/
theorem PiecewiseConstantWith.integ_eq {f:ℝ → ℝ} {I: BoundedInterval} {P P': Partition I}
  (hP: PiecewiseConstantWith f P) (hP': PiecewiseConstantWith f P') : integ f P = integ f P' := by
end Chapter11