(* 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                                                     *)
(** Sets as pointed graphs ~~~~~~~~~~~~~~~~~~~~~~ In the following, we will interpret sets as pointed graphs, that is, as triples of the form (X, A, a) where :  - X : Typ1     is the carrier (i.e. the small type of vertices); - A : (Rel X)  is the edge (or local membership) relation; - a : X        is the root.  Naming conventions ~~~~~~~~~~~~~~~~~~ Since the PTS Fw.2 does not provide any pairing mechanism, we will introduce the three components of each pointed graph separately. Of course, this separation is error-prone, and to avoid confusions we will name components according to the following scheme:  Pointed graph   Vertices -------------   -------- (X, A, a)       x, x', x0, x1, etc. (Y, B, b)       y, y', y0, y1, etc. (Z, C, c)       z, z', z0, z1, etc. etc.  Moreover, a letter affected with a prime (such as x') will usually indicate that the vertex (or carrier, or relation) it denotes is in some sense an element of the corresponding unaffected letter. *)
(********************************)
(** * Equality as bisimilarity  *)
(********************************)
(** Two pointed graphs (X, A, a) and (Y, B, b) are bisimilar if there exists a relation R:X->Y->Prop satisfying the following conditions:  (BIS1) (x,x':X; y:Y) (A x' x)/\(R x y) -> (Ex y':Y | (B y' y)/\(R x' y'))  (BIS2) (x,x':X; y:Y) (A x' x)/\(R x y) -> (Ex y':Y | (B y' y)/\(R x' y'))  (BIS3) (R a b)  Instead of defining this relation by the mean of standard logical connectives, we give an equivalent impredicative encoding for it in order to make the forthcoming proofs a little bit shorter. *)
Definition EQV (X : Typ1) (A : Rel X) (a : X) (Y : Typ1)
(B : Rel Y) (b : Y) : Prop :=
forall E : Prop,
(forall R : X -> Y -> Prop,
(forall (x x' : X) (y : Y),
A x' x -> R x y -> ex2 Y (fun y' => B y' y) (fun y' => R x' y')) ->
(forall (y y' : Y) (x : X),
B y' y -> R x y -> ex2 X (fun x' => A x' x) (fun x' => R x' y')) ->
R a b -> E) -> E.
(** Here is the corresponding introduction rule: *)
Proof fun X A a Y B b R H1 H2 H3 E e => e R H1 H2 H3.
(** It would be useless to define the corresponding elimination rule whose behaviour is obtained by using [Apply H] instead of [Elim H] in the following proof scripts.  We first check that bisimilarity is an equivalence relation: *)
(** The following lemma states that the bisimilarity relation can be shifted one vertex down by following the edges of both local membership relations. *)
(******************************************)
(** * Membership as shifted bisimilarity  *)
(******************************************)
(** A pointed graph (X, A, a) represents an element of a pointed graph (Y, B, b) if there is a vertex b':Y one edge below the root b such that the pointed graphs (X, A, a) and (Y, B, b') are bisimilar.  This relation is impredicatively encoded as follows: *)
Definition ELT (X : Typ1) (A : Rel X) (a : X) (Y : Typ1)
(B : Rel Y) (b : Y) : Prop :=
forall E : Prop, (forall b' : Y, B b' b -> EQV X A a Y B b' -> E) -> E.
Proof fun X A a Y B b b' H1 H2 E e => e b' H1 H2.
(** Direct elements of a pointed graph (X, A, a) are simply the pointed graphs of the form (X, A, a') such that (A a' a), that is, the pointed graphs we obtain by shifting the root one edge downwards: *)
(** We now state the (left and right) compatibility of the membership relation w.r.t. the bisimilarity relation.  Both compatibilities rely on the transitivity of the bisimilarity relation, and the right compatibility uses the EQV_shift lemma we proved above. *)
(************************************)
(** * Inclusion and extensionality  *)
(************************************)
(** The inclusion relation is defined as expected: *)
Definition SUB (X : Typ1) (A : Rel X) (a : X) (Y : Typ1)
(B : Rel Y) (b : Y) : Prop :=
forall (Z : Typ1) (C : Rel Z) (c : Z), ELT Z C c X A a -> ELT Z C c Y B b.
(** Inclusion is clearly reflexive and transitive: *)
(** We now state the extensionality property, which expresses that the inclusion relation is antisymmetric (thus being a partial ordering relation between sets).  Equivalently, this property says that two pointed graphs are bisimilar when they have the same elements. *)
(*******************)
(** * Delocations  *)
(*******************)
(** Let (X, A) and (Y, B) be two graphs.  A delocation from (X, A) to (Y, B) is a function f : X->Y such that:  1. (x,x':X)    (A x' x) -> (B (f x') (f x))  2. (x:X; y':Y) (B y' (f x)) -> (Ex x':X | (A x' x) /\ y'=(f x'))  Intuitively, a delocation is a morphism of graphs in which any edge of the target graph (Y, B) which points to an element of the image of f can be tracked back as an edge in the source graph via f. This notion is formalized as follows: *)
Definition deloc (X : Typ1) (A : Rel X) (Y : Typ1)
(B : Rel Y) (f : X -> Y) : Prop :=
and (forall x x' : X, A x' x -> B (f x') (f x))
(forall (x : X) (y' : Y),
B y' (f x) -> ex2 X (fun x' => A x' x) (fun x' => eq Y y' (f x'))).
(** The notion of delocation f:(X,A)->(Y,B) is interesting since it automatically induces a bisimulation from the pointed graph (X, A, x) to the pointed graph (Y, B, (f x)) for any vertex x:X in the source graph (X,A).  This property is stated by the following lemma: *)

Lemma EQV_deloc :
forall (X : Typ1) (A : Rel X) (Y : Typ1) (B : Rel Y) (f : X -> Y),
deloc X A Y B f -> forall x : X, EQV X A x Y B (f x).