import Mathlib.Tactic

/-!
Self-contained slice for Chapter 3 (minimal context for Section 3.5 target).

We only include the parts of Section 3.1 needed to state the target theorem,
plus a lightweight product-notation mechanism so that `×ˢ` parses on `Set`.
-/

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]

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

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

/-- Empty set notation on Chapter3.Set (from Section 3.1). -/
instance SetTheory.Set.instEmpty : EmptyCollection Set where
  emptyCollection := SetTheory.emptyset

/-- Subset relation on Chapter3.Set (from Section 3.1). -/
instance SetTheory.Set.instSubset : HasSubset Set where
  Subset X Y := ∀ x, x ∈ X → x ∈ Y

/-
Provide a lightweight product-notation mechanism so that `×ˢ` parses on `Set`.
This mimics the notation layer used in the textbook project.
-/

/-- A minimal typeclass providing a binary operation for the `×ˢ` notation. -/
class SProd (α : Sort*) (β : Sort*) (γ : outParam (Sort*)) where
  sprod : α → β → γ

infixl:70 " ×ˢ " => SProd.sprod

/-- A placeholder Cartesian product on Chapter3.Set to support the `×ˢ` notation.
This is only to make the target theorem parse and typecheck. -/
def SetTheory.Set.fakeProd (A B : Set) : Set := A

/-- Instance enabling `×ˢ` on Chapter3.Set. -/
instance SetTheory.Set.inst_SProd : SProd Set Set Set where
  sprod := SetTheory.Set.fakeProd

/--
  Exercise 3.5.6 (target theorem).
  We keep the exact statement and end with `by sorry` as required.
-/
theorem SetTheory.Set.prod_subset_prod {A B C D:Set}
  (hA: A ≠ ∅) (hB: B ≠ ∅) (hC: C ≠ ∅) (hD: D ≠ ∅) :
    A ×ˢ B ⊆ C ×ˢ D ↔ A ⊆ C ∧ B ⊆ D := by
end Chapter3