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