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