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