introvoyz041/TaoAnalysisBench huggingface dataset:
import Mathlib
import Analysis.Section_8_1
import Analysis.Tags.Section_8_1
import Analysis.Section_8_2
import Analysis.Tags.Section_8_2

namespace Chapter8

-- <CONTEXT>
abbrev CartesianProduct {I U: Type} (X : I → Set U) := { x : I → ⋃ α, X α // ∀ α, ↑(x α) ∈ X α }

def CartesianProduct.equiv {I U: Type} (X : I → Set U) :
  CartesianProduct X ≃ ∀ α, X α := {
  toFun x α := ⟨ x.val α, by aesop ⟩
  invFun x := ⟨ fun α ↦ ⟨ x α, by simp; use α; aesop ⟩, by aesop ⟩
  left_inv x := by aesop
  right_inv x := by aesop
  }

def Function.equiv {I X:Type} : (∀ _:I, X) ≃ (I → X) := {
  toFun f := f
  invFun f := f
  left_inv f := rfl
  right_inv f := rfl
}

def product_zero_equiv {X: Fin 0 → Type} : (∀ i:Fin 0, X i) ≃ PUnit := {
  toFun f := PUnit.unit
  invFun x i := nomatch i
  left_inv f := by aesop
  right_inv f := rfl
}

def product_one_equiv {X: Fin 1 → Type} : (∀ i:Fin 1, X i) ≃ X 0 := {
  toFun f := f 0
  invFun x i := by rwa [←Fin.fin_one_eq_zero i] at x
  left_inv f := by ext i; rw [Fin.fin_one_eq_zero i]; simp
  right_inv f := rfl
}

def product_two_equiv {X: Fin 2 → Type} : (∀ i:Fin 2, X i) ≃ (X 0 × X 1) := {
  toFun f := (f 0, f 1)
  invFun f i := match i with
    | 0 => f.1
    | 1 => f.2
  left_inv f := by aesop
  right_inv f := rfl
}

def product_three_equiv {X: Fin 3 → Type} : (∀ i:Fin 3, X i) ≃ (X 0 × X 1 × X 2) := {
  toFun f := (f 0, f 1, f 2)
  invFun f i := match i with
    | 0 => f.1
    | 1 => f.2.1
    | 2 => f.2.2
  left_inv f := by aesop
  right_inv f := rfl
}

theorem axiom_of_choice {I: Type} {X: I → Type} (h : ∀ i, Nonempty (X i)) :
  Nonempty (∀ i, X i) := by
  sorry

theorem axiom_of_countable_choice {I: Type} {X: I → Type} [Countable I] (h : ∀ i, Nonempty (X i)) :
  Nonempty (∀ i, X i) := by
  sorry

theorem exist_tendsTo_sup {E: Set ℝ} (hnon: E.Nonempty) (hbound: BddAbove E) :
  ∃ a : ℕ → ℝ, (∀ n, a n ∈ E) ∧ Filter.atTop.Tendsto a (nhds (sSup E)) := by
  sorry

theorem exist_tendsTo_sup_of_closed {E: Set ℝ} (hnon: E.Nonempty) (hbound: BddAbove E) (hclosed: IsClosed E) :
  ∃ a : ℕ → ℝ, (∀ n, a n ∈ E) ∧ Filter.atTop.Tendsto a (nhds (sSup E)) := by
  sorry
-- </CONTEXT>
-- <TARGET_THEOREM>
theorem Function.Injective.inv_surjective {A B:Type} {g: B → A} (hg: Function.Surjective g) :
  ∃ f : A → B, Function.Injective f ∧ Function.RightInverse f g := by
-- </TARGET_THEOREM>

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

namespace TaoBench

namespace Chapter8

/-- Exercise 8.4.3 (Mathlib version):
If `g : B → A` is surjective, then there exists an injective right inverse to `g`. -/
theorem Function.Injective.inv_surjective
  {A B : Type} {g : B → A} (hg : Function.Surjective g) :
  ∃ f : A → B, Function.Injective f ∧ Function.RightInverse f g := by
end Chapter8

end TaoBench
import Mathlib
import Analysis.Section_8_4
import Analysis.Tags.Section_8_4

namespace Chapter8

-- <CONTEXT>

def PartialOrder.mk {X:Type} [LE X]
  (hrefl: ∀ x:X, x ≤ x)
  (hantisymm: ∀ x y:X, x ≤ y → y ≤ x → x = y)
  (htrans: ∀ x y z:X, x ≤ y → y ≤ z → x ≤ z) : PartialOrder X :=
{
  le := (· ≤ ·)
  le_refl := hrefl
  le_antisymm := hantisymm
  le_trans := htrans
}

def IsTotal (X:Type) [PartialOrder X] : Prop := ∀ x y:X, x ≤ y ∨ y ≤ x

open Classical in
noncomputable def LinearOrder.mk {X:Type} [PartialOrder X]
  (htotal: IsTotal X) : LinearOrder X :=
{
   le_total := htotal
   toDecidableLE := decRel LE.le
}

noncomputable def LinearOrder.subtype {X:Type} [LinearOrder X] (A: Set X) : LinearOrder A :=
LinearOrder.mk (by
  sorry
  )

theorem IsTotal.subtype {X:Type} [PartialOrder X] {A: Set X} (hA: IsTotal X) : IsTotal A := by
  sorry

theorem IsTotal.subset {X:Type} [PartialOrder X] {A B: Set X} (hA: IsTotal A) (hAB: B ⊆ A) : IsTotal B := by
  sorry

abbrev X_8_5_4 : Set (Set ℕ) := { {1,2}, {2}, {2,3}, {2,3,4}, {5} }

theorem IsMax.iff {X:Type} [PartialOrder X] (x:X) :
  IsMax x ↔ ¬ ∃ y, x < y := by
  sorry

theorem IsMin.iff {X:Type} [PartialOrder X] (x:X) :
  IsMin x ↔ ¬ ∃ y, x > y := by
  sorry

theorem WellFoundedLT.iff (X:Type) [LinearOrder X] :
  WellFoundedLT X ↔ ∀ A:Set X, A.Nonempty → ∃ x:A, IsMin x := by
  sorry

theorem WellFoundedLT.iff' {X:Type} [PartialOrder X] (h: IsTotal X) :
  WellFoundedLT X ↔ ∀ A:Set X, A.Nonempty → ∃ x:A, IsMin x := by
  sorry

-- </CONTEXT>
-- <TARGET_THEOREM>
theorem IsMax.ofFinite {X:Type} [LinearOrder X] [Finite X] [Nonempty X] : ∃ x:X, IsMax x := by
-- </TARGET_THEOREM>

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

namespace TaoBench

/-- In a nonempty finite linear order, there exists a maximal element. -/
theorem IsMax.ofFinite {X : Type*} [LinearOrder X] [Finite X] [Nonempty X] :
  ∃ x : X, ∀ y : X, x ≤ y → y ≤ x := by
end TaoBench
import Mathlib.Data.Set.Basic
import Mathlib.Data.Nat.Basic
import Mathlib.Tactic

namespace Chapter8

/-- Proposition 8.1.4 (Well ordering principle / Exercise 8.1.2 -/
theorem Nat.exists_unique_min {X : Set ℕ} (hX : X.Nonempty) :
  ∃! m ∈ X, ∀ n ∈ X, m ≤ n := by
end Chapter8
import Mathlib

namespace Chapter8

-- <CONTEXT>
abbrev EqualCard (X Y : Type) : Prop := ∃ f : X → Y, Function.Bijective f

theorem EqualCard.iff {X Y : Type} : EqualCard X Y ↔ Nonempty (X ≃ Y) := by
  sorry

theorem EqualCard.iff' {X Y : Type} : EqualCard X Y ↔ Cardinal.mk X = Cardinal.mk Y := by
  sorry

theorem EqualCard.refl (X : Type) : EqualCard X X := by
  sorry

theorem EqualCard.symm {X Y : Type} (hXY : EqualCard X Y) : EqualCard Y X := by
  sorry

theorem EqualCard.trans {X Y Z : Type} (hXY : EqualCard X Y) (hYZ : EqualCard Y Z) :
  EqualCard X Z := by
  sorry

instance EqualCard.instSetoid : Setoid Type := ⟨ EqualCard, ⟨ refl, symm, trans ⟩ ⟩

theorem EqualCard.univ (X : Type) : EqualCard (.univ : Set X) X := by
  sorry

abbrev CountablyInfinite (X : Type) : Prop := EqualCard X ℕ

abbrev AtMostCountable (X : Type) : Prop := CountablyInfinite X ∨ Finite X

theorem CountablyInfinite.equiv {X Y: Type} (hXY : EqualCard X Y) :
  CountablyInfinite X ↔ CountablyInfinite Y := by
  sorry

theorem Finite.equiv {X Y: Type} (hXY : EqualCard X Y) :
  Finite X ↔ Finite Y := by
  sorry

theorem AtMostCountable.equiv {X Y: Type} (hXY : EqualCard X Y) :
  AtMostCountable X ↔ AtMostCountable Y := by
  sorry

theorem CountablyInfinite.iff (X : Type) : CountablyInfinite X ↔ Nonempty (Denumerable X) := by
  sorry

theorem CountablyInfinite.iff' (X : Type) : CountablyInfinite X ↔ Countable X ∧ Infinite X := by
  sorry

theorem CountablyInfinite.toCountable {X : Type} (hX: CountablyInfinite X) : Countable X := by
  sorry

theorem CountablyInfinite.toInfinite {X : Type} (hX: CountablyInfinite X) : Infinite X := by
  sorry

theorem AtMostCountable.iff (X : Type) : AtMostCountable X ↔ Countable X := by
  sorry

theorem CountablyInfinite.iff_image_inj {A:Type} (X: Set A) : CountablyInfinite X ↔ ∃ f : ℕ ↪ A, X = f '' .univ := by
  sorry
-- </CONTEXT>
-- <TARGET_THEOREM>
theorem Nat.exists_unique_min {X : Set ℕ} (hX : X.Nonempty) :
  ∃! m ∈ X, ∀ n ∈ X, m ≤ n := by
-- </TARGET_THEOREM>

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

namespace TaoBench

namespace Chapter8

/-- Proposition 8.1.4 (Well ordering principle / Exercise 8.1.2) -/
theorem Nat.exists_unique_min {X : Set ℕ} (hX : X.Nonempty) :
    ∃! m, m ∈ X ∧ ∀ n ∈ X, m ≤ n := by
end Chapter8

end TaoBench
import Mathlib.Data.PNat.Basic

namespace Chapter8

/-- Exercise 8.5.3: The divisibility ordering on PNat. -/
@[reducible] def PNat.divOrder : PartialOrder PNat where
  le x y := ∃ n : PNat, y = n * x
  lt x y := (∃ n : PNat, y = n * x) ∧ ¬∃ n : PNat, x = n * y
  le_refl := by sorry
  le_antisymm := by sorry
  le_trans := by
  lt_iff_le_not_ge := fun _ _ ↦ Iff.rfl

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

namespace TaoBench

namespace Chapter8

/-- Exercise 8.5.3: The divisibility ordering on `PNat`. -/
@[reducible] def PNat.divOrder : PartialOrder PNat := by
end Chapter8

end TaoBench
import Mathlib.Tactic

namespace Chapter8

/-- The definition of equal cardinality. For simplicity we restrict attention to the Type 0 universe.
This is analogous to `Chapter3.SetTheory.Set.EqualCard`, but we are not using the latter since
the Chapter 3 set theory is deprecated. -/
abbrev EqualCard (X Y : Type) : Prop := ∃ f : X → Y, Function.Bijective f

abbrev LeCard (X Y: Type) : Prop := ∃ f: X → Y, Function.Injective f

/-- Exercise 8.3.3 -/
theorem Schroder_Bernstein {X Y:Type} (hXY : LeCard X Y) (hYX : LeCard Y X) : EqualCard X Y := by
end Chapter8
import Mathlib
import Analysis.Section_8_1
import Analysis.Tags.Section_8_1
import Analysis.Section_8_2
import Analysis.Tags.Section_8_2

namespace Chapter8

-- <CONTEXT>
local notation "EqualCard" => (fun X Y : Type => ∃ f : X → Y, Function.Bijective f)
local notation "AtMostCountable" => (fun X : Type => EqualCard X ℕ ∨ Finite X)

theorem EqualCard.power_set_false (X:Type) : ¬ EqualCard X (Set X) := by
  sorry

theorem Uncountable.iff (X:Type) : Uncountable X ↔ ¬ AtMostCountable X := by
  sorry

theorem Uncountable.equiv {X Y: Type} (hXY : EqualCard X Y) :
  Uncountable X ↔ Uncountable Y := by
  sorry

theorem Uncountable.power_set_nat : Uncountable (Set ℕ) := by
  sorry

open Real in
theorem Uncountable.real : Uncountable ℝ := by
  sorry

abbrev LeCard (X Y: Type) : Prop := ∃ f: X → Y, Function.Injective f
-- </CONTEXT>
-- <TARGET_THEOREM>
theorem Schroder_Bernstein {X Y:Type} (hXY : LeCard X Y) (hYX : LeCard Y X) : EqualCard X Y := by
-- </TARGET_THEOREM>

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

namespace TaoBench

/-- Exercise 8.3.3 (Schröder–Bernstein).
If there is an injective map from `X` to `Y` and an injective map from `Y` to `X`,
then there is a bijective map from `X` to `Y`. -/
theorem Schroder_Bernstein {X Y : Type}
    (hXY : ∃ f : X → Y, Function.Injective f)
    (hYX : ∃ g : Y → X, Function.Injective g) :
    ∃ h : X → Y, Function.Injective h ∧ Function.Surjective h := by
end TaoBench
import Mathlib/Tactic
import Mathlib/Data/Set/Lattice
import Mathlib/Order/Lattice
import Mathlib/Order/CompleteLattice
import Mathlib/Order/Disjoint
import Mathlib/Logic/Function/Basic

namespace Chapter8

open Classical in
/-- Exercise 8.3.2.  Some subtle type changes due to how sets are implemented in Mathlib. Also we shift the sequence `D` by one so that we can work in `Set A` rather than `Set B`. -/
theorem Schroder_Bernstein_lemma {X: Type} {A B C: Set X} (hAB: A ⊆ B) (hBC: B ⊆ C) (f: C ↪ A) :
  let D : ℕ → Set A := Nat.rec ((f.image ∘ ((B.embeddingOfSubset _ hBC).image)) {x:B | ↑x ∉ A}) (fun _ ↦ (f.image ∘ ((B.embeddingOfSubset _ hBC).image) ∘ (A.embeddingOfSubset _ hAB).image))
  Set.univ.PairwiseDisjoint D ∧
  let g : A → B := fun x ↦ if h: x ∈ ⋃ n, D n ∧ ∃ y:B, f ⟨↑y, hBC y.property⟩ = x then h.2.choose else ⟨ ↑x, hAB x.property ⟩
  Function.Bijective g
  := by
end Chapter8
import Mathlib
import Analysis.Section_8_1
import Analysis.Tags.Section_8_1
import Analysis.Section_8_2
import Analysis.Tags.Section_8_2

namespace Chapter8

-- <CONTEXT>
theorem EqualCard.power_set_false (X:Type) : ¬ (∃ f : X → Set X, Function.Bijective f) := by
  sorry

theorem Uncountable.iff (X:Type) : Uncountable X ↔ ¬ ((∃ f : X → ℕ, Function.Bijective f) ∨ Finite X) := by
  sorry

theorem Uncountable.equiv {X Y: Type} (hXY : ∃ f : X → Y, Function.Bijective f) :
  Uncountable X ↔ Uncountable Y := by
  sorry

theorem Uncountable.power_set_nat : Uncountable (Set ℕ) := by
  sorry

open Real in
theorem Uncountable.real : Uncountable ℝ := by
  sorry
-- </CONTEXT>
-- <TARGET_THEOREM>
open Classical in
theorem Schroder_Bernstein_lemma {X: Type} {A B C: Set X} (hAB: A ⊆ B) (hBC: B ⊆ C) (f: C ↪ A) :
  let D : ℕ → Set A := Nat.rec ((f.image ∘ ((B.embeddingOfSubset _ hBC).image)) {x:B | ↑x ∉ A}) (fun _ ↦ (f.image ∘ ((B.embeddingOfSubset _ hBC).image) ∘ (A.embeddingOfSubset _ hAB).image))
  Set.univ.PairwiseDisjoint D ∧
  let g : A → B := fun x ↦ if h: x ∈ ⋃ n, D n ∧ ∃ y:B, f ⟨↑y, hBC y.property⟩ = x then h.2.choose else ⟨ ↑x, hAB x.property ⟩
  Function.Bijective g
  := by
-- </TARGET_THEOREM>

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

namespace TaoBench

open Classical in
/-- Exercise 8.3.2 (Schröder–Bernstein lemma, set version).
Some subtle type changes due to how sets are implemented in Mathlib. Also we shift the
sequence `D` by one so that we can work in `Set A` rather than `Set B`. -/
theorem Schroder_Bernstein_lemma {X : Type} {A B C : Set X}
    (hAB : A ⊆ B) (hBC : B ⊆ C) (f : C ↪ A) :
  let D : ℕ → Set A :=
    Nat.rec
      ((f.image ∘ (B.embeddingOfSubset _ hBC).image)
        {x : B | (x : X) ∉ A})
      (fun _ =>
        (f.image ∘ (B.embeddingOfSubset _ hBC).image ∘ (A.embeddingOfSubset _ hAB).image))
  Set.univ.PairwiseDisjoint D ∧
    let g : A → B :=
      fun x =>
        if h : x ∈ ⋃ n, D n ∧ ∃ y : B, f ⟨(y : X), hBC y.property⟩ = x then
          h.2.choose
        else
          ⟨(x : X), hAB x.property⟩
    Function.Bijective g := by
end TaoBench
import Mathlib.Tactic
import Mathlib.Data.Real.Basic
import Mathlib.Data.Finset.Basic
import Mathlib.Topology.Instances.Real

/-!
This file extracts the minimal context to typecheck the target theorem
from Chapter 8, Section 8.2, together with the Series interface from Chapter 7
that is referenced by the definitions here.
-/

namespace Chapter7

/--
  Definition 7.2.1 (Formal infinite series). This is similar to Chapter 6 sequence, but is
  manipulated differently. As with Chapter 5, we will start series from 0 by default.
-/
@[ext]
structure Series where
  m : ℤ
  seq : ℤ → ℝ
  vanish : ∀ n < m, seq n = 0

/-- Functions from ℕ to ℝ can be thought of as series. -/
instance Series.instCoe : Coe (ℕ → ℝ) Series where
  coe := fun a ↦ {
    m := 0
    seq n := if n ≥ 0 then a n.toNat else 0
    vanish := by grind
  }

@[simp]
theorem Series.eval_coe (a: ℕ → ℝ) (n: ℕ) : (a: Series).seq n = a n := by simp

abbrev Series.mk' {m:ℤ} (a: { n // n ≥ m } → ℝ) : Series where
  m := m
  seq n := if h : n ≥ m then a ⟨n, h⟩ else 0
  vanish := by grind

/-- Definition 7.2.2 (Convergence of series) -/
abbrev Series.partial (s : Series) (N:ℤ) : ℝ := ∑ n ∈ Finset.Icc s.m N, s.seq n

abbrev Series.convergesTo (s : Series) (L:ℝ) : Prop := Filter.atTop.Tendsto (s.partial) (nhds L)

abbrev Series.converges (s : Series) : Prop := ∃ L, s.convergesTo L

open Classical in
noncomputable abbrev Series.sum (s : Series) : ℝ := if h : s.converges then h.choose else 0

/-- Definition 7.2.8 (Absolute convergence) -/
abbrev Series.abs (s:Series) : Series := mk' (m:=s.m) (fun n ↦ |s.seq n|)

abbrev Series.absConverges (s:Series) : Prop := s.abs.converges

end Chapter7

/-! Section 8.2 -/

namespace Chapter8
open Chapter7 Chapter7.Series Finset Function Filter

/-- Definition 8.2.1 (Series on countable sets).  Note that with this definition, functions defined
on finite sets will not be absolutely convergent; one should use `AbsConvergent'` instead for such
cases.-/
abbrev AbsConvergent {X:Type} (f: X → ℝ) : Prop := ∃ g: ℕ → X, Bijective g ∧ (f ∘ g:Series).absConverges

open Classical in
/-- The definition has been chosen to give a sensible value when `X` is finite, even though
`AbsConvergent` is by definition false in this context. -/
noncomputable abbrev Sum {X:Type} (f: X → ℝ) : ℝ := if h: AbsConvergent f then (f ∘ h.choose:Series).sum else
  if _hX: Finite X then (∑ x ∈ @univ X (Fintype.ofFinite X), f x) else 0

/-- Lemma 8.2.3 / Exercise 8.2.1 (definition for absolutely convergent sums on arbitrary sets). -/
abbrev AbsConvergent' {X:Type} (f: X → ℝ) : Prop :=
  BddAbove ((fun A ↦ ∑ x ∈ A, |f x|) '' (Set.univ : Set (Finset X)))

/-- A generalized sum.  Note that this will give junk values if `f` is not `AbsConvergent'`. -/
noncomputable abbrev Sum' {X:Type} (f: X → ℝ) : ℝ := Sum (fun x : { x | f x ≠ 0 } ↦ f x)

/-- Proposition 8.2.6 (a) (Absolutely convergent series laws) / Exercise 8.2.3 -/
theorem Sum'.add {X:Type} {f g:X → ℝ} (hf: AbsConvergent' f) (hg: AbsConvergent' g) :
  AbsConvergent' (f+g) ∧ Sum' (f + g) = Sum' f + Sum' g := by
end Chapter8
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

/--
Mathlib version of Tao's Proposition 8.2.6(a) / Exercise 8.2.3:

For functions `f g : X → ℝ` such that the families of absolute values `|f|` and `|g|`
are summable (i.e. the series `∑' x, |f x|` and `∑' x, |g x|` converge),
the family `|f + g|` is also summable, and the (unconditionally defined) infinite sum
is additive:
`∑' x, (f x + g x) = (∑' x, f x) + (∑' x, g x)`.

Here `Summable (fun x => |f x|)` is the Mathlib equivalent of Tao's
`AbsConvergent' f`, and `∑' x, f x` is the Mathlib equivalent of Tao's `Sum' f`.
-/
theorem Sum'.add {X : Type*} {f g : X → ℝ}
    (hf : Summable fun x => |f x|)
    (hg : Summable fun x => |g x|) :
    Summable (fun x => |f x + g x|) ∧
      (∑' x, (f x + g x)) = (∑' x, f x) + (∑' x, g x) := by
end TaoBench
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench
theorem Sum'.of_disjoint_union {X : Type*} {f : X → ℝ}
    (hf : Summable (fun x : X => |f x|))
    {X₁ X₂ : Set X} (hdisj : Disjoint X₁ X₂) :
    (∑' x : (X₁ ∪ X₂ : Set X), f x) =
      (∑' x : X₁, f x) + (∑' x : X₂, f x) := by
end TaoBench
import Mathlib.Tactic

/-! Minimal slice to support Chapter8.Sum'.smul -/

open Lean Elab Tactic

elab "grind" : tactic => do
  evalTactic (← `(tactic| intro n))
  evalTactic (← `(tactic| intro hn))
  evalTactic (← `(tactic| simp [Int.not_le.mpr hn]))

namespace Chapter7

@[ext]
structure Series where
  m : ℤ
  seq : ℤ → ℝ
  vanish : ∀ n < m, seq n = 0

/-- Functions from ℕ to ℝ can be thought of as series. -/
instance : Coe (ℕ → ℝ) Series where
  coe := fun a ↦ {
    m := 0
    seq n := if n ≥ 0 then a n.toNat else 0
    vanish := by grind
  }

abbrev Series.mk' {m:ℤ} (a: { n // n ≥ m } → ℝ) : Series where
  m := m
  seq n := if h : n ≥ m then a ⟨n, h⟩ else 0
  vanish := by grind

/-- Definition 7.2.2 (Convergence of series) -/
abbrev Series.partial (s : Series) (N:ℤ) : ℝ := ∑ n ∈ Finset.Icc s.m N, s.seq n

abbrev Series.convergesTo (s : Series) (L:ℝ) : Prop := Filter.atTop.Tendsto (s.partial) (nhds L)

abbrev Series.converges (s : Series) : Prop := ∃ L, s.convergesTo L

open Classical in
noncomputable abbrev Series.sum (s : Series) : ℝ := if h : s.converges then h.choose else 0

abbrev Series.abs (s:Series) : Series := mk' (m:=s.m) (fun n ↦ |s.seq n|)

abbrev Series.absConverges (s:Series) : Prop := s.abs.converges

end Chapter7

namespace Chapter8
open Chapter7

/-- Definition 8.2.1 (Series on countable sets). -/
abbrev AbsConvergent {X:Type} (f: X → ℝ) : Prop :=
  ∃ g: ℕ → X, Function.Bijective g ∧ (f ∘ g:Chapter7.Series).absConverges

open Classical in
/-- A generalized sum `Sum` for absolutely convergent series on arbitrary sets. -/
noncomputable abbrev Sum {X:Type} (f: X → ℝ) : ℝ :=
  if h: AbsConvergent f then (f ∘ h.choose:Chapter7.Series).sum else
  if _hX: Finite X then (∑ x ∈ @Finset.univ X (Fintype.ofFinite X), f x) else 0

/-- Absolute convergence on arbitrary sets (finite or countable). -/
abbrev AbsConvergent' {X:Type} (f: X → ℝ) : Prop :=
  BddAbove ((fun A ↦ ∑ x ∈ A, |f x|) '' (Set.univ : Set (Finset X)))

/-- A generalized sum. Note: gives junk values if `f` is not `AbsConvergent'`. -/
noncomputable abbrev Sum' {X:Type} (f: X → ℝ) : ℝ :=
  Sum (fun x : { x | f x ≠ 0 } ↦ f x)

/-- Proposition 8.2.6 (b) (Absolutely convergent series laws) / Exercise 8.2.3 -/
theorem Sum'.smul {X:Type} {f:X → ℝ} (hf: AbsConvergent' f) (c: ℝ) :
  AbsConvergent' (c • f) ∧ Sum' (c • f) = c * Sum' f := by
