(* 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 Export DistributedReferenceCounting.machine1.alternate.
Require Export DistributedReferenceCounting.machine1.invariant0.
Require Export DistributedReferenceCounting.machine1.invariant1.
Require Export DistributedReferenceCounting.machine1.invariant2.
Require Export DistributedReferenceCounting.machine1.invariant3.
Require Export DistributedReferenceCounting.machine1.invariant4.

Unset Standard Proposition Elimination Names.

Section INVARIANT5.

(* In this file, we establish the following inequality
   Int + Sum copy + Sum dec - Sum inc_dec >= 0

*) 


(* The message queue between a site and the owner has a particular
structure.  Messages dec and inc_dec are alternated.
 *)


Lemma false_rt_D_queue :
 forall (c : Config) (s : Site),
 legal c -> rt c s = false -> D_queue (bm c s owner).
Proof.
(* Goal: forall (c : Config) (s : Site) (_ : legal c) (_ : @eq bool (rt c s) false), D_queue (bm c s owner) *)
   intros c s H; elim H.
(* Goal: forall _ : @eq bool (rt config_init s) false, D_queue (bm config_init s owner) *)
(* Goal: forall (c : Config) (t : class_trans c) (_ : legal c) (_ : forall _ : @eq bool (rt c s) false, D_queue (bm c s owner)) (_ : @eq bool (rt (transition c t) s) false), D_queue (bm (transition c t) s owner) *)
   simpl in |- *.
(* Goal: forall a : Message, Z.ge (dec_count a) Z0 *)
   intro.
(* Goal: D_queue (bag_init s owner) *)
(* Goal: forall (c : Config) (t : class_trans c) (_ : legal c) (_ : forall _ : @eq bool (rt c s) false, D_queue (bm c s owner)) (_ : @eq bool (rt (transition c t) s) false), D_queue (bm (transition c t) s owner) *)
   apply D_empty.
   
(* Goal: forall (c : Config) (t : class_trans c) (_ : legal c) (_ : alternate (bm c s owner)), alternate (bm (transition c t) s owner) *)
   simple induction t; simpl in |- *; intros.

   (*  1 *)

(* Goal: D_queue (Post_message Message dec (bm c0) s0 owner s owner) *)
   apply D_post_elsewhere; auto.

   (*  2 *)
(* Goal: D_queue (Collect_message Message (bm c0) s1 s2 s owner) *)
(* Goal: D_queue (Post_message Message dec (bm c0) s0 owner s owner) *)
   apply D_collect; auto.
   
   (*  3 *)

(* Goal: D_queue (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: D_queue (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: D_queue (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: D_queue (Post_message Message dec (bm c0) s0 owner s owner) *)
   apply D_post_elsewhere.
(* Goal: or (not (@eq Site owner s)) (not (@eq Site s3 owner)) *)
(* Goal: alternate (Collect_message Message (bm c0) s1 owner s owner) *)
(* Goal: alternate (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: alternate (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: alternate (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
   right.
(* Goal: not (@eq Site s3 owner) *)
(* Goal: alternate (Collect_message Message (bm c0) s1 owner s owner) *)
(* Goal: alternate (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: alternate (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: alternate (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
   apply (not_owner_inc3 c0 s1 owner H0 s3).
(* Goal: In_queue Message (inc_dec s3) (bm c0 s1 owner) *)
(* Goal: alternate (Collect_message Message (bm c0) s1 owner s owner) *)
(* Goal: alternate (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: alternate (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: alternate (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
   apply first_in.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
   auto.
(* Goal: D_queue (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: D_queue (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: D_queue (Post_message Message dec (bm c0) s0 owner s owner) *)
   apply D_collect.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
   auto.
   
   (*  4 *)

(* Goal: forall _ : @eq bool (rt config_init s) false, D_queue (bm config_init s owner) *)
(* Goal: forall (c : Config) (t : class_trans c) (_ : legal c) (_ : forall _ : @eq bool (rt c s) false, D_queue (bm c s owner)) (_ : @eq bool (rt (transition c t) s) false), D_queue (bm (transition c t) s owner) *)
   case (eq_queue_dec s2 s s1 owner); intros; simpl in |- *.
(* Goal: alternate (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: alternate (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: alternate (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: alternate (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
   decompose [and] a.
(* Goal: D_queue (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: D_queue (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: D_queue (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: D_queue (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: D_queue (Post_message Message dec (bm c0) s0 owner s owner) *)
   rewrite H3; rewrite H4.
(* Goal: D_queue (Post_message Message dec (Collect_message Message (bm c0) owner s) s owner s owner) *)
(* Goal: D_queue (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: D_queue (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: D_queue (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: D_queue (Post_message Message dec (bm c0) s0 owner s owner) *)
   apply D_post_dec.
   
(* Goal: D_queue (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: D_queue (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: D_queue (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: D_queue (Post_message Message dec (bm c0) s0 owner s owner) *)
   apply D_post_elsewhere.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
   auto.

(* Goal: D_queue (Collect_message Message (bm c0) s1 s2 s owner) *)
(* Goal: D_queue (Post_message Message dec (bm c0) s0 owner s owner) *)
   apply D_collect; auto.
   
   (* 5 *)

(* Goal: forall a : Message, Z.ge (dec_count a) Z0 *)
   case (eq_site_dec s s2); intro.
(* Goal: D_queue (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: D_queue (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: D_queue (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: D_queue (Post_message Message dec (bm c0) s0 owner s owner) *)
   generalize H2; rewrite e1; unfold Set_rec_table in |- *; rewrite that_site.
(* Goal: forall s : Site, not (@eq Message dec (inc_dec s)) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
   intro; discriminate.
   
(* Goal: D_queue (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: D_queue (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: D_queue (Post_message Message dec (bm c0) s0 owner s owner) *)
   apply D_collect.
(* Goal: D_queue (bm c0 s owner) *)
(* Goal: D_queue (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: D_queue (Post_message Message dec (bm c0) s0 owner s owner) *)
   apply H1.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
   generalize H2; unfold Set_rec_table in |- *; rewrite other_site; auto.

   (*  6 *)
   
(* Goal: forall a : Message, Z.ge (dec_count a) Z0 *)
   case (eq_site_dec s s2); intro.
   generalize H2; rewrite e1; unfold Set_rec_table in |- *; rewrite that_site;
(* Goal: forall s : Site, not (@eq Message dec (inc_dec s)) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
    intro; discriminate.
   
(* Goal: D_queue (Post_message Message dec (bm c0) s0 owner s owner) *)
   apply D_post_elsewhere; auto.
(* Goal: D_queue (Collect_message Message (bm c0) s1 s2 s owner) *)
(* Goal: D_queue (Post_message Message dec (bm c0) s0 owner s owner) *)
   apply D_collect; auto.
   apply H1; generalize H2; unfold Set_rec_table in |- *; rewrite other_site;
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
    auto.

   (*  7 *)
   
(* Goal: forall a : Message, Z.ge (dec_count a) Z0 *)
   case (eq_site_dec s0 s); intro.
(* Goal: D_queue (Post_message Message dec (Collect_message Message (bm c0) owner s) s owner s owner) *)
(* Goal: D_queue (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: D_queue (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: D_queue (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: D_queue (Post_message Message dec (bm c0) s0 owner s owner) *)
   rewrite e1; apply D_post_dec.
   
(* Goal: D_queue (Post_message Message dec (bm c0) s0 owner s owner) *)
   apply D_post_elsewhere; auto.
   apply H1; generalize H2; unfold Reset_rec_table in |- *;
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
    rewrite other_site; auto.
Qed.


Lemma legal_alternate :
 forall (s : Site) (c : Config), legal c -> alternate (bm c s owner).
Proof.  
(* Goal: forall (s : Site) (c : Config) (_ : legal c), alternate (bm c s owner) *)
  intros; elim H.
(* Goal: alternate (bm config_init s owner) *)
(* Goal: forall (c : Config) (t : class_trans c) (_ : legal c) (_ : alternate (bm c s owner)), alternate (bm (transition c t) s owner) *)
  apply alt_null.
  
(* Goal: forall (c : Config) (t : class_trans c) (_ : legal c) (_ : alternate (bm c s owner)), alternate (bm (transition c t) s owner) *)
  simple induction t; simpl in |- *; intros.
  
  (* 1 *)
  
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  apply alt_post_elsewhere; auto.
  
  (* 2 *)
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  apply alt_collect; auto.
  
  (* 3 *)
(* Goal: alternate (Post_message Message dec (Collect_message Message (bm c0) s1 owner) owner s3 s owner) *)
(* Goal: alternate (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: alternate (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: alternate (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
  apply alt_post_elsewhere.
(* Goal: or (not (@eq Site owner s)) (not (@eq Site s3 owner)) *)
(* Goal: alternate (Collect_message Message (bm c0) s1 owner s owner) *)
(* Goal: alternate (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: alternate (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: alternate (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
  right.
(* Goal: not (@eq Site s3 owner) *)
(* Goal: alternate (Collect_message Message (bm c0) s1 owner s owner) *)
(* Goal: alternate (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: alternate (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: alternate (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
  apply (not_owner_inc3 c0 s1 owner H0 s3).
(* Goal: In_queue Message (inc_dec s3) (bm c0 s1 owner) *)
(* Goal: alternate (Collect_message Message (bm c0) s1 owner s owner) *)
(* Goal: alternate (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: alternate (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: alternate (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
  apply first_in.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  auto.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  apply alt_collect; auto.
  
  (* 4 *)
  
(* Goal: alternate (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: alternate (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: alternate (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
  case (eq_queue_dec s2 s s1 owner); intros.
(* Goal: alternate (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: alternate (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: alternate (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: alternate (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
  decompose [and] a.
(* Goal: alternate (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: alternate (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: alternate (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: alternate (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
  rewrite H2; rewrite H3.
(* Goal: alternate (Post_message Message dec (Collect_message Message (bm c0) owner s) s owner s owner) *)
(* Goal: alternate (Post_message Message dec (Collect_message Message (bm c0) s1 s2) s2 s1 s owner) *)
(* Goal: alternate (Collect_message Message (bm c0) owner s2 s owner) *)
(* Goal: alternate (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
  apply alt_post_any.
(* Goal: forall s : Site, not (@eq Message dec (inc_dec s)) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
  intro; discriminate.
  
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  apply alt_collect; auto.
  
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  apply alt_post_elsewhere; auto.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  apply alt_collect; auto.
  
  (* 5 *)
  
(* Goal: alternate (Collect_message Message (bm c0) s1 s2 s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
  apply alt_collect; trivial.
  
  (* 6 *)
  
(* Goal: forall a : Message, Z.ge (dec_count a) Z0 *)
  case (eq_site_dec s2 s); intro.
(* Goal: alternate (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: alternate (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
  rewrite e1; apply alt_post_inc.
(* Goal: alternate (Collect_message Message (bm c0) s1 s2 s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
  apply alt_collect; trivial.
(* Goal: or (@eq (queue Message) (Collect_message Message (bm c0) s1 s s owner) (empty Message)) (@eq (exc Message) (last Message (Collect_message Message (bm c0) s1 s s owner)) (value Message dec)) *)
(* Goal: alternate (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
  rewrite collect_elsewhere.
(* Goal: forall a : Message, Z.ge (dec_count a) Z0 *)
  generalize (false_rt_D_queue c0 s2 H0 e); intro.
(* Goal: or (@eq (queue Message) (bm c0 s owner) (empty Message)) (@eq (exc Message) (last Message (bm c0 s owner)) (value Message dec)) *)
(* Goal: or (not (@eq Site s1 s)) (not (@eq Site s owner)) *)
(* Goal: alternate (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
  rewrite e1 in H2.
(* Goal: or (@eq (queue Message) (bm c0 s owner) (empty Message)) (@eq (exc Message) (last Message (bm c0 s owner)) (value Message dec)) *)
(* Goal: or (not (@eq Site s1 s)) (not (@eq Site s owner)) *)
(* Goal: alternate (Post_message Message (inc_dec s1) (Collect_message Message (bm c0) s1 s2) s2 owner s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
  elim H2.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  auto.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  auto.
(* Goal: forall a : Message, Z.ge (dec_count a) Z0 *)
  case (eq_site_dec s1 s); intro.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  rewrite e2 in n; auto.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  auto.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  apply alt_post_elsewhere; auto.
(* Goal: alternate (Collect_message Message (bm c0) s1 s2 s owner) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
  apply alt_collect; trivial.
  
  (* 7 *)
  
(* Goal: forall a : Message, Z.ge (dec_count a) Z0 *)
  case (eq_site_dec s0 s); intro.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  rewrite e1; apply alt_post_any; auto.
(* Goal: forall s : Site, not (@eq Message dec (inc_dec s)) *)
(* Goal: alternate (Post_message Message dec (bm c0) s0 owner s owner) *)
  intro; discriminate.
  
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  apply alt_post_elsewhere; auto.
Qed.

Lemma count_inc_and_dec_alt_queue :
 forall q : queue Message,
 alternate q ->
 (reduce Message dec_count q + 1 >= reduce Message new_inc_count q)%Z.
Proof.
(* Goal: forall (c : Config) (_ : legal c), Z.ge (st c owner) Z0 *)
  intros.
(* Goal: Z.ge (Z.add (reduce Message dec_count q) (Zpos xH)) (reduce Message new_inc_count q) *)
  elim H.
(* Goal: Z.ge (reduce Message dec_count (empty Message)) (reduce Message new_inc_count (empty Message)) *)
(* Goal: Z.ge (reduce Message dec_count (input Message (inc_dec s0) (empty Message))) (reduce Message new_inc_count (input Message (inc_dec s0) (empty Message))) *)
(* Goal: Z.ge (reduce Message dec_count (input Message m qm)) (reduce Message new_inc_count (input Message m qm)) *)
(* Goal: Z.ge (reduce Message dec_count (input Message (inc_dec s0) (input Message dec qm))) (reduce Message new_inc_count (input Message (inc_dec s0) (input Message dec qm))) *)
  simpl in |- *; omega.
  
(* Goal: Z.ge (reduce Message dec_count (empty Message)) (reduce Message new_inc_count (empty Message)) *)
(* Goal: Z.ge (reduce Message dec_count (input Message (inc_dec s0) (empty Message))) (reduce Message new_inc_count (input Message (inc_dec s0) (empty Message))) *)
(* Goal: Z.ge (reduce Message dec_count (input Message m qm)) (reduce Message new_inc_count (input Message m qm)) *)
(* Goal: Z.ge (reduce Message dec_count (input Message (inc_dec s0) (input Message dec qm))) (reduce Message new_inc_count (input Message (inc_dec s0) (input Message dec qm))) *)
  intro; simpl in |- *; omega.
  
(* Goal: forall (c : Config) (_ : legal c), Z.ge (st c owner) Z0 *)
  simpl in |- *; intros.
(* Goal: forall _ : @eq bool (rt config_init s) false, D_queue (bm config_init s owner) *)
(* Goal: forall (c : Config) (t : class_trans c) (_ : legal c) (_ : forall _ : @eq bool (rt c s) false, D_queue (bm c s owner)) (_ : @eq bool (rt (transition c t) s) false), D_queue (bm (transition c t) s owner) *)
  generalize H0; case m; simpl in |- *.
(* Goal: forall _ : forall s : Site, not (@eq Message copy (inc_dec s)), Z.ge (Z.add (Z.add (reduce Message dec_count qm) Z0) (Zpos xH)) (Z.add (reduce Message new_inc_count qm) Z0) *)
(* Goal: forall (qm : queue Message) (s0 : Site) (_ : alternate qm) (_ : Z.ge (Z.add (reduce Message dec_count qm) (Zpos xH)) (reduce Message new_inc_count qm)), Z.ge (Z.add (reduce Message dec_count (input Message (inc_dec s0) (input Message dec qm))) (Zpos xH)) (reduce Message new_inc_count (input Message (inc_dec s0) (input Message dec qm))) *)
  intro; omega.
  
(* Goal: forall (c : Config) (_ : legal c), Z.ge (st c owner) Z0 *)
  intros.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  elim (H3 s); auto.
  
(* Goal: forall _ : forall s : Site, not (@eq Message copy (inc_dec s)), Z.ge (Z.add (Z.add (reduce Message dec_count qm) Z0) (Zpos xH)) (Z.add (reduce Message new_inc_count qm) Z0) *)
(* Goal: forall (qm : queue Message) (s0 : Site) (_ : alternate qm) (_ : Z.ge (Z.add (reduce Message dec_count qm) (Zpos xH)) (reduce Message new_inc_count qm)), Z.ge (Z.add (reduce Message dec_count (input Message (inc_dec s0) (input Message dec qm))) (Zpos xH)) (reduce Message new_inc_count (input Message (inc_dec s0) (input Message dec qm))) *)
  intro; omega.
  
(* Goal: forall (c : Config) (_ : legal c), Z.ge (st c owner) Z0 *)
  simpl in |- *; intros.
(* Goal: Z.ge (Z.sub (Z.add (Z.add Z0 (reduce Message dec_count (bm c s owner))) (reduce Message copy_count (bm c owner s))) (reduce Message new_inc_count (bm c s owner))) Z0 *)
  omega.
Qed.


Lemma count_inc_and_dec_D_queue :
 forall q : queue Message,
 alternate q ->
 D_queue q ->
 (reduce Message dec_count q >= reduce Message new_inc_count q)%Z.
Proof.
(* Goal: forall (q : queue Message) (_ : alternate q) (_ : D_queue q), Z.ge (reduce Message dec_count q) (reduce Message new_inc_count q) *)
  intros q H.
(* Goal: forall (c : Config) (_ : legal c), Z.ge (st c owner) Z0 *)
  elim H; intros.
(* Goal: Z.ge (reduce Message dec_count (empty Message)) (reduce Message new_inc_count (empty Message)) *)
(* Goal: Z.ge (reduce Message dec_count (input Message (inc_dec s0) (empty Message))) (reduce Message new_inc_count (input Message (inc_dec s0) (empty Message))) *)
(* Goal: Z.ge (reduce Message dec_count (input Message m qm)) (reduce Message new_inc_count (input Message m qm)) *)
(* Goal: Z.ge (reduce Message dec_count (input Message (inc_dec s0) (input Message dec qm))) (reduce Message new_inc_count (input Message (inc_dec s0) (input Message dec qm))) *)
  simpl in |- *; omega.
  
(* Goal: Z.ge (reduce Message dec_count (input Message (inc_dec s0) (empty Message))) (reduce Message new_inc_count (input Message (inc_dec s0) (empty Message))) *)
(* Goal: Z.ge (reduce Message dec_count (input Message m qm)) (reduce Message new_inc_count (input Message m qm)) *)
(* Goal: Z.ge (reduce Message dec_count (input Message (inc_dec s0) (input Message dec qm))) (reduce Message new_inc_count (input Message (inc_dec s0) (input Message dec qm))) *)
  absurd (D_queue (input Message (inc_dec s0) (empty Message))).
(* Goal: not (D_queue (input Message (inc_dec s0) (input Message dec qm))) *)
(* Goal: D_queue (input Message (inc_dec s0) (input Message dec qm)) *)
  apply not_D_queue.
(* Goal: not (@eq Message (inc_dec s0) dec) *)
(* Goal: D_queue (input Message (inc_dec s0) (input Message dec qm)) *)
  discriminate.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  auto.
  
(* Goal: Z.ge (reduce Message dec_count (input Message m qm)) (reduce Message new_inc_count (input Message m qm)) *)
(* Goal: Z.ge (reduce Message dec_count (input Message (inc_dec s0) (input Message dec qm))) (reduce Message new_inc_count (input Message (inc_dec s0) (input Message dec qm))) *)
  generalize H3.
(* Goal: forall _ : D_queue (input Message m qm), Z.ge (reduce Message dec_count (input Message m qm)) (reduce Message new_inc_count (input Message m qm)) *)
(* Goal: Z.ge (reduce Message dec_count (input Message (inc_dec s0) (input Message dec qm))) (reduce Message new_inc_count (input Message (inc_dec s0) (input Message dec qm))) *)
  case m.
(* Goal: forall _ : @eq bool (rt config_init s) false, D_queue (bm config_init s owner) *)
(* Goal: forall (c : Config) (t : class_trans c) (_ : legal c) (_ : forall _ : @eq bool (rt c s) false, D_queue (bm c s owner)) (_ : @eq bool (rt (transition c t) s) false), D_queue (bm (transition c t) s owner) *)
  intro; simpl in |- *.
(* Goal: forall _ : forall s : Site, not (@eq Message copy (inc_dec s)), Z.ge (Z.add (Z.add (reduce Message dec_count qm) Z0) (Zpos xH)) (Z.add (reduce Message new_inc_count qm) Z0) *)
(* Goal: forall (qm : queue Message) (s0 : Site) (_ : alternate qm) (_ : Z.ge (Z.add (reduce Message dec_count qm) (Zpos xH)) (reduce Message new_inc_count qm)), Z.ge (Z.add (reduce Message dec_count (input Message (inc_dec s0) (input Message dec qm))) (Zpos xH)) (reduce Message new_inc_count (input Message (inc_dec s0) (input Message dec qm))) *)
  generalize (count_inc_and_dec_alt_queue qm H1); intro; omega.
  
(* Goal: forall (c : Config) (_ : legal c), Z.ge (st c owner) Z0 *)
  intros.
(* Goal: Z.ge (reduce Message dec_count (input Message (inc_dec s) qm)) (reduce Message new_inc_count (input Message (inc_dec s) qm)) *)
(* Goal: forall _ : D_queue (input Message copy qm), Z.ge (reduce Message dec_count (input Message copy qm)) (reduce Message new_inc_count (input Message copy qm)) *)
(* Goal: Z.ge (reduce Message dec_count (input Message (inc_dec s0) (input Message dec qm))) (reduce Message new_inc_count (input Message (inc_dec s0) (input Message dec qm))) *)
  absurd (D_queue (input Message (inc_dec s) qm)).
(* Goal: not (D_queue (input Message (inc_dec s0) (input Message dec qm))) *)
(* Goal: D_queue (input Message (inc_dec s0) (input Message dec qm)) *)
  apply not_D_queue.
(* Goal: not (@eq Message (inc_dec s0) dec) *)
(* Goal: D_queue (input Message (inc_dec s0) (input Message dec qm)) *)
  discriminate.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  auto.
  
(* Goal: forall (c : Config) (_ : legal c), Z.ge (st c owner) Z0 *)
  intros.
(* Goal: Z.ge (reduce Message dec_count (input Message copy qm)) (reduce Message new_inc_count (input Message copy qm)) *)
(* Goal: Z.ge (reduce Message dec_count (input Message (inc_dec s0) (input Message dec qm))) (reduce Message new_inc_count (input Message (inc_dec s0) (input Message dec qm))) *)
  absurd (D_queue (input Message copy qm)).
(* Goal: not (D_queue (input Message (inc_dec s0) (input Message dec qm))) *)
(* Goal: D_queue (input Message (inc_dec s0) (input Message dec qm)) *)
  apply not_D_queue.
(* Goal: not (@eq Message (inc_dec s0) dec) *)
(* Goal: D_queue (input Message (inc_dec s0) (input Message dec qm)) *)
  discriminate.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  auto.
  
(* Goal: Z.ge (reduce Message dec_count (input Message (inc_dec s0) (input Message dec qm))) (reduce Message new_inc_count (input Message (inc_dec s0) (input Message dec qm))) *)
  absurd (D_queue (input Message (inc_dec s0) (input Message dec qm))).
(* Goal: not (D_queue (input Message (inc_dec s0) (input Message dec qm))) *)
(* Goal: D_queue (input Message (inc_dec s0) (input Message dec qm)) *)
  apply not_D_queue.
(* Goal: not (@eq Message (inc_dec s0) dec) *)
(* Goal: D_queue (input Message (inc_dec s0) (input Message dec qm)) *)
  discriminate.
  
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  auto.
Qed.


Lemma copy_count_is_positive :
 forall q : queue Message, (reduce Message copy_count q >= 0)%Z.
Proof.
(* Goal: forall (c : Config) (_ : legal c), Z.ge (st c owner) Z0 *)
  intros.
(* Goal: Z.ge (reduce Message dec_count q) Z0 *)
  apply reduce_positive_or_null.
(* Goal: forall a : Message, Z.ge (dec_count a) Z0 *)
  intro.
(* Goal: Z.ge (Z.sub (Z.add (Z.add Z0 (reduce Message dec_count (bm c s owner))) (reduce Message copy_count (bm c owner s))) (reduce Message new_inc_count (bm c s owner))) Z0 *)
  elim a; simpl in |- *; intros; omega.
Qed.

Lemma dec_count_is_positive :
 forall q : queue Message, (reduce Message dec_count q >= 0)%Z.
Proof.
(* Goal: forall (c : Config) (_ : legal c), Z.ge (st c owner) Z0 *)
  intros.
(* Goal: Z.ge (reduce Message dec_count q) Z0 *)
  apply reduce_positive_or_null.
(* Goal: forall a : Message, Z.ge (dec_count a) Z0 *)
  intro.
(* Goal: Z.ge (Z.sub (Z.add (Z.add Z0 (reduce Message dec_count (bm c s owner))) (reduce Message copy_count (bm c owner s))) (reduce Message new_inc_count (bm c s owner))) Z0 *)
  elim a; simpl in |- *; intros; omega.
Qed.


Lemma invariant5 :
 forall c : Config,
 legal c ->
 forall s : Site,
 (Int (rt c s) + reduce Message dec_count (bm c s owner) +
  reduce Message copy_count (bm c owner s) -
  reduce Message new_inc_count (bm c s owner) >= 0)%Z.

Proof.
(* Goal: forall (c : Config) (_ : legal c), Z.ge (st c owner) Z0 *)
  intros.
(* Goal: forall a : Message, Z.ge (dec_count a) Z0 *)
  generalize (copy_count_is_positive (bm c owner s)); intro.
(* Goal: forall a : Message, Z.ge (dec_count a) Z0 *)
  generalize (legal_alternate s c H); intro.
(* Goal: Z.ge (Z.sub (Z.add (Z.add (Int (rt c s)) (reduce Message dec_count (bm c s owner))) (reduce Message copy_count (bm c owner s))) (reduce Message new_inc_count (bm c s owner))) Z0 *)
  case (eq_bool_dec (rt c s)); intro; rewrite e; unfold Int in |- *.
(* Goal: forall a : Message, Z.ge (dec_count a) Z0 *)
  generalize (count_inc_and_dec_alt_queue (bm c s owner) H1); intro.
(* Goal: Z.ge (Z.sub (Z.add (Z.add Z0 (reduce Message dec_count (bm c s owner))) (reduce Message copy_count (bm c owner s))) (reduce Message new_inc_count (bm c s owner))) Z0 *)
  omega.
  
(* Goal: forall a : Message, Z.ge (dec_count a) Z0 *)
  generalize (false_rt_D_queue c s H e); intro.
(* Goal: forall a : Message, Z.ge (dec_count a) Z0 *)
  generalize (count_inc_and_dec_D_queue (bm c s owner) H1 H2); intro.
(* Goal: Z.ge (Z.sub (Z.add (Z.add Z0 (reduce Message dec_count (bm c s owner))) (reduce Message copy_count (bm c owner s))) (reduce Message new_inc_count (bm c s owner))) Z0 *)
  omega.
Qed.




Let Recv_T := Site -> bool.

Lemma other_definition_for_sigma_receive_table :
 forall t : Recv_T,
 sigma_receive_table t =
 sigma_table Site LS Z (Z_id Site) (fun s : Site => Int (t s)).
Proof.
(* Goal: forall (c : Config) (_ : legal c), Z.ge (st c owner) Z0 *)
  intros.
(* Goal: @eq Z (sigma_receive_table t) (sigma_table Site LS Z (Z_id Site) (fun s : Site => Int (t s))) *)
  unfold sigma_receive_table in |- *.
(* Goal: Z.ge (sigma_table Site LS Z (Z_id Site) (fun_minus Site (fun_sum Site (fun_sum Site (fun s : Site => Int (rt c s)) (fun s1 : Site => reduce Message dec_count (bm c s1 owner))) (fun s2 : Site => reduce Message copy_count (bm c owner s2))) (fun s1 : Site => reduce Message new_inc_count (bm c s1 owner)))) Z0 *)
  unfold sigma_table in |- *.
(* Goal: forall x_ : Site, Z.ge (Z_id Site x_ (fun_minus Site (fun_sum Site (fun_sum Site (fun s : Site => Int (rt c s)) (fun s1 : Site => reduce Message dec_count (bm c s1 owner))) (fun s2 : Site => reduce Message copy_count (bm c owner s2))) (fun s1 : Site => reduce Message new_inc_count (bm c s1 owner)) x_)) Z0 *)
  unfold Z_id in |- *.
(* Goal: @eq Z (sigma Site LS (fun s : Site => Int (t s))) (sigma Site LS (fun s : Site => Int (t s))) *)
  auto.
Qed.


Lemma owner_st_positive : forall c : Config, legal c -> (st c owner >= 0)%Z.
Proof.
(* Goal: forall (c : Config) (_ : legal c), Z.ge (st c owner) Z0 *)
  intros.
(* Goal: Z.ge (st c owner) Z0 *)
  rewrite (invariant4 c H).
(* Goal: Z.ge (Z.sub (Z.add (Z.add (sigma_receive_table (rt c)) (sigma_table Site LS Z (Z_id Site) (fun s1 : Site => reduce Message dec_count (bm c s1 owner)))) (sigma_table Site LS Z (Z_id Site) (fun s2 : Site => reduce Message copy_count (bm c owner s2)))) (sigma_table Site LS Z (Z_id Site) (fun s1 : Site => reduce Message new_inc_count (bm c s1 owner)))) Z0 *)
  rewrite other_definition_for_sigma_receive_table.
(* Goal: Z.ge (Z.sub (Z.add (sigma_table Site LS Z (Z_id Site) (fun_sum Site (fun s : Site => Int (rt c s)) (fun s1 : Site => reduce Message dec_count (bm c s1 owner)))) (sigma_table Site LS Z (Z_id Site) (fun s2 : Site => reduce Message copy_count (bm c owner s2)))) (sigma_table Site LS Z (Z_id Site) (fun s1 : Site => reduce Message new_inc_count (bm c s1 owner)))) Z0 *)
  rewrite <- sigma_same_table.
(* Goal: Z.ge (Z.sub (Z.add (sigma_table Site LS Z (Z_id Site) (fun_sum Site (fun s : Site => Int (rt c s)) (fun s1 : Site => reduce Message dec_count (bm c s1 owner)))) (sigma_table Site LS Z (Z_id Site) (fun s2 : Site => reduce Message copy_count (bm c owner s2)))) (sigma_table Site LS Z (Z_id Site) (fun s1 : Site => reduce Message new_inc_count (bm c s1 owner)))) Z0 *)
  rewrite <- sigma_same_table.
(* Goal: Z.ge (Z.sub (sigma_table Site LS Z (Z_id Site) (fun_sum Site (fun_sum Site (fun s : Site => Int (rt c s)) (fun s1 : Site => reduce Message dec_count (bm c s1 owner))) (fun s2 : Site => reduce Message copy_count (bm c owner s2)))) (sigma_table Site LS Z (Z_id Site) (fun s1 : Site => reduce Message new_inc_count (bm c s1 owner)))) Z0 *)
  rewrite <- sigma_same_table2.
(* Goal: Z.ge (sigma_table Site LS Z (Z_id Site) (fun_minus Site (fun_sum Site (fun_sum Site (fun s : Site => Int (rt c s)) (fun s1 : Site => reduce Message dec_count (bm c s1 owner))) (fun s2 : Site => reduce Message copy_count (bm c owner s2))) (fun s1 : Site => reduce Message new_inc_count (bm c s1 owner)))) Z0 *)
  unfold sigma_table in |- *.
(* Goal: Z.ge (sigma Site LS (fun s : Site => Z_id Site s (fun_minus Site (fun_sum Site (fun_sum Site (fun s0 : Site => Int (rt c s0)) (fun s1 : Site => reduce Message dec_count (bm c s1 owner))) (fun s2 : Site => reduce Message copy_count (bm c owner s2))) (fun s1 : Site => reduce Message new_inc_count (bm c s1 owner)) s))) Z0 *)
  apply sigma_pos.
(* Goal: forall x_ : Site, Z.ge (Z_id Site x_ (fun_minus Site (fun_sum Site (fun_sum Site (fun s : Site => Int (rt c s)) (fun s1 : Site => reduce Message dec_count (bm c s1 owner))) (fun s2 : Site => reduce Message copy_count (bm c owner s2))) (fun s1 : Site => reduce Message new_inc_count (bm c s1 owner)) x_)) Z0 *)
  unfold Z_id in |- *.
(* Goal: forall x_ : Site, Z.ge (fun_minus Site (fun_sum Site (fun_sum Site (fun s : Site => Int (rt c s)) (fun s1 : Site => reduce Message dec_count (bm c s1 owner))) (fun s2 : Site => reduce Message copy_count (bm c owner s2))) (fun s1 : Site => reduce Message new_inc_count (bm c s1 owner)) x_) Z0 *)
  unfold fun_minus in |- *.
(* Goal: forall x_ : Site, Z.ge (Z.sub (fun_sum Site (fun_sum Site (fun s : Site => Int (rt c s)) (fun s1 : Site => reduce Message dec_count (bm c s1 owner))) (fun s2 : Site => reduce Message copy_count (bm c owner s2)) x_) (reduce Message new_inc_count (bm c x_ owner))) Z0 *)
  unfold fun_sum in |- *.
(* Goal: forall x_ : Site, Z.ge (Z.sub (Z.add (Z.add (Int (rt c x_)) (reduce Message dec_count (bm c x_ owner))) (reduce Message copy_count (bm c owner x_))) (reduce Message new_inc_count (bm c x_ owner))) Z0 *)
  exact (invariant5 c H).
Qed.



End INVARIANT5.
