CpsG.thy~
changeset 62 031d2ae9c9b8
parent 61 f8194fd6214f
child 63 b620a2a0806a
equal deleted inserted replaced
61:f8194fd6214f 62:031d2ae9c9b8
     4 *}
     4 *}
     5 theory CpsG
     5 theory CpsG
     6 imports PrioG Max RTree
     6 imports PrioG Max RTree
     7 begin
     7 begin
     8 
     8 
       
     9 text {* @{text "the_preced"} is also the same as @{text "preced"}, the only
       
    10        difference is the order of arguemts. *}
     9 definition "the_preced s th = preced th s"
    11 definition "the_preced s th = preced th s"
    10 
    12 
       
    13 text {* @{term "the_thread"} extracts thread out of RAG node. *}
    11 fun the_thread :: "node \<Rightarrow> thread" where
    14 fun the_thread :: "node \<Rightarrow> thread" where
    12    "the_thread (Th th) = th"
    15    "the_thread (Th th) = th"
    13 
    16 
       
    17 text {* The following @{text "wRAG"} is the waiting sub-graph of @{text "RAG"}. *}
    14 definition "wRAG (s::state) = {(Th th, Cs cs) | th cs. waiting s th cs}"
    18 definition "wRAG (s::state) = {(Th th, Cs cs) | th cs. waiting s th cs}"
    15 
    19 
       
    20 text {* The following @{text "hRAG"} is the holding sub-graph of @{text "RAG"}. *}
    16 definition "hRAG (s::state) =  {(Cs cs, Th th) | th cs. holding s th cs}"
    21 definition "hRAG (s::state) =  {(Cs cs, Th th) | th cs. holding s th cs}"
    17 
    22 
    18 definition "tRAG s = wRAG s O hRAG s"
    23 text {* The following lemma splits @{term "RAG"} graph into the above two sub-graphs. *}
    19 
       
    20 definition "cp_gen s x =
       
    21                   Max ((the_preced s \<circ> the_thread) ` subtree (tRAG s) x)"
       
    22 (* ccc *)
       
    23 
       
    24 lemma RAG_split: "RAG s = (wRAG s \<union> hRAG s)"
    24 lemma RAG_split: "RAG s = (wRAG s \<union> hRAG s)"
    25   by (unfold s_RAG_abv wRAG_def hRAG_def s_waiting_abv 
    25   by (unfold s_RAG_abv wRAG_def hRAG_def s_waiting_abv 
    26              s_holding_abv cs_RAG_def, auto)
    26              s_holding_abv cs_RAG_def, auto)
       
    27 
       
    28 text {* 
       
    29   The following @{text "tRAG"} is the thread-graph derived from @{term "RAG"}.
       
    30   It characterizes the dependency between threads when calculating current
       
    31   precedences. It is defined as the composition of the above two sub-graphs, 
       
    32   names @{term "wRAG"} and @{term "hRAG"}.
       
    33  *}
       
    34 definition "tRAG s = wRAG s O hRAG s"
       
    35 
       
    36 (* ccc *)
       
    37 
       
    38 definition "cp_gen s x =
       
    39                   Max ((the_preced s \<circ> the_thread) ` subtree (tRAG s) x)"
    27 
    40 
    28 lemma tRAG_alt_def: 
    41 lemma tRAG_alt_def: 
    29   "tRAG s = {(Th th1, Th th2) | th1 th2. 
    42   "tRAG s = {(Th th1, Th th2) | th1 th2. 
    30                   \<exists> cs. (Th th1, Cs cs) \<in> RAG s \<and> (Cs cs, Th th2) \<in> RAG s}"
    43                   \<exists> cs. (Th th1, Cs cs) \<in> RAG s \<and> (Cs cs, Th th2) \<in> RAG s}"
    31  by (auto simp:tRAG_def RAG_split wRAG_def hRAG_def)
    44  by (auto simp:tRAG_def RAG_split wRAG_def hRAG_def)
   361   from finite_RAG[OF vt] show "finite (RAG s)" .
   374   from finite_RAG[OF vt] show "finite (RAG s)" .
   362 next
   375 next
   363   from acyclic_RAG[OF vt] show "acyclic (RAG s)" .
   376   from acyclic_RAG[OF vt] show "acyclic (RAG s)" .
   364 qed
   377 qed
   365 
   378 
   366 end
       
   367 
       
   368 context valid_trace
       
   369 begin
       
   370 
       
   371 lemma sgv_wRAG: "single_valued (wRAG s)"
   379 lemma sgv_wRAG: "single_valued (wRAG s)"
   372   using waiting_unique[OF vt] 
   380   using waiting_unique[OF vt] 
   373   by (unfold single_valued_def wRAG_def, auto)
   381   by (unfold single_valued_def wRAG_def, auto)
   374 
   382 
   375 lemma sgv_hRAG: "single_valued (hRAG s)"
   383 lemma sgv_hRAG: "single_valued (hRAG s)"
   393   using unique_RAG[OF vt] by (auto simp:single_valued_def)
   401   using unique_RAG[OF vt] by (auto simp:single_valued_def)
   394 
   402 
   395 lemma rtree_RAG: "rtree (RAG s)"
   403 lemma rtree_RAG: "rtree (RAG s)"
   396   using sgv_RAG acyclic_RAG[OF vt]
   404   using sgv_RAG acyclic_RAG[OF vt]
   397   by (unfold rtree_def rtree_axioms_def sgv_def, auto)
   405   by (unfold rtree_def rtree_axioms_def sgv_def, auto)
   398 
       
   399 end
   406 end
       
   407 
       
   408 
       
   409 sublocale valid_trace < rtree_RAG: rtree "RAG s"
       
   410 proof
       
   411   show "single_valued (RAG s)"
       
   412   apply (intro_locales)
       
   413     by (unfold single_valued_def, 
       
   414         auto intro:unique_RAG[OF vt])
       
   415 
       
   416   show "acyclic (RAG s)"
       
   417      by (rule acyclic_RAG[OF vt])
       
   418 qed
   400 
   419 
   401 sublocale valid_trace < rtree_s: rtree "tRAG s"
   420 sublocale valid_trace < rtree_s: rtree "tRAG s"
   402 proof(unfold_locales)
   421 proof(unfold_locales)
   403   from sgv_tRAG show "single_valued (tRAG s)" .
   422   from sgv_tRAG show "single_valued (tRAG s)" .
   404 next
   423 next
   475   have "?R = Max (insert y A)" by simp
   494   have "?R = Max (insert y A)" by simp
   476   also from assms have "... = ?L"
   495   also from assms have "... = ?L"
   477       by (subst Max.insert, simp+)
   496       by (subst Max.insert, simp+)
   478   finally show ?thesis by simp
   497   finally show ?thesis by simp
   479 qed
   498 qed
   480 
       
   481 
   499 
   482 context valid_trace
   500 context valid_trace
   483 begin
   501 begin
   484 
   502 
   485 (* ddd *)
   503 (* ddd *)
   558   qed
   576   qed
   559 qed
   577 qed
   560 
   578 
   561 end
   579 end
   562 
   580 
   563 (* ccc *)
   581 (* keep *)
   564 
       
   565 
       
   566 
       
   567 (* obvious lemma *)
       
   568 
       
   569 lemma wf_RAG:
       
   570   assumes vt: "vt s"
       
   571   shows "wf (RAG s)"
       
   572 proof(rule finite_acyclic_wf)
       
   573   from finite_RAG[OF vt] show "finite (RAG s)" .
       
   574 next
       
   575   from acyclic_RAG[OF vt] show "acyclic (RAG s)" .
       
   576 qed
       
   577 
       
   578 definition child :: "state \<Rightarrow> (node \<times> node) set"
       
   579   where "child s \<equiv>
       
   580             {(Th th', Th th) | th th'. \<exists>cs. (Th th', Cs cs) \<in> RAG s \<and> (Cs cs, Th th) \<in> RAG s}"
       
   581 
       
   582 definition children :: "state \<Rightarrow> thread \<Rightarrow> thread set"
       
   583   where "children s th \<equiv> {th'. (Th th', Th th) \<in> child s}"
       
   584 
       
   585 lemma children_def2:
       
   586   "children s th \<equiv> {th'. \<exists> cs. (Th th', Cs cs) \<in> RAG s \<and> (Cs cs, Th th) \<in> RAG s}"
       
   587 unfolding child_def children_def by simp
       
   588 
       
   589 lemma children_dependants: 
       
   590   "children s th \<subseteq> dependants (wq s) th"
       
   591   unfolding children_def2
       
   592   unfolding cs_dependants_def
       
   593   by (auto simp add: eq_RAG)
       
   594 
       
   595 lemma child_unique:
       
   596   assumes vt: "vt s"
       
   597   and ch1: "(Th th, Th th1) \<in> child s"
       
   598   and ch2: "(Th th, Th th2) \<in> child s"
       
   599   shows "th1 = th2"
       
   600 using ch1 ch2 
       
   601 proof(unfold child_def, clarsimp)
       
   602   fix cs csa
       
   603   assume h1: "(Th th, Cs cs) \<in> RAG s"
       
   604     and h2: "(Cs cs, Th th1) \<in> RAG s"
       
   605     and h3: "(Th th, Cs csa) \<in> RAG s"
       
   606     and h4: "(Cs csa, Th th2) \<in> RAG s"
       
   607   from unique_RAG[OF vt h1 h3] have "cs = csa" by simp
       
   608   with h4 have "(Cs cs, Th th2) \<in> RAG s" by simp
       
   609   from unique_RAG[OF vt h2 this]
       
   610   show "th1 = th2" by simp
       
   611 qed 
       
   612 
       
   613 lemma RAG_children:
       
   614   assumes h: "(Th th1, Th th2) \<in> (RAG s)^+"
       
   615   shows "th1 \<in> children s th2 \<or> (\<exists> th3. th3 \<in> children s th2 \<and> (Th th1, Th th3) \<in> (RAG s)^+)"
       
   616 proof -
       
   617   from h show ?thesis
       
   618   proof(induct rule: tranclE)
       
   619     fix c th2
       
   620     assume h1: "(Th th1, c) \<in> (RAG s)\<^sup>+"
       
   621     and h2: "(c, Th th2) \<in> RAG s"
       
   622     from h2 obtain cs where eq_c: "c = Cs cs"
       
   623       by (case_tac c, auto simp:s_RAG_def)
       
   624     show "th1 \<in> children s th2 \<or> (\<exists>th3. th3 \<in> children s th2 \<and> (Th th1, Th th3) \<in> (RAG s)\<^sup>+)"
       
   625     proof(rule tranclE[OF h1])
       
   626       fix ca
       
   627       assume h3: "(Th th1, ca) \<in> (RAG s)\<^sup>+"
       
   628         and h4: "(ca, c) \<in> RAG s"
       
   629       show "th1 \<in> children s th2 \<or> (\<exists>th3. th3 \<in> children s th2 \<and> (Th th1, Th th3) \<in> (RAG s)\<^sup>+)"
       
   630       proof -
       
   631         from eq_c and h4 obtain th3 where eq_ca: "ca = Th th3"
       
   632           by (case_tac ca, auto simp:s_RAG_def)
       
   633         from eq_ca h4 h2 eq_c
       
   634         have "th3 \<in> children s th2" by (auto simp:children_def child_def)
       
   635         moreover from h3 eq_ca have "(Th th1, Th th3) \<in> (RAG s)\<^sup>+" by simp
       
   636         ultimately show ?thesis by auto
       
   637       qed
       
   638     next
       
   639       assume "(Th th1, c) \<in> RAG s"
       
   640       with h2 eq_c
       
   641       have "th1 \<in> children s th2"
       
   642         by (auto simp:children_def child_def)
       
   643       thus ?thesis by auto
       
   644     qed
       
   645   next
       
   646     assume "(Th th1, Th th2) \<in> RAG s"
       
   647     thus ?thesis
       
   648       by (auto simp:s_RAG_def)
       
   649   qed
       
   650 qed
       
   651 
       
   652 lemma sub_child: "child s \<subseteq> (RAG s)^+"
       
   653   by (unfold child_def, auto)
       
   654 
       
   655 lemma wf_child: 
       
   656   assumes vt: "vt s"
       
   657   shows "wf (child s)"
       
   658 apply(rule wf_subset)
       
   659 apply(rule wf_trancl[OF wf_RAG[OF vt]])
       
   660 apply(rule sub_child)
       
   661 done
       
   662 
       
   663 lemma RAG_child_pre:
       
   664   assumes vt: "vt s"
       
   665   shows
       
   666   "(Th th, n) \<in> (RAG s)^+ \<longrightarrow> (\<forall> th'. n = (Th th') \<longrightarrow> (Th th, Th th') \<in> (child s)^+)" (is "?P n")
       
   667 proof -
       
   668   from wf_trancl[OF wf_RAG[OF vt]]
       
   669   have wf: "wf ((RAG s)^+)" .
       
   670   show ?thesis
       
   671   proof(rule wf_induct[OF wf, of ?P], clarsimp)
       
   672     fix th'
       
   673     assume ih[rule_format]: "\<forall>y. (y, Th th') \<in> (RAG s)\<^sup>+ \<longrightarrow>
       
   674                (Th th, y) \<in> (RAG s)\<^sup>+ \<longrightarrow> (\<forall>th'. y = Th th' \<longrightarrow> (Th th, Th th') \<in> (child s)\<^sup>+)"
       
   675     and h: "(Th th, Th th') \<in> (RAG s)\<^sup>+"
       
   676     show "(Th th, Th th') \<in> (child s)\<^sup>+"
       
   677     proof -
       
   678       from RAG_children[OF h]
       
   679       have "th \<in> children s th' \<or> (\<exists>th3. th3 \<in> children s th' \<and> (Th th, Th th3) \<in> (RAG s)\<^sup>+)" .
       
   680       thus ?thesis
       
   681       proof
       
   682         assume "th \<in> children s th'"
       
   683         thus "(Th th, Th th') \<in> (child s)\<^sup>+" by (auto simp:children_def)
       
   684       next
       
   685         assume "\<exists>th3. th3 \<in> children s th' \<and> (Th th, Th th3) \<in> (RAG s)\<^sup>+"
       
   686         then obtain th3 where th3_in: "th3 \<in> children s th'" 
       
   687           and th_dp: "(Th th, Th th3) \<in> (RAG s)\<^sup>+" by auto
       
   688         from th3_in have "(Th th3, Th th') \<in> (RAG s)^+" by (auto simp:children_def child_def)
       
   689         from ih[OF this th_dp, of th3] have "(Th th, Th th3) \<in> (child s)\<^sup>+" by simp
       
   690         with th3_in show "(Th th, Th th') \<in> (child s)\<^sup>+" by (auto simp:children_def)
       
   691       qed
       
   692     qed
       
   693   qed
       
   694 qed
       
   695 
       
   696 lemma RAG_child: "\<lbrakk>vt s; (Th th, Th th') \<in> (RAG s)^+\<rbrakk> \<Longrightarrow> (Th th, Th th') \<in> (child s)^+"
       
   697   by (insert RAG_child_pre, auto)
       
   698 
       
   699 lemma child_RAG_p:
       
   700   assumes "(n1, n2) \<in> (child s)^+"
       
   701   shows "(n1, n2) \<in> (RAG s)^+"
       
   702 proof -
       
   703   from assms show ?thesis
       
   704   proof(induct)
       
   705     case (base y)
       
   706     with sub_child show ?case by auto
       
   707   next
       
   708     case (step y z)
       
   709     assume "(y, z) \<in> child s"
       
   710     with sub_child have "(y, z) \<in> (RAG s)^+" by auto
       
   711     moreover have "(n1, y) \<in> (RAG s)^+" by fact
       
   712     ultimately show ?case by auto
       
   713   qed
       
   714 qed
       
   715 
       
   716 text {* (* ddd *)
       
   717 *}
       
   718 lemma child_RAG_eq: 
       
   719   assumes vt: "vt s"
       
   720   shows "(Th th1, Th th2) \<in> (child s)^+  \<longleftrightarrow> (Th th1, Th th2) \<in> (RAG s)^+"
       
   721   by (auto intro: RAG_child[OF vt] child_RAG_p)
       
   722 
       
   723 text {* (* ddd *)
       
   724 *}
       
   725 lemma children_no_dep:
       
   726   fixes s th th1 th2 th3
       
   727   assumes vt: "vt s"
       
   728   and ch1: "(Th th1, Th th) \<in> child s"
       
   729   and ch2: "(Th th2, Th th) \<in> child s"
       
   730   and ch3: "(Th th1, Th th2) \<in> (RAG s)^+"
       
   731   shows "False"
       
   732 proof -
       
   733   from RAG_child[OF vt ch3]
       
   734   have "(Th th1, Th th2) \<in> (child s)\<^sup>+" .
       
   735   thus ?thesis
       
   736   proof(rule converse_tranclE)
       
   737     assume "(Th th1, Th th2) \<in> child s"
       
   738     from child_unique[OF vt ch1 this] have "th = th2" by simp
       
   739     with ch2 have "(Th th2, Th th2) \<in> child s" by simp
       
   740     with wf_child[OF vt] show ?thesis by auto
       
   741   next
       
   742     fix c
       
   743     assume h1: "(Th th1, c) \<in> child s"
       
   744       and h2: "(c, Th th2) \<in> (child s)\<^sup>+"
       
   745     from h1 obtain th3 where eq_c: "c = Th th3" by (unfold child_def, auto)
       
   746     with h1 have "(Th th1, Th th3) \<in> child s" by simp
       
   747     from child_unique[OF vt ch1 this] have eq_th3: "th3 = th" by simp
       
   748     with eq_c and h2 have "(Th th, Th th2) \<in> (child s)\<^sup>+" by simp
       
   749     with ch2 have "(Th th, Th th) \<in> (child s)\<^sup>+" by auto
       
   750     moreover have "wf ((child s)\<^sup>+)"
       
   751     proof(rule wf_trancl)
       
   752       from wf_child[OF vt] show "wf (child s)" .
       
   753     qed
       
   754     ultimately show False by auto
       
   755   qed
       
   756 qed
       
   757 
       
   758 text {* (* ddd *)
       
   759 *}
       
   760 lemma unique_RAG_p:
       
   761   assumes vt: "vt s"
       
   762   and dp1: "(n, n1) \<in> (RAG s)^+"
       
   763   and dp2: "(n, n2) \<in> (RAG s)^+"
       
   764   and neq: "n1 \<noteq> n2"
       
   765   shows "(n1, n2) \<in> (RAG s)^+ \<or> (n2, n1) \<in> (RAG s)^+"
       
   766 proof(rule unique_chain [OF _ dp1 dp2 neq])
       
   767   from unique_RAG[OF vt]
       
   768   show "\<And>a b c. \<lbrakk>(a, b) \<in> RAG s; (a, c) \<in> RAG s\<rbrakk> \<Longrightarrow> b = c" by auto
       
   769 qed
       
   770 
       
   771 text {* (* ddd *)
       
   772 *}
       
   773 lemma dependants_child_unique:
       
   774   fixes s th th1 th2 th3
       
   775   assumes vt: "vt s"
       
   776   and ch1: "(Th th1, Th th) \<in> child s"
       
   777   and ch2: "(Th th2, Th th) \<in> child s"
       
   778   and dp1: "th3 \<in> dependants s th1"
       
   779   and dp2: "th3 \<in> dependants s th2"
       
   780 shows "th1 = th2"
       
   781 proof -
       
   782   { assume neq: "th1 \<noteq> th2"
       
   783     from dp1 have dp1: "(Th th3, Th th1) \<in> (RAG s)^+" 
       
   784       by (simp add:s_dependants_def eq_RAG)
       
   785     from dp2 have dp2: "(Th th3, Th th2) \<in> (RAG s)^+" 
       
   786       by (simp add:s_dependants_def eq_RAG)
       
   787     from unique_RAG_p[OF vt dp1 dp2] and neq
       
   788     have "(Th th1, Th th2) \<in> (RAG s)\<^sup>+ \<or> (Th th2, Th th1) \<in> (RAG s)\<^sup>+" by auto
       
   789     hence False
       
   790     proof
       
   791       assume "(Th th1, Th th2) \<in> (RAG s)\<^sup>+ "
       
   792       from children_no_dep[OF vt ch1 ch2 this] show ?thesis .
       
   793     next
       
   794       assume " (Th th2, Th th1) \<in> (RAG s)\<^sup>+"
       
   795       from children_no_dep[OF vt ch2 ch1 this] show ?thesis .
       
   796     qed
       
   797   } thus ?thesis by auto
       
   798 qed
       
   799 
       
   800 lemma RAG_plus_elim:
       
   801   assumes "vt s"
       
   802   fixes x
       
   803   assumes "(Th x, Th th) \<in> (RAG (wq s))\<^sup>+"
       
   804   shows "\<exists>th'\<in>children s th. x = th' \<or> (Th x, Th th') \<in> (RAG (wq s))\<^sup>+"
       
   805   using assms(2)[unfolded eq_RAG, folded child_RAG_eq[OF `vt s`]]
       
   806   apply (unfold children_def)
       
   807   by (metis assms(2) children_def RAG_children eq_RAG)
       
   808 
       
   809 text {* (* ddd *)
       
   810 *}
       
   811 lemma dependants_expand:
       
   812   assumes "vt s"
       
   813   shows "dependants (wq s) th = (children s th) \<union> (\<Union>((dependants (wq s)) ` children s th))"
       
   814 apply(simp add: image_def)
       
   815 unfolding cs_dependants_def
       
   816 apply(auto)
       
   817 apply (metis assms RAG_plus_elim mem_Collect_eq)
       
   818 apply (metis child_RAG_p children_def eq_RAG mem_Collect_eq r_into_trancl')
       
   819 by (metis assms child_RAG_eq children_def eq_RAG mem_Collect_eq trancl.trancl_into_trancl)
       
   820 
       
   821 lemma finite_children:
       
   822   assumes "vt s"
       
   823   shows "finite (children s th)"
       
   824   using children_dependants dependants_threads[OF assms] finite_threads[OF assms]
       
   825   by (metis rev_finite_subset)
       
   826   
       
   827 lemma finite_dependants:
       
   828   assumes "vt s"
       
   829   shows "finite (dependants (wq s) th')"
       
   830   using dependants_threads[OF assms] finite_threads[OF assms]
       
   831   by (metis rev_finite_subset)
       
   832 
       
   833 abbreviation
       
   834   "preceds s ths \<equiv> {preced th s| th. th \<in> ths}"
       
   835 
       
   836 abbreviation
       
   837   "cpreceds s ths \<equiv> (cp s) ` ths"
       
   838 
       
   839 lemma Un_compr:
       
   840   "{f th | th. R th \<or> Q th} = ({f th | th. R th} \<union> {f th' | th'. Q th'})"
       
   841 by auto
       
   842 
       
   843 lemma in_disj:
       
   844   shows "x \<in> A \<or> (\<exists>y \<in> A. x \<in> Q y) \<longleftrightarrow> (\<exists>y \<in> A. x = y \<or> x \<in> Q y)"
       
   845 by metis
       
   846 
       
   847 lemma UN_exists:
       
   848   shows "(\<Union>x \<in> A. {f y | y. Q y x}) = ({f y | y. (\<exists>x \<in> A. Q y x)})"
       
   849 by auto
       
   850 
       
   851 text {* (* ddd *)
       
   852   This is the recursive equation used to compute the current precedence of 
       
   853   a thread (the @{text "th"}) here. 
       
   854 *}
       
   855 lemma cp_rec:
       
   856   fixes s th
       
   857   assumes vt: "vt s"
       
   858   shows "cp s th = Max ({preced th s} \<union> (cp s ` children s th))"
       
   859 proof(cases "children s th = {}")
       
   860   case True
       
   861   show ?thesis
       
   862     unfolding cp_eq_cpreced cpreced_def 
       
   863     by (subst dependants_expand[OF `vt s`]) (simp add: True)
       
   864 next
       
   865   case False
       
   866   show ?thesis (is "?LHS = ?RHS")
       
   867   proof -
       
   868     have eq_cp: "cp s = (\<lambda>th. Max (preceds s ({th} \<union> dependants (wq s) th)))"
       
   869       by (simp add: fun_eq_iff cp_eq_cpreced cpreced_def Un_compr image_Collect[symmetric])
       
   870   
       
   871     have not_emptyness_facts[simp]: 
       
   872       "dependants (wq s) th \<noteq> {}" "children s th \<noteq> {}"
       
   873       using False dependants_expand[OF assms] by(auto simp only: Un_empty)
       
   874 
       
   875     have finiteness_facts[simp]:
       
   876       "\<And>th. finite (dependants (wq s) th)" "\<And>th. finite (children s th)"
       
   877       by (simp_all add: finite_dependants[OF `vt s`] finite_children[OF `vt s`])
       
   878 
       
   879     (* expanding definition *)
       
   880     have "?LHS = Max ({preced th s} \<union> preceds s (dependants (wq s) th))"
       
   881       unfolding eq_cp by (simp add: Un_compr)
       
   882     
       
   883     (* moving Max in *)
       
   884     also have "\<dots> = max (Max {preced th s}) (Max (preceds s (dependants (wq s) th)))"
       
   885       by (simp add: Max_Un)
       
   886 
       
   887     (* expanding dependants *)
       
   888     also have "\<dots> = max (Max {preced th s}) 
       
   889       (Max (preceds s (children s th \<union> \<Union>(dependants (wq s) ` children s th))))"
       
   890       by (subst dependants_expand[OF `vt s`]) (simp)
       
   891 
       
   892     (* moving out big Union *)
       
   893     also have "\<dots> = max (Max {preced th s})
       
   894       (Max (preceds s (\<Union> ({children s th} \<union> (dependants (wq s) ` children s th)))))"  
       
   895       by simp
       
   896 
       
   897     (* moving in small union *)
       
   898     also have "\<dots> = max (Max {preced th s})
       
   899       (Max (preceds s (\<Union> ((\<lambda>th. {th} \<union> (dependants (wq s) th)) ` children s th))))"  
       
   900       by (simp add: in_disj)
       
   901 
       
   902     (* moving in preceds *)
       
   903     also have "\<dots> = max (Max {preced th s})  
       
   904       (Max (\<Union> ((\<lambda>th. preceds s ({th} \<union> (dependants (wq s) th))) ` children s th)))" 
       
   905       by (simp add: UN_exists)
       
   906 
       
   907     (* moving in Max *)
       
   908     also have "\<dots> = max (Max {preced th s})  
       
   909       (Max ((\<lambda>th. Max (preceds s ({th} \<union> (dependants (wq s) th)))) ` children s th))" 
       
   910        by (subst Max_Union) (auto simp add: image_image) 
       
   911 
       
   912     (* folding cp + moving out Max *)
       
   913     also have "\<dots> = ?RHS" 
       
   914       unfolding eq_cp by (simp add: Max_insert)
       
   915 
       
   916     finally show "?LHS = ?RHS" .
       
   917   qed
       
   918 qed
       
   919 
       
   920 lemma next_th_holding:
   582 lemma next_th_holding:
   921   assumes vt: "vt s"
   583   assumes vt: "vt s"
   922   and nxt: "next_th s th cs th'"
   584   and nxt: "next_th s th cs th'"
   923   shows "holding (wq s) th cs"
   585   shows "holding (wq s) th cs"
   924 proof -
   586 proof -
  1004   defines s_def : "s \<equiv> (Set th prio#s')" 
   666   defines s_def : "s \<equiv> (Set th prio#s')" 
  1005   -- {* @{text "s"} is assumed to be a legitimate state, from which
   667   -- {* @{text "s"} is assumed to be a legitimate state, from which
  1006          the legitimacy of @{text "s"} can be derived. *}
   668          the legitimacy of @{text "s"} can be derived. *}
  1007   assumes vt_s: "vt s"
   669   assumes vt_s: "vt s"
  1008 
   670 
       
   671 sublocale step_set_cps < vat_s : valid_trace "s"
       
   672 proof
       
   673   from vt_s show "vt s" .
       
   674 qed
       
   675 
       
   676 sublocale step_set_cps < vat_s' : valid_trace "s'"
       
   677 proof
       
   678   from step_back_vt[OF vt_s[unfolded s_def]] show "vt s'" .
       
   679 qed
       
   680 
  1009 context step_set_cps 
   681 context step_set_cps 
  1010 begin
   682 begin
  1011 
   683 
  1012 text {* (* ddd *)
   684 text {* (* ddd *)
  1013   The following two lemmas confirm that @{text "Set"}-operating only changes the precedence 
   685   The following two lemmas confirm that @{text "Set"}-operating only changes the precedence 
  1014   of the initiating thread.
   686   of the initiating thread.
  1015 *}
   687 *}
  1016 
   688 
  1017 lemma eq_preced:
   689 lemma eq_preced:
  1018   fixes th'
       
  1019   assumes "th' \<noteq> th"
   690   assumes "th' \<noteq> th"
  1020   shows "preced th' s = preced th' s'"
   691   shows "preced th' s = preced th' s'"
  1021 proof -
   692 proof -
  1022   from assms show ?thesis 
   693   from assms show ?thesis 
  1023     by (unfold s_def, auto simp:preced_def)
   694     by (unfold s_def, auto simp:preced_def)
  1087     from step_back_step [OF vt_s[unfolded s_def]]
   758     from step_back_step [OF vt_s[unfolded s_def]]
  1088     have "step s' (Set th prio)" .
   759     have "step s' (Set th prio)" .
  1089     hence "th \<in> runing s'" by (cases, simp)
   760     hence "th \<in> runing s'" by (cases, simp)
  1090     thus ?thesis by (simp add:readys_def runing_def)
   761     thus ?thesis by (simp add:readys_def runing_def)
  1091   qed
   762   qed
  1092   from readys_in_no_subtree[OF step_back_vt[OF vt_s[unfolded s_def]] this assms(1)]
   763   find_theorems readys subtree
       
   764   from vat_s'.readys_in_no_subtree[OF this assms(1)]
  1093   show ?thesis by blast
   765   show ?thesis by blast
  1094 qed
   766 qed
  1095 
   767 
  1096 text {* 
   768 text {* 
  1097   By combining @{thm "eq_cp_pre"} and @{thm "th_in_no_subtree"}, 
   769   By combining @{thm "eq_cp_pre"} and @{thm "th_in_no_subtree"}, 
  1116   fixes s' th cs s    -- {* @{text "s'"} is the state before operation*}
   788   fixes s' th cs s    -- {* @{text "s'"} is the state before operation*}
  1117   defines s_def : "s \<equiv> (V th cs#s')" -- {* @{text "s"} is the state after operation*}
   789   defines s_def : "s \<equiv> (V th cs#s')" -- {* @{text "s"} is the state after operation*}
  1118   -- {* @{text "s"} is assumed to be valid, which implies the validity of @{text "s'"} *}
   790   -- {* @{text "s"} is assumed to be valid, which implies the validity of @{text "s'"} *}
  1119   assumes vt_s: "vt s"
   791   assumes vt_s: "vt s"
  1120 
   792 
       
   793 sublocale step_v_cps < vat_s : valid_trace "s"
       
   794 proof
       
   795   from vt_s show "vt s" .
       
   796 qed
       
   797 
       
   798 sublocale step_v_cps < vat_s' : valid_trace "s'"
       
   799 proof
       
   800   from step_back_vt[OF vt_s[unfolded s_def]] show "vt s'" .
       
   801 qed
       
   802 
  1121 context step_v_cps
   803 context step_v_cps
  1122 begin
   804 begin
  1123 
       
  1124 lemma rtree_RAGs: "rtree (RAG s)"
       
  1125 proof
       
  1126   show "single_valued (RAG s)"
       
  1127   apply (intro_locales)
       
  1128     by (unfold single_valued_def, auto intro: unique_RAG[OF vt_s])
       
  1129 
       
  1130   show "acyclic (RAG s)"
       
  1131      by (rule acyclic_RAG[OF vt_s])
       
  1132 qed
       
  1133 
       
  1134 lemma rtree_RAGs': "rtree (RAG s')"
       
  1135 proof
       
  1136   show "single_valued (RAG s')"
       
  1137   apply (intro_locales)
       
  1138     by (unfold single_valued_def, 
       
  1139         auto intro:unique_RAG[OF step_back_vt[OF vt_s[unfolded s_def]]])
       
  1140 
       
  1141   show "acyclic (RAG s')"
       
  1142      by (rule acyclic_RAG[OF step_back_vt[OF vt_s[unfolded s_def]]])
       
  1143 qed
       
  1144 
       
  1145 lemmas vt_s' = step_back_vt[OF vt_s[unfolded s_def]]
       
  1146 
   805 
  1147 lemma ready_th_s': "th \<in> readys s'"
   806 lemma ready_th_s': "th \<in> readys s'"
  1148   using step_back_step[OF vt_s[unfolded s_def]]
   807   using step_back_step[OF vt_s[unfolded s_def]]
  1149   by (cases, simp add:runing_def)
   808   by (cases, simp add:runing_def)
  1150 
   809 
  1151 
       
  1152 lemma ancestors_th: "ancestors (RAG s') (Th th) = {}"
   810 lemma ancestors_th: "ancestors (RAG s') (Th th) = {}"
  1153 proof -
   811 proof -
  1154   from readys_root[OF vt_s' ready_th_s']
   812   from vat_s'.readys_root[OF ready_th_s']
  1155   show ?thesis
   813   show ?thesis
  1156   by (unfold root_def, simp)
   814   by (unfold root_def, simp)
  1157 qed
   815 qed
  1158 
   816 
  1159 lemma holding_th: "holding s' th cs"
   817 lemma holding_th: "holding s' th cs"
  1172 qed
   830 qed
  1173 
   831 
  1174 lemma ancestors_cs: 
   832 lemma ancestors_cs: 
  1175   "ancestors (RAG s') (Cs cs) = {Th th}"
   833   "ancestors (RAG s') (Cs cs) = {Th th}"
  1176 proof -
   834 proof -
  1177   find_theorems ancestors
       
  1178   have "ancestors (RAG s') (Cs cs) = ancestors (RAG s') (Th th)  \<union>  {Th th}"
   835   have "ancestors (RAG s') (Cs cs) = ancestors (RAG s') (Th th)  \<union>  {Th th}"
  1179   proof(rule RTree.rtree.ancestors_accum[OF rtree_RAGs'])
   836   proof(rule vat_s'.rtree_RAG.ancestors_accum)
  1180     from vt_s[unfolded s_def]
   837     from vt_s[unfolded s_def]
  1181     have " PIP s' (V th cs)" by (cases, simp)
   838     have " PIP s' (V th cs)" by (cases, simp)
  1182     thus "(Cs cs, Th th) \<in> RAG s'" 
   839     thus "(Cs cs, Th th) \<in> RAG s'" 
  1183     proof(cases)
   840     proof(cases)
  1184       assume "holding s' th cs"
   841       assume "holding s' th cs"
  1191 
   848 
  1192 lemma preced_kept: "the_preced s = the_preced s'"
   849 lemma preced_kept: "the_preced s = the_preced s'"
  1193   by (auto simp: s_def the_preced_def preced_def)
   850   by (auto simp: s_def the_preced_def preced_def)
  1194 
   851 
  1195 end
   852 end
  1196 
       
  1197 
   853 
  1198 text {*
   854 text {*
  1199   The following @{text "step_v_cps_nt"} is the sub-locale for @{text "V"}-operation, 
   855   The following @{text "step_v_cps_nt"} is the sub-locale for @{text "V"}-operation, 
  1200   which represents the case when there is another thread @{text "th'"}
   856   which represents the case when there is another thread @{text "th'"}
  1201   to take over the critical resource released by the initiating thread @{text "th"}.
   857   to take over the critical resource released by the initiating thread @{text "th"}.
  1247             |
   903             |
  1248     th7 ----|
   904     th7 ----|
  1249 *)
   905 *)
  1250 
   906 
  1251 lemma sub_RAGs': "{(Cs cs, Th th), (Th th', Cs cs)} \<subseteq> RAG s'"
   907 lemma sub_RAGs': "{(Cs cs, Th th), (Th th', Cs cs)} \<subseteq> RAG s'"
  1252                 using next_th_RAG[OF vt_s' nt] .
   908                 using next_th_RAG[OF vat_s'.vt nt]  .
  1253 
   909 
  1254 lemma ancestors_th': 
   910 lemma ancestors_th': 
  1255   "ancestors (RAG s') (Th th') = {Th th, Cs cs}" 
   911   "ancestors (RAG s') (Th th') = {Th th, Cs cs}" 
  1256 proof -
   912 proof -
  1257   have "ancestors (RAG s') (Th th') = ancestors (RAG s') (Cs cs) \<union> {Cs cs}"
   913   have "ancestors (RAG s') (Th th') = ancestors (RAG s') (Cs cs) \<union> {Cs cs}"
  1258   proof(rule  RTree.rtree.ancestors_accum[OF rtree_RAGs'])
   914   proof(rule  vat_s'.rtree_RAG.ancestors_accum)
  1259     from sub_RAGs' show "(Th th', Cs cs) \<in> RAG s'" by auto
   915     from sub_RAGs' show "(Th th', Cs cs) \<in> RAG s'" by auto
  1260   qed
   916   qed
  1261   thus ?thesis using ancestors_th ancestors_cs by auto
   917   thus ?thesis using ancestors_th ancestors_cs by auto
  1262 qed
   918 qed
  1263 
   919 
  1384   ultimately show ?thesis by auto 
  1040   ultimately show ?thesis by auto 
  1385 qed
  1041 qed
  1386 
  1042 
  1387 lemma subtree_th: 
  1043 lemma subtree_th: 
  1388   "subtree (RAG s) (Th th) = subtree (RAG s') (Th th) - {Cs cs}"
  1044   "subtree (RAG s) (Th th) = subtree (RAG s') (Th th) - {Cs cs}"
  1389 proof(unfold RAG_s, fold subtree_cs, rule RTree.rtree.subtree_del_inside[OF rtree_RAGs'])
  1045 find_theorems "subtree" "_ - _" RAG
       
  1046 proof(unfold RAG_s, fold subtree_cs, rule vat_s'.rtree_RAG.subtree_del_inside)
  1390   from edge_of_th
  1047   from edge_of_th
  1391   show "(Cs cs, Th th) \<in> edges_in (RAG s') (Th th)"
  1048   show "(Cs cs, Th th) \<in> edges_in (RAG s') (Th th)"
  1392     by (unfold edges_in_def, auto simp:subtree_def)
  1049     by (unfold edges_in_def, auto simp:subtree_def)
  1393 qed
  1050 qed
  1394 
  1051 
  1399 lemma eq_cp:
  1056 lemma eq_cp:
  1400   fixes th' 
  1057   fixes th' 
  1401   shows "cp s th' = cp s' th'"
  1058   shows "cp s th' = cp s' th'"
  1402   using cp_kept_1 cp_kept_2
  1059   using cp_kept_1 cp_kept_2
  1403   by (cases "th' = th", auto)
  1060   by (cases "th' = th", auto)
  1404  
       
  1405 end
  1061 end
  1406 
  1062 
  1407 find_theorems "_`_" "\<Union> _"
       
  1408 
       
  1409 find_theorems "Max" "\<Union> _"
       
  1410 
       
  1411 find_theorems wf RAG
       
  1412 
       
  1413 thm wf_def
       
  1414 
       
  1415 thm image_Union
       
  1416 
  1063 
  1417 locale step_P_cps =
  1064 locale step_P_cps =
  1418   fixes s' th cs s 
  1065   fixes s' th cs s 
  1419   defines s_def : "s \<equiv> (P th cs#s')"
  1066   defines s_def : "s \<equiv> (P th cs#s')"
  1420   assumes vt_s: "vt s"
  1067   assumes vt_s: "vt s"
  1427 sublocale step_P_cps < vat_s' : valid_trace "s'"
  1074 sublocale step_P_cps < vat_s' : valid_trace "s'"
  1428 proof
  1075 proof
  1429   from step_back_vt[OF vt_s[unfolded s_def]] show "vt s'" .
  1076   from step_back_vt[OF vt_s[unfolded s_def]] show "vt s'" .
  1430 qed
  1077 qed
  1431 
  1078 
  1432 
       
  1433 context step_P_cps
  1079 context step_P_cps
  1434 begin
  1080 begin
  1435 
  1081 
  1436 lemma readys_th: "th \<in> readys s'"
  1082 lemma readys_th: "th \<in> readys s'"
  1437 proof -
  1083 proof -
  1440     hence "th \<in> runing s'" by (cases, simp)
  1086     hence "th \<in> runing s'" by (cases, simp)
  1441     thus ?thesis by (simp add:readys_def runing_def)
  1087     thus ?thesis by (simp add:readys_def runing_def)
  1442 qed
  1088 qed
  1443 
  1089 
  1444 lemma root_th: "root (RAG s') (Th th)"
  1090 lemma root_th: "root (RAG s') (Th th)"
  1445   using readys_root[OF vat_s'.vt readys_th] .
  1091   using readys_root[OF readys_th] .
  1446 
  1092 
  1447 lemma in_no_others_subtree:
  1093 lemma in_no_others_subtree:
  1448   assumes "th' \<noteq> th"
  1094   assumes "th' \<noteq> th"
  1449   shows "Th th \<notin> subtree (RAG s') (Th th')"
  1095   shows "Th th \<notin> subtree (RAG s') (Th th')"
  1450 proof
  1096 proof
  1871       from ancestors_Field[OF 2(2)]
  1517       from ancestors_Field[OF 2(2)]
  1872       and that show ?thesis by (unfold tRAG_alt_def, auto)
  1518       and that show ?thesis by (unfold tRAG_alt_def, auto)
  1873     qed auto
  1519     qed auto
  1874     have neq_th_a: "th_a \<noteq> th"
  1520     have neq_th_a: "th_a \<noteq> th"
  1875     proof -
  1521     proof -
  1876       from readys_in_no_subtree[OF vat_s'.vt th_ready assms]
  1522     find_theorems readys subtree s'
       
  1523       from vat_s'.readys_in_no_subtree[OF th_ready assms]
  1877       have "(Th th) \<notin> subtree (RAG s') (Th th')" .
  1524       have "(Th th) \<notin> subtree (RAG s') (Th th')" .
  1878       with tRAG_subtree_RAG[of s' "Th th'"]
  1525       with tRAG_subtree_RAG[of s' "Th th'"]
  1879       have "(Th th) \<notin> subtree (tRAG s') (Th th')" by auto
  1526       have "(Th th) \<notin> subtree (tRAG s') (Th th')" by auto
  1880       with a_in[unfolded eq_a] show ?thesis by auto
  1527       with a_in[unfolded eq_a] show ?thesis by auto
  1881     qed
  1528     qed