import Mathlib.Tactic

/-!
Self-contained extraction for Chapter 3 Section 3.5 target theorem.
-/

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)

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

/-- Axiom 3.1 (Sets are objects)-/
@[simp]
theorem SetTheory.Set.coe_eq_iff (X Y:Set) : (X: Object) = (Y: Object) ↔  X = Y := by
  constructor
  · intro h; exact SetTheory.set_to_object.inj' h
  · rintro rfl; rfl

/-- Axiom 3.2 (Equality of sets).  The `[ext]` tag allows the `ext` tactic to work for sets. -/
@[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

open SetTheory.Set

@[simp]
theorem SetTheory.Set.not_mem_empty : ∀ x, x ∉ (∅:Set) := SetTheory.emptyset_mem

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

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 SetTheory.Set.instInsert : Insert Object Set where
  insert x X := {x} ∪ X

/-- This defines the subtype `A.toSubtype` for any `A:Set`. -/
abbrev SetTheory.Set.toSubtype (A:Set) := Subtype (fun x ↦ x ∈ A)

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

/-- Elements of a set (implicitly coerced to a subtype) are also elements of the set. -/
lemma SetTheory.Set.subtype_property (A:Set) (x:A) : x.val ∈ A := x.property

/-- Axiom 3.6 (axiom of specification) -/
@[simp]
theorem SetTheory.Set.specification_axiom'' {A:Set} (P: A → Prop) (x:Object) :
    x ∈ SetTheory.specify A P ↔ ∃ h:x ∈ A, P ⟨ x, h ⟩ := by
  constructor
  · intro h
    have h' := (SetTheory.specification_axiom A P).1 x h
    refine ⟨h', ?_⟩
    have := (SetTheory.specification_axiom A P).2 ⟨x, h'⟩
    simpa using this.mp (by
      -- show mem x.val (SetTheory.specify A P) from h; but x.val = x
      simpa using h)
  · rintro ⟨h, hP⟩
    have := (SetTheory.specification_axiom A P).2 ⟨x, h⟩
    simpa using this.mpr hP

/-- Replacement packaged for sets. -/
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

/-- Axiom 3.7 (Axiom of replacement) -/
@[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

/-- Axiom 3.12 (Union) -/
@[simp]
lemma SetTheory.Set.union_axiom (A: Set) (x:Object) :
    x ∈ SetTheory.union A ↔ ∃ (S:Set), x ∈ S 
      ∧ (S:Object) ∈ A :=
  SetTheory.union_axiom A x

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

/-- 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 := OrderedPair.toObject

/--
  A technical operation, turning a object `x` and a set `Y` to a set `{x} × Y`, needed to define
  the full Cartesian product
-/
abbrev SetTheory.Set.slice (x:Object) (Y:Set) : Set :=
  Y.replace (P := fun y z ↦ z = (⟨x, y⟩:OrderedPair)) (by
    intro a b b' h; exact h.1.trans h.2.symm)

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

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

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

@[simp]
theorem SetTheory.Set.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 hx
    rcases hx with ⟨S, hzS, hS⟩
    -- unpack hS via replacement on X
    have hxrep := (SetTheory.Set.replacement_axiom
      (A:=X) (P:= fun x z ↦ z = slice x Y)
      (by intro a b b' h; exact h.1.trans h.2.symm) (y:=(S: _))).mp hS
    rcases hxrep with ⟨x, hx⟩
    -- hx : S = slice x Y
    have hSeq : S = slice x Y := by
      -- hx is equality in Object; convert to Set equality
      exact (SetTheory.set_to_object.inj' hx)
    subst hSeq
    -- now hzS : z ∈ slice x Y
    have hz' := (SetTheory.Set.replacement_axiom
      (A:=Y) (P:= fun y z ↦ z = (⟨x, y⟩:OrderedPair))
      (by intro a b b' h; exact h.1.trans h.2.symm) (y:=z)).mp hzS
    rcases hz' with ⟨y, hy⟩
    rcases hy with rfl
    exact ⟨x, ⟨y, rfl⟩⟩
  · rintro ⟨x, y, rfl⟩
    -- show (⟨x, y⟩ : OrderedPair) ∈ union (X.replace ...)
    -- take S := slice x Y
    refine ⟨slice x Y, ?_, ?_⟩
    · -- membership in slice
      exact (SetTheory.Set.replacement_axiom
        (A:=Y) (P:= fun y z ↦ z = (⟨x, y⟩:OrderedPair))
        (by intro a b b' h; exact h.1.trans h.2.symm) (y:=(⟨x, y⟩:OrderedPair))).mpr
        ⟨y, rfl⟩
    · -- show (slice x Y : Object) ∈ X.replace ...
      have := (SetTheory.Set.replacement_axiom
        (A:=X) (P:= fun x z ↦ z = slice x Y)
        (by intro a b b' h; exact h.1.trans h.2.symm) (y:=(slice x Y : _))).mpr
      exact this ⟨x, rfl⟩

/-- Projections from a Cartesian product. -/
noncomputable abbrev SetTheory.Set.fst {X Y:Set} (z:X ×ˢ Y) : X :=
  ((SetTheory.Set.mem_cartesian _ _ _).mp z.property).choose

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

/-- Exercise 3.5.7 -/
 theorem SetTheory.Set.direct_sum {X Y Z:Set} (f: Z → X) (g: Z → Y) :
    ∃! h: Z → X ×ˢ Y, fst ∘ h = f ∧ snd ∘ h = g := by
end Chapter3