#PIPBasics.thy#
changeset 91 0525670d8e6a
parent 90 ed938e2246b9
child 92 4763aa246dbd
equal deleted inserted replaced
90:ed938e2246b9 91:0525670d8e6a
     1 theory PIPBasics
       
     2 imports PIPDefs 
       
     3 begin
       
     4 
       
     5 locale valid_trace = 
       
     6   fixes s
       
     7   assumes vt : "vt s"
       
     8 
       
     9 locale valid_trace_e = valid_trace +
       
    10   fixes e
       
    11   assumes vt_e: "vt (e#s)"
       
    12 begin
       
    13 
       
    14 lemma pip_e: "PIP s e"
       
    15   using vt_e by (cases, simp)  
       
    16 
       
    17 end
       
    18 
       
    19 lemma runing_ready: 
       
    20   shows "runing s \<subseteq> readys s"
       
    21   unfolding runing_def readys_def
       
    22   by auto 
       
    23 
       
    24 lemma readys_threads:
       
    25   shows "readys s \<subseteq> threads s"
       
    26   unfolding readys_def
       
    27   by auto
       
    28 
       
    29 lemma wq_v_neq:
       
    30    "cs \<noteq> cs' \<Longrightarrow> wq (V thread cs#s) cs' = wq s cs'"
       
    31   by (auto simp:wq_def Let_def cp_def split:list.splits)
       
    32 
       
    33 lemma runing_head:
       
    34   assumes "th \<in> runing s"
       
    35   and "th \<in> set (wq_fun (schs s) cs)"
       
    36   shows "th = hd (wq_fun (schs s) cs)"
       
    37   using assms
       
    38   by (simp add:runing_def readys_def s_waiting_def wq_def)
       
    39 
       
    40 context valid_trace
       
    41 begin
       
    42 
       
    43 lemma actor_inv: 
       
    44   assumes "PIP s e"
       
    45   and "\<not> isCreate e"
       
    46   shows "actor e \<in> runing s"
       
    47   using assms
       
    48   by (induct, auto)
       
    49 
       
    50 lemma ind [consumes 0, case_names Nil Cons, induct type]:
       
    51   assumes "PP []"
       
    52      and "(\<And>s e. valid_trace s \<Longrightarrow> valid_trace (e#s) \<Longrightarrow>
       
    53                    PP s \<Longrightarrow> PIP s e \<Longrightarrow> PP (e # s))"
       
    54      shows "PP s"
       
    55 proof(rule vt.induct[OF vt])
       
    56   from assms(1) show "PP []" .
       
    57 next
       
    58   fix s e
       
    59   assume h: "vt s" "PP s" "PIP s e"
       
    60   show "PP (e # s)"
       
    61   proof(cases rule:assms(2))
       
    62     from h(1) show v1: "valid_trace s" by (unfold_locales, simp)
       
    63   next
       
    64     from h(1,3) have "vt (e#s)" by auto
       
    65     thus "valid_trace (e # s)" by (unfold_locales, simp)
       
    66   qed (insert h, auto)
       
    67 qed
       
    68 
       
    69 lemma wq_distinct: "distinct (wq s cs)"
       
    70 proof(induct rule:ind)
       
    71   case (Cons s e)
       
    72   from Cons(4,3)
       
    73   show ?case 
       
    74   proof(induct)
       
    75     case (thread_P th s cs1)
       
    76     show ?case 
       
    77     proof(cases "cs = cs1")
       
    78       case True
       
    79       thus ?thesis (is "distinct ?L")
       
    80       proof - 
       
    81         have "?L = wq_fun (schs s) cs1 @ [th]" using True
       
    82           by (simp add:wq_def wf_def Let_def split:list.splits)
       
    83         moreover have "distinct ..."
       
    84         proof -
       
    85           have "th \<notin> set (wq_fun (schs s) cs1)"
       
    86           proof
       
    87             assume otherwise: "th \<in> set (wq_fun (schs s) cs1)"
       
    88             from runing_head[OF thread_P(1) this]
       
    89             have "th = hd (wq_fun (schs s) cs1)" .
       
    90             hence "(Cs cs1, Th th) \<in> (RAG s)" using otherwise
       
    91               by (simp add:s_RAG_def s_holding_def wq_def cs_holding_def)
       
    92             with thread_P(2) show False by auto
       
    93           qed
       
    94           moreover have "distinct (wq_fun (schs s) cs1)"
       
    95               using True thread_P wq_def by auto 
       
    96           ultimately show ?thesis by auto
       
    97         qed
       
    98         ultimately show ?thesis by simp
       
    99       qed
       
   100     next
       
   101       case False
       
   102       with thread_P(3)
       
   103       show ?thesis
       
   104         by (auto simp:wq_def wf_def Let_def split:list.splits)
       
   105     qed
       
   106   next
       
   107     case (thread_V th s cs1)
       
   108     thus ?case
       
   109     proof(cases "cs = cs1")
       
   110       case True
       
   111       show ?thesis (is "distinct ?L")
       
   112       proof(cases "(wq s cs)")
       
   113         case Nil
       
   114         thus ?thesis
       
   115           by (auto simp:wq_def wf_def Let_def split:list.splits)
       
   116       next
       
   117         case (Cons w_hd w_tl)
       
   118         moreover have "distinct (SOME q. distinct q \<and> set q = set w_tl)"
       
   119         proof(rule someI2)
       
   120           from thread_V(3)[unfolded Cons]
       
   121           show  "distinct w_tl \<and> set w_tl = set w_tl" by auto
       
   122         qed auto
       
   123         ultimately show ?thesis
       
   124           by (auto simp:wq_def wf_def Let_def True split:list.splits)
       
   125       qed 
       
   126     next
       
   127       case False
       
   128       with thread_V(3)
       
   129       show ?thesis
       
   130         by (auto simp:wq_def wf_def Let_def split:list.splits)
       
   131     qed
       
   132   qed (insert Cons, auto simp: wq_def Let_def split:list.splits)
       
   133 qed (unfold wq_def Let_def, simp)
       
   134 
       
   135 end
       
   136 
       
   137 
       
   138 context valid_trace_e
       
   139 begin
       
   140 
       
   141 text {*
       
   142   The following lemma shows that only the @{text "P"}
       
   143   operation can add new thread into waiting queues. 
       
   144   Such kind of lemmas are very obvious, but need to be checked formally.
       
   145   This is a kind of confirmation that our modelling is correct.
       
   146 *}
       
   147 
       
   148 lemma block_pre: 
       
   149   assumes s_ni: "thread \<notin> set (wq s cs)"
       
   150   and s_i: "thread \<in> set (wq (e#s) cs)"
       
   151   shows "e = P thread cs"
       
   152 proof(cases e)
       
   153   -- {* This is the only non-trivial case: *}
       
   154   case (V th cs1)
       
   155   have False
       
   156   proof(cases "cs1 = cs")
       
   157     case True
       
   158     show ?thesis
       
   159     proof(cases "(wq s cs1)")
       
   160       case (Cons w_hd w_tl)
       
   161       have "set (wq (e#s) cs) \<subseteq> set (wq s cs)"
       
   162       proof -
       
   163         have "(wq (e#s) cs) = (SOME q. distinct q \<and> set q = set w_tl)"
       
   164           using  Cons V by (auto simp:wq_def Let_def True split:if_splits)
       
   165         moreover have "set ... \<subseteq> set (wq s cs)"
       
   166         proof(rule someI2)
       
   167           show "distinct w_tl \<and> set w_tl = set w_tl"
       
   168             by (metis distinct.simps(2) local.Cons wq_distinct)
       
   169         qed (insert Cons True, auto)
       
   170         ultimately show ?thesis by simp
       
   171       qed
       
   172       with assms show ?thesis by auto
       
   173     qed (insert assms V True, auto simp:wq_def Let_def split:if_splits)
       
   174   qed (insert assms V, auto simp:wq_def Let_def split:if_splits)
       
   175   thus ?thesis by auto
       
   176 qed (insert assms, auto simp:wq_def Let_def split:if_splits)
       
   177 
       
   178 end
       
   179 
       
   180 text {*
       
   181   The following lemmas is also obvious and shallow. It says
       
   182   that only running thread can request for a critical resource 
       
   183   and that the requested resource must be one which is
       
   184   not current held by the thread.
       
   185 *}
       
   186 
       
   187 lemma p_pre: "\<lbrakk>vt ((P thread cs)#s)\<rbrakk> \<Longrightarrow> 
       
   188   thread \<in> runing s \<and> (Cs cs, Th thread)  \<notin> (RAG s)^+"
       
   189 apply (ind_cases "vt ((P thread cs)#s)")
       
   190 apply (ind_cases "step s (P thread cs)")
       
   191 by auto
       
   192 
       
   193 lemma abs1:
       
   194   assumes ein: "e \<in> set es"
       
   195   and neq: "hd es \<noteq> hd (es @ [x])"
       
   196   shows "False"
       
   197 proof -
       
   198   from ein have "es \<noteq> []" by auto
       
   199   then obtain e ess where "es = e # ess" by (cases es, auto)
       
   200   with neq show ?thesis by auto
       
   201 qed
       
   202 
       
   203 lemma q_head: "Q (hd es) \<Longrightarrow> hd es = hd [th\<leftarrow>es . Q th]"
       
   204   by (cases es, auto)
       
   205 
       
   206 inductive_cases evt_cons: "vt (a#s)"
       
   207 
       
   208 context valid_trace_e
       
   209 begin
       
   210 
       
   211 lemma abs2:
       
   212   assumes inq: "thread \<in> set (wq s cs)"
       
   213   and nh: "thread = hd (wq s cs)"
       
   214   and qt: "thread \<noteq> hd (wq (e#s) cs)"
       
   215   and inq': "thread \<in> set (wq (e#s) cs)"
       
   216   shows "False"
       
   217 proof -
       
   218   from vt_e assms show "False"
       
   219     apply (cases e)
       
   220     apply ((simp split:if_splits add:Let_def wq_def)[1])+
       
   221     apply (insert abs1, fast)[1]
       
   222     apply (auto simp:wq_def simp:Let_def split:if_splits list.splits)
       
   223   proof -
       
   224     fix th qs
       
   225     assume vt: "vt (V th cs # s)"
       
   226       and th_in: "thread \<in> set (SOME q. distinct q \<and> set q = set qs)"
       
   227       and eq_wq: "wq_fun (schs s) cs = thread # qs"
       
   228     show "False"
       
   229     proof -
       
   230       from wq_distinct[of cs]
       
   231         and eq_wq[folded wq_def] have "distinct (thread#qs)" by simp
       
   232       moreover have "thread \<in> set qs"
       
   233       proof -
       
   234         have "set (SOME q. distinct q \<and> set q = set qs) = set qs"
       
   235         proof(rule someI2)
       
   236           from wq_distinct [of cs]
       
   237           and eq_wq [folded wq_def]
       
   238           show "distinct qs \<and> set qs = set qs" by auto
       
   239         next
       
   240           fix x assume "distinct x \<and> set x = set qs"
       
   241           thus "set x = set qs" by auto
       
   242         qed
       
   243         with th_in show ?thesis by auto
       
   244       qed
       
   245       ultimately show ?thesis by auto
       
   246     qed
       
   247   qed
       
   248 qed
       
   249 
       
   250 end
       
   251 
       
   252 
       
   253 context valid_trace
       
   254 begin
       
   255 lemma  vt_moment: "\<And> t. vt (moment t s)"
       
   256 proof(induct rule:ind)
       
   257   case Nil
       
   258   thus ?case by (simp add:vt_nil)
       
   259 next
       
   260   case (Cons s e t)
       
   261   show ?case
       
   262   proof(cases "t \<ge> length (e#s)")
       
   263     case True
       
   264     from True have "moment t (e#s) = e#s" by simp
       
   265     thus ?thesis using Cons
       
   266       by (simp add:valid_trace_def)
       
   267   next
       
   268     case False
       
   269     from Cons have "vt (moment t s)" by simp
       
   270     moreover have "moment t (e#s) = moment t s"
       
   271     proof -
       
   272       from False have "t \<le> length s" by simp
       
   273       from moment_app [OF this, of "[e]"] 
       
   274       show ?thesis by simp
       
   275     qed
       
   276     ultimately show ?thesis by simp
       
   277   qed
       
   278 qed
       
   279 end
       
   280 
       
   281 locale valid_moment = valid_trace + 
       
   282   fixes i :: nat
       
   283 
       
   284 sublocale valid_moment < vat_moment: valid_trace "(moment i s)"
       
   285   by (unfold_locales, insert vt_moment, auto)
       
   286 
       
   287 context valid_trace
       
   288 begin
       
   289 
       
   290 
       
   291 text {* (* ddd *)
       
   292   The nature of the work is like this: since it starts from a very simple and basic 
       
   293   model, even intuitively very `basic` and `obvious` properties need to derived from scratch.
       
   294   For instance, the fact 
       
   295   that one thread can not be blocked by two critical resources at the same time
       
   296   is obvious, because only running threads can make new requests, if one is waiting for 
       
   297   a critical resource and get blocked, it can not make another resource request and get 
       
   298   blocked the second time (because it is not running). 
       
   299 
       
   300   To derive this fact, one needs to prove by contraction and 
       
   301   reason about time (or @{text "moement"}). The reasoning is based on a generic theorem
       
   302   named @{text "p_split"}, which is about status changing along the time axis. It says if 
       
   303   a condition @{text "Q"} is @{text "True"} at a state @{text "s"},
       
   304   but it was @{text "False"} at the very beginning, then there must exits a moment @{text "t"} 
       
   305   in the history of @{text "s"} (notice that @{text "s"} itself is essentially the history 
       
   306   of events leading to it), such that @{text "Q"} switched 
       
   307   from being @{text "False"} to @{text "True"} and kept being @{text "True"}
       
   308   till the last moment of @{text "s"}.
       
   309 
       
   310   Suppose a thread @{text "th"} is blocked
       
   311   on @{text "cs1"} and @{text "cs2"} in some state @{text "s"}, 
       
   312   since no thread is blocked at the very beginning, by applying 
       
   313   @{text "p_split"} to these two blocking facts, there exist 
       
   314   two moments @{text "t1"} and @{text "t2"}  in @{text "s"}, such that 
       
   315   @{text "th"} got blocked on @{text "cs1"} and @{text "cs2"} 
       
   316   and kept on blocked on them respectively ever since.
       
   317  
       
   318   Without lost of generality, we assume @{text "t1"} is earlier than @{text "t2"}.
       
   319   However, since @{text "th"} was blocked ever since memonent @{text "t1"}, so it was still
       
   320   in blocked state at moment @{text "t2"} and could not
       
   321   make any request and get blocked the second time: Contradiction.
       
   322 *}
       
   323 
       
   324 lemma waiting_unique_pre: (* ccc *)
       
   325   assumes h11: "thread \<in> set (wq s cs1)"
       
   326   and h12: "thread \<noteq> hd (wq s cs1)"
       
   327   assumes h21: "thread \<in> set (wq s cs2)"
       
   328   and h22: "thread \<noteq> hd (wq s cs2)"
       
   329   and neq12: "cs1 \<noteq> cs2"
       
   330   shows "False"
       
   331 proof -
       
   332   let "?Q cs s" = "thread \<in> set (wq s cs) \<and> thread \<noteq> hd (wq s cs)"
       
   333   from h11 and h12 have q1: "?Q cs1 s" by simp
       
   334   from h21 and h22 have q2: "?Q cs2 s" by simp
       
   335   have nq1: "\<not> ?Q cs1 []" by (simp add:wq_def)
       
   336   have nq2: "\<not> ?Q cs2 []" by (simp add:wq_def)
       
   337   from p_split [of "?Q cs1", OF q1 nq1]
       
   338   obtain t1 where lt1: "t1 < length s"
       
   339     and np1: "\<not>(thread \<in> set (wq (moment t1 s) cs1) \<and>
       
   340         thread \<noteq> hd (wq (moment t1 s) cs1))"
       
   341     and nn1: "(\<forall>i'>t1. thread \<in> set (wq (moment i' s) cs1) \<and>
       
   342              thread \<noteq> hd (wq (moment i' s) cs1))" by auto
       
   343   from p_split [of "?Q cs2", OF q2 nq2]
       
   344   obtain t2 where lt2: "t2 < length s"
       
   345     and np2: "\<not>(thread \<in> set (wq (moment t2 s) cs2) \<and>
       
   346         thread \<noteq> hd (wq (moment t2 s) cs2))"
       
   347     and nn2: "(\<forall>i'>t2. thread \<in> set (wq (moment i' s) cs2) \<and>
       
   348              thread \<noteq> hd (wq (moment i' s) cs2))" by auto
       
   349   show ?thesis
       
   350   proof -
       
   351     { 
       
   352       assume lt12: "t1 < t2"
       
   353       let ?t3 = "Suc t2"
       
   354       from lt2 have le_t3: "?t3 \<le> length s" by auto
       
   355       from moment_plus [OF this] 
       
   356       obtain e where eq_m: "moment ?t3 s = e#moment t2 s" by auto
       
   357       have "t2 < ?t3" by simp
       
   358       from nn2 [rule_format, OF this] and eq_m
       
   359       have h1: "thread \<in> set (wq (e#moment t2 s) cs2)" and
       
   360         h2: "thread \<noteq> hd (wq (e#moment t2 s) cs2)" by auto
       
   361       have "vt (e#moment t2 s)"
       
   362       proof -
       
   363         from vt_moment 
       
   364         have "vt (moment ?t3 s)" .
       
   365         with eq_m show ?thesis by simp
       
   366       qed
       
   367       then interpret vt_e: valid_trace_e "moment t2 s" "e"
       
   368         by (unfold_locales, auto, cases, simp)
       
   369       have ?thesis
       
   370       proof(cases "thread \<in> set (wq (moment t2 s) cs2)")
       
   371         case True
       
   372         from True and np2 have eq_th: "thread = hd (wq (moment t2 s) cs2)"
       
   373           by auto 
       
   374         from vt_e.abs2 [OF True eq_th h2 h1]
       
   375         show ?thesis by auto
       
   376       next
       
   377         case False
       
   378         from vt_e.block_pre[OF False h1]
       
   379         have "e = P thread cs2" .
       
   380         with vt_e.vt_e have "vt ((P thread cs2)# moment t2 s)" by simp
       
   381         from p_pre [OF this] have "thread \<in> runing (moment t2 s)" by simp
       
   382         with runing_ready have "thread \<in> readys (moment t2 s)" by auto
       
   383         with nn1 [rule_format, OF lt12]
       
   384         show ?thesis  by (simp add:readys_def wq_def s_waiting_def, auto)
       
   385       qed
       
   386     } moreover {
       
   387       assume lt12: "t2 < t1"
       
   388       let ?t3 = "Suc t1"
       
   389       from lt1 have le_t3: "?t3 \<le> length s" by auto
       
   390       from moment_plus [OF this] 
       
   391       obtain e where eq_m: "moment ?t3 s = e#moment t1 s" by auto
       
   392       have lt_t3: "t1 < ?t3" by simp
       
   393       from nn1 [rule_format, OF this] and eq_m
       
   394       have h1: "thread \<in> set (wq (e#moment t1 s) cs1)" and
       
   395         h2: "thread \<noteq> hd (wq (e#moment t1 s) cs1)" by auto
       
   396       have "vt  (e#moment t1 s)"
       
   397       proof -
       
   398         from vt_moment
       
   399         have "vt (moment ?t3 s)" .
       
   400         with eq_m show ?thesis by simp
       
   401       qed
       
   402       then interpret vt_e: valid_trace_e "moment t1 s" e
       
   403         by (unfold_locales, auto, cases, auto)
       
   404       have ?thesis
       
   405       proof(cases "thread \<in> set (wq (moment t1 s) cs1)")
       
   406         case True
       
   407         from True and np1 have eq_th: "thread = hd (wq (moment t1 s) cs1)"
       
   408           by auto
       
   409         from vt_e.abs2 True eq_th h2 h1
       
   410         show ?thesis by auto
       
   411       next
       
   412         case False
       
   413         from vt_e.block_pre [OF False h1]
       
   414         have "e = P thread cs1" .
       
   415         with vt_e.vt_e have "vt ((P thread cs1)# moment t1 s)" by simp
       
   416         from p_pre [OF this] have "thread \<in> runing (moment t1 s)" by simp
       
   417         with runing_ready have "thread \<in> readys (moment t1 s)" by auto
       
   418         with nn2 [rule_format, OF lt12]
       
   419         show ?thesis  by (simp add:readys_def wq_def s_waiting_def, auto)
       
   420       qed
       
   421     } moreover {
       
   422       assume eqt12: "t1 = t2"
       
   423       let ?t3 = "Suc t1"
       
   424       from lt1 have le_t3: "?t3 \<le> length s" by auto
       
   425       from moment_plus [OF this] 
       
   426       obtain e where eq_m: "moment ?t3 s = e#moment t1 s" by auto
       
   427       have lt_t3: "t1 < ?t3" by simp
       
   428       from nn1 [rule_format, OF this] and eq_m
       
   429       have h1: "thread \<in> set (wq (e#moment t1 s) cs1)" and
       
   430         h2: "thread \<noteq> hd (wq (e#moment t1 s) cs1)" by auto
       
   431       have vt_e: "vt (e#moment t1 s)"
       
   432       proof -
       
   433         from vt_moment
       
   434         have "vt (moment ?t3 s)" .
       
   435         with eq_m show ?thesis by simp
       
   436       qed
       
   437       then interpret vt_e: valid_trace_e "moment t1 s" e
       
   438         by (unfold_locales, auto, cases, auto)
       
   439       have ?thesis
       
   440       proof(cases "thread \<in> set (wq (moment t1 s) cs1)")
       
   441         case True
       
   442         from True and np1 have eq_th: "thread = hd (wq (moment t1 s) cs1)"
       
   443           by auto
       
   444         from vt_e.abs2 [OF True eq_th h2 h1]
       
   445         show ?thesis by auto
       
   446       next
       
   447         case False
       
   448         from vt_e.block_pre [OF False h1]
       
   449         have eq_e1: "e = P thread cs1" .
       
   450         have lt_t3: "t1 < ?t3" by simp
       
   451         with eqt12 have "t2 < ?t3" by simp
       
   452         from nn2 [rule_format, OF this] and eq_m and eqt12
       
   453         have h1: "thread \<in> set (wq (e#moment t2 s) cs2)" and
       
   454           h2: "thread \<noteq> hd (wq (e#moment t2 s) cs2)" by auto
       
   455         show ?thesis
       
   456         proof(cases "thread \<in> set (wq (moment t2 s) cs2)")
       
   457           case True
       
   458           from True and np2 have eq_th: "thread = hd (wq (moment t2 s) cs2)"
       
   459             by auto
       
   460           from vt_e and eqt12 have "vt (e#moment t2 s)" by simp 
       
   461           then interpret vt_e2: valid_trace_e "moment t2 s" e
       
   462             by (unfold_locales, auto, cases, auto)
       
   463           from vt_e2.abs2 [OF True eq_th h2 h1]
       
   464           show ?thesis .
       
   465         next
       
   466           case False
       
   467           have "vt (e#moment t2 s)"
       
   468           proof -
       
   469             from vt_moment eqt12
       
   470             have "vt (moment (Suc t2) s)" by auto
       
   471             with eq_m eqt12 show ?thesis by simp
       
   472           qed
       
   473           then interpret vt_e2: valid_trace_e "moment t2 s" e
       
   474             by (unfold_locales, auto, cases, auto)
       
   475           from vt_e2.block_pre [OF False h1]
       
   476           have "e = P thread cs2" .
       
   477           with eq_e1 neq12 show ?thesis by auto
       
   478         qed
       
   479       qed
       
   480     } ultimately show ?thesis by arith
       
   481   qed
       
   482 qed
       
   483 
       
   484 text {*
       
   485   This lemma is a simple corrolary of @{text "waiting_unique_pre"}.
       
   486 *}
       
   487 
       
   488 lemma waiting_unique:
       
   489   assumes "waiting s th cs1"
       
   490   and "waiting s th cs2"
       
   491   shows "cs1 = cs2"
       
   492 using waiting_unique_pre assms
       
   493 unfolding wq_def s_waiting_def
       
   494 by auto
       
   495 
       
   496 end
       
   497 
       
   498 (* not used *)
       
   499 text {*
       
   500   Every thread can only be blocked on one critical resource, 
       
   501   symmetrically, every critical resource can only be held by one thread. 
       
   502   This fact is much more easier according to our definition. 
       
   503 *}
       
   504 lemma held_unique:
       
   505   assumes "holding (s::event list) th1 cs"
       
   506   and "holding s th2 cs"
       
   507   shows "th1 = th2"
       
   508  by (insert assms, unfold s_holding_def, auto)
       
   509 
       
   510 
       
   511 lemma last_set_lt: "th \<in> threads s \<Longrightarrow> last_set th s < length s"
       
   512   apply (induct s, auto)
       
   513   by (case_tac a, auto split:if_splits)
       
   514 
       
   515 lemma last_set_unique: 
       
   516   "\<lbrakk>last_set th1 s = last_set th2 s; th1 \<in> threads s; th2 \<in> threads s\<rbrakk>
       
   517           \<Longrightarrow> th1 = th2"
       
   518   apply (induct s, auto)
       
   519   by (case_tac a, auto split:if_splits dest:last_set_lt)
       
   520 
       
   521 lemma preced_unique : 
       
   522   assumes pcd_eq: "preced th1 s = preced th2 s"
       
   523   and th_in1: "th1 \<in> threads s"
       
   524   and th_in2: " th2 \<in> threads s"
       
   525   shows "th1 = th2"
       
   526 proof -
       
   527   from pcd_eq have "last_set th1 s = last_set th2 s" by (simp add:preced_def)
       
   528   from last_set_unique [OF this th_in1 th_in2]
       
   529   show ?thesis .
       
   530 qed
       
   531 
       
   532 lemma preced_linorder: 
       
   533   assumes neq_12: "th1 \<noteq> th2"
       
   534   and th_in1: "th1 \<in> threads s"
       
   535   and th_in2: " th2 \<in> threads s"
       
   536   shows "preced th1 s < preced th2 s \<or> preced th1 s > preced th2 s"
       
   537 proof -
       
   538   from preced_unique [OF _ th_in1 th_in2] and neq_12 
       
   539   have "preced th1 s \<noteq> preced th2 s" by auto
       
   540   thus ?thesis by auto
       
   541 qed
       
   542 
       
   543 (* An aux lemma used later *)
       
   544 lemma unique_minus:
       
   545   assumes unique: "\<And> a b c. \<lbrakk>(a, b) \<in> r; (a, c) \<in> r\<rbrakk> \<Longrightarrow> b = c"
       
   546   and xy: "(x, y) \<in> r"
       
   547   and xz: "(x, z) \<in> r^+"
       
   548   and neq: "y \<noteq> z"
       
   549   shows "(y, z) \<in> r^+"
       
   550 proof -
       
   551  from xz and neq show ?thesis
       
   552  proof(induct)
       
   553    case (base ya)
       
   554    have "(x, ya) \<in> r" by fact
       
   555    from unique [OF xy this] have "y = ya" .
       
   556    with base show ?case by auto
       
   557  next
       
   558    case (step ya z)
       
   559    show ?case
       
   560    proof(cases "y = ya")
       
   561      case True
       
   562      from step True show ?thesis by simp
       
   563    next
       
   564      case False
       
   565      from step False
       
   566      show ?thesis by auto
       
   567    qed
       
   568  qed
       
   569 qed
       
   570 
       
   571 lemma unique_base:
       
   572   assumes unique: "\<And> a b c. \<lbrakk>(a, b) \<in> r; (a, c) \<in> r\<rbrakk> \<Longrightarrow> b = c"
       
   573   and xy: "(x, y) \<in> r"
       
   574   and xz: "(x, z) \<in> r^+"
       
   575   and neq_yz: "y \<noteq> z"
       
   576   shows "(y, z) \<in> r^+"
       
   577 proof -
       
   578   from xz neq_yz show ?thesis
       
   579   proof(induct)
       
   580     case (base ya)
       
   581     from xy unique base show ?case by auto
       
   582   next
       
   583     case (step ya z)
       
   584     show ?case
       
   585     proof(cases "y = ya")
       
   586       case True
       
   587       from True step show ?thesis by auto
       
   588     next
       
   589       case False
       
   590       from False step 
       
   591       have "(y, ya) \<in> r\<^sup>+" by auto
       
   592       with step show ?thesis by auto
       
   593     qed
       
   594   qed
       
   595 qed
       
   596 
       
   597 lemma unique_chain:
       
   598   assumes unique: "\<And> a b c. \<lbrakk>(a, b) \<in> r; (a, c) \<in> r\<rbrakk> \<Longrightarrow> b = c"
       
   599   and xy: "(x, y) \<in> r^+"
       
   600   and xz: "(x, z) \<in> r^+"
       
   601   and neq_yz: "y \<noteq> z"
       
   602   shows "(y, z) \<in> r^+ \<or> (z, y) \<in> r^+"
       
   603 proof -
       
   604   from xy xz neq_yz show ?thesis
       
   605   proof(induct)
       
   606     case (base y)
       
   607     have h1: "(x, y) \<in> r" and h2: "(x, z) \<in> r\<^sup>+" and h3: "y \<noteq> z" using base by auto
       
   608     from unique_base [OF _ h1 h2 h3] and unique show ?case by auto
       
   609   next
       
   610     case (step y za)
       
   611     show ?case
       
   612     proof(cases "y = z")
       
   613       case True
       
   614       from True step show ?thesis by auto
       
   615     next
       
   616       case False
       
   617       from False step have "(y, z) \<in> r\<^sup>+ \<or> (z, y) \<in> r\<^sup>+" by auto
       
   618       thus ?thesis
       
   619       proof
       
   620         assume "(z, y) \<in> r\<^sup>+"
       
   621         with step have "(z, za) \<in> r\<^sup>+" by auto
       
   622         thus ?thesis by auto
       
   623       next
       
   624         assume h: "(y, z) \<in> r\<^sup>+"
       
   625         from step have yza: "(y, za) \<in> r" by simp
       
   626         from step have "za \<noteq> z" by simp
       
   627         from unique_minus [OF _ yza h this] and unique
       
   628         have "(za, z) \<in> r\<^sup>+" by auto
       
   629         thus ?thesis by auto
       
   630       qed
       
   631     qed
       
   632   qed
       
   633 qed
       
   634 
       
   635 text {*
       
   636   The following three lemmas show that @{text "RAG"} does not change
       
   637   by the happening of @{text "Set"}, @{text "Create"} and @{text "Exit"}
       
   638   events, respectively.
       
   639 *}
       
   640 
       
   641 lemma RAG_set_unchanged: "(RAG (Set th prio # s)) = RAG s"
       
   642 apply (unfold s_RAG_def s_waiting_def wq_def)
       
   643 by (simp add:Let_def)
       
   644 
       
   645 lemma RAG_create_unchanged: "(RAG (Create th prio # s)) = RAG s"
       
   646 apply (unfold s_RAG_def s_waiting_def wq_def)
       
   647 by (simp add:Let_def)
       
   648 
       
   649 lemma RAG_exit_unchanged: "(RAG (Exit th # s)) = RAG s"
       
   650 apply (unfold s_RAG_def s_waiting_def wq_def)
       
   651 by (simp add:Let_def)
       
   652 
       
   653 
       
   654 text {* 
       
   655   The following lemmas are used in the proof of 
       
   656   lemma @{text "step_RAG_v"}, which characterizes how the @{text "RAG"} is changed
       
   657   by @{text "V"}-events. 
       
   658   However, since our model is very concise, such  seemingly obvious lemmas need to be derived from scratch,
       
   659   starting from the model definitions.
       
   660 *}
       
   661 lemma step_v_hold_inv[elim_format]:
       
   662   "\<And>c t. \<lbrakk>vt (V th cs # s); 
       
   663           \<not> holding (wq s) t c; holding (wq (V th cs # s)) t c\<rbrakk> \<Longrightarrow> 
       
   664             next_th s th cs t \<and> c = cs"
       
   665 proof -
       
   666   fix c t
       
   667   assume vt: "vt (V th cs # s)"
       
   668     and nhd: "\<not> holding (wq s) t c"
       
   669     and hd: "holding (wq (V th cs # s)) t c"
       
   670   show "next_th s th cs t \<and> c = cs"
       
   671   proof(cases "c = cs")
       
   672     case False
       
   673     with nhd hd show ?thesis
       
   674       by (unfold cs_holding_def wq_def, auto simp:Let_def)
       
   675   next
       
   676     case True
       
   677     with step_back_step [OF vt] 
       
   678     have "step s (V th c)" by simp
       
   679     hence "next_th s th cs t"
       
   680     proof(cases)
       
   681       assume "holding s th c"
       
   682       with nhd hd show ?thesis
       
   683         apply (unfold s_holding_def cs_holding_def wq_def next_th_def,
       
   684                auto simp:Let_def split:list.splits if_splits)
       
   685         proof -
       
   686           assume " hd (SOME q. distinct q \<and> q = []) \<in> set (SOME q. distinct q \<and> q = [])"
       
   687           moreover have "\<dots> = set []"
       
   688           proof(rule someI2)
       
   689             show "distinct [] \<and> [] = []" by auto
       
   690           next
       
   691             fix x assume "distinct x \<and> x = []"
       
   692             thus "set x = set []" by auto
       
   693           qed
       
   694           ultimately show False by auto
       
   695         next
       
   696           assume " hd (SOME q. distinct q \<and> q = []) \<in> set (SOME q. distinct q \<and> q = [])"
       
   697           moreover have "\<dots> = set []"
       
   698           proof(rule someI2)
       
   699             show "distinct [] \<and> [] = []" by auto
       
   700           next
       
   701             fix x assume "distinct x \<and> x = []"
       
   702             thus "set x = set []" by auto
       
   703           qed
       
   704           ultimately show False by auto
       
   705         qed
       
   706     qed
       
   707     with True show ?thesis by auto
       
   708   qed
       
   709 qed
       
   710 
       
   711 text {* 
       
   712   The following @{text "step_v_wait_inv"} is also an obvious lemma, which, however, needs to be
       
   713   derived from scratch, which confirms the correctness of the definition of @{text "next_th"}.
       
   714 *}
       
   715 lemma step_v_wait_inv[elim_format]:
       
   716     "\<And>t c. \<lbrakk>vt (V th cs # s); \<not> waiting (wq (V th cs # s)) t c; waiting (wq s) t c
       
   717            \<rbrakk>
       
   718           \<Longrightarrow> (next_th s th cs t \<and> cs = c)"
       
   719 proof -
       
   720   fix t c 
       
   721   assume vt: "vt (V th cs # s)"
       
   722     and nw: "\<not> waiting (wq (V th cs # s)) t c"
       
   723     and wt: "waiting (wq s) t c"
       
   724   from vt interpret vt_v: valid_trace_e s "V th cs" 
       
   725     by  (cases, unfold_locales, simp)
       
   726   show "next_th s th cs t \<and> cs = c"
       
   727   proof(cases "cs = c")
       
   728     case False
       
   729     with nw wt show ?thesis
       
   730       by (auto simp:cs_waiting_def wq_def Let_def)
       
   731   next
       
   732     case True
       
   733     from nw[folded True] wt[folded True]
       
   734     have "next_th s th cs t"
       
   735       apply (unfold next_th_def, auto simp:cs_waiting_def wq_def Let_def split:list.splits)
       
   736     proof -
       
   737       fix a list
       
   738       assume t_in: "t \<in> set list"
       
   739         and t_ni: "t \<notin> set (SOME q. distinct q \<and> set q = set list)"
       
   740         and eq_wq: "wq_fun (schs s) cs = a # list"
       
   741       have " set (SOME q. distinct q \<and> set q = set list) = set list"
       
   742       proof(rule someI2)
       
   743         from vt_v.wq_distinct[of cs] and eq_wq[folded wq_def]
       
   744         show "distinct list \<and> set list = set list" by auto
       
   745       next
       
   746         show "\<And>x. distinct x \<and> set x = set list \<Longrightarrow> set x = set list"
       
   747           by auto
       
   748       qed
       
   749       with t_ni and t_in show "a = th" by auto
       
   750     next
       
   751       fix a list
       
   752       assume t_in: "t \<in> set list"
       
   753         and t_ni: "t \<notin> set (SOME q. distinct q \<and> set q = set list)"
       
   754         and eq_wq: "wq_fun (schs s) cs = a # list"
       
   755       have " set (SOME q. distinct q \<and> set q = set list) = set list"
       
   756       proof(rule someI2)
       
   757         from vt_v.wq_distinct[of cs] and eq_wq[folded wq_def]
       
   758         show "distinct list \<and> set list = set list" by auto
       
   759       next
       
   760         show "\<And>x. distinct x \<and> set x = set list \<Longrightarrow> set x = set list"
       
   761           by auto
       
   762       qed
       
   763       with t_ni and t_in show "t = hd (SOME q. distinct q \<and> set q = set list)" by auto
       
   764     next
       
   765       fix a list
       
   766       assume eq_wq: "wq_fun (schs s) cs = a # list"
       
   767       from step_back_step[OF vt]
       
   768       show "a = th"
       
   769       proof(cases)
       
   770         assume "holding s th cs"
       
   771         with eq_wq show ?thesis
       
   772           by (unfold s_holding_def wq_def, auto)
       
   773       qed
       
   774     qed
       
   775     with True show ?thesis by simp
       
   776   qed
       
   777 qed
       
   778 
       
   779 lemma step_v_not_wait[consumes 3]:
       
   780   "\<lbrakk>vt (V th cs # s); next_th s th cs t; waiting (wq (V th cs # s)) t cs\<rbrakk> \<Longrightarrow> False"
       
   781   by (unfold next_th_def cs_waiting_def wq_def, auto simp:Let_def)
       
   782 
       
   783 lemma step_v_release:
       
   784   "\<lbrakk>vt (V th cs # s); holding (wq (V th cs # s)) th cs\<rbrakk> \<Longrightarrow> False"
       
   785 proof -
       
   786   assume vt: "vt (V th cs # s)"
       
   787     and hd: "holding (wq (V th cs # s)) th cs"
       
   788   from vt interpret vt_v: valid_trace_e s "V th cs"
       
   789     by (cases, unfold_locales, simp+)
       
   790   from step_back_step [OF vt] and hd
       
   791   show "False"
       
   792   proof(cases)
       
   793     assume "holding (wq (V th cs # s)) th cs" and "holding s th cs"
       
   794     thus ?thesis
       
   795       apply (unfold s_holding_def wq_def cs_holding_def)
       
   796       apply (auto simp:Let_def split:list.splits)
       
   797     proof -
       
   798       fix list
       
   799       assume eq_wq[folded wq_def]: 
       
   800         "wq_fun (schs s) cs = hd (SOME q. distinct q \<and> set q = set list) # list"
       
   801       and hd_in: "hd (SOME q. distinct q \<and> set q = set list)
       
   802             \<in> set (SOME q. distinct q \<and> set q = set list)"
       
   803       have "set (SOME q. distinct q \<and> set q = set list) = set list"
       
   804       proof(rule someI2)
       
   805         from vt_v.wq_distinct[of cs] and eq_wq
       
   806         show "distinct list \<and> set list = set list" by auto
       
   807       next
       
   808         show "\<And>x. distinct x \<and> set x = set list \<Longrightarrow> set x = set list"
       
   809           by auto
       
   810       qed
       
   811       moreover have "distinct  (hd (SOME q. distinct q \<and> set q = set list) # list)"
       
   812       proof -
       
   813         from vt_v.wq_distinct[of cs] and eq_wq
       
   814         show ?thesis by auto
       
   815       qed
       
   816       moreover note eq_wq and hd_in
       
   817       ultimately show "False" by auto
       
   818     qed
       
   819   qed
       
   820 qed
       
   821 
       
   822 lemma step_v_get_hold:
       
   823   "\<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"
       
   824   apply (unfold cs_holding_def next_th_def wq_def,
       
   825          auto simp:Let_def)
       
   826 proof -
       
   827   fix rest
       
   828   assume vt: "vt (V th cs # s)"
       
   829     and eq_wq[folded wq_def]: " wq_fun (schs s) cs = th # rest"
       
   830     and nrest: "rest \<noteq> []"
       
   831     and ni: "hd (SOME q. distinct q \<and> set q = set rest)
       
   832             \<notin> set (SOME q. distinct q \<and> set q = set rest)"
       
   833   from vt interpret vt_v: valid_trace_e s "V th cs"
       
   834     by (cases, unfold_locales, simp+)
       
   835   have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
       
   836   proof(rule someI2)
       
   837     from vt_v.wq_distinct[of cs] and eq_wq
       
   838     show "distinct rest \<and> set rest = set rest" by auto
       
   839   next
       
   840     fix x assume "distinct x \<and> set x = set rest"
       
   841     hence "set x = set rest" by auto
       
   842     with nrest
       
   843     show "x \<noteq> []" by (case_tac x, auto)
       
   844   qed
       
   845   with ni show "False" by auto
       
   846 qed
       
   847 
       
   848 lemma step_v_release_inv[elim_format]:
       
   849 "\<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> 
       
   850   c = cs \<and> t = th"
       
   851   apply (unfold cs_holding_def wq_def, auto simp:Let_def split:if_splits list.splits)
       
   852   proof -
       
   853     fix a list
       
   854     assume vt: "vt (V th cs # s)" and eq_wq: "wq_fun (schs s) cs = a # list"
       
   855     from step_back_step [OF vt] show "a = th"
       
   856     proof(cases)
       
   857       assume "holding s th cs" with eq_wq
       
   858       show ?thesis
       
   859         by (unfold s_holding_def wq_def, auto)
       
   860     qed
       
   861   next
       
   862     fix a list
       
   863     assume vt: "vt (V th cs # s)" and eq_wq: "wq_fun (schs s) cs = a # list"
       
   864     from step_back_step [OF vt] show "a = th"
       
   865     proof(cases)
       
   866       assume "holding s th cs" with eq_wq
       
   867       show ?thesis
       
   868         by (unfold s_holding_def wq_def, auto)
       
   869     qed
       
   870   qed
       
   871 
       
   872 lemma step_v_waiting_mono:
       
   873   "\<And>t c. \<lbrakk>vt (V th cs # s); waiting (wq (V th cs # s)) t c\<rbrakk> \<Longrightarrow> waiting (wq s) t c"
       
   874 proof -
       
   875   fix t c
       
   876   let ?s' = "(V th cs # s)"
       
   877   assume vt: "vt ?s'" 
       
   878     and wt: "waiting (wq ?s') t c"
       
   879   from vt interpret vt_v: valid_trace_e s "V th cs"
       
   880     by (cases, unfold_locales, simp+)
       
   881   show "waiting (wq s) t c"
       
   882   proof(cases "c = cs")
       
   883     case False
       
   884     assume neq_cs: "c \<noteq> cs"
       
   885     hence "waiting (wq ?s') t c = waiting (wq s) t c"
       
   886       by (unfold cs_waiting_def wq_def, auto simp:Let_def)
       
   887     with wt show ?thesis by simp
       
   888   next
       
   889     case True
       
   890     with wt show ?thesis
       
   891       apply (unfold cs_waiting_def wq_def, auto simp:Let_def split:list.splits)
       
   892     proof -
       
   893       fix a list
       
   894       assume not_in: "t \<notin> set list"
       
   895         and is_in: "t \<in> set (SOME q. distinct q \<and> set q = set list)"
       
   896         and eq_wq: "wq_fun (schs s) cs = a # list"
       
   897       have "set (SOME q. distinct q \<and> set q = set list) = set list"
       
   898       proof(rule someI2)
       
   899         from vt_v.wq_distinct [of cs]
       
   900         and eq_wq[folded wq_def]
       
   901         show "distinct list \<and> set list = set list" by auto
       
   902       next
       
   903         fix x assume "distinct x \<and> set x = set list"
       
   904         thus "set x = set list" by auto
       
   905       qed
       
   906       with not_in is_in show "t = a" by auto
       
   907     next
       
   908       fix list
       
   909       assume is_waiting: "waiting (wq (V th cs # s)) t cs"
       
   910       and eq_wq: "wq_fun (schs s) cs = t # list"
       
   911       hence "t \<in> set list"
       
   912         apply (unfold wq_def, auto simp:Let_def cs_waiting_def)
       
   913       proof -
       
   914         assume " t \<in> set (SOME q. distinct q \<and> set q = set list)"
       
   915         moreover have "\<dots> = set list" 
       
   916         proof(rule someI2)
       
   917           from vt_v.wq_distinct [of cs]
       
   918             and eq_wq[folded wq_def]
       
   919           show "distinct list \<and> set list = set list" by auto
       
   920         next
       
   921           fix x assume "distinct x \<and> set x = set list" 
       
   922           thus "set x = set list" by auto
       
   923         qed
       
   924         ultimately show "t \<in> set list" by simp
       
   925       qed
       
   926       with eq_wq and vt_v.wq_distinct [of cs, unfolded wq_def]
       
   927       show False by auto
       
   928     qed
       
   929   qed
       
   930 qed
       
   931 
       
   932 text {* (* ddd *) 
       
   933   The following @{text "step_RAG_v"} lemma charaterizes how @{text "RAG"} is changed
       
   934   with the happening of @{text "V"}-events:
       
   935 *}
       
   936 lemma step_RAG_v:
       
   937 assumes vt:
       
   938   "vt (V th cs#s)"
       
   939 shows "
       
   940   RAG (V th cs # s) =
       
   941   RAG s - {(Cs cs, Th th)} -
       
   942   {(Th th', Cs cs) |th'. next_th s th cs th'} \<union>
       
   943   {(Cs cs, Th th') |th'.  next_th s th cs th'}"
       
   944   apply (insert vt, unfold s_RAG_def) 
       
   945   apply (auto split:if_splits list.splits simp:Let_def)
       
   946   apply (auto elim: step_v_waiting_mono step_v_hold_inv 
       
   947               step_v_release step_v_wait_inv
       
   948               step_v_get_hold step_v_release_inv)
       
   949   apply (erule_tac step_v_not_wait, auto)
       
   950   done
       
   951 
       
   952 text {* 
       
   953   The following @{text "step_RAG_p"} lemma charaterizes how @{text "RAG"} is changed
       
   954   with the happening of @{text "P"}-events:
       
   955 *}
       
   956 lemma step_RAG_p:
       
   957   "vt (P th cs#s) \<Longrightarrow>
       
   958   RAG (P th cs # s) =  (if (wq s cs = []) then RAG s \<union> {(Cs cs, Th th)}
       
   959                                              else RAG s \<union> {(Th th, Cs cs)})"
       
   960   apply(simp only: s_RAG_def wq_def)
       
   961   apply (auto split:list.splits prod.splits simp:Let_def wq_def cs_waiting_def cs_holding_def)
       
   962   apply(case_tac "csa = cs", auto)
       
   963   apply(fold wq_def)
       
   964   apply(drule_tac step_back_step)
       
   965   apply(ind_cases " step s (P (hd (wq s cs)) cs)")
       
   966   apply(simp add:s_RAG_def wq_def cs_holding_def)
       
   967   apply(auto)
       
   968   done
       
   969 
       
   970 
       
   971 lemma RAG_target_th: "(Th th, x) \<in> RAG (s::state) \<Longrightarrow> \<exists> cs. x = Cs cs"
       
   972   by (unfold s_RAG_def, auto)
       
   973 
       
   974 context valid_trace
       
   975 begin
       
   976 
       
   977 text {*
       
   978   The following lemma shows that @{text "RAG"} is acyclic.
       
   979   The overall structure is by induction on the formation of @{text "vt s"}
       
   980   and then case analysis on event @{text "e"}, where the non-trivial cases 
       
   981   for those for @{text "V"} and @{text "P"} events.
       
   982 *}
       
   983 lemma acyclic_RAG:
       
   984   shows "acyclic (RAG s)"
       
   985 using vt
       
   986 proof(induct)
       
   987   case (vt_cons s e)
       
   988   interpret vt_s: valid_trace s using vt_cons(1)
       
   989     by (unfold_locales, simp)
       
   990   assume ih: "acyclic (RAG s)"
       
   991     and stp: "step s e"
       
   992     and vt: "vt s"
       
   993   show ?case
       
   994   proof(cases e)
       
   995     case (Create th prio)
       
   996     with ih
       
   997     show ?thesis by (simp add:RAG_create_unchanged)
       
   998   next
       
   999     case (Exit th)
       
  1000     with ih show ?thesis by (simp add:RAG_exit_unchanged)
       
  1001   next
       
  1002     case (V th cs)
       
  1003     from V vt stp have vtt: "vt (V th cs#s)" by auto
       
  1004     from step_RAG_v [OF this]
       
  1005     have eq_de: 
       
  1006       "RAG (e # s) = 
       
  1007       RAG s - {(Cs cs, Th th)} - {(Th th', Cs cs) |th'. next_th s th cs th'} \<union>
       
  1008       {(Cs cs, Th th') |th'. next_th s th cs th'}"
       
  1009       (is "?L = (?A - ?B - ?C) \<union> ?D") by (simp add:V)
       
  1010     from ih have ac: "acyclic (?A - ?B - ?C)" by (auto elim:acyclic_subset)
       
  1011     from step_back_step [OF vtt]
       
  1012     have "step s (V th cs)" .
       
  1013     thus ?thesis
       
  1014     proof(cases)
       
  1015       assume "holding s th cs"
       
  1016       hence th_in: "th \<in> set (wq s cs)" and
       
  1017         eq_hd: "th = hd (wq s cs)" unfolding s_holding_def wq_def by auto
       
  1018       then obtain rest where
       
  1019         eq_wq: "wq s cs = th#rest"
       
  1020         by (cases "wq s cs", auto)
       
  1021       show ?thesis
       
  1022       proof(cases "rest = []")
       
  1023         case False
       
  1024         let ?th' = "hd (SOME q. distinct q \<and> set q = set rest)"
       
  1025         from eq_wq False have eq_D: "?D = {(Cs cs, Th ?th')}"
       
  1026           by (unfold next_th_def, auto)
       
  1027         let ?E = "(?A - ?B - ?C)"
       
  1028         have "(Th ?th', Cs cs) \<notin> ?E\<^sup>*"
       
  1029         proof
       
  1030           assume "(Th ?th', Cs cs) \<in> ?E\<^sup>*"
       
  1031           hence " (Th ?th', Cs cs) \<in> ?E\<^sup>+" by (simp add: rtrancl_eq_or_trancl)
       
  1032           from tranclD [OF this]
       
  1033           obtain x where th'_e: "(Th ?th', x) \<in> ?E" by blast
       
  1034           hence th_d: "(Th ?th', x) \<in> ?A" by simp
       
  1035           from RAG_target_th [OF this]
       
  1036           obtain cs' where eq_x: "x = Cs cs'" by auto
       
  1037           with th_d have "(Th ?th', Cs cs') \<in> ?A" by simp
       
  1038           hence wt_th': "waiting s ?th' cs'"
       
  1039             unfolding s_RAG_def s_waiting_def cs_waiting_def wq_def by simp
       
  1040           hence "cs' = cs"
       
  1041           proof(rule vt_s.waiting_unique)
       
  1042             from eq_wq vt_s.wq_distinct[of cs]
       
  1043             show "waiting s ?th' cs" 
       
  1044               apply (unfold s_waiting_def wq_def, auto)
       
  1045             proof -
       
  1046               assume hd_in: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> set rest"
       
  1047                 and eq_wq: "wq_fun (schs s) cs = th # rest"
       
  1048               have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
       
  1049               proof(rule someI2)
       
  1050                 from vt_s.wq_distinct[of cs] and eq_wq
       
  1051                 show "distinct rest \<and> set rest = set rest" unfolding wq_def by auto
       
  1052               next
       
  1053                 fix x assume "distinct x \<and> set x = set rest"
       
  1054                 with False show "x \<noteq> []" by auto
       
  1055               qed
       
  1056               hence "hd (SOME q. distinct q \<and> set q = set rest) \<in> 
       
  1057                 set (SOME q. distinct q \<and> set q = set rest)" by auto
       
  1058               moreover have "\<dots> = set rest" 
       
  1059               proof(rule someI2)
       
  1060                 from vt_s.wq_distinct[of cs] and eq_wq
       
  1061                 show "distinct rest \<and> set rest = set rest" unfolding wq_def by auto
       
  1062               next
       
  1063                 show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
       
  1064               qed
       
  1065               moreover note hd_in
       
  1066               ultimately show "hd (SOME q. distinct q \<and> set q = set rest) = th" by auto
       
  1067             next
       
  1068               assume hd_in: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> set rest"
       
  1069                 and eq_wq: "wq s cs = hd (SOME q. distinct q \<and> set q = set rest) # rest"
       
  1070               have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
       
  1071               proof(rule someI2)
       
  1072                 from vt_s.wq_distinct[of cs] and eq_wq
       
  1073                 show "distinct rest \<and> set rest = set rest" by auto
       
  1074               next
       
  1075                 fix x assume "distinct x \<and> set x = set rest"
       
  1076                 with False show "x \<noteq> []" by auto
       
  1077               qed
       
  1078               hence "hd (SOME q. distinct q \<and> set q = set rest) \<in> 
       
  1079                 set (SOME q. distinct q \<and> set q = set rest)" by auto
       
  1080               moreover have "\<dots> = set rest" 
       
  1081               proof(rule someI2)
       
  1082                 from vt_s.wq_distinct[of cs] and eq_wq
       
  1083                 show "distinct rest \<and> set rest = set rest" by auto
       
  1084               next
       
  1085                 show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
       
  1086               qed
       
  1087               moreover note hd_in
       
  1088               ultimately show False by auto
       
  1089             qed
       
  1090           qed
       
  1091           with th'_e eq_x have "(Th ?th', Cs cs) \<in> ?E" by simp
       
  1092           with False
       
  1093           show "False" by (auto simp: next_th_def eq_wq)
       
  1094         qed
       
  1095         with acyclic_insert[symmetric] and ac
       
  1096           and eq_de eq_D show ?thesis by auto
       
  1097       next
       
  1098         case True
       
  1099         with eq_wq
       
  1100         have eq_D: "?D = {}"
       
  1101           by (unfold next_th_def, auto)
       
  1102         with eq_de ac
       
  1103         show ?thesis by auto
       
  1104       qed 
       
  1105     qed
       
  1106   next
       
  1107     case (P th cs)
       
  1108     from P vt stp have vtt: "vt (P th cs#s)" by auto
       
  1109     from step_RAG_p [OF this] P
       
  1110     have "RAG (e # s) = 
       
  1111       (if wq s cs = [] then RAG s \<union> {(Cs cs, Th th)} else 
       
  1112       RAG s \<union> {(Th th, Cs cs)})" (is "?L = ?R")
       
  1113       by simp
       
  1114     moreover have "acyclic ?R"
       
  1115     proof(cases "wq s cs = []")
       
  1116       case True
       
  1117       hence eq_r: "?R =  RAG s \<union> {(Cs cs, Th th)}" by simp
       
  1118       have "(Th th, Cs cs) \<notin> (RAG s)\<^sup>*"
       
  1119       proof
       
  1120         assume "(Th th, Cs cs) \<in> (RAG s)\<^sup>*"
       
  1121         hence "(Th th, Cs cs) \<in> (RAG s)\<^sup>+" by (simp add: rtrancl_eq_or_trancl)
       
  1122         from tranclD2 [OF this]
       
  1123         obtain x where "(x, Cs cs) \<in> RAG s" by auto
       
  1124         with True show False by (auto simp:s_RAG_def cs_waiting_def)
       
  1125       qed
       
  1126       with acyclic_insert ih eq_r show ?thesis by auto
       
  1127     next
       
  1128       case False
       
  1129       hence eq_r: "?R =  RAG s \<union> {(Th th, Cs cs)}" by simp
       
  1130       have "(Cs cs, Th th) \<notin> (RAG s)\<^sup>*"
       
  1131       proof
       
  1132         assume "(Cs cs, Th th) \<in> (RAG s)\<^sup>*"
       
  1133         hence "(Cs cs, Th th) \<in> (RAG s)\<^sup>+" by (simp add: rtrancl_eq_or_trancl)
       
  1134         moreover from step_back_step [OF vtt] have "step s (P th cs)" .
       
  1135         ultimately show False
       
  1136         proof -
       
  1137           show " \<lbrakk>(Cs cs, Th th) \<in> (RAG s)\<^sup>+; step s (P th cs)\<rbrakk> \<Longrightarrow> False"
       
  1138             by (ind_cases "step s (P th cs)", simp)
       
  1139         qed
       
  1140       qed
       
  1141       with acyclic_insert ih eq_r show ?thesis by auto
       
  1142       qed
       
  1143       ultimately show ?thesis by simp
       
  1144     next
       
  1145       case (Set thread prio)
       
  1146       with ih
       
  1147       thm RAG_set_unchanged
       
  1148       show ?thesis by (simp add:RAG_set_unchanged)
       
  1149     qed
       
  1150   next
       
  1151     case vt_nil
       
  1152     show "acyclic (RAG ([]::state))"
       
  1153       by (auto simp: s_RAG_def cs_waiting_def 
       
  1154         cs_holding_def wq_def acyclic_def)
       
  1155 qed
       
  1156 
       
  1157 
       
  1158 lemma finite_RAG:
       
  1159   shows "finite (RAG s)"
       
  1160 proof -
       
  1161   from vt show ?thesis
       
  1162   proof(induct)
       
  1163     case (vt_cons s e)
       
  1164     interpret vt_s: valid_trace s using vt_cons(1)
       
  1165       by (unfold_locales, simp)
       
  1166     assume ih: "finite (RAG s)"
       
  1167       and stp: "step s e"
       
  1168       and vt: "vt s"
       
  1169     show ?case
       
  1170     proof(cases e)
       
  1171       case (Create th prio)
       
  1172       with ih
       
  1173       show ?thesis by (simp add:RAG_create_unchanged)
       
  1174     next
       
  1175       case (Exit th)
       
  1176       with ih show ?thesis by (simp add:RAG_exit_unchanged)
       
  1177     next
       
  1178       case (V th cs)
       
  1179       from V vt stp have vtt: "vt (V th cs#s)" by auto
       
  1180       from step_RAG_v [OF this]
       
  1181       have eq_de: "RAG (e # s) = 
       
  1182                    RAG s - {(Cs cs, Th th)} - {(Th th', Cs cs) |th'. next_th s th cs th'} \<union>
       
  1183                       {(Cs cs, Th th') |th'. next_th s th cs th'}
       
  1184 "
       
  1185         (is "?L = (?A - ?B - ?C) \<union> ?D") by (simp add:V)
       
  1186       moreover from ih have ac: "finite (?A - ?B - ?C)" by simp
       
  1187       moreover have "finite ?D"
       
  1188       proof -
       
  1189         have "?D = {} \<or> (\<exists> a. ?D = {a})" 
       
  1190           by (unfold next_th_def, auto)
       
  1191         thus ?thesis
       
  1192         proof
       
  1193           assume h: "?D = {}"
       
  1194           show ?thesis by (unfold h, simp)
       
  1195         next
       
  1196           assume "\<exists> a. ?D = {a}"
       
  1197           thus ?thesis
       
  1198             by (metis finite.simps)
       
  1199         qed
       
  1200       qed
       
  1201       ultimately show ?thesis by simp
       
  1202     next
       
  1203       case (P th cs)
       
  1204       from P vt stp have vtt: "vt (P th cs#s)" by auto
       
  1205       from step_RAG_p [OF this] P
       
  1206       have "RAG (e # s) = 
       
  1207               (if wq s cs = [] then RAG s \<union> {(Cs cs, Th th)} else 
       
  1208                                     RAG s \<union> {(Th th, Cs cs)})" (is "?L = ?R")
       
  1209         by simp
       
  1210       moreover have "finite ?R"
       
  1211       proof(cases "wq s cs = []")
       
  1212         case True
       
  1213         hence eq_r: "?R =  RAG s \<union> {(Cs cs, Th th)}" by simp
       
  1214         with True and ih show ?thesis by auto
       
  1215       next
       
  1216         case False
       
  1217         hence "?R = RAG s \<union> {(Th th, Cs cs)}" by simp
       
  1218         with False and ih show ?thesis by auto
       
  1219       qed
       
  1220       ultimately show ?thesis by auto
       
  1221     next
       
  1222       case (Set thread prio)
       
  1223       with ih
       
  1224       show ?thesis by (simp add:RAG_set_unchanged)
       
  1225     qed
       
  1226   next
       
  1227     case vt_nil
       
  1228     show "finite (RAG ([]::state))"
       
  1229       by (auto simp: s_RAG_def cs_waiting_def 
       
  1230                    cs_holding_def wq_def acyclic_def)
       
  1231   qed
       
  1232 qed
       
  1233 
       
  1234 text {* Several useful lemmas *}
       
  1235 
       
  1236 lemma wf_dep_converse: 
       
  1237   shows "wf ((RAG s)^-1)"
       
  1238 proof(rule finite_acyclic_wf_converse)
       
  1239   from finite_RAG 
       
  1240   show "finite (RAG s)" .
       
  1241 next
       
  1242   from acyclic_RAG
       
  1243   show "acyclic (RAG s)" .
       
  1244 qed
       
  1245 
       
  1246 end
       
  1247 
       
  1248 lemma hd_np_in: "x \<in> set l \<Longrightarrow> hd l \<in> set l"
       
  1249   by (induct l, auto)
       
  1250 
       
  1251 lemma th_chasing: "(Th th, Cs cs) \<in> RAG (s::state) \<Longrightarrow> \<exists> th'. (Cs cs, Th th') \<in> RAG s"
       
  1252   by (auto simp:s_RAG_def s_holding_def cs_holding_def cs_waiting_def wq_def dest:hd_np_in)
       
  1253 
       
  1254 context valid_trace
       
  1255 begin
       
  1256 
       
  1257 lemma wq_threads: 
       
  1258   assumes h: "th \<in> set (wq s cs)"
       
  1259   shows "th \<in> threads s"
       
  1260 proof -
       
  1261  from vt and h show ?thesis
       
  1262   proof(induct arbitrary: th cs)
       
  1263     case (vt_cons s e)
       
  1264     interpret vt_s: valid_trace s
       
  1265       using vt_cons(1) by (unfold_locales, auto)
       
  1266     assume ih: "\<And>th cs. th \<in> set (wq s cs) \<Longrightarrow> th \<in> threads s"
       
  1267       and stp: "step s e"
       
  1268       and vt: "vt s"
       
  1269       and h: "th \<in> set (wq (e # s) cs)"
       
  1270     show ?case
       
  1271     proof(cases e)
       
  1272       case (Create th' prio)
       
  1273       with ih h show ?thesis
       
  1274         by (auto simp:wq_def Let_def)
       
  1275     next
       
  1276       case (Exit th')
       
  1277       with stp ih h show ?thesis
       
  1278         apply (auto simp:wq_def Let_def)
       
  1279         apply (ind_cases "step s (Exit th')")
       
  1280         apply (auto simp:runing_def readys_def s_holding_def s_waiting_def holdents_def
       
  1281                s_RAG_def s_holding_def cs_holding_def)
       
  1282         done
       
  1283     next
       
  1284       case (V th' cs')
       
  1285       show ?thesis
       
  1286       proof(cases "cs' = cs")
       
  1287         case False
       
  1288         with h
       
  1289         show ?thesis
       
  1290           apply(unfold wq_def V, auto simp:Let_def V split:prod.splits, fold wq_def)
       
  1291           by (drule_tac ih, simp)
       
  1292       next
       
  1293         case True
       
  1294         from h
       
  1295         show ?thesis
       
  1296         proof(unfold V wq_def)
       
  1297           assume th_in: "th \<in> set (wq_fun (schs (V th' cs' # s)) cs)" (is "th \<in> set ?l")
       
  1298           show "th \<in> threads (V th' cs' # s)"
       
  1299           proof(cases "cs = cs'")
       
  1300             case False
       
  1301             hence "?l = wq_fun (schs s) cs" by (simp add:Let_def)
       
  1302             with th_in have " th \<in> set (wq s cs)" 
       
  1303               by (fold wq_def, simp)
       
  1304             from ih [OF this] show ?thesis by simp
       
  1305           next
       
  1306             case True
       
  1307             show ?thesis
       
  1308             proof(cases "wq_fun (schs s) cs'")
       
  1309               case Nil
       
  1310               with h V show ?thesis
       
  1311                 apply (auto simp:wq_def Let_def split:if_splits)
       
  1312                 by (fold wq_def, drule_tac ih, simp)
       
  1313             next
       
  1314               case (Cons a rest)
       
  1315               assume eq_wq: "wq_fun (schs s) cs' = a # rest"
       
  1316               with h V show ?thesis
       
  1317                 apply (auto simp:Let_def wq_def split:if_splits)
       
  1318               proof -
       
  1319                 assume th_in: "th \<in> set (SOME q. distinct q \<and> set q = set rest)"
       
  1320                 have "set (SOME q. distinct q \<and> set q = set rest) = set rest" 
       
  1321                 proof(rule someI2)
       
  1322                   from vt_s.wq_distinct[of cs'] and eq_wq[folded wq_def]
       
  1323                   show "distinct rest \<and> set rest = set rest" by auto
       
  1324                 next
       
  1325                   show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest"
       
  1326                     by auto
       
  1327                 qed
       
  1328                 with eq_wq th_in have "th \<in> set (wq_fun (schs s) cs')" by auto
       
  1329                 from ih[OF this[folded wq_def]] show "th \<in> threads s" .
       
  1330               next
       
  1331                 assume th_in: "th \<in> set (wq_fun (schs s) cs)"
       
  1332                 from ih[OF this[folded wq_def]]
       
  1333                 show "th \<in> threads s" .
       
  1334               qed
       
  1335             qed
       
  1336           qed
       
  1337         qed
       
  1338       qed
       
  1339     next
       
  1340       case (P th' cs')
       
  1341       from h stp
       
  1342       show ?thesis
       
  1343         apply (unfold P wq_def)
       
  1344         apply (auto simp:Let_def split:if_splits, fold wq_def)
       
  1345         apply (auto intro:ih)
       
  1346         apply(ind_cases "step s (P th' cs')")
       
  1347         by (unfold runing_def readys_def, auto)
       
  1348     next
       
  1349       case (Set thread prio)
       
  1350       with ih h show ?thesis
       
  1351         by (auto simp:wq_def Let_def)
       
  1352     qed
       
  1353   next
       
  1354     case vt_nil
       
  1355     thus ?case by (auto simp:wq_def)
       
  1356   qed
       
  1357 qed
       
  1358 
       
  1359 lemma range_in: "\<lbrakk>(Th th) \<in> Range (RAG (s::state))\<rbrakk> \<Longrightarrow> th \<in> threads s"
       
  1360   apply(unfold s_RAG_def cs_waiting_def cs_holding_def)
       
  1361   by (auto intro:wq_threads)
       
  1362 
       
  1363 lemma readys_v_eq:
       
  1364   assumes neq_th: "th \<noteq> thread"
       
  1365   and eq_wq: "wq s cs = thread#rest"
       
  1366   and not_in: "th \<notin>  set rest"
       
  1367   shows "(th \<in> readys (V thread cs#s)) = (th \<in> readys s)"
       
  1368 proof -
       
  1369   from assms show ?thesis
       
  1370     apply (auto simp:readys_def)
       
  1371     apply(simp add:s_waiting_def[folded wq_def])
       
  1372     apply (erule_tac x = csa in allE)
       
  1373     apply (simp add:s_waiting_def wq_def Let_def split:if_splits)
       
  1374     apply (case_tac "csa = cs", simp)
       
  1375     apply (erule_tac x = cs in allE)
       
  1376     apply(auto simp add: s_waiting_def[folded wq_def] Let_def split: list.splits)
       
  1377     apply(auto simp add: wq_def)
       
  1378     apply (auto simp:s_waiting_def wq_def Let_def split:list.splits)
       
  1379     proof -
       
  1380        assume th_nin: "th \<notin> set rest"
       
  1381         and th_in: "th \<in> set (SOME q. distinct q \<and> set q = set rest)"
       
  1382         and eq_wq: "wq_fun (schs s) cs = thread # rest"
       
  1383       have "set (SOME q. distinct q \<and> set q = set rest) = set rest"
       
  1384       proof(rule someI2)
       
  1385         from wq_distinct[of cs, unfolded wq_def] and eq_wq[unfolded wq_def]
       
  1386         show "distinct rest \<and> set rest = set rest" by auto
       
  1387       next
       
  1388         show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
       
  1389       qed
       
  1390       with th_nin th_in show False by auto
       
  1391     qed
       
  1392 qed
       
  1393 
       
  1394 text {* \noindent
       
  1395   The following lemmas shows that: starting from any node in @{text "RAG"}, 
       
  1396   by chasing out-going edges, it is always possible to reach a node representing a ready
       
  1397   thread. In this lemma, it is the @{text "th'"}.
       
  1398 *}
       
  1399 
       
  1400 lemma chain_building:
       
  1401   shows "node \<in> Domain (RAG s) \<longrightarrow> (\<exists> th'. th' \<in> readys s \<and> (node, Th th') \<in> (RAG s)^+)"
       
  1402 proof -
       
  1403   from wf_dep_converse
       
  1404   have h: "wf ((RAG s)\<inverse>)" .
       
  1405   show ?thesis
       
  1406   proof(induct rule:wf_induct [OF h])
       
  1407     fix x
       
  1408     assume ih [rule_format]: 
       
  1409       "\<forall>y. (y, x) \<in> (RAG s)\<inverse> \<longrightarrow> 
       
  1410            y \<in> Domain (RAG s) \<longrightarrow> (\<exists>th'. th' \<in> readys s \<and> (y, Th th') \<in> (RAG s)\<^sup>+)"
       
  1411     show "x \<in> Domain (RAG s) \<longrightarrow> (\<exists>th'. th' \<in> readys s \<and> (x, Th th') \<in> (RAG s)\<^sup>+)"
       
  1412     proof
       
  1413       assume x_d: "x \<in> Domain (RAG s)"
       
  1414       show "\<exists>th'. th' \<in> readys s \<and> (x, Th th') \<in> (RAG s)\<^sup>+"
       
  1415       proof(cases x)
       
  1416         case (Th th)
       
  1417         from x_d Th obtain cs where x_in: "(Th th, Cs cs) \<in> RAG s" by (auto simp:s_RAG_def)
       
  1418         with Th have x_in_r: "(Cs cs, x) \<in> (RAG s)^-1" by simp
       
  1419         from th_chasing [OF x_in] obtain th' where "(Cs cs, Th th') \<in> RAG s" by blast
       
  1420         hence "Cs cs \<in> Domain (RAG s)" by auto
       
  1421         from ih [OF x_in_r this] obtain th'
       
  1422           where th'_ready: " th' \<in> readys s" and cs_in: "(Cs cs, Th th') \<in> (RAG s)\<^sup>+" by auto
       
  1423         have "(x, Th th') \<in> (RAG s)\<^sup>+" using Th x_in cs_in by auto
       
  1424         with th'_ready show ?thesis by auto
       
  1425       next
       
  1426         case (Cs cs)
       
  1427         from x_d Cs obtain th' where th'_d: "(Th th', x) \<in> (RAG s)^-1" by (auto simp:s_RAG_def)
       
  1428         show ?thesis
       
  1429         proof(cases "th' \<in> readys s")
       
  1430           case True
       
  1431           from True and th'_d show ?thesis by auto
       
  1432         next
       
  1433           case False
       
  1434           from th'_d and range_in  have "th' \<in> threads s" by auto
       
  1435           with False have "Th th' \<in> Domain (RAG s)" 
       
  1436             by (auto simp:readys_def wq_def s_waiting_def s_RAG_def cs_waiting_def Domain_def)
       
  1437           from ih [OF th'_d this]
       
  1438           obtain th'' where 
       
  1439             th''_r: "th'' \<in> readys s" and 
       
  1440             th''_in: "(Th th', Th th'') \<in> (RAG s)\<^sup>+" by auto
       
  1441           from th'_d and th''_in 
       
  1442           have "(x, Th th'') \<in> (RAG s)\<^sup>+" by auto
       
  1443           with th''_r show ?thesis by auto
       
  1444         qed
       
  1445       qed
       
  1446     qed
       
  1447   qed
       
  1448 qed
       
  1449 
       
  1450 text {* \noindent
       
  1451   The following is just an instance of @{text "chain_building"}.
       
  1452 *}
       
  1453 lemma th_chain_to_ready:
       
  1454   assumes th_in: "th \<in> threads s"
       
  1455   shows "th \<in> readys s \<or> (\<exists> th'. th' \<in> readys s \<and> (Th th, Th th') \<in> (RAG s)^+)"
       
  1456 proof(cases "th \<in> readys s")
       
  1457   case True
       
  1458   thus ?thesis by auto
       
  1459 next
       
  1460   case False
       
  1461   from False and th_in have "Th th \<in> Domain (RAG s)" 
       
  1462     by (auto simp:readys_def s_waiting_def s_RAG_def wq_def cs_waiting_def Domain_def)
       
  1463   from chain_building [rule_format, OF this]
       
  1464   show ?thesis by auto
       
  1465 qed
       
  1466 
       
  1467 end
       
  1468 
       
  1469 lemma waiting_eq: "waiting s th cs = waiting (wq s) th cs"
       
  1470   by  (unfold s_waiting_def cs_waiting_def wq_def, auto)
       
  1471 
       
  1472 lemma holding_eq: "holding (s::state) th cs = holding (wq s) th cs"
       
  1473   by (unfold s_holding_def wq_def cs_holding_def, simp)
       
  1474 
       
  1475 lemma holding_unique: "\<lbrakk>holding (s::state) th1 cs; holding s th2 cs\<rbrakk> \<Longrightarrow> th1 = th2"
       
  1476   by (unfold s_holding_def cs_holding_def, auto)
       
  1477 
       
  1478 context valid_trace
       
  1479 begin
       
  1480 
       
  1481 lemma unique_RAG: "\<lbrakk>(n, n1) \<in> RAG s; (n, n2) \<in> RAG s\<rbrakk> \<Longrightarrow> n1 = n2"
       
  1482   apply(unfold s_RAG_def, auto, fold waiting_eq holding_eq)
       
  1483   by(auto elim:waiting_unique holding_unique)
       
  1484 
       
  1485 end
       
  1486 
       
  1487 
       
  1488 lemma trancl_split: "(a, b) \<in> r^+ \<Longrightarrow> \<exists> c. (a, c) \<in> r"
       
  1489 by (induct rule:trancl_induct, auto)
       
  1490 
       
  1491 context valid_trace
       
  1492 begin
       
  1493 
       
  1494 lemma dchain_unique:
       
  1495   assumes th1_d: "(n, Th th1) \<in> (RAG s)^+"
       
  1496   and th1_r: "th1 \<in> readys s"
       
  1497   and th2_d: "(n, Th th2) \<in> (RAG s)^+"
       
  1498   and th2_r: "th2 \<in> readys s"
       
  1499   shows "th1 = th2"
       
  1500 proof -
       
  1501   { assume neq: "th1 \<noteq> th2"
       
  1502     hence "Th th1 \<noteq> Th th2" by simp
       
  1503     from unique_chain [OF _ th1_d th2_d this] and unique_RAG 
       
  1504     have "(Th th1, Th th2) \<in> (RAG s)\<^sup>+ \<or> (Th th2, Th th1) \<in> (RAG s)\<^sup>+" by auto
       
  1505     hence "False"
       
  1506     proof
       
  1507       assume "(Th th1, Th th2) \<in> (RAG s)\<^sup>+"
       
  1508       from trancl_split [OF this]
       
  1509       obtain n where dd: "(Th th1, n) \<in> RAG s" by auto
       
  1510       then obtain cs where eq_n: "n = Cs cs"
       
  1511         by (auto simp:s_RAG_def s_holding_def cs_holding_def cs_waiting_def wq_def dest:hd_np_in)
       
  1512       from dd eq_n have "th1 \<notin> readys s"
       
  1513         by (auto simp:readys_def s_RAG_def wq_def s_waiting_def cs_waiting_def)
       
  1514       with th1_r show ?thesis by auto
       
  1515     next
       
  1516       assume "(Th th2, Th th1) \<in> (RAG s)\<^sup>+"
       
  1517       from trancl_split [OF this]
       
  1518       obtain n where dd: "(Th th2, n) \<in> RAG s" by auto
       
  1519       then obtain cs where eq_n: "n = Cs cs"
       
  1520         by (auto simp:s_RAG_def s_holding_def cs_holding_def cs_waiting_def wq_def dest:hd_np_in)
       
  1521       from dd eq_n have "th2 \<notin> readys s"
       
  1522         by (auto simp:readys_def wq_def s_RAG_def s_waiting_def cs_waiting_def)
       
  1523       with th2_r show ?thesis by auto
       
  1524     qed
       
  1525   } thus ?thesis by auto
       
  1526 qed
       
  1527 
       
  1528 end
       
  1529              
       
  1530 
       
  1531 lemma step_holdents_p_add:
       
  1532   assumes vt: "vt (P th cs#s)"
       
  1533   and "wq s cs = []"
       
  1534   shows "holdents (P th cs#s) th = holdents s th \<union> {cs}"
       
  1535 proof -
       
  1536   from assms show ?thesis
       
  1537   unfolding  holdents_test step_RAG_p[OF vt] by (auto)
       
  1538 qed
       
  1539 
       
  1540 lemma step_holdents_p_eq:
       
  1541   assumes vt: "vt (P th cs#s)"
       
  1542   and "wq s cs \<noteq> []"
       
  1543   shows "holdents (P th cs#s) th = holdents s th"
       
  1544 proof -
       
  1545   from assms show ?thesis
       
  1546   unfolding  holdents_test step_RAG_p[OF vt] by auto
       
  1547 qed
       
  1548 
       
  1549 
       
  1550 lemma (in valid_trace) finite_holding :
       
  1551   shows "finite (holdents s th)"
       
  1552 proof -
       
  1553   let ?F = "\<lambda> (x, y). the_cs x"
       
  1554   from finite_RAG 
       
  1555   have "finite (RAG s)" .
       
  1556   hence "finite (?F `(RAG s))" by simp
       
  1557   moreover have "{cs . (Cs cs, Th th) \<in> RAG s} \<subseteq> \<dots>" 
       
  1558   proof -
       
  1559     { have h: "\<And> a A f. a \<in> A \<Longrightarrow> f a \<in> f ` A" by auto
       
  1560       fix x assume "(Cs x, Th th) \<in> RAG s"
       
  1561       hence "?F (Cs x, Th th) \<in> ?F `(RAG s)" by (rule h)
       
  1562       moreover have "?F (Cs x, Th th) = x" by simp
       
  1563       ultimately have "x \<in> (\<lambda>(x, y). the_cs x) ` RAG s" by simp 
       
  1564     } thus ?thesis by auto
       
  1565   qed
       
  1566   ultimately show ?thesis by (unfold holdents_test, auto intro:finite_subset)
       
  1567 qed
       
  1568 
       
  1569 lemma cntCS_v_dec: 
       
  1570   assumes vtv: "vt (V thread cs#s)"
       
  1571   shows "(cntCS (V thread cs#s) thread + 1) = cntCS s thread"
       
  1572 proof -
       
  1573   from vtv interpret vt_s: valid_trace s
       
  1574     by (cases, unfold_locales, simp)
       
  1575   from vtv interpret vt_v: valid_trace "V thread cs#s"
       
  1576      by (unfold_locales, simp)
       
  1577   from step_back_step[OF vtv]
       
  1578   have cs_in: "cs \<in> holdents s thread" 
       
  1579     apply (cases, unfold holdents_test s_RAG_def, simp)
       
  1580     by (unfold cs_holding_def s_holding_def wq_def, auto)
       
  1581   moreover have cs_not_in: 
       
  1582     "(holdents (V thread cs#s) thread) = holdents s thread - {cs}"
       
  1583     apply (insert vt_s.wq_distinct[of cs])
       
  1584     apply (unfold holdents_test, unfold step_RAG_v[OF vtv],
       
  1585             auto simp:next_th_def)
       
  1586   proof -
       
  1587     fix rest
       
  1588     assume dst: "distinct (rest::thread list)"
       
  1589       and ne: "rest \<noteq> []"
       
  1590     and hd_ni: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> set rest"
       
  1591     moreover have "set (SOME q. distinct q \<and> set q = set rest) = set rest"
       
  1592     proof(rule someI2)
       
  1593       from dst show "distinct rest \<and> set rest = set rest" by auto
       
  1594     next
       
  1595       show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
       
  1596     qed
       
  1597     ultimately have "hd (SOME q. distinct q \<and> set q = set rest) \<notin> 
       
  1598                      set (SOME q. distinct q \<and> set q = set rest)" by simp
       
  1599     moreover have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
       
  1600     proof(rule someI2)
       
  1601       from dst show "distinct rest \<and> set rest = set rest" by auto
       
  1602     next
       
  1603       fix x assume " distinct x \<and> set x = set rest" with ne
       
  1604       show "x \<noteq> []" by auto
       
  1605     qed
       
  1606     ultimately 
       
  1607     show "(Cs cs, Th (hd (SOME q. distinct q \<and> set q = set rest))) \<in> RAG s"
       
  1608       by auto
       
  1609   next
       
  1610     fix rest
       
  1611     assume dst: "distinct (rest::thread list)"
       
  1612       and ne: "rest \<noteq> []"
       
  1613     and hd_ni: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> set rest"
       
  1614     moreover have "set (SOME q. distinct q \<and> set q = set rest) = set rest"
       
  1615     proof(rule someI2)
       
  1616       from dst show "distinct rest \<and> set rest = set rest" by auto
       
  1617     next
       
  1618       show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
       
  1619     qed
       
  1620     ultimately have "hd (SOME q. distinct q \<and> set q = set rest) \<notin> 
       
  1621                      set (SOME q. distinct q \<and> set q = set rest)" by simp
       
  1622     moreover have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
       
  1623     proof(rule someI2)
       
  1624       from dst show "distinct rest \<and> set rest = set rest" by auto
       
  1625     next
       
  1626       fix x assume " distinct x \<and> set x = set rest" with ne
       
  1627       show "x \<noteq> []" by auto
       
  1628     qed
       
  1629     ultimately show "False" by auto 
       
  1630   qed
       
  1631   ultimately 
       
  1632   have "holdents s thread = insert cs (holdents (V thread cs#s) thread)"
       
  1633     by auto
       
  1634   moreover have "card \<dots> = 
       
  1635                     Suc (card ((holdents (V thread cs#s) thread) - {cs}))"
       
  1636   proof(rule card_insert)
       
  1637     from vt_v.finite_holding
       
  1638     show " finite (holdents (V thread cs # s) thread)" .
       
  1639   qed
       
  1640   moreover from cs_not_in 
       
  1641   have "cs \<notin> (holdents (V thread cs#s) thread)" by auto
       
  1642   ultimately show ?thesis by (simp add:cntCS_def)
       
  1643 qed 
       
  1644 
       
  1645 lemma count_rec1 [simp]: 
       
  1646   assumes "Q e"
       
  1647   shows "count Q (e#es) = Suc (count Q es)"
       
  1648   using assms
       
  1649   by (unfold count_def, auto)
       
  1650 
       
  1651 lemma count_rec2 [simp]: 
       
  1652   assumes "\<not>Q e"
       
  1653   shows "count Q (e#es) = (count Q es)"
       
  1654   using assms
       
  1655   by (unfold count_def, auto)
       
  1656 
       
  1657 lemma count_rec3 [simp]: 
       
  1658   shows "count Q [] =  0"
       
  1659   by (unfold count_def, auto)
       
  1660   
       
  1661 lemma cntP_diff_inv:
       
  1662   assumes "cntP (e#s) th \<noteq> cntP s th"
       
  1663   shows "isP e \<and> actor e = th"
       
  1664 proof(cases e)
       
  1665   case (P th' pty)
       
  1666   show ?thesis
       
  1667   by (cases "(\<lambda>e. \<exists>cs. e = P th cs) (P th' pty)", 
       
  1668         insert assms P, auto simp:cntP_def)
       
  1669 qed (insert assms, auto simp:cntP_def)
       
  1670 
       
  1671 lemma isP_E:
       
  1672   assumes "isP e"
       
  1673   obtains cs where "e = P (actor e) cs"
       
  1674   using assms by (cases e, auto)
       
  1675 
       
  1676 lemma isV_E:
       
  1677   assumes "isV e"
       
  1678   obtains cs where "e = V (actor e) cs"
       
  1679   using assms by (cases e, auto) (* ccc *)
       
  1680 
       
  1681 lemma cntV_diff_inv:
       
  1682   assumes "cntV (e#s) th \<noteq> cntV s th"
       
  1683   shows "isV e \<and> actor e = th"
       
  1684 proof(cases e)
       
  1685   case (V th' pty)
       
  1686   show ?thesis
       
  1687   by (cases "(\<lambda>e. \<exists>cs. e = V th cs) (V th' pty)", 
       
  1688         insert assms V, auto simp:cntV_def)
       
  1689 qed (insert assms, auto simp:cntV_def)
       
  1690 
       
  1691 context valid_trace
       
  1692 begin
       
  1693 
       
  1694 text {* (* ddd *) \noindent
       
  1695   The relationship between @{text "cntP"}, @{text "cntV"} and @{text "cntCS"} 
       
  1696   of one particular thread. 
       
  1697 *} 
       
  1698 
       
  1699 lemma cnp_cnv_cncs:
       
  1700   shows "cntP s th = cntV s th + (if (th \<in> readys s \<or> th \<notin> threads s) 
       
  1701                                        then cntCS s th else cntCS s th + 1)"
       
  1702 proof -
       
  1703   from vt show ?thesis
       
  1704   proof(induct arbitrary:th)
       
  1705     case (vt_cons s e)
       
  1706     interpret vt_s: valid_trace s using vt_cons(1) by (unfold_locales, simp)
       
  1707     assume vt: "vt s"
       
  1708     and ih: "\<And>th. cntP s th  = cntV s th +
       
  1709                (if (th \<in> readys s \<or> th \<notin> threads s) then cntCS s th else cntCS s th + 1)"
       
  1710     and stp: "step s e"
       
  1711     from stp show ?case
       
  1712     proof(cases)
       
  1713       case (thread_create thread prio)
       
  1714       assume eq_e: "e = Create thread prio"
       
  1715         and not_in: "thread \<notin> threads s"
       
  1716       show ?thesis
       
  1717       proof -
       
  1718         { fix cs 
       
  1719           assume "thread \<in> set (wq s cs)"
       
  1720           from vt_s.wq_threads [OF this] have "thread \<in> threads s" .
       
  1721           with not_in have "False" by simp
       
  1722         } with eq_e have eq_readys: "readys (e#s) = readys s \<union> {thread}"
       
  1723           by (auto simp:readys_def threads.simps s_waiting_def 
       
  1724             wq_def cs_waiting_def Let_def)
       
  1725         from eq_e have eq_cnp: "cntP (e#s) th = cntP s th" by (simp add:cntP_def count_def)
       
  1726         from eq_e have eq_cnv: "cntV (e#s) th = cntV s th" by (simp add:cntV_def count_def)
       
  1727         have eq_cncs: "cntCS (e#s) th = cntCS s th"
       
  1728           unfolding cntCS_def holdents_test
       
  1729           by (simp add:RAG_create_unchanged eq_e)
       
  1730         { assume "th \<noteq> thread"
       
  1731           with eq_readys eq_e
       
  1732           have "(th \<in> readys (e # s) \<or> th \<notin> threads (e # s)) = 
       
  1733                       (th \<in> readys (s) \<or> th \<notin> threads (s))" 
       
  1734             by (simp add:threads.simps)
       
  1735           with eq_cnp eq_cnv eq_cncs ih not_in
       
  1736           have ?thesis by simp
       
  1737         } moreover {
       
  1738           assume eq_th: "th = thread"
       
  1739           with not_in ih have " cntP s th  = cntV s th + cntCS s th" by simp
       
  1740           moreover from eq_th and eq_readys have "th \<in> readys (e#s)" by simp
       
  1741           moreover note eq_cnp eq_cnv eq_cncs
       
  1742           ultimately have ?thesis by auto
       
  1743         } ultimately show ?thesis by blast
       
  1744       qed
       
  1745     next
       
  1746       case (thread_exit thread)
       
  1747       assume eq_e: "e = Exit thread" 
       
  1748       and is_runing: "thread \<in> runing s"
       
  1749       and no_hold: "holdents s thread = {}"
       
  1750       from eq_e have eq_cnp: "cntP (e#s) th = cntP s th" by (simp add:cntP_def count_def)
       
  1751       from eq_e have eq_cnv: "cntV (e#s) th = cntV s th" by (simp add:cntV_def count_def)
       
  1752       have eq_cncs: "cntCS (e#s) th = cntCS s th"
       
  1753         unfolding cntCS_def holdents_test
       
  1754         by (simp add:RAG_exit_unchanged eq_e)
       
  1755       { assume "th \<noteq> thread"
       
  1756         with eq_e
       
  1757         have "(th \<in> readys (e # s) \<or> th \<notin> threads (e # s)) = 
       
  1758           (th \<in> readys (s) \<or> th \<notin> threads (s))" 
       
  1759           apply (simp add:threads.simps readys_def)
       
  1760           apply (subst s_waiting_def)
       
  1761           apply (simp add:Let_def)
       
  1762           apply (subst s_waiting_def, simp)
       
  1763           done
       
  1764         with eq_cnp eq_cnv eq_cncs ih
       
  1765         have ?thesis by simp
       
  1766       } moreover {
       
  1767         assume eq_th: "th = thread"
       
  1768         with ih is_runing have " cntP s th = cntV s th + cntCS s th" 
       
  1769           by (simp add:runing_def)
       
  1770         moreover from eq_th eq_e have "th \<notin> threads (e#s)"
       
  1771           by simp
       
  1772         moreover note eq_cnp eq_cnv eq_cncs
       
  1773         ultimately have ?thesis by auto
       
  1774       } ultimately show ?thesis by blast
       
  1775     next
       
  1776       case (thread_P thread cs)
       
  1777       assume eq_e: "e = P thread cs"
       
  1778         and is_runing: "thread \<in> runing s"
       
  1779         and no_dep: "(Cs cs, Th thread) \<notin> (RAG s)\<^sup>+"
       
  1780       from thread_P vt stp ih  have vtp: "vt (P thread cs#s)" by auto
       
  1781       then interpret vt_p: valid_trace "(P thread cs#s)"
       
  1782         by (unfold_locales, simp)
       
  1783       show ?thesis 
       
  1784       proof -
       
  1785         { have hh: "\<And> A B C. (B = C) \<Longrightarrow> (A \<and> B) = (A \<and> C)" by blast
       
  1786           assume neq_th: "th \<noteq> thread"
       
  1787           with eq_e
       
  1788           have eq_readys: "(th \<in> readys (e#s)) = (th \<in> readys (s))"
       
  1789             apply (simp add:readys_def s_waiting_def wq_def Let_def)
       
  1790             apply (rule_tac hh)
       
  1791              apply (intro iffI allI, clarify)
       
  1792             apply (erule_tac x = csa in allE, auto)
       
  1793             apply (subgoal_tac "wq_fun (schs s) cs \<noteq> []", auto)
       
  1794             apply (erule_tac x = cs in allE, auto)
       
  1795             by (case_tac "(wq_fun (schs s) cs)", auto)
       
  1796           moreover from neq_th eq_e have "cntCS (e # s) th = cntCS s th"
       
  1797             apply (simp add:cntCS_def holdents_test)
       
  1798             by (unfold  step_RAG_p [OF vtp], auto)
       
  1799           moreover from eq_e neq_th have "cntP (e # s) th = cntP s th"
       
  1800             by (simp add:cntP_def count_def)
       
  1801           moreover from eq_e neq_th have "cntV (e#s) th = cntV s th"
       
  1802             by (simp add:cntV_def count_def)
       
  1803           moreover from eq_e neq_th have "threads (e#s) = threads s" by simp
       
  1804           moreover note ih [of th] 
       
  1805           ultimately have ?thesis by simp
       
  1806         } moreover {
       
  1807           assume eq_th: "th = thread"
       
  1808           have ?thesis
       
  1809           proof -
       
  1810             from eq_e eq_th have eq_cnp: "cntP (e # s) th  = 1 + (cntP s th)" 
       
  1811               by (simp add:cntP_def count_def)
       
  1812             from eq_e eq_th have eq_cnv: "cntV (e#s) th = cntV s th"
       
  1813               by (simp add:cntV_def count_def)
       
  1814             show ?thesis
       
  1815             proof (cases "wq s cs = []")
       
  1816               case True
       
  1817               with is_runing
       
  1818               have "th \<in> readys (e#s)"
       
  1819                 apply (unfold eq_e wq_def, unfold readys_def s_RAG_def)
       
  1820                 apply (simp add: wq_def[symmetric] runing_def eq_th s_waiting_def)
       
  1821                 by (auto simp:readys_def wq_def Let_def s_waiting_def wq_def)
       
  1822               moreover have "cntCS (e # s) th = 1 + cntCS s th"
       
  1823               proof -
       
  1824                 have "card {csa. csa = cs \<or> (Cs csa, Th thread) \<in> RAG s} =
       
  1825                   Suc (card {cs. (Cs cs, Th thread) \<in> RAG s})" (is "card ?L = Suc (card ?R)")
       
  1826                 proof -
       
  1827                   have "?L = insert cs ?R" by auto
       
  1828                   moreover have "card \<dots> = Suc (card (?R - {cs}))" 
       
  1829                   proof(rule card_insert)
       
  1830                     from vt_s.finite_holding [of thread]
       
  1831                     show " finite {cs. (Cs cs, Th thread) \<in> RAG s}"
       
  1832                       by (unfold holdents_test, simp)
       
  1833                   qed
       
  1834                   moreover have "?R - {cs} = ?R"
       
  1835                   proof -
       
  1836                     have "cs \<notin> ?R"
       
  1837                     proof
       
  1838                       assume "cs \<in> {cs. (Cs cs, Th thread) \<in> RAG s}"
       
  1839                       with no_dep show False by auto
       
  1840                     qed
       
  1841                     thus ?thesis by auto
       
  1842                   qed
       
  1843                   ultimately show ?thesis by auto
       
  1844                 qed
       
  1845                 thus ?thesis
       
  1846                   apply (unfold eq_e eq_th cntCS_def)
       
  1847                   apply (simp add: holdents_test)
       
  1848                   by (unfold step_RAG_p [OF vtp], auto simp:True)
       
  1849               qed
       
  1850               moreover from is_runing have "th \<in> readys s"
       
  1851                 by (simp add:runing_def eq_th)
       
  1852               moreover note eq_cnp eq_cnv ih [of th]
       
  1853               ultimately show ?thesis by auto
       
  1854             next
       
  1855               case False
       
  1856               have eq_wq: "wq (e#s) cs = wq s cs @ [th]"
       
  1857                     by (unfold eq_th eq_e wq_def, auto simp:Let_def)
       
  1858               have "th \<notin> readys (e#s)"
       
  1859               proof
       
  1860                 assume "th \<in> readys (e#s)"
       
  1861                 hence "\<forall>cs. \<not> waiting (e # s) th cs" by (simp add:readys_def)
       
  1862                 from this[rule_format, of cs] have " \<not> waiting (e # s) th cs" .
       
  1863                 hence "th \<in> set (wq (e#s) cs) \<Longrightarrow> th = hd (wq (e#s) cs)" 
       
  1864                   by (simp add:s_waiting_def wq_def)
       
  1865                 moreover from eq_wq have "th \<in> set (wq (e#s) cs)" by auto
       
  1866                 ultimately have "th = hd (wq (e#s) cs)" by blast
       
  1867                 with eq_wq have "th = hd (wq s cs @ [th])" by simp
       
  1868                 hence "th = hd (wq s cs)" using False by auto
       
  1869                 with False eq_wq vt_p.wq_distinct [of cs]
       
  1870                 show False by (fold eq_e, auto)
       
  1871               qed
       
  1872               moreover from is_runing have "th \<in> threads (e#s)" 
       
  1873                 by (unfold eq_e, auto simp:runing_def readys_def eq_th)
       
  1874               moreover have "cntCS (e # s) th = cntCS s th"
       
  1875                 apply (unfold cntCS_def holdents_test eq_e step_RAG_p[OF vtp])
       
  1876                 by (auto simp:False)
       
  1877               moreover note eq_cnp eq_cnv ih[of th]
       
  1878               moreover from is_runing have "th \<in> readys s"
       
  1879                 by (simp add:runing_def eq_th)
       
  1880               ultimately show ?thesis by auto
       
  1881             qed
       
  1882           qed
       
  1883         } ultimately show ?thesis by blast
       
  1884       qed
       
  1885     next
       
  1886       case (thread_V thread cs)
       
  1887       from assms vt stp ih thread_V have vtv: "vt (V thread cs # s)" by auto
       
  1888       then interpret vt_v: valid_trace "(V thread cs # s)" by (unfold_locales, simp)
       
  1889       assume eq_e: "e = V thread cs"
       
  1890         and is_runing: "thread \<in> runing s"
       
  1891         and hold: "holding s thread cs"
       
  1892       from hold obtain rest 
       
  1893         where eq_wq: "wq s cs = thread # rest"
       
  1894         by (case_tac "wq s cs", auto simp: wq_def s_holding_def)
       
  1895       have eq_threads: "threads (e#s) = threads s" by (simp add: eq_e)
       
  1896       have eq_set: "set (SOME q. distinct q \<and> set q = set rest) = set rest"
       
  1897       proof(rule someI2)
       
  1898         from vt_v.wq_distinct[of cs] and eq_wq
       
  1899         show "distinct rest \<and> set rest = set rest"
       
  1900           by (metis distinct.simps(2) vt_s.wq_distinct)
       
  1901       next
       
  1902         show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest"
       
  1903           by auto
       
  1904       qed
       
  1905       show ?thesis
       
  1906       proof -
       
  1907         { assume eq_th: "th = thread"
       
  1908           from eq_th have eq_cnp: "cntP (e # s) th = cntP s th"
       
  1909             by (unfold eq_e, simp add:cntP_def count_def)
       
  1910           moreover from eq_th have eq_cnv: "cntV (e#s) th = 1 + cntV s th"
       
  1911             by (unfold eq_e, simp add:cntV_def count_def)
       
  1912           moreover from cntCS_v_dec [OF vtv] 
       
  1913           have "cntCS (e # s) thread + 1 = cntCS s thread"
       
  1914             by (simp add:eq_e)
       
  1915           moreover from is_runing have rd_before: "thread \<in> readys s"
       
  1916             by (unfold runing_def, simp)
       
  1917           moreover have "thread \<in> readys (e # s)"
       
  1918           proof -
       
  1919             from is_runing
       
  1920             have "thread \<in> threads (e#s)" 
       
  1921               by (unfold eq_e, auto simp:runing_def readys_def)
       
  1922             moreover have "\<forall> cs1. \<not> waiting (e#s) thread cs1"
       
  1923             proof
       
  1924               fix cs1
       
  1925               { assume eq_cs: "cs1 = cs" 
       
  1926                 have "\<not> waiting (e # s) thread cs1"
       
  1927                 proof -
       
  1928                   from eq_wq
       
  1929                   have "thread \<notin> set (wq (e#s) cs1)"
       
  1930                     apply(unfold eq_e wq_def eq_cs s_holding_def)
       
  1931                     apply (auto simp:Let_def)
       
  1932                   proof -
       
  1933                     assume "thread \<in> set (SOME q. distinct q \<and> set q = set rest)"
       
  1934                     with eq_set have "thread \<in> set rest" by simp
       
  1935                     with vt_v.wq_distinct[of cs]
       
  1936                     and eq_wq show False
       
  1937                         by (metis distinct.simps(2) vt_s.wq_distinct)
       
  1938                   qed
       
  1939                   thus ?thesis by (simp add:wq_def s_waiting_def)
       
  1940                 qed
       
  1941               } moreover {
       
  1942                 assume neq_cs: "cs1 \<noteq> cs"
       
  1943                   have "\<not> waiting (e # s) thread cs1" 
       
  1944                   proof -
       
  1945                     from wq_v_neq [OF neq_cs[symmetric]]
       
  1946                     have "wq (V thread cs # s) cs1 = wq s cs1" .
       
  1947                     moreover have "\<not> waiting s thread cs1" 
       
  1948                     proof -
       
  1949                       from runing_ready and is_runing
       
  1950                       have "thread \<in> readys s" by auto
       
  1951                       thus ?thesis by (simp add:readys_def)
       
  1952                     qed
       
  1953                     ultimately show ?thesis 
       
  1954                       by (auto simp:wq_def s_waiting_def eq_e)
       
  1955                   qed
       
  1956               } ultimately show "\<not> waiting (e # s) thread cs1" by blast
       
  1957             qed
       
  1958             ultimately show ?thesis by (simp add:readys_def)
       
  1959           qed
       
  1960           moreover note eq_th ih
       
  1961           ultimately have ?thesis by auto
       
  1962         } moreover {
       
  1963           assume neq_th: "th \<noteq> thread"
       
  1964           from neq_th eq_e have eq_cnp: "cntP (e # s) th = cntP s th" 
       
  1965             by (simp add:cntP_def count_def)
       
  1966           from neq_th eq_e have eq_cnv: "cntV (e # s) th = cntV s th" 
       
  1967             by (simp add:cntV_def count_def)
       
  1968           have ?thesis
       
  1969           proof(cases "th \<in> set rest")
       
  1970             case False
       
  1971             have "(th \<in> readys (e # s)) = (th \<in> readys s)"
       
  1972               apply (insert step_back_vt[OF vtv])
       
  1973               by (simp add: False eq_e eq_wq neq_th vt_s.readys_v_eq)
       
  1974             moreover have "cntCS (e#s) th = cntCS s th"
       
  1975               apply (insert neq_th, unfold eq_e cntCS_def holdents_test step_RAG_v[OF vtv], auto)
       
  1976               proof -
       
  1977                 have "{csa. (Cs csa, Th th) \<in> RAG s \<or> csa = cs \<and> next_th s thread cs th} =
       
  1978                       {cs. (Cs cs, Th th) \<in> RAG s}"
       
  1979                 proof -
       
  1980                   from False eq_wq
       
  1981                   have " next_th s thread cs th \<Longrightarrow> (Cs cs, Th th) \<in> RAG s"
       
  1982                     apply (unfold next_th_def, auto)
       
  1983                   proof -
       
  1984                     assume ne: "rest \<noteq> []"
       
  1985                       and ni: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> set rest"
       
  1986                       and eq_wq: "wq s cs = thread # rest"
       
  1987                     from eq_set ni have "hd (SOME q. distinct q \<and> set q = set rest) \<notin> 
       
  1988                                   set (SOME q. distinct q \<and> set q = set rest)
       
  1989                                   " by simp
       
  1990                     moreover have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
       
  1991                     proof(rule someI2)
       
  1992                       from vt_s.wq_distinct[ of cs] and eq_wq
       
  1993                       show "distinct rest \<and> set rest = set rest" by auto
       
  1994                     next
       
  1995                       fix x assume "distinct x \<and> set x = set rest"
       
  1996                       with ne show "x \<noteq> []" by auto
       
  1997                     qed
       
  1998                     ultimately show 
       
  1999                       "(Cs cs, Th (hd (SOME q. distinct q \<and> set q = set rest))) \<in> RAG s"
       
  2000                       by auto
       
  2001                   qed    
       
  2002                   thus ?thesis by auto
       
  2003                 qed
       
  2004                 thus "card {csa. (Cs csa, Th th) \<in> RAG s \<or> csa = cs \<and> next_th s thread cs th} =
       
  2005                              card {cs. (Cs cs, Th th) \<in> RAG s}" by simp 
       
  2006               qed
       
  2007             moreover note ih eq_cnp eq_cnv eq_threads
       
  2008             ultimately show ?thesis by auto
       
  2009           next
       
  2010             case True
       
  2011             assume th_in: "th \<in> set rest"
       
  2012             show ?thesis
       
  2013             proof(cases "next_th s thread cs th")
       
  2014               case False
       
  2015               with eq_wq and th_in have 
       
  2016                 neq_hd: "th \<noteq> hd (SOME q. distinct q \<and> set q = set rest)" (is "th \<noteq> hd ?rest")
       
  2017                 by (auto simp:next_th_def)
       
  2018               have "(th \<in> readys (e # s)) = (th \<in> readys s)"
       
  2019               proof -
       
  2020                 from eq_wq and th_in
       
  2021                 have "\<not> th \<in> readys s"
       
  2022                   apply (auto simp:readys_def s_waiting_def)
       
  2023                   apply (rule_tac x = cs in exI, auto)
       
  2024                   by (insert vt_s.wq_distinct[of cs], auto simp add: wq_def)
       
  2025                 moreover 
       
  2026                 from eq_wq and th_in and neq_hd
       
  2027                 have "\<not> (th \<in> readys (e # s))"
       
  2028                   apply (auto simp:readys_def s_waiting_def eq_e wq_def Let_def split:list.splits)
       
  2029                   by (rule_tac x = cs in exI, auto simp:eq_set)
       
  2030                 ultimately show ?thesis by auto
       
  2031               qed
       
  2032               moreover have "cntCS (e#s) th = cntCS s th" 
       
  2033               proof -
       
  2034                 from eq_wq and  th_in and neq_hd
       
  2035                 have "(holdents (e # s) th) = (holdents s th)"
       
  2036                   apply (unfold eq_e step_RAG_v[OF vtv], 
       
  2037                          auto simp:next_th_def eq_set s_RAG_def holdents_test wq_def
       
  2038                                    Let_def cs_holding_def)
       
  2039                   by (insert vt_s.wq_distinct[of cs], auto simp:wq_def)
       
  2040                 thus ?thesis by (simp add:cntCS_def)
       
  2041               qed
       
  2042               moreover note ih eq_cnp eq_cnv eq_threads
       
  2043               ultimately show ?thesis by auto
       
  2044             next
       
  2045               case True
       
  2046               let ?rest = " (SOME q. distinct q \<and> set q = set rest)"
       
  2047               let ?t = "hd ?rest"
       
  2048               from True eq_wq th_in neq_th
       
  2049               have "th \<in> readys (e # s)"
       
  2050                 apply (auto simp:eq_e readys_def s_waiting_def wq_def
       
  2051                         Let_def next_th_def)
       
  2052               proof -
       
  2053                 assume eq_wq: "wq_fun (schs s) cs = thread # rest"
       
  2054                   and t_in: "?t \<in> set rest"
       
  2055                 show "?t \<in> threads s"
       
  2056                 proof(rule vt_s.wq_threads)
       
  2057                   from eq_wq and t_in
       
  2058                   show "?t \<in> set (wq s cs)" by (auto simp:wq_def)
       
  2059                 qed
       
  2060               next
       
  2061                 fix csa
       
  2062                 assume eq_wq: "wq_fun (schs s) cs = thread # rest"
       
  2063                   and t_in: "?t \<in> set rest"
       
  2064                   and neq_cs: "csa \<noteq> cs"
       
  2065                   and t_in': "?t \<in>  set (wq_fun (schs s) csa)"
       
  2066                 show "?t = hd (wq_fun (schs s) csa)"
       
  2067                 proof -
       
  2068                   { assume neq_hd': "?t \<noteq> hd (wq_fun (schs s) csa)"
       
  2069                     from vt_s.wq_distinct[of cs] and 
       
  2070                     eq_wq[folded wq_def] and t_in eq_wq
       
  2071                     have "?t \<noteq> thread" by auto
       
  2072                     with eq_wq and t_in
       
  2073                     have w1: "waiting s ?t cs"
       
  2074                       by (auto simp:s_waiting_def wq_def)
       
  2075                     from t_in' neq_hd'
       
  2076                     have w2: "waiting s ?t csa"
       
  2077                       by (auto simp:s_waiting_def wq_def)
       
  2078                     from vt_s.waiting_unique[OF w1 w2]
       
  2079                     and neq_cs have "False" by auto
       
  2080                   } thus ?thesis by auto
       
  2081                 qed
       
  2082               qed
       
  2083               moreover have "cntP s th = cntV s th + cntCS s th + 1"
       
  2084               proof -
       
  2085                 have "th \<notin> readys s" 
       
  2086                 proof -
       
  2087                   from True eq_wq neq_th th_in
       
  2088                   show ?thesis
       
  2089                     apply (unfold readys_def s_waiting_def, auto)
       
  2090                     by (rule_tac x = cs in exI, auto simp add: wq_def)
       
  2091                 qed
       
  2092                 moreover have "th \<in> threads s"
       
  2093                 proof -
       
  2094                   from th_in eq_wq
       
  2095                   have "th \<in> set (wq s cs)" by simp
       
  2096                   from vt_s.wq_threads [OF this] 
       
  2097                   show ?thesis .
       
  2098                 qed
       
  2099                 ultimately show ?thesis using ih by auto
       
  2100               qed
       
  2101               moreover from True neq_th have "cntCS (e # s) th = 1 + cntCS s th"
       
  2102                 apply (unfold cntCS_def holdents_test eq_e step_RAG_v[OF vtv], auto)
       
  2103               proof -
       
  2104                 show "card {csa. (Cs csa, Th th) \<in> RAG s \<or> csa = cs} =
       
  2105                                Suc (card {cs. (Cs cs, Th th) \<in> RAG s})"
       
  2106                   (is "card ?A = Suc (card ?B)")
       
  2107                 proof -
       
  2108                   have "?A = insert cs ?B" by auto
       
  2109                   hence "card ?A = card (insert cs ?B)" by simp
       
  2110                   also have "\<dots> = Suc (card ?B)"
       
  2111                   proof(rule card_insert_disjoint)
       
  2112                     have "?B \<subseteq> ((\<lambda> (x, y). the_cs x) ` RAG s)" 
       
  2113                       apply (auto simp:image_def)
       
  2114                       by (rule_tac x = "(Cs x, Th th)" in bexI, auto)
       
  2115                     with vt_s.finite_RAG
       
  2116                     show "finite {cs. (Cs cs, Th th) \<in> RAG s}" by (auto intro:finite_subset)
       
  2117                   next
       
  2118                     show "cs \<notin> {cs. (Cs cs, Th th) \<in> RAG s}"
       
  2119                     proof
       
  2120                       assume "cs \<in> {cs. (Cs cs, Th th) \<in> RAG s}"
       
  2121                       hence "(Cs cs, Th th) \<in> RAG s" by simp
       
  2122                       with True neq_th eq_wq show False
       
  2123                         by (auto simp:next_th_def s_RAG_def cs_holding_def)
       
  2124                     qed
       
  2125                   qed
       
  2126                   finally show ?thesis .
       
  2127                 qed
       
  2128               qed
       
  2129               moreover note eq_cnp eq_cnv
       
  2130               ultimately show ?thesis by simp
       
  2131             qed
       
  2132           qed
       
  2133         } ultimately show ?thesis by blast
       
  2134       qed
       
  2135     next
       
  2136       case (thread_set thread prio)
       
  2137       assume eq_e: "e = Set thread prio"
       
  2138         and is_runing: "thread \<in> runing s"
       
  2139       show ?thesis
       
  2140       proof -
       
  2141         from eq_e have eq_cnp: "cntP (e#s) th = cntP s th" by (simp add:cntP_def count_def)
       
  2142         from eq_e have eq_cnv: "cntV (e#s) th = cntV s th" by (simp add:cntV_def count_def)
       
  2143         have eq_cncs: "cntCS (e#s) th = cntCS s th"
       
  2144           unfolding cntCS_def holdents_test
       
  2145           by (simp add:RAG_set_unchanged eq_e)
       
  2146         from eq_e have eq_readys: "readys (e#s) = readys s" 
       
  2147           by (simp add:readys_def cs_waiting_def s_waiting_def wq_def,
       
  2148                   auto simp:Let_def)
       
  2149         { assume "th \<noteq> thread"
       
  2150           with eq_readys eq_e
       
  2151           have "(th \<in> readys (e # s) \<or> th \<notin> threads (e # s)) = 
       
  2152                       (th \<in> readys (s) \<or> th \<notin> threads (s))" 
       
  2153             by (simp add:threads.simps)
       
  2154           with eq_cnp eq_cnv eq_cncs ih is_runing
       
  2155           have ?thesis by simp
       
  2156         } moreover {
       
  2157           assume eq_th: "th = thread"
       
  2158           with is_runing ih have " cntP s th  = cntV s th + cntCS s th" 
       
  2159             by (unfold runing_def, auto)
       
  2160           moreover from eq_th and eq_readys is_runing have "th \<in> readys (e#s)"
       
  2161             by (simp add:runing_def)
       
  2162           moreover note eq_cnp eq_cnv eq_cncs
       
  2163           ultimately have ?thesis by auto
       
  2164         } ultimately show ?thesis by blast
       
  2165       qed   
       
  2166     qed
       
  2167   next
       
  2168     case vt_nil
       
  2169     show ?case 
       
  2170       by (unfold cntP_def cntV_def cntCS_def, 
       
  2171         auto simp:count_def holdents_test s_RAG_def wq_def cs_holding_def)
       
  2172   qed
       
  2173 qed
       
  2174 
       
  2175 lemma not_thread_cncs:
       
  2176   assumes not_in: "th \<notin> threads s" 
       
  2177   shows "cntCS s th = 0"
       
  2178 proof -
       
  2179   from vt not_in show ?thesis
       
  2180   proof(induct arbitrary:th)
       
  2181     case (vt_cons s e th)
       
  2182     interpret vt_s: valid_trace s using vt_cons(1)
       
  2183        by (unfold_locales, simp)
       
  2184     assume vt: "vt s"
       
  2185       and ih: "\<And>th. th \<notin> threads s \<Longrightarrow> cntCS s th = 0"
       
  2186       and stp: "step s e"
       
  2187       and not_in: "th \<notin> threads (e # s)"
       
  2188     from stp show ?case
       
  2189     proof(cases)
       
  2190       case (thread_create thread prio)
       
  2191       assume eq_e: "e = Create thread prio"
       
  2192         and not_in': "thread \<notin> threads s"
       
  2193       have "cntCS (e # s) th = cntCS s th"
       
  2194         apply (unfold eq_e cntCS_def holdents_test)
       
  2195         by (simp add:RAG_create_unchanged)
       
  2196       moreover have "th \<notin> threads s" 
       
  2197       proof -
       
  2198         from not_in eq_e show ?thesis by simp
       
  2199       qed
       
  2200       moreover note ih ultimately show ?thesis by auto
       
  2201     next
       
  2202       case (thread_exit thread)
       
  2203       assume eq_e: "e = Exit thread"
       
  2204       and nh: "holdents s thread = {}"
       
  2205       have eq_cns: "cntCS (e # s) th = cntCS s th"
       
  2206         apply (unfold eq_e cntCS_def holdents_test)
       
  2207         by (simp add:RAG_exit_unchanged)
       
  2208       show ?thesis
       
  2209       proof(cases "th = thread")
       
  2210         case True
       
  2211         have "cntCS s th = 0" by (unfold cntCS_def, auto simp:nh True)
       
  2212         with eq_cns show ?thesis by simp
       
  2213       next
       
  2214         case False
       
  2215         with not_in and eq_e
       
  2216         have "th \<notin> threads s" by simp
       
  2217         from ih[OF this] and eq_cns show ?thesis by simp
       
  2218       qed
       
  2219     next
       
  2220       case (thread_P thread cs)
       
  2221       assume eq_e: "e = P thread cs"
       
  2222       and is_runing: "thread \<in> runing s"
       
  2223       from assms thread_P ih vt stp thread_P have vtp: "vt (P thread cs#s)" by auto
       
  2224       have neq_th: "th \<noteq> thread" 
       
  2225       proof -
       
  2226         from not_in eq_e have "th \<notin> threads s" by simp
       
  2227         moreover from is_runing have "thread \<in> threads s"
       
  2228           by (simp add:runing_def readys_def)
       
  2229         ultimately show ?thesis by auto
       
  2230       qed
       
  2231       hence "cntCS (e # s) th  = cntCS s th "
       
  2232         apply (unfold cntCS_def holdents_test eq_e)
       
  2233         by (unfold step_RAG_p[OF vtp], auto)
       
  2234       moreover have "cntCS s th = 0"
       
  2235       proof(rule ih)
       
  2236         from not_in eq_e show "th \<notin> threads s" by simp
       
  2237       qed
       
  2238       ultimately show ?thesis by simp
       
  2239     next
       
  2240       case (thread_V thread cs)
       
  2241       assume eq_e: "e = V thread cs"
       
  2242         and is_runing: "thread \<in> runing s"
       
  2243         and hold: "holding s thread cs"
       
  2244       have neq_th: "th \<noteq> thread" 
       
  2245       proof -
       
  2246         from not_in eq_e have "th \<notin> threads s" by simp
       
  2247         moreover from is_runing have "thread \<in> threads s"
       
  2248           by (simp add:runing_def readys_def)
       
  2249         ultimately show ?thesis by auto
       
  2250       qed
       
  2251       from assms thread_V vt stp ih 
       
  2252       have vtv: "vt (V thread cs#s)" by auto
       
  2253       then interpret vt_v: valid_trace "(V thread cs#s)"
       
  2254         by (unfold_locales, simp)
       
  2255       from hold obtain rest 
       
  2256         where eq_wq: "wq s cs = thread # rest"
       
  2257         by (case_tac "wq s cs", auto simp: wq_def s_holding_def)
       
  2258       from not_in eq_e eq_wq
       
  2259       have "\<not> next_th s thread cs th"
       
  2260         apply (auto simp:next_th_def)
       
  2261       proof -
       
  2262         assume ne: "rest \<noteq> []"
       
  2263           and ni: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> threads s" (is "?t \<notin> threads s")
       
  2264         have "?t \<in> set rest"
       
  2265         proof(rule someI2)
       
  2266           from vt_v.wq_distinct[of cs] and eq_wq
       
  2267           show "distinct rest \<and> set rest = set rest"
       
  2268             by (metis distinct.simps(2) vt_s.wq_distinct) 
       
  2269         next
       
  2270           fix x assume "distinct x \<and> set x = set rest" with ne
       
  2271           show "hd x \<in> set rest" by (cases x, auto)
       
  2272         qed
       
  2273         with eq_wq have "?t \<in> set (wq s cs)" by simp
       
  2274         from vt_s.wq_threads[OF this] and ni
       
  2275         show False
       
  2276           using `hd (SOME q. distinct q \<and> set q = set rest) \<in> set (wq s cs)` 
       
  2277             ni vt_s.wq_threads by blast 
       
  2278       qed
       
  2279       moreover note neq_th eq_wq
       
  2280       ultimately have "cntCS (e # s) th  = cntCS s th"
       
  2281         by (unfold eq_e cntCS_def holdents_test step_RAG_v[OF vtv], auto)
       
  2282       moreover have "cntCS s th = 0"
       
  2283       proof(rule ih)
       
  2284         from not_in eq_e show "th \<notin> threads s" by simp
       
  2285       qed
       
  2286       ultimately show ?thesis by simp
       
  2287     next
       
  2288       case (thread_set thread prio)
       
  2289       print_facts
       
  2290       assume eq_e: "e = Set thread prio"
       
  2291         and is_runing: "thread \<in> runing s"
       
  2292       from not_in and eq_e have "th \<notin> threads s" by auto
       
  2293       from ih [OF this] and eq_e
       
  2294       show ?thesis 
       
  2295         apply (unfold eq_e cntCS_def holdents_test)
       
  2296         by (simp add:RAG_set_unchanged)
       
  2297     qed
       
  2298     next
       
  2299       case vt_nil
       
  2300       show ?case
       
  2301       by (unfold cntCS_def, 
       
  2302         auto simp:count_def holdents_test s_RAG_def wq_def cs_holding_def)
       
  2303   qed
       
  2304 qed
       
  2305 
       
  2306 end
       
  2307 
       
  2308 lemma eq_waiting: "waiting (wq (s::state)) th cs = waiting s th cs"
       
  2309   by (auto simp:s_waiting_def cs_waiting_def wq_def)
       
  2310 
       
  2311 context valid_trace
       
  2312 begin
       
  2313 
       
  2314 lemma dm_RAG_threads:
       
  2315   assumes in_dom: "(Th th) \<in> Domain (RAG s)"
       
  2316   shows "th \<in> threads s"
       
  2317 proof -
       
  2318   from in_dom obtain n where "(Th th, n) \<in> RAG s" by auto
       
  2319   moreover from RAG_target_th[OF this] obtain cs where "n = Cs cs" by auto
       
  2320   ultimately have "(Th th, Cs cs) \<in> RAG s" by simp
       
  2321   hence "th \<in> set (wq s cs)"
       
  2322     by (unfold s_RAG_def, auto simp:cs_waiting_def)
       
  2323   from wq_threads [OF this] show ?thesis .
       
  2324 qed
       
  2325 
       
  2326 end
       
  2327 
       
  2328 lemma cp_eq_cpreced: "cp s th = cpreced (wq s) s th"
       
  2329 unfolding cp_def wq_def
       
  2330 apply(induct s rule: schs.induct)
       
  2331 thm cpreced_initial
       
  2332 apply(simp add: Let_def cpreced_initial)
       
  2333 apply(simp add: Let_def)
       
  2334 apply(simp add: Let_def)
       
  2335 apply(simp add: Let_def)
       
  2336 apply(subst (2) schs.simps)
       
  2337 apply(simp add: Let_def)
       
  2338 apply(subst (2) schs.simps)
       
  2339 apply(simp add: Let_def)
       
  2340 done
       
  2341 
       
  2342 context valid_trace
       
  2343 begin
       
  2344 
       
  2345 lemma runing_unique:
       
  2346   assumes runing_1: "th1 \<in> runing s"
       
  2347   and runing_2: "th2 \<in> runing s"
       
  2348   shows "th1 = th2"
       
  2349 proof -
       
  2350   from runing_1 and runing_2 have "cp s th1 = cp s th2"
       
  2351     unfolding runing_def
       
  2352     apply(simp)
       
  2353     done
       
  2354   hence eq_max: "Max ((\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1)) =
       
  2355                  Max ((\<lambda>th. preced th s) ` ({th2} \<union> dependants (wq s) th2))"
       
  2356     (is "Max (?f ` ?A) = Max (?f ` ?B)")
       
  2357     unfolding cp_eq_cpreced 
       
  2358     unfolding cpreced_def .
       
  2359   obtain th1' where th1_in: "th1' \<in> ?A" and eq_f_th1: "?f th1' = Max (?f ` ?A)"
       
  2360   proof -
       
  2361     have h1: "finite (?f ` ?A)"
       
  2362     proof -
       
  2363       have "finite ?A" 
       
  2364       proof -
       
  2365         have "finite (dependants (wq s) th1)"
       
  2366         proof-
       
  2367           have "finite {th'. (Th th', Th th1) \<in> (RAG (wq s))\<^sup>+}"
       
  2368           proof -
       
  2369             let ?F = "\<lambda> (x, y). the_th x"
       
  2370             have "{th'. (Th th', Th th1) \<in> (RAG (wq s))\<^sup>+} \<subseteq> ?F ` ((RAG (wq s))\<^sup>+)"
       
  2371               apply (auto simp:image_def)
       
  2372               by (rule_tac x = "(Th x, Th th1)" in bexI, auto)
       
  2373             moreover have "finite \<dots>"
       
  2374             proof -
       
  2375               from finite_RAG have "finite (RAG s)" .
       
  2376               hence "finite ((RAG (wq s))\<^sup>+)"
       
  2377                 apply (unfold finite_trancl)
       
  2378                 by (auto simp: s_RAG_def cs_RAG_def wq_def)
       
  2379               thus ?thesis by auto
       
  2380             qed
       
  2381             ultimately show ?thesis by (auto intro:finite_subset)
       
  2382           qed
       
  2383           thus ?thesis by (simp add:cs_dependants_def)
       
  2384         qed
       
  2385         thus ?thesis by simp
       
  2386       qed
       
  2387       thus ?thesis by auto
       
  2388     qed
       
  2389     moreover have h2: "(?f ` ?A) \<noteq> {}"
       
  2390     proof -
       
  2391       have "?A \<noteq> {}" by simp
       
  2392       thus ?thesis by simp
       
  2393     qed
       
  2394     from Max_in [OF h1 h2]
       
  2395     have "Max (?f ` ?A) \<in> (?f ` ?A)" .
       
  2396     thus ?thesis 
       
  2397       thm cpreced_def
       
  2398       unfolding cpreced_def[symmetric] 
       
  2399       unfolding cp_eq_cpreced[symmetric] 
       
  2400       unfolding cpreced_def 
       
  2401       using that[intro] by (auto)
       
  2402   qed
       
  2403   obtain th2' where th2_in: "th2' \<in> ?B" and eq_f_th2: "?f th2' = Max (?f ` ?B)"
       
  2404   proof -
       
  2405     have h1: "finite (?f ` ?B)"
       
  2406     proof -
       
  2407       have "finite ?B" 
       
  2408       proof -
       
  2409         have "finite (dependants (wq s) th2)"
       
  2410         proof-
       
  2411           have "finite {th'. (Th th', Th th2) \<in> (RAG (wq s))\<^sup>+}"
       
  2412           proof -
       
  2413             let ?F = "\<lambda> (x, y). the_th x"
       
  2414             have "{th'. (Th th', Th th2) \<in> (RAG (wq s))\<^sup>+} \<subseteq> ?F ` ((RAG (wq s))\<^sup>+)"
       
  2415               apply (auto simp:image_def)
       
  2416               by (rule_tac x = "(Th x, Th th2)" in bexI, auto)
       
  2417             moreover have "finite \<dots>"
       
  2418             proof -
       
  2419               from finite_RAG have "finite (RAG s)" .
       
  2420               hence "finite ((RAG (wq s))\<^sup>+)"
       
  2421                 apply (unfold finite_trancl)
       
  2422                 by (auto simp: s_RAG_def cs_RAG_def wq_def)
       
  2423               thus ?thesis by auto
       
  2424             qed
       
  2425             ultimately show ?thesis by (auto intro:finite_subset)
       
  2426           qed
       
  2427           thus ?thesis by (simp add:cs_dependants_def)
       
  2428         qed
       
  2429         thus ?thesis by simp
       
  2430       qed
       
  2431       thus ?thesis by auto
       
  2432     qed
       
  2433     moreover have h2: "(?f ` ?B) \<noteq> {}"
       
  2434     proof -
       
  2435       have "?B \<noteq> {}" by simp
       
  2436       thus ?thesis by simp
       
  2437     qed
       
  2438     from Max_in [OF h1 h2]
       
  2439     have "Max (?f ` ?B) \<in> (?f ` ?B)" .
       
  2440     thus ?thesis by (auto intro:that)
       
  2441   qed
       
  2442   from eq_f_th1 eq_f_th2 eq_max 
       
  2443   have eq_preced: "preced th1' s = preced th2' s" by auto
       
  2444   hence eq_th12: "th1' = th2'"
       
  2445   proof (rule preced_unique)
       
  2446     from th1_in have "th1' = th1 \<or> (th1' \<in> dependants (wq s) th1)" by simp
       
  2447     thus "th1' \<in> threads s"
       
  2448     proof
       
  2449       assume "th1' \<in> dependants (wq s) th1"
       
  2450       hence "(Th th1') \<in> Domain ((RAG s)^+)"
       
  2451         apply (unfold cs_dependants_def cs_RAG_def s_RAG_def)
       
  2452         by (auto simp:Domain_def)
       
  2453       hence "(Th th1') \<in> Domain (RAG s)" by (simp add:trancl_domain)
       
  2454       from dm_RAG_threads[OF this] show ?thesis .
       
  2455     next
       
  2456       assume "th1' = th1"
       
  2457       with runing_1 show ?thesis
       
  2458         by (unfold runing_def readys_def, auto)
       
  2459     qed
       
  2460   next
       
  2461     from th2_in have "th2' = th2 \<or> (th2' \<in> dependants (wq s) th2)" by simp
       
  2462     thus "th2' \<in> threads s"
       
  2463     proof
       
  2464       assume "th2' \<in> dependants (wq s) th2"
       
  2465       hence "(Th th2') \<in> Domain ((RAG s)^+)"
       
  2466         apply (unfold cs_dependants_def cs_RAG_def s_RAG_def)
       
  2467         by (auto simp:Domain_def)
       
  2468       hence "(Th th2') \<in> Domain (RAG s)" by (simp add:trancl_domain)
       
  2469       from dm_RAG_threads[OF this] show ?thesis .
       
  2470     next
       
  2471       assume "th2' = th2"
       
  2472       with runing_2 show ?thesis
       
  2473         by (unfold runing_def readys_def, auto)
       
  2474     qed
       
  2475   qed
       
  2476   from th1_in have "th1' = th1 \<or> th1' \<in> dependants (wq s) th1" by simp
       
  2477   thus ?thesis
       
  2478   proof
       
  2479     assume eq_th': "th1' = th1"
       
  2480     from th2_in have "th2' = th2 \<or> th2' \<in> dependants (wq s) th2" by simp
       
  2481     thus ?thesis
       
  2482     proof
       
  2483       assume "th2' = th2" thus ?thesis using eq_th' eq_th12 by simp
       
  2484     next
       
  2485       assume "th2' \<in> dependants (wq s) th2"
       
  2486       with eq_th12 eq_th' have "th1 \<in> dependants (wq s) th2" by simp
       
  2487       hence "(Th th1, Th th2) \<in> (RAG s)^+"
       
  2488         by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp)
       
  2489       hence "Th th1 \<in> Domain ((RAG s)^+)" 
       
  2490         apply (unfold cs_dependants_def cs_RAG_def s_RAG_def)
       
  2491         by (auto simp:Domain_def)
       
  2492       hence "Th th1 \<in> Domain (RAG s)" by (simp add:trancl_domain)
       
  2493       then obtain n where d: "(Th th1, n) \<in> RAG s" by (auto simp:Domain_def)
       
  2494       from RAG_target_th [OF this]
       
  2495       obtain cs' where "n = Cs cs'" by auto
       
  2496       with d have "(Th th1, Cs cs') \<in> RAG s" by simp
       
  2497       with runing_1 have "False"
       
  2498         apply (unfold runing_def readys_def s_RAG_def)
       
  2499         by (auto simp:eq_waiting)
       
  2500       thus ?thesis by simp
       
  2501     qed
       
  2502   next
       
  2503     assume th1'_in: "th1' \<in> dependants (wq s) th1"
       
  2504     from th2_in have "th2' = th2 \<or> th2' \<in> dependants (wq s) th2" by simp
       
  2505     thus ?thesis 
       
  2506     proof
       
  2507       assume "th2' = th2"
       
  2508       with th1'_in eq_th12 have "th2 \<in> dependants (wq s) th1" by simp
       
  2509       hence "(Th th2, Th th1) \<in> (RAG s)^+"
       
  2510         by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp)
       
  2511       hence "Th th2 \<in> Domain ((RAG s)^+)" 
       
  2512         apply (unfold cs_dependants_def cs_RAG_def s_RAG_def)
       
  2513         by (auto simp:Domain_def)
       
  2514       hence "Th th2 \<in> Domain (RAG s)" by (simp add:trancl_domain)
       
  2515       then obtain n where d: "(Th th2, n) \<in> RAG s" by (auto simp:Domain_def)
       
  2516       from RAG_target_th [OF this]
       
  2517       obtain cs' where "n = Cs cs'" by auto
       
  2518       with d have "(Th th2, Cs cs') \<in> RAG s" by simp
       
  2519       with runing_2 have "False"
       
  2520         apply (unfold runing_def readys_def s_RAG_def)
       
  2521         by (auto simp:eq_waiting)
       
  2522       thus ?thesis by simp
       
  2523     next
       
  2524       assume "th2' \<in> dependants (wq s) th2"
       
  2525       with eq_th12 have "th1' \<in> dependants (wq s) th2" by simp
       
  2526       hence h1: "(Th th1', Th th2) \<in> (RAG s)^+"
       
  2527         by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp)
       
  2528       from th1'_in have h2: "(Th th1', Th th1) \<in> (RAG s)^+"
       
  2529         by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp)
       
  2530       show ?thesis
       
  2531       proof(rule dchain_unique[OF h1 _ h2, symmetric])
       
  2532         from runing_1 show "th1 \<in> readys s" by (simp add:runing_def)
       
  2533         from runing_2 show "th2 \<in> readys s" by (simp add:runing_def) 
       
  2534       qed
       
  2535     qed
       
  2536   qed
       
  2537 qed
       
  2538 
       
  2539 
       
  2540 lemma "card (runing s) \<le> 1"
       
  2541 apply(subgoal_tac "finite (runing s)")
       
  2542 prefer 2
       
  2543 apply (metis finite_nat_set_iff_bounded lessI runing_unique)
       
  2544 apply(rule ccontr)
       
  2545 apply(simp)
       
  2546 apply(case_tac "Suc (Suc 0) \<le> card (runing s)")
       
  2547 apply(subst (asm) card_le_Suc_iff)
       
  2548 apply(simp)
       
  2549 apply(auto)[1]
       
  2550 apply (metis insertCI runing_unique)
       
  2551 apply(auto) 
       
  2552 done
       
  2553 
       
  2554 end
       
  2555 
       
  2556 
       
  2557 lemma create_pre:
       
  2558   assumes stp: "step s e"
       
  2559   and not_in: "th \<notin> threads s"
       
  2560   and is_in: "th \<in> threads (e#s)"
       
  2561   obtains prio where "e = Create th prio"
       
  2562 proof -
       
  2563   from assms  
       
  2564   show ?thesis
       
  2565   proof(cases)
       
  2566     case (thread_create thread prio)
       
  2567     with is_in not_in have "e = Create th prio" by simp
       
  2568     from that[OF this] show ?thesis .
       
  2569   next
       
  2570     case (thread_exit thread)
       
  2571     with assms show ?thesis by (auto intro!:that)
       
  2572   next
       
  2573     case (thread_P thread)
       
  2574     with assms show ?thesis by (auto intro!:that)
       
  2575   next
       
  2576     case (thread_V thread)
       
  2577     with assms show ?thesis by (auto intro!:that)
       
  2578   next 
       
  2579     case (thread_set thread)
       
  2580     with assms show ?thesis by (auto intro!:that)
       
  2581   qed
       
  2582 qed
       
  2583 
       
  2584 
       
  2585 context valid_trace
       
  2586 begin
       
  2587 
       
  2588 lemma cnp_cnv_eq:
       
  2589   assumes "th \<notin> threads s"
       
  2590   shows "cntP s th = cntV s th"
       
  2591   using assms
       
  2592   using cnp_cnv_cncs not_thread_cncs by auto
       
  2593 
       
  2594 end
       
  2595 
       
  2596 
       
  2597 lemma eq_RAG: 
       
  2598   "RAG (wq s) = RAG s"
       
  2599 by (unfold cs_RAG_def s_RAG_def, auto)
       
  2600 
       
  2601 context valid_trace
       
  2602 begin
       
  2603 
       
  2604 lemma count_eq_dependants:
       
  2605   assumes eq_pv: "cntP s th = cntV s th"
       
  2606   shows "dependants (wq s) th = {}"
       
  2607 proof -
       
  2608   from cnp_cnv_cncs and eq_pv
       
  2609   have "cntCS s th = 0" 
       
  2610     by (auto split:if_splits)
       
  2611   moreover have "finite {cs. (Cs cs, Th th) \<in> RAG s}"
       
  2612   proof -
       
  2613     from finite_holding[of th] show ?thesis
       
  2614       by (simp add:holdents_test)
       
  2615   qed
       
  2616   ultimately have h: "{cs. (Cs cs, Th th) \<in> RAG s} = {}"
       
  2617     by (unfold cntCS_def holdents_test cs_dependants_def, auto)
       
  2618   show ?thesis
       
  2619   proof(unfold cs_dependants_def)
       
  2620     { assume "{th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} \<noteq> {}"
       
  2621       then obtain th' where "(Th th', Th th) \<in> (RAG (wq s))\<^sup>+" by auto
       
  2622       hence "False"
       
  2623       proof(cases)
       
  2624         assume "(Th th', Th th) \<in> RAG (wq s)"
       
  2625         thus "False" by (auto simp:cs_RAG_def)
       
  2626       next
       
  2627         fix c
       
  2628         assume "(c, Th th) \<in> RAG (wq s)"
       
  2629         with h and eq_RAG show "False"
       
  2630           by (cases c, auto simp:cs_RAG_def)
       
  2631       qed
       
  2632     } thus "{th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} = {}" by auto
       
  2633   qed
       
  2634 qed
       
  2635 
       
  2636 lemma dependants_threads:
       
  2637   shows "dependants (wq s) th \<subseteq> threads s"
       
  2638 proof
       
  2639   { fix th th'
       
  2640     assume h: "th \<in> {th'a. (Th th'a, Th th') \<in> (RAG (wq s))\<^sup>+}"
       
  2641     have "Th th \<in> Domain (RAG s)"
       
  2642     proof -
       
  2643       from h obtain th' where "(Th th, Th th') \<in> (RAG (wq s))\<^sup>+" by auto
       
  2644       hence "(Th th) \<in> Domain ( (RAG (wq s))\<^sup>+)" by (auto simp:Domain_def)
       
  2645       with trancl_domain have "(Th th) \<in> Domain (RAG (wq s))" by simp
       
  2646       thus ?thesis using eq_RAG by simp
       
  2647     qed
       
  2648     from dm_RAG_threads[OF this]
       
  2649     have "th \<in> threads s" .
       
  2650   } note hh = this
       
  2651   fix th1 
       
  2652   assume "th1 \<in> dependants (wq s) th"
       
  2653   hence "th1 \<in> {th'a. (Th th'a, Th th) \<in> (RAG (wq s))\<^sup>+}"
       
  2654     by (unfold cs_dependants_def, simp)
       
  2655   from hh [OF this] show "th1 \<in> threads s" .
       
  2656 qed
       
  2657 
       
  2658 lemma finite_threads:
       
  2659   shows "finite (threads s)"
       
  2660 using vt by (induct) (auto elim: step.cases)
       
  2661 
       
  2662 end
       
  2663 
       
  2664 lemma Max_f_mono:
       
  2665   assumes seq: "A \<subseteq> B"
       
  2666   and np: "A \<noteq> {}"
       
  2667   and fnt: "finite B"
       
  2668   shows "Max (f ` A) \<le> Max (f ` B)"
       
  2669 proof(rule Max_mono)
       
  2670   from seq show "f ` A \<subseteq> f ` B" by auto
       
  2671 next
       
  2672   from np show "f ` A \<noteq> {}" by auto
       
  2673 next
       
  2674   from fnt and seq show "finite (f ` B)" by auto
       
  2675 qed
       
  2676 
       
  2677 context valid_trace
       
  2678 begin
       
  2679 
       
  2680 lemma cp_le:
       
  2681   assumes th_in: "th \<in> threads s"
       
  2682   shows "cp s th \<le> Max ((\<lambda> th. (preced th s)) ` threads s)"
       
  2683 proof(unfold cp_eq_cpreced cpreced_def cs_dependants_def)
       
  2684   show "Max ((\<lambda>th. preced th s) ` ({th} \<union> {th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+}))
       
  2685          \<le> Max ((\<lambda>th. preced th s) ` threads s)"
       
  2686     (is "Max (?f ` ?A) \<le> Max (?f ` ?B)")
       
  2687   proof(rule Max_f_mono)
       
  2688     show "{th} \<union> {th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} \<noteq> {}" by simp
       
  2689   next
       
  2690     from finite_threads
       
  2691     show "finite (threads s)" .
       
  2692   next
       
  2693     from th_in
       
  2694     show "{th} \<union> {th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} \<subseteq> threads s"
       
  2695       apply (auto simp:Domain_def)
       
  2696       apply (rule_tac dm_RAG_threads)
       
  2697       apply (unfold trancl_domain [of "RAG s", symmetric])
       
  2698       by (unfold cs_RAG_def s_RAG_def, auto simp:Domain_def)
       
  2699   qed
       
  2700 qed
       
  2701 
       
  2702 lemma le_cp:
       
  2703   shows "preced th s \<le> cp s th"
       
  2704 proof(unfold cp_eq_cpreced preced_def cpreced_def, simp)
       
  2705   show "Prc (priority th s) (last_set th s)
       
  2706     \<le> Max (insert (Prc (priority th s) (last_set th s))
       
  2707             ((\<lambda>th. Prc (priority th s) (last_set th s)) ` dependants (wq s) th))"
       
  2708     (is "?l \<le> Max (insert ?l ?A)")
       
  2709   proof(cases "?A = {}")
       
  2710     case False
       
  2711     have "finite ?A" (is "finite (?f ` ?B)")
       
  2712     proof -
       
  2713       have "finite ?B" 
       
  2714       proof-
       
  2715         have "finite {th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+}"
       
  2716         proof -
       
  2717           let ?F = "\<lambda> (x, y). the_th x"
       
  2718           have "{th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} \<subseteq> ?F ` ((RAG (wq s))\<^sup>+)"
       
  2719             apply (auto simp:image_def)
       
  2720             by (rule_tac x = "(Th x, Th th)" in bexI, auto)
       
  2721           moreover have "finite \<dots>"
       
  2722           proof -
       
  2723             from finite_RAG have "finite (RAG s)" .
       
  2724             hence "finite ((RAG (wq s))\<^sup>+)"
       
  2725               apply (unfold finite_trancl)
       
  2726               by (auto simp: s_RAG_def cs_RAG_def wq_def)
       
  2727             thus ?thesis by auto
       
  2728           qed
       
  2729           ultimately show ?thesis by (auto intro:finite_subset)
       
  2730         qed
       
  2731         thus ?thesis by (simp add:cs_dependants_def)
       
  2732       qed
       
  2733       thus ?thesis by simp
       
  2734     qed
       
  2735     from Max_insert [OF this False, of ?l] show ?thesis by auto
       
  2736   next
       
  2737     case True
       
  2738     thus ?thesis by auto
       
  2739   qed
       
  2740 qed
       
  2741 
       
  2742 lemma max_cp_eq: 
       
  2743   shows "Max ((cp s) ` threads s) = Max ((\<lambda> th. (preced th s)) ` threads s)"
       
  2744   (is "?l = ?r")
       
  2745 proof(cases "threads s = {}")
       
  2746   case True
       
  2747   thus ?thesis by auto
       
  2748 next
       
  2749   case False
       
  2750   have "?l \<in> ((cp s) ` threads s)"
       
  2751   proof(rule Max_in)
       
  2752     from finite_threads
       
  2753     show "finite (cp s ` threads s)" by auto
       
  2754   next
       
  2755     from False show "cp s ` threads s \<noteq> {}" by auto
       
  2756   qed
       
  2757   then obtain th 
       
  2758     where th_in: "th \<in> threads s" and eq_l: "?l = cp s th" by auto
       
  2759   have "\<dots> \<le> ?r" by (rule cp_le[OF th_in])
       
  2760   moreover have "?r \<le> cp s th" (is "Max (?f ` ?A) \<le> cp s th")
       
  2761   proof -
       
  2762     have "?r \<in> (?f ` ?A)"
       
  2763     proof(rule Max_in)
       
  2764       from finite_threads
       
  2765       show " finite ((\<lambda>th. preced th s) ` threads s)" by auto
       
  2766     next
       
  2767       from False show " (\<lambda>th. preced th s) ` threads s \<noteq> {}" by auto
       
  2768     qed
       
  2769     then obtain th' where 
       
  2770       th_in': "th' \<in> ?A " and eq_r: "?r = ?f th'" by auto
       
  2771     from le_cp [of th']  eq_r
       
  2772     have "?r \<le> cp s th'" by auto
       
  2773     moreover have "\<dots> \<le> cp s th"
       
  2774     proof(fold eq_l)
       
  2775       show " cp s th' \<le> Max (cp s ` threads s)"
       
  2776       proof(rule Max_ge)
       
  2777         from th_in' show "cp s th' \<in> cp s ` threads s"
       
  2778           by auto
       
  2779       next
       
  2780         from finite_threads
       
  2781         show "finite (cp s ` threads s)" by auto
       
  2782       qed
       
  2783     qed
       
  2784     ultimately show ?thesis by auto
       
  2785   qed
       
  2786   ultimately show ?thesis using eq_l by auto
       
  2787 qed
       
  2788 
       
  2789 lemma max_cp_readys_threads_pre:
       
  2790   assumes np: "threads s \<noteq> {}"
       
  2791   shows "Max (cp s ` readys s) = Max (cp s ` threads s)"
       
  2792 proof(unfold max_cp_eq)
       
  2793   show "Max (cp s ` readys s) = Max ((\<lambda>th. preced th s) ` threads s)"
       
  2794   proof -
       
  2795     let ?p = "Max ((\<lambda>th. preced th s) ` threads s)" 
       
  2796     let ?f = "(\<lambda>th. preced th s)"
       
  2797     have "?p \<in> ((\<lambda>th. preced th s) ` threads s)"
       
  2798     proof(rule Max_in)
       
  2799       from finite_threads show "finite (?f ` threads s)" by simp
       
  2800     next
       
  2801       from np show "?f ` threads s \<noteq> {}" by simp
       
  2802     qed
       
  2803     then obtain tm where tm_max: "?f tm = ?p" and tm_in: "tm \<in> threads s"
       
  2804       by (auto simp:Image_def)
       
  2805     from th_chain_to_ready [OF tm_in]
       
  2806     have "tm \<in> readys s \<or> (\<exists>th'. th' \<in> readys s \<and> (Th tm, Th th') \<in> (RAG s)\<^sup>+)" .
       
  2807     thus ?thesis
       
  2808     proof
       
  2809       assume "\<exists>th'. th' \<in> readys s \<and> (Th tm, Th th') \<in> (RAG s)\<^sup>+ "
       
  2810       then obtain th' where th'_in: "th' \<in> readys s" 
       
  2811         and tm_chain:"(Th tm, Th th') \<in> (RAG s)\<^sup>+" by auto
       
  2812       have "cp s th' = ?f tm"
       
  2813       proof(subst cp_eq_cpreced, subst cpreced_def, rule Max_eqI)
       
  2814         from dependants_threads finite_threads
       
  2815         show "finite ((\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th'))" 
       
  2816           by (auto intro:finite_subset)
       
  2817       next
       
  2818         fix p assume p_in: "p \<in> (\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')"
       
  2819         from tm_max have " preced tm s = Max ((\<lambda>th. preced th s) ` threads s)" .
       
  2820         moreover have "p \<le> \<dots>"
       
  2821         proof(rule Max_ge)
       
  2822           from finite_threads
       
  2823           show "finite ((\<lambda>th. preced th s) ` threads s)" by simp
       
  2824         next
       
  2825           from p_in and th'_in and dependants_threads[of th']
       
  2826           show "p \<in> (\<lambda>th. preced th s) ` threads s"
       
  2827             by (auto simp:readys_def)
       
  2828         qed
       
  2829         ultimately show "p \<le> preced tm s" by auto
       
  2830       next
       
  2831         show "preced tm s \<in> (\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')"
       
  2832         proof -
       
  2833           from tm_chain
       
  2834           have "tm \<in> dependants (wq s) th'"
       
  2835             by (unfold cs_dependants_def s_RAG_def cs_RAG_def, auto)
       
  2836           thus ?thesis by auto
       
  2837         qed
       
  2838       qed
       
  2839       with tm_max
       
  2840       have h: "cp s th' = Max ((\<lambda>th. preced th s) ` threads s)" by simp
       
  2841       show ?thesis
       
  2842       proof (fold h, rule Max_eqI)
       
  2843         fix q 
       
  2844         assume "q \<in> cp s ` readys s"
       
  2845         then obtain th1 where th1_in: "th1 \<in> readys s"
       
  2846           and eq_q: "q = cp s th1" by auto
       
  2847         show "q \<le> cp s th'"
       
  2848           apply (unfold h eq_q)
       
  2849           apply (unfold cp_eq_cpreced cpreced_def)
       
  2850           apply (rule Max_mono)
       
  2851         proof -
       
  2852           from dependants_threads [of th1] th1_in
       
  2853           show "(\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1) \<subseteq> 
       
  2854                  (\<lambda>th. preced th s) ` threads s"
       
  2855             by (auto simp:readys_def)
       
  2856         next
       
  2857           show "(\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1) \<noteq> {}" by simp
       
  2858         next
       
  2859           from finite_threads 
       
  2860           show " finite ((\<lambda>th. preced th s) ` threads s)" by simp
       
  2861         qed
       
  2862       next
       
  2863         from finite_threads
       
  2864         show "finite (cp s ` readys s)" by (auto simp:readys_def)
       
  2865       next
       
  2866         from th'_in
       
  2867         show "cp s th' \<in> cp s ` readys s" by simp
       
  2868       qed
       
  2869     next
       
  2870       assume tm_ready: "tm \<in> readys s"
       
  2871       show ?thesis
       
  2872       proof(fold tm_max)
       
  2873         have cp_eq_p: "cp s tm = preced tm s"
       
  2874         proof(unfold cp_eq_cpreced cpreced_def, rule Max_eqI)
       
  2875           fix y 
       
  2876           assume hy: "y \<in> (\<lambda>th. preced th s) ` ({tm} \<union> dependants (wq s) tm)"
       
  2877           show "y \<le> preced tm s"
       
  2878           proof -
       
  2879             { fix y'
       
  2880               assume hy' : "y' \<in> ((\<lambda>th. preced th s) ` dependants (wq s) tm)"
       
  2881               have "y' \<le> preced tm s"
       
  2882               proof(unfold tm_max, rule Max_ge)
       
  2883                 from hy' dependants_threads[of tm]
       
  2884                 show "y' \<in> (\<lambda>th. preced th s) ` threads s" by auto
       
  2885               next
       
  2886                 from finite_threads
       
  2887                 show "finite ((\<lambda>th. preced th s) ` threads s)" by simp
       
  2888               qed
       
  2889             } with hy show ?thesis by auto
       
  2890           qed
       
  2891         next
       
  2892           from dependants_threads[of tm] finite_threads
       
  2893           show "finite ((\<lambda>th. preced th s) ` ({tm} \<union> dependants (wq s) tm))"
       
  2894             by (auto intro:finite_subset)
       
  2895         next
       
  2896           show "preced tm s \<in> (\<lambda>th. preced th s) ` ({tm} \<union> dependants (wq s) tm)"
       
  2897             by simp
       
  2898         qed 
       
  2899         moreover have "Max (cp s ` readys s) = cp s tm"
       
  2900         proof(rule Max_eqI)
       
  2901           from tm_ready show "cp s tm \<in> cp s ` readys s" by simp
       
  2902         next
       
  2903           from finite_threads
       
  2904           show "finite (cp s ` readys s)" by (auto simp:readys_def)
       
  2905         next
       
  2906           fix y assume "y \<in> cp s ` readys s"
       
  2907           then obtain th1 where th1_readys: "th1 \<in> readys s"
       
  2908             and h: "y = cp s th1" by auto
       
  2909           show "y \<le> cp s tm"
       
  2910             apply(unfold cp_eq_p h)
       
  2911             apply(unfold cp_eq_cpreced cpreced_def tm_max, rule Max_mono)
       
  2912           proof -
       
  2913             from finite_threads
       
  2914             show "finite ((\<lambda>th. preced th s) ` threads s)" by simp
       
  2915           next
       
  2916             show "(\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1) \<noteq> {}"
       
  2917               by simp
       
  2918           next
       
  2919             from dependants_threads[of th1] th1_readys
       
  2920             show "(\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1) 
       
  2921                     \<subseteq> (\<lambda>th. preced th s) ` threads s"
       
  2922               by (auto simp:readys_def)
       
  2923           qed
       
  2924         qed
       
  2925         ultimately show " Max (cp s ` readys s) = preced tm s" by simp
       
  2926       qed 
       
  2927     qed
       
  2928   qed
       
  2929 qed
       
  2930 
       
  2931 text {* (* ccc *) \noindent
       
  2932   Since the current precedence of the threads in ready queue will always be boosted,
       
  2933   there must be one inside it has the maximum precedence of the whole system. 
       
  2934 *}
       
  2935 lemma max_cp_readys_threads:
       
  2936   shows "Max (cp s ` readys s) = Max (cp s ` threads s)"
       
  2937 proof(cases "threads s = {}")
       
  2938   case True
       
  2939   thus ?thesis 
       
  2940     by (auto simp:readys_def)
       
  2941 next
       
  2942   case False
       
  2943   show ?thesis by (rule max_cp_readys_threads_pre[OF False])
       
  2944 qed
       
  2945 
       
  2946 end
       
  2947 
       
  2948 lemma eq_holding: "holding (wq s) th cs = holding s th cs"
       
  2949   apply (unfold s_holding_def cs_holding_def wq_def, simp)
       
  2950   done
       
  2951 
       
  2952 lemma f_image_eq:
       
  2953   assumes h: "\<And> a. a \<in> A \<Longrightarrow> f a = g a"
       
  2954   shows "f ` A = g ` A"
       
  2955 proof
       
  2956   show "f ` A \<subseteq> g ` A"
       
  2957     by(rule image_subsetI, auto intro:h)
       
  2958 next
       
  2959   show "g ` A \<subseteq> f ` A"
       
  2960    by (rule image_subsetI, auto intro:h[symmetric])
       
  2961 qed
       
  2962 
       
  2963 
       
  2964 definition detached :: "state \<Rightarrow> thread \<Rightarrow> bool"
       
  2965   where "detached s th \<equiv> (\<not>(\<exists> cs. holding s th cs)) \<and> (\<not>(\<exists>cs. waiting s th cs))"
       
  2966 
       
  2967 
       
  2968 lemma detached_test:
       
  2969   shows "detached s th = (Th th \<notin> Field (RAG s))"
       
  2970 apply(simp add: detached_def Field_def)
       
  2971 apply(simp add: s_RAG_def)
       
  2972 apply(simp add: s_holding_abv s_waiting_abv)
       
  2973 apply(simp add: Domain_iff Range_iff)
       
  2974 apply(simp add: wq_def)
       
  2975 apply(auto)
       
  2976 done
       
  2977 
       
  2978 context valid_trace
       
  2979 begin
       
  2980 
       
  2981 lemma detached_intro:
       
  2982   assumes eq_pv: "cntP s th = cntV s th"
       
  2983   shows "detached s th"
       
  2984 proof -
       
  2985  from cnp_cnv_cncs
       
  2986   have eq_cnt: "cntP s th =
       
  2987     cntV s th + (if th \<in> readys s \<or> th \<notin> threads s then cntCS s th else cntCS s th + 1)" .
       
  2988   hence cncs_zero: "cntCS s th = 0"
       
  2989     by (auto simp:eq_pv split:if_splits)
       
  2990   with eq_cnt
       
  2991   have "th \<in> readys s \<or> th \<notin> threads s" by (auto simp:eq_pv)
       
  2992   thus ?thesis
       
  2993   proof
       
  2994     assume "th \<notin> threads s"
       
  2995     with range_in dm_RAG_threads
       
  2996     show ?thesis
       
  2997       by (auto simp add: detached_def s_RAG_def s_waiting_abv s_holding_abv wq_def Domain_iff Range_iff)
       
  2998   next
       
  2999     assume "th \<in> readys s"
       
  3000     moreover have "Th th \<notin> Range (RAG s)"
       
  3001     proof -
       
  3002       from card_0_eq [OF finite_holding] and cncs_zero
       
  3003       have "holdents s th = {}"
       
  3004         by (simp add:cntCS_def)
       
  3005       thus ?thesis
       
  3006         apply(auto simp:holdents_test)
       
  3007         apply(case_tac a)
       
  3008         apply(auto simp:holdents_test s_RAG_def)
       
  3009         done
       
  3010     qed
       
  3011     ultimately show ?thesis
       
  3012       by (auto simp add: detached_def s_RAG_def s_waiting_abv s_holding_abv wq_def readys_def)
       
  3013   qed
       
  3014 qed
       
  3015 
       
  3016 lemma detached_elim:
       
  3017   assumes dtc: "detached s th"
       
  3018   shows "cntP s th = cntV s th"
       
  3019 proof -
       
  3020   from cnp_cnv_cncs
       
  3021   have eq_pv: " cntP s th =
       
  3022     cntV s th + (if th \<in> readys s \<or> th \<notin> threads s then cntCS s th else cntCS s th + 1)" .
       
  3023   have cncs_z: "cntCS s th = 0"
       
  3024   proof -
       
  3025     from dtc have "holdents s th = {}"
       
  3026       unfolding detached_def holdents_test s_RAG_def
       
  3027       by (simp add: s_waiting_abv wq_def s_holding_abv Domain_iff Range_iff)
       
  3028     thus ?thesis by (auto simp:cntCS_def)
       
  3029   qed
       
  3030   show ?thesis
       
  3031   proof(cases "th \<in> threads s")
       
  3032     case True
       
  3033     with dtc 
       
  3034     have "th \<in> readys s"
       
  3035       by (unfold readys_def detached_def Field_def Domain_def Range_def, 
       
  3036            auto simp:eq_waiting s_RAG_def)
       
  3037     with cncs_z and eq_pv show ?thesis by simp
       
  3038   next
       
  3039     case False
       
  3040     with cncs_z and eq_pv show ?thesis by simp
       
  3041   qed
       
  3042 qed
       
  3043 
       
  3044 lemma detached_eq:
       
  3045   shows "(detached s th) = (cntP s th = cntV s th)"
       
  3046   by (insert vt, auto intro:detached_intro detached_elim)
       
  3047 
       
  3048 end
       
  3049 
       
  3050 text {* 
       
  3051   The lemmas in this .thy file are all obvious lemmas, however, they still needs to be derived
       
  3052   from the concise and miniature model of PIP given in PrioGDef.thy.
       
  3053 *}
       
  3054 
       
  3055 lemma eq_dependants: "dependants (wq s) = dependants s"
       
  3056   by (simp add: s_dependants_abv wq_def)
       
  3057 
       
  3058 lemma next_th_unique: 
       
  3059   assumes nt1: "next_th s th cs th1"
       
  3060   and nt2: "next_th s th cs th2"
       
  3061   shows "th1 = th2"
       
  3062 using assms by (unfold next_th_def, auto)
       
  3063 
       
  3064 lemma birth_time_lt:  "s \<noteq> [] \<Longrightarrow> last_set th s < length s"
       
  3065   apply (induct s, simp)
       
  3066 proof -
       
  3067   fix a s
       
  3068   assume ih: "s \<noteq> [] \<Longrightarrow> last_set th s < length s"
       
  3069     and eq_as: "a # s \<noteq> []"
       
  3070   show "last_set th (a # s) < length (a # s)"
       
  3071   proof(cases "s \<noteq> []")
       
  3072     case False
       
  3073     from False show ?thesis
       
  3074       by (cases a, auto simp:last_set.simps)
       
  3075   next
       
  3076     case True
       
  3077     from ih [OF True] show ?thesis
       
  3078       by (cases a, auto simp:last_set.simps)
       
  3079   qed
       
  3080 qed
       
  3081 
       
  3082 lemma th_in_ne: "th \<in> threads s \<Longrightarrow> s \<noteq> []"
       
  3083   by (induct s, auto simp:threads.simps)
       
  3084 
       
  3085 lemma preced_tm_lt: "th \<in> threads s \<Longrightarrow> preced th s = Prc x y \<Longrightarrow> y < length s"
       
  3086   apply (drule_tac th_in_ne)
       
  3087   by (unfold preced_def, auto intro: birth_time_lt)
       
  3088 
       
  3089 lemma inj_the_preced: 
       
  3090   "inj_on (the_preced s) (threads s)"
       
  3091   by (metis inj_onI preced_unique the_preced_def)
       
  3092 
       
  3093 lemma tRAG_alt_def: 
       
  3094   "tRAG s = {(Th th1, Th th2) | th1 th2. 
       
  3095                   \<exists> cs. (Th th1, Cs cs) \<in> RAG s \<and> (Cs cs, Th th2) \<in> RAG s}"
       
  3096  by (auto simp:tRAG_def RAG_split wRAG_def hRAG_def)
       
  3097 
       
  3098 lemma tRAG_Field:
       
  3099   "Field (tRAG s) \<subseteq> Field (RAG s)"
       
  3100   by (unfold tRAG_alt_def Field_def, auto)
       
  3101 
       
  3102 lemma tRAG_ancestorsE:
       
  3103   assumes "x \<in> ancestors (tRAG s) u"
       
  3104   obtains th where "x = Th th"
       
  3105 proof -
       
  3106   from assms have "(u, x) \<in> (tRAG s)^+" 
       
  3107       by (unfold ancestors_def, auto)
       
  3108   from tranclE[OF this] obtain c where "(c, x) \<in> tRAG s" by auto
       
  3109   then obtain th where "x = Th th"
       
  3110     by (unfold tRAG_alt_def, auto)
       
  3111   from that[OF this] show ?thesis .
       
  3112 qed
       
  3113 
       
  3114 lemma tRAG_mono:
       
  3115   assumes "RAG s' \<subseteq> RAG s"
       
  3116   shows "tRAG s' \<subseteq> tRAG s"
       
  3117   using assms 
       
  3118   by (unfold tRAG_alt_def, auto)
       
  3119 
       
  3120 lemma holding_next_thI:
       
  3121   assumes "holding s th cs"
       
  3122   and "length (wq s cs) > 1"
       
  3123   obtains th' where "next_th s th cs th'"
       
  3124 proof -
       
  3125   from assms(1)[folded eq_holding, unfolded cs_holding_def]
       
  3126   have " th \<in> set (wq s cs) \<and> th = hd (wq s cs)" .
       
  3127   then obtain rest where h1: "wq s cs = th#rest" 
       
  3128     by (cases "wq s cs", auto)
       
  3129   with assms(2) have h2: "rest \<noteq> []" by auto
       
  3130   let ?th' = "hd (SOME q. distinct q \<and> set q = set rest)"
       
  3131   have "next_th s th cs ?th'" using  h1(1) h2 
       
  3132     by (unfold next_th_def, auto)
       
  3133   from that[OF this] show ?thesis .
       
  3134 qed
       
  3135 
       
  3136 lemma RAG_tRAG_transfer:
       
  3137   assumes "vt s'"
       
  3138   assumes "RAG s = RAG s' \<union> {(Th th, Cs cs)}"
       
  3139   and "(Cs cs, Th th'') \<in> RAG s'"
       
  3140   shows "tRAG s = tRAG s' \<union> {(Th th, Th th'')}" (is "?L = ?R")
       
  3141 proof -
       
  3142   interpret vt_s': valid_trace "s'" using assms(1)
       
  3143     by (unfold_locales, simp)
       
  3144   interpret rtree: rtree "RAG s'"
       
  3145   proof
       
  3146   show "single_valued (RAG s')"
       
  3147   apply (intro_locales)
       
  3148     by (unfold single_valued_def, 
       
  3149         auto intro:vt_s'.unique_RAG)
       
  3150 
       
  3151   show "acyclic (RAG s')"
       
  3152      by (rule vt_s'.acyclic_RAG)
       
  3153   qed
       
  3154   { fix n1 n2
       
  3155     assume "(n1, n2) \<in> ?L"
       
  3156     from this[unfolded tRAG_alt_def]
       
  3157     obtain th1 th2 cs' where 
       
  3158       h: "n1 = Th th1" "n2 = Th th2" 
       
  3159          "(Th th1, Cs cs') \<in> RAG s"
       
  3160          "(Cs cs', Th th2) \<in> RAG s" by auto
       
  3161     from h(4) and assms(2) have cs_in: "(Cs cs', Th th2) \<in> RAG s'" by auto
       
  3162     from h(3) and assms(2) 
       
  3163     have "(Th th1, Cs cs') = (Th th, Cs cs) \<or> 
       
  3164           (Th th1, Cs cs') \<in> RAG s'" by auto
       
  3165     hence "(n1, n2) \<in> ?R"
       
  3166     proof
       
  3167       assume h1: "(Th th1, Cs cs') = (Th th, Cs cs)"
       
  3168       hence eq_th1: "th1 = th" by simp
       
  3169       moreover have "th2 = th''"
       
  3170       proof -
       
  3171         from h1 have "cs' = cs" by simp
       
  3172         from assms(3) cs_in[unfolded this] rtree.sgv
       
  3173         show ?thesis
       
  3174           by (unfold single_valued_def, auto)
       
  3175       qed
       
  3176       ultimately show ?thesis using h(1,2) by auto
       
  3177     next
       
  3178       assume "(Th th1, Cs cs') \<in> RAG s'"
       
  3179       with cs_in have "(Th th1, Th th2) \<in> tRAG s'"
       
  3180         by (unfold tRAG_alt_def, auto)
       
  3181       from this[folded h(1, 2)] show ?thesis by auto
       
  3182     qed
       
  3183   } moreover {
       
  3184     fix n1 n2
       
  3185     assume "(n1, n2) \<in> ?R"
       
  3186     hence "(n1, n2) \<in>tRAG s' \<or> (n1, n2) = (Th th, Th th'')" by auto
       
  3187     hence "(n1, n2) \<in> ?L" 
       
  3188     proof
       
  3189       assume "(n1, n2) \<in> tRAG s'"
       
  3190       moreover have "... \<subseteq> ?L"
       
  3191       proof(rule tRAG_mono)
       
  3192         show "RAG s' \<subseteq> RAG s" by (unfold assms(2), auto)
       
  3193       qed
       
  3194       ultimately show ?thesis by auto
       
  3195     next
       
  3196       assume eq_n: "(n1, n2) = (Th th, Th th'')"
       
  3197       from assms(2, 3) have "(Cs cs, Th th'') \<in> RAG s" by auto
       
  3198       moreover have "(Th th, Cs cs) \<in> RAG s" using assms(2) by auto
       
  3199       ultimately show ?thesis 
       
  3200         by (unfold eq_n tRAG_alt_def, auto)
       
  3201     qed
       
  3202   } ultimately show ?thesis by auto
       
  3203 qed
       
  3204 
       
  3205 context valid_trace
       
  3206 begin
       
  3207 
       
  3208 lemmas RAG_tRAG_transfer = RAG_tRAG_transfer[OF vt]
       
  3209 
       
  3210 end
       
  3211 
       
  3212 lemma cp_alt_def:
       
  3213   "cp s th =  
       
  3214            Max ((the_preced s) ` {th'. Th th' \<in> (subtree (RAG s) (Th th))})"
       
  3215 proof -
       
  3216   have "Max (the_preced s ` ({th} \<union> dependants (wq s) th)) =
       
  3217         Max (the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)})" 
       
  3218           (is "Max (_ ` ?L) = Max (_ ` ?R)")
       
  3219   proof -
       
  3220     have "?L = ?R" 
       
  3221     by (auto dest:rtranclD simp:cs_dependants_def cs_RAG_def s_RAG_def subtree_def)
       
  3222     thus ?thesis by simp
       
  3223   qed
       
  3224   thus ?thesis by (unfold cp_eq_cpreced cpreced_def, fold the_preced_def, simp)
       
  3225 qed
       
  3226 
       
  3227 lemma cp_gen_alt_def:
       
  3228   "cp_gen s = (Max \<circ> (\<lambda>x. (the_preced s \<circ> the_thread) ` subtree (tRAG s) x))"
       
  3229     by (auto simp:cp_gen_def)
       
  3230 
       
  3231 lemma tRAG_nodeE:
       
  3232   assumes "(n1, n2) \<in> tRAG s"
       
  3233   obtains th1 th2 where "n1 = Th th1" "n2 = Th th2"
       
  3234   using assms
       
  3235   by (auto simp: tRAG_def wRAG_def hRAG_def tRAG_def)
       
  3236 
       
  3237 lemma subtree_nodeE:
       
  3238   assumes "n \<in> subtree (tRAG s) (Th th)"
       
  3239   obtains th1 where "n = Th th1"
       
  3240 proof -
       
  3241   show ?thesis
       
  3242   proof(rule subtreeE[OF assms])
       
  3243     assume "n = Th th"
       
  3244     from that[OF this] show ?thesis .
       
  3245   next
       
  3246     assume "Th th \<in> ancestors (tRAG s) n"
       
  3247     hence "(n, Th th) \<in> (tRAG s)^+" by (auto simp:ancestors_def)
       
  3248     hence "\<exists> th1. n = Th th1"
       
  3249     proof(induct)
       
  3250       case (base y)
       
  3251       from tRAG_nodeE[OF this] show ?case by metis
       
  3252     next
       
  3253       case (step y z)
       
  3254       thus ?case by auto
       
  3255     qed
       
  3256     with that show ?thesis by auto
       
  3257   qed
       
  3258 qed
       
  3259 
       
  3260 lemma tRAG_star_RAG: "(tRAG s)^* \<subseteq> (RAG s)^*"
       
  3261 proof -
       
  3262   have "(wRAG s O hRAG s)^* \<subseteq> (RAG s O RAG s)^*" 
       
  3263     by (rule rtrancl_mono, auto simp:RAG_split)
       
  3264   also have "... \<subseteq> ((RAG s)^*)^*"
       
  3265     by (rule rtrancl_mono, auto)
       
  3266   also have "... = (RAG s)^*" by simp
       
  3267   finally show ?thesis by (unfold tRAG_def, simp)
       
  3268 qed
       
  3269 
       
  3270 lemma tRAG_subtree_RAG: "subtree (tRAG s) x \<subseteq> subtree (RAG s) x"
       
  3271 proof -
       
  3272   { fix a
       
  3273     assume "a \<in> subtree (tRAG s) x"
       
  3274     hence "(a, x) \<in> (tRAG s)^*" by (auto simp:subtree_def)
       
  3275     with tRAG_star_RAG[of s]
       
  3276     have "(a, x) \<in> (RAG s)^*" by auto
       
  3277     hence "a \<in> subtree (RAG s) x" by (auto simp:subtree_def)
       
  3278   } thus ?thesis by auto
       
  3279 qed
       
  3280 
       
  3281 lemma tRAG_trancl_eq:
       
  3282    "{th'. (Th th', Th th)  \<in> (tRAG s)^+} = 
       
  3283     {th'. (Th th', Th th)  \<in> (RAG s)^+}"
       
  3284    (is "?L = ?R")
       
  3285 proof -
       
  3286   { fix th'
       
  3287     assume "th' \<in> ?L"
       
  3288     hence "(Th th', Th th) \<in> (tRAG s)^+" by auto
       
  3289     from tranclD[OF this]
       
  3290     obtain z where h: "(Th th', z) \<in> tRAG s" "(z, Th th) \<in> (tRAG s)\<^sup>*" by auto
       
  3291     from tRAG_subtree_RAG[of s] and this(2)
       
  3292     have "(z, Th th) \<in> (RAG s)^*" by (meson subsetCE tRAG_star_RAG) 
       
  3293     moreover from h(1) have "(Th th', z) \<in> (RAG s)^+" using tRAG_alt_def by auto 
       
  3294     ultimately have "th' \<in> ?R"  by auto 
       
  3295   } moreover 
       
  3296   { fix th'
       
  3297     assume "th' \<in> ?R"
       
  3298     hence "(Th th', Th th) \<in> (RAG s)^+" by (auto)
       
  3299     from plus_rpath[OF this]
       
  3300     obtain xs where rp: "rpath (RAG s) (Th th') xs (Th th)" "xs \<noteq> []" by auto
       
  3301     hence "(Th th', Th th) \<in> (tRAG s)^+"
       
  3302     proof(induct xs arbitrary:th' th rule:length_induct)
       
  3303       case (1 xs th' th)
       
  3304       then obtain x1 xs1 where Cons1: "xs = x1#xs1" by (cases xs, auto)
       
  3305       show ?case
       
  3306       proof(cases "xs1")
       
  3307         case Nil
       
  3308         from 1(2)[unfolded Cons1 Nil]
       
  3309         have rp: "rpath (RAG s) (Th th') [x1] (Th th)" .
       
  3310         hence "(Th th', x1) \<in> (RAG s)" by (cases, simp)
       
  3311         then obtain cs where "x1 = Cs cs" 
       
  3312               by (unfold s_RAG_def, auto)
       
  3313         from rpath_nnl_lastE[OF rp[unfolded this]]
       
  3314         show ?thesis by auto
       
  3315       next
       
  3316         case (Cons x2 xs2)
       
  3317         from 1(2)[unfolded Cons1[unfolded this]]
       
  3318         have rp: "rpath (RAG s) (Th th') (x1 # x2 # xs2) (Th th)" .
       
  3319         from rpath_edges_on[OF this]
       
  3320         have eds: "edges_on (Th th' # x1 # x2 # xs2) \<subseteq> RAG s" .
       
  3321         have "(Th th', x1) \<in> edges_on (Th th' # x1 # x2 # xs2)"
       
  3322             by (simp add: edges_on_unfold)
       
  3323         with eds have rg1: "(Th th', x1) \<in> RAG s" by auto
       
  3324         then obtain cs1 where eq_x1: "x1 = Cs cs1" by (unfold s_RAG_def, auto)
       
  3325         have "(x1, x2) \<in> edges_on (Th th' # x1 # x2 # xs2)"
       
  3326             by (simp add: edges_on_unfold)
       
  3327         from this eds
       
  3328         have rg2: "(x1, x2) \<in> RAG s" by auto
       
  3329         from this[unfolded eq_x1] 
       
  3330         obtain th1 where eq_x2: "x2 = Th th1" by (unfold s_RAG_def, auto)
       
  3331         from rg1[unfolded eq_x1] rg2[unfolded eq_x1 eq_x2]
       
  3332         have rt1: "(Th th', Th th1) \<in> tRAG s" by (unfold tRAG_alt_def, auto)
       
  3333         from rp have "rpath (RAG s) x2 xs2 (Th th)"
       
  3334            by  (elim rpath_ConsE, simp)
       
  3335         from this[unfolded eq_x2] have rp': "rpath (RAG s) (Th th1) xs2 (Th th)" .
       
  3336         show ?thesis
       
  3337         proof(cases "xs2 = []")
       
  3338           case True
       
  3339           from rpath_nilE[OF rp'[unfolded this]]
       
  3340           have "th1 = th" by auto
       
  3341           from rt1[unfolded this] show ?thesis by auto
       
  3342         next
       
  3343           case False
       
  3344           from 1(1)[rule_format, OF _ rp' this, unfolded Cons1 Cons]
       
  3345           have "(Th th1, Th th) \<in> (tRAG s)\<^sup>+" by simp
       
  3346           with rt1 show ?thesis by auto
       
  3347         qed
       
  3348       qed
       
  3349     qed
       
  3350     hence "th' \<in> ?L" by auto
       
  3351   } ultimately show ?thesis by blast
       
  3352 qed
       
  3353 
       
  3354 lemma tRAG_trancl_eq_Th:
       
  3355    "{Th th' | th'. (Th th', Th th)  \<in> (tRAG s)^+} = 
       
  3356     {Th th' | th'. (Th th', Th th)  \<in> (RAG s)^+}"
       
  3357     using tRAG_trancl_eq by auto
       
  3358 
       
  3359 lemma dependants_alt_def:
       
  3360   "dependants s th = {th'. (Th th', Th th) \<in> (tRAG s)^+}"
       
  3361   by (metis eq_RAG s_dependants_def tRAG_trancl_eq)
       
  3362   
       
  3363 context valid_trace
       
  3364 begin
       
  3365 
       
  3366 lemma count_eq_tRAG_plus:
       
  3367   assumes "cntP s th = cntV s th"
       
  3368   shows "{th'. (Th th', Th th) \<in> (tRAG s)^+} = {}"
       
  3369   using assms count_eq_dependants dependants_alt_def eq_dependants by auto 
       
  3370 
       
  3371 lemma count_eq_RAG_plus:
       
  3372   assumes "cntP s th = cntV s th"
       
  3373   shows "{th'. (Th th', Th th) \<in> (RAG s)^+} = {}"
       
  3374   using assms count_eq_dependants cs_dependants_def eq_RAG by auto
       
  3375 
       
  3376 lemma count_eq_RAG_plus_Th:
       
  3377   assumes "cntP s th = cntV s th"
       
  3378   shows "{Th th' | th'. (Th th', Th th) \<in> (RAG s)^+} = {}"
       
  3379   using count_eq_RAG_plus[OF assms] by auto
       
  3380 
       
  3381 lemma count_eq_tRAG_plus_Th:
       
  3382   assumes "cntP s th = cntV s th"
       
  3383   shows "{Th th' | th'. (Th th', Th th) \<in> (tRAG s)^+} = {}"
       
  3384    using count_eq_tRAG_plus[OF assms] by auto
       
  3385 
       
  3386 end
       
  3387 
       
  3388 lemma tRAG_subtree_eq: 
       
  3389    "(subtree (tRAG s) (Th th)) = {Th th' | th'. Th th'  \<in> (subtree (RAG s) (Th th))}"
       
  3390    (is "?L = ?R")
       
  3391 proof -
       
  3392   { fix n 
       
  3393     assume h: "n \<in> ?L"
       
  3394     hence "n \<in> ?R"
       
  3395     by (smt mem_Collect_eq subsetCE subtree_def subtree_nodeE tRAG_subtree_RAG) 
       
  3396   } moreover {
       
  3397     fix n
       
  3398     assume "n \<in> ?R"
       
  3399     then obtain th' where h: "n = Th th'" "(Th th', Th th) \<in> (RAG s)^*"
       
  3400       by (auto simp:subtree_def)
       
  3401     from rtranclD[OF this(2)]
       
  3402     have "n \<in> ?L"
       
  3403     proof
       
  3404       assume "Th th' \<noteq> Th th \<and> (Th th', Th th) \<in> (RAG s)\<^sup>+"
       
  3405       with h have "n \<in> {Th th' | th'. (Th th', Th th)  \<in> (RAG s)^+}" by auto
       
  3406       thus ?thesis using subtree_def tRAG_trancl_eq by fastforce
       
  3407     qed (insert h, auto simp:subtree_def)
       
  3408   } ultimately show ?thesis by auto
       
  3409 qed
       
  3410 
       
  3411 lemma threads_set_eq: 
       
  3412    "the_thread ` (subtree (tRAG s) (Th th)) = 
       
  3413                   {th'. Th th' \<in> (subtree (RAG s) (Th th))}" (is "?L = ?R")
       
  3414    by (auto intro:rev_image_eqI simp:tRAG_subtree_eq)
       
  3415 
       
  3416 lemma cp_alt_def1: 
       
  3417   "cp s th = Max ((the_preced s o the_thread) ` (subtree (tRAG s) (Th th)))"
       
  3418 proof -
       
  3419   have "(the_preced s ` the_thread ` subtree (tRAG s) (Th th)) =
       
  3420        ((the_preced s \<circ> the_thread) ` subtree (tRAG s) (Th th))"
       
  3421        by auto
       
  3422   thus ?thesis by (unfold cp_alt_def, fold threads_set_eq, auto)
       
  3423 qed
       
  3424 
       
  3425 lemma cp_gen_def_cond: 
       
  3426   assumes "x = Th th"
       
  3427   shows "cp s th = cp_gen s (Th th)"
       
  3428 by (unfold cp_alt_def1 cp_gen_def, simp)
       
  3429 
       
  3430 lemma cp_gen_over_set:
       
  3431   assumes "\<forall> x \<in> A. \<exists> th. x = Th th"
       
  3432   shows "cp_gen s ` A = (cp s \<circ> the_thread) ` A"
       
  3433 proof(rule f_image_eq)
       
  3434   fix a
       
  3435   assume "a \<in> A"
       
  3436   from assms[rule_format, OF this]
       
  3437   obtain th where eq_a: "a = Th th" by auto
       
  3438   show "cp_gen s a = (cp s \<circ> the_thread) a"
       
  3439     by  (unfold eq_a, simp, unfold cp_gen_def_cond[OF refl[of "Th th"]], simp)
       
  3440 qed
       
  3441 
       
  3442 
       
  3443 context valid_trace
       
  3444 begin
       
  3445 
       
  3446 lemma RAG_threads:
       
  3447   assumes "(Th th) \<in> Field (RAG s)"
       
  3448   shows "th \<in> threads s"
       
  3449   using assms
       
  3450   by (metis Field_def UnE dm_RAG_threads range_in vt)
       
  3451 
       
  3452 lemma subtree_tRAG_thread:
       
  3453   assumes "th \<in> threads s"
       
  3454   shows "subtree (tRAG s) (Th th) \<subseteq> Th ` threads s" (is "?L \<subseteq> ?R")
       
  3455 proof -
       
  3456   have "?L = {Th th' |th'. Th th' \<in> subtree (RAG s) (Th th)}"
       
  3457     by (unfold tRAG_subtree_eq, simp)
       
  3458   also have "... \<subseteq> ?R"
       
  3459   proof
       
  3460     fix x
       
  3461     assume "x \<in> {Th th' |th'. Th th' \<in> subtree (RAG s) (Th th)}"
       
  3462     then obtain th' where h: "x = Th th'" "Th th' \<in> subtree (RAG s) (Th th)" by auto
       
  3463     from this(2)
       
  3464     show "x \<in> ?R"
       
  3465     proof(cases rule:subtreeE)
       
  3466       case 1
       
  3467       thus ?thesis by (simp add: assms h(1)) 
       
  3468     next
       
  3469       case 2
       
  3470       thus ?thesis by (metis ancestors_Field dm_RAG_threads h(1) image_eqI) 
       
  3471     qed
       
  3472   qed
       
  3473   finally show ?thesis .
       
  3474 qed
       
  3475 
       
  3476 lemma readys_root:
       
  3477   assumes "th \<in> readys s"
       
  3478   shows "root (RAG s) (Th th)"
       
  3479 proof -
       
  3480   { fix x
       
  3481     assume "x \<in> ancestors (RAG s) (Th th)"
       
  3482     hence h: "(Th th, x) \<in> (RAG s)^+" by (auto simp:ancestors_def)
       
  3483     from tranclD[OF this]
       
  3484     obtain z where "(Th th, z) \<in> RAG s" by auto
       
  3485     with assms(1) have False
       
  3486          apply (case_tac z, auto simp:readys_def s_RAG_def s_waiting_def cs_waiting_def)
       
  3487          by (fold wq_def, blast)
       
  3488   } thus ?thesis by (unfold root_def, auto)
       
  3489 qed
       
  3490 
       
  3491 lemma readys_in_no_subtree:
       
  3492   assumes "th \<in> readys s"
       
  3493   and "th' \<noteq> th"
       
  3494   shows "Th th \<notin> subtree (RAG s) (Th th')" 
       
  3495 proof
       
  3496    assume "Th th \<in> subtree (RAG s) (Th th')"
       
  3497    thus False
       
  3498    proof(cases rule:subtreeE)
       
  3499       case 1
       
  3500       with assms show ?thesis by auto
       
  3501    next
       
  3502       case 2
       
  3503       with readys_root[OF assms(1)]
       
  3504       show ?thesis by (auto simp:root_def)
       
  3505    qed
       
  3506 qed
       
  3507 
       
  3508 lemma not_in_thread_isolated:
       
  3509   assumes "th \<notin> threads s"
       
  3510   shows "(Th th) \<notin> Field (RAG s)"
       
  3511 proof
       
  3512   assume "(Th th) \<in> Field (RAG s)"
       
  3513   with dm_RAG_threads and range_in assms
       
  3514   show False by (unfold Field_def, blast)
       
  3515 qed
       
  3516 
       
  3517 lemma wf_RAG: "wf (RAG s)"
       
  3518 proof(rule finite_acyclic_wf)
       
  3519   from finite_RAG show "finite (RAG s)" .
       
  3520 next
       
  3521   from acyclic_RAG show "acyclic (RAG s)" .
       
  3522 qed
       
  3523 
       
  3524 lemma sgv_wRAG: "single_valued (wRAG s)"
       
  3525   using waiting_unique
       
  3526   by (unfold single_valued_def wRAG_def, auto)
       
  3527 
       
  3528 lemma sgv_hRAG: "single_valued (hRAG s)"
       
  3529   using holding_unique 
       
  3530   by (unfold single_valued_def hRAG_def, auto)
       
  3531 
       
  3532 lemma sgv_tRAG: "single_valued (tRAG s)"
       
  3533   by (unfold tRAG_def, rule single_valued_relcomp, 
       
  3534               insert sgv_wRAG sgv_hRAG, auto)
       
  3535 
       
  3536 lemma acyclic_tRAG: "acyclic (tRAG s)"
       
  3537 proof(unfold tRAG_def, rule acyclic_compose)
       
  3538   show "acyclic (RAG s)" using acyclic_RAG .
       
  3539 next
       
  3540   show "wRAG s \<subseteq> RAG s" unfolding RAG_split by auto
       
  3541 next
       
  3542   show "hRAG s \<subseteq> RAG s" unfolding RAG_split by auto
       
  3543 qed
       
  3544 
       
  3545 lemma sgv_RAG: "single_valued (RAG s)"
       
  3546   using unique_RAG by (auto simp:single_valued_def)
       
  3547 
       
  3548 lemma rtree_RAG: "rtree (RAG s)"
       
  3549   using sgv_RAG acyclic_RAG
       
  3550   by (unfold rtree_def rtree_axioms_def sgv_def, auto)
       
  3551 
       
  3552 end
       
  3553 
       
  3554 sublocale valid_trace < rtree_RAG: rtree "RAG s"
       
  3555 proof
       
  3556   show "single_valued (RAG s)"
       
  3557   apply (intro_locales)
       
  3558     by (unfold single_valued_def, 
       
  3559         auto intro:unique_RAG)
       
  3560 
       
  3561   show "acyclic (RAG s)"
       
  3562      by (rule acyclic_RAG)
       
  3563 qed
       
  3564 
       
  3565 sublocale valid_trace < rtree_s: rtree "tRAG s"
       
  3566 proof(unfold_locales)
       
  3567   from sgv_tRAG show "single_valued (tRAG s)" .
       
  3568 next
       
  3569   from acyclic_tRAG show "acyclic (tRAG s)" .
       
  3570 qed
       
  3571 
       
  3572 sublocale valid_trace < fsbtRAGs : fsubtree "RAG s"
       
  3573 proof -
       
  3574   show "fsubtree (RAG s)"
       
  3575   proof(intro_locales)
       
  3576     show "fbranch (RAG s)" using finite_fbranchI[OF finite_RAG] .
       
  3577   next
       
  3578     show "fsubtree_axioms (RAG s)"
       
  3579     proof(unfold fsubtree_axioms_def)
       
  3580       from wf_RAG show "wf (RAG s)" .
       
  3581     qed
       
  3582   qed
       
  3583 qed
       
  3584 
       
  3585 sublocale valid_trace < fsbttRAGs: fsubtree "tRAG s"
       
  3586 proof -
       
  3587   have "fsubtree (tRAG s)"
       
  3588   proof -
       
  3589     have "fbranch (tRAG s)"
       
  3590     proof(unfold tRAG_def, rule fbranch_compose)
       
  3591         show "fbranch (wRAG s)"
       
  3592         proof(rule finite_fbranchI)
       
  3593            from finite_RAG show "finite (wRAG s)"
       
  3594            by (unfold RAG_split, auto)
       
  3595         qed
       
  3596     next
       
  3597         show "fbranch (hRAG s)"
       
  3598         proof(rule finite_fbranchI)
       
  3599            from finite_RAG 
       
  3600            show "finite (hRAG s)" by (unfold RAG_split, auto)
       
  3601         qed
       
  3602     qed
       
  3603     moreover have "wf (tRAG s)"
       
  3604     proof(rule wf_subset)
       
  3605       show "wf (RAG s O RAG s)" using wf_RAG
       
  3606         by (fold wf_comp_self, simp)
       
  3607     next
       
  3608       show "tRAG s \<subseteq> (RAG s O RAG s)"
       
  3609         by (unfold tRAG_alt_def, auto)
       
  3610     qed
       
  3611     ultimately show ?thesis
       
  3612       by (unfold fsubtree_def fsubtree_axioms_def,auto)
       
  3613   qed
       
  3614   from this[folded tRAG_def] show "fsubtree (tRAG s)" .
       
  3615 qed
       
  3616 
       
  3617 lemma Max_UNION: 
       
  3618   assumes "finite A"
       
  3619   and "A \<noteq> {}"
       
  3620   and "\<forall> M \<in> f ` A. finite M"
       
  3621   and "\<forall> M \<in> f ` A. M \<noteq> {}"
       
  3622   shows "Max (\<Union>x\<in> A. f x) = Max (Max ` f ` A)" (is "?L = ?R")
       
  3623   using assms[simp]
       
  3624 proof -
       
  3625   have "?L = Max (\<Union>(f ` A))"
       
  3626     by (fold Union_image_eq, simp)
       
  3627   also have "... = ?R"
       
  3628     by (subst Max_Union, simp+)
       
  3629   finally show ?thesis .
       
  3630 qed
       
  3631 
       
  3632 lemma max_Max_eq:
       
  3633   assumes "finite A"
       
  3634     and "A \<noteq> {}"
       
  3635     and "x = y"
       
  3636   shows "max x (Max A) = Max ({y} \<union> A)" (is "?L = ?R")
       
  3637 proof -
       
  3638   have "?R = Max (insert y A)" by simp
       
  3639   also from assms have "... = ?L"
       
  3640       by (subst Max.insert, simp+)
       
  3641   finally show ?thesis by simp
       
  3642 qed
       
  3643 
       
  3644 context valid_trace
       
  3645 begin
       
  3646 
       
  3647 (* ddd *)
       
  3648 lemma cp_gen_rec:
       
  3649   assumes "x = Th th"
       
  3650   shows "cp_gen s x = Max ({the_preced s th} \<union> (cp_gen s) ` children (tRAG s) x)"
       
  3651 proof(cases "children (tRAG s) x = {}")
       
  3652   case True
       
  3653   show ?thesis
       
  3654     by (unfold True cp_gen_def subtree_children, simp add:assms)
       
  3655 next
       
  3656   case False
       
  3657   hence [simp]: "children (tRAG s) x \<noteq> {}" by auto
       
  3658   note fsbttRAGs.finite_subtree[simp]
       
  3659   have [simp]: "finite (children (tRAG s) x)"
       
  3660      by (intro rev_finite_subset[OF fsbttRAGs.finite_subtree], 
       
  3661             rule children_subtree)
       
  3662   { fix r x
       
  3663     have "subtree r x \<noteq> {}" by (auto simp:subtree_def)
       
  3664   } note this[simp]
       
  3665   have [simp]: "\<exists>x\<in>children (tRAG s) x. subtree (tRAG s) x \<noteq> {}"
       
  3666   proof -
       
  3667     from False obtain q where "q \<in> children (tRAG s) x" by blast
       
  3668     moreover have "subtree (tRAG s) q \<noteq> {}" by simp
       
  3669     ultimately show ?thesis by blast
       
  3670   qed
       
  3671   have h: "Max ((the_preced s \<circ> the_thread) `
       
  3672                 ({x} \<union> \<Union>(subtree (tRAG s) ` children (tRAG s) x))) =
       
  3673         Max ({the_preced s th} \<union> cp_gen s ` children (tRAG s) x)"
       
  3674                      (is "?L = ?R")
       
  3675   proof -
       
  3676     let "Max (?f ` (?A \<union> \<Union> (?g ` ?B)))" = ?L
       
  3677     let "Max (_ \<union> (?h ` ?B))" = ?R
       
  3678     let ?L1 = "?f ` \<Union>(?g ` ?B)"
       
  3679     have eq_Max_L1: "Max ?L1 = Max (?h ` ?B)"
       
  3680     proof -
       
  3681       have "?L1 = ?f ` (\<Union> x \<in> ?B.(?g x))" by simp
       
  3682       also have "... =  (\<Union> x \<in> ?B. ?f ` (?g x))" by auto
       
  3683       finally have "Max ?L1 = Max ..." by simp
       
  3684       also have "... = Max (Max ` (\<lambda>x. ?f ` subtree (tRAG s) x) ` ?B)"
       
  3685         by (subst Max_UNION, simp+)
       
  3686       also have "... = Max (cp_gen s ` children (tRAG s) x)"
       
  3687           by (unfold image_comp cp_gen_alt_def, simp)
       
  3688       finally show ?thesis .
       
  3689     qed
       
  3690     show ?thesis
       
  3691     proof -
       
  3692       have "?L = Max (?f ` ?A \<union> ?L1)" by simp
       
  3693       also have "... = max (the_preced s (the_thread x)) (Max ?L1)"
       
  3694             by (subst Max_Un, simp+)
       
  3695       also have "... = max (?f x) (Max (?h ` ?B))"
       
  3696         by (unfold eq_Max_L1, simp)
       
  3697       also have "... =?R"
       
  3698         by (rule max_Max_eq, (simp)+, unfold assms, simp)
       
  3699       finally show ?thesis .
       
  3700     qed
       
  3701   qed  thus ?thesis 
       
  3702           by (fold h subtree_children, unfold cp_gen_def, simp) 
       
  3703 qed
       
  3704 
       
  3705 lemma cp_rec:
       
  3706   "cp s th = Max ({the_preced s th} \<union> 
       
  3707                      (cp s o the_thread) ` children (tRAG s) (Th th))"
       
  3708 proof -
       
  3709   have "Th th = Th th" by simp
       
  3710   note h =  cp_gen_def_cond[OF this] cp_gen_rec[OF this]
       
  3711   show ?thesis 
       
  3712   proof -
       
  3713     have "cp_gen s ` children (tRAG s) (Th th) = 
       
  3714                 (cp s \<circ> the_thread) ` children (tRAG s) (Th th)"
       
  3715     proof(rule cp_gen_over_set)
       
  3716       show " \<forall>x\<in>children (tRAG s) (Th th). \<exists>th. x = Th th"
       
  3717         by (unfold tRAG_alt_def, auto simp:children_def)
       
  3718     qed
       
  3719     thus ?thesis by (subst (1) h(1), unfold h(2), simp)
       
  3720   qed
       
  3721 qed
       
  3722 
       
  3723 end
       
  3724 
       
  3725 (* keep *)
       
  3726 lemma next_th_holding:
       
  3727   assumes vt: "vt s"
       
  3728   and nxt: "next_th s th cs th'"
       
  3729   shows "holding (wq s) th cs"
       
  3730 proof -
       
  3731   from nxt[unfolded next_th_def]
       
  3732   obtain rest where h: "wq s cs = th # rest"
       
  3733                        "rest \<noteq> []" 
       
  3734                        "th' = hd (SOME q. distinct q \<and> set q = set rest)" by auto
       
  3735   thus ?thesis
       
  3736     by (unfold cs_holding_def, auto)
       
  3737 qed
       
  3738 
       
  3739 context valid_trace
       
  3740 begin
       
  3741 
       
  3742 lemma next_th_waiting:
       
  3743   assumes nxt: "next_th s th cs th'"
       
  3744   shows "waiting (wq s) th' cs"
       
  3745 proof -
       
  3746   from nxt[unfolded next_th_def]
       
  3747   obtain rest where h: "wq s cs = th # rest"
       
  3748                        "rest \<noteq> []" 
       
  3749                        "th' = hd (SOME q. distinct q \<and> set q = set rest)" by auto
       
  3750   from wq_distinct[of cs, unfolded h]
       
  3751   have dst: "distinct (th # rest)" .
       
  3752   have in_rest: "th' \<in> set rest"
       
  3753   proof(unfold h, rule someI2)
       
  3754     show "distinct rest \<and> set rest = set rest" using dst by auto
       
  3755   next
       
  3756     fix x assume "distinct x \<and> set x = set rest"
       
  3757     with h(2)
       
  3758     show "hd x \<in> set (rest)" by (cases x, auto)
       
  3759   qed
       
  3760   hence "th' \<in> set (wq s cs)" by (unfold h(1), auto)
       
  3761   moreover have "th' \<noteq> hd (wq s cs)"
       
  3762     by (unfold h(1), insert in_rest dst, auto)
       
  3763   ultimately show ?thesis by (auto simp:cs_waiting_def)
       
  3764 qed
       
  3765 
       
  3766 lemma next_th_RAG:
       
  3767   assumes nxt: "next_th (s::event list) th cs th'"
       
  3768   shows "{(Cs cs, Th th), (Th th', Cs cs)} \<subseteq> RAG s"
       
  3769   using vt assms next_th_holding next_th_waiting
       
  3770   by (unfold s_RAG_def, simp)
       
  3771 
       
  3772 end
       
  3773 
       
  3774 -- {* A useless definition *}
       
  3775 definition cps:: "state \<Rightarrow> (thread \<times> precedence) set"
       
  3776 where "cps s = {(th, cp s th) | th . th \<in> threads s}"
       
  3777 
       
  3778 
       
  3779 find_theorems "waiting" holding
       
  3780 context valid_trace
       
  3781 begin
       
  3782 
       
  3783 find_theorems "waiting" holding
       
  3784 
       
  3785 end
       
  3786 
       
  3787 end