Commit to revert
authorzhangx
Wed, 03 Feb 2016 22:17:29 +0800
changeset 105 0c89419b4742
parent 104 43482ab31341
child 106 5454387e42ce
Commit to revert
CpsG.thy
ExtGG.thy
PrioG.thy
--- a/CpsG.thy	Wed Feb 03 21:51:57 2016 +0800
+++ b/CpsG.thy	Wed Feb 03 22:17:29 2016 +0800
@@ -1,7 +1,9 @@
-theory CpsG
+theory PIPBasics
 imports PIPDefs
 begin
 
+section {* Generic aulxiliary lemmas *}
+
 lemma f_image_eq:
   assumes h: "\<And> a. a \<in> A \<Longrightarrow> f a = g a"
   shows "f ` A = g ` A"
@@ -84,6 +86,14 @@
   finally show ?thesis by simp
 qed
 
+lemma rel_eqI:
+  assumes "\<And> x y. (x,y) \<in> A \<Longrightarrow> (x,y) \<in> B"
+  and "\<And> x y. (x,y) \<in> B \<Longrightarrow> (x, y) \<in> A"
+  shows "A = B"
+  using assms by auto
+
+section {* Lemmas do not depend on trace validity *}
+
 lemma birth_time_lt:  
   assumes "s \<noteq> []"
   shows "last_set th s < length s"
@@ -152,23 +162,286 @@
   thus ?thesis by (unfold cp_eq_cpreced cpreced_def, fold the_preced_def, simp)
 qed
 
