import Mathlib.Tactic

/-!
Self-contained slice for Chapter 3 Section 3.6 target theorem
-/

namespace Chapter3

universe u v

/-- ZF-style axioms used throughout Chapter 3. -/
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 : Set) (P : Subtype (mem . A) → Prop) : Set
  specification_axiom (A : Set) (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 : Set) (P : Subtype (mem . A) → Object → Prop)
    (hP : ∀ x y y', P x y ∧ P x y' → y = y') : Set
  replacement_axiom (A : Set) (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]

/-- Objects are elements of sets. -/
instance SetTheory.objects_mem_sets : Membership Object Set where
  mem X x := SetTheory.mem x X

/-- Sets are objects. -/
instance SetTheory.sets_are_objects : Coe Set Object where
  coe X := SetTheory.set_to_object X

/-- Coerce a set to a type (its subtype of elements). -/
abbrev SetTheory.Set.toSubtype (A : Set) := Subtype (fun x ↦ x ∈ A)

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

/-- Empty set notation for Chapter3.Set. -/
instance SetTheory.Set.instEmpty : EmptyCollection Set where
  emptyCollection := SetTheory.emptyset

/-- A convenient abbreviation for specification in the Set namespace. -/
abbrev SetTheory.Set.specify (A : Set) (P : A → Prop) : Set := SetTheory.specify A P

/-- Alias for the Chapter-3 naturals set. -/
abbrev Nat := SetTheory.nat

/-- The equivalence between ℕ and the Chapter-3 naturals set. -/
def SetTheory.Set.nat_equiv : ℕ ≃ Nat := SetTheory.nat_equiv

/-- Coerce a Chapter-3 natural to ℕ. -/
instance SetTheory.Set.toNat : Coe Nat ℕ where
  coe n := nat_equiv.symm n

/-- Definition 3.6.1 (Equal cardinality). -/
abbrev SetTheory.Set.EqualCard (X Y : Set) : Prop := ∃ f : X → Y, Function.Bijective f

@[refl]
theorem SetTheory.Set.EqualCard.refl (X : Set) : EqualCard X X := by
  -- original proof replaced to keep snippet minimal
  sorry

@[symm]
theorem SetTheory.Set.EqualCard.symm {X Y : Set} (h : EqualCard X Y) : EqualCard Y X := by
  -- original proof replaced to keep snippet minimal
  sorry

@[trans]
theorem SetTheory.Set.EqualCard.trans {X Y Z : Set}
    (h1 : EqualCard X Y) (h2 : EqualCard Y Z) : EqualCard X Z := by
  -- original proof replaced to keep snippet minimal
  sorry

/-- Proposition 3.6.4 / Exercise 3.6.1: EqualCard is an equivalence relation. -/
instance SetTheory.Set.EqualCard.inst_setoid : Setoid SetTheory.Set :=
  ⟨ SetTheory.Set.EqualCard,
    ⟨ SetTheory.Set.EqualCard.refl,
      SetTheory.Set.EqualCard.symm,
      SetTheory.Set.EqualCard.trans ⟩ ⟩

/-- A Chapter-3-style Fin n set. -/
abbrev SetTheory.Set.Fin (n : ℕ) : Set := nat.specify (fun m ↦ (m : ℕ) < n)

/-- Definition 3.6.5: has_cardinality n. -/
abbrev SetTheory.Set.has_card (X : Set) (n : ℕ) : Prop := X ≈ SetTheory.Set.Fin n

/-- Exercise 3.6.2a -/
theorem SetTheory.Set.has_card_zero {X : Set} : X.has_card 0 ↔ X = ∅ := by
end Chapter3