import Mathlib.Tactic
set_option diagnostics true

namespace Chapter3

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)

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 :=
  SetTheory.extensionality _ _ h

namespace SetTheory
namespace Set

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

end Set
end SetTheory

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

namespace SetTheory
namespace Set

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

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

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

@[simp]
 theorem specification_axiom'' {A : Set} (P : A → Prop) (x : Object) :
    x ∈ A.specify P ↔ ∃ h : x ∈ A, P ⟨x, h⟩ := by
  constructor
  · intro h; exact ⟨specification_axiom h, by
      have := (SetTheory.specification_axiom A P).2
      simpa [specify] using (this ⟨x, specification_axiom h⟩).mp h⟩
  · rintro ⟨h, hP⟩
    have := (SetTheory.specification_axiom A P).2
    simpa [specify] using (this ⟨x, h⟩).mpr hP

abbrev 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 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 :=
  SetTheory.replacement_axiom A P hP y

end Set
end SetTheory

abbrev Nat := SetTheory.nat

namespace SetTheory
namespace Set

def nat_equiv : ℕ ≃ Nat := SetTheory.nat_equiv

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

instance instNatCast : NatCast Nat where
  natCast n := nat_equiv n

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

end Set
end SetTheory

namespace SetTheory
namespace Object

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

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

end Object
end SetTheory

namespace SetTheory
namespace Set

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

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

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

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

end Set
end SetTheory

namespace SetTheory
namespace Set

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

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

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

end Set
end SetTheory

namespace SetTheory
namespace Set

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

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

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

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

instance Set.EqualCard.inst_setoid : Setoid SetTheory.Set := by
  classical
  refine
    { r := EqualCard
      iseqv := ?_ }
  refine
    { refl := ?_ , symm := ?_ , trans := ?_ }
  · intro X; exact Set.EqualCard.refl X
  · intro X Y h; exact Set.EqualCard.symm h
  · intro X Y Z h1 h2; exact Set.EqualCard.trans h1 h2

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

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

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

end Set
end SetTheory

namespace SetTheory
namespace Set

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

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

noncomputable def 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 Permutations_bijective {n : ℕ} (p : Permutations n) :
    Function.Bijective (Permutations_toFun p) := by sorry

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

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

end Set
end SetTheory

open SetTheory SetTheory.Set

/-- Exercise 3.6.12 (i), second part -/
theorem SetTheory.Set.Permutations_ih (n: ℕ):
    (Permutations (n + 1)).card = (n + 1) * (Permutations n).card := by
  let S i := (Permutations (n + 1)).specify (fun p ↦ perm_equiv_equiv p (Fin.last n) = i)

  have hSe : ∀ i, S i ≈ Permutations n := by
end Chapter3