import Mathlib
import Analysis.Section_3_5
import Analysis.Tags.Section_3_5

namespace Chapter3

-- <CONTEXT>
universe u v

class SetTheory where
  Set : Type u
  Object : Type v
  set_to_object : Set ↪ Object
  mem : Object → Set → Prop
  extensionality X Y : (∀ x, mem x X ↔ mem x Y) → X = Y
  emptyset : Set
  emptyset_mem x : ¬ mem x emptyset
  singleton : Object → Set
  singleton_axiom x y : mem x (singleton y) ↔ x = y
  union_pair : Set → Set → Set
  union_pair_axiom X Y x : mem x (union_pair X Y) ↔ (mem x X ∨ mem x Y)
  specify A (P : Subtype (mem . A) → Prop) : Set
  specification_axiom A (P : Subtype (mem . A) → Prop) :
    (∀ x, mem x (specify A P) → mem x A) ∧ ∀ x, mem x.val (specify A P) ↔ P x
  replace A (P : Subtype (mem . A) → Object → Prop)
    (hP : ∀ x y y', P x y ∧ P x y' → y = y') : Set
  replacement_axiom A (P : Subtype (mem . A) → Object → Prop)
    (hP : ∀ x y y', P x y ∧ P x y' → y = y') : ∀ y, mem y (replace A P hP) ↔ ∃ x, P x y
  nat : Set
  nat_equiv : ℕ ≃ Subtype (mem . nat)
  regularity_axiom A (hA : ∃ x, mem x A) :
    ∃ x, mem x A ∧ ∀ S, x = set_to_object S → ¬ ∃ y, mem y A ∧ mem y S
  pow : Set → Set → Set
  function_to_object (X : Set) (Y : Set) :
    (Subtype (mem . X) → Subtype (mem . Y)) ↪ Object
  powerset_axiom (X : Set) (Y : Set) (F : Object) :
    mem F (pow X Y) ↔ ∃ f : Subtype (mem . Y) → Subtype (mem . X),
      function_to_object Y X f = F
  union : Set → Set
  union_axiom A x : mem x (union A) ↔ ∃ S, mem x S ∧ mem (set_to_object S) A

export SetTheory (Set Object nat)

variable [SetTheory]

instance SetTheory.objects_mem_sets : Membership Object Set where
  mem X x := SetTheory.mem x X

instance SetTheory.sets_are_objects : Coe Set Object where
  coe X := SetTheory.set_to_object X

@[ext]
theorem SetTheory.Set.ext {X Y : Set} (h : ∀ x, x ∈ X ↔ x ∈ Y) : X = Y := by
  sorry

instance SetTheory.Set.instEmpty : EmptyCollection Set where
  emptyCollection := SetTheory.emptyset

instance SetTheory.Set.instSingleton : Singleton Object Set where
  singleton := SetTheory.singleton

instance SetTheory.Set.instUnion : Union Set where
  union := SetTheory.union_pair

instance SetTheory.Set.instInsert : Insert Object Set where
  insert x X := {x} ∪ X

instance SetTheory.Set.instSubset : HasSubset Set where
  Subset X Y := ∀ x, x ∈ X → x ∈ Y

instance SetTheory.Set.instSSubset : HasSSubset Set where
  SSubset X Y := X ⊆ Y ∧ X ≠ Y

instance SetTheory.Set.instPartialOrder : PartialOrder Set where
  le X Y := X ⊆ Y
  le_refl := by sorry
  le_trans := by sorry
  le_antisymm := by sorry

instance SetTheory.Set.instOrderBot : OrderBot Set where
  bot := ∅
  bot_le := by sorry

abbrev SetTheory.Set.toSubtype (A : Set) := Subtype (fun x ↦ x ∈ A)

instance : CoeSort (SetTheory.Set) (Type v) where
  coe A := SetTheory.Set.toSubtype A

abbrev SetTheory.Set.specify (A : Set) (P : A → Prop) : Set := SetTheory.specify A P

theorem SetTheory.Set.specification_axiom {A : Set} {P : A → Prop} {x : Object}
    (h : x ∈ A.specify P) : x ∈ A := by
  sorry

theorem SetTheory.Set.specification_axiom' {A : Set} (P : A → Prop) (x : A) :
    x.val ∈ A.specify P ↔ P x := by
  sorry

@[simp]
theorem SetTheory.Set.specification_axiom'' {A : Set} (P : A → Prop) (x : Object) :
    x ∈ A.specify P ↔ ∃ h : x ∈ A, P ⟨x, h⟩ := by
  sorry

instance SetTheory.Set.instIntersection : Inter Set where
  inter X Y := X.specify (fun x ↦ x.val ∈ Y)

instance SetTheory.Set.instSDiff : SDiff Set where
  sdiff X Y := X.specify (fun x ↦ x.val ∉ Y)

abbrev SetTheory.Set.replace (A : Set) {P : A → Object → Prop}
  (hP : ∀ x y y', P x y ∧ P x y' → y = y') : Set :=
  SetTheory.replace A P hP

@[simp]
theorem SetTheory.Set.replacement_axiom {A : Set} {P : A → Object → Prop}
  (hP : ∀ x y y', P x y ∧ P x y' → y = y') (y : Object) :
    y ∈ A.replace hP ↔ ∃ x, P x y := by
  sorry

abbrev Nat := SetTheory.nat

def SetTheory.Set.nat_equiv : ℕ ≃ Nat := SetTheory.nat_equiv

instance SetTheory.Set.instOfNat {n : ℕ} : OfNat Nat n where
  ofNat := SetTheory.Set.nat_equiv n

instance SetTheory.Set.instNatCast : NatCast Nat where
  natCast n := SetTheory.Set.nat_equiv n

instance SetTheory.Set.toNat : Coe Nat ℕ where
  coe n := SetTheory.Set.nat_equiv.symm n

instance SetTheory.Object.instNatCast : NatCast Object where
  natCast n := (n : Chapter3.Nat).val

instance SetTheory.Object.instOfNat {n : ℕ} : OfNat Object n where
  ofNat := ((n : Chapter3.Nat) : Object)

instance SetTheory.Set.inst_pow : Pow Set Set where
  pow := SetTheory.pow

@[coe]
def SetTheory.Set.coe_of_fun {X Y : Set} (f : X → Y) : Object :=
  SetTheory.function_to_object X Y f

instance SetTheory.Set.inst_coe_of_fun {X Y : Set} : CoeOut (X → Y) Object where
  coe := SetTheory.Set.coe_of_fun

@[simp]
theorem SetTheory.Set.powerset_axiom {X Y : Set} (F : Object) :
    F ∈ (X ^ Y) ↔ ∃ f : Y → X, f = F := by
  sorry

abbrev SetTheory.Set.cartesian (X Y : Set) : Set := ∅

instance SetTheory.Set.inst_SProd : SProd Set Set Set where
  sprod := SetTheory.Set.cartesian

abbrev SetTheory.Set.iUnion (I : Set) (A : I → Set) : Set := ∅

abbrev SetTheory.Set.Fin (n : ℕ) : Set := SetTheory.nat.specify (fun m ↦ (m : ℕ) < n)

theorem SetTheory.Set.mem_Fin (n : ℕ) (x : Object) : x ∈ Fin n ↔ ∃ m, m < n ∧ x = m := by
  sorry

abbrev SetTheory.Set.Fin_mk (n m : ℕ) (h : m < n) : Fin n := ⟨(m : Object),
  (SetTheory.Set.mem_Fin n (m : Object)).2 ⟨m, h, rfl⟩⟩

theorem SetTheory.Set.mem_Fin' {n : ℕ} (x : Fin n) : ∃ m, ∃ h : m < n, x = Fin_mk n m h := by
  sorry

@[coe]
noncomputable abbrev SetTheory.Set.Fin.toNat {n : ℕ} (i : Fin n) : ℕ := (SetTheory.Set.mem_Fin' i).choose

noncomputable instance SetTheory.Set.Fin.inst_coeNat {n : ℕ} : CoeOut (Fin n) ℕ where
  coe := SetTheory.Set.Fin.toNat

theorem SetTheory.Set.Fin.toNat_spec {n : ℕ} (i : Fin n) :
    ∃ h : i < n, i = Fin_mk n i h := by
  sorry

theorem SetTheory.Set.Fin.toNat_lt {n : ℕ} (i : Fin n) : i < n := by
  sorry

abbrev SetTheory.Set.Fin_embed (n N : ℕ) (h : n ≤ N) (i : Fin n) : Fin N := ⟨i.val, by
  sorry⟩

abbrev SetTheory.Set.EqualCard (X Y:Set) : Prop := ∃ f : X → Y, Function.Bijective f

theorem SetTheory.Set.Example_3_6_2 : EqualCard {0,1,2} {3,4,5} := by
  sorry

theorem SetTheory.Set.Example_3_6_3 : EqualCard nat (nat.specify (fun x ↦ Even (x:ℕ))) := by
  sorry

@[refl]
theorem SetTheory.Set.EqualCard.refl (X:Set) : EqualCard X X := by
  sorry

@[symm]
theorem SetTheory.Set.EqualCard.symm {X Y:Set} (h: EqualCard X Y) : EqualCard Y X := by
  sorry

@[trans]
theorem SetTheory.Set.EqualCard.trans {X Y Z:Set} (h1: EqualCard X Y) (h2: EqualCard Y Z) : EqualCard X Z := by
  sorry

instance SetTheory.Set.EqualCard.inst_setoid : Setoid SetTheory.Set := ⟨ EqualCard, {refl, symm, trans} ⟩

abbrev SetTheory.Set.has_card (X:Set) (n:ℕ) : Prop := X ≈ Fin n

theorem SetTheory.Set.has_card_iff (X:Set) (n:ℕ) :
    X.has_card n ↔ ∃ f: X → Fin n, Function.Bijective f := by
  sorry

theorem SetTheory.Set.Remark_3_6_6 (n:ℕ) :
    (nat.specify (fun x ↦ 1 ≤ (x:ℕ) ∧ (x:ℕ) ≤ n)).has_card n := by
  sorry

theorem SetTheory.Set.Example_3_6_7a (a:Object) : ({a}:Set).has_card 1 := by
  sorry

theorem SetTheory.Set.Example_3_6_7b {a b c d:Object} (hab: a ≠ b) (hac: a ≠ c) (had: a ≠ d)
    (hbc: b ≠ c) (hbd: b ≠ d) (hcd: c ≠ d) : ({a,b,c,d}:Set).has_card 4 := by
  sorry

theorem SetTheory.Set.pos_card_nonempty {n:ℕ} (h: n ≥ 1) {X:Set} (hX: X.has_card n) : X ≠ ∅ := by
  sorry

theorem SetTheory.Set.card_erase {n:ℕ} (h: n ≥ 1) {X:Set} (hX: X.has_card n) (x:X) :
    (X \ {x.val}).has_card (n-1) := by
  sorry

theorem SetTheory.Set.card_uniq {X:Set} {n m:ℕ} (h1: X.has_card n) (h2: X.has_card m) : n = m := by
  sorry

lemma SetTheory.Set.Example_3_6_8_a: ({0,1,2}:Set).has_card 3 := by
  sorry

lemma SetTheory.Set.Example_3_6_8_b: ({3,4}:Set).has_card 2 := by
  sorry

lemma SetTheory.Set.Example_3_6_8_c : ¬({0,1,2}:Set) ≈ ({3,4}:Set) := by
  sorry

abbrev SetTheory.Set.finite (X:Set) : Prop := ∃ n:ℕ, X.has_card n

abbrev SetTheory.Set.infinite (X:Set) : Prop := ¬ finite X

theorem SetTheory.Set.nat_infinite : infinite nat := by
  sorry

open Classical in
noncomputable def SetTheory.Set.card (X:Set) : ℕ := if h:X.finite then h.choose else 0

theorem SetTheory.Set.has_card_card {X:Set} (hX: X.finite) : X.has_card (SetTheory.Set.card X) := by
  sorry

theorem SetTheory.Set.has_card_to_card {X:Set} {n: ℕ}: X.has_card n → X.card = n := by
  sorry

theorem SetTheory.Set.card_to_has_card {X:Set} {n: ℕ} (hn: n ≠ 0): X.card = n → X.has_card n := by
  sorry

theorem SetTheory.Set.card_fin_eq (n:ℕ): (Fin n).has_card n := by
  sorry

theorem SetTheory.Set.Fin_card (n:ℕ): (Fin n).card = n := by
  sorry

theorem SetTheory.Set.Fin_finite (n:ℕ): (Fin n).finite := by
  sorry

theorem SetTheory.Set.EquivCard_to_has_card_eq {X Y:Set} {n: ℕ} (h: X ≈ Y): X.has_card n ↔ Y.has_card n := by
  sorry

theorem SetTheory.Set.EquivCard_to_card_eq {X Y:Set} (h: X ≈ Y): X.card = Y.card := by
  sorry

lemma SetTheory.Set.empty_of_card_eq_zero {X:Set} (hX : X.finite) : X.card = 0 → X = ∅ := by
  sorry

lemma SetTheory.Set.finite_of_empty {X:Set} : X = ∅ → X.finite := by
  sorry

lemma SetTheory.Set.card_eq_zero_of_empty {X:Set} : X = ∅ → X.card = 0 := by
  sorry

@[simp]
lemma SetTheory.Set.empty_finite : (∅: Set).finite := by
  sorry

@[simp]
lemma SetTheory.Set.empty_card_eq_zero : (∅: Set).card = 0 := by
  sorry

noncomputable def SetTheory.Set.pow_fun_equiv {A B : Set} : ↑(A ^ B) ≃ (B → A) where
  toFun := sorry
  invFun := sorry
  left_inv := sorry
  right_inv := sorry

lemma SetTheory.Set.pow_fun_eq_iff {A B : Set} (x y : ↑(A ^ B)) : x = y ↔ pow_fun_equiv x = pow_fun_equiv y := by
  sorry

noncomputable abbrev SetTheory.Set.pow_fun_equiv' (A B : Set) : ↑(A ^ B) ≃ (B → A) :=
  pow_fun_equiv (A:=A) (B:=B)

theorem SetTheory.Set.pow_pow_eq_pow_mul (a b c:ℕ): (a^b)^c = a^(b*c) := by
  sorry

theorem SetTheory.Set.pow_prod_pow_EqualCard_pow_union (A B C:Set) (hd: Disjoint B C) :
    EqualCard ((A ^ B) ×ˢ (A ^ C)) (A ^ (B ∪ C)) := by
  sorry

theorem SetTheory.Set.pow_mul_pow_eq_pow_add (a b c:ℕ): (a^b) * a^c = a^(b+c) := by
  sorry

def SetTheory.Set.Permutations (n: ℕ): Set := (Fin n ^ Fin n).specify (fun F ↦
    Function.Bijective (pow_fun_equiv F))

noncomputable def SetTheory.Set.Permutations_toFun {n: ℕ} (p: Permutations n) : (Fin n) → (Fin n) := by
  have := p.property
  simp only [Permutations, specification_axiom'', powerset_axiom] at this
  exact this.choose.choose

theorem SetTheory.Set.Permutations_bijective {n: ℕ} (p: Permutations n) :
    Function.Bijective (Permutations_toFun p) := by
  sorry

theorem SetTheory.Set.Permutations_inj {n: ℕ} (p1 p2: Permutations n) :
    Permutations_toFun p1 = Permutations_toFun p2 ↔ p1 = p2 := by
  sorry

noncomputable def SetTheory.Set.perm_equiv_equiv {n : ℕ} : Permutations n ≃ (Fin n ≃ Fin n) := {
  toFun := fun p => Equiv.ofBijective (Permutations_toFun p) (Permutations_bijective p)
  invFun := sorry
  left_inv := sorry
  right_inv := sorry
}

def SetTheory.Set.Fin.castSucc {n} (x : Fin n) : Fin (n + 1) :=
  Fin_embed _ _ (by omega) x

@[simp]
lemma SetTheory.Set.Fin.castSucc_inj {n} {x y : Fin n} : castSucc x = castSucc y ↔ x = y := by
  sorry

@[simp]
theorem SetTheory.Set.Fin.castSucc_ne {n} (x : Fin n) : castSucc x ≠ n := by
  sorry

noncomputable def SetTheory.Set.Fin.castPred {n} (x : Fin (n + 1)) (h : (x : ℕ) ≠ n) : Fin n :=
  Fin_mk _ (x : ℕ) (by have := Fin.toNat_lt x; omega)

@[simp]
theorem SetTheory.Set.Fin.castSucc_castPred {n} (x : Fin (n + 1)) (h : (x : ℕ) ≠ n) :
    castSucc (castPred x h) = x := by
  sorry

@[simp]
theorem SetTheory.Set.Fin.castPred_castSucc {n} (x : Fin n) (h : ((castSucc x : Fin (n + 1)) : ℕ) ≠ n) :
    castPred (castSucc x) h = x := by
  sorry

def SetTheory.Set.Fin.last (n : ℕ) : Fin (n + 1) := Fin_mk _ n (by omega)

theorem SetTheory.Set.card_iUnion_card_disjoint {n m: ℕ} {S : Fin n → Set}
    (hSc : ∀ i, (S i).has_card m)
    (hSd : Pairwise fun i j => Disjoint (S i) (S j)) :
    ((Fin n).iUnion S).finite ∧ ((Fin n).iUnion S).card = n * m := by
  sorry

noncomputable def SetTheory.Set.Fin.predAbove {n} (i : Fin (n + 1)) (x : Fin (n + 1)) (h : x ≠ i) : Fin n :=
  if hx : (x:ℕ) < i then
    Fin_mk _ (x:ℕ) (by sorry)
  else
    Fin_mk _ ((x:ℕ) - 1) (by sorry)

noncomputable def SetTheory.Set.Fin.succAbove {n} (i : Fin (n + 1)) (x : Fin n) : Fin (n + 1) :=
  if (x:ℕ) < i then
    Fin_embed _ _ (by sorry) x
  else
    Fin_mk _ ((x:ℕ) + 1) (by sorry)

@[simp]
theorem SetTheory.Set.Fin.succAbove_ne {n} (i : Fin (n + 1)) (x : Fin n) : succAbove i x ≠ i := by
  sorry

@[simp]
theorem SetTheory.Set.Fin.succAbove_predAbove {n} (i : Fin (n + 1)) (x : Fin (n + 1)) (h : x ≠ i) :
    (succAbove i) (predAbove i x h) = x := by
  sorry

@[simp]
theorem SetTheory.Set.Fin.predAbove_succAbove {n} (i : Fin (n + 1)) (x : Fin n) :
    (predAbove i) (succAbove i x) (succAbove_ne i x) = x := by
  sorry
-- </CONTEXT>
-- <TARGET_THEOREM>
theorem SetTheory.Set.Permutations_ih (n: ℕ):
    (Permutations (n + 1)).card = (n + 1) * (Permutations n).card := by
-- </TARGET_THEOREM>

end Chapter3