+lemma RAG_target_th: "(Th th, x) \<in> RAG (s::state) \<Longrightarrow> \<exists> cs. x = Cs cs"
+  by (unfold s_RAG_def, auto)
+
+lemma waiting_eq: "waiting s th cs = waiting (wq s) th cs"
+  by  (unfold s_waiting_def cs_waiting_def wq_def, auto)
+
+lemma holding_eq: "holding (s::state) th cs = holding (wq s) th cs"
+  by (unfold s_holding_def wq_def cs_holding_def, simp)
+
+lemma children_RAG_alt_def:
+  "children (RAG (s::state)) (Th th) = Cs ` {cs. holding s th cs}"
+  by (unfold s_RAG_def, auto simp:children_def holding_eq)
+
+lemma holdents_alt_def:
+  "holdents s th = the_cs ` (children (RAG (s::state)) (Th th))"
+  by (unfold children_RAG_alt_def holdents_def, simp add: image_image)
+
+lemma cntCS_alt_def:
+  "cntCS s th = card (children (RAG s) (Th th))"
+  apply (unfold children_RAG_alt_def cntCS_def holdents_def)
+  by (rule card_image[symmetric], auto simp:inj_on_def)
+
+lemma runing_ready: 
+  shows "runing s \<subseteq> readys s"
+  unfolding runing_def readys_def
+  by auto 
+
+lemma readys_threads:
+  shows "readys s \<subseteq> threads s"
+  unfolding readys_def
+  by auto
+
+lemma wq_v_neq [simp]:
+   "cs \<noteq> cs' \<Longrightarrow> wq (V thread cs#s) cs' = wq s cs'"
+  by (auto simp:wq_def Let_def cp_def split:list.splits)
+
+lemma runing_head:
+  assumes "th \<in> runing s"
+  and "th \<in> set (wq_fun (schs s) cs)"
+  shows "th = hd (wq_fun (schs s) cs)"
+  using assms
+  by (simp add:runing_def readys_def s_waiting_def wq_def)
+
+lemma runing_wqE:
+  assumes "th \<in> runing s"
+  and "th \<in> set (wq s cs)"
+  obtains rest where "wq s cs = th#rest"
+proof -
+  from assms(2) obtain th' rest where eq_wq: "wq s cs = th'#rest"
+    by (meson list.set_cases)
+  have "th' = th"
+  proof(rule ccontr)
+    assume "th' \<noteq> th"
+    hence "th \<noteq> hd (wq s cs)" using eq_wq by auto 
+    with assms(2)
+    have "waiting s th cs" 
+      by (unfold s_waiting_def, fold wq_def, auto)
+    with assms show False 
+      by (unfold runing_def readys_def, auto)
+  qed
+  with eq_wq that show ?thesis by metis
+qed
+
+lemma isP_E:
+  assumes "isP e"
+  obtains cs where "e = P (actor e) cs"
+  using assms by (cases e, auto)
+
+lemma isV_E:
+  assumes "isV e"
+  obtains cs where "e = V (actor e) cs"
+  using assms by (cases e, auto) 
+
+
+text {*
+  Every thread can only be blocked on one critical resource, 
+  symmetrically, every critical resource can only be held by one thread. 
+  This fact is much more easier according to our definition. 
+*}
+lemma held_unique:
+  assumes "holding (s::event list) th1 cs"
+  and "holding s th2 cs"
+  shows "th1 = th2"
+ by (insert assms, unfold s_holding_def, auto)
+
+lemma last_set_lt: "th \<in> threads s \<Longrightarrow> last_set th s < length s"
+  apply (induct s, auto)
+  by (case_tac a, auto split:if_splits)
+
+lemma last_set_unique: 
+  "\<lbrakk>last_set th1 s = last_set th2 s; th1 \<in> threads s; th2 \<in> threads s\<rbrakk>
+          \<Longrightarrow> th1 = th2"
+  apply (induct s, auto)
+  by (case_tac a, auto split:if_splits dest:last_set_lt)
+
+lemma preced_unique : 
+  assumes pcd_eq: "preced th1 s = preced th2 s"
+  and th_in1: "th1 \<in> threads s"
+  and th_in2: " th2 \<in> threads s"
+  shows "th1 = th2"
+proof -
+  from pcd_eq have "last_set th1 s = last_set th2 s" by (simp add:preced_def)
+  from last_set_unique [OF this th_in1 th_in2]
+  show ?thesis .
+qed
+                      
+lemma preced_linorder: 
+  assumes neq_12: "th1 \<noteq> th2"
+  and th_in1: "th1 \<in> threads s"
+  and th_in2: " th2 \<in> threads s"
+  shows "preced th1 s < preced th2 s \<or> preced th1 s > preced th2 s"
+proof -
+  from preced_unique [OF _ th_in1 th_in2] and neq_12 
+  have "preced th1 s \<noteq> preced th2 s" by auto
+  thus ?thesis by auto
+qed
+
+lemma in_RAG_E:
+  assumes "(n1, n2) \<in> RAG (s::state)"
+  obtains (waiting) th cs where "n1 = Th th" "n2 = Cs cs" "waiting s th cs"
+      | (holding) th cs where "n1 = Cs cs" "n2 = Th th" "holding s th cs"
+  using assms[unfolded s_RAG_def, folded waiting_eq holding_eq]
+  by auto
+
+lemma count_rec1 [simp]: 
+  assumes "Q e"
+  shows "count Q (e#es) = Suc (count Q es)"
+  using assms
+  by (unfold count_def, auto)
+
+lemma count_rec2 [simp]: 
+  assumes "\<not>Q e"
+  shows "count Q (e#es) = (count Q es)"
+  using assms
+  by (unfold count_def, auto)
+
+lemma count_rec3 [simp]: 
+  shows "count Q [] =  0"
+  by (unfold count_def, auto)
+
+lemma cntP_simp1[simp]:
+  "cntP (P th cs'#s) th = cntP s th + 1"
+  by (unfold cntP_def, simp)
+
+lemma cntP_simp2[simp]:
+  assumes "th' \<noteq> th"
+  shows "cntP (P th cs'#s) th' = cntP s th'"
+  using assms
+  by (unfold cntP_def, simp)
+
+lemma cntP_simp3[simp]:
+  assumes "\<not> isP e"
+  shows "cntP (e#s) th' = cntP s th'"
+  using assms
+  by (unfold cntP_def, cases e, simp+)
+
+lemma cntV_simp1[simp]:
+  "cntV (V th cs'#s) th = cntV s th + 1"
+  by (unfold cntV_def, simp)
+
+lemma cntV_simp2[simp]:
+  assumes "th' \<noteq> th"
+  shows "cntV (V th cs'#s) th' = cntV s th'"
+  using assms
+  by (unfold cntV_def, simp)
+
+lemma cntV_simp3[simp]:
+  assumes "\<not> isV e"
+  shows "cntV (e#s) th' = cntV s th'"
+  using assms
+  by (unfold cntV_def, cases e, simp+)
+
+lemma cntP_diff_inv:
+  assumes "cntP (e#s) th \<noteq> cntP s th"
+  shows "isP e \<and> actor e = th"
+proof(cases e)
+  case (P th' pty)
+  show ?thesis
+  by (cases "(\<lambda>e. \<exists>cs. e = P th cs) (P th' pty)", 
+        insert assms P, auto simp:cntP_def)
+qed (insert assms, auto simp:cntP_def)
+  
+lemma cntV_diff_inv:
+  assumes "cntV (e#s) th \<noteq> cntV s th"
+  shows "isV e \<and> actor e = th"
+proof(cases e)
+  case (V th' pty)
+  show ?thesis
+  by (cases "(\<lambda>e. \<exists>cs. e = V th cs) (V th' pty)", 
+        insert assms V, auto simp:cntV_def)
+qed (insert assms, auto simp:cntV_def)
+
+lemma eq_dependants: "dependants (wq s) = dependants s"
+  by (simp add: s_dependants_abv wq_def)
+
+lemma inj_the_preced: 
+  "inj_on (the_preced s) (threads s)"
+  by (metis inj_onI preced_unique the_preced_def)
+
+lemma holding_next_thI:
+  assumes "holding s th cs"
+  and "length (wq s cs) > 1"
+  obtains th' where "next_th s th cs th'"
+proof -
+  from assms(1)[folded holding_eq, unfolded cs_holding_def]
+  have " th \<in> set (wq s cs) \<and> th = hd (wq s cs)" 
+    by (unfold s_holding_def, fold wq_def, auto)
+  then obtain rest where h1: "wq s cs = th#rest" 
+    by (cases "wq s cs", auto)
+  with assms(2) have h2: "rest \<noteq> []" by auto
+  let ?th' = "hd (SOME q. distinct q \<and> set q = set rest)"
+  have "next_th s th cs ?th'" using  h1(1) h2 
+    by (unfold next_th_def, auto)
+  from that[OF this] show ?thesis .
+qed
+
 (* ccc *)
 
-
+section {* Locales used to investigate the execution of PIP *}
+
+text {* 
+  The following locale @{text valid_trace} is used to constrain the 
+  trace to be valid. All properties hold for valid traces are 
+  derived under this locale. 
+*}
 locale valid_trace = 
   fixes s
   assumes vt : "vt s"
 
+text {* 
+  The following locale @{text valid_trace_e} describes 
+  the valid extension of a valid trace. The event @{text "e"}
+  represents an event in the system, which corresponds 
+  to a one step operation of the PIP protocol. 
+  It is required that @{text "e"} is an event eligible to happen
+  under state @{text "s"}, which is already required to be valid
+  by the parent locale @{text "valid_trace"}.
+
+  This locale is used to investigate one step execution of PIP, 
+  properties concerning the effects of @{text "e"}'s execution, 
+  for example, how the values of observation functions are changed, 
+  or how desirable properties are kept invariant, are derived
+  under this locale. The state before execution is @{text "s"}, while
+  the state after execution is @{text "e#s"}. Therefore, the lemmas 
+  derived usually relate observations on @{text "e#s"} to those 
+  on @{text "s"}.
+*}
+
 locale valid_trace_e = valid_trace +
   fixes e
   assumes vt_e: "vt (e#s)"
 begin
 
+text {*
+  The following lemma shows that @{text "e"} must be a 
+  eligible event (or a valid step) to be taken under
+  the state represented by @{text "s"}.
+*}
 lemma pip_e: "PIP s e"
   using vt_e by (cases, simp)  
 
 end
 
+text {*
+  Because @{term "e#s"} is also a valid trace, properties 
+  derived for valid trace @{term s} also hold on @{term "e#s"}.
+*}
+sublocale valid_trace_e < vat_es!: valid_trace "e#s" 
+  using vt_e
+  by (unfold_locales, simp)
+
+text {*
+  For each specific event (or operation), there is a sublocale
+  further constraining that the event @{text e} to be that 
+  particular event. 
+
+  For example, the following 
+  locale @{text "valid_trace_create"} is the sublocale for 
+  event @{term "Create"}:
+*}
 locale valid_trace_create = valid_trace_e + 
   fixes th prio
   assumes is_create: "e = Create th prio"
@@ -181,27 +454,145 @@
   fixes th cs
   assumes is_p: "e = P th cs"
 
+text {*
+  locale @{text "valid_trace_p"} is divided further into two 
+  sublocales, namely, @{text "valid_trace_p_h"} 
+  and @{text "valid_trace_p_w"}.
+*}
+
+text {*
+  The following two sublocales @{text "valid_trace_p_h"}
+  and @{text "valid_trace_p_w"} represent two complementary 
+  cases under @{text "valid_trace_p"}, where
+  @{text "valid_trace_p_h"} further constraints that
+  @{text "wq s cs = []"}, which means the waiting queue of 
+  the requested resource @{text "cs"} is empty, in which
+  case,  the requesting thread @{text "th"} 
+  will take hold of @{text "cs"}. 
+
+  Opposite to @{text "valid_trace_p_h"},
+  @{text "valid_trace_p_w"} constraints that
+  @{text "wq s cs \<noteq> []"}, which means the waiting queue of 
+  the requested resource @{text "cs"} is nonempty, in which
+  case,  the requesting thread @{text "th"} will be blocked
+  on @{text "cs"}: 
+
+  Peculiar properties will be derived under respective 
+  locales.
+*}
+
+locale valid_trace_p_h = valid_trace_p +
+  assumes we: "wq s cs = []"
+
+locale valid_trace_p_w = valid_trace_p +
+  assumes wne: "wq s cs \<noteq> []"
+begin
+
+text {*
+  The following @{text "holder"} designates
+  the holder of @{text "cs"} before the @{text "P"}-operation.
+*}
+definition "holder = hd (wq s cs)"
+
+text {*
+  The following @{text "waiters"} designates
+  the list of threads waiting for @{text "cs"} 
+  before the @{text "P"}-operation.
+*}
+definition "waiters = tl (wq s cs)"
+end
+
+text {* 
+  @{text "valid_trace_v"} is set for the @{term V}-operation.
+*}
 locale valid_trace_v = valid_trace_e + 
   fixes th cs
   assumes is_v: "e = V th cs"
 begin
+  -- {* The following @{text "rest"} is the tail of 
+        waiting queue of the resource @{text "cs"}
+        to be released by this @{text "V"}-operation.
+     *}
   definition "rest = tl (wq s cs)"
+
+  text {*
+    The following @{text "wq'"} is the waiting
+    queue of @{term "cs"}
+    after the @{text "V"}-operation, which
+    is simply a reordering of @{term "rest"}. 
+
+    The effect of this reordering needs to be 
+    understood by two cases:
+    \begin{enumerate}
+    \item When @{text "rest = []"},
+    the reordering gives rise to an empty list as well, 
+    which means there is no thread holding or waiting 
+    for resource @{term "cs"}, therefore, it is free.
+
+    \item When @{text "rest \<noteq> []"}, the effect of 
+    this reordering is to arbitrarily 
+    switch one thread in @{term "rest"} to the 
+    head, which, by definition take over the hold
+    of @{term "cs"} and is designated by @{text "taker"}
+    in the following sublocale @{text "valid_trace_v_n"}.
+  *}
   definition "wq' = (SOME q. distinct q \<and> set q = set rest)"
+
+  text {* 
+  The following @{text "rest'"} is the tail of the 
+  waiting queue after the @{text "V"}-operation. 
+  It plays only auxiliary role to ease reasoning. 
+  *}
+  definition "rest' = tl wq'"
+
 end
 
+text {* 
+  In the following, @{text "valid_trace_v"} is also 
+  divided into two 
+  sublocales: when @{text "rest"} is empty (represented
+  by @{text "valid_trace_v_e"}), which means, there is no thread waiting 
+  for @{text "cs"}, therefore, after the @{text "V"}-operation, 
+  it will become free; otherwise (represented 
+  by @{text "valid_trace_v_n"}), one thread 
+  will be picked from those in @{text "rest"} to take 
+  over @{text "cs"}.
+*}
+
+locale valid_trace_v_e = valid_trace_v +
+  assumes rest_nil: "rest = []"
+
 locale valid_trace_v_n = valid_trace_v +
   assumes rest_nnl: "rest \<noteq> []"
-
-locale valid_trace_v_e = valid_trace_v +
-  assumes rest_nil: "rest = []"
-
-locale valid_trace_set= valid_trace_e + 
+begin
+
+text {* 
+  The following @{text "taker"} is the thread to 
+  take over @{text "cs"}. 
+*}
+  definition "taker = hd wq'"
+
+end
+
+
+locale valid_trace_set = valid_trace_e + 
   fixes th prio
   assumes is_set: "e = Set th prio"
 
 context valid_trace
 begin
 
+text {*
+  Induction rule introduced to easy the 
+  derivation of properties for valid trace @{term "s"}.
+  One more premises, namely @{term "valid_trace_e s e"}
+  is added, so that an interpretation of 
+  @{text "valid_trace_e"} can be instantiated 
+  so that all properties derived so far becomes 
+  available in the proof of induction step.
+
+  You will see its use in the proofs that follows.
+*}
 lemma ind [consumes 0, case_names Nil Cons, induct type]:
   assumes "PP []"
      and "(\<And>s e. valid_trace_e s e \<Longrightarrow>
@@ -222,6 +613,11 @@
   qed
 qed
 
+text {*
+  The following lemma says that if @{text "s"} is a valid state, so 
+  is its any postfix. Where @{term "monent t s"} is the postfix of 
+  @{term "s"} with length @{term "t"}.
+*}
 lemma  vt_moment: "\<And> t. vt (moment t s)"
 proof(induct rule:ind)
   case Nil
@@ -246,78 +642,43 @@
     ultimately show ?thesis by simp
   qed
 qed
-
-lemma finite_threads:
-  shows "finite (threads s)"
-using vt by (induct) (auto elim: step.cases)
-
 end
 
-lemma RAG_target_th: "(Th th, x) \<in> RAG (s::state) \<Longrightarrow> \<exists> cs. x = Cs cs"
-  by (unfold s_RAG_def, auto)
-
+text {*
+  The following locale @{text "valid_moment"} is to inherit the properties 
+  derived on any valid state to the prefix of it, with length @{text "i"}.
+*}
 locale valid_moment = valid_trace + 
   fixes i :: nat
 
-sublocale valid_moment < vat_moment: valid_trace "(moment i s)"
+sublocale valid_moment < vat_moment!: valid_trace "(moment i s)"
   by (unfold_locales, insert vt_moment, auto)
 
-lemma waiting_eq: "waiting s th cs = waiting (wq s) th cs"
-  by  (unfold s_waiting_def cs_waiting_def wq_def, auto)
-
-lemma holding_eq: "holding (s::state) th cs = holding (wq s) th cs"
-  by (unfold s_holding_def wq_def cs_holding_def, simp)
-
-lemma runing_ready: 
-  shows "runing s \<subseteq> readys s"
-  unfolding runing_def readys_def
-  by auto 
-
-lemma readys_threads:
-  shows "readys s \<subseteq> threads s"
-  unfolding readys_def
-  by auto
-
-lemma wq_v_neq [simp]:
-   "cs \<noteq> cs' \<Longrightarrow> wq (V thread cs#s) cs' = wq s cs'"
-  by (auto simp:wq_def Let_def cp_def split:list.splits)
-
-lemma runing_head:
-  assumes "th \<in> runing s"
-  and "th \<in> set (wq_fun (schs s) cs)"
-  shows "th = hd (wq_fun (schs s) cs)"
-  using assms
-  by (simp add:runing_def readys_def s_waiting_def wq_def)
-
-context valid_trace
+locale valid_moment_e = valid_moment +
+  assumes less_i: "i < length s"
 begin
-
-lemma runing_wqE:
-  assumes "th \<in> runing s"
-  and "th \<in> set (wq s cs)"
-  obtains rest where "wq s cs = th#rest"
-proof -
-  from assms(2) obtain th' rest where eq_wq: "wq s cs = th'#rest"
-    by (meson list.set_cases)
-  have "th' = th"
-  proof(rule ccontr)
-    assume "th' \<noteq> th"
-    hence "th \<noteq> hd (wq s cs)" using eq_wq by auto 
-    with assms(2)
-    have "waiting s th cs" 
-      by (unfold s_waiting_def, fold wq_def, auto)
-    with assms show False 
-      by (unfold runing_def readys_def, auto)
-  qed
-  with eq_wq that show ?thesis by metis
-qed
+  definition "next_e  = hd (moment (Suc i) s)"
+
+  lemma trace_e: 
+    "moment (Suc i) s = next_e#moment i s"
+   proof -
+    from less_i have "Suc i \<le> length s" by auto
+    from moment_plus[OF this, folded next_e_def]
+    show ?thesis .
+   qed
 
 end
 
+sublocale valid_moment_e < vat_moment_e!: valid_trace_e "moment i s" "next_e"
+  using vt_moment[of "Suc i", unfolded trace_e]
+  by (unfold_locales, simp)
+
+section {* Distinctiveness of waiting queues *}
+
 context valid_trace_create
 begin
 
-lemma wq_neq_simp [simp]:
+lemma wq_kept [simp]:
   shows "wq (e#s) cs' = wq s cs'"
     using assms unfolding is_create wq_def
   by (auto simp:Let_def)
@@ -331,7 +692,7 @@
 context valid_trace_exit
 begin
 
-lemma wq_neq_simp [simp]:
+lemma wq_kept [simp]:
   shows "wq (e#s) cs' = wq s cs'"
     using assms unfolding is_exit wq_def
   by (auto simp:Let_def)
@@ -342,7 +703,7 @@
   using assms by simp
 end
 
-context valid_trace_p
+context valid_trace_p 
 begin
 
 lemma wq_neq_simp [simp]:
@@ -358,31 +719,6 @@
   show ?thesis by (cases, simp)
 qed
 
-lemma ready_th_s: "th \<in> readys s"
-  using runing_th_s
-  by (unfold runing_def, auto)
-
-lemma live_th_s: "th \<in> threads s"
-  using readys_threads ready_th_s by auto
-
-lemma live_th_es: "th \<in> threads (e#s)"
-  using live_th_s 
-  by (unfold is_p, simp)
-
-lemma th_not_waiting: 
-  "\<not> waiting s th c"
-proof -
-  have "th \<in> readys s"
-    using runing_ready runing_th_s by blast 
-  thus ?thesis
-    by (unfold readys_def, auto)
-qed
-
-lemma waiting_neq_th: 
-  assumes "waiting s t c"
-  shows "t \<noteq> th"
-  using assms using th_not_waiting by blast 
-
 lemma th_not_in_wq: 
   shows "th \<notin> set (wq s cs)"
 proof
@@ -426,27 +762,6 @@
     using assms unfolding is_v wq_def
   by (auto simp:Let_def)
 
-lemma runing_th_s:
-  shows "th \<in> runing s"
-proof -
-  from pip_e[unfolded is_v]
-  show ?thesis by (cases, simp)
-qed
-
-lemma th_not_waiting: 
-  "\<not> waiting s th c"
-proof -
-  have "th \<in> readys s"
-    using runing_ready runing_th_s by blast 
-  thus ?thesis
-    by (unfold readys_def, auto)
-qed
-
-lemma waiting_neq_th: 
-  assumes "waiting s t c"
-  shows "t \<noteq> th"
-  using assms using th_not_waiting by blast 
-
 lemma wq_s_cs:
   "wq s cs = th#rest"
 proof -
@@ -482,7 +797,7 @@
 context valid_trace_set
 begin
 
-lemma wq_neq_simp [simp]:
+lemma wq_kept [simp]:
   shows "wq (e#s) cs' = wq s cs'"
     using assms unfolding is_set wq_def
   by (auto simp:Let_def)
@@ -496,22 +811,12 @@
 context valid_trace
 begin
 
-lemma actor_inv: 
-  assumes "PIP s e"
-  and "\<not> isCreate e"
-  shows "actor e \<in> runing s"
-  using assms
-  by (induct, auto)
-
-lemma isP_E:
-  assumes "isP e"
-  obtains cs where "e = P (actor e) cs"
-  using assms by (cases e, auto)
-
-lemma isV_E:
-  assumes "isV e"
-  obtains cs where "e = V (actor e) cs"
-  using assms by (cases e, auto) 
+lemma  finite_threads:
+  shows "finite (threads s)"
+  using vt by (induct) (auto elim: step.cases)
+
+lemma finite_readys [simp]: "finite (readys s)"
+  using finite_threads readys_threads rev_finite_subset by blast
 
 lemma wq_distinct: "distinct (wq s cs)"
 proof(induct rule:ind)
@@ -546,46 +851,11 @@
 
 end
 
+section {* Waiting queues and threads *}
+
 context valid_trace_e
 begin
 
-text {*
-  The following lemma shows that only the @{text "P"}
-  operation can add new thread into waiting queues. 
-  Such kind of lemmas are very obvious, but need to be checked formally.
-  This is a kind of confirmation that our modelling is correct.
-*}
-
-lemma wq_in_inv: 
-  assumes s_ni: "thread \<notin> set (wq s cs)"
-  and s_i: "thread \<in> set (wq (e#s) cs)"
-  shows "e = P thread cs"
-proof(cases e)
-  -- {* This is the only non-trivial case: *}
-  case (V th cs1)
-  have False
-  proof(cases "cs1 = cs")
-    case True
-    show ?thesis
-    proof(cases "(wq s cs1)")
-      case (Cons w_hd w_tl)
-      have "set (wq (e#s) cs) \<subseteq> set (wq s cs)"
-      proof -
-        have "(wq (e#s) cs) = (SOME q. distinct q \<and> set q = set w_tl)"
-          using  Cons V by (auto simp:wq_def Let_def True split:if_splits)
-        moreover have "set ... \<subseteq> set (wq s cs)"
-        proof(rule someI2)
-          show "distinct w_tl \<and> set w_tl = set w_tl"
-            by (metis distinct.simps(2) local.Cons wq_distinct)
-        qed (insert Cons True, auto)
-        ultimately show ?thesis by simp
-      qed
-      with assms show ?thesis by auto
-    qed (insert assms V True, auto simp:wq_def Let_def split:if_splits)
-  qed (insert assms V, auto simp:wq_def Let_def split:if_splits)
-  thus ?thesis by auto
-qed (insert assms, auto simp:wq_def Let_def split:if_splits)
-
 lemma wq_out_inv: 
   assumes s_in: "thread \<in> set (wq s cs)"
   and s_hd: "thread = hd (wq s cs)"
@@ -619,248 +889,173 @@
   qed (insert assms P, auto simp:wq_def Let_def split:if_splits)
 qed (insert assms, auto simp:wq_def Let_def split:if_splits)
 
+lemma wq_in_inv: 
+  assumes s_ni: "thread \<notin> set (wq s cs)"
+  and s_i: "thread \<in> set (wq (e#s) cs)"
+  shows "e = P thread cs"
+proof(cases e)
+  -- {* This is the only non-trivial case: *}
+  case (V th cs1)
+  have False
+  proof(cases "cs1 = cs")
+    case True
+    show ?thesis
+    proof(cases "(wq s cs1)")
+      case (Cons w_hd w_tl)
+      have "set (wq (e#s) cs) \<subseteq> set (wq s cs)"
+      proof -
+        have "(wq (e#s) cs) = (SOME q. distinct q \<and> set q = set w_tl)"
+          using  Cons V by (auto simp:wq_def Let_def True split:if_splits)
+        moreover have "set ... \<subseteq> set (wq s cs)"
+        proof(rule someI2)
+          show "distinct w_tl \<and> set w_tl = set w_tl"
+            by (metis distinct.simps(2) local.Cons wq_distinct)
+        qed (insert Cons True, auto)
+        ultimately show ?thesis by simp
+      qed
+      with assms show ?thesis by auto
+    qed (insert assms V True, auto simp:wq_def Let_def split:if_splits)
+  qed (insert assms V, auto simp:wq_def Let_def split:if_splits)
+  thus ?thesis by auto
+qed (insert assms, auto simp:wq_def Let_def split:if_splits)
+
 end
 
+lemma (in valid_trace_create)
+  th_not_in_threads: "th \<notin> threads s"
+proof -
+  from pip_e[unfolded is_create]
+  show ?thesis by (cases, simp)
+qed
+
+lemma (in valid_trace_create)
+  threads_es [simp]: "threads (e#s) = threads s \<union> {th}"
+  by (unfold is_create, simp)
+
+lemma (in valid_trace_exit)
+  threads_es [simp]: "threads (e#s) = threads s - {th}"
+  by (unfold is_exit, simp)
+
+lemma (in valid_trace_p)
+  threads_es [simp]: "threads (e#s) = threads s"
+  by (unfold is_p, simp)
+
+lemma (in valid_trace_v)
+  threads_es [simp]: "threads (e#s) = threads s"
+  by (unfold is_v, simp)
+
+lemma (in valid_trace_v)
+  th_not_in_rest[simp]: "th \<notin> set rest"
+proof
+  assume otherwise: "th \<in> set rest"
+  have "distinct (wq s cs)" by (simp add: wq_distinct)
+  from this[unfolded wq_s_cs] and otherwise
+  show False by auto
+qed
+
+lemma (in valid_trace_v) distinct_rest: "distinct rest"
+  by (simp add: distinct_tl rest_def wq_distinct)
+
+lemma (in valid_trace_v)
+  set_wq_es_cs [simp]: "set (wq (e#s) cs) = set (wq s cs) - {th}"
+proof(unfold wq_es_cs wq'_def, rule someI2)
+  show "distinct rest \<and> set rest = set rest"
+    by (simp add: distinct_rest) 
+next
+  fix x
+  assume "distinct x \<and> set x = set rest"
+  thus "set x = set (wq s cs) - {th}" 
+      by (unfold wq_s_cs, simp)
+qed
+
+lemma (in valid_trace_exit)
+  th_not_in_wq: "th \<notin> set (wq s cs)"
+proof -
+  from pip_e[unfolded is_exit]
+  show ?thesis
+  by (cases, unfold holdents_def s_holding_def, fold wq_def, 
+             auto elim!:runing_wqE)
+qed
+
+lemma (in valid_trace) wq_threads: 
+  assumes "th \<in> set (wq s cs)"
+  shows "th \<in> threads s"
+  using assms
+proof(induct rule:ind)
+  case (Nil)
+  thus ?case by (auto simp:wq_def)
+next
+  case (Cons s e)
+  interpret vt_e: valid_trace_e s e using Cons by simp
+  show ?case
+  proof(cases e)
+    case (Create th' prio')
+    interpret vt: valid_trace_create s e th' prio'
+      using Create by (unfold_locales, simp)
+    show ?thesis
+      using Cons.hyps(2) Cons.prems by auto
+  next
+    case (Exit th')
+    interpret vt: valid_trace_exit s e th'
+      using Exit by (unfold_locales, simp)
+    show ?thesis
+      using Cons.hyps(2) Cons.prems vt.th_not_in_wq by auto 
+  next
+    case (P th' cs')
+    interpret vt: valid_trace_p s e th' cs'
+      using P by (unfold_locales, simp)
+    show ?thesis
+      using Cons.hyps(2) Cons.prems readys_threads 
+        runing_ready vt.is_p vt.runing_th_s vt_e.wq_in_inv 
+        by fastforce 
+  next
+    case (V th' cs')
+    interpret vt: valid_trace_v s e th' cs'
+      using V by (unfold_locales, simp)
+    show ?thesis using Cons
+      using vt.is_v vt.threads_es vt_e.wq_in_inv by blast
+  next
+    case (Set th' prio)
+    interpret vt: valid_trace_set s e th' prio
+      using Set by (unfold_locales, simp)
+    show ?thesis using Cons.hyps(2) Cons.prems vt.is_set 
+        by (auto simp:wq_def Let_def)
+  qed
+qed 
+
+section {* RAG and threads *}
 
 context valid_trace
 begin
 
-
-text {* (* ddd *)
-  The nature of the work is like this: since it starts from a very simple and basic 
-  model, even intuitively very `basic` and `obvious` properties need to derived from scratch.
-  For instance, the fact 
-  that one thread can not be blocked by two critical resources at the same time
-  is obvious, because only running threads can make new requests, if one is waiting for 
-  a critical resource and get blocked, it can not make another resource request and get 
-  blocked the second time (because it is not running). 
-
-  To derive this fact, one needs to prove by contraction and 
-  reason about time (or @{text "moement"}). The reasoning is based on a generic theorem
-  named @{text "p_split"}, which is about status changing along the time axis. It says if 
-  a condition @{text "Q"} is @{text "True"} at a state @{text "s"},
-  but it was @{text "False"} at the very beginning, then there must exits a moment @{text "t"} 
-  in the history of @{text "s"} (notice that @{text "s"} itself is essentially the history 
-  of events leading to it), such that @{text "Q"} switched 
-  from being @{text "False"} to @{text "True"} and kept being @{text "True"}
-  till the last moment of @{text "s"}.
-
-  Suppose a thread @{text "th"} is blocked
-  on @{text "cs1"} and @{text "cs2"} in some state @{text "s"}, 
-  since no thread is blocked at the very beginning, by applying 
-  @{text "p_split"} to these two blocking facts, there exist 
-  two moments @{text "t1"} and @{text "t2"}  in @{text "s"}, such that 
-  @{text "th"} got blocked on @{text "cs1"} and @{text "cs2"} 
-  and kept on blocked on them respectively ever since.
- 
-  Without lost of generality, we assume @{text "t1"} is earlier than @{text "t2"}.
-  However, since @{text "th"} was blocked ever since memonent @{text "t1"}, so it was still
-  in blocked state at moment @{text "t2"} and could not
-  make any request and get blocked the second time: Contradiction.
-*}
-
-lemma waiting_unique_pre: (* ddd *)
-  assumes h11: "thread \<in> set (wq s cs1)"
-  and h12: "thread \<noteq> hd (wq s cs1)"
-  assumes h21: "thread \<in> set (wq s cs2)"
-  and h22: "thread \<noteq> hd (wq s cs2)"
-  and neq12: "cs1 \<noteq> cs2"
-  shows "False"
+lemma  dm_RAG_threads:
+  assumes in_dom: "(Th th) \<in> Domain (RAG s)"
+  shows "th \<in> threads s"
 proof -
-  let "?Q" = "\<lambda> cs s. thread \<in> set (wq s cs) \<and> thread \<noteq> hd (wq s cs)"
-  from h11 and h12 have q1: "?Q cs1 s" by simp
-  from h21 and h22 have q2: "?Q cs2 s" by simp
-  have nq1: "\<not> ?Q cs1 []" by (simp add:wq_def)
-  have nq2: "\<not> ?Q cs2 []" by (simp add:wq_def)
-  from p_split [of "?Q cs1", OF q1 nq1]
-  obtain t1 where lt1: "t1 < length s"
-    and np1: "\<not> ?Q cs1 (moment t1 s)"
-    and nn1: "(\<forall>i'>t1. ?Q cs1 (moment i' s))" by auto
-  from p_split [of "?Q cs2", OF q2 nq2]
-  obtain t2 where lt2: "t2 < length s"
-    and np2: "\<not> ?Q cs2 (moment t2 s)"
-    and nn2: "(\<forall>i'>t2. ?Q cs2 (moment i' s))" by auto
-  { fix s cs
-    assume q: "?Q cs s"
-    have "thread \<notin> runing s"
-    proof
-      assume "thread \<in> runing s"
-      hence " \<forall>cs. \<not> (thread \<in> set (wq_fun (schs s) cs) \<and> 
-                 thread \<noteq> hd (wq_fun (schs s) cs))"
-        by (unfold runing_def s_waiting_def readys_def, auto)
-      from this[rule_format, of cs] q 
-      show False by (simp add: wq_def) 
-    qed
-  } note q_not_runing = this
-  { fix t1 t2 cs1 cs2
-    assume  lt1: "t1 < length s"
-    and np1: "\<not> ?Q cs1 (moment t1 s)"
-    and nn1: "(\<forall>i'>t1. ?Q cs1 (moment i' s))"
-    and lt2: "t2 < length s"
-    and np2: "\<not> ?Q cs2 (moment t2 s)"
-    and nn2: "(\<forall>i'>t2. ?Q cs2 (moment i' s))"
-    and lt12: "t1 < t2"
-    let ?t3 = "Suc t2"
-    from lt2 have le_t3: "?t3 \<le> length s" by auto
-    from moment_plus [OF this] 
-    obtain e where eq_m: "moment ?t3 s = e#moment t2 s" by auto
-    have "t2 < ?t3" by simp
-    from nn2 [rule_format, OF this] and eq_m
-    have h1: "thread \<in> set (wq (e#moment t2 s) cs2)" and
-         h2: "thread \<noteq> hd (wq (e#moment t2 s) cs2)" by auto
-    have "vt (e#moment t2 s)"
-    proof -
-      from vt_moment 
-      have "vt (moment ?t3 s)" .
-      with eq_m show ?thesis by simp
-    qed
-    then interpret vt_e: valid_trace_e "moment t2 s" "e"
-        by (unfold_locales, auto, cases, simp)
-    have ?thesis
-    proof -
-      have "thread \<in> runing (moment t2 s)"
-      proof(cases "thread \<in> set (wq (moment t2 s) cs2)")
-        case True
-        have "e = V thread cs2"
-        proof -
-          have eq_th: "thread = hd (wq (moment t2 s) cs2)" 
-              using True and np2  by auto 
-          from vt_e.wq_out_inv[OF True this h2]
-          show ?thesis .
-        qed
-        thus ?thesis using vt_e.actor_inv[OF vt_e.pip_e] by auto
-      next
-        case False
-        have "e = P thread cs2" using vt_e.wq_in_inv[OF False h1] .
-        with vt_e.actor_inv[OF vt_e.pip_e]
-        show ?thesis by auto
-      qed
-      moreover have "thread \<notin> runing (moment t2 s)"
-        by (rule q_not_runing[OF nn1[rule_format, OF lt12]])
-      ultimately show ?thesis by simp
-    qed
-  } note lt_case = this
-  show ?thesis
-  proof -
-    { assume "t1 < t2"
-      from lt_case[OF lt1 np1 nn1 lt2 np2 nn2 this]
-      have ?thesis .
-    } moreover {
-      assume "t2 < t1"
-      from lt_case[OF lt2 np2 nn2 lt1 np1 nn1 this]
-      have ?thesis .
-    } moreover {
-      assume eq_12: "t1 = t2"
-      let ?t3 = "Suc t2"
-      from lt2 have le_t3: "?t3 \<le> length s" by auto
-      from moment_plus [OF this] 
-      obtain e where eq_m: "moment ?t3 s = e#moment t2 s" by auto
-      have lt_2: "t2 < ?t3" by simp
-      from nn2 [rule_format, OF this] and eq_m
-      have h1: "thread \<in> set (wq (e#moment t2 s) cs2)" and
-           h2: "thread \<noteq> hd (wq (e#moment t2 s) cs2)" by auto
-      from nn1[rule_format, OF lt_2[folded eq_12]] eq_m[folded eq_12]
-      have g1: "thread \<in> set (wq (e#moment t1 s) cs1)" and
-           g2: "thread \<noteq> hd (wq (e#moment t1 s) cs1)" by auto
-      have "vt (e#moment t2 s)"
-      proof -
-        from vt_moment 
-        have "vt (moment ?t3 s)" .
-        with eq_m show ?thesis by simp
-      qed
-      then interpret vt_e: valid_trace_e "moment t2 s" "e"
-          by (unfold_locales, auto, cases, simp)
-      have "e = V thread cs2 \<or> e = P thread cs2"
-      proof(cases "thread \<in> set (wq (moment t2 s) cs2)")
-        case True
-        have "e = V thread cs2"
-        proof -
-          have eq_th: "thread = hd (wq (moment t2 s) cs2)" 
-              using True and np2  by auto 
-          from vt_e.wq_out_inv[OF True this h2]
-          show ?thesis .
-        qed
-        thus ?thesis by auto
-      next
-        case False
-        have "e = P thread cs2" using vt_e.wq_in_inv[OF False h1] .
-        thus ?thesis by auto
-      qed
-      moreover have "e = V thread cs1 \<or> e = P thread cs1"
-      proof(cases "thread \<in> set (wq (moment t1 s) cs1)")
-        case True
-        have eq_th: "thread = hd (wq (moment t1 s) cs1)" 
-              using True and np1  by auto 
-        from vt_e.wq_out_inv[folded eq_12, OF True this g2]
-        have "e = V thread cs1" .
-        thus ?thesis by auto
-      next
-        case False
-        have "e = P thread cs1" using vt_e.wq_in_inv[folded eq_12, OF False g1] .
-        thus ?thesis by auto
-      qed
-      ultimately have ?thesis using neq12 by auto
-    } ultimately show ?thesis using nat_neq_iff by blast 
-  qed
+  from in_dom obtain n where "(Th th, n) \<in> RAG s" by auto
+  moreover from RAG_target_th[OF this] obtain cs where "n = Cs cs" by auto
+  ultimately have "(Th th, Cs cs) \<in> RAG s" by simp
+  hence "th \<in> set (wq s cs)"
+    by (unfold s_RAG_def, auto simp:cs_waiting_def)
+  from wq_threads [OF this] show ?thesis .
 qed
 
-text {*
-  This lemma is a simple corrolary of @{text "waiting_unique_pre"}.
-*}
-
-lemma waiting_unique:
-  assumes "waiting s th cs1"
-  and "waiting s th cs2"
-  shows "cs1 = cs2"
-  using waiting_unique_pre assms
-  unfolding wq_def s_waiting_def
-  by auto
+lemma rg_RAG_threads: 
+  assumes "(Th th) \<in> Range (RAG s)"
+  shows "th \<in> threads s"
+  using assms
+  by (unfold s_RAG_def cs_waiting_def cs_holding_def, 
+       auto intro:wq_threads)
+
+lemma RAG_threads:
+  assumes "(Th th) \<in> Field (RAG s)"
+  shows "th \<in> threads s"
+  using assms
+  by (metis Field_def UnE dm_RAG_threads rg_RAG_threads)
 
 end
 
-(* not used *)
-text {*
-  Every thread can only be blocked on one critical resource, 
-  symmetrically, every critical resource can only be held by one thread. 
-  This fact is much more easier according to our definition. 
-*}
-lemma held_unique:
-  assumes "holding (s::event list) th1 cs"
-  and "holding s th2 cs"
-  shows "th1 = th2"
- by (insert assms, unfold s_holding_def, auto)
-
-lemma last_set_lt: "th \<in> threads s \<Longrightarrow> last_set th s < length s"
-  apply (induct s, auto)
-  by (case_tac a, auto split:if_splits)
-
-lemma last_set_unique: 
-  "\<lbrakk>last_set th1 s = last_set th2 s; th1 \<in> threads s; th2 \<in> threads s\<rbrakk>
-          \<Longrightarrow> th1 = th2"
-  apply (induct s, auto)
-  by (case_tac a, auto split:if_splits dest:last_set_lt)
-
-lemma preced_unique : 
-  assumes pcd_eq: "preced th1 s = preced th2 s"
-  and th_in1: "th1 \<in> threads s"
-  and th_in2: " th2 \<in> threads s"
-  shows "th1 = th2"
-proof -
-  from pcd_eq have "last_set th1 s = last_set th2 s" by (simp add:preced_def)
-  from last_set_unique [OF this th_in1 th_in2]
-  show ?thesis .
-qed
-                      
-lemma preced_linorder: 
-  assumes neq_12: "th1 \<noteq> th2"
-  and th_in1: "th1 \<in> threads s"
-  and th_in2: " th2 \<in> threads s"
-  shows "preced th1 s < preced th2 s \<or> preced th1 s > preced th2 s"
-proof -
-  from preced_unique [OF _ th_in1 th_in2] and neq_12 
-  have "preced th1 s \<noteq> preced th2 s" by auto
-  thus ?thesis by auto
-qed
+section {* The change of @{term RAG} *}
 
 text {*
   The following three lemmas show that @{text "RAG"} does not change
@@ -868,36 +1063,18 @@
   events, respectively.
 *}
 
-lemma RAG_set_unchanged: "(RAG (Set th prio # s)) = RAG s"
-apply (unfold s_RAG_def s_waiting_def wq_def)
-by (simp add:Let_def)
-
-lemma (in valid_trace_set)
-   RAG_unchanged: "(RAG (e # s)) = RAG s"
-   by (unfold is_set RAG_set_unchanged, simp)
-
-lemma RAG_create_unchanged: "(RAG (Create th prio # s)) = RAG s"
-apply (unfold s_RAG_def s_waiting_def wq_def)
-by (simp add:Let_def)
-
-lemma (in valid_trace_create)
-   RAG_unchanged: "(RAG (e # s)) = RAG s"
-   by (unfold is_create RAG_create_unchanged, simp)
-
-lemma RAG_exit_unchanged: "(RAG (Exit th # s)) = RAG s"
-apply (unfold s_RAG_def s_waiting_def wq_def)
-by (simp add:Let_def)
-
-lemma (in valid_trace_exit)
-   RAG_unchanged: "(RAG (e # s)) = RAG s"
-   by (unfold is_exit RAG_exit_unchanged, simp)
+lemma (in valid_trace_set) RAG_unchanged [simp]: "(RAG (e # s)) = RAG s"
+   by (unfold is_set s_RAG_def s_waiting_def wq_def, simp add:Let_def)
+
+lemma (in valid_trace_create) RAG_unchanged [simp]: "(RAG (e # s)) = RAG s"
+ by (unfold is_create s_RAG_def s_waiting_def wq_def, simp add:Let_def)
+
+lemma (in valid_trace_exit) RAG_unchanged[simp]: "(RAG (e # s)) = RAG s"
+  by (unfold is_exit s_RAG_def s_waiting_def wq_def, simp add:Let_def)
 
 context valid_trace_v
 begin
 
-lemma distinct_rest: "distinct rest"
-  by (simp add: distinct_tl rest_def wq_distinct)
-
 lemma holding_cs_eq_th:
   assumes "holding s t cs"
   shows "t = th"
@@ -915,14 +1092,20 @@
   by (metis (mono_tags, lifting) distinct_rest  some_eq_ex wq'_def)
   
 lemma set_wq': "set wq' = set rest"
-  by (metis (mono_tags, lifting) distinct_rest rest_def 
-      some_eq_ex wq'_def)
+  by (metis (mono_tags, lifting) distinct_rest some_eq_ex wq'_def)
     
 lemma th'_in_inv:
   assumes "th' \<in> set wq'"
   shows "th' \<in> set rest"
   using assms set_wq' by simp
 
+lemma runing_th_s:
+  shows "th \<in> runing s"
+proof -
+  from pip_e[unfolded is_v]
+  show ?thesis by (cases, simp)
+qed
+
 lemma neq_t_th: 
   assumes "waiting (e#s) t c"
   shows "t \<noteq> th"
@@ -945,7 +1128,7 @@
         by (simp add: cs_waiting_def waiting_eq)
     hence "t \<notin> readys s" by (unfold readys_def, auto)
     hence "t \<notin> runing s" using runing_ready by auto 
-    with runing_th_s[folded otherwise] show ?thesis by auto
+    with runing_th_s[folded otherwise] show ?thesis by auto 
   qed
 qed
 
@@ -996,10 +1179,6 @@
   thus "x \<noteq> []" using rest_nnl by auto
 qed 
 
-definition "taker = hd wq'"
-
-definition "rest' = tl wq'"
-
 lemma eq_wq': "wq' = taker # rest'"
   by (simp add: neq_wq' rest'_def taker_def)
 
@@ -1202,18 +1381,6 @@
 
 end 
 
-lemma rel_eqI:
-  assumes "\<And> x y. (x,y) \<in> A \<Longrightarrow> (x,y) \<in> B"
-  and "\<And> x y. (x,y) \<in> B \<Longrightarrow> (x, y) \<in> A"
-  shows "A = B"
-  using assms by auto
-
-lemma in_RAG_E:
-  assumes "(n1, n2) \<in> RAG (s::state)"
-  obtains (waiting) th cs where "n1 = Th th" "n2 = Cs cs" "waiting s th cs"
-      | (holding) th cs where "n1 = Cs cs" "n2 = Th th" "holding s th cs"
-  using assms[unfolded s_RAG_def, folded waiting_eq holding_eq]
-  by auto
   
 context valid_trace_v
 begin
@@ -1400,183 +1567,29 @@
  qed
 qed
 
-end
-
-lemma step_RAG_v: 
-assumes vt:
-  "vt (V th cs#s)"
-shows "
-  RAG (V th cs # s) =
-  RAG s - {(Cs cs, Th th)} -
-  {(Th th', Cs cs) |th'. next_th s th cs th'} \<union>
-  {(Cs cs, Th th') |th'.  next_th s th cs th'}" (is "?L = ?R")
-proof -
-  interpret vt_v: valid_trace_v s "V th cs"
-    using assms step_back_vt by (unfold_locales, auto) 
-  show ?thesis using vt_v.RAG_es .
-qed
-
-lemma (in valid_trace_create)
-  th_not_in_threads: "th \<notin> threads s"
-proof -
-  from pip_e[unfolded is_create]
-  show ?thesis by (cases, simp)
-qed
-
-lemma (in valid_trace_create)
-  threads_es [simp]: "threads (e#s) = threads s \<union> {th}"
-  by (unfold is_create, simp)
-
-lemma (in valid_trace_exit)
-  threads_es [simp]: "threads (e#s) = threads s - {th}"
-  by (unfold is_exit, simp)
-
-lemma (in valid_trace_p)
-  threads_es [simp]: "threads (e#s) = threads s"
-  by (unfold is_p, simp)
-
-lemma (in valid_trace_v)
-  threads_es [simp]: "threads (e#s) = threads s"
-  by (unfold is_v, simp)
-
-lemma (in valid_trace_v)
-  th_not_in_rest[simp]: "th \<notin> set rest"
-proof
-  assume otherwise: "th \<in> set rest"
-  have "distinct (wq s cs)" by (simp add: wq_distinct)
-  from this[unfolded wq_s_cs] and otherwise
-  show False by auto
-qed
-
-lemma (in valid_trace_v)
-  set_wq_es_cs [simp]: "set (wq (e#s) cs) = set (wq s cs) - {th}"
-proof(unfold wq_es_cs wq'_def, rule someI2)
-  show "distinct rest \<and> set rest = set rest"
-    by (simp add: distinct_rest)
+lemma 
+  finite_RAG_kept:
+  assumes "finite (RAG s)"
+  shows "finite (RAG (e#s))"
+proof(cases "rest = []")
+  case True
+  interpret vt: valid_trace_v_e using True
+    by (unfold_locales, simp)
+  show ?thesis using assms
+    by  (unfold RAG_es vt.waiting_set_eq vt.holding_set_eq, simp)
 next
-  fix x
-  assume "distinct x \<and> set x = set rest"
-  thus "set x = set (wq s cs) - {th}" 
-      by (unfold wq_s_cs, simp)
-qed
-
-lemma (in valid_trace_exit)
-  th_not_in_wq: "th \<notin> set (wq s cs)"
-proof -
-  from pip_e[unfolded is_exit]
-  show ?thesis
-  by (cases, unfold holdents_def s_holding_def, fold wq_def, 
-             auto elim!:runing_wqE)
+  case False
+  interpret vt: valid_trace_v_n using False
+    by (unfold_locales, simp)
+  show ?thesis using assms
+    by  (unfold RAG_es vt.waiting_set_eq vt.holding_set_eq, simp)
 qed
 
-lemma (in valid_trace) wq_threads: 
-  assumes "th \<in> set (wq s cs)"
-  shows "th \<in> threads s"
-  using assms
-proof(induct rule:ind)
-  case (Nil)
-  thus ?case by (auto simp:wq_def)
-next
-  case (Cons s e)
-  interpret vt_e: valid_trace_e s e using Cons by simp
-  show ?case
-  proof(cases e)
-    case (Create th' prio')
-    interpret vt: valid_trace_create s e th' prio'
-      using Create by (unfold_locales, simp)
-    show ?thesis
-      using Cons.hyps(2) Cons.prems by auto
-  next
-    case (Exit th')
-    interpret vt: valid_trace_exit s e th'
-      using Exit by (unfold_locales, simp)
-    show ?thesis
-      using Cons.hyps(2) Cons.prems vt.th_not_in_wq by auto 
-  next
-    case (P th' cs')
-    interpret vt: valid_trace_p s e th' cs'
-      using P by (unfold_locales, simp)
-    show ?thesis
-      using Cons.hyps(2) Cons.prems readys_threads 
-        runing_ready vt.is_p vt.runing_th_s vt_e.wq_in_inv 
-        by fastforce 
-  next
-    case (V th' cs')
-    interpret vt: valid_trace_v s e th' cs'
-      using V by (unfold_locales, simp)
-    show ?thesis using Cons
-      using vt.is_v vt.threads_es vt_e.wq_in_inv by blast
-  next
-    case (Set th' prio)
-    interpret vt: valid_trace_set s e th' prio
-      using Set by (unfold_locales, simp)
-    show ?thesis using Cons.hyps(2) Cons.prems vt.is_set 
-        by (auto simp:wq_def Let_def)
-  qed
-qed 
-
-context valid_trace
-begin
-
-lemma  dm_RAG_threads:
-  assumes in_dom: "(Th th) \<in> Domain (RAG s)"
-  shows "th \<in> threads s"
-proof -
-  from in_dom obtain n where "(Th th, n) \<in> RAG s" by auto
-  moreover from RAG_target_th[OF this] obtain cs where "n = Cs cs" by auto
-  ultimately have "(Th th, Cs cs) \<in> RAG s" by simp
-  hence "th \<in> set (wq s cs)"
-    by (unfold s_RAG_def, auto simp:cs_waiting_def)
-  from wq_threads [OF this] show ?thesis .
-qed
-
-lemma rg_RAG_threads: 
-  assumes "(Th th) \<in> Range (RAG s)"
-  shows "th \<in> threads s"
-  using assms
-  by (unfold s_RAG_def cs_waiting_def cs_holding_def, 
-       auto intro:wq_threads)
-
-lemma RAG_threads:
-  assumes "(Th th) \<in> Field (RAG s)"
-  shows "th \<in> threads s"
-  using assms
-  by (metis Field_def UnE dm_RAG_threads rg_RAG_threads)
-
 end
 
-lemma (in valid_trace_v)
-  preced_es [simp]: "preced th (e#s) = preced th s"
-  by (unfold is_v preced_def, simp)
-
-lemma the_preced_v[simp]: "the_preced (V th cs#s) = the_preced s"
-proof
-  fix th'
-  show "the_preced (V th cs # s) th' = the_preced s th'"
-    by (unfold the_preced_def preced_def, simp)
-qed
-
-lemma (in valid_trace_v)
-  the_preced_es: "the_preced (e#s) = the_preced s"
-  by (unfold is_v preced_def, simp)
-
 context valid_trace_p
 begin
 
-lemma not_holding_s_th_cs: "\<not> holding s th cs"
-proof
-  assume otherwise: "holding s th cs"
-  from pip_e[unfolded is_p]
-  show False
-  proof(cases)
-    case (thread_P)
-    moreover have "(Cs cs, Th th) \<in> RAG s"
-      using otherwise cs_holding_def 
-            holding_eq th_not_in_wq by auto
-    ultimately show ?thesis by auto
-  qed
-qed
-
 lemma waiting_kept:
   assumes "waiting s th' cs'"
   shows "waiting (e#s) th' cs'"
@@ -1584,7 +1597,7 @@
   by (metis cs_waiting_def hd_append2 list.sel(1) list.set_intros(2) 
       rotate1.simps(2) self_append_conv2 set_rotate1 
         th_not_in_wq waiting_eq wq_es_cs wq_neq_simp)
-  
+
 lemma holding_kept:
   assumes "holding s th' cs'"
   shows "holding (e#s) th' cs'"
@@ -1602,123 +1615,16 @@
   thus ?thesis
     by (simp add: cs_holding_def holding_eq) 
 qed
-
-end
-
-locale valid_trace_p_h = valid_trace_p +
-  assumes we: "wq s cs = []"
-
-locale valid_trace_p_w = valid_trace_p +
-  assumes wne: "wq s cs \<noteq> []"
-begin
-
-definition "holder = hd (wq s cs)"
-definition "waiters = tl (wq s cs)"
-definition "waiters' = waiters @ [th]"
-
-lemma wq_s_cs: "wq s cs = holder#waiters"
-    by (simp add: holder_def waiters_def wne)
-    
-lemma wq_es_cs': "wq (e#s) cs = holder#waiters@[th]"
-  by (simp add: wq_es_cs wq_s_cs)
-
-lemma waiting_es_th_cs: "waiting (e#s) th cs"
-  using cs_waiting_def th_not_in_wq waiting_eq wq_es_cs' wq_s_cs by auto
-
-lemma RAG_edge: "(Th th, Cs cs) \<in> RAG (e#s)"
-   by (unfold s_RAG_def, fold waiting_eq, insert waiting_es_th_cs, auto)
-
-lemma holding_esE:
-  assumes "holding (e#s) th' cs'"
-  obtains "holding s th' cs'"
-  using assms 
-proof(cases "cs' = cs")
-  case False
-  hence "wq (e#s) cs' = wq s cs'" by simp
-  with assms show ?thesis
-    using cs_holding_def holding_eq that by auto 
-next
-  case True
-  with assms show ?thesis
-  by (metis cs_holding_def holding_eq list.sel(1) list.set_intros(1) that 
-        wq_es_cs' wq_s_cs) 
+end 
+
+lemma (in valid_trace_p) th_not_waiting: "\<not> waiting s th c"
+proof -
+  have "th \<in> readys s"
+    using runing_ready runing_th_s by blast 
+  thus ?thesis
+    by (unfold readys_def, auto)
 qed
 
-lemma waiting_esE:
-  assumes "waiting (e#s) th' cs'"
-  obtains "th' \<noteq> th" "waiting s th' cs'"
-     |  "th' = th" "cs' = cs"
-proof(cases "waiting s th' cs'")
-  case True
-  have "th' \<noteq> th"
-  proof
-    assume otherwise: "th' = th"
-    from True[unfolded this]
-    show False by (simp add: th_not_waiting) 
-  qed
-  from that(1)[OF this True] show ?thesis .
-next
-  case False
-  hence "th' = th \<and> cs' = cs"
-      by (metis assms cs_waiting_def holder_def list.sel(1) rotate1.simps(2) 
-        set_ConsD set_rotate1 waiting_eq wq_es_cs wq_es_cs' wq_neq_simp)
-  with that(2) show ?thesis by metis
-qed
-
-lemma RAG_es: "RAG (e # s) =  RAG s \<union> {(Th th, Cs cs)}" (is "?L = ?R")
-proof(rule rel_eqI)
-  fix n1 n2
-  assume "(n1, n2) \<in> ?L"
-  thus "(n1, n2) \<in> ?R" 
-  proof(cases rule:in_RAG_E)
-    case (waiting th' cs')
-    from this(3)
-    show ?thesis
-    proof(cases rule:waiting_esE)
-      case 1
-      thus ?thesis using waiting(1,2)
-        by (unfold s_RAG_def, fold waiting_eq, auto)
-    next
-      case 2
-      thus ?thesis using waiting(1,2) by auto
-    qed
-  next
-    case (holding th' cs')
-    from this(3)
-    show ?thesis
-    proof(cases rule:holding_esE)
-      case 1
-      with holding(1,2)
-      show ?thesis by (unfold s_RAG_def, fold holding_eq, auto)
-    qed
-  qed
-next
-  fix n1 n2
-  assume "(n1, n2) \<in> ?R"
-  hence "(n1, n2) \<in> RAG s \<or> (n1 = Th th \<and> n2 = Cs cs)" by auto
-  thus "(n1, n2) \<in> ?L"
-  proof
-    assume "(n1, n2) \<in> RAG s"
-    thus ?thesis
-    proof(cases rule:in_RAG_E)
-      case (waiting th' cs')
-      from waiting_kept[OF this(3)]
-      show ?thesis using waiting(1,2)
-         by (unfold s_RAG_def, fold waiting_eq, auto)
-    next
-      case (holding th' cs')
-      from holding_kept[OF this(3)]
-      show ?thesis using holding(1,2)
-         by (unfold s_RAG_def, fold holding_eq, auto)
-    qed
-  next
-    assume "n1 = Th th \<and> n2 = Cs cs"
-    thus ?thesis using RAG_edge by auto
-  qed
-qed
-
-end
-
 context valid_trace_p_h
 begin
 
@@ -1815,10 +1721,116 @@
 
 end
 
+context valid_trace_p_w
+begin
+
+lemma wq_s_cs: "wq s cs = holder#waiters"
+    by (simp add: holder_def waiters_def wne)
+    
+lemma wq_es_cs': "wq (e#s) cs = holder#waiters@[th]"
+  by (simp add: wq_es_cs wq_s_cs)
+
+lemma waiting_es_th_cs: "waiting (e#s) th cs"
+  using cs_waiting_def th_not_in_wq waiting_eq wq_es_cs' wq_s_cs by auto
+
+lemma RAG_edge: "(Th th, Cs cs) \<in> RAG (e#s)"
+   by (unfold s_RAG_def, fold waiting_eq, insert waiting_es_th_cs, auto)
+
+lemma holding_esE:
+  assumes "holding (e#s) th' cs'"
+  obtains "holding s th' cs'"
+  using assms 
+proof(cases "cs' = cs")
+  case False
+  hence "wq (e#s) cs' = wq s cs'" by simp
+  with assms show ?thesis
+    using cs_holding_def holding_eq that by auto 
+next
+  case True
+  with assms show ?thesis
+  by (metis cs_holding_def holding_eq list.sel(1) list.set_intros(1) that 
+        wq_es_cs' wq_s_cs) 
+qed
+
+lemma waiting_esE:
+  assumes "waiting (e#s) th' cs'"
+  obtains "th' \<noteq> th" "waiting s th' cs'"
+     |  "th' = th" "cs' = cs"
+proof(cases "waiting s th' cs'")
+  case True
+  have "th' \<noteq> th"
+  proof
+    assume otherwise: "th' = th"
+    from True[unfolded this]
+    show False by (simp add: th_not_waiting)
+  qed
+  from that(1)[OF this True] show ?thesis .
+next
+  case False
+  hence "th' = th \<and> cs' = cs"
+      by (metis assms cs_waiting_def holder_def list.sel(1) rotate1.simps(2) 
+        set_ConsD set_rotate1 waiting_eq wq_es_cs wq_es_cs' wq_neq_simp)
+  with that(2) show ?thesis by metis
+qed
+
+lemma RAG_es: "RAG (e # s) =  RAG s \<union> {(Th th, Cs cs)}" (is "?L = ?R")
+proof(rule rel_eqI)
+  fix n1 n2
+  assume "(n1, n2) \<in> ?L"
+  thus "(n1, n2) \<in> ?R" 
+  proof(cases rule:in_RAG_E)
+    case (waiting th' cs')
+    from this(3)
+    show ?thesis
+    proof(cases rule:waiting_esE)
+      case 1
+      thus ?thesis using waiting(1,2)
+        by (unfold s_RAG_def, fold waiting_eq, auto)
+    next
+      case 2
+      thus ?thesis using waiting(1,2) by auto
+    qed
+  next
+    case (holding th' cs')
+    from this(3)
+    show ?thesis
+    proof(cases rule:holding_esE)
+      case 1
+      with holding(1,2)
+      show ?thesis by (unfold s_RAG_def, fold holding_eq, auto)
+    qed
+  qed
+next
+  fix n1 n2
+  assume "(n1, n2) \<in> ?R"
+  hence "(n1, n2) \<in> RAG s \<or> (n1 = Th th \<and> n2 = Cs cs)" by auto
+  thus "(n1, n2) \<in> ?L"
+  proof
+    assume "(n1, n2) \<in> RAG s"
+    thus ?thesis
+    proof(cases rule:in_RAG_E)
+      case (waiting th' cs')
+      from waiting_kept[OF this(3)]
+      show ?thesis using waiting(1,2)
+         by (unfold s_RAG_def, fold waiting_eq, auto)
+    next
+      case (holding th' cs')
+      from holding_kept[OF this(3)]
+      show ?thesis using holding(1,2)
+         by (unfold s_RAG_def, fold holding_eq, auto)
+    qed
+  next
+    assume "n1 = Th th \<and> n2 = Cs cs"
+    thus ?thesis using RAG_edge by auto
+  qed
+qed
+
+end
+
 context valid_trace_p
 begin
 
-lemma RAG_es': "RAG (e # s) =  (if (wq s cs = []) then RAG s \<union> {(Cs cs, Th th)}
+lemma RAG_es: "RAG (e # s) =  (if (wq s cs = []) then RAG s \<union> {(Cs cs, Th th)}
                                                   else RAG s \<union> {(Th th, Cs cs)})"
 proof(cases "wq s cs = []")
   case True
@@ -1834,6 +1846,252 @@
 
 end
 
+section {* Finiteness of RAG *}
+
+context valid_trace
+begin
+
+lemma finite_RAG:
+  shows "finite (RAG s)"
+proof(induct rule:ind)
+  case Nil
+  show ?case 
+  by (auto simp: s_RAG_def cs_waiting_def 
+                   cs_holding_def wq_def acyclic_def)
+next
+  case (Cons s e)
+  interpret vt_e: valid_trace_e s e using Cons by simp
+  show ?case
+  proof(cases e)
+    case (Create th prio)
+    interpret vt: valid_trace_create s e th prio using Create
+      by (unfold_locales, simp)
+    show ?thesis using Cons by simp
+  next
+    case (Exit th)
+    interpret vt: valid_trace_exit s e th using Exit
+      by (unfold_locales, simp)
+    show ?thesis using Cons by simp
+  next
+    case (P th cs)
+    interpret vt: valid_trace_p s e th cs using P
+      by (unfold_locales, simp)
+    show ?thesis using Cons using vt.RAG_es by auto 
+  next
+    case (V th cs)
+    interpret vt: valid_trace_v s e th cs using V
+      by (unfold_locales, simp)
+    show ?thesis using Cons by (simp add: vt.finite_RAG_kept) 
+  next
+    case (Set th prio)
+    interpret vt: valid_trace_set s e th prio using Set
+      by (unfold_locales, simp)
+    show ?thesis using Cons by simp
+  qed
+qed
+end
+
+section {* RAG is acyclic *}
+
+text {* (* ddd *)
+  The nature of the work is like this: since it starts from a very simple and basic 
+  model, even intuitively very `basic` and `obvious` properties need to derived from scratch.
+  For instance, the fact 
+  that one thread can not be blocked by two critical resources at the same time
+  is obvious, because only running threads can make new requests, if one is waiting for 
+  a critical resource and get blocked, it can not make another resource request and get 
+  blocked the second time (because it is not running). 
+
+  To derive this fact, one needs to prove by contraction and 
+  reason about time (or @{text "moement"}). The reasoning is based on a generic theorem
+  named @{text "p_split"}, which is about status changing along the time axis. It says if 
+  a condition @{text "Q"} is @{text "True"} at a state @{text "s"},
+  but it was @{text "False"} at the very beginning, then there must exits a moment @{text "t"} 
+  in the history of @{text "s"} (notice that @{text "s"} itself is essentially the history 
+  of events leading to it), such that @{text "Q"} switched 
+  from being @{text "False"} to @{text "True"} and kept being @{text "True"}
+  till the last moment of @{text "s"}.
+
+  Suppose a thread @{text "th"} is blocked
+  on @{text "cs1"} and @{text "cs2"} in some state @{text "s"}, 
+  since no thread is blocked at the very beginning, by applying 
+  @{text "p_split"} to these two blocking facts, there exist 
+  two moments @{text "t1"} and @{text "t2"}  in @{text "s"}, such that 
+  @{text "th"} got blocked on @{text "cs1"} and @{text "cs2"} 
+  and kept on blocked on them respectively ever since.
+ 
+  Without lost of generality, we assume @{text "t1"} is earlier than @{text "t2"}.
+  However, since @{text "th"} was blocked ever since memonent @{text "t1"}, so it was still
+  in blocked state at moment @{text "t2"} and could not
+  make any request and get blocked the second time: Contradiction.
+*}
+
+
+context valid_trace
+begin
+
+lemma waiting_unique_pre: (* ddd *)
+  assumes h11: "thread \<in> set (wq s cs1)"
+  and h12: "thread \<noteq> hd (wq s cs1)"
+  assumes h21: "thread \<in> set (wq s cs2)"
+  and h22: "thread \<noteq> hd (wq s cs2)"
+  and neq12: "cs1 \<noteq> cs2"
+  shows "False"
+proof -
+  let "?Q" = "\<lambda> cs s. thread \<in> set (wq s cs) \<and> thread \<noteq> hd (wq s cs)"
+  from h11 and h12 have q1: "?Q cs1 s" by simp
+  from h21 and h22 have q2: "?Q cs2 s" by simp
+  have nq1: "\<not> ?Q cs1 []" by (simp add:wq_def)
+  have nq2: "\<not> ?Q cs2 []" by (simp add:wq_def)
+  from p_split [of "?Q cs1", OF q1 nq1]
+  obtain t1 where lt1: "t1 < length s"
+    and np1: "\<not> ?Q cs1 (moment t1 s)"
+    and nn1: "(\<forall>i'>t1. ?Q cs1 (moment i' s))" by auto
+  from p_split [of "?Q cs2", OF q2 nq2]
+  obtain t2 where lt2: "t2 < length s"
+    and np2: "\<not> ?Q cs2 (moment t2 s)"
+    and nn2: "(\<forall>i'>t2. ?Q cs2 (moment i' s))" by auto
+  { fix s cs
+    assume q: "?Q cs s"
+    have "thread \<notin> runing s"
+    proof
+      assume "thread \<in> runing s"
+      hence " \<forall>cs. \<not> (thread \<in> set (wq_fun (schs s) cs) \<and> 
+                 thread \<noteq> hd (wq_fun (schs s) cs))"
+        by (unfold runing_def s_waiting_def readys_def, auto)
+      from this[rule_format, of cs] q 
+      show False by (simp add: wq_def) 
+    qed
+  } note q_not_runing = this
+  { fix t1 t2 cs1 cs2
+    assume  lt1: "t1 < length s"
+    and np1: "\<not> ?Q cs1 (moment t1 s)"
+    and nn1: "(\<forall>i'>t1. ?Q cs1 (moment i' s))"
+    and lt2: "t2 < length s"
+    and np2: "\<not> ?Q cs2 (moment t2 s)"
+    and nn2: "(\<forall>i'>t2. ?Q cs2 (moment i' s))"
+    and lt12: "t1 < t2"
+    let ?t3 = "Suc t2" 
+    interpret ve2: valid_moment_e _ t2 using lt2
+     by (unfold_locales, simp)
+    let ?e = ve2.next_e
+    have "t2 < ?t3" by simp
+    from nn2 [rule_format, OF this] and ve2.trace_e
+    have h1: "thread \<in> set (wq (?e#moment t2 s) cs2)" and
+         h2: "thread \<noteq> hd (wq (?e#moment t2 s) cs2)" by auto
+    have ?thesis
+    proof -
+      have "thread \<in> runing (moment t2 s)"
+      proof(cases "thread \<in> set (wq (moment t2 s) cs2)")
+        case True
+        have "?e = V thread cs2"
+        proof -
+          have eq_th: "thread = hd (wq (moment t2 s) cs2)" 
+              using True and np2  by auto 
+          thus ?thesis
+            using True h2 ve2.vat_moment_e.wq_out_inv by blast 
+        qed
+        thus ?thesis
+          using step.cases ve2.vat_moment_e.pip_e by auto 
+      next
+        case False
+        hence "?e = P thread cs2"
+          using h1 ve2.vat_moment_e.wq_in_inv by blast 
+        thus ?thesis
+          using step.cases ve2.vat_moment_e.pip_e by auto 
+      qed
+      moreover have "thread \<notin> runing (moment t2 s)"
+        by (rule q_not_runing[OF nn1[rule_format, OF lt12]])
+      ultimately show ?thesis by simp
+    qed
+  } note lt_case = this
+  show ?thesis
+  proof -
+    { assume "t1 < t2"
+      from lt_case[OF lt1 np1 nn1 lt2 np2 nn2 this]
+      have ?thesis .
+    } moreover {
+      assume "t2 < t1"
+      from lt_case[OF lt2 np2 nn2 lt1 np1 nn1 this]
+      have ?thesis .
+    } moreover { 
+      assume eq_12: "t1 = t2"
+      let ?t3 = "Suc t2"
+      interpret ve2: valid_moment_e _ t2 using lt2
+        by (unfold_locales, simp)
+      let ?e = ve2.next_e
+      have "t2 < ?t3" by simp
+      from nn2 [rule_format, OF this] and ve2.trace_e
+      have h1: "thread \<in> set (wq (?e#moment t2 s) cs2)" by auto
+      have lt_2: "t2 < ?t3" by simp
+      from nn2 [rule_format, OF this] and ve2.trace_e
+      have h1: "thread \<in> set (wq (?e#moment t2 s) cs2)" and
+           h2: "thread \<noteq> hd (wq (?e#moment t2 s) cs2)" by auto
+      from nn1[rule_format, OF lt_2[folded eq_12], unfolded ve2.trace_e[folded eq_12]] 
+           eq_12[symmetric]
+      have g1: "thread \<in> set (wq (?e#moment t1 s) cs1)" and
+           g2: "thread \<noteq> hd (wq (?e#moment t1 s) cs1)" by auto
+      have "?e = V thread cs2 \<or> ?e = P thread cs2"
+        using h1 h2 np2 ve2.vat_moment_e.wq_in_inv 
+              ve2.vat_moment_e.wq_out_inv by blast
+      moreover have "?e = V thread cs1 \<or> ?e = P thread cs1"
+        using eq_12 g1 g2 np1 ve2.vat_moment_e.wq_in_inv 
+              ve2.vat_moment_e.wq_out_inv by blast
+      ultimately have ?thesis using neq12 by auto
+    } ultimately show ?thesis using nat_neq_iff by blast 
+  qed
+qed
+
+text {*
+  This lemma is a simple corrolary of @{text "waiting_unique_pre"}.
+*}
+
+lemma waiting_unique:
+  assumes "waiting s th cs1"
+  and "waiting s th cs2"
+  shows "cs1 = cs2"
+  using waiting_unique_pre assms
+  unfolding wq_def s_waiting_def
+  by auto
+
+end
+
+lemma (in valid_trace_v)
+  preced_es [simp]: "preced th (e#s) = preced th s"
+  by (unfold is_v preced_def, simp)
+
+lemma the_preced_v[simp]: "the_preced (V th cs#s) = the_preced s"
+proof
+  fix th'
+  show "the_preced (V th cs # s) th' = the_preced s th'"
+    by (unfold the_preced_def preced_def, simp)
+qed
+
+
+lemma (in valid_trace_v)
+  the_preced_es: "the_preced (e#s) = the_preced s"
+  by (unfold is_v preced_def, simp)
+
+context valid_trace_p
+begin
+
+lemma not_holding_s_th_cs: "\<not> holding s th cs"
+proof
+  assume otherwise: "holding s th cs"
+  from pip_e[unfolded is_p]
+  show False
+  proof(cases)
+    case (thread_P)
+    moreover have "(Cs cs, Th th) \<in> RAG s"
+      using otherwise cs_holding_def 
+            holding_eq th_not_in_wq by auto
+    ultimately show ?thesis by auto
+  qed
+qed
+
+end
+
+
 lemma (in valid_trace_v_n) finite_waiting_set:
   "finite {(Th th', Cs cs) |th'. next_th s th cs th'}"
     by (simp add: waiting_set_eq)
@@ -1850,28 +2108,6 @@
   "finite {(Cs cs, Th th') |th'. next_th s th cs th'}"
     by (simp add: holding_set_eq)
 
-context valid_trace_v
-begin
-
-lemma 
-  finite_RAG_kept:
-  assumes "finite (RAG s)"
-  shows "finite (RAG (e#s))"
-proof(cases "rest = []")
-  case True
-  interpret vt: valid_trace_v_e using True
-    by (unfold_locales, simp)
-  show ?thesis using assms
-    by  (unfold RAG_es vt.waiting_set_eq vt.holding_set_eq, simp)
-next
-  case False
-  interpret vt: valid_trace_v_n using False
-    by (unfold_locales, simp)
-  show ?thesis using assms
-    by  (unfold RAG_es vt.waiting_set_eq vt.holding_set_eq, simp)
-qed
-
-end
 
 context valid_trace_v_e
 begin 
@@ -1916,7 +2152,7 @@
         by (unfold s_RAG_def, auto)
       from this(2) have "waiting s taker cs'" 
         by (unfold s_RAG_def, fold waiting_eq, auto)
-      from waiting_unique[OF this waiting_taker]
+      from waiting_unique[OF this waiting_taker] 
       have "cs' = cs" .
       from h(1)[unfolded this] show False by auto
     qed
@@ -1951,7 +2187,7 @@
         by (unfold s_RAG_def, auto)
       hence "waiting s th cs'" 
         by (unfold s_RAG_def, fold waiting_eq, auto)
-      with th_not_waiting show False by auto
+      with th_not_waiting show False by auto 
     qed
     ultimately show ?thesis by auto
   qed
@@ -1995,45 +2231,6 @@
 context valid_trace
 begin
 
-lemma finite_RAG:
-  shows "finite (RAG s)"
-proof(induct rule:ind)
-  case Nil
-  show ?case 
-  by (auto simp: s_RAG_def cs_waiting_def 
-                   cs_holding_def wq_def acyclic_def)
-next
-  case (Cons s e)
-  interpret vt_e: valid_trace_e s e using Cons by simp
-  show ?case
-  proof(cases e)
-    case (Create th prio)
-    interpret vt: valid_trace_create s e th prio using Create
-      by (unfold_locales, simp)
-    show ?thesis using Cons by (simp add: vt.RAG_unchanged) 
-  next
-    case (Exit th)
-    interpret vt: valid_trace_exit s e th using Exit
-      by (unfold_locales, simp)
-    show ?thesis using Cons by (simp add: vt.RAG_unchanged)
-  next
-    case (P th cs)
-    interpret vt: valid_trace_p s e th cs using P
-      by (unfold_locales, simp)
-    show ?thesis using Cons using vt.RAG_es' by auto 
-  next
-    case (V th cs)
-    interpret vt: valid_trace_v s e th cs using V
-      by (unfold_locales, simp)
-    show ?thesis using Cons by (simp add: vt.finite_RAG_kept) 
-  next
-    case (Set th prio)
-    interpret vt: valid_trace_set s e th prio using Set
-      by (unfold_locales, simp)
-    show ?thesis using Cons by (simp add: vt.RAG_unchanged) 
-  qed
-qed
-
 lemma acyclic_RAG:
   shows "acyclic (RAG s)"
 proof(induct rule:ind)
@@ -2049,12 +2246,12 @@
     case (Create th prio)
     interpret vt: valid_trace_create s e th prio using Create
       by (unfold_locales, simp)
-    show ?thesis using Cons by (simp add: vt.RAG_unchanged) 
+    show ?thesis using Cons by simp 
   next
     case (Exit th)
     interpret vt: valid_trace_exit s e th using Exit
       by (unfold_locales, simp)
-    show ?thesis using Cons by (simp add: vt.RAG_unchanged)
+    show ?thesis using Cons by simp
   next
     case (P th cs)
     interpret vt: valid_trace_p s e th cs using P
@@ -2091,10 +2288,31 @@
     case (Set th prio)
     interpret vt: valid_trace_set s e th prio using Set
       by (unfold_locales, simp)
-    show ?thesis using Cons by (simp add: vt.RAG_unchanged) 
+    show ?thesis using Cons by simp 
   qed
 qed
 
+end
+
+section {* RAG is single-valued *}
+
+context valid_trace
+begin
+
+lemma unique_RAG: "\<lbrakk>(n, n1) \<in> RAG s; (n, n2) \<in> RAG s\<rbrakk> \<Longrightarrow> n1 = n2"
+  apply(unfold s_RAG_def, auto, fold waiting_eq holding_eq)
+  by(auto elim:waiting_unique held_unique)
+
+lemma sgv_RAG: "single_valued (RAG s)"
+  using unique_RAG by (auto simp:single_valued_def)
+
+end
+
+section {* RAG is well-founded *}
+
+context valid_trace
+begin
+
 lemma wf_RAG: "wf (RAG s)"
 proof(rule finite_acyclic_wf)
   from finite_RAG show "finite (RAG s)" .
@@ -2102,6 +2320,60 @@
   from acyclic_RAG show "acyclic (RAG s)" .
 qed
 
+lemma wf_RAG_converse: 
+  shows "wf ((RAG s)^-1)"
+proof(rule finite_acyclic_wf_converse)
+  from finite_RAG 
+  show "finite (RAG s)" .
+next
+  from acyclic_RAG
+  show "acyclic (RAG s)" .
+qed
+
+end
+
+section {* RAG forms a forest (or tree) *}
+
+context valid_trace
+begin
+
+lemma rtree_RAG: "rtree (RAG s)"
+  using sgv_RAG acyclic_RAG
+  by (unfold rtree_def rtree_axioms_def sgv_def, auto)
+
+end
+
+sublocale valid_trace < rtree_RAG: rtree "RAG s"
+  using rtree_RAG .
+
+sublocale valid_trace < fsbtRAGs : fsubtree "RAG s"
+proof -
+  show "fsubtree (RAG s)"
+  proof(intro_locales)
+    show "fbranch (RAG s)" using finite_fbranchI[OF finite_RAG] .
+  next
+    show "fsubtree_axioms (RAG s)"
+    proof(unfold fsubtree_axioms_def)
+      from wf_RAG show "wf (RAG s)" .
+    qed
+  qed
+qed
+
+
+section {* Derived properties for parts of RAG *}
+
+context valid_trace
+begin
+
+lemma acyclic_tRAG: "acyclic (tRAG s)"
+proof(unfold tRAG_def, rule acyclic_compose)
+  show "acyclic (RAG s)" using acyclic_RAG .
+next
+  show "wRAG s \<subseteq> RAG s" unfolding RAG_split by auto
+next
+  show "hRAG s \<subseteq> RAG s" unfolding RAG_split by auto
+qed
+
 lemma sgv_wRAG: "single_valued (wRAG s)"
   using waiting_unique
   by (unfold single_valued_def wRAG_def, auto)
@@ -2114,39 +2386,8 @@
   by (unfold tRAG_def, rule single_valued_relcomp, 
               insert sgv_wRAG sgv_hRAG, auto)
 
-lemma acyclic_tRAG: "acyclic (tRAG s)"
-proof(unfold tRAG_def, rule acyclic_compose)
-  show "acyclic (RAG s)" using acyclic_RAG .
-next
-  show "wRAG s \<subseteq> RAG s" unfolding RAG_split by auto
-next
-  show "hRAG s \<subseteq> RAG s" unfolding RAG_split by auto
-qed
-
-lemma unique_RAG: "\<lbrakk>(n, n1) \<in> RAG s; (n, n2) \<in> RAG s\<rbrakk> \<Longrightarrow> n1 = n2"
-  apply(unfold s_RAG_def, auto, fold waiting_eq holding_eq)
-  by(auto elim:waiting_unique held_unique)
-
-lemma sgv_RAG: "single_valued (RAG s)"
-  using unique_RAG by (auto simp:single_valued_def)
-
-lemma rtree_RAG: "rtree (RAG s)"
-  using sgv_RAG acyclic_RAG
-  by (unfold rtree_def rtree_axioms_def sgv_def, auto)
-
 end
 
-sublocale valid_trace < rtree_RAG: rtree "RAG s"
-proof
-  show "single_valued (RAG s)"
-  apply (intro_locales)
-    by (unfold single_valued_def, 
-        auto intro:unique_RAG)
-
-  show "acyclic (RAG s)"
-     by (rule acyclic_RAG)
-qed
-
 sublocale valid_trace < rtree_s: rtree "tRAG s"
 proof(unfold_locales)
   from sgv_tRAG show "single_valued (tRAG s)" .
@@ -2154,24 +2395,6 @@
   from acyclic_tRAG show "acyclic (tRAG s)" .
 qed
 
-sublocale valid_trace < fsbtRAGs : fsubtree "RAG s"
-proof -
-  show "fsubtree (RAG s)"
-  proof(intro_locales)
-    show "fbranch (RAG s)" using finite_fbranchI[OF finite_RAG] .
-  next
-    show "fsubtree_axioms (RAG s)"
-    proof(unfold fsubtree_axioms_def)
-      from wf_RAG show "wf (RAG s)" .
-    qed
-  qed
-qed
-
-lemma tRAG_alt_def: 
-  "tRAG s = {(Th th1, Th th2) | th1 th2. 
-                  \<exists> cs. (Th th1, Cs cs) \<in> RAG s \<and> (Cs cs, Th th2) \<in> RAG s}"
- by (auto simp:tRAG_def RAG_split wRAG_def hRAG_def)
-
 sublocale valid_trace < fsbttRAGs: fsubtree "tRAG s"
 proof -
   have "fsubtree (tRAG s)"
@@ -2204,80 +2427,282 @@
   from this[folded tRAG_def] show "fsubtree (tRAG s)" .
 qed
 
+lemma tRAG_nodeE:
+  assumes "(n1, n2) \<in> tRAG s"
+  obtains th1 th2 where "n1 = Th th1" "n2 = Th th2"
+  using assms
+  by (auto simp: tRAG_def wRAG_def hRAG_def)
+
+lemma tRAG_ancestorsE:
+  assumes "x \<in> ancestors (tRAG s) u"
+  obtains th where "x = Th th"
+proof -
+  from assms have "(u, x) \<in> (tRAG s)^+" 
+      by (unfold ancestors_def, auto)
+  from tranclE[OF this] obtain c where "(c, x) \<in> tRAG s" by auto
+  then obtain th where "x = Th th"
+    by (unfold tRAG_alt_def, auto)
+  from that[OF this] show ?thesis .
+qed
+                   
+lemma subtree_nodeE:
+  assumes "n \<in> subtree (tRAG s) (Th th)"
+  obtains th1 where "n = Th th1"
+proof -
+  show ?thesis
+  proof(rule subtreeE[OF assms])
+    assume "n = Th th"
+    from that[OF this] show ?thesis .
+  next
+    assume "Th th \<in> ancestors (tRAG s) n"
+    hence "(n, Th th) \<in> (tRAG s)^+" by (auto simp:ancestors_def)
+    hence "\<exists> th1. n = Th th1"
+    proof(induct)
+      case (base y)
+      from tRAG_nodeE[OF this] show ?case by metis
+    next
+      case (step y z)
+      thus ?case by auto
+    qed
+    with that show ?thesis by auto
+  qed
+qed
+
+lemma tRAG_star_RAG: "(tRAG s)^* \<subseteq> (RAG s)^*"
+proof -
+  have "(wRAG s O hRAG s)^* \<subseteq> (RAG s O RAG s)^*" 
+    by (rule rtrancl_mono, auto simp:RAG_split)
+  also have "... \<subseteq> ((RAG s)^*)^*"
+    by (rule rtrancl_mono, auto)
+  also have "... = (RAG s)^*" by simp
+  finally show ?thesis by (unfold tRAG_def, simp)
+qed
+
+lemma tRAG_subtree_RAG: "subtree (tRAG s) x \<subseteq> subtree (RAG s) x"
+proof -
+  { fix a
+    assume "a \<in> subtree (tRAG s) x"
+    hence "(a, x) \<in> (tRAG s)^*" by (auto simp:subtree_def)
+    with tRAG_star_RAG
+    have "(a, x) \<in> (RAG s)^*" by auto
+    hence "a \<in> subtree (RAG s) x" by (auto simp:subtree_def)
+  } thus ?thesis by auto
+qed
+
+lemma tRAG_trancl_eq:
+   "{th'. (Th th', Th th)  \<in> (tRAG s)^+} = 
+    {th'. (Th th', Th th)  \<in> (RAG s)^+}"
+   (is "?L = ?R")
+proof -
+  { fix th'
+    assume "th' \<in> ?L"
+    hence "(Th th', Th th) \<in> (tRAG s)^+" by auto
+    from tranclD[OF this]
+    obtain z where h: "(Th th', z) \<in> tRAG s" "(z, Th th) \<in> (tRAG s)\<^sup>*" by auto
+    from tRAG_subtree_RAG and this(2)
+    have "(z, Th th) \<in> (RAG s)^*" by (meson subsetCE tRAG_star_RAG) 
+    moreover from h(1) have "(Th th', z) \<in> (RAG s)^+" using tRAG_alt_def by auto 
+    ultimately have "th' \<in> ?R"  by auto 
+  } moreover 
+  { fix th'
+    assume "th' \<in> ?R"
+    hence "(Th th', Th th) \<in> (RAG s)^+" by (auto)
+    from plus_rpath[OF this]
+    obtain xs where rp: "rpath (RAG s) (Th th') xs (Th th)" "xs \<noteq> []" by auto
+    hence "(Th th', Th th) \<in> (tRAG s)^+"
+    proof(induct xs arbitrary:th' th rule:length_induct)
+      case (1 xs th' th)
+      then obtain x1 xs1 where Cons1: "xs = x1#xs1" by (cases xs, auto)
+      show ?case
+      proof(cases "xs1")
+        case Nil
+        from 1(2)[unfolded Cons1 Nil]
+        have rp: "rpath (RAG s) (Th th') [x1] (Th th)" .
+        hence "(Th th', x1) \<in> (RAG s)" 
+          by (cases, auto)
+        then obtain cs where "x1 = Cs cs" 
+              by (unfold s_RAG_def, auto)
+        from rpath_nnl_lastE[OF rp[unfolded this]]
+        show ?thesis by auto
+      next
+        case (Cons x2 xs2)
+        from 1(2)[unfolded Cons1[unfolded this]]
+        have rp: "rpath (RAG s) (Th th') (x1 # x2 # xs2) (Th th)" .
+        from rpath_edges_on[OF this]
+        have eds: "edges_on (Th th' # x1 # x2 # xs2) \<subseteq> RAG s" .
+        have "(Th th', x1) \<in> edges_on (Th th' # x1 # x2 # xs2)"
+            by (simp add: edges_on_unfold)
+        with eds have rg1: "(Th th', x1) \<in> RAG s" by auto
+        then obtain cs1 where eq_x1: "x1 = Cs cs1" by (unfold s_RAG_def, auto)
+        have "(x1, x2) \<in> edges_on (Th th' # x1 # x2 # xs2)"
+            by (simp add: edges_on_unfold)
+        from this eds
+        have rg2: "(x1, x2) \<in> RAG s" by auto
+        from this[unfolded eq_x1] 
+        obtain th1 where eq_x2: "x2 = Th th1" by (unfold s_RAG_def, auto)
+        from rg1[unfolded eq_x1] rg2[unfolded eq_x1 eq_x2]
+        have rt1: "(Th th', Th th1) \<in> tRAG s" by (unfold tRAG_alt_def, auto)
+        from rp have "rpath (RAG s) x2 xs2 (Th th)"
+           by  (elim rpath_ConsE, simp)
+        from this[unfolded eq_x2] have rp': "rpath (RAG s) (Th th1) xs2 (Th th)" .
+        show ?thesis
+        proof(cases "xs2 = []")
+          case True
+          from rpath_nilE[OF rp'[unfolded this]]
+          have "th1 = th" by auto
+          from rt1[unfolded this] show ?thesis by auto
+        next
+          case False
+          from 1(1)[rule_format, OF _ rp' this, unfolded Cons1 Cons]
+          have "(Th th1, Th th) \<in> (tRAG s)\<^sup>+" by simp
+          with rt1 show ?thesis by auto
+        qed
+      qed
+    qed
+    hence "th' \<in> ?L" by auto
+  } ultimately show ?thesis by blast
+qed
+
+lemma tRAG_trancl_eq_Th:
+   "{Th th' | th'. (Th th', Th th)  \<in> (tRAG s)^+} = 
+    {Th th' | th'. (Th th', Th th)  \<in> (RAG s)^+}"
+    using tRAG_trancl_eq by auto
+
+
+lemma tRAG_Field:
+  "Field (tRAG s) \<subseteq> Field (RAG s)"
+  by (unfold tRAG_alt_def Field_def, auto)
+
+lemma tRAG_mono:
+  assumes "RAG s' \<subseteq> RAG s"
+  shows "tRAG s' \<subseteq> tRAG s"
+  using assms 
+  by (unfold tRAG_alt_def, auto)
+
+lemma tRAG_subtree_eq: 
+   "(subtree (tRAG s) (Th th)) = {Th th' | th'. Th th'  \<in> (subtree (RAG s) (Th th))}"
+   (is "?L = ?R")
+proof -
+  { fix n 
+    assume h: "n \<in> ?L"
+    hence "n \<in> ?R"
+    by (smt mem_Collect_eq subsetCE subtree_def subtree_nodeE tRAG_subtree_RAG) 
+  } moreover {
+    fix n
+    assume "n \<in> ?R"
+    then obtain th' where h: "n = Th th'" "(Th th', Th th) \<in> (RAG s)^*"
+      by (auto simp:subtree_def)
+    from rtranclD[OF this(2)]
+    have "n \<in> ?L"
+    proof
+      assume "Th th' \<noteq> Th th \<and> (Th th', Th th) \<in> (RAG s)\<^sup>+"
+      with h have "n \<in> {Th th' | th'. (Th th', Th th)  \<in> (RAG s)^+}" by auto
+      thus ?thesis using subtree_def tRAG_trancl_eq by fastforce
+    qed (insert h, auto simp:subtree_def)
+  } ultimately show ?thesis by auto
+qed
+
+lemma threads_set_eq: 
+   "the_thread ` (subtree (tRAG s) (Th th)) = 
+                  {th'. Th th' \<in> (subtree (RAG s) (Th th))}" (is "?L = ?R")
+   by (auto intro:rev_image_eqI simp:tRAG_subtree_eq)
 
 context valid_trace
 begin
 
-lemma finite_subtree_threads:
-    "finite {th'. Th th' \<in> subtree (RAG s) (Th th)}" (is "finite ?A")
+lemma RAG_tRAG_transfer:
+  assumes  "RAG s' = RAG s \<union> {(Th th, Cs cs)}"
+  and "(Cs cs, Th th'') \<in> RAG s"
+  shows "tRAG s' = tRAG s \<union> {(Th th, Th th'')}" (is "?L = ?R")
 proof -
-  have "?A = the_thread ` {Th th' | th' . Th th' \<in> subtree (RAG s) (Th th)}"
-        by (auto, insert image_iff, fastforce)
-  moreover have "finite {Th th' | th' . Th th' \<in> subtree (RAG s) (Th th)}"
-        (is "finite ?B")
-  proof -
-     have "?B = (subtree (RAG s) (Th th)) \<inter> {Th th' | th'. True}"
-      by auto
-     moreover have "... \<subseteq> (subtree (RAG s) (Th th))" by auto
-     moreover have "finite ..." by (simp add: fsbtRAGs.finite_subtree) 
-     ultimately show ?thesis by auto
-  qed
-  ultimately show ?thesis by auto
+  { fix n1 n2
+    assume "(n1, n2) \<in> ?L"
+    from this[unfolded tRAG_alt_def]
+    obtain th1 th2 cs' where 
+      h: "n1 = Th th1" "n2 = Th th2" 
+         "(Th th1, Cs cs') \<in> RAG s'"
+         "(Cs cs', Th th2) \<in> RAG s'" by auto
+    from h(4) and assms(1) have cs_in: "(Cs cs', Th th2) \<in> RAG s" by auto
+    from h(3) and assms(1) 
+    have "(Th th1, Cs cs') = (Th th, Cs cs) \<or> 
+          (Th th1, Cs cs') \<in> RAG s" by auto
+    hence "(n1, n2) \<in> ?R"
+    proof
+      assume h1: "(Th th1, Cs cs') = (Th th, Cs cs)"
+      hence eq_th1: "th1 = th" by simp
+      moreover have "th2 = th''"
+      proof -
+        from h1 have "cs' = cs" by simp
+        from assms(2) cs_in[unfolded this]
+        show ?thesis using unique_RAG by auto 
+      qed
+      ultimately show ?thesis using h(1,2) by auto
+    next
+      assume "(Th th1, Cs cs') \<in> RAG s"
+      with cs_in have "(Th th1, Th th2) \<in> tRAG s"
+        by (unfold tRAG_alt_def, auto)
+      from this[folded h(1, 2)] show ?thesis by auto
+    qed
+  } moreover {
+    fix n1 n2
+    assume "(n1, n2) \<in> ?R"
+    hence "(n1, n2) \<in>tRAG s \<or> (n1, n2) = (Th th, Th th'')" by auto
+    hence "(n1, n2) \<in> ?L" 
+    proof
+      assume "(n1, n2) \<in> tRAG s"
+      moreover have "... \<subseteq> ?L"
+      proof(rule tRAG_mono)
+        show "RAG s \<subseteq> RAG s'" by (unfold assms(1), auto)
+      qed
+      ultimately show ?thesis by auto
+    next
+      assume eq_n: "(n1, n2) = (Th th, Th th'')"
+      from assms(1, 2) have "(Cs cs, Th th'') \<in> RAG s'" by auto
+      moreover have "(Th th, Cs cs) \<in> RAG s'" using assms(1) by auto
+      ultimately show ?thesis 
+        by (unfold eq_n tRAG_alt_def, auto)
+    qed
+  } ultimately show ?thesis by auto
 qed
 
-lemma le_cp:
-  shows "preced th s \<le> cp s th"
-  proof(unfold cp_alt_def, rule Max_ge)
-    show "finite (the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)})"
-      by (simp add: finite_subtree_threads)
-  next
-    show "preced th s \<in> the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)}"
-      by (simp add: subtree_def the_preced_def)   
+lemma subtree_tRAG_thread:
+  assumes "th \<in> threads s"
+  shows "subtree (tRAG s) (Th th) \<subseteq> Th ` threads s" (is "?L \<subseteq> ?R")
+proof -
+  have "?L = {Th th' |th'. Th th' \<in> subtree (RAG s) (Th th)}"
+    by (unfold tRAG_subtree_eq, simp)
+  also have "... \<subseteq> ?R"
+  proof
+    fix x
+    assume "x \<in> {Th th' |th'. Th th' \<in> subtree (RAG s) (Th th)}"
+    then obtain th' where h: "x = Th th'" "Th th' \<in> subtree (RAG s) (Th th)" by auto
+    from this(2)
+    show "x \<in> ?R"
+    proof(cases rule:subtreeE)
+      case 1
+      thus ?thesis by (simp add: assms h(1)) 
+    next
+      case 2
+      thus ?thesis by (metis ancestors_Field dm_RAG_threads h(1) image_eqI) 
+    qed
   qed
-
-lemma cp_le:
-  assumes th_in: "th \<in> threads s"
-  shows "cp s th \<le> Max (the_preced s ` threads s)"
-proof(unfold cp_alt_def, rule Max_f_mono)
-  show "finite (threads s)" by (simp add: finite_threads) 
-next
-  show " {th'. Th th' \<in> subtree (RAG s) (Th th)} \<noteq> {}"
-    using subtree_def by fastforce
-next
-  show "{th'. Th th' \<in> subtree (RAG s) (Th th)} \<subseteq> threads s"
-    using assms
-    by (smt Domain.DomainI dm_RAG_threads mem_Collect_eq 
-        node.inject(1) rtranclD subsetI subtree_def trancl_domain) 
+  finally show ?thesis .
 qed
 
-lemma max_cp_eq: 
-  shows "Max ((cp s) ` threads s) = Max (the_preced s ` threads s)"
-  (is "?L = ?R")
-proof -
-  have "?L \<le> ?R" 
-  proof(cases "threads s = {}")
-    case False
-    show ?thesis 
-      by (rule Max.boundedI, 
-          insert cp_le, 
-          auto simp:finite_threads False)
-  qed auto
-  moreover have "?R \<le> ?L"
-    by (rule Max_fg_mono, 
-        simp add: finite_threads,
-        simp add: le_cp the_preced_def)
-  ultimately show ?thesis by auto
-qed
-
-lemma wf_RAG_converse: 
-  shows "wf ((RAG s)^-1)"
-proof(rule finite_acyclic_wf_converse)
-  from finite_RAG 
-  show "finite (RAG s)" .
-next
-  from acyclic_RAG
-  show "acyclic (RAG s)" .
-qed
+lemma dependants_alt_def:
+  "dependants s th = {th'. (Th th', Th th) \<in> (tRAG s)^+}"
+  by (metis eq_RAG s_dependants_def tRAG_trancl_eq)
+
+lemma dependants_alt_def1:
+  "dependants (s::state) th = {th'. (Th th', Th th) \<in> (RAG s)^+}"
+  using dependants_alt_def tRAG_trancl_eq by auto
+
+end
+
+section {* Chain to readys *}
+
+context valid_trace
+begin
 
 lemma chain_building:
   assumes "node \<in> Domain (RAG s)"
@@ -2324,7 +2749,7 @@
 
 text {* \noindent
   The following is just an instance of @{text "chain_building"}.
-*}
+*}                    
 lemma th_chain_to_ready:
   assumes th_in: "th \<in> threads s"
   shows "th \<in> readys s \<or> (\<exists> th'. th' \<in> readys s \<and> (Th th, Th th') \<in> (RAG s)^+)"
@@ -2339,97 +2764,274 @@
   show ?thesis by auto
 qed
 
+lemma finite_subtree_threads:
+    "finite {th'. Th th' \<in> subtree (RAG s) (Th th)}" (is "finite ?A")
+proof -
+  have "?A = the_thread ` {Th th' | th' . Th th' \<in> subtree (RAG s) (Th th)}"
+        by (auto, insert image_iff, fastforce)
+  moreover have "finite {Th th' | th' . Th th' \<in> subtree (RAG s) (Th th)}"
+        (is "finite ?B")
+  proof -
+     have "?B = (subtree (RAG s) (Th th)) \<inter> {Th th' | th'. True}"
+      by auto
+     moreover have "... \<subseteq> (subtree (RAG s) (Th th))" by auto
+     moreover have "finite ..." by (simp add: fsbtRAGs.finite_subtree) 
+     ultimately show ?thesis by auto
+  qed
+  ultimately show ?thesis by auto
+qed
+
+lemma runing_unique:
+  assumes runing_1: "th1 \<in> runing s"
+  and runing_2: "th2 \<in> runing s"
+  shows "th1 = th2"
+proof -
+  from runing_1 and runing_2 have "cp s th1 = cp s th2"
+    unfolding runing_def by auto
+  from this[unfolded cp_alt_def]
+  have eq_max: 
+    "Max (the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th1)}) =
+     Max (the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th2)})" 
+        (is "Max ?L = Max ?R") .
+  have "Max ?L \<in> ?L"
+  proof(rule Max_in)
+    show "finite ?L" by (simp add: finite_subtree_threads) 
+  next
+    show "?L \<noteq> {}" using subtree_def by fastforce 
+  qed
+  then obtain th1' where 
+    h_1: "Th th1' \<in> subtree (RAG s) (Th th1)" "the_preced s th1' = Max ?L"
+    by auto
+  have "Max ?R \<in> ?R"
+  proof(rule Max_in)
+    show "finite ?R" by (simp add: finite_subtree_threads)
+  next
+    show "?R \<noteq> {}" using subtree_def by fastforce 
+  qed
+  then obtain th2' where 
+    h_2: "Th th2' \<in> subtree (RAG s) (Th th2)" "the_preced s th2' = Max ?R"
+    by auto
+  have "th1' = th2'"
+  proof(rule preced_unique)
+    from h_1(1)
+    show "th1' \<in> threads s"
+    proof(cases rule:subtreeE)
+      case 1
+      hence "th1' = th1" by simp
+      with runing_1 show ?thesis by (auto simp:runing_def readys_def)
+    next
+      case 2
+      from this(2)
+      have "(Th th1', Th th1) \<in> (RAG s)^+" by (auto simp:ancestors_def)
+      from tranclD[OF this]
+      have "(Th th1') \<in> Domain (RAG s)" by auto
+      from dm_RAG_threads[OF this] show ?thesis .
+    qed
+  next
+    from h_2(1)
+    show "th2' \<in> threads s"
+    proof(cases rule:subtreeE)
+      case 1
+      hence "th2' = th2" by simp
+      with runing_2 show ?thesis by (auto simp:runing_def readys_def)
+    next
+      case 2
+      from this(2)
+      have "(Th th2', Th th2) \<in> (RAG s)^+" by (auto simp:ancestors_def)
+      from tranclD[OF this]
+      have "(Th th2') \<in> Domain (RAG s)" by auto
+      from dm_RAG_threads[OF this] show ?thesis .
+    qed
+  next
+    have "the_preced s th1' = the_preced s th2'" 
+     using eq_max h_1(2) h_2(2) by metis
+    thus "preced th1' s = preced th2' s" by (simp add:the_preced_def)
+  qed
+  from h_1(1)[unfolded this]
+  have star1: "(Th th2', Th th1) \<in> (RAG s)^*" by (auto simp:subtree_def)
+  from h_2(1)[unfolded this]
+  have star2: "(Th th2', Th th2) \<in> (RAG s)^*" by (auto simp:subtree_def)
+  from star_rpath[OF star1] obtain xs1 
+    where rp1: "rpath (RAG s) (Th th2') xs1 (Th th1)"
+    by auto
+  from star_rpath[OF star2] obtain xs2 
+    where rp2: "rpath (RAG s) (Th th2') xs2 (Th th2)"
+    by auto
+  from rp1 rp2
+  show ?thesis
+  proof(cases)
+    case (less_1 xs')
+    moreover have "xs' = []"
+    proof(rule ccontr)
+      assume otherwise: "xs' \<noteq> []"
+      from rpath_plus[OF less_1(3) this]
+      have "(Th th1, Th th2) \<in> (RAG s)\<^sup>+" .
+      from tranclD[OF this]
+      obtain cs where "waiting s th1 cs"
+        by (unfold s_RAG_def, fold waiting_eq, auto)
+      with runing_1 show False
+        by (unfold runing_def readys_def, auto)
+    qed
+    ultimately have "xs2 = xs1" by simp
+    from rpath_dest_eq[OF rp1 rp2[unfolded this]]
+    show ?thesis by simp
+  next
+    case (less_2 xs')
+    moreover have "xs' = []"
+    proof(rule ccontr)
+      assume otherwise: "xs' \<noteq> []"
+      from rpath_plus[OF less_2(3) this]
+      have "(Th th2, Th th1) \<in> (RAG s)\<^sup>+" .
+      from tranclD[OF this]
+      obtain cs where "waiting s th2 cs"
+        by (unfold s_RAG_def, fold waiting_eq, auto)
+      with runing_2 show False
+        by (unfold runing_def readys_def, auto)
+    qed
+    ultimately have "xs2 = xs1" by simp
+    from rpath_dest_eq[OF rp1 rp2[unfolded this]]
+    show ?thesis by simp
+  qed
+qed
+
+lemma card_runing: "card (runing s) \<le> 1"
+proof(cases "runing s = {}")
+  case True
+  thus ?thesis by auto
+next
+  case False
+  then obtain th where [simp]: "th \<in> runing s" by auto
+  from runing_unique[OF this]
+  have "runing s = {th}" by auto
+  thus ?thesis by auto
+qed
+
 end
 
-lemma count_rec1 [simp]: 
-  assumes "Q e"
-  shows "count Q (e#es) = Suc (count Q es)"
-  using assms
-  by (unfold count_def, auto)
-
-lemma count_rec2 [simp]: 
-  assumes "\<not>Q e"
-  shows "count Q (e#es) = (count Q es)"
-  using assms
-  by (unfold count_def, auto)
-
-lemma count_rec3 [simp]: 
-  shows "count Q [] =  0"
-  by (unfold count_def, auto)
-
-lemma cntP_simp1[simp]:
-  "cntP (P th cs'#s) th = cntP s th + 1"
-  by (unfold cntP_def, simp)
-
-lemma cntP_simp2[simp]:
-  assumes "th' \<noteq> th"
-  shows "cntP (P th cs'#s) th' = cntP s th'"
-  using assms
-  by (unfold cntP_def, simp)
-
-lemma cntP_simp3[simp]:
-  assumes "\<not> isP e"
-  shows "cntP (e#s) th' = cntP s th'"
-  using assms
-  by (unfold cntP_def, cases e, simp+)
-
-lemma cntV_simp1[simp]:
-  "cntV (V th cs'#s) th = cntV s th + 1"
-  by (unfold cntV_def, simp)
-
-lemma cntV_simp2[simp]:
-  assumes "th' \<noteq> th"
-  shows "cntV (V th cs'#s) th' = cntV s th'"
-  using assms
-  by (unfold cntV_def, simp)
-
-lemma cntV_simp3[simp]:
-  assumes "\<not> isV e"
-  shows "cntV (e#s) th' = cntV s th'"
-  using assms
-  by (unfold cntV_def, cases e, simp+)
-
-lemma cntP_diff_inv:
-  assumes "cntP (e#s) th \<noteq> cntP s th"
-  shows "isP e \<and> actor e = th"
-proof(cases e)
-  case (P th' pty)
-  show ?thesis
-  by (cases "(\<lambda>e. \<exists>cs. e = P th cs) (P th' pty)", 
-        insert assms P, auto simp:cntP_def)
-qed (insert assms, auto simp:cntP_def)
-  
-lemma cntV_diff_inv:
-  assumes "cntV (e#s) th \<noteq> cntV s th"
-  shows "isV e \<and> actor e = th"
-proof(cases e)
-  case (V th' pty)
-  show ?thesis
-  by (cases "(\<lambda>e. \<exists>cs. e = V th cs) (V th' pty)", 
-        insert assms V, auto simp:cntV_def)
-qed (insert assms, auto simp:cntV_def)
-
-lemma children_RAG_alt_def:
-  "children (RAG (s::state)) (Th th) = Cs ` {cs. holding s th cs}"
-  by (unfold s_RAG_def, auto simp:children_def holding_eq)
-
-lemma holdents_alt_def:
-  "holdents s th = the_cs ` (children (RAG (s::state)) (Th th))"
-  by (unfold children_RAG_alt_def holdents_def, simp add: image_image)
-
-lemma cntCS_alt_def:
-  "cntCS s th = card (children (RAG s) (Th th))"
-  apply (unfold children_RAG_alt_def cntCS_def holdents_def)
-  by (rule card_image[symmetric], auto simp:inj_on_def)
+
+section {* Relating @{term cp} and @{term the_preced} and @{term preced} *}
 
 context valid_trace
 begin
 
-lemma finite_holdents: "finite (holdents s th)"
-  by (unfold holdents_alt_def, insert fsbtRAGs.finite_children, auto)
-  
+lemma le_cp:
+  shows "preced th s \<le> cp s th"
+  proof(unfold cp_alt_def, rule Max_ge)
+    show "finite (the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)})"
+      by (simp add: finite_subtree_threads)
+  next
+    show "preced th s \<in> the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)}"
+      by (simp add: subtree_def the_preced_def)   
+  qed
+
+
+lemma cp_le:
+  assumes th_in: "th \<in> threads s"
+  shows "cp s th \<le> Max (the_preced s ` threads s)"
+proof(unfold cp_alt_def, rule Max_f_mono)
+  show "finite (threads s)" by (simp add: finite_threads) 
+next
+  show " {th'. Th th' \<in> subtree (RAG s) (Th th)} \<noteq> {}"
+    using subtree_def by fastforce
+next
+  show "{th'. Th th' \<in> subtree (RAG s) (Th th)} \<subseteq> threads s"
+    using assms
+    by (smt Domain.DomainI dm_RAG_threads mem_Collect_eq 
+        node.inject(1) rtranclD subsetI subtree_def trancl_domain) 
+qed
+
+lemma max_cp_eq: 
+  shows "Max ((cp s) ` threads s) = Max (the_preced s ` threads s)"
+  (is "?L = ?R")
+proof -
+  have "?L \<le> ?R" 
+  proof(cases "threads s = {}")
+    case False
+    show ?thesis 
+      by (rule Max.boundedI, 
+          insert cp_le, 
+          auto simp:finite_threads False)
+  qed auto
+  moreover have "?R \<le> ?L"
+    by (rule Max_fg_mono, 
+        simp add: finite_threads,
+        simp add: le_cp the_preced_def)
+  ultimately show ?thesis by auto
+qed
+
+lemma threads_alt_def:
+  "(threads s) = (\<Union> th \<in> readys s. {th'. Th th' \<in> subtree (RAG s) (Th th)})"
+    (is "?L = ?R")
+proof -
+  { fix th1
+    assume "th1 \<in> ?L"
+    from th_chain_to_ready[OF this]
+    have "th1 \<in> readys s \<or> (\<exists>th'. th' \<in> readys s \<and> (Th th1, Th th') \<in> (RAG s)\<^sup>+)" .
+    hence "th1 \<in> ?R" by (auto simp:subtree_def)
+  } moreover 
+  { fix th'
+    assume "th' \<in> ?R"
+    then obtain th where h: "th \<in> readys s" " Th th' \<in> subtree (RAG s) (Th th)"
+      by auto
+    from this(2)
+    have "th' \<in> ?L" 
+    proof(cases rule:subtreeE)
+      case 1
+      with h(1) show ?thesis by (auto simp:readys_def)
+    next
+      case 2
+      from tranclD[OF this(2)[unfolded ancestors_def, simplified]]
+      have "Th th' \<in> Domain (RAG s)" by auto
+      from dm_RAG_threads[OF this]
+      show ?thesis .
+    qed
+  } ultimately show ?thesis by auto
+qed
+
+
+text {* (* ccc *) \noindent
+  Since the current precedence of the threads in ready queue will always be boosted,
+  there must be one inside it has the maximum precedence of the whole system. 
+*}
+lemma max_cp_readys_threads:
+  shows "Max (cp s ` readys s) = Max (cp s ` threads s)" (is "?L = ?R")
+proof(cases "readys s = {}")
+  case False
+  have "?R = Max (the_preced s ` threads s)" by (unfold max_cp_eq, simp)
+  also have "... = 
+    Max (the_preced s ` (\<Union>th\<in>readys s. {th'. Th th' \<in> subtree (RAG s) (Th th)}))" 
+         by (unfold threads_alt_def, simp)
+  also have "... = 
+    Max ((\<Union>th\<in>readys s. the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)}))"
+          by (unfold image_UN, simp)
+  also have "... = 
+    Max (Max ` (\<lambda>th. the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)}) ` readys s)" 
+  proof(rule Max_UNION)
+    show "\<forall>M\<in>(\<lambda>x. the_preced s ` 
+                    {th'. Th th' \<in> subtree (RAG s) (Th x)}) ` readys s. finite M"
+                        using finite_subtree_threads by auto
+  qed (auto simp:False subtree_def)
+  also have "... =  
+    Max ((Max \<circ> (\<lambda>th. the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)})) ` readys s)" 
+      by (unfold image_comp, simp)
+  also have "... = ?L" (is "Max (?f ` ?A) = Max (?g ` ?A)")
+  proof -
+    have "(?f ` ?A) = (?g ` ?A)"
+    proof(rule f_image_eq)
+      fix th1 
+      assume "th1 \<in> ?A"
+      thus "?f th1 = ?g th1"
+        by (unfold cp_alt_def, simp)
+    qed
+    thus ?thesis by simp
+  qed
+  finally show ?thesis by simp
+qed (auto simp:threads_alt_def)
+
 end
 
+section {* Relating @{term cntP}, @{term cntV}, @{term cntCS} and @{term pvD} *}
+
 context valid_trace_p_w
 begin
 
@@ -2492,6 +3094,30 @@
 
 end
   
+lemma (in valid_trace) finite_holdents: "finite (holdents s th)"
+  by (unfold holdents_alt_def, insert fsbtRAGs.finite_children, auto)
+
+context valid_trace_p 
+begin
+
+lemma ready_th_s: "th \<in> readys s"
+  using runing_th_s
+  by (unfold runing_def, auto)
+
+lemma live_th_s: "th \<in> threads s"
+  using readys_threads ready_th_s by auto
+
+lemma live_th_es: "th \<in> threads (e#s)"
+  using live_th_s 
+  by (unfold is_p, simp)
+
+lemma waiting_neq_th: 
+  assumes "waiting s t c"
+  shows "t \<noteq> th"
+  using assms using th_not_waiting by blast 
+
+end
+
 context valid_trace_p_h
 begin
 
@@ -2711,7 +3337,7 @@
 end
 
 
-context valid_trace_v (* ccc *)
+context valid_trace_v 
 begin
 
 lemma holding_th_cs_s: 
@@ -2742,7 +3368,7 @@
 proof -
   have "cs \<in> holdents s th" using holding_th_cs_s
     by (unfold holdents_def, simp)
-  moreover have "finite (holdents s th)" using finite_holdents
+  moreover have "finite (holdents s th)" using finite_holdents 
     by simp
   ultimately show ?thesis
     by (unfold cntCS_def, 
@@ -2751,6 +3377,25 @@
 
 end
 
+context valid_trace_v
+begin
+
+lemma th_not_waiting: 
+  "\<not> waiting s th c"
+proof -
+  have "th \<in> readys s"
+    using runing_ready runing_th_s by blast 
+  thus ?thesis
+    by (unfold readys_def, auto)
+qed
+
+lemma waiting_neq_th: 
+  assumes "waiting s t c"
+  shows "t \<noteq> th"
+  using assms using th_not_waiting by blast 
+
+end
+
 context valid_trace_v_n
 begin
 
@@ -2785,7 +3430,7 @@
 qed
 
 lemma neq_taker_th: "taker \<noteq> th"
-  using th_not_waiting waiting_taker by blast
+  using th_not_waiting waiting_taker by blast 
 
 lemma not_holding_taker_s_cs:
   shows "\<not> holding s taker cs"
@@ -3212,7 +3857,7 @@
 lemma not_waiting_th_s [simp]: "\<not> waiting s th cs'"
 proof
   assume "waiting s th cs'"
-  from this[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp]
+  from this[unfolded s_waiting_def, folded wq_def, unfolded wq_kept]
   have "th \<in> set (wq s cs')" by auto
   from wq_threads[OF this] have "th \<in> threads s" .
   with th_not_live_s show False by simp
@@ -3221,7 +3866,7 @@
 lemma not_holding_th_s [simp]: "\<not> holding s th cs'"
 proof
   assume "holding s th cs'"
-  from this[unfolded s_holding_def, folded wq_def, unfolded wq_neq_simp]
+  from this[unfolded s_holding_def, folded wq_def, unfolded wq_kept]
   have "th \<in> set (wq s cs')" by auto
   from wq_threads[OF this] have "th \<in> threads s" .
   with th_not_live_s show False by simp
@@ -3230,7 +3875,7 @@
 lemma not_waiting_th_es [simp]: "\<not> waiting (e#s) th cs'"
 proof
   assume "waiting (e # s) th cs'"
-  from this[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp]
+  from this[unfolded s_waiting_def, folded wq_def, unfolded wq_kept]
   have "th \<in> set (wq s cs')" by auto
   from wq_threads[OF this] have "th \<in> threads s" .
   with th_not_live_s show False by simp
@@ -3239,7 +3884,7 @@
 lemma not_holding_th_es [simp]: "\<not> holding (e#s) th cs'"
 proof
   assume "holding (e # s) th cs'"
-  from this[unfolded s_holding_def, folded wq_def, unfolded wq_neq_simp]
+  from this[unfolded s_holding_def, folded wq_def, unfolded wq_kept]
   have "th \<in> set (wq s cs')" by auto
   from wq_threads[OF this] have "th \<in> threads s" .
   with th_not_live_s show False by simp
@@ -3274,13 +3919,13 @@
     assume h: "cs' \<in> ?L"
     hence "cs' \<in> ?R"
       by (unfold holdents_def s_holding_def, fold wq_def, 
-             unfold wq_neq_simp, auto)
+             unfold wq_kept, auto)
   } moreover {
     fix cs'
     assume h: "cs' \<in> ?R"
     hence "cs' \<in> ?L"
       by (unfold holdents_def s_holding_def, fold wq_def, 
-             unfold wq_neq_simp, auto)
+             unfold wq_kept, auto)
   } ultimately show ?thesis by auto
 qed
 
@@ -3300,7 +3945,7 @@
     have n_wait: "\<not> waiting (e#s) th' cs'" 
       using assms by (auto simp:readys_def)
     from wait[unfolded s_waiting_def, folded wq_def]
-         n_wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp]
+         n_wait[unfolded s_waiting_def, folded wq_def, unfolded wq_kept]
     have False by auto
   } thus ?thesis using assms
     by (unfold readys_def, auto)
@@ -3315,7 +3960,7 @@
     assume wait: "waiting (e#s) th' cs'"
     have n_wait: "\<not> waiting s th' cs'"
       using assms(2) by (auto simp:readys_def)
-    from wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp]
+    from wait[unfolded s_waiting_def, folded wq_def, unfolded wq_kept]
          n_wait[unfolded s_waiting_def, folded wq_def]
     have False by auto
   } with assms show ?thesis  
@@ -3388,7 +4033,7 @@
 lemma not_holding_th_es [simp]: "\<not> holding (e#s) th cs'"
 proof
   assume "holding (e # s) th cs'"
-  from this[unfolded s_holding_def, folded wq_def, unfolded wq_neq_simp]
+  from this[unfolded s_holding_def, folded wq_def, unfolded wq_kept]
   have "holding s th cs'" 
     by (unfold s_holding_def, fold wq_def, auto)
   with not_holding_th_s 
@@ -3421,13 +4066,13 @@
     assume h: "cs' \<in> ?L"
     hence "cs' \<in> ?R"
       by (unfold holdents_def s_holding_def, fold wq_def, 
-             unfold wq_neq_simp, auto)
+             unfold wq_kept, auto)
   } moreover {
     fix cs'
     assume h: "cs' \<in> ?R"
     hence "cs' \<in> ?L"
       by (unfold holdents_def s_holding_def, fold wq_def, 
-             unfold wq_neq_simp, auto)
+             unfold wq_kept, auto)
   } ultimately show ?thesis by auto
 qed
 
@@ -3447,7 +4092,7 @@
     have n_wait: "\<not> waiting (e#s) th' cs'" 
       using assms by (auto simp:readys_def)
     from wait[unfolded s_waiting_def, folded wq_def]
-         n_wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp]
+         n_wait[unfolded s_waiting_def, folded wq_def, unfolded wq_kept]
     have False by auto
   } thus ?thesis using assms
     by (unfold readys_def, auto)
@@ -3462,7 +4107,7 @@
     assume wait: "waiting (e#s) th' cs'"
     have n_wait: "\<not> waiting s th' cs'"
       using assms(2) by (auto simp:readys_def)
-    from wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp]
+    from wait[unfolded s_waiting_def, folded wq_def, unfolded wq_kept]
          n_wait[unfolded s_waiting_def, folded wq_def]
     have False by auto
   } with assms show ?thesis  
@@ -3526,13 +4171,13 @@
     assume h: "cs' \<in> ?L"
     hence "cs' \<in> ?R"
       by (unfold holdents_def s_holding_def, fold wq_def, 
-             unfold wq_neq_simp, auto)
+             unfold wq_kept, auto)
   } moreover {
     fix cs'
     assume h: "cs' \<in> ?R"
     hence "cs' \<in> ?L"
       by (unfold holdents_def s_holding_def, fold wq_def, 
-             unfold wq_neq_simp, auto)
+             unfold wq_kept, auto)
   } ultimately show ?thesis by auto
 qed
 
@@ -3554,7 +4199,7 @@
     have n_wait: "\<not> waiting (e#s) th' cs'" 
       using assms by (auto simp:readys_def)
     from wait[unfolded s_waiting_def, folded wq_def]
-         n_wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp]
+         n_wait[unfolded s_waiting_def, folded wq_def, unfolded wq_kept]
     have False by auto
   } moreover have "th' \<in> threads s" 
     using assms[unfolded readys_def] by auto
@@ -3570,7 +4215,7 @@
     assume wait: "waiting (e#s) th' cs'"
     have n_wait: "\<not> waiting s th' cs'"
       using assms by (auto simp:readys_def)
-    from wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp]
+    from wait[unfolded s_waiting_def, folded wq_def, unfolded wq_kept]
          n_wait[unfolded s_waiting_def, folded wq_def]
     have False by auto
   } with assms show ?thesis  
@@ -3633,6 +4278,13 @@
   qed
 qed
 
+end
+
+section {* Corollaries of @{thm valid_trace.cnp_cnv_cncs} *}
+
+context valid_trace
+begin
+
 lemma not_thread_holdents:
   assumes not_in: "th \<notin> threads s" 
   shows "holdents s th = {}"
@@ -3660,158 +4312,6 @@
   using assms cnp_cnv_cncs not_thread_cncs pvD_def
   by (auto)
 
-lemma runing_unique:
-  assumes runing_1: "th1 \<in> runing s"
-  and runing_2: "th2 \<in> runing s"
-  shows "th1 = th2"
-proof -
-  from runing_1 and runing_2 have "cp s th1 = cp s th2"
-    unfolding runing_def by auto
-  from this[unfolded cp_alt_def]
-  have eq_max: 
-    "Max (the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th1)}) =
-     Max (the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th2)})" 
-        (is "Max ?L = Max ?R") .
-  have "Max ?L \<in> ?L"
-  proof(rule Max_in)
-    show "finite ?L" by (simp add: finite_subtree_threads)
-  next
-    show "?L \<noteq> {}" using subtree_def by fastforce 
-  qed
-  then obtain th1' where 
-    h_1: "Th th1' \<in> subtree (RAG s) (Th th1)" "the_preced s th1' = Max ?L"
-    by auto
-  have "Max ?R \<in> ?R"
-  proof(rule Max_in)
-    show "finite ?R" by (simp add: finite_subtree_threads)
-  next
-    show "?R \<noteq> {}" using subtree_def by fastforce 
-  qed
-  then obtain th2' where 
-    h_2: "Th th2' \<in> subtree (RAG s) (Th th2)" "the_preced s th2' = Max ?R"
-    by auto
-  have "th1' = th2'"
-  proof(rule preced_unique)
-    from h_1(1)
-    show "th1' \<in> threads s"
-    proof(cases rule:subtreeE)
-      case 1
-      hence "th1' = th1" by simp
-      with runing_1 show ?thesis by (auto simp:runing_def readys_def)
-    next
-      case 2
-      from this(2)
-      have "(Th th1', Th th1) \<in> (RAG s)^+" by (auto simp:ancestors_def)
-      from tranclD[OF this]
-      have "(Th th1') \<in> Domain (RAG s)" by auto
-      from dm_RAG_threads[OF this] show ?thesis .
-    qed
-  next
-    from h_2(1)
-    show "th2' \<in> threads s"
-    proof(cases rule:subtreeE)
-      case 1
-      hence "th2' = th2" by simp
-      with runing_2 show ?thesis by (auto simp:runing_def readys_def)
-    next
-      case 2
-      from this(2)
-      have "(Th th2', Th th2) \<in> (RAG s)^+" by (auto simp:ancestors_def)
-      from tranclD[OF this]
-      have "(Th th2') \<in> Domain (RAG s)" by auto
-      from dm_RAG_threads[OF this] show ?thesis .
-    qed
-  next
-    have "the_preced s th1' = the_preced s th2'" 
-     using eq_max h_1(2) h_2(2) by metis
-    thus "preced th1' s = preced th2' s" by (simp add:the_preced_def)
-  qed
-  from h_1(1)[unfolded this]
-  have star1: "(Th th2', Th th1) \<in> (RAG s)^*" by (auto simp:subtree_def)
-  from h_2(1)[unfolded this]
-  have star2: "(Th th2', Th th2) \<in> (RAG s)^*" by (auto simp:subtree_def)
-  from star_rpath[OF star1] obtain xs1 
-    where rp1: "rpath (RAG s) (Th th2') xs1 (Th th1)"
-    by auto
-  from star_rpath[OF star2] obtain xs2 
-    where rp2: "rpath (RAG s) (Th th2') xs2 (Th th2)"
-    by auto
-  from rp1 rp2
-  show ?thesis
-  proof(cases)
-    case (less_1 xs')
-    moreover have "xs' = []"
-    proof(rule ccontr)
-      assume otherwise: "xs' \<noteq> []"
-      from rpath_plus[OF less_1(3) this]
-      have "(Th th1, Th th2) \<in> (RAG s)\<^sup>+" .
-      from tranclD[OF this]
-      obtain cs where "waiting s th1 cs"
-        by (unfold s_RAG_def, fold waiting_eq, auto)
-      with runing_1 show False
-        by (unfold runing_def readys_def, auto)
-    qed
-    ultimately have "xs2 = xs1" by simp
-    from rpath_dest_eq[OF rp1 rp2[unfolded this]]
-    show ?thesis by simp
-  next
-    case (less_2 xs')
-    moreover have "xs' = []"
-    proof(rule ccontr)
-      assume otherwise: "xs' \<noteq> []"
-      from rpath_plus[OF less_2(3) this]
-      have "(Th th2, Th th1) \<in> (RAG s)\<^sup>+" .
-      from tranclD[OF this]
-      obtain cs where "waiting s th2 cs"
-        by (unfold s_RAG_def, fold waiting_eq, auto)
-      with runing_2 show False
-        by (unfold runing_def readys_def, auto)
-    qed
-    ultimately have "xs2 = xs1" by simp
-    from rpath_dest_eq[OF rp1 rp2[unfolded this]]
-    show ?thesis by simp
-  qed
-qed
-
-lemma card_runing: "card (runing s) \<le> 1"
-proof(cases "runing s = {}")
-  case True
-  thus ?thesis by auto
-next
-  case False
-  then obtain th where [simp]: "th \<in> runing s" by auto
-  from runing_unique[OF this]
-  have "runing s = {th}" by auto
-  thus ?thesis by auto
-qed
-
-lemma create_pre:
-  assumes stp: "step s e"
-  and not_in: "th \<notin> threads s"
-  and is_in: "th \<in> threads (e#s)"
-  obtains prio where "e = Create th prio"
-proof -
-  from assms  
-  show ?thesis
-  proof(cases)
-    case (thread_create thread prio)
-    with is_in not_in have "e = Create th prio" by simp
-    from that[OF this] show ?thesis .
-  next
-    case (thread_exit thread)
-    with assms show ?thesis by (auto intro!:that)
-  next
-    case (thread_P thread)
-    with assms show ?thesis by (auto intro!:that)
-  next
-    case (thread_V thread)
-    with assms show ?thesis by (auto intro!:that)
-  next 
-    case (thread_set thread)
-    with assms show ?thesis by (auto intro!:that)
-  qed
-qed
-
 lemma eq_pv_children:
   assumes eq_pv: "cntP s th = cntV s th"
   shows "children (RAG s) (Th th) = {}"
@@ -3838,151 +4338,6 @@
   using eq_pv_children[OF assms]
     by (unfold subtree_children, simp)
 
-end
-
-lemma cp_gen_alt_def:
-  "cp_gen s = (Max \<circ> (\<lambda>x. (the_preced s \<circ> the_thread) ` subtree (tRAG s) x))"
-    by (auto simp:cp_gen_def)
-
-lemma tRAG_nodeE:
-  assumes "(n1, n2) \<in> tRAG s"
-  obtains th1 th2 where "n1 = Th th1" "n2 = Th th2"
-  using assms
-  by (auto simp: tRAG_def wRAG_def hRAG_def tRAG_def)
-
-lemma subtree_nodeE:
-  assumes "n \<in> subtree (tRAG s) (Th th)"
-  obtains th1 where "n = Th th1"
-proof -
-  show ?thesis
-  proof(rule subtreeE[OF assms])
-    assume "n = Th th"
-    from that[OF this] show ?thesis .
-  next
-    assume "Th th \<in> ancestors (tRAG s) n"
-    hence "(n, Th th) \<in> (tRAG s)^+" by (auto simp:ancestors_def)
-    hence "\<exists> th1. n = Th th1"
-    proof(induct)
-      case (base y)
-      from tRAG_nodeE[OF this] show ?case by metis
-    next
-      case (step y z)
-      thus ?case by auto
-    qed
-    with that show ?thesis by auto
-  qed
-qed
-
-lemma tRAG_star_RAG: "(tRAG s)^* \<subseteq> (RAG s)^*"
-proof -
-  have "(wRAG s O hRAG s)^* \<subseteq> (RAG s O RAG s)^*" 
-    by (rule rtrancl_mono, auto simp:RAG_split)
-  also have "... \<subseteq> ((RAG s)^*)^*"
-    by (rule rtrancl_mono, auto)
-  also have "... = (RAG s)^*" by simp
-  finally show ?thesis by (unfold tRAG_def, simp)
-qed
-
-lemma tRAG_subtree_RAG: "subtree (tRAG s) x \<subseteq> subtree (RAG s) x"
-proof -
-  { fix a
-    assume "a \<in> subtree (tRAG s) x"
-    hence "(a, x) \<in> (tRAG s)^*" by (auto simp:subtree_def)
-    with tRAG_star_RAG
-    have "(a, x) \<in> (RAG s)^*" by auto
-    hence "a \<in> subtree (RAG s) x" by (auto simp:subtree_def)
-  } thus ?thesis by auto
-qed
-
-lemma tRAG_trancl_eq:
-   "{th'. (Th th', Th th)  \<in> (tRAG s)^+} = 
-    {th'. (Th th', Th th)  \<in> (RAG s)^+}"
-   (is "?L = ?R")
-proof -
-  { fix th'
-    assume "th' \<in> ?L"
-    hence "(Th th', Th th) \<in> (tRAG s)^+" by auto
-    from tranclD[OF this]
-    obtain z where h: "(Th th', z) \<in> tRAG s" "(z, Th th) \<in> (tRAG s)\<^sup>*" by auto
-    from tRAG_subtree_RAG and this(2)
-    have "(z, Th th) \<in> (RAG s)^*" by (meson subsetCE tRAG_star_RAG) 
-    moreover from h(1) have "(Th th', z) \<in> (RAG s)^+" using tRAG_alt_def by auto 
-    ultimately have "th' \<in> ?R"  by auto 
-  } moreover 
-  { fix th'
-    assume "th' \<in> ?R"
-    hence "(Th th', Th th) \<in> (RAG s)^+" by (auto)
-    from plus_rpath[OF this]
-    obtain xs where rp: "rpath (RAG s) (Th th') xs (Th th)" "xs \<noteq> []" by auto
-    hence "(Th th', Th th) \<in> (tRAG s)^+"
-    proof(induct xs arbitrary:th' th rule:length_induct)
-      case (1 xs th' th)
-      then obtain x1 xs1 where Cons1: "xs = x1#xs1" by (cases xs, auto)
-      show ?case
-      proof(cases "xs1")
-        case Nil
-        from 1(2)[unfolded Cons1 Nil]
-        have rp: "rpath (RAG s) (Th th') [x1] (Th th)" .
-        hence "(Th th', x1) \<in> (RAG s)" 
-          by (cases, auto)
-        then obtain cs where "x1 = Cs cs" 
-              by (unfold s_RAG_def, auto)
-        from rpath_nnl_lastE[OF rp[unfolded this]]
-        show ?thesis by auto
-      next
-        case (Cons x2 xs2)
-        from 1(2)[unfolded Cons1[unfolded this]]
-        have rp: "rpath (RAG s) (Th th') (x1 # x2 # xs2) (Th th)" .
-        from rpath_edges_on[OF this]
-        have eds: "edges_on (Th th' # x1 # x2 # xs2) \<subseteq> RAG s" .
-        have "(Th th', x1) \<in> edges_on (Th th' # x1 # x2 # xs2)"
-            by (simp add: edges_on_unfold)
-        with eds have rg1: "(Th th', x1) \<in> RAG s" by auto
-        then obtain cs1 where eq_x1: "x1 = Cs cs1" by (unfold s_RAG_def, auto)
-        have "(x1, x2) \<in> edges_on (Th th' # x1 # x2 # xs2)"
-            by (simp add: edges_on_unfold)
-        from this eds
-        have rg2: "(x1, x2) \<in> RAG s" by auto
-        from this[unfolded eq_x1] 
-        obtain th1 where eq_x2: "x2 = Th th1" by (unfold s_RAG_def, auto)
-        from rg1[unfolded eq_x1] rg2[unfolded eq_x1 eq_x2]
-        have rt1: "(Th th', Th th1) \<in> tRAG s" by (unfold tRAG_alt_def, auto)
-        from rp have "rpath (RAG s) x2 xs2 (Th th)"
-           by  (elim rpath_ConsE, simp)
-        from this[unfolded eq_x2] have rp': "rpath (RAG s) (Th th1) xs2 (Th th)" .
-        show ?thesis
-        proof(cases "xs2 = []")
-          case True
-          from rpath_nilE[OF rp'[unfolded this]]
-          have "th1 = th" by auto
-          from rt1[unfolded this] show ?thesis by auto
-        next
-          case False
-          from 1(1)[rule_format, OF _ rp' this, unfolded Cons1 Cons]
-          have "(Th th1, Th th) \<in> (tRAG s)\<^sup>+" by simp
-          with rt1 show ?thesis by auto
-        qed
-      qed
-    qed
-    hence "th' \<in> ?L" by auto
-  } ultimately show ?thesis by blast
-qed
-
-lemma tRAG_trancl_eq_Th:
-   "{Th th' | th'. (Th th', Th th)  \<in> (tRAG s)^+} = 
-    {Th th' | th'. (Th th', Th th)  \<in> (RAG s)^+}"
-    using tRAG_trancl_eq by auto
-
-lemma dependants_alt_def:
-  "dependants s th = {th'. (Th th', Th th) \<in> (tRAG s)^+}"
-  by (metis eq_RAG s_dependants_def tRAG_trancl_eq)
-
-lemma dependants_alt_def1:
-  "dependants (s::state) th = {th'. (Th th', Th th) \<in> (RAG s)^+}"
-  using dependants_alt_def tRAG_trancl_eq by auto
-
-context valid_trace
-begin
 lemma count_eq_RAG_plus:
   assumes "cntP s th = cntV s th"
   shows "{th'. (Th th', Th th) \<in> (RAG s)^+} = {}"
@@ -4004,14 +4359,6 @@
   show ?thesis .
 qed
 
-end
-
-lemma eq_dependants: "dependants (wq s) = dependants s"
-  by (simp add: s_dependants_abv wq_def)
-
-context valid_trace
-begin
-
 lemma count_eq_tRAG_plus:
   assumes "cntP s th = cntV s th"
   shows "{th'. (Th th', Th th) \<in> (tRAG s)^+} = {}"
@@ -4026,244 +4373,12 @@
   assumes "cntP s th = cntV s th"
   shows "{Th th' | th'. (Th th', Th th) \<in> (tRAG s)^+} = {}"
    using count_eq_tRAG_plus[OF assms] by auto
-end
-
-lemma inj_the_preced: 
-  "inj_on (the_preced s) (threads s)"
-  by (metis inj_onI preced_unique the_preced_def)
-
-lemma tRAG_Field:
-  "Field (tRAG s) \<subseteq> Field (RAG s)"
-  by (unfold tRAG_alt_def Field_def, auto)
-
-lemma tRAG_ancestorsE:
-  assumes "x \<in> ancestors (tRAG s) u"
-  obtains th where "x = Th th"
-proof -
-  from assms have "(u, x) \<in> (tRAG s)^+" 
-      by (unfold ancestors_def, auto)
-  from tranclE[OF this] obtain c where "(c, x) \<in> tRAG s" by auto
-  then obtain th where "x = Th th"
-    by (unfold tRAG_alt_def, auto)
-  from that[OF this] show ?thesis .
-qed
-
-lemma tRAG_mono:
-  assumes "RAG s' \<subseteq> RAG s"
-  shows "tRAG s' \<subseteq> tRAG s"
-  using assms 
-  by (unfold tRAG_alt_def, auto)
-
-lemma holding_next_thI:
-  assumes "holding s th cs"
-  and "length (wq s cs) > 1"
-  obtains th' where "next_th s th cs th'"
-proof -
-  from assms(1)[folded holding_eq, unfolded cs_holding_def]
-  have " th \<in> set (wq s cs) \<and> th = hd (wq s cs)" 
-    by (unfold s_holding_def, fold wq_def, auto)
-  then obtain rest where h1: "wq s cs = th#rest" 
-    by (cases "wq s cs", auto)
-  with assms(2) have h2: "rest \<noteq> []" by auto
-  let ?th' = "hd (SOME q. distinct q \<and> set q = set rest)"
-  have "next_th s th cs ?th'" using  h1(1) h2 
-    by (unfold next_th_def, auto)
-  from that[OF this] show ?thesis .
-qed
-
-lemma RAG_tRAG_transfer:
-  assumes "vt s'"
-  assumes "RAG s = RAG s' \<union> {(Th th, Cs cs)}"
-  and "(Cs cs, Th th'') \<in> RAG s'"
-  shows "tRAG s = tRAG s' \<union> {(Th th, Th th'')}" (is "?L = ?R")
-proof -
-  interpret vt_s': valid_trace "s'" using assms(1)
-    by (unfold_locales, simp)
-  { fix n1 n2
-    assume "(n1, n2) \<in> ?L"
-    from this[unfolded tRAG_alt_def]
-    obtain th1 th2 cs' where 
-      h: "n1 = Th th1" "n2 = Th th2" 
-         "(Th th1, Cs cs') \<in> RAG s"
-         "(Cs cs', Th th2) \<in> RAG s" by auto
-    from h(4) and assms(2) have cs_in: "(Cs cs', Th th2) \<in> RAG s'" by auto
-    from h(3) and assms(2) 
-    have "(Th th1, Cs cs') = (Th th, Cs cs) \<or> 
-          (Th th1, Cs cs') \<in> RAG s'" by auto
-    hence "(n1, n2) \<in> ?R"
-    proof
-      assume h1: "(Th th1, Cs cs') = (Th th, Cs cs)"
-      hence eq_th1: "th1 = th" by simp
-      moreover have "th2 = th''"
-      proof -
-        from h1 have "cs' = cs" by simp
-        from assms(3) cs_in[unfolded this]
-        show ?thesis using vt_s'.unique_RAG by auto 
-      qed
-      ultimately show ?thesis using h(1,2) by auto
-    next
-      assume "(Th th1, Cs cs') \<in> RAG s'"
-      with cs_in have "(Th th1, Th th2) \<in> tRAG s'"
-        by (unfold tRAG_alt_def, auto)
-      from this[folded h(1, 2)] show ?thesis by auto
-    qed
-  } moreover {
-    fix n1 n2
-    assume "(n1, n2) \<in> ?R"
-    hence "(n1, n2) \<in>tRAG s' \<or> (n1, n2) = (Th th, Th th'')" by auto
-    hence "(n1, n2) \<in> ?L" 
-    proof
-      assume "(n1, n2) \<in> tRAG s'"
-      moreover have "... \<subseteq> ?L"
-      proof(rule tRAG_mono)
-        show "RAG s' \<subseteq> RAG s" by (unfold assms(2), auto)
-      qed
-      ultimately show ?thesis by auto
-    next
-      assume eq_n: "(n1, n2) = (Th th, Th th'')"
-      from assms(2, 3) have "(Cs cs, Th th'') \<in> RAG s" by auto
-      moreover have "(Th th, Cs cs) \<in> RAG s" using assms(2) by auto
-      ultimately show ?thesis 
-        by (unfold eq_n tRAG_alt_def, auto)
-    qed
-  } ultimately show ?thesis by auto
-qed
-
-context valid_trace
-begin
-
-lemmas RAG_tRAG_transfer = RAG_tRAG_transfer[OF vt]
-
-end
-
-lemma tRAG_subtree_eq: 
-   "(subtree (tRAG s) (Th th)) = {Th th' | th'. Th th'  \<in> (subtree (RAG s) (Th th))}"
-   (is "?L = ?R")
-proof -
-  { fix n 
-    assume h: "n \<in> ?L"
-    hence "n \<in> ?R"
-    by (smt mem_Collect_eq subsetCE subtree_def subtree_nodeE tRAG_subtree_RAG) 
-  } moreover {
-    fix n
-    assume "n \<in> ?R"
-    then obtain th' where h: "n = Th th'" "(Th th', Th th) \<in> (RAG s)^*"
-      by (auto simp:subtree_def)
-    from rtranclD[OF this(2)]
-    have "n \<in> ?L"
-    proof
-      assume "Th th' \<noteq> Th th \<and> (Th th', Th th) \<in> (RAG s)\<^sup>+"
-      with h have "n \<in> {Th th' | th'. (Th th', Th th)  \<in> (RAG s)^+}" by auto
-      thus ?thesis using subtree_def tRAG_trancl_eq by fastforce
-    qed (insert h, auto simp:subtree_def)
-  } ultimately show ?thesis by auto
-qed
-
-lemma threads_set_eq: 
-   "the_thread ` (subtree (tRAG s) (Th th)) = 
-                  {th'. Th th' \<in> (subtree (RAG s) (Th th))}" (is "?L = ?R")
-   by (auto intro:rev_image_eqI simp:tRAG_subtree_eq)
-
-lemma cp_alt_def1: 
-  "cp s th = Max ((the_preced s o the_thread) ` (subtree (tRAG s) (Th th)))"
-proof -
-  have "(the_preced s ` the_thread ` subtree (tRAG s) (Th th)) =
-       ((the_preced s \<circ> the_thread) ` subtree (tRAG s) (Th th))"
-       by auto
-  thus ?thesis by (unfold cp_alt_def, fold threads_set_eq, auto)
-qed
-
-lemma cp_gen_def_cond: 
-  assumes "x = Th th"
-  shows "cp s th = cp_gen s (Th th)"
-by (unfold cp_alt_def1 cp_gen_def, simp)
-
-lemma cp_gen_over_set:
-  assumes "\<forall> x \<in> A. \<exists> th. x = Th th"
-  shows "cp_gen s ` A = (cp s \<circ> the_thread) ` A"
-proof(rule f_image_eq)
-  fix a
-  assume "a \<in> A"
-  from assms[rule_format, OF this]
-  obtain th where eq_a: "a = Th th" by auto
-  show "cp_gen s a = (cp s \<circ> the_thread) a"
-    by  (unfold eq_a, simp, unfold cp_gen_def_cond[OF refl[of "Th th"]], simp)
-qed
-
-context valid_trace
-begin
-
-lemma subtree_tRAG_thread:
-  assumes "th \<in> threads s"
-  shows "subtree (tRAG s) (Th th) \<subseteq> Th ` threads s" (is "?L \<subseteq> ?R")
-proof -
-  have "?L = {Th th' |th'. Th th' \<in> subtree (RAG s) (Th th)}"
-    by (unfold tRAG_subtree_eq, simp)
-  also have "... \<subseteq> ?R"
-  proof
-    fix x
-    assume "x \<in> {Th th' |th'. Th th' \<in> subtree (RAG s) (Th th)}"
-    then obtain th' where h: "x = Th th'" "Th th' \<in> subtree (RAG s) (Th th)" by auto
-    from this(2)
-    show "x \<in> ?R"
-    proof(cases rule:subtreeE)
-      case 1
-      thus ?thesis by (simp add: assms h(1)) 
-    next
-      case 2
-      thus ?thesis by (metis ancestors_Field dm_RAG_threads h(1) image_eqI) 
-    qed
-  qed
-  finally show ?thesis .
-qed
-
-lemma readys_root:
-  assumes "th \<in> readys s"
-  shows "root (RAG s) (Th th)"
-proof -
-  { fix x
-    assume "x \<in> ancestors (RAG s) (Th th)"
-    hence h: "(Th th, x) \<in> (RAG s)^+" by (auto simp:ancestors_def)
-    from tranclD[OF this]
-    obtain z where "(Th th, z) \<in> RAG s" by auto
-    with assms(1) have False
-         apply (case_tac z, auto simp:readys_def s_RAG_def s_waiting_def cs_waiting_def)
-         by (fold wq_def, blast)
-  } thus ?thesis by (unfold root_def, auto)
-qed
-
-lemma readys_in_no_subtree:
-  assumes "th \<in> readys s"
-  and "th' \<noteq> th"
-  shows "Th th \<notin> subtree (RAG s) (Th th')" 
-proof
-   assume "Th th \<in> subtree (RAG s) (Th th')"
-   thus False
-   proof(cases rule:subtreeE)
-      case 1
-      with assms show ?thesis by auto
-   next
-      case 2
-      with readys_root[OF assms(1)]
-      show ?thesis by (auto simp:root_def)
-   qed
-qed
-
-lemma not_in_thread_isolated:
-  assumes "th \<notin> threads s"
-  shows "(Th th) \<notin> Field (RAG s)"
-proof
-  assume "(Th th) \<in> Field (RAG s)"
-  with dm_RAG_threads and rg_RAG_threads assms
-  show False by (unfold Field_def, blast)
-qed
 
 end
 
 definition detached :: "state \<Rightarrow> thread \<Rightarrow> bool"
   where "detached s th \<equiv> (\<not>(\<exists> cs. holding s th cs)) \<and> (\<not>(\<exists>cs. waiting s th cs))"
 
-
 lemma detached_test:
   shows "detached s th = (Th th \<notin> Field (RAG s))"
 apply(simp add: detached_def Field_def)
@@ -4336,6 +4451,35 @@
 
 end
 
+section {* Recursive definition of @{term "cp"} *}
+
+lemma cp_alt_def1: 
+  "cp s th = Max ((the_preced s o the_thread) ` (subtree (tRAG s) (Th th)))"
+proof -
+  have "(the_preced s ` the_thread ` subtree (tRAG s) (Th th)) =
+       ((the_preced s \<circ> the_thread) ` subtree (tRAG s) (Th th))"
+       by auto
+  thus ?thesis by (unfold cp_alt_def, fold threads_set_eq, auto)
+qed
+
+lemma cp_gen_def_cond: 
+  assumes "x = Th th"
+  shows "cp s th = cp_gen s (Th th)"
+by (unfold cp_alt_def1 cp_gen_def, simp)
+
+lemma cp_gen_over_set:
+  assumes "\<forall> x \<in> A. \<exists> th. x = Th th"
+  shows "cp_gen s ` A = (cp s \<circ> the_thread) ` A"
+proof(rule f_image_eq)
+  fix a
+  assume "a \<in> A"
+  from assms[rule_format, OF this]
+  obtain th where eq_a: "a = Th th" by auto
+  show "cp_gen s a = (cp s \<circ> the_thread) a"
+    by  (unfold eq_a, simp, unfold cp_gen_def_cond[OF refl[of "Th th"]], simp)
+qed
+
+
 context valid_trace
 begin
 (* ddd *)
@@ -4413,6 +4557,63 @@
     thus ?thesis by (subst (1) h(1), unfold h(2), simp)
   qed
 qed
+end
+
+section {* Other properties useful in Implementation.thy or Correctness.thy *}
+
+context valid_trace_e 
+begin
+
+lemma actor_inv: 
+  assumes "\<not> isCreate e"
+  shows "actor e \<in> runing s"
+  using pip_e assms 
+  by (induct, auto)
+end
+
+context valid_trace
+begin
+
+lemma readys_root:
+  assumes "th \<in> readys s"
+  shows "root (RAG s) (Th th)"
+proof -
+  { fix x
+    assume "x \<in> ancestors (RAG s) (Th th)"
+    hence h: "(Th th, x) \<in> (RAG s)^+" by (auto simp:ancestors_def)
+    from tranclD[OF this]
+    obtain z where "(Th th, z) \<in> RAG s" by auto
+    with assms(1) have False
+         apply (case_tac z, auto simp:readys_def s_RAG_def s_waiting_def cs_waiting_def)
+         by (fold wq_def, blast)
+  } thus ?thesis by (unfold root_def, auto)
+qed
+
+lemma readys_in_no_subtree:
+  assumes "th \<in> readys s"
+  and "th' \<noteq> th"
+  shows "Th th \<notin> subtree (RAG s) (Th th')" 
+proof
+   assume "Th th \<in> subtree (RAG s) (Th th')"
+   thus False
+   proof(cases rule:subtreeE)
+      case 1
+      with assms show ?thesis by auto
+   next
+      case 2
+      with readys_root[OF assms(1)]
+      show ?thesis by (auto simp:root_def)
+   qed
+qed
+
+lemma not_in_thread_isolated:
+  assumes "th \<notin> threads s"
+  shows "(Th th) \<notin> Field (RAG s)"
+proof
+  assume "(Th th) \<in> Field (RAG s)"
+  with dm_RAG_threads and rg_RAG_threads assms
+  show False by (unfold Field_def, blast)
+qed
 
 lemma next_th_holding:
   assumes nxt: "next_th s th cs th'"
@@ -4458,91 +4659,4 @@
 
 end
 
-lemma next_th_unique: 
-  assumes nt1: "next_th s th cs th1"
-  and nt2: "next_th s th cs th2"
-  shows "th1 = th2"
-using assms by (unfold next_th_def, auto)
-
-context valid_trace
-begin
-
-thm th_chain_to_ready
-
-find_theorems subtree Th RAG
-
-lemma threads_alt_def:
-  "(threads s) = (\<Union> th \<in> readys s. {th'. Th th' \<in> subtree (RAG s) (Th th)})"
-    (is "?L = ?R")
-proof -
-  { fix th1
-    assume "th1 \<in> ?L"
-    from th_chain_to_ready[OF this]
-    have "th1 \<in> readys s \<or> (\<exists>th'. th' \<in> readys s \<and> (Th th1, Th th') \<in> (RAG s)\<^sup>+)" .
-    hence "th1 \<in> ?R" by (auto simp:subtree_def)
-  } moreover 
-  { fix th'
-    assume "th' \<in> ?R"
-    then obtain th where h: "th \<in> readys s" " Th th' \<in> subtree (RAG s) (Th th)"
-      by auto
-    from this(2)
-    have "th' \<in> ?L" 
-    proof(cases rule:subtreeE)
-      case 1
-      with h(1) show ?thesis by (auto simp:readys_def)
-    next
-      case 2
-      from tranclD[OF this(2)[unfolded ancestors_def, simplified]]
-      have "Th th' \<in> Domain (RAG s)" by auto
-      from dm_RAG_threads[OF this]
-      show ?thesis .
-    qed
-  } ultimately show ?thesis by auto
-qed
-
-lemma finite_readys [simp]: "finite (readys s)"
-  using finite_threads readys_threads rev_finite_subset by blast
-
-text {* (* ccc *) \noindent
-  Since the current precedence of the threads in ready queue will always be boosted,
-  there must be one inside it has the maximum precedence of the whole system. 
-*}
-lemma max_cp_readys_threads:
-  shows "Max (cp s ` readys s) = Max (cp s ` threads s)" (is "?L = ?R")
-proof(cases "readys s = {}")
-  case False
-  have "?R = Max (the_preced s ` threads s)" by (unfold max_cp_eq, simp)
-  also have "... = 
-    Max (the_preced s ` (\<Union>th\<in>readys s. {th'. Th th' \<in> subtree (RAG s) (Th th)}))" 
-         by (unfold threads_alt_def, simp)
-  also have "... = 
-    Max ((\<Union>th\<in>readys s. the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)}))"
-          by (unfold image_UN, simp)
-  also have "... = 
-    Max (Max ` (\<lambda>th. the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)}) ` readys s)" 
-  proof(rule Max_UNION)
-    show "\<forall>M\<in>(\<lambda>x. the_preced s ` 
-                    {th'. Th th' \<in> subtree (RAG s) (Th x)}) ` readys s. finite M"
-                        using finite_subtree_threads by auto
-  qed (auto simp:False subtree_def)
-  also have "... =  
-    Max ((Max \<circ> (\<lambda>th. the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)})) ` readys s)" 
-      by (unfold image_comp, simp)
-  also have "... = ?L" (is "Max (?f ` ?A) = Max (?g ` ?A)")
-  proof -
-    have "(?f ` ?A) = (?g ` ?A)"
-    proof(rule f_image_eq)
-      fix th1 
-      assume "th1 \<in> ?A"
-      thus "?f th1 = ?g th1"
-        by (unfold cp_alt_def, simp)
-    qed
-    thus ?thesis by simp
-  qed
-  finally show ?thesis by simp
-qed (auto simp:threads_alt_def)
-
-end
-
-end
-
+end
\ No newline at end of file
--- a/ExtGG.thy	Wed Feb 03 21:51:57 2016 +0800
+++ b/ExtGG.thy	Wed Feb 03 22:17:29 2016 +0800
@@ -2,8 +2,8 @@
   This file contains lemmas used to guide the recalculation of current precedence 
   after every system call (or system operation)
 *}
-theory ExtGG
-imports CpsG
+theory Implementation
+imports PIPBasics
 begin
 
 text {* (* ddd *)
@@ -376,9 +376,6 @@
 context valid_trace_p_w
 begin
 
-interpretation vat_e: valid_trace "e#s"
-  by (unfold_locales, insert vt_e, simp)
-
 lemma cs_held: "(Cs cs, Th holder) \<in> RAG s"
   using holding_s_holder
   by (unfold s_RAG_def, fold holding_eq, auto)
@@ -428,13 +425,13 @@
   and "y \<in> ancestors (tRAG (e#s)) u"
   shows "cp_gen (e#s) y = cp_gen s y"
   using assms(3)
-proof(induct rule:wf_induct[OF vat_e.fsbttRAGs.wf])
+proof(induct rule:wf_induct[OF vat_es.fsbttRAGs.wf])
   case (1 x)
   show ?case (is "?L = ?R")
   proof -
     from tRAG_ancestorsE[OF 1(2)]
     obtain th2 where eq_x: "x = Th th2" by blast
-    from vat_e.cp_gen_rec[OF this]
+    from vat_es.cp_gen_rec[OF this]
     have "?L = 
           Max ({the_preced (e#s) th2} \<union> cp_gen (e#s) ` RTree.children (tRAG (e#s)) x)" .
     also have "... = 
@@ -454,15 +451,15 @@
             assume "x \<in> Range {(Th th, Th holder)}"
             hence eq_x: "x = Th holder" using RangeE by auto
             show False
-            proof(cases rule:vat_e.ancestors_headE[OF assms(1) start])
+            proof(cases rule:vat_es.ancestors_headE[OF assms(1) start])
               case 1
-              from x_u[folded this, unfolded eq_x] vat_e.acyclic_tRAG
+              from x_u[folded this, unfolded eq_x] vat_es.acyclic_tRAG
               show ?thesis by (auto simp:ancestors_def acyclic_def)
             next
               case 2
               with x_u[unfolded eq_x]
               have "(Th holder, Th holder) \<in> (tRAG (e#s))^+" by (auto simp:ancestors_def)
-              with vat_e.acyclic_tRAG show ?thesis by (auto simp:acyclic_def)
+              with vat_es.acyclic_tRAG show ?thesis by (auto simp:acyclic_def)
             qed
           qed
         qed
@@ -473,7 +470,7 @@
           assume a_in: "a \<in> ?A"
           from 1(2)
           show "?f a = ?g a"
-          proof(cases rule:vat_e.rtree_s.ancestors_childrenE[case_names in_ch out_ch])
+          proof(cases rule:vat_es.rtree_s.ancestors_childrenE[case_names in_ch out_ch])
              case in_ch
              show ?thesis
              proof(cases "a = u")
@@ -485,7 +482,7 @@
                 proof
                   assume a_in': "a \<in> ancestors (tRAG (e#s)) (Th th)"
                   have "a = u"
-                  proof(rule vat_e.rtree_s.ancestors_children_unique)
+                  proof(rule vat_es.rtree_s.ancestors_children_unique)
                     from a_in' a_in show "a \<in> ancestors (tRAG (e#s)) (Th th) \<inter> 
                                           RTree.children (tRAG (e#s)) x" by auto
                   next 
@@ -519,7 +516,7 @@
               proof
                 assume a_in': "a \<in> ancestors (tRAG (e#s)) (Th th)"
                 have "a = z"
-                proof(rule vat_e.rtree_s.ancestors_children_unique)
+                proof(rule vat_es.rtree_s.ancestors_children_unique)
                   from assms(1) h(1) have "z \<in> ancestors (tRAG (e#s)) (Th th)"
                       by (auto simp:ancestors_def)
                   with h(2) show " z \<in> ancestors (tRAG (e#s)) (Th th) \<inter> 
@@ -570,9 +567,6 @@
 context valid_trace_create
 begin 
 
-interpretation vat_e: valid_trace "e#s"
-  by (unfold_locales, insert vt_e, simp)
-
 lemma tRAG_kept: "tRAG (e#s) = tRAG s"
   by (unfold tRAG_alt_def RAG_unchanged, auto)
 
@@ -632,7 +626,7 @@
 qed
 
 lemma eq_cp_th: "cp (e#s) th = preced th (e#s)"
- by (unfold vat_e.cp_rec children_of_th, simp add:the_preced_def)
+ by (unfold vat_es.cp_rec children_of_th, simp add:the_preced_def)
 
 end
 
@@ -706,924 +700,3 @@
 
 end
 
-=======
-theory ExtGG
-imports PrioG CpsG
-begin
-
-text {* 
-  The following two auxiliary lemmas are used to reason about @{term Max}.
-*}
-lemma image_Max_eqI: 
-  assumes "finite B"
-  and "b \<in> B"
-  and "\<forall> x \<in> B. f x \<le> f b"
-  shows "Max (f ` B) = f b"
-  using assms
-  using Max_eqI by blast 
-
-lemma image_Max_subset:
-  assumes "finite A"
-  and "B \<subseteq> A"
-  and "a \<in> B"
-  and "Max (f ` A) = f a"
-  shows "Max (f ` B) = f a"
-proof(rule image_Max_eqI)
-  show "finite B"
-    using assms(1) assms(2) finite_subset by auto 
-next
-  show "a \<in> B" using assms by simp
-next
-  show "\<forall>x\<in>B. f x \<le> f a"
-    by (metis Max_ge assms(1) assms(2) assms(4) 
-            finite_imageI image_eqI subsetCE) 
-qed
-
-text {*
-  The following locale @{text "highest_gen"} sets the basic context for our
-  investigation: supposing thread @{text th} holds the highest @{term cp}-value
-  in state @{text s}, which means the task for @{text th} is the 
-  most urgent. We want to show that  
-  @{text th} is treated correctly by PIP, which means
-  @{text th} will not be blocked unreasonably by other less urgent
-  threads. 
-*}
-locale highest_gen =
-  fixes s th prio tm
-  assumes vt_s: "vt s"
-  and threads_s: "th \<in> threads s"
-  and highest: "preced th s = Max ((cp s)`threads s)"
-  -- {* The internal structure of @{term th}'s precedence is exposed:*}
-  and preced_th: "preced th s = Prc prio tm" 
-
--- {* @{term s} is a valid trace, so it will inherit all results derived for
-      a valid trace: *}
-sublocale highest_gen < vat_s: valid_trace "s"
-  by (unfold_locales, insert vt_s, simp)
-
-context highest_gen
-begin
-
-text {*
-  @{term tm} is the time when the precedence of @{term th} is set, so 
-  @{term tm} must be a valid moment index into @{term s}.
-*}
-lemma lt_tm: "tm < length s"
-  by (insert preced_tm_lt[OF threads_s preced_th], simp)
-
-text {*
-  Since @{term th} holds the highest precedence and @{text "cp"}
-  is the highest precedence of all threads in the sub-tree of 
-  @{text "th"} and @{text th} is among these threads, 
-  its @{term cp} must equal to its precedence:
-*}
-lemma eq_cp_s_th: "cp s th = preced th s" (is "?L = ?R")
-proof -
-  have "?L \<le> ?R"
-  by (unfold highest, rule Max_ge, 
-        auto simp:threads_s finite_threads)
-  moreover have "?R \<le> ?L"
-    by (unfold vat_s.cp_rec, rule Max_ge, 
-        auto simp:the_preced_def vat_s.fsbttRAGs.finite_children)
-  ultimately show ?thesis by auto
-qed
-
-(* ccc *)
-lemma highest_cp_preced: "cp s th = Max ((\<lambda> th'. preced th' s) ` threads s)"
-  by (fold max_cp_eq, unfold eq_cp_s_th, insert highest, simp)
-
-lemma highest_preced_thread: "preced th s = Max ((\<lambda> th'. preced th' s) ` threads s)"
-  by (fold eq_cp_s_th, unfold highest_cp_preced, simp)
-
-lemma highest': "cp s th = Max (cp s ` threads s)"
-proof -
-  from highest_cp_preced max_cp_eq[symmetric]
-  show ?thesis by simp
-qed
-
-end
-
-locale extend_highest_gen = highest_gen + 
-  fixes t 
-  assumes vt_t: "vt (t@s)"
-  and create_low: "Create th' prio' \<in> set t \<Longrightarrow> prio' \<le> prio"
-  and set_diff_low: "Set th' prio' \<in> set t \<Longrightarrow> th' \<noteq> th \<and> prio' \<le> prio"
-  and exit_diff: "Exit th' \<in> set t \<Longrightarrow> th' \<noteq> th"
-
-sublocale extend_highest_gen < vat_t: valid_trace "t@s"
-  by (unfold_locales, insert vt_t, simp)
-
-lemma step_back_vt_app: 
-  assumes vt_ts: "vt (t@s)" 
-  shows "vt s"
-proof -
-  from vt_ts show ?thesis
-  proof(induct t)
-    case Nil
-    from Nil show ?case by auto
-  next
-    case (Cons e t)
-    assume ih: " vt (t @ s) \<Longrightarrow> vt s"
-      and vt_et: "vt ((e # t) @ s)"
-    show ?case
-    proof(rule ih)
-      show "vt (t @ s)"
-      proof(rule step_back_vt)
-        from vt_et show "vt (e # t @ s)" by simp
-      qed
-    qed
-  qed
-qed
-
-
-locale red_extend_highest_gen = extend_highest_gen +
-   fixes i::nat
-
-sublocale red_extend_highest_gen <   red_moment: extend_highest_gen "s" "th" "prio" "tm" "(moment i t)"
-  apply (insert extend_highest_gen_axioms, subst (asm) (1) moment_restm_s [of i t, symmetric])
-  apply (unfold extend_highest_gen_def extend_highest_gen_axioms_def, clarsimp)
-  by (unfold highest_gen_def, auto dest:step_back_vt_app)
-
-
-context extend_highest_gen
-begin
-
- lemma ind [consumes 0, case_names Nil Cons, induct type]:
-  assumes 
-    h0: "R []"
-  and h2: "\<And> e t. \<lbrakk>vt (t@s); step (t@s) e; 
-                    extend_highest_gen s th prio tm t; 
-                    extend_highest_gen s th prio tm (e#t); R t\<rbrakk> \<Longrightarrow> R (e#t)"
-  shows "R t"
-proof -
-  from vt_t extend_highest_gen_axioms show ?thesis
-  proof(induct t)
-    from h0 show "R []" .
-  next
-    case (Cons e t')
-    assume ih: "\<lbrakk>vt (t' @ s); extend_highest_gen s th prio tm t'\<rbrakk> \<Longrightarrow> R t'"
-      and vt_e: "vt ((e # t') @ s)"
-      and et: "extend_highest_gen s th prio tm (e # t')"
-    from vt_e and step_back_step have stp: "step (t'@s) e" by auto
-    from vt_e and step_back_vt have vt_ts: "vt (t'@s)" by auto
-    show ?case
-    proof(rule h2 [OF vt_ts stp _ _ _ ])
-      show "R t'"
-      proof(rule ih)
-        from et show ext': "extend_highest_gen s th prio tm t'"
-          by (unfold extend_highest_gen_def extend_highest_gen_axioms_def, auto dest:step_back_vt)
-      next
-        from vt_ts show "vt (t' @ s)" .
-      qed
-    next
-      from et show "extend_highest_gen s th prio tm (e # t')" .
-    next
-      from et show ext': "extend_highest_gen s th prio tm t'"
-          by (unfold extend_highest_gen_def extend_highest_gen_axioms_def, auto dest:step_back_vt)
-    qed
-  qed
-qed
-
-
-lemma th_kept: "th \<in> threads (t @ s) \<and> 
-                 preced th (t@s) = preced th s" (is "?Q t") 
-proof -
-  show ?thesis
-  proof(induct rule:ind)
-    case Nil
-    from threads_s
-    show ?case
-      by auto
-  next
-    case (Cons e t)
-    interpret h_e: extend_highest_gen _ _ _ _ "(e # t)" using Cons by auto
-    interpret h_t: extend_highest_gen _ _ _ _ t using Cons by auto
-    show ?case
-    proof(cases e)
-      case (Create thread prio)
-      show ?thesis
-      proof -
-        from Cons and Create have "step (t@s) (Create thread prio)" by auto
-        hence "th \<noteq> thread"
-        proof(cases)
-          case thread_create
-          with Cons show ?thesis by auto
-        qed
-        hence "preced th ((e # t) @ s)  = preced th (t @ s)"
-          by (unfold Create, auto simp:preced_def)
-        moreover note Cons
-        ultimately show ?thesis
-          by (auto simp:Create)
-      qed
-    next
-      case (Exit thread)
-      from h_e.exit_diff and Exit
-      have neq_th: "thread \<noteq> th" by auto
-      with Cons
-      show ?thesis
-        by (unfold Exit, auto simp:preced_def)
-    next
-      case (P thread cs)
-      with Cons
-      show ?thesis 
-        by (auto simp:P preced_def)
-    next
-      case (V thread cs)
-      with Cons
-      show ?thesis 
-        by (auto simp:V preced_def)
-    next
-      case (Set thread prio')
-      show ?thesis
-      proof -
-        from h_e.set_diff_low and Set
-        have "th \<noteq> thread" by auto
-        hence "preced th ((e # t) @ s)  = preced th (t @ s)"
-          by (unfold Set, auto simp:preced_def)
-        moreover note Cons
-        ultimately show ?thesis
-          by (auto simp:Set)
-      qed
-    qed
-  qed
-qed
-
-text {*
-  According to @{thm th_kept}, thread @{text "th"} has its living status
-  and precedence kept along the way of @{text "t"}. The following lemma
-  shows that this preserved precedence of @{text "th"} remains as the highest
-  along the way of @{text "t"}.
-
-  The proof goes by induction over @{text "t"} using the specialized
-  induction rule @{thm ind}, followed by case analysis of each possible 
-  operations of PIP. All cases follow the same pattern rendered by the 
-  generalized introduction rule @{thm "image_Max_eqI"}. 
-
-  The very essence is to show that precedences, no matter whether they are newly introduced 
-  or modified, are always lower than the one held by @{term "th"},
-  which by @{thm th_kept} is preserved along the way.
-*}
-lemma max_kept: "Max (the_preced (t @ s) ` (threads (t@s))) = preced th s"
-proof(induct rule:ind)
-  case Nil
-  from highest_preced_thread
-  show ?case
-    by (unfold the_preced_def, simp)
-next
-  case (Cons e t)
-    interpret h_e: extend_highest_gen _ _ _ _ "(e # t)" using Cons by auto
-    interpret h_t: extend_highest_gen _ _ _ _ t using Cons by auto
-  show ?case
-  proof(cases e)
-    case (Create thread prio')
-    show ?thesis (is "Max (?f ` ?A) = ?t")
-    proof -
-      -- {* The following is the common pattern of each branch of the case analysis. *}
-      -- {* The major part is to show that @{text "th"} holds the highest precedence: *}
-      have "Max (?f ` ?A) = ?f th"
-      proof(rule image_Max_eqI)
-        show "finite ?A" using h_e.finite_threads by auto 
-      next
-        show "th \<in> ?A" using h_e.th_kept by auto 
-      next
-        show "\<forall>x\<in>?A. ?f x \<le> ?f th"
-        proof 
-          fix x
-          assume "x \<in> ?A"
-          hence "x = thread \<or> x \<in> threads (t@s)" by (auto simp:Create)
-          thus "?f x \<le> ?f th"
-          proof
-            assume "x = thread"
-            thus ?thesis 
-              apply (simp add:Create the_preced_def preced_def, fold preced_def)
-              using Create h_e.create_low h_t.th_kept lt_tm preced_leI2 preced_th by force
-          next
-            assume h: "x \<in> threads (t @ s)"
-            from Cons(2)[unfolded Create] 
-            have "x \<noteq> thread" using h by (cases, auto)
-            hence "?f x = the_preced (t@s) x" 
-              by (simp add:Create the_preced_def preced_def)
-            hence "?f x \<le> Max (the_preced (t@s) ` threads (t@s))"
-              by (simp add: h_t.finite_threads h)
-            also have "... = ?f th"
-              by (metis Cons.hyps(5) h_e.th_kept the_preced_def) 
-            finally show ?thesis .
-          qed
-        qed
-      qed
-     -- {* The minor part is to show that the precedence of @{text "th"} 
-           equals to preserved one, given by the foregoing lemma @{thm th_kept} *}
-      also have "... = ?t" using h_e.th_kept the_preced_def by auto
-      -- {* Then it follows trivially that the precedence preserved
-            for @{term "th"} remains the maximum of all living threads along the way. *}
-      finally show ?thesis .
-    qed 
-  next 
-    case (Exit thread)
-    show ?thesis (is "Max (?f ` ?A) = ?t")
-    proof -
-      have "Max (?f ` ?A) = ?f th"
-      proof(rule image_Max_eqI)
-        show "finite ?A" using h_e.finite_threads by auto 
-      next
-        show "th \<in> ?A" using h_e.th_kept by auto 
-      next
-        show "\<forall>x\<in>?A. ?f x \<le> ?f th"
-        proof 
-          fix x
-          assume "x \<in> ?A"
-          hence "x \<in> threads (t@s)" by (simp add: Exit) 
-          hence "?f x \<le> Max (?f ` threads (t@s))" 
-            by (simp add: h_t.finite_threads) 
-          also have "... \<le> ?f th" 
-            apply (simp add:Exit the_preced_def preced_def, fold preced_def)
-            using Cons.hyps(5) h_t.th_kept the_preced_def by auto
-          finally show "?f x \<le> ?f th" .
-        qed
-      qed
-      also have "... = ?t" using h_e.th_kept the_preced_def by auto
-      finally show ?thesis .
-    qed 
-  next
-    case (P thread cs)
-    with Cons
-    show ?thesis by (auto simp:preced_def the_preced_def)
-  next
-    case (V thread cs)
-    with Cons
-    show ?thesis by (auto simp:preced_def the_preced_def)
-  next 
-    case (Set thread prio')
-    show ?thesis (is "Max (?f ` ?A) = ?t")
-    proof -
-      have "Max (?f ` ?A) = ?f th"
-      proof(rule image_Max_eqI)
-        show "finite ?A" using h_e.finite_threads by auto 
-      next
-        show "th \<in> ?A" using h_e.th_kept by auto 
-      next
-        show "\<forall>x\<in>?A. ?f x \<le> ?f th"
-        proof 
-          fix x
-          assume h: "x \<in> ?A"
-          show "?f x \<le> ?f th"
-          proof(cases "x = thread")
-            case True
-            moreover have "the_preced (Set thread prio' # t @ s) thread \<le> the_preced (t @ s) th"
-            proof -
-              have "the_preced (t @ s) th = Prc prio tm"  
-                using h_t.th_kept preced_th by (simp add:the_preced_def)
-              moreover have "prio' \<le> prio" using Set h_e.set_diff_low by auto
-              ultimately show ?thesis by (insert lt_tm, auto simp:the_preced_def preced_def)
-            qed
-            ultimately show ?thesis
-              by (unfold Set, simp add:the_preced_def preced_def)
-          next
-            case False
-            then have "?f x  = the_preced (t@s) x"
-              by (simp add:the_preced_def preced_def Set)
-            also have "... \<le> Max (the_preced (t@s) ` threads (t@s))"
-              using Set h h_t.finite_threads by auto 
-            also have "... = ?f th" by (metis Cons.hyps(5) h_e.th_kept the_preced_def) 
-            finally show ?thesis .
-          qed
-        qed
-      qed
-      also have "... = ?t" using h_e.th_kept the_preced_def by auto
-      finally show ?thesis .
-    qed 
-  qed
-qed
-
-lemma max_preced: "preced th (t@s) = Max (the_preced (t@s) ` (threads (t@s)))"
-  by (insert th_kept max_kept, auto)
-
-text {*
-  The reason behind the following lemma is that:
-  Since @{term "cp"} is defined as the maximum precedence 
-  of those threads contained in the sub-tree of node @{term "Th th"} 
-  in @{term "RAG (t@s)"}, and all these threads are living threads, and 
-  @{term "th"} is also among them, the maximum precedence of 
-  them all must be the one for @{text "th"}.
-*}
-lemma th_cp_max_preced: 
-  "cp (t@s) th = Max (the_preced (t@s) ` (threads (t@s)))" (is "?L = ?R") 
-proof -
-  let ?f = "the_preced (t@s)"
-  have "?L = ?f th"
-  proof(unfold cp_alt_def, rule image_Max_eqI)
-    show "finite {th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}"
-    proof -
-      have "{th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)} = 
-            the_thread ` {n . n \<in> subtree (RAG (t @ s)) (Th th) \<and>
-                            (\<exists> th'. n = Th th')}"
-      by (smt Collect_cong Setcompr_eq_image mem_Collect_eq the_thread.simps)
-      moreover have "finite ..." by (simp add: vat_t.fsbtRAGs.finite_subtree) 
-      ultimately show ?thesis by simp
-    qed
-  next
-    show "th \<in> {th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}"
-      by (auto simp:subtree_def)
-  next
-    show "\<forall>x\<in>{th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}.
-               the_preced (t @ s) x \<le> the_preced (t @ s) th"
-    proof
-      fix th'
-      assume "th' \<in> {th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}"
-      hence "Th th' \<in> subtree (RAG (t @ s)) (Th th)" by auto
-      moreover have "... \<subseteq> Field (RAG (t @ s)) \<union> {Th th}"
-        by (meson subtree_Field)
-      ultimately have "Th th' \<in> ..." by auto
-      hence "th' \<in> threads (t@s)" 
-      proof
-        assume "Th th' \<in> {Th th}"
-        thus ?thesis using th_kept by auto 
-      next
-        assume "Th th' \<in> Field (RAG (t @ s))"
-        thus ?thesis using vat_t.not_in_thread_isolated by blast 
-      qed
-      thus "the_preced (t @ s) th' \<le> the_preced (t @ s) th"
-        by (metis Max_ge finite_imageI finite_threads image_eqI 
-               max_kept th_kept the_preced_def)
-    qed
-  qed
-  also have "... = ?R" by (simp add: max_preced the_preced_def) 
-  finally show ?thesis .
-qed
-
-lemma th_cp_max: "cp (t@s) th = Max (cp (t@s) ` threads (t@s))"
-  using max_cp_eq th_cp_max_preced the_preced_def vt_t by presburger
-
-lemma th_cp_preced: "cp (t@s) th = preced th s"
-  by (fold max_kept, unfold th_cp_max_preced, simp)
-
-lemma preced_less:
-  assumes th'_in: "th' \<in> threads s"
-  and neq_th': "th' \<noteq> th"
-  shows "preced th' s < preced th s"
-  using assms
-by (metis Max.coboundedI finite_imageI highest not_le order.trans 
-    preced_linorder rev_image_eqI threads_s vat_s.finite_threads 
-    vat_s.le_cp)
-
-text {*
-  Counting of the number of @{term "P"} and @{term "V"} operations 
-  is the cornerstone of a large number of the following proofs. 
-  The reason is that this counting is quite easy to calculate and 
-  convenient to use in the reasoning. 
-
-  The following lemma shows that the counting controls whether 
-  a thread is running or not.
-*}
-
-lemma pv_blocked_pre:
-  assumes th'_in: "th' \<in> threads (t@s)"
-  and neq_th': "th' \<noteq> th"
-  and eq_pv: "cntP (t@s) th' = cntV (t@s) th'"
-  shows "th' \<notin> runing (t@s)"
-proof
-  assume otherwise: "th' \<in> runing (t@s)"
-  show False
-  proof -
-    have "th' = th"
-    proof(rule preced_unique)
-      show "preced th' (t @ s) = preced th (t @ s)" (is "?L = ?R")
-      proof -
-        have "?L = cp (t@s) th'"
-          by (unfold cp_eq_cpreced cpreced_def count_eq_dependants[OF eq_pv], simp)
-        also have "... = cp (t @ s) th" using otherwise 
-          by (metis (mono_tags, lifting) mem_Collect_eq 
-                    runing_def th_cp_max vat_t.max_cp_readys_threads)
-        also have "... = ?R" by (metis th_cp_preced th_kept) 
-        finally show ?thesis .
-      qed
-    qed (auto simp: th'_in th_kept)
-    moreover have "th' \<noteq> th" using neq_th' .
-    ultimately show ?thesis by simp
- qed
-qed
-
-lemmas pv_blocked = pv_blocked_pre[folded detached_eq]
-
-lemma runing_precond_pre:
-  fixes th'
-  assumes th'_in: "th' \<in> threads s"
-  and eq_pv: "cntP s th' = cntV s th'"
-  and neq_th': "th' \<noteq> th"
-  shows "th' \<in> threads (t@s) \<and>
-         cntP (t@s) th' = cntV (t@s) th'"
-proof(induct rule:ind)
-  case (Cons e t)
-    interpret vat_t: extend_highest_gen s th prio tm t using Cons by simp
-    interpret vat_e: extend_highest_gen s th prio tm "(e # t)" using Cons by simp
-    show ?case
-    proof(cases e)
-      case (P thread cs)
-      show ?thesis
-      proof -
-        have "cntP ((e # t) @ s) th' = cntV ((e # t) @ s) th'"
-        proof -
-          have "thread \<noteq> th'"
-          proof -
-            have "step (t@s) (P thread cs)" using Cons P by auto
-            thus ?thesis
-            proof(cases)
-              assume "thread \<in> runing (t@s)"
-              moreover have "th' \<notin> runing (t@s)" using Cons(5)
-                by (metis neq_th' vat_t.pv_blocked_pre) 
-              ultimately show ?thesis by auto
-            qed
-          qed with Cons show ?thesis
-            by (unfold P, simp add:cntP_def cntV_def count_def)
-        qed
-        moreover have "th' \<in> threads ((e # t) @ s)" using Cons by (unfold P, simp)
-        ultimately show ?thesis by auto
-      qed
-    next
-      case (V thread cs)
-      show ?thesis
-      proof -
-        have "cntP ((e # t) @ s) th' = cntV ((e # t) @ s) th'"
-        proof -
-          have "thread \<noteq> th'"
-          proof -
-            have "step (t@s) (V thread cs)" using Cons V by auto
-            thus ?thesis
-            proof(cases)
-              assume "thread \<in> runing (t@s)"
-              moreover have "th' \<notin> runing (t@s)" using Cons(5)
-                by (metis neq_th' vat_t.pv_blocked_pre) 
-              ultimately show ?thesis by auto
-            qed
-          qed with Cons show ?thesis
-            by (unfold V, simp add:cntP_def cntV_def count_def)
-        qed
-        moreover have "th' \<in> threads ((e # t) @ s)" using Cons by (unfold V, simp)
-        ultimately show ?thesis by auto
-      qed
-    next
-      case (Create thread prio')
-      show ?thesis
-      proof -
-        have "cntP ((e # t) @ s) th' = cntV ((e # t) @ s) th'"
-        proof -
-          have "thread \<noteq> th'"
-          proof -
-            have "step (t@s) (Create thread prio')" using Cons Create by auto
-            thus ?thesis using Cons(5) by (cases, auto)
-          qed with Cons show ?thesis
-            by (unfold Create, simp add:cntP_def cntV_def count_def)
-        qed
-        moreover have "th' \<in> threads ((e # t) @ s)" using Cons by (unfold Create, simp)
-        ultimately show ?thesis by auto
-      qed
-    next
-      case (Exit thread)
-      show ?thesis
-      proof -
-        have neq_thread: "thread \<noteq> th'"
-        proof -
-          have "step (t@s) (Exit thread)" using Cons Exit by auto
-          thus ?thesis apply (cases) using Cons(5)
-                by (metis neq_th' vat_t.pv_blocked_pre) 
-        qed 
-        hence "cntP ((e # t) @ s) th' = cntV ((e # t) @ s) th'" using Cons
-            by (unfold Exit, simp add:cntP_def cntV_def count_def)
-        moreover have "th' \<in> threads ((e # t) @ s)" using Cons neq_thread 
-          by (unfold Exit, simp) 
-        ultimately show ?thesis by auto
-      qed
-    next
-      case (Set thread prio')
-      with Cons
-      show ?thesis 
-        by (auto simp:cntP_def cntV_def count_def)
-    qed
-next
-  case Nil
-  with assms
-  show ?case by auto
-qed
-
-text {* Changing counting balance to detachedness *}
-lemmas runing_precond_pre_dtc = runing_precond_pre
-         [folded vat_t.detached_eq vat_s.detached_eq]
-
-lemma runing_precond:
-  fixes th'
-  assumes th'_in: "th' \<in> threads s"
-  and neq_th': "th' \<noteq> th"
-  and is_runing: "th' \<in> runing (t@s)"
-  shows "cntP s th' > cntV s th'"
-  using assms
-proof -
-  have "cntP s th' \<noteq> cntV s th'"
-    by (metis is_runing neq_th' pv_blocked_pre runing_precond_pre th'_in)
-  moreover have "cntV s th' \<le> cntP s th'" using vat_s.cnp_cnv_cncs by auto
-  ultimately show ?thesis by auto
-qed
-
-lemma moment_blocked_pre:
-  assumes neq_th': "th' \<noteq> th"
-  and th'_in: "th' \<in> threads ((moment i t)@s)"
-  and eq_pv: "cntP ((moment i t)@s) th' = cntV ((moment i t)@s) th'"
-  shows "cntP ((moment (i+j) t)@s) th' = cntV ((moment (i+j) t)@s) th' \<and>
-         th' \<in> threads ((moment (i+j) t)@s)"
-proof -
-  interpret h_i: red_extend_highest_gen _ _ _ _ _ i
-      by (unfold_locales)
-  interpret h_j: red_extend_highest_gen _ _ _ _ _ "i+j"
-      by (unfold_locales)
-  interpret h:  extend_highest_gen "((moment i t)@s)" th prio tm "moment j (restm i t)"
-  proof(unfold_locales)
-    show "vt (moment i t @ s)" by (metis h_i.vt_t) 
-  next
-    show "th \<in> threads (moment i t @ s)" by (metis h_i.th_kept)
-  next
-    show "preced th (moment i t @ s) = 
-            Max (cp (moment i t @ s) ` threads (moment i t @ s))"
-              by (metis h_i.th_cp_max h_i.th_cp_preced h_i.th_kept)
-  next
-    show "preced th (moment i t @ s) = Prc prio tm" by (metis h_i.th_kept preced_th) 
-  next
-    show "vt (moment j (restm i t) @ moment i t @ s)"
-      using moment_plus_split by (metis add.commute append_assoc h_j.vt_t)
-  next
-    fix th' prio'
-    assume "Create th' prio' \<in> set (moment j (restm i t))"
-    thus "prio' \<le> prio" using assms
-       by (metis Un_iff add.commute h_j.create_low moment_plus_split set_append)
-  next
-    fix th' prio'
-    assume "Set th' prio' \<in> set (moment j (restm i t))"
-    thus "th' \<noteq> th \<and> prio' \<le> prio"
-    by (metis Un_iff add.commute h_j.set_diff_low moment_plus_split set_append)
-  next
-    fix th'
-    assume "Exit th' \<in> set (moment j (restm i t))"
-    thus "th' \<noteq> th"
-      by (metis Un_iff add.commute h_j.exit_diff moment_plus_split set_append)
-  qed
-  show ?thesis 
-    by (metis add.commute append_assoc eq_pv h.runing_precond_pre
-          moment_plus_split neq_th' th'_in)
-qed
-
-lemma moment_blocked_eqpv:
-  assumes neq_th': "th' \<noteq> th"
-  and th'_in: "th' \<in> threads ((moment i t)@s)"
-  and eq_pv: "cntP ((moment i t)@s) th' = cntV ((moment i t)@s) th'"
-  and le_ij: "i \<le> j"
-  shows "cntP ((moment j t)@s) th' = cntV ((moment j t)@s) th' \<and>
-         th' \<in> threads ((moment j t)@s) \<and>
-         th' \<notin> runing ((moment j t)@s)"
-proof -
-  from moment_blocked_pre [OF neq_th' th'_in eq_pv, of "j-i"] and le_ij
-  have h1: "cntP ((moment j t)@s) th' = cntV ((moment j t)@s) th'"
-   and h2: "th' \<in> threads ((moment j t)@s)" by auto
-  moreover have "th' \<notin> runing ((moment j t)@s)"
-  proof -
-    interpret h: red_extend_highest_gen _ _ _ _ _ j by (unfold_locales)
-    show ?thesis
-      using h.pv_blocked_pre h1 h2 neq_th' by auto 
-  qed
-  ultimately show ?thesis by auto
-qed
-
-(* The foregoing two lemmas are preparation for this one, but
-   in long run can be combined. Maybe I am wrong.
-*)
-lemma moment_blocked:
-  assumes neq_th': "th' \<noteq> th"
-  and th'_in: "th' \<in> threads ((moment i t)@s)"
-  and dtc: "detached (moment i t @ s) th'"
-  and le_ij: "i \<le> j"
-  shows "detached (moment j t @ s) th' \<and>
-         th' \<in> threads ((moment j t)@s) \<and>
-         th' \<notin> runing ((moment j t)@s)"
-proof -
-  interpret h_i: red_extend_highest_gen _ _ _ _ _ i by (unfold_locales)
-  interpret h_j: red_extend_highest_gen _ _ _ _ _ j by (unfold_locales) 
-  have cnt_i: "cntP (moment i t @ s) th' = cntV (moment i t @ s) th'"
-                by (metis dtc h_i.detached_elim)
-  from moment_blocked_eqpv[OF neq_th' th'_in cnt_i le_ij]
-  show ?thesis by (metis h_j.detached_intro) 
-qed
-
-lemma runing_preced_inversion:
-  assumes runing': "th' \<in> runing (t@s)"
-  shows "cp (t@s) th' = preced th s" (is "?L = ?R")
-proof -
-  have "?L = Max (cp (t @ s) ` readys (t @ s))" using assms
-      by (unfold runing_def, auto)
-  also have "\<dots> = ?R"
-      by (metis th_cp_max th_cp_preced vat_t.max_cp_readys_threads) 
-  finally show ?thesis .
-qed
-
-text {*
-  The situation when @{term "th"} is blocked is analyzed by the following lemmas.
-*}
-
-text {*
-  The following lemmas shows the running thread @{text "th'"}, if it is different from
-  @{term th}, must be live at the very beginning. By the term {\em the very beginning},
-  we mean the moment where the formal investigation starts, i.e. the moment (or state)
-  @{term s}. 
-*}
-
-lemma runing_inversion_0:
-  assumes neq_th': "th' \<noteq> th"
-  and runing': "th' \<in> runing (t@s)"
-  shows "th' \<in> threads s"
-proof -
-    -- {* The proof is by contradiction: *}
-    { assume otherwise: "\<not> ?thesis"
-      have "th' \<notin> runing (t @ s)"
-      proof -
-        -- {* Since @{term "th'"} is running at time @{term "t@s"}, so it exists that time. *}
-        have th'_in: "th' \<in> threads (t@s)" using runing' by (simp add:runing_def readys_def)
-        -- {* However, @{text "th'"} does not exist at very beginning. *}
-        have th'_notin: "th' \<notin> threads (moment 0 t @ s)" using otherwise
-          by (metis append.simps(1) moment_zero)
-        -- {* Therefore, there must be a moment during @{text "t"}, when 
-              @{text "th'"} came into being. *}
-        -- {* Let us suppose the moment being @{text "i"}: *}
-        from p_split_gen[OF th'_in th'_notin]
-        obtain i where lt_its: "i < length t"
-                 and le_i: "0 \<le> i"
-                 and pre: " th' \<notin> threads (moment i t @ s)" (is "th' \<notin> threads ?pre")
-                 and post: "(\<forall>i'>i. th' \<in> threads (moment i' t @ s))" by (auto)
-        interpret h_i: red_extend_highest_gen _ _ _ _ _ i by (unfold_locales)
-        interpret h_i': red_extend_highest_gen _ _ _ _ _ "(Suc i)" by (unfold_locales)
-        from lt_its have "Suc i \<le> length t" by auto
-        -- {* Let us also suppose the event which makes this change is @{text e}: *}
-        from moment_head[OF this] obtain e where 
-          eq_me: "moment (Suc i) t = e # moment i t" by blast
-        hence "vt (e # (moment i t @ s))" by (metis append_Cons h_i'.vt_t) 
-        hence "PIP (moment i t @ s) e" by (cases, simp)
-        -- {* It can be derived that this event @{text "e"}, which 
-              gives birth to @{term "th'"} must be a @{term "Create"}: *}
-        from create_pre[OF this, of th']
-        obtain prio where eq_e: "e = Create th' prio"
-            by (metis append_Cons eq_me lessI post pre) 
-        have h1: "th' \<in> threads (moment (Suc i) t @ s)" using post by auto 
-        have h2: "cntP (moment (Suc i) t @ s) th' = cntV (moment (Suc i) t@ s) th'"
-        proof -
-          have "cntP (moment i t@s) th' = cntV (moment i t@s) th'"
-            by (metis h_i.cnp_cnv_eq pre)
-          thus ?thesis by (simp add:eq_me eq_e cntP_def cntV_def count_def)
-        qed
-        show ?thesis 
-          using moment_blocked_eqpv [OF neq_th' h1 h2, of "length t"] lt_its moment_ge
-            by auto
-      qed
-      with `th' \<in> runing (t@s)`
-      have False by simp
-    } thus ?thesis by auto
-qed
-
-text {* 
-  The second lemma says, if the running thread @{text th'} is different from 
-  @{term th}, then this @{text th'} must in the possession of some resources
-  at the very beginning. 
-
-  To ease the reasoning of resource possession of one particular thread, 
-  we used two auxiliary functions @{term cntV} and @{term cntP}, 
-  which are the counters of @{term P}-operations and 
-  @{term V}-operations respectively. 
-  If the number of @{term V}-operation is less than the number of 
-  @{term "P"}-operations, the thread must have some unreleased resource. 
-*}
-
-lemma runing_inversion_1: (* ddd *)
-  assumes neq_th': "th' \<noteq> th"
-  and runing': "th' \<in> runing (t@s)"
-  -- {* thread @{term "th'"} is a live on in state @{term "s"} and 
-        it has some unreleased resource. *}
-  shows "th' \<in> threads s \<and> cntV s th' < cntP s th'"
-proof -
-  -- {* The proof is a simple composition of @{thm runing_inversion_0} and 
-        @{thm runing_precond}: *}
-  -- {* By applying @{thm runing_inversion_0} to assumptions,
-        it can be shown that @{term th'} is live in state @{term s}: *}
-  have "th' \<in> threads s"  using runing_inversion_0[OF assms(1,2)] .
-  -- {* Then the thesis is derived easily by applying @{thm runing_precond}: *}
-  with runing_precond [OF this neq_th' runing'] show ?thesis by simp
-qed
-
-text {* 
-  The following lemma is just a rephrasing of @{thm runing_inversion_1}:
-*}
-lemma runing_inversion_2:
-  assumes runing': "th' \<in> runing (t@s)"
-  shows "th' = th \<or> (th' \<noteq> th \<and> th' \<in> threads s \<and> cntV s th' < cntP s th')"
-proof -
-  from runing_inversion_1[OF _ runing']
-  show ?thesis by auto
-qed
-
-lemma runing_inversion_3:
-  assumes runing': "th' \<in> runing (t@s)"
-  and neq_th: "th' \<noteq> th"
-  shows "th' \<in> threads s \<and> (cntV s th' < cntP s th' \<and> cp (t@s) th' = preced th s)"
-  by (metis neq_th runing' runing_inversion_2 runing_preced_inversion)
-
-lemma runing_inversion_4:
-  assumes runing': "th' \<in> runing (t@s)"
-  and neq_th: "th' \<noteq> th"
-  shows "th' \<in> threads s"
-  and    "\<not>detached s th'"
-  and    "cp (t@s) th' = preced th s"
-  apply (metis neq_th runing' runing_inversion_2)
-  apply (metis neq_th pv_blocked runing' runing_inversion_2 runing_precond_pre_dtc)
-  by (metis neq_th runing' runing_inversion_3)
-
-
-text {* 
-  Suppose @{term th} is not running, it is first shown that
-  there is a path in RAG leading from node @{term th} to another thread @{text "th'"} 
-  in the @{term readys}-set (So @{text "th'"} is an ancestor of @{term th}}).
-
-  Now, since @{term readys}-set is non-empty, there must be
-  one in it which holds the highest @{term cp}-value, which, by definition, 
-  is the @{term runing}-thread. However, we are going to show more: this running thread
-  is exactly @{term "th'"}.
-     *}
-lemma th_blockedE: (* ddd *)
-  assumes "th \<notin> runing (t@s)"
-  obtains th' where "Th th' \<in> ancestors (RAG (t @ s)) (Th th)"
-                    "th' \<in> runing (t@s)"
-proof -
-  -- {* According to @{thm vat_t.th_chain_to_ready}, either 
-        @{term "th"} is in @{term "readys"} or there is path leading from it to 
-        one thread in @{term "readys"}. *}
-  have "th \<in> readys (t @ s) \<or> (\<exists>th'. th' \<in> readys (t @ s) \<and> (Th th, Th th') \<in> (RAG (t @ s))\<^sup>+)" 
-    using th_kept vat_t.th_chain_to_ready by auto
-  -- {* However, @{term th} can not be in @{term readys}, because otherwise, since 
-       @{term th} holds the highest @{term cp}-value, it must be @{term "runing"}. *}
-  moreover have "th \<notin> readys (t@s)" 
-    using assms runing_def th_cp_max vat_t.max_cp_readys_threads by auto 
-  -- {* So, there must be a path from @{term th} to another thread @{text "th'"} in 
-        term @{term readys}: *}
-  ultimately obtain th' where th'_in: "th' \<in> readys (t@s)"
-                          and dp: "(Th th, Th th') \<in> (RAG (t @ s))\<^sup>+" by auto
-  -- {* We are going to show that this @{term th'} is running. *}
-  have "th' \<in> runing (t@s)"
-  proof -
-    -- {* We only need to show that this @{term th'} holds the highest @{term cp}-value: *}
-    have "cp (t@s) th' = Max (cp (t@s) ` readys (t@s))" (is "?L = ?R")
-    proof -
-      have "?L =  Max ((the_preced (t @ s) \<circ> the_thread) ` subtree (tRAG (t @ s)) (Th th'))"
-        by (unfold cp_alt_def1, simp)
-      also have "... = (the_preced (t @ s) \<circ> the_thread) (Th th)"
-      proof(rule image_Max_subset)
-        show "finite (Th ` (threads (t@s)))" by (simp add: vat_t.finite_threads)
-      next
-        show "subtree (tRAG (t @ s)) (Th th') \<subseteq> Th ` threads (t @ s)"
-          by (metis Range.intros dp trancl_range vat_t.range_in vat_t.subtree_tRAG_thread) 
-      next
-        show "Th th \<in> subtree (tRAG (t @ s)) (Th th')" using dp
-                    by (unfold tRAG_subtree_eq, auto simp:subtree_def)
-      next
-        show "Max ((the_preced (t @ s) \<circ> the_thread) ` Th ` threads (t @ s)) =
-                      (the_preced (t @ s) \<circ> the_thread) (Th th)" (is "Max ?L = _")
-        proof -
-          have "?L = the_preced (t @ s) `  threads (t @ s)" 
-                     by (unfold image_comp, rule image_cong, auto)
-          thus ?thesis using max_preced the_preced_def by auto
-        qed
-      qed
-      also have "... = ?R"
-        using th_cp_max th_cp_preced th_kept 
-              the_preced_def vat_t.max_cp_readys_threads by auto
-      finally show ?thesis .
-    qed 
-    -- {* Now, since @{term th'} holds the highest @{term cp} 
-          and we have already show it is in @{term readys},
-          it is @{term runing} by definition. *}
-    with `th' \<in> readys (t@s)` show ?thesis by (simp add: runing_def) 
-  qed
-  -- {* It is easy to show @{term th'} is an ancestor of @{term th}: *}
-  moreover have "Th th' \<in> ancestors (RAG (t @ s)) (Th th)" 
-    using `(Th th, Th th') \<in> (RAG (t @ s))\<^sup>+` by (auto simp:ancestors_def)
-  ultimately show ?thesis using that by metis
-qed
-
-text {*
-  Now it is easy to see there is always a thread to run by case analysis
-  on whether thread @{term th} is running: if the answer is Yes, the 
-  the running thread is obviously @{term th} itself; otherwise, the running
-  thread is the @{text th'} given by lemma @{thm th_blockedE}.
-*}
-lemma live: "runing (t@s) \<noteq> {}"
-proof(cases "th \<in> runing (t@s)") 
-  case True thus ?thesis by auto
-next
-  case False
-  thus ?thesis using th_blockedE by auto
-qed
-
-end
-end
-
--- a/PrioG.thy	Wed Feb 03 21:51:57 2016 +0800
+++ b/PrioG.thy	Wed Feb 03 22:17:29 2016 +0800
@@ -1,5 +1,5 @@
-theory PrioG
-imports CpsG
+theory Correctness
+imports PIPBasics
 begin
 
 text {* 
@@ -580,6 +580,7 @@
   -- {* All results derived so far hold for both @{term s} and @{term "t@s"}: *}
   interpret vat_t: extend_highest_gen s th prio tm t using Cons by simp
   interpret vat_e: extend_highest_gen s th prio tm "(e # t)" using Cons by simp
+  interpret vat_es: valid_trace_e "t@s" e using Cons(1,2) by (unfold_locales, auto)
   show ?case
   proof -
     -- {* It can be proved that @{term cntP}-value of @{term th'} does not change
@@ -591,8 +592,8 @@
       -- {* Then the actor of @{term e} must be @{term th'} and @{term e}
             must be a @{term P}-event: *}
       hence "isP e" "actor e = th'" by (auto simp:cntP_diff_inv) 
-      with vat_t.actor_inv[OF Cons(2)]
-      -- {* According to @{thm actor_inv}, @{term th'} must be running at 
+      with vat_es.actor_inv
+      -- {* According to @{thm vat_es.actor_inv}, @{term th'} must be running at 
             the moment @{term "t@s"}: *}
       have "th' \<in> runing (t@s)" by (cases e, auto)
       -- {* However, an application of @{thm eq_pv_blocked} to induction hypothesis
@@ -609,7 +610,7 @@
     proof(rule ccontr) -- {* Proof by contradiction. *}
       assume otherwise: "cntV ((e # t) @ s) th' \<noteq> cntV (t @ s) th'"
       hence "isV e" "actor e = th'" by (auto simp:cntV_diff_inv) 
-      with vat_t.actor_inv[OF Cons(2)]
+      with vat_es.actor_inv
       have "th' \<in> runing (t@s)" by (cases e, auto)
       moreover have "th' \<notin> runing (t@s)"
           using vat_t.eq_pv_blocked[OF neq_th' Cons(5)] .