import Mathlib.Tactic

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

namespace Chapter3

/- From Section 3.1: core set-theory class and basic instances -/

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]

/-- Objects can be members of sets. -/
instance SetTheory.objects_mem_sets : Membership Object Set where
  mem X x := SetTheory.mem x X

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

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

-- Abbreviations and basic coercions to work with subtypes of objects in a set
abbrev SetTheory.Set.toSubtype (A:Set) := Subtype (fun x ↦ x ∈ A)

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

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

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

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

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

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

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

/-- Alias for the axiom of union. -/
 theorem SetTheory.Set.union_axiom (A: Set) (x:Object) :
    x ∈ SetTheory.union A ↔ ∃ (S:Set), x ∈ S ∧ (S:Object) ∈ A := SetTheory.union_axiom A x

/- From Section 3.5: ordered pairs and cartesian product -/

namespace OrderedPair

@[ext]
structure OrderedPair where
  fst: Object
  snd: Object

end OrderedPair

open OrderedPair

/-- Definition 3.5.1 (Ordered pair). -/
@[simp]
 theorem OrderedPair.eq (x y x' y' : Object) :
    (⟨ x, y ⟩ : OrderedPair) = (⟨ x', y' ⟩ : OrderedPair) ↔ x = x' ∧ y = y' := by aesop

/-- Exercise 3.5.1, first part -/
def OrderedPair.toObject : OrderedPair ↪ Object where
  toFun p := ({ (({p.fst}:Set):Object), (({p.fst, p.snd}:Set):Object) }:Set)
  inj' := by sorry

instance OrderedPair.inst_coeObject : Coe OrderedPair Object where
  coe := toObject

namespace SetTheory.Set

/-- A technical operation used to define cartesian product. -/
abbrev slice (x:Object) (Y:Set) : Set :=
  Y.replace (P := fun y z ↦ z = (⟨x, y⟩:OrderedPair)) (by grind)

@[simp]
 theorem mem_slice (x z:Object) (Y:Set) :
    z ∈ (slice x Y) ↔ ∃ y:Y, z = (⟨x, y⟩:OrderedPair) := replacement_axiom _ _

/-- Cartesian product -/
abbrev cartesian (X Y:Set) : Set :=
  SetTheory.union (X.replace (P := fun x z ↦ z = slice x Y) (by intro _ _ _ ⟨h1, h2⟩; exact h1.trans h2.symm))

/-- This instance enables the ×ˢ notation for Cartesian product. -/
instance inst_SProd : SProd Set Set Set where
  sprod := cartesian

@[simp]
 theorem mem_cartesian (z:Object) (X Y:Set) :
    z ∈ X ×ˢ Y ↔ ∃ x:X, ∃ y:Y, z = (⟨x, y⟩:OrderedPair) := by
  simp only [SProd.sprod, SetTheory.Set.union_axiom]; constructor
  · intro ⟨ S, hz, hS ⟩; rw [SetTheory.Set.replacement_axiom] at hS; obtain ⟨ x, hx ⟩ := hS
    use x; simp_all
  rintro ⟨ x, y, rfl ⟩; use slice x Y; refine ⟨ by simp, ?_ ⟩
  rw [SetTheory.Set.replacement_axiom]; use x

noncomputable abbrev fst {X Y:Set} (z:X ×ˢ Y) : X :=
  ((mem_cartesian _ _ _).mp z.property).choose

noncomputable abbrev snd {X Y:Set} (z:X ×ˢ Y) : Y :=
  (exists_comm.mp ((mem_cartesian _ _ _).mp z.property)).choose

end SetTheory.Set

/-- Indexed union (from Section 3.4) -/
abbrev SetTheory.Set.iUnion (I: Set) (A: I → Set) : Set :=
  SetTheory.union (I.replace (P := fun α S ↦ S = A α) (by intro _ _ _ ⟨h1, h2⟩; exact h1.trans h2.symm))

-- TARGET THEOREM (Exercise 3.5.9)
 /-- Exercise 3.5.9 -/
 theorem SetTheory.Set.iUnion_inter_iUnion {I J: Set} (A: I → Set) (B: J → Set) :
    (iUnion I A) ∩ (iUnion J B) = iUnion (I ×ˢ J) (fun p ↦ (A (SetTheory.Set.fst p)) ∩ (B (SetTheory.Set.snd p))) := by
end Chapter3