Added generic theory "RTree.thy"
authorxingyuan zhang <xingyuanzhang@126.com>
Thu, 03 Dec 2015 14:34:29 +0800
changeset 57 f1b39d77db00
parent 56 0fd478e14e87
child 58 ad57323fd4d6
Added generic theory "RTree.thy"
CpsG.thy~
PrioG.thy~
PrioGDef.thy~
RTree.thy
red_1.thy
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CpsG.thy~	Thu Dec 03 14:34:29 2015 +0800
@@ -0,0 +1,1811 @@
+section {*
+  This file contains lemmas used to guide the recalculation of current precedence 
+  after every system call (or system operation)
+*}
+theory CpsG
+imports PrioG Max RTree
+begin
+
+locale pip = 
+  fixes s
+  assumes vt: "vt s"
+
+context pip
+begin
+
+interpretation rtree_RAG: rtree "RAG s"
+proof
+  show "single_valued (RAG s)"
+    by (unfold single_valued_def, auto intro: unique_RAG[OF vt])
+
+  show "acyclic (RAG s)"
+     by (rule acyclic_RAG[OF vt])
+qed
+
+thm rtree_RAG.rpath_overlap_oneside
+
+end
+
+
+
+definition "the_preced s th = preced th s"
+
+lemma cp_alt_def:
+  "cp s th =  
+           Max ((the_preced s) ` {th'. Th th' \<in> (subtree (RAG s) (Th th))})"
+proof -
+  have "Max (the_preced s ` ({th} \<union> dependants (wq s) th)) =
+        Max (the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)})" 
+          (is "Max (_ ` ?L) = Max (_ ` ?R)")
+  proof -
+    have "?L = ?R" 
+    by (auto dest:rtranclD simp:cs_dependants_def cs_RAG_def s_RAG_def subtree_def)
+    thus ?thesis by simp
+  qed
+  thus ?thesis by (unfold cp_eq_cpreced cpreced_def, fold the_preced_def, simp)
+qed
+
+lemma eq_dependants: "dependants (wq s) = dependants s"
+  by (simp add: s_dependants_abv wq_def)
+  
+(* obvious lemma *)
+lemma not_thread_holdents:
+  fixes th s
+  assumes vt: "vt s"
+  and not_in: "th \<notin> threads s" 
+  shows "holdents s th = {}"
+proof -
+  from vt not_in show ?thesis
+  proof(induct arbitrary:th)
+    case (vt_cons s e th)
+    assume vt: "vt s"
+      and ih: "\<And>th. th \<notin> threads s \<Longrightarrow> holdents s th = {}"
+      and stp: "step s e"
+      and not_in: "th \<notin> threads (e # s)"
+    from stp show ?case
+    proof(cases)
+      case (thread_create thread prio)
+      assume eq_e: "e = Create thread prio"
+        and not_in': "thread \<notin> threads s"
+      have "holdents (e # s) th = holdents s th"
+        apply (unfold eq_e holdents_test)
+        by (simp add:RAG_create_unchanged)
+      moreover have "th \<notin> threads s" 
+      proof -
+        from not_in eq_e show ?thesis by simp
+      qed
+      moreover note ih ultimately show ?thesis by auto
+    next
+      case (thread_exit thread)
+      assume eq_e: "e = Exit thread"
+      and nh: "holdents s thread = {}"
+      show ?thesis
+      proof(cases "th = thread")
+        case True
+        with nh eq_e
+        show ?thesis 
+          by (auto simp:holdents_test RAG_exit_unchanged)
+      next
+        case False
+        with not_in and eq_e
+        have "th \<notin> threads s" by simp
+        from ih[OF this] False eq_e show ?thesis 
+          by (auto simp:holdents_test RAG_exit_unchanged)
+      qed
+    next
+      case (thread_P thread cs)
+      assume eq_e: "e = P thread cs"
+      and is_runing: "thread \<in> runing s"
+      from assms thread_exit ih stp not_in vt eq_e have vtp: "vt (P thread cs#s)" by auto
+      have neq_th: "th \<noteq> thread" 
+      proof -
+        from not_in eq_e have "th \<notin> threads s" by simp
+        moreover from is_runing have "thread \<in> threads s"
+          by (simp add:runing_def readys_def)
+        ultimately show ?thesis by auto
+      qed
+      hence "holdents (e # s) th  = holdents s th "
+        apply (unfold cntCS_def holdents_test eq_e)
+        by (unfold step_RAG_p[OF vtp], auto)
+      moreover have "holdents s th = {}"
+      proof(rule ih)
+        from not_in eq_e show "th \<notin> threads s" by simp
+      qed
+      ultimately show ?thesis by simp
+    next
+      case (thread_V thread cs)
+      assume eq_e: "e = V thread cs"
+        and is_runing: "thread \<in> runing s"
+        and hold: "holding s thread cs"
+      have neq_th: "th \<noteq> thread" 
+      proof -
+        from not_in eq_e have "th \<notin> threads s" by simp
+        moreover from is_runing have "thread \<in> threads s"
+          by (simp add:runing_def readys_def)
+        ultimately show ?thesis by auto
+      qed
+      from assms thread_V eq_e ih stp not_in vt have vtv: "vt (V thread cs#s)" by auto
+      from hold obtain rest 
+        where eq_wq: "wq s cs = thread # rest"
+        by (case_tac "wq s cs", auto simp: wq_def s_holding_def)
+      from not_in eq_e eq_wq
+      have "\<not> next_th s thread cs th"
+        apply (auto simp:next_th_def)
+      proof -
+        assume ne: "rest \<noteq> []"
+          and ni: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> threads s" (is "?t \<notin> threads s")
+        have "?t \<in> set rest"
+        proof(rule someI2)
+          from wq_distinct[OF step_back_vt[OF vtv], of cs] and eq_wq
+          show "distinct rest \<and> set rest = set rest" by auto
+        next
+          fix x assume "distinct x \<and> set x = set rest" with ne
+          show "hd x \<in> set rest" by (cases x, auto)
+        qed
+        with eq_wq have "?t \<in> set (wq s cs)" by simp
+        from wq_threads[OF step_back_vt[OF vtv], OF this] and ni
+        show False by auto
+      qed
+      moreover note neq_th eq_wq
+      ultimately have "holdents (e # s) th  = holdents s th"
+        by (unfold eq_e cntCS_def holdents_test step_RAG_v[OF vtv], auto)
+      moreover have "holdents s th = {}"
+      proof(rule ih)
+        from not_in eq_e show "th \<notin> threads s" by simp
+      qed
+      ultimately show ?thesis by simp
+    next
+      case (thread_set thread prio)
+      print_facts
+      assume eq_e: "e = Set thread prio"
+        and is_runing: "thread \<in> runing s"
+      from not_in and eq_e have "th \<notin> threads s" by auto
+      from ih [OF this] and eq_e
+      show ?thesis 
+        apply (unfold eq_e cntCS_def holdents_test)
+        by (simp add:RAG_set_unchanged)
+    qed
+    next
+      case vt_nil
+      show ?case
+      by (auto simp:count_def holdents_test s_RAG_def wq_def cs_holding_def)
+  qed
+qed
+
+(* obvious lemma *)
+lemma next_th_neq: 
+  assumes vt: "vt s"
+  and nt: "next_th s th cs th'"
+  shows "th' \<noteq> th"
+proof -
+  from nt show ?thesis
+    apply (auto simp:next_th_def)
+  proof -
+    fix rest
+    assume eq_wq: "wq s cs = hd (SOME q. distinct q \<and> set q = set rest) # rest"
+      and ne: "rest \<noteq> []"
+    have "hd (SOME q. distinct q \<and> set q = set rest) \<in> set rest" 
+    proof(rule someI2)
+      from wq_distinct[OF vt, of cs] eq_wq
+      show "distinct rest \<and> set rest = set rest" by auto
+    next
+      fix x
+      assume "distinct x \<and> set x = set rest"
+      hence eq_set: "set x = set rest" by auto
+      with ne have "x \<noteq> []" by auto
+      hence "hd x \<in> set x" by auto
+      with eq_set show "hd x \<in> set rest" by auto
+    qed
+    with wq_distinct[OF vt, of cs] eq_wq show False by auto
+  qed
+qed
+
+(* obvious lemma *)
+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)
+
+lemma wf_RAG:
+  assumes vt: "vt s"
+  shows "wf (RAG s)"
+proof(rule finite_acyclic_wf)
+  from finite_RAG[OF vt] show "finite (RAG s)" .
+next
+  from acyclic_RAG[OF vt] show "acyclic (RAG s)" .
+qed
+
+definition child :: "state \<Rightarrow> (node \<times> node) set"
+  where "child s \<equiv>
+            {(Th th', Th th) | th th'. \<exists>cs. (Th th', Cs cs) \<in> RAG s \<and> (Cs cs, Th th) \<in> RAG s}"
+
+definition children :: "state \<Rightarrow> thread \<Rightarrow> thread set"
+  where "children s th \<equiv> {th'. (Th th', Th th) \<in> child s}"
+
+lemma children_def2:
+  "children s th \<equiv> {th'. \<exists> cs. (Th th', Cs cs) \<in> RAG s \<and> (Cs cs, Th th) \<in> RAG s}"
+unfolding child_def children_def by simp
+
+lemma children_dependants: 
+  "children s th \<subseteq> dependants (wq s) th"
+  unfolding children_def2
+  unfolding cs_dependants_def
+  by (auto simp add: eq_RAG)
+
+lemma child_unique:
+  assumes vt: "vt s"
+  and ch1: "(Th th, Th th1) \<in> child s"
+  and ch2: "(Th th, Th th2) \<in> child s"
+  shows "th1 = th2"
+using ch1 ch2 
+proof(unfold child_def, clarsimp)
+  fix cs csa
+  assume h1: "(Th th, Cs cs) \<in> RAG s"
+    and h2: "(Cs cs, Th th1) \<in> RAG s"
+    and h3: "(Th th, Cs csa) \<in> RAG s"
+    and h4: "(Cs csa, Th th2) \<in> RAG s"
+  from unique_RAG[OF vt h1 h3] have "cs = csa" by simp
+  with h4 have "(Cs cs, Th th2) \<in> RAG s" by simp
+  from unique_RAG[OF vt h2 this]
+  show "th1 = th2" by simp
+qed 
+
+lemma RAG_children:
+  assumes h: "(Th th1, Th th2) \<in> (RAG s)^+"
+  shows "th1 \<in> children s th2 \<or> (\<exists> th3. th3 \<in> children s th2 \<and> (Th th1, Th th3) \<in> (RAG s)^+)"
+proof -
+  from h show ?thesis
+  proof(induct rule: tranclE)
+    fix c th2
+    assume h1: "(Th th1, c) \<in> (RAG s)\<^sup>+"
+    and h2: "(c, Th th2) \<in> RAG s"
+    from h2 obtain cs where eq_c: "c = Cs cs"
+      by (case_tac c, auto simp:s_RAG_def)
+    show "th1 \<in> children s th2 \<or> (\<exists>th3. th3 \<in> children s th2 \<and> (Th th1, Th th3) \<in> (RAG s)\<^sup>+)"
+    proof(rule tranclE[OF h1])
+      fix ca
+      assume h3: "(Th th1, ca) \<in> (RAG s)\<^sup>+"
+        and h4: "(ca, c) \<in> RAG s"
+      show "th1 \<in> children s th2 \<or> (\<exists>th3. th3 \<in> children s th2 \<and> (Th th1, Th th3) \<in> (RAG s)\<^sup>+)"
+      proof -
+        from eq_c and h4 obtain th3 where eq_ca: "ca = Th th3"
+          by (case_tac ca, auto simp:s_RAG_def)
+        from eq_ca h4 h2 eq_c
+        have "th3 \<in> children s th2" by (auto simp:children_def child_def)
+        moreover from h3 eq_ca have "(Th th1, Th th3) \<in> (RAG s)\<^sup>+" by simp
+        ultimately show ?thesis by auto
+      qed
+    next
+      assume "(Th th1, c) \<in> RAG s"
+      with h2 eq_c
+      have "th1 \<in> children s th2"
+        by (auto simp:children_def child_def)
+      thus ?thesis by auto
+    qed
+  next
+    assume "(Th th1, Th th2) \<in> RAG s"
+    thus ?thesis
+      by (auto simp:s_RAG_def)
+  qed
+qed
+
+lemma sub_child: "child s \<subseteq> (RAG s)^+"
+  by (unfold child_def, auto)
+
+lemma wf_child: 
+  assumes vt: "vt s"
+  shows "wf (child s)"
+apply(rule wf_subset)
+apply(rule wf_trancl[OF wf_RAG[OF vt]])
+apply(rule sub_child)
+done
+
+lemma RAG_child_pre:
+  assumes vt: "vt s"
+  shows
+  "(Th th, n) \<in> (RAG s)^+ \<longrightarrow> (\<forall> th'. n = (Th th') \<longrightarrow> (Th th, Th th') \<in> (child s)^+)" (is "?P n")
+proof -
+  from wf_trancl[OF wf_RAG[OF vt]]
+  have wf: "wf ((RAG s)^+)" .
+  show ?thesis
+  proof(rule wf_induct[OF wf, of ?P], clarsimp)
+    fix th'
+    assume ih[rule_format]: "\<forall>y. (y, Th th') \<in> (RAG s)\<^sup>+ \<longrightarrow>
+               (Th th, y) \<in> (RAG s)\<^sup>+ \<longrightarrow> (\<forall>th'. y = Th th' \<longrightarrow> (Th th, Th th') \<in> (child s)\<^sup>+)"
+    and h: "(Th th, Th th') \<in> (RAG s)\<^sup>+"
+    show "(Th th, Th th') \<in> (child s)\<^sup>+"
+    proof -
+      from RAG_children[OF h]
+      have "th \<in> children s th' \<or> (\<exists>th3. th3 \<in> children s th' \<and> (Th th, Th th3) \<in> (RAG s)\<^sup>+)" .
+      thus ?thesis
+      proof
+        assume "th \<in> children s th'"
+        thus "(Th th, Th th') \<in> (child s)\<^sup>+" by (auto simp:children_def)
+      next
+        assume "\<exists>th3. th3 \<in> children s th' \<and> (Th th, Th th3) \<in> (RAG s)\<^sup>+"
+        then obtain th3 where th3_in: "th3 \<in> children s th'" 
+          and th_dp: "(Th th, Th th3) \<in> (RAG s)\<^sup>+" by auto
+        from th3_in have "(Th th3, Th th') \<in> (RAG s)^+" by (auto simp:children_def child_def)
+        from ih[OF this th_dp, of th3] have "(Th th, Th th3) \<in> (child s)\<^sup>+" by simp
+        with th3_in show "(Th th, Th th') \<in> (child s)\<^sup>+" by (auto simp:children_def)
+      qed
+    qed
+  qed
+qed
+
+lemma RAG_child: "\<lbrakk>vt s; (Th th, Th th') \<in> (RAG s)^+\<rbrakk> \<Longrightarrow> (Th th, Th th') \<in> (child s)^+"
+  by (insert RAG_child_pre, auto)
+
+lemma child_RAG_p:
+  assumes "(n1, n2) \<in> (child s)^+"
+  shows "(n1, n2) \<in> (RAG s)^+"
+proof -
+  from assms show ?thesis
+  proof(induct)
+    case (base y)
+    with sub_child show ?case by auto
+  next
+    case (step y z)
+    assume "(y, z) \<in> child s"
+    with sub_child have "(y, z) \<in> (RAG s)^+" by auto
+    moreover have "(n1, y) \<in> (RAG s)^+" by fact
+    ultimately show ?case by auto
+  qed
+qed
+
+text {* (* ddd *)
+*}
+lemma child_RAG_eq: 
+  assumes vt: "vt s"
+  shows "(Th th1, Th th2) \<in> (child s)^+  \<longleftrightarrow> (Th th1, Th th2) \<in> (RAG s)^+"
+  by (auto intro: RAG_child[OF vt] child_RAG_p)
+
+text {* (* ddd *)
+*}
+lemma children_no_dep:
+  fixes s th th1 th2 th3
+  assumes vt: "vt s"
+  and ch1: "(Th th1, Th th) \<in> child s"
+  and ch2: "(Th th2, Th th) \<in> child s"
+  and ch3: "(Th th1, Th th2) \<in> (RAG s)^+"
+  shows "False"
+proof -
+  from RAG_child[OF vt ch3]
+  have "(Th th1, Th th2) \<in> (child s)\<^sup>+" .
+  thus ?thesis
+  proof(rule converse_tranclE)
+    assume "(Th th1, Th th2) \<in> child s"
+    from child_unique[OF vt ch1 this] have "th = th2" by simp
+    with ch2 have "(Th th2, Th th2) \<in> child s" by simp
+    with wf_child[OF vt] show ?thesis by auto
+  next
+    fix c
+    assume h1: "(Th th1, c) \<in> child s"
+      and h2: "(c, Th th2) \<in> (child s)\<^sup>+"
+    from h1 obtain th3 where eq_c: "c = Th th3" by (unfold child_def, auto)
+    with h1 have "(Th th1, Th th3) \<in> child s" by simp
+    from child_unique[OF vt ch1 this] have eq_th3: "th3 = th" by simp
+    with eq_c and h2 have "(Th th, Th th2) \<in> (child s)\<^sup>+" by simp
+    with ch2 have "(Th th, Th th) \<in> (child s)\<^sup>+" by auto
+    moreover have "wf ((child s)\<^sup>+)"
+    proof(rule wf_trancl)
+      from wf_child[OF vt] show "wf (child s)" .
+    qed
+    ultimately show False by auto
+  qed
+qed
+
+text {* (* ddd *)
+*}
+lemma unique_RAG_p:
+  assumes vt: "vt s"
+  and dp1: "(n, n1) \<in> (RAG s)^+"
+  and dp2: "(n, n2) \<in> (RAG s)^+"
+  and neq: "n1 \<noteq> n2"
+  shows "(n1, n2) \<in> (RAG s)^+ \<or> (n2, n1) \<in> (RAG s)^+"
+proof(rule unique_chain [OF _ dp1 dp2 neq])
+  from unique_RAG[OF vt]
+  show "\<And>a b c. \<lbrakk>(a, b) \<in> RAG s; (a, c) \<in> RAG s\<rbrakk> \<Longrightarrow> b = c" by auto
+qed
+
+text {* (* ddd *)
+*}
+lemma dependants_child_unique:
+  fixes s th th1 th2 th3
+  assumes vt: "vt s"
+  and ch1: "(Th th1, Th th) \<in> child s"
+  and ch2: "(Th th2, Th th) \<in> child s"
+  and dp1: "th3 \<in> dependants s th1"
+  and dp2: "th3 \<in> dependants s th2"
+shows "th1 = th2"
+proof -
+  { assume neq: "th1 \<noteq> th2"
+    from dp1 have dp1: "(Th th3, Th th1) \<in> (RAG s)^+" 
+      by (simp add:s_dependants_def eq_RAG)
+    from dp2 have dp2: "(Th th3, Th th2) \<in> (RAG s)^+" 
+      by (simp add:s_dependants_def eq_RAG)
+    from unique_RAG_p[OF vt dp1 dp2] and neq
+    have "(Th th1, Th th2) \<in> (RAG s)\<^sup>+ \<or> (Th th2, Th th1) \<in> (RAG s)\<^sup>+" by auto
+    hence False
+    proof
+      assume "(Th th1, Th th2) \<in> (RAG s)\<^sup>+ "
+      from children_no_dep[OF vt ch1 ch2 this] show ?thesis .
+    next
+      assume " (Th th2, Th th1) \<in> (RAG s)\<^sup>+"
+      from children_no_dep[OF vt ch2 ch1 this] show ?thesis .
+    qed
+  } thus ?thesis by auto
+qed
+
+lemma RAG_plus_elim:
+  assumes "vt s"
+  fixes x
+  assumes "(Th x, Th th) \<in> (RAG (wq s))\<^sup>+"
+  shows "\<exists>th'\<in>children s th. x = th' \<or> (Th x, Th th') \<in> (RAG (wq s))\<^sup>+"
+  using assms(2)[unfolded eq_RAG, folded child_RAG_eq[OF `vt s`]]
+  apply (unfold children_def)
+  by (metis assms(2) children_def RAG_children eq_RAG)
+
+text {* (* ddd *)
+*}
+lemma dependants_expand:
+  assumes "vt s"
+  shows "dependants (wq s) th = (children s th) \<union> (\<Union>((dependants (wq s)) ` children s th))"
+apply(simp add: image_def)
+unfolding cs_dependants_def
+apply(auto)
+apply (metis assms RAG_plus_elim mem_Collect_eq)
+apply (metis child_RAG_p children_def eq_RAG mem_Collect_eq r_into_trancl')
+by (metis assms child_RAG_eq children_def eq_RAG mem_Collect_eq trancl.trancl_into_trancl)
+
+lemma finite_children:
+  assumes "vt s"
+  shows "finite (children s th)"
+  using children_dependants dependants_threads[OF assms] finite_threads[OF assms]
+  by (metis rev_finite_subset)
+  
+lemma finite_dependants:
+  assumes "vt s"
+  shows "finite (dependants (wq s) th')"
+  using dependants_threads[OF assms] finite_threads[OF assms]
+  by (metis rev_finite_subset)
+
+abbreviation
+  "preceds s ths \<equiv> {preced th s| th. th \<in> ths}"
+
+abbreviation
+  "cpreceds s ths \<equiv> (cp s) ` ths"
+
+lemma Un_compr:
+  "{f th | th. R th \<or> Q th} = ({f th | th. R th} \<union> {f th' | th'. Q th'})"
+by auto
+
+lemma in_disj:
+  shows "x \<in> A \<or> (\<exists>y \<in> A. x \<in> Q y) \<longleftrightarrow> (\<exists>y \<in> A. x = y \<or> x \<in> Q y)"
+by metis
+
+lemma UN_exists:
+  shows "(\<Union>x \<in> A. {f y | y. Q y x}) = ({f y | y. (\<exists>x \<in> A. Q y x)})"
+by auto
+
+text {* (* ddd *)
+  This is the recursive equation used to compute the current precedence of 
+  a thread (the @{text "th"}) here. 
+*}
+lemma cp_rec:
+  fixes s th
+  assumes vt: "vt s"
+  shows "cp s th = Max ({preced th s} \<union> (cp s ` children s th))"
+proof(cases "children s th = {}")
+  case True
+  show ?thesis
+    unfolding cp_eq_cpreced cpreced_def 
+    by (subst dependants_expand[OF `vt s`]) (simp add: True)
+next
+  case False
+  show ?thesis (is "?LHS = ?RHS")
+  proof -
+    have eq_cp: "cp s = (\<lambda>th. Max (preceds s ({th} \<union> dependants (wq s) th)))"
+      by (simp add: fun_eq_iff cp_eq_cpreced cpreced_def Un_compr image_Collect[symmetric])
+  
+    have not_emptyness_facts[simp]: 
+      "dependants (wq s) th \<noteq> {}" "children s th \<noteq> {}"
+      using False dependants_expand[OF assms] by(auto simp only: Un_empty)
+
+    have finiteness_facts[simp]:
+      "\<And>th. finite (dependants (wq s) th)" "\<And>th. finite (children s th)"
+      by (simp_all add: finite_dependants[OF `vt s`] finite_children[OF `vt s`])
+
+    (* expanding definition *)
+    have "?LHS = Max ({preced th s} \<union> preceds s (dependants (wq s) th))"
+      unfolding eq_cp by (simp add: Un_compr)
+    
+    (* moving Max in *)
+    also have "\<dots> = max (Max {preced th s}) (Max (preceds s (dependants (wq s) th)))"
+      by (simp add: Max_Un)
+
+    (* expanding dependants *)
+    also have "\<dots> = max (Max {preced th s}) 
+      (Max (preceds s (children s th \<union> \<Union>(dependants (wq s) ` children s th))))"
+      by (subst dependants_expand[OF `vt s`]) (simp)
+
+    (* moving out big Union *)
+    also have "\<dots> = max (Max {preced th s})
+      (Max (preceds s (\<Union> ({children s th} \<union> (dependants (wq s) ` children s th)))))"  
+      by simp
+
+    (* moving in small union *)
+    also have "\<dots> = max (Max {preced th s})
+      (Max (preceds s (\<Union> ((\<lambda>th. {th} \<union> (dependants (wq s) th)) ` children s th))))"  
+      by (simp add: in_disj)
+
+    (* moving in preceds *)
+    also have "\<dots> = max (Max {preced th s})  
+      (Max (\<Union> ((\<lambda>th. preceds s ({th} \<union> (dependants (wq s) th))) ` children s th)))" 
+      by (simp add: UN_exists)
+
+    (* moving in Max *)
+    also have "\<dots> = max (Max {preced th s})  
+      (Max ((\<lambda>th. Max (preceds s ({th} \<union> (dependants (wq s) th)))) ` children s th))" 
+      by (subst Max_Union) (auto simp add: image_image)
+
+    (* folding cp + moving out Max *)
+    also have "\<dots> = ?RHS" 
+      unfolding eq_cp by (simp add: Max_insert)
+
+    finally show "?LHS = ?RHS" .
+  qed
+qed
+
+lemma next_waiting:
+  assumes vt: "vt s"
+  and nxt: "next_th s th cs th'"
+  shows "waiting s th' cs"
+proof -
+  from assms show ?thesis
+    apply (auto simp:next_th_def s_waiting_def[folded wq_def])
+  proof -
+    fix rest
+    assume ni: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> set rest"
+      and eq_wq: "wq s cs = th # rest"
+      and ne: "rest \<noteq> []"
+    have "set (SOME q. distinct q \<and> set q = set rest) = set rest" 
+    proof(rule someI2)
+      from wq_distinct[OF vt, of cs] eq_wq
+      show "distinct rest \<and> set rest = set rest" by auto
+    next
+      show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
+    qed
+    with ni
+    have "hd (SOME q. distinct q \<and> set q = set rest) \<notin>  set (SOME q. distinct q \<and> set q = set rest)"
+      by simp
+    moreover have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
+    proof(rule someI2)
+      from wq_distinct[OF vt, of cs] eq_wq
+      show "distinct rest \<and> set rest = set rest" by auto
+    next
+      from ne show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> x \<noteq> []" by auto
+    qed
+    ultimately show "hd (SOME q. distinct q \<and> set q = set rest) = th" by auto
+  next
+    fix rest
+    assume eq_wq: "wq s cs = hd (SOME q. distinct q \<and> set q = set rest) # rest"
+      and ne: "rest \<noteq> []"
+    have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
+    proof(rule someI2)
+      from wq_distinct[OF vt, of cs] eq_wq
+      show "distinct rest \<and> set rest = set rest" by auto
+    next
+      from ne show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> x \<noteq> []" by auto
+    qed
+    hence "hd (SOME q. distinct q \<and> set q = set rest) \<in> set (SOME q. distinct q \<and> set q = set rest)"
+      by auto
+    moreover have "set (SOME q. distinct q \<and> set q = set rest) = set rest" 
+    proof(rule someI2)
+      from wq_distinct[OF vt, of cs] eq_wq
+      show "distinct rest \<and> set rest = set rest" by auto
+    next
+      show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
+    qed
+    ultimately have "hd (SOME q. distinct q \<and> set q = set rest) \<in> set rest" by simp
+    with eq_wq and wq_distinct[OF vt, of cs]
+    show False by auto
+  qed
+qed
+
+-- {* A useless definition *}
+definition cps:: "state \<Rightarrow> (thread \<times> precedence) set"
+where "cps s = {(th, cp s th) | th . th \<in> threads s}"
+
+
+text {* (* ddd *)
+  One beauty of our modelling is that we follow the definitional extension tradition of HOL.
+  The benefit of such a concise and miniature model is that  large number of intuitively 
+  obvious facts are derived as lemmas, rather than asserted as axioms.
+*}
+
+text {*
+  However, the lemmas in the forthcoming several locales are no longer 
+  obvious. These lemmas show how the current precedences should be recalculated 
+  after every execution step (in our model, every step is represented by an event, 
+  which in turn, represents a system call, or operation). Each operation is 
+  treated in a separate locale.
+
+  The complication of current precedence recalculation comes 
+  because the changing of RAG needs to be taken into account, 
+  in addition to the changing of precedence. 
+  The reason RAG changing affects current precedence is that,
+  according to the definition, current precedence 
+  of a thread is the maximum of the precedences of its dependants, 
+  where the dependants are defined in terms of RAG.
+
+  Therefore, each operation, lemmas concerning the change of the precedences 
+  and RAG are derived first, so that the lemmas about
+  current precedence recalculation can be based on.
+*}
+
+text {* (* ddd *)
+  The following locale @{text "step_set_cps"} investigates the recalculation 
+  after the @{text "Set"} operation.
+*}
+locale step_set_cps =
+  fixes s' th prio s 
+  -- {* @{text "s'"} is the system state before the operation *}
+  -- {* @{text "s"} is the system state after the operation *}
+  defines s_def : "s \<equiv> (Set th prio#s')" 
+  -- {* @{text "s"} is assumed to be a legitimate state, from which
+         the legitimacy of @{text "s"} can be derived. *}
+  assumes vt_s: "vt s"
+
+context step_set_cps 
+begin
+
+interpretation h: pip "s"
+  by (unfold pip_def, insert vt_s, simp)
+
+find_theorems 
+
+(* *)
+
+text {* (* ddd *)
+  The following lemma confirms that @{text "Set"}-operating only changes the precedence 
+  of initiating thread.
+*}
+
+lemma eq_preced:
+  fixes th'
+  assumes "th' \<noteq> th"
+  shows "preced th' s = preced th' s'"
+proof -
+  from assms show ?thesis 
+    by (unfold s_def, auto simp:preced_def)
+qed
+
+text {* (* ddd *)
+  The following lemma assures that the resetting of priority does not change the RAG. 
+*}
+
+lemma eq_dep: "RAG s = RAG s'"
+  by (unfold s_def RAG_set_unchanged, auto)
+
+text {*
+  Th following lemma @{text "eq_cp_pre"} circumscribe a rough range of recalculation.
+  It says, thread other than the initiating thread @{text "th"} does not need recalculation
+  unless it lies upstream of @{text "th"} in the RAG. 
+
+  The reason behind this lemma is that: 
+  the change of precedence of one thread can only affect it's upstream threads, according to 
+  lemma @{text "eq_preced"}. Since the only thread which might change precedence is
+  @{text "th"}, so only @{text "th"} and its upstream threads need recalculation.
+  (* ccc *)
+*}
+
+lemma eq_cp_pre:
+  fixes th' 
+  assumes neq_th: "th' \<noteq> th"
+  and nd: "th \<notin> dependants s th'"
+  shows "cp s th' = cp s' th'"
+proof -
+  -- {* This is what we need to prove after expanding the definition of @{text "cp"} *}
+  have "Max ((\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')) =
+        Max ((\<lambda>th. preced th s') ` ({th'} \<union> dependants (wq s') th'))"
+   (is "Max (?f1 ` ({th'} \<union> ?A)) = Max (?f2 ` ({th'} \<union> ?B))") 
+  proof -
+      -- {* Since RAG is not changed by @{text "Set"}-operation, the dependants of 
+            any threads are not changed, this is one of key facts underpinning this 
+            lemma *}
+      have eq_ab: "?A = ?B" by (unfold cs_dependants_def, auto simp:eq_dep eq_RAG)
+      have "(?f1 ` ({th'} \<union> ?A)) =  (?f2 ` ({th'} \<union> ?B))"
+      proof(rule image_cong)
+        show "{th'} \<union> ?A = {th'} \<union> ?B" by (simp only:eq_ab)
+      next  
+        fix x
+        assume x_in: "x \<in> {th'} \<union> ?B"
+        show "?f1 x = ?f2 x"
+        proof(rule eq_preced) -- {* The other key fact underpinning this lemma is @{text "eq_preced"} *}
+          from x_in[folded eq_ab, unfolded eq_dependants]
+          have "x \<in> {th'} \<union> dependants s th'" .
+          thus "x \<noteq> th"
+          proof
+            assume "x \<in> {th'}" 
+            with `th' \<noteq> th` show ?thesis by simp
+          next
+            assume "x \<in> dependants s th'"
+            with `th \<notin> dependants s th'` show ?thesis by auto
+          qed 
+        qed 
+      qed
+      thus ?thesis by simp
+  qed 
+  thus ?thesis by (unfold cp_eq_cpreced cpreced_def)
+qed
+
+text {*
+  The following lemma shows that no thread lies upstream of the initiating thread @{text "th"}. 
+  The reason for this is that only no-blocked thread can initiate 
+  a system call. Since thread @{text "th"} is non-blocked, it is not waiting for any 
+  critical resource. Therefore, there is edge leading out of @{text "th"} in the RAG.
+  Consequently, there is no node (neither resource nor thread) upstream of @{text "th"}.
+*}
+lemma no_dependants:
+  shows "th \<notin> dependants s th'"
+proof
+  assume "th \<in> dependants s th'"
+  from `th \<in> dependants s th'` have "(Th th, Th th') \<in> (RAG s')\<^sup>+"
+    by (unfold s_dependants_def, unfold eq_RAG, unfold eq_dep, auto)
+  from tranclD[OF this]
+  obtain z where "(Th th, z) \<in> RAG s'" by auto
+  moreover have "th \<in> readys s'"
+  proof -
+    from step_back_step [OF vt_s[unfolded s_def]]
+    have "step s' (Set th prio)" .
+    hence "th \<in> runing s'" by (cases, simp)
+    thus ?thesis by (simp add:readys_def runing_def)
+  qed
+  ultimately show "False"
+    apply (case_tac z, auto simp:readys_def s_RAG_def s_waiting_def cs_waiting_def)
+    by (fold wq_def, blast)
+qed
+
+(* Result improved *)
+text {* 
+  A simple combination of @{text "eq_cp_pre"} and @{text "no_dependants"}
+  gives the main lemma of this locale, which shows that
+  only the initiating thread needs a recalculation of current precedence.
+*}
+lemma eq_cp:
+  fixes th' 
+  assumes "th' \<noteq> th"
+  shows "cp s th' = cp s' th'"
+  by (rule eq_cp_pre[OF assms no_dependants])
+
+end
+
+text {*
+  The following @{text "step_v_cps"} is the locale for @{text "V"}-operation.
+*}
+
+locale step_v_cps =
+  -- {* @{text "th"} is the initiating thread *}
+  -- {* @{text "cs"} is the critical resource release by the @{text "V"}-operation *}
+  fixes s' th cs s    -- {* @{text "s'"} is the state before operation*}
+  defines s_def : "s \<equiv> (V th cs#s')" -- {* @{text "s"} is the state after operation*}
+  -- {* @{text "s"} is assumed to be valid, which implies the validity of @{text "s'"} *}
+  assumes vt_s: "vt s"
+
+text {*
+  The following @{text "step_v_cps_nt"} is the sub-locale for @{text "V"}-operation, 
+  which represents the case when there is another thread @{text "th'"}
+  to take over the critical resource released by the initiating thread @{text "th"}.
+*}
+locale step_v_cps_nt = step_v_cps +
+  fixes th'
+  -- {* @{text "th'"} is assumed to take over @{text "cs"} *}
+  assumes nt: "next_th s' th cs th'" 
+
+context step_v_cps_nt
+begin
+
+text {*
+  Lemma @{text "RAG_s"} confirms the change of RAG:
+  two edges removed and one added, as shown by the following diagram.
+*}
+
+(*
+  RAG before the V-operation
+    th1 ----|
+            |
+    th' ----|
+            |----> cs -----|
+    th2 ----|              |
+            |              |
+    th3 ----|              |
+                           |------> th
+    th4 ----|              |
+            |              |
+    th5 ----|              |
+            |----> cs'-----|
+    th6 ----|
+            |
+    th7 ----|
+
+ RAG after the V-operation
+    th1 ----|
+            |
+            |----> cs ----> th'
+    th2 ----|              
+            |              
+    th3 ----|              
+                           
+    th4 ----|              
+            |              
+    th5 ----|              
+            |----> cs'----> th
+    th6 ----|
+            |
+    th7 ----|
+*)
+
+lemma RAG_s:
+  "RAG s = (RAG s' - {(Cs cs, Th th), (Th th', Cs cs)}) \<union>
+                                         {(Cs cs, Th th')}"
+proof -
+  from step_RAG_v[OF vt_s[unfolded s_def], folded s_def]
+    and nt show ?thesis  by (auto intro:next_th_unique)
+qed
+
+text {*
+  Lemma @{text "dependants_kept"} shows only @{text "th"} and @{text "th'"}
+  have their dependants changed.
+*}
+lemma dependants_kept:
+  fixes th''
+  assumes neq1: "th'' \<noteq> th"
+  and neq2: "th'' \<noteq> th'"
+  shows "dependants (wq s) th'' = dependants (wq s') th''"
+proof(auto) (* ccc *)
+  fix x
+  assume "x \<in> dependants (wq s) th''"
+  hence dp: "(Th x, Th th'') \<in> (RAG s)^+"
+    by (auto simp:cs_dependants_def eq_RAG)
+  { fix n
+    have "(n, Th th'') \<in> (RAG s)^+ \<Longrightarrow>  (n, Th th'') \<in> (RAG s')^+"
+    proof(induct rule:converse_trancl_induct)
+      fix y 
+      assume "(y, Th th'') \<in> RAG s"
+      with RAG_s neq1 neq2
+      have "(y, Th th'') \<in> RAG s'" by auto
+      thus "(y, Th th'') \<in> (RAG s')\<^sup>+" by auto
+    next
+      fix y z 
+      assume yz: "(y, z) \<in> RAG s"
+        and ztp: "(z, Th th'') \<in> (RAG s)\<^sup>+"
+        and ztp': "(z, Th th'') \<in> (RAG s')\<^sup>+"
+      have "y \<noteq> Cs cs \<and> y \<noteq> Th th'"
+      proof
+        show "y \<noteq> Cs cs"
+        proof
+          assume eq_y: "y = Cs cs"
+          with yz have dp_yz: "(Cs cs, z) \<in> RAG s" by simp
+          from RAG_s
+          have cst': "(Cs cs, Th th') \<in> RAG s" by simp
+          from unique_RAG[OF vt_s this dp_yz] 
+          have eq_z: "z = Th th'" by simp
+          with ztp have "(Th th', Th th'') \<in> (RAG s)^+" by simp
+          from converse_tranclE[OF this]
+          obtain cs' where dp'': "(Th th', Cs cs') \<in> RAG s"
+            by (auto simp:s_RAG_def)
+          with RAG_s have dp': "(Th th', Cs cs') \<in> RAG s'" by auto
+          from dp'' eq_y yz eq_z have "(Cs cs, Cs cs') \<in> (RAG s)^+" by auto
+          moreover have "cs' = cs"
+          proof -
+            from next_waiting[OF step_back_vt[OF vt_s[unfolded s_def]] nt]
+            have "(Th th', Cs cs) \<in> RAG s'"
+              by (auto simp:s_waiting_def wq_def s_RAG_def cs_waiting_def)
+            from unique_RAG[OF step_back_vt[OF vt_s[unfolded s_def]] this dp']
+            show ?thesis by simp
+          qed
+          ultimately have "(Cs cs, Cs cs) \<in> (RAG s)^+" by simp
+          moreover note wf_trancl[OF wf_RAG[OF vt_s]]
+          ultimately show False by auto
+        qed
+      next
+        show "y \<noteq> Th th'"
+        proof
+          assume eq_y: "y = Th th'"
+          with yz have dps: "(Th th', z) \<in> RAG s" by simp
+          with RAG_s have dps': "(Th th', z) \<in> RAG s'" by auto
+          have "z = Cs cs"
+          proof -
+            from next_waiting[OF step_back_vt[OF vt_s[unfolded s_def]] nt]
+            have "(Th th', Cs cs) \<in> RAG s'"
+              by (auto simp:s_waiting_def wq_def s_RAG_def cs_waiting_def)
+            from unique_RAG[OF step_back_vt[OF vt_s[unfolded s_def]] dps' this]
+            show ?thesis .
+          qed
+          with dps RAG_s show False by auto
+        qed
+      qed
+      with RAG_s yz have "(y, z) \<in> RAG s'" by auto
+      with ztp'
+      show "(y, Th th'') \<in> (RAG s')\<^sup>+" by auto
+    qed    
+  }
+  from this[OF dp]
+  show "x \<in> dependants (wq s') th''" 
+    by (auto simp:cs_dependants_def eq_RAG)
+next
+  fix x
+  assume "x \<in> dependants (wq s') th''"
+  hence dp: "(Th x, Th th'') \<in> (RAG s')^+"
+    by (auto simp:cs_dependants_def eq_RAG)
+  { fix n
+    have "(n, Th th'') \<in> (RAG s')^+ \<Longrightarrow>  (n, Th th'') \<in> (RAG s)^+"
+    proof(induct rule:converse_trancl_induct)
+      fix y 
+      assume "(y, Th th'') \<in> RAG s'"
+      with RAG_s neq1 neq2
+      have "(y, Th th'') \<in> RAG s" by auto
+      thus "(y, Th th'') \<in> (RAG s)\<^sup>+" by auto
+    next
+      fix y z 
+      assume yz: "(y, z) \<in> RAG s'"
+        and ztp: "(z, Th th'') \<in> (RAG s')\<^sup>+"
+        and ztp': "(z, Th th'') \<in> (RAG s)\<^sup>+"
+      have "y \<noteq> Cs cs \<and> y \<noteq> Th th'"
+      proof
+        show "y \<noteq> Cs cs"
+        proof
+          assume eq_y: "y = Cs cs"
+          with yz have dp_yz: "(Cs cs, z) \<in> RAG s'" by simp
+          from this have eq_z: "z = Th th"
+          proof -
+            from step_back_step[OF vt_s[unfolded s_def]]
+            have "(Cs cs, Th th) \<in> RAG s'"
+              by(cases, auto simp: wq_def s_RAG_def cs_holding_def s_holding_def)
+            from unique_RAG[OF step_back_vt[OF vt_s[unfolded s_def]] this dp_yz]
+            show ?thesis by simp
+          qed
+          from converse_tranclE[OF ztp]
+          obtain u where "(z, u) \<in> RAG s'" by auto
+          moreover 
+          from step_back_step[OF vt_s[unfolded s_def]]
+          have "th \<in> readys s'" by (cases, simp add:runing_def)
+          moreover note eq_z
+          ultimately show False 
+            by (auto simp:readys_def wq_def s_RAG_def s_waiting_def cs_waiting_def)
+        qed
+      next
+        show "y \<noteq> Th th'"
+        proof
+          assume eq_y: "y = Th th'"
+          with yz have dps: "(Th th', z) \<in> RAG s'" by simp
+          have "z = Cs cs"
+          proof -
+            from next_waiting[OF step_back_vt[OF vt_s[unfolded s_def]] nt]
+            have "(Th th', Cs cs) \<in> RAG s'"
+              by (auto simp:s_waiting_def wq_def s_RAG_def cs_waiting_def)
+            from unique_RAG[OF step_back_vt[OF vt_s[unfolded s_def]] dps this]
+            show ?thesis .
+          qed
+          with ztp have cs_i: "(Cs cs, Th th'') \<in>  (RAG s')\<^sup>+" by simp
+          from step_back_step[OF vt_s[unfolded s_def]]
+          have cs_th: "(Cs cs, Th th) \<in> RAG s'"
+            by(cases, auto simp: s_RAG_def wq_def cs_holding_def s_holding_def)
+          have "(Cs cs, Th th'') \<notin>  RAG s'"
+          proof
+            assume "(Cs cs, Th th'') \<in> RAG s'"
+            from unique_RAG[OF step_back_vt[OF vt_s[unfolded s_def]] this cs_th]
+            and neq1 show "False" by simp
+          qed
+          with converse_tranclE[OF cs_i]
+          obtain u where cu: "(Cs cs, u) \<in> RAG s'"  
+            and u_t: "(u, Th th'') \<in> (RAG s')\<^sup>+" by auto
+          have "u = Th th"
+          proof -
+            from unique_RAG[OF step_back_vt[OF vt_s[unfolded s_def]] cu cs_th]
+            show ?thesis .
+          qed
+          with u_t have "(Th th, Th th'') \<in> (RAG s')\<^sup>+" by simp
+          from converse_tranclE[OF this]
+          obtain v where "(Th th, v) \<in> (RAG s')" by auto
+          moreover from step_back_step[OF vt_s[unfolded s_def]]
+          have "th \<in> readys s'" by (cases, simp add:runing_def)
+          ultimately show False 
+            by (auto simp:readys_def wq_def s_RAG_def s_waiting_def cs_waiting_def)
+        qed
+      qed
+      with RAG_s yz have "(y, z) \<in> RAG s" by auto
+      with ztp'
+      show "(y, Th th'') \<in> (RAG s)\<^sup>+" by auto
+    qed    
+  }
+  from this[OF dp]
+  show "x \<in> dependants (wq s) th''"
+    by (auto simp:cs_dependants_def eq_RAG)
+qed
+
+lemma cp_kept:
+  fixes th''
+  assumes neq1: "th'' \<noteq> th"
+  and neq2: "th'' \<noteq> th'"
+  shows "cp s th'' = cp s' th''"
+proof -
+  from dependants_kept[OF neq1 neq2]
+  have "dependants (wq s) th'' = dependants (wq s') th''" .
+  moreover {
+    fix th1
+    assume "th1 \<in> dependants (wq s) th''"
+    have "preced th1 s = preced th1 s'" 
+      by (unfold s_def, auto simp:preced_def)
+  }
+  moreover have "preced th'' s = preced th'' s'" 
+    by (unfold s_def, auto simp:preced_def)
+  ultimately have "((\<lambda>th. preced th s) ` ({th''} \<union> dependants (wq s) th'')) = 
+    ((\<lambda>th. preced th s') ` ({th''} \<union> dependants (wq s') th''))"
+    by (auto simp:image_def)
+  thus ?thesis
+    by (unfold cp_eq_cpreced cpreced_def, simp)
+qed
+
+end
+
+locale step_v_cps_nnt = step_v_cps +
+  assumes nnt: "\<And> th'. (\<not> next_th s' th cs th')"
+
+context step_v_cps_nnt
+begin
+
+lemma nw_cs: "(Th th1, Cs cs) \<notin> RAG s'"
+proof
+  assume "(Th th1, Cs cs) \<in> RAG s'"
+  thus "False"
+    apply (auto simp:s_RAG_def cs_waiting_def)
+  proof -
+    assume h1: "th1 \<in> set (wq s' cs)"
+      and h2: "th1 \<noteq> hd (wq s' cs)"
+    from step_back_step[OF vt_s[unfolded s_def]]
+    show "False"
+    proof(cases)
+      assume "holding s' th cs" 
+      then obtain rest where
+        eq_wq: "wq s' cs = th#rest"
+        apply (unfold s_holding_def wq_def[symmetric])
+        by (case_tac "(wq s' cs)", auto)
+      with h1 h2 have ne: "rest \<noteq> []" by auto
+      with eq_wq
+      have "next_th s' th cs (hd (SOME q. distinct q \<and> set q = set rest))"
+        by(unfold next_th_def, rule_tac x = "rest" in exI, auto)
+      with nnt show ?thesis by auto
+    qed
+  qed
+qed
+
+lemma RAG_s: "RAG s = RAG s' - {(Cs cs, Th th)}"
+proof -
+  from nnt and  step_RAG_v[OF vt_s[unfolded s_def], folded s_def]
+  show ?thesis by auto
+qed
+
+lemma child_kept_left:
+  assumes 
+  "(n1, n2) \<in> (child s')^+"
+  shows "(n1, n2) \<in> (child s)^+"
+proof -
+  from assms show ?thesis 
+  proof(induct rule: converse_trancl_induct)
+    case (base y)
+    from base obtain th1 cs1 th2
+      where h1: "(Th th1, Cs cs1) \<in> RAG s'"
+      and h2: "(Cs cs1, Th th2) \<in> RAG s'"
+      and eq_y: "y = Th th1" and eq_n2: "n2 = Th th2"  by (auto simp:child_def)
+    have "cs1 \<noteq> cs"
+    proof
+      assume eq_cs: "cs1 = cs"
+      with h1 have "(Th th1, Cs cs1) \<in> RAG s'" by simp
+      with nw_cs eq_cs show False by auto
+    qed
+    with h1 h2 RAG_s have 
+      h1': "(Th th1, Cs cs1) \<in> RAG s" and
+      h2': "(Cs cs1, Th th2) \<in> RAG s" by auto
+    hence "(Th th1, Th th2) \<in> child s" by (auto simp:child_def)
+    with eq_y eq_n2 have "(y, n2) \<in> child s" by simp
+    thus ?case by auto
+  next
+    case (step y z)
+    have "(y, z) \<in> child s'" by fact
+    then obtain th1 cs1 th2
+      where h1: "(Th th1, Cs cs1) \<in> RAG s'"
+      and h2: "(Cs cs1, Th th2) \<in> RAG s'"
+      and eq_y: "y = Th th1" and eq_z: "z = Th th2"  by (auto simp:child_def)
+    have "cs1 \<noteq> cs"
+    proof
+      assume eq_cs: "cs1 = cs"
+      with h1 have "(Th th1, Cs cs1) \<in> RAG s'" by simp
+      with nw_cs eq_cs show False by auto
+    qed
+    with h1 h2 RAG_s have 
+      h1': "(Th th1, Cs cs1) \<in> RAG s" and
+      h2': "(Cs cs1, Th th2) \<in> RAG s" by auto
+    hence "(Th th1, Th th2) \<in> child s" by (auto simp:child_def)
+    with eq_y eq_z have "(y, z) \<in> child s" by simp
+    moreover have "(z, n2) \<in> (child s)^+" by fact
+    ultimately show ?case by auto
+  qed
+qed
+
+lemma  child_kept_right:
+  assumes
+  "(n1, n2) \<in> (child s)^+"
+  shows "(n1, n2) \<in> (child s')^+"
+proof -
+  from assms show ?thesis
+  proof(induct)
+    case (base y)
+    from base and RAG_s 
+    have "(n1, y) \<in> child s'"
+      by (auto simp:child_def)
+    thus ?case by auto
+  next
+    case (step y z)
+    have "(y, z) \<in> child s" by fact
+    with RAG_s have "(y, z) \<in> child s'"
+      by (auto simp:child_def)
+    moreover have "(n1, y) \<in> (child s')\<^sup>+" by fact
+    ultimately show ?case by auto
+  qed
+qed
+
+lemma eq_child: "(child s)^+ = (child s')^+"
+  by (insert child_kept_left child_kept_right, auto)
+
+lemma eq_cp:
+  fixes th' 
+  shows "cp s th' = cp s' th'"
+  apply (unfold cp_eq_cpreced cpreced_def)
+proof -
+  have eq_dp: "\<And> th. dependants (wq s) th = dependants (wq s') th"
+    apply (unfold cs_dependants_def, unfold eq_RAG)
+  proof -
+    from eq_child
+    have "\<And>th. {th'. (Th th', Th th) \<in> (child s)\<^sup>+} = {th'. (Th th', Th th) \<in> (child s')\<^sup>+}"
+      by simp
+    with child_RAG_eq[OF vt_s] child_RAG_eq[OF step_back_vt[OF vt_s[unfolded s_def]]]
+    show "\<And>th. {th'. (Th th', Th th) \<in> (RAG s)\<^sup>+} = {th'. (Th th', Th th) \<in> (RAG s')\<^sup>+}"
+      by simp
+  qed
+  moreover {
+    fix th1 
+    assume "th1 \<in> {th'} \<union> dependants (wq s') th'"
+    hence "th1 = th' \<or> th1 \<in> dependants (wq s') th'" by auto
+    hence "preced th1 s = preced th1 s'"
+    proof
+      assume "th1 = th'"
+      show "preced th1 s = preced th1 s'" by (simp add:s_def preced_def)
+    next
+      assume "th1 \<in> dependants (wq s') th'"
+      show "preced th1 s = preced th1 s'" by (simp add:s_def preced_def)
+    qed
+  } ultimately have "((\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')) = 
+                     ((\<lambda>th. preced th s') ` ({th'} \<union> dependants (wq s') th'))" 
+    by (auto simp:image_def)
+  thus "Max ((\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')) =
+        Max ((\<lambda>th. preced th s') ` ({th'} \<union> dependants (wq s') th'))" by simp
+qed
+
+end
+
+locale step_P_cps =
+  fixes s' th cs s 
+  defines s_def : "s \<equiv> (P th cs#s')"
+  assumes vt_s: "vt s"
+
+locale step_P_cps_ne =step_P_cps +
+  assumes ne: "wq s' cs \<noteq> []"
+
+locale step_P_cps_e =step_P_cps +
+  assumes ee: "wq s' cs = []"
+
+context step_P_cps_e
+begin
+
+lemma RAG_s: "RAG s = RAG s' \<union> {(Cs cs, Th th)}"
+proof -
+  from ee and  step_RAG_p[OF vt_s[unfolded s_def], folded s_def]
+  show ?thesis by auto
+qed
+
+lemma child_kept_left:
+  assumes 
+  "(n1, n2) \<in> (child s')^+"
+  shows "(n1, n2) \<in> (child s)^+"
+proof -
+  from assms show ?thesis 
+  proof(induct rule: converse_trancl_induct)
+    case (base y)
+    from base obtain th1 cs1 th2
+      where h1: "(Th th1, Cs cs1) \<in> RAG s'"
+      and h2: "(Cs cs1, Th th2) \<in> RAG s'"
+      and eq_y: "y = Th th1" and eq_n2: "n2 = Th th2"  by (auto simp:child_def)
+    have "cs1 \<noteq> cs"
+    proof
+      assume eq_cs: "cs1 = cs"
+      with h1 have "(Th th1, Cs cs) \<in> RAG s'" by simp
+      with ee show False
+        by (auto simp:s_RAG_def cs_waiting_def)
+    qed
+    with h1 h2 RAG_s have 
+      h1': "(Th th1, Cs cs1) \<in> RAG s" and
+      h2': "(Cs cs1, Th th2) \<in> RAG s" by auto
+    hence "(Th th1, Th th2) \<in> child s" by (auto simp:child_def)
+    with eq_y eq_n2 have "(y, n2) \<in> child s" by simp
+    thus ?case by auto
+  next
+    case (step y z)
+    have "(y, z) \<in> child s'" by fact
+    then obtain th1 cs1 th2
+      where h1: "(Th th1, Cs cs1) \<in> RAG s'"
+      and h2: "(Cs cs1, Th th2) \<in> RAG s'"
+      and eq_y: "y = Th th1" and eq_z: "z = Th th2"  by (auto simp:child_def)
+    have "cs1 \<noteq> cs"
+    proof
+      assume eq_cs: "cs1 = cs"
+      with h1 have "(Th th1, Cs cs) \<in> RAG s'" by simp
+      with ee show False 
+        by (auto simp:s_RAG_def cs_waiting_def)
+    qed
+    with h1 h2 RAG_s have 
+      h1': "(Th th1, Cs cs1) \<in> RAG s" and
+      h2': "(Cs cs1, Th th2) \<in> RAG s" by auto
+    hence "(Th th1, Th th2) \<in> child s" by (auto simp:child_def)
+    with eq_y eq_z have "(y, z) \<in> child s" by simp
+    moreover have "(z, n2) \<in> (child s)^+" by fact
+    ultimately show ?case by auto
+  qed
+qed
+
+lemma  child_kept_right:
+  assumes
+  "(n1, n2) \<in> (child s)^+"
+  shows "(n1, n2) \<in> (child s')^+"
+proof -
+  from assms show ?thesis
+  proof(induct)
+    case (base y)
+    from base and RAG_s
+    have "(n1, y) \<in> child s'"
+      apply (auto simp:child_def)
+      proof -
+        fix th'
+        assume "(Th th', Cs cs) \<in> RAG s'"
+        with ee have "False"
+          by (auto simp:s_RAG_def cs_waiting_def)
+        thus "\<exists>cs. (Th th', Cs cs) \<in> RAG s' \<and> (Cs cs, Th th) \<in> RAG s'" by auto 
+      qed
+    thus ?case by auto
+  next
+    case (step y z)
+    have "(y, z) \<in> child s" by fact
+    with RAG_s have "(y, z) \<in> child s'"
+      apply (auto simp:child_def)
+      proof -
+        fix th'
+        assume "(Th th', Cs cs) \<in> RAG s'"
+        with ee have "False"
+          by (auto simp:s_RAG_def cs_waiting_def)
+        thus "\<exists>cs. (Th th', Cs cs) \<in> RAG s' \<and> (Cs cs, Th th) \<in> RAG s'" by auto 
+      qed
+    moreover have "(n1, y) \<in> (child s')\<^sup>+" by fact
+    ultimately show ?case by auto
+  qed
+qed
+
+lemma eq_child: "(child s)^+ = (child s')^+"
+  by (insert child_kept_left child_kept_right, auto)
+
+lemma eq_cp:
+  fixes th' 
+  shows "cp s th' = cp s' th'"
+  apply (unfold cp_eq_cpreced cpreced_def)
+proof -
+  have eq_dp: "\<And> th. dependants (wq s) th = dependants (wq s') th"
+    apply (unfold cs_dependants_def, unfold eq_RAG)
+  proof -
+    from eq_child
+    have "\<And>th. {th'. (Th th', Th th) \<in> (child s)\<^sup>+} = {th'. (Th th', Th th) \<in> (child s')\<^sup>+}"
+      by auto
+    with child_RAG_eq[OF vt_s] child_RAG_eq[OF step_back_vt[OF vt_s[unfolded s_def]]]
+    show "\<And>th. {th'. (Th th', Th th) \<in> (RAG s)\<^sup>+} = {th'. (Th th', Th th) \<in> (RAG s')\<^sup>+}"
+      by simp
+  qed
+  moreover {
+    fix th1 
+    assume "th1 \<in> {th'} \<union> dependants (wq s') th'"
+    hence "th1 = th' \<or> th1 \<in> dependants (wq s') th'" by auto
+    hence "preced th1 s = preced th1 s'"
+    proof
+      assume "th1 = th'"
+      show "preced th1 s = preced th1 s'" by (simp add:s_def preced_def)
+    next
+      assume "th1 \<in> dependants (wq s') th'"
+      show "preced th1 s = preced th1 s'" by (simp add:s_def preced_def)
+    qed
+  } ultimately have "((\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')) = 
+                     ((\<lambda>th. preced th s') ` ({th'} \<union> dependants (wq s') th'))" 
+    by (auto simp:image_def)
+  thus "Max ((\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')) =
+        Max ((\<lambda>th. preced th s') ` ({th'} \<union> dependants (wq s') th'))" by simp
+qed
+
+end
+
+context step_P_cps_ne
+begin
+
+lemma RAG_s: "RAG s = RAG s' \<union> {(Th th, Cs cs)}"
+proof -
+  from step_RAG_p[OF vt_s[unfolded s_def]] and ne
+  show ?thesis by (simp add:s_def)
+qed
+
+
+lemma eq_child_left:
+  assumes nd: "(Th th, Th th') \<notin> (child s)^+"
+  shows "(n1, Th th') \<in> (child s)^+ \<Longrightarrow> (n1, Th th') \<in> (child s')^+"
+proof(induct rule:converse_trancl_induct)
+  case (base y)
+  from base obtain th1 cs1
+    where h1: "(Th th1, Cs cs1) \<in> RAG s"
+    and h2: "(Cs cs1, Th th') \<in> RAG s"
+    and eq_y: "y = Th th1"   by (auto simp:child_def)
+  have "th1 \<noteq> th"
+  proof
+    assume "th1 = th"
+    with base eq_y have "(Th th, Th th') \<in> child s" by simp
+    with nd show False by auto
+  qed
+  with h1 h2 RAG_s 
+  have h1': "(Th th1, Cs cs1) \<in> RAG s'" and 
+       h2': "(Cs cs1, Th th') \<in> RAG s'" by auto
+  with eq_y show ?case by (auto simp:child_def)
+next
+  case (step y z)
+  have yz: "(y, z) \<in> child s" by fact
+  then obtain th1 cs1 th2
+    where h1: "(Th th1, Cs cs1) \<in> RAG s"
+    and h2: "(Cs cs1, Th th2) \<in> RAG s"
+    and eq_y: "y = Th th1" and eq_z: "z = Th th2"  by (auto simp:child_def)
+  have "th1 \<noteq> th"
+  proof
+    assume "th1 = th"
+    with yz eq_y have "(Th th, z) \<in> child s" by simp
+    moreover have "(z, Th th') \<in> (child s)^+" by fact
+    ultimately have "(Th th, Th th') \<in> (child s)^+" by auto
+    with nd show False by auto
+  qed
+  with h1 h2 RAG_s have h1': "(Th th1, Cs cs1) \<in> RAG s'"
+                       and h2': "(Cs cs1, Th th2) \<in> RAG s'" by auto
+  with eq_y eq_z have "(y, z) \<in> child s'" by (auto simp:child_def)
+  moreover have "(z, Th th') \<in> (child s')^+" by fact
+  ultimately show ?case by auto
+qed
+
+lemma eq_child_right:
+  shows "(n1, Th th') \<in> (child s')^+ \<Longrightarrow> (n1, Th th') \<in> (child s)^+"
+proof(induct rule:converse_trancl_induct)
+  case (base y)
+  with RAG_s show ?case by (auto simp:child_def)
+next
+  case (step y z)
+  have "(y, z) \<in> child s'" by fact
+  with RAG_s have "(y, z) \<in> child s" by (auto simp:child_def)
+  moreover have "(z, Th th') \<in> (child s)^+" by fact
+  ultimately show ?case by auto
+qed
+
+lemma eq_child:
+  assumes nd: "(Th th, Th th') \<notin> (child s)^+"
+  shows "((n1, Th th') \<in> (child s)^+) = ((n1, Th th') \<in> (child s')^+)"
+  by (insert eq_child_left[OF nd] eq_child_right, auto)
+
+lemma eq_cp:
+  fixes th' 
+  assumes nd: "th \<notin> dependants s th'"
+  shows "cp s th' = cp s' th'"
+  apply (unfold cp_eq_cpreced cpreced_def)
+proof -
+  have nd': "(Th th, Th th') \<notin> (child s)^+"
+  proof
+    assume "(Th th, Th th') \<in> (child s)\<^sup>+"
+    with child_RAG_eq[OF vt_s]
+    have "(Th th, Th th') \<in> (RAG s)\<^sup>+" by simp
+    with nd show False 
+      by (simp add:s_dependants_def eq_RAG)
+  qed
+  have eq_dp: "dependants (wq s) th' = dependants (wq s') th'"
+  proof(auto)
+    fix x assume " x \<in> dependants (wq s) th'"
+    thus "x \<in> dependants (wq s') th'"
+      apply (auto simp:cs_dependants_def eq_RAG)
+    proof -
+      assume "(Th x, Th th') \<in> (RAG s)\<^sup>+"
+      with  child_RAG_eq[OF vt_s] have "(Th x, Th th') \<in> (child s)\<^sup>+" by simp
+      with eq_child[OF nd'] have "(Th x, Th th') \<in> (child s')^+" by simp
+      with child_RAG_eq[OF step_back_vt[OF vt_s[unfolded s_def]]]
+      show "(Th x, Th th') \<in> (RAG s')\<^sup>+" by simp
+    qed
+  next
+    fix x assume "x \<in> dependants (wq s') th'"
+    thus "x \<in> dependants (wq s) th'"
+      apply (auto simp:cs_dependants_def eq_RAG)
+    proof -
+      assume "(Th x, Th th') \<in> (RAG s')\<^sup>+"
+      with child_RAG_eq[OF step_back_vt[OF vt_s[unfolded s_def]]] 
+      have "(Th x, Th th') \<in> (child s')\<^sup>+" by simp
+      with eq_child[OF nd'] have "(Th x, Th th') \<in> (child s)^+" by simp
+      with  child_RAG_eq[OF vt_s]
+      show "(Th x, Th th') \<in> (RAG s)\<^sup>+" by simp
+    qed
+  qed
+  moreover {
+    fix th1 have "preced th1 s = preced th1 s'" by (simp add:s_def preced_def)
+  } ultimately have "((\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')) = 
+                     ((\<lambda>th. preced th s') ` ({th'} \<union> dependants (wq s') th'))" 
+    by (auto simp:image_def)
+  thus "Max ((\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')) =
+        Max ((\<lambda>th. preced th s') ` ({th'} \<union> dependants (wq s') th'))" by simp
+qed
+
+lemma eq_up:
+  fixes th' th''
+  assumes dp1: "th \<in> dependants s th'"
+  and dp2: "th' \<in> dependants s th''"
+  and eq_cps: "cp s th' = cp s' th'"
+  shows "cp s th'' = cp s' th''"
+proof -
+  from dp2
+  have "(Th th', Th th'') \<in> (RAG (wq s))\<^sup>+" by (simp add:s_dependants_def)
+  from RAG_child[OF vt_s this[unfolded eq_RAG]]
+  have ch_th': "(Th th', Th th'') \<in> (child s)\<^sup>+" .
+  moreover {
+    fix n th''
+    have "\<lbrakk>(Th th', n) \<in> (child s)^+\<rbrakk> \<Longrightarrow>
+                   (\<forall> th'' . n = Th th'' \<longrightarrow> cp s th'' = cp s' th'')"
+    proof(erule trancl_induct, auto)
+      fix y th''
+      assume y_ch: "(y, Th th'') \<in> child s"
+        and ih: "\<forall>th''. y = Th th'' \<longrightarrow> cp s th'' = cp s' th''"
+        and ch': "(Th th', y) \<in> (child s)\<^sup>+"
+      from y_ch obtain thy where eq_y: "y = Th thy" by (auto simp:child_def)
+      with ih have eq_cpy:"cp s thy = cp s' thy" by blast
+      from dp1 have "(Th th, Th th') \<in> (RAG s)^+" by (auto simp:s_dependants_def eq_RAG)
+      moreover from child_RAG_p[OF ch'] and eq_y
+      have "(Th th', Th thy) \<in> (RAG s)^+" by simp
+      ultimately have dp_thy: "(Th th, Th thy) \<in> (RAG s)^+" by auto
+      show "cp s th'' = cp s' th''"
+        apply (subst cp_rec[OF vt_s])
+      proof -
+        have "preced th'' s = preced th'' s'"
+          by (simp add:s_def preced_def)
+        moreover { 
+          fix th1
+          assume th1_in: "th1 \<in> children s th''"
+          have "cp s th1 = cp s' th1"
+          proof(cases "th1 = thy")
+            case True
+            with eq_cpy show ?thesis by simp
+          next
+            case False
+            have neq_th1: "th1 \<noteq> th"
+            proof
+              assume eq_th1: "th1 = th"
+              with dp_thy have "(Th th1, Th thy) \<in> (RAG s)^+" by simp
+              from children_no_dep[OF vt_s _ _ this] and 
+              th1_in y_ch eq_y show False by (auto simp:children_def)
+            qed
+            have "th \<notin> dependants s th1"
+            proof
+              assume h:"th \<in> dependants s th1"
+              from eq_y dp_thy have "th \<in> dependants s thy" by (auto simp:s_dependants_def eq_RAG)
+              from dependants_child_unique[OF vt_s _ _ h this]
+              th1_in y_ch eq_y have "th1 = thy" by (auto simp:children_def child_def)
+              with False show False by auto
+            qed
+            from eq_cp[OF this]
+            show ?thesis .
+          qed
+        }
+        ultimately have "{preced th'' s} \<union> (cp s ` children s th'') = 
+          {preced th'' s'} \<union> (cp s' ` children s th'')" by (auto simp:image_def)
+        moreover have "children s th'' = children s' th''"
+          apply (unfold children_def child_def s_def RAG_set_unchanged, simp)
+          apply (fold s_def, auto simp:RAG_s)
+          proof -
+            assume "(Cs cs, Th th'') \<in> RAG s'"
+            with RAG_s have cs_th': "(Cs cs, Th th'') \<in> RAG s" by auto
+            from dp1 have "(Th th, Th th') \<in> (RAG s)^+"
+              by (auto simp:s_dependants_def eq_RAG)
+            from converse_tranclE[OF this]
+            obtain cs1 where h1: "(Th th, Cs cs1) \<in> RAG s"
+              and h2: "(Cs cs1 , Th th') \<in> (RAG s)\<^sup>+"
+              by (auto simp:s_RAG_def)
+            have eq_cs: "cs1 = cs" 
+            proof -
+              from RAG_s have "(Th th, Cs cs) \<in> RAG s" by simp
+              from unique_RAG[OF vt_s this h1]
+              show ?thesis by simp
+            qed
+            have False
+            proof(rule converse_tranclE[OF h2])
+              assume "(Cs cs1, Th th') \<in> RAG s"
+              with eq_cs have "(Cs cs, Th th') \<in> RAG s" by simp
+              from unique_RAG[OF vt_s this cs_th']
+              have "th' = th''" by simp
+              with ch' y_ch have "(Th th'', Th th'') \<in> (child s)^+" by auto
+              with wf_trancl[OF wf_child[OF vt_s]] 
+              show False by auto
+            next
+              fix y
+              assume "(Cs cs1, y) \<in> RAG s"
+                and ytd: " (y, Th th') \<in> (RAG s)\<^sup>+"
+              with eq_cs have csy: "(Cs cs, y) \<in> RAG s" by simp
+              from unique_RAG[OF vt_s this cs_th']
+              have "y = Th th''" .
+              with ytd have "(Th th'', Th th') \<in> (RAG s)^+" by simp
+              from RAG_child[OF vt_s this]
+              have "(Th th'', Th th') \<in> (child s)\<^sup>+" .
+              moreover from ch' y_ch have ch'': "(Th th', Th th'') \<in> (child s)^+" by auto
+              ultimately have "(Th th'', Th th'') \<in> (child s)^+" by auto 
+              with wf_trancl[OF wf_child[OF vt_s]] 
+              show False by auto
+            qed
+            thus "\<exists>cs. (Th th, Cs cs) \<in> RAG s' \<and> (Cs cs, Th th'') \<in> RAG s'" by auto
+          qed
+          ultimately show "Max ({preced th'' s} \<union> cp s ` children s th'') = cp s' th''"
+          by (simp add:cp_rec[OF step_back_vt[OF vt_s[unfolded s_def]]])
+      qed
+    next
+      fix th''
+      assume dp': "(Th th', Th th'') \<in> child s"
+      show "cp s th'' = cp s' th''"
+        apply (subst cp_rec[OF vt_s])
+      proof -
+        have "preced th'' s = preced th'' s'"
+          by (simp add:s_def preced_def)
+        moreover { 
+          fix th1
+          assume th1_in: "th1 \<in> children s th''"
+          have "cp s th1 = cp s' th1"
+          proof(cases "th1 = th'")
+            case True
+            with eq_cps show ?thesis by simp
+          next
+            case False
+            have neq_th1: "th1 \<noteq> th"
+            proof
+              assume eq_th1: "th1 = th"
+              with dp1 have "(Th th1, Th th') \<in> (RAG s)^+" 
+                by (auto simp:s_dependants_def eq_RAG)
+              from children_no_dep[OF vt_s _ _ this]
+              th1_in dp'
+              show False by (auto simp:children_def)
+            qed
+            show ?thesis
+            proof(rule eq_cp)
+              show "th \<notin> dependants s th1"
+              proof
+                assume "th \<in> dependants s th1"
+                from dependants_child_unique[OF vt_s _ _ this dp1]
+                th1_in dp' have "th1 = th'"
+                  by (auto simp:children_def)
+                with False show False by auto
+              qed
+            qed
+          qed
+        }
+        ultimately have "{preced th'' s} \<union> (cp s ` children s th'') = 
+          {preced th'' s'} \<union> (cp s' ` children s th'')" by (auto simp:image_def)
+        moreover have "children s th'' = children s' th''"
+          apply (unfold children_def child_def s_def RAG_set_unchanged, simp)
+          apply (fold s_def, auto simp:RAG_s)
+          proof -
+            assume "(Cs cs, Th th'') \<in> RAG s'"
+            with RAG_s have cs_th': "(Cs cs, Th th'') \<in> RAG s" by auto
+            from dp1 have "(Th th, Th th') \<in> (RAG s)^+"
+              by (auto simp:s_dependants_def eq_RAG)
+            from converse_tranclE[OF this]
+            obtain cs1 where h1: "(Th th, Cs cs1) \<in> RAG s"
+              and h2: "(Cs cs1 , Th th') \<in> (RAG s)\<^sup>+"
+              by (auto simp:s_RAG_def)
+            have eq_cs: "cs1 = cs" 
+            proof -
+              from RAG_s have "(Th th, Cs cs) \<in> RAG s" by simp
+              from unique_RAG[OF vt_s this h1]
+              show ?thesis by simp
+            qed
+            have False
+            proof(rule converse_tranclE[OF h2])
+              assume "(Cs cs1, Th th') \<in> RAG s"
+              with eq_cs have "(Cs cs, Th th') \<in> RAG s" by simp
+              from unique_RAG[OF vt_s this cs_th']
+              have "th' = th''" by simp
+              with dp' have "(Th th'', Th th'') \<in> (child s)^+" by auto
+              with wf_trancl[OF wf_child[OF vt_s]] 
+              show False by auto
+            next
+              fix y
+              assume "(Cs cs1, y) \<in> RAG s"
+                and ytd: " (y, Th th') \<in> (RAG s)\<^sup>+"
+              with eq_cs have csy: "(Cs cs, y) \<in> RAG s" by simp
+              from unique_RAG[OF vt_s this cs_th']
+              have "y = Th th''" .
+              with ytd have "(Th th'', Th th') \<in> (RAG s)^+" by simp
+              from RAG_child[OF vt_s this]
+              have "(Th th'', Th th') \<in> (child s)\<^sup>+" .
+              moreover from dp' have ch'': "(Th th', Th th'') \<in> (child s)^+" by auto
+              ultimately have "(Th th'', Th th'') \<in> (child s)^+" by auto 
+              with wf_trancl[OF wf_child[OF vt_s]] 
+              show False by auto
+            qed
+            thus "\<exists>cs. (Th th, Cs cs) \<in> RAG s' \<and> (Cs cs, Th th'') \<in> RAG s'" by auto
+          qed
+        ultimately show "Max ({preced th'' s} \<union> cp s ` children s th'') = cp s' th''"
+          by (simp add:cp_rec[OF step_back_vt[OF vt_s[unfolded s_def]]])
+      qed     
+    qed
+  }
+  ultimately show ?thesis by auto
+qed
+
+end
+
+locale step_create_cps =
+  fixes s' th prio s 
+  defines s_def : "s \<equiv> (Create th prio#s')"
+  assumes vt_s: "vt s"
+
+context step_create_cps
+begin
+
+lemma eq_dep: "RAG s = RAG s'"
+  by (unfold s_def RAG_create_unchanged, auto)
+
+lemma eq_cp:
+  fixes th' 
+  assumes neq_th: "th' \<noteq> th"
+  shows "cp s th' = cp s' th'"
+  apply (unfold cp_eq_cpreced cpreced_def)
+proof -
+  have nd: "th \<notin> dependants s th'"
+  proof
+    assume "th \<in> dependants s th'"
+    hence "(Th th, Th th') \<in> (RAG s)^+" by (simp add:s_dependants_def eq_RAG)
+    with eq_dep have "(Th th, Th th') \<in> (RAG s')^+" by simp
+    from converse_tranclE[OF this]
+    obtain y where "(Th th, y) \<in> RAG s'" by auto
+    with dm_RAG_threads[OF step_back_vt[OF vt_s[unfolded s_def]]]
+    have in_th: "th \<in> threads s'" by auto
+    from step_back_step[OF vt_s[unfolded s_def]]
+    show False
+    proof(cases)
+      assume "th \<notin> threads s'" 
+      with in_th show ?thesis by simp
+    qed
+  qed
+  have eq_dp: "\<And> th. dependants (wq s) th = dependants (wq s') th"
+    by (unfold cs_dependants_def, auto simp:eq_dep eq_RAG)
+  moreover {
+    fix th1 
+    assume "th1 \<in> {th'} \<union> dependants (wq s') th'"
+    hence "th1 = th' \<or> th1 \<in> dependants (wq s') th'" by auto
+    hence "preced th1 s = preced th1 s'"
+    proof
+      assume "th1 = th'"
+      with neq_th
+      show "preced th1 s = preced th1 s'" by (auto simp:s_def preced_def)
+    next
+      assume "th1 \<in> dependants (wq s') th'"
+      with nd and eq_dp have "th1 \<noteq> th"
+        by (auto simp:eq_RAG cs_dependants_def s_dependants_def eq_dep)
+      thus "preced th1 s = preced th1 s'" by (auto simp:s_def preced_def)
+    qed
+  } ultimately have "((\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')) = 
+                     ((\<lambda>th. preced th s') ` ({th'} \<union> dependants (wq s') th'))" 
+    by (auto simp:image_def)
+  thus "Max ((\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')) =
+        Max ((\<lambda>th. preced th s') ` ({th'} \<union> dependants (wq s') th'))" by simp
+qed
+
+lemma nil_dependants: "dependants s th = {}"
+proof -
+  from step_back_step[OF vt_s[unfolded s_def]]
+  show ?thesis
+  proof(cases)
+    assume "th \<notin> threads s'"
+    from not_thread_holdents[OF step_back_vt[OF vt_s[unfolded s_def]] this]
+    have hdn: " holdents s' th = {}" .
+    have "dependants s' th = {}"
+    proof -
+      { assume "dependants s' th \<noteq> {}"
+        then obtain th' where dp: "(Th th', Th th) \<in> (RAG s')^+"
+          by (auto simp:s_dependants_def eq_RAG)
+        from tranclE[OF this] obtain cs' where 
+          "(Cs cs', Th th) \<in> RAG s'" by (auto simp:s_RAG_def)
+        with hdn
+        have False by (auto simp:holdents_test)
+      } thus ?thesis by auto
+    qed
+    thus ?thesis 
+      by (unfold s_def s_dependants_def eq_RAG RAG_create_unchanged, simp)
+  qed
+qed
+
+lemma eq_cp_th: "cp s th = preced th s"
+  apply (unfold cp_eq_cpreced cpreced_def)
+  by (insert nil_dependants, unfold s_dependants_def cs_dependants_def, auto)
+
+end
+
+
+locale step_exit_cps =
+  fixes s' th prio s 
+  defines s_def : "s \<equiv> Exit th # s'"
+  assumes vt_s: "vt s"
+
+context step_exit_cps
+begin
+
+lemma eq_dep: "RAG s = RAG s'"
+  by (unfold s_def RAG_exit_unchanged, auto)
+
+lemma eq_cp:
+  fixes th' 
+  assumes neq_th: "th' \<noteq> th"
+  shows "cp s th' = cp s' th'"
+  apply (unfold cp_eq_cpreced cpreced_def)
+proof -
+  have nd: "th \<notin> dependants s th'"
+  proof
+    assume "th \<in> dependants s th'"
+    hence "(Th th, Th th') \<in> (RAG s)^+" by (simp add:s_dependants_def eq_RAG)
+    with eq_dep have "(Th th, Th th') \<in> (RAG s')^+" by simp
+    from converse_tranclE[OF this]
+    obtain cs' where bk: "(Th th, Cs cs') \<in> RAG s'"
+      by (auto simp:s_RAG_def)
+    from step_back_step[OF vt_s[unfolded s_def]]
+    show False
+    proof(cases)
+      assume "th \<in> runing s'"
+      with bk show ?thesis
+        apply (unfold runing_def readys_def s_waiting_def s_RAG_def)
+        by (auto simp:cs_waiting_def wq_def)
+    qed
+  qed
+  have eq_dp: "\<And> th. dependants (wq s) th = dependants (wq s') th"
+    by (unfold cs_dependants_def, auto simp:eq_dep eq_RAG)
+  moreover {
+    fix th1 
+    assume "th1 \<in> {th'} \<union> dependants (wq s') th'"
+    hence "th1 = th' \<or> th1 \<in> dependants (wq s') th'" by auto
+    hence "preced th1 s = preced th1 s'"
+    proof
+      assume "th1 = th'"
+      with neq_th
+      show "preced th1 s = preced th1 s'" by (auto simp:s_def preced_def)
+    next
+      assume "th1 \<in> dependants (wq s') th'"
+      with nd and eq_dp have "th1 \<noteq> th"
+        by (auto simp:eq_RAG cs_dependants_def s_dependants_def eq_dep)
+      thus "preced th1 s = preced th1 s'" by (auto simp:s_def preced_def)
+    qed
+  } ultimately have "((\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')) = 
+                     ((\<lambda>th. preced th s') ` ({th'} \<union> dependants (wq s') th'))" 
+    by (auto simp:image_def)
+  thus "Max ((\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')) =
+        Max ((\<lambda>th. preced th s') ` ({th'} \<union> dependants (wq s') th'))" by simp
+qed
+
+end
+end
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PrioG.thy~	Thu Dec 03 14:34:29 2015 +0800
@@ -0,0 +1,2920 @@
+theory PrioG
+imports PrioGDef 
+begin
+
+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:
+   "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 wq_distinct: "vt s \<Longrightarrow> distinct (wq s cs)"
+proof(erule_tac vt.induct, simp add:wq_def)
+  fix s e
+  assume h1: "step s e"
+  and h2: "distinct (wq s cs)"
+  thus "distinct (wq (e # s) cs)"
+  proof(induct rule:step.induct, auto simp: wq_def Let_def split:list.splits)
+    fix thread s
+    assume h1: "(Cs cs, Th thread) \<notin> (RAG s)\<^sup>+"
+      and h2: "thread \<in> set (wq_fun (schs s) cs)"
+      and h3: "thread \<in> runing s"
+    show "False" 
+    proof -
+      from h3 have "\<And> cs. thread \<in>  set (wq_fun (schs s) cs) \<Longrightarrow> 
+                             thread = hd ((wq_fun (schs s) cs))" 
+        by (simp add:runing_def readys_def s_waiting_def wq_def)
+      from this [OF h2] have "thread = hd (wq_fun (schs s) cs)" .
+      with h2
+      have "(Cs cs, Th thread) \<in> (RAG s)"
+        by (simp add:s_RAG_def s_holding_def wq_def cs_holding_def)
+      with h1 show False by auto
+    qed
+  next
+    fix thread s a list
+    assume dst: "distinct list"
+    show "distinct (SOME q. distinct q \<and> set q = set list)"
+    proof(rule someI2)
+      from dst show  "distinct list \<and> set list = set list" by auto
+    next
+      fix q assume "distinct q \<and> set q = set list"
+      thus "distinct q" by auto
+    qed
+  qed
+qed
+
+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 block_pre: 
+  fixes thread cs s
+  assumes vt_e: "vt (e#s)"
+  and s_ni: "thread \<notin>  set (wq s cs)"
+  and s_i: "thread \<in> set (wq (e#s) cs)"
+  shows "e = P thread cs"
+proof -
+  show ?thesis
+  proof(cases e)
+    case (P th cs)
+    with assms
+    show ?thesis
+      by (auto simp:wq_def Let_def split:if_splits)
+  next
+    case (Create th prio)
+    with assms show ?thesis
+      by (auto simp:wq_def Let_def split:if_splits)
+  next
+    case (Exit th)
+    with assms show ?thesis
+      by (auto simp:wq_def Let_def split:if_splits)
+  next
+    case (Set th prio)
+    with assms show ?thesis
+      by (auto simp:wq_def Let_def split:if_splits)
+  next
+    case (V th cs)
+    with assms show ?thesis
+      apply (auto simp:wq_def Let_def split:if_splits)
+    proof -
+      fix q qs
+      assume h1: "thread \<notin> set (wq_fun (schs s) cs)"
+        and h2: "q # qs = wq_fun (schs s) cs"
+        and h3: "thread \<in> set (SOME q. distinct q \<and> set q = set qs)"
+        and vt: "vt (V th cs # s)"
+      from h1 and h2[symmetric] have "thread \<notin> set (q # qs)" by simp
+      moreover have "thread \<in> set qs"
+      proof -
+        have "set (SOME q. distinct q \<and> set q = set qs) = set qs"
+        proof(rule someI2)
+          from wq_distinct [OF step_back_vt[OF vt], of cs]
+          and h2[symmetric, folded wq_def]
+          show "distinct qs \<and> set qs = set qs" by auto
+        next
+          fix x assume "distinct x \<and> set x = set qs"
+          thus "set x = set qs" by auto
+        qed
+        with h3 show ?thesis by simp
+      qed
+      ultimately show "False" by auto
+      qed
+  qed
+qed
+
+text {*
+  The following lemmas is also obvious and shallow. It says
+  that only running thread can request for a critical resource 
+  and that the requested resource must be one which is
+  not current held by the thread.
+*}
+
+lemma p_pre: "\<lbrakk>vt ((P thread cs)#s)\<rbrakk> \<Longrightarrow> 
+  thread \<in> runing s \<and> (Cs cs, Th thread)  \<notin> (RAG s)^+"
+apply (ind_cases "vt ((P thread cs)#s)")
+apply (ind_cases "step s (P thread cs)")
+by auto
+
+lemma abs1:
+  fixes e es
+  assumes ein: "e \<in> set es"
+  and neq: "hd es \<noteq> hd (es @ [x])"
+  shows "False"
+proof -
+  from ein have "es \<noteq> []" by auto
+  then obtain e ess where "es = e # ess" by (cases es, auto)
+  with neq show ?thesis by auto
+qed
+
+lemma q_head: "Q (hd es) \<Longrightarrow> hd es = hd [th\<leftarrow>es . Q th]"
+  by (cases es, auto)
+
+inductive_cases evt_cons: "vt (a#s)"
+
+lemma abs2:
+  assumes vt: "vt (e#s)"
+  and inq: "thread \<in> set (wq s cs)"
+  and nh: "thread = hd (wq s cs)"
+  and qt: "thread \<noteq> hd (wq (e#s) cs)"
+  and inq': "thread \<in> set (wq (e#s) cs)"
+  shows "False"
+proof -
+  from assms show "False"
+    apply (cases e)
+    apply ((simp split:if_splits add:Let_def wq_def)[1])+
+    apply (insert abs1, fast)[1]
+    apply (auto simp:wq_def simp:Let_def split:if_splits list.splits)
+  proof -
+    fix th qs
+    assume vt: "vt (V th cs # s)"
+      and th_in: "thread \<in> set (SOME q. distinct q \<and> set q = set qs)"
+      and eq_wq: "wq_fun (schs s) cs = thread # qs"
+    show "False"
+    proof -
+      from wq_distinct[OF step_back_vt[OF vt], of cs]
+        and eq_wq[folded wq_def] have "distinct (thread#qs)" by simp
+      moreover have "thread \<in> set qs"
+      proof -
+        have "set (SOME q. distinct q \<and> set q = set qs) = set qs"
+        proof(rule someI2)
+          from wq_distinct [OF step_back_vt[OF vt], of cs]
+          and eq_wq [folded wq_def]
+          show "distinct qs \<and> set qs = set qs" by auto
+        next
+          fix x assume "distinct x \<and> set x = set qs"
+          thus "set x = set qs" by auto
+        qed
+        with th_in show ?thesis by auto
+      qed
+      ultimately show ?thesis by auto
+    qed
+  qed
+qed
+
+lemma vt_moment: "\<And> t. \<lbrakk>vt s\<rbrakk> \<Longrightarrow> vt (moment t s)"
+proof(induct s, simp)
+  fix a s t
+  assume h: "\<And>t.\<lbrakk>vt s\<rbrakk> \<Longrightarrow> vt (moment t s)"
+    and vt_a: "vt (a # s)"
+  show "vt (moment t (a # s))"
+  proof(cases "t \<ge> length (a#s)")
+    case True
+    from True have "moment t (a#s) = a#s" by simp
+    with vt_a show ?thesis by simp
+  next
+    case False
+    hence le_t1: "t \<le> length s" by simp
+    from vt_a have "vt s"
+      by (erule_tac evt_cons, simp)
+    from h [OF this] have "vt (moment t s)" .
+    moreover have "moment t (a#s) = moment t s"
+    proof -
+      from moment_app [OF le_t1, of "[a]"] 
+      show ?thesis by simp
+    qed
+    ultimately show ?thesis by auto
+  qed
+qed
+
+(* Wrong:
+    lemma \<lbrakk>thread \<in> set (wq_fun cs1 s); thread \<in> set (wq_fun cs2 s)\<rbrakk> \<Longrightarrow> cs1 = cs2"
+*)
+
+text {* (* ??? *)
+  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 lose 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:
+  fixes cs1 cs2 s thread
+  assumes vt: "vt s"
+  and 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 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>(thread \<in> set (wq (moment t1 s) cs1) \<and>
+        thread \<noteq> hd (wq (moment t1 s) cs1))"
+    and nn1: "(\<forall>i'>t1. thread \<in> set (wq (moment i' s) cs1) \<and>
+             thread \<noteq> hd (wq (moment i' s) cs1))" by auto
+  from p_split [of "?Q cs2", OF q2 nq2]
+  obtain t2 where lt2: "t2 < length s"
+    and np2: "\<not>(thread \<in> set (wq (moment t2 s) cs2) \<and>
+        thread \<noteq> hd (wq (moment t2 s) cs2))"
+    and nn2: "(\<forall>i'>t2. thread \<in> set (wq (moment i' s) cs2) \<and>
+             thread \<noteq> hd (wq (moment i' s) cs2))" by auto
+  show ?thesis
+  proof -
+    { 
+      assume 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: "vt (e#moment t2 s)"
+      proof -
+        from vt_moment [OF vt]
+        have "vt (moment ?t3 s)" .
+        with eq_m show ?thesis by simp
+      qed
+      have ?thesis
+      proof(cases "thread \<in> set (wq (moment t2 s) cs2)")
+        case True
+        from True and np2 have eq_th: "thread = hd (wq (moment t2 s) cs2)"
+          by auto
+          thm abs2
+        from abs2 [OF vt_e True eq_th h2 h1]
+        show ?thesis by auto
+      next
+        case False
+        from block_pre [OF vt_e False h1]
+        have "e = P thread cs2" .
+        with vt_e have "vt ((P thread cs2)# moment t2 s)" by simp
+        from p_pre [OF this] have "thread \<in> runing (moment t2 s)" by simp
+        with runing_ready have "thread \<in> readys (moment t2 s)" by auto
+        with nn1 [rule_format, OF lt12]
+        show ?thesis  by (simp add:readys_def wq_def s_waiting_def, auto)
+      qed
+    } moreover {
+      assume lt12: "t2 < t1"
+      let ?t3 = "Suc t1"
+      from lt1 have le_t3: "?t3 \<le> length s" by auto
+      from moment_plus [OF this] 
+      obtain e where eq_m: "moment ?t3 s = e#moment t1 s" by auto
+      have lt_t3: "t1 < ?t3" by simp
+      from nn1 [rule_format, OF this] and eq_m
+      have h1: "thread \<in> set (wq (e#moment t1 s) cs1)" and
+        h2: "thread \<noteq> hd (wq (e#moment t1 s) cs1)" by auto
+      have vt_e: "vt  (e#moment t1 s)"
+      proof -
+        from vt_moment [OF vt]
+        have "vt (moment ?t3 s)" .
+        with eq_m show ?thesis by simp
+      qed
+      have ?thesis
+      proof(cases "thread \<in> set (wq (moment t1 s) cs1)")
+        case True
+        from True and np1 have eq_th: "thread = hd (wq (moment t1 s) cs1)"
+          by auto
+        from abs2 [OF vt_e True eq_th h2 h1]
+        show ?thesis by auto
+      next
+        case False
+        from block_pre [OF vt_e False h1]
+        have "e = P thread cs1" .
+        with vt_e have "vt ((P thread cs1)# moment t1 s)" by simp
+        from p_pre [OF this] have "thread \<in> runing (moment t1 s)" by simp
+        with runing_ready have "thread \<in> readys (moment t1 s)" by auto
+        with nn2 [rule_format, OF lt12]
+        show ?thesis  by (simp add:readys_def wq_def s_waiting_def, auto)
+      qed
+    } moreover {
+      assume eqt12: "t1 = t2"
+      let ?t3 = "Suc t1"
+      from lt1 have le_t3: "?t3 \<le> length s" by auto
+      from moment_plus [OF this] 
+      obtain e where eq_m: "moment ?t3 s = e#moment t1 s" by auto
+      have lt_t3: "t1 < ?t3" by simp
+      from nn1 [rule_format, OF this] and eq_m
+      have h1: "thread \<in> set (wq (e#moment t1 s) cs1)" and
+        h2: "thread \<noteq> hd (wq (e#moment t1 s) cs1)" by auto
+      have vt_e: "vt (e#moment t1 s)"
+      proof -
+        from vt_moment [OF vt]
+        have "vt (moment ?t3 s)" .
+        with eq_m show ?thesis by simp
+      qed
+      have ?thesis
+      proof(cases "thread \<in> set (wq (moment t1 s) cs1)")
+        case True
+        from True and np1 have eq_th: "thread = hd (wq (moment t1 s) cs1)"
+          by auto
+        from abs2 [OF vt_e True eq_th h2 h1]
+        show ?thesis by auto
+      next
+        case False
+        from block_pre [OF vt_e False h1]
+        have eq_e1: "e = P thread cs1" .
+        have lt_t3: "t1 < ?t3" by simp
+        with eqt12 have "t2 < ?t3" by simp
+        from nn2 [rule_format, OF this] and eq_m and eqt12
+        have h1: "thread \<in> set (wq (e#moment t2 s) cs2)" and
+          h2: "thread \<noteq> hd (wq (e#moment t2 s) cs2)" by auto
+        show ?thesis
+        proof(cases "thread \<in> set (wq (moment t2 s) cs2)")
+          case True
+          from True and np2 have eq_th: "thread = hd (wq (moment t2 s) cs2)"
+            by auto
+          from vt_e and eqt12 have "vt (e#moment t2 s)" by simp 
+          from abs2 [OF this True eq_th h2 h1]
+          show ?thesis .
+        next
+          case False
+          have vt_e: "vt (e#moment t2 s)"
+          proof -
+            from vt_moment [OF vt] eqt12
+            have "vt (moment (Suc t2) s)" by auto
+            with eq_m eqt12 show ?thesis by simp
+          qed
+          from block_pre [OF vt_e False h1]
+          have "e = P thread cs2" .
+          with eq_e1 neq12 show ?thesis by auto
+        qed
+      qed
+    } ultimately show ?thesis by arith
+  qed
+qed
+
+text {*
+  This lemma is a simple corrolary of @{text "waiting_unique_pre"}.
+*}
+
+lemma waiting_unique:
+  fixes s cs1 cs2
+  assumes "vt s"
+  and "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
+
+(* 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:
+  fixes s::"state"
+  assumes "holding s th1 cs"
+  and "holding s th2 cs"
+  shows "th1 = th2"
+using assms
+unfolding s_holding_def
+by 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
+
+(* An aux lemma used later *)
+lemma unique_minus:
+  fixes x y z r
+  assumes unique: "\<And> a b c. \<lbrakk>(a, b) \<in> r; (a, c) \<in> r\<rbrakk> \<Longrightarrow> b = c"
+  and xy: "(x, y) \<in> r"
+  and xz: "(x, z) \<in> r^+"
+  and neq: "y \<noteq> z"
+  shows "(y, z) \<in> r^+"
+proof -
+ from xz and neq show ?thesis
+ proof(induct)
+   case (base ya)
+   have "(x, ya) \<in> r" by fact
+   from unique [OF xy this] have "y = ya" .
+   with base show ?case by auto
+ next
+   case (step ya z)
+   show ?case
+   proof(cases "y = ya")
+     case True
+     from step True show ?thesis by simp
+   next
+     case False
+     from step False
+     show ?thesis by auto
+   qed
+ qed
+qed
+
+lemma unique_base:
+  fixes r x y z
+  assumes unique: "\<And> a b c. \<lbrakk>(a, b) \<in> r; (a, c) \<in> r\<rbrakk> \<Longrightarrow> b = c"
+  and xy: "(x, y) \<in> r"
+  and xz: "(x, z) \<in> r^+"
+  and neq_yz: "y \<noteq> z"
+  shows "(y, z) \<in> r^+"
+proof -
+  from xz neq_yz show ?thesis
+  proof(induct)
+    case (base ya)
+    from xy unique base show ?case by auto
+  next
+    case (step ya z)
+    show ?case
+    proof(cases "y = ya")
+      case True
+      from True step show ?thesis by auto
+    next
+      case False
+      from False step 
+      have "(y, ya) \<in> r\<^sup>+" by auto
+      with step show ?thesis by auto
+    qed
+  qed
+qed
+
+lemma unique_chain:
+  fixes r x y z
+  assumes unique: "\<And> a b c. \<lbrakk>(a, b) \<in> r; (a, c) \<in> r\<rbrakk> \<Longrightarrow> b = c"
+  and xy: "(x, y) \<in> r^+"
+  and xz: "(x, z) \<in> r^+"
+  and neq_yz: "y \<noteq> z"
+  shows "(y, z) \<in> r^+ \<or> (z, y) \<in> r^+"
+proof -
+  from xy xz neq_yz show ?thesis
+  proof(induct)
+    case (base y)
+    have h1: "(x, y) \<in> r" and h2: "(x, z) \<in> r\<^sup>+" and h3: "y \<noteq> z" using base by auto
+    from unique_base [OF _ h1 h2 h3] and unique show ?case by auto
+  next
+    case (step y za)
+    show ?case
+    proof(cases "y = z")
+      case True
+      from True step show ?thesis by auto
+    next
+      case False
+      from False step have "(y, z) \<in> r\<^sup>+ \<or> (z, y) \<in> r\<^sup>+" by auto
+      thus ?thesis
+      proof
+        assume "(z, y) \<in> r\<^sup>+"
+        with step have "(z, za) \<in> r\<^sup>+" by auto
+        thus ?thesis by auto
+      next
+        assume h: "(y, z) \<in> r\<^sup>+"
+        from step have yza: "(y, za) \<in> r" by simp
+        from step have "za \<noteq> z" by simp
+        from unique_minus [OF _ yza h this] and unique
+        have "(za, z) \<in> r\<^sup>+" by auto
+        thus ?thesis by auto
+      qed
+    qed
+  qed
+qed
+
+text {*
+  The following three lemmas show that @{text "RAG"} does not change
+  by the happening of @{text "Set"}, @{text "Create"} and @{text "Exit"}
+  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 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 RAG_exit_unchanged: "(RAG (Exit th # s)) = RAG s"
+apply (unfold s_RAG_def s_waiting_def wq_def)
+by (simp add:Let_def)
+
+
+text {* 
+  The following lemmas are used in the proof of 
+  lemma @{text "step_RAG_v"}, which characterizes how the @{text "RAG"} is changed
+  by @{text "V"}-events. 
+  However, since our model is very concise, such  seemingly obvious lemmas need to be derived from scratch,
+  starting from the model definitions.
+*}
+lemma step_v_hold_inv[elim_format]:
+  "\<And>c t. \<lbrakk>vt (V th cs # s); 
+          \<not> holding (wq s) t c; holding (wq (V th cs # s)) t c\<rbrakk> \<Longrightarrow> 
+            next_th s th cs t \<and> c = cs"
+proof -
+  fix c t
+  assume vt: "vt (V th cs # s)"
+    and nhd: "\<not> holding (wq s) t c"
+    and hd: "holding (wq (V th cs # s)) t c"
+  show "next_th s th cs t \<and> c = cs"
+  proof(cases "c = cs")
+    case False
+    with nhd hd show ?thesis
+      by (unfold cs_holding_def wq_def, auto simp:Let_def)
+  next
+    case True
+    with step_back_step [OF vt] 
+    have "step s (V th c)" by simp
+    hence "next_th s th cs t"
+    proof(cases)
+      assume "holding s th c"
+      with nhd hd show ?thesis
+        apply (unfold s_holding_def cs_holding_def wq_def next_th_def,
+               auto simp:Let_def split:list.splits if_splits)
+        proof -
+          assume " hd (SOME q. distinct q \<and> q = []) \<in> set (SOME q. distinct q \<and> q = [])"
+          moreover have "\<dots> = set []"
+          proof(rule someI2)
+            show "distinct [] \<and> [] = []" by auto
+          next
+            fix x assume "distinct x \<and> x = []"
+            thus "set x = set []" by auto
+          qed
+          ultimately show False by auto
+        next
+          assume " hd (SOME q. distinct q \<and> q = []) \<in> set (SOME q. distinct q \<and> q = [])"
+          moreover have "\<dots> = set []"
+          proof(rule someI2)
+            show "distinct [] \<and> [] = []" by auto
+          next
+            fix x assume "distinct x \<and> x = []"
+            thus "set x = set []" by auto
+          qed
+          ultimately show False by auto
+        qed
+    qed
+    with True show ?thesis by auto
+  qed
+qed
+
+text {* 
+  The following @{text "step_v_wait_inv"} is also an obvious lemma, which, however, needs to be
+  derived from scratch, which confirms the correctness of the definition of @{text "next_th"}.
+*}
+lemma step_v_wait_inv[elim_format]:
+    "\<And>t c. \<lbrakk>vt (V th cs # s); \<not> waiting (wq (V th cs # s)) t c; waiting (wq s) t c
+           \<rbrakk>
+          \<Longrightarrow> (next_th s th cs t \<and> cs = c)"
+proof -
+  fix t c 
+  assume vt: "vt (V th cs # s)"
+    and nw: "\<not> waiting (wq (V th cs # s)) t c"
+    and wt: "waiting (wq s) t c"
+  show "next_th s th cs t \<and> cs = c"
+  proof(cases "cs = c")
+    case False
+    with nw wt show ?thesis
+      by (auto simp:cs_waiting_def wq_def Let_def)
+  next
+    case True
+    from nw[folded True] wt[folded True]
+    have "next_th s th cs t"
+      apply (unfold next_th_def, auto simp:cs_waiting_def wq_def Let_def split:list.splits)
+    proof -
+      fix a list
+      assume t_in: "t \<in> set list"
+        and t_ni: "t \<notin> set (SOME q. distinct q \<and> set q = set list)"
+        and eq_wq: "wq_fun (schs s) cs = a # list"
+      have " set (SOME q. distinct q \<and> set q = set list) = set list"
+      proof(rule someI2)
+        from wq_distinct[OF step_back_vt[OF vt], of cs] and eq_wq[folded wq_def]
+        show "distinct list \<and> set list = set list" by auto
+      next
+        show "\<And>x. distinct x \<and> set x = set list \<Longrightarrow> set x = set list"
+          by auto
+      qed
+      with t_ni and t_in show "a = th" by auto
+    next
+      fix a list
+      assume t_in: "t \<in> set list"
+        and t_ni: "t \<notin> set (SOME q. distinct q \<and> set q = set list)"
+        and eq_wq: "wq_fun (schs s) cs = a # list"
+      have " set (SOME q. distinct q \<and> set q = set list) = set list"
+      proof(rule someI2)
+        from wq_distinct[OF step_back_vt[OF vt], of cs] and eq_wq[folded wq_def]
+        show "distinct list \<and> set list = set list" by auto
+      next
+        show "\<And>x. distinct x \<and> set x = set list \<Longrightarrow> set x = set list"
+          by auto
+      qed
+      with t_ni and t_in show "t = hd (SOME q. distinct q \<and> set q = set list)" by auto
+    next
+      fix a list
+      assume eq_wq: "wq_fun (schs s) cs = a # list"
+      from step_back_step[OF vt]
+      show "a = th"
+      proof(cases)
+        assume "holding s th cs"
+        with eq_wq show ?thesis
+          by (unfold s_holding_def wq_def, auto)
+      qed
+    qed
+    with True show ?thesis by simp
+  qed
+qed
+
+lemma step_v_not_wait[consumes 3]:
+  "\<lbrakk>vt (V th cs # s); next_th s th cs t; waiting (wq (V th cs # s)) t cs\<rbrakk> \<Longrightarrow> False"
+  by (unfold next_th_def cs_waiting_def wq_def, auto simp:Let_def)
+
+lemma step_v_release:
+  "\<lbrakk>vt (V th cs # s); holding (wq (V th cs # s)) th cs\<rbrakk> \<Longrightarrow> False"
+proof -
+  assume vt: "vt (V th cs # s)"
+    and hd: "holding (wq (V th cs # s)) th cs"
+  from step_back_step [OF vt] and hd
+  show "False"
+  proof(cases)
+    assume "holding (wq (V th cs # s)) th cs" and "holding s th cs"
+    thus ?thesis
+      apply (unfold s_holding_def wq_def cs_holding_def)
+      apply (auto simp:Let_def split:list.splits)
+    proof -
+      fix list
+      assume eq_wq[folded wq_def]: 
+        "wq_fun (schs s) cs = hd (SOME q. distinct q \<and> set q = set list) # list"
+      and hd_in: "hd (SOME q. distinct q \<and> set q = set list)
+            \<in> set (SOME q. distinct q \<and> set q = set list)"
+      have "set (SOME q. distinct q \<and> set q = set list) = set list"
+      proof(rule someI2)
+        from wq_distinct[OF step_back_vt[OF vt], of cs] and eq_wq
+        show "distinct list \<and> set list = set list" by auto
+      next
+        show "\<And>x. distinct x \<and> set x = set list \<Longrightarrow> set x = set list"
+          by auto
+      qed
+      moreover have "distinct  (hd (SOME q. distinct q \<and> set q = set list) # list)"
+      proof -
+        from wq_distinct[OF step_back_vt[OF vt], of cs] and eq_wq
+        show ?thesis by auto
+      qed
+      moreover note eq_wq and hd_in
+      ultimately show "False" by auto
+    qed
+  qed
+qed
+
+lemma step_v_get_hold:
+  "\<And>th'. \<lbrakk>vt (V th cs # s); \<not> holding (wq (V th cs # s)) th' cs; next_th s th cs th'\<rbrakk> \<Longrightarrow> False"
+  apply (unfold cs_holding_def next_th_def wq_def,
+         auto simp:Let_def)
+proof -
+  fix rest
+  assume vt: "vt (V th cs # s)"
+    and eq_wq[folded wq_def]: " wq_fun (schs s) cs = th # rest"
+    and nrest: "rest \<noteq> []"
+    and ni: "hd (SOME q. distinct q \<and> set q = set rest)
+            \<notin> set (SOME q. distinct q \<and> set q = set rest)"
+  have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
+  proof(rule someI2)
+    from wq_distinct[OF step_back_vt[OF vt], of cs] and eq_wq
+    show "distinct rest \<and> set rest = set rest" by auto
+  next
+    fix x assume "distinct x \<and> set x = set rest"
+    hence "set x = set rest" by auto
+    with nrest
+    show "x \<noteq> []" by (case_tac x, auto)
+  qed
+  with ni show "False" by auto
+qed
+
+lemma step_v_release_inv[elim_format]:
+"\<And>c t. \<lbrakk>vt (V th cs # s); \<not> holding (wq (V th cs # s)) t c; holding (wq s) t c\<rbrakk> \<Longrightarrow> 
+  c = cs \<and> t = th"
+  apply (unfold cs_holding_def wq_def, auto simp:Let_def split:if_splits list.splits)
+  proof -
+    fix a list
+    assume vt: "vt (V th cs # s)" and eq_wq: "wq_fun (schs s) cs = a # list"
+    from step_back_step [OF vt] show "a = th"
+    proof(cases)
+      assume "holding s th cs" with eq_wq
+      show ?thesis
+        by (unfold s_holding_def wq_def, auto)
+    qed
+  next
+    fix a list
+    assume vt: "vt (V th cs # s)" and eq_wq: "wq_fun (schs s) cs = a # list"
+    from step_back_step [OF vt] show "a = th"
+    proof(cases)
+      assume "holding s th cs" with eq_wq
+      show ?thesis
+        by (unfold s_holding_def wq_def, auto)
+    qed
+  qed
+
+lemma step_v_waiting_mono:
+  "\<And>t c. \<lbrakk>vt (V th cs # s); waiting (wq (V th cs # s)) t c\<rbrakk> \<Longrightarrow> waiting (wq s) t c"
+proof -
+  fix t c
+  let ?s' = "(V th cs # s)"
+  assume vt: "vt ?s'" 
+    and wt: "waiting (wq ?s') t c"
+  show "waiting (wq s) t c"
+  proof(cases "c = cs")
+    case False
+    assume neq_cs: "c \<noteq> cs"
+    hence "waiting (wq ?s') t c = waiting (wq s) t c"
+      by (unfold cs_waiting_def wq_def, auto simp:Let_def)
+    with wt show ?thesis by simp
+  next
+    case True
+    with wt show ?thesis
+      apply (unfold cs_waiting_def wq_def, auto simp:Let_def split:list.splits)
+    proof -
+      fix a list
+      assume not_in: "t \<notin> set list"
+        and is_in: "t \<in> set (SOME q. distinct q \<and> set q = set list)"
+        and eq_wq: "wq_fun (schs s) cs = a # list"
+      have "set (SOME q. distinct q \<and> set q = set list) = set list"
+      proof(rule someI2)
+        from wq_distinct [OF step_back_vt[OF vt], of cs]
+        and eq_wq[folded wq_def]
+        show "distinct list \<and> set list = set list" by auto
+      next
+        fix x assume "distinct x \<and> set x = set list"
+        thus "set x = set list" by auto
+      qed
+      with not_in is_in show "t = a" by auto
+    next
+      fix list
+      assume is_waiting: "waiting (wq (V th cs # s)) t cs"
+      and eq_wq: "wq_fun (schs s) cs = t # list"
+      hence "t \<in> set list"
+        apply (unfold wq_def, auto simp:Let_def cs_waiting_def)
+      proof -
+        assume " t \<in> set (SOME q. distinct q \<and> set q = set list)"
+        moreover have "\<dots> = set list" 
+        proof(rule someI2)
+          from wq_distinct [OF step_back_vt[OF vt], of cs]
+            and eq_wq[folded wq_def]
+          show "distinct list \<and> set list = set list" by auto
+        next
+          fix x assume "distinct x \<and> set x = set list" 
+          thus "set x = set list" by auto
+        qed
+        ultimately show "t \<in> set list" by simp
+      qed
+      with eq_wq and wq_distinct [OF step_back_vt[OF vt], of cs, unfolded wq_def]
+      show False by auto
+    qed
+  qed
+qed
+
+text {* (* ??? *) 
+  The following @{text "step_RAG_v"} lemma charaterizes how @{text "RAG"} is changed
+  with the happening of @{text "V"}-events:
+*}
+lemma step_RAG_v:
+fixes th::thread
+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'}"
+  apply (insert vt, unfold s_RAG_def) 
+  apply (auto split:if_splits list.splits simp:Let_def)
+  apply (auto elim: step_v_waiting_mono step_v_hold_inv 
+              step_v_release step_v_wait_inv
+              step_v_get_hold step_v_release_inv)
+  apply (erule_tac step_v_not_wait, auto)
+  done
+
+text {* 
+  The following @{text "step_RAG_p"} lemma charaterizes how @{text "RAG"} is changed
+  with the happening of @{text "P"}-events:
+*}
+lemma step_RAG_p:
+  "vt (P th cs#s) \<Longrightarrow>
+  RAG (P th cs # s) =  (if (wq s cs = []) then RAG s \<union> {(Cs cs, Th th)}
+                                             else RAG s \<union> {(Th th, Cs cs)})"
+  apply(simp only: s_RAG_def wq_def)
+  apply (auto split:list.splits prod.splits simp:Let_def wq_def cs_waiting_def cs_holding_def)
+  apply(case_tac "csa = cs", auto)
+  apply(fold wq_def)
+  apply(drule_tac step_back_step)
+  apply(ind_cases " step s (P (hd (wq s cs)) cs)")
+  apply(simp add:s_RAG_def wq_def cs_holding_def)
+  apply(auto)
+  done
+
+
+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 lemma shows that @{text "RAG"} is acyclic.
+  The overall structure is by induction on the formation of @{text "vt s"}
+  and then case analysis on event @{text "e"}, where the non-trivial cases 
+  for those for @{text "V"} and @{text "P"} events.
+*}
+lemma acyclic_RAG: 
+  fixes s
+  assumes vt: "vt s"
+  shows "acyclic (RAG s)"
+using assms
+proof(induct)
+  case (vt_cons s e)
+  assume ih: "acyclic (RAG s)"
+    and stp: "step s e"
+    and vt: "vt s"
+  show ?case
+  proof(cases e)
+    case (Create th prio)
+    with ih
+    show ?thesis by (simp add:RAG_create_unchanged)
+  next
+    case (Exit th)
+    with ih show ?thesis by (simp add:RAG_exit_unchanged)
+  next
+    case (V th cs)
+    from V vt stp have vtt: "vt (V th cs#s)" by auto
+    from step_RAG_v [OF this]
+    have eq_de: 
+      "RAG (e # 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 = (?A - ?B - ?C) \<union> ?D") by (simp add:V)
+    from ih have ac: "acyclic (?A - ?B - ?C)" by (auto elim:acyclic_subset)
+    from step_back_step [OF vtt]
+    have "step s (V th cs)" .
+    thus ?thesis
+    proof(cases)
+      assume "holding s th cs"
+      hence th_in: "th \<in> set (wq s cs)" and
+        eq_hd: "th = hd (wq s cs)" unfolding s_holding_def wq_def by auto
+      then obtain rest where
+        eq_wq: "wq s cs = th#rest"
+        by (cases "wq s cs", auto)
+      show ?thesis
+      proof(cases "rest = []")
+        case False
+        let ?th' = "hd (SOME q. distinct q \<and> set q = set rest)"
+        from eq_wq False have eq_D: "?D = {(Cs cs, Th ?th')}"
+          by (unfold next_th_def, auto)
+        let ?E = "(?A - ?B - ?C)"
+        have "(Th ?th', Cs cs) \<notin> ?E\<^sup>*"
+        proof
+          assume "(Th ?th', Cs cs) \<in> ?E\<^sup>*"
+          hence " (Th ?th', Cs cs) \<in> ?E\<^sup>+" by (simp add: rtrancl_eq_or_trancl)
+          from tranclD [OF this]
+          obtain x where th'_e: "(Th ?th', x) \<in> ?E" by blast
+          hence th_d: "(Th ?th', x) \<in> ?A" by simp
+          from RAG_target_th [OF this]
+          obtain cs' where eq_x: "x = Cs cs'" by auto
+          with th_d have "(Th ?th', Cs cs') \<in> ?A" by simp
+          hence wt_th': "waiting s ?th' cs'"
+            unfolding s_RAG_def s_waiting_def cs_waiting_def wq_def by simp
+          hence "cs' = cs"
+          proof(rule waiting_unique [OF vt])
+            from eq_wq wq_distinct[OF vt, of cs]
+            show "waiting s ?th' cs" 
+              apply (unfold s_waiting_def wq_def, auto)
+            proof -
+              assume hd_in: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> set rest"
+                and eq_wq: "wq_fun (schs s) cs = th # rest"
+              have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
+              proof(rule someI2)
+                from wq_distinct[OF vt, of cs] and eq_wq
+                show "distinct rest \<and> set rest = set rest" unfolding wq_def by auto
+              next
+                fix x assume "distinct x \<and> set x = set rest"
+                with False show "x \<noteq> []" by auto
+              qed
+              hence "hd (SOME q. distinct q \<and> set q = set rest) \<in> 
+                set (SOME q. distinct q \<and> set q = set rest)" by auto
+              moreover have "\<dots> = set rest" 
+              proof(rule someI2)
+                from wq_distinct[OF vt, of cs] and eq_wq
+                show "distinct rest \<and> set rest = set rest" unfolding wq_def by auto
+              next
+                show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
+              qed
+              moreover note hd_in
+              ultimately show "hd (SOME q. distinct q \<and> set q = set rest) = th" by auto
+            next
+              assume hd_in: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> set rest"
+                and eq_wq: "wq s cs = hd (SOME q. distinct q \<and> set q = set rest) # rest"
+              have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
+              proof(rule someI2)
+                from wq_distinct[OF vt, of cs] and eq_wq
+                show "distinct rest \<and> set rest = set rest" by auto
+              next
+                fix x assume "distinct x \<and> set x = set rest"
+                with False show "x \<noteq> []" by auto
+              qed
+              hence "hd (SOME q. distinct q \<and> set q = set rest) \<in> 
+                set (SOME q. distinct q \<and> set q = set rest)" by auto
+              moreover have "\<dots> = set rest" 
+              proof(rule someI2)
+                from wq_distinct[OF vt, of cs] and eq_wq
+                show "distinct rest \<and> set rest = set rest" by auto
+              next
+                show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
+              qed
+              moreover note hd_in
+              ultimately show False by auto
+            qed
+          qed
+          with th'_e eq_x have "(Th ?th', Cs cs) \<in> ?E" by simp
+          with False
+          show "False" by (auto simp: next_th_def eq_wq)
+        qed
+        with acyclic_insert[symmetric] and ac
+          and eq_de eq_D show ?thesis by auto
+      next
+        case True
+        with eq_wq
+        have eq_D: "?D = {}"
+          by (unfold next_th_def, auto)
+        with eq_de ac
+        show ?thesis by auto
+      qed 
+    qed
+  next
+    case (P th cs)
+    from P vt stp have vtt: "vt (P th cs#s)" by auto
+    from step_RAG_p [OF this] P
+    have "RAG (e # s) = 
+      (if wq s cs = [] then RAG s \<union> {(Cs cs, Th th)} else 
+      RAG s \<union> {(Th th, Cs cs)})" (is "?L = ?R")
+      by simp
+    moreover have "acyclic ?R"
+    proof(cases "wq s cs = []")
+      case True
+      hence eq_r: "?R =  RAG s \<union> {(Cs cs, Th th)}" by simp
+      have "(Th th, Cs cs) \<notin> (RAG s)\<^sup>*"
+      proof
+        assume "(Th th, Cs cs) \<in> (RAG s)\<^sup>*"
+        hence "(Th th, Cs cs) \<in> (RAG s)\<^sup>+" by (simp add: rtrancl_eq_or_trancl)
+        from tranclD2 [OF this]
+        obtain x where "(x, Cs cs) \<in> RAG s" by auto
+        with True show False by (auto simp:s_RAG_def cs_waiting_def)
+      qed
+      with acyclic_insert ih eq_r show ?thesis by auto
+    next
+      case False
+      hence eq_r: "?R =  RAG s \<union> {(Th th, Cs cs)}" by simp
+      have "(Cs cs, Th th) \<notin> (RAG s)\<^sup>*"
+      proof
+        assume "(Cs cs, Th th) \<in> (RAG s)\<^sup>*"
+        hence "(Cs cs, Th th) \<in> (RAG s)\<^sup>+" by (simp add: rtrancl_eq_or_trancl)
+        moreover from step_back_step [OF vtt] have "step s (P th cs)" .
+        ultimately show False
+        proof -
+          show " \<lbrakk>(Cs cs, Th th) \<in> (RAG s)\<^sup>+; step s (P th cs)\<rbrakk> \<Longrightarrow> False"
+            by (ind_cases "step s (P th cs)", simp)
+        qed
+      qed
+      with acyclic_insert ih eq_r show ?thesis by auto
+      qed
+      ultimately show ?thesis by simp
+    next
+      case (Set thread prio)
+      with ih
+      thm RAG_set_unchanged
+      show ?thesis by (simp add:RAG_set_unchanged)
+    qed
+  next
+    case vt_nil
+    show "acyclic (RAG ([]::state))"
+      by (auto simp: s_RAG_def cs_waiting_def 
+        cs_holding_def wq_def acyclic_def)
+qed
+
+
+lemma finite_RAG: 
+  fixes s
+  assumes vt: "vt s"
+  shows "finite (RAG s)"
+proof -
+  from vt show ?thesis
+  proof(induct)
+    case (vt_cons s e)
+    assume ih: "finite (RAG s)"
+      and stp: "step s e"
+      and vt: "vt s"
+    show ?case
+    proof(cases e)
+      case (Create th prio)
+      with ih
+      show ?thesis by (simp add:RAG_create_unchanged)
+    next
+      case (Exit th)
+      with ih show ?thesis by (simp add:RAG_exit_unchanged)
+    next
+      case (V th cs)
+      from V vt stp have vtt: "vt (V th cs#s)" by auto
+      from step_RAG_v [OF this]
+      have eq_de: "RAG (e # 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 = (?A - ?B - ?C) \<union> ?D") by (simp add:V)
+      moreover from ih have ac: "finite (?A - ?B - ?C)" by simp
+      moreover have "finite ?D"
+      proof -
+        have "?D = {} \<or> (\<exists> a. ?D = {a})" 
+          by (unfold next_th_def, auto)
+        thus ?thesis
+        proof
+          assume h: "?D = {}"
+          show ?thesis by (unfold h, simp)
+        next
+          assume "\<exists> a. ?D = {a}"
+          thus ?thesis
+            by (metis finite.simps)
+        qed
+      qed
+      ultimately show ?thesis by simp
+    next
+      case (P th cs)
+      from P vt stp have vtt: "vt (P th cs#s)" by auto
+      from step_RAG_p [OF this] P
+      have "RAG (e # s) = 
+              (if wq s cs = [] then RAG s \<union> {(Cs cs, Th th)} else 
+                                    RAG s \<union> {(Th th, Cs cs)})" (is "?L = ?R")
+        by simp
+      moreover have "finite ?R"
+      proof(cases "wq s cs = []")
+        case True
+        hence eq_r: "?R =  RAG s \<union> {(Cs cs, Th th)}" by simp
+        with True and ih show ?thesis by auto
+      next
+        case False
+        hence "?R = RAG s \<union> {(Th th, Cs cs)}" by simp
+        with False and ih show ?thesis by auto
+      qed
+      ultimately show ?thesis by auto
+    next
+      case (Set thread prio)
+      with ih
+      show ?thesis by (simp add:RAG_set_unchanged)
+    qed
+  next
+    case vt_nil
+    show "finite (RAG ([]::state))"
+      by (auto simp: s_RAG_def cs_waiting_def 
+                   cs_holding_def wq_def acyclic_def)
+  qed
+qed
+
+text {* Several useful lemmas *}
+
+lemma wf_dep_converse: 
+  fixes s
+  assumes vt: "vt s"
+  shows "wf ((RAG s)^-1)"
+proof(rule finite_acyclic_wf_converse)
+  from finite_RAG [OF vt]
+  show "finite (RAG s)" .
+next
+  from acyclic_RAG[OF vt]
+  show "acyclic (RAG s)" .
+qed
+
+lemma hd_np_in: "x \<in> set l \<Longrightarrow> hd l \<in> set l"
+by (induct l, auto)
+
+lemma th_chasing: "(Th th, Cs cs) \<in> RAG (s::state) \<Longrightarrow> \<exists> th'. (Cs cs, Th th') \<in> RAG s"
+  by (auto simp:s_RAG_def s_holding_def cs_holding_def cs_waiting_def wq_def dest:hd_np_in)
+
+lemma wq_threads: 
+  fixes s cs
+  assumes vt: "vt s"
+  and h: "th \<in> set (wq s cs)"
+  shows "th \<in> threads s"
+proof -
+ from vt and h show ?thesis
+  proof(induct arbitrary: th cs)
+    case (vt_cons s e)
+    assume ih: "\<And>th cs. th \<in> set (wq s cs) \<Longrightarrow> th \<in> threads s"
+      and stp: "step s e"
+      and vt: "vt s"
+      and h: "th \<in> set (wq (e # s) cs)"
+    show ?case
+    proof(cases e)
+      case (Create th' prio)
+      with ih h show ?thesis
+        by (auto simp:wq_def Let_def)
+    next
+      case (Exit th')
+      with stp ih h show ?thesis
+        apply (auto simp:wq_def Let_def)
+        apply (ind_cases "step s (Exit th')")
+        apply (auto simp:runing_def readys_def s_holding_def s_waiting_def holdents_def
+               s_RAG_def s_holding_def cs_holding_def)
+        done
+    next
+      case (V th' cs')
+      show ?thesis
+      proof(cases "cs' = cs")
+        case False
+        with h
+        show ?thesis
+          apply(unfold wq_def V, auto simp:Let_def V split:prod.splits, fold wq_def)
+          by (drule_tac ih, simp)
+      next
+        case True
+        from h
+        show ?thesis
+        proof(unfold V wq_def)
+          assume th_in: "th \<in> set (wq_fun (schs (V th' cs' # s)) cs)" (is "th \<in> set ?l")
+          show "th \<in> threads (V th' cs' # s)"
+          proof(cases "cs = cs'")
+            case False
+            hence "?l = wq_fun (schs s) cs" by (simp add:Let_def)
+            with th_in have " th \<in> set (wq s cs)" 
+              by (fold wq_def, simp)
+            from ih [OF this] show ?thesis by simp
+          next
+            case True
+            show ?thesis
+            proof(cases "wq_fun (schs s) cs'")
+              case Nil
+              with h V show ?thesis
+                apply (auto simp:wq_def Let_def split:if_splits)
+                by (fold wq_def, drule_tac ih, simp)
+            next
+              case (Cons a rest)
+              assume eq_wq: "wq_fun (schs s) cs' = a # rest"
+              with h V show ?thesis
+                apply (auto simp:Let_def wq_def split:if_splits)
+              proof -
+                assume th_in: "th \<in> set (SOME q. distinct q \<and> set q = set rest)"
+                have "set (SOME q. distinct q \<and> set q = set rest) = set rest" 
+                proof(rule someI2)
+                  from wq_distinct[OF vt, of cs'] and eq_wq[folded wq_def]
+                  show "distinct rest \<and> set rest = set rest" by auto
+                next
+                  show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest"
+                    by auto
+                qed
+                with eq_wq th_in have "th \<in> set (wq_fun (schs s) cs')" by auto
+                from ih[OF this[folded wq_def]] show "th \<in> threads s" .
+              next
+                assume th_in: "th \<in> set (wq_fun (schs s) cs)"
+                from ih[OF this[folded wq_def]]
+                show "th \<in> threads s" .
+              qed
+            qed
+          qed
+        qed
+      qed
+    next
+      case (P th' cs')
+      from h stp
+      show ?thesis
+        apply (unfold P wq_def)
+        apply (auto simp:Let_def split:if_splits, fold wq_def)
+        apply (auto intro:ih)
+        apply(ind_cases "step s (P th' cs')")
+        by (unfold runing_def readys_def, auto)
+    next
+      case (Set thread prio)
+      with ih h show ?thesis
+        by (auto simp:wq_def Let_def)
+    qed
+  next
+    case vt_nil
+    thus ?case by (auto simp:wq_def)
+  qed
+qed
+
+lemma range_in: "\<lbrakk>vt s; (Th th) \<in> Range (RAG (s::state))\<rbrakk> \<Longrightarrow> th \<in> threads s"
+  apply(unfold s_RAG_def cs_waiting_def cs_holding_def)
+  by (auto intro:wq_threads)
+
+lemma readys_v_eq:
+  fixes th thread cs rest
+  assumes vt: "vt s"
+  and neq_th: "th \<noteq> thread"
+  and eq_wq: "wq s cs = thread#rest"
+  and not_in: "th \<notin>  set rest"
+  shows "(th \<in> readys (V thread cs#s)) = (th \<in> readys s)"
+proof -
+  from assms show ?thesis
+    apply (auto simp:readys_def)
+    apply(simp add:s_waiting_def[folded wq_def])
+    apply (erule_tac x = csa in allE)
+    apply (simp add:s_waiting_def wq_def Let_def split:if_splits)
+    apply (case_tac "csa = cs", simp)
+    apply (erule_tac x = cs in allE)
+    apply(auto simp add: s_waiting_def[folded wq_def] Let_def split: list.splits)
+    apply(auto simp add: wq_def)
+    apply (auto simp:s_waiting_def wq_def Let_def split:list.splits)
+    proof -
+       assume th_nin: "th \<notin> set rest"
+        and th_in: "th \<in> set (SOME q. distinct q \<and> set q = set rest)"
+        and eq_wq: "wq_fun (schs s) cs = thread # rest"
+      have "set (SOME q. distinct q \<and> set q = set rest) = set rest"
+      proof(rule someI2)
+        from wq_distinct[OF vt, of cs, unfolded wq_def] and eq_wq[unfolded wq_def]
+        show "distinct rest \<and> set rest = set rest" by auto
+      next
+        show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
+      qed
+      with th_nin th_in show False by auto
+    qed
+qed
+
+text {* \noindent
+  The following lemmas shows that: starting from any node in @{text "RAG"}, 
+  by chasing out-going edges, it is always possible to reach a node representing a ready
+  thread. In this lemma, it is the @{text "th'"}.
+*}
+
+lemma chain_building:
+  assumes vt: "vt s"
+  shows "node \<in> Domain (RAG s) \<longrightarrow> (\<exists> th'. th' \<in> readys s \<and> (node, Th th') \<in> (RAG s)^+)"
+proof -
+  from wf_dep_converse [OF vt]
+  have h: "wf ((RAG s)\<inverse>)" .
+  show ?thesis
+  proof(induct rule:wf_induct [OF h])
+    fix x
+    assume ih [rule_format]: 
+      "\<forall>y. (y, x) \<in> (RAG s)\<inverse> \<longrightarrow> 
+           y \<in> Domain (RAG s) \<longrightarrow> (\<exists>th'. th' \<in> readys s \<and> (y, Th th') \<in> (RAG s)\<^sup>+)"
+    show "x \<in> Domain (RAG s) \<longrightarrow> (\<exists>th'. th' \<in> readys s \<and> (x, Th th') \<in> (RAG s)\<^sup>+)"
+    proof
+      assume x_d: "x \<in> Domain (RAG s)"
+      show "\<exists>th'. th' \<in> readys s \<and> (x, Th th') \<in> (RAG s)\<^sup>+"
+      proof(cases x)
+        case (Th th)
+        from x_d Th obtain cs where x_in: "(Th th, Cs cs) \<in> RAG s" by (auto simp:s_RAG_def)
+        with Th have x_in_r: "(Cs cs, x) \<in> (RAG s)^-1" by simp
+        from th_chasing [OF x_in] obtain th' where "(Cs cs, Th th') \<in> RAG s" by blast
+        hence "Cs cs \<in> Domain (RAG s)" by auto
+        from ih [OF x_in_r this] obtain th'
+          where th'_ready: " th' \<in> readys s" and cs_in: "(Cs cs, Th th') \<in> (RAG s)\<^sup>+" by auto
+        have "(x, Th th') \<in> (RAG s)\<^sup>+" using Th x_in cs_in by auto
+        with th'_ready show ?thesis by auto
+      next
+        case (Cs cs)
+        from x_d Cs obtain th' where th'_d: "(Th th', x) \<in> (RAG s)^-1" by (auto simp:s_RAG_def)
+        show ?thesis
+        proof(cases "th' \<in> readys s")
+          case True
+          from True and th'_d show ?thesis by auto
+        next
+          case False
+          from th'_d and range_in [OF vt] have "th' \<in> threads s" by auto
+          with False have "Th th' \<in> Domain (RAG s)" 
+            by (auto simp:readys_def wq_def s_waiting_def s_RAG_def cs_waiting_def Domain_def)
+          from ih [OF th'_d this]
+          obtain th'' where 
+            th''_r: "th'' \<in> readys s" and 
+            th''_in: "(Th th', Th th'') \<in> (RAG s)\<^sup>+" by auto
+          from th'_d and th''_in 
+          have "(x, Th th'') \<in> (RAG s)\<^sup>+" by auto
+          with th''_r show ?thesis by auto
+        qed
+      qed
+    qed
+  qed
+qed
+
+text {* \noindent
+  The following is just an instance of @{text "chain_building"}.
+*}
+lemma th_chain_to_ready:
+  fixes s th
+  assumes vt: "vt s"
+  and 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)^+)"
+proof(cases "th \<in> readys s")
+  case True
+  thus ?thesis by auto
+next
+  case False
+  from False and th_in have "Th th \<in> Domain (RAG s)" 
+    by (auto simp:readys_def s_waiting_def s_RAG_def wq_def cs_waiting_def Domain_def)
+  from chain_building [rule_format, OF vt this]
+  show ?thesis by auto
+qed
+
+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 holding_unique: "\<lbrakk>holding (s::state) th1 cs; holding s th2 cs\<rbrakk> \<Longrightarrow> th1 = th2"
+  by (unfold s_holding_def cs_holding_def, auto)
+
+lemma unique_RAG: "\<lbrakk>vt s; (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 holding_unique)
+
+lemma trancl_split: "(a, b) \<in> r^+ \<Longrightarrow> \<exists> c. (a, c) \<in> r"
+by (induct rule:trancl_induct, auto)
+
+lemma dchain_unique:
+  assumes vt: "vt s"
+  and th1_d: "(n, Th th1) \<in> (RAG s)^+"
+  and th1_r: "th1 \<in> readys s"
+  and th2_d: "(n, Th th2) \<in> (RAG s)^+"
+  and th2_r: "th2 \<in> readys s"
+  shows "th1 = th2"
+proof -
+  { assume neq: "th1 \<noteq> th2"
+    hence "Th th1 \<noteq> Th th2" by simp
+    from unique_chain [OF _ th1_d th2_d this] and unique_RAG [OF vt]
+    have "(Th th1, Th th2) \<in> (RAG s)\<^sup>+ \<or> (Th th2, Th th1) \<in> (RAG s)\<^sup>+" by auto
+    hence "False"
+    proof
+      assume "(Th th1, Th th2) \<in> (RAG s)\<^sup>+"
+      from trancl_split [OF this]
+      obtain n where dd: "(Th th1, n) \<in> RAG s" by auto
+      then obtain cs where eq_n: "n = Cs cs"
+        by (auto simp:s_RAG_def s_holding_def cs_holding_def cs_waiting_def wq_def dest:hd_np_in)
+      from dd eq_n have "th1 \<notin> readys s"
+        by (auto simp:readys_def s_RAG_def wq_def s_waiting_def cs_waiting_def)
+      with th1_r show ?thesis by auto
+    next
+      assume "(Th th2, Th th1) \<in> (RAG s)\<^sup>+"
+      from trancl_split [OF this]
+      obtain n where dd: "(Th th2, n) \<in> RAG s" by auto
+      then obtain cs where eq_n: "n = Cs cs"
+        by (auto simp:s_RAG_def s_holding_def cs_holding_def cs_waiting_def wq_def dest:hd_np_in)
+      from dd eq_n have "th2 \<notin> readys s"
+        by (auto simp:readys_def wq_def s_RAG_def s_waiting_def cs_waiting_def)
+      with th2_r show ?thesis by auto
+    qed
+  } thus ?thesis by auto
+qed
+             
+
+lemma step_holdents_p_add:
+  fixes th cs s
+  assumes vt: "vt (P th cs#s)"
+  and "wq s cs = []"
+  shows "holdents (P th cs#s) th = holdents s th \<union> {cs}"
+proof -
+  from assms show ?thesis
+  unfolding  holdents_test step_RAG_p[OF vt] by (auto)
+qed
+
+lemma step_holdents_p_eq:
+  fixes th cs s
+  assumes vt: "vt (P th cs#s)"
+  and "wq s cs \<noteq> []"
+  shows "holdents (P th cs#s) th = holdents s th"
+proof -
+  from assms show ?thesis
+  unfolding  holdents_test step_RAG_p[OF vt] by auto
+qed
+
+
+lemma finite_holding:
+  fixes s th cs
+  assumes vt: "vt s"
+  shows "finite (holdents s th)"
+proof -
+  let ?F = "\<lambda> (x, y). the_cs x"
+  from finite_RAG [OF vt]
+  have "finite (RAG s)" .
+  hence "finite (?F `(RAG s))" by simp
+  moreover have "{cs . (Cs cs, Th th) \<in> RAG s} \<subseteq> \<dots>" 
+  proof -
+    { have h: "\<And> a A f. a \<in> A \<Longrightarrow> f a \<in> f ` A" by auto
+      fix x assume "(Cs x, Th th) \<in> RAG s"
+      hence "?F (Cs x, Th th) \<in> ?F `(RAG s)" by (rule h)
+      moreover have "?F (Cs x, Th th) = x" by simp
+      ultimately have "x \<in> (\<lambda>(x, y). the_cs x) ` RAG s" by simp 
+    } thus ?thesis by auto
+  qed
+  ultimately show ?thesis by (unfold holdents_test, auto intro:finite_subset)
+qed
+
+lemma cntCS_v_dec: 
+  fixes s thread cs
+  assumes vtv: "vt (V thread cs#s)"
+  shows "(cntCS (V thread cs#s) thread + 1) = cntCS s thread"
+proof -
+  from step_back_step[OF vtv]
+  have cs_in: "cs \<in> holdents s thread" 
+    apply (cases, unfold holdents_test s_RAG_def, simp)
+    by (unfold cs_holding_def s_holding_def wq_def, auto)
+  moreover have cs_not_in: 
+    "(holdents (V thread cs#s) thread) = holdents s thread - {cs}"
+    apply (insert wq_distinct[OF step_back_vt[OF vtv], of cs])
+    apply (unfold holdents_test, unfold step_RAG_v[OF vtv],
+            auto simp:next_th_def)
+  proof -
+    fix rest
+    assume dst: "distinct (rest::thread list)"
+      and ne: "rest \<noteq> []"
+    and hd_ni: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> set rest"
+    moreover have "set (SOME q. distinct q \<and> set q = set rest) = set rest"
+    proof(rule someI2)
+      from dst show "distinct rest \<and> set rest = set rest" by auto
+    next
+      show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
+    qed
+    ultimately have "hd (SOME q. distinct q \<and> set q = set rest) \<notin> 
+                     set (SOME q. distinct q \<and> set q = set rest)" by simp
+    moreover have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
+    proof(rule someI2)
+      from dst show "distinct rest \<and> set rest = set rest" by auto
+    next
+      fix x assume " distinct x \<and> set x = set rest" with ne
+      show "x \<noteq> []" by auto
+    qed
+    ultimately 
+    show "(Cs cs, Th (hd (SOME q. distinct q \<and> set q = set rest))) \<in> RAG s"
+      by auto
+  next
+    fix rest
+    assume dst: "distinct (rest::thread list)"
+      and ne: "rest \<noteq> []"
+    and hd_ni: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> set rest"
+    moreover have "set (SOME q. distinct q \<and> set q = set rest) = set rest"
+    proof(rule someI2)
+      from dst show "distinct rest \<and> set rest = set rest" by auto
+    next
+      show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
+    qed
+    ultimately have "hd (SOME q. distinct q \<and> set q = set rest) \<notin> 
+                     set (SOME q. distinct q \<and> set q = set rest)" by simp
+    moreover have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
+    proof(rule someI2)
+      from dst show "distinct rest \<and> set rest = set rest" by auto
+    next
+      fix x assume " distinct x \<and> set x = set rest" with ne
+      show "x \<noteq> []" by auto
+    qed
+    ultimately show "False" by auto 
+  qed
+  ultimately 
+  have "holdents s thread = insert cs (holdents (V thread cs#s) thread)"
+    by auto
+  moreover have "card \<dots> = 
+                    Suc (card ((holdents (V thread cs#s) thread) - {cs}))"
+  proof(rule card_insert)
+    from finite_holding [OF vtv]
+    show " finite (holdents (V thread cs # s) thread)" .
+  qed
+  moreover from cs_not_in 
+  have "cs \<notin> (holdents (V thread cs#s) thread)" by auto
+  ultimately show ?thesis by (simp add:cntCS_def)
+qed 
+
+text {* (* ??? *) \noindent
+  The relationship between @{text "cntP"}, @{text "cntV"} and @{text "cntCS"} 
+  of one particular thread. 
+*} 
+
+lemma cnp_cnv_cncs:
+  fixes s th
+  assumes vt: "vt s"
+  shows "cntP s th = cntV s th + (if (th \<in> readys s \<or> th \<notin> threads s) 
+                                       then cntCS s th else cntCS s th + 1)"
+proof -
+  from vt show ?thesis
+  proof(induct arbitrary:th)
+    case (vt_cons s e)
+    assume vt: "vt s"
+    and ih: "\<And>th. cntP s th  = cntV s th +
+               (if (th \<in> readys s \<or> th \<notin> threads s) then cntCS s th else cntCS s th + 1)"
+    and stp: "step s e"
+    from stp show ?case
+    proof(cases)
+      case (thread_create thread prio)
+      assume eq_e: "e = Create thread prio"
+        and not_in: "thread \<notin> threads s"
+      show ?thesis
+      proof -
+        { fix cs 
+          assume "thread \<in> set (wq s cs)"
+          from wq_threads [OF vt this] have "thread \<in> threads s" .
+          with not_in have "False" by simp
+        } with eq_e have eq_readys: "readys (e#s) = readys s \<union> {thread}"
+          by (auto simp:readys_def threads.simps s_waiting_def 
+            wq_def cs_waiting_def Let_def)
+        from eq_e have eq_cnp: "cntP (e#s) th = cntP s th" by (simp add:cntP_def count_def)
+        from eq_e have eq_cnv: "cntV (e#s) th = cntV s th" by (simp add:cntV_def count_def)
+        have eq_cncs: "cntCS (e#s) th = cntCS s th"
+          unfolding cntCS_def holdents_test
+          by (simp add:RAG_create_unchanged eq_e)
+        { assume "th \<noteq> thread"
+          with eq_readys eq_e
+          have "(th \<in> readys (e # s) \<or> th \<notin> threads (e # s)) = 
+                      (th \<in> readys (s) \<or> th \<notin> threads (s))" 
+            by (simp add:threads.simps)
+          with eq_cnp eq_cnv eq_cncs ih not_in
+          have ?thesis by simp
+        } moreover {
+          assume eq_th: "th = thread"
+          with not_in ih have " cntP s th  = cntV s th + cntCS s th" by simp
+          moreover from eq_th and eq_readys have "th \<in> readys (e#s)" by simp
+          moreover note eq_cnp eq_cnv eq_cncs
+          ultimately have ?thesis by auto
+        } ultimately show ?thesis by blast
+      qed
+    next
+      case (thread_exit thread)
+      assume eq_e: "e = Exit thread" 
+      and is_runing: "thread \<in> runing s"
+      and no_hold: "holdents s thread = {}"
+      from eq_e have eq_cnp: "cntP (e#s) th = cntP s th" by (simp add:cntP_def count_def)
+      from eq_e have eq_cnv: "cntV (e#s) th = cntV s th" by (simp add:cntV_def count_def)
+      have eq_cncs: "cntCS (e#s) th = cntCS s th"
+        unfolding cntCS_def holdents_test
+        by (simp add:RAG_exit_unchanged eq_e)
+      { assume "th \<noteq> thread"
+        with eq_e
+        have "(th \<in> readys (e # s) \<or> th \<notin> threads (e # s)) = 
+          (th \<in> readys (s) \<or> th \<notin> threads (s))" 
+          apply (simp add:threads.simps readys_def)
+          apply (subst s_waiting_def)
+          apply (simp add:Let_def)
+          apply (subst s_waiting_def, simp)
+          done
+        with eq_cnp eq_cnv eq_cncs ih
+        have ?thesis by simp
+      } moreover {
+        assume eq_th: "th = thread"
+        with ih is_runing have " cntP s th = cntV s th + cntCS s th" 
+          by (simp add:runing_def)
+        moreover from eq_th eq_e have "th \<notin> threads (e#s)"
+          by simp
+        moreover note eq_cnp eq_cnv eq_cncs
+        ultimately have ?thesis by auto
+      } ultimately show ?thesis by blast
+    next
+      case (thread_P thread cs)
+      assume eq_e: "e = P thread cs"
+        and is_runing: "thread \<in> runing s"
+        and no_dep: "(Cs cs, Th thread) \<notin> (RAG s)\<^sup>+"
+      from thread_P vt stp ih  have vtp: "vt (P thread cs#s)" by auto
+      show ?thesis 
+      proof -
+        { have hh: "\<And> A B C. (B = C) \<Longrightarrow> (A \<and> B) = (A \<and> C)" by blast
+          assume neq_th: "th \<noteq> thread"
+          with eq_e
+          have eq_readys: "(th \<in> readys (e#s)) = (th \<in> readys (s))"
+            apply (simp add:readys_def s_waiting_def wq_def Let_def)
+            apply (rule_tac hh)
+             apply (intro iffI allI, clarify)
+            apply (erule_tac x = csa in allE, auto)
+            apply (subgoal_tac "wq_fun (schs s) cs \<noteq> []", auto)
+            apply (erule_tac x = cs in allE, auto)
+            by (case_tac "(wq_fun (schs s) cs)", auto)
+          moreover from neq_th eq_e have "cntCS (e # s) th = cntCS s th"
+            apply (simp add:cntCS_def holdents_test)
+            by (unfold  step_RAG_p [OF vtp], auto)
+          moreover from eq_e neq_th have "cntP (e # s) th = cntP s th"
+            by (simp add:cntP_def count_def)
+          moreover from eq_e neq_th have "cntV (e#s) th = cntV s th"
+            by (simp add:cntV_def count_def)
+          moreover from eq_e neq_th have "threads (e#s) = threads s" by simp
+          moreover note ih [of th] 
+          ultimately have ?thesis by simp
+        } moreover {
+          assume eq_th: "th = thread"
+          have ?thesis
+          proof -
+            from eq_e eq_th have eq_cnp: "cntP (e # s) th  = 1 + (cntP s th)" 
+              by (simp add:cntP_def count_def)
+            from eq_e eq_th have eq_cnv: "cntV (e#s) th = cntV s th"
+              by (simp add:cntV_def count_def)
+            show ?thesis
+            proof (cases "wq s cs = []")
+              case True
+              with is_runing
+              have "th \<in> readys (e#s)"
+                apply (unfold eq_e wq_def, unfold readys_def s_RAG_def)
+                apply (simp add: wq_def[symmetric] runing_def eq_th s_waiting_def)
+                by (auto simp:readys_def wq_def Let_def s_waiting_def wq_def)
+              moreover have "cntCS (e # s) th = 1 + cntCS s th"
+              proof -
+                have "card {csa. csa = cs \<or> (Cs csa, Th thread) \<in> RAG s} =
+                  Suc (card {cs. (Cs cs, Th thread) \<in> RAG s})" (is "card ?L = Suc (card ?R)")
+                proof -
+                  have "?L = insert cs ?R" by auto
+                  moreover have "card \<dots> = Suc (card (?R - {cs}))" 
+                  proof(rule card_insert)
+                    from finite_holding [OF vt, of thread]
+                    show " finite {cs. (Cs cs, Th thread) \<in> RAG s}"
+                      by (unfold holdents_test, simp)
+                  qed
+                  moreover have "?R - {cs} = ?R"
+                  proof -
+                    have "cs \<notin> ?R"
+                    proof
+                      assume "cs \<in> {cs. (Cs cs, Th thread) \<in> RAG s}"
+                      with no_dep show False by auto
+                    qed
+                    thus ?thesis by auto
+                  qed
+                  ultimately show ?thesis by auto
+                qed
+                thus ?thesis
+                  apply (unfold eq_e eq_th cntCS_def)
+                  apply (simp add: holdents_test)
+                  by (unfold step_RAG_p [OF vtp], auto simp:True)
+              qed
+              moreover from is_runing have "th \<in> readys s"
+                by (simp add:runing_def eq_th)
+              moreover note eq_cnp eq_cnv ih [of th]
+              ultimately show ?thesis by auto
+            next
+              case False
+              have eq_wq: "wq (e#s) cs = wq s cs @ [th]"
+                    by (unfold eq_th eq_e wq_def, auto simp:Let_def)
+              have "th \<notin> readys (e#s)"
+              proof
+                assume "th \<in> readys (e#s)"
+                hence "\<forall>cs. \<not> waiting (e # s) th cs" by (simp add:readys_def)
+                from this[rule_format, of cs] have " \<not> waiting (e # s) th cs" .
+                hence "th \<in> set (wq (e#s) cs) \<Longrightarrow> th = hd (wq (e#s) cs)" 
+                  by (simp add:s_waiting_def wq_def)
+                moreover from eq_wq have "th \<in> set (wq (e#s) cs)" by auto
+                ultimately have "th = hd (wq (e#s) cs)" by blast
+                with eq_wq have "th = hd (wq s cs @ [th])" by simp
+                hence "th = hd (wq s cs)" using False by auto
+                with False eq_wq wq_distinct [OF vtp, of cs]
+                show False by (fold eq_e, auto)
+              qed
+              moreover from is_runing have "th \<in> threads (e#s)" 
+                by (unfold eq_e, auto simp:runing_def readys_def eq_th)
+              moreover have "cntCS (e # s) th = cntCS s th"
+                apply (unfold cntCS_def holdents_test eq_e step_RAG_p[OF vtp])
+                by (auto simp:False)
+              moreover note eq_cnp eq_cnv ih[of th]
+              moreover from is_runing have "th \<in> readys s"
+                by (simp add:runing_def eq_th)
+              ultimately show ?thesis by auto
+            qed
+          qed
+        } ultimately show ?thesis by blast
+      qed
+    next
+      case (thread_V thread cs)
+      from assms vt stp ih thread_V have vtv: "vt (V thread cs # s)" by auto
+      assume eq_e: "e = V thread cs"
+        and is_runing: "thread \<in> runing s"
+        and hold: "holding s thread cs"
+      from hold obtain rest 
+        where eq_wq: "wq s cs = thread # rest"
+        by (case_tac "wq s cs", auto simp: wq_def s_holding_def)
+      have eq_threads: "threads (e#s) = threads s" by (simp add: eq_e)
+      have eq_set: "set (SOME q. distinct q \<and> set q = set rest) = set rest"
+      proof(rule someI2)
+        from wq_distinct[OF step_back_vt[OF vtv], of cs] and eq_wq
+        show "distinct rest \<and> set rest = set rest" by auto
+      next
+        show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest"
+          by auto
+      qed
+      show ?thesis
+      proof -
+        { assume eq_th: "th = thread"
+          from eq_th have eq_cnp: "cntP (e # s) th = cntP s th"
+            by (unfold eq_e, simp add:cntP_def count_def)
+          moreover from eq_th have eq_cnv: "cntV (e#s) th = 1 + cntV s th"
+            by (unfold eq_e, simp add:cntV_def count_def)
+          moreover from cntCS_v_dec [OF vtv] 
+          have "cntCS (e # s) thread + 1 = cntCS s thread"
+            by (simp add:eq_e)
+          moreover from is_runing have rd_before: "thread \<in> readys s"
+            by (unfold runing_def, simp)
+          moreover have "thread \<in> readys (e # s)"
+          proof -
+            from is_runing
+            have "thread \<in> threads (e#s)" 
+              by (unfold eq_e, auto simp:runing_def readys_def)
+            moreover have "\<forall> cs1. \<not> waiting (e#s) thread cs1"
+            proof
+              fix cs1
+              { assume eq_cs: "cs1 = cs" 
+                have "\<not> waiting (e # s) thread cs1"
+                proof -
+                  from eq_wq
+                  have "thread \<notin> set (wq (e#s) cs1)"
+                    apply(unfold eq_e wq_def eq_cs s_holding_def)
+                    apply (auto simp:Let_def)
+                  proof -
+                    assume "thread \<in> set (SOME q. distinct q \<and> set q = set rest)"
+                    with eq_set have "thread \<in> set rest" by simp
+                    with wq_distinct[OF step_back_vt[OF vtv], of cs]
+                    and eq_wq show False by auto
+                  qed
+                  thus ?thesis by (simp add:wq_def s_waiting_def)
+                qed
+              } moreover {
+                assume neq_cs: "cs1 \<noteq> cs"
+                  have "\<not> waiting (e # s) thread cs1" 
+                  proof -
+                    from wq_v_neq [OF neq_cs[symmetric]]
+                    have "wq (V thread cs # s) cs1 = wq s cs1" .
+                    moreover have "\<not> waiting s thread cs1" 
+                    proof -
+                      from runing_ready and is_runing
+                      have "thread \<in> readys s" by auto
+                      thus ?thesis by (simp add:readys_def)
+                    qed
+                    ultimately show ?thesis 
+                      by (auto simp:wq_def s_waiting_def eq_e)
+                  qed
+              } ultimately show "\<not> waiting (e # s) thread cs1" by blast
+            qed
+            ultimately show ?thesis by (simp add:readys_def)
+          qed
+          moreover note eq_th ih
+          ultimately have ?thesis by auto
+        } moreover {
+          assume neq_th: "th \<noteq> thread"
+          from neq_th eq_e have eq_cnp: "cntP (e # s) th = cntP s th" 
+            by (simp add:cntP_def count_def)
+          from neq_th eq_e have eq_cnv: "cntV (e # s) th = cntV s th" 
+            by (simp add:cntV_def count_def)
+          have ?thesis
+          proof(cases "th \<in> set rest")
+            case False
+            have "(th \<in> readys (e # s)) = (th \<in> readys s)"
+              apply (insert step_back_vt[OF vtv])
+              by (unfold eq_e, rule readys_v_eq [OF _ neq_th eq_wq False], auto)
+            moreover have "cntCS (e#s) th = cntCS s th"
+              apply (insert neq_th, unfold eq_e cntCS_def holdents_test step_RAG_v[OF vtv], auto)
+              proof -
+                have "{csa. (Cs csa, Th th) \<in> RAG s \<or> csa = cs \<and> next_th s thread cs th} =
+                      {cs. (Cs cs, Th th) \<in> RAG s}"
+                proof -
+                  from False eq_wq
+                  have " next_th s thread cs th \<Longrightarrow> (Cs cs, Th th) \<in> RAG s"
+                    apply (unfold next_th_def, auto)
+                  proof -
+                    assume ne: "rest \<noteq> []"
+                      and ni: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> set rest"
+                      and eq_wq: "wq s cs = thread # rest"
+                    from eq_set ni have "hd (SOME q. distinct q \<and> set q = set rest) \<notin> 
+                                  set (SOME q. distinct q \<and> set q = set rest)
+                                  " by simp
+                    moreover have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
+                    proof(rule someI2)
+                      from wq_distinct[OF step_back_vt[OF vtv], of cs] and eq_wq
+                      show "distinct rest \<and> set rest = set rest" by auto
+                    next
+                      fix x assume "distinct x \<and> set x = set rest"
+                      with ne show "x \<noteq> []" by auto
+                    qed
+                    ultimately show 
+                      "(Cs cs, Th (hd (SOME q. distinct q \<and> set q = set rest))) \<in> RAG s"
+                      by auto
+                  qed    
+                  thus ?thesis by auto
+                qed
+                thus "card {csa. (Cs csa, Th th) \<in> RAG s \<or> csa = cs \<and> next_th s thread cs th} =
+                             card {cs. (Cs cs, Th th) \<in> RAG s}" by simp 
+              qed
+            moreover note ih eq_cnp eq_cnv eq_threads
+            ultimately show ?thesis by auto
+          next
+            case True
+            assume th_in: "th \<in> set rest"
+            show ?thesis
+            proof(cases "next_th s thread cs th")
+              case False
+              with eq_wq and th_in have 
+                neq_hd: "th \<noteq> hd (SOME q. distinct q \<and> set q = set rest)" (is "th \<noteq> hd ?rest")
+                by (auto simp:next_th_def)
+              have "(th \<in> readys (e # s)) = (th \<in> readys s)"
+              proof -
+                from eq_wq and th_in
+                have "\<not> th \<in> readys s"
+                  apply (auto simp:readys_def s_waiting_def)
+                  apply (rule_tac x = cs in exI, auto)
+                  by (insert wq_distinct[OF step_back_vt[OF vtv], of cs], auto simp add: wq_def)
+                moreover 
+                from eq_wq and th_in and neq_hd
+                have "\<not> (th \<in> readys (e # s))"
+                  apply (auto simp:readys_def s_waiting_def eq_e wq_def Let_def split:list.splits)
+                  by (rule_tac x = cs in exI, auto simp:eq_set)
+                ultimately show ?thesis by auto
+              qed
+              moreover have "cntCS (e#s) th = cntCS s th" 
+              proof -
+                from eq_wq and  th_in and neq_hd
+                have "(holdents (e # s) th) = (holdents s th)"
+                  apply (unfold eq_e step_RAG_v[OF vtv], 
+                         auto simp:next_th_def eq_set s_RAG_def holdents_test wq_def
+                                   Let_def cs_holding_def)
+                  by (insert wq_distinct[OF step_back_vt[OF vtv], of cs], auto simp:wq_def)
+                thus ?thesis by (simp add:cntCS_def)
+              qed
+              moreover note ih eq_cnp eq_cnv eq_threads
+              ultimately show ?thesis by auto
+            next
+              case True
+              let ?rest = " (SOME q. distinct q \<and> set q = set rest)"
+              let ?t = "hd ?rest"
+              from True eq_wq th_in neq_th
+              have "th \<in> readys (e # s)"
+                apply (auto simp:eq_e readys_def s_waiting_def wq_def
+                        Let_def next_th_def)
+              proof -
+                assume eq_wq: "wq_fun (schs s) cs = thread # rest"
+                  and t_in: "?t \<in> set rest"
+                show "?t \<in> threads s"
+                proof(rule wq_threads[OF step_back_vt[OF vtv]])
+                  from eq_wq and t_in
+                  show "?t \<in> set (wq s cs)" by (auto simp:wq_def)
+                qed
+              next
+                fix csa
+                assume eq_wq: "wq_fun (schs s) cs = thread # rest"
+                  and t_in: "?t \<in> set rest"
+                  and neq_cs: "csa \<noteq> cs"
+                  and t_in': "?t \<in>  set (wq_fun (schs s) csa)"
+                show "?t = hd (wq_fun (schs s) csa)"
+                proof -
+                  { assume neq_hd': "?t \<noteq> hd (wq_fun (schs s) csa)"
+                    from wq_distinct[OF step_back_vt[OF vtv], of cs] and 
+                    eq_wq[folded wq_def] and t_in eq_wq
+                    have "?t \<noteq> thread" by auto
+                    with eq_wq and t_in
+                    have w1: "waiting s ?t cs"
+                      by (auto simp:s_waiting_def wq_def)
+                    from t_in' neq_hd'
+                    have w2: "waiting s ?t csa"
+                      by (auto simp:s_waiting_def wq_def)
+                    from waiting_unique[OF step_back_vt[OF vtv] w1 w2]
+                    and neq_cs have "False" by auto
+                  } thus ?thesis by auto
+                qed
+              qed
+              moreover have "cntP s th = cntV s th + cntCS s th + 1"
+              proof -
+                have "th \<notin> readys s" 
+                proof -
+                  from True eq_wq neq_th th_in
+                  show ?thesis
+                    apply (unfold readys_def s_waiting_def, auto)
+                    by (rule_tac x = cs in exI, auto simp add: wq_def)
+                qed
+                moreover have "th \<in> threads s"
+                proof -
+                  from th_in eq_wq
+                  have "th \<in> set (wq s cs)" by simp
+                  from wq_threads [OF step_back_vt[OF vtv] this] 
+                  show ?thesis .
+                qed
+                ultimately show ?thesis using ih by auto
+              qed
+              moreover from True neq_th have "cntCS (e # s) th = 1 + cntCS s th"
+                apply (unfold cntCS_def holdents_test eq_e step_RAG_v[OF vtv], auto)
+              proof -
+                show "card {csa. (Cs csa, Th th) \<in> RAG s \<or> csa = cs} =
+                               Suc (card {cs. (Cs cs, Th th) \<in> RAG s})"
+                  (is "card ?A = Suc (card ?B)")
+                proof -
+                  have "?A = insert cs ?B" by auto
+                  hence "card ?A = card (insert cs ?B)" by simp
+                  also have "\<dots> = Suc (card ?B)"
+                  proof(rule card_insert_disjoint)
+                    have "?B \<subseteq> ((\<lambda> (x, y). the_cs x) ` RAG s)" 
+                      apply (auto simp:image_def)
+                      by (rule_tac x = "(Cs x, Th th)" in bexI, auto)
+                    with finite_RAG[OF step_back_vt[OF vtv]]
+                    show "finite {cs. (Cs cs, Th th) \<in> RAG s}" by (auto intro:finite_subset)
+                  next
+                    show "cs \<notin> {cs. (Cs cs, Th th) \<in> RAG s}"
+                    proof
+                      assume "cs \<in> {cs. (Cs cs, Th th) \<in> RAG s}"
+                      hence "(Cs cs, Th th) \<in> RAG s" by simp
+                      with True neq_th eq_wq show False
+                        by (auto simp:next_th_def s_RAG_def cs_holding_def)
+                    qed
+                  qed
+                  finally show ?thesis .
+                qed
+              qed
+              moreover note eq_cnp eq_cnv
+              ultimately show ?thesis by simp
+            qed
+          qed
+        } ultimately show ?thesis by blast
+      qed
+    next
+      case (thread_set thread prio)
+      assume eq_e: "e = Set thread prio"
+        and is_runing: "thread \<in> runing s"
+      show ?thesis
+      proof -
+        from eq_e have eq_cnp: "cntP (e#s) th = cntP s th" by (simp add:cntP_def count_def)
+        from eq_e have eq_cnv: "cntV (e#s) th = cntV s th" by (simp add:cntV_def count_def)
+        have eq_cncs: "cntCS (e#s) th = cntCS s th"
+          unfolding cntCS_def holdents_test
+          by (simp add:RAG_set_unchanged eq_e)
+        from eq_e have eq_readys: "readys (e#s) = readys s" 
+          by (simp add:readys_def cs_waiting_def s_waiting_def wq_def,
+                  auto simp:Let_def)
+        { assume "th \<noteq> thread"
+          with eq_readys eq_e
+          have "(th \<in> readys (e # s) \<or> th \<notin> threads (e # s)) = 
+                      (th \<in> readys (s) \<or> th \<notin> threads (s))" 
+            by (simp add:threads.simps)
+          with eq_cnp eq_cnv eq_cncs ih is_runing
+          have ?thesis by simp
+        } moreover {
+          assume eq_th: "th = thread"
+          with is_runing ih have " cntP s th  = cntV s th + cntCS s th" 
+            by (unfold runing_def, auto)
+          moreover from eq_th and eq_readys is_runing have "th \<in> readys (e#s)"
+            by (simp add:runing_def)
+          moreover note eq_cnp eq_cnv eq_cncs
+          ultimately have ?thesis by auto
+        } ultimately show ?thesis by blast
+      qed   
+    qed
+  next
+    case vt_nil
+    show ?case 
+      by (unfold cntP_def cntV_def cntCS_def, 
+        auto simp:count_def holdents_test s_RAG_def wq_def cs_holding_def)
+  qed
+qed
+
+lemma not_thread_cncs:
+  fixes th s
+  assumes vt: "vt s"
+  and not_in: "th \<notin> threads s" 
+  shows "cntCS s th = 0"
+proof -
+  from vt not_in show ?thesis
+  proof(induct arbitrary:th)
+    case (vt_cons s e th)
+    assume vt: "vt s"
+      and ih: "\<And>th. th \<notin> threads s \<Longrightarrow> cntCS s th = 0"
+      and stp: "step s e"
+      and not_in: "th \<notin> threads (e # s)"
+    from stp show ?case
+    proof(cases)
+      case (thread_create thread prio)
+      assume eq_e: "e = Create thread prio"
+        and not_in': "thread \<notin> threads s"
+      have "cntCS (e # s) th = cntCS s th"
+        apply (unfold eq_e cntCS_def holdents_test)
+        by (simp add:RAG_create_unchanged)
+      moreover have "th \<notin> threads s" 
+      proof -
+        from not_in eq_e show ?thesis by simp
+      qed
+      moreover note ih ultimately show ?thesis by auto
+    next
+      case (thread_exit thread)
+      assume eq_e: "e = Exit thread"
+      and nh: "holdents s thread = {}"
+      have eq_cns: "cntCS (e # s) th = cntCS s th"
+        apply (unfold eq_e cntCS_def holdents_test)
+        by (simp add:RAG_exit_unchanged)
+      show ?thesis
+      proof(cases "th = thread")
+        case True
+        have "cntCS s th = 0" by (unfold cntCS_def, auto simp:nh True)
+        with eq_cns show ?thesis by simp
+      next
+        case False
+        with not_in and eq_e
+        have "th \<notin> threads s" by simp
+        from ih[OF this] and eq_cns show ?thesis by simp
+      qed
+    next
+      case (thread_P thread cs)
+      assume eq_e: "e = P thread cs"
+      and is_runing: "thread \<in> runing s"
+      from assms thread_P ih vt stp thread_P have vtp: "vt (P thread cs#s)" by auto
+      have neq_th: "th \<noteq> thread" 
+      proof -
+        from not_in eq_e have "th \<notin> threads s" by simp
+        moreover from is_runing have "thread \<in> threads s"
+          by (simp add:runing_def readys_def)
+        ultimately show ?thesis by auto
+      qed
+      hence "cntCS (e # s) th  = cntCS s th "
+        apply (unfold cntCS_def holdents_test eq_e)
+        by (unfold step_RAG_p[OF vtp], auto)
+      moreover have "cntCS s th = 0"
+      proof(rule ih)
+        from not_in eq_e show "th \<notin> threads s" by simp
+      qed
+      ultimately show ?thesis by simp
+    next
+      case (thread_V thread cs)
+      assume eq_e: "e = V thread cs"
+        and is_runing: "thread \<in> runing s"
+        and hold: "holding s thread cs"
+      have neq_th: "th \<noteq> thread" 
+      proof -
+        from not_in eq_e have "th \<notin> threads s" by simp
+        moreover from is_runing have "thread \<in> threads s"
+          by (simp add:runing_def readys_def)
+        ultimately show ?thesis by auto
+      qed
+      from assms thread_V vt stp ih have vtv: "vt (V thread cs#s)" by auto
+      from hold obtain rest 
+        where eq_wq: "wq s cs = thread # rest"
+        by (case_tac "wq s cs", auto simp: wq_def s_holding_def)
+      from not_in eq_e eq_wq
+      have "\<not> next_th s thread cs th"
+        apply (auto simp:next_th_def)
+      proof -
+        assume ne: "rest \<noteq> []"
+          and ni: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> threads s" (is "?t \<notin> threads s")
+        have "?t \<in> set rest"
+        proof(rule someI2)
+          from wq_distinct[OF step_back_vt[OF vtv], of cs] and eq_wq
+          show "distinct rest \<and> set rest = set rest" by auto
+        next
+          fix x assume "distinct x \<and> set x = set rest" with ne
+          show "hd x \<in> set rest" by (cases x, auto)
+        qed
+        with eq_wq have "?t \<in> set (wq s cs)" by simp
+        from wq_threads[OF step_back_vt[OF vtv], OF this] and ni
+        show False by auto
+      qed
+      moreover note neq_th eq_wq
+      ultimately have "cntCS (e # s) th  = cntCS s th"
+        by (unfold eq_e cntCS_def holdents_test step_RAG_v[OF vtv], auto)
+      moreover have "cntCS s th = 0"
+      proof(rule ih)
+        from not_in eq_e show "th \<notin> threads s" by simp
+      qed
+      ultimately show ?thesis by simp
+    next
+      case (thread_set thread prio)
+      print_facts
+      assume eq_e: "e = Set thread prio"
+        and is_runing: "thread \<in> runing s"
+      from not_in and eq_e have "th \<notin> threads s" by auto
+      from ih [OF this] and eq_e
+      show ?thesis 
+        apply (unfold eq_e cntCS_def holdents_test)
+        by (simp add:RAG_set_unchanged)
+    qed
+    next
+      case vt_nil
+      show ?case
+      by (unfold cntCS_def, 
+        auto simp:count_def holdents_test s_RAG_def wq_def cs_holding_def)
+  qed
+qed
+
+lemma eq_waiting: "waiting (wq (s::state)) th cs = waiting s th cs"
+  by (auto simp:s_waiting_def cs_waiting_def wq_def)
+
+lemma dm_RAG_threads:
+  fixes th s
+  assumes vt: "vt s"
+  and 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 vt this] show ?thesis .
+qed
+
+lemma cp_eq_cpreced: "cp s th = cpreced (wq s) s th"
+unfolding cp_def wq_def
+apply(induct s rule: schs.induct)
+thm cpreced_initial
+apply(simp add: Let_def cpreced_initial)
+apply(simp add: Let_def)
+apply(simp add: Let_def)
+apply(simp add: Let_def)
+apply(subst (2) schs.simps)
+apply(simp add: Let_def)
+apply(subst (2) schs.simps)
+apply(simp add: Let_def)
+done
+
+(* FIXME: NOT NEEDED *)
+lemma runing_unique:
+  fixes th1 th2 s
+  assumes vt: "vt s"
+  and 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
+    apply(simp)
+    done
+  hence eq_max: "Max ((\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1)) =
+                 Max ((\<lambda>th. preced th s) ` ({th2} \<union> dependants (wq s) th2))"
+    (is "Max (?f ` ?A) = Max (?f ` ?B)")
+    thm cp_def image_Collect
+    unfolding cp_eq_cpreced 
+    unfolding cpreced_def .
+  obtain th1' where th1_in: "th1' \<in> ?A" and eq_f_th1: "?f th1' = Max (?f ` ?A)"
+    thm Max_in
+  proof -
+    have h1: "finite (?f ` ?A)"
+    proof -
+      have "finite ?A" 
+      proof -
+        have "finite (dependants (wq s) th1)"
+        proof-
+          have "finite {th'. (Th th', Th th1) \<in> (RAG (wq s))\<^sup>+}"
+          proof -
+            let ?F = "\<lambda> (x, y). the_th x"
+            have "{th'. (Th th', Th th1) \<in> (RAG (wq s))\<^sup>+} \<subseteq> ?F ` ((RAG (wq s))\<^sup>+)"
+              apply (auto simp:image_def)
+              by (rule_tac x = "(Th x, Th th1)" in bexI, auto)
+            moreover have "finite \<dots>"
+            proof -
+              from finite_RAG[OF vt] have "finite (RAG s)" .
+              hence "finite ((RAG (wq s))\<^sup>+)"
+                apply (unfold finite_trancl)
+                by (auto simp: s_RAG_def cs_RAG_def wq_def)
+              thus ?thesis by auto
+            qed
+            ultimately show ?thesis by (auto intro:finite_subset)
+          qed
+          thus ?thesis by (simp add:cs_dependants_def)
+        qed
+        thus ?thesis by simp
+      qed
+      thus ?thesis by auto
+    qed
+    moreover have h2: "(?f ` ?A) \<noteq> {}"
+    proof -
+      have "?A \<noteq> {}" by simp
+      thus ?thesis by simp
+    qed
+    thm Max_in
+    from Max_in [OF h1 h2]
+    have "Max (?f ` ?A) \<in> (?f ` ?A)" .
+    thus ?thesis 
+      thm cpreced_def
+      unfolding cpreced_def[symmetric] 
+      unfolding cp_eq_cpreced[symmetric] 
+      unfolding cpreced_def 
+      using that[intro] by (auto)
+  qed
+  obtain th2' where th2_in: "th2' \<in> ?B" and eq_f_th2: "?f th2' = Max (?f ` ?B)"
+  proof -
+    have h1: "finite (?f ` ?B)"
+    proof -
+      have "finite ?B" 
+      proof -
+        have "finite (dependants (wq s) th2)"
+        proof-
+          have "finite {th'. (Th th', Th th2) \<in> (RAG (wq s))\<^sup>+}"
+          proof -
+            let ?F = "\<lambda> (x, y). the_th x"
+            have "{th'. (Th th', Th th2) \<in> (RAG (wq s))\<^sup>+} \<subseteq> ?F ` ((RAG (wq s))\<^sup>+)"
+              apply (auto simp:image_def)
+              by (rule_tac x = "(Th x, Th th2)" in bexI, auto)
+            moreover have "finite \<dots>"
+            proof -
+              from finite_RAG[OF vt] have "finite (RAG s)" .
+              hence "finite ((RAG (wq s))\<^sup>+)"
+                apply (unfold finite_trancl)
+                by (auto simp: s_RAG_def cs_RAG_def wq_def)
+              thus ?thesis by auto
+            qed
+            ultimately show ?thesis by (auto intro:finite_subset)
+          qed
+          thus ?thesis by (simp add:cs_dependants_def)
+        qed
+        thus ?thesis by simp
+      qed
+      thus ?thesis by auto
+    qed
+    moreover have h2: "(?f ` ?B) \<noteq> {}"
+    proof -
+      have "?B \<noteq> {}" by simp
+      thus ?thesis by simp
+    qed
+    from Max_in [OF h1 h2]
+    have "Max (?f ` ?B) \<in> (?f ` ?B)" .
+    thus ?thesis by (auto intro:that)
+  qed
+  from eq_f_th1 eq_f_th2 eq_max 
+  have eq_preced: "preced th1' s = preced th2' s" by auto
+  hence eq_th12: "th1' = th2'"
+  proof (rule preced_unique)
+    from th1_in have "th1' = th1 \<or> (th1' \<in> dependants (wq s) th1)" by simp
+    thus "th1' \<in> threads s"
+    proof
+      assume "th1' \<in> dependants (wq s) th1"
+      hence "(Th th1') \<in> Domain ((RAG s)^+)"
+        apply (unfold cs_dependants_def cs_RAG_def s_RAG_def)
+        by (auto simp:Domain_def)
+      hence "(Th th1') \<in> Domain (RAG s)" by (simp add:trancl_domain)
+      from dm_RAG_threads[OF vt this] show ?thesis .
+    next
+      assume "th1' = th1"
+      with runing_1 show ?thesis
+        by (unfold runing_def readys_def, auto)
+    qed
+  next
+    from th2_in have "th2' = th2 \<or> (th2' \<in> dependants (wq s) th2)" by simp
+    thus "th2' \<in> threads s"
+    proof
+      assume "th2' \<in> dependants (wq s) th2"
+      hence "(Th th2') \<in> Domain ((RAG s)^+)"
+        apply (unfold cs_dependants_def cs_RAG_def s_RAG_def)
+        by (auto simp:Domain_def)
+      hence "(Th th2') \<in> Domain (RAG s)" by (simp add:trancl_domain)
+      from dm_RAG_threads[OF vt this] show ?thesis .
+    next
+      assume "th2' = th2"
+      with runing_2 show ?thesis
+        by (unfold runing_def readys_def, auto)
+    qed
+  qed
+  from th1_in have "th1' = th1 \<or> th1' \<in> dependants (wq s) th1" by simp
+  thus ?thesis
+  proof
+    assume eq_th': "th1' = th1"
+    from th2_in have "th2' = th2 \<or> th2' \<in> dependants (wq s) th2" by simp
+    thus ?thesis
+    proof
+      assume "th2' = th2" thus ?thesis using eq_th' eq_th12 by simp
+    next
+      assume "th2' \<in> dependants (wq s) th2"
+      with eq_th12 eq_th' have "th1 \<in> dependants (wq s) th2" by simp
+      hence "(Th th1, Th th2) \<in> (RAG s)^+"
+        by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp)
+      hence "Th th1 \<in> Domain ((RAG s)^+)" 
+        apply (unfold cs_dependants_def cs_RAG_def s_RAG_def)
+        by (auto simp:Domain_def)
+      hence "Th th1 \<in> Domain (RAG s)" by (simp add:trancl_domain)
+      then obtain n where d: "(Th th1, n) \<in> RAG s" by (auto simp:Domain_def)
+      from RAG_target_th [OF this]
+      obtain cs' where "n = Cs cs'" by auto
+      with d have "(Th th1, Cs cs') \<in> RAG s" by simp
+      with runing_1 have "False"
+        apply (unfold runing_def readys_def s_RAG_def)
+        by (auto simp:eq_waiting)
+      thus ?thesis by simp
+    qed
+  next
+    assume th1'_in: "th1' \<in> dependants (wq s) th1"
+    from th2_in have "th2' = th2 \<or> th2' \<in> dependants (wq s) th2" by simp
+    thus ?thesis 
+    proof
+      assume "th2' = th2"
+      with th1'_in eq_th12 have "th2 \<in> dependants (wq s) th1" by simp
+      hence "(Th th2, Th th1) \<in> (RAG s)^+"
+        by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp)
+      hence "Th th2 \<in> Domain ((RAG s)^+)" 
+        apply (unfold cs_dependants_def cs_RAG_def s_RAG_def)
+        by (auto simp:Domain_def)
+      hence "Th th2 \<in> Domain (RAG s)" by (simp add:trancl_domain)
+      then obtain n where d: "(Th th2, n) \<in> RAG s" by (auto simp:Domain_def)
+      from RAG_target_th [OF this]
+      obtain cs' where "n = Cs cs'" by auto
+      with d have "(Th th2, Cs cs') \<in> RAG s" by simp
+      with runing_2 have "False"
+        apply (unfold runing_def readys_def s_RAG_def)
+        by (auto simp:eq_waiting)
+      thus ?thesis by simp
+    next
+      assume "th2' \<in> dependants (wq s) th2"
+      with eq_th12 have "th1' \<in> dependants (wq s) th2" by simp
+      hence h1: "(Th th1', Th th2) \<in> (RAG s)^+"
+        by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp)
+      from th1'_in have h2: "(Th th1', Th th1) \<in> (RAG s)^+"
+        by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp)
+      show ?thesis
+      proof(rule dchain_unique[OF vt h1 _ h2, symmetric])
+        from runing_1 show "th1 \<in> readys s" by (simp add:runing_def)
+        from runing_2 show "th2 \<in> readys s" by (simp add:runing_def) 
+      qed
+    qed
+  qed
+qed
+
+
+lemma "vt s \<Longrightarrow> card (runing s) \<le> 1"
+apply(subgoal_tac "finite (runing s)")
+prefer 2
+apply (metis finite_nat_set_iff_bounded lessI runing_unique)
+apply(rule ccontr)
+apply(simp)
+apply(case_tac "Suc (Suc 0) \<le> card (runing s)")
+apply(subst (asm) card_le_Suc_iff)
+apply(simp)
+apply(auto)[1]
+apply (metis insertCI runing_unique)
+apply(auto) 
+done
+
+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 length_down_to_in: 
+  assumes le_ij: "i \<le> j"
+    and le_js: "j \<le> length s"
+  shows "length (down_to j i s) = j - i"
+proof -
+  have "length (down_to j i s) = length (from_to i j (rev s))"
+    by (unfold down_to_def, auto)
+  also have "\<dots> = j - i"
+  proof(rule length_from_to_in[OF le_ij])
+    from le_js show "j \<le> length (rev s)" by simp
+  qed
+  finally show ?thesis .
+qed
+
+
+lemma moment_head: 
+  assumes le_it: "Suc i \<le> length t"
+  obtains e where "moment (Suc i) t = e#moment i t"
+proof -
+  have "i \<le> Suc i" by simp
+  from length_down_to_in [OF this le_it]
+  have "length (down_to (Suc i) i t) = 1" by auto
+  then obtain e where "down_to (Suc i) i t = [e]"
+    apply (cases "(down_to (Suc i) i t)") by auto
+  moreover have "down_to (Suc i) 0 t = down_to (Suc i) i t @ down_to i 0 t"
+    by (rule down_to_conc[symmetric], auto)
+  ultimately have eq_me: "moment (Suc i) t = e#(moment i t)"
+    by (auto simp:down_to_moment)
+  from that [OF this] show ?thesis .
+qed
+
+lemma cnp_cnv_eq:
+  fixes th s
+  assumes "vt s"
+  and "th \<notin> threads s"
+  shows "cntP s th = cntV s th"
+ by (simp add: assms(1) assms(2) cnp_cnv_cncs not_thread_cncs)
+
+lemma eq_RAG: 
+  "RAG (wq s) = RAG s"
+by (unfold cs_RAG_def s_RAG_def, auto)
+
+lemma count_eq_dependants:
+  assumes vt: "vt s"
+  and eq_pv: "cntP s th = cntV s th"
+  shows "dependants (wq s) th = {}"
+proof -
+  from cnp_cnv_cncs[OF vt] and eq_pv
+  have "cntCS s th = 0" 
+    by (auto split:if_splits)
+  moreover have "finite {cs. (Cs cs, Th th) \<in> RAG s}"
+  proof -
+    from finite_holding[OF vt, of th] show ?thesis
+      by (simp add:holdents_test)
+  qed
+  ultimately have h: "{cs. (Cs cs, Th th) \<in> RAG s} = {}"
+    by (unfold cntCS_def holdents_test cs_dependants_def, auto)
+  show ?thesis
+  proof(unfold cs_dependants_def)
+    { assume "{th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} \<noteq> {}"
+      then obtain th' where "(Th th', Th th) \<in> (RAG (wq s))\<^sup>+" by auto
+      hence "False"
+      proof(cases)
+        assume "(Th th', Th th) \<in> RAG (wq s)"
+        thus "False" by (auto simp:cs_RAG_def)
+      next
+        fix c
+        assume "(c, Th th) \<in> RAG (wq s)"
+        with h and eq_RAG show "False"
+          by (cases c, auto simp:cs_RAG_def)
+      qed
+    } thus "{th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} = {}" by auto
+  qed
+qed
+
+lemma dependants_threads:
+  fixes s th
+  assumes vt: "vt s"
+  shows "dependants (wq s) th \<subseteq> threads s"
+proof
+  { fix th th'
+    assume h: "th \<in> {th'a. (Th th'a, Th th') \<in> (RAG (wq s))\<^sup>+}"
+    have "Th th \<in> Domain (RAG s)"
+    proof -
+      from h obtain th' where "(Th th, Th th') \<in> (RAG (wq s))\<^sup>+" by auto
+      hence "(Th th) \<in> Domain ( (RAG (wq s))\<^sup>+)" by (auto simp:Domain_def)
+      with trancl_domain have "(Th th) \<in> Domain (RAG (wq s))" by simp
+      thus ?thesis using eq_RAG by simp
+    qed
+    from dm_RAG_threads[OF vt this]
+    have "th \<in> threads s" .
+  } note hh = this
+  fix th1 
+  assume "th1 \<in> dependants (wq s) th"
+  hence "th1 \<in> {th'a. (Th th'a, Th th) \<in> (RAG (wq s))\<^sup>+}"
+    by (unfold cs_dependants_def, simp)
+  from hh [OF this] show "th1 \<in> threads s" .
+qed
+
+lemma finite_threads:
+  assumes vt: "vt s"
+  shows "finite (threads s)"
+using vt
+by (induct) (auto elim: step.cases)
+
+lemma Max_f_mono:
+  assumes seq: "A \<subseteq> B"
+  and np: "A \<noteq> {}"
+  and fnt: "finite B"
+  shows "Max (f ` A) \<le> Max (f ` B)"
+proof(rule Max_mono)
+  from seq show "f ` A \<subseteq> f ` B" by auto
+next
+  from np show "f ` A \<noteq> {}" by auto
+next
+  from fnt and seq show "finite (f ` B)" by auto
+qed
+
+lemma cp_le:
+  assumes vt: "vt s"
+  and th_in: "th \<in> threads s"
+  shows "cp s th \<le> Max ((\<lambda> th. (preced th s)) ` threads s)"
+proof(unfold cp_eq_cpreced cpreced_def cs_dependants_def)
+  show "Max ((\<lambda>th. preced th s) ` ({th} \<union> {th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+}))
+         \<le> Max ((\<lambda>th. preced th s) ` threads s)"
+    (is "Max (?f ` ?A) \<le> Max (?f ` ?B)")
+  proof(rule Max_f_mono)
+    show "{th} \<union> {th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} \<noteq> {}" by simp
+  next
+    from finite_threads [OF vt]
+    show "finite (threads s)" .
+  next
+    from th_in
+    show "{th} \<union> {th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} \<subseteq> threads s"
+      apply (auto simp:Domain_def)
+      apply (rule_tac dm_RAG_threads[OF vt])
+      apply (unfold trancl_domain [of "RAG s", symmetric])
+      by (unfold cs_RAG_def s_RAG_def, auto simp:Domain_def)
+  qed
+qed
+
+lemma le_cp:
+  assumes vt: "vt s"
+  shows "preced th s \<le> cp s th"
+proof(unfold cp_eq_cpreced preced_def cpreced_def, simp)
+  show "Prc (priority th s) (last_set th s)
+    \<le> Max (insert (Prc (priority th s) (last_set th s))
+            ((\<lambda>th. Prc (priority th s) (last_set th s)) ` dependants (wq s) th))"
+    (is "?l \<le> Max (insert ?l ?A)")
+  proof(cases "?A = {}")
+    case False
+    have "finite ?A" (is "finite (?f ` ?B)")
+    proof -
+      have "finite ?B" 
+      proof-
+        have "finite {th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+}"
+        proof -
+          let ?F = "\<lambda> (x, y). the_th x"
+          have "{th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} \<subseteq> ?F ` ((RAG (wq s))\<^sup>+)"
+            apply (auto simp:image_def)
+            by (rule_tac x = "(Th x, Th th)" in bexI, auto)
+          moreover have "finite \<dots>"
+          proof -
+            from finite_RAG[OF vt] have "finite (RAG s)" .
+            hence "finite ((RAG (wq s))\<^sup>+)"
+              apply (unfold finite_trancl)
+              by (auto simp: s_RAG_def cs_RAG_def wq_def)
+            thus ?thesis by auto
+          qed
+          ultimately show ?thesis by (auto intro:finite_subset)
+        qed
+        thus ?thesis by (simp add:cs_dependants_def)
+      qed
+      thus ?thesis by simp
+    qed
+    from Max_insert [OF this False, of ?l] show ?thesis by auto
+  next
+    case True
+    thus ?thesis by auto
+  qed
+qed
+
+lemma max_cp_eq: 
+  assumes vt: "vt s"
+  shows "Max ((cp s) ` threads s) = Max ((\<lambda> th. (preced th s)) ` threads s)"
+  (is "?l = ?r")
+proof(cases "threads s = {}")
+  case True
+  thus ?thesis by auto
+next
+  case False
+  have "?l \<in> ((cp s) ` threads s)"
+  proof(rule Max_in)
+    from finite_threads[OF vt] 
+    show "finite (cp s ` threads s)" by auto
+  next
+    from False show "cp s ` threads s \<noteq> {}" by auto
+  qed
+  then obtain th 
+    where th_in: "th \<in> threads s" and eq_l: "?l = cp s th" by auto
+  have "\<dots> \<le> ?r" by (rule cp_le[OF vt th_in])
+  moreover have "?r \<le> cp s th" (is "Max (?f ` ?A) \<le> cp s th")
+  proof -
+    have "?r \<in> (?f ` ?A)"
+    proof(rule Max_in)
+      from finite_threads[OF vt]
+      show " finite ((\<lambda>th. preced th s) ` threads s)" by auto
+    next
+      from False show " (\<lambda>th. preced th s) ` threads s \<noteq> {}" by auto
+    qed
+    then obtain th' where 
+      th_in': "th' \<in> ?A " and eq_r: "?r = ?f th'" by auto
+    from le_cp [OF vt, of th']  eq_r
+    have "?r \<le> cp s th'" by auto
+    moreover have "\<dots> \<le> cp s th"
+    proof(fold eq_l)
+      show " cp s th' \<le> Max (cp s ` threads s)"
+      proof(rule Max_ge)
+        from th_in' show "cp s th' \<in> cp s ` threads s"
+          by auto
+      next
+        from finite_threads[OF vt]
+        show "finite (cp s ` threads s)" by auto
+      qed
+    qed
+    ultimately show ?thesis by auto
+  qed
+  ultimately show ?thesis using eq_l by auto
+qed
+
+lemma max_cp_readys_threads_pre:
+  assumes vt: "vt s"
+  and np: "threads s \<noteq> {}"
+  shows "Max (cp s ` readys s) = Max (cp s ` threads s)"
+proof(unfold max_cp_eq[OF vt])
+  show "Max (cp s ` readys s) = Max ((\<lambda>th. preced th s) ` threads s)"
+  proof -
+    let ?p = "Max ((\<lambda>th. preced th s) ` threads s)" 
+    let ?f = "(\<lambda>th. preced th s)"
+    have "?p \<in> ((\<lambda>th. preced th s) ` threads s)"
+    proof(rule Max_in)
+      from finite_threads[OF vt] show "finite (?f ` threads s)" by simp
+    next
+      from np show "?f ` threads s \<noteq> {}" by simp
+    qed
+    then obtain tm where tm_max: "?f tm = ?p" and tm_in: "tm \<in> threads s"
+      by (auto simp:Image_def)
+    from th_chain_to_ready [OF vt tm_in]
+    have "tm \<in> readys s \<or> (\<exists>th'. th' \<in> readys s \<and> (Th tm, Th th') \<in> (RAG s)\<^sup>+)" .
+    thus ?thesis
+    proof
+      assume "\<exists>th'. th' \<in> readys s \<and> (Th tm, Th th') \<in> (RAG s)\<^sup>+ "
+      then obtain th' where th'_in: "th' \<in> readys s" 
+        and tm_chain:"(Th tm, Th th') \<in> (RAG s)\<^sup>+" by auto
+      have "cp s th' = ?f tm"
+      proof(subst cp_eq_cpreced, subst cpreced_def, rule Max_eqI)
+        from dependants_threads[OF vt] finite_threads[OF vt]
+        show "finite ((\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th'))" 
+          by (auto intro:finite_subset)
+      next
+        fix p assume p_in: "p \<in> (\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')"
+        from tm_max have " preced tm s = Max ((\<lambda>th. preced th s) ` threads s)" .
+        moreover have "p \<le> \<dots>"
+        proof(rule Max_ge)
+          from finite_threads[OF vt]
+          show "finite ((\<lambda>th. preced th s) ` threads s)" by simp
+        next
+          from p_in and th'_in and dependants_threads[OF vt, of th']
+          show "p \<in> (\<lambda>th. preced th s) ` threads s"
+            by (auto simp:readys_def)
+        qed
+        ultimately show "p \<le> preced tm s" by auto
+      next
+        show "preced tm s \<in> (\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')"
+        proof -
+          from tm_chain
+          have "tm \<in> dependants (wq s) th'"
+            by (unfold cs_dependants_def s_RAG_def cs_RAG_def, auto)
+          thus ?thesis by auto
+        qed
+      qed
+      with tm_max
+      have h: "cp s th' = Max ((\<lambda>th. preced th s) ` threads s)" by simp
+      show ?thesis
+      proof (fold h, rule Max_eqI)
+        fix q 
+        assume "q \<in> cp s ` readys s"
+        then obtain th1 where th1_in: "th1 \<in> readys s"
+          and eq_q: "q = cp s th1" by auto
+        show "q \<le> cp s th'"
+          apply (unfold h eq_q)
+          apply (unfold cp_eq_cpreced cpreced_def)
+          apply (rule Max_mono)
+        proof -
+          from dependants_threads [OF vt, of th1] th1_in
+          show "(\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1) \<subseteq> 
+                 (\<lambda>th. preced th s) ` threads s"
+            by (auto simp:readys_def)
+        next
+          show "(\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1) \<noteq> {}" by simp
+        next
+          from finite_threads[OF vt] 
+          show " finite ((\<lambda>th. preced th s) ` threads s)" by simp
+        qed
+      next
+        from finite_threads[OF vt]
+        show "finite (cp s ` readys s)" by (auto simp:readys_def)
+      next
+        from th'_in
+        show "cp s th' \<in> cp s ` readys s" by simp
+      qed
+    next
+      assume tm_ready: "tm \<in> readys s"
+      show ?thesis
+      proof(fold tm_max)
+        have cp_eq_p: "cp s tm = preced tm s"
+        proof(unfold cp_eq_cpreced cpreced_def, rule Max_eqI)
+          fix y 
+          assume hy: "y \<in> (\<lambda>th. preced th s) ` ({tm} \<union> dependants (wq s) tm)"
+          show "y \<le> preced tm s"
+          proof -
+            { fix y'
+              assume hy' : "y' \<in> ((\<lambda>th. preced th s) ` dependants (wq s) tm)"
+              have "y' \<le> preced tm s"
+              proof(unfold tm_max, rule Max_ge)
+                from hy' dependants_threads[OF vt, of tm]
+                show "y' \<in> (\<lambda>th. preced th s) ` threads s" by auto
+              next
+                from finite_threads[OF vt] 
+                show "finite ((\<lambda>th. preced th s) ` threads s)" by simp
+              qed
+            } with hy show ?thesis by auto
+          qed
+        next
+          from dependants_threads[OF vt, of tm] finite_threads[OF vt]
+          show "finite ((\<lambda>th. preced th s) ` ({tm} \<union> dependants (wq s) tm))"
+            by (auto intro:finite_subset)
+        next
+          show "preced tm s \<in> (\<lambda>th. preced th s) ` ({tm} \<union> dependants (wq s) tm)"
+            by simp
+        qed 
+        moreover have "Max (cp s ` readys s) = cp s tm"
+        proof(rule Max_eqI)
+          from tm_ready show "cp s tm \<in> cp s ` readys s" by simp
+        next
+          from finite_threads[OF vt]
+          show "finite (cp s ` readys s)" by (auto simp:readys_def)
+        next
+          fix y assume "y \<in> cp s ` readys s"
+          then obtain th1 where th1_readys: "th1 \<in> readys s"
+            and h: "y = cp s th1" by auto
+          show "y \<le> cp s tm"
+            apply(unfold cp_eq_p h)
+            apply(unfold cp_eq_cpreced cpreced_def tm_max, rule Max_mono)
+          proof -
+            from finite_threads[OF vt]
+            show "finite ((\<lambda>th. preced th s) ` threads s)" by simp
+          next
+            show "(\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1) \<noteq> {}"
+              by simp
+          next
+            from dependants_threads[OF vt, of th1] th1_readys
+            show "(\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1) 
+                    \<subseteq> (\<lambda>th. preced th s) ` threads s"
+              by (auto simp:readys_def)
+          qed
+        qed
+        ultimately show " Max (cp s ` readys s) = preced tm s" by simp
+      qed 
+    qed
+  qed
+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:
+  assumes vt: "vt s"
+  shows "Max (cp s ` readys s) = Max (cp s ` threads s)"
+proof(cases "threads s = {}")
+  case True
+  thus ?thesis 
+    by (auto simp:readys_def)
+next
+  case False
+  show ?thesis by (rule max_cp_readys_threads_pre[OF vt False])
+qed
+
+
+lemma eq_holding: "holding (wq s) th cs = holding s th cs"
+  apply (unfold s_holding_def cs_holding_def wq_def, simp)
+  done
+
+lemma f_image_eq:
+  assumes h: "\<And> a. a \<in> A \<Longrightarrow> f a = g a"
+  shows "f ` A = g ` A"
+proof
+  show "f ` A \<subseteq> g ` A"
+    by(rule image_subsetI, auto intro:h)
+next
+  show "g ` A \<subseteq> f ` A"
+   by (rule image_subsetI, auto intro:h[symmetric])
+qed
+
+
+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)
+apply(simp add: s_RAG_def)
+apply(simp add: s_holding_abv s_waiting_abv)
+apply(simp add: Domain_iff Range_iff)
+apply(simp add: wq_def)
+apply(auto)
+done
+
+lemma detached_intro:
+  fixes s th
+  assumes vt: "vt s"
+  and eq_pv: "cntP s th = cntV s th"
+  shows "detached s th"
+proof -
+ from cnp_cnv_cncs[OF vt]
+  have eq_cnt: "cntP s th =
+    cntV s th + (if th \<in> readys s \<or> th \<notin> threads s then cntCS s th else cntCS s th + 1)" .
+  hence cncs_zero: "cntCS s th = 0"
+    by (auto simp:eq_pv split:if_splits)
+  with eq_cnt
+  have "th \<in> readys s \<or> th \<notin> threads s" by (auto simp:eq_pv)
+  thus ?thesis
+  proof
+    assume "th \<notin> threads s"
+    with range_in[OF vt] dm_RAG_threads[OF vt]
+    show ?thesis
+      by (auto simp add: detached_def s_RAG_def s_waiting_abv s_holding_abv wq_def Domain_iff Range_iff)
+  next
+    assume "th \<in> readys s"
+    moreover have "Th th \<notin> Range (RAG s)"
+    proof -
+      from card_0_eq [OF finite_holding [OF vt]] and cncs_zero
+      have "holdents s th = {}"
+        by (simp add:cntCS_def)
+      thus ?thesis
+        apply(auto simp:holdents_test)
+        apply(case_tac a)
+        apply(auto simp:holdents_test s_RAG_def)
+        done
+    qed
+    ultimately show ?thesis
+      by (auto simp add: detached_def s_RAG_def s_waiting_abv s_holding_abv wq_def readys_def)
+  qed
+qed
+
+lemma detached_elim:
+  fixes s th
+  assumes vt: "vt s"
+  and dtc: "detached s th"
+  shows "cntP s th = cntV s th"
+proof -
+  from cnp_cnv_cncs[OF vt]
+  have eq_pv: " cntP s th =
+    cntV s th + (if th \<in> readys s \<or> th \<notin> threads s then cntCS s th else cntCS s th + 1)" .
+  have cncs_z: "cntCS s th = 0"
+  proof -
+    from dtc have "holdents s th = {}"
+      unfolding detached_def holdents_test s_RAG_def
+      by (simp add: s_waiting_abv wq_def s_holding_abv Domain_iff Range_iff)
+    thus ?thesis by (auto simp:cntCS_def)
+  qed
+  show ?thesis
+  proof(cases "th \<in> threads s")
+    case True
+    with dtc 
+    have "th \<in> readys s"
+      by (unfold readys_def detached_def Field_def Domain_def Range_def, 
+           auto simp:eq_waiting s_RAG_def)
+    with cncs_z and eq_pv show ?thesis by simp
+  next
+    case False
+    with cncs_z and eq_pv show ?thesis by simp
+  qed
+qed
+
+lemma detached_eq:
+  fixes s th
+  assumes vt: "vt s"
+  shows "(detached s th) = (cntP s th = cntV s th)"
+  by (insert vt, auto intro:detached_intro detached_elim)
+
+text {* 
+  The lemmas in this .thy file are all obvious lemmas, however, they still needs to be derived
+  from the concise and miniature model of PIP given in PrioGDef.thy.
+*}
+
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PrioGDef.thy~	Thu Dec 03 14:34:29 2015 +0800
@@ -0,0 +1,613 @@
+ {* Definitions *}
+(*<*)
+theory PrioGDef
+imports Precedence_ord Moment
+begin
+(*>*)
+
+text {*
+  In this section, the formal model of  Priority Inheritance Protocol (PIP) is presented. 
+  The model is based on Paulson's inductive protocol verification method, where 
+  the state of the system is modelled as a list of events happened so far with the latest 
+  event put at the head. 
+*}
+
+text {*
+  To define events, the identifiers of {\em threads},
+  {\em priority} and {\em critical resources } (abbreviated as @{text "cs"}) 
+  need to be represented. All three are represetned using standard 
+  Isabelle/HOL type @{typ "nat"}:
+*}
+
+type_synonym thread = nat -- {* Type for thread identifiers. *}
+type_synonym priority = nat  -- {* Type for priorities. *}
+type_synonym cs = nat -- {* Type for critical sections (or critical resources). *}
+
+text {*
+  \noindent
+  The abstraction of Priority Inheritance Protocol (PIP) is set at the system call level.
+  Every system call is represented as an event. The format of events is defined 
+  defined as follows:
+  *}
+
+datatype event = 
+  Create thread priority | -- {* Thread @{text "thread"} is created with priority @{text "priority"}. *}
+  Exit thread | -- {* Thread @{text "thread"} finishing its execution. *}
+  P thread cs | -- {* Thread @{text "thread"} requesting critical resource @{text "cs"}. *}
+  V thread cs | -- {* Thread @{text "thread"}  releasing critical resource @{text "cs"}. *}
+  Set thread priority -- {* Thread @{text "thread"} resets its priority to @{text "priority"}. *}
+
+
+text {* 
+  As mentioned earlier, in Paulson's inductive method, the states of system are represented as lists of events,
+  which is defined by the following type @{text "state"}:
+  *}
+type_synonym state = "event list"
+
+
+text {* 
+\noindent
+  Resource Allocation Graph (RAG for short) is used extensively in our formal analysis. 
+  The following type @{text "node"} is used to represent nodes in RAG.
+  *}
+datatype node = 
+   Th "thread" | -- {* Node for thread. *}
+   Cs "cs" -- {* Node for critical resource. *}
+
+text {*
+  \noindent
+  The following function
+  @{text "threads"} is used to calculate the set of live threads (@{text "threads s"})
+  in state @{text "s"}.
+  *}
+fun threads :: "state \<Rightarrow> thread set"
+  where 
+  -- {* At the start of the system, the set of threads is empty: *}
+  "threads [] = {}" | 
+  -- {* New thread is added to the @{text "threads"}: *}
+  "threads (Create thread prio#s) = {thread} \<union> threads s" | 
+  -- {* Finished thread is removed: *}
+  "threads (Exit thread # s) = (threads s) - {thread}" | 
+  -- {* Other kind of events does not affect the value of @{text "threads"}: *}
+  "threads (e#s) = threads s" 
+
+text {* 
+  \noindent
+  The function @{text "threads"} defined above is one of 
+  the so called {\em observation function}s which forms 
+  the very basis of Paulson's inductive protocol verification method.
+  Each observation function {\em observes} one particular aspect (or attribute)
+  of the system. For example, the attribute observed by  @{text "threads s"}
+  is the set of threads living in state @{text "s"}. 
+  The protocol being modelled 
+  The decision made the protocol being modelled is based on the {\em observation}s
+  returned by {\em observation function}s. Since {\observation function}s forms 
+  the very basis on which Paulson's inductive method is based, there will be 
+  a lot of such observation functions introduced in the following. In fact, any function 
+  which takes event list as argument is a {\em observation function}.
+  *}
+
+text {* \noindent
+  Observation @{text "priority th s"} is
+  the {\em original priority} of thread @{text "th"} in state @{text "s"}. 
+  The {\em original priority} is the priority 
+  assigned to a thread when it is created or when it is reset by system call 
+  (represented by event @{text "Set thread priority"}).
+*}
+
+fun priority :: "thread \<Rightarrow> state \<Rightarrow> priority"
+  where
+  -- {* @{text "0"} is assigned to threads which have never been created: *}
+  "priority thread [] = 0" |
+  "priority thread (Create thread' prio#s) = 
+     (if thread' = thread then prio else priority thread s)" |
+  "priority thread (Set thread' prio#s) = 
+     (if thread' = thread then prio else priority thread s)" |
+  "priority thread (e#s) = priority thread s"
+
+text {*
+  \noindent
+  Observation @{text "last_set th s"} is the last time when the priority of thread @{text "th"} is set, 
+  observed from state @{text "s"}.
+  The time in the system is measured by the number of events happened so far since the very beginning.
+*}
+fun last_set :: "thread \<Rightarrow> state \<Rightarrow> nat"
+  where
+  "last_set thread [] = 0" |
+  "last_set thread ((Create thread' prio)#s) = 
+       (if (thread = thread') then length s else last_set thread s)" |
+  "last_set thread ((Set thread' prio)#s) = 
+       (if (thread = thread') then length s else last_set thread s)" |
+  "last_set thread (_#s) = last_set thread s"
+
+text {*
+  \noindent 
+  The {\em precedence} is a notion derived from {\em priority}, where the {\em precedence} of 
+  a thread is the combination of its {\em original priority} and {\em time} the priority is set. 
+  The intention is to discriminate threads with the same priority by giving threads whose priority
+  is assigned earlier higher precedences, becasue such threads are more urgent to finish. 
+  This explains the following definition:
+  *}
+definition preced :: "thread \<Rightarrow> state \<Rightarrow> precedence"
+  where "preced thread s \<equiv> Prc (priority thread s) (last_set thread s)"
+
+
+text {*
+  \noindent
+  A number of important notions in PIP are represented as the following functions, 
+  defined in terms of the waiting queues of the system, where the waiting queues 
+  , as a whole, is represented by the @{text "wq"} argument of every notion function.
+  The @{text "wq"} argument is itself a functions which maps every critical resource 
+  @{text "cs"} to the list of threads which are holding or waiting for it. 
+  The thread at the head of this list is designated as the thread which is current 
+  holding the resrouce, which is slightly different from tradition where
+  all threads in the waiting queue are considered as waiting for the resource.
+  *}
+
+consts 
+  holding :: "'b \<Rightarrow> thread \<Rightarrow> cs \<Rightarrow> bool" 
+  waiting :: "'b \<Rightarrow> thread \<Rightarrow> cs \<Rightarrow> bool"
+  RAG :: "'b \<Rightarrow> (node \<times> node) set"
+  dependants :: "'b \<Rightarrow> thread \<Rightarrow> thread set"
+
+defs (overloaded) 
+  -- {* 
+  \begin{minipage}{0.9\textwidth}
+  This meaning of @{text "wq"} is reflected in the following definition of @{text "holding wq th cs"},
+  where @{text "holding wq th cs"} means thread @{text "th"} is holding the critical 
+  resource @{text "cs"}. This decision is based on @{text "wq"}.
+  \end{minipage}
+  *}
+
+  cs_holding_def: 
+  "holding wq thread cs \<equiv> (thread \<in> set (wq cs) \<and> thread = hd (wq cs))"
+  -- {* 
+  \begin{minipage}{0.9\textwidth}
+  In accordance with the definition of @{text "holding wq th cs"}, 
+  a thread @{text "th"} is considered waiting for @{text "cs"} if 
+  it is in the {\em waiting queue} of critical resource @{text "cs"}, but not at the head.
+  This is reflected in the definition of @{text "waiting wq th cs"} as follows:
+  \end{minipage}
+  *}
+  cs_waiting_def: 
+  "waiting wq thread cs \<equiv> (thread \<in> set (wq cs) \<and> thread \<noteq> hd (wq cs))"
+  -- {* 
+  \begin{minipage}{0.9\textwidth}
+  @{text "RAG wq"} generates RAG (a binary relations on @{text "node"})
+  out of waiting queues of the system (represented by the @{text "wq"} argument):
+  \end{minipage}
+  *}
+  cs_RAG_def: 
+  "RAG (wq::cs \<Rightarrow> thread list) \<equiv>
+      {(Th th, Cs cs) | th cs. waiting wq th cs} \<union> {(Cs cs, Th th) | cs th. holding wq th cs}"
+  -- {* 
+  \begin{minipage}{0.9\textwidth}
+  The following @{text "dependants wq th"} represents the set of threads which are RAGing on
+  thread @{text "th"} in Resource Allocation Graph @{text "RAG wq"}. 
+  Here, "RAGing" means waiting directly or indirectly on the critical resource. 
+  \end{minipage}
+  *}
+  cs_dependants_def: 
+  "dependants (wq::cs \<Rightarrow> thread list) th \<equiv> {th' . (Th th', Th th) \<in> (RAG wq)^+}"
+
+
+text {* \noindent 
+  The following
+  @{text "cpreced s th"} gives the {\em current precedence} of thread @{text "th"} under
+  state @{text "s"}. The definition of @{text "cpreced"} reflects the basic idea of 
+  Priority Inheritance that the {\em current precedence} of a thread is the precedence 
+  inherited from the maximum of all its dependants, i.e. the threads which are waiting 
+  directly or indirectly waiting for some resources from it. If no such thread exits, 
+  @{text "th"}'s {\em current precedence} equals its original precedence, i.e. 
+  @{text "preced th s"}.
+  *}
+
+definition cpreced :: "(cs \<Rightarrow> thread list) \<Rightarrow> state \<Rightarrow> thread \<Rightarrow> precedence"
+  where "cpreced wq s = (\<lambda>th. Max ((\<lambda>th'. preced th' s) ` ({th} \<union> dependants wq th)))"
+
+text {*
+  Notice that the current precedence (@{text "cpreced"}) of one thread @{text "th"} can be boosted 
+  (becoming larger than its own precedence) by those threads in 
+  the @{text "dependants wq th"}-set. If one thread get boosted, we say 
+  it inherits the priority (or, more precisely, the precedence) of 
+  its dependants. This is how the word "Inheritance" in 
+  Priority Inheritance Protocol comes.
+*}
+
+(*<*)
+lemma 
+  cpreced_def2:
+  "cpreced wq s th \<equiv> Max ({preced th s} \<union> {preced th' s | th'. th' \<in> dependants wq th})"
+  unfolding cpreced_def image_def
+  apply(rule eq_reflection)
+  apply(rule_tac f="Max" in arg_cong)
+  by (auto)
+(*>*)
+
+
+text {* \noindent
+  Assuming @{text "qs"} be the waiting queue of a critical resource, 
+  the following abbreviation "release qs" is the waiting queue after the thread 
+  holding the resource (which is thread at the head of @{text "qs"}) released
+  the resource:
+*}
+abbreviation
+  "release qs \<equiv> case qs of
+             [] => [] 
+          |  (_#qs') => (SOME q. distinct q \<and> set q = set qs')"
+text {* \noindent
+  It can be seen from the definition that the thread at the head of @{text "qs"} is removed
+  from the return value, and the value @{term "q"} is an reordering of @{text "qs'"}, the 
+  tail of @{text "qs"}. Through this reordering, one of the waiting threads (those in @{text "qs'"} }
+  is chosen nondeterministically to be the head of the new queue @{text "q"}. 
+  Therefore, this thread is the one who takes over the resource. This is a little better different 
+  from common sense that the thread who comes the earliest should take over.  
+  The intention of this definition is to show that the choice of which thread to take over the 
+  release resource does not affect the correctness of the PIP protocol. 
+*}
+
+text {*
+  The data structure used by the operating system for scheduling is referred to as 
+  {\em schedule state}. It is represented as a record consisting of 
+  a function assigning waiting queue to resources 
+  (to be used as the @{text "wq"} argument in @{text "holding"}, @{text "waiting"} 
+  and  @{text "RAG"}, etc) and a function assigning precedence to threads:
+  *}
+
+record schedule_state = 
+    wq_fun :: "cs \<Rightarrow> thread list" -- {* The function assigning waiting queue. *}
+    cprec_fun :: "thread \<Rightarrow> precedence" -- {* The function assigning precedence. *}
+
+text {* \noindent
+  The following two abbreviations (@{text "all_unlocked"} and @{text "initial_cprec"}) 
+  are used to set the initial values of the @{text "wq_fun"} @{text "cprec_fun"} fields 
+  respectively of the @{text "schedule_state"} record by the following function @{text "sch"},
+  which is used to calculate the system's {\em schedule state}.
+
+  Since there is no thread at the very beginning to make request, all critical resources 
+  are free (or unlocked). This status is represented by the abbreviation
+  @{text "all_unlocked"}. 
+  *}
+abbreviation
+  "all_unlocked \<equiv> \<lambda>_::cs. ([]::thread list)"
+
+
+text {* \noindent
+  The initial current precedence for a thread can be anything, because there is no thread then. 
+  We simply assume every thread has precedence @{text "Prc 0 0"}.
+  *}
+
+abbreviation 
+  "initial_cprec \<equiv> \<lambda>_::thread. Prc 0 0"
+
+
+text {* \noindent
+  The following function @{text "schs"} is used to calculate the system's schedule state @{text "schs s"}
+  out of the current system state @{text "s"}. It is the central function to model Priority Inheritance:
+  *}
+fun schs :: "state \<Rightarrow> schedule_state"
+  where 
+  -- {*
+  \begin{minipage}{0.9\textwidth}
+    Setting the initial value of the @{text "schedule_state"} record (see the explanations above).
+  \end{minipage}
+  *}
+  "schs [] = (| wq_fun = all_unlocked,  cprec_fun = initial_cprec |)" |
+
+  -- {*
+  \begin{minipage}{0.9\textwidth}
+  \begin{enumerate}
+  \item @{text "ps"} is the schedule state of last moment.
+  \item @{text "pwq"} is the waiting queue function of last moment.
+  \item @{text "pcp"} is the precedence function of last moment (NOT USED). 
+  \item @{text "nwq"} is the new waiting queue function. It is calculated using a @{text "case"} statement:
+  \begin{enumerate}
+      \item If the happening event is @{text "P thread cs"}, @{text "thread"} is added to 
+            the end of @{text "cs"}'s waiting queue.
+      \item If the happening event is @{text "V thread cs"} and @{text "s"} is a legal state,
+            @{text "th'"} must equal to @{text "thread"}, 
+            because @{text "thread"} is the one currently holding @{text "cs"}. 
+            The case @{text "[] \<Longrightarrow> []"} may never be executed in a legal state.
+            the @{text "(SOME q. distinct q \<and> set q = set qs)"} is used to choose arbitrarily one 
+            thread in waiting to take over the released resource @{text "cs"}. In our representation,
+            this amounts to rearrange elements in waiting queue, so that one of them is put at the head.
+      \item For other happening event, the schedule state just does not change.
+  \end{enumerate}
+  \item @{text "ncp"} is new precedence function, it is calculated from the newly updated waiting queue 
+        function. The RAGency of precedence function on waiting queue function is the reason to 
+        put them in the same record so that they can evolve together.
+  \end{enumerate}
+  
+
+  The calculation of @{text "cprec_fun"} depends on the value of @{text "wq_fun"}. 
+  Therefore, in the following cases, @{text "wq_fun"} is always calculated first, in 
+  the name of @{text "wq"} (if  @{text "wq_fun"} is not changed
+  by the happening event) or @{text "new_wq"} (if the value of @{text "wq_fun"} is changed).
+  \end{minipage}
+     *}
+   "schs (Create th prio # s) = 
+       (let wq = wq_fun (schs s) in
+          (|wq_fun = wq, cprec_fun = cpreced wq (Create th prio # s)|))"
+|  "schs (Exit th # s) = 
+       (let wq = wq_fun (schs s) in
+          (|wq_fun = wq, cprec_fun = cpreced wq (Exit th # s)|))"
+|  "schs (Set th prio # s) = 
+       (let wq = wq_fun (schs s) in
+          (|wq_fun = wq, cprec_fun = cpreced wq (Set th prio # s)|))"
+   -- {*
+   \begin{minipage}{0.9\textwidth}
+      Different from the forth coming cases, the @{text "wq_fun"} field of the schedule state 
+      is changed. So, the new value is calculated first, in the name of @{text "new_wq"}.
+   \end{minipage}   
+   *}
+|  "schs (P th cs # s) = 
+       (let wq = wq_fun (schs s) in
+        let new_wq = wq(cs := (wq cs @ [th])) in
+          (|wq_fun = new_wq, cprec_fun = cpreced new_wq (P th cs # s)|))"
+|  "schs (V th cs # s) = 
+       (let wq = wq_fun (schs s) in
+        let new_wq = wq(cs := release (wq cs)) in
+          (|wq_fun = new_wq, cprec_fun = cpreced new_wq (V th cs # s)|))"
+
+lemma cpreced_initial: 
+  "cpreced (\<lambda> cs. []) [] = (\<lambda>_. (Prc 0 0))"
+apply(simp add: cpreced_def)
+apply(simp add: cs_dependants_def cs_RAG_def cs_waiting_def cs_holding_def)
+apply(simp add: preced_def)
+done
+
+lemma sch_old_def:
+  "schs (e#s) = (let ps = schs s in 
+                  let pwq = wq_fun ps in 
+                  let nwq = case e of
+                             P th cs \<Rightarrow>  pwq(cs:=(pwq cs @ [th])) |
+                             V th cs \<Rightarrow> let nq = case (pwq cs) of
+                                                      [] \<Rightarrow> [] | 
+                                                      (_#qs) \<Rightarrow> (SOME q. distinct q \<and> set q = set qs)
+                                            in pwq(cs:=nq)                 |
+                              _ \<Rightarrow> pwq
+                  in let ncp = cpreced nwq (e#s) in 
+                     \<lparr>wq_fun = nwq, cprec_fun = ncp\<rparr>
+                 )"
+apply(cases e)
+apply(simp_all)
+done
+
+
+text {* 
+  \noindent
+  The following @{text "wq"} is a shorthand for @{text "wq_fun"}. 
+  *}
+definition wq :: "state \<Rightarrow> cs \<Rightarrow> thread list" 
+  where "wq s = wq_fun (schs s)"
+
+text {* \noindent 
+  The following @{text "cp"} is a shorthand for @{text "cprec_fun"}. 
+  *}
+definition cp :: "state \<Rightarrow> thread \<Rightarrow> precedence"
+  where "cp s \<equiv> cprec_fun (schs s)"
+
+text {* \noindent
+  Functions @{text "holding"}, @{text "waiting"}, @{text "RAG"} and 
+  @{text "dependants"} still have the 
+  same meaning, but redefined so that they no longer RAG on the 
+  fictitious {\em waiting queue function}
+  @{text "wq"}, but on system state @{text "s"}.
+  *}
+defs (overloaded) 
+  s_holding_abv: 
+  "holding (s::state) \<equiv> holding (wq_fun (schs s))"
+  s_waiting_abv: 
+  "waiting (s::state) \<equiv> waiting (wq_fun (schs s))"
+  s_RAG_abv: 
+  "RAG (s::state) \<equiv> RAG (wq_fun (schs s))"
+  s_dependants_abv: 
+  "dependants (s::state) \<equiv> dependants (wq_fun (schs s))"
+
+
+text {* 
+  The following lemma can be proved easily, and the meaning is obvious.
+  *}
+lemma
+  s_holding_def: 
+  "holding (s::state) th cs \<equiv> (th \<in> set (wq_fun (schs s) cs) \<and> th = hd (wq_fun (schs s) cs))"
+  by (auto simp:s_holding_abv wq_def cs_holding_def)
+
+lemma s_waiting_def: 
+  "waiting (s::state) th cs \<equiv> (th \<in> set (wq_fun (schs s) cs) \<and> th \<noteq> hd (wq_fun (schs s) cs))"
+  by (auto simp:s_waiting_abv wq_def cs_waiting_def)
+
+lemma s_RAG_def: 
+  "RAG (s::state) =
+    {(Th th, Cs cs) | th cs. waiting (wq s) th cs} \<union> {(Cs cs, Th th) | cs th. holding (wq s) th cs}"
+  by (auto simp:s_RAG_abv wq_def cs_RAG_def)
+
+lemma
+  s_dependants_def: 
+  "dependants (s::state) th \<equiv> {th' . (Th th', Th th) \<in> (RAG (wq s))^+}"
+  by (auto simp:s_dependants_abv wq_def cs_dependants_def)
+
+text {*
+  The following function @{text "readys"} calculates the set of ready threads. A thread is {\em ready} 
+  for running if it is a live thread and it is not waiting for any critical resource.
+  *}
+definition readys :: "state \<Rightarrow> thread set"
+  where "readys s \<equiv> {th . th \<in> threads s \<and> (\<forall> cs. \<not> waiting s th cs)}"
+
+text {* \noindent
+  The following function @{text "runing"} calculates the set of running thread, which is the ready 
+  thread with the highest precedence.  
+  *}
+definition runing :: "state \<Rightarrow> thread set"
+  where "runing s \<equiv> {th . th \<in> readys s \<and> cp s th = Max ((cp s) ` (readys s))}"
+
+text {* \noindent
+  Notice that the definition of @{text "running"} reflects the preemptive scheduling strategy,  
+  because, if the @{text "running"}-thread (the one in @{text "runing"} set) 
+  lowered its precedence by resetting its own priority to a lower
+  one, it will lose its status of being the max in @{text "ready"}-set and be superseded.
+*}
+
+text {* \noindent
+  The following function @{text "holdents s th"} returns the set of resources held by thread 
+  @{text "th"} in state @{text "s"}.
+  *}
+definition holdents :: "state \<Rightarrow> thread \<Rightarrow> cs set"
+  where "holdents s th \<equiv> {cs . holding s th cs}"
+
+lemma holdents_test: 
+  "holdents s th = {cs . (Cs cs, Th th) \<in> RAG s}"
+unfolding holdents_def
+unfolding s_RAG_def
+unfolding s_holding_abv
+unfolding wq_def
+by (simp)
+
+text {* \noindent
+  Observation @{text "cntCS s th"} returns the number of resources held by thread @{text "th"} in
+  state @{text "s"}:
+  *}
+definition cntCS :: "state \<Rightarrow> thread \<Rightarrow> nat"
+  where "cntCS s th = card (holdents s th)"
+
+text {* \noindent
+  According to the convention of Paulson's inductive method,
+  the decision made by a protocol that event @{text "e"} is eligible to happen next under state @{text "s"} 
+  is expressed as @{text "step s e"}. The predicate @{text "step"} is inductively defined as 
+  follows (notice how the decision is based on the {\em observation function}s 
+  defined above, and also notice how a complicated protocol is modeled by a few simple 
+  observations, and how such a kind of simplicity gives rise to improved trust on
+  faithfulness):
+  *}
+inductive step :: "state \<Rightarrow> event \<Rightarrow> bool"
+  where
+  -- {* 
+  A thread can be created if it is not a live thread:
+  *}
+  thread_create: "\<lbrakk>thread \<notin> threads s\<rbrakk> \<Longrightarrow> step s (Create thread prio)" |
+  -- {*
+  A thread can exit if it no longer hold any resource:
+  *}
+  thread_exit: "\<lbrakk>thread \<in> runing s; holdents s thread = {}\<rbrakk> \<Longrightarrow> step s (Exit thread)" |
+  -- {*
+  \begin{minipage}{0.9\textwidth}
+  A thread can request for an critical resource @{text "cs"}, if it is running and 
+  the request does not form a loop in the current RAG. The latter condition 
+  is set up to avoid deadlock. The condition also reflects our assumption all threads are 
+  carefully programmed so that deadlock can not happen:
+  \end{minipage}
+  *}
+  thread_P: "\<lbrakk>thread \<in> runing s;  (Cs cs, Th thread)  \<notin> (RAG s)^+\<rbrakk> \<Longrightarrow> 
+                                                                step s (P thread cs)" |
+  -- {*
+  \begin{minipage}{0.9\textwidth}
+  A thread can release a critical resource @{text "cs"} 
+  if it is running and holding that resource:
+  \end{minipage}
+  *}
+  thread_V: "\<lbrakk>thread \<in> runing s;  holding s thread cs\<rbrakk> \<Longrightarrow> step s (V thread cs)" |
+  -- {*
+  \begin{minipage}{0.9\textwidth}
+  A thread can adjust its own priority as long as it is current running. 
+  With the resetting of one thread's priority, its precedence may change. 
+  If this change lowered the precedence, according to the definition of @{text "running"}
+  function, 
+  \end{minipage}
+  *}  
+  thread_set: "\<lbrakk>thread \<in> runing s\<rbrakk> \<Longrightarrow> step s (Set thread prio)"
+
+text {*
+  In Paulson's inductive method, every protocol is defined by such a @{text "step"}
+  predicate. For instance, the predicate @{text "step"} given above 
+  defines the PIP protocol. So, it can also be called "PIP".
+*}
+
+abbreviation
+  "PIP \<equiv> step"
+
+
+text {* \noindent
+  For any protocol defined by a @{text "step"} predicate, 
+  the fact that @{text "s"} is a legal state in 
+  the protocol is expressed as: @{text "vt step s"}, where
+  the predicate @{text "vt"} can be defined as the following:
+  *}
+inductive vt :: "state \<Rightarrow> bool"
+  where
+  -- {* Empty list @{text "[]"} is a legal state in any protocol:*}
+  vt_nil[intro]: "vt []" |
+  -- {* 
+  \begin{minipage}{0.9\textwidth}
+  If @{text "s"} a legal state of the protocol defined by predicate @{text "step"}, 
+  and event @{text "e"} is allowed to happen under state @{text "s"} by the protocol 
+  predicate @{text "step"}, then @{text "e#s"} is a new legal state rendered by the 
+  happening of @{text "e"}:
+  \end{minipage}
+  *}
+  vt_cons[intro]: "\<lbrakk>vt s; step s e\<rbrakk> \<Longrightarrow> vt (e#s)"
+
+text {*  \noindent
+  It is easy to see that the definition of @{text "vt"} is generic. It can be applied to 
+  any specific protocol specified by a @{text "step"}-predicate to get the set of
+  legal states of that particular protocol.
+  *}
+
+text {* 
+  The following are two very basic properties of @{text "vt"}.
+*}
+
+lemma step_back_vt: "vt (e#s) \<Longrightarrow> vt s"
+  by(ind_cases "vt (e#s)", simp)
+
+lemma step_back_step: "vt (e#s) \<Longrightarrow> step s e"
+  by(ind_cases "vt (e#s)", simp)
+
+text {* \noindent
+  The following two auxiliary functions @{text "the_cs"} and @{text "the_th"} are used to extract
+  critical resource and thread respectively out of RAG nodes.
+  *}
+fun the_cs :: "node \<Rightarrow> cs"
+  where "the_cs (Cs cs) = cs"
+
+fun the_th :: "node \<Rightarrow> thread"
+  where "the_th (Th th) = th"
+
+text {* \noindent
+  The following predicate @{text "next_th"} describe the next thread to 
+  take over when a critical resource is released. In @{text "next_th s th cs t"}, 
+  @{text "th"} is the thread to release, @{text "t"} is the one to take over.
+  Notice how this definition is backed up by the @{text "release"} function and its use 
+  in the @{text "V"}-branch of @{text "schs"} function. This @{text "next_th"} function
+  is not needed for the execution of PIP. It is introduced as an auxiliary function 
+  to state lemmas. The correctness of this definition will be confirmed by 
+  lemmas @{text "step_v_hold_inv"}, @{text " step_v_wait_inv"}, 
+  @{text "step_v_get_hold"} and @{text "step_v_not_wait"}.
+  *}
+definition next_th:: "state \<Rightarrow> thread \<Rightarrow> cs \<Rightarrow> thread \<Rightarrow> bool"
+  where "next_th s th cs t = (\<exists> rest. wq s cs = th#rest \<and> rest \<noteq> [] \<and> 
+
+text {* \noindent
+  The aux function @{text "count Q l"} is used to count the occurrence of situation @{text "Q"}
+  in list @{text "l"}:
+  *}
+definition count :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> nat"
+  where "count Q l = length (filter Q l)"
+
+text {* \noindent
+  The following observation @{text "cntP s"} returns the number of operation @{text "P"} happened 
+  before reaching state @{text "s"}.
+  *}
+definition cntP :: "state \<Rightarrow> thread \<Rightarrow> nat"
+  where "cntP s th = count (\<lambda> e. \<exists> cs. e = P th cs) s"
+
+text {* \noindent
+  The following observation @{text "cntV s"} returns the number of operation @{text "V"} happened 
+  before reaching state @{text "s"}.
+  *}
+definition cntV :: "state \<Rightarrow> thread \<Rightarrow> nat"
+  where "cntV s th = count (\<lambda> e. \<exists> cs. e = V th cs) s"
+(*<*)
+
+end
+(*>*)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RTree.thy	Thu Dec 03 14:34:29 2015 +0800
@@ -0,0 +1,958 @@
+theory RTree
+imports "~~/src/HOL/Library/Transitive_Closure_Table"
+begin
+
+section {* A theory of relational trees *}
+
+inductive_cases path_nilE [elim!]: "rtrancl_path r x [] y"
+inductive_cases path_consE [elim!]: "rtrancl_path r x (z#zs) y"
+
+subsection {* Definitions *}
+
+text {*
+  In this theory, we are giving to give a notion of of `Relational Graph` and
+  its derived notion `Relational Tree`. Given a binary relation @{text "r"},
+  the `Relational Graph of @{text "r"}` is the graph, the edges of which
+  are those in @{text "r"}. In this way, any binary relation can be viewed
+  as a `Relational Graph`. Note, this notion of graph includes infinite graphs. 
+
+  A `Relation Graph` @{text "r"} is said to be a `Relational Tree` if it is both
+  {\em single valued} and {\em acyclic}. 
+*}
+
+text {*
+  The following @{text "sgv"} specifies that relation @{text "r"} is {\em single valued}.
+*}
+locale sgv = 
+  fixes r
+  assumes sgv: "single_valued r"
+
+text {*
+  The following @{text "rtree"} specifies that @{text "r"} is a 
+  {\em Relational Tree}.
+*}
+locale rtree = sgv + 
+  assumes acl: "acyclic r"
+
+text {* 
+  The following two auxiliary functions @{text "rel_of"} and @{text "pred_of"} 
+  transfer between the predicate and set representation of binary relations.
+*}
+
+definition "rel_of r = {(x, y) | x y. r x y}"
+
+definition "pred_of r = (\<lambda> x y. (x, y) \<in> r)"
+
+text {*
+  To reason about {\em Relational Graph}, a notion of path is 
+  needed, which is given by the following @{text "rpath"} (short 
+  for `relational path`). 
+  The path @{text "xs"} in proposition @{text "rpath r x xs y"} is 
+  a path leading from @{text "x"} to @{text "y"}, which serves as a 
+  witness of the fact @{text "(x, y) \<in> r^*"}. 
+
+  @{text "rpath"}
+  is simply a wrapper of the @{text "rtrancl_path"} defined in the imported 
+  theory @{text "Transitive_Closure_Table"}, which defines 
+  a notion of path for the predicate form of binary relations. 
+*}
+definition "rpath r x xs y = rtrancl_path (pred_of r) x xs y"
+
+text {*
+  Given a path @{text "ps"}, @{text "edges_on ps"} is the 
+  set of edges along the path, which is defined as follows:
+*}
+
+definition "edges_on ps = {(a,b) | a b. \<exists> xs ys. ps = xs@[a,b]@ys}"
+
+text {*
+   The following @{text "indep"} defines a notion of independence. 
+   Two nodes @{text "x"} and @{text "y"} are said to be independent
+   (expressed as @{text "indep x y"}),  if neither one is reachable 
+   from the other in relational graph @{text "r"}.
+*}
+definition "indep r x y = (((x, y) \<notin> r^*) \<and> ((y, x) \<notin> r^*))"
+
+text {*
+  In relational tree @{text "r"}, the sub tree of node @{text "x"} is written
+  @{text "subtree r x"}, which is defined to be the set of nodes (including itself) 
+  which can reach @{text "x"} by following some path in @{text "r"}:
+*}
+
+definition "subtree r x = {y . (y, x) \<in> r^*}"
+
+text {*
+  The following @{text "edge_in r x"} is the set of edges
+  contained in the sub-tree of @{text "x"}, with @{text "r"} as the underlying graph.
+*}
+
+definition "edges_in r x = {(a, b) | a b. (a, b) \<in> r \<and> b \<in> subtree r x}"
+
+text {*
+  The following lemma @{text "edges_in_meaning"} shows the intuitive meaning 
+  of `an edge @{text "(a, b)"} is in the sub-tree of @{text "x"}`, 
+  i.e., both @{text "a"} and @{text "b"} are in the sub-tree.
+*}
+lemma edges_in_meaning: 
+  "edges_in r x = {(a, b) | a b. (a, b) \<in> r \<and> a \<in> subtree r x \<and> b \<in> subtree r x}"
+proof -
+  { fix a b
+    assume h: "(a, b) \<in> r" "b \<in> subtree r x"
+    moreover have "a \<in> subtree r x"
+    proof -
+      from h(2)[unfolded subtree_def] have "(b, x) \<in> r^*" by simp
+      with h(1) have "(a, x) \<in> r^*" by auto
+      thus ?thesis by (auto simp:subtree_def)
+    qed
+    ultimately have "((a, b) \<in> r \<and> a \<in> subtree r x \<and> b \<in> subtree r x)" 
+      by (auto)
+  } thus ?thesis by (auto simp:edges_in_def)
+qed
+
+text {*
+  The following lemma shows the means of @{term "edges_in"} from the other side, 
+  which says to for the edge @{text "(a,b)"} to be outside of the sub-tree of @{text "x"}, 
+  it is sufficient if @{text "b"} is.
+*}
+lemma edges_in_refutation:
+  assumes "b \<notin> subtree r x"
+  shows "(a, b) \<notin> edges_in r x"
+  using assms by (unfold edges_in_def subtree_def, auto)
+
+subsection {* Auxiliary lemmas *}
+
+lemma index_minimize:
+  assumes "P (i::nat)"
+  obtains j where "P j" and "\<forall> k < j. \<not> P k" 
+proof -
+  have "\<exists> j. P j \<and> (\<forall> k < j. \<not> P k)"
+  using assms
+  proof(induct i rule:less_induct)
+    case (less t)
+    show ?case
+    proof(cases "\<forall> j < t. \<not> P j")
+      case True
+      with less (2) show ?thesis by blast
+    next
+      case False
+      then obtain j where "j < t" "P j" by auto
+      from less(1)[OF this]
+      show ?thesis .
+    qed
+  qed 
+  with that show ?thesis by metis
+qed
+
+subsection {* Properties of Relational Graphs and Relational Trees *}
+
+subsubsection {* Properties of @{text "rel_of"} and @{text "pred_of"} *}
+
+text {* The following lemmas establish bijectivity of the two functions *}
+
+lemma pred_rel_eq: "pred_of (rel_of r) = r" by (auto simp:rel_of_def pred_of_def)
+
+lemma rel_pred_eq: "rel_of (pred_of r) = r" by (auto simp:rel_of_def pred_of_def)
+
+lemma rel_of_star: "rel_of (r^**) = (rel_of r)^*"
+  by (unfold rel_of_def rtranclp_rtrancl_eq, auto)
+
+lemma pred_of_star: "pred_of (r^*) = (pred_of r)^**"
+proof -
+  { fix x y
+    have "pred_of (r^*) x y = (pred_of r)^** x y"
+    by (unfold pred_of_def rtranclp_rtrancl_eq, auto)
+  } thus ?thesis by auto
+qed
+
+lemma star_2_pstar: "(x, y) \<in> r^* = (pred_of (r^*)) x y"
+  by (simp add: pred_of_def)
+
+subsubsection {* Properties of @{text "rpath"} *}
+
+text {* Induction rule for @{text "rpath"}: *}
+
+print_statement rtrancl_path.induct
+
+lemma rpath_induct [consumes 1, case_names rbase rstep, induct pred: rpath]:
+  assumes "rpath r x1 x2 x3"
+    and "\<And>x. P x [] x"
+    and "\<And>x y ys z. (x, y) \<in> r \<Longrightarrow> rpath r y ys z \<Longrightarrow> P y ys z \<Longrightarrow> P x (y # ys) z"
+  shows "P x1 x2 x3"
+  using assms[unfolded rpath_def]
+  by (induct, auto simp:pred_of_def rpath_def)
+
+text {* Introduction rule for empty path *}
+lemma rbaseI [intro!]: 
+  assumes "x = y"
+  shows "rpath r x [] y"
+  by  (unfold rpath_def assms, 
+         rule Transitive_Closure_Table.rtrancl_path.base)
+
+text {* Introduction rule for non-empty path *}
+lemma rstepI [intro!]: 
+  assumes "(x, y) \<in> r"
+    and "rpath r y ys z"
+  shows "rpath r x (y#ys) z" 
+proof(unfold rpath_def, rule Transitive_Closure_Table.rtrancl_path.step)
+  from assms(1) show "pred_of r x y" by (auto simp:pred_of_def)
+next
+  from assms(2) show "rtrancl_path (pred_of r) y ys z"  
+  by (auto simp:pred_of_def rpath_def)
+qed
+
+text {* Introduction rule for @{text "@"}-path *}
+lemma rpath_appendI [intro]: 
+  assumes "rpath r x xs a" and "rpath r a ys y"
+  shows "rpath r x (xs @ ys) y"
+  using assms 
+  by (unfold rpath_def, auto intro:rtrancl_path_trans)
+
+text {* Elimination rule for empty path *}
+
+lemma rpath_cases [cases pred:rpath]:
+  assumes "rpath r a1 a2 a3"
+  obtains (rbase)  "a1 = a3" and "a2 = []"
+    | (rstep)  y :: "'a" and ys :: "'a list"  
+         where "(a1, y) \<in> r" and "a2 = y # ys" and "rpath r y ys a3"
+  using assms [unfolded rpath_def]
+  by (cases, auto simp:rpath_def pred_of_def)
+
+lemma rpath_nilE [elim!, cases pred:rpath]: 
+  assumes "rpath r x [] y"
+  obtains "y = x"
+  using assms[unfolded rpath_def] by auto
+
+-- {* This is a auxiliary lemmas used only in the proof of @{text "rpath_nnl_lastE"} *}
+lemma rpath_nnl_last:
+  assumes "rtrancl_path r x xs y"
+  and "xs \<noteq> []"
+  obtains xs' where "xs = xs'@[y]"
+proof -
+  from append_butlast_last_id[OF `xs \<noteq> []`, symmetric] 
+  obtain xs' y' where eq_xs: "xs = (xs' @ y' # [])" by simp
+  with assms(1)
+  have "rtrancl_path r x ... y" by simp
+  hence "y = y'" by (rule rtrancl_path_appendE, auto)
+  with eq_xs have "xs = xs'@[y]" by simp
+  from that[OF this] show ?thesis .
+qed
+
+text {*
+  Elimination rule for non-empty paths constructed with @{text "#"}.
+*}
+
+lemma rpath_ConsE [elim!, cases pred:rpath]:
+  assumes "rpath r x (y # ys) x2"
+  obtains (rstep) "(x, y) \<in> r" and "rpath r y ys x2"
+  using assms[unfolded rpath_def]
+  by (cases, auto simp:rpath_def pred_of_def)
+
+text {*
+  Elimination rule for non-empty path, where the destination node 
+  @{text "y"} is shown to be at the end of the path.
+*}
+lemma rpath_nnl_lastE: 
+  assumes "rpath r x xs y"
+  and "xs \<noteq> []"
+  obtains xs' where "xs = xs'@[y]"
+  using assms[unfolded rpath_def]
+  by (rule rpath_nnl_last, auto)
+
+text {* Other elimination rules of @{text "rpath"} *}
+
+lemma rpath_appendE:
+  assumes "rpath r x (xs @ [a] @ ys) y"
+  obtains "rpath r x (xs @ [a]) a" and "rpath r a ys y"
+  using rtrancl_path_appendE[OF assms[unfolded rpath_def, simplified], folded rpath_def]
+  by auto
+
+lemma rpath_subE: 
+  assumes "rpath r x (xs @ [a] @ ys @ [b] @ zs) y"
+  obtains "rpath r x (xs @ [a]) a" and "rpath r a (ys @ [b]) b" and "rpath r b zs y" 
+  using assms
+ by (elim rpath_appendE, auto)
+
+text {* Every path has a unique end point. *}
+lemma rpath_dest_eq:
+  assumes "rpath r x xs x1"
+  and "rpath r x xs x2"
+  shows "x1 = x2"
+  using assms
+  by (induct, auto)
+
+subsubsection {* Properites of @{text "edges_on"} *}
+
+lemma edges_on_len:
+  assumes "(a,b) \<in> edges_on l"
+  shows "length l \<ge> 2"
+  using assms
+  by (unfold edges_on_def, auto)
+
+text {* Elimination of @{text "edges_on"} for non-empty path *}
+lemma edges_on_consE [elim, cases set:edges_on]:
+  assumes "(a,b) \<in> edges_on (x#xs)"
+  obtains (head)  xs' where "x = a" and "xs = b#xs'"
+      |  (tail)  "(a,b) \<in> edges_on xs"
+proof -
+  from assms obtain l1 l2 
+  where h: "(x#xs) = l1 @ [a,b] @ l2" by (unfold edges_on_def, blast)
+  have "(\<exists> xs'. x = a \<and> xs = b#xs') \<or> ((a,b) \<in> edges_on xs)"
+  proof(cases "l1")
+    case Nil with h 
+    show ?thesis by auto
+  next
+    case (Cons e el)
+    from h[unfolded this] 
+    have "xs = el @ [a,b] @ l2" by auto
+    thus ?thesis 
+      by (unfold edges_on_def, auto)
+  qed
+  thus ?thesis 
+  proof
+    assume "(\<exists>xs'. x = a \<and> xs = b # xs')"
+    then obtain xs' where "x = a" "xs = b#xs'" by blast
+    from that(1)[OF this] show ?thesis .
+  next
+    assume "(a, b) \<in> edges_on xs"
+    from that(2)[OF this] show ?thesis .
+  qed
+qed
+
+text {*
+  Every edges on the path is a graph edges:
+*}
+lemma rpath_edges_on:
+  assumes "rpath r x xs y"
+  shows "(edges_on (x#xs)) \<subseteq> r"
+  using assms
+proof(induct arbitrary:y)
+  case (rbase x)
+  thus ?case by (unfold edges_on_def, auto)
+next
+  case (rstep x y ys z)
+  show ?case
+  proof -
+    { fix a b
+      assume "(a, b) \<in> edges_on (x # y # ys)"
+      hence "(a, b) \<in> r" by (cases, insert rstep, auto)
+    } thus ?thesis by auto
+  qed
+qed
+
+text {* @{text "edges_on"} is mono with respect to @{text "#"}-operation: *}
+lemma edges_on_Cons_mono:
+   shows "edges_on xs \<subseteq> edges_on (x#xs)"
+proof -
+  { fix a b
+    assume "(a, b) \<in> edges_on xs"
+    then obtain l1 l2 where "xs = l1 @ [a,b] @ l2" 
+      by (auto simp:edges_on_def)
+    hence "x # xs = (x#l1) @ [a, b] @ l2" by auto
+    hence "(a, b) \<in> edges_on (x#xs)" 
+      by (unfold edges_on_def, blast)
+  } thus ?thesis by auto
+qed
+
+text {*
+  The following rule @{text "rpath_transfer"} is used to show 
+  that one path is intact as long as all the edges on it are intact
+  with the change of graph.
+
+  If @{text "x#xs"} is path in graph @{text "r1"} and 
+  every edges along the path is also in @{text "r2"}, 
+  then @{text "x#xs"} is also a edge in graph @{text "r2"}:
+*}
+
+lemma rpath_transfer:
+  assumes "rpath r1 x xs y"
+  and "edges_on (x#xs) \<subseteq> r2"
+  shows "rpath r2 x xs y"
+  using assms
+proof(induct)
+  case (rstep x y ys z)
+  show ?case 
+  proof(rule rstepI)
+    show "(x, y) \<in> r2"
+    proof -
+      have "(x, y) \<in> edges_on  (x # y # ys)"
+          by (unfold edges_on_def, auto)
+     with rstep(4) show ?thesis by auto
+    qed
+  next
+    show "rpath r2 y ys z" 
+     using rstep edges_on_Cons_mono[of "y#ys" "x"] by (auto)
+  qed
+qed (unfold rpath_def, auto intro!:Transitive_Closure_Table.rtrancl_path.base)
+
+
+text {*
+  The following lemma extracts the path from @{text "x"} to @{text "y"}
+  from proposition @{text "(x, y) \<in> r^*"}
+*}
+lemma star_rpath:
+  assumes "(x, y) \<in> r^*"
+  obtains xs where "rpath r x xs y"
+proof -
+  have "\<exists> xs. rpath r x xs y"
+  proof(unfold rpath_def, rule iffD1[OF rtranclp_eq_rtrancl_path])
+    from assms
+    show "(pred_of r)\<^sup>*\<^sup>* x y"
+      apply (fold pred_of_star)
+      by (auto simp:pred_of_def)
+  qed
+  from that and this show ?thesis by blast
+qed
+
+text {*
+  The following lemma uses the path @{text "xs"} from @{text "x"} to @{text "y"}
+  as a witness to show @{text "(x, y) \<in> r^*"}.
+*}
+lemma rpath_star: 
+  assumes "rpath r x xs y"
+  shows "(x, y) \<in> r^*"
+proof -
+  from iffD2[OF rtranclp_eq_rtrancl_path] and assms[unfolded rpath_def]
+  have "(pred_of r)\<^sup>*\<^sup>* x y" by metis
+  thus ?thesis by (simp add: pred_of_star star_2_pstar)
+qed
+
+text {*
+  The following lemmas establishes a relation from pathes in @{text "r"}
+  to @{text "r^+"} relation.
+*}
+lemma rpath_plus: 
+  assumes "rpath r x xs y"
+  and "xs \<noteq> []"
+  shows "(x, y) \<in> r^+"
+proof -
+  from assms(2) obtain e es where "xs = e#es" by (cases xs, auto)
+  from assms(1)[unfolded this]
+  show ?thesis
+  proof(cases)
+    case rstep
+    show ?thesis
+    proof -
+      from rpath_star[OF rstep(2)] have "(e, y) \<in> r\<^sup>*" .
+      with rstep(1) show "(x, y) \<in> r^+" by auto
+    qed
+  qed
+qed
+
+subsubsection {* Properties of @{text "subtree"} *}
+
+text {*
+  @{text "subtree"} is mono with respect to the underlying graph.
+*}
+lemma subtree_mono:
+  assumes "r1 \<subseteq> r2"
+  shows "subtree r1 x \<subseteq> subtree r2 x"
+proof
+  fix c
+  assume "c \<in> subtree r1 x"
+  hence "(c, x) \<in> r1^*" by (auto simp:subtree_def)
+  from star_rpath[OF this] obtain xs 
+  where rp:"rpath r1 c xs x" by metis
+  hence "rpath r2 c xs x"
+  proof(rule rpath_transfer)
+    from rpath_edges_on[OF rp] have "edges_on (c # xs) \<subseteq> r1" .
+    with assms show "edges_on (c # xs) \<subseteq> r2" by auto
+  qed
+  thus "c \<in> subtree r2 x"
+    by (rule rpath_star[elim_format], auto simp:subtree_def)
+qed
+
+text {*
+  The following lemma characterizes the change of sub-tree of @{text "x"}
+  with the removal of an outside edge @{text "(a,b)"}. 
+
+  Note that, according to lemma @{thm edges_in_refutation}, the assumption
+  @{term "b \<notin> subtree r x"} amounts to saying @{text "(a, b)"} 
+  is outside the sub-tree of @{text "x"}.
+*}
+lemma subtree_del_outside: (* ddd *)
+    assumes "b \<notin> subtree r x" 
+    shows "subtree (r - {(a, b)}) x = (subtree r x)" 
+proof -
+  { fix c
+    assume "c \<in> (subtree r x)"
+    hence "(c, x) \<in> r^*" by (auto simp:subtree_def)
+    hence "c \<in> subtree (r - {(a, b)}) x"
+    proof(rule star_rpath)
+      fix xs
+      assume rp: "rpath r c xs x"
+      show ?thesis
+      proof -
+        from rp
+        have "rpath  (r - {(a, b)}) c xs x"
+        proof(rule rpath_transfer)
+          from rpath_edges_on[OF rp] have "edges_on (c # xs) \<subseteq> r" .
+          moreover have "(a, b) \<notin> edges_on (c#xs)"
+          proof
+            assume "(a, b) \<in> edges_on (c # xs)"
+            then obtain l1 l2 where h: "c#xs = l1@[a,b]@l2" by (auto simp:edges_on_def)
+            hence "tl (c#xs) = tl (l1@[a,b]@l2)" by simp
+            then obtain l1' where eq_xs_b: "xs = l1'@[b]@l2" by (cases l1, auto)
+            from rp[unfolded this]
+            show False
+            proof(rule rpath_appendE)
+              assume "rpath r b l2 x"
+              thus ?thesis
+              by(rule rpath_star[elim_format], insert assms(1), auto simp:subtree_def)
+            qed
+          qed
+          ultimately show "edges_on (c # xs) \<subseteq> r - {(a,b)}" by auto
+        qed
+        thus ?thesis by (rule rpath_star[elim_format], auto simp:subtree_def)
+      qed
+    qed
+  } moreover {
+    fix c
+    assume "c \<in> subtree (r - {(a, b)}) x"
+    moreover have "... \<subseteq> (subtree r x)" by (rule subtree_mono, auto)
+    ultimately have "c \<in> (subtree r x)" by auto
+  } ultimately show ?thesis by auto
+qed
+
+lemma subtree_insert_ext:
+    assumes "b \<in> subtree r x"
+    shows "subtree (r \<union> {(a, b)}) x = (subtree r x) \<union> (subtree r a)" 
+    using assms by (auto simp:subtree_def rtrancl_insert)
+
+lemma subtree_insert_next:
+    assumes "b \<notin> subtree r x"
+    shows "subtree (r \<union> {(a, b)}) x = (subtree r x)" 
+    using assms
+    by (auto simp:subtree_def rtrancl_insert)
+
+subsubsection {* Properties about relational trees *}
+
+context rtree 
+begin
+
+lemma rpath_overlap_oneside: (* ddd *)
+  assumes "rpath r x xs1 x1"
+  and "rpath r x xs2 x2"
+  and "length xs1 \<le> length xs2"
+  obtains xs3 where "xs2 = xs1 @ xs3"
+proof(cases "xs1 = []")
+  case True
+  with that show ?thesis by auto
+next
+  case False
+  have "\<forall> i \<le> length xs1. take i xs1 = take i xs2"
+  proof -
+     { assume "\<not> (\<forall> i \<le> length xs1. take i xs1 = take i xs2)"
+       then obtain i where "i \<le> length xs1 \<and> take i xs1 \<noteq> take i xs2" by auto
+       from this(1) have "False"
+       proof(rule index_minimize)
+          fix j
+          assume h1: "j \<le> length xs1 \<and> take j xs1 \<noteq> take j xs2"
+          and h2: " \<forall>k<j. \<not> (k \<le> length xs1 \<and> take k xs1 \<noteq> take k xs2)"
+          -- {* @{text "j - 1"} is the branch point between @{text "xs1"} and @{text "xs2"} *}
+          let ?idx = "j - 1"
+          -- {* A number of inequalities concerning @{text "j - 1"} are derived first *}
+          have lt_i: "?idx < length xs1" using False h1 
+            by (metis Suc_diff_1 le_neq_implies_less length_greater_0_conv lessI less_imp_diff_less)
+          have lt_i': "?idx < length xs2" using lt_i and assms(3) by auto
+          have lt_j: "?idx < j" using h1 by (cases j, auto)
+          -- {* From thesis inequalities, a number of equations concerning @{text "xs1"}
+                 and @{text "xs2"} are derived *}
+          have eq_take: "take ?idx xs1 = take ?idx xs2"
+            using h2[rule_format, OF lt_j] and h1 by auto
+          have eq_xs1: " xs1 = take ?idx xs1 @ xs1 ! (?idx) # drop (Suc (?idx)) xs1" 
+            using id_take_nth_drop[OF lt_i] .
+          have eq_xs2: "xs2 = take ?idx xs2 @ xs2 ! (?idx) # drop (Suc (?idx)) xs2" 
+              using id_take_nth_drop[OF lt_i'] .
+          -- {* The branch point along the path is finally pinpointed *}
+          have neq_idx: "xs1!?idx \<noteq> xs2!?idx" 
+          proof -
+            have "take j xs1 = take ?idx xs1 @ [xs1 ! ?idx]"
+                using eq_xs1 Suc_diff_1 lt_i lt_j take_Suc_conv_app_nth by fastforce 
+            moreover have eq_tk2: "take j xs2 = take ?idx xs2 @ [xs2 ! ?idx]"
+                using Suc_diff_1 lt_i' lt_j take_Suc_conv_app_nth by fastforce 
+            ultimately show ?thesis using eq_take h1 by auto
+          qed
+          show ?thesis
+          proof(cases " take (j - 1) xs1 = []")
+            case True
+            have "(x, xs1!?idx) \<in> r"
+            proof -
+                from eq_xs1[unfolded True, simplified, symmetric] assms(1) 
+                have "rpath r x ( xs1 ! ?idx # drop (Suc ?idx) xs1) x1" by simp
+                from this[unfolded rpath_def]
+                show ?thesis by (auto simp:pred_of_def)
+            qed
+            moreover have "(x, xs2!?idx) \<in> r"
+            proof -
+              from eq_xs2[folded eq_take, unfolded True, simplified, symmetric] assms(2)
+              have "rpath r x ( xs2 ! ?idx # drop (Suc ?idx) xs2) x2" by simp
+              from this[unfolded rpath_def]
+              show ?thesis by (auto simp:pred_of_def)
+            qed
+            ultimately show ?thesis using neq_idx sgv[unfolded single_valued_def] by metis
+        next
+           case False
+           then obtain e es where eq_es: "take ?idx xs1 = es@[e]" 
+            using rev_exhaust by blast 
+           have "(e, xs1!?idx) \<in> r"
+           proof -
+            from eq_xs1[unfolded eq_es] 
+            have "xs1 = es@[e, xs1!?idx]@drop (Suc ?idx) xs1" by simp
+            hence "(e, xs1!?idx) \<in> edges_on xs1" by (simp add:edges_on_def, metis)
+            with rpath_edges_on[OF assms(1)] edges_on_Cons_mono[of xs1 x]
+            show ?thesis by auto
+           qed moreover have "(e, xs2!?idx) \<in> r"
+           proof -
+            from eq_xs2[folded eq_take, unfolded eq_es]
+            have "xs2 = es@[e, xs2!?idx]@drop (Suc ?idx) xs2" by simp
+            hence "(e, xs2!?idx) \<in> edges_on xs2" by (simp add:edges_on_def, metis)
+            with rpath_edges_on[OF assms(2)] edges_on_Cons_mono[of xs2 x]
+            show ?thesis by auto
+           qed
+           ultimately show ?thesis 
+              using sgv[unfolded single_valued_def] neq_idx by metis
+        qed
+       qed
+     } thus ?thesis by auto
+  qed
+  from this[rule_format, of "length xs1"]
+  have "take (length xs1) xs1 = take (length xs1) xs2" by simp
+  moreover have "xs2 = take (length xs1) xs2 @ drop (length xs1) xs2" by simp
+  ultimately have "xs2 = xs1 @ drop (length xs1) xs2" by auto
+  from that[OF this] show ?thesis .
+qed
+
+lemma rpath_overlap [consumes 2, cases pred:rpath]:
+  assumes "rpath r x xs1 x1"
+  and "rpath r x xs2 x2"
+  obtains (less_1) xs3 where "xs2 = xs1 @ xs3"
+     |    (less_2) xs3 where "xs1 = xs2 @ xs3"
+proof -
+  have "length xs1 \<le> length xs2 \<or> length xs2 \<le> length xs1" by auto
+  with assms rpath_overlap_oneside that show ?thesis by metis
+qed
+
+text {*
+  As a corollary of @{thm "rpath_overlap_oneside"}, 
+  the following two lemmas gives one important property of relation tree, 
+  i.e. there is at most one path between any two nodes.
+  Similar to the proof of @{thm rpath_overlap}, we starts with
+  the one side version first.
+*}
+
+lemma rpath_unique_oneside:
+  assumes "rpath r x xs1 y"
+    and "rpath r x xs2 y"
+    and "length xs1 \<le> length xs2"
+  shows "xs1 = xs2"
+proof -
+  from rpath_overlap_oneside[OF assms] 
+  obtain xs3 where less_1: "xs2 = xs1 @ xs3" by blast
+  show ?thesis
+  proof(cases "xs3 = []") 
+    case True
+    from less_1[unfolded this] show ?thesis by simp
+  next
+    case False
+    note FalseH = this
+    show ?thesis
+    proof(cases "xs1 = []")
+      case True
+      have "(x, x) \<in> r^+"
+      proof(rule rpath_plus)
+        from assms(1)[unfolded True] 
+        have "y = x" by (cases rule:rpath_nilE, simp)
+        from assms(2)[unfolded this] show "rpath r x xs2 x" .
+      next
+        from less_1 and False show "xs2 \<noteq> []" by simp
+      qed
+      with acl show ?thesis by (unfold acyclic_def, auto)
+    next 
+      case False
+      then obtain e es where eq_xs1: "xs1 = es@[e]" using rev_exhaust by auto
+      from assms(2)[unfolded less_1 this]
+      have "rpath r x (es @ [e] @ xs3) y" by simp
+      thus ?thesis
+      proof(cases rule:rpath_appendE)
+        case 1
+        from rpath_dest_eq [OF 1(1)[folded eq_xs1] assms(1)]
+        have "e = y" .
+        from rpath_plus [OF 1(2)[unfolded this] FalseH]
+        have "(y, y) \<in> r^+" .
+        with acl show ?thesis by (unfold acyclic_def, auto)
+      qed
+    qed
+  qed
+qed
+
+text {*
+  The following is the full version of path uniqueness.
+*}
+lemma rpath_unique:
+  assumes "rpath r x xs1 y"
+    and "rpath r x xs2 y"
+  shows "xs1 = xs2"
+proof(cases "length xs1 \<le> length xs2")
+   case True
+   from rpath_unique_oneside[OF assms this] show ?thesis .
+next
+  case False
+  hence "length xs2 \<le> length xs1" by simp
+  from rpath_unique_oneside[OF assms(2,1) this]
+  show ?thesis by simp
+qed
+
+text {*
+  The following lemma shows that the `independence` relation is symmetric.
+  It is an obvious auxiliary lemma which will be used later. 
+*}
+lemma sym_indep: "indep r x y \<Longrightarrow> indep r y x"
+  by (unfold indep_def, auto)
+
+text {*
+  This is another `obvious` lemma about trees, which says trees rooted at 
+  independent nodes are disjoint.
+*}
+lemma subtree_disjoint:
+  assumes "indep r x y"
+  shows "subtree r x \<inter> subtree r y = {}"
+proof -
+  { fix z x y xs1 xs2 xs3
+      assume ind: "indep r x y"
+      and rp1: "rpath r z xs1 x"
+      and rp2: "rpath r z xs2 y"
+      and h: "xs2 = xs1 @ xs3"
+      have False
+      proof(cases "xs1 = []")
+        case True
+        from rp1[unfolded this] have "x = z" by auto
+        from rp2[folded this] rpath_star ind[unfolded indep_def]
+        show ?thesis by metis
+      next
+        case False
+        then obtain e es where eq_xs1: "xs1 = es@[e]" using rev_exhaust by blast
+        from rp2[unfolded h this]
+        have "rpath r z (es @ [e] @ xs3) y" by simp
+        thus ?thesis
+        proof(cases rule:rpath_appendE)
+          case 1
+          have "e = x" using 1(1)[folded eq_xs1] rp1 rpath_dest_eq by metis
+          from rpath_star[OF 1(2)[unfolded this]] ind[unfolded indep_def]
+          show ?thesis by auto
+        qed
+      qed
+  } note my_rule = this
+  { fix z
+    assume h: "z \<in> subtree r x" "z \<in> subtree r y"
+    from h(1) have "(z, x) \<in> r^*" by (unfold subtree_def, auto)
+    then obtain xs1 where rp1: "rpath r z xs1 x" using star_rpath by metis
+    from h(2) have "(z, y) \<in> r^*" by (unfold subtree_def, auto)
+    then obtain xs2 where rp2: "rpath r z xs2 y" using star_rpath by metis
+    from rp1 rp2
+    have False
+    by (cases, insert my_rule[OF sym_indep[OF assms(1)] rp2 rp1] 
+                  my_rule[OF assms(1) rp1 rp2], auto)
+  } thus ?thesis by auto
+qed
+
+text {*
+  The following lemma @{text "subtree_del"} characterizes the change of sub-tree of 
+  @{text "x"} with the removal of an inside edge @{text "(a, b)"}. 
+  Note that, the case for the removal of an outside edge has already been dealt with
+  in lemma @{text "subtree_del_outside"}). 
+
+  This lemma is underpinned by the following two `obvious` facts:
+  \begin{enumearte}
+  \item
+  In graph @{text "r"}, for an inside edge @{text "(a,b) \<in> edges_in r x"},  
+  every node @{text "c"} in the sub-tree of @{text "a"} has a path
+  which goes first from @{text "c"} to @{text "a"}, then through edge @{text "(a, b)"}, and 
+  finally reaches @{text "x"}. By the uniqueness of path in a tree,
+  all paths from sub-tree of @{text "a"} to @{text "x"} are such constructed, therefore 
+  must go through @{text "(a, b)"}. The consequence is: with the removal of @{text "(a,b)"},
+  all such paths will be broken. 
+
+  \item
+  On the other hand, all paths not originate from within the sub-tree of @{text "a"}
+  will not be affected by the removal of edge @{text "(a, b)"}. 
+  The reason is simple: if the path is affected by the removal, it must 
+  contain @{text "(a, b)"}, then it must originate from within the sub-tree of @{text "a"}.
+  \end{enumearte}
+*}
+
+lemma subtree_del_inside: (* ddd *)
+    assumes "(a,b) \<in> edges_in r x"
+    shows "subtree (r - {(a, b)}) x = (subtree r x) - subtree r a"
+proof -
+  from assms have asm: "b \<in> subtree r x" "(a, b) \<in> r" by (auto simp:edges_in_def)
+  -- {* The proof follows a common pattern to prove the equality of sets. *}
+  { -- {* The `left to right` direction.
+       *}
+    fix c
+    -- {* Assuming @{text "c"} is inside the sub-tree of @{text "x"} in the reduced graph *}
+    assume h: "c \<in> subtree (r - {(a, b)}) x" 
+    -- {* We are going to show that @{text "c"} can not be in the sub-tree of @{text "a"} in 
+          the original graph. *}
+    -- {* In other words, all nodes inside the sub-tree of @{text "a"} in the original 
+          graph will be removed from the sub-tree of @{text "x"} in the reduced graph. *}
+    -- {* The reason, as analyzed before, is that all paths from within the 
+          sub-tree of @{text "a"} are broken with the removal of edge @{text "(a,b)"}.
+       *}
+    have "c \<in> (subtree r x) - subtree r a" 
+    proof -
+      let ?r' = "r - {(a, b)}" -- {* The reduced graph is abbreviated as @{text "?r'"} *}
+      from h have "(c, x) \<in> ?r'^*" by (auto simp:subtree_def)
+      -- {* Extract from the reduced graph the path @{text "xs"} from @{text "c"} to @{text "x"}. *}
+      then obtain xs where rp0: "rpath ?r' c xs x" by (rule star_rpath, auto)
+      -- {* It is easy to show @{text "xs"} is also a path in the original graph *}
+      hence rp1: "rpath r c xs x"
+      proof(rule rpath_transfer)
+          from rpath_edges_on[OF rp0] 
+          show "edges_on (c # xs) \<subseteq> r" by auto
+      qed
+      -- {* @{text "xs"} is used as the witness to show that @{text "c"} 
+                   in the sub-tree of @{text "x"} in the original graph. *}
+      hence "c \<in> subtree r x"
+         by (rule rpath_star[elim_format], auto simp:subtree_def)
+      -- {* The next step is to show that @{text "c"} can not be in the sub-tree of @{text "a"}
+            in the original graph. *}
+      -- {* We need to use the fact that all paths originate from within sub-tree of @{text "a"}
+             are broken. *}
+      moreover have "c \<notin> subtree r a"
+      proof
+        -- {* Proof by contradiction, suppose otherwise *}
+        assume otherwise: "c \<in> subtree r a"
+        -- {* Then there is a path in original graph leading from @{text "c"} to @{text "a"} *}
+        obtain xs1 where rp_c: "rpath r c xs1 a" 
+        proof -
+          from otherwise have "(c, a) \<in> r^*" by (auto simp:subtree_def)
+          thus ?thesis by (rule star_rpath, auto intro!:that)
+        qed
+        -- {* Starting from this path, we are going to construct a fictional 
+                  path from @{text "c"} to @{text "x"}, which, as explained before,
+              is broken, so that contradiction can be derived. *}
+        -- {* First, there is a path from @{text "b"} to @{text "x"} *}
+        obtain ys where rp_b: "rpath r b ys x" 
+        proof -
+          from asm have "(b, x) \<in> r^*" by (auto simp:subtree_def)
+          thus ?thesis by (rule star_rpath, auto intro!:that)
+        qed
+        -- {* The paths @{text "xs1"} and @{text "ys"} can be 
+                 tied together using @{text "(a,b)"} to form a path 
+               from @{text "c"} to @{text "x"}: *}
+        have "rpath r c (xs1 @ b # ys) x"
+        proof -
+          from rstepI[OF asm(2) rp_b] have "rpath r a (b # ys) x" .
+          from rpath_appendI[OF rp_c this]
+          show ?thesis .
+        qed
+        -- {* By the uniqueness of path between two nodes of a tree, we have: *}
+        from rpath_unique[OF rp1 this] have eq_xs: "xs = xs1 @ b # ys" .
+        -- {* Contradiction can be derived from from this fictional path . *}
+        show False
+        proof -
+          -- {* It can be shown that @{term "(a,b)"} is on this fictional path. *}
+          have "(a, b) \<in> edges_on (c#xs)"
+          proof(cases "xs1 = []")
+            case True
+            from rp_c[unfolded this] have "rpath r c [] a" .
+            hence eq_c: "c = a" by (rule rpath_nilE, simp)
+            hence "c#xs = a#xs" by simp
+            from this and eq_xs have "c#xs = a # xs1 @ b # ys" by simp
+            from this[unfolded True] have "c#xs = []@[a,b]@ys" by simp
+            thus ?thesis by (auto simp:edges_on_def)
+          next
+            case False
+            from rpath_nnl_lastE[OF rp_c this]
+            obtain xs' where "xs1 = xs'@[a]" by auto
+            from eq_xs[unfolded this] have "c#xs = (c#xs')@[a,b]@ys" by simp
+            thus ?thesis by (unfold edges_on_def, blast)
+          qed
+          -- {* It can also be shown that @{term "(a,b)"} is not on this fictional path. *}
+          moreover have "(a, b) \<notin> edges_on (c#xs)"
+              using rpath_edges_on[OF rp0] by auto
+          -- {* Contradiction is thus derived. *}
+          ultimately show False by auto
+        qed
+      qed
+      ultimately show ?thesis by auto
+    qed
+  } moreover {
+    -- {* The `right to left` direction.
+       *} 
+     fix c
+   -- {* Assuming that @{text "c"} is in the sub-tree of @{text "x"}, but
+         outside of the sub-tree of @{text "a"} in the original graph, *}
+   assume h: "c \<in> (subtree r x) - subtree r a"
+   -- {* we need to show that in the reduced graph, @{text "c"} is still in 
+         the sub-tree of @{text "x"}. *}
+   have "c \<in> subtree (r - {(a, b)}) x"
+   proof -
+      -- {* The proof goes by showing that the path from @{text "c"} to @{text "x"}
+            in the original graph is not affected by the removal of @{text "(a,b)"}.
+         *}
+      from h have "(c, x) \<in> r^*" by (unfold subtree_def, auto)
+      -- {* Extract the path @{text "xs"} from @{text "c"} to @{text "x"} in the original graph. *}
+      from star_rpath[OF this] obtain xs where rp: "rpath r c xs x" by auto
+      -- {* Show that it is also a path in the reduced graph. *}
+      hence "rpath (r - {(a, b)}) c xs x"
+      -- {* The proof goes by using rule @{thm rpath_transfer} *} 
+      proof(rule rpath_transfer)
+        -- {* We need to show all edges on the path are still in the reduced graph. *}
+        show "edges_on (c # xs) \<subseteq> r - {(a, b)}"
+        proof -
+          -- {* It is easy to show that all the edges are in the original graph. *}
+          from rpath_edges_on [OF rp] have " edges_on (c # xs) \<subseteq> r" .
+          -- {* The essential part is to show that @{text "(a, b)"} is not on the path. *}
+          moreover have "(a,b) \<notin> edges_on (c#xs)"
+          proof
+            -- {* Proof by contradiction, suppose otherwise: *}
+            assume otherwise: "(a, b) \<in> edges_on (c#xs)"
+            -- {* Then @{text "(a, b)"} is in the middle of the path. 
+                  with @{text "l1"} and @{text "l2"} be the nodes in 
+                  the front and rear respectively. *}
+              then obtain l1 l2 where eq_xs: 
+                "c#xs = l1 @ [a, b] @ l2" by (unfold edges_on_def, blast)
+            -- {* From this, it can be shown that @{text "c"} is 
+                      in the sub-tree of @{text "a"} *}
+            have "c \<in> subtree r a" 
+            proof(cases "l1 = []")
+              case True
+              -- {* If @{text "l1"} is null, it can be derived that @{text "c = a"}. *}
+              with eq_xs have "c = a" by auto
+              -- {* So, @{text "c"} is obviously in the sub-tree of @{text "a"}. *}
+              thus ?thesis by (unfold subtree_def, auto)
+            next
+              case False
+              -- {* When @{text "l1"} is not null, it must have a tail @{text "es"}: *}
+              then obtain e es where "l1 = e#es" by (cases l1, auto)
+              -- {* The relation of this tail with @{text "xs"} is derived: *}
+              with eq_xs have "xs = es@[a,b]@l2" by auto
+              -- {* From this, a path from @{text "c"} to @{text "a"} is made visible: *}
+              from rp[unfolded this] have "rpath r c (es @ [a] @ (b#l2)) x" by simp
+              thus ?thesis
+              proof(cases rule:rpath_appendE)
+                -- {* The path from @{text "c"} to @{text "a"} is extraced 
+                             using @{thm "rpath_appendE"}: *}
+                case 1
+                from rpath_star[OF this(1)] 
+                -- {* The extracted path servers as a witness that @{text "c"} is 
+                          in the sub-tree of @{text "a"}: *}
+                show ?thesis by (simp add:subtree_def)
+            qed
+          qed with h show False by auto         
+         qed ultimately show ?thesis by auto
+       qed
+     qed
+     -- {* From , it is shown that @{text "c"} is in the sub-tree of @{text "x"}
+           inthe reduced graph. *}
+     from rpath_star[OF this] show ?thesis by (auto simp:subtree_def)
+    qed
+  } 
+  -- {* The equality of sets is derived from the two directions just proved. *}
+  ultimately show ?thesis by auto
+qed 
+
+end
+
+end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/red_1.thy	Thu Dec 03 14:34:29 2015 +0800
@@ -0,0 +1,359 @@
+section {*
+  This file contains lemmas used to guide the recalculation of current precedence 
+  after every system call (or system operation)
+*}
+theory CpsG
+imports PrioG Max RTree
+begin
+
+
+definition "wRAG (s::state) = {(Th th, Cs cs) | th cs. waiting s th cs}"
+
+definition "hRAG (s::state) =  {(Cs cs, Th th) | th cs. holding s th cs}"
+
+definition "tRAG s = wRAG s O hRAG s"
+
+definition "pairself f = (\<lambda>(a, b). (f a, f b))"
+
+definition "rel_map f r = (pairself f ` r)"
+
+fun the_thread :: "node \<Rightarrow> thread" where
+   "the_thread (Th th) = th"
+
+definition "tG s = rel_map the_thread (tRAG s)"
+
+locale pip = 
+  fixes s
+  assumes vt: "vt s"
+
+
+lemma RAG_split: "RAG s = (wRAG s \<union> hRAG s)"
+  by (unfold s_RAG_abv wRAG_def hRAG_def s_waiting_abv 
+             s_holding_abv cs_RAG_def, auto)
+
+lemma relpow_mult: 
+  "((r::'a rel) ^^ m) ^^ n = r ^^ (m*n)"
+proof(induct n arbitrary:m)
+  case (Suc k m)
+  thus ?case (is "?L = ?R")
+  proof -
+    have h: "(m * k + m) = (m + m * k)" by auto
+    show ?thesis 
+      apply (simp add:Suc relpow_add[symmetric])
+      by (unfold h, simp)
+  qed
+qed simp
+
+lemma compose_relpow_2:
+  assumes "r1 \<subseteq> r"
+  and "r2 \<subseteq> r"
+  shows "r1 O r2 \<subseteq> r ^^ (2::nat)"
+proof -
+  { fix a b
+    assume "(a, b) \<in> r1 O r2"
+    then obtain e where "(a, e) \<in> r1" "(e, b) \<in> r2"
+      by auto
+    with assms have "(a, e) \<in> r" "(e, b) \<in> r" by auto
+    hence "(a, b) \<in> r ^^ (Suc (Suc 0))" by auto
+  } thus ?thesis by (auto simp:numeral_2_eq_2)
+qed
+
+
+lemma acyclic_compose:
+  assumes "acyclic r"
+  and "r1 \<subseteq> r"
+  and "r2 \<subseteq> r"
+  shows "acyclic (r1 O r2)"
+proof -
+  { fix a
+    assume "(a, a) \<in> (r1 O r2)^+"
+    from trancl_mono[OF this compose_relpow_2[OF assms(2, 3)]]
+    have "(a, a) \<in> (r ^^ 2) ^+" .
+    from trancl_power[THEN iffD1, OF this]
+    obtain n where h: "(a, a) \<in> (r ^^ 2) ^^ n" "n > 0" by blast
+    from this(1)[unfolded relpow_mult] have h2: "(a, a) \<in> r ^^ (2 * n)" .
+    have "(a, a) \<in> r^+" 
+    proof(cases rule:trancl_power[THEN iffD2])
+      from h(2) h2 show "\<exists>n>0. (a, a) \<in> r ^^ n" 
+        by (rule_tac x = "2*n" in exI, auto)
+    qed
+    with assms have "False" by (auto simp:acyclic_def)
+  } thus ?thesis by (auto simp:acyclic_def)
+qed
+
+lemma range_tRAG: "Range (tRAG s) \<subseteq> {Th th | th. True}"
+proof -
+  have "Range (wRAG s O hRAG s) \<subseteq> {Th th |th. True}" (is "?L \<subseteq> ?R")
+  proof -
+    have "?L \<subseteq> Range (hRAG s)" by auto
+    also have "... \<subseteq> ?R" 
+      by (unfold hRAG_def, auto)
+    finally show ?thesis by auto
+  qed
+  thus ?thesis by (simp add:tRAG_def)
+qed
+
+lemma domain_tRAG: "Domain (tRAG s) \<subseteq> {Th th | th. True}"
+proof -
+  have "Domain (wRAG s O hRAG s) \<subseteq> {Th th |th. True}" (is "?L \<subseteq> ?R")
+  proof -
+    have "?L \<subseteq> Domain (wRAG s)" by auto
+    also have "... \<subseteq> ?R" 
+      by (unfold wRAG_def, auto)
+    finally show ?thesis by auto
+  qed
+  thus ?thesis by (simp add:tRAG_def)
+qed
+
+lemma rel_mapE: 
+  assumes "(a, b) \<in> rel_map f r"
+  obtains c d 
+  where "(c, d) \<in> r" "(a, b) = (f c, f d)"
+  using assms
+  by (unfold rel_map_def pairself_def, auto)
+
+lemma rel_mapI: 
+  assumes "(a, b) \<in> r"
+    and "c = f a"
+    and "d = f b"
+  shows "(c, d) \<in> rel_map f r"
+  using assms
+  by (unfold rel_map_def pairself_def, auto)
+
+lemma map_appendE:
+  assumes "map f zs = xs @ ys"
+  obtains xs' ys' 
+  where "zs = xs' @ ys'" "xs = map f xs'" "ys = map f ys'"
+proof -
+  have "\<exists> xs' ys'. zs = xs' @ ys' \<and> xs = map f xs' \<and> ys = map f ys'"
+  using assms
+  proof(induct xs arbitrary:zs ys)
+    case (Nil zs ys)
+    thus ?case by auto
+  next
+    case (Cons x xs zs ys)
+    note h = this
+    show ?case
+    proof(cases zs)
+      case (Cons e es)
+      with h have eq_x: "map f es = xs @ ys" "x = f e" by auto
+      from h(1)[OF this(1)]
+      obtain xs' ys' where "es = xs' @ ys'" "xs = map f xs'" "ys = map f ys'"
+        by blast
+      with Cons eq_x
+      have "zs = (e#xs') @ ys' \<and> x # xs = map f (e#xs') \<and> ys = map f ys'" by auto
+      thus ?thesis by metis
+    qed (insert h, auto)
+  qed
+  thus ?thesis by (auto intro!:that)
+qed
+
+lemma rel_map_mono:
+  assumes "r1 \<subseteq> r2"
+  shows "rel_map f r1 \<subseteq> rel_map f r2"
+  using assms
+  by (auto simp:rel_map_def pairself_def)
+
+lemma rel_map_compose [simp]:
+    shows "rel_map f1 (rel_map f2 r) = rel_map (f1 o f2) r"
+    by (auto simp:rel_map_def pairself_def)
+
+lemma edges_on_map: "edges_on (map f xs) = rel_map f (edges_on xs)"
+proof -
+  { fix a b
+    assume "(a, b) \<in> edges_on (map f xs)"
+    then obtain l1 l2 where eq_map: "map f xs = l1 @ [a, b] @ l2" 
+      by (unfold edges_on_def, auto)
+    hence "(a, b) \<in> rel_map f (edges_on xs)"
+      by (auto elim!:map_appendE intro!:rel_mapI simp:edges_on_def)
+  } moreover { 
+    fix a b
+    assume "(a, b) \<in> rel_map f (edges_on xs)"
+    then obtain c d where 
+        h: "(c, d) \<in> edges_on xs" "(a, b) = (f c, f d)" 
+             by (elim rel_mapE, auto)
+    then obtain l1 l2 where
+        eq_xs: "xs = l1 @ [c, d] @ l2" 
+             by (auto simp:edges_on_def)
+    hence eq_map: "map f xs = map f l1 @ [f c, f d] @ map f l2" by auto
+    have "(a, b) \<in> edges_on (map f xs)"
+    proof -
+      from h(2) have "[f c, f d] = [a, b]" by simp
+      from eq_map[unfolded this] show ?thesis by (auto simp:edges_on_def)
+    qed
+  } ultimately show ?thesis by auto
+qed
+
+lemma plus_rpath: 
+  assumes "(a, b) \<in> r^+"
+  obtains xs where "rpath r a xs b" "xs \<noteq> []"
+proof -
+  from assms obtain m where h: "(a, m) \<in> r" "(m, b) \<in> r^*"
+      by (auto dest!:tranclD)
+  from star_rpath[OF this(2)] obtain xs where "rpath r m xs b" by auto
+  from rstepI[OF h(1) this] have "rpath r a (m # xs) b" .
+  from that[OF this] show ?thesis by auto
+qed
+
+lemma edges_on_unfold:
+  "edges_on (a # b # xs) = {(a, b)} \<union> edges_on (b # xs)" (is "?L = ?R")
+proof -
+  { fix c d
+    assume "(c, d) \<in> ?L"
+    then obtain l1 l2 where h: "(a # b # xs) = l1 @ [c, d] @ l2" 
+        by (auto simp:edges_on_def)
+    have "(c, d) \<in> ?R"
+    proof(cases "l1")
+      case Nil
+      with h have "(c, d) = (a, b)" by auto
+      thus ?thesis by auto
+    next
+      case (Cons e es)
+      from h[unfolded this] have "b#xs = es@[c, d]@l2" by auto
+      thus ?thesis by (auto simp:edges_on_def)
+    qed
+  } moreover
+  { fix c d
+    assume "(c, d) \<in> ?R"
+    moreover have "(a, b) \<in> ?L" 
+    proof -
+      have "(a # b # xs) = []@[a,b]@xs" by simp
+      hence "\<exists> l1 l2. (a # b # xs) = l1@[a,b]@l2" by auto
+      thus ?thesis by (unfold edges_on_def, simp)
+    qed
+    moreover {
+        assume "(c, d) \<in> edges_on (b#xs)"
+        then obtain l1 l2 where "b#xs = l1@[c, d]@l2" by (unfold edges_on_def, auto)
+        hence "a#b#xs = (a#l1)@[c,d]@l2" by simp
+        hence "\<exists> l1 l2. (a # b # xs) = l1@[c,d]@l2" by metis
+        hence "(c,d) \<in> ?L" by (unfold edges_on_def, simp)
+    }
+    ultimately have "(c, d) \<in> ?L" by auto
+  } ultimately show ?thesis by auto
+qed
+
+lemma edges_on_rpathI:
+  assumes "edges_on (a#xs@[b]) \<subseteq> r"
+  shows "rpath r a (xs@[b]) b"
+  using assms
+proof(induct xs arbitrary: a b)
+  case Nil
+  moreover have "(a, b) \<in> edges_on (a # [] @ [b])"
+      by (unfold edges_on_def, auto)
+  ultimately have "(a, b) \<in> r" by auto
+  thus ?case by auto
+next
+  case (Cons x xs a b)
+  from this(2) have "edges_on (x # xs @ [b]) \<subseteq> r" by (simp add:edges_on_unfold)
+  from Cons(1)[OF this] have " rpath r x (xs @ [b]) b" .
+  moreover from Cons(2) have "(a, x) \<in> r" by (auto simp:edges_on_unfold)
+  ultimately show ?case by (auto intro!:rstepI)
+qed
+
+lemma image_id:
+  assumes "\<And> x. x \<in> A \<Longrightarrow> f x = x"
+  shows "f ` A = A"
+  using assms by (auto simp:image_def)
+
+lemma rel_map_inv_id:
+  assumes "inj_on f ((Domain r) \<union> (Range r))"
+  shows "(rel_map (inv_into ((Domain r) \<union> (Range r)) f \<circ> f) r) = r"
+proof -
+ let ?f = "(inv_into (Domain r \<union> Range r) f \<circ> f)"
+ {
+  fix a b
+  assume h0: "(a, b) \<in> r"
+  have "pairself ?f (a, b) = (a, b)"
+  proof -
+    from assms h0 have "?f a = a" by (auto intro:inv_into_f_f)
+    moreover have "?f b = b"
+      by (insert h0, simp, intro inv_into_f_f[OF assms], auto intro!:RangeI)
+    ultimately show ?thesis by (auto simp:pairself_def)
+  qed
+ } thus ?thesis by (unfold rel_map_def, intro image_id, case_tac x, auto)
+qed 
+
+lemma rel_map_acyclic:
+  assumes "acyclic r"
+  and "inj_on f ((Domain r) \<union> (Range r))"
+  shows "acyclic (rel_map f r)"
+proof -
+  let ?D = "Domain r \<union> Range r"
+  { fix a 
+    assume "(a, a) \<in> (rel_map f r)^+" 
+    from plus_rpath[OF this]
+    obtain xs where rp: "rpath (rel_map f r) a xs a" "xs \<noteq> []" by auto
+    from rpath_nnl_lastE[OF this] obtain xs' where eq_xs: "xs = xs'@[a]" by auto
+    from rpath_edges_on[OF rp(1)]
+    have h: "edges_on (a # xs) \<subseteq> rel_map f r" .
+    from edges_on_map[of "inv_into ?D f" "a#xs"]
+    have "edges_on (map (inv_into ?D f) (a # xs)) = rel_map (inv_into ?D f) (edges_on (a # xs))" .
+    with rel_map_mono[OF h, of "inv_into ?D f"]
+    have "edges_on (map (inv_into ?D f) (a # xs)) \<subseteq> rel_map ((inv_into ?D f) o f) r" by simp
+    from this[unfolded eq_xs]
+    have subr: "edges_on (map (inv_into ?D f) (a # xs' @ [a])) \<subseteq> rel_map (inv_into ?D f \<circ> f) r" .
+    have "(map (inv_into ?D f) (a # xs' @ [a])) = (inv_into ?D f a) # map (inv_into ?D f) xs' @ [inv_into ?D f a]"
+      by simp
+    from edges_on_rpathI[OF subr[unfolded this]]
+    have "rpath (rel_map (inv_into ?D f \<circ> f) r) 
+                      (inv_into ?D f a) (map (inv_into ?D f) xs' @ [inv_into ?D f a]) (inv_into ?D f a)" .
+    hence "(inv_into ?D f a, inv_into ?D f a) \<in> (rel_map (inv_into ?D f \<circ> f) r)^+"
+        by (rule rpath_plus, simp)
+    moreover have "(rel_map (inv_into ?D f \<circ> f) r) = r" by (rule rel_map_inv_id[OF assms(2)])
+    moreover note assms(1) 
+    ultimately have False by (unfold acyclic_def, auto)
+  } thus ?thesis by (auto simp:acyclic_def)
+qed
+
+context pip
+begin
+
+interpretation rtree_RAG: rtree "RAG s"
+proof
+  show "single_valued (RAG s)"
+    by (unfold single_valued_def, auto intro: unique_RAG[OF vt])
+
+  show "acyclic (RAG s)"
+     by (rule acyclic_RAG[OF vt])
+qed
+
+lemma sgv_wRAG: 
+  shows "single_valued (wRAG s)"
+  using waiting_unique[OF vt]
+  by (unfold single_valued_def wRAG_def, auto)
+
+lemma sgv_hRAG: 
+  shows "single_valued (hRAG s)"
+  using held_unique
+  by (unfold single_valued_def hRAG_def, auto)
+
+lemma sgv_tRAG: shows "single_valued (tRAG s)"
+  by (unfold tRAG_def, rule Relation.single_valued_relcomp, 
+        insert sgv_hRAG sgv_wRAG, auto)
+
+lemma acyclic_hRAG: 
+  shows "acyclic (hRAG s)"
+  by (rule acyclic_subset[OF acyclic_RAG[OF vt]], insert RAG_split, auto)
+
+lemma acyclic_wRAG: 
+  shows "acyclic (wRAG s)"
+  by (rule acyclic_subset[OF acyclic_RAG[OF vt]], insert RAG_split, auto)
+
+lemma acyclic_tRAG: 
+  shows "acyclic (tRAG s)"
+  by (unfold tRAG_def, rule acyclic_compose[OF acyclic_RAG[OF vt]],
+         unfold RAG_split, auto)
+
+lemma acyclic_tG:
+  shows "acyclic (tG s)"
+proof(unfold tG_def, rule rel_map_acyclic[OF acyclic_tRAG])
+  show "inj_on the_thread (Domain (tRAG s) \<union> Range (tRAG s))"
+  proof(rule subset_inj_on)
+    show " inj_on the_thread {Th th |th. True}" by (unfold inj_on_def, auto)
+  next
+    from domain_tRAG range_tRAG 
+    show " Domain (tRAG s) \<union> Range (tRAG s) \<subseteq> {Th th |th. True}" by auto
+  qed
+qed
+
+end