import Mathlib.Tactic

/-!
Self-contained slice for Chapter 3 minimal context to typecheck the target theorem
-/

namespace Chapter3

universe u v

/-- The axioms of Zermelo-Frankel theory with atoms. -/
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

/-- Coercion of sets to objects. -/
instance SetTheory.sets_are_objects : Coe Set Object where
  coe X := SetTheory.set_to_object X

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

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

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

/-- Abbreviation for specification on sets. -/
abbrev SetTheory.Set.specify (A:Set) (P: A → Prop) : Set := SetTheory.specify A P

/-- Use the set of natural numbers from the set theory axioms as a set. -/
abbrev Nat := SetTheory.nat

/-- Axiom 3.8 (Axiom of infinity): bijection with Lean naturals. -/
def SetTheory.Set.nat_equiv : ℕ ≃ Nat := SetTheory.nat_equiv

/-- Provide numerals for `Nat` (elements of the set of naturals). -/
instance SetTheory.Set.instOfNat {n:ℕ} : OfNat Nat n where
  ofNat := SetTheory.Set.nat_equiv n

/-- Cast ℕ to `Nat`. -/
instance SetTheory.Set.instNatCast : NatCast Nat where
  natCast n := SetTheory.Set.nat_equiv n

/-- Cast `Nat` to ℕ. -/
instance SetTheory.Set.toNat : Coe Nat ℕ where
  coe n := (SetTheory.Set.nat_equiv).symm n

/-- Cast ℕ to `Object`. -/
instance SetTheory.Object.instNatCast : NatCast Object where
  natCast n := ((n:Nat):Object)

/-- Some simp lemma used later in other parts. -/
@[simp]
lemma SetTheory.Object.ofnat_eq {n:ℕ} : ((n:Nat):Object) = (n:Object) := rfl

/-- Another simp lemma from the source. -/
@[simp]
lemma SetTheory.Object.natCast_inj (n m:ℕ) :
    (n : Object) = (m : Object) ↔ n = m := by
  -- This proof in the original file relies on more API; we can leave it as sorry.
  -- It is not needed for the target theorem to typecheck.
  sorry

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

/-- Reflexivity of equal cardinality. -/
@[refl]
theorem SetTheory.Set.EqualCard.refl (X:Set) : SetTheory.Set.EqualCard X X := by
  sorry

/-- Symmetry of equal cardinality. -/
@[symm]
theorem SetTheory.Set.EqualCard.symm {X Y:Set}
    (h: SetTheory.Set.EqualCard X Y) : SetTheory.Set.EqualCard Y X := by
  sorry

/-- Transitivity of equal cardinality. -/
@[trans]
theorem SetTheory.Set.EqualCard.trans {X Y Z:Set}
    (h1: SetTheory.Set.EqualCard X Y) (h2: SetTheory.Set.EqualCard Y Z) :
    SetTheory.Set.EqualCard X Z := by
  sorry

/-- Make equal cardinality into a setoid to support the `≈` notation. -/
instance SetTheory.Set.EqualCard.inst_setoid : Setoid SetTheory.Set where
  r := SetTheory.Set.EqualCard
  iseqv := ⟨ SetTheory.Set.EqualCard.refl, SetTheory.Set.EqualCard.symm, SetTheory.Set.EqualCard.trans ⟩

/-- The finite set `{0,1,...,n-1}` inside set theory. -/
abbrev SetTheory.Set.Fin (n : ℕ) : Set :=
  (Nat).specify (fun m ↦ ((m : ℕ) < n))

/-- Cardinality of a set as equivalence to `Fin n`. -/
abbrev SetTheory.Set.has_card (X:Set) (n:ℕ) : Prop := X ≈ SetTheory.Set.Fin n

/-- Example 3.6.7(a) from the text. -/
-- Target theorem to be proved (left as sorry as per instructions).
 theorem SetTheory.Set.Example_3_6_7a (a:Object) : ({a}:Set).has_card 1 := by
end Chapter3