(** Let (X, A, a) and (Y, B, b) be two pointed graphs (with X,Y : Typ1).
    The unordered pair formed by the sets represented by these pointed
    graphs is itself represented by the pointed graph

      ((sum X Y), (PAIR X A a Y B b), (out X Y))

    whose edge relation (PAIR X A a Y B b) : (Rel (sum X Y)) is
    defined by the following four clauses :

    1. Delocate A in the new graph via (inl X Y):

         if (A x' x), then (PAIR X A a Y B b (inl X Y x') (inl X Y x))

    2. Delocate B in the new graph via (inr X Y):

         if (B y' y), then (PAIR X A a Y B b (inr X Y y') (inr X Y y))

    3. Connect the (image of the) root a to the new root (out X Y):

         (PAIR X A a Y B b (inl X Y a) (out X Y))

    4. Connect the (image of the) root b to the new root (out X Y):

         (PAIR X A a Y B b (inr X Y b) (out X Y))

   As usual, we define this relation by a direct impredicative encoding: *)

Definition PAIR (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) 
  (B : Rel Y) (b : Y) (z' z : sum X Y) :=
  forall E : Prop,
  (forall x x' : X,
   eq (sum X Y) z (inl X Y x) -> eq (sum X Y) z' (inl X Y x') -> A x' x -> E) ->
  (forall y y' : Y,
   eq (sum X Y) z (inr X Y y) -> eq (sum X Y) z' (inr X Y y') -> B y' y -> E) ->
  (eq (sum X Y) z' (inl X Y a) -> eq (sum X Y) z (out X Y) -> E) ->
  (eq (sum X Y) z' (inr X Y b) -> eq (sum X Y) z (out X Y) -> E) -> E.

(* Specifications: *)
Lemma PAIR_in1 : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y) (x x' : X), A x' x -> PAIR X A a Y B b (inl X Y x') (inl X Y x).
Lemma PAIR_in2 : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b y y' : Y), B y' y -> PAIR X A a Y B b (inr X Y y') (inr X Y y).
Lemma PAIR_rt1 : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y), PAIR X A a Y B b (inl X Y a) (out X Y).
Lemma PAIR_rt2 : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y), PAIR X A a Y B b (inr X Y b) (out X Y).
Lemma PAIR_deloc1 : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y), deloc X A (sum X Y) (PAIR X A a Y B b) (inl X Y).
Lemma PAIR_eqv1 : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y), EQV X A a (sum X Y) (PAIR X A a Y B b) (inl X Y a).
Lemma PAIR_deloc2 : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y), deloc Y B (sum X Y) (PAIR X A a Y B b) (inr X Y).
Lemma PAIR_eqv2 : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y), EQV Y B b (sum X Y) (PAIR X A a Y B b) (inr X Y b).
Lemma pairing_intro1 : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y), ELT X A a (sum X Y) (PAIR X A a Y B b) (out X Y).
Lemma pairing_intro2 : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y), ELT Y B b (sum X Y) (PAIR X A a Y B b) (out X Y).
Lemma pairing_elim : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y) (Z : Typ1) (C : Rel Z) (c : Z), ELT Z C c (sum X Y) (PAIR X A a Y B b) (out X Y) -> or (EQV Z C c X A a) (EQV Z C c Y B b).
Theorem pairing : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y) (Z : Typ1) (C : Rel Z) (c : Z), iff (ELT Z C c (sum X Y) (PAIR X A a Y B b) (out X Y)) (or (EQV Z C c X A a) (EQV Z C c Y B b)).