import Mathlib.Tactic

/-!
Self-contained slice for Chapter 3, minimal context to state Set product and EqualCard,
plus the target theorem. We avoid importing any Analysis.Section_* modules.
-/

namespace Chapter3

universe u v

/-- The axioms of Zermelo-Frankel theory with atoms (from Section 3.1). -/
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]

/-- Definition 3.1.1 (objects can be elements of sets). -/
instance SetTheory.objects_mem_sets : Membership Object Set where
  mem X x := SetTheory.mem x X

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

namespace SetTheory
namespace Set

/-- Axiom 3.2 (Equality of sets) with `[ext]`. -/
@[ext]
 theorem ext {X Y:Set} (h: ∀ x, x ∈ X ↔ x ∈ Y) : X = Y :=
  SetTheory.extensionality _ _ h

/-- Coerce a Chapter3 set to a type (as a subtype of Object). -/
abbrev toSubtype (A : Set) := Subtype (fun x ↦ x ∈ A)

end Set
end SetTheory

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

namespace SetTheory
namespace Set

/-- Union instance for sets (pairwise union). -/
instance instUnion : Union Set where
  union := SetTheory.union_pair

/-- Axiom 3.4 (Pairwise union). -/
@[simp]
 theorem mem_union (x:Object) (X Y:Set) : x ∈ (X ∪ Y) ↔ (x ∈ X ∨ x ∈ Y) :=
  SetTheory.union_pair_axiom X Y x

/-- Singleton instance for sets. -/
instance instSingleton : Singleton Object Set where
  singleton := SetTheory.singleton

@[simp]
 theorem mem_singleton (x a:Object) : x ∈ ({a}:Set) ↔ x = a := SetTheory.singleton_axiom x a

/-- Insert instance for sets: `insert b X = {b} ∪ X`. -/
instance instInsert : Insert Object Set where
  insert x X := ({x}:Set) ∪ X

/-- Abbreviation for replacement on sets. -/
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

/-- Replacement axiom specialized to `Set.replace`. -/
@[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

open SetTheory.Set

/-- Definition 3.5.1 (Ordered pair). -/
@[ext]
structure OrderedPair where
  fst: Object
  snd: Object

/-- Helper: embed an ordered pair as an object (Kuratowski-style). -/
def OrderedPair.toObject : OrderedPair ↪ Object where
  toFun p := ({ (({p.fst}:Set):Object), (({p.fst, p.snd}:Set):Object) }:Set)
  inj' := by
    sorry

/-- Coercion from ordered pairs to objects via Kuratowski encoding. -/
instance OrderedPair.inst_coeObject : Coe OrderedPair Object where
  coe := OrderedPair.toObject

/-- Provide a very small `grind` tactic used in the textbook sources. -/
macro "grind" : tactic => `(tactic| aesop)

namespace SetTheory
namespace Set

/-- A technical operation: `{x} × Y` as a set via replacement (Section 3.5). -/
abbrev slice (x:Object) (Y:Set) : Set :=
  Y.replace (P := fun y z ↦ z = (⟨x, y⟩:OrderedPair)) (by grind)

/-- Definition 3.5.4 (Cartesian product) via union and replacement. -/
abbrev cartesian (X Y:Set) : Set :=
  SetTheory.union (X.replace (P := fun x z ↦ z = slice x Y)
    (by intro _ _ _ h; exact h.1.trans h.2.symm))

end Set
end SetTheory

/-- Local notation for the Cartesian product on Chapter3 sets. -/
infixr:70 " ×ˢ " => SetTheory.Set.cartesian

namespace SetTheory
namespace Set

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

end Set
end SetTheory

/-- Exercise 3.6.5. You might find `SetTheory.Set.prod_commutator` useful. -/
 theorem SetTheory.Set.prod_EqualCard_prod (A B:Set) :
    EqualCard (A ×ˢ B) (B ×ˢ A) := by
end Chapter3