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