import Mathlib.Tactic

namespace Chapter3

universe u v

-- The axioms of Zermelo-Frankel theory with atoms.
class SetTheory where
  Set : Type u -- Axiom 3.1
  Object : Type v -- Axiom 3.1
  set_to_object : Set ↪ Object -- Axiom 3.1
  mem : Object → Set → Prop -- Axiom 3.1
  extensionality X Y : (∀ x, mem x X ↔ mem x Y) → X = Y -- Axiom 3.2
  emptyset: Set -- Axiom 3.3
  emptyset_mem x : ¬ mem x emptyset -- Axiom 3.3
  singleton : Object → Set -- Axiom 3.4
  singleton_axiom x y : mem x (singleton y) ↔ x = y -- Axiom 3.4
  union_pair : Set → Set → Set -- Axiom 3.5
  union_pair_axiom X Y x : mem x (union_pair X Y) ↔ (mem x X ∨ mem x Y) -- Axiom 3.5
  specify A (P: Subtype (mem . A) → Prop) : Set -- Axiom 3.6
  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 -- Axiom 3.6
  replace A (P: Subtype (mem . A) → Object → Prop)
    (hP: ∀ x y y', P x y ∧ P x y' → y = y') : Set -- Axiom 3.7
  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 -- Axiom 3.7
  nat : Set -- Axiom 3.8
  nat_equiv : ℕ ≃ Subtype (mem . nat) -- Axiom 3.8
  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 -- Axiom 3.9
  pow : Set → Set → Set -- Axiom 3.11
  function_to_object (X: Set) (Y: Set) :
    (Subtype (mem . X) → Subtype (mem . Y)) ↪ Object -- Axiom 3.11
  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 -- Axiom 3.11
  union : Set → Set -- Axiom 3.12
  union_axiom A x : mem x (union A) ↔ ∃ S, mem x S ∧ mem (set_to_object S) A -- Axiom 3.12

export SetTheory (Set Object nat)

variable [SetTheory]

-- Allow writing x ∈ X for x:Object and X:Set
instance SetTheory.objects_mem_sets : Membership Object Set where
  mem X x := SetTheory.mem x X

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

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

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

@[simp]
theorem SetTheory.Set.mem_union (x:Object) (X Y:Set) : x ∈ (X ∪ Y) ↔ (x ∈ X ∨ x ∈ Y) :=
  SetTheory.union_pair_axiom X Y x

instance : CoeSort (Set) (Type v) where
  coe A := Subtype (fun x ↦ x ∈ A)

-- Axiom 3.8 (Axiom of infinity)
def SetTheory.Set.nat_equiv : ℕ ≃ SetTheory.nat := SetTheory.nat_equiv

-- Treat `Nat` as alias for the set of naturals in the set theory
abbrev Nat := SetTheory.nat

-- Coerce elements of Nat to ℕ via the equivalence
instance SetTheory.Set.toNat : Coe Nat ℕ where
  coe n := SetTheory.Set.nat_equiv.symm n

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

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

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

@[refl]
theorem SetTheory.Set.EqualCard.refl (X:Set) : SetTheory.Set.EqualCard X X := by
  refine ⟨id, ?_⟩; exact Function.bijective_id

@[symm]
theorem SetTheory.Set.EqualCard.symm {X Y:Set} (h: SetTheory.Set.EqualCard X Y) :
    SetTheory.Set.EqualCard Y X := by
  rcases h with ⟨f, hf⟩
  refine ⟨(Equiv.ofBijective f hf).symm, ?_⟩
  simpa using (Equiv.ofBijective f hf).symm.bijective

@[trans]
theorem SetTheory.Set.EqualCard.trans {X Y Z:Set}
    (h1: SetTheory.Set.EqualCard X Y) (h2: SetTheory.Set.EqualCard Y Z) :
    SetTheory.Set.EqualCard X Z := by
  rcases h1 with ⟨f, hf⟩; rcases h2 with ⟨g, hg⟩
  refine ⟨fun x => g (f x), ?_⟩
  exact hg.comp hf

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

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

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

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

-- Proposition 3.6.14 (b) / Exercise 3.6.4
 theorem SetTheory.Set.card_union {X Y:Set} (hX: X.finite) (hY: Y.finite) :
    (X ∪ Y).finite ∧ (X ∪ Y).card ≤ X.card + Y.card := by
end Chapter3