(* This program is free software; you can redistribute it and/or      *)
(* modify it under the terms of the GNU Lesser General Public License *)
(* as published by the Free Software Foundation; either version 2.1   *)
(* of the License, or (at your option) any later version.             *)
(*                                                                    *)
(* This program is distributed in the hope that it will be useful,    *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of     *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      *)
(* GNU General Public License for more details.                       *)
(*                                                                    *)
(* You should have received a copy of the GNU Lesser General Public   *)
(* License along with this program; if not, write to the Free         *)
(* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *)
(* 02110-1301 USA                                                     *)


Require Import IZF_logic.
Require Import IZF_base.

(** 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.

(** Introduction rules corresponding to the clauses: *)

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).

Proof
  fun X A a x x' H E e _ _ =>
  e x x' (eq_refl (sum X (X -> Prop)) (inl X (X -> Prop) x'))
    (eq_refl (sum X (X -> Prop)) (inl X (X -> Prop) x)) H.

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).

Proof
  fun X A a x' p H1 H2 E _ e _ =>
  e x' p (eq_refl (sum X (X -> Prop)) (inl X (X -> Prop) x'))
    (eq_refl (sum X (X -> Prop)) (inr X (X -> Prop) p)) H1 H2.

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)).

Proof
  fun X A a p E _ _ e =>
  e p (eq_refl (sum X (X -> Prop)) (inr X (X -> Prop) p))
    (eq_refl (sum X (X -> Prop)) (out X (X -> Prop))).

(** We now prove that the left injection (inl X X->Prop) is a delocation
    from (X, A) to ((sum X X->Prop), (POWER X A a)), and deduce the
    expected property of bisimilarity. *)

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)).

