{-

This file introduces the "powerset" of a type in the style of
Escardó's lecture notes:

https://www.cs.bham.ac.uk/~mhe/HoTT-UF-in-Agda-Lecture-Notes/HoTT-UF-Agda.html#propositionalextensionality

-}
{-# OPTIONS --safe #-}
module Cubical.Foundations.Powerset where

open import Cubical.Foundations.Prelude
open import Cubical.Foundations.Equiv
open import Cubical.Foundations.HLevels
open import Cubical.Foundations.Isomorphism
open import Cubical.Foundations.Structure
open import Cubical.Foundations.Function
open import Cubical.Foundations.Univalence using (hPropExt)

open import Cubical.Data.Sigma

private
  variable
     : Level
    X : Type 

 : Type   Type (ℓ-suc )
 X = X  hProp _

isSetℙ : isSet ( X)
isSetℙ = isSetΠ λ x  isSetHProp

infix 5 _∈_

_∈_ : {X : Type }  X   X  Type 
x  A =  A x 

_⊆_ : {X : Type }   X   X  Type 
A  B =  x  x  A  x  B

∈-isProp : (A :  X) (x : X)  isProp (x  A)
∈-isProp A = snd  A

⊆-isProp : (A B :  X)  isProp (A  B)
⊆-isProp A B = isPropΠ2  x _  ∈-isProp B x)

⊆-refl : (A :  X)  A  A
⊆-refl A x = idfun (x  A)

subst-∈ : (A :  X) {x y : X}  x  y  x  A  y  A
subst-∈ A = subst (_∈ A)

⊆-refl-consequence : (A B :  X)  A  B  (A  B) × (B  A)
⊆-refl-consequence A B p = subst (A ⊆_) p (⊆-refl A)
                         , subst (B ⊆_) (sym p) (⊆-refl B)

⊆-extensionality : (A B :  X)  (A  B) × (B  A)  A  B
⊆-extensionality A B (φ , ψ) =
  funExt  x  TypeOfHLevel≡ 1 (hPropExt (A x .snd) (B x .snd) (φ x) (ψ x)))

⊆-extensionalityEquiv : (A B :  X)  (A  B) × (B  A)  (A  B)
⊆-extensionalityEquiv A B = isoToEquiv (iso (⊆-extensionality A B)
                                            (⊆-refl-consequence A B)
                                             _  isSetℙ A B _ _)
                                             _  isPropΣ (⊆-isProp A B)  _  ⊆-isProp B A) _ _))