import Mathlib.Tactic
import Mathlib.Algebra.Group.MinimalAxioms


-- We place Rat.Close outside the Chapter5 namespace, as in Section 4.3
def Rat.Close (ε : ℚ) (x y : ℚ) := |x - y| ≤ ε

namespace Chapter5

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

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

@[coe]
def Sequence.ofNatFun (a : ℕ → ℚ) : Sequence where
  n₀ := 0
  seq n := if n ≥ 0 then a n.toNat else 0
  vanish := by
    sorry

instance : Coe (ℕ → ℚ) Sequence where
  coe := Sequence.ofNatFun

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
    sorry

end Chapter5

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

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

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

namespace Chapter5

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

end Chapter5

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

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

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

namespace Chapter5

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

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

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

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

@[simp]
theorem Sequence_IsCauchy_const (a : ℚ) : ((fun _ : ℕ ↦ a) : Sequence).IsCauchy := by
  -- This matches Sequence.IsCauchy.const from Section 5.3
  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))

noncomputable instance Real.add_inst : Add Real where
  add := fun x y ↦
    Quotient.liftOn₂ x y (fun a b ↦ LIM (a + b))
      (by
        intro a b a' b' _ _
        -- well-definedness proof, omitted
        sorry)

noncomputable instance Real.mul_inst : Mul Real where
  mul := fun x y ↦
    Quotient.liftOn₂ x y (fun a b ↦ LIM (a * b))
      (by
        intro a b a' b' _ _
        -- well-definedness proof, omitted
        sorry)

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)

noncomputable instance Real.instNeg : Neg Real where neg x := ((-1 : ℚ) : Real) * x

noncomputable instance Real.addGroup_inst : AddGroup Real :=
  AddGroup.ofLeftAxioms (by sorry) (by sorry) (by sorry)

-- Section 5.4 ordering and positivity
abbrev BoundedAwayPos (a : ℕ → ℚ) : Prop :=
  ∃ c : ℚ, c > 0 ∧ ∀ n, a n ≥ c

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

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

instance Real.instLT : LT Real where
  lt x y := (x - y).IsNeg

instance Real.instLE : LE Real where
  le x y := (x < y) ∨ (x = y)
theorem Real.mul_le_mul_left {x y z : Real} (hxy : x ≤ y) (hz : z.IsPos) : z * x ≤ z * y := by
end Chapter5