end Chapter8
import Mathlib
import Aesop
set_option maxHeartbeats 0
open BigOperators Real Nat Topology Rat Set Filter

namespace TaoBench

/-- Proposition 8.2.6 (b) (Absolutely convergent series laws) / Exercise 8.2.3,
rephrased in Mathlib: for an absolutely summable family `f : X → ℝ` and a scalar
`c : ℝ`, scaling preserves absolute summability and scales the (unconditional)
sum. -/
theorem Sum'.smul {X : Type*} {f : X → ℝ}
    (hf : Summable fun x : X => |f x|)
    (c : ℝ) :
    Summable (fun x : X => |(c • f) x|) ∧
      (∑' x : X, (c • f) x) = c * ∑' x : X, f x := by
end TaoBench
import Mathlib
import Analysis.Section_8_4
import Analysis.Tags.Section_8_4

namespace Chapter8

-- <CONTEXT>

def PartialOrder.mk {X:Type} [LE X]
  (hrefl: ∀ x:X, x ≤ x)
  (hantisymm: ∀ x y:X, x ≤ y → y ≤ x → x = y)
  (htrans: ∀ x y z:X, x ≤ y → y ≤ z → x ≤ z) : PartialOrder X :=
{
  le := (· ≤ ·)
  le_refl := hrefl
  le_antisymm := hantisymm
  le_trans := htrans
}

def IsTotal (X:Type) [PartialOrder X] : Prop := ∀ x y:X, x ≤ y ∨ y ≤ x

open Classical in
noncomputable def LinearOrder.mk {X:Type} [PartialOrder X]
  (htotal: IsTotal X) : LinearOrder X :=
{
   le_total := htotal
   toDecidableLE := decRel LE.le
}

noncomputable def LinearOrder.subtype {X:Type} [LinearOrder X] (A: Set X) : LinearOrder A :=
LinearOrder.mk (by
  sorry
  )

theorem IsTotal.subtype {X:Type} [PartialOrder X] {A: Set X} (hA: IsTotal X) : IsTotal A := by
  sorry

theorem IsTotal.subset {X:Type} [PartialOrder X] {A B: Set X} (hA: IsTotal A) (hAB: B ⊆ A) : IsTotal B := by
  sorry

abbrev X_8_5_4 : Set (Set ℕ) := { {1,2}, {2}, {2,3}, {2,3,4}, {5} }

theorem IsMax.iff {X:Type} [PartialOrder X] (x:X) :
  IsMax x ↔ ¬ ∃ y, x < y := by
  sorry

theorem IsMin.iff {X:Type} [PartialOrder X] (x:X) :
  IsMin x ↔ ¬ ∃ y, x > y := by
  sorry

theorem WellFoundedLT.iff (X:Type) [LinearOrder X] :
  WellFoundedLT X ↔ ∀ A:Set X, A.Nonempty → ∃ x:A, IsMin x := by
  sorry

theorem WellFoundedLT.iff' {X:Type} [PartialOrder X] (h: IsTotal X) :
  WellFoundedLT X ↔ ∀ A:Set X, A.Nonempty → ∃ x:A, IsMin x := by
  sorry

theorem IsMin.ofFinite {X:Type} [LinearOrder X] [Finite X] [Nonempty X] : ∃ x:X, IsMin x := by
  sorry

-- </CONTEXT>
-- <TARGET_THEOREM>
theorem WellFoundedLT.ofFinite {X:Type} [LinearOrder X] [Finite X] : WellFoundedLT X := by
-- </TARGET_THEOREM>

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

namespace TaoBench

/-- Exercise 8.5.8 - In any finite linear order, `<` is well-founded. -/
theorem WellFoundedLT.ofFinite {X : Type} [LinearOrder X] [Finite X] :
    WellFoundedLT X := by
end TaoBench
import Mathlib.Tactic
import Mathlib.Order.WellFounded

namespace Chapter8

/-- Proposition 8.5.10 / Exercise 8.5.10 -/
theorem WellFoundedLT.strong_induction {X:Type} [LinearOrder X] [WellFoundedLT X] {P:X → Prop}
  (h: ∀ n, (∀ m < n, P m) → P n) : ∀ n, P n := by
end Chapter8