(** Let (X, A, a) be a pointed graph.  The powerset of the set represented
    by this pointed graph is itself represented by the pointed graph

      ((sum X X->Prop), (POWER X A a), (out X X->Prop))

    whose edge relation (POWER X A a) : (Rel (sum X X->Prop)) is defined
    by the following 3 clauses:

    1. Delocating A into the powerset:

         if (A x' x),
         then (POWER X A a (inl X X->Prop x') (inl X X->Prop x))

    2. Connecting each vertex x':X such that (A x' a) to all the
       predicates p : X->Prop such that (p x'):

         if (A x' a) and (p x'),
         then (POWER X A a (inl X X->Prop x') (inr X X->Prop p))

    3. Connecting all the predicates p : X->Prop to the new root:

         (POWER X A a (inr X X->Prop p) (out X X->Prop)).

    This relation is impredicatively encoded as follows: *)

Definition POWER (X : Typ1) (A : Rel X) (a : X) (z' z : sum X (X -> Prop)) :=
  forall E : Prop,
  (forall x x' : X,
   eq (sum X (X -> Prop)) z' (inl X (X -> Prop) x') ->
   eq (sum X (X -> Prop)) z (inl X (X -> Prop) x) -> A x' x -> E) ->
  (forall (x' : X) (p : X -> Prop),
   eq (sum X (X -> Prop)) z' (inl X (X -> Prop) x') ->
   eq (sum X (X -> Prop)) z (inr X (X -> Prop) p) -> A x' a -> p x' -> E) ->
  (forall p : X -> Prop,
   eq (sum X (X -> Prop)) z' (inr X (X -> Prop) p) ->
   eq (sum X (X -> Prop)) z (out X (X -> Prop)) -> E) -> E.

(* Specifications: *)
Lemma POWER_in1 : forall (X : Typ1) (A : Rel X) (a x x' : X), A x' x -> POWER X A a (inl X (X -> Prop) x') (inl X (X -> Prop) x).
Lemma POWER_in2 : forall (X : Typ1) (A : Rel X) (a x' : X) (p : X -> Prop), A x' a -> p x' -> POWER X A a (inl X (X -> Prop) x') (inr X (X -> Prop) p).
Lemma POWER_rt : forall (X : Typ1) (A : Rel X) (a : X) (p : X -> Prop), POWER X A a (inr X (X -> Prop) p) (out X (X -> Prop)).
Lemma POWER_deloc : forall (X : Typ1) (A : Rel X) (a : X), deloc X A (sum X (X -> Prop)) (POWER X A a) (inl X (X -> Prop)).
Lemma POWER_eqv : forall (X : Typ1) (A : Rel X) (a x : X), EQV X A x (sum X (X -> Prop)) (POWER X A a) (inl X (X -> Prop) x).
Lemma POWER_subset_eqv : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y), SUB Y B b X A a -> EQV Y B b (sum X (X -> Prop)) (POWER X A a) (inr X (X -> Prop) (fun x' => and (A x' a) (ELT X A x' Y B b))).
Lemma powerset_intro : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y), SUB Y B b X A a -> ELT Y B b (sum X (X -> Prop)) (POWER X A a) (out X (X -> Prop)).
Lemma powerset_elim : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y), ELT Y B b (sum X (X -> Prop)) (POWER X A a) (out X (X -> Prop)) -> SUB Y B b X A a.
Theorem powerset : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y), iff (ELT Y B b (sum X (X -> Prop)) (POWER X A a) (out X (X -> Prop))) (SUB Y B b X A a).