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