Proof.
(* Goal: forall (X : Typ1) (A : Rel X) (a : X), deloc X A (sum X (forall _ : X, Prop)) (POWER X A a) (inl X (forall _ : X, Prop)) *)
intros X A a; unfold deloc in |- *; apply and_intro.
(* Deloc 1 *)
(* Goal: forall (x x' : X) (_ : A x' x), POWER X A a (inl X (forall _ : X, Prop) x') (inl X (forall _ : X, Prop) x) *)
(* Goal: forall (x : X) (y' : sum X (forall _ : X, Prop)) (_ : POWER X A a y' (inl X (forall _ : X, Prop) x)), ex2 X (fun x' : X => A x' x) (fun x' : X => eq (sum X (forall _ : X, Prop)) y' (inl X (forall _ : X, Prop) x')) *)
exact (POWER_in1 X A a).
(* Deloc 2 (case distinction) *)
(* Goal: forall (x : X) (y' : sum X (forall _ : X, Prop)) (_ : POWER X A a y' (inl X (forall _ : X, Prop) x)), ex2 X (fun x' : X => A x' x) (fun x' : X => eq (sum X (forall _ : X, Prop)) y' (inl X (forall _ : X, Prop) x')) *)
intros x y' H; apply H; clear H.
(* Deloc 2, case 1 *)
(* Goal: forall (x0 x' : X) (_ : eq (sum X (forall _ : X, Prop)) y' (inl X (forall _ : X, Prop) x')) (_ : eq (sum X (forall _ : X, Prop)) (inl X (forall _ : X, Prop) x) (inl X (forall _ : X, Prop) x0)) (_ : A x' x0), ex2 X (fun x'0 : X => A x'0 x) (fun x'0 : X => eq (sum X (forall _ : X, Prop)) y' (inl X (forall _ : X, Prop) x'0)) *)
(* Goal: forall (x' : X) (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) y' (inl X (forall _ : X, Prop) x')) (_ : eq (sum X (forall _ : X, Prop)) (inl X (forall _ : X, Prop) x) (inr X (forall _ : X, Prop) p)) (_ : A x' a) (_ : p x'), ex2 X (fun x'0 : X => A x'0 x) (fun x'0 : X => eq (sum X (forall _ : X, Prop)) y' (inl X (forall _ : X, Prop) x'0)) *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) y' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) (inl X (forall _ : X, Prop) x) (out X (forall _ : X, Prop))), ex2 X (fun x' : X => A x' x) (fun x' : X => eq (sum X (forall _ : X, Prop)) y' (inl X (forall _ : X, Prop) x')) *)
intros x0 x' H1 H2 H3; apply ex2_intro with x'.
(* Goal: A x' x *)
(* Goal: eq (sum X (forall _ : X, Prop)) y' (inl X (forall _ : X, Prop) x') *)
(* Goal: forall (x' : X) (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) y' (inl X (forall _ : X, Prop) x')) (_ : eq (sum X (forall _ : X, Prop)) (inl X (forall _ : X, Prop) x) (inr X (forall _ : X, Prop) p)) (_ : A x' a) (_ : p x'), ex2 X (fun x'0 : X => A x'0 x) (fun x'0 : X => eq (sum X (forall _ : X, Prop)) y' (inl X (forall _ : X, Prop) x'0)) *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) y' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) (inl X (forall _ : X, Prop) x) (out X (forall _ : X, Prop))), ex2 X (fun x' : X => A x' x) (fun x' : X => eq (sum X (forall _ : X, Prop)) y' (inl X (forall _ : X, Prop) x')) *)
apply (eq_sym _ _ _ (eq_inl_inl X (X -> Prop) x x0 H2)); assumption.
(* Goal: A x' a *)
(* Goal: EQV Z C c X A x' *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) z (out X (forall _ : X, Prop))), ELT Z C c X A a *)
assumption.
(* Deloc 2, case 2 (absurd) *)
(* Goal: forall (x' : X) (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) y' (inl X (forall _ : X, Prop) x')) (_ : eq (sum X (forall _ : X, Prop)) (inl X (forall _ : X, Prop) x) (inr X (forall _ : X, Prop) p)) (_ : A x' a) (_ : p x'), ex2 X (fun x'0 : X => A x'0 x) (fun x'0 : X => eq (sum X (forall _ : X, Prop)) y' (inl X (forall _ : X, Prop) x'0)) *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) y' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) (inl X (forall _ : X, Prop) x) (out X (forall _ : X, Prop))), ex2 X (fun x' : X => A x' x) (fun x' : X => eq (sum X (forall _ : X, Prop)) y' (inl X (forall _ : X, Prop) x')) *)
intros x' p H1 H2 H3 H4; apply (eq_inl_inr X (X -> Prop) x p H2).
(* Deloc 2, case 3 (absurd) *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) y' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) (inl X (forall _ : X, Prop) x) (out X (forall _ : X, Prop))), ex2 X (fun x' : X => A x' x) (fun x' : X => eq (sum X (forall _ : X, Prop)) y' (inl X (forall _ : X, Prop) x')) *)
intros p H1 H2; apply (eq_inl_out X (X -> Prop) x H2).
Qed.

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).

Proof.
(* Goal: forall (X : Typ1) (A : Rel X) (a x : X), EQV X A x (sum X (forall _ : X, Prop)) (POWER X A a) (inl X (forall _ : X, Prop) x) *)
intros X A a x; apply EQV_deloc; apply POWER_deloc.
Qed.

(** Moreover, any subset of (X, A, a) is bisimilar to a pointed graph
    of the form ((sum X X->Prop), (POWER X A a), (inr X X->Prop) p)
    for a suitable predicate p : X->Prop. *)

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))).

