(*******************************)
(** * The successor function   *)
(*******************************)

(** In set theory, the successor of von Neumann numeral n is encoded
    as the set  n U {n}, whose existence follows from the pairing
    axiom (used twice) and the union axiom, that have been already
    derived in the files "IZF_pair.v"  and "IZF_union.v".

    However, it is simpler to give here a direct implementation of the
    successor as follows.  Formally, the successor of a given pointed
    graph (X, A, a) is represented as the pointed graph

        ((opt X), (SUCC X A a), (none X))

    whose edge relation is defined by the following three clauses:

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

         if (A x' x), then (SUCC X A a (some X x') (some X x))

    2. Connect the image of any direct element of the old root a:X
       to the new root:

         if (A x a), then (SUCC X A a (some X x) (none X))

    3. Connect the old root to the new root:

         (SUCC X A a (some X a) (none X)).

    As usual, we use a second-order encoding to define this relation. *)

Definition SUCC (X : Typ1) (A : Rel X) (a : X) : Rel (opt X) :=
  fun z' z =>
  forall E : Prop,
  (forall x x' : X,
   eq (opt X) z (some X x) -> eq (opt X) z' (some X x') -> A x' x -> E) ->
  (forall x' : X,
   eq (opt X) z (none X) -> eq (opt X) z' (some X x') -> A x' a -> E) ->
  (eq (opt X) z (none X) -> eq (opt X) z' (some X a) -> E) -> E.

(* Specifications: *)
Lemma SUCC_in : forall (X : Typ1) (A : Rel X) (a x x' : X), A x' x -> SUCC X A a (some X x') (some X x).
Lemma SUCC_rt1 : forall (X : Typ1) (A : Rel X) (a x' : X), A x' a -> SUCC X A a (some X x') (none X).
Lemma SUCC_rt2 : forall (X : Typ1) (A : Rel X) (a : X), SUCC X A a (some X a) (none X).
Lemma SUCC_deloc : forall (X : Typ1) (A : Rel X) (a : X), deloc X A (opt X) (SUCC X A a) (some X).
Lemma SUCC_eqv : forall (X : Typ1) (A : Rel X) (a x : X), EQV X A x (opt X) (SUCC X A a) (some X x).
Lemma SUCC_intro1 : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y), ELT Y B b X A a -> ELT Y B b (opt X) (SUCC X A a) (none X).
Lemma SUCC_intro2 : forall (X : Typ1) (A : Rel X) (a : X), ELT X A a (opt X) (SUCC X A a) (none X).
Lemma SUCC_elim : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y), ELT Y B b (opt X) (SUCC X A a) (none X) -> or (ELT Y B b X A a) (EQV Y B b X A a).
Lemma successor : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y), iff (ELT Y B b (opt X) (SUCC X A a) (none X)) (or (ELT Y B b X A a) (EQV Y B b X A a)).
Lemma SUCC_compat : forall (X : Typ1) (A : Rel X) (a : X) (Y : Typ1) (B : Rel Y) (b : Y), EQV X A a Y B b -> EQV (opt X) (SUCC X A a) (none X) (opt Y) (SUCC Y B b) (none Y).