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

-- Section 4.3 essentials
 
def Rat.Close (ε : ℚ) (x y : ℚ) := |x - y| ≤ ε

namespace Section_4_3

abbrev dist (x y : ℚ) := |x - y|

theorem dist_eq (x y : ℚ) : dist x y = |x - y| := rfl

end Section_4_3

-- Section 5.1 essentials
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
    grind

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 grind

@[simp]
lemma Sequence.eval_coe (n : ℕ) (a : ℕ → ℚ) : (a : Sequence) n = a n := by norm_cast

@[simp]
lemma Sequence.eval_coe_at_int (n : ℤ) (a : ℕ → ℚ) : (a : Sequence) n = if n ≥ 0 then a n.toNat else 0 := by
  norm_cast

@[simp]
lemma Sequence.n0_coe (a : ℕ → ℚ) : (a : Sequence).n₀ = 0 := by norm_cast

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

end Chapter5

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

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

namespace Chapter5

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

-- Section 5.2 essentials
end Chapter5

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)

namespace Chapter5

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

-- Section 5.3 essentials
@[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
    intro n hn
    simpa using ((a : Sequence).vanish n hn)
  zero := rfl
  cauchy := ha

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

@[simp]
lemma CauchySequence.coe_to_sequence (a : CauchySequence) :
    ((a : ℕ → ℚ) : Sequence) = a.toSequence := by
  apply Sequence.ext (by simp [Sequence.n0_coe, a.zero])
  ext n; by_cases h : n ≥ 0 <;> simp_all
  rw [a.vanish]; rwa [a.zero]

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

-- zero instance for CauchySequence
@[simp]
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))

lemma LIM_def {a : ℕ → ℚ} (ha : (a : Sequence).IsCauchy) :
    LIM a = Quotient.mk _ (CauchySequence.mk' ha) := by
  rw [LIM, dif_pos ha]

theorem Real.LIM_eq_LIM {a b : ℕ → ℚ} (ha : (a : Sequence).IsCauchy) (hb : (b : Sequence).IsCauchy) :
    LIM a = LIM b ↔ Sequence.Equiv a b := by
  constructor
  · intro h; replace h := Quotient.exact h
    rwa [dif_pos ha, dif_pos hb] at h
  intro h; apply Quotient.sound; simpa [dif_pos ha, dif_pos hb]

theorem Sequence.IsCauchy.add {a b : ℕ → ℚ} (ha : (a : Sequence).IsCauchy) (hb : (b : Sequence).IsCauchy) :
    ((a + b : ℕ → ℚ) : Sequence).IsCauchy := by sorry

theorem Sequence.add_equiv {a b a' b' : ℕ → ℚ} (haa' : Sequence.Equiv a a')
  (hbb' : Sequence.Equiv b b') :
    Sequence.Equiv (a + b) (a' + b') := by sorry

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' _ _
        change LIM ((a : ℕ → ℚ) + (b : ℕ → ℚ)) = LIM ((a' : ℕ → ℚ) + (b' : ℕ → ℚ))
        rw [Real.LIM_eq_LIM]
        · solve_by_elim [Sequence.add_equiv]
        all_goals
          apply Sequence.IsCauchy.add <;> rw [CauchySequence.coe_to_sequence] <;> convert @CauchySequence.cauchy ?_)

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' haa' hbb'
        change LIM ((a : ℕ → ℚ) * (b : ℕ → ℚ)) = LIM ((a' : ℕ → ℚ) * (b' : ℕ → ℚ))
        rw [Real.LIM_eq_LIM]
        · sorry
        all_goals
          sorry)

instance Real.instRatCast : RatCast Real where
  ratCast := fun q ↦ Quotient.mk _ (CauchySequence.mk' (a := fun _ ↦ q) (by
    -- constant sequences are Cauchy
    simpa using (Sequence.IsCauchy.const q)))

-- OfNat/NatCast/IntCast instances so numerals and casts work
instance Real.instOfNat {n : ℕ} : OfNat Real n where ofNat := ((n : ℚ) : Real)

instance Real.instNatCast : NatCast Real where natCast n := ((n : ℚ) : Real)

instance Real.instIntCast : IntCast Real where intCast n := ((n : ℚ) : Real)

-- For subtraction and order we will need neg and add structures
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)

-- Inversion and division to parse 1/(M+1) as a Real
abbrev BoundedAwayZero (a : ℕ → ℚ) : Prop := ∃ c : ℚ, c > 0 ∧ ∀ n, |a n| ≥ c

-- We only need the existence statement to define inv; we leave the proof as sorry.
theorem Real.boundedAwayZero_of_nonzero {x : Real} (hx : x ≠ 0) :
    ∃ a : ℕ → ℚ, (a : Sequence).IsCauchy ∧ BoundedAwayZero a ∧ x = LIM a := by
  sorry

open Classical in
noncomputable instance Real.instInv : Inv Real where
  inv x := if h : x ≠ 0 then LIM (Real.boundedAwayZero_of_nonzero h).choose⁻¹ else 0

-- Use HDiv for `/` notation on Real
noncomputable instance Real.instHDiv : HDiv Real Real Real where hDiv x y := x * y⁻¹

-- Section 5.4 essentials
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

open Classical in
noncomputable abbrev Real.abs (x : Real) : Real := if x.IsPos then x else (if x.IsNeg then -x else 0)

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)

noncomputable instance Real.instLinearOrder : LinearOrder Real where
  le_refl := by sorry
  le_trans := by sorry
  lt_iff_le_not_ge := by sorry
  le_antisymm := by sorry
  le_total := by sorry
  toDecidableLE := Classical.decRel _

-- Target theorem from Section 5.5
namespace Chapter5
theorem Real.LIM_of_Cauchy {q:ℕ → ℚ} (hq: ∀ M, ∀ n ≥ M, ∀ n' ≥ M, |q n - q n'| ≤ 1 / (M+1)) :
    (q:Sequence).IsCauchy ∧ ∀ M, |q M - LIM q| ≤ 1 / (M+1) := by
end Chapter5