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