import Mathlib
import Analysis.Section_5_3
import Analysis.Tags.Section_5_3

namespace Chapter5

-- <CONTEXT>
def Rat.Close (ε : ℚ) (x y : ℚ) : Prop := |x - y| ≤ ε

@[ext]
structure Sequence where
  n₀ : ℤ
  seq : ℤ → ℚ
  vanish : ∀ n < n₀, seq n = 0

instance Sequence.instCoeFun : CoeFun Sequence (fun _ ↦ ℤ → ℚ) where
  coe := fun a ↦ a.seq

abbrev Sequence.mk' (n₀ : ℤ) (a : { n // n ≥ n₀ } → ℚ) : Sequence where
  n₀ := n₀
  seq n := if h : n ≥ n₀ then a ⟨n, h⟩ else 0
  vanish := by
    intro n hn
    simp [not_le.mpr hn]

abbrev Sequence.from (a : Sequence) (n₁ : ℤ) : Sequence :=
  mk' (max a.n₀ n₁) (fun n ↦ a (n : ℤ))

instance : Coe (ℕ → ℚ) Sequence where
  coe a :=
    { n₀ := 0
      seq := fun n ↦ if n ≥ 0 then a n.toNat else 0
      vanish := by
        intro n hn
        have : ¬ n ≥ 0 := by exact not_le.mpr hn
        simp [this] }

abbrev Rat.Steady (ε : ℚ) (a : Sequence) : Prop :=
  ∀ n ≥ a.n₀, ∀ m ≥ a.n₀, Rat.Close ε (a n) (a m)

abbrev Rat.EventuallySteady (ε : ℚ) (a : Sequence) : Prop :=
  ∃ N ≥ a.n₀, Rat.Steady ε (a.from N)

abbrev Sequence.IsCauchy (a : Sequence) : Prop := ∀ ε > (0 : ℚ), Rat.EventuallySteady ε a

abbrev Rat.CloseSeq (ε : ℚ) (a b : Chapter5.Sequence) : Prop :=
  ∀ n, n ≥ a.n₀ → n ≥ b.n₀ → Rat.Close ε (a n) (b n)

abbrev Rat.EventuallyClose (ε : ℚ) (a b : Chapter5.Sequence) : Prop :=
  ∃ N, Rat.CloseSeq ε (a.from N) (b.from N)

abbrev Sequence.Equiv (a b : ℕ → ℚ) : Prop :=
  ∀ ε > (0 : ℚ), Rat.EventuallyClose ε (a : Sequence) (b : Sequence)

@[ext]
class CauchySequence extends Sequence where
  zero : n₀ = 0
  cauchy : toSequence.IsCauchy

instance CauchySequence.instCoeFun : CoeFun CauchySequence (fun _ ↦ ℕ → ℚ) where
  coe a n := a.toSequence (n : ℤ)

abbrev CauchySequence.mk' {a : ℕ → ℚ} (ha : (a : Sequence).IsCauchy) : CauchySequence where
  n₀ := 0
  seq := (a : Sequence).seq
  vanish := by aesop
  zero := rfl
  cauchy := ha

instance CauchySequence.instSetoid : Setoid CauchySequence where
  r := fun a b ↦ Sequence.Equiv a b
  iseqv :=
    { refl := by sorry
      symm := by sorry
      trans := by sorry }

theorem Sequence.IsCauchy.const (a : ℚ) : ((fun _ : ℕ ↦ a) : Sequence).IsCauchy := by
  sorry

instance CauchySequence.instZero : Zero CauchySequence where
  zero := CauchySequence.mk' (a := fun _ : ℕ ↦ 0) (Sequence.IsCauchy.const (0 : ℚ))

abbrev Real := Quotient CauchySequence.instSetoid

open Classical in
noncomputable abbrev LIM (a : ℕ → ℚ) : Real :=
  Quotient.mk _ (if h : (a : Sequence).IsCauchy then CauchySequence.mk' h else (0 : CauchySequence))

instance Real.instRatCast : RatCast Real where
  ratCast := fun q ↦ Quotient.mk _ (CauchySequence.mk' (a := fun _ ↦ q) (Sequence.IsCauchy.const q))

instance Real.instOfNat {n : ℕ} : OfNat Real n where ofNat := ((n : ℚ) : Real)

abbrev BoundedAwayZero (a:ℕ → ℚ) : Prop :=
  ∃ (c:ℚ), c > 0 ∧ ∀ n, |a n| ≥ c

abbrev BoundedAwayPos (a:ℕ → ℚ) : Prop :=
  ∃ (c:ℚ), c > 0 ∧ ∀ n, a n ≥ c

abbrev BoundedAwayNeg (a:ℕ → ℚ) : Prop :=
  ∃ (c:ℚ), c > 0 ∧ ∀ n, a n ≤ -c

theorem boundedAwayPos_def (a:ℕ → ℚ) : BoundedAwayPos a ↔ ∃ (c:ℚ), c > 0 ∧ ∀ n, a n ≥ c := by
  sorry

theorem boundedAwayNeg_def (a:ℕ → ℚ) : BoundedAwayNeg a ↔ ∃ (c:ℚ), c > 0 ∧ ∀ n, a n ≤ -c := by
  sorry

theorem BoundedAwayZero.boundedAwayPos {a:ℕ → ℚ} (ha: BoundedAwayPos a) : BoundedAwayZero a := by
  sorry

theorem BoundedAwayZero.boundedAwayNeg {a:ℕ → ℚ} (ha: BoundedAwayNeg a) : BoundedAwayZero a := by
  sorry

theorem not_boundedAwayPos_boundedAwayNeg {a:ℕ → ℚ} : ¬ (BoundedAwayPos a ∧ BoundedAwayNeg a) := by
  sorry

abbrev Real.IsPos (x:Real) : Prop :=
  ∃ a:ℕ → ℚ, BoundedAwayPos a ∧ (a:Sequence).IsCauchy ∧ x = LIM a

abbrev Real.IsNeg (x:Real) : Prop :=
  ∃ a:ℕ → ℚ, BoundedAwayNeg a ∧ (a:Sequence).IsCauchy ∧ x = LIM a

theorem Real.isPos_def (x:Real) :
    IsPos x ↔ ∃ a:ℕ → ℚ, BoundedAwayPos a ∧ (a:Sequence).IsCauchy ∧ x = LIM a := by
  sorry

theorem Real.isNeg_def (x:Real) :
    IsNeg x ↔ ∃ a:ℕ → ℚ, BoundedAwayNeg a ∧ (a:Sequence).IsCauchy ∧ x = LIM a := by
  sorry
-- </CONTEXT>
-- <TARGET_THEOREM>
theorem Real.not_zero_pos (x:Real) : ¬(x = 0 ∧ x.IsPos) := by
-- </TARGET_THEOREM>

end Chapter5