{-# OPTIONS --safe #-}

module Data.Sigma.Subset where

open import Prelude

Subset : (A : Type a) (P : A  Type p)  Type (a ℓ⊔ p)
Subset A P = Σ[ x  A ] ×  P x 

Subset/ : (A : Type a) (P : A  Type p)  Type (a ℓ⊔ p)
Subset/ A P = (Σ[ x  A ] × P x) / λ x y  fst x  fst y

subset-quot : Subset A P  Subset/ A P
subset-quot (x , ∥Px∥) = ∥rec∥→Set squash/  Px  [ x , Px ])  Px₁ Px₂  eq/ (x , Px₁) (x , Px₂) refl) ∥Px∥

module _ (set : isSet A) where
  subset-iso : Subset A P  Subset/ A P
  subset-iso .fun = subset-quot
  subset-iso .inv = rec/ (isSetΣ set λ _  isProp→isSet squash) (map₂ ∣_∣) λ _ _  Σ≡Prop λ _  squash 
  subset-iso .rightInv = elimProp/  _  squash/ _ _)  _  refl)
  subset-iso .leftInv (x , Px) =
    ∥elim∥
      {P = λ Px  subset-iso .inv (subset-quot (x , Px))  (x , Px)}
       _  λ p q  isSetΣ set  _  isProp→isSet squash) _ _ p q )
       _  refl)
      Px