Proof.
(* We reason by extensionality: *)
(* Goal: 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 (forall _ : X, Prop)) (POWER X A a) (inr X (forall _ : X, Prop) (fun x' : X => and (A x' a) (ELT X A x' Y B b))) *)
intros X A a Y B b H1; apply extensionality.
(* Inclusion (SUB Y B b (sum ...) (POWER ...) (inr ... p)) *)
(* Goal: SUB Y B b (sum X (forall _ : X, Prop)) (POWER X A a) (inr X (forall _ : X, Prop) (fun x' : X => and (A x' a) (ELT X A x' Y B b))) *)
(* Goal: SUB (sum X (forall _ : X, Prop)) (POWER X A a) (inr X (forall _ : X, Prop) (fun x' : X => and (A x' a) (ELT X A x' Y B b))) Y B b *)
unfold SUB in |- *; intros Z C c H2.
(* Goal: ELT Z C c (sum X (forall _ : X, Prop)) (POWER X A a) (inr X (forall _ : X, Prop) (fun x' : X => and (A x' a) (ELT X A x' Y B b))) *)
(* Goal: SUB (sum X (forall _ : X, Prop)) (POWER X A a) (inr X (forall _ : X, Prop) (fun x' : X => and (A x' a) (ELT X A x' Y B b))) Y B b *)
apply (H1 Z C c H2); intros x' H3 H4.
(* Goal: ELT Z C c (sum X (forall _ : X, Prop)) (POWER X A a) (inr X (forall _ : X, Prop) (fun x' : X => and (A x' a) (ELT X A x' Y B b))) *)
(* Goal: SUB (sum X (forall _ : X, Prop)) (POWER X A a) (inr X (forall _ : X, Prop) (fun x' : X => and (A x' a) (ELT X A x' Y B b))) Y B b *)
apply ELT_intro with (inl X (X -> Prop) x').
(* Goal: A x' a *)
(* Goal: EQV Z C c X A x' *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) z (out X (forall _ : X, Prop))), ELT Z C c X A a *)
apply POWER_in2.  assumption.
(* Goal: A x' a *)
(* Goal: EQV Z C c X A x' *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) z (out X (forall _ : X, Prop))), ELT Z C c X A a *)
apply and_intro. assumption.
(* Goal: ELT X A x' Y B b *)
(* Goal: EQV Z C c (sum X (forall _ : X, Prop)) (POWER X A a) (inl X (forall _ : X, Prop) x') *)
(* Goal: SUB (sum X (forall _ : X, Prop)) (POWER X A a) (inr X (forall _ : X, Prop) (fun x' : X => and (A x' a) (ELT X A x' Y B b))) Y B b *)
apply ELT_compat_l with Z C c.
(* Goal: A x' a *)
(* Goal: EQV Z C c X A x' *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) z (out X (forall _ : X, Prop))), ELT Z C c X A a *)
apply EQV_sym; assumption. assumption.
(* Goal: EQV Z C c (sum X (forall _ : X, Prop)) (POWER X A a) (inl X (forall _ : X, Prop) x') *)
(* Goal: SUB (sum X (forall _ : X, Prop)) (POWER X A a) (inr X (forall _ : X, Prop) (fun x' : X => and (A x' a) (ELT X A x' Y B b))) Y B b *)
apply EQV_trans with X A x'.
(* Goal: A x' a *)
(* Goal: EQV Z C c X A x' *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) z (out X (forall _ : X, Prop))), ELT Z C c X A a *)
assumption. apply POWER_eqv; assumption.
(* Inclusion (SUB (sum ...) (POWER ...) (inr ... p) Y B b) *)
(* Goal: SUB (sum X (forall _ : X, Prop)) (POWER X A a) (inr X (forall _ : X, Prop) (fun x' : X => and (A x' a) (ELT X A x' Y B b))) Y B b *)
unfold SUB in |- *; intros Z C c H2; apply H2; clear H2.
(* Goal: forall (b' : sum X (forall _ : X, Prop)) (_ : POWER X A a b' (inr X (forall _ : X, Prop) (fun x' : X => and (A x' a) (ELT X A x' Y B b)))) (_ : EQV Z C c (sum X (forall _ : X, Prop)) (POWER X A a) b'), ELT Z C c Y B b *)
intros z H2 H3; apply H2; clear H2. (* Case distinction *)
(* Case 1 (absurd) *)
(* Goal: forall (x x' : X) (_ : eq (sum X (forall _ : X, Prop)) z (inl X (forall _ : X, Prop) x')) (_ : eq (sum X (forall _ : X, Prop)) (inr X (forall _ : X, Prop) (fun x'0 : X => and (A x'0 a) (ELT X A x'0 Y B b))) (inl X (forall _ : X, Prop) x)) (_ : A x' x), ELT Z C c Y B b *)
(* Goal: forall (x' : X) (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z (inl X (forall _ : X, Prop) x')) (_ : eq (sum X (forall _ : X, Prop)) (inr X (forall _ : X, Prop) (fun x'0 : X => and (A x'0 a) (ELT X A x'0 Y B b))) (inr X (forall _ : X, Prop) p)) (_ : A x' a) (_ : p x'), ELT Z C c Y B b *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) (inr X (forall _ : X, Prop) (fun x' : X => and (A x' a) (ELT X A x' Y B b))) (out X (forall _ : X, Prop))), ELT Z C c Y B b *)
intros x x' H4 H5 H6; apply (eq_inr_inl _ _ _ _ H5).
(* Case 2 *)
(* Goal: forall (x' : X) (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z (inl X (forall _ : X, Prop) x')) (_ : eq (sum X (forall _ : X, Prop)) (inr X (forall _ : X, Prop) (fun x'0 : X => and (A x'0 a) (ELT X A x'0 Y B b))) (inr X (forall _ : X, Prop) p)) (_ : A x' a) (_ : p x'), ELT Z C c Y B b *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) (inr X (forall _ : X, Prop) (fun x' : X => and (A x' a) (ELT X A x' Y B b))) (out X (forall _ : X, Prop))), ELT Z C c Y B b *)
intros x' p H4 H5 H6 H7; generalize (eq_inr_inr _ _ _ _ H5); intro H8.
(* Goal: ELT Z C c Y B b *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) (inr X (forall _ : X, Prop) (fun x' : X => and (A x' a) (ELT X A x' Y B b))) (out X (forall _ : X, Prop))), ELT Z C c Y B b *)
generalize (eq_sym _ _ _ H8 (fun q => q x') H7); clear H8; intro H8.
(* Goal: ELT Z C c Y B b *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) (inr X (forall _ : X, Prop) (fun x' : X => and (A x' a) (ELT X A x' Y B b))) (out X (forall _ : X, Prop))), ELT Z C c Y B b *)
apply H8; clear H8; intros H8 H9; apply ELT_compat_l with X A x'.
(* Goal: EQV Z C c X A x' *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) z (out X (forall _ : X, Prop))), ELT Z C c X A a *)
apply EQV_trans with (sum X (X -> Prop)) (POWER X A a) (inl X (X -> Prop) x').
(* Goal: A x' a *)
(* Goal: EQV Z C c X A x' *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) z (out X (forall _ : X, Prop))), ELT Z C c X A a *)
apply H4; assumption. apply EQV_sym; apply POWER_eqv. assumption.
(* Case 3 (absurd) *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) (inr X (forall _ : X, Prop) (fun x' : X => and (A x' a) (ELT X A x' Y B b))) (out X (forall _ : X, Prop))), ELT Z C c Y B b *)
intros p H4 H5; apply (eq_inr_out _ _ _ H5).
Qed.

(** From this, we deduce the introduction rule of the powerset: *)

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)).

Proof.
intros X A a Y B b H;
 apply
  ELT_intro
   with (inr X (X -> Prop) (fun x' => and (A x' a) (ELT X A x' Y B b))).
(* Goal: A x' a *)
(* Goal: EQV Z C c X A x' *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) z (out X (forall _ : X, Prop))), ELT Z C c X A a *)
apply POWER_rt.  apply POWER_subset_eqv; assumption.
Qed.

(** And the elimination rule comes easily: *)

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.

Proof.
(* Goal: forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y) (_ : ELT Y B b (sum X (forall _ : X, Prop)) (POWER X A a) (out X (forall _ : X, Prop))), SUB Y B b X A a *)
intros X A a Y B b H; apply H; clear H.
(* Goal: forall (b' : sum X (forall _ : X, Prop)) (_ : POWER X A a b' (out X (forall _ : X, Prop))) (_ : EQV Y B b (sum X (forall _ : X, Prop)) (POWER X A a) b'), SUB Y B b X A a *)
intros z H1 H2; apply H1; clear H1. (* Case distinction *)
(* Case 1 (absurd) *)
(* Goal: forall (x x' : X) (_ : eq (sum X (forall _ : X, Prop)) z (inl X (forall _ : X, Prop) x')) (_ : eq (sum X (forall _ : X, Prop)) (out X (forall _ : X, Prop)) (inl X (forall _ : X, Prop) x)) (_ : A x' x), SUB Y B b X A a *)
(* Goal: forall (x' : X) (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z (inl X (forall _ : X, Prop) x')) (_ : eq (sum X (forall _ : X, Prop)) (out X (forall _ : X, Prop)) (inr X (forall _ : X, Prop) p)) (_ : A x' a) (_ : p x'), SUB Y B b X A a *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) (out X (forall _ : X, Prop)) (out X (forall _ : X, Prop))), SUB Y B b X A a *)
intros x x' H3 H4 H5; apply (eq_out_inl _ _ _ H4).
(* Case 2 (absurd) *)
(* Goal: forall (x' : X) (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z (inl X (forall _ : X, Prop) x')) (_ : eq (sum X (forall _ : X, Prop)) (out X (forall _ : X, Prop)) (inr X (forall _ : X, Prop) p)) (_ : A x' a) (_ : p x'), SUB Y B b X A a *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) (out X (forall _ : X, Prop)) (out X (forall _ : X, Prop))), SUB Y B b X A a *)
intros x' p H3 H4 H5 H6; apply (eq_out_inr _ _ _ H4).
(* Case 3 *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) (out X (forall _ : X, Prop)) (out X (forall _ : X, Prop))), SUB Y B b X A a *)
unfold SUB in |- *; intros p H3 H4 Z C c H5.
(* Goal: ELT Z C c X A a *)
apply (ELT_compat_r _ _ _ _ _ _ _ _ _ H5 H2).
(* Goal: forall (b' : sum X (forall _ : X, Prop)) (_ : POWER X A a b' z) (_ : EQV Z C c (sum X (forall _ : X, Prop)) (POWER X A a) b'), ELT Z C c X A a *)
intros z' H6 H7; apply H6; clear H6.
(* Case 3-1 (absurd) *)
(* Goal: forall (x x' : X) (_ : eq (sum X (forall _ : X, Prop)) z' (inl X (forall _ : X, Prop) x')) (_ : eq (sum X (forall _ : X, Prop)) z (inl X (forall _ : X, Prop) x)) (_ : A x' x), ELT Z C c X A a *)
(* Goal: forall (x' : X) (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inl X (forall _ : X, Prop) x')) (_ : eq (sum X (forall _ : X, Prop)) z (inr X (forall _ : X, Prop) p)) (_ : A x' a) (_ : p x'), ELT Z C c X A a *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) z (out X (forall _ : X, Prop))), ELT Z C c X A a *)
intros x x' H8 H9 H10.
(* Goal: ELT Z C c X A a *)
(* Goal: forall (x' : X) (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inl X (forall _ : X, Prop) x')) (_ : eq (sum X (forall _ : X, Prop)) z (inr X (forall _ : X, Prop) p)) (_ : A x' a) (_ : p x'), ELT Z C c X A a *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) z (out X (forall _ : X, Prop))), ELT Z C c X A a *)
apply (eq_inl_inr _ _ _ _ (eq_trans _ _ _ _ (eq_sym _ _ _ H9) H3)).
(* Case 3-2 *)
(* Goal: A x' a *)
(* Goal: EQV Z C c X A x' *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) z (out X (forall _ : X, Prop))), ELT Z C c X A a *)
intros x' p0 H8 H9 H10 H11; apply ELT_intro with x'.  assumption.
(* Goal: EQV Z C c X A x' *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) z (out X (forall _ : X, Prop))), ELT Z C c X A a *)
apply EQV_trans with (sum X (X -> Prop)) (POWER X A a) (inl X (X -> Prop) x').
(* Goal: A x' a *)
(* Goal: EQV Z C c X A x' *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) z (out X (forall _ : X, Prop))), ELT Z C c X A a *)
apply H8; assumption.  apply EQV_sym; apply POWER_eqv.
(* Case 3-3 (absurd) *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) z (out X (forall _ : X, Prop))), ELT Z C c X A a *)
intros p0 H8 H9.
(* Goal: ELT Z C c X A a *)
apply (eq_inr_out _ _ _ (eq_trans _ _ _ _ (eq_sym _ _ _ H3) H9)).
Qed.

(** From this, we can conclude: *)

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).

Proof.
(* Goal: and (A x' a) (ELT X A x' Y B b) *)
(* Goal: EQV Z C c (sum X (forall _ : X, Prop)) (POWER X A a) (inl X (forall _ : X, Prop) x') *)
(* Goal: SUB (sum X (forall _ : X, Prop)) (POWER X A a) (inr X (forall _ : X, Prop) (fun x' : X => and (A x' a) (ELT X A x' Y B b))) Y B b *)
intros X A a Y B b; unfold iff in |- *; apply and_intro.
(* Goal: A x' a *)
(* Goal: EQV Z C c X A x' *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) z (out X (forall _ : X, Prop))), ELT Z C c X A a *)
intro; apply powerset_elim; assumption.
(* Goal: A x' a *)
(* Goal: EQV Z C c X A x' *)
(* Goal: forall (p : forall _ : X, Prop) (_ : eq (sum X (forall _ : X, Prop)) z' (inr X (forall _ : X, Prop) p)) (_ : eq (sum X (forall _ : X, Prop)) z (out X (forall _ : X, Prop))), ELT Z C c X A a *)
intro; apply powerset_intro; assumption.
Qed.
