import Mathlib.Tactic
import Mathlib.Data.Nat.Factorial

/-!
Self-contained slice for Chapter 3 Section 3.6 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 nat)

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

/-- 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

/-- Axiom 3.6 (axiom of specification) -/
abbrev SetTheory.Set.specify (A:Set) (P: A → Prop) : Set := SetTheory.specify A P

/-- Axiom 3.8 (Axiom of infinity) -/
abbrev Nat := SetTheory.nat

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

-- Coerce elements of the set `Nat` to natural numbers.
instance SetTheory.Set.toNat : Coe Nat ℕ where
  coe n := SetTheory.Set.nat_equiv.symm n

-- (not strictly needed, but mirrors Section 3.1)
instance SetTheory.Set.instNatCast : NatCast Nat where
  natCast n := SetTheory.Set.nat_equiv n

/-- Provide `^` on sets via the `pow` field from the axioms. -/
instance SetTheory.Set.inst_pow : Pow Set Set where pow := SetTheory.pow

namespace SetTheory.Set

/-- `Fin n` as a set, following Section 3.6. -/
abbrev Fin (n : ℕ) : Set :=
  nat.specify (fun m ↦ (m : ℕ) < n)

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

@[refl]
 theorem EqualCard.refl (X:Set) : EqualCard X X := by
  sorry

@[symm]
 theorem EqualCard.symm {X Y:Set} (h: EqualCard X Y) : EqualCard Y X := by
  sorry

@[trans]
 theorem EqualCard.trans {X Y Z:Set} (h1: EqualCard X Y) (h2: EqualCard Y Z) : EqualCard X Z := by
  sorry

/-- Proposition 3.6.4 / Exercise 3.6.1 -/
instance EqualCard.inst_setoid : Setoid SetTheory.Set := ⟨ EqualCard, {refl, symm, trans} ⟩

/-- Definition 3.6.5 -/
abbrev has_card (X:Set) (n:ℕ) : Prop := X ≈ Fin n

/-- `finite` in terms of `has_card`. -/
abbrev finite (X:Set) : Prop := ∃ n:ℕ, X.has_card n

open Classical
/-- It is convenient for Lean purposes to give infinite sets the ``junk`` cardinality of zero. -/
noncomputable def card (X:Set) : ℕ := if h:X.finite then h.choose else 0

/-- Pow-to-function equivalence, as in Section 3.6. -/
noncomputable def pow_fun_equiv {A B : Set} : ↑(A ^ B) ≃ (B → A) where
  toFun := sorry
  invFun := sorry
  left_inv := sorry
  right_inv := sorry

/-- Exercise 3.6.12 -/
 def Permutations (n: ℕ): Set := (Fin n ^ Fin n).specify (fun F ↦
    Function.Bijective (pow_fun_equiv F))

end SetTheory.Set

open SetTheory.Set

namespace SetTheory.Set

/-- Exercise 3.6.12 (ii) -/
 theorem Permutations_card (n: ℕ):
    (Permutations n).card = n.factorial := by
end SetTheory.Set

end Chapter3