# HG changeset patch # User zhangx # Date 1453894122 -28800 # Node ID c495eb16beb6ee35fedca21666bf98ce817dc3b4 # Parent 17305a85493dc68a39212bb6ce8db8e774085cc5 CpsG.thy restored. It was deleted. But now restored as a temporary holder of PIPBasics.thy. diff -r 17305a85493d -r c495eb16beb6 #PIPBasics.thy# --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/#PIPBasics.thy# Wed Jan 27 19:28:42 2016 +0800 @@ -0,0 +1,3787 @@ +theory PIPBasics +imports PIPDefs +begin + +locale valid_trace = + fixes s + assumes vt : "vt s" + +locale valid_trace_e = valid_trace + + fixes e + assumes vt_e: "vt (e#s)" +begin + +lemma pip_e: "PIP s e" + using vt_e by (cases, simp) + +end + +lemma runing_ready: + shows "runing s \ readys s" + unfolding runing_def readys_def + by auto + +lemma readys_threads: + shows "readys s \ threads s" + unfolding readys_def + by auto + +lemma wq_v_neq: + "cs \ cs' \ wq (V thread cs#s) cs' = wq s cs'" + by (auto simp:wq_def Let_def cp_def split:list.splits) + +lemma runing_head: + assumes "th \ runing s" + and "th \ set (wq_fun (schs s) cs)" + shows "th = hd (wq_fun (schs s) cs)" + using assms + by (simp add:runing_def readys_def s_waiting_def wq_def) + +context valid_trace +begin + +lemma actor_inv: + assumes "PIP s e" + and "\ isCreate e" + shows "actor e \ runing s" + using assms + by (induct, auto) + +lemma ind [consumes 0, case_names Nil Cons, induct type]: + assumes "PP []" + and "(\s e. valid_trace s \ valid_trace (e#s) \ + PP s \ PIP s e \ PP (e # s))" + shows "PP s" +proof(rule vt.induct[OF vt]) + from assms(1) show "PP []" . +next + fix s e + assume h: "vt s" "PP s" "PIP s e" + show "PP (e # s)" + proof(cases rule:assms(2)) + from h(1) show v1: "valid_trace s" by (unfold_locales, simp) + next + from h(1,3) have "vt (e#s)" by auto + thus "valid_trace (e # s)" by (unfold_locales, simp) + qed (insert h, auto) +qed + +lemma wq_distinct: "distinct (wq s cs)" +proof(induct rule:ind) + case (Cons s e) + from Cons(4,3) + show ?case + proof(induct) + case (thread_P th s cs1) + show ?case + proof(cases "cs = cs1") + case True + thus ?thesis (is "distinct ?L") + proof - + have "?L = wq_fun (schs s) cs1 @ [th]" using True + by (simp add:wq_def wf_def Let_def split:list.splits) + moreover have "distinct ..." + proof - + have "th \ set (wq_fun (schs s) cs1)" + proof + assume otherwise: "th \ set (wq_fun (schs s) cs1)" + from runing_head[OF thread_P(1) this] + have "th = hd (wq_fun (schs s) cs1)" . + hence "(Cs cs1, Th th) \ (RAG s)" using otherwise + by (simp add:s_RAG_def s_holding_def wq_def cs_holding_def) + with thread_P(2) show False by auto + qed + moreover have "distinct (wq_fun (schs s) cs1)" + using True thread_P wq_def by auto + ultimately show ?thesis by auto + qed + ultimately show ?thesis by simp + qed + next + case False + with thread_P(3) + show ?thesis + by (auto simp:wq_def wf_def Let_def split:list.splits) + qed + next + case (thread_V th s cs1) + thus ?case + proof(cases "cs = cs1") + case True + show ?thesis (is "distinct ?L") + proof(cases "(wq s cs)") + case Nil + thus ?thesis + by (auto simp:wq_def wf_def Let_def split:list.splits) + next + case (Cons w_hd w_tl) + moreover have "distinct (SOME q. distinct q \ set q = set w_tl)" + proof(rule someI2) + from thread_V(3)[unfolded Cons] + show "distinct w_tl \ set w_tl = set w_tl" by auto + qed auto + ultimately show ?thesis + by (auto simp:wq_def wf_def Let_def True split:list.splits) + qed + next + case False + with thread_V(3) + show ?thesis + by (auto simp:wq_def wf_def Let_def split:list.splits) + qed + qed (insert Cons, auto simp: wq_def Let_def split:list.splits) +qed (unfold wq_def Let_def, simp) + +end + + +context valid_trace_e +begin + +text {* + The following lemma shows that only the @{text "P"} + operation can add new thread into waiting queues. + Such kind of lemmas are very obvious, but need to be checked formally. + This is a kind of confirmation that our modelling is correct. +*} + +lemma block_pre: + assumes s_ni: "thread \ set (wq s cs)" + and s_i: "thread \ set (wq (e#s) cs)" + shows "e = P thread cs" +proof(cases e) + -- {* This is the only non-trivial case: *} + case (V th cs1) + have False + proof(cases "cs1 = cs") + case True + show ?thesis + proof(cases "(wq s cs1)") + case (Cons w_hd w_tl) + have "set (wq (e#s) cs) \ set (wq s cs)" + proof - + have "(wq (e#s) cs) = (SOME q. distinct q \ set q = set w_tl)" + using Cons V by (auto simp:wq_def Let_def True split:if_splits) + moreover have "set ... \ set (wq s cs)" + proof(rule someI2) + show "distinct w_tl \ set w_tl = set w_tl" + by (metis distinct.simps(2) local.Cons wq_distinct) + qed (insert Cons True, auto) + ultimately show ?thesis by simp + qed + with assms show ?thesis by auto + qed (insert assms V True, auto simp:wq_def Let_def split:if_splits) + qed (insert assms V, auto simp:wq_def Let_def split:if_splits) + thus ?thesis by auto +qed (insert assms, auto simp:wq_def Let_def split:if_splits) + +end + +text {* + The following lemmas is also obvious and shallow. It says + that only running thread can request for a critical resource + and that the requested resource must be one which is + not current held by the thread. +*} + +lemma p_pre: "\vt ((P thread cs)#s)\ \ + thread \ runing s \ (Cs cs, Th thread) \ (RAG s)^+" +apply (ind_cases "vt ((P thread cs)#s)") +apply (ind_cases "step s (P thread cs)") +by auto + +lemma abs1: + assumes ein: "e \ set es" + and neq: "hd es \ hd (es @ [x])" + shows "False" +proof - + from ein have "es \ []" by auto + then obtain e ess where "es = e # ess" by (cases es, auto) + with neq show ?thesis by auto +qed + +lemma q_head: "Q (hd es) \ hd es = hd [th\es . Q th]" + by (cases es, auto) + +inductive_cases evt_cons: "vt (a#s)" + +context valid_trace_e +begin + +lemma abs2: + assumes inq: "thread \ set (wq s cs)" + and nh: "thread = hd (wq s cs)" + and qt: "thread \ hd (wq (e#s) cs)" + and inq': "thread \ set (wq (e#s) cs)" + shows "False" +proof - + from vt_e assms show "False" + apply (cases e) + apply ((simp split:if_splits add:Let_def wq_def)[1])+ + apply (insert abs1, fast)[1] + apply (auto simp:wq_def simp:Let_def split:if_splits list.splits) + proof - + fix th qs + assume vt: "vt (V th cs # s)" + and th_in: "thread \ set (SOME q. distinct q \ set q = set qs)" + and eq_wq: "wq_fun (schs s) cs = thread # qs" + show "False" + proof - + from wq_distinct[of cs] + and eq_wq[folded wq_def] have "distinct (thread#qs)" by simp + moreover have "thread \ set qs" + proof - + have "set (SOME q. distinct q \ set q = set qs) = set qs" + proof(rule someI2) + from wq_distinct [of cs] + and eq_wq [folded wq_def] + show "distinct qs \ set qs = set qs" by auto + next + fix x assume "distinct x \ set x = set qs" + thus "set x = set qs" by auto + qed + with th_in show ?thesis by auto + qed + ultimately show ?thesis by auto + qed + qed +qed + +end + + +context valid_trace +begin +lemma vt_moment: "\ t. vt (moment t s)" +proof(induct rule:ind) + case Nil + thus ?case by (simp add:vt_nil) +next + case (Cons s e t) + show ?case + proof(cases "t \ length (e#s)") + case True + from True have "moment t (e#s) = e#s" by simp + thus ?thesis using Cons + by (simp add:valid_trace_def) + next + case False + from Cons have "vt (moment t s)" by simp + moreover have "moment t (e#s) = moment t s" + proof - + from False have "t \ length s" by simp + from moment_app [OF this, of "[e]"] + show ?thesis by simp + qed + ultimately show ?thesis by simp + qed +qed +end + +locale valid_moment = valid_trace + + fixes i :: nat + +sublocale valid_moment < vat_moment: valid_trace "(moment i s)" + by (unfold_locales, insert vt_moment, auto) + +context valid_trace +begin + + +text {* (* ddd *) + The nature of the work is like this: since it starts from a very simple and basic + model, even intuitively very `basic` and `obvious` properties need to derived from scratch. + For instance, the fact + that one thread can not be blocked by two critical resources at the same time + is obvious, because only running threads can make new requests, if one is waiting for + a critical resource and get blocked, it can not make another resource request and get + blocked the second time (because it is not running). + + To derive this fact, one needs to prove by contraction and + reason about time (or @{text "moement"}). The reasoning is based on a generic theorem + named @{text "p_split"}, which is about status changing along the time axis. It says if + a condition @{text "Q"} is @{text "True"} at a state @{text "s"}, + but it was @{text "False"} at the very beginning, then there must exits a moment @{text "t"} + in the history of @{text "s"} (notice that @{text "s"} itself is essentially the history + of events leading to it), such that @{text "Q"} switched + from being @{text "False"} to @{text "True"} and kept being @{text "True"} + till the last moment of @{text "s"}. + + Suppose a thread @{text "th"} is blocked + on @{text "cs1"} and @{text "cs2"} in some state @{text "s"}, + since no thread is blocked at the very beginning, by applying + @{text "p_split"} to these two blocking facts, there exist + two moments @{text "t1"} and @{text "t2"} in @{text "s"}, such that + @{text "th"} got blocked on @{text "cs1"} and @{text "cs2"} + and kept on blocked on them respectively ever since. + + Without lost of generality, we assume @{text "t1"} is earlier than @{text "t2"}. + However, since @{text "th"} was blocked ever since memonent @{text "t1"}, so it was still + in blocked state at moment @{text "t2"} and could not + make any request and get blocked the second time: Contradiction. +*} + +lemma waiting_unique_pre: (* ccc *) + assumes h11: "thread \ set (wq s cs1)" + and h12: "thread \ hd (wq s cs1)" + assumes h21: "thread \ set (wq s cs2)" + and h22: "thread \ hd (wq s cs2)" + and neq12: "cs1 \ cs2" + shows "False" +proof - + let "?Q cs s" = "thread \ set (wq s cs) \ thread \ hd (wq s cs)" + from h11 and h12 have q1: "?Q cs1 s" by simp + from h21 and h22 have q2: "?Q cs2 s" by simp + have nq1: "\ ?Q cs1 []" by (simp add:wq_def) + have nq2: "\ ?Q cs2 []" by (simp add:wq_def) + from p_split [of "?Q cs1", OF q1 nq1] + obtain t1 where lt1: "t1 < length s" + and np1: "\(thread \ set (wq (moment t1 s) cs1) \ + thread \ hd (wq (moment t1 s) cs1))" + and nn1: "(\i'>t1. thread \ set (wq (moment i' s) cs1) \ + thread \ hd (wq (moment i' s) cs1))" by auto + from p_split [of "?Q cs2", OF q2 nq2] + obtain t2 where lt2: "t2 < length s" + and np2: "\(thread \ set (wq (moment t2 s) cs2) \ + thread \ hd (wq (moment t2 s) cs2))" + and nn2: "(\i'>t2. thread \ set (wq (moment i' s) cs2) \ + thread \ hd (wq (moment i' s) cs2))" by auto + show ?thesis + proof - + { + assume lt12: "t1 < t2" + let ?t3 = "Suc t2" + from lt2 have le_t3: "?t3 \ length s" by auto + from moment_plus [OF this] + obtain e where eq_m: "moment ?t3 s = e#moment t2 s" by auto + have "t2 < ?t3" by simp + from nn2 [rule_format, OF this] and eq_m + have h1: "thread \ set (wq (e#moment t2 s) cs2)" and + h2: "thread \ hd (wq (e#moment t2 s) cs2)" by auto + have "vt (e#moment t2 s)" + proof - + from vt_moment + have "vt (moment ?t3 s)" . + with eq_m show ?thesis by simp + qed + then interpret vt_e: valid_trace_e "moment t2 s" "e" + by (unfold_locales, auto, cases, simp) + have ?thesis + proof(cases "thread \ set (wq (moment t2 s) cs2)") + case True + from True and np2 have eq_th: "thread = hd (wq (moment t2 s) cs2)" + by auto + from vt_e.abs2 [OF True eq_th h2 h1] + show ?thesis by auto + next + case False + from vt_e.block_pre[OF False h1] + have "e = P thread cs2" . + with vt_e.vt_e have "vt ((P thread cs2)# moment t2 s)" by simp + from p_pre [OF this] have "thread \ runing (moment t2 s)" by simp + with runing_ready have "thread \ readys (moment t2 s)" by auto + with nn1 [rule_format, OF lt12] + show ?thesis by (simp add:readys_def wq_def s_waiting_def, auto) + qed + } moreover { + assume lt12: "t2 < t1" + let ?t3 = "Suc t1" + from lt1 have le_t3: "?t3 \ length s" by auto + from moment_plus [OF this] + obtain e where eq_m: "moment ?t3 s = e#moment t1 s" by auto + have lt_t3: "t1 < ?t3" by simp + from nn1 [rule_format, OF this] and eq_m + have h1: "thread \ set (wq (e#moment t1 s) cs1)" and + h2: "thread \ hd (wq (e#moment t1 s) cs1)" by auto + have "vt (e#moment t1 s)" + proof - + from vt_moment + have "vt (moment ?t3 s)" . + with eq_m show ?thesis by simp + qed + then interpret vt_e: valid_trace_e "moment t1 s" e + by (unfold_locales, auto, cases, auto) + have ?thesis + proof(cases "thread \ set (wq (moment t1 s) cs1)") + case True + from True and np1 have eq_th: "thread = hd (wq (moment t1 s) cs1)" + by auto + from vt_e.abs2 True eq_th h2 h1 + show ?thesis by auto + next + case False + from vt_e.block_pre [OF False h1] + have "e = P thread cs1" . + with vt_e.vt_e have "vt ((P thread cs1)# moment t1 s)" by simp + from p_pre [OF this] have "thread \ runing (moment t1 s)" by simp + with runing_ready have "thread \ readys (moment t1 s)" by auto + with nn2 [rule_format, OF lt12] + show ?thesis by (simp add:readys_def wq_def s_waiting_def, auto) + qed + } moreover { + assume eqt12: "t1 = t2" + let ?t3 = "Suc t1" + from lt1 have le_t3: "?t3 \ length s" by auto + from moment_plus [OF this] + obtain e where eq_m: "moment ?t3 s = e#moment t1 s" by auto + have lt_t3: "t1 < ?t3" by simp + from nn1 [rule_format, OF this] and eq_m + have h1: "thread \ set (wq (e#moment t1 s) cs1)" and + h2: "thread \ hd (wq (e#moment t1 s) cs1)" by auto + have vt_e: "vt (e#moment t1 s)" + proof - + from vt_moment + have "vt (moment ?t3 s)" . + with eq_m show ?thesis by simp + qed + then interpret vt_e: valid_trace_e "moment t1 s" e + by (unfold_locales, auto, cases, auto) + have ?thesis + proof(cases "thread \ set (wq (moment t1 s) cs1)") + case True + from True and np1 have eq_th: "thread = hd (wq (moment t1 s) cs1)" + by auto + from vt_e.abs2 [OF True eq_th h2 h1] + show ?thesis by auto + next + case False + from vt_e.block_pre [OF False h1] + have eq_e1: "e = P thread cs1" . + have lt_t3: "t1 < ?t3" by simp + with eqt12 have "t2 < ?t3" by simp + from nn2 [rule_format, OF this] and eq_m and eqt12 + have h1: "thread \ set (wq (e#moment t2 s) cs2)" and + h2: "thread \ hd (wq (e#moment t2 s) cs2)" by auto + show ?thesis + proof(cases "thread \ set (wq (moment t2 s) cs2)") + case True + from True and np2 have eq_th: "thread = hd (wq (moment t2 s) cs2)" + by auto + from vt_e and eqt12 have "vt (e#moment t2 s)" by simp + then interpret vt_e2: valid_trace_e "moment t2 s" e + by (unfold_locales, auto, cases, auto) + from vt_e2.abs2 [OF True eq_th h2 h1] + show ?thesis . + next + case False + have "vt (e#moment t2 s)" + proof - + from vt_moment eqt12 + have "vt (moment (Suc t2) s)" by auto + with eq_m eqt12 show ?thesis by simp + qed + then interpret vt_e2: valid_trace_e "moment t2 s" e + by (unfold_locales, auto, cases, auto) + from vt_e2.block_pre [OF False h1] + have "e = P thread cs2" . + with eq_e1 neq12 show ?thesis by auto + qed + qed + } ultimately show ?thesis by arith + qed +qed + +text {* + This lemma is a simple corrolary of @{text "waiting_unique_pre"}. +*} + +lemma waiting_unique: + assumes "waiting s th cs1" + and "waiting s th cs2" + shows "cs1 = cs2" +using waiting_unique_pre assms +unfolding wq_def s_waiting_def +by auto + +end + +(* not used *) +text {* + Every thread can only be blocked on one critical resource, + symmetrically, every critical resource can only be held by one thread. + This fact is much more easier according to our definition. +*} +lemma held_unique: + assumes "holding (s::event list) th1 cs" + and "holding s th2 cs" + shows "th1 = th2" + by (insert assms, unfold s_holding_def, auto) + + +lemma last_set_lt: "th \ threads s \ last_set th s < length s" + apply (induct s, auto) + by (case_tac a, auto split:if_splits) + +lemma last_set_unique: + "\last_set th1 s = last_set th2 s; th1 \ threads s; th2 \ threads s\ + \ th1 = th2" + apply (induct s, auto) + by (case_tac a, auto split:if_splits dest:last_set_lt) + +lemma preced_unique : + assumes pcd_eq: "preced th1 s = preced th2 s" + and th_in1: "th1 \ threads s" + and th_in2: " th2 \ threads s" + shows "th1 = th2" +proof - + from pcd_eq have "last_set th1 s = last_set th2 s" by (simp add:preced_def) + from last_set_unique [OF this th_in1 th_in2] + show ?thesis . +qed + +lemma preced_linorder: + assumes neq_12: "th1 \ th2" + and th_in1: "th1 \ threads s" + and th_in2: " th2 \ threads s" + shows "preced th1 s < preced th2 s \ preced th1 s > preced th2 s" +proof - + from preced_unique [OF _ th_in1 th_in2] and neq_12 + have "preced th1 s \ preced th2 s" by auto + thus ?thesis by auto +qed + +(* An aux lemma used later *) +lemma unique_minus: + assumes unique: "\ a b c. \(a, b) \ r; (a, c) \ r\ \ b = c" + and xy: "(x, y) \ r" + and xz: "(x, z) \ r^+" + and neq: "y \ z" + shows "(y, z) \ r^+" +proof - + from xz and neq show ?thesis + proof(induct) + case (base ya) + have "(x, ya) \ r" by fact + from unique [OF xy this] have "y = ya" . + with base show ?case by auto + next + case (step ya z) + show ?case + proof(cases "y = ya") + case True + from step True show ?thesis by simp + next + case False + from step False + show ?thesis by auto + qed + qed +qed + +lemma unique_base: + assumes unique: "\ a b c. \(a, b) \ r; (a, c) \ r\ \ b = c" + and xy: "(x, y) \ r" + and xz: "(x, z) \ r^+" + and neq_yz: "y \ z" + shows "(y, z) \ r^+" +proof - + from xz neq_yz show ?thesis + proof(induct) + case (base ya) + from xy unique base show ?case by auto + next + case (step ya z) + show ?case + proof(cases "y = ya") + case True + from True step show ?thesis by auto + next + case False + from False step + have "(y, ya) \ r\<^sup>+" by auto + with step show ?thesis by auto + qed + qed +qed + +lemma unique_chain: + assumes unique: "\ a b c. \(a, b) \ r; (a, c) \ r\ \ b = c" + and xy: "(x, y) \ r^+" + and xz: "(x, z) \ r^+" + and neq_yz: "y \ z" + shows "(y, z) \ r^+ \ (z, y) \ r^+" +proof - + from xy xz neq_yz show ?thesis + proof(induct) + case (base y) + have h1: "(x, y) \ r" and h2: "(x, z) \ r\<^sup>+" and h3: "y \ z" using base by auto + from unique_base [OF _ h1 h2 h3] and unique show ?case by auto + next + case (step y za) + show ?case + proof(cases "y = z") + case True + from True step show ?thesis by auto + next + case False + from False step have "(y, z) \ r\<^sup>+ \ (z, y) \ r\<^sup>+" by auto + thus ?thesis + proof + assume "(z, y) \ r\<^sup>+" + with step have "(z, za) \ r\<^sup>+" by auto + thus ?thesis by auto + next + assume h: "(y, z) \ r\<^sup>+" + from step have yza: "(y, za) \ r" by simp + from step have "za \ z" by simp + from unique_minus [OF _ yza h this] and unique + have "(za, z) \ r\<^sup>+" by auto + thus ?thesis by auto + qed + qed + qed +qed + +text {* + The following three lemmas show that @{text "RAG"} does not change + by the happening of @{text "Set"}, @{text "Create"} and @{text "Exit"} + events, respectively. +*} + +lemma RAG_set_unchanged: "(RAG (Set th prio # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + +lemma RAG_create_unchanged: "(RAG (Create th prio # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + +lemma RAG_exit_unchanged: "(RAG (Exit th # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + + +text {* + The following lemmas are used in the proof of + lemma @{text "step_RAG_v"}, which characterizes how the @{text "RAG"} is changed + by @{text "V"}-events. + However, since our model is very concise, such seemingly obvious lemmas need to be derived from scratch, + starting from the model definitions. +*} +lemma step_v_hold_inv[elim_format]: + "\c t. \vt (V th cs # s); + \ holding (wq s) t c; holding (wq (V th cs # s)) t c\ \ + next_th s th cs t \ c = cs" +proof - + fix c t + assume vt: "vt (V th cs # s)" + and nhd: "\ holding (wq s) t c" + and hd: "holding (wq (V th cs # s)) t c" + show "next_th s th cs t \ c = cs" + proof(cases "c = cs") + case False + with nhd hd show ?thesis + by (unfold cs_holding_def wq_def, auto simp:Let_def) + next + case True + with step_back_step [OF vt] + have "step s (V th c)" by simp + hence "next_th s th cs t" + proof(cases) + assume "holding s th c" + with nhd hd show ?thesis + apply (unfold s_holding_def cs_holding_def wq_def next_th_def, + auto simp:Let_def split:list.splits if_splits) + proof - + assume " hd (SOME q. distinct q \ q = []) \ set (SOME q. distinct q \ q = [])" + moreover have "\ = set []" + proof(rule someI2) + show "distinct [] \ [] = []" by auto + next + fix x assume "distinct x \ x = []" + thus "set x = set []" by auto + qed + ultimately show False by auto + next + assume " hd (SOME q. distinct q \ q = []) \ set (SOME q. distinct q \ q = [])" + moreover have "\ = set []" + proof(rule someI2) + show "distinct [] \ [] = []" by auto + next + fix x assume "distinct x \ x = []" + thus "set x = set []" by auto + qed + ultimately show False by auto + qed + qed + with True show ?thesis by auto + qed +qed + +text {* + The following @{text "step_v_wait_inv"} is also an obvious lemma, which, however, needs to be + derived from scratch, which confirms the correctness of the definition of @{text "next_th"}. +*} +lemma step_v_wait_inv[elim_format]: + "\t c. \vt (V th cs # s); \ waiting (wq (V th cs # s)) t c; waiting (wq s) t c + \ + \ (next_th s th cs t \ cs = c)" +proof - + fix t c + assume vt: "vt (V th cs # s)" + and nw: "\ waiting (wq (V th cs # s)) t c" + and wt: "waiting (wq s) t c" + from vt interpret vt_v: valid_trace_e s "V th cs" + by (cases, unfold_locales, simp) + show "next_th s th cs t \ cs = c" + proof(cases "cs = c") + case False + with nw wt show ?thesis + by (auto simp:cs_waiting_def wq_def Let_def) + next + case True + from nw[folded True] wt[folded True] + have "next_th s th cs t" + apply (unfold next_th_def, auto simp:cs_waiting_def wq_def Let_def split:list.splits) + proof - + fix a list + assume t_in: "t \ set list" + and t_ni: "t \ set (SOME q. distinct q \ set q = set list)" + and eq_wq: "wq_fun (schs s) cs = a # list" + have " set (SOME q. distinct q \ set q = set list) = set list" + proof(rule someI2) + from vt_v.wq_distinct[of cs] and eq_wq[folded wq_def] + show "distinct list \ set list = set list" by auto + next + show "\x. distinct x \ set x = set list \ set x = set list" + by auto + qed + with t_ni and t_in show "a = th" by auto + next + fix a list + assume t_in: "t \ set list" + and t_ni: "t \ set (SOME q. distinct q \ set q = set list)" + and eq_wq: "wq_fun (schs s) cs = a # list" + have " set (SOME q. distinct q \ set q = set list) = set list" + proof(rule someI2) + from vt_v.wq_distinct[of cs] and eq_wq[folded wq_def] + show "distinct list \ set list = set list" by auto + next + show "\x. distinct x \ set x = set list \ set x = set list" + by auto + qed + with t_ni and t_in show "t = hd (SOME q. distinct q \ set q = set list)" by auto + next + fix a list + assume eq_wq: "wq_fun (schs s) cs = a # list" + from step_back_step[OF vt] + show "a = th" + proof(cases) + assume "holding s th cs" + with eq_wq show ?thesis + by (unfold s_holding_def wq_def, auto) + qed + qed + with True show ?thesis by simp + qed +qed + +lemma step_v_not_wait[consumes 3]: + "\vt (V th cs # s); next_th s th cs t; waiting (wq (V th cs # s)) t cs\ \ False" + by (unfold next_th_def cs_waiting_def wq_def, auto simp:Let_def) + +lemma step_v_release: + "\vt (V th cs # s); holding (wq (V th cs # s)) th cs\ \ False" +proof - + assume vt: "vt (V th cs # s)" + and hd: "holding (wq (V th cs # s)) th cs" + from vt interpret vt_v: valid_trace_e s "V th cs" + by (cases, unfold_locales, simp+) + from step_back_step [OF vt] and hd + show "False" + proof(cases) + assume "holding (wq (V th cs # s)) th cs" and "holding s th cs" + thus ?thesis + apply (unfold s_holding_def wq_def cs_holding_def) + apply (auto simp:Let_def split:list.splits) + proof - + fix list + assume eq_wq[folded wq_def]: + "wq_fun (schs s) cs = hd (SOME q. distinct q \ set q = set list) # list" + and hd_in: "hd (SOME q. distinct q \ set q = set list) + \ set (SOME q. distinct q \ set q = set list)" + have "set (SOME q. distinct q \ set q = set list) = set list" + proof(rule someI2) + from vt_v.wq_distinct[of cs] and eq_wq + show "distinct list \ set list = set list" by auto + next + show "\x. distinct x \ set x = set list \ set x = set list" + by auto + qed + moreover have "distinct (hd (SOME q. distinct q \ set q = set list) # list)" + proof - + from vt_v.wq_distinct[of cs] and eq_wq + show ?thesis by auto + qed + moreover note eq_wq and hd_in + ultimately show "False" by auto + qed + qed +qed + +lemma step_v_get_hold: + "\th'. \vt (V th cs # s); \ holding (wq (V th cs # s)) th' cs; next_th s th cs th'\ \ False" + apply (unfold cs_holding_def next_th_def wq_def, + auto simp:Let_def) +proof - + fix rest + assume vt: "vt (V th cs # s)" + and eq_wq[folded wq_def]: " wq_fun (schs s) cs = th # rest" + and nrest: "rest \ []" + and ni: "hd (SOME q. distinct q \ set q = set rest) + \ set (SOME q. distinct q \ set q = set rest)" + from vt interpret vt_v: valid_trace_e s "V th cs" + by (cases, unfold_locales, simp+) + have "(SOME q. distinct q \ set q = set rest) \ []" + proof(rule someI2) + from vt_v.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" by auto + next + fix x assume "distinct x \ set x = set rest" + hence "set x = set rest" by auto + with nrest + show "x \ []" by (case_tac x, auto) + qed + with ni show "False" by auto +qed + +lemma step_v_release_inv[elim_format]: +"\c t. \vt (V th cs # s); \ holding (wq (V th cs # s)) t c; holding (wq s) t c\ \ + c = cs \ t = th" + apply (unfold cs_holding_def wq_def, auto simp:Let_def split:if_splits list.splits) + proof - + fix a list + assume vt: "vt (V th cs # s)" and eq_wq: "wq_fun (schs s) cs = a # list" + from step_back_step [OF vt] show "a = th" + proof(cases) + assume "holding s th cs" with eq_wq + show ?thesis + by (unfold s_holding_def wq_def, auto) + qed + next + fix a list + assume vt: "vt (V th cs # s)" and eq_wq: "wq_fun (schs s) cs = a # list" + from step_back_step [OF vt] show "a = th" + proof(cases) + assume "holding s th cs" with eq_wq + show ?thesis + by (unfold s_holding_def wq_def, auto) + qed + qed + +lemma step_v_waiting_mono: + "\t c. \vt (V th cs # s); waiting (wq (V th cs # s)) t c\ \ waiting (wq s) t c" +proof - + fix t c + let ?s' = "(V th cs # s)" + assume vt: "vt ?s'" + and wt: "waiting (wq ?s') t c" + from vt interpret vt_v: valid_trace_e s "V th cs" + by (cases, unfold_locales, simp+) + show "waiting (wq s) t c" + proof(cases "c = cs") + case False + assume neq_cs: "c \ cs" + hence "waiting (wq ?s') t c = waiting (wq s) t c" + by (unfold cs_waiting_def wq_def, auto simp:Let_def) + with wt show ?thesis by simp + next + case True + with wt show ?thesis + apply (unfold cs_waiting_def wq_def, auto simp:Let_def split:list.splits) + proof - + fix a list + assume not_in: "t \ set list" + and is_in: "t \ set (SOME q. distinct q \ set q = set list)" + and eq_wq: "wq_fun (schs s) cs = a # list" + have "set (SOME q. distinct q \ set q = set list) = set list" + proof(rule someI2) + from vt_v.wq_distinct [of cs] + and eq_wq[folded wq_def] + show "distinct list \ set list = set list" by auto + next + fix x assume "distinct x \ set x = set list" + thus "set x = set list" by auto + qed + with not_in is_in show "t = a" by auto + next + fix list + assume is_waiting: "waiting (wq (V th cs # s)) t cs" + and eq_wq: "wq_fun (schs s) cs = t # list" + hence "t \ set list" + apply (unfold wq_def, auto simp:Let_def cs_waiting_def) + proof - + assume " t \ set (SOME q. distinct q \ set q = set list)" + moreover have "\ = set list" + proof(rule someI2) + from vt_v.wq_distinct [of cs] + and eq_wq[folded wq_def] + show "distinct list \ set list = set list" by auto + next + fix x assume "distinct x \ set x = set list" + thus "set x = set list" by auto + qed + ultimately show "t \ set list" by simp + qed + with eq_wq and vt_v.wq_distinct [of cs, unfolded wq_def] + show False by auto + qed + qed +qed + +text {* (* ddd *) + The following @{text "step_RAG_v"} lemma charaterizes how @{text "RAG"} is changed + with the happening of @{text "V"}-events: +*} +lemma step_RAG_v: +assumes vt: + "vt (V th cs#s)" +shows " + RAG (V th cs # s) = + RAG s - {(Cs cs, Th th)} - + {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'}" + apply (insert vt, unfold s_RAG_def) + apply (auto split:if_splits list.splits simp:Let_def) + apply (auto elim: step_v_waiting_mono step_v_hold_inv + step_v_release step_v_wait_inv + step_v_get_hold step_v_release_inv) + apply (erule_tac step_v_not_wait, auto) + done + +text {* + The following @{text "step_RAG_p"} lemma charaterizes how @{text "RAG"} is changed + with the happening of @{text "P"}-events: +*} +lemma step_RAG_p: + "vt (P th cs#s) \ + RAG (P th cs # s) = (if (wq s cs = []) then RAG s \ {(Cs cs, Th th)} + else RAG s \ {(Th th, Cs cs)})" + apply(simp only: s_RAG_def wq_def) + apply (auto split:list.splits prod.splits simp:Let_def wq_def cs_waiting_def cs_holding_def) + apply(case_tac "csa = cs", auto) + apply(fold wq_def) + apply(drule_tac step_back_step) + apply(ind_cases " step s (P (hd (wq s cs)) cs)") + apply(simp add:s_RAG_def wq_def cs_holding_def) + apply(auto) + done + + +lemma RAG_target_th: "(Th th, x) \ RAG (s::state) \ \ cs. x = Cs cs" + by (unfold s_RAG_def, auto) + +context valid_trace +begin + +text {* + The following lemma shows that @{text "RAG"} is acyclic. + The overall structure is by induction on the formation of @{text "vt s"} + and then case analysis on event @{text "e"}, where the non-trivial cases + for those for @{text "V"} and @{text "P"} events. +*} +lemma acyclic_RAG: + shows "acyclic (RAG s)" +using vt +proof(induct) + case (vt_cons s e) + interpret vt_s: valid_trace s using vt_cons(1) + by (unfold_locales, simp) + assume ih: "acyclic (RAG s)" + and stp: "step s e" + and vt: "vt s" + show ?case + proof(cases e) + case (Create th prio) + with ih + show ?thesis by (simp add:RAG_create_unchanged) + next + case (Exit th) + with ih show ?thesis by (simp add:RAG_exit_unchanged) + next + case (V th cs) + from V vt stp have vtt: "vt (V th cs#s)" by auto + from step_RAG_v [OF this] + have eq_de: + "RAG (e # s) = + RAG s - {(Cs cs, Th th)} - {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'}" + (is "?L = (?A - ?B - ?C) \ ?D") by (simp add:V) + from ih have ac: "acyclic (?A - ?B - ?C)" by (auto elim:acyclic_subset) + from step_back_step [OF vtt] + have "step s (V th cs)" . + thus ?thesis + proof(cases) + assume "holding s th cs" + hence th_in: "th \ set (wq s cs)" and + eq_hd: "th = hd (wq s cs)" unfolding s_holding_def wq_def by auto + then obtain rest where + eq_wq: "wq s cs = th#rest" + by (cases "wq s cs", auto) + show ?thesis + proof(cases "rest = []") + case False + let ?th' = "hd (SOME q. distinct q \ set q = set rest)" + from eq_wq False have eq_D: "?D = {(Cs cs, Th ?th')}" + by (unfold next_th_def, auto) + let ?E = "(?A - ?B - ?C)" + have "(Th ?th', Cs cs) \ ?E\<^sup>*" + proof + assume "(Th ?th', Cs cs) \ ?E\<^sup>*" + hence " (Th ?th', Cs cs) \ ?E\<^sup>+" by (simp add: rtrancl_eq_or_trancl) + from tranclD [OF this] + obtain x where th'_e: "(Th ?th', x) \ ?E" by blast + hence th_d: "(Th ?th', x) \ ?A" by simp + from RAG_target_th [OF this] + obtain cs' where eq_x: "x = Cs cs'" by auto + with th_d have "(Th ?th', Cs cs') \ ?A" by simp + hence wt_th': "waiting s ?th' cs'" + unfolding s_RAG_def s_waiting_def cs_waiting_def wq_def by simp + hence "cs' = cs" + proof(rule vt_s.waiting_unique) + from eq_wq vt_s.wq_distinct[of cs] + show "waiting s ?th' cs" + apply (unfold s_waiting_def wq_def, auto) + proof - + assume hd_in: "hd (SOME q. distinct q \ set q = set rest) \ set rest" + and eq_wq: "wq_fun (schs s) cs = th # rest" + have "(SOME q. distinct q \ set q = set rest) \ []" + proof(rule someI2) + from vt_s.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" unfolding wq_def by auto + next + fix x assume "distinct x \ set x = set rest" + with False show "x \ []" by auto + qed + hence "hd (SOME q. distinct q \ set q = set rest) \ + set (SOME q. distinct q \ set q = set rest)" by auto + moreover have "\ = set rest" + proof(rule someI2) + from vt_s.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" unfolding wq_def by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" by auto + qed + moreover note hd_in + ultimately show "hd (SOME q. distinct q \ set q = set rest) = th" by auto + next + assume hd_in: "hd (SOME q. distinct q \ set q = set rest) \ set rest" + and eq_wq: "wq s cs = hd (SOME q. distinct q \ set q = set rest) # rest" + have "(SOME q. distinct q \ set q = set rest) \ []" + proof(rule someI2) + from vt_s.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" by auto + next + fix x assume "distinct x \ set x = set rest" + with False show "x \ []" by auto + qed + hence "hd (SOME q. distinct q \ set q = set rest) \ + set (SOME q. distinct q \ set q = set rest)" by auto + moreover have "\ = set rest" + proof(rule someI2) + from vt_s.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" by auto + qed + moreover note hd_in + ultimately show False by auto + qed + qed + with th'_e eq_x have "(Th ?th', Cs cs) \ ?E" by simp + with False + show "False" by (auto simp: next_th_def eq_wq) + qed + with acyclic_insert[symmetric] and ac + and eq_de eq_D show ?thesis by auto + next + case True + with eq_wq + have eq_D: "?D = {}" + by (unfold next_th_def, auto) + with eq_de ac + show ?thesis by auto + qed + qed + next + case (P th cs) + from P vt stp have vtt: "vt (P th cs#s)" by auto + from step_RAG_p [OF this] P + have "RAG (e # s) = + (if wq s cs = [] then RAG s \ {(Cs cs, Th th)} else + RAG s \ {(Th th, Cs cs)})" (is "?L = ?R") + by simp + moreover have "acyclic ?R" + proof(cases "wq s cs = []") + case True + hence eq_r: "?R = RAG s \ {(Cs cs, Th th)}" by simp + have "(Th th, Cs cs) \ (RAG s)\<^sup>*" + proof + assume "(Th th, Cs cs) \ (RAG s)\<^sup>*" + hence "(Th th, Cs cs) \ (RAG s)\<^sup>+" by (simp add: rtrancl_eq_or_trancl) + from tranclD2 [OF this] + obtain x where "(x, Cs cs) \ RAG s" by auto + with True show False by (auto simp:s_RAG_def cs_waiting_def) + qed + with acyclic_insert ih eq_r show ?thesis by auto + next + case False + hence eq_r: "?R = RAG s \ {(Th th, Cs cs)}" by simp + have "(Cs cs, Th th) \ (RAG s)\<^sup>*" + proof + assume "(Cs cs, Th th) \ (RAG s)\<^sup>*" + hence "(Cs cs, Th th) \ (RAG s)\<^sup>+" by (simp add: rtrancl_eq_or_trancl) + moreover from step_back_step [OF vtt] have "step s (P th cs)" . + ultimately show False + proof - + show " \(Cs cs, Th th) \ (RAG s)\<^sup>+; step s (P th cs)\ \ False" + by (ind_cases "step s (P th cs)", simp) + qed + qed + with acyclic_insert ih eq_r show ?thesis by auto + qed + ultimately show ?thesis by simp + next + case (Set thread prio) + with ih + thm RAG_set_unchanged + show ?thesis by (simp add:RAG_set_unchanged) + qed + next + case vt_nil + show "acyclic (RAG ([]::state))" + by (auto simp: s_RAG_def cs_waiting_def + cs_holding_def wq_def acyclic_def) +qed + + +lemma finite_RAG: + shows "finite (RAG s)" +proof - + from vt show ?thesis + proof(induct) + case (vt_cons s e) + interpret vt_s: valid_trace s using vt_cons(1) + by (unfold_locales, simp) + assume ih: "finite (RAG s)" + and stp: "step s e" + and vt: "vt s" + show ?case + proof(cases e) + case (Create th prio) + with ih + show ?thesis by (simp add:RAG_create_unchanged) + next + case (Exit th) + with ih show ?thesis by (simp add:RAG_exit_unchanged) + next + case (V th cs) + from V vt stp have vtt: "vt (V th cs#s)" by auto + from step_RAG_v [OF this] + have eq_de: "RAG (e # s) = + RAG s - {(Cs cs, Th th)} - {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'} +" + (is "?L = (?A - ?B - ?C) \ ?D") by (simp add:V) + moreover from ih have ac: "finite (?A - ?B - ?C)" by simp + moreover have "finite ?D" + proof - + have "?D = {} \ (\ a. ?D = {a})" + by (unfold next_th_def, auto) + thus ?thesis + proof + assume h: "?D = {}" + show ?thesis by (unfold h, simp) + next + assume "\ a. ?D = {a}" + thus ?thesis + by (metis finite.simps) + qed + qed + ultimately show ?thesis by simp + next + case (P th cs) + from P vt stp have vtt: "vt (P th cs#s)" by auto + from step_RAG_p [OF this] P + have "RAG (e # s) = + (if wq s cs = [] then RAG s \ {(Cs cs, Th th)} else + RAG s \ {(Th th, Cs cs)})" (is "?L = ?R") + by simp + moreover have "finite ?R" + proof(cases "wq s cs = []") + case True + hence eq_r: "?R = RAG s \ {(Cs cs, Th th)}" by simp + with True and ih show ?thesis by auto + next + case False + hence "?R = RAG s \ {(Th th, Cs cs)}" by simp + with False and ih show ?thesis by auto + qed + ultimately show ?thesis by auto + next + case (Set thread prio) + with ih + show ?thesis by (simp add:RAG_set_unchanged) + qed + next + case vt_nil + show "finite (RAG ([]::state))" + by (auto simp: s_RAG_def cs_waiting_def + cs_holding_def wq_def acyclic_def) + qed +qed + +text {* Several useful lemmas *} + +lemma wf_dep_converse: + shows "wf ((RAG s)^-1)" +proof(rule finite_acyclic_wf_converse) + from finite_RAG + show "finite (RAG s)" . +next + from acyclic_RAG + show "acyclic (RAG s)" . +qed + +end + +lemma hd_np_in: "x \ set l \ hd l \ set l" + by (induct l, auto) + +lemma th_chasing: "(Th th, Cs cs) \ RAG (s::state) \ \ th'. (Cs cs, Th th') \ RAG s" + by (auto simp:s_RAG_def s_holding_def cs_holding_def cs_waiting_def wq_def dest:hd_np_in) + +context valid_trace +begin + +lemma wq_threads: + assumes h: "th \ set (wq s cs)" + shows "th \ threads s" +proof - + from vt and h show ?thesis + proof(induct arbitrary: th cs) + case (vt_cons s e) + interpret vt_s: valid_trace s + using vt_cons(1) by (unfold_locales, auto) + assume ih: "\th cs. th \ set (wq s cs) \ th \ threads s" + and stp: "step s e" + and vt: "vt s" + and h: "th \ set (wq (e # s) cs)" + show ?case + proof(cases e) + case (Create th' prio) + with ih h show ?thesis + by (auto simp:wq_def Let_def) + next + case (Exit th') + with stp ih h show ?thesis + apply (auto simp:wq_def Let_def) + apply (ind_cases "step s (Exit th')") + apply (auto simp:runing_def readys_def s_holding_def s_waiting_def holdents_def + s_RAG_def s_holding_def cs_holding_def) + done + next + case (V th' cs') + show ?thesis + proof(cases "cs' = cs") + case False + with h + show ?thesis + apply(unfold wq_def V, auto simp:Let_def V split:prod.splits, fold wq_def) + by (drule_tac ih, simp) + next + case True + from h + show ?thesis + proof(unfold V wq_def) + assume th_in: "th \ set (wq_fun (schs (V th' cs' # s)) cs)" (is "th \ set ?l") + show "th \ threads (V th' cs' # s)" + proof(cases "cs = cs'") + case False + hence "?l = wq_fun (schs s) cs" by (simp add:Let_def) + with th_in have " th \ set (wq s cs)" + by (fold wq_def, simp) + from ih [OF this] show ?thesis by simp + next + case True + show ?thesis + proof(cases "wq_fun (schs s) cs'") + case Nil + with h V show ?thesis + apply (auto simp:wq_def Let_def split:if_splits) + by (fold wq_def, drule_tac ih, simp) + next + case (Cons a rest) + assume eq_wq: "wq_fun (schs s) cs' = a # rest" + with h V show ?thesis + apply (auto simp:Let_def wq_def split:if_splits) + proof - + assume th_in: "th \ set (SOME q. distinct q \ set q = set rest)" + have "set (SOME q. distinct q \ set q = set rest) = set rest" + proof(rule someI2) + from vt_s.wq_distinct[of cs'] and eq_wq[folded wq_def] + show "distinct rest \ set rest = set rest" by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" + by auto + qed + with eq_wq th_in have "th \ set (wq_fun (schs s) cs')" by auto + from ih[OF this[folded wq_def]] show "th \ threads s" . + next + assume th_in: "th \ set (wq_fun (schs s) cs)" + from ih[OF this[folded wq_def]] + show "th \ threads s" . + qed + qed + qed + qed + qed + next + case (P th' cs') + from h stp + show ?thesis + apply (unfold P wq_def) + apply (auto simp:Let_def split:if_splits, fold wq_def) + apply (auto intro:ih) + apply(ind_cases "step s (P th' cs')") + by (unfold runing_def readys_def, auto) + next + case (Set thread prio) + with ih h show ?thesis + by (auto simp:wq_def Let_def) + qed + next + case vt_nil + thus ?case by (auto simp:wq_def) + qed +qed + +lemma range_in: "\(Th th) \ Range (RAG (s::state))\ \ th \ threads s" + apply(unfold s_RAG_def cs_waiting_def cs_holding_def) + by (auto intro:wq_threads) + +lemma readys_v_eq: + assumes neq_th: "th \ thread" + and eq_wq: "wq s cs = thread#rest" + and not_in: "th \ set rest" + shows "(th \ readys (V thread cs#s)) = (th \ readys s)" +proof - + from assms show ?thesis + apply (auto simp:readys_def) + apply(simp add:s_waiting_def[folded wq_def]) + apply (erule_tac x = csa in allE) + apply (simp add:s_waiting_def wq_def Let_def split:if_splits) + apply (case_tac "csa = cs", simp) + apply (erule_tac x = cs in allE) + apply(auto simp add: s_waiting_def[folded wq_def] Let_def split: list.splits) + apply(auto simp add: wq_def) + apply (auto simp:s_waiting_def wq_def Let_def split:list.splits) + proof - + assume th_nin: "th \ set rest" + and th_in: "th \ set (SOME q. distinct q \ set q = set rest)" + and eq_wq: "wq_fun (schs s) cs = thread # rest" + have "set (SOME q. distinct q \ set q = set rest) = set rest" + proof(rule someI2) + from wq_distinct[of cs, unfolded wq_def] and eq_wq[unfolded wq_def] + show "distinct rest \ set rest = set rest" by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" by auto + qed + with th_nin th_in show False by auto + qed +qed + +text {* \noindent + The following lemmas shows that: starting from any node in @{text "RAG"}, + by chasing out-going edges, it is always possible to reach a node representing a ready + thread. In this lemma, it is the @{text "th'"}. +*} + +lemma chain_building: + shows "node \ Domain (RAG s) \ (\ th'. th' \ readys s \ (node, Th th') \ (RAG s)^+)" +proof - + from wf_dep_converse + have h: "wf ((RAG s)\)" . + show ?thesis + proof(induct rule:wf_induct [OF h]) + fix x + assume ih [rule_format]: + "\y. (y, x) \ (RAG s)\ \ + y \ Domain (RAG s) \ (\th'. th' \ readys s \ (y, Th th') \ (RAG s)\<^sup>+)" + show "x \ Domain (RAG s) \ (\th'. th' \ readys s \ (x, Th th') \ (RAG s)\<^sup>+)" + proof + assume x_d: "x \ Domain (RAG s)" + show "\th'. th' \ readys s \ (x, Th th') \ (RAG s)\<^sup>+" + proof(cases x) + case (Th th) + from x_d Th obtain cs where x_in: "(Th th, Cs cs) \ RAG s" by (auto simp:s_RAG_def) + with Th have x_in_r: "(Cs cs, x) \ (RAG s)^-1" by simp + from th_chasing [OF x_in] obtain th' where "(Cs cs, Th th') \ RAG s" by blast + hence "Cs cs \ Domain (RAG s)" by auto + from ih [OF x_in_r this] obtain th' + where th'_ready: " th' \ readys s" and cs_in: "(Cs cs, Th th') \ (RAG s)\<^sup>+" by auto + have "(x, Th th') \ (RAG s)\<^sup>+" using Th x_in cs_in by auto + with th'_ready show ?thesis by auto + next + case (Cs cs) + from x_d Cs obtain th' where th'_d: "(Th th', x) \ (RAG s)^-1" by (auto simp:s_RAG_def) + show ?thesis + proof(cases "th' \ readys s") + case True + from True and th'_d show ?thesis by auto + next + case False + from th'_d and range_in have "th' \ threads s" by auto + with False have "Th th' \ Domain (RAG s)" + by (auto simp:readys_def wq_def s_waiting_def s_RAG_def cs_waiting_def Domain_def) + from ih [OF th'_d this] + obtain th'' where + th''_r: "th'' \ readys s" and + th''_in: "(Th th', Th th'') \ (RAG s)\<^sup>+" by auto + from th'_d and th''_in + have "(x, Th th'') \ (RAG s)\<^sup>+" by auto + with th''_r show ?thesis by auto + qed + qed + qed + qed +qed + +text {* \noindent + The following is just an instance of @{text "chain_building"}. +*} +lemma th_chain_to_ready: + assumes th_in: "th \ threads s" + shows "th \ readys s \ (\ th'. th' \ readys s \ (Th th, Th th') \ (RAG s)^+)" +proof(cases "th \ readys s") + case True + thus ?thesis by auto +next + case False + from False and th_in have "Th th \ Domain (RAG s)" + by (auto simp:readys_def s_waiting_def s_RAG_def wq_def cs_waiting_def Domain_def) + from chain_building [rule_format, OF this] + show ?thesis by auto +qed + +end + +lemma waiting_eq: "waiting s th cs = waiting (wq s) th cs" + by (unfold s_waiting_def cs_waiting_def wq_def, auto) + +lemma holding_eq: "holding (s::state) th cs = holding (wq s) th cs" + by (unfold s_holding_def wq_def cs_holding_def, simp) + +lemma holding_unique: "\holding (s::state) th1 cs; holding s th2 cs\ \ th1 = th2" + by (unfold s_holding_def cs_holding_def, auto) + +context valid_trace +begin + +lemma unique_RAG: "\(n, n1) \ RAG s; (n, n2) \ RAG s\ \ n1 = n2" + apply(unfold s_RAG_def, auto, fold waiting_eq holding_eq) + by(auto elim:waiting_unique holding_unique) + +end + + +lemma trancl_split: "(a, b) \ r^+ \ \ c. (a, c) \ r" +by (induct rule:trancl_induct, auto) + +context valid_trace +begin + +lemma dchain_unique: + assumes th1_d: "(n, Th th1) \ (RAG s)^+" + and th1_r: "th1 \ readys s" + and th2_d: "(n, Th th2) \ (RAG s)^+" + and th2_r: "th2 \ readys s" + shows "th1 = th2" +proof - + { assume neq: "th1 \ th2" + hence "Th th1 \ Th th2" by simp + from unique_chain [OF _ th1_d th2_d this] and unique_RAG + have "(Th th1, Th th2) \ (RAG s)\<^sup>+ \ (Th th2, Th th1) \ (RAG s)\<^sup>+" by auto + hence "False" + proof + assume "(Th th1, Th th2) \ (RAG s)\<^sup>+" + from trancl_split [OF this] + obtain n where dd: "(Th th1, n) \ RAG s" by auto + then obtain cs where eq_n: "n = Cs cs" + by (auto simp:s_RAG_def s_holding_def cs_holding_def cs_waiting_def wq_def dest:hd_np_in) + from dd eq_n have "th1 \ readys s" + by (auto simp:readys_def s_RAG_def wq_def s_waiting_def cs_waiting_def) + with th1_r show ?thesis by auto + next + assume "(Th th2, Th th1) \ (RAG s)\<^sup>+" + from trancl_split [OF this] + obtain n where dd: "(Th th2, n) \ RAG s" by auto + then obtain cs where eq_n: "n = Cs cs" + by (auto simp:s_RAG_def s_holding_def cs_holding_def cs_waiting_def wq_def dest:hd_np_in) + from dd eq_n have "th2 \ readys s" + by (auto simp:readys_def wq_def s_RAG_def s_waiting_def cs_waiting_def) + with th2_r show ?thesis by auto + qed + } thus ?thesis by auto +qed + +end + + +lemma step_holdents_p_add: + assumes vt: "vt (P th cs#s)" + and "wq s cs = []" + shows "holdents (P th cs#s) th = holdents s th \ {cs}" +proof - + from assms show ?thesis + unfolding holdents_test step_RAG_p[OF vt] by (auto) +qed + +lemma step_holdents_p_eq: + assumes vt: "vt (P th cs#s)" + and "wq s cs \ []" + shows "holdents (P th cs#s) th = holdents s th" +proof - + from assms show ?thesis + unfolding holdents_test step_RAG_p[OF vt] by auto +qed + + +lemma (in valid_trace) finite_holding : + shows "finite (holdents s th)" +proof - + let ?F = "\ (x, y). the_cs x" + from finite_RAG + have "finite (RAG s)" . + hence "finite (?F `(RAG s))" by simp + moreover have "{cs . (Cs cs, Th th) \ RAG s} \ \" + proof - + { have h: "\ a A f. a \ A \ f a \ f ` A" by auto + fix x assume "(Cs x, Th th) \ RAG s" + hence "?F (Cs x, Th th) \ ?F `(RAG s)" by (rule h) + moreover have "?F (Cs x, Th th) = x" by simp + ultimately have "x \ (\(x, y). the_cs x) ` RAG s" by simp + } thus ?thesis by auto + qed + ultimately show ?thesis by (unfold holdents_test, auto intro:finite_subset) +qed + +lemma cntCS_v_dec: + assumes vtv: "vt (V thread cs#s)" + shows "(cntCS (V thread cs#s) thread + 1) = cntCS s thread" +proof - + from vtv interpret vt_s: valid_trace s + by (cases, unfold_locales, simp) + from vtv interpret vt_v: valid_trace "V thread cs#s" + by (unfold_locales, simp) + from step_back_step[OF vtv] + have cs_in: "cs \ holdents s thread" + apply (cases, unfold holdents_test s_RAG_def, simp) + by (unfold cs_holding_def s_holding_def wq_def, auto) + moreover have cs_not_in: + "(holdents (V thread cs#s) thread) = holdents s thread - {cs}" + apply (insert vt_s.wq_distinct[of cs]) + apply (unfold holdents_test, unfold step_RAG_v[OF vtv], + auto simp:next_th_def) + proof - + fix rest + assume dst: "distinct (rest::thread list)" + and ne: "rest \ []" + and hd_ni: "hd (SOME q. distinct q \ set q = set rest) \ set rest" + moreover have "set (SOME q. distinct q \ set q = set rest) = set rest" + proof(rule someI2) + from dst show "distinct rest \ set rest = set rest" by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" by auto + qed + ultimately have "hd (SOME q. distinct q \ set q = set rest) \ + set (SOME q. distinct q \ set q = set rest)" by simp + moreover have "(SOME q. distinct q \ set q = set rest) \ []" + proof(rule someI2) + from dst show "distinct rest \ set rest = set rest" by auto + next + fix x assume " distinct x \ set x = set rest" with ne + show "x \ []" by auto + qed + ultimately + show "(Cs cs, Th (hd (SOME q. distinct q \ set q = set rest))) \ RAG s" + by auto + next + fix rest + assume dst: "distinct (rest::thread list)" + and ne: "rest \ []" + and hd_ni: "hd (SOME q. distinct q \ set q = set rest) \ set rest" + moreover have "set (SOME q. distinct q \ set q = set rest) = set rest" + proof(rule someI2) + from dst show "distinct rest \ set rest = set rest" by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" by auto + qed + ultimately have "hd (SOME q. distinct q \ set q = set rest) \ + set (SOME q. distinct q \ set q = set rest)" by simp + moreover have "(SOME q. distinct q \ set q = set rest) \ []" + proof(rule someI2) + from dst show "distinct rest \ set rest = set rest" by auto + next + fix x assume " distinct x \ set x = set rest" with ne + show "x \ []" by auto + qed + ultimately show "False" by auto + qed + ultimately + have "holdents s thread = insert cs (holdents (V thread cs#s) thread)" + by auto + moreover have "card \ = + Suc (card ((holdents (V thread cs#s) thread) - {cs}))" + proof(rule card_insert) + from vt_v.finite_holding + show " finite (holdents (V thread cs # s) thread)" . + qed + moreover from cs_not_in + have "cs \ (holdents (V thread cs#s) thread)" by auto + ultimately show ?thesis by (simp add:cntCS_def) +qed + +lemma count_rec1 [simp]: + assumes "Q e" + shows "count Q (e#es) = Suc (count Q es)" + using assms + by (unfold count_def, auto) + +lemma count_rec2 [simp]: + assumes "\Q e" + shows "count Q (e#es) = (count Q es)" + using assms + by (unfold count_def, auto) + +lemma count_rec3 [simp]: + shows "count Q [] = 0" + by (unfold count_def, auto) + +lemma cntP_diff_inv: + assumes "cntP (e#s) th \ cntP s th" + shows "isP e \ actor e = th" +proof(cases e) + case (P th' pty) + show ?thesis + by (cases "(\e. \cs. e = P th cs) (P th' pty)", + insert assms P, auto simp:cntP_def) +qed (insert assms, auto simp:cntP_def) + +lemma isP_E: + assumes "isP e" + obtains cs where "e = P (actor e) cs" + using assms by (cases e, auto) + +lemma isV_E: + assumes "isV e" + obtains cs where "e = V (actor e) cs" + using assms by (cases e, auto) (* ccc *) + +lemma cntV_diff_inv: + assumes "cntV (e#s) th \ cntV s th" + shows "isV e \ actor e = th" +proof(cases e) + case (V th' pty) + show ?thesis + by (cases "(\e. \cs. e = V th cs) (V th' pty)", + insert assms V, auto simp:cntV_def) +qed (insert assms, auto simp:cntV_def) + +context valid_trace +begin + +text {* (* ddd *) \noindent + The relationship between @{text "cntP"}, @{text "cntV"} and @{text "cntCS"} + of one particular thread. +*} + +lemma cnp_cnv_cncs: + shows "cntP s th = cntV s th + (if (th \ readys s \ th \ threads s) + then cntCS s th else cntCS s th + 1)" +proof - + from vt show ?thesis + proof(induct arbitrary:th) + case (vt_cons s e) + interpret vt_s: valid_trace s using vt_cons(1) by (unfold_locales, simp) + assume vt: "vt s" + and ih: "\th. cntP s th = cntV s th + + (if (th \ readys s \ th \ threads s) then cntCS s th else cntCS s th + 1)" + and stp: "step s e" + from stp show ?case + proof(cases) + case (thread_create thread prio) + assume eq_e: "e = Create thread prio" + and not_in: "thread \ threads s" + show ?thesis + proof - + { fix cs + assume "thread \ set (wq s cs)" + from vt_s.wq_threads [OF this] have "thread \ threads s" . + with not_in have "False" by simp + } with eq_e have eq_readys: "readys (e#s) = readys s \ {thread}" + by (auto simp:readys_def threads.simps s_waiting_def + wq_def cs_waiting_def Let_def) + from eq_e have eq_cnp: "cntP (e#s) th = cntP s th" by (simp add:cntP_def count_def) + from eq_e have eq_cnv: "cntV (e#s) th = cntV s th" by (simp add:cntV_def count_def) + have eq_cncs: "cntCS (e#s) th = cntCS s th" + unfolding cntCS_def holdents_test + by (simp add:RAG_create_unchanged eq_e) + { assume "th \ thread" + with eq_readys eq_e + have "(th \ readys (e # s) \ th \ threads (e # s)) = + (th \ readys (s) \ th \ threads (s))" + by (simp add:threads.simps) + with eq_cnp eq_cnv eq_cncs ih not_in + have ?thesis by simp + } moreover { + assume eq_th: "th = thread" + with not_in ih have " cntP s th = cntV s th + cntCS s th" by simp + moreover from eq_th and eq_readys have "th \ readys (e#s)" by simp + moreover note eq_cnp eq_cnv eq_cncs + ultimately have ?thesis by auto + } ultimately show ?thesis by blast + qed + next + case (thread_exit thread) + assume eq_e: "e = Exit thread" + and is_runing: "thread \ runing s" + and no_hold: "holdents s thread = {}" + from eq_e have eq_cnp: "cntP (e#s) th = cntP s th" by (simp add:cntP_def count_def) + from eq_e have eq_cnv: "cntV (e#s) th = cntV s th" by (simp add:cntV_def count_def) + have eq_cncs: "cntCS (e#s) th = cntCS s th" + unfolding cntCS_def holdents_test + by (simp add:RAG_exit_unchanged eq_e) + { assume "th \ thread" + with eq_e + have "(th \ readys (e # s) \ th \ threads (e # s)) = + (th \ readys (s) \ th \ threads (s))" + apply (simp add:threads.simps readys_def) + apply (subst s_waiting_def) + apply (simp add:Let_def) + apply (subst s_waiting_def, simp) + done + with eq_cnp eq_cnv eq_cncs ih + have ?thesis by simp + } moreover { + assume eq_th: "th = thread" + with ih is_runing have " cntP s th = cntV s th + cntCS s th" + by (simp add:runing_def) + moreover from eq_th eq_e have "th \ threads (e#s)" + by simp + moreover note eq_cnp eq_cnv eq_cncs + ultimately have ?thesis by auto + } ultimately show ?thesis by blast + next + case (thread_P thread cs) + assume eq_e: "e = P thread cs" + and is_runing: "thread \ runing s" + and no_dep: "(Cs cs, Th thread) \ (RAG s)\<^sup>+" + from thread_P vt stp ih have vtp: "vt (P thread cs#s)" by auto + then interpret vt_p: valid_trace "(P thread cs#s)" + by (unfold_locales, simp) + show ?thesis + proof - + { have hh: "\ A B C. (B = C) \ (A \ B) = (A \ C)" by blast + assume neq_th: "th \ thread" + with eq_e + have eq_readys: "(th \ readys (e#s)) = (th \ readys (s))" + apply (simp add:readys_def s_waiting_def wq_def Let_def) + apply (rule_tac hh) + apply (intro iffI allI, clarify) + apply (erule_tac x = csa in allE, auto) + apply (subgoal_tac "wq_fun (schs s) cs \ []", auto) + apply (erule_tac x = cs in allE, auto) + by (case_tac "(wq_fun (schs s) cs)", auto) + moreover from neq_th eq_e have "cntCS (e # s) th = cntCS s th" + apply (simp add:cntCS_def holdents_test) + by (unfold step_RAG_p [OF vtp], auto) + moreover from eq_e neq_th have "cntP (e # s) th = cntP s th" + by (simp add:cntP_def count_def) + moreover from eq_e neq_th have "cntV (e#s) th = cntV s th" + by (simp add:cntV_def count_def) + moreover from eq_e neq_th have "threads (e#s) = threads s" by simp + moreover note ih [of th] + ultimately have ?thesis by simp + } moreover { + assume eq_th: "th = thread" + have ?thesis + proof - + from eq_e eq_th have eq_cnp: "cntP (e # s) th = 1 + (cntP s th)" + by (simp add:cntP_def count_def) + from eq_e eq_th have eq_cnv: "cntV (e#s) th = cntV s th" + by (simp add:cntV_def count_def) + show ?thesis + proof (cases "wq s cs = []") + case True + with is_runing + have "th \ readys (e#s)" + apply (unfold eq_e wq_def, unfold readys_def s_RAG_def) + apply (simp add: wq_def[symmetric] runing_def eq_th s_waiting_def) + by (auto simp:readys_def wq_def Let_def s_waiting_def wq_def) + moreover have "cntCS (e # s) th = 1 + cntCS s th" + proof - + have "card {csa. csa = cs \ (Cs csa, Th thread) \ RAG s} = + Suc (card {cs. (Cs cs, Th thread) \ RAG s})" (is "card ?L = Suc (card ?R)") + proof - + have "?L = insert cs ?R" by auto + moreover have "card \ = Suc (card (?R - {cs}))" + proof(rule card_insert) + from vt_s.finite_holding [of thread] + show " finite {cs. (Cs cs, Th thread) \ RAG s}" + by (unfold holdents_test, simp) + qed + moreover have "?R - {cs} = ?R" + proof - + have "cs \ ?R" + proof + assume "cs \ {cs. (Cs cs, Th thread) \ RAG s}" + with no_dep show False by auto + qed + thus ?thesis by auto + qed + ultimately show ?thesis by auto + qed + thus ?thesis + apply (unfold eq_e eq_th cntCS_def) + apply (simp add: holdents_test) + by (unfold step_RAG_p [OF vtp], auto simp:True) + qed + moreover from is_runing have "th \ readys s" + by (simp add:runing_def eq_th) + moreover note eq_cnp eq_cnv ih [of th] + ultimately show ?thesis by auto + next + case False + have eq_wq: "wq (e#s) cs = wq s cs @ [th]" + by (unfold eq_th eq_e wq_def, auto simp:Let_def) + have "th \ readys (e#s)" + proof + assume "th \ readys (e#s)" + hence "\cs. \ waiting (e # s) th cs" by (simp add:readys_def) + from this[rule_format, of cs] have " \ waiting (e # s) th cs" . + hence "th \ set (wq (e#s) cs) \ th = hd (wq (e#s) cs)" + by (simp add:s_waiting_def wq_def) + moreover from eq_wq have "th \ set (wq (e#s) cs)" by auto + ultimately have "th = hd (wq (e#s) cs)" by blast + with eq_wq have "th = hd (wq s cs @ [th])" by simp + hence "th = hd (wq s cs)" using False by auto + with False eq_wq vt_p.wq_distinct [of cs] + show False by (fold eq_e, auto) + qed + moreover from is_runing have "th \ threads (e#s)" + by (unfold eq_e, auto simp:runing_def readys_def eq_th) + moreover have "cntCS (e # s) th = cntCS s th" + apply (unfold cntCS_def holdents_test eq_e step_RAG_p[OF vtp]) + by (auto simp:False) + moreover note eq_cnp eq_cnv ih[of th] + moreover from is_runing have "th \ readys s" + by (simp add:runing_def eq_th) + ultimately show ?thesis by auto + qed + qed + } ultimately show ?thesis by blast + qed + next + case (thread_V thread cs) + from assms vt stp ih thread_V have vtv: "vt (V thread cs # s)" by auto + then interpret vt_v: valid_trace "(V thread cs # s)" by (unfold_locales, simp) + assume eq_e: "e = V thread cs" + and is_runing: "thread \ runing s" + and hold: "holding s thread cs" + from hold obtain rest + where eq_wq: "wq s cs = thread # rest" + by (case_tac "wq s cs", auto simp: wq_def s_holding_def) + have eq_threads: "threads (e#s) = threads s" by (simp add: eq_e) + have eq_set: "set (SOME q. distinct q \ set q = set rest) = set rest" + proof(rule someI2) + from vt_v.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" + by (metis distinct.simps(2) vt_s.wq_distinct) + next + show "\x. distinct x \ set x = set rest \ set x = set rest" + by auto + qed + show ?thesis + proof - + { assume eq_th: "th = thread" + from eq_th have eq_cnp: "cntP (e # s) th = cntP s th" + by (unfold eq_e, simp add:cntP_def count_def) + moreover from eq_th have eq_cnv: "cntV (e#s) th = 1 + cntV s th" + by (unfold eq_e, simp add:cntV_def count_def) + moreover from cntCS_v_dec [OF vtv] + have "cntCS (e # s) thread + 1 = cntCS s thread" + by (simp add:eq_e) + moreover from is_runing have rd_before: "thread \ readys s" + by (unfold runing_def, simp) + moreover have "thread \ readys (e # s)" + proof - + from is_runing + have "thread \ threads (e#s)" + by (unfold eq_e, auto simp:runing_def readys_def) + moreover have "\ cs1. \ waiting (e#s) thread cs1" + proof + fix cs1 + { assume eq_cs: "cs1 = cs" + have "\ waiting (e # s) thread cs1" + proof - + from eq_wq + have "thread \ set (wq (e#s) cs1)" + apply(unfold eq_e wq_def eq_cs s_holding_def) + apply (auto simp:Let_def) + proof - + assume "thread \ set (SOME q. distinct q \ set q = set rest)" + with eq_set have "thread \ set rest" by simp + with vt_v.wq_distinct[of cs] + and eq_wq show False + by (metis distinct.simps(2) vt_s.wq_distinct) + qed + thus ?thesis by (simp add:wq_def s_waiting_def) + qed + } moreover { + assume neq_cs: "cs1 \ cs" + have "\ waiting (e # s) thread cs1" + proof - + from wq_v_neq [OF neq_cs[symmetric]] + have "wq (V thread cs # s) cs1 = wq s cs1" . + moreover have "\ waiting s thread cs1" + proof - + from runing_ready and is_runing + have "thread \ readys s" by auto + thus ?thesis by (simp add:readys_def) + qed + ultimately show ?thesis + by (auto simp:wq_def s_waiting_def eq_e) + qed + } ultimately show "\ waiting (e # s) thread cs1" by blast + qed + ultimately show ?thesis by (simp add:readys_def) + qed + moreover note eq_th ih + ultimately have ?thesis by auto + } moreover { + assume neq_th: "th \ thread" + from neq_th eq_e have eq_cnp: "cntP (e # s) th = cntP s th" + by (simp add:cntP_def count_def) + from neq_th eq_e have eq_cnv: "cntV (e # s) th = cntV s th" + by (simp add:cntV_def count_def) + have ?thesis + proof(cases "th \ set rest") + case False + have "(th \ readys (e # s)) = (th \ readys s)" + apply (insert step_back_vt[OF vtv]) + by (simp add: False eq_e eq_wq neq_th vt_s.readys_v_eq) + moreover have "cntCS (e#s) th = cntCS s th" + apply (insert neq_th, unfold eq_e cntCS_def holdents_test step_RAG_v[OF vtv], auto) + proof - + have "{csa. (Cs csa, Th th) \ RAG s \ csa = cs \ next_th s thread cs th} = + {cs. (Cs cs, Th th) \ RAG s}" + proof - + from False eq_wq + have " next_th s thread cs th \ (Cs cs, Th th) \ RAG s" + apply (unfold next_th_def, auto) + proof - + assume ne: "rest \ []" + and ni: "hd (SOME q. distinct q \ set q = set rest) \ set rest" + and eq_wq: "wq s cs = thread # rest" + from eq_set ni have "hd (SOME q. distinct q \ set q = set rest) \ + set (SOME q. distinct q \ set q = set rest) + " by simp + moreover have "(SOME q. distinct q \ set q = set rest) \ []" + proof(rule someI2) + from vt_s.wq_distinct[ of cs] and eq_wq + show "distinct rest \ set rest = set rest" by auto + next + fix x assume "distinct x \ set x = set rest" + with ne show "x \ []" by auto + qed + ultimately show + "(Cs cs, Th (hd (SOME q. distinct q \ set q = set rest))) \ RAG s" + by auto + qed + thus ?thesis by auto + qed + thus "card {csa. (Cs csa, Th th) \ RAG s \ csa = cs \ next_th s thread cs th} = + card {cs. (Cs cs, Th th) \ RAG s}" by simp + qed + moreover note ih eq_cnp eq_cnv eq_threads + ultimately show ?thesis by auto + next + case True + assume th_in: "th \ set rest" + show ?thesis + proof(cases "next_th s thread cs th") + case False + with eq_wq and th_in have + neq_hd: "th \ hd (SOME q. distinct q \ set q = set rest)" (is "th \ hd ?rest") + by (auto simp:next_th_def) + have "(th \ readys (e # s)) = (th \ readys s)" + proof - + from eq_wq and th_in + have "\ th \ readys s" + apply (auto simp:readys_def s_waiting_def) + apply (rule_tac x = cs in exI, auto) + by (insert vt_s.wq_distinct[of cs], auto simp add: wq_def) + moreover + from eq_wq and th_in and neq_hd + have "\ (th \ readys (e # s))" + apply (auto simp:readys_def s_waiting_def eq_e wq_def Let_def split:list.splits) + by (rule_tac x = cs in exI, auto simp:eq_set) + ultimately show ?thesis by auto + qed + moreover have "cntCS (e#s) th = cntCS s th" + proof - + from eq_wq and th_in and neq_hd + have "(holdents (e # s) th) = (holdents s th)" + apply (unfold eq_e step_RAG_v[OF vtv], + auto simp:next_th_def eq_set s_RAG_def holdents_test wq_def + Let_def cs_holding_def) + by (insert vt_s.wq_distinct[of cs], auto simp:wq_def) + thus ?thesis by (simp add:cntCS_def) + qed + moreover note ih eq_cnp eq_cnv eq_threads + ultimately show ?thesis by auto + next + case True + let ?rest = " (SOME q. distinct q \ set q = set rest)" + let ?t = "hd ?rest" + from True eq_wq th_in neq_th + have "th \ readys (e # s)" + apply (auto simp:eq_e readys_def s_waiting_def wq_def + Let_def next_th_def) + proof - + assume eq_wq: "wq_fun (schs s) cs = thread # rest" + and t_in: "?t \ set rest" + show "?t \ threads s" + proof(rule vt_s.wq_threads) + from eq_wq and t_in + show "?t \ set (wq s cs)" by (auto simp:wq_def) + qed + next + fix csa + assume eq_wq: "wq_fun (schs s) cs = thread # rest" + and t_in: "?t \ set rest" + and neq_cs: "csa \ cs" + and t_in': "?t \ set (wq_fun (schs s) csa)" + show "?t = hd (wq_fun (schs s) csa)" + proof - + { assume neq_hd': "?t \ hd (wq_fun (schs s) csa)" + from vt_s.wq_distinct[of cs] and + eq_wq[folded wq_def] and t_in eq_wq + have "?t \ thread" by auto + with eq_wq and t_in + have w1: "waiting s ?t cs" + by (auto simp:s_waiting_def wq_def) + from t_in' neq_hd' + have w2: "waiting s ?t csa" + by (auto simp:s_waiting_def wq_def) + from vt_s.waiting_unique[OF w1 w2] + and neq_cs have "False" by auto + } thus ?thesis by auto + qed + qed + moreover have "cntP s th = cntV s th + cntCS s th + 1" + proof - + have "th \ readys s" + proof - + from True eq_wq neq_th th_in + show ?thesis + apply (unfold readys_def s_waiting_def, auto) + by (rule_tac x = cs in exI, auto simp add: wq_def) + qed + moreover have "th \ threads s" + proof - + from th_in eq_wq + have "th \ set (wq s cs)" by simp + from vt_s.wq_threads [OF this] + show ?thesis . + qed + ultimately show ?thesis using ih by auto + qed + moreover from True neq_th have "cntCS (e # s) th = 1 + cntCS s th" + apply (unfold cntCS_def holdents_test eq_e step_RAG_v[OF vtv], auto) + proof - + show "card {csa. (Cs csa, Th th) \ RAG s \ csa = cs} = + Suc (card {cs. (Cs cs, Th th) \ RAG s})" + (is "card ?A = Suc (card ?B)") + proof - + have "?A = insert cs ?B" by auto + hence "card ?A = card (insert cs ?B)" by simp + also have "\ = Suc (card ?B)" + proof(rule card_insert_disjoint) + have "?B \ ((\ (x, y). the_cs x) ` RAG s)" + apply (auto simp:image_def) + by (rule_tac x = "(Cs x, Th th)" in bexI, auto) + with vt_s.finite_RAG + show "finite {cs. (Cs cs, Th th) \ RAG s}" by (auto intro:finite_subset) + next + show "cs \ {cs. (Cs cs, Th th) \ RAG s}" + proof + assume "cs \ {cs. (Cs cs, Th th) \ RAG s}" + hence "(Cs cs, Th th) \ RAG s" by simp + with True neq_th eq_wq show False + by (auto simp:next_th_def s_RAG_def cs_holding_def) + qed + qed + finally show ?thesis . + qed + qed + moreover note eq_cnp eq_cnv + ultimately show ?thesis by simp + qed + qed + } ultimately show ?thesis by blast + qed + next + case (thread_set thread prio) + assume eq_e: "e = Set thread prio" + and is_runing: "thread \ runing s" + show ?thesis + proof - + from eq_e have eq_cnp: "cntP (e#s) th = cntP s th" by (simp add:cntP_def count_def) + from eq_e have eq_cnv: "cntV (e#s) th = cntV s th" by (simp add:cntV_def count_def) + have eq_cncs: "cntCS (e#s) th = cntCS s th" + unfolding cntCS_def holdents_test + by (simp add:RAG_set_unchanged eq_e) + from eq_e have eq_readys: "readys (e#s) = readys s" + by (simp add:readys_def cs_waiting_def s_waiting_def wq_def, + auto simp:Let_def) + { assume "th \ thread" + with eq_readys eq_e + have "(th \ readys (e # s) \ th \ threads (e # s)) = + (th \ readys (s) \ th \ threads (s))" + by (simp add:threads.simps) + with eq_cnp eq_cnv eq_cncs ih is_runing + have ?thesis by simp + } moreover { + assume eq_th: "th = thread" + with is_runing ih have " cntP s th = cntV s th + cntCS s th" + by (unfold runing_def, auto) + moreover from eq_th and eq_readys is_runing have "th \ readys (e#s)" + by (simp add:runing_def) + moreover note eq_cnp eq_cnv eq_cncs + ultimately have ?thesis by auto + } ultimately show ?thesis by blast + qed + qed + next + case vt_nil + show ?case + by (unfold cntP_def cntV_def cntCS_def, + auto simp:count_def holdents_test s_RAG_def wq_def cs_holding_def) + qed +qed + +lemma not_thread_cncs: + assumes not_in: "th \ threads s" + shows "cntCS s th = 0" +proof - + from vt not_in show ?thesis + proof(induct arbitrary:th) + case (vt_cons s e th) + interpret vt_s: valid_trace s using vt_cons(1) + by (unfold_locales, simp) + assume vt: "vt s" + and ih: "\th. th \ threads s \ cntCS s th = 0" + and stp: "step s e" + and not_in: "th \ threads (e # s)" + from stp show ?case + proof(cases) + case (thread_create thread prio) + assume eq_e: "e = Create thread prio" + and not_in': "thread \ threads s" + have "cntCS (e # s) th = cntCS s th" + apply (unfold eq_e cntCS_def holdents_test) + by (simp add:RAG_create_unchanged) + moreover have "th \ threads s" + proof - + from not_in eq_e show ?thesis by simp + qed + moreover note ih ultimately show ?thesis by auto + next + case (thread_exit thread) + assume eq_e: "e = Exit thread" + and nh: "holdents s thread = {}" + have eq_cns: "cntCS (e # s) th = cntCS s th" + apply (unfold eq_e cntCS_def holdents_test) + by (simp add:RAG_exit_unchanged) + show ?thesis + proof(cases "th = thread") + case True + have "cntCS s th = 0" by (unfold cntCS_def, auto simp:nh True) + with eq_cns show ?thesis by simp + next + case False + with not_in and eq_e + have "th \ threads s" by simp + from ih[OF this] and eq_cns show ?thesis by simp + qed + next + case (thread_P thread cs) + assume eq_e: "e = P thread cs" + and is_runing: "thread \ runing s" + from assms thread_P ih vt stp thread_P have vtp: "vt (P thread cs#s)" by auto + have neq_th: "th \ thread" + proof - + from not_in eq_e have "th \ threads s" by simp + moreover from is_runing have "thread \ threads s" + by (simp add:runing_def readys_def) + ultimately show ?thesis by auto + qed + hence "cntCS (e # s) th = cntCS s th " + apply (unfold cntCS_def holdents_test eq_e) + by (unfold step_RAG_p[OF vtp], auto) + moreover have "cntCS s th = 0" + proof(rule ih) + from not_in eq_e show "th \ threads s" by simp + qed + ultimately show ?thesis by simp + next + case (thread_V thread cs) + assume eq_e: "e = V thread cs" + and is_runing: "thread \ runing s" + and hold: "holding s thread cs" + have neq_th: "th \ thread" + proof - + from not_in eq_e have "th \ threads s" by simp + moreover from is_runing have "thread \ threads s" + by (simp add:runing_def readys_def) + ultimately show ?thesis by auto + qed + from assms thread_V vt stp ih + have vtv: "vt (V thread cs#s)" by auto + then interpret vt_v: valid_trace "(V thread cs#s)" + by (unfold_locales, simp) + from hold obtain rest + where eq_wq: "wq s cs = thread # rest" + by (case_tac "wq s cs", auto simp: wq_def s_holding_def) + from not_in eq_e eq_wq + have "\ next_th s thread cs th" + apply (auto simp:next_th_def) + proof - + assume ne: "rest \ []" + and ni: "hd (SOME q. distinct q \ set q = set rest) \ threads s" (is "?t \ threads s") + have "?t \ set rest" + proof(rule someI2) + from vt_v.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" + by (metis distinct.simps(2) vt_s.wq_distinct) + next + fix x assume "distinct x \ set x = set rest" with ne + show "hd x \ set rest" by (cases x, auto) + qed + with eq_wq have "?t \ set (wq s cs)" by simp + from vt_s.wq_threads[OF this] and ni + show False + using `hd (SOME q. distinct q \ set q = set rest) \ set (wq s cs)` + ni vt_s.wq_threads by blast + qed + moreover note neq_th eq_wq + ultimately have "cntCS (e # s) th = cntCS s th" + by (unfold eq_e cntCS_def holdents_test step_RAG_v[OF vtv], auto) + moreover have "cntCS s th = 0" + proof(rule ih) + from not_in eq_e show "th \ threads s" by simp + qed + ultimately show ?thesis by simp + next + case (thread_set thread prio) + print_facts + assume eq_e: "e = Set thread prio" + and is_runing: "thread \ runing s" + from not_in and eq_e have "th \ threads s" by auto + from ih [OF this] and eq_e + show ?thesis + apply (unfold eq_e cntCS_def holdents_test) + by (simp add:RAG_set_unchanged) + qed + next + case vt_nil + show ?case + by (unfold cntCS_def, + auto simp:count_def holdents_test s_RAG_def wq_def cs_holding_def) + qed +qed + +end + +lemma eq_waiting: "waiting (wq (s::state)) th cs = waiting s th cs" + by (auto simp:s_waiting_def cs_waiting_def wq_def) + +context valid_trace +begin + +lemma dm_RAG_threads: + assumes in_dom: "(Th th) \ Domain (RAG s)" + shows "th \ threads s" +proof - + from in_dom obtain n where "(Th th, n) \ RAG s" by auto + moreover from RAG_target_th[OF this] obtain cs where "n = Cs cs" by auto + ultimately have "(Th th, Cs cs) \ RAG s" by simp + hence "th \ set (wq s cs)" + by (unfold s_RAG_def, auto simp:cs_waiting_def) + from wq_threads [OF this] show ?thesis . +qed + +end + +lemma cp_eq_cpreced: "cp s th = cpreced (wq s) s th" +unfolding cp_def wq_def +apply(induct s rule: schs.induct) +thm cpreced_initial +apply(simp add: Let_def cpreced_initial) +apply(simp add: Let_def) +apply(simp add: Let_def) +apply(simp add: Let_def) +apply(subst (2) schs.simps) +apply(simp add: Let_def) +apply(subst (2) schs.simps) +apply(simp add: Let_def) +done + +context valid_trace +begin + +lemma runing_unique: + assumes runing_1: "th1 \ runing s" + and runing_2: "th2 \ runing s" + shows "th1 = th2" +proof - + from runing_1 and runing_2 have "cp s th1 = cp s th2" + unfolding runing_def + apply(simp) + done + hence eq_max: "Max ((\th. preced th s) ` ({th1} \ dependants (wq s) th1)) = + Max ((\th. preced th s) ` ({th2} \ dependants (wq s) th2))" + (is "Max (?f ` ?A) = Max (?f ` ?B)") + unfolding cp_eq_cpreced + unfolding cpreced_def . + obtain th1' where th1_in: "th1' \ ?A" and eq_f_th1: "?f th1' = Max (?f ` ?A)" + proof - + have h1: "finite (?f ` ?A)" + proof - + have "finite ?A" + proof - + have "finite (dependants (wq s) th1)" + proof- + have "finite {th'. (Th th', Th th1) \ (RAG (wq s))\<^sup>+}" + proof - + let ?F = "\ (x, y). the_th x" + have "{th'. (Th th', Th th1) \ (RAG (wq s))\<^sup>+} \ ?F ` ((RAG (wq s))\<^sup>+)" + apply (auto simp:image_def) + by (rule_tac x = "(Th x, Th th1)" in bexI, auto) + moreover have "finite \" + proof - + from finite_RAG have "finite (RAG s)" . + hence "finite ((RAG (wq s))\<^sup>+)" + apply (unfold finite_trancl) + by (auto simp: s_RAG_def cs_RAG_def wq_def) + thus ?thesis by auto + qed + ultimately show ?thesis by (auto intro:finite_subset) + qed + thus ?thesis by (simp add:cs_dependants_def) + qed + thus ?thesis by simp + qed + thus ?thesis by auto + qed + moreover have h2: "(?f ` ?A) \ {}" + proof - + have "?A \ {}" by simp + thus ?thesis by simp + qed + from Max_in [OF h1 h2] + have "Max (?f ` ?A) \ (?f ` ?A)" . + thus ?thesis + thm cpreced_def + unfolding cpreced_def[symmetric] + unfolding cp_eq_cpreced[symmetric] + unfolding cpreced_def + using that[intro] by (auto) + qed + obtain th2' where th2_in: "th2' \ ?B" and eq_f_th2: "?f th2' = Max (?f ` ?B)" + proof - + have h1: "finite (?f ` ?B)" + proof - + have "finite ?B" + proof - + have "finite (dependants (wq s) th2)" + proof- + have "finite {th'. (Th th', Th th2) \ (RAG (wq s))\<^sup>+}" + proof - + let ?F = "\ (x, y). the_th x" + have "{th'. (Th th', Th th2) \ (RAG (wq s))\<^sup>+} \ ?F ` ((RAG (wq s))\<^sup>+)" + apply (auto simp:image_def) + by (rule_tac x = "(Th x, Th th2)" in bexI, auto) + moreover have "finite \" + proof - + from finite_RAG have "finite (RAG s)" . + hence "finite ((RAG (wq s))\<^sup>+)" + apply (unfold finite_trancl) + by (auto simp: s_RAG_def cs_RAG_def wq_def) + thus ?thesis by auto + qed + ultimately show ?thesis by (auto intro:finite_subset) + qed + thus ?thesis by (simp add:cs_dependants_def) + qed + thus ?thesis by simp + qed + thus ?thesis by auto + qed + moreover have h2: "(?f ` ?B) \ {}" + proof - + have "?B \ {}" by simp + thus ?thesis by simp + qed + from Max_in [OF h1 h2] + have "Max (?f ` ?B) \ (?f ` ?B)" . + thus ?thesis by (auto intro:that) + qed + from eq_f_th1 eq_f_th2 eq_max + have eq_preced: "preced th1' s = preced th2' s" by auto + hence eq_th12: "th1' = th2'" + proof (rule preced_unique) + from th1_in have "th1' = th1 \ (th1' \ dependants (wq s) th1)" by simp + thus "th1' \ threads s" + proof + assume "th1' \ dependants (wq s) th1" + hence "(Th th1') \ Domain ((RAG s)^+)" + apply (unfold cs_dependants_def cs_RAG_def s_RAG_def) + by (auto simp:Domain_def) + hence "(Th th1') \ Domain (RAG s)" by (simp add:trancl_domain) + from dm_RAG_threads[OF this] show ?thesis . + next + assume "th1' = th1" + with runing_1 show ?thesis + by (unfold runing_def readys_def, auto) + qed + next + from th2_in have "th2' = th2 \ (th2' \ dependants (wq s) th2)" by simp + thus "th2' \ threads s" + proof + assume "th2' \ dependants (wq s) th2" + hence "(Th th2') \ Domain ((RAG s)^+)" + apply (unfold cs_dependants_def cs_RAG_def s_RAG_def) + by (auto simp:Domain_def) + hence "(Th th2') \ Domain (RAG s)" by (simp add:trancl_domain) + from dm_RAG_threads[OF this] show ?thesis . + next + assume "th2' = th2" + with runing_2 show ?thesis + by (unfold runing_def readys_def, auto) + qed + qed + from th1_in have "th1' = th1 \ th1' \ dependants (wq s) th1" by simp + thus ?thesis + proof + assume eq_th': "th1' = th1" + from th2_in have "th2' = th2 \ th2' \ dependants (wq s) th2" by simp + thus ?thesis + proof + assume "th2' = th2" thus ?thesis using eq_th' eq_th12 by simp + next + assume "th2' \ dependants (wq s) th2" + with eq_th12 eq_th' have "th1 \ dependants (wq s) th2" by simp + hence "(Th th1, Th th2) \ (RAG s)^+" + by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp) + hence "Th th1 \ Domain ((RAG s)^+)" + apply (unfold cs_dependants_def cs_RAG_def s_RAG_def) + by (auto simp:Domain_def) + hence "Th th1 \ Domain (RAG s)" by (simp add:trancl_domain) + then obtain n where d: "(Th th1, n) \ RAG s" by (auto simp:Domain_def) + from RAG_target_th [OF this] + obtain cs' where "n = Cs cs'" by auto + with d have "(Th th1, Cs cs') \ RAG s" by simp + with runing_1 have "False" + apply (unfold runing_def readys_def s_RAG_def) + by (auto simp:eq_waiting) + thus ?thesis by simp + qed + next + assume th1'_in: "th1' \ dependants (wq s) th1" + from th2_in have "th2' = th2 \ th2' \ dependants (wq s) th2" by simp + thus ?thesis + proof + assume "th2' = th2" + with th1'_in eq_th12 have "th2 \ dependants (wq s) th1" by simp + hence "(Th th2, Th th1) \ (RAG s)^+" + by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp) + hence "Th th2 \ Domain ((RAG s)^+)" + apply (unfold cs_dependants_def cs_RAG_def s_RAG_def) + by (auto simp:Domain_def) + hence "Th th2 \ Domain (RAG s)" by (simp add:trancl_domain) + then obtain n where d: "(Th th2, n) \ RAG s" by (auto simp:Domain_def) + from RAG_target_th [OF this] + obtain cs' where "n = Cs cs'" by auto + with d have "(Th th2, Cs cs') \ RAG s" by simp + with runing_2 have "False" + apply (unfold runing_def readys_def s_RAG_def) + by (auto simp:eq_waiting) + thus ?thesis by simp + next + assume "th2' \ dependants (wq s) th2" + with eq_th12 have "th1' \ dependants (wq s) th2" by simp + hence h1: "(Th th1', Th th2) \ (RAG s)^+" + by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp) + from th1'_in have h2: "(Th th1', Th th1) \ (RAG s)^+" + by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp) + show ?thesis + proof(rule dchain_unique[OF h1 _ h2, symmetric]) + from runing_1 show "th1 \ readys s" by (simp add:runing_def) + from runing_2 show "th2 \ readys s" by (simp add:runing_def) + qed + qed + qed +qed + + +lemma "card (runing s) \ 1" +apply(subgoal_tac "finite (runing s)") +prefer 2 +apply (metis finite_nat_set_iff_bounded lessI runing_unique) +apply(rule ccontr) +apply(simp) +apply(case_tac "Suc (Suc 0) \ card (runing s)") +apply(subst (asm) card_le_Suc_iff) +apply(simp) +apply(auto)[1] +apply (metis insertCI runing_unique) +apply(auto) +done + +end + + +lemma create_pre: + assumes stp: "step s e" + and not_in: "th \ threads s" + and is_in: "th \ threads (e#s)" + obtains prio where "e = Create th prio" +proof - + from assms + show ?thesis + proof(cases) + case (thread_create thread prio) + with is_in not_in have "e = Create th prio" by simp + from that[OF this] show ?thesis . + next + case (thread_exit thread) + with assms show ?thesis by (auto intro!:that) + next + case (thread_P thread) + with assms show ?thesis by (auto intro!:that) + next + case (thread_V thread) + with assms show ?thesis by (auto intro!:that) + next + case (thread_set thread) + with assms show ?thesis by (auto intro!:that) + qed +qed + + +context valid_trace +begin + +lemma cnp_cnv_eq: + assumes "th \ threads s" + shows "cntP s th = cntV s th" + using assms + using cnp_cnv_cncs not_thread_cncs by auto + +end + + +lemma eq_RAG: + "RAG (wq s) = RAG s" +by (unfold cs_RAG_def s_RAG_def, auto) + +context valid_trace +begin + +lemma count_eq_dependants: + assumes eq_pv: "cntP s th = cntV s th" + shows "dependants (wq s) th = {}" +proof - + from cnp_cnv_cncs and eq_pv + have "cntCS s th = 0" + by (auto split:if_splits) + moreover have "finite {cs. (Cs cs, Th th) \ RAG s}" + proof - + from finite_holding[of th] show ?thesis + by (simp add:holdents_test) + qed + ultimately have h: "{cs. (Cs cs, Th th) \ RAG s} = {}" + by (unfold cntCS_def holdents_test cs_dependants_def, auto) + show ?thesis + proof(unfold cs_dependants_def) + { assume "{th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ {}" + then obtain th' where "(Th th', Th th) \ (RAG (wq s))\<^sup>+" by auto + hence "False" + proof(cases) + assume "(Th th', Th th) \ RAG (wq s)" + thus "False" by (auto simp:cs_RAG_def) + next + fix c + assume "(c, Th th) \ RAG (wq s)" + with h and eq_RAG show "False" + by (cases c, auto simp:cs_RAG_def) + qed + } thus "{th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} = {}" by auto + qed +qed + +lemma dependants_threads: + shows "dependants (wq s) th \ threads s" +proof + { fix th th' + assume h: "th \ {th'a. (Th th'a, Th th') \ (RAG (wq s))\<^sup>+}" + have "Th th \ Domain (RAG s)" + proof - + from h obtain th' where "(Th th, Th th') \ (RAG (wq s))\<^sup>+" by auto + hence "(Th th) \ Domain ( (RAG (wq s))\<^sup>+)" by (auto simp:Domain_def) + with trancl_domain have "(Th th) \ Domain (RAG (wq s))" by simp + thus ?thesis using eq_RAG by simp + qed + from dm_RAG_threads[OF this] + have "th \ threads s" . + } note hh = this + fix th1 + assume "th1 \ dependants (wq s) th" + hence "th1 \ {th'a. (Th th'a, Th th) \ (RAG (wq s))\<^sup>+}" + by (unfold cs_dependants_def, simp) + from hh [OF this] show "th1 \ threads s" . +qed + +lemma finite_threads: + shows "finite (threads s)" +using vt by (induct) (auto elim: step.cases) + +end + +lemma Max_f_mono: + assumes seq: "A \ B" + and np: "A \ {}" + and fnt: "finite B" + shows "Max (f ` A) \ Max (f ` B)" +proof(rule Max_mono) + from seq show "f ` A \ f ` B" by auto +next + from np show "f ` A \ {}" by auto +next + from fnt and seq show "finite (f ` B)" by auto +qed + +context valid_trace +begin + +lemma cp_le: + assumes th_in: "th \ threads s" + shows "cp s th \ Max ((\ th. (preced th s)) ` threads s)" +proof(unfold cp_eq_cpreced cpreced_def cs_dependants_def) + show "Max ((\th. preced th s) ` ({th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+})) + \ Max ((\th. preced th s) ` threads s)" + (is "Max (?f ` ?A) \ Max (?f ` ?B)") + proof(rule Max_f_mono) + show "{th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ {}" by simp + next + from finite_threads + show "finite (threads s)" . + next + from th_in + show "{th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ threads s" + apply (auto simp:Domain_def) + apply (rule_tac dm_RAG_threads) + apply (unfold trancl_domain [of "RAG s", symmetric]) + by (unfold cs_RAG_def s_RAG_def, auto simp:Domain_def) + qed +qed + +lemma le_cp: + shows "preced th s \ cp s th" +proof(unfold cp_eq_cpreced preced_def cpreced_def, simp) + show "Prc (priority th s) (last_set th s) + \ Max (insert (Prc (priority th s) (last_set th s)) + ((\th. Prc (priority th s) (last_set th s)) ` dependants (wq s) th))" + (is "?l \ Max (insert ?l ?A)") + proof(cases "?A = {}") + case False + have "finite ?A" (is "finite (?f ` ?B)") + proof - + have "finite ?B" + proof- + have "finite {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+}" + proof - + let ?F = "\ (x, y). the_th x" + have "{th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ ?F ` ((RAG (wq s))\<^sup>+)" + apply (auto simp:image_def) + by (rule_tac x = "(Th x, Th th)" in bexI, auto) + moreover have "finite \" + proof - + from finite_RAG have "finite (RAG s)" . + hence "finite ((RAG (wq s))\<^sup>+)" + apply (unfold finite_trancl) + by (auto simp: s_RAG_def cs_RAG_def wq_def) + thus ?thesis by auto + qed + ultimately show ?thesis by (auto intro:finite_subset) + qed + thus ?thesis by (simp add:cs_dependants_def) + qed + thus ?thesis by simp + qed + from Max_insert [OF this False, of ?l] show ?thesis by auto + next + case True + thus ?thesis by auto + qed +qed + +lemma max_cp_eq: + shows "Max ((cp s) ` threads s) = Max ((\ th. (preced th s)) ` threads s)" + (is "?l = ?r") +proof(cases "threads s = {}") + case True + thus ?thesis by auto +next + case False + have "?l \ ((cp s) ` threads s)" + proof(rule Max_in) + from finite_threads + show "finite (cp s ` threads s)" by auto + next + from False show "cp s ` threads s \ {}" by auto + qed + then obtain th + where th_in: "th \ threads s" and eq_l: "?l = cp s th" by auto + have "\ \ ?r" by (rule cp_le[OF th_in]) + moreover have "?r \ cp s th" (is "Max (?f ` ?A) \ cp s th") + proof - + have "?r \ (?f ` ?A)" + proof(rule Max_in) + from finite_threads + show " finite ((\th. preced th s) ` threads s)" by auto + next + from False show " (\th. preced th s) ` threads s \ {}" by auto + qed + then obtain th' where + th_in': "th' \ ?A " and eq_r: "?r = ?f th'" by auto + from le_cp [of th'] eq_r + have "?r \ cp s th'" by auto + moreover have "\ \ cp s th" + proof(fold eq_l) + show " cp s th' \ Max (cp s ` threads s)" + proof(rule Max_ge) + from th_in' show "cp s th' \ cp s ` threads s" + by auto + next + from finite_threads + show "finite (cp s ` threads s)" by auto + qed + qed + ultimately show ?thesis by auto + qed + ultimately show ?thesis using eq_l by auto +qed + +lemma max_cp_readys_threads_pre: + assumes np: "threads s \ {}" + shows "Max (cp s ` readys s) = Max (cp s ` threads s)" +proof(unfold max_cp_eq) + show "Max (cp s ` readys s) = Max ((\th. preced th s) ` threads s)" + proof - + let ?p = "Max ((\th. preced th s) ` threads s)" + let ?f = "(\th. preced th s)" + have "?p \ ((\th. preced th s) ` threads s)" + proof(rule Max_in) + from finite_threads show "finite (?f ` threads s)" by simp + next + from np show "?f ` threads s \ {}" by simp + qed + then obtain tm where tm_max: "?f tm = ?p" and tm_in: "tm \ threads s" + by (auto simp:Image_def) + from th_chain_to_ready [OF tm_in] + have "tm \ readys s \ (\th'. th' \ readys s \ (Th tm, Th th') \ (RAG s)\<^sup>+)" . + thus ?thesis + proof + assume "\th'. th' \ readys s \ (Th tm, Th th') \ (RAG s)\<^sup>+ " + then obtain th' where th'_in: "th' \ readys s" + and tm_chain:"(Th tm, Th th') \ (RAG s)\<^sup>+" by auto + have "cp s th' = ?f tm" + proof(subst cp_eq_cpreced, subst cpreced_def, rule Max_eqI) + from dependants_threads finite_threads + show "finite ((\th. preced th s) ` ({th'} \ dependants (wq s) th'))" + by (auto intro:finite_subset) + next + fix p assume p_in: "p \ (\th. preced th s) ` ({th'} \ dependants (wq s) th')" + from tm_max have " preced tm s = Max ((\th. preced th s) ` threads s)" . + moreover have "p \ \" + proof(rule Max_ge) + from finite_threads + show "finite ((\th. preced th s) ` threads s)" by simp + next + from p_in and th'_in and dependants_threads[of th'] + show "p \ (\th. preced th s) ` threads s" + by (auto simp:readys_def) + qed + ultimately show "p \ preced tm s" by auto + next + show "preced tm s \ (\th. preced th s) ` ({th'} \ dependants (wq s) th')" + proof - + from tm_chain + have "tm \ dependants (wq s) th'" + by (unfold cs_dependants_def s_RAG_def cs_RAG_def, auto) + thus ?thesis by auto + qed + qed + with tm_max + have h: "cp s th' = Max ((\th. preced th s) ` threads s)" by simp + show ?thesis + proof (fold h, rule Max_eqI) + fix q + assume "q \ cp s ` readys s" + then obtain th1 where th1_in: "th1 \ readys s" + and eq_q: "q = cp s th1" by auto + show "q \ cp s th'" + apply (unfold h eq_q) + apply (unfold cp_eq_cpreced cpreced_def) + apply (rule Max_mono) + proof - + from dependants_threads [of th1] th1_in + show "(\th. preced th s) ` ({th1} \ dependants (wq s) th1) \ + (\th. preced th s) ` threads s" + by (auto simp:readys_def) + next + show "(\th. preced th s) ` ({th1} \ dependants (wq s) th1) \ {}" by simp + next + from finite_threads + show " finite ((\th. preced th s) ` threads s)" by simp + qed + next + from finite_threads + show "finite (cp s ` readys s)" by (auto simp:readys_def) + next + from th'_in + show "cp s th' \ cp s ` readys s" by simp + qed + next + assume tm_ready: "tm \ readys s" + show ?thesis + proof(fold tm_max) + have cp_eq_p: "cp s tm = preced tm s" + proof(unfold cp_eq_cpreced cpreced_def, rule Max_eqI) + fix y + assume hy: "y \ (\th. preced th s) ` ({tm} \ dependants (wq s) tm)" + show "y \ preced tm s" + proof - + { fix y' + assume hy' : "y' \ ((\th. preced th s) ` dependants (wq s) tm)" + have "y' \ preced tm s" + proof(unfold tm_max, rule Max_ge) + from hy' dependants_threads[of tm] + show "y' \ (\th. preced th s) ` threads s" by auto + next + from finite_threads + show "finite ((\th. preced th s) ` threads s)" by simp + qed + } with hy show ?thesis by auto + qed + next + from dependants_threads[of tm] finite_threads + show "finite ((\th. preced th s) ` ({tm} \ dependants (wq s) tm))" + by (auto intro:finite_subset) + next + show "preced tm s \ (\th. preced th s) ` ({tm} \ dependants (wq s) tm)" + by simp + qed + moreover have "Max (cp s ` readys s) = cp s tm" + proof(rule Max_eqI) + from tm_ready show "cp s tm \ cp s ` readys s" by simp + next + from finite_threads + show "finite (cp s ` readys s)" by (auto simp:readys_def) + next + fix y assume "y \ cp s ` readys s" + then obtain th1 where th1_readys: "th1 \ readys s" + and h: "y = cp s th1" by auto + show "y \ cp s tm" + apply(unfold cp_eq_p h) + apply(unfold cp_eq_cpreced cpreced_def tm_max, rule Max_mono) + proof - + from finite_threads + show "finite ((\th. preced th s) ` threads s)" by simp + next + show "(\th. preced th s) ` ({th1} \ dependants (wq s) th1) \ {}" + by simp + next + from dependants_threads[of th1] th1_readys + show "(\th. preced th s) ` ({th1} \ dependants (wq s) th1) + \ (\th. preced th s) ` threads s" + by (auto simp:readys_def) + qed + qed + ultimately show " Max (cp s ` readys s) = preced tm s" by simp + qed + qed + qed +qed + +text {* (* ccc *) \noindent + Since the current precedence of the threads in ready queue will always be boosted, + there must be one inside it has the maximum precedence of the whole system. +*} +lemma max_cp_readys_threads: + shows "Max (cp s ` readys s) = Max (cp s ` threads s)" +proof(cases "threads s = {}") + case True + thus ?thesis + by (auto simp:readys_def) +next + case False + show ?thesis by (rule max_cp_readys_threads_pre[OF False]) +qed + +end + +lemma eq_holding: "holding (wq s) th cs = holding s th cs" + apply (unfold s_holding_def cs_holding_def wq_def, simp) + done + +lemma f_image_eq: + assumes h: "\ a. a \ A \ f a = g a" + shows "f ` A = g ` A" +proof + show "f ` A \ g ` A" + by(rule image_subsetI, auto intro:h) +next + show "g ` A \ f ` A" + by (rule image_subsetI, auto intro:h[symmetric]) +qed + + +definition detached :: "state \ thread \ bool" + where "detached s th \ (\(\ cs. holding s th cs)) \ (\(\cs. waiting s th cs))" + + +lemma detached_test: + shows "detached s th = (Th th \ Field (RAG s))" +apply(simp add: detached_def Field_def) +apply(simp add: s_RAG_def) +apply(simp add: s_holding_abv s_waiting_abv) +apply(simp add: Domain_iff Range_iff) +apply(simp add: wq_def) +apply(auto) +done + +context valid_trace +begin + +lemma detached_intro: + assumes eq_pv: "cntP s th = cntV s th" + shows "detached s th" +proof - + from cnp_cnv_cncs + have eq_cnt: "cntP s th = + cntV s th + (if th \ readys s \ th \ threads s then cntCS s th else cntCS s th + 1)" . + hence cncs_zero: "cntCS s th = 0" + by (auto simp:eq_pv split:if_splits) + with eq_cnt + have "th \ readys s \ th \ threads s" by (auto simp:eq_pv) + thus ?thesis + proof + assume "th \ threads s" + with range_in dm_RAG_threads + show ?thesis + by (auto simp add: detached_def s_RAG_def s_waiting_abv s_holding_abv wq_def Domain_iff Range_iff) + next + assume "th \ readys s" + moreover have "Th th \ Range (RAG s)" + proof - + from card_0_eq [OF finite_holding] and cncs_zero + have "holdents s th = {}" + by (simp add:cntCS_def) + thus ?thesis + apply(auto simp:holdents_test) + apply(case_tac a) + apply(auto simp:holdents_test s_RAG_def) + done + qed + ultimately show ?thesis + by (auto simp add: detached_def s_RAG_def s_waiting_abv s_holding_abv wq_def readys_def) + qed +qed + +lemma detached_elim: + assumes dtc: "detached s th" + shows "cntP s th = cntV s th" +proof - + from cnp_cnv_cncs + have eq_pv: " cntP s th = + cntV s th + (if th \ readys s \ th \ threads s then cntCS s th else cntCS s th + 1)" . + have cncs_z: "cntCS s th = 0" + proof - + from dtc have "holdents s th = {}" + unfolding detached_def holdents_test s_RAG_def + by (simp add: s_waiting_abv wq_def s_holding_abv Domain_iff Range_iff) + thus ?thesis by (auto simp:cntCS_def) + qed + show ?thesis + proof(cases "th \ threads s") + case True + with dtc + have "th \ readys s" + by (unfold readys_def detached_def Field_def Domain_def Range_def, + auto simp:eq_waiting s_RAG_def) + with cncs_z and eq_pv show ?thesis by simp + next + case False + with cncs_z and eq_pv show ?thesis by simp + qed +qed + +lemma detached_eq: + shows "(detached s th) = (cntP s th = cntV s th)" + by (insert vt, auto intro:detached_intro detached_elim) + +end + +text {* + The lemmas in this .thy file are all obvious lemmas, however, they still needs to be derived + from the concise and miniature model of PIP given in PrioGDef.thy. +*} + +lemma eq_dependants: "dependants (wq s) = dependants s" + by (simp add: s_dependants_abv wq_def) + +lemma next_th_unique: + assumes nt1: "next_th s th cs th1" + and nt2: "next_th s th cs th2" + shows "th1 = th2" +using assms by (unfold next_th_def, auto) + +lemma birth_time_lt: "s \ [] \ last_set th s < length s" + apply (induct s, simp) +proof - + fix a s + assume ih: "s \ [] \ last_set th s < length s" + and eq_as: "a # s \ []" + show "last_set th (a # s) < length (a # s)" + proof(cases "s \ []") + case False + from False show ?thesis + by (cases a, auto simp:last_set.simps) + next + case True + from ih [OF True] show ?thesis + by (cases a, auto simp:last_set.simps) + qed +qed + +lemma th_in_ne: "th \ threads s \ s \ []" + by (induct s, auto simp:threads.simps) + +lemma preced_tm_lt: "th \ threads s \ preced th s = Prc x y \ y < length s" + apply (drule_tac th_in_ne) + by (unfold preced_def, auto intro: birth_time_lt) + +lemma inj_the_preced: + "inj_on (the_preced s) (threads s)" + by (metis inj_onI preced_unique the_preced_def) + +lemma tRAG_alt_def: + "tRAG s = {(Th th1, Th th2) | th1 th2. + \ cs. (Th th1, Cs cs) \ RAG s \ (Cs cs, Th th2) \ RAG s}" + by (auto simp:tRAG_def RAG_split wRAG_def hRAG_def) + +lemma tRAG_Field: + "Field (tRAG s) \ Field (RAG s)" + by (unfold tRAG_alt_def Field_def, auto) + +lemma tRAG_ancestorsE: + assumes "x \ ancestors (tRAG s) u" + obtains th where "x = Th th" +proof - + from assms have "(u, x) \ (tRAG s)^+" + by (unfold ancestors_def, auto) + from tranclE[OF this] obtain c where "(c, x) \ tRAG s" by auto + then obtain th where "x = Th th" + by (unfold tRAG_alt_def, auto) + from that[OF this] show ?thesis . +qed + +lemma tRAG_mono: + assumes "RAG s' \ RAG s" + shows "tRAG s' \ tRAG s" + using assms + by (unfold tRAG_alt_def, auto) + +lemma holding_next_thI: + assumes "holding s th cs" + and "length (wq s cs) > 1" + obtains th' where "next_th s th cs th'" +proof - + from assms(1)[folded eq_holding, unfolded cs_holding_def] + have " th \ set (wq s cs) \ th = hd (wq s cs)" . + then obtain rest where h1: "wq s cs = th#rest" + by (cases "wq s cs", auto) + with assms(2) have h2: "rest \ []" by auto + let ?th' = "hd (SOME q. distinct q \ set q = set rest)" + have "next_th s th cs ?th'" using h1(1) h2 + by (unfold next_th_def, auto) + from that[OF this] show ?thesis . +qed + +lemma RAG_tRAG_transfer: + assumes "vt s'" + assumes "RAG s = RAG s' \ {(Th th, Cs cs)}" + and "(Cs cs, Th th'') \ RAG s'" + shows "tRAG s = tRAG s' \ {(Th th, Th th'')}" (is "?L = ?R") +proof - + interpret vt_s': valid_trace "s'" using assms(1) + by (unfold_locales, simp) + interpret rtree: rtree "RAG s'" + proof + show "single_valued (RAG s')" + apply (intro_locales) + by (unfold single_valued_def, + auto intro:vt_s'.unique_RAG) + + show "acyclic (RAG s')" + by (rule vt_s'.acyclic_RAG) + qed + { fix n1 n2 + assume "(n1, n2) \ ?L" + from this[unfolded tRAG_alt_def] + obtain th1 th2 cs' where + h: "n1 = Th th1" "n2 = Th th2" + "(Th th1, Cs cs') \ RAG s" + "(Cs cs', Th th2) \ RAG s" by auto + from h(4) and assms(2) have cs_in: "(Cs cs', Th th2) \ RAG s'" by auto + from h(3) and assms(2) + have "(Th th1, Cs cs') = (Th th, Cs cs) \ + (Th th1, Cs cs') \ RAG s'" by auto + hence "(n1, n2) \ ?R" + proof + assume h1: "(Th th1, Cs cs') = (Th th, Cs cs)" + hence eq_th1: "th1 = th" by simp + moreover have "th2 = th''" + proof - + from h1 have "cs' = cs" by simp + from assms(3) cs_in[unfolded this] rtree.sgv + show ?thesis + by (unfold single_valued_def, auto) + qed + ultimately show ?thesis using h(1,2) by auto + next + assume "(Th th1, Cs cs') \ RAG s'" + with cs_in have "(Th th1, Th th2) \ tRAG s'" + by (unfold tRAG_alt_def, auto) + from this[folded h(1, 2)] show ?thesis by auto + qed + } moreover { + fix n1 n2 + assume "(n1, n2) \ ?R" + hence "(n1, n2) \tRAG s' \ (n1, n2) = (Th th, Th th'')" by auto + hence "(n1, n2) \ ?L" + proof + assume "(n1, n2) \ tRAG s'" + moreover have "... \ ?L" + proof(rule tRAG_mono) + show "RAG s' \ RAG s" by (unfold assms(2), auto) + qed + ultimately show ?thesis by auto + next + assume eq_n: "(n1, n2) = (Th th, Th th'')" + from assms(2, 3) have "(Cs cs, Th th'') \ RAG s" by auto + moreover have "(Th th, Cs cs) \ RAG s" using assms(2) by auto + ultimately show ?thesis + by (unfold eq_n tRAG_alt_def, auto) + qed + } ultimately show ?thesis by auto +qed + +context valid_trace +begin + +lemmas RAG_tRAG_transfer = RAG_tRAG_transfer[OF vt] + +end + +lemma cp_alt_def: + "cp s th = + Max ((the_preced s) ` {th'. Th th' \ (subtree (RAG s) (Th th))})" +proof - + have "Max (the_preced s ` ({th} \ dependants (wq s) th)) = + Max (the_preced s ` {th'. Th th' \ subtree (RAG s) (Th th)})" + (is "Max (_ ` ?L) = Max (_ ` ?R)") + proof - + have "?L = ?R" + by (auto dest:rtranclD simp:cs_dependants_def cs_RAG_def s_RAG_def subtree_def) + thus ?thesis by simp + qed + thus ?thesis by (unfold cp_eq_cpreced cpreced_def, fold the_preced_def, simp) +qed + +lemma cp_gen_alt_def: + "cp_gen s = (Max \ (\x. (the_preced s \ the_thread) ` subtree (tRAG s) x))" + by (auto simp:cp_gen_def) + +lemma tRAG_nodeE: + assumes "(n1, n2) \ tRAG s" + obtains th1 th2 where "n1 = Th th1" "n2 = Th th2" + using assms + by (auto simp: tRAG_def wRAG_def hRAG_def tRAG_def) + +lemma subtree_nodeE: + assumes "n \ subtree (tRAG s) (Th th)" + obtains th1 where "n = Th th1" +proof - + show ?thesis + proof(rule subtreeE[OF assms]) + assume "n = Th th" + from that[OF this] show ?thesis . + next + assume "Th th \ ancestors (tRAG s) n" + hence "(n, Th th) \ (tRAG s)^+" by (auto simp:ancestors_def) + hence "\ th1. n = Th th1" + proof(induct) + case (base y) + from tRAG_nodeE[OF this] show ?case by metis + next + case (step y z) + thus ?case by auto + qed + with that show ?thesis by auto + qed +qed + +lemma tRAG_star_RAG: "(tRAG s)^* \ (RAG s)^*" +proof - + have "(wRAG s O hRAG s)^* \ (RAG s O RAG s)^*" + by (rule rtrancl_mono, auto simp:RAG_split) + also have "... \ ((RAG s)^*)^*" + by (rule rtrancl_mono, auto) + also have "... = (RAG s)^*" by simp + finally show ?thesis by (unfold tRAG_def, simp) +qed + +lemma tRAG_subtree_RAG: "subtree (tRAG s) x \ subtree (RAG s) x" +proof - + { fix a + assume "a \ subtree (tRAG s) x" + hence "(a, x) \ (tRAG s)^*" by (auto simp:subtree_def) + with tRAG_star_RAG[of s] + have "(a, x) \ (RAG s)^*" by auto + hence "a \ subtree (RAG s) x" by (auto simp:subtree_def) + } thus ?thesis by auto +qed + +lemma tRAG_trancl_eq: + "{th'. (Th th', Th th) \ (tRAG s)^+} = + {th'. (Th th', Th th) \ (RAG s)^+}" + (is "?L = ?R") +proof - + { fix th' + assume "th' \ ?L" + hence "(Th th', Th th) \ (tRAG s)^+" by auto + from tranclD[OF this] + obtain z where h: "(Th th', z) \ tRAG s" "(z, Th th) \ (tRAG s)\<^sup>*" by auto + from tRAG_subtree_RAG[of s] and this(2) + have "(z, Th th) \ (RAG s)^*" by (meson subsetCE tRAG_star_RAG) + moreover from h(1) have "(Th th', z) \ (RAG s)^+" using tRAG_alt_def by auto + ultimately have "th' \ ?R" by auto + } moreover + { fix th' + assume "th' \ ?R" + hence "(Th th', Th th) \ (RAG s)^+" by (auto) + from plus_rpath[OF this] + obtain xs where rp: "rpath (RAG s) (Th th') xs (Th th)" "xs \ []" by auto + hence "(Th th', Th th) \ (tRAG s)^+" + proof(induct xs arbitrary:th' th rule:length_induct) + case (1 xs th' th) + then obtain x1 xs1 where Cons1: "xs = x1#xs1" by (cases xs, auto) + show ?case + proof(cases "xs1") + case Nil + from 1(2)[unfolded Cons1 Nil] + have rp: "rpath (RAG s) (Th th') [x1] (Th th)" . + hence "(Th th', x1) \ (RAG s)" by (cases, simp) + then obtain cs where "x1 = Cs cs" + by (unfold s_RAG_def, auto) + from rpath_nnl_lastE[OF rp[unfolded this]] + show ?thesis by auto + next + case (Cons x2 xs2) + from 1(2)[unfolded Cons1[unfolded this]] + have rp: "rpath (RAG s) (Th th') (x1 # x2 # xs2) (Th th)" . + from rpath_edges_on[OF this] + have eds: "edges_on (Th th' # x1 # x2 # xs2) \ RAG s" . + have "(Th th', x1) \ edges_on (Th th' # x1 # x2 # xs2)" + by (simp add: edges_on_unfold) + with eds have rg1: "(Th th', x1) \ RAG s" by auto + then obtain cs1 where eq_x1: "x1 = Cs cs1" by (unfold s_RAG_def, auto) + have "(x1, x2) \ edges_on (Th th' # x1 # x2 # xs2)" + by (simp add: edges_on_unfold) + from this eds + have rg2: "(x1, x2) \ RAG s" by auto + from this[unfolded eq_x1] + obtain th1 where eq_x2: "x2 = Th th1" by (unfold s_RAG_def, auto) + from rg1[unfolded eq_x1] rg2[unfolded eq_x1 eq_x2] + have rt1: "(Th th', Th th1) \ tRAG s" by (unfold tRAG_alt_def, auto) + from rp have "rpath (RAG s) x2 xs2 (Th th)" + by (elim rpath_ConsE, simp) + from this[unfolded eq_x2] have rp': "rpath (RAG s) (Th th1) xs2 (Th th)" . + show ?thesis + proof(cases "xs2 = []") + case True + from rpath_nilE[OF rp'[unfolded this]] + have "th1 = th" by auto + from rt1[unfolded this] show ?thesis by auto + next + case False + from 1(1)[rule_format, OF _ rp' this, unfolded Cons1 Cons] + have "(Th th1, Th th) \ (tRAG s)\<^sup>+" by simp + with rt1 show ?thesis by auto + qed + qed + qed + hence "th' \ ?L" by auto + } ultimately show ?thesis by blast +qed + +lemma tRAG_trancl_eq_Th: + "{Th th' | th'. (Th th', Th th) \ (tRAG s)^+} = + {Th th' | th'. (Th th', Th th) \ (RAG s)^+}" + using tRAG_trancl_eq by auto + +lemma dependants_alt_def: + "dependants s th = {th'. (Th th', Th th) \ (tRAG s)^+}" + by (metis eq_RAG s_dependants_def tRAG_trancl_eq) + +context valid_trace +begin + +lemma count_eq_tRAG_plus: + assumes "cntP s th = cntV s th" + shows "{th'. (Th th', Th th) \ (tRAG s)^+} = {}" + using assms count_eq_dependants dependants_alt_def eq_dependants by auto + +lemma count_eq_RAG_plus: + assumes "cntP s th = cntV s th" + shows "{th'. (Th th', Th th) \ (RAG s)^+} = {}" + using assms count_eq_dependants cs_dependants_def eq_RAG by auto + +lemma count_eq_RAG_plus_Th: + assumes "cntP s th = cntV s th" + shows "{Th th' | th'. (Th th', Th th) \ (RAG s)^+} = {}" + using count_eq_RAG_plus[OF assms] by auto + +lemma count_eq_tRAG_plus_Th: + assumes "cntP s th = cntV s th" + shows "{Th th' | th'. (Th th', Th th) \ (tRAG s)^+} = {}" + using count_eq_tRAG_plus[OF assms] by auto + +end + +lemma tRAG_subtree_eq: + "(subtree (tRAG s) (Th th)) = {Th th' | th'. Th th' \ (subtree (RAG s) (Th th))}" + (is "?L = ?R") +proof - + { fix n + assume h: "n \ ?L" + hence "n \ ?R" + by (smt mem_Collect_eq subsetCE subtree_def subtree_nodeE tRAG_subtree_RAG) + } moreover { + fix n + assume "n \ ?R" + then obtain th' where h: "n = Th th'" "(Th th', Th th) \ (RAG s)^*" + by (auto simp:subtree_def) + from rtranclD[OF this(2)] + have "n \ ?L" + proof + assume "Th th' \ Th th \ (Th th', Th th) \ (RAG s)\<^sup>+" + with h have "n \ {Th th' | th'. (Th th', Th th) \ (RAG s)^+}" by auto + thus ?thesis using subtree_def tRAG_trancl_eq by fastforce + qed (insert h, auto simp:subtree_def) + } ultimately show ?thesis by auto +qed + +lemma threads_set_eq: + "the_thread ` (subtree (tRAG s) (Th th)) = + {th'. Th th' \ (subtree (RAG s) (Th th))}" (is "?L = ?R") + by (auto intro:rev_image_eqI simp:tRAG_subtree_eq) + +lemma cp_alt_def1: + "cp s th = Max ((the_preced s o the_thread) ` (subtree (tRAG s) (Th th)))" +proof - + have "(the_preced s ` the_thread ` subtree (tRAG s) (Th th)) = + ((the_preced s \ the_thread) ` subtree (tRAG s) (Th th))" + by auto + thus ?thesis by (unfold cp_alt_def, fold threads_set_eq, auto) +qed + +lemma cp_gen_def_cond: + assumes "x = Th th" + shows "cp s th = cp_gen s (Th th)" +by (unfold cp_alt_def1 cp_gen_def, simp) + +lemma cp_gen_over_set: + assumes "\ x \ A. \ th. x = Th th" + shows "cp_gen s ` A = (cp s \ the_thread) ` A" +proof(rule f_image_eq) + fix a + assume "a \ A" + from assms[rule_format, OF this] + obtain th where eq_a: "a = Th th" by auto + show "cp_gen s a = (cp s \ the_thread) a" + by (unfold eq_a, simp, unfold cp_gen_def_cond[OF refl[of "Th th"]], simp) +qed + + +context valid_trace +begin + +lemma RAG_threads: + assumes "(Th th) \ Field (RAG s)" + shows "th \ threads s" + using assms + by (metis Field_def UnE dm_RAG_threads range_in vt) + +lemma subtree_tRAG_thread: + assumes "th \ threads s" + shows "subtree (tRAG s) (Th th) \ Th ` threads s" (is "?L \ ?R") +proof - + have "?L = {Th th' |th'. Th th' \ subtree (RAG s) (Th th)}" + by (unfold tRAG_subtree_eq, simp) + also have "... \ ?R" + proof + fix x + assume "x \ {Th th' |th'. Th th' \ subtree (RAG s) (Th th)}" + then obtain th' where h: "x = Th th'" "Th th' \ subtree (RAG s) (Th th)" by auto + from this(2) + show "x \ ?R" + proof(cases rule:subtreeE) + case 1 + thus ?thesis by (simp add: assms h(1)) + next + case 2 + thus ?thesis by (metis ancestors_Field dm_RAG_threads h(1) image_eqI) + qed + qed + finally show ?thesis . +qed + +lemma readys_root: + assumes "th \ readys s" + shows "root (RAG s) (Th th)" +proof - + { fix x + assume "x \ ancestors (RAG s) (Th th)" + hence h: "(Th th, x) \ (RAG s)^+" by (auto simp:ancestors_def) + from tranclD[OF this] + obtain z where "(Th th, z) \ RAG s" by auto + with assms(1) have False + apply (case_tac z, auto simp:readys_def s_RAG_def s_waiting_def cs_waiting_def) + by (fold wq_def, blast) + } thus ?thesis by (unfold root_def, auto) +qed + +lemma readys_in_no_subtree: + assumes "th \ readys s" + and "th' \ th" + shows "Th th \ subtree (RAG s) (Th th')" +proof + assume "Th th \ subtree (RAG s) (Th th')" + thus False + proof(cases rule:subtreeE) + case 1 + with assms show ?thesis by auto + next + case 2 + with readys_root[OF assms(1)] + show ?thesis by (auto simp:root_def) + qed +qed + +lemma not_in_thread_isolated: + assumes "th \ threads s" + shows "(Th th) \ Field (RAG s)" +proof + assume "(Th th) \ Field (RAG s)" + with dm_RAG_threads and range_in assms + show False by (unfold Field_def, blast) +qed + +lemma wf_RAG: "wf (RAG s)" +proof(rule finite_acyclic_wf) + from finite_RAG show "finite (RAG s)" . +next + from acyclic_RAG show "acyclic (RAG s)" . +qed + +lemma sgv_wRAG: "single_valued (wRAG s)" + using waiting_unique + by (unfold single_valued_def wRAG_def, auto) + +lemma sgv_hRAG: "single_valued (hRAG s)" + using holding_unique + by (unfold single_valued_def hRAG_def, auto) + +lemma sgv_tRAG: "single_valued (tRAG s)" + by (unfold tRAG_def, rule single_valued_relcomp, + insert sgv_wRAG sgv_hRAG, auto) + +lemma acyclic_tRAG: "acyclic (tRAG s)" +proof(unfold tRAG_def, rule acyclic_compose) + show "acyclic (RAG s)" using acyclic_RAG . +next + show "wRAG s \ RAG s" unfolding RAG_split by auto +next + show "hRAG s \ RAG s" unfolding RAG_split by auto +qed + +lemma sgv_RAG: "single_valued (RAG s)" + using unique_RAG by (auto simp:single_valued_def) + +lemma rtree_RAG: "rtree (RAG s)" + using sgv_RAG acyclic_RAG + by (unfold rtree_def rtree_axioms_def sgv_def, auto) + +end + +sublocale valid_trace < rtree_RAG: rtree "RAG s" +proof + show "single_valued (RAG s)" + apply (intro_locales) + by (unfold single_valued_def, + auto intro:unique_RAG) + + show "acyclic (RAG s)" + by (rule acyclic_RAG) +qed + +sublocale valid_trace < rtree_s: rtree "tRAG s" +proof(unfold_locales) + from sgv_tRAG show "single_valued (tRAG s)" . +next + from acyclic_tRAG show "acyclic (tRAG s)" . +qed + +sublocale valid_trace < fsbtRAGs : fsubtree "RAG s" +proof - + show "fsubtree (RAG s)" + proof(intro_locales) + show "fbranch (RAG s)" using finite_fbranchI[OF finite_RAG] . + next + show "fsubtree_axioms (RAG s)" + proof(unfold fsubtree_axioms_def) + from wf_RAG show "wf (RAG s)" . + qed + qed +qed + +sublocale valid_trace < fsbttRAGs: fsubtree "tRAG s" +proof - + have "fsubtree (tRAG s)" + proof - + have "fbranch (tRAG s)" + proof(unfold tRAG_def, rule fbranch_compose) + show "fbranch (wRAG s)" + proof(rule finite_fbranchI) + from finite_RAG show "finite (wRAG s)" + by (unfold RAG_split, auto) + qed + next + show "fbranch (hRAG s)" + proof(rule finite_fbranchI) + from finite_RAG + show "finite (hRAG s)" by (unfold RAG_split, auto) + qed + qed + moreover have "wf (tRAG s)" + proof(rule wf_subset) + show "wf (RAG s O RAG s)" using wf_RAG + by (fold wf_comp_self, simp) + next + show "tRAG s \ (RAG s O RAG s)" + by (unfold tRAG_alt_def, auto) + qed + ultimately show ?thesis + by (unfold fsubtree_def fsubtree_axioms_def,auto) + qed + from this[folded tRAG_def] show "fsubtree (tRAG s)" . +qed + +lemma Max_UNION: + assumes "finite A" + and "A \ {}" + and "\ M \ f ` A. finite M" + and "\ M \ f ` A. M \ {}" + shows "Max (\x\ A. f x) = Max (Max ` f ` A)" (is "?L = ?R") + using assms[simp] +proof - + have "?L = Max (\(f ` A))" + by (fold Union_image_eq, simp) + also have "... = ?R" + by (subst Max_Union, simp+) + finally show ?thesis . +qed + +lemma max_Max_eq: + assumes "finite A" + and "A \ {}" + and "x = y" + shows "max x (Max A) = Max ({y} \ A)" (is "?L = ?R") +proof - + have "?R = Max (insert y A)" by simp + also from assms have "... = ?L" + by (subst Max.insert, simp+) + finally show ?thesis by simp +qed + +context valid_trace +begin + +(* ddd *) +lemma cp_gen_rec: + assumes "x = Th th" + shows "cp_gen s x = Max ({the_preced s th} \ (cp_gen s) ` children (tRAG s) x)" +proof(cases "children (tRAG s) x = {}") + case True + show ?thesis + by (unfold True cp_gen_def subtree_children, simp add:assms) +next + case False + hence [simp]: "children (tRAG s) x \ {}" by auto + note fsbttRAGs.finite_subtree[simp] + have [simp]: "finite (children (tRAG s) x)" + by (intro rev_finite_subset[OF fsbttRAGs.finite_subtree], + rule children_subtree) + { fix r x + have "subtree r x \ {}" by (auto simp:subtree_def) + } note this[simp] + have [simp]: "\x\children (tRAG s) x. subtree (tRAG s) x \ {}" + proof - + from False obtain q where "q \ children (tRAG s) x" by blast + moreover have "subtree (tRAG s) q \ {}" by simp + ultimately show ?thesis by blast + qed + have h: "Max ((the_preced s \ the_thread) ` + ({x} \ \(subtree (tRAG s) ` children (tRAG s) x))) = + Max ({the_preced s th} \ cp_gen s ` children (tRAG s) x)" + (is "?L = ?R") + proof - + let "Max (?f ` (?A \ \ (?g ` ?B)))" = ?L + let "Max (_ \ (?h ` ?B))" = ?R + let ?L1 = "?f ` \(?g ` ?B)" + have eq_Max_L1: "Max ?L1 = Max (?h ` ?B)" + proof - + have "?L1 = ?f ` (\ x \ ?B.(?g x))" by simp + also have "... = (\ x \ ?B. ?f ` (?g x))" by auto + finally have "Max ?L1 = Max ..." by simp + also have "... = Max (Max ` (\x. ?f ` subtree (tRAG s) x) ` ?B)" + by (subst Max_UNION, simp+) + also have "... = Max (cp_gen s ` children (tRAG s) x)" + by (unfold image_comp cp_gen_alt_def, simp) + finally show ?thesis . + qed + show ?thesis + proof - + have "?L = Max (?f ` ?A \ ?L1)" by simp + also have "... = max (the_preced s (the_thread x)) (Max ?L1)" + by (subst Max_Un, simp+) + also have "... = max (?f x) (Max (?h ` ?B))" + by (unfold eq_Max_L1, simp) + also have "... =?R" + by (rule max_Max_eq, (simp)+, unfold assms, simp) + finally show ?thesis . + qed + qed thus ?thesis + by (fold h subtree_children, unfold cp_gen_def, simp) +qed + +lemma cp_rec: + "cp s th = Max ({the_preced s th} \ + (cp s o the_thread) ` children (tRAG s) (Th th))" +proof - + have "Th th = Th th" by simp + note h = cp_gen_def_cond[OF this] cp_gen_rec[OF this] + show ?thesis + proof - + have "cp_gen s ` children (tRAG s) (Th th) = + (cp s \ the_thread) ` children (tRAG s) (Th th)" + proof(rule cp_gen_over_set) + show " \x\children (tRAG s) (Th th). \th. x = Th th" + by (unfold tRAG_alt_def, auto simp:children_def) + qed + thus ?thesis by (subst (1) h(1), unfold h(2), simp) + qed +qed + +end + +(* keep *) +lemma next_th_holding: + assumes vt: "vt s" + and nxt: "next_th s th cs th'" + shows "holding (wq s) th cs" +proof - + from nxt[unfolded next_th_def] + obtain rest where h: "wq s cs = th # rest" + "rest \ []" + "th' = hd (SOME q. distinct q \ set q = set rest)" by auto + thus ?thesis + by (unfold cs_holding_def, auto) +qed + +context valid_trace +begin + +lemma next_th_waiting: + assumes nxt: "next_th s th cs th'" + shows "waiting (wq s) th' cs" +proof - + from nxt[unfolded next_th_def] + obtain rest where h: "wq s cs = th # rest" + "rest \ []" + "th' = hd (SOME q. distinct q \ set q = set rest)" by auto + from wq_distinct[of cs, unfolded h] + have dst: "distinct (th # rest)" . + have in_rest: "th' \ set rest" + proof(unfold h, rule someI2) + show "distinct rest \ set rest = set rest" using dst by auto + next + fix x assume "distinct x \ set x = set rest" + with h(2) + show "hd x \ set (rest)" by (cases x, auto) + qed + hence "th' \ set (wq s cs)" by (unfold h(1), auto) + moreover have "th' \ hd (wq s cs)" + by (unfold h(1), insert in_rest dst, auto) + ultimately show ?thesis by (auto simp:cs_waiting_def) +qed + +lemma next_th_RAG: + assumes nxt: "next_th (s::event list) th cs th'" + shows "{(Cs cs, Th th), (Th th', Cs cs)} \ RAG s" + using vt assms next_th_holding next_th_waiting + by (unfold s_RAG_def, simp) + +end + +-- {* A useless definition *} +definition cps:: "state \ (thread \ precedence) set" +where "cps s = {(th, cp s th) | th . th \ threads s}" + + +find_theorems "waiting" holding +context valid_trace +begin + +find_theorems "waiting" holding + +end + +end diff -r 17305a85493d -r c495eb16beb6 CpsG - ¸±±¾.thy~ --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CpsG - ¸±±¾.thy~ Wed Jan 27 19:28:42 2016 +0800 @@ -0,0 +1,4410 @@ +theory CpsG +imports PIPDefs +begin + +lemma Max_f_mono: + assumes seq: "A \ B" + and np: "A \ {}" + and fnt: "finite B" + shows "Max (f ` A) \ Max (f ` B)" +proof(rule Max_mono) + from seq show "f ` A \ f ` B" by auto +next + from np show "f ` A \ {}" by auto +next + from fnt and seq show "finite (f ` B)" by auto +qed + +(* I am going to use this file as a start point to retrofiting + PIPBasics.thy, which is originally called CpsG.ghy *) + +locale valid_trace = + fixes s + assumes vt : "vt s" + +locale valid_trace_e = valid_trace + + fixes e + assumes vt_e: "vt (e#s)" +begin + +lemma pip_e: "PIP s e" + using vt_e by (cases, simp) + +end + +locale valid_trace_create = valid_trace_e + + fixes th prio + assumes is_create: "e = Create th prio" + +locale valid_trace_exit = valid_trace_e + + fixes th + assumes is_exit: "e = Exit th" + +locale valid_trace_p = valid_trace_e + + fixes th cs + assumes is_p: "e = P th cs" + +locale valid_trace_v = valid_trace_e + + fixes th cs + assumes is_v: "e = V th cs" +begin + definition "rest = tl (wq s cs)" + definition "wq' = (SOME q. distinct q \ set q = set rest)" +end + +locale valid_trace_v_n = valid_trace_v + + assumes rest_nnl: "rest \ []" + +locale valid_trace_v_e = valid_trace_v + + assumes rest_nil: "rest = []" + +locale valid_trace_set= valid_trace_e + + fixes th prio + assumes is_set: "e = Set th prio" + +context valid_trace +begin + +lemma ind [consumes 0, case_names Nil Cons, induct type]: + assumes "PP []" + and "(\s e. valid_trace_e s e \ + PP s \ PIP s e \ PP (e # s))" + shows "PP s" +proof(induct rule:vt.induct[OF vt, case_names Init Step]) + case Init + from assms(1) show ?case . +next + case (Step s e) + show ?case + proof(rule assms(2)) + show "valid_trace_e s e" using Step by (unfold_locales, auto) + next + show "PP s" using Step by simp + next + show "PIP s e" using Step by simp + qed +qed + +end + + +lemma waiting_eq: "waiting s th cs = waiting (wq s) th cs" + by (unfold s_waiting_def cs_waiting_def wq_def, auto) + +lemma holding_eq: "holding (s::state) th cs = holding (wq s) th cs" + by (unfold s_holding_def wq_def cs_holding_def, simp) + +lemma runing_ready: + shows "runing s \ readys s" + unfolding runing_def readys_def + by auto + +lemma readys_threads: + shows "readys s \ threads s" + unfolding readys_def + by auto + +lemma wq_v_neq [simp]: + "cs \ cs' \ wq (V thread cs#s) cs' = wq s cs'" + by (auto simp:wq_def Let_def cp_def split:list.splits) + +lemma runing_head: + assumes "th \ runing s" + and "th \ set (wq_fun (schs s) cs)" + shows "th = hd (wq_fun (schs s) cs)" + using assms + by (simp add:runing_def readys_def s_waiting_def wq_def) + +context valid_trace +begin + +lemma runing_wqE: + assumes "th \ runing s" + and "th \ set (wq s cs)" + obtains rest where "wq s cs = th#rest" +proof - + from assms(2) obtain th' rest where eq_wq: "wq s cs = th'#rest" + by (meson list.set_cases) + have "th' = th" + proof(rule ccontr) + assume "th' \ th" + hence "th \ hd (wq s cs)" using eq_wq by auto + with assms(2) + have "waiting s th cs" + by (unfold s_waiting_def, fold wq_def, auto) + with assms show False + by (unfold runing_def readys_def, auto) + qed + with eq_wq that show ?thesis by metis +qed + +end + +context valid_trace_p +begin + +lemma wq_neq_simp [simp]: + assumes "cs' \ cs" + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_p wq_def + by (auto simp:Let_def) + +lemma runing_th_s: + shows "th \ runing s" +proof - + from pip_e[unfolded is_p] + show ?thesis by (cases, simp) +qed + +lemma th_not_waiting: + "\ waiting s th c" +proof - + have "th \ readys s" + using runing_ready runing_th_s by blast + thus ?thesis + by (unfold readys_def, auto) +qed + +lemma waiting_neq_th: + assumes "waiting s t c" + shows "t \ th" + using assms using th_not_waiting by blast + +lemma th_not_in_wq: + shows "th \ set (wq s cs)" +proof + assume otherwise: "th \ set (wq s cs)" + from runing_wqE[OF runing_th_s this] + obtain rest where eq_wq: "wq s cs = th#rest" by blast + with otherwise + have "holding s th cs" + by (unfold s_holding_def, fold wq_def, simp) + hence cs_th_RAG: "(Cs cs, Th th) \ RAG s" + by (unfold s_RAG_def, fold holding_eq, auto) + from pip_e[unfolded is_p] + show False + proof(cases) + case (thread_P) + with cs_th_RAG show ?thesis by auto + qed +qed + +lemma wq_es_cs: + "wq (e#s) cs = wq s cs @ [th]" + by (unfold is_p wq_def, auto simp:Let_def) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" +proof(cases "cs' = cs") + case True + show ?thesis using True assms th_not_in_wq + by (unfold True wq_es_cs, auto) +qed (insert assms, simp) + +end + + +context valid_trace_v +begin + +lemma wq_neq_simp [simp]: + assumes "cs' \ cs" + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_v wq_def + by (auto simp:Let_def) + +lemma runing_th_s: + shows "th \ runing s" +proof - + from pip_e[unfolded is_v] + show ?thesis by (cases, simp) +qed + +lemma th_not_waiting: + "\ waiting s th c" +proof - + have "th \ readys s" + using runing_ready runing_th_s by blast + thus ?thesis + by (unfold readys_def, auto) +qed + +lemma waiting_neq_th: + assumes "waiting s t c" + shows "t \ th" + using assms using th_not_waiting by blast + +lemma wq_s_cs: + "wq s cs = th#rest" +proof - + from pip_e[unfolded is_v] + show ?thesis + proof(cases) + case (thread_V) + from this(2) show ?thesis + by (unfold rest_def s_holding_def, fold wq_def, + metis empty_iff list.collapse list.set(1)) + qed +qed + +lemma wq_es_cs: + "wq (e#s) cs = wq'" + using wq_s_cs[unfolded wq_def] + by (auto simp:Let_def wq_def rest_def wq'_def is_v, simp) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" +proof(cases "cs' = cs") + case True + show ?thesis + proof(unfold True wq_es_cs wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + using assms[unfolded True wq_s_cs] by auto + qed simp +qed (insert assms, simp) + +end + +context valid_trace +begin + +lemma actor_inv: + assumes "PIP s e" + and "\ isCreate e" + shows "actor e \ runing s" + using assms + by (induct, auto) + +lemma isP_E: + assumes "isP e" + obtains cs where "e = P (actor e) cs" + using assms by (cases e, auto) + +lemma isV_E: + assumes "isV e" + obtains cs where "e = V (actor e) cs" + using assms by (cases e, auto) + +lemma wq_distinct: "distinct (wq s cs)" +proof(induct rule:ind) + case (Cons s e) + interpret vt_e: valid_trace_e s e using Cons by simp + show ?case + proof(cases e) + case (V th cs) + interpret vt_v: valid_trace_v s e th cs using V by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_v.wq_distinct_kept) + qed +qed (unfold wq_def Let_def, simp) + +end + +context valid_trace_e +begin + +text {* + The following lemma shows that only the @{text "P"} + operation can add new thread into waiting queues. + Such kind of lemmas are very obvious, but need to be checked formally. + This is a kind of confirmation that our modelling is correct. +*} + +lemma wq_in_inv: + assumes s_ni: "thread \ set (wq s cs)" + and s_i: "thread \ set (wq (e#s) cs)" + shows "e = P thread cs" +proof(cases e) + -- {* This is the only non-trivial case: *} + case (V th cs1) + have False + proof(cases "cs1 = cs") + case True + show ?thesis + proof(cases "(wq s cs1)") + case (Cons w_hd w_tl) + have "set (wq (e#s) cs) \ set (wq s cs)" + proof - + have "(wq (e#s) cs) = (SOME q. distinct q \ set q = set w_tl)" + using Cons V by (auto simp:wq_def Let_def True split:if_splits) + moreover have "set ... \ set (wq s cs)" + proof(rule someI2) + show "distinct w_tl \ set w_tl = set w_tl" + by (metis distinct.simps(2) local.Cons wq_distinct) + qed (insert Cons True, auto) + ultimately show ?thesis by simp + qed + with assms show ?thesis by auto + qed (insert assms V True, auto simp:wq_def Let_def split:if_splits) + qed (insert assms V, auto simp:wq_def Let_def split:if_splits) + thus ?thesis by auto +qed (insert assms, auto simp:wq_def Let_def split:if_splits) + +lemma wq_out_inv: + assumes s_in: "thread \ set (wq s cs)" + and s_hd: "thread = hd (wq s cs)" + and s_i: "thread \ hd (wq (e#s) cs)" + shows "e = V thread cs" +proof(cases e) +-- {* There are only two non-trivial cases: *} + case (V th cs1) + show ?thesis + proof(cases "cs1 = cs") + case True + have "PIP s (V th cs)" using pip_e[unfolded V[unfolded True]] . + thus ?thesis + proof(cases) + case (thread_V) + moreover have "th = thread" using thread_V(2) s_hd + by (unfold s_holding_def wq_def, simp) + ultimately show ?thesis using V True by simp + qed + qed (insert assms V, auto simp:wq_def Let_def split:if_splits) +next + case (P th cs1) + show ?thesis + proof(cases "cs1 = cs") + case True + with P have "wq (e#s) cs = wq_fun (schs s) cs @ [th]" + by (auto simp:wq_def Let_def split:if_splits) + with s_i s_hd s_in have False + by (metis empty_iff hd_append2 list.set(1) wq_def) + thus ?thesis by simp + qed (insert assms P, auto simp:wq_def Let_def split:if_splits) +qed (insert assms, auto simp:wq_def Let_def split:if_splits) + +end + + +context valid_trace +begin + +end + + + +context valid_trace +begin + + +text {* (* ddd *) + The nature of the work is like this: since it starts from a very simple and basic + model, even intuitively very `basic` and `obvious` properties need to derived from scratch. + For instance, the fact + that one thread can not be blocked by two critical resources at the same time + is obvious, because only running threads can make new requests, if one is waiting for + a critical resource and get blocked, it can not make another resource request and get + blocked the second time (because it is not running). + + To derive this fact, one needs to prove by contraction and + reason about time (or @{text "moement"}). The reasoning is based on a generic theorem + named @{text "p_split"}, which is about status changing along the time axis. It says if + a condition @{text "Q"} is @{text "True"} at a state @{text "s"}, + but it was @{text "False"} at the very beginning, then there must exits a moment @{text "t"} + in the history of @{text "s"} (notice that @{text "s"} itself is essentially the history + of events leading to it), such that @{text "Q"} switched + from being @{text "False"} to @{text "True"} and kept being @{text "True"} + till the last moment of @{text "s"}. + + Suppose a thread @{text "th"} is blocked + on @{text "cs1"} and @{text "cs2"} in some state @{text "s"}, + since no thread is blocked at the very beginning, by applying + @{text "p_split"} to these two blocking facts, there exist + two moments @{text "t1"} and @{text "t2"} in @{text "s"}, such that + @{text "th"} got blocked on @{text "cs1"} and @{text "cs2"} + and kept on blocked on them respectively ever since. + + Without lost of generality, we assume @{text "t1"} is earlier than @{text "t2"}. + However, since @{text "th"} was blocked ever since memonent @{text "t1"}, so it was still + in blocked state at moment @{text "t2"} and could not + make any request and get blocked the second time: Contradiction. +*} + +lemma waiting_unique_pre: (* ddd *) + assumes h11: "thread \ set (wq s cs1)" + and h12: "thread \ hd (wq s cs1)" + assumes h21: "thread \ set (wq s cs2)" + and h22: "thread \ hd (wq s cs2)" + and neq12: "cs1 \ cs2" + shows "False" +proof - + let "?Q" = "\ cs s. thread \ set (wq s cs) \ thread \ hd (wq s cs)" + from h11 and h12 have q1: "?Q cs1 s" by simp + from h21 and h22 have q2: "?Q cs2 s" by simp + have nq1: "\ ?Q cs1 []" by (simp add:wq_def) + have nq2: "\ ?Q cs2 []" by (simp add:wq_def) + from p_split [of "?Q cs1", OF q1 nq1] + obtain t1 where lt1: "t1 < length s" + and np1: "\ ?Q cs1 (moment t1 s)" + and nn1: "(\i'>t1. ?Q cs1 (moment i' s))" by auto + from p_split [of "?Q cs2", OF q2 nq2] + obtain t2 where lt2: "t2 < length s" + and np2: "\ ?Q cs2 (moment t2 s)" + and nn2: "(\i'>t2. ?Q cs2 (moment i' s))" by auto + { fix s cs + assume q: "?Q cs s" + have "thread \ runing s" + proof + assume "thread \ runing s" + hence " \cs. \ (thread \ set (wq_fun (schs s) cs) \ + thread \ hd (wq_fun (schs s) cs))" + by (unfold runing_def s_waiting_def readys_def, auto) + from this[rule_format, of cs] q + show False by (simp add: wq_def) + qed + } note q_not_runing = this + { fix t1 t2 cs1 cs2 + assume lt1: "t1 < length s" + and np1: "\ ?Q cs1 (moment t1 s)" + and nn1: "(\i'>t1. ?Q cs1 (moment i' s))" + and lt2: "t2 < length s" + and np2: "\ ?Q cs2 (moment t2 s)" + and nn2: "(\i'>t2. ?Q cs2 (moment i' s))" + and lt12: "t1 < t2" + let ?t3 = "Suc t2" + from lt2 have le_t3: "?t3 \ length s" by auto + from moment_plus [OF this] + obtain e where eq_m: "moment ?t3 s = e#moment t2 s" by auto + have "t2 < ?t3" by simp + from nn2 [rule_format, OF this] and eq_m + have h1: "thread \ set (wq (e#moment t2 s) cs2)" and + h2: "thread \ hd (wq (e#moment t2 s) cs2)" by auto + have "vt (e#moment t2 s)" + proof - + from vt_moment + have "vt (moment ?t3 s)" . + with eq_m show ?thesis by simp + qed + then interpret vt_e: valid_trace_e "moment t2 s" "e" + by (unfold_locales, auto, cases, simp) + have ?thesis + proof - + have "thread \ runing (moment t2 s)" + proof(cases "thread \ set (wq (moment t2 s) cs2)") + case True + have "e = V thread cs2" + proof - + have eq_th: "thread = hd (wq (moment t2 s) cs2)" + using True and np2 by auto + from vt_e.wq_out_inv[OF True this h2] + show ?thesis . + qed + thus ?thesis using vt_e.actor_inv[OF vt_e.pip_e] by auto + next + case False + have "e = P thread cs2" using vt_e.wq_in_inv[OF False h1] . + with vt_e.actor_inv[OF vt_e.pip_e] + show ?thesis by auto + qed + moreover have "thread \ runing (moment t2 s)" + by (rule q_not_runing[OF nn1[rule_format, OF lt12]]) + ultimately show ?thesis by simp + qed + } note lt_case = this + show ?thesis + proof - + { assume "t1 < t2" + from lt_case[OF lt1 np1 nn1 lt2 np2 nn2 this] + have ?thesis . + } moreover { + assume "t2 < t1" + from lt_case[OF lt2 np2 nn2 lt1 np1 nn1 this] + have ?thesis . + } moreover { + assume eq_12: "t1 = t2" + let ?t3 = "Suc t2" + from lt2 have le_t3: "?t3 \ length s" by auto + from moment_plus [OF this] + obtain e where eq_m: "moment ?t3 s = e#moment t2 s" by auto + have lt_2: "t2 < ?t3" by simp + from nn2 [rule_format, OF this] and eq_m + have h1: "thread \ set (wq (e#moment t2 s) cs2)" and + h2: "thread \ hd (wq (e#moment t2 s) cs2)" by auto + from nn1[rule_format, OF lt_2[folded eq_12]] eq_m[folded eq_12] + have g1: "thread \ set (wq (e#moment t1 s) cs1)" and + g2: "thread \ hd (wq (e#moment t1 s) cs1)" by auto + have "vt (e#moment t2 s)" + proof - + from vt_moment + have "vt (moment ?t3 s)" . + with eq_m show ?thesis by simp + qed + then interpret vt_e: valid_trace_e "moment t2 s" "e" + by (unfold_locales, auto, cases, simp) + have "e = V thread cs2 \ e = P thread cs2" + proof(cases "thread \ set (wq (moment t2 s) cs2)") + case True + have "e = V thread cs2" + proof - + have eq_th: "thread = hd (wq (moment t2 s) cs2)" + using True and np2 by auto + from vt_e.wq_out_inv[OF True this h2] + show ?thesis . + qed + thus ?thesis by auto + next + case False + have "e = P thread cs2" using vt_e.wq_in_inv[OF False h1] . + thus ?thesis by auto + qed + moreover have "e = V thread cs1 \ e = P thread cs1" + proof(cases "thread \ set (wq (moment t1 s) cs1)") + case True + have eq_th: "thread = hd (wq (moment t1 s) cs1)" + using True and np1 by auto + from vt_e.wq_out_inv[folded eq_12, OF True this g2] + have "e = V thread cs1" . + thus ?thesis by auto + next + case False + have "e = P thread cs1" using vt_e.wq_in_inv[folded eq_12, OF False g1] . + thus ?thesis by auto + qed + ultimately have ?thesis using neq12 by auto + } ultimately show ?thesis using nat_neq_iff by blast + qed +qed + +text {* + This lemma is a simple corrolary of @{text "waiting_unique_pre"}. +*} + +lemma waiting_unique: + assumes "waiting s th cs1" + and "waiting s th cs2" + shows "cs1 = cs2" + using waiting_unique_pre assms + unfolding wq_def s_waiting_def + by auto + +end + +(* not used *) +text {* + Every thread can only be blocked on one critical resource, + symmetrically, every critical resource can only be held by one thread. + This fact is much more easier according to our definition. +*} +lemma held_unique: + assumes "holding (s::event list) th1 cs" + and "holding s th2 cs" + shows "th1 = th2" + by (insert assms, unfold s_holding_def, auto) + + +lemma last_set_lt: "th \ threads s \ last_set th s < length s" + apply (induct s, auto) + by (case_tac a, auto split:if_splits) + +lemma last_set_unique: + "\last_set th1 s = last_set th2 s; th1 \ threads s; th2 \ threads s\ + \ th1 = th2" + apply (induct s, auto) + by (case_tac a, auto split:if_splits dest:last_set_lt) + +lemma preced_unique : + assumes pcd_eq: "preced th1 s = preced th2 s" + and th_in1: "th1 \ threads s" + and th_in2: " th2 \ threads s" + shows "th1 = th2" +proof - + from pcd_eq have "last_set th1 s = last_set th2 s" by (simp add:preced_def) + from last_set_unique [OF this th_in1 th_in2] + show ?thesis . +qed + +lemma preced_linorder: + assumes neq_12: "th1 \ th2" + and th_in1: "th1 \ threads s" + and th_in2: " th2 \ threads s" + shows "preced th1 s < preced th2 s \ preced th1 s > preced th2 s" +proof - + from preced_unique [OF _ th_in1 th_in2] and neq_12 + have "preced th1 s \ preced th2 s" by auto + thus ?thesis by auto +qed + +(* An aux lemma used later *) +lemma unique_minus: + assumes unique: "\ a b c. \(a, b) \ r; (a, c) \ r\ \ b = c" + and xy: "(x, y) \ r" + and xz: "(x, z) \ r^+" + and neq: "y \ z" + shows "(y, z) \ r^+" +proof - + from xz and neq show ?thesis + proof(induct) + case (base ya) + have "(x, ya) \ r" by fact + from unique [OF xy this] have "y = ya" . + with base show ?case by auto + next + case (step ya z) + show ?case + proof(cases "y = ya") + case True + from step True show ?thesis by simp + next + case False + from step False + show ?thesis by auto + qed + qed +qed + +lemma unique_base: + assumes unique: "\ a b c. \(a, b) \ r; (a, c) \ r\ \ b = c" + and xy: "(x, y) \ r" + and xz: "(x, z) \ r^+" + and neq_yz: "y \ z" + shows "(y, z) \ r^+" +proof - + from xz neq_yz show ?thesis + proof(induct) + case (base ya) + from xy unique base show ?case by auto + next + case (step ya z) + show ?case + proof(cases "y = ya") + case True + from True step show ?thesis by auto + next + case False + from False step + have "(y, ya) \ r\<^sup>+" by auto + with step show ?thesis by auto + qed + qed +qed + +lemma unique_chain: + assumes unique: "\ a b c. \(a, b) \ r; (a, c) \ r\ \ b = c" + and xy: "(x, y) \ r^+" + and xz: "(x, z) \ r^+" + and neq_yz: "y \ z" + shows "(y, z) \ r^+ \ (z, y) \ r^+" +proof - + from xy xz neq_yz show ?thesis + proof(induct) + case (base y) + have h1: "(x, y) \ r" and h2: "(x, z) \ r\<^sup>+" and h3: "y \ z" using base by auto + from unique_base [OF _ h1 h2 h3] and unique show ?case by auto + next + case (step y za) + show ?case + proof(cases "y = z") + case True + from True step show ?thesis by auto + next + case False + from False step have "(y, z) \ r\<^sup>+ \ (z, y) \ r\<^sup>+" by auto + thus ?thesis + proof + assume "(z, y) \ r\<^sup>+" + with step have "(z, za) \ r\<^sup>+" by auto + thus ?thesis by auto + next + assume h: "(y, z) \ r\<^sup>+" + from step have yza: "(y, za) \ r" by simp + from step have "za \ z" by simp + from unique_minus [OF _ yza h this] and unique + have "(za, z) \ r\<^sup>+" by auto + thus ?thesis by auto + qed + qed + qed +qed + +text {* + The following three lemmas show that @{text "RAG"} does not change + by the happening of @{text "Set"}, @{text "Create"} and @{text "Exit"} + events, respectively. +*} + +lemma RAG_set_unchanged: "(RAG (Set th prio # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + +lemma RAG_create_unchanged: "(RAG (Create th prio # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + +lemma RAG_exit_unchanged: "(RAG (Exit th # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + + + +context valid_trace_v +begin + + +lemma distinct_rest: "distinct rest" + by (simp add: distinct_tl rest_def wq_distinct) + +definition "wq' = (SOME q. distinct q \ set q = set rest)" + +lemma runing_th_s: + shows "th \ runing s" +proof - + from pip_e[unfolded is_v] + show ?thesis by (cases, simp) +qed + +lemma holding_cs_eq_th: + assumes "holding s t cs" + shows "t = th" +proof - + from pip_e[unfolded is_v] + show ?thesis + proof(cases) + case (thread_V) + from held_unique[OF this(2) assms] + show ?thesis by simp + qed +qed + +lemma th_not_waiting: + "\ waiting s th c" +proof - + have "th \ readys s" + using runing_ready runing_th_s by blast + thus ?thesis + by (unfold readys_def, auto) +qed + +lemma waiting_neq_th: + assumes "waiting s t c" + shows "t \ th" + using assms using th_not_waiting by blast + +lemma wq_s_cs: + "wq s cs = th#rest" +proof - + from pip_e[unfolded is_v] + show ?thesis + proof(cases) + case (thread_V) + from this(2) show ?thesis + by (unfold rest_def s_holding_def, fold wq_def, + metis empty_iff list.collapse list.set(1)) + qed +qed + +lemma wq_es_cs: + "wq (e#s) cs = wq'" + using wq_s_cs[unfolded wq_def] + by (auto simp:Let_def wq_def rest_def wq'_def is_v, simp) + +lemma distinct_wq': "distinct wq'" + by (metis (mono_tags, lifting) distinct_rest some_eq_ex wq'_def) + +lemma th'_in_inv: + assumes "th' \ set wq'" + shows "th' \ set rest" + using assms + by (metis (mono_tags, lifting) distinct.simps(2) + rest_def some_eq_ex wq'_def wq_distinct wq_s_cs) + +lemma neq_t_th: + assumes "waiting (e#s) t c" + shows "t \ th" +proof + assume otherwise: "t = th" + show False + proof(cases "c = cs") + case True + have "t \ set wq'" + using assms[unfolded True s_waiting_def, folded wq_def, unfolded wq_es_cs] + by simp + from th'_in_inv[OF this] have "t \ set rest" . + with wq_s_cs[folded otherwise] wq_distinct[of cs] + show ?thesis by simp + next + case False + have "wq (e#s) c = wq s c" using False + by (unfold is_v, simp) + hence "waiting s t c" using assms + by (simp add: cs_waiting_def waiting_eq) + hence "t \ readys s" by (unfold readys_def, auto) + hence "t \ runing s" using runing_ready by auto + with runing_th_s[folded otherwise] show ?thesis by auto + qed +qed + +lemma waiting_esI1: + assumes "waiting s t c" + and "c \ cs" + shows "waiting (e#s) t c" +proof - + have "wq (e#s) c = wq s c" + using assms(2) is_v by auto + with assms(1) show ?thesis + using cs_waiting_def waiting_eq by auto +qed + +lemma holding_esI2: + assumes "c \ cs" + and "holding s t c" + shows "holding (e#s) t c" +proof - + from assms(1) have "wq (e#s) c = wq s c" using is_v by auto + from assms(2)[unfolded s_holding_def, folded wq_def, + folded this, unfolded wq_def, folded s_holding_def] + show ?thesis . +qed + +lemma holding_esI1: + assumes "holding s t c" + and "t \ th" + shows "holding (e#s) t c" +proof - + have "c \ cs" using assms using holding_cs_eq_th by blast + from holding_esI2[OF this assms(1)] + show ?thesis . +qed + +end + +context valid_trace_v_n +begin + +lemma neq_wq': "wq' \ []" +proof (unfold wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) +next + fix x + assume " distinct x \ set x = set rest" + thus "x \ []" using rest_nnl by auto +qed + +definition "taker = hd wq'" + +definition "rest' = tl wq'" + +lemma eq_wq': "wq' = taker # rest'" + by (simp add: neq_wq' rest'_def taker_def) + +lemma next_th_taker: + shows "next_th s th cs taker" + using rest_nnl taker_def wq'_def wq_s_cs + by (auto simp:next_th_def) + +lemma taker_unique: + assumes "next_th s th cs taker'" + shows "taker' = taker" +proof - + from assms + obtain rest' where + h: "wq s cs = th # rest'" + "taker' = hd (SOME q. distinct q \ set q = set rest')" + by (unfold next_th_def, auto) + with wq_s_cs have "rest' = rest" by auto + thus ?thesis using h(2) taker_def wq'_def by auto +qed + +lemma waiting_set_eq: + "{(Th th', Cs cs) |th'. next_th s th cs th'} = {(Th taker, Cs cs)}" + by (smt all_not_in_conv bot.extremum insertI1 insert_subset + mem_Collect_eq next_th_taker subsetI subset_antisym taker_def taker_unique) + +lemma holding_set_eq: + "{(Cs cs, Th th') |th'. next_th s th cs th'} = {(Cs cs, Th taker)}" + using next_th_taker taker_def waiting_set_eq + by fastforce + +lemma holding_taker: + shows "holding (e#s) taker cs" + by (unfold s_holding_def, fold wq_def, unfold wq_es_cs, + auto simp:neq_wq' taker_def) + +lemma waiting_esI2: + assumes "waiting s t cs" + and "t \ taker" + shows "waiting (e#s) t cs" +proof - + have "t \ set wq'" + proof(unfold wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) + next + fix x + assume "distinct x \ set x = set rest" + moreover have "t \ set rest" + using assms(1) cs_waiting_def waiting_eq wq_s_cs by auto + ultimately show "t \ set x" by simp + qed + moreover have "t \ hd wq'" + using assms(2) taker_def by auto + ultimately show ?thesis + by (unfold s_waiting_def, fold wq_def, unfold wq_es_cs, simp) +qed + +lemma waiting_esE: + assumes "waiting (e#s) t c" + obtains "c \ cs" "waiting s t c" + | "c = cs" "t \ taker" "waiting s t cs" "t \ set rest'" +proof(cases "c = cs") + case False + hence "wq (e#s) c = wq s c" using is_v by auto + with assms have "waiting s t c" using cs_waiting_def waiting_eq by auto + from that(1)[OF False this] show ?thesis . +next + case True + from assms[unfolded s_waiting_def True, folded wq_def, unfolded wq_es_cs] + have "t \ hd wq'" "t \ set wq'" by auto + hence "t \ taker" by (simp add: taker_def) + moreover hence "t \ th" using assms neq_t_th by blast + moreover have "t \ set rest" by (simp add: `t \ set wq'` th'_in_inv) + ultimately have "waiting s t cs" + by (metis cs_waiting_def list.distinct(2) list.sel(1) + list.set_sel(2) rest_def waiting_eq wq_s_cs) + show ?thesis using that(2) + using True `t \ set wq'` `t \ taker` `waiting s t cs` eq_wq' by auto +qed + +lemma holding_esI1: + assumes "c = cs" + and "t = taker" + shows "holding (e#s) t c" + by (unfold assms, simp add: holding_taker) + +lemma holding_esE: + assumes "holding (e#s) t c" + obtains "c = cs" "t = taker" + | "c \ cs" "holding s t c" +proof(cases "c = cs") + case True + from assms[unfolded True, unfolded s_holding_def, + folded wq_def, unfolded wq_es_cs] + have "t = taker" by (simp add: taker_def) + from that(1)[OF True this] show ?thesis . +next + case False + hence "wq (e#s) c = wq s c" using is_v by auto + from assms[unfolded s_holding_def, folded wq_def, + unfolded this, unfolded wq_def, folded s_holding_def] + have "holding s t c" . + from that(2)[OF False this] show ?thesis . +qed + +end + + +context valid_trace_v_n +begin + +lemma nil_wq': "wq' = []" +proof (unfold wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) +next + fix x + assume " distinct x \ set x = set rest" + thus "x = []" using rest_nil by auto +qed + +lemma no_taker: + assumes "next_th s th cs taker" + shows "False" +proof - + from assms[unfolded next_th_def] + obtain rest' where "wq s cs = th # rest'" "rest' \ []" + by auto + thus ?thesis using rest_def rest_nil by auto +qed + +lemma waiting_set_eq: + "{(Th th', Cs cs) |th'. next_th s th cs th'} = {}" + using no_taker by auto + +lemma holding_set_eq: + "{(Cs cs, Th th') |th'. next_th s th cs th'} = {}" + using no_taker by auto + +lemma no_holding: + assumes "holding (e#s) taker cs" + shows False +proof - + from wq_es_cs[unfolded nil_wq'] + have " wq (e # s) cs = []" . + from assms[unfolded s_holding_def, folded wq_def, unfolded this] + show ?thesis by auto +qed + +lemma no_waiting: + assumes "waiting (e#s) t cs" + shows False +proof - + from wq_es_cs[unfolded nil_wq'] + have " wq (e # s) cs = []" . + from assms[unfolded s_waiting_def, folded wq_def, unfolded this] + show ?thesis by auto +qed + +lemma waiting_esI2: + assumes "waiting s t c" + shows "waiting (e#s) t c" +proof - + have "c \ cs" using assms + using cs_waiting_def rest_nil waiting_eq wq_s_cs by auto + from waiting_esI1[OF assms this] + show ?thesis . +qed + +lemma waiting_esE: + assumes "waiting (e#s) t c" + obtains "c \ cs" "waiting s t c" +proof(cases "c = cs") + case False + hence "wq (e#s) c = wq s c" using is_v by auto + with assms have "waiting s t c" using cs_waiting_def waiting_eq by auto + from that(1)[OF False this] show ?thesis . +next + case True + from no_waiting[OF assms[unfolded True]] + show ?thesis by auto +qed + +lemma holding_esE: + assumes "holding (e#s) t c" + obtains "c \ cs" "holding s t c" +proof(cases "c = cs") + case True + from no_holding[OF assms[unfolded True]] + show ?thesis by auto +next + case False + hence "wq (e#s) c = wq s c" using is_v by auto + from assms[unfolded s_holding_def, folded wq_def, + unfolded this, unfolded wq_def, folded s_holding_def] + have "holding s t c" . + from that[OF False this] show ?thesis . +qed + +end (* ccc *) + +lemma rel_eqI: + assumes "\ x y. (x,y) \ A \ (x,y) \ B" + and "\ x y. (x,y) \ B \ (x, y) \ A" + shows "A = B" + using assms by auto + +lemma in_RAG_E: + assumes "(n1, n2) \ RAG (s::state)" + obtains (waiting) th cs where "n1 = Th th" "n2 = Cs cs" "waiting s th cs" + | (holding) th cs where "n1 = Cs cs" "n2 = Th th" "holding s th cs" + using assms[unfolded s_RAG_def, folded waiting_eq holding_eq] + by auto + +context valid_trace_v +begin + +lemma RAG_es: + "RAG (e # s) = + RAG s - {(Cs cs, Th th)} - + {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'}" (is "?L = ?R") +proof(rule rel_eqI) + fix n1 n2 + assume "(n1, n2) \ ?L" + thus "(n1, n2) \ ?R" + proof(cases rule:in_RAG_E) + case (waiting th' cs') + show ?thesis + proof(cases "rest = []") + case False + interpret h_n: valid_trace_v_n s e th cs + by (unfold_locales, insert False, simp) + from waiting(3) + show ?thesis + proof(cases rule:h_n.waiting_esE) + case 1 + with waiting(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + next + case 2 + with waiting(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + qed + next + case True + interpret h_e: valid_trace_v_e s e th cs + by (unfold_locales, insert True, simp) + from waiting(3) + show ?thesis + proof(cases rule:h_e.waiting_esE) + case 1 + with waiting(1,2) + show ?thesis + by (unfold h_e.waiting_set_eq h_e.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + qed + qed + next + case (holding th' cs') + show ?thesis + proof(cases "rest = []") + case False + interpret h_n: valid_trace_v_n s e th cs + by (unfold_locales, insert False, simp) + from holding(3) + show ?thesis + proof(cases rule:h_n.holding_esE) + case 1 + with holding(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + next + case 2 + with holding(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold holding_eq, auto) + qed + next + case True + interpret h_e: valid_trace_v_e s e th cs + by (unfold_locales, insert True, simp) + from holding(3) + show ?thesis + proof(cases rule:h_e.holding_esE) + case 1 + with holding(1,2) + show ?thesis + by (unfold h_e.waiting_set_eq h_e.holding_set_eq s_RAG_def, + fold holding_eq, auto) + qed + qed + qed +next + fix n1 n2 + assume h: "(n1, n2) \ ?R" + show "(n1, n2) \ ?L" + proof(cases "rest = []") + case False + interpret h_n: valid_trace_v_n s e th cs + by (unfold_locales, insert False, simp) + from h[unfolded h_n.waiting_set_eq h_n.holding_set_eq] + have "((n1, n2) \ RAG s \ (n1 \ Cs cs \ n2 \ Th th) + \ (n1 \ Th h_n.taker \ n2 \ Cs cs)) \ + (n2 = Th h_n.taker \ n1 = Cs cs)" + by auto + thus ?thesis + proof + assume "n2 = Th h_n.taker \ n1 = Cs cs" + with h_n.holding_taker + show ?thesis + by (unfold s_RAG_def, fold holding_eq, auto) + next + assume h: "(n1, n2) \ RAG s \ + (n1 \ Cs cs \ n2 \ Th th) \ (n1 \ Th h_n.taker \ n2 \ Cs cs)" + hence "(n1, n2) \ RAG s" by simp + thus ?thesis + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from h and this(1,2) + have "th' \ h_n.taker \ cs' \ cs" by auto + hence "waiting (e#s) th' cs'" + proof + assume "cs' \ cs" + from waiting_esI1[OF waiting(3) this] + show ?thesis . + next + assume neq_th': "th' \ h_n.taker" + show ?thesis + proof(cases "cs' = cs") + case False + from waiting_esI1[OF waiting(3) this] + show ?thesis . + next + case True + from h_n.waiting_esI2[OF waiting(3)[unfolded True] neq_th', folded True] + show ?thesis . + qed + qed + thus ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case (holding th' cs') + from h this(1,2) + have "cs' \ cs \ th' \ th" by auto + hence "holding (e#s) th' cs'" + proof + assume "cs' \ cs" + from holding_esI2[OF this holding(3)] + show ?thesis . + next + assume "th' \ th" + from holding_esI1[OF holding(3) this] + show ?thesis . + qed + thus ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + qed + qed + next + case True + interpret h_e: valid_trace_v_e s e th cs + by (unfold_locales, insert True, simp) + from h[unfolded h_e.waiting_set_eq h_e.holding_set_eq] + have h_s: "(n1, n2) \ RAG s" "(n1, n2) \ (Cs cs, Th th)" + by auto + from h_s(1) + show ?thesis + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from h_e.waiting_esI2[OF this(3)] + show ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case (holding th' cs') + with h_s(2) + have "cs' \ cs \ th' \ th" by auto + thus ?thesis + proof + assume neq_cs: "cs' \ cs" + from holding_esI2[OF this holding(3)] + show ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + next + assume "th' \ th" + from holding_esI1[OF holding(3) this] + show ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + qed + qed + qed +qed + +end + + + +context valid_trace +begin + +lemma finite_threads: + shows "finite (threads s)" +using vt by (induct) (auto elim: step.cases) + +lemma cp_eq_cpreced: "cp s th = cpreced (wq s) s th" +unfolding cp_def wq_def +apply(induct s rule: schs.induct) +apply(simp add: Let_def cpreced_initial) +apply(simp add: Let_def) +apply(simp add: Let_def) +apply(simp add: Let_def) +apply(subst (2) schs.simps) +apply(simp add: Let_def) +apply(subst (2) schs.simps) +apply(simp add: Let_def) +done + +lemma RAG_target_th: "(Th th, x) \ RAG (s::state) \ \ cs. x = Cs cs" + by (unfold s_RAG_def, auto) + +lemma wq_threads: + assumes h: "th \ set (wq s cs)" + shows "th \ threads s" + + +lemma wq_threads: + assumes h: "th \ set (wq s cs)" + shows "th \ threads s" +proof - + from vt and h show ?thesis + proof(induct arbitrary: th cs) + case (vt_cons s e) + interpret vt_s: valid_trace s + using vt_cons(1) by (unfold_locales, auto) + assume ih: "\th cs. th \ set (wq s cs) \ th \ threads s" + and stp: "step s e" + and vt: "vt s" + and h: "th \ set (wq (e # s) cs)" + show ?case + proof(cases e) + case (Create th' prio) + with ih h show ?thesis + by (auto simp:wq_def Let_def) + next + case (Exit th') + with stp ih h show ?thesis + apply (auto simp:wq_def Let_def) + apply (ind_cases "step s (Exit th')") + apply (auto simp:runing_def readys_def s_holding_def s_waiting_def holdents_def + s_RAG_def s_holding_def cs_holding_def) + done + next + case (V th' cs') + show ?thesis + proof(cases "cs' = cs") + case False + with h + show ?thesis + apply(unfold wq_def V, auto simp:Let_def V split:prod.splits, fold wq_def) + by (drule_tac ih, simp) + next + case True + from h + show ?thesis + proof(unfold V wq_def) + assume th_in: "th \ set (wq_fun (schs (V th' cs' # s)) cs)" (is "th \ set ?l") + show "th \ threads (V th' cs' # s)" + proof(cases "cs = cs'") + case False + hence "?l = wq_fun (schs s) cs" by (simp add:Let_def) + with th_in have " th \ set (wq s cs)" + by (fold wq_def, simp) + from ih [OF this] show ?thesis by simp + next + case True + show ?thesis + proof(cases "wq_fun (schs s) cs'") + case Nil + with h V show ?thesis + apply (auto simp:wq_def Let_def split:if_splits) + by (fold wq_def, drule_tac ih, simp) + next + case (Cons a rest) + assume eq_wq: "wq_fun (schs s) cs' = a # rest" + with h V show ?thesis + apply (auto simp:Let_def wq_def split:if_splits) + proof - + assume th_in: "th \ set (SOME q. distinct q \ set q = set rest)" + have "set (SOME q. distinct q \ set q = set rest) = set rest" + proof(rule someI2) + from vt_s.wq_distinct[of cs'] and eq_wq[folded wq_def] + show "distinct rest \ set rest = set rest" by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" + by auto + qed + with eq_wq th_in have "th \ set (wq_fun (schs s) cs')" by auto + from ih[OF this[folded wq_def]] show "th \ threads s" . + next + assume th_in: "th \ set (wq_fun (schs s) cs)" + from ih[OF this[folded wq_def]] + show "th \ threads s" . + qed + qed + qed + qed + qed + next + case (P th' cs') + from h stp + show ?thesis + apply (unfold P wq_def) + apply (auto simp:Let_def split:if_splits, fold wq_def) + apply (auto intro:ih) + apply(ind_cases "step s (P th' cs')") + by (unfold runing_def readys_def, auto) + next + case (Set thread prio) + with ih h show ?thesis + by (auto simp:wq_def Let_def) + qed + next + case vt_nil + thus ?case by (auto simp:wq_def) + qed +qed + +lemma dm_RAG_threads: + assumes in_dom: "(Th th) \ Domain (RAG s)" + shows "th \ threads s" +proof - + from in_dom obtain n where "(Th th, n) \ RAG s" by auto + moreover from RAG_target_th[OF this] obtain cs where "n = Cs cs" by auto + ultimately have "(Th th, Cs cs) \ RAG s" by simp + hence "th \ set (wq s cs)" + by (unfold s_RAG_def, auto simp:cs_waiting_def) + from wq_threads [OF this] show ?thesis . +qed + + +lemma cp_le: + assumes th_in: "th \ threads s" + shows "cp s th \ Max ((\ th. (preced th s)) ` threads s)" +proof(unfold cp_eq_cpreced cpreced_def cs_dependants_def) + show "Max ((\th. preced th s) ` ({th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+})) + \ Max ((\th. preced th s) ` threads s)" + (is "Max (?f ` ?A) \ Max (?f ` ?B)") + proof(rule Max_f_mono) + show "{th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ {}" by simp + next + from finite_threads + show "finite (threads s)" . + next + from th_in + show "{th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ threads s" + apply (auto simp:Domain_def) + apply (rule_tac dm_RAG_threads) + apply (unfold trancl_domain [of "RAG s", symmetric]) + by (unfold cs_RAG_def s_RAG_def, auto simp:Domain_def) + qed +qed + +lemma le_cp: + shows "preced th s \ cp s th" +proof(unfold cp_eq_cpreced preced_def cpreced_def, simp) + show "Prc (priority th s) (last_set th s) + \ Max (insert (Prc (priority th s) (last_set th s)) + ((\th. Prc (priority th s) (last_set th s)) ` dependants (wq s) th))" + (is "?l \ Max (insert ?l ?A)") + proof(cases "?A = {}") + case False + have "finite ?A" (is "finite (?f ` ?B)") + proof - + have "finite ?B" + proof- + have "finite {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+}" + proof - + let ?F = "\ (x, y). the_th x" + have "{th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ ?F ` ((RAG (wq s))\<^sup>+)" + apply (auto simp:image_def) + by (rule_tac x = "(Th x, Th th)" in bexI, auto) + moreover have "finite \" + proof - + from finite_RAG have "finite (RAG s)" . + hence "finite ((RAG (wq s))\<^sup>+)" + apply (unfold finite_trancl) + by (auto simp: s_RAG_def cs_RAG_def wq_def) + thus ?thesis by auto + qed + ultimately show ?thesis by (auto intro:finite_subset) + qed + thus ?thesis by (simp add:cs_dependants_def) + qed + thus ?thesis by simp + qed + from Max_insert [OF this False, of ?l] show ?thesis by auto + next + case True + thus ?thesis by auto + qed +qed + +lemma max_cp_eq: + shows "Max ((cp s) ` threads s) = Max ((\ th. (preced th s)) ` threads s)" + (is "?l = ?r") +proof(cases "threads s = {}") + case True + thus ?thesis by auto +next + case False + have "?l \ ((cp s) ` threads s)" + proof(rule Max_in) + from finite_threads + show "finite (cp s ` threads s)" by auto + next + from False show "cp s ` threads s \ {}" by auto + qed + then obtain th + where th_in: "th \ threads s" and eq_l: "?l = cp s th" by auto + have "\ \ ?r" by (rule cp_le[OF th_in]) + moreover have "?r \ cp s th" (is "Max (?f ` ?A) \ cp s th") + proof - + have "?r \ (?f ` ?A)" + proof(rule Max_in) + from finite_threads + show " finite ((\th. preced th s) ` threads s)" by auto + next + from False show " (\th. preced th s) ` threads s \ {}" by auto + qed + then obtain th' where + th_in': "th' \ ?A " and eq_r: "?r = ?f th'" by auto + from le_cp [of th'] eq_r + have "?r \ cp s th'" by auto + moreover have "\ \ cp s th" + proof(fold eq_l) + show " cp s th' \ Max (cp s ` threads s)" + proof(rule Max_ge) + from th_in' show "cp s th' \ cp s ` threads s" + by auto + next + from finite_threads + show "finite (cp s ` threads s)" by auto + qed + qed + ultimately show ?thesis by auto + qed + ultimately show ?thesis using eq_l by auto +qed + +lemma max_cp_eq_the_preced: + shows "Max ((cp s) ` threads s) = Max (the_preced s ` threads s)" + using max_cp_eq using the_preced_def by presburger + +end + +lemma preced_v [simp]: "preced th' (V th cs#s) = preced th' s" + by (unfold preced_def, simp) + +lemma the_preced_v[simp]: "the_preced (V th cs#s) = the_preced s" +proof + fix th' + show "the_preced (V th cs # s) th' = the_preced s th'" + by (unfold the_preced_def preced_def, simp) +qed + +lemma step_RAG_v: +assumes vt: + "vt (V th cs#s)" +shows " + RAG (V th cs # s) = + RAG s - {(Cs cs, Th th)} - + {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'}" (is "?L = ?R") +proof - + interpret vt_v: valid_trace_v s "V th cs" + using assms step_back_vt by (unfold_locales, auto) + show ?thesis using vt_v.RAG_es . +qed + + + + + +text {* (* ddd *) + The following @{text "step_RAG_v"} lemma charaterizes how @{text "RAG"} is changed + with the happening of @{text "V"}-events: +*} +lemma step_RAG_v: +assumes vt: + "vt (V th cs#s)" +shows " + RAG (V th cs # s) = + RAG s - {(Cs cs, Th th)} - + {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'}" + apply (insert vt, unfold s_RAG_def) + apply (auto split:if_splits list.splits simp:Let_def) + apply (auto elim: step_v_waiting_mono step_v_hold_inv + step_v_release step_v_wait_inv + step_v_get_hold step_v_release_inv) + apply (erule_tac step_v_not_wait, auto) + done + +text {* + The following @{text "step_RAG_p"} lemma charaterizes how @{text "RAG"} is changed + with the happening of @{text "P"}-events: +*} +lemma step_RAG_p: + "vt (P th cs#s) \ + RAG (P th cs # s) = (if (wq s cs = []) then RAG s \ {(Cs cs, Th th)} + else RAG s \ {(Th th, Cs cs)})" + apply(simp only: s_RAG_def wq_def) + apply (auto split:list.splits prod.splits simp:Let_def wq_def cs_waiting_def cs_holding_def) + apply(case_tac "csa = cs", auto) + apply(fold wq_def) + apply(drule_tac step_back_step) + apply(ind_cases " step s (P (hd (wq s cs)) cs)") + apply(simp add:s_RAG_def wq_def cs_holding_def) + apply(auto) + done + + +lemma RAG_target_th: "(Th th, x) \ RAG (s::state) \ \ cs. x = Cs cs" + by (unfold s_RAG_def, auto) + +context valid_trace +begin + +text {* + The following lemma shows that @{text "RAG"} is acyclic. + The overall structure is by induction on the formation of @{text "vt s"} + and then case analysis on event @{text "e"}, where the non-trivial cases + for those for @{text "V"} and @{text "P"} events. +*} +lemma acyclic_RAG: + shows "acyclic (RAG s)" +using vt +proof(induct) + case (vt_cons s e) + interpret vt_s: valid_trace s using vt_cons(1) + by (unfold_locales, simp) + assume ih: "acyclic (RAG s)" + and stp: "step s e" + and vt: "vt s" + show ?case + proof(cases e) + case (Create th prio) + with ih + show ?thesis by (simp add:RAG_create_unchanged) + next + case (Exit th) + with ih show ?thesis by (simp add:RAG_exit_unchanged) + next + case (V th cs) + from V vt stp have vtt: "vt (V th cs#s)" by auto + from step_RAG_v [OF this] + have eq_de: + "RAG (e # s) = + RAG s - {(Cs cs, Th th)} - {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'}" + (is "?L = (?A - ?B - ?C) \ ?D") by (simp add:V) + from ih have ac: "acyclic (?A - ?B - ?C)" by (auto elim:acyclic_subset) + from step_back_step [OF vtt] + have "step s (V th cs)" . + thus ?thesis + proof(cases) + assume "holding s th cs" + hence th_in: "th \ set (wq s cs)" and + eq_hd: "th = hd (wq s cs)" unfolding s_holding_def wq_def by auto + then obtain rest where + eq_wq: "wq s cs = th#rest" + by (cases "wq s cs", auto) + show ?thesis + proof(cases "rest = []") + case False + let ?th' = "hd (SOME q. distinct q \ set q = set rest)" + from eq_wq False have eq_D: "?D = {(Cs cs, Th ?th')}" + by (unfold next_th_def, auto) + let ?E = "(?A - ?B - ?C)" + have "(Th ?th', Cs cs) \ ?E\<^sup>*" + proof + assume "(Th ?th', Cs cs) \ ?E\<^sup>*" + hence " (Th ?th', Cs cs) \ ?E\<^sup>+" by (simp add: rtrancl_eq_or_trancl) + from tranclD [OF this] + obtain x where th'_e: "(Th ?th', x) \ ?E" by blast + hence th_d: "(Th ?th', x) \ ?A" by simp + from RAG_target_th [OF this] + obtain cs' where eq_x: "x = Cs cs'" by auto + with th_d have "(Th ?th', Cs cs') \ ?A" by simp + hence wt_th': "waiting s ?th' cs'" + unfolding s_RAG_def s_waiting_def cs_waiting_def wq_def by simp + hence "cs' = cs" + proof(rule vt_s.waiting_unique) + from eq_wq vt_s.wq_distinct[of cs] + show "waiting s ?th' cs" + apply (unfold s_waiting_def wq_def, auto) + proof - + assume hd_in: "hd (SOME q. distinct q \ set q = set rest) \ set rest" + and eq_wq: "wq_fun (schs s) cs = th # rest" + have "(SOME q. distinct q \ set q = set rest) \ []" + proof(rule someI2) + from vt_s.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" unfolding wq_def by auto + next + fix x assume "distinct x \ set x = set rest" + with False show "x \ []" by auto + qed + hence "hd (SOME q. distinct q \ set q = set rest) \ + set (SOME q. distinct q \ set q = set rest)" by auto + moreover have "\ = set rest" + proof(rule someI2) + from vt_s.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" unfolding wq_def by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" by auto + qed + moreover note hd_in + ultimately show "hd (SOME q. distinct q \ set q = set rest) = th" by auto + next + assume hd_in: "hd (SOME q. distinct q \ set q = set rest) \ set rest" + and eq_wq: "wq s cs = hd (SOME q. distinct q \ set q = set rest) # rest" + have "(SOME q. distinct q \ set q = set rest) \ []" + proof(rule someI2) + from vt_s.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" by auto + next + fix x assume "distinct x \ set x = set rest" + with False show "x \ []" by auto + qed + hence "hd (SOME q. distinct q \ set q = set rest) \ + set (SOME q. distinct q \ set q = set rest)" by auto + moreover have "\ = set rest" + proof(rule someI2) + from vt_s.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" by auto + qed + moreover note hd_in + ultimately show False by auto + qed + qed + with th'_e eq_x have "(Th ?th', Cs cs) \ ?E" by simp + with False + show "False" by (auto simp: next_th_def eq_wq) + qed + with acyclic_insert[symmetric] and ac + and eq_de eq_D show ?thesis by auto + next + case True + with eq_wq + have eq_D: "?D = {}" + by (unfold next_th_def, auto) + with eq_de ac + show ?thesis by auto + qed + qed + next + case (P th cs) + from P vt stp have vtt: "vt (P th cs#s)" by auto + from step_RAG_p [OF this] P + have "RAG (e # s) = + (if wq s cs = [] then RAG s \ {(Cs cs, Th th)} else + RAG s \ {(Th th, Cs cs)})" (is "?L = ?R") + by simp + moreover have "acyclic ?R" + proof(cases "wq s cs = []") + case True + hence eq_r: "?R = RAG s \ {(Cs cs, Th th)}" by simp + have "(Th th, Cs cs) \ (RAG s)\<^sup>*" + proof + assume "(Th th, Cs cs) \ (RAG s)\<^sup>*" + hence "(Th th, Cs cs) \ (RAG s)\<^sup>+" by (simp add: rtrancl_eq_or_trancl) + from tranclD2 [OF this] + obtain x where "(x, Cs cs) \ RAG s" by auto + with True show False by (auto simp:s_RAG_def cs_waiting_def) + qed + with acyclic_insert ih eq_r show ?thesis by auto + next + case False + hence eq_r: "?R = RAG s \ {(Th th, Cs cs)}" by simp + have "(Cs cs, Th th) \ (RAG s)\<^sup>*" + proof + assume "(Cs cs, Th th) \ (RAG s)\<^sup>*" + hence "(Cs cs, Th th) \ (RAG s)\<^sup>+" by (simp add: rtrancl_eq_or_trancl) + moreover from step_back_step [OF vtt] have "step s (P th cs)" . + ultimately show False + proof - + show " \(Cs cs, Th th) \ (RAG s)\<^sup>+; step s (P th cs)\ \ False" + by (ind_cases "step s (P th cs)", simp) + qed + qed + with acyclic_insert ih eq_r show ?thesis by auto + qed + ultimately show ?thesis by simp + next + case (Set thread prio) + with ih + thm RAG_set_unchanged + show ?thesis by (simp add:RAG_set_unchanged) + qed + next + case vt_nil + show "acyclic (RAG ([]::state))" + by (auto simp: s_RAG_def cs_waiting_def + cs_holding_def wq_def acyclic_def) +qed + + +lemma finite_RAG: + shows "finite (RAG s)" +proof - + from vt show ?thesis + proof(induct) + case (vt_cons s e) + interpret vt_s: valid_trace s using vt_cons(1) + by (unfold_locales, simp) + assume ih: "finite (RAG s)" + and stp: "step s e" + and vt: "vt s" + show ?case + proof(cases e) + case (Create th prio) + with ih + show ?thesis by (simp add:RAG_create_unchanged) + next + case (Exit th) + with ih show ?thesis by (simp add:RAG_exit_unchanged) + next + case (V th cs) + from V vt stp have vtt: "vt (V th cs#s)" by auto + from step_RAG_v [OF this] + have eq_de: "RAG (e # s) = + RAG s - {(Cs cs, Th th)} - {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'} +" + (is "?L = (?A - ?B - ?C) \ ?D") by (simp add:V) + moreover from ih have ac: "finite (?A - ?B - ?C)" by simp + moreover have "finite ?D" + proof - + have "?D = {} \ (\ a. ?D = {a})" + by (unfold next_th_def, auto) + thus ?thesis + proof + assume h: "?D = {}" + show ?thesis by (unfold h, simp) + next + assume "\ a. ?D = {a}" + thus ?thesis + by (metis finite.simps) + qed + qed + ultimately show ?thesis by simp + next + case (P th cs) + from P vt stp have vtt: "vt (P th cs#s)" by auto + from step_RAG_p [OF this] P + have "RAG (e # s) = + (if wq s cs = [] then RAG s \ {(Cs cs, Th th)} else + RAG s \ {(Th th, Cs cs)})" (is "?L = ?R") + by simp + moreover have "finite ?R" + proof(cases "wq s cs = []") + case True + hence eq_r: "?R = RAG s \ {(Cs cs, Th th)}" by simp + with True and ih show ?thesis by auto + next + case False + hence "?R = RAG s \ {(Th th, Cs cs)}" by simp + with False and ih show ?thesis by auto + qed + ultimately show ?thesis by auto + next + case (Set thread prio) + with ih + show ?thesis by (simp add:RAG_set_unchanged) + qed + next + case vt_nil + show "finite (RAG ([]::state))" + by (auto simp: s_RAG_def cs_waiting_def + cs_holding_def wq_def acyclic_def) + qed +qed + +text {* Several useful lemmas *} + +lemma wf_dep_converse: + shows "wf ((RAG s)^-1)" +proof(rule finite_acyclic_wf_converse) + from finite_RAG + show "finite (RAG s)" . +next + from acyclic_RAG + show "acyclic (RAG s)" . +qed + +end + +lemma hd_np_in: "x \ set l \ hd l \ set l" + by (induct l, auto) + +lemma th_chasing: "(Th th, Cs cs) \ RAG (s::state) \ \ th'. (Cs cs, Th th') \ RAG s" + by (auto simp:s_RAG_def s_holding_def cs_holding_def cs_waiting_def wq_def dest:hd_np_in) + +context valid_trace +begin + +lemma wq_threads: + assumes h: "th \ set (wq s cs)" + shows "th \ threads s" +proof - + from vt and h show ?thesis + proof(induct arbitrary: th cs) + case (vt_cons s e) + interpret vt_s: valid_trace s + using vt_cons(1) by (unfold_locales, auto) + assume ih: "\th cs. th \ set (wq s cs) \ th \ threads s" + and stp: "step s e" + and vt: "vt s" + and h: "th \ set (wq (e # s) cs)" + show ?case + proof(cases e) + case (Create th' prio) + with ih h show ?thesis + by (auto simp:wq_def Let_def) + next + case (Exit th') + with stp ih h show ?thesis + apply (auto simp:wq_def Let_def) + apply (ind_cases "step s (Exit th')") + apply (auto simp:runing_def readys_def s_holding_def s_waiting_def holdents_def + s_RAG_def s_holding_def cs_holding_def) + done + next + case (V th' cs') + show ?thesis + proof(cases "cs' = cs") + case False + with h + show ?thesis + apply(unfold wq_def V, auto simp:Let_def V split:prod.splits, fold wq_def) + by (drule_tac ih, simp) + next + case True + from h + show ?thesis + proof(unfold V wq_def) + assume th_in: "th \ set (wq_fun (schs (V th' cs' # s)) cs)" (is "th \ set ?l") + show "th \ threads (V th' cs' # s)" + proof(cases "cs = cs'") + case False + hence "?l = wq_fun (schs s) cs" by (simp add:Let_def) + with th_in have " th \ set (wq s cs)" + by (fold wq_def, simp) + from ih [OF this] show ?thesis by simp + next + case True + show ?thesis + proof(cases "wq_fun (schs s) cs'") + case Nil + with h V show ?thesis + apply (auto simp:wq_def Let_def split:if_splits) + by (fold wq_def, drule_tac ih, simp) + next + case (Cons a rest) + assume eq_wq: "wq_fun (schs s) cs' = a # rest" + with h V show ?thesis + apply (auto simp:Let_def wq_def split:if_splits) + proof - + assume th_in: "th \ set (SOME q. distinct q \ set q = set rest)" + have "set (SOME q. distinct q \ set q = set rest) = set rest" + proof(rule someI2) + from vt_s.wq_distinct[of cs'] and eq_wq[folded wq_def] + show "distinct rest \ set rest = set rest" by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" + by auto + qed + with eq_wq th_in have "th \ set (wq_fun (schs s) cs')" by auto + from ih[OF this[folded wq_def]] show "th \ threads s" . + next + assume th_in: "th \ set (wq_fun (schs s) cs)" + from ih[OF this[folded wq_def]] + show "th \ threads s" . + qed + qed + qed + qed + qed + next + case (P th' cs') + from h stp + show ?thesis + apply (unfold P wq_def) + apply (auto simp:Let_def split:if_splits, fold wq_def) + apply (auto intro:ih) + apply(ind_cases "step s (P th' cs')") + by (unfold runing_def readys_def, auto) + next + case (Set thread prio) + with ih h show ?thesis + by (auto simp:wq_def Let_def) + qed + next + case vt_nil + thus ?case by (auto simp:wq_def) + qed +qed + +lemma range_in: "\(Th th) \ Range (RAG (s::state))\ \ th \ threads s" + apply(unfold s_RAG_def cs_waiting_def cs_holding_def) + by (auto intro:wq_threads) + +lemma readys_v_eq: + assumes neq_th: "th \ thread" + and eq_wq: "wq s cs = thread#rest" + and not_in: "th \ set rest" + shows "(th \ readys (V thread cs#s)) = (th \ readys s)" +proof - + from assms show ?thesis + apply (auto simp:readys_def) + apply(simp add:s_waiting_def[folded wq_def]) + apply (erule_tac x = csa in allE) + apply (simp add:s_waiting_def wq_def Let_def split:if_splits) + apply (case_tac "csa = cs", simp) + apply (erule_tac x = cs in allE) + apply(auto simp add: s_waiting_def[folded wq_def] Let_def split: list.splits) + apply(auto simp add: wq_def) + apply (auto simp:s_waiting_def wq_def Let_def split:list.splits) + proof - + assume th_nin: "th \ set rest" + and th_in: "th \ set (SOME q. distinct q \ set q = set rest)" + and eq_wq: "wq_fun (schs s) cs = thread # rest" + have "set (SOME q. distinct q \ set q = set rest) = set rest" + proof(rule someI2) + from wq_distinct[of cs, unfolded wq_def] and eq_wq[unfolded wq_def] + show "distinct rest \ set rest = set rest" by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" by auto + qed + with th_nin th_in show False by auto + qed +qed + +text {* \noindent + The following lemmas shows that: starting from any node in @{text "RAG"}, + by chasing out-going edges, it is always possible to reach a node representing a ready + thread. In this lemma, it is the @{text "th'"}. +*} + +lemma chain_building: + shows "node \ Domain (RAG s) \ (\ th'. th' \ readys s \ (node, Th th') \ (RAG s)^+)" +proof - + from wf_dep_converse + have h: "wf ((RAG s)\)" . + show ?thesis + proof(induct rule:wf_induct [OF h]) + fix x + assume ih [rule_format]: + "\y. (y, x) \ (RAG s)\ \ + y \ Domain (RAG s) \ (\th'. th' \ readys s \ (y, Th th') \ (RAG s)\<^sup>+)" + show "x \ Domain (RAG s) \ (\th'. th' \ readys s \ (x, Th th') \ (RAG s)\<^sup>+)" + proof + assume x_d: "x \ Domain (RAG s)" + show "\th'. th' \ readys s \ (x, Th th') \ (RAG s)\<^sup>+" + proof(cases x) + case (Th th) + from x_d Th obtain cs where x_in: "(Th th, Cs cs) \ RAG s" by (auto simp:s_RAG_def) + with Th have x_in_r: "(Cs cs, x) \ (RAG s)^-1" by simp + from th_chasing [OF x_in] obtain th' where "(Cs cs, Th th') \ RAG s" by blast + hence "Cs cs \ Domain (RAG s)" by auto + from ih [OF x_in_r this] obtain th' + where th'_ready: " th' \ readys s" and cs_in: "(Cs cs, Th th') \ (RAG s)\<^sup>+" by auto + have "(x, Th th') \ (RAG s)\<^sup>+" using Th x_in cs_in by auto + with th'_ready show ?thesis by auto + next + case (Cs cs) + from x_d Cs obtain th' where th'_d: "(Th th', x) \ (RAG s)^-1" by (auto simp:s_RAG_def) + show ?thesis + proof(cases "th' \ readys s") + case True + from True and th'_d show ?thesis by auto + next + case False + from th'_d and range_in have "th' \ threads s" by auto + with False have "Th th' \ Domain (RAG s)" + by (auto simp:readys_def wq_def s_waiting_def s_RAG_def cs_waiting_def Domain_def) + from ih [OF th'_d this] + obtain th'' where + th''_r: "th'' \ readys s" and + th''_in: "(Th th', Th th'') \ (RAG s)\<^sup>+" by auto + from th'_d and th''_in + have "(x, Th th'') \ (RAG s)\<^sup>+" by auto + with th''_r show ?thesis by auto + qed + qed + qed + qed +qed + +text {* \noindent + The following is just an instance of @{text "chain_building"}. +*} +lemma th_chain_to_ready: + assumes th_in: "th \ threads s" + shows "th \ readys s \ (\ th'. th' \ readys s \ (Th th, Th th') \ (RAG s)^+)" +proof(cases "th \ readys s") + case True + thus ?thesis by auto +next + case False + from False and th_in have "Th th \ Domain (RAG s)" + by (auto simp:readys_def s_waiting_def s_RAG_def wq_def cs_waiting_def Domain_def) + from chain_building [rule_format, OF this] + show ?thesis by auto +qed + +end + + + +lemma holding_unique: "\holding (s::state) th1 cs; holding s th2 cs\ \ th1 = th2" + by (unfold s_holding_def cs_holding_def, auto) + +context valid_trace +begin + +lemma unique_RAG: "\(n, n1) \ RAG s; (n, n2) \ RAG s\ \ n1 = n2" + apply(unfold s_RAG_def, auto, fold waiting_eq holding_eq) + by(auto elim:waiting_unique holding_unique) + +end + + +lemma trancl_split: "(a, b) \ r^+ \ \ c. (a, c) \ r" +by (induct rule:trancl_induct, auto) + +context valid_trace +begin + +lemma dchain_unique: + assumes th1_d: "(n, Th th1) \ (RAG s)^+" + and th1_r: "th1 \ readys s" + and th2_d: "(n, Th th2) \ (RAG s)^+" + and th2_r: "th2 \ readys s" + shows "th1 = th2" +proof - + { assume neq: "th1 \ th2" + hence "Th th1 \ Th th2" by simp + from unique_chain [OF _ th1_d th2_d this] and unique_RAG + have "(Th th1, Th th2) \ (RAG s)\<^sup>+ \ (Th th2, Th th1) \ (RAG s)\<^sup>+" by auto + hence "False" + proof + assume "(Th th1, Th th2) \ (RAG s)\<^sup>+" + from trancl_split [OF this] + obtain n where dd: "(Th th1, n) \ RAG s" by auto + then obtain cs where eq_n: "n = Cs cs" + by (auto simp:s_RAG_def s_holding_def cs_holding_def cs_waiting_def wq_def dest:hd_np_in) + from dd eq_n have "th1 \ readys s" + by (auto simp:readys_def s_RAG_def wq_def s_waiting_def cs_waiting_def) + with th1_r show ?thesis by auto + next + assume "(Th th2, Th th1) \ (RAG s)\<^sup>+" + from trancl_split [OF this] + obtain n where dd: "(Th th2, n) \ RAG s" by auto + then obtain cs where eq_n: "n = Cs cs" + by (auto simp:s_RAG_def s_holding_def cs_holding_def cs_waiting_def wq_def dest:hd_np_in) + from dd eq_n have "th2 \ readys s" + by (auto simp:readys_def wq_def s_RAG_def s_waiting_def cs_waiting_def) + with th2_r show ?thesis by auto + qed + } thus ?thesis by auto +qed + +end + + +lemma step_holdents_p_add: + assumes vt: "vt (P th cs#s)" + and "wq s cs = []" + shows "holdents (P th cs#s) th = holdents s th \ {cs}" +proof - + from assms show ?thesis + unfolding holdents_test step_RAG_p[OF vt] by (auto) +qed + +lemma step_holdents_p_eq: + assumes vt: "vt (P th cs#s)" + and "wq s cs \ []" + shows "holdents (P th cs#s) th = holdents s th" +proof - + from assms show ?thesis + unfolding holdents_test step_RAG_p[OF vt] by auto +qed + + +lemma (in valid_trace) finite_holding : + shows "finite (holdents s th)" +proof - + let ?F = "\ (x, y). the_cs x" + from finite_RAG + have "finite (RAG s)" . + hence "finite (?F `(RAG s))" by simp + moreover have "{cs . (Cs cs, Th th) \ RAG s} \ \" + proof - + { have h: "\ a A f. a \ A \ f a \ f ` A" by auto + fix x assume "(Cs x, Th th) \ RAG s" + hence "?F (Cs x, Th th) \ ?F `(RAG s)" by (rule h) + moreover have "?F (Cs x, Th th) = x" by simp + ultimately have "x \ (\(x, y). the_cs x) ` RAG s" by simp + } thus ?thesis by auto + qed + ultimately show ?thesis by (unfold holdents_test, auto intro:finite_subset) +qed + +lemma cntCS_v_dec: + assumes vtv: "vt (V thread cs#s)" + shows "(cntCS (V thread cs#s) thread + 1) = cntCS s thread" +proof - + from vtv interpret vt_s: valid_trace s + by (cases, unfold_locales, simp) + from vtv interpret vt_v: valid_trace "V thread cs#s" + by (unfold_locales, simp) + from step_back_step[OF vtv] + have cs_in: "cs \ holdents s thread" + apply (cases, unfold holdents_test s_RAG_def, simp) + by (unfold cs_holding_def s_holding_def wq_def, auto) + moreover have cs_not_in: + "(holdents (V thread cs#s) thread) = holdents s thread - {cs}" + apply (insert vt_s.wq_distinct[of cs]) + apply (unfold holdents_test, unfold step_RAG_v[OF vtv], + auto simp:next_th_def) + proof - + fix rest + assume dst: "distinct (rest::thread list)" + and ne: "rest \ []" + and hd_ni: "hd (SOME q. distinct q \ set q = set rest) \ set rest" + moreover have "set (SOME q. distinct q \ set q = set rest) = set rest" + proof(rule someI2) + from dst show "distinct rest \ set rest = set rest" by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" by auto + qed + ultimately have "hd (SOME q. distinct q \ set q = set rest) \ + set (SOME q. distinct q \ set q = set rest)" by simp + moreover have "(SOME q. distinct q \ set q = set rest) \ []" + proof(rule someI2) + from dst show "distinct rest \ set rest = set rest" by auto + next + fix x assume " distinct x \ set x = set rest" with ne + show "x \ []" by auto + qed + ultimately + show "(Cs cs, Th (hd (SOME q. distinct q \ set q = set rest))) \ RAG s" + by auto + next + fix rest + assume dst: "distinct (rest::thread list)" + and ne: "rest \ []" + and hd_ni: "hd (SOME q. distinct q \ set q = set rest) \ set rest" + moreover have "set (SOME q. distinct q \ set q = set rest) = set rest" + proof(rule someI2) + from dst show "distinct rest \ set rest = set rest" by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" by auto + qed + ultimately have "hd (SOME q. distinct q \ set q = set rest) \ + set (SOME q. distinct q \ set q = set rest)" by simp + moreover have "(SOME q. distinct q \ set q = set rest) \ []" + proof(rule someI2) + from dst show "distinct rest \ set rest = set rest" by auto + next + fix x assume " distinct x \ set x = set rest" with ne + show "x \ []" by auto + qed + ultimately show "False" by auto + qed + ultimately + have "holdents s thread = insert cs (holdents (V thread cs#s) thread)" + by auto + moreover have "card \ = + Suc (card ((holdents (V thread cs#s) thread) - {cs}))" + proof(rule card_insert) + from vt_v.finite_holding + show " finite (holdents (V thread cs # s) thread)" . + qed + moreover from cs_not_in + have "cs \ (holdents (V thread cs#s) thread)" by auto + ultimately show ?thesis by (simp add:cntCS_def) +qed + +lemma count_rec1 [simp]: + assumes "Q e" + shows "count Q (e#es) = Suc (count Q es)" + using assms + by (unfold count_def, auto) + +lemma count_rec2 [simp]: + assumes "\Q e" + shows "count Q (e#es) = (count Q es)" + using assms + by (unfold count_def, auto) + +lemma count_rec3 [simp]: + shows "count Q [] = 0" + by (unfold count_def, auto) + +lemma cntP_diff_inv: + assumes "cntP (e#s) th \ cntP s th" + shows "isP e \ actor e = th" +proof(cases e) + case (P th' pty) + show ?thesis + by (cases "(\e. \cs. e = P th cs) (P th' pty)", + insert assms P, auto simp:cntP_def) +qed (insert assms, auto simp:cntP_def) + +lemma cntV_diff_inv: + assumes "cntV (e#s) th \ cntV s th" + shows "isV e \ actor e = th" +proof(cases e) + case (V th' pty) + show ?thesis + by (cases "(\e. \cs. e = V th cs) (V th' pty)", + insert assms V, auto simp:cntV_def) +qed (insert assms, auto simp:cntV_def) + +context valid_trace +begin + +text {* (* ddd *) \noindent + The relationship between @{text "cntP"}, @{text "cntV"} and @{text "cntCS"} + of one particular thread. +*} + +lemma cnp_cnv_cncs: + shows "cntP s th = cntV s th + (if (th \ readys s \ th \ threads s) + then cntCS s th else cntCS s th + 1)" +proof - + from vt show ?thesis + proof(induct arbitrary:th) + case (vt_cons s e) + interpret vt_s: valid_trace s using vt_cons(1) by (unfold_locales, simp) + assume vt: "vt s" + and ih: "\th. cntP s th = cntV s th + + (if (th \ readys s \ th \ threads s) then cntCS s th else cntCS s th + 1)" + and stp: "step s e" + from stp show ?case + proof(cases) + case (thread_create thread prio) + assume eq_e: "e = Create thread prio" + and not_in: "thread \ threads s" + show ?thesis + proof - + { fix cs + assume "thread \ set (wq s cs)" + from vt_s.wq_threads [OF this] have "thread \ threads s" . + with not_in have "False" by simp + } with eq_e have eq_readys: "readys (e#s) = readys s \ {thread}" + by (auto simp:readys_def threads.simps s_waiting_def + wq_def cs_waiting_def Let_def) + from eq_e have eq_cnp: "cntP (e#s) th = cntP s th" by (simp add:cntP_def count_def) + from eq_e have eq_cnv: "cntV (e#s) th = cntV s th" by (simp add:cntV_def count_def) + have eq_cncs: "cntCS (e#s) th = cntCS s th" + unfolding cntCS_def holdents_test + by (simp add:RAG_create_unchanged eq_e) + { assume "th \ thread" + with eq_readys eq_e + have "(th \ readys (e # s) \ th \ threads (e # s)) = + (th \ readys (s) \ th \ threads (s))" + by (simp add:threads.simps) + with eq_cnp eq_cnv eq_cncs ih not_in + have ?thesis by simp + } moreover { + assume eq_th: "th = thread" + with not_in ih have " cntP s th = cntV s th + cntCS s th" by simp + moreover from eq_th and eq_readys have "th \ readys (e#s)" by simp + moreover note eq_cnp eq_cnv eq_cncs + ultimately have ?thesis by auto + } ultimately show ?thesis by blast + qed + next + case (thread_exit thread) + assume eq_e: "e = Exit thread" + and is_runing: "thread \ runing s" + and no_hold: "holdents s thread = {}" + from eq_e have eq_cnp: "cntP (e#s) th = cntP s th" by (simp add:cntP_def count_def) + from eq_e have eq_cnv: "cntV (e#s) th = cntV s th" by (simp add:cntV_def count_def) + have eq_cncs: "cntCS (e#s) th = cntCS s th" + unfolding cntCS_def holdents_test + by (simp add:RAG_exit_unchanged eq_e) + { assume "th \ thread" + with eq_e + have "(th \ readys (e # s) \ th \ threads (e # s)) = + (th \ readys (s) \ th \ threads (s))" + apply (simp add:threads.simps readys_def) + apply (subst s_waiting_def) + apply (simp add:Let_def) + apply (subst s_waiting_def, simp) + done + with eq_cnp eq_cnv eq_cncs ih + have ?thesis by simp + } moreover { + assume eq_th: "th = thread" + with ih is_runing have " cntP s th = cntV s th + cntCS s th" + by (simp add:runing_def) + moreover from eq_th eq_e have "th \ threads (e#s)" + by simp + moreover note eq_cnp eq_cnv eq_cncs + ultimately have ?thesis by auto + } ultimately show ?thesis by blast + next + case (thread_P thread cs) + assume eq_e: "e = P thread cs" + and is_runing: "thread \ runing s" + and no_dep: "(Cs cs, Th thread) \ (RAG s)\<^sup>+" + from thread_P vt stp ih have vtp: "vt (P thread cs#s)" by auto + then interpret vt_p: valid_trace "(P thread cs#s)" + by (unfold_locales, simp) + show ?thesis + proof - + { have hh: "\ A B C. (B = C) \ (A \ B) = (A \ C)" by blast + assume neq_th: "th \ thread" + with eq_e + have eq_readys: "(th \ readys (e#s)) = (th \ readys (s))" + apply (simp add:readys_def s_waiting_def wq_def Let_def) + apply (rule_tac hh) + apply (intro iffI allI, clarify) + apply (erule_tac x = csa in allE, auto) + apply (subgoal_tac "wq_fun (schs s) cs \ []", auto) + apply (erule_tac x = cs in allE, auto) + by (case_tac "(wq_fun (schs s) cs)", auto) + moreover from neq_th eq_e have "cntCS (e # s) th = cntCS s th" + apply (simp add:cntCS_def holdents_test) + by (unfold step_RAG_p [OF vtp], auto) + moreover from eq_e neq_th have "cntP (e # s) th = cntP s th" + by (simp add:cntP_def count_def) + moreover from eq_e neq_th have "cntV (e#s) th = cntV s th" + by (simp add:cntV_def count_def) + moreover from eq_e neq_th have "threads (e#s) = threads s" by simp + moreover note ih [of th] + ultimately have ?thesis by simp + } moreover { + assume eq_th: "th = thread" + have ?thesis + proof - + from eq_e eq_th have eq_cnp: "cntP (e # s) th = 1 + (cntP s th)" + by (simp add:cntP_def count_def) + from eq_e eq_th have eq_cnv: "cntV (e#s) th = cntV s th" + by (simp add:cntV_def count_def) + show ?thesis + proof (cases "wq s cs = []") + case True + with is_runing + have "th \ readys (e#s)" + apply (unfold eq_e wq_def, unfold readys_def s_RAG_def) + apply (simp add: wq_def[symmetric] runing_def eq_th s_waiting_def) + by (auto simp:readys_def wq_def Let_def s_waiting_def wq_def) + moreover have "cntCS (e # s) th = 1 + cntCS s th" + proof - + have "card {csa. csa = cs \ (Cs csa, Th thread) \ RAG s} = + Suc (card {cs. (Cs cs, Th thread) \ RAG s})" (is "card ?L = Suc (card ?R)") + proof - + have "?L = insert cs ?R" by auto + moreover have "card \ = Suc (card (?R - {cs}))" + proof(rule card_insert) + from vt_s.finite_holding [of thread] + show " finite {cs. (Cs cs, Th thread) \ RAG s}" + by (unfold holdents_test, simp) + qed + moreover have "?R - {cs} = ?R" + proof - + have "cs \ ?R" + proof + assume "cs \ {cs. (Cs cs, Th thread) \ RAG s}" + with no_dep show False by auto + qed + thus ?thesis by auto + qed + ultimately show ?thesis by auto + qed + thus ?thesis + apply (unfold eq_e eq_th cntCS_def) + apply (simp add: holdents_test) + by (unfold step_RAG_p [OF vtp], auto simp:True) + qed + moreover from is_runing have "th \ readys s" + by (simp add:runing_def eq_th) + moreover note eq_cnp eq_cnv ih [of th] + ultimately show ?thesis by auto + next + case False + have eq_wq: "wq (e#s) cs = wq s cs @ [th]" + by (unfold eq_th eq_e wq_def, auto simp:Let_def) + have "th \ readys (e#s)" + proof + assume "th \ readys (e#s)" + hence "\cs. \ waiting (e # s) th cs" by (simp add:readys_def) + from this[rule_format, of cs] have " \ waiting (e # s) th cs" . + hence "th \ set (wq (e#s) cs) \ th = hd (wq (e#s) cs)" + by (simp add:s_waiting_def wq_def) + moreover from eq_wq have "th \ set (wq (e#s) cs)" by auto + ultimately have "th = hd (wq (e#s) cs)" by blast + with eq_wq have "th = hd (wq s cs @ [th])" by simp + hence "th = hd (wq s cs)" using False by auto + with False eq_wq vt_p.wq_distinct [of cs] + show False by (fold eq_e, auto) + qed + moreover from is_runing have "th \ threads (e#s)" + by (unfold eq_e, auto simp:runing_def readys_def eq_th) + moreover have "cntCS (e # s) th = cntCS s th" + apply (unfold cntCS_def holdents_test eq_e step_RAG_p[OF vtp]) + by (auto simp:False) + moreover note eq_cnp eq_cnv ih[of th] + moreover from is_runing have "th \ readys s" + by (simp add:runing_def eq_th) + ultimately show ?thesis by auto + qed + qed + } ultimately show ?thesis by blast + qed + next + case (thread_V thread cs) + from assms vt stp ih thread_V have vtv: "vt (V thread cs # s)" by auto + then interpret vt_v: valid_trace "(V thread cs # s)" by (unfold_locales, simp) + assume eq_e: "e = V thread cs" + and is_runing: "thread \ runing s" + and hold: "holding s thread cs" + from hold obtain rest + where eq_wq: "wq s cs = thread # rest" + by (case_tac "wq s cs", auto simp: wq_def s_holding_def) + have eq_threads: "threads (e#s) = threads s" by (simp add: eq_e) + have eq_set: "set (SOME q. distinct q \ set q = set rest) = set rest" + proof(rule someI2) + from vt_v.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" + by (metis distinct.simps(2) vt_s.wq_distinct) + next + show "\x. distinct x \ set x = set rest \ set x = set rest" + by auto + qed + show ?thesis + proof - + { assume eq_th: "th = thread" + from eq_th have eq_cnp: "cntP (e # s) th = cntP s th" + by (unfold eq_e, simp add:cntP_def count_def) + moreover from eq_th have eq_cnv: "cntV (e#s) th = 1 + cntV s th" + by (unfold eq_e, simp add:cntV_def count_def) + moreover from cntCS_v_dec [OF vtv] + have "cntCS (e # s) thread + 1 = cntCS s thread" + by (simp add:eq_e) + moreover from is_runing have rd_before: "thread \ readys s" + by (unfold runing_def, simp) + moreover have "thread \ readys (e # s)" + proof - + from is_runing + have "thread \ threads (e#s)" + by (unfold eq_e, auto simp:runing_def readys_def) + moreover have "\ cs1. \ waiting (e#s) thread cs1" + proof + fix cs1 + { assume eq_cs: "cs1 = cs" + have "\ waiting (e # s) thread cs1" + proof - + from eq_wq + have "thread \ set (wq (e#s) cs1)" + apply(unfold eq_e wq_def eq_cs s_holding_def) + apply (auto simp:Let_def) + proof - + assume "thread \ set (SOME q. distinct q \ set q = set rest)" + with eq_set have "thread \ set rest" by simp + with vt_v.wq_distinct[of cs] + and eq_wq show False + by (metis distinct.simps(2) vt_s.wq_distinct) + qed + thus ?thesis by (simp add:wq_def s_waiting_def) + qed + } moreover { + assume neq_cs: "cs1 \ cs" + have "\ waiting (e # s) thread cs1" + proof - + from wq_v_neq [OF neq_cs[symmetric]] + have "wq (V thread cs # s) cs1 = wq s cs1" . + moreover have "\ waiting s thread cs1" + proof - + from runing_ready and is_runing + have "thread \ readys s" by auto + thus ?thesis by (simp add:readys_def) + qed + ultimately show ?thesis + by (auto simp:wq_def s_waiting_def eq_e) + qed + } ultimately show "\ waiting (e # s) thread cs1" by blast + qed + ultimately show ?thesis by (simp add:readys_def) + qed + moreover note eq_th ih + ultimately have ?thesis by auto + } moreover { + assume neq_th: "th \ thread" + from neq_th eq_e have eq_cnp: "cntP (e # s) th = cntP s th" + by (simp add:cntP_def count_def) + from neq_th eq_e have eq_cnv: "cntV (e # s) th = cntV s th" + by (simp add:cntV_def count_def) + have ?thesis + proof(cases "th \ set rest") + case False + have "(th \ readys (e # s)) = (th \ readys s)" + apply (insert step_back_vt[OF vtv]) + by (simp add: False eq_e eq_wq neq_th vt_s.readys_v_eq) + moreover have "cntCS (e#s) th = cntCS s th" + apply (insert neq_th, unfold eq_e cntCS_def holdents_test step_RAG_v[OF vtv], auto) + proof - + have "{csa. (Cs csa, Th th) \ RAG s \ csa = cs \ next_th s thread cs th} = + {cs. (Cs cs, Th th) \ RAG s}" + proof - + from False eq_wq + have " next_th s thread cs th \ (Cs cs, Th th) \ RAG s" + apply (unfold next_th_def, auto) + proof - + assume ne: "rest \ []" + and ni: "hd (SOME q. distinct q \ set q = set rest) \ set rest" + and eq_wq: "wq s cs = thread # rest" + from eq_set ni have "hd (SOME q. distinct q \ set q = set rest) \ + set (SOME q. distinct q \ set q = set rest) + " by simp + moreover have "(SOME q. distinct q \ set q = set rest) \ []" + proof(rule someI2) + from vt_s.wq_distinct[ of cs] and eq_wq + show "distinct rest \ set rest = set rest" by auto + next + fix x assume "distinct x \ set x = set rest" + with ne show "x \ []" by auto + qed + ultimately show + "(Cs cs, Th (hd (SOME q. distinct q \ set q = set rest))) \ RAG s" + by auto + qed + thus ?thesis by auto + qed + thus "card {csa. (Cs csa, Th th) \ RAG s \ csa = cs \ next_th s thread cs th} = + card {cs. (Cs cs, Th th) \ RAG s}" by simp + qed + moreover note ih eq_cnp eq_cnv eq_threads + ultimately show ?thesis by auto + next + case True + assume th_in: "th \ set rest" + show ?thesis + proof(cases "next_th s thread cs th") + case False + with eq_wq and th_in have + neq_hd: "th \ hd (SOME q. distinct q \ set q = set rest)" (is "th \ hd ?rest") + by (auto simp:next_th_def) + have "(th \ readys (e # s)) = (th \ readys s)" + proof - + from eq_wq and th_in + have "\ th \ readys s" + apply (auto simp:readys_def s_waiting_def) + apply (rule_tac x = cs in exI, auto) + by (insert vt_s.wq_distinct[of cs], auto simp add: wq_def) + moreover + from eq_wq and th_in and neq_hd + have "\ (th \ readys (e # s))" + apply (auto simp:readys_def s_waiting_def eq_e wq_def Let_def split:list.splits) + by (rule_tac x = cs in exI, auto simp:eq_set) + ultimately show ?thesis by auto + qed + moreover have "cntCS (e#s) th = cntCS s th" + proof - + from eq_wq and th_in and neq_hd + have "(holdents (e # s) th) = (holdents s th)" + apply (unfold eq_e step_RAG_v[OF vtv], + auto simp:next_th_def eq_set s_RAG_def holdents_test wq_def + Let_def cs_holding_def) + by (insert vt_s.wq_distinct[of cs], auto simp:wq_def) + thus ?thesis by (simp add:cntCS_def) + qed + moreover note ih eq_cnp eq_cnv eq_threads + ultimately show ?thesis by auto + next + case True + let ?rest = " (SOME q. distinct q \ set q = set rest)" + let ?t = "hd ?rest" + from True eq_wq th_in neq_th + have "th \ readys (e # s)" + apply (auto simp:eq_e readys_def s_waiting_def wq_def + Let_def next_th_def) + proof - + assume eq_wq: "wq_fun (schs s) cs = thread # rest" + and t_in: "?t \ set rest" + show "?t \ threads s" + proof(rule vt_s.wq_threads) + from eq_wq and t_in + show "?t \ set (wq s cs)" by (auto simp:wq_def) + qed + next + fix csa + assume eq_wq: "wq_fun (schs s) cs = thread # rest" + and t_in: "?t \ set rest" + and neq_cs: "csa \ cs" + and t_in': "?t \ set (wq_fun (schs s) csa)" + show "?t = hd (wq_fun (schs s) csa)" + proof - + { assume neq_hd': "?t \ hd (wq_fun (schs s) csa)" + from vt_s.wq_distinct[of cs] and + eq_wq[folded wq_def] and t_in eq_wq + have "?t \ thread" by auto + with eq_wq and t_in + have w1: "waiting s ?t cs" + by (auto simp:s_waiting_def wq_def) + from t_in' neq_hd' + have w2: "waiting s ?t csa" + by (auto simp:s_waiting_def wq_def) + from vt_s.waiting_unique[OF w1 w2] + and neq_cs have "False" by auto + } thus ?thesis by auto + qed + qed + moreover have "cntP s th = cntV s th + cntCS s th + 1" + proof - + have "th \ readys s" + proof - + from True eq_wq neq_th th_in + show ?thesis + apply (unfold readys_def s_waiting_def, auto) + by (rule_tac x = cs in exI, auto simp add: wq_def) + qed + moreover have "th \ threads s" + proof - + from th_in eq_wq + have "th \ set (wq s cs)" by simp + from vt_s.wq_threads [OF this] + show ?thesis . + qed + ultimately show ?thesis using ih by auto + qed + moreover from True neq_th have "cntCS (e # s) th = 1 + cntCS s th" + apply (unfold cntCS_def holdents_test eq_e step_RAG_v[OF vtv], auto) + proof - + show "card {csa. (Cs csa, Th th) \ RAG s \ csa = cs} = + Suc (card {cs. (Cs cs, Th th) \ RAG s})" + (is "card ?A = Suc (card ?B)") + proof - + have "?A = insert cs ?B" by auto + hence "card ?A = card (insert cs ?B)" by simp + also have "\ = Suc (card ?B)" + proof(rule card_insert_disjoint) + have "?B \ ((\ (x, y). the_cs x) ` RAG s)" + apply (auto simp:image_def) + by (rule_tac x = "(Cs x, Th th)" in bexI, auto) + with vt_s.finite_RAG + show "finite {cs. (Cs cs, Th th) \ RAG s}" by (auto intro:finite_subset) + next + show "cs \ {cs. (Cs cs, Th th) \ RAG s}" + proof + assume "cs \ {cs. (Cs cs, Th th) \ RAG s}" + hence "(Cs cs, Th th) \ RAG s" by simp + with True neq_th eq_wq show False + by (auto simp:next_th_def s_RAG_def cs_holding_def) + qed + qed + finally show ?thesis . + qed + qed + moreover note eq_cnp eq_cnv + ultimately show ?thesis by simp + qed + qed + } ultimately show ?thesis by blast + qed + next + case (thread_set thread prio) + assume eq_e: "e = Set thread prio" + and is_runing: "thread \ runing s" + show ?thesis + proof - + from eq_e have eq_cnp: "cntP (e#s) th = cntP s th" by (simp add:cntP_def count_def) + from eq_e have eq_cnv: "cntV (e#s) th = cntV s th" by (simp add:cntV_def count_def) + have eq_cncs: "cntCS (e#s) th = cntCS s th" + unfolding cntCS_def holdents_test + by (simp add:RAG_set_unchanged eq_e) + from eq_e have eq_readys: "readys (e#s) = readys s" + by (simp add:readys_def cs_waiting_def s_waiting_def wq_def, + auto simp:Let_def) + { assume "th \ thread" + with eq_readys eq_e + have "(th \ readys (e # s) \ th \ threads (e # s)) = + (th \ readys (s) \ th \ threads (s))" + by (simp add:threads.simps) + with eq_cnp eq_cnv eq_cncs ih is_runing + have ?thesis by simp + } moreover { + assume eq_th: "th = thread" + with is_runing ih have " cntP s th = cntV s th + cntCS s th" + by (unfold runing_def, auto) + moreover from eq_th and eq_readys is_runing have "th \ readys (e#s)" + by (simp add:runing_def) + moreover note eq_cnp eq_cnv eq_cncs + ultimately have ?thesis by auto + } ultimately show ?thesis by blast + qed + qed + next + case vt_nil + show ?case + by (unfold cntP_def cntV_def cntCS_def, + auto simp:count_def holdents_test s_RAG_def wq_def cs_holding_def) + qed +qed + +lemma not_thread_cncs: + assumes not_in: "th \ threads s" + shows "cntCS s th = 0" +proof - + from vt not_in show ?thesis + proof(induct arbitrary:th) + case (vt_cons s e th) + interpret vt_s: valid_trace s using vt_cons(1) + by (unfold_locales, simp) + assume vt: "vt s" + and ih: "\th. th \ threads s \ cntCS s th = 0" + and stp: "step s e" + and not_in: "th \ threads (e # s)" + from stp show ?case + proof(cases) + case (thread_create thread prio) + assume eq_e: "e = Create thread prio" + and not_in': "thread \ threads s" + have "cntCS (e # s) th = cntCS s th" + apply (unfold eq_e cntCS_def holdents_test) + by (simp add:RAG_create_unchanged) + moreover have "th \ threads s" + proof - + from not_in eq_e show ?thesis by simp + qed + moreover note ih ultimately show ?thesis by auto + next + case (thread_exit thread) + assume eq_e: "e = Exit thread" + and nh: "holdents s thread = {}" + have eq_cns: "cntCS (e # s) th = cntCS s th" + apply (unfold eq_e cntCS_def holdents_test) + by (simp add:RAG_exit_unchanged) + show ?thesis + proof(cases "th = thread") + case True + have "cntCS s th = 0" by (unfold cntCS_def, auto simp:nh True) + with eq_cns show ?thesis by simp + next + case False + with not_in and eq_e + have "th \ threads s" by simp + from ih[OF this] and eq_cns show ?thesis by simp + qed + next + case (thread_P thread cs) + assume eq_e: "e = P thread cs" + and is_runing: "thread \ runing s" + from assms thread_P ih vt stp thread_P have vtp: "vt (P thread cs#s)" by auto + have neq_th: "th \ thread" + proof - + from not_in eq_e have "th \ threads s" by simp + moreover from is_runing have "thread \ threads s" + by (simp add:runing_def readys_def) + ultimately show ?thesis by auto + qed + hence "cntCS (e # s) th = cntCS s th " + apply (unfold cntCS_def holdents_test eq_e) + by (unfold step_RAG_p[OF vtp], auto) + moreover have "cntCS s th = 0" + proof(rule ih) + from not_in eq_e show "th \ threads s" by simp + qed + ultimately show ?thesis by simp + next + case (thread_V thread cs) + assume eq_e: "e = V thread cs" + and is_runing: "thread \ runing s" + and hold: "holding s thread cs" + have neq_th: "th \ thread" + proof - + from not_in eq_e have "th \ threads s" by simp + moreover from is_runing have "thread \ threads s" + by (simp add:runing_def readys_def) + ultimately show ?thesis by auto + qed + from assms thread_V vt stp ih + have vtv: "vt (V thread cs#s)" by auto + then interpret vt_v: valid_trace "(V thread cs#s)" + by (unfold_locales, simp) + from hold obtain rest + where eq_wq: "wq s cs = thread # rest" + by (case_tac "wq s cs", auto simp: wq_def s_holding_def) + from not_in eq_e eq_wq + have "\ next_th s thread cs th" + apply (auto simp:next_th_def) + proof - + assume ne: "rest \ []" + and ni: "hd (SOME q. distinct q \ set q = set rest) \ threads s" (is "?t \ threads s") + have "?t \ set rest" + proof(rule someI2) + from vt_v.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" + by (metis distinct.simps(2) vt_s.wq_distinct) + next + fix x assume "distinct x \ set x = set rest" with ne + show "hd x \ set rest" by (cases x, auto) + qed + with eq_wq have "?t \ set (wq s cs)" by simp + from vt_s.wq_threads[OF this] and ni + show False + using `hd (SOME q. distinct q \ set q = set rest) \ set (wq s cs)` + ni vt_s.wq_threads by blast + qed + moreover note neq_th eq_wq + ultimately have "cntCS (e # s) th = cntCS s th" + by (unfold eq_e cntCS_def holdents_test step_RAG_v[OF vtv], auto) + moreover have "cntCS s th = 0" + proof(rule ih) + from not_in eq_e show "th \ threads s" by simp + qed + ultimately show ?thesis by simp + next + case (thread_set thread prio) + print_facts + assume eq_e: "e = Set thread prio" + and is_runing: "thread \ runing s" + from not_in and eq_e have "th \ threads s" by auto + from ih [OF this] and eq_e + show ?thesis + apply (unfold eq_e cntCS_def holdents_test) + by (simp add:RAG_set_unchanged) + qed + next + case vt_nil + show ?case + by (unfold cntCS_def, + auto simp:count_def holdents_test s_RAG_def wq_def cs_holding_def) + qed +qed + +end + + +context valid_trace +begin + +lemma dm_RAG_threads: + assumes in_dom: "(Th th) \ Domain (RAG s)" + shows "th \ threads s" +proof - + from in_dom obtain n where "(Th th, n) \ RAG s" by auto + moreover from RAG_target_th[OF this] obtain cs where "n = Cs cs" by auto + ultimately have "(Th th, Cs cs) \ RAG s" by simp + hence "th \ set (wq s cs)" + by (unfold s_RAG_def, auto simp:cs_waiting_def) + from wq_threads [OF this] show ?thesis . +qed + +end + +lemma cp_eq_cpreced: "cp s th = cpreced (wq s) s th" +unfolding cp_def wq_def +apply(induct s rule: schs.induct) +thm cpreced_initial +apply(simp add: Let_def cpreced_initial) +apply(simp add: Let_def) +apply(simp add: Let_def) +apply(simp add: Let_def) +apply(subst (2) schs.simps) +apply(simp add: Let_def) +apply(subst (2) schs.simps) +apply(simp add: Let_def) +done + +context valid_trace +begin + +lemma runing_unique: + assumes runing_1: "th1 \ runing s" + and runing_2: "th2 \ runing s" + shows "th1 = th2" +proof - + from runing_1 and runing_2 have "cp s th1 = cp s th2" + unfolding runing_def + apply(simp) + done + hence eq_max: "Max ((\th. preced th s) ` ({th1} \ dependants (wq s) th1)) = + Max ((\th. preced th s) ` ({th2} \ dependants (wq s) th2))" + (is "Max (?f ` ?A) = Max (?f ` ?B)") + unfolding cp_eq_cpreced + unfolding cpreced_def . + obtain th1' where th1_in: "th1' \ ?A" and eq_f_th1: "?f th1' = Max (?f ` ?A)" + proof - + have h1: "finite (?f ` ?A)" + proof - + have "finite ?A" + proof - + have "finite (dependants (wq s) th1)" + proof- + have "finite {th'. (Th th', Th th1) \ (RAG (wq s))\<^sup>+}" + proof - + let ?F = "\ (x, y). the_th x" + have "{th'. (Th th', Th th1) \ (RAG (wq s))\<^sup>+} \ ?F ` ((RAG (wq s))\<^sup>+)" + apply (auto simp:image_def) + by (rule_tac x = "(Th x, Th th1)" in bexI, auto) + moreover have "finite \" + proof - + from finite_RAG have "finite (RAG s)" . + hence "finite ((RAG (wq s))\<^sup>+)" + apply (unfold finite_trancl) + by (auto simp: s_RAG_def cs_RAG_def wq_def) + thus ?thesis by auto + qed + ultimately show ?thesis by (auto intro:finite_subset) + qed + thus ?thesis by (simp add:cs_dependants_def) + qed + thus ?thesis by simp + qed + thus ?thesis by auto + qed + moreover have h2: "(?f ` ?A) \ {}" + proof - + have "?A \ {}" by simp + thus ?thesis by simp + qed + from Max_in [OF h1 h2] + have "Max (?f ` ?A) \ (?f ` ?A)" . + thus ?thesis + thm cpreced_def + unfolding cpreced_def[symmetric] + unfolding cp_eq_cpreced[symmetric] + unfolding cpreced_def + using that[intro] by (auto) + qed + obtain th2' where th2_in: "th2' \ ?B" and eq_f_th2: "?f th2' = Max (?f ` ?B)" + proof - + have h1: "finite (?f ` ?B)" + proof - + have "finite ?B" + proof - + have "finite (dependants (wq s) th2)" + proof- + have "finite {th'. (Th th', Th th2) \ (RAG (wq s))\<^sup>+}" + proof - + let ?F = "\ (x, y). the_th x" + have "{th'. (Th th', Th th2) \ (RAG (wq s))\<^sup>+} \ ?F ` ((RAG (wq s))\<^sup>+)" + apply (auto simp:image_def) + by (rule_tac x = "(Th x, Th th2)" in bexI, auto) + moreover have "finite \" + proof - + from finite_RAG have "finite (RAG s)" . + hence "finite ((RAG (wq s))\<^sup>+)" + apply (unfold finite_trancl) + by (auto simp: s_RAG_def cs_RAG_def wq_def) + thus ?thesis by auto + qed + ultimately show ?thesis by (auto intro:finite_subset) + qed + thus ?thesis by (simp add:cs_dependants_def) + qed + thus ?thesis by simp + qed + thus ?thesis by auto + qed + moreover have h2: "(?f ` ?B) \ {}" + proof - + have "?B \ {}" by simp + thus ?thesis by simp + qed + from Max_in [OF h1 h2] + have "Max (?f ` ?B) \ (?f ` ?B)" . + thus ?thesis by (auto intro:that) + qed + from eq_f_th1 eq_f_th2 eq_max + have eq_preced: "preced th1' s = preced th2' s" by auto + hence eq_th12: "th1' = th2'" + proof (rule preced_unique) + from th1_in have "th1' = th1 \ (th1' \ dependants (wq s) th1)" by simp + thus "th1' \ threads s" + proof + assume "th1' \ dependants (wq s) th1" + hence "(Th th1') \ Domain ((RAG s)^+)" + apply (unfold cs_dependants_def cs_RAG_def s_RAG_def) + by (auto simp:Domain_def) + hence "(Th th1') \ Domain (RAG s)" by (simp add:trancl_domain) + from dm_RAG_threads[OF this] show ?thesis . + next + assume "th1' = th1" + with runing_1 show ?thesis + by (unfold runing_def readys_def, auto) + qed + next + from th2_in have "th2' = th2 \ (th2' \ dependants (wq s) th2)" by simp + thus "th2' \ threads s" + proof + assume "th2' \ dependants (wq s) th2" + hence "(Th th2') \ Domain ((RAG s)^+)" + apply (unfold cs_dependants_def cs_RAG_def s_RAG_def) + by (auto simp:Domain_def) + hence "(Th th2') \ Domain (RAG s)" by (simp add:trancl_domain) + from dm_RAG_threads[OF this] show ?thesis . + next + assume "th2' = th2" + with runing_2 show ?thesis + by (unfold runing_def readys_def, auto) + qed + qed + from th1_in have "th1' = th1 \ th1' \ dependants (wq s) th1" by simp + thus ?thesis + proof + assume eq_th': "th1' = th1" + from th2_in have "th2' = th2 \ th2' \ dependants (wq s) th2" by simp + thus ?thesis + proof + assume "th2' = th2" thus ?thesis using eq_th' eq_th12 by simp + next + assume "th2' \ dependants (wq s) th2" + with eq_th12 eq_th' have "th1 \ dependants (wq s) th2" by simp + hence "(Th th1, Th th2) \ (RAG s)^+" + by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp) + hence "Th th1 \ Domain ((RAG s)^+)" + apply (unfold cs_dependants_def cs_RAG_def s_RAG_def) + by (auto simp:Domain_def) + hence "Th th1 \ Domain (RAG s)" by (simp add:trancl_domain) + then obtain n where d: "(Th th1, n) \ RAG s" by (auto simp:Domain_def) + from RAG_target_th [OF this] + obtain cs' where "n = Cs cs'" by auto + with d have "(Th th1, Cs cs') \ RAG s" by simp + with runing_1 have "False" + apply (unfold runing_def readys_def s_RAG_def) + by (auto simp:waiting_eq) + thus ?thesis by simp + qed + next + assume th1'_in: "th1' \ dependants (wq s) th1" + from th2_in have "th2' = th2 \ th2' \ dependants (wq s) th2" by simp + thus ?thesis + proof + assume "th2' = th2" + with th1'_in eq_th12 have "th2 \ dependants (wq s) th1" by simp + hence "(Th th2, Th th1) \ (RAG s)^+" + by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp) + hence "Th th2 \ Domain ((RAG s)^+)" + apply (unfold cs_dependants_def cs_RAG_def s_RAG_def) + by (auto simp:Domain_def) + hence "Th th2 \ Domain (RAG s)" by (simp add:trancl_domain) + then obtain n where d: "(Th th2, n) \ RAG s" by (auto simp:Domain_def) + from RAG_target_th [OF this] + obtain cs' where "n = Cs cs'" by auto + with d have "(Th th2, Cs cs') \ RAG s" by simp + with runing_2 have "False" + apply (unfold runing_def readys_def s_RAG_def) + by (auto simp:waiting_eq) + thus ?thesis by simp + next + assume "th2' \ dependants (wq s) th2" + with eq_th12 have "th1' \ dependants (wq s) th2" by simp + hence h1: "(Th th1', Th th2) \ (RAG s)^+" + by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp) + from th1'_in have h2: "(Th th1', Th th1) \ (RAG s)^+" + by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp) + show ?thesis + proof(rule dchain_unique[OF h1 _ h2, symmetric]) + from runing_1 show "th1 \ readys s" by (simp add:runing_def) + from runing_2 show "th2 \ readys s" by (simp add:runing_def) + qed + qed + qed +qed + + +lemma "card (runing s) \ 1" +apply(subgoal_tac "finite (runing s)") +prefer 2 +apply (metis finite_nat_set_iff_bounded lessI runing_unique) +apply(rule ccontr) +apply(simp) +apply(case_tac "Suc (Suc 0) \ card (runing s)") +apply(subst (asm) card_le_Suc_iff) +apply(simp) +apply(auto)[1] +apply (metis insertCI runing_unique) +apply(auto) +done + +end + + +lemma create_pre: + assumes stp: "step s e" + and not_in: "th \ threads s" + and is_in: "th \ threads (e#s)" + obtains prio where "e = Create th prio" +proof - + from assms + show ?thesis + proof(cases) + case (thread_create thread prio) + with is_in not_in have "e = Create th prio" by simp + from that[OF this] show ?thesis . + next + case (thread_exit thread) + with assms show ?thesis by (auto intro!:that) + next + case (thread_P thread) + with assms show ?thesis by (auto intro!:that) + next + case (thread_V thread) + with assms show ?thesis by (auto intro!:that) + next + case (thread_set thread) + with assms show ?thesis by (auto intro!:that) + qed +qed + +context valid_trace +begin + +lemma cnp_cnv_eq: + assumes "th \ threads s" + shows "cntP s th = cntV s th" + using assms + using cnp_cnv_cncs not_thread_cncs by auto + +end + + +lemma eq_RAG: + "RAG (wq s) = RAG s" +by (unfold cs_RAG_def s_RAG_def, auto) + +context valid_trace +begin + +lemma count_eq_dependants: + assumes eq_pv: "cntP s th = cntV s th" + shows "dependants (wq s) th = {}" +proof - + from cnp_cnv_cncs and eq_pv + have "cntCS s th = 0" + by (auto split:if_splits) + moreover have "finite {cs. (Cs cs, Th th) \ RAG s}" + proof - + from finite_holding[of th] show ?thesis + by (simp add:holdents_test) + qed + ultimately have h: "{cs. (Cs cs, Th th) \ RAG s} = {}" + by (unfold cntCS_def holdents_test cs_dependants_def, auto) + show ?thesis + proof(unfold cs_dependants_def) + { assume "{th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ {}" + then obtain th' where "(Th th', Th th) \ (RAG (wq s))\<^sup>+" by auto + hence "False" + proof(cases) + assume "(Th th', Th th) \ RAG (wq s)" + thus "False" by (auto simp:cs_RAG_def) + next + fix c + assume "(c, Th th) \ RAG (wq s)" + with h and eq_RAG show "False" + by (cases c, auto simp:cs_RAG_def) + qed + } thus "{th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} = {}" by auto + qed +qed + +lemma dependants_threads: + shows "dependants (wq s) th \ threads s" +proof + { fix th th' + assume h: "th \ {th'a. (Th th'a, Th th') \ (RAG (wq s))\<^sup>+}" + have "Th th \ Domain (RAG s)" + proof - + from h obtain th' where "(Th th, Th th') \ (RAG (wq s))\<^sup>+" by auto + hence "(Th th) \ Domain ( (RAG (wq s))\<^sup>+)" by (auto simp:Domain_def) + with trancl_domain have "(Th th) \ Domain (RAG (wq s))" by simp + thus ?thesis using eq_RAG by simp + qed + from dm_RAG_threads[OF this] + have "th \ threads s" . + } note hh = this + fix th1 + assume "th1 \ dependants (wq s) th" + hence "th1 \ {th'a. (Th th'a, Th th) \ (RAG (wq s))\<^sup>+}" + by (unfold cs_dependants_def, simp) + from hh [OF this] show "th1 \ threads s" . +qed + +lemma finite_threads: + shows "finite (threads s)" +using vt by (induct) (auto elim: step.cases) + +end + +lemma Max_f_mono: + assumes seq: "A \ B" + and np: "A \ {}" + and fnt: "finite B" + shows "Max (f ` A) \ Max (f ` B)" +proof(rule Max_mono) + from seq show "f ` A \ f ` B" by auto +next + from np show "f ` A \ {}" by auto +next + from fnt and seq show "finite (f ` B)" by auto +qed + +context valid_trace +begin + +lemma cp_le: + assumes th_in: "th \ threads s" + shows "cp s th \ Max ((\ th. (preced th s)) ` threads s)" +proof(unfold cp_eq_cpreced cpreced_def cs_dependants_def) + show "Max ((\th. preced th s) ` ({th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+})) + \ Max ((\th. preced th s) ` threads s)" + (is "Max (?f ` ?A) \ Max (?f ` ?B)") + proof(rule Max_f_mono) + show "{th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ {}" by simp + next + from finite_threads + show "finite (threads s)" . + next + from th_in + show "{th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ threads s" + apply (auto simp:Domain_def) + apply (rule_tac dm_RAG_threads) + apply (unfold trancl_domain [of "RAG s", symmetric]) + by (unfold cs_RAG_def s_RAG_def, auto simp:Domain_def) + qed +qed + +lemma le_cp: + shows "preced th s \ cp s th" +proof(unfold cp_eq_cpreced preced_def cpreced_def, simp) + show "Prc (priority th s) (last_set th s) + \ Max (insert (Prc (priority th s) (last_set th s)) + ((\th. Prc (priority th s) (last_set th s)) ` dependants (wq s) th))" + (is "?l \ Max (insert ?l ?A)") + proof(cases "?A = {}") + case False + have "finite ?A" (is "finite (?f ` ?B)") + proof - + have "finite ?B" + proof- + have "finite {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+}" + proof - + let ?F = "\ (x, y). the_th x" + have "{th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ ?F ` ((RAG (wq s))\<^sup>+)" + apply (auto simp:image_def) + by (rule_tac x = "(Th x, Th th)" in bexI, auto) + moreover have "finite \" + proof - + from finite_RAG have "finite (RAG s)" . + hence "finite ((RAG (wq s))\<^sup>+)" + apply (unfold finite_trancl) + by (auto simp: s_RAG_def cs_RAG_def wq_def) + thus ?thesis by auto + qed + ultimately show ?thesis by (auto intro:finite_subset) + qed + thus ?thesis by (simp add:cs_dependants_def) + qed + thus ?thesis by simp + qed + from Max_insert [OF this False, of ?l] show ?thesis by auto + next + case True + thus ?thesis by auto + qed +qed + +lemma max_cp_eq: + shows "Max ((cp s) ` threads s) = Max ((\ th. (preced th s)) ` threads s)" + (is "?l = ?r") +proof(cases "threads s = {}") + case True + thus ?thesis by auto +next + case False + have "?l \ ((cp s) ` threads s)" + proof(rule Max_in) + from finite_threads + show "finite (cp s ` threads s)" by auto + next + from False show "cp s ` threads s \ {}" by auto + qed + then obtain th + where th_in: "th \ threads s" and eq_l: "?l = cp s th" by auto + have "\ \ ?r" by (rule cp_le[OF th_in]) + moreover have "?r \ cp s th" (is "Max (?f ` ?A) \ cp s th") + proof - + have "?r \ (?f ` ?A)" + proof(rule Max_in) + from finite_threads + show " finite ((\th. preced th s) ` threads s)" by auto + next + from False show " (\th. preced th s) ` threads s \ {}" by auto + qed + then obtain th' where + th_in': "th' \ ?A " and eq_r: "?r = ?f th'" by auto + from le_cp [of th'] eq_r + have "?r \ cp s th'" by auto + moreover have "\ \ cp s th" + proof(fold eq_l) + show " cp s th' \ Max (cp s ` threads s)" + proof(rule Max_ge) + from th_in' show "cp s th' \ cp s ` threads s" + by auto + next + from finite_threads + show "finite (cp s ` threads s)" by auto + qed + qed + ultimately show ?thesis by auto + qed + ultimately show ?thesis using eq_l by auto +qed + +lemma max_cp_readys_threads_pre: + assumes np: "threads s \ {}" + shows "Max (cp s ` readys s) = Max (cp s ` threads s)" +proof(unfold max_cp_eq) + show "Max (cp s ` readys s) = Max ((\th. preced th s) ` threads s)" + proof - + let ?p = "Max ((\th. preced th s) ` threads s)" + let ?f = "(\th. preced th s)" + have "?p \ ((\th. preced th s) ` threads s)" + proof(rule Max_in) + from finite_threads show "finite (?f ` threads s)" by simp + next + from np show "?f ` threads s \ {}" by simp + qed + then obtain tm where tm_max: "?f tm = ?p" and tm_in: "tm \ threads s" + by (auto simp:Image_def) + from th_chain_to_ready [OF tm_in] + have "tm \ readys s \ (\th'. th' \ readys s \ (Th tm, Th th') \ (RAG s)\<^sup>+)" . + thus ?thesis + proof + assume "\th'. th' \ readys s \ (Th tm, Th th') \ (RAG s)\<^sup>+ " + then obtain th' where th'_in: "th' \ readys s" + and tm_chain:"(Th tm, Th th') \ (RAG s)\<^sup>+" by auto + have "cp s th' = ?f tm" + proof(subst cp_eq_cpreced, subst cpreced_def, rule Max_eqI) + from dependants_threads finite_threads + show "finite ((\th. preced th s) ` ({th'} \ dependants (wq s) th'))" + by (auto intro:finite_subset) + next + fix p assume p_in: "p \ (\th. preced th s) ` ({th'} \ dependants (wq s) th')" + from tm_max have " preced tm s = Max ((\th. preced th s) ` threads s)" . + moreover have "p \ \" + proof(rule Max_ge) + from finite_threads + show "finite ((\th. preced th s) ` threads s)" by simp + next + from p_in and th'_in and dependants_threads[of th'] + show "p \ (\th. preced th s) ` threads s" + by (auto simp:readys_def) + qed + ultimately show "p \ preced tm s" by auto + next + show "preced tm s \ (\th. preced th s) ` ({th'} \ dependants (wq s) th')" + proof - + from tm_chain + have "tm \ dependants (wq s) th'" + by (unfold cs_dependants_def s_RAG_def cs_RAG_def, auto) + thus ?thesis by auto + qed + qed + with tm_max + have h: "cp s th' = Max ((\th. preced th s) ` threads s)" by simp + show ?thesis + proof (fold h, rule Max_eqI) + fix q + assume "q \ cp s ` readys s" + then obtain th1 where th1_in: "th1 \ readys s" + and eq_q: "q = cp s th1" by auto + show "q \ cp s th'" + apply (unfold h eq_q) + apply (unfold cp_eq_cpreced cpreced_def) + apply (rule Max_mono) + proof - + from dependants_threads [of th1] th1_in + show "(\th. preced th s) ` ({th1} \ dependants (wq s) th1) \ + (\th. preced th s) ` threads s" + by (auto simp:readys_def) + next + show "(\th. preced th s) ` ({th1} \ dependants (wq s) th1) \ {}" by simp + next + from finite_threads + show " finite ((\th. preced th s) ` threads s)" by simp + qed + next + from finite_threads + show "finite (cp s ` readys s)" by (auto simp:readys_def) + next + from th'_in + show "cp s th' \ cp s ` readys s" by simp + qed + next + assume tm_ready: "tm \ readys s" + show ?thesis + proof(fold tm_max) + have cp_eq_p: "cp s tm = preced tm s" + proof(unfold cp_eq_cpreced cpreced_def, rule Max_eqI) + fix y + assume hy: "y \ (\th. preced th s) ` ({tm} \ dependants (wq s) tm)" + show "y \ preced tm s" + proof - + { fix y' + assume hy' : "y' \ ((\th. preced th s) ` dependants (wq s) tm)" + have "y' \ preced tm s" + proof(unfold tm_max, rule Max_ge) + from hy' dependants_threads[of tm] + show "y' \ (\th. preced th s) ` threads s" by auto + next + from finite_threads + show "finite ((\th. preced th s) ` threads s)" by simp + qed + } with hy show ?thesis by auto + qed + next + from dependants_threads[of tm] finite_threads + show "finite ((\th. preced th s) ` ({tm} \ dependants (wq s) tm))" + by (auto intro:finite_subset) + next + show "preced tm s \ (\th. preced th s) ` ({tm} \ dependants (wq s) tm)" + by simp + qed + moreover have "Max (cp s ` readys s) = cp s tm" + proof(rule Max_eqI) + from tm_ready show "cp s tm \ cp s ` readys s" by simp + next + from finite_threads + show "finite (cp s ` readys s)" by (auto simp:readys_def) + next + fix y assume "y \ cp s ` readys s" + then obtain th1 where th1_readys: "th1 \ readys s" + and h: "y = cp s th1" by auto + show "y \ cp s tm" + apply(unfold cp_eq_p h) + apply(unfold cp_eq_cpreced cpreced_def tm_max, rule Max_mono) + proof - + from finite_threads + show "finite ((\th. preced th s) ` threads s)" by simp + next + show "(\th. preced th s) ` ({th1} \ dependants (wq s) th1) \ {}" + by simp + next + from dependants_threads[of th1] th1_readys + show "(\th. preced th s) ` ({th1} \ dependants (wq s) th1) + \ (\th. preced th s) ` threads s" + by (auto simp:readys_def) + qed + qed + ultimately show " Max (cp s ` readys s) = preced tm s" by simp + qed + qed + qed +qed + +text {* (* ccc *) \noindent + Since the current precedence of the threads in ready queue will always be boosted, + there must be one inside it has the maximum precedence of the whole system. +*} +lemma max_cp_readys_threads: + shows "Max (cp s ` readys s) = Max (cp s ` threads s)" +proof(cases "threads s = {}") + case True + thus ?thesis + by (auto simp:readys_def) +next + case False + show ?thesis by (rule max_cp_readys_threads_pre[OF False]) +qed + +end + +lemma eq_holding: "holding (wq s) th cs = holding s th cs" + apply (unfold s_holding_def cs_holding_def wq_def, simp) + done + +lemma f_image_eq: + assumes h: "\ a. a \ A \ f a = g a" + shows "f ` A = g ` A" +proof + show "f ` A \ g ` A" + by(rule image_subsetI, auto intro:h) +next + show "g ` A \ f ` A" + by (rule image_subsetI, auto intro:h[symmetric]) +qed + + +definition detached :: "state \ thread \ bool" + where "detached s th \ (\(\ cs. holding s th cs)) \ (\(\cs. waiting s th cs))" + +lemma detached_test: + shows "detached s th = (Th th \ Field (RAG s))" +apply(simp add: detached_def Field_def) +apply(simp add: s_RAG_def) +apply(simp add: s_holding_abv s_waiting_abv) +apply(simp add: Domain_iff Range_iff) +apply(simp add: wq_def) +apply(auto) +done + +context valid_trace +begin + +lemma detached_intro: + assumes eq_pv: "cntP s th = cntV s th" + shows "detached s th" +proof - + from cnp_cnv_cncs + have eq_cnt: "cntP s th = + cntV s th + (if th \ readys s \ th \ threads s then cntCS s th else cntCS s th + 1)" . + hence cncs_zero: "cntCS s th = 0" + by (auto simp:eq_pv split:if_splits) + with eq_cnt + have "th \ readys s \ th \ threads s" by (auto simp:eq_pv) + thus ?thesis + proof + assume "th \ threads s" + with range_in dm_RAG_threads + show ?thesis + by (auto simp add: detached_def s_RAG_def s_waiting_abv s_holding_abv wq_def Domain_iff Range_iff) + next + assume "th \ readys s" + moreover have "Th th \ Range (RAG s)" + proof - + from card_0_eq [OF finite_holding] and cncs_zero + have "holdents s th = {}" + by (simp add:cntCS_def) + thus ?thesis + apply(auto simp:holdents_test) + apply(case_tac a) + apply(auto simp:holdents_test s_RAG_def) + done + qed + ultimately show ?thesis + by (auto simp add: detached_def s_RAG_def s_waiting_abv s_holding_abv wq_def readys_def) + qed +qed + +lemma detached_elim: + assumes dtc: "detached s th" + shows "cntP s th = cntV s th" +proof - + from cnp_cnv_cncs + have eq_pv: " cntP s th = + cntV s th + (if th \ readys s \ th \ threads s then cntCS s th else cntCS s th + 1)" . + have cncs_z: "cntCS s th = 0" + proof - + from dtc have "holdents s th = {}" + unfolding detached_def holdents_test s_RAG_def + by (simp add: s_waiting_abv wq_def s_holding_abv Domain_iff Range_iff) + thus ?thesis by (auto simp:cntCS_def) + qed + show ?thesis + proof(cases "th \ threads s") + case True + with dtc + have "th \ readys s" + by (unfold readys_def detached_def Field_def Domain_def Range_def, + auto simp:waiting_eq s_RAG_def) + with cncs_z and eq_pv show ?thesis by simp + next + case False + with cncs_z and eq_pv show ?thesis by simp + qed +qed + +lemma detached_eq: + shows "(detached s th) = (cntP s th = cntV s th)" + by (insert vt, auto intro:detached_intro detached_elim) + +end + +text {* + The lemmas in this .thy file are all obvious lemmas, however, they still needs to be derived + from the concise and miniature model of PIP given in PrioGDef.thy. +*} + +lemma eq_dependants: "dependants (wq s) = dependants s" + by (simp add: s_dependants_abv wq_def) + +lemma next_th_unique: + assumes nt1: "next_th s th cs th1" + and nt2: "next_th s th cs th2" + shows "th1 = th2" +using assms by (unfold next_th_def, auto) + +lemma birth_time_lt: "s \ [] \ last_set th s < length s" + apply (induct s, simp) +proof - + fix a s + assume ih: "s \ [] \ last_set th s < length s" + and eq_as: "a # s \ []" + show "last_set th (a # s) < length (a # s)" + proof(cases "s \ []") + case False + from False show ?thesis + by (cases a, auto simp:last_set.simps) + next + case True + from ih [OF True] show ?thesis + by (cases a, auto simp:last_set.simps) + qed +qed + +lemma th_in_ne: "th \ threads s \ s \ []" + by (induct s, auto simp:threads.simps) + +lemma preced_tm_lt: "th \ threads s \ preced th s = Prc x y \ y < length s" + apply (drule_tac th_in_ne) + by (unfold preced_def, auto intro: birth_time_lt) + +lemma inj_the_preced: + "inj_on (the_preced s) (threads s)" + by (metis inj_onI preced_unique the_preced_def) + +lemma tRAG_alt_def: + "tRAG s = {(Th th1, Th th2) | th1 th2. + \ cs. (Th th1, Cs cs) \ RAG s \ (Cs cs, Th th2) \ RAG s}" + by (auto simp:tRAG_def RAG_split wRAG_def hRAG_def) + +lemma tRAG_Field: + "Field (tRAG s) \ Field (RAG s)" + by (unfold tRAG_alt_def Field_def, auto) + +lemma tRAG_ancestorsE: + assumes "x \ ancestors (tRAG s) u" + obtains th where "x = Th th" +proof - + from assms have "(u, x) \ (tRAG s)^+" + by (unfold ancestors_def, auto) + from tranclE[OF this] obtain c where "(c, x) \ tRAG s" by auto + then obtain th where "x = Th th" + by (unfold tRAG_alt_def, auto) + from that[OF this] show ?thesis . +qed + +lemma tRAG_mono: + assumes "RAG s' \ RAG s" + shows "tRAG s' \ tRAG s" + using assms + by (unfold tRAG_alt_def, auto) + +lemma holding_next_thI: + assumes "holding s th cs" + and "length (wq s cs) > 1" + obtains th' where "next_th s th cs th'" +proof - + from assms(1)[folded eq_holding, unfolded cs_holding_def] + have " th \ set (wq s cs) \ th = hd (wq s cs)" . + then obtain rest where h1: "wq s cs = th#rest" + by (cases "wq s cs", auto) + with assms(2) have h2: "rest \ []" by auto + let ?th' = "hd (SOME q. distinct q \ set q = set rest)" + have "next_th s th cs ?th'" using h1(1) h2 + by (unfold next_th_def, auto) + from that[OF this] show ?thesis . +qed + +lemma RAG_tRAG_transfer: + assumes "vt s'" + assumes "RAG s = RAG s' \ {(Th th, Cs cs)}" + and "(Cs cs, Th th'') \ RAG s'" + shows "tRAG s = tRAG s' \ {(Th th, Th th'')}" (is "?L = ?R") +proof - + interpret vt_s': valid_trace "s'" using assms(1) + by (unfold_locales, simp) + interpret rtree: rtree "RAG s'" + proof + show "single_valued (RAG s')" + apply (intro_locales) + by (unfold single_valued_def, + auto intro:vt_s'.unique_RAG) + + show "acyclic (RAG s')" + by (rule vt_s'.acyclic_RAG) + qed + { fix n1 n2 + assume "(n1, n2) \ ?L" + from this[unfolded tRAG_alt_def] + obtain th1 th2 cs' where + h: "n1 = Th th1" "n2 = Th th2" + "(Th th1, Cs cs') \ RAG s" + "(Cs cs', Th th2) \ RAG s" by auto + from h(4) and assms(2) have cs_in: "(Cs cs', Th th2) \ RAG s'" by auto + from h(3) and assms(2) + have "(Th th1, Cs cs') = (Th th, Cs cs) \ + (Th th1, Cs cs') \ RAG s'" by auto + hence "(n1, n2) \ ?R" + proof + assume h1: "(Th th1, Cs cs') = (Th th, Cs cs)" + hence eq_th1: "th1 = th" by simp + moreover have "th2 = th''" + proof - + from h1 have "cs' = cs" by simp + from assms(3) cs_in[unfolded this] rtree.sgv + show ?thesis + by (unfold single_valued_def, auto) + qed + ultimately show ?thesis using h(1,2) by auto + next + assume "(Th th1, Cs cs') \ RAG s'" + with cs_in have "(Th th1, Th th2) \ tRAG s'" + by (unfold tRAG_alt_def, auto) + from this[folded h(1, 2)] show ?thesis by auto + qed + } moreover { + fix n1 n2 + assume "(n1, n2) \ ?R" + hence "(n1, n2) \tRAG s' \ (n1, n2) = (Th th, Th th'')" by auto + hence "(n1, n2) \ ?L" + proof + assume "(n1, n2) \ tRAG s'" + moreover have "... \ ?L" + proof(rule tRAG_mono) + show "RAG s' \ RAG s" by (unfold assms(2), auto) + qed + ultimately show ?thesis by auto + next + assume eq_n: "(n1, n2) = (Th th, Th th'')" + from assms(2, 3) have "(Cs cs, Th th'') \ RAG s" by auto + moreover have "(Th th, Cs cs) \ RAG s" using assms(2) by auto + ultimately show ?thesis + by (unfold eq_n tRAG_alt_def, auto) + qed + } ultimately show ?thesis by auto +qed + +context valid_trace +begin + +lemmas RAG_tRAG_transfer = RAG_tRAG_transfer[OF vt] + +end + +lemma cp_alt_def: + "cp s th = + Max ((the_preced s) ` {th'. Th th' \ (subtree (RAG s) (Th th))})" +proof - + have "Max (the_preced s ` ({th} \ dependants (wq s) th)) = + Max (the_preced s ` {th'. Th th' \ subtree (RAG s) (Th th)})" + (is "Max (_ ` ?L) = Max (_ ` ?R)") + proof - + have "?L = ?R" + by (auto dest:rtranclD simp:cs_dependants_def cs_RAG_def s_RAG_def subtree_def) + thus ?thesis by simp + qed + thus ?thesis by (unfold cp_eq_cpreced cpreced_def, fold the_preced_def, simp) +qed + +lemma cp_gen_alt_def: + "cp_gen s = (Max \ (\x. (the_preced s \ the_thread) ` subtree (tRAG s) x))" + by (auto simp:cp_gen_def) + +lemma tRAG_nodeE: + assumes "(n1, n2) \ tRAG s" + obtains th1 th2 where "n1 = Th th1" "n2 = Th th2" + using assms + by (auto simp: tRAG_def wRAG_def hRAG_def tRAG_def) + +lemma subtree_nodeE: + assumes "n \ subtree (tRAG s) (Th th)" + obtains th1 where "n = Th th1" +proof - + show ?thesis + proof(rule subtreeE[OF assms]) + assume "n = Th th" + from that[OF this] show ?thesis . + next + assume "Th th \ ancestors (tRAG s) n" + hence "(n, Th th) \ (tRAG s)^+" by (auto simp:ancestors_def) + hence "\ th1. n = Th th1" + proof(induct) + case (base y) + from tRAG_nodeE[OF this] show ?case by metis + next + case (step y z) + thus ?case by auto + qed + with that show ?thesis by auto + qed +qed + +lemma tRAG_star_RAG: "(tRAG s)^* \ (RAG s)^*" +proof - + have "(wRAG s O hRAG s)^* \ (RAG s O RAG s)^*" + by (rule rtrancl_mono, auto simp:RAG_split) + also have "... \ ((RAG s)^*)^*" + by (rule rtrancl_mono, auto) + also have "... = (RAG s)^*" by simp + finally show ?thesis by (unfold tRAG_def, simp) +qed + +lemma tRAG_subtree_RAG: "subtree (tRAG s) x \ subtree (RAG s) x" +proof - + { fix a + assume "a \ subtree (tRAG s) x" + hence "(a, x) \ (tRAG s)^*" by (auto simp:subtree_def) + with tRAG_star_RAG[of s] + have "(a, x) \ (RAG s)^*" by auto + hence "a \ subtree (RAG s) x" by (auto simp:subtree_def) + } thus ?thesis by auto +qed + +lemma tRAG_trancl_eq: + "{th'. (Th th', Th th) \ (tRAG s)^+} = + {th'. (Th th', Th th) \ (RAG s)^+}" + (is "?L = ?R") +proof - + { fix th' + assume "th' \ ?L" + hence "(Th th', Th th) \ (tRAG s)^+" by auto + from tranclD[OF this] + obtain z where h: "(Th th', z) \ tRAG s" "(z, Th th) \ (tRAG s)\<^sup>*" by auto + from tRAG_subtree_RAG[of s] and this(2) + have "(z, Th th) \ (RAG s)^*" by (meson subsetCE tRAG_star_RAG) + moreover from h(1) have "(Th th', z) \ (RAG s)^+" using tRAG_alt_def by auto + ultimately have "th' \ ?R" by auto + } moreover + { fix th' + assume "th' \ ?R" + hence "(Th th', Th th) \ (RAG s)^+" by (auto) + from plus_rpath[OF this] + obtain xs where rp: "rpath (RAG s) (Th th') xs (Th th)" "xs \ []" by auto + hence "(Th th', Th th) \ (tRAG s)^+" + proof(induct xs arbitrary:th' th rule:length_induct) + case (1 xs th' th) + then obtain x1 xs1 where Cons1: "xs = x1#xs1" by (cases xs, auto) + show ?case + proof(cases "xs1") + case Nil + from 1(2)[unfolded Cons1 Nil] + have rp: "rpath (RAG s) (Th th') [x1] (Th th)" . + hence "(Th th', x1) \ (RAG s)" by (cases, simp) + then obtain cs where "x1 = Cs cs" + by (unfold s_RAG_def, auto) + from rpath_nnl_lastE[OF rp[unfolded this]] + show ?thesis by auto + next + case (Cons x2 xs2) + from 1(2)[unfolded Cons1[unfolded this]] + have rp: "rpath (RAG s) (Th th') (x1 # x2 # xs2) (Th th)" . + from rpath_edges_on[OF this] + have eds: "edges_on (Th th' # x1 # x2 # xs2) \ RAG s" . + have "(Th th', x1) \ edges_on (Th th' # x1 # x2 # xs2)" + by (simp add: edges_on_unfold) + with eds have rg1: "(Th th', x1) \ RAG s" by auto + then obtain cs1 where eq_x1: "x1 = Cs cs1" by (unfold s_RAG_def, auto) + have "(x1, x2) \ edges_on (Th th' # x1 # x2 # xs2)" + by (simp add: edges_on_unfold) + from this eds + have rg2: "(x1, x2) \ RAG s" by auto + from this[unfolded eq_x1] + obtain th1 where eq_x2: "x2 = Th th1" by (unfold s_RAG_def, auto) + from rg1[unfolded eq_x1] rg2[unfolded eq_x1 eq_x2] + have rt1: "(Th th', Th th1) \ tRAG s" by (unfold tRAG_alt_def, auto) + from rp have "rpath (RAG s) x2 xs2 (Th th)" + by (elim rpath_ConsE, simp) + from this[unfolded eq_x2] have rp': "rpath (RAG s) (Th th1) xs2 (Th th)" . + show ?thesis + proof(cases "xs2 = []") + case True + from rpath_nilE[OF rp'[unfolded this]] + have "th1 = th" by auto + from rt1[unfolded this] show ?thesis by auto + next + case False + from 1(1)[rule_format, OF _ rp' this, unfolded Cons1 Cons] + have "(Th th1, Th th) \ (tRAG s)\<^sup>+" by simp + with rt1 show ?thesis by auto + qed + qed + qed + hence "th' \ ?L" by auto + } ultimately show ?thesis by blast +qed + +lemma tRAG_trancl_eq_Th: + "{Th th' | th'. (Th th', Th th) \ (tRAG s)^+} = + {Th th' | th'. (Th th', Th th) \ (RAG s)^+}" + using tRAG_trancl_eq by auto + +lemma dependants_alt_def: + "dependants s th = {th'. (Th th', Th th) \ (tRAG s)^+}" + by (metis eq_RAG s_dependants_def tRAG_trancl_eq) + +context valid_trace +begin + +lemma count_eq_tRAG_plus: + assumes "cntP s th = cntV s th" + shows "{th'. (Th th', Th th) \ (tRAG s)^+} = {}" + using assms count_eq_dependants dependants_alt_def eq_dependants by auto + +lemma count_eq_RAG_plus: + assumes "cntP s th = cntV s th" + shows "{th'. (Th th', Th th) \ (RAG s)^+} = {}" + using assms count_eq_dependants cs_dependants_def eq_RAG by auto + +lemma count_eq_RAG_plus_Th: + assumes "cntP s th = cntV s th" + shows "{Th th' | th'. (Th th', Th th) \ (RAG s)^+} = {}" + using count_eq_RAG_plus[OF assms] by auto + +lemma count_eq_tRAG_plus_Th: + assumes "cntP s th = cntV s th" + shows "{Th th' | th'. (Th th', Th th) \ (tRAG s)^+} = {}" + using count_eq_tRAG_plus[OF assms] by auto + +end + +lemma tRAG_subtree_eq: + "(subtree (tRAG s) (Th th)) = {Th th' | th'. Th th' \ (subtree (RAG s) (Th th))}" + (is "?L = ?R") +proof - + { fix n + assume h: "n \ ?L" + hence "n \ ?R" + by (smt mem_Collect_eq subsetCE subtree_def subtree_nodeE tRAG_subtree_RAG) + } moreover { + fix n + assume "n \ ?R" + then obtain th' where h: "n = Th th'" "(Th th', Th th) \ (RAG s)^*" + by (auto simp:subtree_def) + from rtranclD[OF this(2)] + have "n \ ?L" + proof + assume "Th th' \ Th th \ (Th th', Th th) \ (RAG s)\<^sup>+" + with h have "n \ {Th th' | th'. (Th th', Th th) \ (RAG s)^+}" by auto + thus ?thesis using subtree_def tRAG_trancl_eq by fastforce + qed (insert h, auto simp:subtree_def) + } ultimately show ?thesis by auto +qed + +lemma threads_set_eq: + "the_thread ` (subtree (tRAG s) (Th th)) = + {th'. Th th' \ (subtree (RAG s) (Th th))}" (is "?L = ?R") + by (auto intro:rev_image_eqI simp:tRAG_subtree_eq) + +lemma cp_alt_def1: + "cp s th = Max ((the_preced s o the_thread) ` (subtree (tRAG s) (Th th)))" +proof - + have "(the_preced s ` the_thread ` subtree (tRAG s) (Th th)) = + ((the_preced s \ the_thread) ` subtree (tRAG s) (Th th))" + by auto + thus ?thesis by (unfold cp_alt_def, fold threads_set_eq, auto) +qed + +lemma cp_gen_def_cond: + assumes "x = Th th" + shows "cp s th = cp_gen s (Th th)" +by (unfold cp_alt_def1 cp_gen_def, simp) + +lemma cp_gen_over_set: + assumes "\ x \ A. \ th. x = Th th" + shows "cp_gen s ` A = (cp s \ the_thread) ` A" +proof(rule f_image_eq) + fix a + assume "a \ A" + from assms[rule_format, OF this] + obtain th where eq_a: "a = Th th" by auto + show "cp_gen s a = (cp s \ the_thread) a" + by (unfold eq_a, simp, unfold cp_gen_def_cond[OF refl[of "Th th"]], simp) +qed + + +context valid_trace +begin + +lemma RAG_threads: + assumes "(Th th) \ Field (RAG s)" + shows "th \ threads s" + using assms + by (metis Field_def UnE dm_RAG_threads range_in vt) + +lemma subtree_tRAG_thread: + assumes "th \ threads s" + shows "subtree (tRAG s) (Th th) \ Th ` threads s" (is "?L \ ?R") +proof - + have "?L = {Th th' |th'. Th th' \ subtree (RAG s) (Th th)}" + by (unfold tRAG_subtree_eq, simp) + also have "... \ ?R" + proof + fix x + assume "x \ {Th th' |th'. Th th' \ subtree (RAG s) (Th th)}" + then obtain th' where h: "x = Th th'" "Th th' \ subtree (RAG s) (Th th)" by auto + from this(2) + show "x \ ?R" + proof(cases rule:subtreeE) + case 1 + thus ?thesis by (simp add: assms h(1)) + next + case 2 + thus ?thesis by (metis ancestors_Field dm_RAG_threads h(1) image_eqI) + qed + qed + finally show ?thesis . +qed + +lemma readys_root: + assumes "th \ readys s" + shows "root (RAG s) (Th th)" +proof - + { fix x + assume "x \ ancestors (RAG s) (Th th)" + hence h: "(Th th, x) \ (RAG s)^+" by (auto simp:ancestors_def) + from tranclD[OF this] + obtain z where "(Th th, z) \ RAG s" by auto + with assms(1) have False + apply (case_tac z, auto simp:readys_def s_RAG_def s_waiting_def cs_waiting_def) + by (fold wq_def, blast) + } thus ?thesis by (unfold root_def, auto) +qed + +lemma readys_in_no_subtree: + assumes "th \ readys s" + and "th' \ th" + shows "Th th \ subtree (RAG s) (Th th')" +proof + assume "Th th \ subtree (RAG s) (Th th')" + thus False + proof(cases rule:subtreeE) + case 1 + with assms show ?thesis by auto + next + case 2 + with readys_root[OF assms(1)] + show ?thesis by (auto simp:root_def) + qed +qed + +lemma not_in_thread_isolated: + assumes "th \ threads s" + shows "(Th th) \ Field (RAG s)" +proof + assume "(Th th) \ Field (RAG s)" + with dm_RAG_threads and range_in assms + show False by (unfold Field_def, blast) +qed + +lemma wf_RAG: "wf (RAG s)" +proof(rule finite_acyclic_wf) + from finite_RAG show "finite (RAG s)" . +next + from acyclic_RAG show "acyclic (RAG s)" . +qed + +lemma sgv_wRAG: "single_valued (wRAG s)" + using waiting_unique + by (unfold single_valued_def wRAG_def, auto) + +lemma sgv_hRAG: "single_valued (hRAG s)" + using holding_unique + by (unfold single_valued_def hRAG_def, auto) + +lemma sgv_tRAG: "single_valued (tRAG s)" + by (unfold tRAG_def, rule single_valued_relcomp, + insert sgv_wRAG sgv_hRAG, auto) + +lemma acyclic_tRAG: "acyclic (tRAG s)" +proof(unfold tRAG_def, rule acyclic_compose) + show "acyclic (RAG s)" using acyclic_RAG . +next + show "wRAG s \ RAG s" unfolding RAG_split by auto +next + show "hRAG s \ RAG s" unfolding RAG_split by auto +qed + +lemma sgv_RAG: "single_valued (RAG s)" + using unique_RAG by (auto simp:single_valued_def) + +lemma rtree_RAG: "rtree (RAG s)" + using sgv_RAG acyclic_RAG + by (unfold rtree_def rtree_axioms_def sgv_def, auto) + +end + +sublocale valid_trace < rtree_RAG: rtree "RAG s" +proof + show "single_valued (RAG s)" + apply (intro_locales) + by (unfold single_valued_def, + auto intro:unique_RAG) + + show "acyclic (RAG s)" + by (rule acyclic_RAG) +qed + +sublocale valid_trace < rtree_s: rtree "tRAG s" +proof(unfold_locales) + from sgv_tRAG show "single_valued (tRAG s)" . +next + from acyclic_tRAG show "acyclic (tRAG s)" . +qed + +sublocale valid_trace < fsbtRAGs : fsubtree "RAG s" +proof - + show "fsubtree (RAG s)" + proof(intro_locales) + show "fbranch (RAG s)" using finite_fbranchI[OF finite_RAG] . + next + show "fsubtree_axioms (RAG s)" + proof(unfold fsubtree_axioms_def) + from wf_RAG show "wf (RAG s)" . + qed + qed +qed + +sublocale valid_trace < fsbttRAGs: fsubtree "tRAG s" +proof - + have "fsubtree (tRAG s)" + proof - + have "fbranch (tRAG s)" + proof(unfold tRAG_def, rule fbranch_compose) + show "fbranch (wRAG s)" + proof(rule finite_fbranchI) + from finite_RAG show "finite (wRAG s)" + by (unfold RAG_split, auto) + qed + next + show "fbranch (hRAG s)" + proof(rule finite_fbranchI) + from finite_RAG + show "finite (hRAG s)" by (unfold RAG_split, auto) + qed + qed + moreover have "wf (tRAG s)" + proof(rule wf_subset) + show "wf (RAG s O RAG s)" using wf_RAG + by (fold wf_comp_self, simp) + next + show "tRAG s \ (RAG s O RAG s)" + by (unfold tRAG_alt_def, auto) + qed + ultimately show ?thesis + by (unfold fsubtree_def fsubtree_axioms_def,auto) + qed + from this[folded tRAG_def] show "fsubtree (tRAG s)" . +qed + +lemma Max_UNION: + assumes "finite A" + and "A \ {}" + and "\ M \ f ` A. finite M" + and "\ M \ f ` A. M \ {}" + shows "Max (\x\ A. f x) = Max (Max ` f ` A)" (is "?L = ?R") + using assms[simp] +proof - + have "?L = Max (\(f ` A))" + by (fold Union_image_eq, simp) + also have "... = ?R" + by (subst Max_Union, simp+) + finally show ?thesis . +qed + +lemma max_Max_eq: + assumes "finite A" + and "A \ {}" + and "x = y" + shows "max x (Max A) = Max ({y} \ A)" (is "?L = ?R") +proof - + have "?R = Max (insert y A)" by simp + also from assms have "... = ?L" + by (subst Max.insert, simp+) + finally show ?thesis by simp +qed + +context valid_trace +begin + +(* ddd *) +lemma cp_gen_rec: + assumes "x = Th th" + shows "cp_gen s x = Max ({the_preced s th} \ (cp_gen s) ` children (tRAG s) x)" +proof(cases "children (tRAG s) x = {}") + case True + show ?thesis + by (unfold True cp_gen_def subtree_children, simp add:assms) +next + case False + hence [simp]: "children (tRAG s) x \ {}" by auto + note fsbttRAGs.finite_subtree[simp] + have [simp]: "finite (children (tRAG s) x)" + by (intro rev_finite_subset[OF fsbttRAGs.finite_subtree], + rule children_subtree) + { fix r x + have "subtree r x \ {}" by (auto simp:subtree_def) + } note this[simp] + have [simp]: "\x\children (tRAG s) x. subtree (tRAG s) x \ {}" + proof - + from False obtain q where "q \ children (tRAG s) x" by blast + moreover have "subtree (tRAG s) q \ {}" by simp + ultimately show ?thesis by blast + qed + have h: "Max ((the_preced s \ the_thread) ` + ({x} \ \(subtree (tRAG s) ` children (tRAG s) x))) = + Max ({the_preced s th} \ cp_gen s ` children (tRAG s) x)" + (is "?L = ?R") + proof - + let "Max (?f ` (?A \ \ (?g ` ?B)))" = ?L + let "Max (_ \ (?h ` ?B))" = ?R + let ?L1 = "?f ` \(?g ` ?B)" + have eq_Max_L1: "Max ?L1 = Max (?h ` ?B)" + proof - + have "?L1 = ?f ` (\ x \ ?B.(?g x))" by simp + also have "... = (\ x \ ?B. ?f ` (?g x))" by auto + finally have "Max ?L1 = Max ..." by simp + also have "... = Max (Max ` (\x. ?f ` subtree (tRAG s) x) ` ?B)" + by (subst Max_UNION, simp+) + also have "... = Max (cp_gen s ` children (tRAG s) x)" + by (unfold image_comp cp_gen_alt_def, simp) + finally show ?thesis . + qed + show ?thesis + proof - + have "?L = Max (?f ` ?A \ ?L1)" by simp + also have "... = max (the_preced s (the_thread x)) (Max ?L1)" + by (subst Max_Un, simp+) + also have "... = max (?f x) (Max (?h ` ?B))" + by (unfold eq_Max_L1, simp) + also have "... =?R" + by (rule max_Max_eq, (simp)+, unfold assms, simp) + finally show ?thesis . + qed + qed thus ?thesis + by (fold h subtree_children, unfold cp_gen_def, simp) +qed + +lemma cp_rec: + "cp s th = Max ({the_preced s th} \ + (cp s o the_thread) ` children (tRAG s) (Th th))" +proof - + have "Th th = Th th" by simp + note h = cp_gen_def_cond[OF this] cp_gen_rec[OF this] + show ?thesis + proof - + have "cp_gen s ` children (tRAG s) (Th th) = + (cp s \ the_thread) ` children (tRAG s) (Th th)" + proof(rule cp_gen_over_set) + show " \x\children (tRAG s) (Th th). \th. x = Th th" + by (unfold tRAG_alt_def, auto simp:children_def) + qed + thus ?thesis by (subst (1) h(1), unfold h(2), simp) + qed +qed + +end + +(* keep *) +lemma next_th_holding: + assumes vt: "vt s" + and nxt: "next_th s th cs th'" + shows "holding (wq s) th cs" +proof - + from nxt[unfolded next_th_def] + obtain rest where h: "wq s cs = th # rest" + "rest \ []" + "th' = hd (SOME q. distinct q \ set q = set rest)" by auto + thus ?thesis + by (unfold cs_holding_def, auto) +qed + +context valid_trace +begin + +lemma next_th_waiting: + assumes nxt: "next_th s th cs th'" + shows "waiting (wq s) th' cs" +proof - + from nxt[unfolded next_th_def] + obtain rest where h: "wq s cs = th # rest" + "rest \ []" + "th' = hd (SOME q. distinct q \ set q = set rest)" by auto + from wq_distinct[of cs, unfolded h] + have dst: "distinct (th # rest)" . + have in_rest: "th' \ set rest" + proof(unfold h, rule someI2) + show "distinct rest \ set rest = set rest" using dst by auto + next + fix x assume "distinct x \ set x = set rest" + with h(2) + show "hd x \ set (rest)" by (cases x, auto) + qed + hence "th' \ set (wq s cs)" by (unfold h(1), auto) + moreover have "th' \ hd (wq s cs)" + by (unfold h(1), insert in_rest dst, auto) + ultimately show ?thesis by (auto simp:cs_waiting_def) +qed + +lemma next_th_RAG: + assumes nxt: "next_th (s::event list) th cs th'" + shows "{(Cs cs, Th th), (Th th', Cs cs)} \ RAG s" + using vt assms next_th_holding next_th_waiting + by (unfold s_RAG_def, simp) + +end + +-- {* A useless definition *} +definition cps:: "state \ (thread \ precedence) set" +where "cps s = {(th, cp s th) | th . th \ threads s}" + +lemma "wq (V th cs # s) cs1 = ttt" + apply (unfold wq_def, auto simp:Let_def) + +end + diff -r 17305a85493d -r c495eb16beb6 CpsG.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CpsG.thy Wed Jan 27 19:28:42 2016 +0800 @@ -0,0 +1,4565 @@ +theory CpsG +imports PIPDefs +begin + +lemma f_image_eq: + assumes h: "\ a. a \ A \ f a = g a" + shows "f ` A = g ` A" +proof + show "f ` A \ g ` A" + by(rule image_subsetI, auto intro:h) +next + show "g ` A \ f ` A" + by (rule image_subsetI, auto intro:h[symmetric]) +qed + +lemma Max_fg_mono: + assumes "finite A" + and "\ a \ A. f a \ g a" + shows "Max (f ` A) \ Max (g ` A)" +proof(cases "A = {}") + case True + thus ?thesis by auto +next + case False + show ?thesis + proof(rule Max.boundedI) + from assms show "finite (f ` A)" by auto + next + from False show "f ` A \ {}" by auto + next + fix fa + assume "fa \ f ` A" + then obtain a where h_fa: "a \ A" "fa = f a" by auto + show "fa \ Max (g ` A)" + proof(rule Max_ge_iff[THEN iffD2]) + from assms show "finite (g ` A)" by auto + next + from False show "g ` A \ {}" by auto + next + from h_fa have "g a \ g ` A" by auto + moreover have "fa \ g a" using h_fa assms(2) by auto + ultimately show "\a\g ` A. fa \ a" by auto + qed + qed +qed + +lemma Max_f_mono: + assumes seq: "A \ B" + and np: "A \ {}" + and fnt: "finite B" + shows "Max (f ` A) \ Max (f ` B)" +proof(rule Max_mono) + from seq show "f ` A \ f ` B" by auto +next + from np show "f ` A \ {}" by auto +next + from fnt and seq show "finite (f ` B)" by auto +qed + +lemma Max_UNION: + assumes "finite A" + and "A \ {}" + and "\ M \ f ` A. finite M" + and "\ M \ f ` A. M \ {}" + shows "Max (\x\ A. f x) = Max (Max ` f ` A)" (is "?L = ?R") + using assms[simp] +proof - + have "?L = Max (\(f ` A))" + by (fold Union_image_eq, simp) + also have "... = ?R" + by (subst Max_Union, simp+) + finally show ?thesis . +qed + +lemma max_Max_eq: + assumes "finite A" + and "A \ {}" + and "x = y" + shows "max x (Max A) = Max ({y} \ A)" (is "?L = ?R") +proof - + have "?R = Max (insert y A)" by simp + also from assms have "... = ?L" + by (subst Max.insert, simp+) + finally show ?thesis by simp +qed + +lemma eq_RAG: + "RAG (wq s) = RAG s" + by (unfold cs_RAG_def s_RAG_def, auto) + +lemma waiting_holding: + assumes "waiting (s::state) th cs" + obtains th' where "holding s th' cs" +proof - + from assms[unfolded s_waiting_def, folded wq_def] + obtain th' where "th' \ set (wq s cs)" "th' = hd (wq s cs)" + by (metis empty_iff hd_in_set list.set(1)) + hence "holding s th' cs" + by (unfold s_holding_def, fold wq_def, auto) + from that[OF this] show ?thesis . +qed + +lemma cp_eq_cpreced: "cp s th = cpreced (wq s) s th" +unfolding cp_def wq_def +apply(induct s rule: schs.induct) +apply(simp add: Let_def cpreced_initial) +apply(simp add: Let_def) +apply(simp add: Let_def) +apply(simp add: Let_def) +apply(subst (2) schs.simps) +apply(simp add: Let_def) +apply(subst (2) schs.simps) +apply(simp add: Let_def) +done + +lemma cp_alt_def: + "cp s th = + Max ((the_preced s) ` {th'. Th th' \ (subtree (RAG s) (Th th))})" +proof - + have "Max (the_preced s ` ({th} \ dependants (wq s) th)) = + Max (the_preced s ` {th'. Th th' \ subtree (RAG s) (Th th)})" + (is "Max (_ ` ?L) = Max (_ ` ?R)") + proof - + have "?L = ?R" + by (auto dest:rtranclD simp:cs_dependants_def cs_RAG_def s_RAG_def subtree_def) + thus ?thesis by simp + qed + thus ?thesis by (unfold cp_eq_cpreced cpreced_def, fold the_preced_def, simp) +qed + +(* ccc *) + + +locale valid_trace = + fixes s + assumes vt : "vt s" + +locale valid_trace_e = valid_trace + + fixes e + assumes vt_e: "vt (e#s)" +begin + +lemma pip_e: "PIP s e" + using vt_e by (cases, simp) + +end + +locale valid_trace_create = valid_trace_e + + fixes th prio + assumes is_create: "e = Create th prio" + +locale valid_trace_exit = valid_trace_e + + fixes th + assumes is_exit: "e = Exit th" + +locale valid_trace_p = valid_trace_e + + fixes th cs + assumes is_p: "e = P th cs" + +locale valid_trace_v = valid_trace_e + + fixes th cs + assumes is_v: "e = V th cs" +begin + definition "rest = tl (wq s cs)" + definition "wq' = (SOME q. distinct q \ set q = set rest)" +end + +locale valid_trace_v_n = valid_trace_v + + assumes rest_nnl: "rest \ []" + +locale valid_trace_v_e = valid_trace_v + + assumes rest_nil: "rest = []" + +locale valid_trace_set= valid_trace_e + + fixes th prio + assumes is_set: "e = Set th prio" + +context valid_trace +begin + +lemma ind [consumes 0, case_names Nil Cons, induct type]: + assumes "PP []" + and "(\s e. valid_trace_e s e \ + PP s \ PIP s e \ PP (e # s))" + shows "PP s" +proof(induct rule:vt.induct[OF vt, case_names Init Step]) + case Init + from assms(1) show ?case . +next + case (Step s e) + show ?case + proof(rule assms(2)) + show "valid_trace_e s e" using Step by (unfold_locales, auto) + next + show "PP s" using Step by simp + next + show "PIP s e" using Step by simp + qed +qed + +lemma vt_moment: "\ t. vt (moment t s)" +proof(induct rule:ind) + case Nil + thus ?case by (simp add:vt_nil) +next + case (Cons s e t) + show ?case + proof(cases "t \ length (e#s)") + case True + from True have "moment t (e#s) = e#s" by simp + thus ?thesis using Cons + by (simp add:valid_trace_def valid_trace_e_def, auto) + next + case False + from Cons have "vt (moment t s)" by simp + moreover have "moment t (e#s) = moment t s" + proof - + from False have "t \ length s" by simp + from moment_app [OF this, of "[e]"] + show ?thesis by simp + qed + ultimately show ?thesis by simp + qed +qed + +lemma finite_threads: + shows "finite (threads s)" +using vt by (induct) (auto elim: step.cases) + +end + +lemma RAG_target_th: "(Th th, x) \ RAG (s::state) \ \ cs. x = Cs cs" + by (unfold s_RAG_def, auto) + +locale valid_moment = valid_trace + + fixes i :: nat + +sublocale valid_moment < vat_moment: valid_trace "(moment i s)" + by (unfold_locales, insert vt_moment, auto) + +lemma waiting_eq: "waiting s th cs = waiting (wq s) th cs" + by (unfold s_waiting_def cs_waiting_def wq_def, auto) + +lemma holding_eq: "holding (s::state) th cs = holding (wq s) th cs" + by (unfold s_holding_def wq_def cs_holding_def, simp) + +lemma runing_ready: + shows "runing s \ readys s" + unfolding runing_def readys_def + by auto + +lemma readys_threads: + shows "readys s \ threads s" + unfolding readys_def + by auto + +lemma wq_v_neq [simp]: + "cs \ cs' \ wq (V thread cs#s) cs' = wq s cs'" + by (auto simp:wq_def Let_def cp_def split:list.splits) + +lemma runing_head: + assumes "th \ runing s" + and "th \ set (wq_fun (schs s) cs)" + shows "th = hd (wq_fun (schs s) cs)" + using assms + by (simp add:runing_def readys_def s_waiting_def wq_def) + +context valid_trace +begin + +lemma runing_wqE: + assumes "th \ runing s" + and "th \ set (wq s cs)" + obtains rest where "wq s cs = th#rest" +proof - + from assms(2) obtain th' rest where eq_wq: "wq s cs = th'#rest" + by (meson list.set_cases) + have "th' = th" + proof(rule ccontr) + assume "th' \ th" + hence "th \ hd (wq s cs)" using eq_wq by auto + with assms(2) + have "waiting s th cs" + by (unfold s_waiting_def, fold wq_def, auto) + with assms show False + by (unfold runing_def readys_def, auto) + qed + with eq_wq that show ?thesis by metis +qed + +end + +context valid_trace_create +begin + +lemma wq_neq_simp [simp]: + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_create wq_def + by (auto simp:Let_def) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" + using assms by simp +end + +context valid_trace_exit +begin + +lemma wq_neq_simp [simp]: + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_exit wq_def + by (auto simp:Let_def) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" + using assms by simp +end + +context valid_trace_p +begin + +lemma wq_neq_simp [simp]: + assumes "cs' \ cs" + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_p wq_def + by (auto simp:Let_def) + +lemma runing_th_s: + shows "th \ runing s" +proof - + from pip_e[unfolded is_p] + show ?thesis by (cases, simp) +qed + +lemma ready_th_s: "th \ readys s" + using runing_th_s + by (unfold runing_def, auto) + +lemma live_th_s: "th \ threads s" + using readys_threads ready_th_s by auto + +lemma live_th_es: "th \ threads (e#s)" + using live_th_s + by (unfold is_p, simp) + +lemma th_not_waiting: + "\ waiting s th c" +proof - + have "th \ readys s" + using runing_ready runing_th_s by blast + thus ?thesis + by (unfold readys_def, auto) +qed + +lemma waiting_neq_th: + assumes "waiting s t c" + shows "t \ th" + using assms using th_not_waiting by blast + +lemma th_not_in_wq: + shows "th \ set (wq s cs)" +proof + assume otherwise: "th \ set (wq s cs)" + from runing_wqE[OF runing_th_s this] + obtain rest where eq_wq: "wq s cs = th#rest" by blast + with otherwise + have "holding s th cs" + by (unfold s_holding_def, fold wq_def, simp) + hence cs_th_RAG: "(Cs cs, Th th) \ RAG s" + by (unfold s_RAG_def, fold holding_eq, auto) + from pip_e[unfolded is_p] + show False + proof(cases) + case (thread_P) + with cs_th_RAG show ?thesis by auto + qed +qed + +lemma wq_es_cs: + "wq (e#s) cs = wq s cs @ [th]" + by (unfold is_p wq_def, auto simp:Let_def) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" +proof(cases "cs' = cs") + case True + show ?thesis using True assms th_not_in_wq + by (unfold True wq_es_cs, auto) +qed (insert assms, simp) + +end + +context valid_trace_v +begin + +lemma wq_neq_simp [simp]: + assumes "cs' \ cs" + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_v wq_def + by (auto simp:Let_def) + +lemma runing_th_s: + shows "th \ runing s" +proof - + from pip_e[unfolded is_v] + show ?thesis by (cases, simp) +qed + +lemma th_not_waiting: + "\ waiting s th c" +proof - + have "th \ readys s" + using runing_ready runing_th_s by blast + thus ?thesis + by (unfold readys_def, auto) +qed + +lemma waiting_neq_th: + assumes "waiting s t c" + shows "t \ th" + using assms using th_not_waiting by blast + +lemma wq_s_cs: + "wq s cs = th#rest" +proof - + from pip_e[unfolded is_v] + show ?thesis + proof(cases) + case (thread_V) + from this(2) show ?thesis + by (unfold rest_def s_holding_def, fold wq_def, + metis empty_iff list.collapse list.set(1)) + qed +qed + +lemma wq_es_cs: + "wq (e#s) cs = wq'" + using wq_s_cs[unfolded wq_def] + by (auto simp:Let_def wq_def rest_def wq'_def is_v, simp) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" +proof(cases "cs' = cs") + case True + show ?thesis + proof(unfold True wq_es_cs wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + using assms[unfolded True wq_s_cs] by auto + qed simp +qed (insert assms, simp) + +end + +context valid_trace_set +begin + +lemma wq_neq_simp [simp]: + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_set wq_def + by (auto simp:Let_def) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" + using assms by simp +end + +context valid_trace +begin + +lemma actor_inv: + assumes "PIP s e" + and "\ isCreate e" + shows "actor e \ runing s" + using assms + by (induct, auto) + +lemma isP_E: + assumes "isP e" + obtains cs where "e = P (actor e) cs" + using assms by (cases e, auto) + +lemma isV_E: + assumes "isV e" + obtains cs where "e = V (actor e) cs" + using assms by (cases e, auto) + +lemma wq_distinct: "distinct (wq s cs)" +proof(induct rule:ind) + case (Cons s e) + interpret vt_e: valid_trace_e s e using Cons by simp + show ?case + proof(cases e) + case (Create th prio) + interpret vt_create: valid_trace_create s e th prio + using Create by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_create.wq_distinct_kept) + next + case (Exit th) + interpret vt_exit: valid_trace_exit s e th + using Exit by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_exit.wq_distinct_kept) + next + case (P th cs) + interpret vt_p: valid_trace_p s e th cs using P by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_p.wq_distinct_kept) + next + case (V th cs) + interpret vt_v: valid_trace_v s e th cs using V by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_v.wq_distinct_kept) + next + case (Set th prio) + interpret vt_set: valid_trace_set s e th prio + using Set by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_set.wq_distinct_kept) + qed +qed (unfold wq_def Let_def, simp) + +end + +context valid_trace_e +begin + +text {* + The following lemma shows that only the @{text "P"} + operation can add new thread into waiting queues. + Such kind of lemmas are very obvious, but need to be checked formally. + This is a kind of confirmation that our modelling is correct. +*} + +lemma wq_in_inv: + assumes s_ni: "thread \ set (wq s cs)" + and s_i: "thread \ set (wq (e#s) cs)" + shows "e = P thread cs" +proof(cases e) + -- {* This is the only non-trivial case: *} + case (V th cs1) + have False + proof(cases "cs1 = cs") + case True + show ?thesis + proof(cases "(wq s cs1)") + case (Cons w_hd w_tl) + have "set (wq (e#s) cs) \ set (wq s cs)" + proof - + have "(wq (e#s) cs) = (SOME q. distinct q \ set q = set w_tl)" + using Cons V by (auto simp:wq_def Let_def True split:if_splits) + moreover have "set ... \ set (wq s cs)" + proof(rule someI2) + show "distinct w_tl \ set w_tl = set w_tl" + by (metis distinct.simps(2) local.Cons wq_distinct) + qed (insert Cons True, auto) + ultimately show ?thesis by simp + qed + with assms show ?thesis by auto + qed (insert assms V True, auto simp:wq_def Let_def split:if_splits) + qed (insert assms V, auto simp:wq_def Let_def split:if_splits) + thus ?thesis by auto +qed (insert assms, auto simp:wq_def Let_def split:if_splits) + +lemma wq_out_inv: + assumes s_in: "thread \ set (wq s cs)" + and s_hd: "thread = hd (wq s cs)" + and s_i: "thread \ hd (wq (e#s) cs)" + shows "e = V thread cs" +proof(cases e) +-- {* There are only two non-trivial cases: *} + case (V th cs1) + show ?thesis + proof(cases "cs1 = cs") + case True + have "PIP s (V th cs)" using pip_e[unfolded V[unfolded True]] . + thus ?thesis + proof(cases) + case (thread_V) + moreover have "th = thread" using thread_V(2) s_hd + by (unfold s_holding_def wq_def, simp) + ultimately show ?thesis using V True by simp + qed + qed (insert assms V, auto simp:wq_def Let_def split:if_splits) +next + case (P th cs1) + show ?thesis + proof(cases "cs1 = cs") + case True + with P have "wq (e#s) cs = wq_fun (schs s) cs @ [th]" + by (auto simp:wq_def Let_def split:if_splits) + with s_i s_hd s_in have False + by (metis empty_iff hd_append2 list.set(1) wq_def) + thus ?thesis by simp + qed (insert assms P, auto simp:wq_def Let_def split:if_splits) +qed (insert assms, auto simp:wq_def Let_def split:if_splits) + +end + + +context valid_trace +begin + + +text {* (* ddd *) + The nature of the work is like this: since it starts from a very simple and basic + model, even intuitively very `basic` and `obvious` properties need to derived from scratch. + For instance, the fact + that one thread can not be blocked by two critical resources at the same time + is obvious, because only running threads can make new requests, if one is waiting for + a critical resource and get blocked, it can not make another resource request and get + blocked the second time (because it is not running). + + To derive this fact, one needs to prove by contraction and + reason about time (or @{text "moement"}). The reasoning is based on a generic theorem + named @{text "p_split"}, which is about status changing along the time axis. It says if + a condition @{text "Q"} is @{text "True"} at a state @{text "s"}, + but it was @{text "False"} at the very beginning, then there must exits a moment @{text "t"} + in the history of @{text "s"} (notice that @{text "s"} itself is essentially the history + of events leading to it), such that @{text "Q"} switched + from being @{text "False"} to @{text "True"} and kept being @{text "True"} + till the last moment of @{text "s"}. + + Suppose a thread @{text "th"} is blocked + on @{text "cs1"} and @{text "cs2"} in some state @{text "s"}, + since no thread is blocked at the very beginning, by applying + @{text "p_split"} to these two blocking facts, there exist + two moments @{text "t1"} and @{text "t2"} in @{text "s"}, such that + @{text "th"} got blocked on @{text "cs1"} and @{text "cs2"} + and kept on blocked on them respectively ever since. + + Without lost of generality, we assume @{text "t1"} is earlier than @{text "t2"}. + However, since @{text "th"} was blocked ever since memonent @{text "t1"}, so it was still + in blocked state at moment @{text "t2"} and could not + make any request and get blocked the second time: Contradiction. +*} + +lemma waiting_unique_pre: (* ddd *) + assumes h11: "thread \ set (wq s cs1)" + and h12: "thread \ hd (wq s cs1)" + assumes h21: "thread \ set (wq s cs2)" + and h22: "thread \ hd (wq s cs2)" + and neq12: "cs1 \ cs2" + shows "False" +proof - + let "?Q" = "\ cs s. thread \ set (wq s cs) \ thread \ hd (wq s cs)" + from h11 and h12 have q1: "?Q cs1 s" by simp + from h21 and h22 have q2: "?Q cs2 s" by simp + have nq1: "\ ?Q cs1 []" by (simp add:wq_def) + have nq2: "\ ?Q cs2 []" by (simp add:wq_def) + from p_split [of "?Q cs1", OF q1 nq1] + obtain t1 where lt1: "t1 < length s" + and np1: "\ ?Q cs1 (moment t1 s)" + and nn1: "(\i'>t1. ?Q cs1 (moment i' s))" by auto + from p_split [of "?Q cs2", OF q2 nq2] + obtain t2 where lt2: "t2 < length s" + and np2: "\ ?Q cs2 (moment t2 s)" + and nn2: "(\i'>t2. ?Q cs2 (moment i' s))" by auto + { fix s cs + assume q: "?Q cs s" + have "thread \ runing s" + proof + assume "thread \ runing s" + hence " \cs. \ (thread \ set (wq_fun (schs s) cs) \ + thread \ hd (wq_fun (schs s) cs))" + by (unfold runing_def s_waiting_def readys_def, auto) + from this[rule_format, of cs] q + show False by (simp add: wq_def) + qed + } note q_not_runing = this + { fix t1 t2 cs1 cs2 + assume lt1: "t1 < length s" + and np1: "\ ?Q cs1 (moment t1 s)" + and nn1: "(\i'>t1. ?Q cs1 (moment i' s))" + and lt2: "t2 < length s" + and np2: "\ ?Q cs2 (moment t2 s)" + and nn2: "(\i'>t2. ?Q cs2 (moment i' s))" + and lt12: "t1 < t2" + let ?t3 = "Suc t2" + from lt2 have le_t3: "?t3 \ length s" by auto + from moment_plus [OF this] + obtain e where eq_m: "moment ?t3 s = e#moment t2 s" by auto + have "t2 < ?t3" by simp + from nn2 [rule_format, OF this] and eq_m + have h1: "thread \ set (wq (e#moment t2 s) cs2)" and + h2: "thread \ hd (wq (e#moment t2 s) cs2)" by auto + have "vt (e#moment t2 s)" + proof - + from vt_moment + have "vt (moment ?t3 s)" . + with eq_m show ?thesis by simp + qed + then interpret vt_e: valid_trace_e "moment t2 s" "e" + by (unfold_locales, auto, cases, simp) + have ?thesis + proof - + have "thread \ runing (moment t2 s)" + proof(cases "thread \ set (wq (moment t2 s) cs2)") + case True + have "e = V thread cs2" + proof - + have eq_th: "thread = hd (wq (moment t2 s) cs2)" + using True and np2 by auto + from vt_e.wq_out_inv[OF True this h2] + show ?thesis . + qed + thus ?thesis using vt_e.actor_inv[OF vt_e.pip_e] by auto + next + case False + have "e = P thread cs2" using vt_e.wq_in_inv[OF False h1] . + with vt_e.actor_inv[OF vt_e.pip_e] + show ?thesis by auto + qed + moreover have "thread \ runing (moment t2 s)" + by (rule q_not_runing[OF nn1[rule_format, OF lt12]]) + ultimately show ?thesis by simp + qed + } note lt_case = this + show ?thesis + proof - + { assume "t1 < t2" + from lt_case[OF lt1 np1 nn1 lt2 np2 nn2 this] + have ?thesis . + } moreover { + assume "t2 < t1" + from lt_case[OF lt2 np2 nn2 lt1 np1 nn1 this] + have ?thesis . + } moreover { + assume eq_12: "t1 = t2" + let ?t3 = "Suc t2" + from lt2 have le_t3: "?t3 \ length s" by auto + from moment_plus [OF this] + obtain e where eq_m: "moment ?t3 s = e#moment t2 s" by auto + have lt_2: "t2 < ?t3" by simp + from nn2 [rule_format, OF this] and eq_m + have h1: "thread \ set (wq (e#moment t2 s) cs2)" and + h2: "thread \ hd (wq (e#moment t2 s) cs2)" by auto + from nn1[rule_format, OF lt_2[folded eq_12]] eq_m[folded eq_12] + have g1: "thread \ set (wq (e#moment t1 s) cs1)" and + g2: "thread \ hd (wq (e#moment t1 s) cs1)" by auto + have "vt (e#moment t2 s)" + proof - + from vt_moment + have "vt (moment ?t3 s)" . + with eq_m show ?thesis by simp + qed + then interpret vt_e: valid_trace_e "moment t2 s" "e" + by (unfold_locales, auto, cases, simp) + have "e = V thread cs2 \ e = P thread cs2" + proof(cases "thread \ set (wq (moment t2 s) cs2)") + case True + have "e = V thread cs2" + proof - + have eq_th: "thread = hd (wq (moment t2 s) cs2)" + using True and np2 by auto + from vt_e.wq_out_inv[OF True this h2] + show ?thesis . + qed + thus ?thesis by auto + next + case False + have "e = P thread cs2" using vt_e.wq_in_inv[OF False h1] . + thus ?thesis by auto + qed + moreover have "e = V thread cs1 \ e = P thread cs1" + proof(cases "thread \ set (wq (moment t1 s) cs1)") + case True + have eq_th: "thread = hd (wq (moment t1 s) cs1)" + using True and np1 by auto + from vt_e.wq_out_inv[folded eq_12, OF True this g2] + have "e = V thread cs1" . + thus ?thesis by auto + next + case False + have "e = P thread cs1" using vt_e.wq_in_inv[folded eq_12, OF False g1] . + thus ?thesis by auto + qed + ultimately have ?thesis using neq12 by auto + } ultimately show ?thesis using nat_neq_iff by blast + qed +qed + +text {* + This lemma is a simple corrolary of @{text "waiting_unique_pre"}. +*} + +lemma waiting_unique: + assumes "waiting s th cs1" + and "waiting s th cs2" + shows "cs1 = cs2" + using waiting_unique_pre assms + unfolding wq_def s_waiting_def + by auto + +end + +(* not used *) +text {* + Every thread can only be blocked on one critical resource, + symmetrically, every critical resource can only be held by one thread. + This fact is much more easier according to our definition. +*} +lemma held_unique: + assumes "holding (s::event list) th1 cs" + and "holding s th2 cs" + shows "th1 = th2" + by (insert assms, unfold s_holding_def, auto) + +lemma last_set_lt: "th \ threads s \ last_set th s < length s" + apply (induct s, auto) + by (case_tac a, auto split:if_splits) + +lemma last_set_unique: + "\last_set th1 s = last_set th2 s; th1 \ threads s; th2 \ threads s\ + \ th1 = th2" + apply (induct s, auto) + by (case_tac a, auto split:if_splits dest:last_set_lt) + +lemma preced_unique : + assumes pcd_eq: "preced th1 s = preced th2 s" + and th_in1: "th1 \ threads s" + and th_in2: " th2 \ threads s" + shows "th1 = th2" +proof - + from pcd_eq have "last_set th1 s = last_set th2 s" by (simp add:preced_def) + from last_set_unique [OF this th_in1 th_in2] + show ?thesis . +qed + +lemma preced_linorder: + assumes neq_12: "th1 \ th2" + and th_in1: "th1 \ threads s" + and th_in2: " th2 \ threads s" + shows "preced th1 s < preced th2 s \ preced th1 s > preced th2 s" +proof - + from preced_unique [OF _ th_in1 th_in2] and neq_12 + have "preced th1 s \ preced th2 s" by auto + thus ?thesis by auto +qed + +text {* + The following three lemmas show that @{text "RAG"} does not change + by the happening of @{text "Set"}, @{text "Create"} and @{text "Exit"} + events, respectively. +*} + +lemma RAG_set_unchanged: "(RAG (Set th prio # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + +lemma (in valid_trace_set) + RAG_unchanged: "(RAG (e # s)) = RAG s" + by (unfold is_set RAG_set_unchanged, simp) + +lemma RAG_create_unchanged: "(RAG (Create th prio # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + +lemma (in valid_trace_create) + RAG_unchanged: "(RAG (e # s)) = RAG s" + by (unfold is_create RAG_create_unchanged, simp) + +lemma RAG_exit_unchanged: "(RAG (Exit th # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + +lemma (in valid_trace_exit) + RAG_unchanged: "(RAG (e # s)) = RAG s" + by (unfold is_exit RAG_exit_unchanged, simp) + +context valid_trace_v +begin + +lemma distinct_rest: "distinct rest" + by (simp add: distinct_tl rest_def wq_distinct) + +lemma holding_cs_eq_th: + assumes "holding s t cs" + shows "t = th" +proof - + from pip_e[unfolded is_v] + show ?thesis + proof(cases) + case (thread_V) + from held_unique[OF this(2) assms] + show ?thesis by simp + qed +qed + +lemma distinct_wq': "distinct wq'" + by (metis (mono_tags, lifting) distinct_rest some_eq_ex wq'_def) + +lemma set_wq': "set wq' = set rest" + by (metis (mono_tags, lifting) distinct_rest rest_def + some_eq_ex wq'_def) + +lemma th'_in_inv: + assumes "th' \ set wq'" + shows "th' \ set rest" + using assms set_wq' by simp + +lemma neq_t_th: + assumes "waiting (e#s) t c" + shows "t \ th" +proof + assume otherwise: "t = th" + show False + proof(cases "c = cs") + case True + have "t \ set wq'" + using assms[unfolded True s_waiting_def, folded wq_def, unfolded wq_es_cs] + by simp + from th'_in_inv[OF this] have "t \ set rest" . + with wq_s_cs[folded otherwise] wq_distinct[of cs] + show ?thesis by simp + next + case False + have "wq (e#s) c = wq s c" using False + by (unfold is_v, simp) + hence "waiting s t c" using assms + by (simp add: cs_waiting_def waiting_eq) + hence "t \ readys s" by (unfold readys_def, auto) + hence "t \ runing s" using runing_ready by auto + with runing_th_s[folded otherwise] show ?thesis by auto + qed +qed + +lemma waiting_esI1: + assumes "waiting s t c" + and "c \ cs" + shows "waiting (e#s) t c" +proof - + have "wq (e#s) c = wq s c" + using assms(2) is_v by auto + with assms(1) show ?thesis + using cs_waiting_def waiting_eq by auto +qed + +lemma holding_esI2: + assumes "c \ cs" + and "holding s t c" + shows "holding (e#s) t c" +proof - + from assms(1) have "wq (e#s) c = wq s c" using is_v by auto + from assms(2)[unfolded s_holding_def, folded wq_def, + folded this, unfolded wq_def, folded s_holding_def] + show ?thesis . +qed + +lemma holding_esI1: + assumes "holding s t c" + and "t \ th" + shows "holding (e#s) t c" +proof - + have "c \ cs" using assms using holding_cs_eq_th by blast + from holding_esI2[OF this assms(1)] + show ?thesis . +qed + +end + +context valid_trace_v_n +begin + +lemma neq_wq': "wq' \ []" +proof (unfold wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) +next + fix x + assume " distinct x \ set x = set rest" + thus "x \ []" using rest_nnl by auto +qed + +definition "taker = hd wq'" + +definition "rest' = tl wq'" + +lemma eq_wq': "wq' = taker # rest'" + by (simp add: neq_wq' rest'_def taker_def) + +lemma next_th_taker: + shows "next_th s th cs taker" + using rest_nnl taker_def wq'_def wq_s_cs + by (auto simp:next_th_def) + +lemma taker_unique: + assumes "next_th s th cs taker'" + shows "taker' = taker" +proof - + from assms + obtain rest' where + h: "wq s cs = th # rest'" + "taker' = hd (SOME q. distinct q \ set q = set rest')" + by (unfold next_th_def, auto) + with wq_s_cs have "rest' = rest" by auto + thus ?thesis using h(2) taker_def wq'_def by auto +qed + +lemma waiting_set_eq: + "{(Th th', Cs cs) |th'. next_th s th cs th'} = {(Th taker, Cs cs)}" + by (smt all_not_in_conv bot.extremum insertI1 insert_subset + mem_Collect_eq next_th_taker subsetI subset_antisym taker_def taker_unique) + +lemma holding_set_eq: + "{(Cs cs, Th th') |th'. next_th s th cs th'} = {(Cs cs, Th taker)}" + using next_th_taker taker_def waiting_set_eq + by fastforce + +lemma holding_taker: + shows "holding (e#s) taker cs" + by (unfold s_holding_def, fold wq_def, unfold wq_es_cs, + auto simp:neq_wq' taker_def) + +lemma waiting_esI2: + assumes "waiting s t cs" + and "t \ taker" + shows "waiting (e#s) t cs" +proof - + have "t \ set wq'" + proof(unfold wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) + next + fix x + assume "distinct x \ set x = set rest" + moreover have "t \ set rest" + using assms(1) cs_waiting_def waiting_eq wq_s_cs by auto + ultimately show "t \ set x" by simp + qed + moreover have "t \ hd wq'" + using assms(2) taker_def by auto + ultimately show ?thesis + by (unfold s_waiting_def, fold wq_def, unfold wq_es_cs, simp) +qed + +lemma waiting_esE: + assumes "waiting (e#s) t c" + obtains "c \ cs" "waiting s t c" + | "c = cs" "t \ taker" "waiting s t cs" "t \ set rest'" +proof(cases "c = cs") + case False + hence "wq (e#s) c = wq s c" using is_v by auto + with assms have "waiting s t c" using cs_waiting_def waiting_eq by auto + from that(1)[OF False this] show ?thesis . +next + case True + from assms[unfolded s_waiting_def True, folded wq_def, unfolded wq_es_cs] + have "t \ hd wq'" "t \ set wq'" by auto + hence "t \ taker" by (simp add: taker_def) + moreover hence "t \ th" using assms neq_t_th by blast + moreover have "t \ set rest" by (simp add: `t \ set wq'` th'_in_inv) + ultimately have "waiting s t cs" + by (metis cs_waiting_def list.distinct(2) list.sel(1) + list.set_sel(2) rest_def waiting_eq wq_s_cs) + show ?thesis using that(2) + using True `t \ set wq'` `t \ taker` `waiting s t cs` eq_wq' by auto +qed + +lemma holding_esI1: + assumes "c = cs" + and "t = taker" + shows "holding (e#s) t c" + by (unfold assms, simp add: holding_taker) + +lemma holding_esE: + assumes "holding (e#s) t c" + obtains "c = cs" "t = taker" + | "c \ cs" "holding s t c" +proof(cases "c = cs") + case True + from assms[unfolded True, unfolded s_holding_def, + folded wq_def, unfolded wq_es_cs] + have "t = taker" by (simp add: taker_def) + from that(1)[OF True this] show ?thesis . +next + case False + hence "wq (e#s) c = wq s c" using is_v by auto + from assms[unfolded s_holding_def, folded wq_def, + unfolded this, unfolded wq_def, folded s_holding_def] + have "holding s t c" . + from that(2)[OF False this] show ?thesis . +qed + +end + + +context valid_trace_v_e +begin + +lemma nil_wq': "wq' = []" +proof (unfold wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) +next + fix x + assume " distinct x \ set x = set rest" + thus "x = []" using rest_nil by auto +qed + +lemma no_taker: + assumes "next_th s th cs taker" + shows "False" +proof - + from assms[unfolded next_th_def] + obtain rest' where "wq s cs = th # rest'" "rest' \ []" + by auto + thus ?thesis using rest_def rest_nil by auto +qed + +lemma waiting_set_eq: + "{(Th th', Cs cs) |th'. next_th s th cs th'} = {}" + using no_taker by auto + +lemma holding_set_eq: + "{(Cs cs, Th th') |th'. next_th s th cs th'} = {}" + using no_taker by auto + +lemma no_holding: + assumes "holding (e#s) taker cs" + shows False +proof - + from wq_es_cs[unfolded nil_wq'] + have " wq (e # s) cs = []" . + from assms[unfolded s_holding_def, folded wq_def, unfolded this] + show ?thesis by auto +qed + +lemma no_waiting: + assumes "waiting (e#s) t cs" + shows False +proof - + from wq_es_cs[unfolded nil_wq'] + have " wq (e # s) cs = []" . + from assms[unfolded s_waiting_def, folded wq_def, unfolded this] + show ?thesis by auto +qed + +lemma waiting_esI2: + assumes "waiting s t c" + shows "waiting (e#s) t c" +proof - + have "c \ cs" using assms + using cs_waiting_def rest_nil waiting_eq wq_s_cs by auto + from waiting_esI1[OF assms this] + show ?thesis . +qed + +lemma waiting_esE: + assumes "waiting (e#s) t c" + obtains "c \ cs" "waiting s t c" +proof(cases "c = cs") + case False + hence "wq (e#s) c = wq s c" using is_v by auto + with assms have "waiting s t c" using cs_waiting_def waiting_eq by auto + from that(1)[OF False this] show ?thesis . +next + case True + from no_waiting[OF assms[unfolded True]] + show ?thesis by auto +qed + +lemma holding_esE: + assumes "holding (e#s) t c" + obtains "c \ cs" "holding s t c" +proof(cases "c = cs") + case True + from no_holding[OF assms[unfolded True]] + show ?thesis by auto +next + case False + hence "wq (e#s) c = wq s c" using is_v by auto + from assms[unfolded s_holding_def, folded wq_def, + unfolded this, unfolded wq_def, folded s_holding_def] + have "holding s t c" . + from that[OF False this] show ?thesis . +qed + +end + +lemma rel_eqI: + assumes "\ x y. (x,y) \ A \ (x,y) \ B" + and "\ x y. (x,y) \ B \ (x, y) \ A" + shows "A = B" + using assms by auto + +lemma in_RAG_E: + assumes "(n1, n2) \ RAG (s::state)" + obtains (waiting) th cs where "n1 = Th th" "n2 = Cs cs" "waiting s th cs" + | (holding) th cs where "n1 = Cs cs" "n2 = Th th" "holding s th cs" + using assms[unfolded s_RAG_def, folded waiting_eq holding_eq] + by auto + +context valid_trace_v +begin + +lemma RAG_es: + "RAG (e # s) = + RAG s - {(Cs cs, Th th)} - + {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'}" (is "?L = ?R") +proof(rule rel_eqI) + fix n1 n2 + assume "(n1, n2) \ ?L" + thus "(n1, n2) \ ?R" + proof(cases rule:in_RAG_E) + case (waiting th' cs') + show ?thesis + proof(cases "rest = []") + case False + interpret h_n: valid_trace_v_n s e th cs + by (unfold_locales, insert False, simp) + from waiting(3) + show ?thesis + proof(cases rule:h_n.waiting_esE) + case 1 + with waiting(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + next + case 2 + with waiting(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + qed + next + case True + interpret h_e: valid_trace_v_e s e th cs + by (unfold_locales, insert True, simp) + from waiting(3) + show ?thesis + proof(cases rule:h_e.waiting_esE) + case 1 + with waiting(1,2) + show ?thesis + by (unfold h_e.waiting_set_eq h_e.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + qed + qed + next + case (holding th' cs') + show ?thesis + proof(cases "rest = []") + case False + interpret h_n: valid_trace_v_n s e th cs + by (unfold_locales, insert False, simp) + from holding(3) + show ?thesis + proof(cases rule:h_n.holding_esE) + case 1 + with holding(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + next + case 2 + with holding(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold holding_eq, auto) + qed + next + case True + interpret h_e: valid_trace_v_e s e th cs + by (unfold_locales, insert True, simp) + from holding(3) + show ?thesis + proof(cases rule:h_e.holding_esE) + case 1 + with holding(1,2) + show ?thesis + by (unfold h_e.waiting_set_eq h_e.holding_set_eq s_RAG_def, + fold holding_eq, auto) + qed + qed + qed +next + fix n1 n2 + assume h: "(n1, n2) \ ?R" + show "(n1, n2) \ ?L" + proof(cases "rest = []") + case False + interpret h_n: valid_trace_v_n s e th cs + by (unfold_locales, insert False, simp) + from h[unfolded h_n.waiting_set_eq h_n.holding_set_eq] + have "((n1, n2) \ RAG s \ (n1 \ Cs cs \ n2 \ Th th) + \ (n1 \ Th h_n.taker \ n2 \ Cs cs)) \ + (n2 = Th h_n.taker \ n1 = Cs cs)" + by auto + thus ?thesis + proof + assume "n2 = Th h_n.taker \ n1 = Cs cs" + with h_n.holding_taker + show ?thesis + by (unfold s_RAG_def, fold holding_eq, auto) + next + assume h: "(n1, n2) \ RAG s \ + (n1 \ Cs cs \ n2 \ Th th) \ (n1 \ Th h_n.taker \ n2 \ Cs cs)" + hence "(n1, n2) \ RAG s" by simp + thus ?thesis + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from h and this(1,2) + have "th' \ h_n.taker \ cs' \ cs" by auto + hence "waiting (e#s) th' cs'" + proof + assume "cs' \ cs" + from waiting_esI1[OF waiting(3) this] + show ?thesis . + next + assume neq_th': "th' \ h_n.taker" + show ?thesis + proof(cases "cs' = cs") + case False + from waiting_esI1[OF waiting(3) this] + show ?thesis . + next + case True + from h_n.waiting_esI2[OF waiting(3)[unfolded True] neq_th', folded True] + show ?thesis . + qed + qed + thus ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case (holding th' cs') + from h this(1,2) + have "cs' \ cs \ th' \ th" by auto + hence "holding (e#s) th' cs'" + proof + assume "cs' \ cs" + from holding_esI2[OF this holding(3)] + show ?thesis . + next + assume "th' \ th" + from holding_esI1[OF holding(3) this] + show ?thesis . + qed + thus ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + qed + qed + next + case True + interpret h_e: valid_trace_v_e s e th cs + by (unfold_locales, insert True, simp) + from h[unfolded h_e.waiting_set_eq h_e.holding_set_eq] + have h_s: "(n1, n2) \ RAG s" "(n1, n2) \ (Cs cs, Th th)" + by auto + from h_s(1) + show ?thesis + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from h_e.waiting_esI2[OF this(3)] + show ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case (holding th' cs') + with h_s(2) + have "cs' \ cs \ th' \ th" by auto + thus ?thesis + proof + assume neq_cs: "cs' \ cs" + from holding_esI2[OF this holding(3)] + show ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + next + assume "th' \ th" + from holding_esI1[OF holding(3) this] + show ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + qed + qed + qed +qed + +end + +lemma step_RAG_v: +assumes vt: + "vt (V th cs#s)" +shows " + RAG (V th cs # s) = + RAG s - {(Cs cs, Th th)} - + {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'}" (is "?L = ?R") +proof - + interpret vt_v: valid_trace_v s "V th cs" + using assms step_back_vt by (unfold_locales, auto) + show ?thesis using vt_v.RAG_es . +qed + +lemma (in valid_trace_create) + th_not_in_threads: "th \ threads s" +proof - + from pip_e[unfolded is_create] + show ?thesis by (cases, simp) +qed + +lemma (in valid_trace_create) + threads_es [simp]: "threads (e#s) = threads s \ {th}" + by (unfold is_create, simp) + +lemma (in valid_trace_exit) + threads_es [simp]: "threads (e#s) = threads s - {th}" + by (unfold is_exit, simp) + +lemma (in valid_trace_p) + threads_es [simp]: "threads (e#s) = threads s" + by (unfold is_p, simp) + +lemma (in valid_trace_v) + threads_es [simp]: "threads (e#s) = threads s" + by (unfold is_v, simp) + +lemma (in valid_trace_v) + th_not_in_rest[simp]: "th \ set rest" +proof + assume otherwise: "th \ set rest" + have "distinct (wq s cs)" by (simp add: wq_distinct) + from this[unfolded wq_s_cs] and otherwise + show False by auto +qed + +lemma (in valid_trace_v) + set_wq_es_cs [simp]: "set (wq (e#s) cs) = set (wq s cs) - {th}" +proof(unfold wq_es_cs wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) +next + fix x + assume "distinct x \ set x = set rest" + thus "set x = set (wq s cs) - {th}" + by (unfold wq_s_cs, simp) +qed + +lemma (in valid_trace_exit) + th_not_in_wq: "th \ set (wq s cs)" +proof - + from pip_e[unfolded is_exit] + show ?thesis + by (cases, unfold holdents_def s_holding_def, fold wq_def, + auto elim!:runing_wqE) +qed + +lemma (in valid_trace) wq_threads: + assumes "th \ set (wq s cs)" + shows "th \ threads s" + using assms +proof(induct rule:ind) + case (Nil) + thus ?case by (auto simp:wq_def) +next + case (Cons s e) + interpret vt_e: valid_trace_e s e using Cons by simp + show ?case + proof(cases e) + case (Create th' prio') + interpret vt: valid_trace_create s e th' prio' + using Create by (unfold_locales, simp) + show ?thesis + using Cons.hyps(2) Cons.prems by auto + next + case (Exit th') + interpret vt: valid_trace_exit s e th' + using Exit by (unfold_locales, simp) + show ?thesis + using Cons.hyps(2) Cons.prems vt.th_not_in_wq by auto + next + case (P th' cs') + interpret vt: valid_trace_p s e th' cs' + using P by (unfold_locales, simp) + show ?thesis + using Cons.hyps(2) Cons.prems readys_threads + runing_ready vt.is_p vt.runing_th_s vt_e.wq_in_inv + by fastforce + next + case (V th' cs') + interpret vt: valid_trace_v s e th' cs' + using V by (unfold_locales, simp) + show ?thesis using Cons + using vt.is_v vt.threads_es vt_e.wq_in_inv by blast + next + case (Set th' prio) + interpret vt: valid_trace_set s e th' prio + using Set by (unfold_locales, simp) + show ?thesis using Cons.hyps(2) Cons.prems vt.is_set + by (auto simp:wq_def Let_def) + qed +qed + +context valid_trace +begin + +lemma dm_RAG_threads: + assumes in_dom: "(Th th) \ Domain (RAG s)" + shows "th \ threads s" +proof - + from in_dom obtain n where "(Th th, n) \ RAG s" by auto + moreover from RAG_target_th[OF this] obtain cs where "n = Cs cs" by auto + ultimately have "(Th th, Cs cs) \ RAG s" by simp + hence "th \ set (wq s cs)" + by (unfold s_RAG_def, auto simp:cs_waiting_def) + from wq_threads [OF this] show ?thesis . +qed + +lemma rg_RAG_threads: + assumes "(Th th) \ Range (RAG s)" + shows "th \ threads s" + using assms + by (unfold s_RAG_def cs_waiting_def cs_holding_def, + auto intro:wq_threads) + +lemma RAG_threads: + assumes "(Th th) \ Field (RAG s)" + shows "th \ threads s" + using assms + by (metis Field_def UnE dm_RAG_threads rg_RAG_threads) + +end + +lemma (in valid_trace_v) + preced_es [simp]: "preced th (e#s) = preced th s" + by (unfold is_v preced_def, simp) + +lemma the_preced_v[simp]: "the_preced (V th cs#s) = the_preced s" +proof + fix th' + show "the_preced (V th cs # s) th' = the_preced s th'" + by (unfold the_preced_def preced_def, simp) +qed + +lemma (in valid_trace_v) + the_preced_es: "the_preced (e#s) = the_preced s" + by (unfold is_v preced_def, simp) + +context valid_trace_p +begin + +lemma not_holding_s_th_cs: "\ holding s th cs" +proof + assume otherwise: "holding s th cs" + from pip_e[unfolded is_p] + show False + proof(cases) + case (thread_P) + moreover have "(Cs cs, Th th) \ RAG s" + using otherwise cs_holding_def + holding_eq th_not_in_wq by auto + ultimately show ?thesis by auto + qed +qed + +lemma waiting_kept: + assumes "waiting s th' cs'" + shows "waiting (e#s) th' cs'" + using assms + by (metis cs_waiting_def hd_append2 list.sel(1) list.set_intros(2) + rotate1.simps(2) self_append_conv2 set_rotate1 + th_not_in_wq waiting_eq wq_es_cs wq_neq_simp) + +lemma holding_kept: + assumes "holding s th' cs'" + shows "holding (e#s) th' cs'" +proof(cases "cs' = cs") + case False + hence "wq (e#s) cs' = wq s cs'" by simp + with assms show ?thesis using cs_holding_def holding_eq by auto +next + case True + from assms[unfolded s_holding_def, folded wq_def] + obtain rest where eq_wq: "wq s cs' = th'#rest" + by (metis empty_iff list.collapse list.set(1)) + hence "wq (e#s) cs' = th'#(rest@[th])" + by (simp add: True wq_es_cs) + thus ?thesis + by (simp add: cs_holding_def holding_eq) +qed + +end + +locale valid_trace_p_h = valid_trace_p + + assumes we: "wq s cs = []" + +locale valid_trace_p_w = valid_trace_p + + assumes wne: "wq s cs \ []" +begin + +definition "holder = hd (wq s cs)" +definition "waiters = tl (wq s cs)" +definition "waiters' = waiters @ [th]" + +lemma wq_s_cs: "wq s cs = holder#waiters" + by (simp add: holder_def waiters_def wne) + +lemma wq_es_cs': "wq (e#s) cs = holder#waiters@[th]" + by (simp add: wq_es_cs wq_s_cs) + +lemma waiting_es_th_cs: "waiting (e#s) th cs" + using cs_waiting_def th_not_in_wq waiting_eq wq_es_cs' wq_s_cs by auto + +lemma RAG_edge: "(Th th, Cs cs) \ RAG (e#s)" + by (unfold s_RAG_def, fold waiting_eq, insert waiting_es_th_cs, auto) + +lemma holding_esE: + assumes "holding (e#s) th' cs'" + obtains "holding s th' cs'" + using assms +proof(cases "cs' = cs") + case False + hence "wq (e#s) cs' = wq s cs'" by simp + with assms show ?thesis + using cs_holding_def holding_eq that by auto +next + case True + with assms show ?thesis + by (metis cs_holding_def holding_eq list.sel(1) list.set_intros(1) that + wq_es_cs' wq_s_cs) +qed + +lemma waiting_esE: + assumes "waiting (e#s) th' cs'" + obtains "th' \ th" "waiting s th' cs'" + | "th' = th" "cs' = cs" +proof(cases "waiting s th' cs'") + case True + have "th' \ th" + proof + assume otherwise: "th' = th" + from True[unfolded this] + show False by (simp add: th_not_waiting) + qed + from that(1)[OF this True] show ?thesis . +next + case False + hence "th' = th \ cs' = cs" + by (metis assms cs_waiting_def holder_def list.sel(1) rotate1.simps(2) + set_ConsD set_rotate1 waiting_eq wq_es_cs wq_es_cs' wq_neq_simp) + with that(2) show ?thesis by metis +qed + +lemma RAG_es: "RAG (e # s) = RAG s \ {(Th th, Cs cs)}" (is "?L = ?R") +proof(rule rel_eqI) + fix n1 n2 + assume "(n1, n2) \ ?L" + thus "(n1, n2) \ ?R" + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from this(3) + show ?thesis + proof(cases rule:waiting_esE) + case 1 + thus ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case 2 + thus ?thesis using waiting(1,2) by auto + qed + next + case (holding th' cs') + from this(3) + show ?thesis + proof(cases rule:holding_esE) + case 1 + with holding(1,2) + show ?thesis by (unfold s_RAG_def, fold holding_eq, auto) + qed + qed +next + fix n1 n2 + assume "(n1, n2) \ ?R" + hence "(n1, n2) \ RAG s \ (n1 = Th th \ n2 = Cs cs)" by auto + thus "(n1, n2) \ ?L" + proof + assume "(n1, n2) \ RAG s" + thus ?thesis + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from waiting_kept[OF this(3)] + show ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case (holding th' cs') + from holding_kept[OF this(3)] + show ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + qed + next + assume "n1 = Th th \ n2 = Cs cs" + thus ?thesis using RAG_edge by auto + qed +qed + +end + +context valid_trace_p_h +begin + +lemma wq_es_cs': "wq (e#s) cs = [th]" + using wq_es_cs[unfolded we] by simp + +lemma holding_es_th_cs: + shows "holding (e#s) th cs" +proof - + from wq_es_cs' + have "th \ set (wq (e#s) cs)" "th = hd (wq (e#s) cs)" by auto + thus ?thesis using cs_holding_def holding_eq by blast +qed + +lemma RAG_edge: "(Cs cs, Th th) \ RAG (e#s)" + by (unfold s_RAG_def, fold holding_eq, insert holding_es_th_cs, auto) + +lemma waiting_esE: + assumes "waiting (e#s) th' cs'" + obtains "waiting s th' cs'" + using assms + by (metis cs_waiting_def event.distinct(15) is_p list.sel(1) + set_ConsD waiting_eq we wq_es_cs' wq_neq_simp wq_out_inv) + +lemma holding_esE: + assumes "holding (e#s) th' cs'" + obtains "cs' \ cs" "holding s th' cs'" + | "cs' = cs" "th' = th" +proof(cases "cs' = cs") + case True + from held_unique[OF holding_es_th_cs assms[unfolded True]] + have "th' = th" by simp + from that(2)[OF True this] show ?thesis . +next + case False + have "holding s th' cs'" using assms + using False cs_holding_def holding_eq by auto + from that(1)[OF False this] show ?thesis . +qed + +lemma RAG_es: "RAG (e # s) = RAG s \ {(Cs cs, Th th)}" (is "?L = ?R") +proof(rule rel_eqI) + fix n1 n2 + assume "(n1, n2) \ ?L" + thus "(n1, n2) \ ?R" + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from this(3) + show ?thesis + proof(cases rule:waiting_esE) + case 1 + thus ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + qed + next + case (holding th' cs') + from this(3) + show ?thesis + proof(cases rule:holding_esE) + case 1 + with holding(1,2) + show ?thesis by (unfold s_RAG_def, fold holding_eq, auto) + next + case 2 + with holding(1,2) show ?thesis by auto + qed + qed +next + fix n1 n2 + assume "(n1, n2) \ ?R" + hence "(n1, n2) \ RAG s \ (n1 = Cs cs \ n2 = Th th)" by auto + thus "(n1, n2) \ ?L" + proof + assume "(n1, n2) \ RAG s" + thus ?thesis + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from waiting_kept[OF this(3)] + show ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case (holding th' cs') + from holding_kept[OF this(3)] + show ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + qed + next + assume "n1 = Cs cs \ n2 = Th th" + with holding_es_th_cs + show ?thesis + by (unfold s_RAG_def, fold holding_eq, auto) + qed +qed + +end + +context valid_trace_p +begin + +lemma RAG_es': "RAG (e # s) = (if (wq s cs = []) then RAG s \ {(Cs cs, Th th)} + else RAG s \ {(Th th, Cs cs)})" +proof(cases "wq s cs = []") + case True + interpret vt_p: valid_trace_p_h using True + by (unfold_locales, simp) + show ?thesis by (simp add: vt_p.RAG_es vt_p.we) +next + case False + interpret vt_p: valid_trace_p_w using False + by (unfold_locales, simp) + show ?thesis by (simp add: vt_p.RAG_es vt_p.wne) +qed + +end + +lemma (in valid_trace_v_n) finite_waiting_set: + "finite {(Th th', Cs cs) |th'. next_th s th cs th'}" + by (simp add: waiting_set_eq) + +lemma (in valid_trace_v_n) finite_holding_set: + "finite {(Cs cs, Th th') |th'. next_th s th cs th'}" + by (simp add: holding_set_eq) + +lemma (in valid_trace_v_e) finite_waiting_set: + "finite {(Th th', Cs cs) |th'. next_th s th cs th'}" + by (simp add: waiting_set_eq) + +lemma (in valid_trace_v_e) finite_holding_set: + "finite {(Cs cs, Th th') |th'. next_th s th cs th'}" + by (simp add: holding_set_eq) + +context valid_trace_v +begin + +lemma + finite_RAG_kept: + assumes "finite (RAG s)" + shows "finite (RAG (e#s))" +proof(cases "rest = []") + case True + interpret vt: valid_trace_v_e using True + by (unfold_locales, simp) + show ?thesis using assms + by (unfold RAG_es vt.waiting_set_eq vt.holding_set_eq, simp) +next + case False + interpret vt: valid_trace_v_n using False + by (unfold_locales, simp) + show ?thesis using assms + by (unfold RAG_es vt.waiting_set_eq vt.holding_set_eq, simp) +qed + +end + +context valid_trace_v_e +begin + +lemma + acylic_RAG_kept: + assumes "acyclic (RAG s)" + shows "acyclic (RAG (e#s))" +proof(rule acyclic_subset[OF assms]) + show "RAG (e # s) \ RAG s" + by (unfold RAG_es waiting_set_eq holding_set_eq, auto) +qed + +end + +context valid_trace_v_n +begin + +lemma waiting_taker: "waiting s taker cs" + apply (unfold s_waiting_def, fold wq_def, unfold wq_s_cs taker_def) + using eq_wq' th'_in_inv wq'_def by fastforce + +lemma + acylic_RAG_kept: + assumes "acyclic (RAG s)" + shows "acyclic (RAG (e#s))" +proof - + have "acyclic ((RAG s - {(Cs cs, Th th)} - {(Th taker, Cs cs)}) \ + {(Cs cs, Th taker)})" (is "acyclic (?A \ _)") + proof - + from assms + have "acyclic ?A" + by (rule acyclic_subset, auto) + moreover have "(Th taker, Cs cs) \ ?A^*" + proof + assume otherwise: "(Th taker, Cs cs) \ ?A^*" + hence "(Th taker, Cs cs) \ ?A^+" + by (unfold rtrancl_eq_or_trancl, auto) + from tranclD[OF this] + obtain cs' where h: "(Th taker, Cs cs') \ ?A" + "(Th taker, Cs cs') \ RAG s" + by (unfold s_RAG_def, auto) + from this(2) have "waiting s taker cs'" + by (unfold s_RAG_def, fold waiting_eq, auto) + from waiting_unique[OF this waiting_taker] + have "cs' = cs" . + from h(1)[unfolded this] show False by auto + qed + ultimately show ?thesis by auto + qed + thus ?thesis + by (unfold RAG_es waiting_set_eq holding_set_eq, simp) +qed + +end + +context valid_trace_p_h +begin + +lemma + acylic_RAG_kept: + assumes "acyclic (RAG s)" + shows "acyclic (RAG (e#s))" +proof - + have "acyclic (RAG s \ {(Cs cs, Th th)})" (is "acyclic (?A \ _)") + proof - + from assms + have "acyclic ?A" + by (rule acyclic_subset, auto) + moreover have "(Th th, Cs cs) \ ?A^*" + proof + assume otherwise: "(Th th, Cs cs) \ ?A^*" + hence "(Th th, Cs cs) \ ?A^+" + by (unfold rtrancl_eq_or_trancl, auto) + from tranclD[OF this] + obtain cs' where h: "(Th th, Cs cs') \ RAG s" + by (unfold s_RAG_def, auto) + hence "waiting s th cs'" + by (unfold s_RAG_def, fold waiting_eq, auto) + with th_not_waiting show False by auto + qed + ultimately show ?thesis by auto + qed + thus ?thesis by (unfold RAG_es, simp) +qed + +end + +context valid_trace_p_w +begin + +lemma + acylic_RAG_kept: + assumes "acyclic (RAG s)" + shows "acyclic (RAG (e#s))" +proof - + have "acyclic (RAG s \ {(Th th, Cs cs)})" (is "acyclic (?A \ _)") + proof - + from assms + have "acyclic ?A" + by (rule acyclic_subset, auto) + moreover have "(Cs cs, Th th) \ ?A^*" + proof + assume otherwise: "(Cs cs, Th th) \ ?A^*" + from pip_e[unfolded is_p] + show False + proof(cases) + case (thread_P) + moreover from otherwise have "(Cs cs, Th th) \ ?A^+" + by (unfold rtrancl_eq_or_trancl, auto) + ultimately show ?thesis by auto + qed + qed + ultimately show ?thesis by auto + qed + thus ?thesis by (unfold RAG_es, simp) +qed + +end + +context valid_trace +begin + +lemma finite_RAG: + shows "finite (RAG s)" +proof(induct rule:ind) + case Nil + show ?case + by (auto simp: s_RAG_def cs_waiting_def + cs_holding_def wq_def acyclic_def) +next + case (Cons s e) + interpret vt_e: valid_trace_e s e using Cons by simp + show ?case + proof(cases e) + case (Create th prio) + interpret vt: valid_trace_create s e th prio using Create + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt.RAG_unchanged) + next + case (Exit th) + interpret vt: valid_trace_exit s e th using Exit + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt.RAG_unchanged) + next + case (P th cs) + interpret vt: valid_trace_p s e th cs using P + by (unfold_locales, simp) + show ?thesis using Cons using vt.RAG_es' by auto + next + case (V th cs) + interpret vt: valid_trace_v s e th cs using V + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt.finite_RAG_kept) + next + case (Set th prio) + interpret vt: valid_trace_set s e th prio using Set + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt.RAG_unchanged) + qed +qed + +lemma acyclic_RAG: + shows "acyclic (RAG s)" +proof(induct rule:ind) + case Nil + show ?case + by (auto simp: s_RAG_def cs_waiting_def + cs_holding_def wq_def acyclic_def) +next + case (Cons s e) + interpret vt_e: valid_trace_e s e using Cons by simp + show ?case + proof(cases e) + case (Create th prio) + interpret vt: valid_trace_create s e th prio using Create + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt.RAG_unchanged) + next + case (Exit th) + interpret vt: valid_trace_exit s e th using Exit + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt.RAG_unchanged) + next + case (P th cs) + interpret vt: valid_trace_p s e th cs using P + by (unfold_locales, simp) + show ?thesis + proof(cases "wq s cs = []") + case True + then interpret vt_h: valid_trace_p_h s e th cs + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_h.acylic_RAG_kept) + next + case False + then interpret vt_w: valid_trace_p_w s e th cs + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_w.acylic_RAG_kept) + qed + next + case (V th cs) + interpret vt: valid_trace_v s e th cs using V + by (unfold_locales, simp) + show ?thesis + proof(cases "vt.rest = []") + case True + then interpret vt_e: valid_trace_v_e s e th cs + by (unfold_locales, simp) + show ?thesis by (simp add: Cons.hyps(2) vt_e.acylic_RAG_kept) + next + case False + then interpret vt_n: valid_trace_v_n s e th cs + by (unfold_locales, simp) + show ?thesis by (simp add: Cons.hyps(2) vt_n.acylic_RAG_kept) + qed + next + case (Set th prio) + interpret vt: valid_trace_set s e th prio using Set + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt.RAG_unchanged) + qed +qed + +lemma wf_RAG: "wf (RAG s)" +proof(rule finite_acyclic_wf) + from finite_RAG show "finite (RAG s)" . +next + from acyclic_RAG show "acyclic (RAG s)" . +qed + +lemma sgv_wRAG: "single_valued (wRAG s)" + using waiting_unique + by (unfold single_valued_def wRAG_def, auto) + +lemma sgv_hRAG: "single_valued (hRAG s)" + using held_unique + by (unfold single_valued_def hRAG_def, auto) + +lemma sgv_tRAG: "single_valued (tRAG s)" + by (unfold tRAG_def, rule single_valued_relcomp, + insert sgv_wRAG sgv_hRAG, auto) + +lemma acyclic_tRAG: "acyclic (tRAG s)" +proof(unfold tRAG_def, rule acyclic_compose) + show "acyclic (RAG s)" using acyclic_RAG . +next + show "wRAG s \ RAG s" unfolding RAG_split by auto +next + show "hRAG s \ RAG s" unfolding RAG_split by auto +qed + +lemma unique_RAG: "\(n, n1) \ RAG s; (n, n2) \ RAG s\ \ n1 = n2" + apply(unfold s_RAG_def, auto, fold waiting_eq holding_eq) + by(auto elim:waiting_unique held_unique) + +lemma sgv_RAG: "single_valued (RAG s)" + using unique_RAG by (auto simp:single_valued_def) + +lemma rtree_RAG: "rtree (RAG s)" + using sgv_RAG acyclic_RAG + by (unfold rtree_def rtree_axioms_def sgv_def, auto) + +end + +sublocale valid_trace < rtree_RAG: rtree "RAG s" +proof + show "single_valued (RAG s)" + apply (intro_locales) + by (unfold single_valued_def, + auto intro:unique_RAG) + + show "acyclic (RAG s)" + by (rule acyclic_RAG) +qed + +sublocale valid_trace < rtree_s: rtree "tRAG s" +proof(unfold_locales) + from sgv_tRAG show "single_valued (tRAG s)" . +next + from acyclic_tRAG show "acyclic (tRAG s)" . +qed + +sublocale valid_trace < fsbtRAGs : fsubtree "RAG s" +proof - + show "fsubtree (RAG s)" + proof(intro_locales) + show "fbranch (RAG s)" using finite_fbranchI[OF finite_RAG] . + next + show "fsubtree_axioms (RAG s)" + proof(unfold fsubtree_axioms_def) + from wf_RAG show "wf (RAG s)" . + qed + qed +qed + +lemma tRAG_alt_def: + "tRAG s = {(Th th1, Th th2) | th1 th2. + \ cs. (Th th1, Cs cs) \ RAG s \ (Cs cs, Th th2) \ RAG s}" + by (auto simp:tRAG_def RAG_split wRAG_def hRAG_def) + +sublocale valid_trace < fsbttRAGs: fsubtree "tRAG s" +proof - + have "fsubtree (tRAG s)" + proof - + have "fbranch (tRAG s)" + proof(unfold tRAG_def, rule fbranch_compose) + show "fbranch (wRAG s)" + proof(rule finite_fbranchI) + from finite_RAG show "finite (wRAG s)" + by (unfold RAG_split, auto) + qed + next + show "fbranch (hRAG s)" + proof(rule finite_fbranchI) + from finite_RAG + show "finite (hRAG s)" by (unfold RAG_split, auto) + qed + qed + moreover have "wf (tRAG s)" + proof(rule wf_subset) + show "wf (RAG s O RAG s)" using wf_RAG + by (fold wf_comp_self, simp) + next + show "tRAG s \ (RAG s O RAG s)" + by (unfold tRAG_alt_def, auto) + qed + ultimately show ?thesis + by (unfold fsubtree_def fsubtree_axioms_def,auto) + qed + from this[folded tRAG_def] show "fsubtree (tRAG s)" . +qed + + +context valid_trace +begin + +lemma finite_subtree_threads: + "finite {th'. Th th' \ subtree (RAG s) (Th th)}" (is "finite ?A") +proof - + have "?A = the_thread ` {Th th' | th' . Th th' \ subtree (RAG s) (Th th)}" + by (auto, insert image_iff, fastforce) + moreover have "finite {Th th' | th' . Th th' \ subtree (RAG s) (Th th)}" + (is "finite ?B") + proof - + have "?B = (subtree (RAG s) (Th th)) \ {Th th' | th'. True}" + by auto + moreover have "... \ (subtree (RAG s) (Th th))" by auto + moreover have "finite ..." by (simp add: fsbtRAGs.finite_subtree) + ultimately show ?thesis by auto + qed + ultimately show ?thesis by auto +qed + +lemma le_cp: + shows "preced th s \ cp s th" + proof(unfold cp_alt_def, rule Max_ge) + show "finite (the_preced s ` {th'. Th th' \ subtree (RAG s) (Th th)})" + by (simp add: finite_subtree_threads) + next + show "preced th s \ the_preced s ` {th'. Th th' \ subtree (RAG s) (Th th)}" + by (simp add: subtree_def the_preced_def) + qed + +lemma cp_le: + assumes th_in: "th \ threads s" + shows "cp s th \ Max (the_preced s ` threads s)" +proof(unfold cp_alt_def, rule Max_f_mono) + show "finite (threads s)" by (simp add: finite_threads) +next + show " {th'. Th th' \ subtree (RAG s) (Th th)} \ {}" + using subtree_def by fastforce +next + show "{th'. Th th' \ subtree (RAG s) (Th th)} \ threads s" + using assms + by (smt Domain.DomainI dm_RAG_threads mem_Collect_eq + node.inject(1) rtranclD subsetI subtree_def trancl_domain) +qed + +lemma max_cp_eq: + shows "Max ((cp s) ` threads s) = Max (the_preced s ` threads s)" + (is "?L = ?R") +proof - + have "?L \ ?R" + proof(cases "threads s = {}") + case False + show ?thesis + by (rule Max.boundedI, + insert cp_le, + auto simp:finite_threads False) + qed auto + moreover have "?R \ ?L" + by (rule Max_fg_mono, + simp add: finite_threads, + simp add: le_cp the_preced_def) + ultimately show ?thesis by auto +qed + +lemma max_cp_eq_the_preced: + shows "Max ((cp s) ` threads s) = Max (the_preced s ` threads s)" + using max_cp_eq using the_preced_def by presburger + +lemma wf_RAG_converse: + shows "wf ((RAG s)^-1)" +proof(rule finite_acyclic_wf_converse) + from finite_RAG + show "finite (RAG s)" . +next + from acyclic_RAG + show "acyclic (RAG s)" . +qed + +lemma chain_building: + assumes "node \ Domain (RAG s)" + obtains th' where "th' \ readys s" "(node, Th th') \ (RAG s)^+" +proof - + from assms have "node \ Range ((RAG s)^-1)" by auto + from wf_base[OF wf_RAG_converse this] + obtain b where h_b: "(b, node) \ ((RAG s)\)\<^sup>+" "\c. (c, b) \ (RAG s)\" by auto + obtain th' where eq_b: "b = Th th'" + proof(cases b) + case (Cs cs) + from h_b(1)[unfolded trancl_converse] + have "(node, b) \ ((RAG s)\<^sup>+)" by auto + from tranclE[OF this] + obtain n where "(n, b) \ RAG s" by auto + from this[unfolded Cs] + obtain th1 where "waiting s th1 cs" + by (unfold s_RAG_def, fold waiting_eq, auto) + from waiting_holding[OF this] + obtain th2 where "holding s th2 cs" . + hence "(Cs cs, Th th2) \ RAG s" + by (unfold s_RAG_def, fold holding_eq, auto) + with h_b(2)[unfolded Cs, rule_format] + have False by auto + thus ?thesis by auto + qed auto + have "th' \ readys s" + proof - + from h_b(2)[unfolded eq_b] + have "\cs. \ waiting s th' cs" + by (unfold s_RAG_def, fold waiting_eq, auto) + moreover have "th' \ threads s" + proof(rule rg_RAG_threads) + from tranclD[OF h_b(1), unfolded eq_b] + obtain z where "(z, Th th') \ (RAG s)" by auto + thus "Th th' \ Range (RAG s)" by auto + qed + ultimately show ?thesis by (auto simp:readys_def) + qed + moreover have "(node, Th th') \ (RAG s)^+" + using h_b(1)[unfolded trancl_converse] eq_b by auto + ultimately show ?thesis using that by metis +qed + +text {* \noindent + The following is just an instance of @{text "chain_building"}. +*} +lemma th_chain_to_ready: + assumes th_in: "th \ threads s" + shows "th \ readys s \ (\ th'. th' \ readys s \ (Th th, Th th') \ (RAG s)^+)" +proof(cases "th \ readys s") + case True + thus ?thesis by auto +next + case False + from False and th_in have "Th th \ Domain (RAG s)" + by (auto simp:readys_def s_waiting_def s_RAG_def wq_def cs_waiting_def Domain_def) + from chain_building [rule_format, OF this] + show ?thesis by auto +qed + +end + +lemma count_rec1 [simp]: + assumes "Q e" + shows "count Q (e#es) = Suc (count Q es)" + using assms + by (unfold count_def, auto) + +lemma count_rec2 [simp]: + assumes "\Q e" + shows "count Q (e#es) = (count Q es)" + using assms + by (unfold count_def, auto) + +lemma count_rec3 [simp]: + shows "count Q [] = 0" + by (unfold count_def, auto) + +lemma cntP_simp1[simp]: + "cntP (P th cs'#s) th = cntP s th + 1" + by (unfold cntP_def, simp) + +lemma cntP_simp2[simp]: + assumes "th' \ th" + shows "cntP (P th cs'#s) th' = cntP s th'" + using assms + by (unfold cntP_def, simp) + +lemma cntP_simp3[simp]: + assumes "\ isP e" + shows "cntP (e#s) th' = cntP s th'" + using assms + by (unfold cntP_def, cases e, simp+) + +lemma cntV_simp1[simp]: + "cntV (V th cs'#s) th = cntV s th + 1" + by (unfold cntV_def, simp) + +lemma cntV_simp2[simp]: + assumes "th' \ th" + shows "cntV (V th cs'#s) th' = cntV s th'" + using assms + by (unfold cntV_def, simp) + +lemma cntV_simp3[simp]: + assumes "\ isV e" + shows "cntV (e#s) th' = cntV s th'" + using assms + by (unfold cntV_def, cases e, simp+) + +lemma cntP_diff_inv: + assumes "cntP (e#s) th \ cntP s th" + shows "isP e \ actor e = th" +proof(cases e) + case (P th' pty) + show ?thesis + by (cases "(\e. \cs. e = P th cs) (P th' pty)", + insert assms P, auto simp:cntP_def) +qed (insert assms, auto simp:cntP_def) + +lemma cntV_diff_inv: + assumes "cntV (e#s) th \ cntV s th" + shows "isV e \ actor e = th" +proof(cases e) + case (V th' pty) + show ?thesis + by (cases "(\e. \cs. e = V th cs) (V th' pty)", + insert assms V, auto simp:cntV_def) +qed (insert assms, auto simp:cntV_def) + +lemma children_RAG_alt_def: + "children (RAG (s::state)) (Th th) = Cs ` {cs. holding s th cs}" + by (unfold s_RAG_def, auto simp:children_def holding_eq) + +lemma holdents_alt_def: + "holdents s th = the_cs ` (children (RAG (s::state)) (Th th))" + by (unfold children_RAG_alt_def holdents_def, simp add: image_image) + +lemma cntCS_alt_def: + "cntCS s th = card (children (RAG s) (Th th))" + apply (unfold children_RAG_alt_def cntCS_def holdents_def) + by (rule card_image[symmetric], auto simp:inj_on_def) + +context valid_trace +begin + +lemma finite_holdents: "finite (holdents s th)" + by (unfold holdents_alt_def, insert fsbtRAGs.finite_children, auto) + +end + +context valid_trace_p_w +begin + +lemma holding_s_holder: "holding s holder cs" + by (unfold s_holding_def, fold wq_def, unfold wq_s_cs, auto) + +lemma holding_es_holder: "holding (e#s) holder cs" + by (unfold s_holding_def, fold wq_def, unfold wq_es_cs wq_s_cs, auto) + +lemma holdents_es: + shows "holdents (e#s) th' = holdents s th'" (is "?L = ?R") +proof - + { fix cs' + assume "cs' \ ?L" + hence h: "holding (e#s) th' cs'" by (auto simp:holdents_def) + have "holding s th' cs'" + proof(cases "cs' = cs") + case True + from held_unique[OF h[unfolded True] holding_es_holder] + have "th' = holder" . + thus ?thesis + by (unfold True holdents_def, insert holding_s_holder, simp) + next + case False + hence "wq (e#s) cs' = wq s cs'" by simp + from h[unfolded s_holding_def, folded wq_def, unfolded this] + show ?thesis + by (unfold s_holding_def, fold wq_def, auto) + qed + hence "cs' \ ?R" by (auto simp:holdents_def) + } moreover { + fix cs' + assume "cs' \ ?R" + hence h: "holding s th' cs'" by (auto simp:holdents_def) + have "holding (e#s) th' cs'" + proof(cases "cs' = cs") + case True + from held_unique[OF h[unfolded True] holding_s_holder] + have "th' = holder" . + thus ?thesis + by (unfold True holdents_def, insert holding_es_holder, simp) + next + case False + hence "wq s cs' = wq (e#s) cs'" by simp + from h[unfolded s_holding_def, folded wq_def, unfolded this] + show ?thesis + by (unfold s_holding_def, fold wq_def, auto) + qed + hence "cs' \ ?L" by (auto simp:holdents_def) + } ultimately show ?thesis by auto +qed + +lemma cntCS_es_th[simp]: "cntCS (e#s) th' = cntCS s th'" + by (unfold cntCS_def holdents_es, simp) + +lemma th_not_ready_es: + shows "th \ readys (e#s)" + using waiting_es_th_cs + by (unfold readys_def, auto) + +end + +context valid_trace_p_h +begin + +lemma th_not_waiting': + "\ waiting (e#s) th cs'" +proof(cases "cs' = cs") + case True + show ?thesis + by (unfold True s_waiting_def, fold wq_def, unfold wq_es_cs', auto) +next + case False + from th_not_waiting[of cs', unfolded s_waiting_def, folded wq_def] + show ?thesis + by (unfold s_waiting_def, fold wq_def, insert False, simp) +qed + +lemma ready_th_es: + shows "th \ readys (e#s)" + using th_not_waiting' + by (unfold readys_def, insert live_th_es, auto) + +lemma holdents_es_th: + "holdents (e#s) th = (holdents s th) \ {cs}" (is "?L = ?R") +proof - + { fix cs' + assume "cs' \ ?L" + hence "holding (e#s) th cs'" + by (unfold holdents_def, auto) + hence "cs' \ ?R" + by (cases rule:holding_esE, auto simp:holdents_def) + } moreover { + fix cs' + assume "cs' \ ?R" + hence "holding s th cs' \ cs' = cs" + by (auto simp:holdents_def) + hence "cs' \ ?L" + proof + assume "holding s th cs'" + from holding_kept[OF this] + show ?thesis by (auto simp:holdents_def) + next + assume "cs' = cs" + thus ?thesis using holding_es_th_cs + by (unfold holdents_def, auto) + qed + } ultimately show ?thesis by auto +qed + +lemma cntCS_es_th: "cntCS (e#s) th = cntCS s th + 1" +proof - + have "card (holdents s th \ {cs}) = card (holdents s th) + 1" + proof(subst card_Un_disjoint) + show "holdents s th \ {cs} = {}" + using not_holding_s_th_cs by (auto simp:holdents_def) + qed (auto simp:finite_holdents) + thus ?thesis + by (unfold cntCS_def holdents_es_th, simp) +qed + +lemma no_holder: + "\ holding s th' cs" +proof + assume otherwise: "holding s th' cs" + from this[unfolded s_holding_def, folded wq_def, unfolded we] + show False by auto +qed + +lemma holdents_es_th': + assumes "th' \ th" + shows "holdents (e#s) th' = holdents s th'" (is "?L = ?R") +proof - + { fix cs' + assume "cs' \ ?L" + hence h_e: "holding (e#s) th' cs'" by (auto simp:holdents_def) + have "cs' \ cs" + proof + assume "cs' = cs" + from held_unique[OF h_e[unfolded this] holding_es_th_cs] + have "th' = th" . + with assms show False by simp + qed + from h_e[unfolded s_holding_def, folded wq_def, unfolded wq_neq_simp[OF this]] + have "th' \ set (wq s cs') \ th' = hd (wq s cs')" . + hence "cs' \ ?R" + by (unfold holdents_def s_holding_def, fold wq_def, auto) + } moreover { + fix cs' + assume "cs' \ ?R" + hence "holding s th' cs'" by (auto simp:holdents_def) + from holding_kept[OF this] + have "holding (e # s) th' cs'" . + hence "cs' \ ?L" + by (unfold holdents_def, auto) + } ultimately show ?thesis by auto +qed + +lemma cntCS_es_th'[simp]: + assumes "th' \ th" + shows "cntCS (e#s) th' = cntCS s th'" + by (unfold cntCS_def holdents_es_th'[OF assms], simp) + +end + +context valid_trace_p +begin + +lemma readys_kept1: + assumes "th' \ th" + and "th' \ readys (e#s)" + shows "th' \ readys s" +proof - + { fix cs' + assume wait: "waiting s th' cs'" + have n_wait: "\ waiting (e#s) th' cs'" + using assms(2)[unfolded readys_def] by auto + have False + proof(cases "cs' = cs") + case False + with n_wait wait + show ?thesis + by (unfold s_waiting_def, fold wq_def, auto) + next + case True + show ?thesis + proof(cases "wq s cs = []") + case True + then interpret vt: valid_trace_p_h + by (unfold_locales, simp) + show ?thesis using n_wait wait waiting_kept by auto + next + case False + then interpret vt: valid_trace_p_w by (unfold_locales, simp) + show ?thesis using n_wait wait waiting_kept by blast + qed + qed + } with assms(2) show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_kept2: + assumes "th' \ th" + and "th' \ readys s" + shows "th' \ readys (e#s)" +proof - + { fix cs' + assume wait: "waiting (e#s) th' cs'" + have n_wait: "\ waiting s th' cs'" + using assms(2)[unfolded readys_def] by auto + have False + proof(cases "cs' = cs") + case False + with n_wait wait + show ?thesis + by (unfold s_waiting_def, fold wq_def, auto) + next + case True + show ?thesis + proof(cases "wq s cs = []") + case True + then interpret vt: valid_trace_p_h + by (unfold_locales, simp) + show ?thesis using n_wait vt.waiting_esE wait by blast + next + case False + then interpret vt: valid_trace_p_w by (unfold_locales, simp) + show ?thesis using assms(1) n_wait vt.waiting_esE wait by auto + qed + qed + } with assms(2) show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_simp [simp]: + assumes "th' \ th" + shows "(th' \ readys (e#s)) = (th' \ readys s)" + using readys_kept1[OF assms] readys_kept2[OF assms] + by metis + +lemma cnp_cnv_cncs_kept: (* ddd *) + assumes "cntP s th' = cntV s th' + cntCS s th' + pvD s th'" + shows "cntP (e#s) th' = cntV (e#s) th' + cntCS (e#s) th' + pvD (e#s) th'" +proof(cases "th' = th") + case True + note eq_th' = this + show ?thesis + proof(cases "wq s cs = []") + case True + then interpret vt: valid_trace_p_h by (unfold_locales, simp) + show ?thesis + using assms eq_th' is_p ready_th_s vt.cntCS_es_th vt.ready_th_es pvD_def by auto + next + case False + then interpret vt: valid_trace_p_w by (unfold_locales, simp) + show ?thesis + using add.commute add.left_commute assms eq_th' is_p live_th_s + ready_th_s vt.th_not_ready_es pvD_def + apply (auto) + by (fold is_p, simp) + qed +next + case False + note h_False = False + thus ?thesis + proof(cases "wq s cs = []") + case True + then interpret vt: valid_trace_p_h by (unfold_locales, simp) + show ?thesis using assms + by (insert True h_False pvD_def, auto split:if_splits,unfold is_p, auto) + next + case False + then interpret vt: valid_trace_p_w by (unfold_locales, simp) + show ?thesis using assms + by (insert False h_False pvD_def, auto split:if_splits,unfold is_p, auto) + qed +qed + +end + + +context valid_trace_v (* ccc *) +begin + +lemma holding_th_cs_s: + "holding s th cs" + by (unfold s_holding_def, fold wq_def, unfold wq_s_cs, auto) + +lemma th_ready_s [simp]: "th \ readys s" + using runing_th_s + by (unfold runing_def readys_def, auto) + +lemma th_live_s [simp]: "th \ threads s" + using th_ready_s by (unfold readys_def, auto) + +lemma th_ready_es [simp]: "th \ readys (e#s)" + using runing_th_s neq_t_th + by (unfold is_v runing_def readys_def, auto) + +lemma th_live_es [simp]: "th \ threads (e#s)" + using th_ready_es by (unfold readys_def, auto) + +lemma pvD_th_s[simp]: "pvD s th = 0" + by (unfold pvD_def, simp) + +lemma pvD_th_es[simp]: "pvD (e#s) th = 0" + by (unfold pvD_def, simp) + +lemma cntCS_s_th [simp]: "cntCS s th > 0" +proof - + have "cs \ holdents s th" using holding_th_cs_s + by (unfold holdents_def, simp) + moreover have "finite (holdents s th)" using finite_holdents + by simp + ultimately show ?thesis + by (unfold cntCS_def, + auto intro!:card_gt_0_iff[symmetric, THEN iffD1]) +qed + +end + +context valid_trace_v_n +begin + +lemma not_ready_taker_s[simp]: + "taker \ readys s" + using waiting_taker + by (unfold readys_def, auto) + +lemma taker_live_s [simp]: "taker \ threads s" +proof - + have "taker \ set wq'" by (simp add: eq_wq') + from th'_in_inv[OF this] + have "taker \ set rest" . + hence "taker \ set (wq s cs)" by (simp add: wq_s_cs) + thus ?thesis using wq_threads by auto +qed + +lemma taker_live_es [simp]: "taker \ threads (e#s)" + using taker_live_s threads_es by blast + +lemma taker_ready_es [simp]: + shows "taker \ readys (e#s)" +proof - + { fix cs' + assume "waiting (e#s) taker cs'" + hence False + proof(cases rule:waiting_esE) + case 1 + thus ?thesis using waiting_taker waiting_unique by auto + qed simp + } thus ?thesis by (unfold readys_def, auto) +qed + +lemma neq_taker_th: "taker \ th" + using th_not_waiting waiting_taker by blast + +lemma not_holding_taker_s_cs: + shows "\ holding s taker cs" + using holding_cs_eq_th neq_taker_th by auto + +lemma holdents_es_taker: + "holdents (e#s) taker = holdents s taker \ {cs}" (is "?L = ?R") +proof - + { fix cs' + assume "cs' \ ?L" + hence "holding (e#s) taker cs'" by (auto simp:holdents_def) + hence "cs' \ ?R" + proof(cases rule:holding_esE) + case 2 + thus ?thesis by (auto simp:holdents_def) + qed auto + } moreover { + fix cs' + assume "cs' \ ?R" + hence "holding s taker cs' \ cs' = cs" by (auto simp:holdents_def) + hence "cs' \ ?L" + proof + assume "holding s taker cs'" + hence "holding (e#s) taker cs'" + using holding_esI2 holding_taker by fastforce + thus ?thesis by (auto simp:holdents_def) + next + assume "cs' = cs" + with holding_taker + show ?thesis by (auto simp:holdents_def) + qed + } ultimately show ?thesis by auto +qed + +lemma cntCS_es_taker [simp]: "cntCS (e#s) taker = cntCS s taker + 1" +proof - + have "card (holdents s taker \ {cs}) = card (holdents s taker) + 1" + proof(subst card_Un_disjoint) + show "holdents s taker \ {cs} = {}" + using not_holding_taker_s_cs by (auto simp:holdents_def) + qed (auto simp:finite_holdents) + thus ?thesis + by (unfold cntCS_def, insert holdents_es_taker, simp) +qed + +lemma pvD_taker_s[simp]: "pvD s taker = 1" + by (unfold pvD_def, simp) + +lemma pvD_taker_es[simp]: "pvD (e#s) taker = 0" + by (unfold pvD_def, simp) + +lemma pvD_th_s[simp]: "pvD s th = 0" + by (unfold pvD_def, simp) + +lemma pvD_th_es[simp]: "pvD (e#s) th = 0" + by (unfold pvD_def, simp) + +lemma holdents_es_th: + "holdents (e#s) th = holdents s th - {cs}" (is "?L = ?R") +proof - + { fix cs' + assume "cs' \ ?L" + hence "holding (e#s) th cs'" by (auto simp:holdents_def) + hence "cs' \ ?R" + proof(cases rule:holding_esE) + case 2 + thus ?thesis by (auto simp:holdents_def) + qed (insert neq_taker_th, auto) + } moreover { + fix cs' + assume "cs' \ ?R" + hence "cs' \ cs" "holding s th cs'" by (auto simp:holdents_def) + from holding_esI2[OF this] + have "cs' \ ?L" by (auto simp:holdents_def) + } ultimately show ?thesis by auto +qed + +lemma cntCS_es_th [simp]: "cntCS (e#s) th = cntCS s th - 1" +proof - + have "card (holdents s th - {cs}) = card (holdents s th) - 1" + proof - + have "cs \ holdents s th" using holding_th_cs_s + by (auto simp:holdents_def) + moreover have "finite (holdents s th)" + by (simp add: finite_holdents) + ultimately show ?thesis by auto + qed + thus ?thesis by (unfold cntCS_def holdents_es_th) +qed + +lemma holdents_kept: + assumes "th' \ taker" + and "th' \ th" + shows "holdents (e#s) th' = holdents s th'" (is "?L = ?R") +proof - + { fix cs' + assume h: "cs' \ ?L" + have "cs' \ ?R" + proof(cases "cs' = cs") + case False + hence eq_wq: "wq (e#s) cs' = wq s cs'" by simp + from h have "holding (e#s) th' cs'" by (auto simp:holdents_def) + from this[unfolded s_holding_def, folded wq_def, unfolded eq_wq] + show ?thesis + by (unfold holdents_def s_holding_def, fold wq_def, auto) + next + case True + from h[unfolded this] + have "holding (e#s) th' cs" by (auto simp:holdents_def) + from held_unique[OF this holding_taker] + have "th' = taker" . + with assms show ?thesis by auto + qed + } moreover { + fix cs' + assume h: "cs' \ ?R" + have "cs' \ ?L" + proof(cases "cs' = cs") + case False + hence eq_wq: "wq (e#s) cs' = wq s cs'" by simp + from h have "holding s th' cs'" by (auto simp:holdents_def) + from this[unfolded s_holding_def, folded wq_def, unfolded eq_wq] + show ?thesis + by (unfold holdents_def s_holding_def, fold wq_def, insert eq_wq, simp) + next + case True + from h[unfolded this] + have "holding s th' cs" by (auto simp:holdents_def) + from held_unique[OF this holding_th_cs_s] + have "th' = th" . + with assms show ?thesis by auto + qed + } ultimately show ?thesis by auto +qed + +lemma cntCS_kept [simp]: + assumes "th' \ taker" + and "th' \ th" + shows "cntCS (e#s) th' = cntCS s th'" + by (unfold cntCS_def holdents_kept[OF assms], simp) + +lemma readys_kept1: + assumes "th' \ taker" + and "th' \ readys (e#s)" + shows "th' \ readys s" +proof - + { fix cs' + assume wait: "waiting s th' cs'" + have n_wait: "\ waiting (e#s) th' cs'" + using assms(2)[unfolded readys_def] by auto + have False + proof(cases "cs' = cs") + case False + with n_wait wait + show ?thesis + by (unfold s_waiting_def, fold wq_def, auto) + next + case True + have "th' \ set (th # rest) \ th' \ hd (th # rest)" + using wait[unfolded True s_waiting_def, folded wq_def, unfolded wq_s_cs] . + moreover have "\ (th' \ set rest \ th' \ hd (taker # rest'))" + using n_wait[unfolded True s_waiting_def, folded wq_def, + unfolded wq_es_cs set_wq', unfolded eq_wq'] . + ultimately have "th' = taker" by auto + with assms(1) + show ?thesis by simp + qed + } with assms(2) show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_kept2: + assumes "th' \ taker" + and "th' \ readys s" + shows "th' \ readys (e#s)" +proof - + { fix cs' + assume wait: "waiting (e#s) th' cs'" + have n_wait: "\ waiting s th' cs'" + using assms(2)[unfolded readys_def] by auto + have False + proof(cases "cs' = cs") + case False + with n_wait wait + show ?thesis + by (unfold s_waiting_def, fold wq_def, auto) + next + case True + have "th' \ set rest \ th' \ hd (taker # rest')" + using wait [unfolded True s_waiting_def, folded wq_def, + unfolded wq_es_cs set_wq', unfolded eq_wq'] . + moreover have "\ (th' \ set (th # rest) \ th' \ hd (th # rest))" + using n_wait[unfolded True s_waiting_def, folded wq_def, unfolded wq_s_cs] . + ultimately have "th' = taker" by auto + with assms(1) + show ?thesis by simp + qed + } with assms(2) show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_simp [simp]: + assumes "th' \ taker" + shows "(th' \ readys (e#s)) = (th' \ readys s)" + using readys_kept1[OF assms] readys_kept2[OF assms] + by metis + +lemma cnp_cnv_cncs_kept: + assumes "cntP s th' = cntV s th' + cntCS s th' + pvD s th'" + shows "cntP (e#s) th' = cntV (e#s) th' + cntCS (e#s) th' + pvD (e#s) th'" +proof - + { assume eq_th': "th' = taker" + have ?thesis + apply (unfold eq_th' pvD_taker_es cntCS_es_taker) + by (insert neq_taker_th assms[unfolded eq_th'], unfold is_v, simp) + } moreover { + assume eq_th': "th' = th" + have ?thesis + apply (unfold eq_th' pvD_th_es cntCS_es_th) + by (insert assms[unfolded eq_th'], unfold is_v, simp) + } moreover { + assume h: "th' \ taker" "th' \ th" + have ?thesis using assms + apply (unfold cntCS_kept[OF h], insert h, unfold is_v, simp) + by (fold is_v, unfold pvD_def, simp) + } ultimately show ?thesis by metis +qed + +end + +context valid_trace_v_e +begin + +lemma holdents_es_th: + "holdents (e#s) th = holdents s th - {cs}" (is "?L = ?R") +proof - + { fix cs' + assume "cs' \ ?L" + hence "holding (e#s) th cs'" by (auto simp:holdents_def) + hence "cs' \ ?R" + proof(cases rule:holding_esE) + case 1 + thus ?thesis by (auto simp:holdents_def) + qed + } moreover { + fix cs' + assume "cs' \ ?R" + hence "cs' \ cs" "holding s th cs'" by (auto simp:holdents_def) + from holding_esI2[OF this] + have "cs' \ ?L" by (auto simp:holdents_def) + } ultimately show ?thesis by auto +qed + +lemma cntCS_es_th [simp]: "cntCS (e#s) th = cntCS s th - 1" +proof - + have "card (holdents s th - {cs}) = card (holdents s th) - 1" + proof - + have "cs \ holdents s th" using holding_th_cs_s + by (auto simp:holdents_def) + moreover have "finite (holdents s th)" + by (simp add: finite_holdents) + ultimately show ?thesis by auto + qed + thus ?thesis by (unfold cntCS_def holdents_es_th) +qed + +lemma holdents_kept: + assumes "th' \ th" + shows "holdents (e#s) th' = holdents s th'" (is "?L = ?R") +proof - + { fix cs' + assume h: "cs' \ ?L" + have "cs' \ ?R" + proof(cases "cs' = cs") + case False + hence eq_wq: "wq (e#s) cs' = wq s cs'" by simp + from h have "holding (e#s) th' cs'" by (auto simp:holdents_def) + from this[unfolded s_holding_def, folded wq_def, unfolded eq_wq] + show ?thesis + by (unfold holdents_def s_holding_def, fold wq_def, auto) + next + case True + from h[unfolded this] + have "holding (e#s) th' cs" by (auto simp:holdents_def) + from this[unfolded s_holding_def, folded wq_def, + unfolded wq_es_cs nil_wq'] + show ?thesis by auto + qed + } moreover { + fix cs' + assume h: "cs' \ ?R" + have "cs' \ ?L" + proof(cases "cs' = cs") + case False + hence eq_wq: "wq (e#s) cs' = wq s cs'" by simp + from h have "holding s th' cs'" by (auto simp:holdents_def) + from this[unfolded s_holding_def, folded wq_def, unfolded eq_wq] + show ?thesis + by (unfold holdents_def s_holding_def, fold wq_def, insert eq_wq, simp) + next + case True + from h[unfolded this] + have "holding s th' cs" by (auto simp:holdents_def) + from held_unique[OF this holding_th_cs_s] + have "th' = th" . + with assms show ?thesis by auto + qed + } ultimately show ?thesis by auto +qed + +lemma cntCS_kept [simp]: + assumes "th' \ th" + shows "cntCS (e#s) th' = cntCS s th'" + by (unfold cntCS_def holdents_kept[OF assms], simp) + +lemma readys_kept1: + assumes "th' \ readys (e#s)" + shows "th' \ readys s" +proof - + { fix cs' + assume wait: "waiting s th' cs'" + have n_wait: "\ waiting (e#s) th' cs'" + using assms(1)[unfolded readys_def] by auto + have False + proof(cases "cs' = cs") + case False + with n_wait wait + show ?thesis + by (unfold s_waiting_def, fold wq_def, auto) + next + case True + have "th' \ set (th # rest) \ th' \ hd (th # rest)" + using wait[unfolded True s_waiting_def, folded wq_def, unfolded wq_s_cs] . + hence "th' \ set rest" by auto + with set_wq' have "th' \ set wq'" by metis + with nil_wq' show ?thesis by simp + qed + } thus ?thesis using assms + by (unfold readys_def, auto) +qed + +lemma readys_kept2: + assumes "th' \ readys s" + shows "th' \ readys (e#s)" +proof - + { fix cs' + assume wait: "waiting (e#s) th' cs'" + have n_wait: "\ waiting s th' cs'" + using assms[unfolded readys_def] by auto + have False + proof(cases "cs' = cs") + case False + with n_wait wait + show ?thesis + by (unfold s_waiting_def, fold wq_def, auto) + next + case True + have "th' \ set [] \ th' \ hd []" + using wait[unfolded True s_waiting_def, folded wq_def, + unfolded wq_es_cs nil_wq'] . + thus ?thesis by simp + qed + } with assms show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_simp [simp]: + shows "(th' \ readys (e#s)) = (th' \ readys s)" + using readys_kept1[OF assms] readys_kept2[OF assms] + by metis + +lemma cnp_cnv_cncs_kept: + assumes "cntP s th' = cntV s th' + cntCS s th' + pvD s th'" + shows "cntP (e#s) th' = cntV (e#s) th' + cntCS (e#s) th' + pvD (e#s) th'" +proof - + { + assume eq_th': "th' = th" + have ?thesis + apply (unfold eq_th' pvD_th_es cntCS_es_th) + by (insert assms[unfolded eq_th'], unfold is_v, simp) + } moreover { + assume h: "th' \ th" + have ?thesis using assms + apply (unfold cntCS_kept[OF h], insert h, unfold is_v, simp) + by (fold is_v, unfold pvD_def, simp) + } ultimately show ?thesis by metis +qed + +end + +context valid_trace_v +begin + +lemma cnp_cnv_cncs_kept: + assumes "cntP s th' = cntV s th' + cntCS s th' + pvD s th'" + shows "cntP (e#s) th' = cntV (e#s) th' + cntCS (e#s) th' + pvD (e#s) th'" +proof(cases "rest = []") + case True + then interpret vt: valid_trace_v_e by (unfold_locales, simp) + show ?thesis using assms using vt.cnp_cnv_cncs_kept by blast +next + case False + then interpret vt: valid_trace_v_n by (unfold_locales, simp) + show ?thesis using assms using vt.cnp_cnv_cncs_kept by blast +qed + +end + +context valid_trace_create +begin + +lemma th_not_live_s [simp]: "th \ threads s" +proof - + from pip_e[unfolded is_create] + show ?thesis by (cases, simp) +qed + +lemma th_not_ready_s [simp]: "th \ readys s" + using th_not_live_s by (unfold readys_def, simp) + +lemma th_live_es [simp]: "th \ threads (e#s)" + by (unfold is_create, simp) + +lemma not_waiting_th_s [simp]: "\ waiting s th cs'" +proof + assume "waiting s th cs'" + from this[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp] + have "th \ set (wq s cs')" by auto + from wq_threads[OF this] have "th \ threads s" . + with th_not_live_s show False by simp +qed + +lemma not_holding_th_s [simp]: "\ holding s th cs'" +proof + assume "holding s th cs'" + from this[unfolded s_holding_def, folded wq_def, unfolded wq_neq_simp] + have "th \ set (wq s cs')" by auto + from wq_threads[OF this] have "th \ threads s" . + with th_not_live_s show False by simp +qed + +lemma not_waiting_th_es [simp]: "\ waiting (e#s) th cs'" +proof + assume "waiting (e # s) th cs'" + from this[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp] + have "th \ set (wq s cs')" by auto + from wq_threads[OF this] have "th \ threads s" . + with th_not_live_s show False by simp +qed + +lemma not_holding_th_es [simp]: "\ holding (e#s) th cs'" +proof + assume "holding (e # s) th cs'" + from this[unfolded s_holding_def, folded wq_def, unfolded wq_neq_simp] + have "th \ set (wq s cs')" by auto + from wq_threads[OF this] have "th \ threads s" . + with th_not_live_s show False by simp +qed + +lemma ready_th_es [simp]: "th \ readys (e#s)" + by (simp add:readys_def) + +lemma holdents_th_s: "holdents s th = {}" + by (unfold holdents_def, auto) + +lemma holdents_th_es: "holdents (e#s) th = {}" + by (unfold holdents_def, auto) + +lemma cntCS_th_s [simp]: "cntCS s th = 0" + by (unfold cntCS_def, simp add:holdents_th_s) + +lemma cntCS_th_es [simp]: "cntCS (e#s) th = 0" + by (unfold cntCS_def, simp add:holdents_th_es) + +lemma pvD_th_s [simp]: "pvD s th = 0" + by (unfold pvD_def, simp) + +lemma pvD_th_es [simp]: "pvD (e#s) th = 0" + by (unfold pvD_def, simp) + +lemma holdents_kept: + assumes "th' \ th" + shows "holdents (e#s) th' = holdents s th'" (is "?L = ?R") +proof - + { fix cs' + assume h: "cs' \ ?L" + hence "cs' \ ?R" + by (unfold holdents_def s_holding_def, fold wq_def, + unfold wq_neq_simp, auto) + } moreover { + fix cs' + assume h: "cs' \ ?R" + hence "cs' \ ?L" + by (unfold holdents_def s_holding_def, fold wq_def, + unfold wq_neq_simp, auto) + } ultimately show ?thesis by auto +qed + +lemma cntCS_kept [simp]: + assumes "th' \ th" + shows "cntCS (e#s) th' = cntCS s th'" (is "?L = ?R") + using holdents_kept[OF assms] + by (unfold cntCS_def, simp) + +lemma readys_kept1: + assumes "th' \ th" + and "th' \ readys (e#s)" + shows "th' \ readys s" +proof - + { fix cs' + assume wait: "waiting s th' cs'" + have n_wait: "\ waiting (e#s) th' cs'" + using assms by (auto simp:readys_def) + from wait[unfolded s_waiting_def, folded wq_def] + n_wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp] + have False by auto + } thus ?thesis using assms + by (unfold readys_def, auto) +qed + +lemma readys_kept2: + assumes "th' \ th" + and "th' \ readys s" + shows "th' \ readys (e#s)" +proof - + { fix cs' + assume wait: "waiting (e#s) th' cs'" + have n_wait: "\ waiting s th' cs'" + using assms(2) by (auto simp:readys_def) + from wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp] + n_wait[unfolded s_waiting_def, folded wq_def] + have False by auto + } with assms show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_simp [simp]: + assumes "th' \ th" + shows "(th' \ readys (e#s)) = (th' \ readys s)" + using readys_kept1[OF assms] readys_kept2[OF assms] + by metis + +lemma pvD_kept [simp]: + assumes "th' \ th" + shows "pvD (e#s) th' = pvD s th'" + using assms + by (unfold pvD_def, simp) + +lemma cnp_cnv_cncs_kept: + assumes "cntP s th' = cntV s th' + cntCS s th' + pvD s th'" + shows "cntP (e#s) th' = cntV (e#s) th' + cntCS (e#s) th' + pvD (e#s) th'" +proof - + { + assume eq_th': "th' = th" + have ?thesis using assms + by (unfold eq_th', simp, unfold is_create, simp) + } moreover { + assume h: "th' \ th" + hence ?thesis using assms + by (simp, simp add:is_create) + } ultimately show ?thesis by metis +qed + +end + +context valid_trace_exit +begin + +lemma th_live_s [simp]: "th \ threads s" +proof - + from pip_e[unfolded is_exit] + show ?thesis + by (cases, unfold runing_def readys_def, simp) +qed + +lemma th_ready_s [simp]: "th \ readys s" +proof - + from pip_e[unfolded is_exit] + show ?thesis + by (cases, unfold runing_def, simp) +qed + +lemma th_not_live_es [simp]: "th \ threads (e#s)" + by (unfold is_exit, simp) + +lemma not_holding_th_s [simp]: "\ holding s th cs'" +proof - + from pip_e[unfolded is_exit] + show ?thesis + by (cases, unfold holdents_def, auto) +qed + +lemma cntCS_th_s [simp]: "cntCS s th = 0" +proof - + from pip_e[unfolded is_exit] + show ?thesis + by (cases, unfold cntCS_def, simp) +qed + +lemma not_holding_th_es [simp]: "\ holding (e#s) th cs'" +proof + assume "holding (e # s) th cs'" + from this[unfolded s_holding_def, folded wq_def, unfolded wq_neq_simp] + have "holding s th cs'" + by (unfold s_holding_def, fold wq_def, auto) + with not_holding_th_s + show False by simp +qed + +lemma ready_th_es [simp]: "th \ readys (e#s)" + by (simp add:readys_def) + +lemma holdents_th_s: "holdents s th = {}" + by (unfold holdents_def, auto) + +lemma holdents_th_es: "holdents (e#s) th = {}" + by (unfold holdents_def, auto) + +lemma cntCS_th_es [simp]: "cntCS (e#s) th = 0" + by (unfold cntCS_def, simp add:holdents_th_es) + +lemma pvD_th_s [simp]: "pvD s th = 0" + by (unfold pvD_def, simp) + +lemma pvD_th_es [simp]: "pvD (e#s) th = 0" + by (unfold pvD_def, simp) + +lemma holdents_kept: + assumes "th' \ th" + shows "holdents (e#s) th' = holdents s th'" (is "?L = ?R") +proof - + { fix cs' + assume h: "cs' \ ?L" + hence "cs' \ ?R" + by (unfold holdents_def s_holding_def, fold wq_def, + unfold wq_neq_simp, auto) + } moreover { + fix cs' + assume h: "cs' \ ?R" + hence "cs' \ ?L" + by (unfold holdents_def s_holding_def, fold wq_def, + unfold wq_neq_simp, auto) + } ultimately show ?thesis by auto +qed + +lemma cntCS_kept [simp]: + assumes "th' \ th" + shows "cntCS (e#s) th' = cntCS s th'" (is "?L = ?R") + using holdents_kept[OF assms] + by (unfold cntCS_def, simp) + +lemma readys_kept1: + assumes "th' \ th" + and "th' \ readys (e#s)" + shows "th' \ readys s" +proof - + { fix cs' + assume wait: "waiting s th' cs'" + have n_wait: "\ waiting (e#s) th' cs'" + using assms by (auto simp:readys_def) + from wait[unfolded s_waiting_def, folded wq_def] + n_wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp] + have False by auto + } thus ?thesis using assms + by (unfold readys_def, auto) +qed + +lemma readys_kept2: + assumes "th' \ th" + and "th' \ readys s" + shows "th' \ readys (e#s)" +proof - + { fix cs' + assume wait: "waiting (e#s) th' cs'" + have n_wait: "\ waiting s th' cs'" + using assms(2) by (auto simp:readys_def) + from wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp] + n_wait[unfolded s_waiting_def, folded wq_def] + have False by auto + } with assms show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_simp [simp]: + assumes "th' \ th" + shows "(th' \ readys (e#s)) = (th' \ readys s)" + using readys_kept1[OF assms] readys_kept2[OF assms] + by metis + +lemma pvD_kept [simp]: + assumes "th' \ th" + shows "pvD (e#s) th' = pvD s th'" + using assms + by (unfold pvD_def, simp) + +lemma cnp_cnv_cncs_kept: + assumes "cntP s th' = cntV s th' + cntCS s th' + pvD s th'" + shows "cntP (e#s) th' = cntV (e#s) th' + cntCS (e#s) th' + pvD (e#s) th'" +proof - + { + assume eq_th': "th' = th" + have ?thesis using assms + by (unfold eq_th', simp, unfold is_exit, simp) + } moreover { + assume h: "th' \ th" + hence ?thesis using assms + by (simp, simp add:is_exit) + } ultimately show ?thesis by metis +qed + +end + +context valid_trace_set +begin + +lemma th_live_s [simp]: "th \ threads s" +proof - + from pip_e[unfolded is_set] + show ?thesis + by (cases, unfold runing_def readys_def, simp) +qed + +lemma th_ready_s [simp]: "th \ readys s" +proof - + from pip_e[unfolded is_set] + show ?thesis + by (cases, unfold runing_def, simp) +qed + +lemma th_not_live_es [simp]: "th \ threads (e#s)" + by (unfold is_set, simp) + + +lemma holdents_kept: + shows "holdents (e#s) th' = holdents s th'" (is "?L = ?R") +proof - + { fix cs' + assume h: "cs' \ ?L" + hence "cs' \ ?R" + by (unfold holdents_def s_holding_def, fold wq_def, + unfold wq_neq_simp, auto) + } moreover { + fix cs' + assume h: "cs' \ ?R" + hence "cs' \ ?L" + by (unfold holdents_def s_holding_def, fold wq_def, + unfold wq_neq_simp, auto) + } ultimately show ?thesis by auto +qed + +lemma cntCS_kept [simp]: + shows "cntCS (e#s) th' = cntCS s th'" (is "?L = ?R") + using holdents_kept + by (unfold cntCS_def, simp) + +lemma threads_kept[simp]: + "threads (e#s) = threads s" + by (unfold is_set, simp) + +lemma readys_kept1: + assumes "th' \ readys (e#s)" + shows "th' \ readys s" +proof - + { fix cs' + assume wait: "waiting s th' cs'" + have n_wait: "\ waiting (e#s) th' cs'" + using assms by (auto simp:readys_def) + from wait[unfolded s_waiting_def, folded wq_def] + n_wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp] + have False by auto + } moreover have "th' \ threads s" + using assms[unfolded readys_def] by auto + ultimately show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_kept2: + assumes "th' \ readys s" + shows "th' \ readys (e#s)" +proof - + { fix cs' + assume wait: "waiting (e#s) th' cs'" + have n_wait: "\ waiting s th' cs'" + using assms by (auto simp:readys_def) + from wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp] + n_wait[unfolded s_waiting_def, folded wq_def] + have False by auto + } with assms show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_simp [simp]: + shows "(th' \ readys (e#s)) = (th' \ readys s)" + using readys_kept1 readys_kept2 + by metis + +lemma pvD_kept [simp]: + shows "pvD (e#s) th' = pvD s th'" + by (unfold pvD_def, simp) + +lemma cnp_cnv_cncs_kept: + assumes "cntP s th' = cntV s th' + cntCS s th' + pvD s th'" + shows "cntP (e#s) th' = cntV (e#s) th' + cntCS (e#s) th' + pvD (e#s) th'" + using assms + by (unfold is_set, simp, fold is_set, simp) + +end + +context valid_trace +begin + +lemma cnp_cnv_cncs: "cntP s th' = cntV s th' + cntCS s th' + pvD s th'" +proof(induct rule:ind) + case Nil + thus ?case + by (unfold cntP_def cntV_def pvD_def cntCS_def holdents_def + s_holding_def, simp) +next + case (Cons s e) + interpret vt_e: valid_trace_e s e using Cons by simp + show ?case + proof(cases e) + case (Create th prio) + interpret vt_create: valid_trace_create s e th prio + using Create by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_create.cnp_cnv_cncs_kept) + next + case (Exit th) + interpret vt_exit: valid_trace_exit s e th + using Exit by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_exit.cnp_cnv_cncs_kept) + next + case (P th cs) + interpret vt_p: valid_trace_p s e th cs using P by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_p.cnp_cnv_cncs_kept) + next + case (V th cs) + interpret vt_v: valid_trace_v s e th cs using V by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_v.cnp_cnv_cncs_kept) + next + case (Set th prio) + interpret vt_set: valid_trace_set s e th prio + using Set by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_set.cnp_cnv_cncs_kept) + qed +qed + +lemma not_thread_holdents: + assumes not_in: "th \ threads s" + shows "holdents s th = {}" +proof - + { fix cs + assume "cs \ holdents s th" + hence "holding s th cs" by (auto simp:holdents_def) + from this[unfolded s_holding_def, folded wq_def] + have "th \ set (wq s cs)" by auto + with wq_threads have "th \ threads s" by auto + with assms + have False by simp + } thus ?thesis by auto +qed + +lemma not_thread_cncs: + assumes not_in: "th \ threads s" + shows "cntCS s th = 0" + using not_thread_holdents[OF assms] + by (simp add:cntCS_def) + +lemma cnp_cnv_eq: + assumes "th \ threads s" + shows "cntP s th = cntV s th" + using assms cnp_cnv_cncs not_thread_cncs pvD_def + by (auto) + +lemma runing_unique: + assumes runing_1: "th1 \ runing s" + and runing_2: "th2 \ runing s" + shows "th1 = th2" +proof - + from runing_1 and runing_2 have "cp s th1 = cp s th2" + unfolding runing_def by auto + from this[unfolded cp_alt_def] + have eq_max: + "Max (the_preced s ` {th'. Th th' \ subtree (RAG s) (Th th1)}) = + Max (the_preced s ` {th'. Th th' \ subtree (RAG s) (Th th2)})" + (is "Max ?L = Max ?R") . + have "Max ?L \ ?L" + proof(rule Max_in) + show "finite ?L" by (simp add: finite_subtree_threads) + next + show "?L \ {}" using subtree_def by fastforce + qed + then obtain th1' where + h_1: "Th th1' \ subtree (RAG s) (Th th1)" "the_preced s th1' = Max ?L" + by auto + have "Max ?R \ ?R" + proof(rule Max_in) + show "finite ?R" by (simp add: finite_subtree_threads) + next + show "?R \ {}" using subtree_def by fastforce + qed + then obtain th2' where + h_2: "Th th2' \ subtree (RAG s) (Th th2)" "the_preced s th2' = Max ?R" + by auto + have "th1' = th2'" + proof(rule preced_unique) + from h_1(1) + show "th1' \ threads s" + proof(cases rule:subtreeE) + case 1 + hence "th1' = th1" by simp + with runing_1 show ?thesis by (auto simp:runing_def readys_def) + next + case 2 + from this(2) + have "(Th th1', Th th1) \ (RAG s)^+" by (auto simp:ancestors_def) + from tranclD[OF this] + have "(Th th1') \ Domain (RAG s)" by auto + from dm_RAG_threads[OF this] show ?thesis . + qed + next + from h_2(1) + show "th2' \ threads s" + proof(cases rule:subtreeE) + case 1 + hence "th2' = th2" by simp + with runing_2 show ?thesis by (auto simp:runing_def readys_def) + next + case 2 + from this(2) + have "(Th th2', Th th2) \ (RAG s)^+" by (auto simp:ancestors_def) + from tranclD[OF this] + have "(Th th2') \ Domain (RAG s)" by auto + from dm_RAG_threads[OF this] show ?thesis . + qed + next + have "the_preced s th1' = the_preced s th2'" + using eq_max h_1(2) h_2(2) by metis + thus "preced th1' s = preced th2' s" by (simp add:the_preced_def) + qed + from h_1(1)[unfolded this] + have star1: "(Th th2', Th th1) \ (RAG s)^*" by (auto simp:subtree_def) + from h_2(1)[unfolded this] + have star2: "(Th th2', Th th2) \ (RAG s)^*" by (auto simp:subtree_def) + from star_rpath[OF star1] obtain xs1 + where rp1: "rpath (RAG s) (Th th2') xs1 (Th th1)" + by auto + from star_rpath[OF star2] obtain xs2 + where rp2: "rpath (RAG s) (Th th2') xs2 (Th th2)" + by auto + from rp1 rp2 + show ?thesis + proof(cases) + case (less_1 xs') + moreover have "xs' = []" + proof(rule ccontr) + assume otherwise: "xs' \ []" + from rpath_plus[OF less_1(3) this] + have "(Th th1, Th th2) \ (RAG s)\<^sup>+" . + from tranclD[OF this] + obtain cs where "waiting s th1 cs" + by (unfold s_RAG_def, fold waiting_eq, auto) + with runing_1 show False + by (unfold runing_def readys_def, auto) + qed + ultimately have "xs2 = xs1" by simp + from rpath_dest_eq[OF rp1 rp2[unfolded this]] + show ?thesis by simp + next + case (less_2 xs') + moreover have "xs' = []" + proof(rule ccontr) + assume otherwise: "xs' \ []" + from rpath_plus[OF less_2(3) this] + have "(Th th2, Th th1) \ (RAG s)\<^sup>+" . + from tranclD[OF this] + obtain cs where "waiting s th2 cs" + by (unfold s_RAG_def, fold waiting_eq, auto) + with runing_2 show False + by (unfold runing_def readys_def, auto) + qed + ultimately have "xs2 = xs1" by simp + from rpath_dest_eq[OF rp1 rp2[unfolded this]] + show ?thesis by simp + qed +qed + +lemma card_runing: "card (runing s) \ 1" +proof(cases "runing s = {}") + case True + thus ?thesis by auto +next + case False + then obtain th where [simp]: "th \ runing s" by auto + from runing_unique[OF this] + have "runing s = {th}" by auto + thus ?thesis by auto +qed + +lemma create_pre: + assumes stp: "step s e" + and not_in: "th \ threads s" + and is_in: "th \ threads (e#s)" + obtains prio where "e = Create th prio" +proof - + from assms + show ?thesis + proof(cases) + case (thread_create thread prio) + with is_in not_in have "e = Create th prio" by simp + from that[OF this] show ?thesis . + next + case (thread_exit thread) + with assms show ?thesis by (auto intro!:that) + next + case (thread_P thread) + with assms show ?thesis by (auto intro!:that) + next + case (thread_V thread) + with assms show ?thesis by (auto intro!:that) + next + case (thread_set thread) + with assms show ?thesis by (auto intro!:that) + qed +qed + +lemma eq_pv_children: + assumes eq_pv: "cntP s th = cntV s th" + shows "children (RAG s) (Th th) = {}" +proof - + from cnp_cnv_cncs and eq_pv + have "cntCS s th = 0" + by (auto split:if_splits) + from this[unfolded cntCS_def holdents_alt_def] + have card_0: "card (the_cs ` children (RAG s) (Th th)) = 0" . + have "finite (the_cs ` children (RAG s) (Th th))" + by (simp add: fsbtRAGs.finite_children) + from card_0[unfolded card_0_eq[OF this]] + show ?thesis by auto +qed + +lemma eq_pv_holdents: + assumes eq_pv: "cntP s th = cntV s th" + shows "holdents s th = {}" + by (unfold holdents_alt_def eq_pv_children[OF assms], simp) + +lemma eq_pv_subtree: + assumes eq_pv: "cntP s th = cntV s th" + shows "subtree (RAG s) (Th th) = {Th th}" + using eq_pv_children[OF assms] + by (unfold subtree_children, simp) + +end + +lemma cp_gen_alt_def: + "cp_gen s = (Max \ (\x. (the_preced s \ the_thread) ` subtree (tRAG s) x))" + by (auto simp:cp_gen_def) + +lemma tRAG_nodeE: + assumes "(n1, n2) \ tRAG s" + obtains th1 th2 where "n1 = Th th1" "n2 = Th th2" + using assms + by (auto simp: tRAG_def wRAG_def hRAG_def tRAG_def) + +lemma subtree_nodeE: + assumes "n \ subtree (tRAG s) (Th th)" + obtains th1 where "n = Th th1" +proof - + show ?thesis + proof(rule subtreeE[OF assms]) + assume "n = Th th" + from that[OF this] show ?thesis . + next + assume "Th th \ ancestors (tRAG s) n" + hence "(n, Th th) \ (tRAG s)^+" by (auto simp:ancestors_def) + hence "\ th1. n = Th th1" + proof(induct) + case (base y) + from tRAG_nodeE[OF this] show ?case by metis + next + case (step y z) + thus ?case by auto + qed + with that show ?thesis by auto + qed +qed + +lemma tRAG_star_RAG: "(tRAG s)^* \ (RAG s)^*" +proof - + have "(wRAG s O hRAG s)^* \ (RAG s O RAG s)^*" + by (rule rtrancl_mono, auto simp:RAG_split) + also have "... \ ((RAG s)^*)^*" + by (rule rtrancl_mono, auto) + also have "... = (RAG s)^*" by simp + finally show ?thesis by (unfold tRAG_def, simp) +qed + +lemma tRAG_subtree_RAG: "subtree (tRAG s) x \ subtree (RAG s) x" +proof - + { fix a + assume "a \ subtree (tRAG s) x" + hence "(a, x) \ (tRAG s)^*" by (auto simp:subtree_def) + with tRAG_star_RAG + have "(a, x) \ (RAG s)^*" by auto + hence "a \ subtree (RAG s) x" by (auto simp:subtree_def) + } thus ?thesis by auto +qed + +lemma tRAG_trancl_eq: + "{th'. (Th th', Th th) \ (tRAG s)^+} = + {th'. (Th th', Th th) \ (RAG s)^+}" + (is "?L = ?R") +proof - + { fix th' + assume "th' \ ?L" + hence "(Th th', Th th) \ (tRAG s)^+" by auto + from tranclD[OF this] + obtain z where h: "(Th th', z) \ tRAG s" "(z, Th th) \ (tRAG s)\<^sup>*" by auto + from tRAG_subtree_RAG and this(2) + have "(z, Th th) \ (RAG s)^*" by (meson subsetCE tRAG_star_RAG) + moreover from h(1) have "(Th th', z) \ (RAG s)^+" using tRAG_alt_def by auto + ultimately have "th' \ ?R" by auto + } moreover + { fix th' + assume "th' \ ?R" + hence "(Th th', Th th) \ (RAG s)^+" by (auto) + from plus_rpath[OF this] + obtain xs where rp: "rpath (RAG s) (Th th') xs (Th th)" "xs \ []" by auto + hence "(Th th', Th th) \ (tRAG s)^+" + proof(induct xs arbitrary:th' th rule:length_induct) + case (1 xs th' th) + then obtain x1 xs1 where Cons1: "xs = x1#xs1" by (cases xs, auto) + show ?case + proof(cases "xs1") + case Nil + from 1(2)[unfolded Cons1 Nil] + have rp: "rpath (RAG s) (Th th') [x1] (Th th)" . + hence "(Th th', x1) \ (RAG s)" + by (cases, auto) + then obtain cs where "x1 = Cs cs" + by (unfold s_RAG_def, auto) + from rpath_nnl_lastE[OF rp[unfolded this]] + show ?thesis by auto + next + case (Cons x2 xs2) + from 1(2)[unfolded Cons1[unfolded this]] + have rp: "rpath (RAG s) (Th th') (x1 # x2 # xs2) (Th th)" . + from rpath_edges_on[OF this] + have eds: "edges_on (Th th' # x1 # x2 # xs2) \ RAG s" . + have "(Th th', x1) \ edges_on (Th th' # x1 # x2 # xs2)" + by (simp add: edges_on_unfold) + with eds have rg1: "(Th th', x1) \ RAG s" by auto + then obtain cs1 where eq_x1: "x1 = Cs cs1" by (unfold s_RAG_def, auto) + have "(x1, x2) \ edges_on (Th th' # x1 # x2 # xs2)" + by (simp add: edges_on_unfold) + from this eds + have rg2: "(x1, x2) \ RAG s" by auto + from this[unfolded eq_x1] + obtain th1 where eq_x2: "x2 = Th th1" by (unfold s_RAG_def, auto) + from rg1[unfolded eq_x1] rg2[unfolded eq_x1 eq_x2] + have rt1: "(Th th', Th th1) \ tRAG s" by (unfold tRAG_alt_def, auto) + from rp have "rpath (RAG s) x2 xs2 (Th th)" + by (elim rpath_ConsE, simp) + from this[unfolded eq_x2] have rp': "rpath (RAG s) (Th th1) xs2 (Th th)" . + show ?thesis + proof(cases "xs2 = []") + case True + from rpath_nilE[OF rp'[unfolded this]] + have "th1 = th" by auto + from rt1[unfolded this] show ?thesis by auto + next + case False + from 1(1)[rule_format, OF _ rp' this, unfolded Cons1 Cons] + have "(Th th1, Th th) \ (tRAG s)\<^sup>+" by simp + with rt1 show ?thesis by auto + qed + qed + qed + hence "th' \ ?L" by auto + } ultimately show ?thesis by blast +qed + +lemma tRAG_trancl_eq_Th: + "{Th th' | th'. (Th th', Th th) \ (tRAG s)^+} = + {Th th' | th'. (Th th', Th th) \ (RAG s)^+}" + using tRAG_trancl_eq by auto + +lemma dependants_alt_def: + "dependants s th = {th'. (Th th', Th th) \ (tRAG s)^+}" + by (metis eq_RAG s_dependants_def tRAG_trancl_eq) + +lemma dependants_alt_def1: + "dependants (s::state) th = {th'. (Th th', Th th) \ (RAG s)^+}" + using dependants_alt_def tRAG_trancl_eq by auto + +context valid_trace +begin +lemma count_eq_RAG_plus: + assumes "cntP s th = cntV s th" + shows "{th'. (Th th', Th th) \ (RAG s)^+} = {}" +proof(rule ccontr) + assume otherwise: "{th'. (Th th', Th th) \ (RAG s)\<^sup>+} \ {}" + then obtain th' where "(Th th', Th th) \ (RAG s)^+" by auto + from tranclD2[OF this] + obtain z where "z \ children (RAG s) (Th th)" + by (auto simp:children_def) + with eq_pv_children[OF assms] + show False by simp +qed + +lemma eq_pv_dependants: + assumes eq_pv: "cntP s th = cntV s th" + shows "dependants s th = {}" +proof - + from count_eq_RAG_plus[OF assms, folded dependants_alt_def1] + show ?thesis . +qed + +end + +lemma eq_dependants: "dependants (wq s) = dependants s" + by (simp add: s_dependants_abv wq_def) + +context valid_trace +begin + +lemma count_eq_tRAG_plus: + assumes "cntP s th = cntV s th" + shows "{th'. (Th th', Th th) \ (tRAG s)^+} = {}" + using assms eq_pv_dependants dependants_alt_def eq_dependants by auto + +lemma count_eq_RAG_plus_Th: + assumes "cntP s th = cntV s th" + shows "{Th th' | th'. (Th th', Th th) \ (RAG s)^+} = {}" + using count_eq_RAG_plus[OF assms] by auto + +lemma count_eq_tRAG_plus_Th: + assumes "cntP s th = cntV s th" + shows "{Th th' | th'. (Th th', Th th) \ (tRAG s)^+} = {}" + using count_eq_tRAG_plus[OF assms] by auto +end + +lemma inj_the_preced: + "inj_on (the_preced s) (threads s)" + by (metis inj_onI preced_unique the_preced_def) + +lemma tRAG_Field: + "Field (tRAG s) \ Field (RAG s)" + by (unfold tRAG_alt_def Field_def, auto) + +lemma tRAG_ancestorsE: + assumes "x \ ancestors (tRAG s) u" + obtains th where "x = Th th" +proof - + from assms have "(u, x) \ (tRAG s)^+" + by (unfold ancestors_def, auto) + from tranclE[OF this] obtain c where "(c, x) \ tRAG s" by auto + then obtain th where "x = Th th" + by (unfold tRAG_alt_def, auto) + from that[OF this] show ?thesis . +qed + +lemma tRAG_mono: + assumes "RAG s' \ RAG s" + shows "tRAG s' \ tRAG s" + using assms + by (unfold tRAG_alt_def, auto) + +lemma holding_next_thI: + assumes "holding s th cs" + and "length (wq s cs) > 1" + obtains th' where "next_th s th cs th'" +proof - + from assms(1)[folded holding_eq, unfolded cs_holding_def] + have " th \ set (wq s cs) \ th = hd (wq s cs)" + by (unfold s_holding_def, fold wq_def, auto) + then obtain rest where h1: "wq s cs = th#rest" + by (cases "wq s cs", auto) + with assms(2) have h2: "rest \ []" by auto + let ?th' = "hd (SOME q. distinct q \ set q = set rest)" + have "next_th s th cs ?th'" using h1(1) h2 + by (unfold next_th_def, auto) + from that[OF this] show ?thesis . +qed + +lemma RAG_tRAG_transfer: + assumes "vt s'" + assumes "RAG s = RAG s' \ {(Th th, Cs cs)}" + and "(Cs cs, Th th'') \ RAG s'" + shows "tRAG s = tRAG s' \ {(Th th, Th th'')}" (is "?L = ?R") +proof - + interpret vt_s': valid_trace "s'" using assms(1) + by (unfold_locales, simp) + { fix n1 n2 + assume "(n1, n2) \ ?L" + from this[unfolded tRAG_alt_def] + obtain th1 th2 cs' where + h: "n1 = Th th1" "n2 = Th th2" + "(Th th1, Cs cs') \ RAG s" + "(Cs cs', Th th2) \ RAG s" by auto + from h(4) and assms(2) have cs_in: "(Cs cs', Th th2) \ RAG s'" by auto + from h(3) and assms(2) + have "(Th th1, Cs cs') = (Th th, Cs cs) \ + (Th th1, Cs cs') \ RAG s'" by auto + hence "(n1, n2) \ ?R" + proof + assume h1: "(Th th1, Cs cs') = (Th th, Cs cs)" + hence eq_th1: "th1 = th" by simp + moreover have "th2 = th''" + proof - + from h1 have "cs' = cs" by simp + from assms(3) cs_in[unfolded this] + show ?thesis using vt_s'.unique_RAG by auto + qed + ultimately show ?thesis using h(1,2) by auto + next + assume "(Th th1, Cs cs') \ RAG s'" + with cs_in have "(Th th1, Th th2) \ tRAG s'" + by (unfold tRAG_alt_def, auto) + from this[folded h(1, 2)] show ?thesis by auto + qed + } moreover { + fix n1 n2 + assume "(n1, n2) \ ?R" + hence "(n1, n2) \tRAG s' \ (n1, n2) = (Th th, Th th'')" by auto + hence "(n1, n2) \ ?L" + proof + assume "(n1, n2) \ tRAG s'" + moreover have "... \ ?L" + proof(rule tRAG_mono) + show "RAG s' \ RAG s" by (unfold assms(2), auto) + qed + ultimately show ?thesis by auto + next + assume eq_n: "(n1, n2) = (Th th, Th th'')" + from assms(2, 3) have "(Cs cs, Th th'') \ RAG s" by auto + moreover have "(Th th, Cs cs) \ RAG s" using assms(2) by auto + ultimately show ?thesis + by (unfold eq_n tRAG_alt_def, auto) + qed + } ultimately show ?thesis by auto +qed + +context valid_trace +begin + +lemmas RAG_tRAG_transfer = RAG_tRAG_transfer[OF vt] + +lemma tRAG_subtree_eq: + "(subtree (tRAG s) (Th th)) = {Th th' | th'. Th th' \ (subtree (RAG s) (Th th))}" + (is "?L = ?R") +proof - + { fix n + assume h: "n \ ?L" + hence "n \ ?R" + by (smt mem_Collect_eq subsetCE subtree_def subtree_nodeE tRAG_subtree_RAG) + } moreover { + fix n + assume "n \ ?R" + then obtain th' where h: "n = Th th'" "(Th th', Th th) \ (RAG s)^*" + by (auto simp:subtree_def) + from rtranclD[OF this(2)] + have "n \ ?L" + proof + assume "Th th' \ Th th \ (Th th', Th th) \ (RAG s)\<^sup>+" + with h have "n \ {Th th' | th'. (Th th', Th th) \ (RAG s)^+}" by auto + thus ?thesis using subtree_def tRAG_trancl_eq by fastforce + qed (insert h, auto simp:subtree_def) + } ultimately show ?thesis by auto +qed + +lemma threads_set_eq: + "the_thread ` (subtree (tRAG s) (Th th)) = + {th'. Th th' \ (subtree (RAG s) (Th th))}" (is "?L = ?R") + by (auto intro:rev_image_eqI simp:tRAG_subtree_eq) + +lemma cp_alt_def1: + "cp s th = Max ((the_preced s o the_thread) ` (subtree (tRAG s) (Th th)))" +proof - + have "(the_preced s ` the_thread ` subtree (tRAG s) (Th th)) = + ((the_preced s \ the_thread) ` subtree (tRAG s) (Th th))" + by auto + thus ?thesis by (unfold cp_alt_def, fold threads_set_eq, auto) +qed + +lemma cp_gen_def_cond: + assumes "x = Th th" + shows "cp s th = cp_gen s (Th th)" +by (unfold cp_alt_def1 cp_gen_def, simp) + +lemma cp_gen_over_set: + assumes "\ x \ A. \ th. x = Th th" + shows "cp_gen s ` A = (cp s \ the_thread) ` A" +proof(rule f_image_eq) + fix a + assume "a \ A" + from assms[rule_format, OF this] + obtain th where eq_a: "a = Th th" by auto + show "cp_gen s a = (cp s \ the_thread) a" + by (unfold eq_a, simp, unfold cp_gen_def_cond[OF refl[of "Th th"]], simp) +qed + +lemma subtree_tRAG_thread: + assumes "th \ threads s" + shows "subtree (tRAG s) (Th th) \ Th ` threads s" (is "?L \ ?R") +proof - + have "?L = {Th th' |th'. Th th' \ subtree (RAG s) (Th th)}" + by (unfold tRAG_subtree_eq, simp) + also have "... \ ?R" + proof + fix x + assume "x \ {Th th' |th'. Th th' \ subtree (RAG s) (Th th)}" + then obtain th' where h: "x = Th th'" "Th th' \ subtree (RAG s) (Th th)" by auto + from this(2) + show "x \ ?R" + proof(cases rule:subtreeE) + case 1 + thus ?thesis by (simp add: assms h(1)) + next + case 2 + thus ?thesis by (metis ancestors_Field dm_RAG_threads h(1) image_eqI) + qed + qed + finally show ?thesis . +qed + +lemma readys_root: + assumes "th \ readys s" + shows "root (RAG s) (Th th)" +proof - + { fix x + assume "x \ ancestors (RAG s) (Th th)" + hence h: "(Th th, x) \ (RAG s)^+" by (auto simp:ancestors_def) + from tranclD[OF this] + obtain z where "(Th th, z) \ RAG s" by auto + with assms(1) have False + apply (case_tac z, auto simp:readys_def s_RAG_def s_waiting_def cs_waiting_def) + by (fold wq_def, blast) + } thus ?thesis by (unfold root_def, auto) +qed + +lemma readys_in_no_subtree: + assumes "th \ readys s" + and "th' \ th" + shows "Th th \ subtree (RAG s) (Th th')" +proof + assume "Th th \ subtree (RAG s) (Th th')" + thus False + proof(cases rule:subtreeE) + case 1 + with assms show ?thesis by auto + next + case 2 + with readys_root[OF assms(1)] + show ?thesis by (auto simp:root_def) + qed +qed + +lemma not_in_thread_isolated: + assumes "th \ threads s" + shows "(Th th) \ Field (RAG s)" +proof + assume "(Th th) \ Field (RAG s)" + with dm_RAG_threads and rg_RAG_threads assms + show False by (unfold Field_def, blast) +qed + +end + +definition detached :: "state \ thread \ bool" + where "detached s th \ (\(\ cs. holding s th cs)) \ (\(\cs. waiting s th cs))" + + +lemma detached_test: + shows "detached s th = (Th th \ Field (RAG s))" +apply(simp add: detached_def Field_def) +apply(simp add: s_RAG_def) +apply(simp add: s_holding_abv s_waiting_abv) +apply(simp add: Domain_iff Range_iff) +apply(simp add: wq_def) +apply(auto) +done + +context valid_trace +begin + +lemma detached_intro: + assumes eq_pv: "cntP s th = cntV s th" + shows "detached s th" +proof - + from eq_pv cnp_cnv_cncs + have "th \ readys s \ th \ threads s" by (auto simp:pvD_def) + thus ?thesis + proof + assume "th \ threads s" + with rg_RAG_threads dm_RAG_threads + show ?thesis + by (auto simp add: detached_def s_RAG_def s_waiting_abv + s_holding_abv wq_def Domain_iff Range_iff) + next + assume "th \ readys s" + moreover have "Th th \ Range (RAG s)" + proof - + from eq_pv_children[OF assms] + have "children (RAG s) (Th th) = {}" . + thus ?thesis + by (unfold children_def, auto) + qed + ultimately show ?thesis + by (auto simp add: detached_def s_RAG_def s_waiting_abv + s_holding_abv wq_def readys_def) + qed +qed + +lemma detached_elim: + assumes dtc: "detached s th" + shows "cntP s th = cntV s th" +proof - + have cncs_z: "cntCS s th = 0" + proof - + from dtc have "holdents s th = {}" + unfolding detached_def holdents_test s_RAG_def + by (simp add: s_waiting_abv wq_def s_holding_abv Domain_iff Range_iff) + thus ?thesis by (auto simp:cntCS_def) + qed + show ?thesis + proof(cases "th \ threads s") + case True + with dtc + have "th \ readys s" + by (unfold readys_def detached_def Field_def Domain_def Range_def, + auto simp:waiting_eq s_RAG_def) + with cncs_z show ?thesis using cnp_cnv_cncs by (simp add:pvD_def) + next + case False + with cncs_z and cnp_cnv_cncs show ?thesis by (simp add:pvD_def) + qed +qed + +lemma detached_eq: + shows "(detached s th) = (cntP s th = cntV s th)" + by (insert vt, auto intro:detached_intro detached_elim) + +lemma tRAG_nodeE: + assumes "(n1, n2) \ tRAG s" + obtains th1 th2 where "n1 = Th th1" "n2 = Th th2" + using assms + by (auto simp: tRAG_def wRAG_def hRAG_def tRAG_def) + +lemma subtree_nodeE: + assumes "n \ subtree (tRAG s) (Th th)" + obtains th1 where "n = Th th1" +proof - + show ?thesis + proof(rule subtreeE[OF assms]) + assume "n = Th th" + from that[OF this] show ?thesis . + next + assume "Th th \ ancestors (tRAG s) n" + hence "(n, Th th) \ (tRAG s)^+" by (auto simp:ancestors_def) + hence "\ th1. n = Th th1" + proof(induct) + case (base y) + from tRAG_nodeE[OF this] show ?case by metis + next + case (step y z) + thus ?case by auto + qed + with that show ?thesis by auto + qed +qed + +lemma tRAG_star_RAG: "(tRAG s)^* \ (RAG s)^*" +proof - + have "(wRAG s O hRAG s)^* \ (RAG s O RAG s)^*" + by (rule rtrancl_mono, auto simp:RAG_split) + also have "... \ ((RAG s)^*)^*" + by (rule rtrancl_mono, auto) + also have "... = (RAG s)^*" by simp + finally show ?thesis by (unfold tRAG_def, simp) +qed + +lemma tRAG_subtree_RAG: "subtree (tRAG s) x \ subtree (RAG s) x" +proof - + { fix a + assume "a \ subtree (tRAG s) x" + hence "(a, x) \ (tRAG s)^*" by (auto simp:subtree_def) + with tRAG_star_RAG + have "(a, x) \ (RAG s)^*" by auto + hence "a \ subtree (RAG s) x" by (auto simp:subtree_def) + } thus ?thesis by auto +qed + +lemma tRAG_trancl_eq: + "{th'. (Th th', Th th) \ (tRAG s)^+} = + {th'. (Th th', Th th) \ (RAG s)^+}" + (is "?L = ?R") +proof - + { fix th' + assume "th' \ ?L" + hence "(Th th', Th th) \ (tRAG s)^+" by auto + from tranclD[OF this] + obtain z where h: "(Th th', z) \ tRAG s" "(z, Th th) \ (tRAG s)\<^sup>*" by auto + from tRAG_subtree_RAG and this(2) + have "(z, Th th) \ (RAG s)^*" by (meson subsetCE tRAG_star_RAG) + moreover from h(1) have "(Th th', z) \ (RAG s)^+" using tRAG_alt_def by auto + ultimately have "th' \ ?R" by auto + } moreover + { fix th' + assume "th' \ ?R" + hence "(Th th', Th th) \ (RAG s)^+" by (auto) + from plus_rpath[OF this] + obtain xs where rp: "rpath (RAG s) (Th th') xs (Th th)" "xs \ []" by auto + hence "(Th th', Th th) \ (tRAG s)^+" + proof(induct xs arbitrary:th' th rule:length_induct) + case (1 xs th' th) + then obtain x1 xs1 where Cons1: "xs = x1#xs1" by (cases xs, auto) + show ?case + proof(cases "xs1") + case Nil + from 1(2)[unfolded Cons1 Nil] + have rp: "rpath (RAG s) (Th th') [x1] (Th th)" . + hence "(Th th', x1) \ (RAG s)" by (cases, auto) + then obtain cs where "x1 = Cs cs" + by (unfold s_RAG_def, auto) + from rpath_nnl_lastE[OF rp[unfolded this]] + show ?thesis by auto + next + case (Cons x2 xs2) + from 1(2)[unfolded Cons1[unfolded this]] + have rp: "rpath (RAG s) (Th th') (x1 # x2 # xs2) (Th th)" . + from rpath_edges_on[OF this] + have eds: "edges_on (Th th' # x1 # x2 # xs2) \ RAG s" . + have "(Th th', x1) \ edges_on (Th th' # x1 # x2 # xs2)" + by (simp add: edges_on_unfold) + with eds have rg1: "(Th th', x1) \ RAG s" by auto + then obtain cs1 where eq_x1: "x1 = Cs cs1" by (unfold s_RAG_def, auto) + have "(x1, x2) \ edges_on (Th th' # x1 # x2 # xs2)" + by (simp add: edges_on_unfold) + from this eds + have rg2: "(x1, x2) \ RAG s" by auto + from this[unfolded eq_x1] + obtain th1 where eq_x2: "x2 = Th th1" by (unfold s_RAG_def, auto) + from rg1[unfolded eq_x1] rg2[unfolded eq_x1 eq_x2] + have rt1: "(Th th', Th th1) \ tRAG s" by (unfold tRAG_alt_def, auto) + from rp have "rpath (RAG s) x2 xs2 (Th th)" + by (elim rpath_ConsE, simp) + from this[unfolded eq_x2] have rp': "rpath (RAG s) (Th th1) xs2 (Th th)" . + show ?thesis + proof(cases "xs2 = []") + case True + from rpath_nilE[OF rp'[unfolded this]] + have "th1 = th" by auto + from rt1[unfolded this] show ?thesis by auto + next + case False + from 1(1)[rule_format, OF _ rp' this, unfolded Cons1 Cons] + have "(Th th1, Th th) \ (tRAG s)\<^sup>+" by simp + with rt1 show ?thesis by auto + qed + qed + qed + hence "th' \ ?L" by auto + } ultimately show ?thesis by blast +qed + +lemma tRAG_trancl_eq_Th: + "{Th th' | th'. (Th th', Th th) \ (tRAG s)^+} = + {Th th' | th'. (Th th', Th th) \ (RAG s)^+}" + using tRAG_trancl_eq by auto + +lemma dependants_alt_def: + "dependants s th = {th'. (Th th', Th th) \ (tRAG s)^+}" + by (metis eq_RAG s_dependants_def tRAG_trancl_eq) + +(* ddd *) +lemma cp_gen_rec: + assumes "x = Th th" + shows "cp_gen s x = Max ({the_preced s th} \ (cp_gen s) ` children (tRAG s) x)" +proof(cases "children (tRAG s) x = {}") + case True + show ?thesis + by (unfold True cp_gen_def subtree_children, simp add:assms) +next + case False + hence [simp]: "children (tRAG s) x \ {}" by auto + note fsbttRAGs.finite_subtree[simp] + have [simp]: "finite (children (tRAG s) x)" + by (intro rev_finite_subset[OF fsbttRAGs.finite_subtree], + rule children_subtree) + { fix r x + have "subtree r x \ {}" by (auto simp:subtree_def) + } note this[simp] + have [simp]: "\x\children (tRAG s) x. subtree (tRAG s) x \ {}" + proof - + from False obtain q where "q \ children (tRAG s) x" by blast + moreover have "subtree (tRAG s) q \ {}" by simp + ultimately show ?thesis by blast + qed + have h: "Max ((the_preced s \ the_thread) ` + ({x} \ \(subtree (tRAG s) ` children (tRAG s) x))) = + Max ({the_preced s th} \ cp_gen s ` children (tRAG s) x)" + (is "?L = ?R") + proof - + let "Max (?f ` (?A \ \ (?g ` ?B)))" = ?L + let "Max (_ \ (?h ` ?B))" = ?R + let ?L1 = "?f ` \(?g ` ?B)" + have eq_Max_L1: "Max ?L1 = Max (?h ` ?B)" + proof - + have "?L1 = ?f ` (\ x \ ?B.(?g x))" by simp + also have "... = (\ x \ ?B. ?f ` (?g x))" by auto + finally have "Max ?L1 = Max ..." by simp + also have "... = Max (Max ` (\x. ?f ` subtree (tRAG s) x) ` ?B)" + by (subst Max_UNION, simp+) + also have "... = Max (cp_gen s ` children (tRAG s) x)" + by (unfold image_comp cp_gen_alt_def, simp) + finally show ?thesis . + qed + show ?thesis + proof - + have "?L = Max (?f ` ?A \ ?L1)" by simp + also have "... = max (the_preced s (the_thread x)) (Max ?L1)" + by (subst Max_Un, simp+) + also have "... = max (?f x) (Max (?h ` ?B))" + by (unfold eq_Max_L1, simp) + also have "... =?R" + by (rule max_Max_eq, (simp)+, unfold assms, simp) + finally show ?thesis . + qed + qed thus ?thesis + by (fold h subtree_children, unfold cp_gen_def, simp) +qed + +lemma cp_rec: + "cp s th = Max ({the_preced s th} \ + (cp s o the_thread) ` children (tRAG s) (Th th))" +proof - + have "Th th = Th th" by simp + note h = cp_gen_def_cond[OF this] cp_gen_rec[OF this] + show ?thesis + proof - + have "cp_gen s ` children (tRAG s) (Th th) = + (cp s \ the_thread) ` children (tRAG s) (Th th)" + proof(rule cp_gen_over_set) + show " \x\children (tRAG s) (Th th). \th. x = Th th" + by (unfold tRAG_alt_def, auto simp:children_def) + qed + thus ?thesis by (subst (1) h(1), unfold h(2), simp) + qed +qed + +lemma next_th_holding: + assumes nxt: "next_th s th cs th'" + shows "holding (wq s) th cs" +proof - + from nxt[unfolded next_th_def] + obtain rest where h: "wq s cs = th # rest" + "rest \ []" + "th' = hd (SOME q. distinct q \ set q = set rest)" by auto + thus ?thesis + by (unfold cs_holding_def, auto) +qed + +lemma next_th_waiting: + assumes nxt: "next_th s th cs th'" + shows "waiting (wq s) th' cs" +proof - + from nxt[unfolded next_th_def] + obtain rest where h: "wq s cs = th # rest" + "rest \ []" + "th' = hd (SOME q. distinct q \ set q = set rest)" by auto + from wq_distinct[of cs, unfolded h] + have dst: "distinct (th # rest)" . + have in_rest: "th' \ set rest" + proof(unfold h, rule someI2) + show "distinct rest \ set rest = set rest" using dst by auto + next + fix x assume "distinct x \ set x = set rest" + with h(2) + show "hd x \ set (rest)" by (cases x, auto) + qed + hence "th' \ set (wq s cs)" by (unfold h(1), auto) + moreover have "th' \ hd (wq s cs)" + by (unfold h(1), insert in_rest dst, auto) + ultimately show ?thesis by (auto simp:cs_waiting_def) +qed + +lemma next_th_RAG: + assumes nxt: "next_th (s::event list) th cs th'" + shows "{(Cs cs, Th th), (Th th', Cs cs)} \ RAG s" + using vt assms next_th_holding next_th_waiting + by (unfold s_RAG_def, simp) + +end + +end + diff -r 17305a85493d -r c495eb16beb6 CpsG.thy_1_1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CpsG.thy_1_1 Wed Jan 27 19:28:42 2016 +0800 @@ -0,0 +1,1751 @@ +theory CpsG +imports PIPDefs +begin + +lemma Max_f_mono: + assumes seq: "A \ B" + and np: "A \ {}" + and fnt: "finite B" + shows "Max (f ` A) \ Max (f ` B)" +proof(rule Max_mono) + from seq show "f ` A \ f ` B" by auto +next + from np show "f ` A \ {}" by auto +next + from fnt and seq show "finite (f ` B)" by auto +qed + + +locale valid_trace = + fixes s + assumes vt : "vt s" + +locale valid_trace_e = valid_trace + + fixes e + assumes vt_e: "vt (e#s)" +begin + +lemma pip_e: "PIP s e" + using vt_e by (cases, simp) + +end + +locale valid_trace_create = valid_trace_e + + fixes th prio + assumes is_create: "e = Create th prio" + +locale valid_trace_exit = valid_trace_e + + fixes th + assumes is_exit: "e = Exit th" + +locale valid_trace_p = valid_trace_e + + fixes th cs + assumes is_p: "e = P th cs" + +locale valid_trace_v = valid_trace_e + + fixes th cs + assumes is_v: "e = V th cs" +begin + definition "rest = tl (wq s cs)" + definition "wq' = (SOME q. distinct q \ set q = set rest)" +end + +locale valid_trace_v_n = valid_trace_v + + assumes rest_nnl: "rest \ []" + +locale valid_trace_v_e = valid_trace_v + + assumes rest_nil: "rest = []" + +locale valid_trace_set= valid_trace_e + + fixes th prio + assumes is_set: "e = Set th prio" + +context valid_trace +begin + +lemma ind [consumes 0, case_names Nil Cons, induct type]: + assumes "PP []" + and "(\s e. valid_trace_e s e \ + PP s \ PIP s e \ PP (e # s))" + shows "PP s" +proof(induct rule:vt.induct[OF vt, case_names Init Step]) + case Init + from assms(1) show ?case . +next + case (Step s e) + show ?case + proof(rule assms(2)) + show "valid_trace_e s e" using Step by (unfold_locales, auto) + next + show "PP s" using Step by simp + next + show "PIP s e" using Step by simp + qed +qed + +lemma vt_moment: "\ t. vt (moment t s)" +proof(induct rule:ind) + case Nil + thus ?case by (simp add:vt_nil) +next + case (Cons s e t) + show ?case + proof(cases "t \ length (e#s)") + case True + from True have "moment t (e#s) = e#s" by simp + thus ?thesis using Cons + by (simp add:valid_trace_def valid_trace_e_def, auto) + next + case False + from Cons have "vt (moment t s)" by simp + moreover have "moment t (e#s) = moment t s" + proof - + from False have "t \ length s" by simp + from moment_app [OF this, of "[e]"] + show ?thesis by simp + qed + ultimately show ?thesis by simp + qed +qed + +lemma finite_threads: + shows "finite (threads s)" +using vt by (induct) (auto elim: step.cases) + +end + +lemma cp_eq_cpreced: "cp s th = cpreced (wq s) s th" +unfolding cp_def wq_def +apply(induct s rule: schs.induct) +apply(simp add: Let_def cpreced_initial) +apply(simp add: Let_def) +apply(simp add: Let_def) +apply(simp add: Let_def) +apply(subst (2) schs.simps) +apply(simp add: Let_def) +apply(subst (2) schs.simps) +apply(simp add: Let_def) +done + +lemma RAG_target_th: "(Th th, x) \ RAG (s::state) \ \ cs. x = Cs cs" + by (unfold s_RAG_def, auto) + +locale valid_moment = valid_trace + + fixes i :: nat + +sublocale valid_moment < vat_moment: valid_trace "(moment i s)" + by (unfold_locales, insert vt_moment, auto) + +lemma waiting_eq: "waiting s th cs = waiting (wq s) th cs" + by (unfold s_waiting_def cs_waiting_def wq_def, auto) + +lemma holding_eq: "holding (s::state) th cs = holding (wq s) th cs" + by (unfold s_holding_def wq_def cs_holding_def, simp) + +lemma runing_ready: + shows "runing s \ readys s" + unfolding runing_def readys_def + by auto + +lemma readys_threads: + shows "readys s \ threads s" + unfolding readys_def + by auto + +lemma wq_v_neq [simp]: + "cs \ cs' \ wq (V thread cs#s) cs' = wq s cs'" + by (auto simp:wq_def Let_def cp_def split:list.splits) + +lemma runing_head: + assumes "th \ runing s" + and "th \ set (wq_fun (schs s) cs)" + shows "th = hd (wq_fun (schs s) cs)" + using assms + by (simp add:runing_def readys_def s_waiting_def wq_def) + +context valid_trace +begin + +lemma runing_wqE: + assumes "th \ runing s" + and "th \ set (wq s cs)" + obtains rest where "wq s cs = th#rest" +proof - + from assms(2) obtain th' rest where eq_wq: "wq s cs = th'#rest" + by (meson list.set_cases) + have "th' = th" + proof(rule ccontr) + assume "th' \ th" + hence "th \ hd (wq s cs)" using eq_wq by auto + with assms(2) + have "waiting s th cs" + by (unfold s_waiting_def, fold wq_def, auto) + with assms show False + by (unfold runing_def readys_def, auto) + qed + with eq_wq that show ?thesis by metis +qed + +end + +context valid_trace_create +begin + +lemma wq_neq_simp [simp]: + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_create wq_def + by (auto simp:Let_def) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" + using assms by simp +end + +context valid_trace_exit +begin + +lemma wq_neq_simp [simp]: + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_exit wq_def + by (auto simp:Let_def) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" + using assms by simp +end + +context valid_trace_p +begin + +lemma wq_neq_simp [simp]: + assumes "cs' \ cs" + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_p wq_def + by (auto simp:Let_def) + +lemma runing_th_s: + shows "th \ runing s" +proof - + from pip_e[unfolded is_p] + show ?thesis by (cases, simp) +qed + +lemma th_not_waiting: + "\ waiting s th c" +proof - + have "th \ readys s" + using runing_ready runing_th_s by blast + thus ?thesis + by (unfold readys_def, auto) +qed + +lemma waiting_neq_th: + assumes "waiting s t c" + shows "t \ th" + using assms using th_not_waiting by blast + +lemma th_not_in_wq: + shows "th \ set (wq s cs)" +proof + assume otherwise: "th \ set (wq s cs)" + from runing_wqE[OF runing_th_s this] + obtain rest where eq_wq: "wq s cs = th#rest" by blast + with otherwise + have "holding s th cs" + by (unfold s_holding_def, fold wq_def, simp) + hence cs_th_RAG: "(Cs cs, Th th) \ RAG s" + by (unfold s_RAG_def, fold holding_eq, auto) + from pip_e[unfolded is_p] + show False + proof(cases) + case (thread_P) + with cs_th_RAG show ?thesis by auto + qed +qed + +lemma wq_es_cs: + "wq (e#s) cs = wq s cs @ [th]" + by (unfold is_p wq_def, auto simp:Let_def) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" +proof(cases "cs' = cs") + case True + show ?thesis using True assms th_not_in_wq + by (unfold True wq_es_cs, auto) +qed (insert assms, simp) + +end + +context valid_trace_v +begin + +lemma wq_neq_simp [simp]: + assumes "cs' \ cs" + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_v wq_def + by (auto simp:Let_def) + +lemma runing_th_s: + shows "th \ runing s" +proof - + from pip_e[unfolded is_v] + show ?thesis by (cases, simp) +qed + +lemma th_not_waiting: + "\ waiting s th c" +proof - + have "th \ readys s" + using runing_ready runing_th_s by blast + thus ?thesis + by (unfold readys_def, auto) +qed + +lemma waiting_neq_th: + assumes "waiting s t c" + shows "t \ th" + using assms using th_not_waiting by blast + +lemma wq_s_cs: + "wq s cs = th#rest" +proof - + from pip_e[unfolded is_v] + show ?thesis + proof(cases) + case (thread_V) + from this(2) show ?thesis + by (unfold rest_def s_holding_def, fold wq_def, + metis empty_iff list.collapse list.set(1)) + qed +qed + +lemma wq_es_cs: + "wq (e#s) cs = wq'" + using wq_s_cs[unfolded wq_def] + by (auto simp:Let_def wq_def rest_def wq'_def is_v, simp) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" +proof(cases "cs' = cs") + case True + show ?thesis + proof(unfold True wq_es_cs wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + using assms[unfolded True wq_s_cs] by auto + qed simp +qed (insert assms, simp) + +end + +context valid_trace_set +begin + +lemma wq_neq_simp [simp]: + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_set wq_def + by (auto simp:Let_def) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" + using assms by simp +end + +context valid_trace +begin + +lemma actor_inv: + assumes "PIP s e" + and "\ isCreate e" + shows "actor e \ runing s" + using assms + by (induct, auto) + +lemma isP_E: + assumes "isP e" + obtains cs where "e = P (actor e) cs" + using assms by (cases e, auto) + +lemma isV_E: + assumes "isV e" + obtains cs where "e = V (actor e) cs" + using assms by (cases e, auto) + +lemma wq_distinct: "distinct (wq s cs)" +proof(induct rule:ind) + case (Cons s e) + interpret vt_e: valid_trace_e s e using Cons by simp + show ?case + proof(cases e) + case (Create th prio) + interpret vt_create: valid_trace_create s e th prio + using Create by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_create.wq_distinct_kept) + next + case (Exit th) + interpret vt_exit: valid_trace_exit s e th + using Exit by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_exit.wq_distinct_kept) + next + case (P th cs) + interpret vt_p: valid_trace_p s e th cs using P by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_p.wq_distinct_kept) + next + case (V th cs) + interpret vt_v: valid_trace_v s e th cs using V by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_v.wq_distinct_kept) + next + case (Set th prio) + interpret vt_set: valid_trace_set s e th prio + using Set by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_set.wq_distinct_kept) + qed +qed (unfold wq_def Let_def, simp) + +end + +context valid_trace_e +begin + +text {* + The following lemma shows that only the @{text "P"} + operation can add new thread into waiting queues. + Such kind of lemmas are very obvious, but need to be checked formally. + This is a kind of confirmation that our modelling is correct. +*} + +lemma wq_in_inv: + assumes s_ni: "thread \ set (wq s cs)" + and s_i: "thread \ set (wq (e#s) cs)" + shows "e = P thread cs" +proof(cases e) + -- {* This is the only non-trivial case: *} + case (V th cs1) + have False + proof(cases "cs1 = cs") + case True + show ?thesis + proof(cases "(wq s cs1)") + case (Cons w_hd w_tl) + have "set (wq (e#s) cs) \ set (wq s cs)" + proof - + have "(wq (e#s) cs) = (SOME q. distinct q \ set q = set w_tl)" + using Cons V by (auto simp:wq_def Let_def True split:if_splits) + moreover have "set ... \ set (wq s cs)" + proof(rule someI2) + show "distinct w_tl \ set w_tl = set w_tl" + by (metis distinct.simps(2) local.Cons wq_distinct) + qed (insert Cons True, auto) + ultimately show ?thesis by simp + qed + with assms show ?thesis by auto + qed (insert assms V True, auto simp:wq_def Let_def split:if_splits) + qed (insert assms V, auto simp:wq_def Let_def split:if_splits) + thus ?thesis by auto +qed (insert assms, auto simp:wq_def Let_def split:if_splits) + +lemma wq_out_inv: + assumes s_in: "thread \ set (wq s cs)" + and s_hd: "thread = hd (wq s cs)" + and s_i: "thread \ hd (wq (e#s) cs)" + shows "e = V thread cs" +proof(cases e) +-- {* There are only two non-trivial cases: *} + case (V th cs1) + show ?thesis + proof(cases "cs1 = cs") + case True + have "PIP s (V th cs)" using pip_e[unfolded V[unfolded True]] . + thus ?thesis + proof(cases) + case (thread_V) + moreover have "th = thread" using thread_V(2) s_hd + by (unfold s_holding_def wq_def, simp) + ultimately show ?thesis using V True by simp + qed + qed (insert assms V, auto simp:wq_def Let_def split:if_splits) +next + case (P th cs1) + show ?thesis + proof(cases "cs1 = cs") + case True + with P have "wq (e#s) cs = wq_fun (schs s) cs @ [th]" + by (auto simp:wq_def Let_def split:if_splits) + with s_i s_hd s_in have False + by (metis empty_iff hd_append2 list.set(1) wq_def) + thus ?thesis by simp + qed (insert assms P, auto simp:wq_def Let_def split:if_splits) +qed (insert assms, auto simp:wq_def Let_def split:if_splits) + +end + + +context valid_trace +begin + + +text {* (* ddd *) + The nature of the work is like this: since it starts from a very simple and basic + model, even intuitively very `basic` and `obvious` properties need to derived from scratch. + For instance, the fact + that one thread can not be blocked by two critical resources at the same time + is obvious, because only running threads can make new requests, if one is waiting for + a critical resource and get blocked, it can not make another resource request and get + blocked the second time (because it is not running). + + To derive this fact, one needs to prove by contraction and + reason about time (or @{text "moement"}). The reasoning is based on a generic theorem + named @{text "p_split"}, which is about status changing along the time axis. It says if + a condition @{text "Q"} is @{text "True"} at a state @{text "s"}, + but it was @{text "False"} at the very beginning, then there must exits a moment @{text "t"} + in the history of @{text "s"} (notice that @{text "s"} itself is essentially the history + of events leading to it), such that @{text "Q"} switched + from being @{text "False"} to @{text "True"} and kept being @{text "True"} + till the last moment of @{text "s"}. + + Suppose a thread @{text "th"} is blocked + on @{text "cs1"} and @{text "cs2"} in some state @{text "s"}, + since no thread is blocked at the very beginning, by applying + @{text "p_split"} to these two blocking facts, there exist + two moments @{text "t1"} and @{text "t2"} in @{text "s"}, such that + @{text "th"} got blocked on @{text "cs1"} and @{text "cs2"} + and kept on blocked on them respectively ever since. + + Without lost of generality, we assume @{text "t1"} is earlier than @{text "t2"}. + However, since @{text "th"} was blocked ever since memonent @{text "t1"}, so it was still + in blocked state at moment @{text "t2"} and could not + make any request and get blocked the second time: Contradiction. +*} + +lemma waiting_unique_pre: (* ddd *) + assumes h11: "thread \ set (wq s cs1)" + and h12: "thread \ hd (wq s cs1)" + assumes h21: "thread \ set (wq s cs2)" + and h22: "thread \ hd (wq s cs2)" + and neq12: "cs1 \ cs2" + shows "False" +proof - + let "?Q" = "\ cs s. thread \ set (wq s cs) \ thread \ hd (wq s cs)" + from h11 and h12 have q1: "?Q cs1 s" by simp + from h21 and h22 have q2: "?Q cs2 s" by simp + have nq1: "\ ?Q cs1 []" by (simp add:wq_def) + have nq2: "\ ?Q cs2 []" by (simp add:wq_def) + from p_split [of "?Q cs1", OF q1 nq1] + obtain t1 where lt1: "t1 < length s" + and np1: "\ ?Q cs1 (moment t1 s)" + and nn1: "(\i'>t1. ?Q cs1 (moment i' s))" by auto + from p_split [of "?Q cs2", OF q2 nq2] + obtain t2 where lt2: "t2 < length s" + and np2: "\ ?Q cs2 (moment t2 s)" + and nn2: "(\i'>t2. ?Q cs2 (moment i' s))" by auto + { fix s cs + assume q: "?Q cs s" + have "thread \ runing s" + proof + assume "thread \ runing s" + hence " \cs. \ (thread \ set (wq_fun (schs s) cs) \ + thread \ hd (wq_fun (schs s) cs))" + by (unfold runing_def s_waiting_def readys_def, auto) + from this[rule_format, of cs] q + show False by (simp add: wq_def) + qed + } note q_not_runing = this + { fix t1 t2 cs1 cs2 + assume lt1: "t1 < length s" + and np1: "\ ?Q cs1 (moment t1 s)" + and nn1: "(\i'>t1. ?Q cs1 (moment i' s))" + and lt2: "t2 < length s" + and np2: "\ ?Q cs2 (moment t2 s)" + and nn2: "(\i'>t2. ?Q cs2 (moment i' s))" + and lt12: "t1 < t2" + let ?t3 = "Suc t2" + from lt2 have le_t3: "?t3 \ length s" by auto + from moment_plus [OF this] + obtain e where eq_m: "moment ?t3 s = e#moment t2 s" by auto + have "t2 < ?t3" by simp + from nn2 [rule_format, OF this] and eq_m + have h1: "thread \ set (wq (e#moment t2 s) cs2)" and + h2: "thread \ hd (wq (e#moment t2 s) cs2)" by auto + have "vt (e#moment t2 s)" + proof - + from vt_moment + have "vt (moment ?t3 s)" . + with eq_m show ?thesis by simp + qed + then interpret vt_e: valid_trace_e "moment t2 s" "e" + by (unfold_locales, auto, cases, simp) + have ?thesis + proof - + have "thread \ runing (moment t2 s)" + proof(cases "thread \ set (wq (moment t2 s) cs2)") + case True + have "e = V thread cs2" + proof - + have eq_th: "thread = hd (wq (moment t2 s) cs2)" + using True and np2 by auto + from vt_e.wq_out_inv[OF True this h2] + show ?thesis . + qed + thus ?thesis using vt_e.actor_inv[OF vt_e.pip_e] by auto + next + case False + have "e = P thread cs2" using vt_e.wq_in_inv[OF False h1] . + with vt_e.actor_inv[OF vt_e.pip_e] + show ?thesis by auto + qed + moreover have "thread \ runing (moment t2 s)" + by (rule q_not_runing[OF nn1[rule_format, OF lt12]]) + ultimately show ?thesis by simp + qed + } note lt_case = this + show ?thesis + proof - + { assume "t1 < t2" + from lt_case[OF lt1 np1 nn1 lt2 np2 nn2 this] + have ?thesis . + } moreover { + assume "t2 < t1" + from lt_case[OF lt2 np2 nn2 lt1 np1 nn1 this] + have ?thesis . + } moreover { + assume eq_12: "t1 = t2" + let ?t3 = "Suc t2" + from lt2 have le_t3: "?t3 \ length s" by auto + from moment_plus [OF this] + obtain e where eq_m: "moment ?t3 s = e#moment t2 s" by auto + have lt_2: "t2 < ?t3" by simp + from nn2 [rule_format, OF this] and eq_m + have h1: "thread \ set (wq (e#moment t2 s) cs2)" and + h2: "thread \ hd (wq (e#moment t2 s) cs2)" by auto + from nn1[rule_format, OF lt_2[folded eq_12]] eq_m[folded eq_12] + have g1: "thread \ set (wq (e#moment t1 s) cs1)" and + g2: "thread \ hd (wq (e#moment t1 s) cs1)" by auto + have "vt (e#moment t2 s)" + proof - + from vt_moment + have "vt (moment ?t3 s)" . + with eq_m show ?thesis by simp + qed + then interpret vt_e: valid_trace_e "moment t2 s" "e" + by (unfold_locales, auto, cases, simp) + have "e = V thread cs2 \ e = P thread cs2" + proof(cases "thread \ set (wq (moment t2 s) cs2)") + case True + have "e = V thread cs2" + proof - + have eq_th: "thread = hd (wq (moment t2 s) cs2)" + using True and np2 by auto + from vt_e.wq_out_inv[OF True this h2] + show ?thesis . + qed + thus ?thesis by auto + next + case False + have "e = P thread cs2" using vt_e.wq_in_inv[OF False h1] . + thus ?thesis by auto + qed + moreover have "e = V thread cs1 \ e = P thread cs1" + proof(cases "thread \ set (wq (moment t1 s) cs1)") + case True + have eq_th: "thread = hd (wq (moment t1 s) cs1)" + using True and np1 by auto + from vt_e.wq_out_inv[folded eq_12, OF True this g2] + have "e = V thread cs1" . + thus ?thesis by auto + next + case False + have "e = P thread cs1" using vt_e.wq_in_inv[folded eq_12, OF False g1] . + thus ?thesis by auto + qed + ultimately have ?thesis using neq12 by auto + } ultimately show ?thesis using nat_neq_iff by blast + qed +qed + +text {* + This lemma is a simple corrolary of @{text "waiting_unique_pre"}. +*} + +lemma waiting_unique: + assumes "waiting s th cs1" + and "waiting s th cs2" + shows "cs1 = cs2" + using waiting_unique_pre assms + unfolding wq_def s_waiting_def + by auto + +end + +(* not used *) +text {* + Every thread can only be blocked on one critical resource, + symmetrically, every critical resource can only be held by one thread. + This fact is much more easier according to our definition. +*} +lemma held_unique: + assumes "holding (s::event list) th1 cs" + and "holding s th2 cs" + shows "th1 = th2" + by (insert assms, unfold s_holding_def, auto) + +lemma last_set_lt: "th \ threads s \ last_set th s < length s" + apply (induct s, auto) + by (case_tac a, auto split:if_splits) + +lemma last_set_unique: + "\last_set th1 s = last_set th2 s; th1 \ threads s; th2 \ threads s\ + \ th1 = th2" + apply (induct s, auto) + by (case_tac a, auto split:if_splits dest:last_set_lt) + +lemma preced_unique : + assumes pcd_eq: "preced th1 s = preced th2 s" + and th_in1: "th1 \ threads s" + and th_in2: " th2 \ threads s" + shows "th1 = th2" +proof - + from pcd_eq have "last_set th1 s = last_set th2 s" by (simp add:preced_def) + from last_set_unique [OF this th_in1 th_in2] + show ?thesis . +qed + +lemma preced_linorder: + assumes neq_12: "th1 \ th2" + and th_in1: "th1 \ threads s" + and th_in2: " th2 \ threads s" + shows "preced th1 s < preced th2 s \ preced th1 s > preced th2 s" +proof - + from preced_unique [OF _ th_in1 th_in2] and neq_12 + have "preced th1 s \ preced th2 s" by auto + thus ?thesis by auto +qed + +text {* + The following three lemmas show that @{text "RAG"} does not change + by the happening of @{text "Set"}, @{text "Create"} and @{text "Exit"} + events, respectively. +*} + +lemma RAG_set_unchanged: "(RAG (Set th prio # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + +lemma RAG_create_unchanged: "(RAG (Create th prio # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + +lemma RAG_exit_unchanged: "(RAG (Exit th # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + + +context valid_trace_v +begin + +lemma distinct_rest: "distinct rest" + by (simp add: distinct_tl rest_def wq_distinct) + +lemma holding_cs_eq_th: + assumes "holding s t cs" + shows "t = th" +proof - + from pip_e[unfolded is_v] + show ?thesis + proof(cases) + case (thread_V) + from held_unique[OF this(2) assms] + show ?thesis by simp + qed +qed + +lemma distinct_wq': "distinct wq'" + by (metis (mono_tags, lifting) distinct_rest some_eq_ex wq'_def) + +lemma th'_in_inv: + assumes "th' \ set wq'" + shows "th' \ set rest" + using assms + by (metis (mono_tags, lifting) distinct.simps(2) + rest_def some_eq_ex wq'_def wq_distinct wq_s_cs) + +lemma neq_t_th: + assumes "waiting (e#s) t c" + shows "t \ th" +proof + assume otherwise: "t = th" + show False + proof(cases "c = cs") + case True + have "t \ set wq'" + using assms[unfolded True s_waiting_def, folded wq_def, unfolded wq_es_cs] + by simp + from th'_in_inv[OF this] have "t \ set rest" . + with wq_s_cs[folded otherwise] wq_distinct[of cs] + show ?thesis by simp + next + case False + have "wq (e#s) c = wq s c" using False + by (unfold is_v, simp) + hence "waiting s t c" using assms + by (simp add: cs_waiting_def waiting_eq) + hence "t \ readys s" by (unfold readys_def, auto) + hence "t \ runing s" using runing_ready by auto + with runing_th_s[folded otherwise] show ?thesis by auto + qed +qed + +lemma waiting_esI1: + assumes "waiting s t c" + and "c \ cs" + shows "waiting (e#s) t c" +proof - + have "wq (e#s) c = wq s c" + using assms(2) is_v by auto + with assms(1) show ?thesis + using cs_waiting_def waiting_eq by auto +qed + +lemma holding_esI2: + assumes "c \ cs" + and "holding s t c" + shows "holding (e#s) t c" +proof - + from assms(1) have "wq (e#s) c = wq s c" using is_v by auto + from assms(2)[unfolded s_holding_def, folded wq_def, + folded this, unfolded wq_def, folded s_holding_def] + show ?thesis . +qed + +lemma holding_esI1: + assumes "holding s t c" + and "t \ th" + shows "holding (e#s) t c" +proof - + have "c \ cs" using assms using holding_cs_eq_th by blast + from holding_esI2[OF this assms(1)] + show ?thesis . +qed + +end + +context valid_trace_v_n +begin + +lemma neq_wq': "wq' \ []" +proof (unfold wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) +next + fix x + assume " distinct x \ set x = set rest" + thus "x \ []" using rest_nnl by auto +qed + +definition "taker = hd wq'" + +definition "rest' = tl wq'" + +lemma eq_wq': "wq' = taker # rest'" + by (simp add: neq_wq' rest'_def taker_def) + +lemma next_th_taker: + shows "next_th s th cs taker" + using rest_nnl taker_def wq'_def wq_s_cs + by (auto simp:next_th_def) + +lemma taker_unique: + assumes "next_th s th cs taker'" + shows "taker' = taker" +proof - + from assms + obtain rest' where + h: "wq s cs = th # rest'" + "taker' = hd (SOME q. distinct q \ set q = set rest')" + by (unfold next_th_def, auto) + with wq_s_cs have "rest' = rest" by auto + thus ?thesis using h(2) taker_def wq'_def by auto +qed + +lemma waiting_set_eq: + "{(Th th', Cs cs) |th'. next_th s th cs th'} = {(Th taker, Cs cs)}" + by (smt all_not_in_conv bot.extremum insertI1 insert_subset + mem_Collect_eq next_th_taker subsetI subset_antisym taker_def taker_unique) + +lemma holding_set_eq: + "{(Cs cs, Th th') |th'. next_th s th cs th'} = {(Cs cs, Th taker)}" + using next_th_taker taker_def waiting_set_eq + by fastforce + +lemma holding_taker: + shows "holding (e#s) taker cs" + by (unfold s_holding_def, fold wq_def, unfold wq_es_cs, + auto simp:neq_wq' taker_def) + +lemma waiting_esI2: + assumes "waiting s t cs" + and "t \ taker" + shows "waiting (e#s) t cs" +proof - + have "t \ set wq'" + proof(unfold wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) + next + fix x + assume "distinct x \ set x = set rest" + moreover have "t \ set rest" + using assms(1) cs_waiting_def waiting_eq wq_s_cs by auto + ultimately show "t \ set x" by simp + qed + moreover have "t \ hd wq'" + using assms(2) taker_def by auto + ultimately show ?thesis + by (unfold s_waiting_def, fold wq_def, unfold wq_es_cs, simp) +qed + +lemma waiting_esE: + assumes "waiting (e#s) t c" + obtains "c \ cs" "waiting s t c" + | "c = cs" "t \ taker" "waiting s t cs" "t \ set rest'" +proof(cases "c = cs") + case False + hence "wq (e#s) c = wq s c" using is_v by auto + with assms have "waiting s t c" using cs_waiting_def waiting_eq by auto + from that(1)[OF False this] show ?thesis . +next + case True + from assms[unfolded s_waiting_def True, folded wq_def, unfolded wq_es_cs] + have "t \ hd wq'" "t \ set wq'" by auto + hence "t \ taker" by (simp add: taker_def) + moreover hence "t \ th" using assms neq_t_th by blast + moreover have "t \ set rest" by (simp add: `t \ set wq'` th'_in_inv) + ultimately have "waiting s t cs" + by (metis cs_waiting_def list.distinct(2) list.sel(1) + list.set_sel(2) rest_def waiting_eq wq_s_cs) + show ?thesis using that(2) + using True `t \ set wq'` `t \ taker` `waiting s t cs` eq_wq' by auto +qed + +lemma holding_esI1: + assumes "c = cs" + and "t = taker" + shows "holding (e#s) t c" + by (unfold assms, simp add: holding_taker) + +lemma holding_esE: + assumes "holding (e#s) t c" + obtains "c = cs" "t = taker" + | "c \ cs" "holding s t c" +proof(cases "c = cs") + case True + from assms[unfolded True, unfolded s_holding_def, + folded wq_def, unfolded wq_es_cs] + have "t = taker" by (simp add: taker_def) + from that(1)[OF True this] show ?thesis . +next + case False + hence "wq (e#s) c = wq s c" using is_v by auto + from assms[unfolded s_holding_def, folded wq_def, + unfolded this, unfolded wq_def, folded s_holding_def] + have "holding s t c" . + from that(2)[OF False this] show ?thesis . +qed + +end + + +context valid_trace_v_e +begin + +lemma nil_wq': "wq' = []" +proof (unfold wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) +next + fix x + assume " distinct x \ set x = set rest" + thus "x = []" using rest_nil by auto +qed + +lemma no_taker: + assumes "next_th s th cs taker" + shows "False" +proof - + from assms[unfolded next_th_def] + obtain rest' where "wq s cs = th # rest'" "rest' \ []" + by auto + thus ?thesis using rest_def rest_nil by auto +qed + +lemma waiting_set_eq: + "{(Th th', Cs cs) |th'. next_th s th cs th'} = {}" + using no_taker by auto + +lemma holding_set_eq: + "{(Cs cs, Th th') |th'. next_th s th cs th'} = {}" + using no_taker by auto + +lemma no_holding: + assumes "holding (e#s) taker cs" + shows False +proof - + from wq_es_cs[unfolded nil_wq'] + have " wq (e # s) cs = []" . + from assms[unfolded s_holding_def, folded wq_def, unfolded this] + show ?thesis by auto +qed + +lemma no_waiting: + assumes "waiting (e#s) t cs" + shows False +proof - + from wq_es_cs[unfolded nil_wq'] + have " wq (e # s) cs = []" . + from assms[unfolded s_waiting_def, folded wq_def, unfolded this] + show ?thesis by auto +qed + +lemma waiting_esI2: + assumes "waiting s t c" + shows "waiting (e#s) t c" +proof - + have "c \ cs" using assms + using cs_waiting_def rest_nil waiting_eq wq_s_cs by auto + from waiting_esI1[OF assms this] + show ?thesis . +qed + +lemma waiting_esE: + assumes "waiting (e#s) t c" + obtains "c \ cs" "waiting s t c" +proof(cases "c = cs") + case False + hence "wq (e#s) c = wq s c" using is_v by auto + with assms have "waiting s t c" using cs_waiting_def waiting_eq by auto + from that(1)[OF False this] show ?thesis . +next + case True + from no_waiting[OF assms[unfolded True]] + show ?thesis by auto +qed + +lemma holding_esE: + assumes "holding (e#s) t c" + obtains "c \ cs" "holding s t c" +proof(cases "c = cs") + case True + from no_holding[OF assms[unfolded True]] + show ?thesis by auto +next + case False + hence "wq (e#s) c = wq s c" using is_v by auto + from assms[unfolded s_holding_def, folded wq_def, + unfolded this, unfolded wq_def, folded s_holding_def] + have "holding s t c" . + from that[OF False this] show ?thesis . +qed + +end + +lemma rel_eqI: + assumes "\ x y. (x,y) \ A \ (x,y) \ B" + and "\ x y. (x,y) \ B \ (x, y) \ A" + shows "A = B" + using assms by auto + +lemma in_RAG_E: + assumes "(n1, n2) \ RAG (s::state)" + obtains (waiting) th cs where "n1 = Th th" "n2 = Cs cs" "waiting s th cs" + | (holding) th cs where "n1 = Cs cs" "n2 = Th th" "holding s th cs" + using assms[unfolded s_RAG_def, folded waiting_eq holding_eq] + by auto + +context valid_trace_v +begin + +lemma RAG_es: + "RAG (e # s) = + RAG s - {(Cs cs, Th th)} - + {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'}" (is "?L = ?R") +proof(rule rel_eqI) + fix n1 n2 + assume "(n1, n2) \ ?L" + thus "(n1, n2) \ ?R" + proof(cases rule:in_RAG_E) + case (waiting th' cs') + show ?thesis + proof(cases "rest = []") + case False + interpret h_n: valid_trace_v_n s e th cs + by (unfold_locales, insert False, simp) + from waiting(3) + show ?thesis + proof(cases rule:h_n.waiting_esE) + case 1 + with waiting(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + next + case 2 + with waiting(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + qed + next + case True + interpret h_e: valid_trace_v_e s e th cs + by (unfold_locales, insert True, simp) + from waiting(3) + show ?thesis + proof(cases rule:h_e.waiting_esE) + case 1 + with waiting(1,2) + show ?thesis + by (unfold h_e.waiting_set_eq h_e.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + qed + qed + next + case (holding th' cs') + show ?thesis + proof(cases "rest = []") + case False + interpret h_n: valid_trace_v_n s e th cs + by (unfold_locales, insert False, simp) + from holding(3) + show ?thesis + proof(cases rule:h_n.holding_esE) + case 1 + with holding(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + next + case 2 + with holding(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold holding_eq, auto) + qed + next + case True + interpret h_e: valid_trace_v_e s e th cs + by (unfold_locales, insert True, simp) + from holding(3) + show ?thesis + proof(cases rule:h_e.holding_esE) + case 1 + with holding(1,2) + show ?thesis + by (unfold h_e.waiting_set_eq h_e.holding_set_eq s_RAG_def, + fold holding_eq, auto) + qed + qed + qed +next + fix n1 n2 + assume h: "(n1, n2) \ ?R" + show "(n1, n2) \ ?L" + proof(cases "rest = []") + case False + interpret h_n: valid_trace_v_n s e th cs + by (unfold_locales, insert False, simp) + from h[unfolded h_n.waiting_set_eq h_n.holding_set_eq] + have "((n1, n2) \ RAG s \ (n1 \ Cs cs \ n2 \ Th th) + \ (n1 \ Th h_n.taker \ n2 \ Cs cs)) \ + (n2 = Th h_n.taker \ n1 = Cs cs)" + by auto + thus ?thesis + proof + assume "n2 = Th h_n.taker \ n1 = Cs cs" + with h_n.holding_taker + show ?thesis + by (unfold s_RAG_def, fold holding_eq, auto) + next + assume h: "(n1, n2) \ RAG s \ + (n1 \ Cs cs \ n2 \ Th th) \ (n1 \ Th h_n.taker \ n2 \ Cs cs)" + hence "(n1, n2) \ RAG s" by simp + thus ?thesis + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from h and this(1,2) + have "th' \ h_n.taker \ cs' \ cs" by auto + hence "waiting (e#s) th' cs'" + proof + assume "cs' \ cs" + from waiting_esI1[OF waiting(3) this] + show ?thesis . + next + assume neq_th': "th' \ h_n.taker" + show ?thesis + proof(cases "cs' = cs") + case False + from waiting_esI1[OF waiting(3) this] + show ?thesis . + next + case True + from h_n.waiting_esI2[OF waiting(3)[unfolded True] neq_th', folded True] + show ?thesis . + qed + qed + thus ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case (holding th' cs') + from h this(1,2) + have "cs' \ cs \ th' \ th" by auto + hence "holding (e#s) th' cs'" + proof + assume "cs' \ cs" + from holding_esI2[OF this holding(3)] + show ?thesis . + next + assume "th' \ th" + from holding_esI1[OF holding(3) this] + show ?thesis . + qed + thus ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + qed + qed + next + case True + interpret h_e: valid_trace_v_e s e th cs + by (unfold_locales, insert True, simp) + from h[unfolded h_e.waiting_set_eq h_e.holding_set_eq] + have h_s: "(n1, n2) \ RAG s" "(n1, n2) \ (Cs cs, Th th)" + by auto + from h_s(1) + show ?thesis + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from h_e.waiting_esI2[OF this(3)] + show ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case (holding th' cs') + with h_s(2) + have "cs' \ cs \ th' \ th" by auto + thus ?thesis + proof + assume neq_cs: "cs' \ cs" + from holding_esI2[OF this holding(3)] + show ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + next + assume "th' \ th" + from holding_esI1[OF holding(3) this] + show ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + qed + qed + qed +qed + +end + +lemma step_RAG_v: +assumes vt: + "vt (V th cs#s)" +shows " + RAG (V th cs # s) = + RAG s - {(Cs cs, Th th)} - + {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'}" (is "?L = ?R") +proof - + interpret vt_v: valid_trace_v s "V th cs" + using assms step_back_vt by (unfold_locales, auto) + show ?thesis using vt_v.RAG_es . +qed + +lemma (in valid_trace_create) + th_not_in_threads: "th \ threads s" +proof - + from pip_e[unfolded is_create] + show ?thesis by (cases, simp) +qed + +lemma (in valid_trace_create) + threads_es [simp]: "threads (e#s) = threads s \ {th}" + by (unfold is_create, simp) + +lemma (in valid_trace_exit) + threads_es [simp]: "threads (e#s) = threads s - {th}" + by (unfold is_exit, simp) + +lemma (in valid_trace_p) + threads_es [simp]: "threads (e#s) = threads s" + by (unfold is_p, simp) + +lemma (in valid_trace_v) + threads_es [simp]: "threads (e#s) = threads s" + by (unfold is_v, simp) + +lemma (in valid_trace_v) + th_not_in_rest[simp]: "th \ set rest" +proof + assume otherwise: "th \ set rest" + have "distinct (wq s cs)" by (simp add: wq_distinct) + from this[unfolded wq_s_cs] and otherwise + show False by auto +qed + +lemma (in valid_trace_v) + set_wq_es_cs [simp]: "set (wq (e#s) cs) = set (wq s cs) - {th}" +proof(unfold wq_es_cs wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) +next + fix x + assume "distinct x \ set x = set rest" + thus "set x = set (wq s cs) - {th}" + by (unfold wq_s_cs, simp) +qed + +lemma (in valid_trace_exit) + th_not_in_wq: "th \ set (wq s cs)" +proof - + from pip_e[unfolded is_exit] + show ?thesis + by (cases, unfold holdents_def s_holding_def, fold wq_def, + auto elim!:runing_wqE) +qed + +lemma (in valid_trace) wq_threads: + assumes "th \ set (wq s cs)" + shows "th \ threads s" + using assms +proof(induct rule:ind) + case (Nil) + thus ?case by (auto simp:wq_def) +next + case (Cons s e) + interpret vt_e: valid_trace_e s e using Cons by simp + show ?case + proof(cases e) + case (Create th' prio') + interpret vt: valid_trace_create s e th' prio' + using Create by (unfold_locales, simp) + show ?thesis + using Cons.hyps(2) Cons.prems by auto + next + case (Exit th') + interpret vt: valid_trace_exit s e th' + using Exit by (unfold_locales, simp) + show ?thesis + using Cons.hyps(2) Cons.prems vt.th_not_in_wq by auto + next + case (P th' cs') + interpret vt: valid_trace_p s e th' cs' + using P by (unfold_locales, simp) + show ?thesis + using Cons.hyps(2) Cons.prems readys_threads + runing_ready vt.is_p vt.runing_th_s vt_e.wq_in_inv + by fastforce + next + case (V th' cs') + interpret vt: valid_trace_v s e th' cs' + using V by (unfold_locales, simp) + show ?thesis using Cons + using vt.is_v vt.threads_es vt_e.wq_in_inv by blast + next + case (Set th' prio) + interpret vt: valid_trace_set s e th' prio + using Set by (unfold_locales, simp) + show ?thesis using Cons.hyps(2) Cons.prems vt.is_set + by (auto simp:wq_def Let_def) + qed +qed + +context valid_trace +begin + +lemma dm_RAG_threads: + assumes in_dom: "(Th th) \ Domain (RAG s)" + shows "th \ threads s" +proof - + from in_dom obtain n where "(Th th, n) \ RAG s" by auto + moreover from RAG_target_th[OF this] obtain cs where "n = Cs cs" by auto + ultimately have "(Th th, Cs cs) \ RAG s" by simp + hence "th \ set (wq s cs)" + by (unfold s_RAG_def, auto simp:cs_waiting_def) + from wq_threads [OF this] show ?thesis . +qed + +lemma cp_le: + assumes th_in: "th \ threads s" + shows "cp s th \ Max ((\ th. (preced th s)) ` threads s)" +proof(unfold cp_eq_cpreced cpreced_def cs_dependants_def) + show "Max ((\th. preced th s) ` ({th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+})) + \ Max ((\th. preced th s) ` threads s)" + (is "Max (?f ` ?A) \ Max (?f ` ?B)") + proof(rule Max_f_mono) + show "{th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ {}" by simp + next + from finite_threads + show "finite (threads s)" . + next + from th_in + show "{th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ threads s" + apply (auto simp:Domain_def) + apply (rule_tac dm_RAG_threads) + apply (unfold trancl_domain [of "RAG s", symmetric]) + by (unfold cs_RAG_def s_RAG_def, auto simp:Domain_def) + qed +qed + +lemma max_cp_eq: + shows "Max ((cp s) ` threads s) = Max ((\ th. (preced th s)) ` threads s)" + (is "?l = ?r") +proof(cases "threads s = {}") + case True + thus ?thesis by auto +next + case False + have "?l \ ((cp s) ` threads s)" + proof(rule Max_in) + from finite_threads + show "finite (cp s ` threads s)" by auto + next + from False show "cp s ` threads s \ {}" by auto + qed + then obtain th + where th_in: "th \ threads s" and eq_l: "?l = cp s th" by auto + have "\ \ ?r" by (rule cp_le[OF th_in]) + moreover have "?r \ cp s th" (is "Max (?f ` ?A) \ cp s th") + proof - + have "?r \ (?f ` ?A)" + proof(rule Max_in) + from finite_threads + show " finite ((\th. preced th s) ` threads s)" by auto + next + from False show " (\th. preced th s) ` threads s \ {}" by auto + qed + then obtain th' where + th_in': "th' \ ?A " and eq_r: "?r = ?f th'" by auto + from le_cp [of th'] eq_r + have "?r \ cp s th'" + moreover have "\ \ cp s th" + proof(fold eq_l) + show " cp s th' \ Max (cp s ` threads s)" + proof(rule Max_ge) + from th_in' show "cp s th' \ cp s ` threads s" + by auto + next + from finite_threads + show "finite (cp s ` threads s)" by auto + qed + qed + ultimately show ?thesis by auto + qed + ultimately show ?thesis using eq_l by auto +qed + +lemma max_cp_eq_the_preced: + shows "Max ((cp s) ` threads s) = Max (the_preced s ` threads s)" + using max_cp_eq using the_preced_def by presburger + +end + +lemma preced_v [simp]: "preced th' (V th cs#s) = preced th' s" + by (unfold preced_def, simp) + +lemma (in valid_trace_v) + preced_es: "preced th (e#s) = preced th s" + by (unfold is_v preced_def, simp) + +lemma the_preced_v[simp]: "the_preced (V th cs#s) = the_preced s" +proof + fix th' + show "the_preced (V th cs # s) th' = the_preced s th'" + by (unfold the_preced_def preced_def, simp) +qed + +lemma (in valid_trace_v) + the_preced_es: "the_preced (e#s) = the_preced s" + by (unfold is_v preced_def, simp) + +context valid_trace_p +begin + +lemma not_holding_es_th_cs: "\ holding s th cs" +proof + assume otherwise: "holding s th cs" + from pip_e[unfolded is_p] + show False + proof(cases) + case (thread_P) + moreover have "(Cs cs, Th th) \ RAG s" + using otherwise cs_holding_def + holding_eq th_not_in_wq by auto + ultimately show ?thesis by auto + qed +qed + +lemma waiting_kept: + assumes "waiting s th' cs'" + shows "waiting (e#s) th' cs'" + using assms + by (metis cs_waiting_def hd_append2 list.sel(1) list.set_intros(2) + rotate1.simps(2) self_append_conv2 set_rotate1 + th_not_in_wq waiting_eq wq_es_cs wq_neq_simp) + +lemma holding_kept: + assumes "holding s th' cs'" + shows "holding (e#s) th' cs'" +proof(cases "cs' = cs") + case False + hence "wq (e#s) cs' = wq s cs'" by simp + with assms show ?thesis using cs_holding_def holding_eq by auto +next + case True + from assms[unfolded s_holding_def, folded wq_def] + obtain rest where eq_wq: "wq s cs' = th'#rest" + by (metis empty_iff list.collapse list.set(1)) + hence "wq (e#s) cs' = th'#(rest@[th])" + by (simp add: True wq_es_cs) + thus ?thesis + by (simp add: cs_holding_def holding_eq) +qed + +end + +locale valid_trace_p_h = valid_trace_p + + assumes we: "wq s cs = []" + +locale valid_trace_p_w = valid_trace_p + + assumes wne: "wq s cs \ []" +begin + +definition "holder = hd (wq s cs)" +definition "waiters = tl (wq s cs)" +definition "waiters' = waiters @ [th]" + +lemma wq_s_cs: "wq s cs = holder#waiters" + by (simp add: holder_def waiters_def wne) + +lemma wq_es_cs': "wq (e#s) cs = holder#waiters@[th]" + by (simp add: wq_es_cs wq_s_cs) + +lemma waiting_es_th_cs: "waiting (e#s) th cs" + using cs_waiting_def th_not_in_wq waiting_eq wq_es_cs' wq_s_cs by auto + +lemma RAG_edge: "(Th th, Cs cs) \ RAG (e#s)" + by (unfold s_RAG_def, fold waiting_eq, insert waiting_es_th_cs, auto) + +lemma holding_esE: + assumes "holding (e#s) th' cs'" + obtains "holding s th' cs'" + using assms +proof(cases "cs' = cs") + case False + hence "wq (e#s) cs' = wq s cs'" by simp + with assms show ?thesis + using cs_holding_def holding_eq that by auto +next + case True + with assms show ?thesis + by (metis cs_holding_def holding_eq list.sel(1) list.set_intros(1) that + wq_es_cs' wq_s_cs) +qed + +lemma waiting_esE: + assumes "waiting (e#s) th' cs'" + obtains "th' \ th" "waiting s th' cs'" + | "th' = th" "cs' = cs" +proof(cases "waiting s th' cs'") + case True + have "th' \ th" + proof + assume otherwise: "th' = th" + from True[unfolded this] + show False by (simp add: th_not_waiting) + qed + from that(1)[OF this True] show ?thesis . +next + case False + hence "th' = th \ cs' = cs" + by (metis assms cs_waiting_def holder_def list.sel(1) rotate1.simps(2) + set_ConsD set_rotate1 waiting_eq wq_es_cs wq_es_cs' wq_neq_simp) + with that(2) show ?thesis by metis +qed + +lemma RAG_es: "RAG (e # s) = RAG s \ {(Th th, Cs cs)}" (is "?L = ?R") +proof(rule rel_eqI) + fix n1 n2 + assume "(n1, n2) \ ?L" + thus "(n1, n2) \ ?R" + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from this(3) + show ?thesis + proof(cases rule:waiting_esE) + case 1 + thus ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case 2 + thus ?thesis using waiting(1,2) by auto + qed + next + case (holding th' cs') + from this(3) + show ?thesis + proof(cases rule:holding_esE) + case 1 + with holding(1,2) + show ?thesis by (unfold s_RAG_def, fold holding_eq, auto) + qed + qed +next + fix n1 n2 + assume "(n1, n2) \ ?R" + hence "(n1, n2) \ RAG s \ (n1 = Th th \ n2 = Cs cs)" by auto + thus "(n1, n2) \ ?L" + proof + assume "(n1, n2) \ RAG s" + thus ?thesis + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from waiting_kept[OF this(3)] + show ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case (holding th' cs') + from holding_kept[OF this(3)] + show ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + qed + next + assume "n1 = Th th \ n2 = Cs cs" + thus ?thesis using RAG_edge by auto + qed +qed + +end + +context valid_trace_p_h +begin + +lemma wq_es_cs': "wq (e#s) cs = [th]" + using wq_es_cs[unfolded we] by simp + +lemma holding_es_th_cs: + shows "holding (e#s) th cs" +proof - + from wq_es_cs' + have "th \ set (wq (e#s) cs)" "th = hd (wq (e#s) cs)" by auto + thus ?thesis using cs_holding_def holding_eq by blast +qed + +lemma RAG_edge: "(Cs cs, Th th) \ RAG (e#s)" + by (unfold s_RAG_def, fold holding_eq, insert holding_es_th_cs, auto) + +lemma waiting_esE: + assumes "waiting (e#s) th' cs'" + obtains "waiting s th' cs'" + using assms + by (metis cs_waiting_def event.distinct(15) is_p list.sel(1) + set_ConsD waiting_eq we wq_es_cs' wq_neq_simp wq_out_inv) + +lemma holding_esE: + assumes "holding (e#s) th' cs'" + obtains "cs' \ cs" "holding s th' cs'" + | "cs' = cs" "th' = th" +proof(cases "cs' = cs") + case True + from held_unique[OF holding_es_th_cs assms[unfolded True]] + have "th' = th" by simp + from that(2)[OF True this] show ?thesis . +next + case False + have "holding s th' cs'" using assms + using False cs_holding_def holding_eq by auto + from that(1)[OF False this] show ?thesis . +qed + +lemma RAG_es: "RAG (e # s) = RAG s \ {(Cs cs, Th th)}" (is "?L = ?R") +proof(rule rel_eqI) + fix n1 n2 + assume "(n1, n2) \ ?L" + thus "(n1, n2) \ ?R" + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from this(3) + show ?thesis + proof(cases rule:waiting_esE) + case 1 + thus ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + qed + next + case (holding th' cs') + from this(3) + show ?thesis + proof(cases rule:holding_esE) + case 1 + with holding(1,2) + show ?thesis by (unfold s_RAG_def, fold holding_eq, auto) + next + case 2 + with holding(1,2) show ?thesis by auto + qed + qed +next + fix n1 n2 + assume "(n1, n2) \ ?R" + hence "(n1, n2) \ RAG s \ (n1 = Cs cs \ n2 = Th th)" by auto + thus "(n1, n2) \ ?L" + proof + assume "(n1, n2) \ RAG s" + thus ?thesis + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from waiting_kept[OF this(3)] + show ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case (holding th' cs') + from holding_kept[OF this(3)] + show ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + qed + next + assume "n1 = Cs cs \ n2 = Th th" + with holding_es_th_cs + show ?thesis + by (unfold s_RAG_def, fold holding_eq, auto) + qed +qed + +end + +context valid_trace_p +begin + +lemma RAG_es': "RAG (e # s) = (if (wq s cs = []) then RAG s \ {(Cs cs, Th th)} + else RAG s \ {(Th th, Cs cs)})" +proof(cases "wq s cs = []") + case True + interpret vt_p: valid_trace_p_h using True + by (unfold_locales, simp) + show ?thesis by (simp add: vt_p.RAG_es vt_p.we) +next + case False + interpret vt_p: valid_trace_p_w using False + by (unfold_locales, simp) + show ?thesis by (simp add: vt_p.RAG_es vt_p.wne) +qed + +end + + +end diff -r 17305a85493d -r c495eb16beb6 CpsG_1.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CpsG_1.thy Wed Jan 27 19:28:42 2016 +0800 @@ -0,0 +1,4403 @@ +theory CpsG +imports PIPDefs +begin + +lemma Max_f_mono: + assumes seq: "A \ B" + and np: "A \ {}" + and fnt: "finite B" + shows "Max (f ` A) \ Max (f ` B)" +proof(rule Max_mono) + from seq show "f ` A \ f ` B" by auto +next + from np show "f ` A \ {}" by auto +next + from fnt and seq show "finite (f ` B)" by auto +qed + +(* I am going to use this file as a start point to retrofiting + PIPBasics.thy, which is originally called CpsG.ghy *) + +locale valid_trace = + fixes s + assumes vt : "vt s" + +locale valid_trace_e = valid_trace + + fixes e + assumes vt_e: "vt (e#s)" +begin + +lemma pip_e: "PIP s e" + using vt_e by (cases, simp) + +end + +locale valid_trace_create = valid_trace_e + + fixes th prio + assumes is_create: "e = Create th prio" + +locale valid_trace_exit = valid_trace_e + + fixes th + assumes is_exit: "e = Exit th" + +locale valid_trace_p = valid_trace_e + + fixes th cs + assumes is_p: "e = P th cs" + +locale valid_trace_v = valid_trace_e + + fixes th cs + assumes is_v: "e = V th cs" +begin + definition "rest = tl (wq s cs)" + definition "wq' = (SOME q. distinct q \ set q = set rest)" +end + +locale valid_trace_v_n = valid_trace_v + + assumes rest_nnl: "rest \ []" + +locale valid_trace_v_e = valid_trace_v + + assumes rest_nil: "rest = []" + +locale valid_trace_set= valid_trace_e + + fixes th prio + assumes is_set: "e = Set th prio" + +context valid_trace +begin + +lemma ind [consumes 0, case_names Nil Cons, induct type]: + assumes "PP []" + and "(\s e. valid_trace_e s e \ + PP s \ PIP s e \ PP (e # s))" + shows "PP s" +proof(induct rule:vt.induct[OF vt, case_names Init Step]) + case Init + from assms(1) show ?case . +next + case (Step s e) + show ?case + proof(rule assms(2)) + show "valid_trace_e s e" using Step by (unfold_locales, auto) + next + show "PP s" using Step by simp + next + show "PIP s e" using Step by simp + qed +qed + +end + + +lemma waiting_eq: "waiting s th cs = waiting (wq s) th cs" + by (unfold s_waiting_def cs_waiting_def wq_def, auto) + +lemma holding_eq: "holding (s::state) th cs = holding (wq s) th cs" + by (unfold s_holding_def wq_def cs_holding_def, simp) + +lemma runing_ready: + shows "runing s \ readys s" + unfolding runing_def readys_def + by auto + +lemma readys_threads: + shows "readys s \ threads s" + unfolding readys_def + by auto + +lemma wq_v_neq [simp]: + "cs \ cs' \ wq (V thread cs#s) cs' = wq s cs'" + by (auto simp:wq_def Let_def cp_def split:list.splits) + +lemma runing_head: + assumes "th \ runing s" + and "th \ set (wq_fun (schs s) cs)" + shows "th = hd (wq_fun (schs s) cs)" + using assms + by (simp add:runing_def readys_def s_waiting_def wq_def) + +context valid_trace +begin + +lemma runing_wqE: + assumes "th \ runing s" + and "th \ set (wq s cs)" + obtains rest where "wq s cs = th#rest" +proof - + from assms(2) obtain th' rest where eq_wq: "wq s cs = th'#rest" + by (meson list.set_cases) + have "th' = th" + proof(rule ccontr) + assume "th' \ th" + hence "th \ hd (wq s cs)" using eq_wq by auto + with assms(2) + have "waiting s th cs" + by (unfold s_waiting_def, fold wq_def, auto) + with assms show False + by (unfold runing_def readys_def, auto) + qed + with eq_wq that show ?thesis by metis +qed + +end + +context valid_trace_p +begin + +lemma wq_neq_simp [simp]: + assumes "cs' \ cs" + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_p wq_def + by (auto simp:Let_def) + +lemma runing_th_s: + shows "th \ runing s" +proof - + from pip_e[unfolded is_p] + show ?thesis by (cases, simp) +qed + +lemma th_not_waiting: + "\ waiting s th c" +proof - + have "th \ readys s" + using runing_ready runing_th_s by blast + thus ?thesis + by (unfold readys_def, auto) +qed + +lemma waiting_neq_th: + assumes "waiting s t c" + shows "t \ th" + using assms using th_not_waiting by blast + +lemma th_not_in_wq: + shows "th \ set (wq s cs)" +proof + assume otherwise: "th \ set (wq s cs)" + from runing_wqE[OF runing_th_s this] + obtain rest where eq_wq: "wq s cs = th#rest" by blast + with otherwise + have "holding s th cs" + by (unfold s_holding_def, fold wq_def, simp) + hence cs_th_RAG: "(Cs cs, Th th) \ RAG s" + by (unfold s_RAG_def, fold holding_eq, auto) + from pip_e[unfolded is_p] + show False + proof(cases) + case (thread_P) + with cs_th_RAG show ?thesis by auto + qed +qed + +lemma wq_es_cs: + "wq (e#s) cs = wq s cs @ [th]" + by (unfold is_p wq_def, auto simp:Let_def) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" +proof(cases "cs' = cs") + case True + show ?thesis using True assms th_not_in_wq + by (unfold True wq_es_cs, auto) +qed (insert assms, simp) + +end + + +context valid_trace_v +begin + +lemma wq_neq_simp [simp]: + assumes "cs' \ cs" + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_v wq_def + by (auto simp:Let_def) + +lemma runing_th_s: + shows "th \ runing s" +proof - + from pip_e[unfolded is_v] + show ?thesis by (cases, simp) +qed + +lemma th_not_waiting: + "\ waiting s th c" +proof - + have "th \ readys s" + using runing_ready runing_th_s by blast + thus ?thesis + by (unfold readys_def, auto) +qed + +lemma waiting_neq_th: + assumes "waiting s t c" + shows "t \ th" + using assms using th_not_waiting by blast + +lemma wq_s_cs: + "wq s cs = th#rest" +proof - + from pip_e[unfolded is_v] + show ?thesis + proof(cases) + case (thread_V) + from this(2) show ?thesis + by (unfold rest_def s_holding_def, fold wq_def, + metis empty_iff list.collapse list.set(1)) + qed +qed + +lemma wq_es_cs: + "wq (e#s) cs = wq'" + using wq_s_cs[unfolded wq_def] + by (auto simp:Let_def wq_def rest_def wq'_def is_v, simp) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" +proof(cases "cs' = cs") + case True + show ?thesis + proof(unfold True wq_es_cs wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + using assms[unfolded True wq_s_cs] by auto + qed simp +qed (insert assms, simp) + +end + +context valid_trace +begin + +lemma actor_inv: + assumes "PIP s e" + and "\ isCreate e" + shows "actor e \ runing s" + using assms + by (induct, auto) + +lemma isP_E: + assumes "isP e" + obtains cs where "e = P (actor e) cs" + using assms by (cases e, auto) + +lemma isV_E: + assumes "isV e" + obtains cs where "e = V (actor e) cs" + using assms by (cases e, auto) + +lemma wq_distinct: "distinct (wq s cs)" +proof(induct rule:ind) + case (Cons s e) + interpret vt_e: valid_trace_e s e using Cons by simp + show ?case + proof(cases e) + case (V th cs) + interpret vt_v: valid_trace_v s e th cs using V by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_v.wq_distinct_kept) + qed +qed (unfold wq_def Let_def, simp) + +end + +context valid_trace_e +begin + +text {* + The following lemma shows that only the @{text "P"} + operation can add new thread into waiting queues. + Such kind of lemmas are very obvious, but need to be checked formally. + This is a kind of confirmation that our modelling is correct. +*} + +lemma wq_in_inv: + assumes s_ni: "thread \ set (wq s cs)" + and s_i: "thread \ set (wq (e#s) cs)" + shows "e = P thread cs" +proof(cases e) + -- {* This is the only non-trivial case: *} + case (V th cs1) + have False + proof(cases "cs1 = cs") + case True + show ?thesis + proof(cases "(wq s cs1)") + case (Cons w_hd w_tl) + have "set (wq (e#s) cs) \ set (wq s cs)" + proof - + have "(wq (e#s) cs) = (SOME q. distinct q \ set q = set w_tl)" + using Cons V by (auto simp:wq_def Let_def True split:if_splits) + moreover have "set ... \ set (wq s cs)" + proof(rule someI2) + show "distinct w_tl \ set w_tl = set w_tl" + by (metis distinct.simps(2) local.Cons wq_distinct) + qed (insert Cons True, auto) + ultimately show ?thesis by simp + qed + with assms show ?thesis by auto + qed (insert assms V True, auto simp:wq_def Let_def split:if_splits) + qed (insert assms V, auto simp:wq_def Let_def split:if_splits) + thus ?thesis by auto +qed (insert assms, auto simp:wq_def Let_def split:if_splits) + +lemma wq_out_inv: + assumes s_in: "thread \ set (wq s cs)" + and s_hd: "thread = hd (wq s cs)" + and s_i: "thread \ hd (wq (e#s) cs)" + shows "e = V thread cs" +proof(cases e) +-- {* There are only two non-trivial cases: *} + case (V th cs1) + show ?thesis + proof(cases "cs1 = cs") + case True + have "PIP s (V th cs)" using pip_e[unfolded V[unfolded True]] . + thus ?thesis + proof(cases) + case (thread_V) + moreover have "th = thread" using thread_V(2) s_hd + by (unfold s_holding_def wq_def, simp) + ultimately show ?thesis using V True by simp + qed + qed (insert assms V, auto simp:wq_def Let_def split:if_splits) +next + case (P th cs1) + show ?thesis + proof(cases "cs1 = cs") + case True + with P have "wq (e#s) cs = wq_fun (schs s) cs @ [th]" + by (auto simp:wq_def Let_def split:if_splits) + with s_i s_hd s_in have False + by (metis empty_iff hd_append2 list.set(1) wq_def) + thus ?thesis by simp + qed (insert assms P, auto simp:wq_def Let_def split:if_splits) +qed (insert assms, auto simp:wq_def Let_def split:if_splits) + +end + + + +context valid_trace +begin + + +text {* (* ddd *) + The nature of the work is like this: since it starts from a very simple and basic + model, even intuitively very `basic` and `obvious` properties need to derived from scratch. + For instance, the fact + that one thread can not be blocked by two critical resources at the same time + is obvious, because only running threads can make new requests, if one is waiting for + a critical resource and get blocked, it can not make another resource request and get + blocked the second time (because it is not running). + + To derive this fact, one needs to prove by contraction and + reason about time (or @{text "moement"}). The reasoning is based on a generic theorem + named @{text "p_split"}, which is about status changing along the time axis. It says if + a condition @{text "Q"} is @{text "True"} at a state @{text "s"}, + but it was @{text "False"} at the very beginning, then there must exits a moment @{text "t"} + in the history of @{text "s"} (notice that @{text "s"} itself is essentially the history + of events leading to it), such that @{text "Q"} switched + from being @{text "False"} to @{text "True"} and kept being @{text "True"} + till the last moment of @{text "s"}. + + Suppose a thread @{text "th"} is blocked + on @{text "cs1"} and @{text "cs2"} in some state @{text "s"}, + since no thread is blocked at the very beginning, by applying + @{text "p_split"} to these two blocking facts, there exist + two moments @{text "t1"} and @{text "t2"} in @{text "s"}, such that + @{text "th"} got blocked on @{text "cs1"} and @{text "cs2"} + and kept on blocked on them respectively ever since. + + Without lost of generality, we assume @{text "t1"} is earlier than @{text "t2"}. + However, since @{text "th"} was blocked ever since memonent @{text "t1"}, so it was still + in blocked state at moment @{text "t2"} and could not + make any request and get blocked the second time: Contradiction. +*} + +lemma waiting_unique_pre: (* ddd *) + assumes h11: "thread \ set (wq s cs1)" + and h12: "thread \ hd (wq s cs1)" + assumes h21: "thread \ set (wq s cs2)" + and h22: "thread \ hd (wq s cs2)" + and neq12: "cs1 \ cs2" + shows "False" +proof - + let "?Q" = "\ cs s. thread \ set (wq s cs) \ thread \ hd (wq s cs)" + from h11 and h12 have q1: "?Q cs1 s" by simp + from h21 and h22 have q2: "?Q cs2 s" by simp + have nq1: "\ ?Q cs1 []" by (simp add:wq_def) + have nq2: "\ ?Q cs2 []" by (simp add:wq_def) + from p_split [of "?Q cs1", OF q1 nq1] + obtain t1 where lt1: "t1 < length s" + and np1: "\ ?Q cs1 (moment t1 s)" + and nn1: "(\i'>t1. ?Q cs1 (moment i' s))" by auto + from p_split [of "?Q cs2", OF q2 nq2] + obtain t2 where lt2: "t2 < length s" + and np2: "\ ?Q cs2 (moment t2 s)" + and nn2: "(\i'>t2. ?Q cs2 (moment i' s))" by auto + { fix s cs + assume q: "?Q cs s" + have "thread \ runing s" + proof + assume "thread \ runing s" + hence " \cs. \ (thread \ set (wq_fun (schs s) cs) \ + thread \ hd (wq_fun (schs s) cs))" + by (unfold runing_def s_waiting_def readys_def, auto) + from this[rule_format, of cs] q + show False by (simp add: wq_def) + qed + } note q_not_runing = this + { fix t1 t2 cs1 cs2 + assume lt1: "t1 < length s" + and np1: "\ ?Q cs1 (moment t1 s)" + and nn1: "(\i'>t1. ?Q cs1 (moment i' s))" + and lt2: "t2 < length s" + and np2: "\ ?Q cs2 (moment t2 s)" + and nn2: "(\i'>t2. ?Q cs2 (moment i' s))" + and lt12: "t1 < t2" + let ?t3 = "Suc t2" + from lt2 have le_t3: "?t3 \ length s" by auto + from moment_plus [OF this] + obtain e where eq_m: "moment ?t3 s = e#moment t2 s" by auto + have "t2 < ?t3" by simp + from nn2 [rule_format, OF this] and eq_m + have h1: "thread \ set (wq (e#moment t2 s) cs2)" and + h2: "thread \ hd (wq (e#moment t2 s) cs2)" by auto + have "vt (e#moment t2 s)" + proof - + from vt_moment + have "vt (moment ?t3 s)" . + with eq_m show ?thesis by simp + qed + then interpret vt_e: valid_trace_e "moment t2 s" "e" + by (unfold_locales, auto, cases, simp) + have ?thesis + proof - + have "thread \ runing (moment t2 s)" + proof(cases "thread \ set (wq (moment t2 s) cs2)") + case True + have "e = V thread cs2" + proof - + have eq_th: "thread = hd (wq (moment t2 s) cs2)" + using True and np2 by auto + from vt_e.wq_out_inv[OF True this h2] + show ?thesis . + qed + thus ?thesis using vt_e.actor_inv[OF vt_e.pip_e] by auto + next + case False + have "e = P thread cs2" using vt_e.wq_in_inv[OF False h1] . + with vt_e.actor_inv[OF vt_e.pip_e] + show ?thesis by auto + qed + moreover have "thread \ runing (moment t2 s)" + by (rule q_not_runing[OF nn1[rule_format, OF lt12]]) + ultimately show ?thesis by simp + qed + } note lt_case = this + show ?thesis + proof - + { assume "t1 < t2" + from lt_case[OF lt1 np1 nn1 lt2 np2 nn2 this] + have ?thesis . + } moreover { + assume "t2 < t1" + from lt_case[OF lt2 np2 nn2 lt1 np1 nn1 this] + have ?thesis . + } moreover { + assume eq_12: "t1 = t2" + let ?t3 = "Suc t2" + from lt2 have le_t3: "?t3 \ length s" by auto + from moment_plus [OF this] + obtain e where eq_m: "moment ?t3 s = e#moment t2 s" by auto + have lt_2: "t2 < ?t3" by simp + from nn2 [rule_format, OF this] and eq_m + have h1: "thread \ set (wq (e#moment t2 s) cs2)" and + h2: "thread \ hd (wq (e#moment t2 s) cs2)" by auto + from nn1[rule_format, OF lt_2[folded eq_12]] eq_m[folded eq_12] + have g1: "thread \ set (wq (e#moment t1 s) cs1)" and + g2: "thread \ hd (wq (e#moment t1 s) cs1)" by auto + have "vt (e#moment t2 s)" + proof - + from vt_moment + have "vt (moment ?t3 s)" . + with eq_m show ?thesis by simp + qed + then interpret vt_e: valid_trace_e "moment t2 s" "e" + by (unfold_locales, auto, cases, simp) + have "e = V thread cs2 \ e = P thread cs2" + proof(cases "thread \ set (wq (moment t2 s) cs2)") + case True + have "e = V thread cs2" + proof - + have eq_th: "thread = hd (wq (moment t2 s) cs2)" + using True and np2 by auto + from vt_e.wq_out_inv[OF True this h2] + show ?thesis . + qed + thus ?thesis by auto + next + case False + have "e = P thread cs2" using vt_e.wq_in_inv[OF False h1] . + thus ?thesis by auto + qed + moreover have "e = V thread cs1 \ e = P thread cs1" + proof(cases "thread \ set (wq (moment t1 s) cs1)") + case True + have eq_th: "thread = hd (wq (moment t1 s) cs1)" + using True and np1 by auto + from vt_e.wq_out_inv[folded eq_12, OF True this g2] + have "e = V thread cs1" . + thus ?thesis by auto + next + case False + have "e = P thread cs1" using vt_e.wq_in_inv[folded eq_12, OF False g1] . + thus ?thesis by auto + qed + ultimately have ?thesis using neq12 by auto + } ultimately show ?thesis using nat_neq_iff by blast + qed +qed + +text {* + This lemma is a simple corrolary of @{text "waiting_unique_pre"}. +*} + +lemma waiting_unique: + assumes "waiting s th cs1" + and "waiting s th cs2" + shows "cs1 = cs2" + using waiting_unique_pre assms + unfolding wq_def s_waiting_def + by auto + +end + +(* not used *) +text {* + Every thread can only be blocked on one critical resource, + symmetrically, every critical resource can only be held by one thread. + This fact is much more easier according to our definition. +*} +lemma held_unique: + assumes "holding (s::event list) th1 cs" + and "holding s th2 cs" + shows "th1 = th2" + by (insert assms, unfold s_holding_def, auto) + + +lemma last_set_lt: "th \ threads s \ last_set th s < length s" + apply (induct s, auto) + by (case_tac a, auto split:if_splits) + +lemma last_set_unique: + "\last_set th1 s = last_set th2 s; th1 \ threads s; th2 \ threads s\ + \ th1 = th2" + apply (induct s, auto) + by (case_tac a, auto split:if_splits dest:last_set_lt) + +lemma preced_unique : + assumes pcd_eq: "preced th1 s = preced th2 s" + and th_in1: "th1 \ threads s" + and th_in2: " th2 \ threads s" + shows "th1 = th2" +proof - + from pcd_eq have "last_set th1 s = last_set th2 s" by (simp add:preced_def) + from last_set_unique [OF this th_in1 th_in2] + show ?thesis . +qed + +lemma preced_linorder: + assumes neq_12: "th1 \ th2" + and th_in1: "th1 \ threads s" + and th_in2: " th2 \ threads s" + shows "preced th1 s < preced th2 s \ preced th1 s > preced th2 s" +proof - + from preced_unique [OF _ th_in1 th_in2] and neq_12 + have "preced th1 s \ preced th2 s" by auto + thus ?thesis by auto +qed + +(* An aux lemma used later *) +lemma unique_minus: + assumes unique: "\ a b c. \(a, b) \ r; (a, c) \ r\ \ b = c" + and xy: "(x, y) \ r" + and xz: "(x, z) \ r^+" + and neq: "y \ z" + shows "(y, z) \ r^+" +proof - + from xz and neq show ?thesis + proof(induct) + case (base ya) + have "(x, ya) \ r" by fact + from unique [OF xy this] have "y = ya" . + with base show ?case by auto + next + case (step ya z) + show ?case + proof(cases "y = ya") + case True + from step True show ?thesis by simp + next + case False + from step False + show ?thesis by auto + qed + qed +qed + +lemma unique_base: + assumes unique: "\ a b c. \(a, b) \ r; (a, c) \ r\ \ b = c" + and xy: "(x, y) \ r" + and xz: "(x, z) \ r^+" + and neq_yz: "y \ z" + shows "(y, z) \ r^+" +proof - + from xz neq_yz show ?thesis + proof(induct) + case (base ya) + from xy unique base show ?case by auto + next + case (step ya z) + show ?case + proof(cases "y = ya") + case True + from True step show ?thesis by auto + next + case False + from False step + have "(y, ya) \ r\<^sup>+" by auto + with step show ?thesis by auto + qed + qed +qed + +lemma unique_chain: + assumes unique: "\ a b c. \(a, b) \ r; (a, c) \ r\ \ b = c" + and xy: "(x, y) \ r^+" + and xz: "(x, z) \ r^+" + and neq_yz: "y \ z" + shows "(y, z) \ r^+ \ (z, y) \ r^+" +proof - + from xy xz neq_yz show ?thesis + proof(induct) + case (base y) + have h1: "(x, y) \ r" and h2: "(x, z) \ r\<^sup>+" and h3: "y \ z" using base by auto + from unique_base [OF _ h1 h2 h3] and unique show ?case by auto + next + case (step y za) + show ?case + proof(cases "y = z") + case True + from True step show ?thesis by auto + next + case False + from False step have "(y, z) \ r\<^sup>+ \ (z, y) \ r\<^sup>+" by auto + thus ?thesis + proof + assume "(z, y) \ r\<^sup>+" + with step have "(z, za) \ r\<^sup>+" by auto + thus ?thesis by auto + next + assume h: "(y, z) \ r\<^sup>+" + from step have yza: "(y, za) \ r" by simp + from step have "za \ z" by simp + from unique_minus [OF _ yza h this] and unique + have "(za, z) \ r\<^sup>+" by auto + thus ?thesis by auto + qed + qed + qed +qed + +text {* + The following three lemmas show that @{text "RAG"} does not change + by the happening of @{text "Set"}, @{text "Create"} and @{text "Exit"} + events, respectively. +*} + +lemma RAG_set_unchanged: "(RAG (Set th prio # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + +lemma RAG_create_unchanged: "(RAG (Create th prio # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + +lemma RAG_exit_unchanged: "(RAG (Exit th # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + + +context valid_trace_v +begin + + +lemma distinct_rest: "distinct rest" + by (simp add: distinct_tl rest_def wq_distinct) + +definition "wq' = (SOME q. distinct q \ set q = set rest)" + +lemma runing_th_s: + shows "th \ runing s" +proof - + from pip_e[unfolded is_v] + show ?thesis by (cases, simp) +qed + +lemma holding_cs_eq_th: + assumes "holding s t cs" + shows "t = th" +proof - + from pip_e[unfolded is_v] + show ?thesis + proof(cases) + case (thread_V) + from held_unique[OF this(2) assms] + show ?thesis by simp + qed +qed + +lemma th_not_waiting: + "\ waiting s th c" +proof - + have "th \ readys s" + using runing_ready runing_th_s by blast + thus ?thesis + by (unfold readys_def, auto) +qed + +lemma waiting_neq_th: + assumes "waiting s t c" + shows "t \ th" + using assms using th_not_waiting by blast + +lemma wq_s_cs: + "wq s cs = th#rest" +proof - + from pip_e[unfolded is_v] + show ?thesis + proof(cases) + case (thread_V) + from this(2) show ?thesis + by (unfold rest_def s_holding_def, fold wq_def, + metis empty_iff list.collapse list.set(1)) + qed +qed + +lemma wq_es_cs: + "wq (e#s) cs = wq'" + using wq_s_cs[unfolded wq_def] + by (auto simp:Let_def wq_def rest_def wq'_def is_v, simp) + +lemma distinct_wq': "distinct wq'" + by (metis (mono_tags, lifting) distinct_rest some_eq_ex wq'_def) + +lemma th'_in_inv: + assumes "th' \ set wq'" + shows "th' \ set rest" + using assms + by (metis (mono_tags, lifting) distinct.simps(2) + rest_def some_eq_ex wq'_def wq_distinct wq_s_cs) + +lemma neq_t_th: + assumes "waiting (e#s) t c" + shows "t \ th" +proof + assume otherwise: "t = th" + show False + proof(cases "c = cs") + case True + have "t \ set wq'" + using assms[unfolded True s_waiting_def, folded wq_def, unfolded wq_es_cs] + by simp + from th'_in_inv[OF this] have "t \ set rest" . + with wq_s_cs[folded otherwise] wq_distinct[of cs] + show ?thesis by simp + next + case False + have "wq (e#s) c = wq s c" using False + by (unfold is_v, simp) + hence "waiting s t c" using assms + by (simp add: cs_waiting_def waiting_eq) + hence "t \ readys s" by (unfold readys_def, auto) + hence "t \ runing s" using runing_ready by auto + with runing_th_s[folded otherwise] show ?thesis by auto + qed +qed + +lemma waiting_esI1: + assumes "waiting s t c" + and "c \ cs" + shows "waiting (e#s) t c" +proof - + have "wq (e#s) c = wq s c" + using assms(2) is_v by auto + with assms(1) show ?thesis + using cs_waiting_def waiting_eq by auto +qed + +lemma holding_esI2: + assumes "c \ cs" + and "holding s t c" + shows "holding (e#s) t c" +proof - + from assms(1) have "wq (e#s) c = wq s c" using is_v by auto + from assms(2)[unfolded s_holding_def, folded wq_def, + folded this, unfolded wq_def, folded s_holding_def] + show ?thesis . +qed + +lemma holding_esI1: + assumes "holding s t c" + and "t \ th" + shows "holding (e#s) t c" +proof - + have "c \ cs" using assms using holding_cs_eq_th by blast + from holding_esI2[OF this assms(1)] + show ?thesis . +qed + +end + +context valid_trace_v_n +begin + +lemma neq_wq': "wq' \ []" +proof (unfold wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) +next + fix x + assume " distinct x \ set x = set rest" + thus "x \ []" using rest_nnl by auto +qed + +definition "taker = hd wq'" + +definition "rest' = tl wq'" + +lemma eq_wq': "wq' = taker # rest'" + by (simp add: neq_wq' rest'_def taker_def) + +lemma next_th_taker: + shows "next_th s th cs taker" + using rest_nnl taker_def wq'_def wq_s_cs + by (auto simp:next_th_def) + +lemma taker_unique: + assumes "next_th s th cs taker'" + shows "taker' = taker" +proof - + from assms + obtain rest' where + h: "wq s cs = th # rest'" + "taker' = hd (SOME q. distinct q \ set q = set rest')" + by (unfold next_th_def, auto) + with wq_s_cs have "rest' = rest" by auto + thus ?thesis using h(2) taker_def wq'_def by auto +qed + +lemma waiting_set_eq: + "{(Th th', Cs cs) |th'. next_th s th cs th'} = {(Th taker, Cs cs)}" + by (smt all_not_in_conv bot.extremum insertI1 insert_subset + mem_Collect_eq next_th_taker subsetI subset_antisym taker_def taker_unique) + +lemma holding_set_eq: + "{(Cs cs, Th th') |th'. next_th s th cs th'} = {(Cs cs, Th taker)}" + using next_th_taker taker_def waiting_set_eq + by fastforce + +lemma holding_taker: + shows "holding (e#s) taker cs" + by (unfold s_holding_def, fold wq_def, unfold wq_es_cs, + auto simp:neq_wq' taker_def) + +lemma waiting_esI2: + assumes "waiting s t cs" + and "t \ taker" + shows "waiting (e#s) t cs" +proof - + have "t \ set wq'" + proof(unfold wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) + next + fix x + assume "distinct x \ set x = set rest" + moreover have "t \ set rest" + using assms(1) cs_waiting_def waiting_eq wq_s_cs by auto + ultimately show "t \ set x" by simp + qed + moreover have "t \ hd wq'" + using assms(2) taker_def by auto + ultimately show ?thesis + by (unfold s_waiting_def, fold wq_def, unfold wq_es_cs, simp) +qed + +lemma waiting_esE: + assumes "waiting (e#s) t c" + obtains "c \ cs" "waiting s t c" + | "c = cs" "t \ taker" "waiting s t cs" "t \ set rest'" +proof(cases "c = cs") + case False + hence "wq (e#s) c = wq s c" using is_v by auto + with assms have "waiting s t c" using cs_waiting_def waiting_eq by auto + from that(1)[OF False this] show ?thesis . +next + case True + from assms[unfolded s_waiting_def True, folded wq_def, unfolded wq_es_cs] + have "t \ hd wq'" "t \ set wq'" by auto + hence "t \ taker" by (simp add: taker_def) + moreover hence "t \ th" using assms neq_t_th by blast + moreover have "t \ set rest" by (simp add: `t \ set wq'` th'_in_inv) + ultimately have "waiting s t cs" + by (metis cs_waiting_def list.distinct(2) list.sel(1) + list.set_sel(2) rest_def waiting_eq wq_s_cs) + show ?thesis using that(2) + using True `t \ set wq'` `t \ taker` `waiting s t cs` eq_wq' by auto +qed + +lemma holding_esI1: + assumes "c = cs" + and "t = taker" + shows "holding (e#s) t c" + by (unfold assms, simp add: holding_taker) + +lemma holding_esE: + assumes "holding (e#s) t c" + obtains "c = cs" "t = taker" + | "c \ cs" "holding s t c" +proof(cases "c = cs") + case True + from assms[unfolded True, unfolded s_holding_def, + folded wq_def, unfolded wq_es_cs] + have "t = taker" by (simp add: taker_def) + from that(1)[OF True this] show ?thesis . +next + case False + hence "wq (e#s) c = wq s c" using is_v by auto + from assms[unfolded s_holding_def, folded wq_def, + unfolded this, unfolded wq_def, folded s_holding_def] + have "holding s t c" . + from that(2)[OF False this] show ?thesis . +qed + +end + + +context valid_trace_v_n +begin + +lemma nil_wq': "wq' = []" +proof (unfold wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) +next + fix x + assume " distinct x \ set x = set rest" + thus "x = []" using rest_nil by auto +qed + +lemma no_taker: + assumes "next_th s th cs taker" + shows "False" +proof - + from assms[unfolded next_th_def] + obtain rest' where "wq s cs = th # rest'" "rest' \ []" + by auto + thus ?thesis using rest_def rest_nil by auto +qed + +lemma waiting_set_eq: + "{(Th th', Cs cs) |th'. next_th s th cs th'} = {}" + using no_taker by auto + +lemma holding_set_eq: + "{(Cs cs, Th th') |th'. next_th s th cs th'} = {}" + using no_taker by auto + +lemma no_holding: + assumes "holding (e#s) taker cs" + shows False +proof - + from wq_es_cs[unfolded nil_wq'] + have " wq (e # s) cs = []" . + from assms[unfolded s_holding_def, folded wq_def, unfolded this] + show ?thesis by auto +qed + +lemma no_waiting: + assumes "waiting (e#s) t cs" + shows False +proof - + from wq_es_cs[unfolded nil_wq'] + have " wq (e # s) cs = []" . + from assms[unfolded s_waiting_def, folded wq_def, unfolded this] + show ?thesis by auto +qed + +lemma waiting_esI2: + assumes "waiting s t c" + shows "waiting (e#s) t c" +proof - + have "c \ cs" using assms + using cs_waiting_def rest_nil waiting_eq wq_s_cs by auto + from waiting_esI1[OF assms this] + show ?thesis . +qed + +lemma waiting_esE: + assumes "waiting (e#s) t c" + obtains "c \ cs" "waiting s t c" +proof(cases "c = cs") + case False + hence "wq (e#s) c = wq s c" using is_v by auto + with assms have "waiting s t c" using cs_waiting_def waiting_eq by auto + from that(1)[OF False this] show ?thesis . +next + case True + from no_waiting[OF assms[unfolded True]] + show ?thesis by auto +qed + +lemma holding_esE: + assumes "holding (e#s) t c" + obtains "c \ cs" "holding s t c" +proof(cases "c = cs") + case True + from no_holding[OF assms[unfolded True]] + show ?thesis by auto +next + case False + hence "wq (e#s) c = wq s c" using is_v by auto + from assms[unfolded s_holding_def, folded wq_def, + unfolded this, unfolded wq_def, folded s_holding_def] + have "holding s t c" . + from that[OF False this] show ?thesis . +qed + +end (* ccc *) + +lemma rel_eqI: + assumes "\ x y. (x,y) \ A \ (x,y) \ B" + and "\ x y. (x,y) \ B \ (x, y) \ A" + shows "A = B" + using assms by auto + +lemma in_RAG_E: + assumes "(n1, n2) \ RAG (s::state)" + obtains (waiting) th cs where "n1 = Th th" "n2 = Cs cs" "waiting s th cs" + | (holding) th cs where "n1 = Cs cs" "n2 = Th th" "holding s th cs" + using assms[unfolded s_RAG_def, folded waiting_eq holding_eq] + by auto + +context valid_trace_v +begin + +lemma RAG_es: + "RAG (e # s) = + RAG s - {(Cs cs, Th th)} - + {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'}" (is "?L = ?R") +proof(rule rel_eqI) + fix n1 n2 + assume "(n1, n2) \ ?L" + thus "(n1, n2) \ ?R" + proof(cases rule:in_RAG_E) + case (waiting th' cs') + show ?thesis + proof(cases "rest = []") + case False + interpret h_n: valid_trace_v_n s e th cs + by (unfold_locales, insert False, simp) + from waiting(3) + show ?thesis + proof(cases rule:h_n.waiting_esE) + case 1 + with waiting(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + next + case 2 + with waiting(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + qed + next + case True + interpret h_e: valid_trace_v_e s e th cs + by (unfold_locales, insert True, simp) + from waiting(3) + show ?thesis + proof(cases rule:h_e.waiting_esE) + case 1 + with waiting(1,2) + show ?thesis + by (unfold h_e.waiting_set_eq h_e.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + qed + qed + next + case (holding th' cs') + show ?thesis + proof(cases "rest = []") + case False + interpret h_n: valid_trace_v_n s e th cs + by (unfold_locales, insert False, simp) + from holding(3) + show ?thesis + proof(cases rule:h_n.holding_esE) + case 1 + with holding(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + next + case 2 + with holding(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold holding_eq, auto) + qed + next + case True + interpret h_e: valid_trace_v_e s e th cs + by (unfold_locales, insert True, simp) + from holding(3) + show ?thesis + proof(cases rule:h_e.holding_esE) + case 1 + with holding(1,2) + show ?thesis + by (unfold h_e.waiting_set_eq h_e.holding_set_eq s_RAG_def, + fold holding_eq, auto) + qed + qed + qed +next + fix n1 n2 + assume h: "(n1, n2) \ ?R" + show "(n1, n2) \ ?L" + proof(cases "rest = []") + case False + interpret h_n: valid_trace_v_n s e th cs + by (unfold_locales, insert False, simp) + from h[unfolded h_n.waiting_set_eq h_n.holding_set_eq] + have "((n1, n2) \ RAG s \ (n1 \ Cs cs \ n2 \ Th th) + \ (n1 \ Th h_n.taker \ n2 \ Cs cs)) \ + (n2 = Th h_n.taker \ n1 = Cs cs)" + by auto + thus ?thesis + proof + assume "n2 = Th h_n.taker \ n1 = Cs cs" + with h_n.holding_taker + show ?thesis + by (unfold s_RAG_def, fold holding_eq, auto) + next + assume h: "(n1, n2) \ RAG s \ + (n1 \ Cs cs \ n2 \ Th th) \ (n1 \ Th h_n.taker \ n2 \ Cs cs)" + hence "(n1, n2) \ RAG s" by simp + thus ?thesis + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from h and this(1,2) + have "th' \ h_n.taker \ cs' \ cs" by auto + hence "waiting (e#s) th' cs'" + proof + assume "cs' \ cs" + from waiting_esI1[OF waiting(3) this] + show ?thesis . + next + assume neq_th': "th' \ h_n.taker" + show ?thesis + proof(cases "cs' = cs") + case False + from waiting_esI1[OF waiting(3) this] + show ?thesis . + next + case True + from h_n.waiting_esI2[OF waiting(3)[unfolded True] neq_th', folded True] + show ?thesis . + qed + qed + thus ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case (holding th' cs') + from h this(1,2) + have "cs' \ cs \ th' \ th" by auto + hence "holding (e#s) th' cs'" + proof + assume "cs' \ cs" + from holding_esI2[OF this holding(3)] + show ?thesis . + next + assume "th' \ th" + from holding_esI1[OF holding(3) this] + show ?thesis . + qed + thus ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + qed + qed + next + case True + interpret h_e: valid_trace_v_e s e th cs + by (unfold_locales, insert True, simp) + from h[unfolded h_e.waiting_set_eq h_e.holding_set_eq] + have h_s: "(n1, n2) \ RAG s" "(n1, n2) \ (Cs cs, Th th)" + by auto + from h_s(1) + show ?thesis + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from h_e.waiting_esI2[OF this(3)] + show ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case (holding th' cs') + with h_s(2) + have "cs' \ cs \ th' \ th" by auto + thus ?thesis + proof + assume neq_cs: "cs' \ cs" + from holding_esI2[OF this holding(3)] + show ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + next + assume "th' \ th" + from holding_esI1[OF holding(3) this] + show ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + qed + qed + qed +qed + +end + + + +context valid_trace +begin + +lemma finite_threads: + shows "finite (threads s)" +using vt by (induct) (auto elim: step.cases) + +lemma cp_eq_cpreced: "cp s th = cpreced (wq s) s th" +unfolding cp_def wq_def +apply(induct s rule: schs.induct) +apply(simp add: Let_def cpreced_initial) +apply(simp add: Let_def) +apply(simp add: Let_def) +apply(simp add: Let_def) +apply(subst (2) schs.simps) +apply(simp add: Let_def) +apply(subst (2) schs.simps) +apply(simp add: Let_def) +done + +lemma RAG_target_th: "(Th th, x) \ RAG (s::state) \ \ cs. x = Cs cs" + by (unfold s_RAG_def, auto) + +lemma wq_threads: + assumes h: "th \ set (wq s cs)" + shows "th \ threads s" + + +lemma wq_threads: + assumes h: "th \ set (wq s cs)" + shows "th \ threads s" +proof - + from vt and h show ?thesis + proof(induct arbitrary: th cs) + case (vt_cons s e) + interpret vt_s: valid_trace s + using vt_cons(1) by (unfold_locales, auto) + assume ih: "\th cs. th \ set (wq s cs) \ th \ threads s" + and stp: "step s e" + and vt: "vt s" + and h: "th \ set (wq (e # s) cs)" + show ?case + proof(cases e) + case (Create th' prio) + with ih h show ?thesis + by (auto simp:wq_def Let_def) + next + case (Exit th') + with stp ih h show ?thesis + apply (auto simp:wq_def Let_def) + apply (ind_cases "step s (Exit th')") + apply (auto simp:runing_def readys_def s_holding_def s_waiting_def holdents_def + s_RAG_def s_holding_def cs_holding_def) + done + next + case (V th' cs') + show ?thesis + proof(cases "cs' = cs") + case False + with h + show ?thesis + apply(unfold wq_def V, auto simp:Let_def V split:prod.splits, fold wq_def) + by (drule_tac ih, simp) + next + case True + from h + show ?thesis + proof(unfold V wq_def) + assume th_in: "th \ set (wq_fun (schs (V th' cs' # s)) cs)" (is "th \ set ?l") + show "th \ threads (V th' cs' # s)" + proof(cases "cs = cs'") + case False + hence "?l = wq_fun (schs s) cs" by (simp add:Let_def) + with th_in have " th \ set (wq s cs)" + by (fold wq_def, simp) + from ih [OF this] show ?thesis by simp + next + case True + show ?thesis + proof(cases "wq_fun (schs s) cs'") + case Nil + with h V show ?thesis + apply (auto simp:wq_def Let_def split:if_splits) + by (fold wq_def, drule_tac ih, simp) + next + case (Cons a rest) + assume eq_wq: "wq_fun (schs s) cs' = a # rest" + with h V show ?thesis + apply (auto simp:Let_def wq_def split:if_splits) + proof - + assume th_in: "th \ set (SOME q. distinct q \ set q = set rest)" + have "set (SOME q. distinct q \ set q = set rest) = set rest" + proof(rule someI2) + from vt_s.wq_distinct[of cs'] and eq_wq[folded wq_def] + show "distinct rest \ set rest = set rest" by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" + by auto + qed + with eq_wq th_in have "th \ set (wq_fun (schs s) cs')" by auto + from ih[OF this[folded wq_def]] show "th \ threads s" . + next + assume th_in: "th \ set (wq_fun (schs s) cs)" + from ih[OF this[folded wq_def]] + show "th \ threads s" . + qed + qed + qed + qed + qed + next + case (P th' cs') + from h stp + show ?thesis + apply (unfold P wq_def) + apply (auto simp:Let_def split:if_splits, fold wq_def) + apply (auto intro:ih) + apply(ind_cases "step s (P th' cs')") + by (unfold runing_def readys_def, auto) + next + case (Set thread prio) + with ih h show ?thesis + by (auto simp:wq_def Let_def) + qed + next + case vt_nil + thus ?case by (auto simp:wq_def) + qed +qed + +lemma dm_RAG_threads: + assumes in_dom: "(Th th) \ Domain (RAG s)" + shows "th \ threads s" +proof - + from in_dom obtain n where "(Th th, n) \ RAG s" by auto + moreover from RAG_target_th[OF this] obtain cs where "n = Cs cs" by auto + ultimately have "(Th th, Cs cs) \ RAG s" by simp + hence "th \ set (wq s cs)" + by (unfold s_RAG_def, auto simp:cs_waiting_def) + from wq_threads [OF this] show ?thesis . +qed + + +lemma cp_le: + assumes th_in: "th \ threads s" + shows "cp s th \ Max ((\ th. (preced th s)) ` threads s)" +proof(unfold cp_eq_cpreced cpreced_def cs_dependants_def) + show "Max ((\th. preced th s) ` ({th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+})) + \ Max ((\th. preced th s) ` threads s)" + (is "Max (?f ` ?A) \ Max (?f ` ?B)") + proof(rule Max_f_mono) + show "{th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ {}" by simp + next + from finite_threads + show "finite (threads s)" . + next + from th_in + show "{th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ threads s" + apply (auto simp:Domain_def) + apply (rule_tac dm_RAG_threads) + apply (unfold trancl_domain [of "RAG s", symmetric]) + by (unfold cs_RAG_def s_RAG_def, auto simp:Domain_def) + qed +qed + +lemma le_cp: + shows "preced th s \ cp s th" +proof(unfold cp_eq_cpreced preced_def cpreced_def, simp) + show "Prc (priority th s) (last_set th s) + \ Max (insert (Prc (priority th s) (last_set th s)) + ((\th. Prc (priority th s) (last_set th s)) ` dependants (wq s) th))" + (is "?l \ Max (insert ?l ?A)") + proof(cases "?A = {}") + case False + have "finite ?A" (is "finite (?f ` ?B)") + proof - + have "finite ?B" + proof- + have "finite {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+}" + proof - + let ?F = "\ (x, y). the_th x" + have "{th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ ?F ` ((RAG (wq s))\<^sup>+)" + apply (auto simp:image_def) + by (rule_tac x = "(Th x, Th th)" in bexI, auto) + moreover have "finite \" + proof - + from finite_RAG have "finite (RAG s)" . + hence "finite ((RAG (wq s))\<^sup>+)" + apply (unfold finite_trancl) + by (auto simp: s_RAG_def cs_RAG_def wq_def) + thus ?thesis by auto + qed + ultimately show ?thesis by (auto intro:finite_subset) + qed + thus ?thesis by (simp add:cs_dependants_def) + qed + thus ?thesis by simp + qed + from Max_insert [OF this False, of ?l] show ?thesis by auto + next + case True + thus ?thesis by auto + qed +qed + +lemma max_cp_eq: + shows "Max ((cp s) ` threads s) = Max ((\ th. (preced th s)) ` threads s)" + (is "?l = ?r") +proof(cases "threads s = {}") + case True + thus ?thesis by auto +next + case False + have "?l \ ((cp s) ` threads s)" + proof(rule Max_in) + from finite_threads + show "finite (cp s ` threads s)" by auto + next + from False show "cp s ` threads s \ {}" by auto + qed + then obtain th + where th_in: "th \ threads s" and eq_l: "?l = cp s th" by auto + have "\ \ ?r" by (rule cp_le[OF th_in]) + moreover have "?r \ cp s th" (is "Max (?f ` ?A) \ cp s th") + proof - + have "?r \ (?f ` ?A)" + proof(rule Max_in) + from finite_threads + show " finite ((\th. preced th s) ` threads s)" by auto + next + from False show " (\th. preced th s) ` threads s \ {}" by auto + qed + then obtain th' where + th_in': "th' \ ?A " and eq_r: "?r = ?f th'" by auto + from le_cp [of th'] eq_r + have "?r \ cp s th'" by auto + moreover have "\ \ cp s th" + proof(fold eq_l) + show " cp s th' \ Max (cp s ` threads s)" + proof(rule Max_ge) + from th_in' show "cp s th' \ cp s ` threads s" + by auto + next + from finite_threads + show "finite (cp s ` threads s)" by auto + qed + qed + ultimately show ?thesis by auto + qed + ultimately show ?thesis using eq_l by auto +qed + +lemma max_cp_eq_the_preced: + shows "Max ((cp s) ` threads s) = Max (the_preced s ` threads s)" + using max_cp_eq using the_preced_def by presburger + +end + +lemma preced_v [simp]: "preced th' (V th cs#s) = preced th' s" + by (unfold preced_def, simp) + +lemma the_preced_v[simp]: "the_preced (V th cs#s) = the_preced s" +proof + fix th' + show "the_preced (V th cs # s) th' = the_preced s th'" + by (unfold the_preced_def preced_def, simp) +qed + +lemma step_RAG_v: +assumes vt: + "vt (V th cs#s)" +shows " + RAG (V th cs # s) = + RAG s - {(Cs cs, Th th)} - + {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'}" (is "?L = ?R") +proof - + interpret vt_v: valid_trace_v s "V th cs" + using assms step_back_vt by (unfold_locales, auto) + show ?thesis using vt_v.RAG_es . +qed + + + + + +text {* (* ddd *) + The following @{text "step_RAG_v"} lemma charaterizes how @{text "RAG"} is changed + with the happening of @{text "V"}-events: +*} +lemma step_RAG_v: +assumes vt: + "vt (V th cs#s)" +shows " + RAG (V th cs # s) = + RAG s - {(Cs cs, Th th)} - + {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'}" + apply (insert vt, unfold s_RAG_def) + apply (auto split:if_splits list.splits simp:Let_def) + apply (auto elim: step_v_waiting_mono step_v_hold_inv + step_v_release step_v_wait_inv + step_v_get_hold step_v_release_inv) + apply (erule_tac step_v_not_wait, auto) + done + +text {* + The following @{text "step_RAG_p"} lemma charaterizes how @{text "RAG"} is changed + with the happening of @{text "P"}-events: +*} +lemma step_RAG_p: + "vt (P th cs#s) \ + RAG (P th cs # s) = (if (wq s cs = []) then RAG s \ {(Cs cs, Th th)} + else RAG s \ {(Th th, Cs cs)})" + apply(simp only: s_RAG_def wq_def) + apply (auto split:list.splits prod.splits simp:Let_def wq_def cs_waiting_def cs_holding_def) + apply(case_tac "csa = cs", auto) + apply(fold wq_def) + apply(drule_tac step_back_step) + apply(ind_cases " step s (P (hd (wq s cs)) cs)") + apply(simp add:s_RAG_def wq_def cs_holding_def) + apply(auto) + done + + +lemma RAG_target_th: "(Th th, x) \ RAG (s::state) \ \ cs. x = Cs cs" + by (unfold s_RAG_def, auto) + +context valid_trace +begin + +text {* + The following lemma shows that @{text "RAG"} is acyclic. + The overall structure is by induction on the formation of @{text "vt s"} + and then case analysis on event @{text "e"}, where the non-trivial cases + for those for @{text "V"} and @{text "P"} events. +*} +lemma acyclic_RAG: + shows "acyclic (RAG s)" +using vt +proof(induct) + case (vt_cons s e) + interpret vt_s: valid_trace s using vt_cons(1) + by (unfold_locales, simp) + assume ih: "acyclic (RAG s)" + and stp: "step s e" + and vt: "vt s" + show ?case + proof(cases e) + case (Create th prio) + with ih + show ?thesis by (simp add:RAG_create_unchanged) + next + case (Exit th) + with ih show ?thesis by (simp add:RAG_exit_unchanged) + next + case (V th cs) + from V vt stp have vtt: "vt (V th cs#s)" by auto + from step_RAG_v [OF this] + have eq_de: + "RAG (e # s) = + RAG s - {(Cs cs, Th th)} - {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'}" + (is "?L = (?A - ?B - ?C) \ ?D") by (simp add:V) + from ih have ac: "acyclic (?A - ?B - ?C)" by (auto elim:acyclic_subset) + from step_back_step [OF vtt] + have "step s (V th cs)" . + thus ?thesis + proof(cases) + assume "holding s th cs" + hence th_in: "th \ set (wq s cs)" and + eq_hd: "th = hd (wq s cs)" unfolding s_holding_def wq_def by auto + then obtain rest where + eq_wq: "wq s cs = th#rest" + by (cases "wq s cs", auto) + show ?thesis + proof(cases "rest = []") + case False + let ?th' = "hd (SOME q. distinct q \ set q = set rest)" + from eq_wq False have eq_D: "?D = {(Cs cs, Th ?th')}" + by (unfold next_th_def, auto) + let ?E = "(?A - ?B - ?C)" + have "(Th ?th', Cs cs) \ ?E\<^sup>*" + proof + assume "(Th ?th', Cs cs) \ ?E\<^sup>*" + hence " (Th ?th', Cs cs) \ ?E\<^sup>+" by (simp add: rtrancl_eq_or_trancl) + from tranclD [OF this] + obtain x where th'_e: "(Th ?th', x) \ ?E" by blast + hence th_d: "(Th ?th', x) \ ?A" by simp + from RAG_target_th [OF this] + obtain cs' where eq_x: "x = Cs cs'" by auto + with th_d have "(Th ?th', Cs cs') \ ?A" by simp + hence wt_th': "waiting s ?th' cs'" + unfolding s_RAG_def s_waiting_def cs_waiting_def wq_def by simp + hence "cs' = cs" + proof(rule vt_s.waiting_unique) + from eq_wq vt_s.wq_distinct[of cs] + show "waiting s ?th' cs" + apply (unfold s_waiting_def wq_def, auto) + proof - + assume hd_in: "hd (SOME q. distinct q \ set q = set rest) \ set rest" + and eq_wq: "wq_fun (schs s) cs = th # rest" + have "(SOME q. distinct q \ set q = set rest) \ []" + proof(rule someI2) + from vt_s.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" unfolding wq_def by auto + next + fix x assume "distinct x \ set x = set rest" + with False show "x \ []" by auto + qed + hence "hd (SOME q. distinct q \ set q = set rest) \ + set (SOME q. distinct q \ set q = set rest)" by auto + moreover have "\ = set rest" + proof(rule someI2) + from vt_s.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" unfolding wq_def by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" by auto + qed + moreover note hd_in + ultimately show "hd (SOME q. distinct q \ set q = set rest) = th" by auto + next + assume hd_in: "hd (SOME q. distinct q \ set q = set rest) \ set rest" + and eq_wq: "wq s cs = hd (SOME q. distinct q \ set q = set rest) # rest" + have "(SOME q. distinct q \ set q = set rest) \ []" + proof(rule someI2) + from vt_s.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" by auto + next + fix x assume "distinct x \ set x = set rest" + with False show "x \ []" by auto + qed + hence "hd (SOME q. distinct q \ set q = set rest) \ + set (SOME q. distinct q \ set q = set rest)" by auto + moreover have "\ = set rest" + proof(rule someI2) + from vt_s.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" by auto + qed + moreover note hd_in + ultimately show False by auto + qed + qed + with th'_e eq_x have "(Th ?th', Cs cs) \ ?E" by simp + with False + show "False" by (auto simp: next_th_def eq_wq) + qed + with acyclic_insert[symmetric] and ac + and eq_de eq_D show ?thesis by auto + next + case True + with eq_wq + have eq_D: "?D = {}" + by (unfold next_th_def, auto) + with eq_de ac + show ?thesis by auto + qed + qed + next + case (P th cs) + from P vt stp have vtt: "vt (P th cs#s)" by auto + from step_RAG_p [OF this] P + have "RAG (e # s) = + (if wq s cs = [] then RAG s \ {(Cs cs, Th th)} else + RAG s \ {(Th th, Cs cs)})" (is "?L = ?R") + by simp + moreover have "acyclic ?R" + proof(cases "wq s cs = []") + case True + hence eq_r: "?R = RAG s \ {(Cs cs, Th th)}" by simp + have "(Th th, Cs cs) \ (RAG s)\<^sup>*" + proof + assume "(Th th, Cs cs) \ (RAG s)\<^sup>*" + hence "(Th th, Cs cs) \ (RAG s)\<^sup>+" by (simp add: rtrancl_eq_or_trancl) + from tranclD2 [OF this] + obtain x where "(x, Cs cs) \ RAG s" by auto + with True show False by (auto simp:s_RAG_def cs_waiting_def) + qed + with acyclic_insert ih eq_r show ?thesis by auto + next + case False + hence eq_r: "?R = RAG s \ {(Th th, Cs cs)}" by simp + have "(Cs cs, Th th) \ (RAG s)\<^sup>*" + proof + assume "(Cs cs, Th th) \ (RAG s)\<^sup>*" + hence "(Cs cs, Th th) \ (RAG s)\<^sup>+" by (simp add: rtrancl_eq_or_trancl) + moreover from step_back_step [OF vtt] have "step s (P th cs)" . + ultimately show False + proof - + show " \(Cs cs, Th th) \ (RAG s)\<^sup>+; step s (P th cs)\ \ False" + by (ind_cases "step s (P th cs)", simp) + qed + qed + with acyclic_insert ih eq_r show ?thesis by auto + qed + ultimately show ?thesis by simp + next + case (Set thread prio) + with ih + thm RAG_set_unchanged + show ?thesis by (simp add:RAG_set_unchanged) + qed + next + case vt_nil + show "acyclic (RAG ([]::state))" + by (auto simp: s_RAG_def cs_waiting_def + cs_holding_def wq_def acyclic_def) +qed + + +lemma finite_RAG: + shows "finite (RAG s)" +proof - + from vt show ?thesis + proof(induct) + case (vt_cons s e) + interpret vt_s: valid_trace s using vt_cons(1) + by (unfold_locales, simp) + assume ih: "finite (RAG s)" + and stp: "step s e" + and vt: "vt s" + show ?case + proof(cases e) + case (Create th prio) + with ih + show ?thesis by (simp add:RAG_create_unchanged) + next + case (Exit th) + with ih show ?thesis by (simp add:RAG_exit_unchanged) + next + case (V th cs) + from V vt stp have vtt: "vt (V th cs#s)" by auto + from step_RAG_v [OF this] + have eq_de: "RAG (e # s) = + RAG s - {(Cs cs, Th th)} - {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'} +" + (is "?L = (?A - ?B - ?C) \ ?D") by (simp add:V) + moreover from ih have ac: "finite (?A - ?B - ?C)" by simp + moreover have "finite ?D" + proof - + have "?D = {} \ (\ a. ?D = {a})" + by (unfold next_th_def, auto) + thus ?thesis + proof + assume h: "?D = {}" + show ?thesis by (unfold h, simp) + next + assume "\ a. ?D = {a}" + thus ?thesis + by (metis finite.simps) + qed + qed + ultimately show ?thesis by simp + next + case (P th cs) + from P vt stp have vtt: "vt (P th cs#s)" by auto + from step_RAG_p [OF this] P + have "RAG (e # s) = + (if wq s cs = [] then RAG s \ {(Cs cs, Th th)} else + RAG s \ {(Th th, Cs cs)})" (is "?L = ?R") + by simp + moreover have "finite ?R" + proof(cases "wq s cs = []") + case True + hence eq_r: "?R = RAG s \ {(Cs cs, Th th)}" by simp + with True and ih show ?thesis by auto + next + case False + hence "?R = RAG s \ {(Th th, Cs cs)}" by simp + with False and ih show ?thesis by auto + qed + ultimately show ?thesis by auto + next + case (Set thread prio) + with ih + show ?thesis by (simp add:RAG_set_unchanged) + qed + next + case vt_nil + show "finite (RAG ([]::state))" + by (auto simp: s_RAG_def cs_waiting_def + cs_holding_def wq_def acyclic_def) + qed +qed + +text {* Several useful lemmas *} + +lemma wf_dep_converse: + shows "wf ((RAG s)^-1)" +proof(rule finite_acyclic_wf_converse) + from finite_RAG + show "finite (RAG s)" . +next + from acyclic_RAG + show "acyclic (RAG s)" . +qed + +end + +lemma hd_np_in: "x \ set l \ hd l \ set l" + by (induct l, auto) + +lemma th_chasing: "(Th th, Cs cs) \ RAG (s::state) \ \ th'. (Cs cs, Th th') \ RAG s" + by (auto simp:s_RAG_def s_holding_def cs_holding_def cs_waiting_def wq_def dest:hd_np_in) + +context valid_trace +begin + +lemma wq_threads: + assumes h: "th \ set (wq s cs)" + shows "th \ threads s" +proof - + from vt and h show ?thesis + proof(induct arbitrary: th cs) + case (vt_cons s e) + interpret vt_s: valid_trace s + using vt_cons(1) by (unfold_locales, auto) + assume ih: "\th cs. th \ set (wq s cs) \ th \ threads s" + and stp: "step s e" + and vt: "vt s" + and h: "th \ set (wq (e # s) cs)" + show ?case + proof(cases e) + case (Create th' prio) + with ih h show ?thesis + by (auto simp:wq_def Let_def) + next + case (Exit th') + with stp ih h show ?thesis + apply (auto simp:wq_def Let_def) + apply (ind_cases "step s (Exit th')") + apply (auto simp:runing_def readys_def s_holding_def s_waiting_def holdents_def + s_RAG_def s_holding_def cs_holding_def) + done + next + case (V th' cs') + show ?thesis + proof(cases "cs' = cs") + case False + with h + show ?thesis + apply(unfold wq_def V, auto simp:Let_def V split:prod.splits, fold wq_def) + by (drule_tac ih, simp) + next + case True + from h + show ?thesis + proof(unfold V wq_def) + assume th_in: "th \ set (wq_fun (schs (V th' cs' # s)) cs)" (is "th \ set ?l") + show "th \ threads (V th' cs' # s)" + proof(cases "cs = cs'") + case False + hence "?l = wq_fun (schs s) cs" by (simp add:Let_def) + with th_in have " th \ set (wq s cs)" + by (fold wq_def, simp) + from ih [OF this] show ?thesis by simp + next + case True + show ?thesis + proof(cases "wq_fun (schs s) cs'") + case Nil + with h V show ?thesis + apply (auto simp:wq_def Let_def split:if_splits) + by (fold wq_def, drule_tac ih, simp) + next + case (Cons a rest) + assume eq_wq: "wq_fun (schs s) cs' = a # rest" + with h V show ?thesis + apply (auto simp:Let_def wq_def split:if_splits) + proof - + assume th_in: "th \ set (SOME q. distinct q \ set q = set rest)" + have "set (SOME q. distinct q \ set q = set rest) = set rest" + proof(rule someI2) + from vt_s.wq_distinct[of cs'] and eq_wq[folded wq_def] + show "distinct rest \ set rest = set rest" by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" + by auto + qed + with eq_wq th_in have "th \ set (wq_fun (schs s) cs')" by auto + from ih[OF this[folded wq_def]] show "th \ threads s" . + next + assume th_in: "th \ set (wq_fun (schs s) cs)" + from ih[OF this[folded wq_def]] + show "th \ threads s" . + qed + qed + qed + qed + qed + next + case (P th' cs') + from h stp + show ?thesis + apply (unfold P wq_def) + apply (auto simp:Let_def split:if_splits, fold wq_def) + apply (auto intro:ih) + apply(ind_cases "step s (P th' cs')") + by (unfold runing_def readys_def, auto) + next + case (Set thread prio) + with ih h show ?thesis + by (auto simp:wq_def Let_def) + qed + next + case vt_nil + thus ?case by (auto simp:wq_def) + qed +qed + +lemma range_in: "\(Th th) \ Range (RAG (s::state))\ \ th \ threads s" + apply(unfold s_RAG_def cs_waiting_def cs_holding_def) + by (auto intro:wq_threads) + +lemma readys_v_eq: + assumes neq_th: "th \ thread" + and eq_wq: "wq s cs = thread#rest" + and not_in: "th \ set rest" + shows "(th \ readys (V thread cs#s)) = (th \ readys s)" +proof - + from assms show ?thesis + apply (auto simp:readys_def) + apply(simp add:s_waiting_def[folded wq_def]) + apply (erule_tac x = csa in allE) + apply (simp add:s_waiting_def wq_def Let_def split:if_splits) + apply (case_tac "csa = cs", simp) + apply (erule_tac x = cs in allE) + apply(auto simp add: s_waiting_def[folded wq_def] Let_def split: list.splits) + apply(auto simp add: wq_def) + apply (auto simp:s_waiting_def wq_def Let_def split:list.splits) + proof - + assume th_nin: "th \ set rest" + and th_in: "th \ set (SOME q. distinct q \ set q = set rest)" + and eq_wq: "wq_fun (schs s) cs = thread # rest" + have "set (SOME q. distinct q \ set q = set rest) = set rest" + proof(rule someI2) + from wq_distinct[of cs, unfolded wq_def] and eq_wq[unfolded wq_def] + show "distinct rest \ set rest = set rest" by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" by auto + qed + with th_nin th_in show False by auto + qed +qed + +text {* \noindent + The following lemmas shows that: starting from any node in @{text "RAG"}, + by chasing out-going edges, it is always possible to reach a node representing a ready + thread. In this lemma, it is the @{text "th'"}. +*} + +lemma chain_building: + shows "node \ Domain (RAG s) \ (\ th'. th' \ readys s \ (node, Th th') \ (RAG s)^+)" +proof - + from wf_dep_converse + have h: "wf ((RAG s)\)" . + show ?thesis + proof(induct rule:wf_induct [OF h]) + fix x + assume ih [rule_format]: + "\y. (y, x) \ (RAG s)\ \ + y \ Domain (RAG s) \ (\th'. th' \ readys s \ (y, Th th') \ (RAG s)\<^sup>+)" + show "x \ Domain (RAG s) \ (\th'. th' \ readys s \ (x, Th th') \ (RAG s)\<^sup>+)" + proof + assume x_d: "x \ Domain (RAG s)" + show "\th'. th' \ readys s \ (x, Th th') \ (RAG s)\<^sup>+" + proof(cases x) + case (Th th) + from x_d Th obtain cs where x_in: "(Th th, Cs cs) \ RAG s" by (auto simp:s_RAG_def) + with Th have x_in_r: "(Cs cs, x) \ (RAG s)^-1" by simp + from th_chasing [OF x_in] obtain th' where "(Cs cs, Th th') \ RAG s" by blast + hence "Cs cs \ Domain (RAG s)" by auto + from ih [OF x_in_r this] obtain th' + where th'_ready: " th' \ readys s" and cs_in: "(Cs cs, Th th') \ (RAG s)\<^sup>+" by auto + have "(x, Th th') \ (RAG s)\<^sup>+" using Th x_in cs_in by auto + with th'_ready show ?thesis by auto + next + case (Cs cs) + from x_d Cs obtain th' where th'_d: "(Th th', x) \ (RAG s)^-1" by (auto simp:s_RAG_def) + show ?thesis + proof(cases "th' \ readys s") + case True + from True and th'_d show ?thesis by auto + next + case False + from th'_d and range_in have "th' \ threads s" by auto + with False have "Th th' \ Domain (RAG s)" + by (auto simp:readys_def wq_def s_waiting_def s_RAG_def cs_waiting_def Domain_def) + from ih [OF th'_d this] + obtain th'' where + th''_r: "th'' \ readys s" and + th''_in: "(Th th', Th th'') \ (RAG s)\<^sup>+" by auto + from th'_d and th''_in + have "(x, Th th'') \ (RAG s)\<^sup>+" by auto + with th''_r show ?thesis by auto + qed + qed + qed + qed +qed + +text {* \noindent + The following is just an instance of @{text "chain_building"}. +*} +lemma th_chain_to_ready: + assumes th_in: "th \ threads s" + shows "th \ readys s \ (\ th'. th' \ readys s \ (Th th, Th th') \ (RAG s)^+)" +proof(cases "th \ readys s") + case True + thus ?thesis by auto +next + case False + from False and th_in have "Th th \ Domain (RAG s)" + by (auto simp:readys_def s_waiting_def s_RAG_def wq_def cs_waiting_def Domain_def) + from chain_building [rule_format, OF this] + show ?thesis by auto +qed + +end + + + +lemma holding_unique: "\holding (s::state) th1 cs; holding s th2 cs\ \ th1 = th2" + by (unfold s_holding_def cs_holding_def, auto) + +context valid_trace +begin + +lemma unique_RAG: "\(n, n1) \ RAG s; (n, n2) \ RAG s\ \ n1 = n2" + apply(unfold s_RAG_def, auto, fold waiting_eq holding_eq) + by(auto elim:waiting_unique holding_unique) + +end + + +lemma trancl_split: "(a, b) \ r^+ \ \ c. (a, c) \ r" +by (induct rule:trancl_induct, auto) + +context valid_trace +begin + +lemma dchain_unique: + assumes th1_d: "(n, Th th1) \ (RAG s)^+" + and th1_r: "th1 \ readys s" + and th2_d: "(n, Th th2) \ (RAG s)^+" + and th2_r: "th2 \ readys s" + shows "th1 = th2" +proof - + { assume neq: "th1 \ th2" + hence "Th th1 \ Th th2" by simp + from unique_chain [OF _ th1_d th2_d this] and unique_RAG + have "(Th th1, Th th2) \ (RAG s)\<^sup>+ \ (Th th2, Th th1) \ (RAG s)\<^sup>+" by auto + hence "False" + proof + assume "(Th th1, Th th2) \ (RAG s)\<^sup>+" + from trancl_split [OF this] + obtain n where dd: "(Th th1, n) \ RAG s" by auto + then obtain cs where eq_n: "n = Cs cs" + by (auto simp:s_RAG_def s_holding_def cs_holding_def cs_waiting_def wq_def dest:hd_np_in) + from dd eq_n have "th1 \ readys s" + by (auto simp:readys_def s_RAG_def wq_def s_waiting_def cs_waiting_def) + with th1_r show ?thesis by auto + next + assume "(Th th2, Th th1) \ (RAG s)\<^sup>+" + from trancl_split [OF this] + obtain n where dd: "(Th th2, n) \ RAG s" by auto + then obtain cs where eq_n: "n = Cs cs" + by (auto simp:s_RAG_def s_holding_def cs_holding_def cs_waiting_def wq_def dest:hd_np_in) + from dd eq_n have "th2 \ readys s" + by (auto simp:readys_def wq_def s_RAG_def s_waiting_def cs_waiting_def) + with th2_r show ?thesis by auto + qed + } thus ?thesis by auto +qed + +end + + +lemma step_holdents_p_add: + assumes vt: "vt (P th cs#s)" + and "wq s cs = []" + shows "holdents (P th cs#s) th = holdents s th \ {cs}" +proof - + from assms show ?thesis + unfolding holdents_test step_RAG_p[OF vt] by (auto) +qed + +lemma step_holdents_p_eq: + assumes vt: "vt (P th cs#s)" + and "wq s cs \ []" + shows "holdents (P th cs#s) th = holdents s th" +proof - + from assms show ?thesis + unfolding holdents_test step_RAG_p[OF vt] by auto +qed + + +lemma (in valid_trace) finite_holding : + shows "finite (holdents s th)" +proof - + let ?F = "\ (x, y). the_cs x" + from finite_RAG + have "finite (RAG s)" . + hence "finite (?F `(RAG s))" by simp + moreover have "{cs . (Cs cs, Th th) \ RAG s} \ \" + proof - + { have h: "\ a A f. a \ A \ f a \ f ` A" by auto + fix x assume "(Cs x, Th th) \ RAG s" + hence "?F (Cs x, Th th) \ ?F `(RAG s)" by (rule h) + moreover have "?F (Cs x, Th th) = x" by simp + ultimately have "x \ (\(x, y). the_cs x) ` RAG s" by simp + } thus ?thesis by auto + qed + ultimately show ?thesis by (unfold holdents_test, auto intro:finite_subset) +qed + +lemma cntCS_v_dec: + assumes vtv: "vt (V thread cs#s)" + shows "(cntCS (V thread cs#s) thread + 1) = cntCS s thread" +proof - + from vtv interpret vt_s: valid_trace s + by (cases, unfold_locales, simp) + from vtv interpret vt_v: valid_trace "V thread cs#s" + by (unfold_locales, simp) + from step_back_step[OF vtv] + have cs_in: "cs \ holdents s thread" + apply (cases, unfold holdents_test s_RAG_def, simp) + by (unfold cs_holding_def s_holding_def wq_def, auto) + moreover have cs_not_in: + "(holdents (V thread cs#s) thread) = holdents s thread - {cs}" + apply (insert vt_s.wq_distinct[of cs]) + apply (unfold holdents_test, unfold step_RAG_v[OF vtv], + auto simp:next_th_def) + proof - + fix rest + assume dst: "distinct (rest::thread list)" + and ne: "rest \ []" + and hd_ni: "hd (SOME q. distinct q \ set q = set rest) \ set rest" + moreover have "set (SOME q. distinct q \ set q = set rest) = set rest" + proof(rule someI2) + from dst show "distinct rest \ set rest = set rest" by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" by auto + qed + ultimately have "hd (SOME q. distinct q \ set q = set rest) \ + set (SOME q. distinct q \ set q = set rest)" by simp + moreover have "(SOME q. distinct q \ set q = set rest) \ []" + proof(rule someI2) + from dst show "distinct rest \ set rest = set rest" by auto + next + fix x assume " distinct x \ set x = set rest" with ne + show "x \ []" by auto + qed + ultimately + show "(Cs cs, Th (hd (SOME q. distinct q \ set q = set rest))) \ RAG s" + by auto + next + fix rest + assume dst: "distinct (rest::thread list)" + and ne: "rest \ []" + and hd_ni: "hd (SOME q. distinct q \ set q = set rest) \ set rest" + moreover have "set (SOME q. distinct q \ set q = set rest) = set rest" + proof(rule someI2) + from dst show "distinct rest \ set rest = set rest" by auto + next + show "\x. distinct x \ set x = set rest \ set x = set rest" by auto + qed + ultimately have "hd (SOME q. distinct q \ set q = set rest) \ + set (SOME q. distinct q \ set q = set rest)" by simp + moreover have "(SOME q. distinct q \ set q = set rest) \ []" + proof(rule someI2) + from dst show "distinct rest \ set rest = set rest" by auto + next + fix x assume " distinct x \ set x = set rest" with ne + show "x \ []" by auto + qed + ultimately show "False" by auto + qed + ultimately + have "holdents s thread = insert cs (holdents (V thread cs#s) thread)" + by auto + moreover have "card \ = + Suc (card ((holdents (V thread cs#s) thread) - {cs}))" + proof(rule card_insert) + from vt_v.finite_holding + show " finite (holdents (V thread cs # s) thread)" . + qed + moreover from cs_not_in + have "cs \ (holdents (V thread cs#s) thread)" by auto + ultimately show ?thesis by (simp add:cntCS_def) +qed + +lemma count_rec1 [simp]: + assumes "Q e" + shows "count Q (e#es) = Suc (count Q es)" + using assms + by (unfold count_def, auto) + +lemma count_rec2 [simp]: + assumes "\Q e" + shows "count Q (e#es) = (count Q es)" + using assms + by (unfold count_def, auto) + +lemma count_rec3 [simp]: + shows "count Q [] = 0" + by (unfold count_def, auto) + +lemma cntP_diff_inv: + assumes "cntP (e#s) th \ cntP s th" + shows "isP e \ actor e = th" +proof(cases e) + case (P th' pty) + show ?thesis + by (cases "(\e. \cs. e = P th cs) (P th' pty)", + insert assms P, auto simp:cntP_def) +qed (insert assms, auto simp:cntP_def) + +lemma cntV_diff_inv: + assumes "cntV (e#s) th \ cntV s th" + shows "isV e \ actor e = th" +proof(cases e) + case (V th' pty) + show ?thesis + by (cases "(\e. \cs. e = V th cs) (V th' pty)", + insert assms V, auto simp:cntV_def) +qed (insert assms, auto simp:cntV_def) + +context valid_trace +begin + +text {* (* ddd *) \noindent + The relationship between @{text "cntP"}, @{text "cntV"} and @{text "cntCS"} + of one particular thread. t +*} + +lemma cnp_cnv_cncs: + shows "cntP s th = cntV s th + (if (th \ readys s \ th \ threads s) + then cntCS s th else cntCS s th + 1)" +proof - + from vt show ?thesis + proof(induct arbitrary:th) + case (vt_cons s e) + interpret vt_s: valid_trace s using vt_cons(1) by (unfold_locales, simp) + assume vt: "vt s" + and ih: "\th. cntP s th = cntV s th + + (if (th \ readys s \ th \ threads s) then cntCS s th else cntCS s th + 1)" + and stp: "step s e" + from stp show ?case + proof(cases) + case (thread_create thread prio) + assume eq_e: "e = Create thread prio" + and not_in: "thread \ threads s" + show ?thesis + proof - + { fix cs + assume "thread \ set (wq s cs)" + from vt_s.wq_threads [OF this] have "thread \ threads s" . + with not_in have "False" by simp + } with eq_e have eq_readys: "readys (e#s) = readys s \ {thread}" + by (auto simp:readys_def threads.simps s_waiting_def + wq_def cs_waiting_def Let_def) + from eq_e have eq_cnp: "cntP (e#s) th = cntP s th" by (simp add:cntP_def count_def) + from eq_e have eq_cnv: "cntV (e#s) th = cntV s th" by (simp add:cntV_def count_def) + have eq_cncs: "cntCS (e#s) th = cntCS s th" + unfolding cntCS_def holdents_test + by (simp add:RAG_create_unchanged eq_e) + { assume "th \ thread" + with eq_readys eq_e + have "(th \ readys (e # s) \ th \ threads (e # s)) = + (th \ readys (s) \ th \ threads (s))" + by (simp add:threads.simps) + with eq_cnp eq_cnv eq_cncs ih not_in + have ?thesis by simp + } moreover { + assume eq_th: "th = thread" + with not_in ih have " cntP s th = cntV s th + cntCS s th" by simp + moreover from eq_th and eq_readys have "th \ readys (e#s)" by simp + moreover note eq_cnp eq_cnv eq_cncs + ultimately have ?thesis by auto + } ultimately show ?thesis by blast + qed + next + case (thread_exit thread) + assume eq_e: "e = Exit thread" + and is_runing: "thread \ runing s" + and no_hold: "holdents s thread = {}" + from eq_e have eq_cnp: "cntP (e#s) th = cntP s th" by (simp add:cntP_def count_def) + from eq_e have eq_cnv: "cntV (e#s) th = cntV s th" by (simp add:cntV_def count_def) + have eq_cncs: "cntCS (e#s) th = cntCS s th" + unfolding cntCS_def holdents_test + by (simp add:RAG_exit_unchanged eq_e) + { assume "th \ thread" + with eq_e + have "(th \ readys (e # s) \ th \ threads (e # s)) = + (th \ readys (s) \ th \ threads (s))" + apply (simp add:threads.simps readys_def) + apply (subst s_waiting_def) + apply (simp add:Let_def) + apply (subst s_waiting_def, simp) + done + with eq_cnp eq_cnv eq_cncs ih + have ?thesis by simp + } moreover { + assume eq_th: "th = thread" + with ih is_runing have " cntP s th = cntV s th + cntCS s th" + by (simp add:runing_def) + moreover from eq_th eq_e have "th \ threads (e#s)" + by simp + moreover note eq_cnp eq_cnv eq_cncs + ultimately have ?thesis by auto + } ultimately show ?thesis by blast + next + case (thread_P thread cs) + assume eq_e: "e = P thread cs" + and is_runing: "thread \ runing s" + and no_dep: "(Cs cs, Th thread) \ (RAG s)\<^sup>+" + from thread_P vt stp ih have vtp: "vt (P thread cs#s)" by auto + then interpret vt_p: valid_trace "(P thread cs#s)" + by (unfold_locales, simp) + show ?thesis + proof - + { have hh: "\ A B C. (B = C) \ (A \ B) = (A \ C)" by blast + assume neq_th: "th \ thread" + with eq_e + have eq_readys: "(th \ readys (e#s)) = (th \ readys (s))" + apply (simp add:readys_def s_waiting_def wq_def Let_def) + apply (rule_tac hh) + apply (intro iffI allI, clarify) + apply (erule_tac x = csa in allE, auto) + apply (subgoal_tac "wq_fun (schs s) cs \ []", auto) + apply (erule_tac x = cs in allE, auto) + by (case_tac "(wq_fun (schs s) cs)", auto) + moreover from neq_th eq_e have "cntCS (e # s) th = cntCS s th" + apply (simp add:cntCS_def holdents_test) + by (unfold step_RAG_p [OF vtp], auto) + moreover from eq_e neq_th have "cntP (e # s) th = cntP s th" + by (simp add:cntP_def count_def) + moreover from eq_e neq_th have "cntV (e#s) th = cntV s th" + by (simp add:cntV_def count_def) + moreover from eq_e neq_th have "threads (e#s) = threads s" by simp + moreover note ih [of th] + ultimately have ?thesis by simp + } moreover { + assume eq_th: "th = thread" + have ?thesis + proof - + from eq_e eq_th have eq_cnp: "cntP (e # s) th = 1 + (cntP s th)" + by (simp add:cntP_def count_def) + from eq_e eq_th have eq_cnv: "cntV (e#s) th = cntV s th" + by (simp add:cntV_def count_def) + show ?thesis + proof (cases "wq s cs = []") + case True + with is_runing + have "th \ readys (e#s)" + apply (unfold eq_e wq_def, unfold readys_def s_RAG_def) + apply (simp add: wq_def[symmetric] runing_def eq_th s_waiting_def) + by (auto simp:readys_def wq_def Let_def s_waiting_def wq_def) + moreover have "cntCS (e # s) th = 1 + cntCS s th" + proof - + have "card {csa. csa = cs \ (Cs csa, Th thread) \ RAG s} = + Suc (card {cs. (Cs cs, Th thread) \ RAG s})" (is "card ?L = Suc (card ?R)") + proof - + have "?L = insert cs ?R" by auto + moreover have "card \ = Suc (card (?R - {cs}))" + proof(rule card_insert) + from vt_s.finite_holding [of thread] + show " finite {cs. (Cs cs, Th thread) \ RAG s}" + by (unfold holdents_test, simp) + qed + moreover have "?R - {cs} = ?R" + proof - + have "cs \ ?R" + proof + assume "cs \ {cs. (Cs cs, Th thread) \ RAG s}" + with no_dep show False by auto + qed + thus ?thesis by auto + qed + ultimately show ?thesis by auto + qed + thus ?thesis + apply (unfold eq_e eq_th cntCS_def) + apply (simp add: holdents_test) + by (unfold step_RAG_p [OF vtp], auto simp:True) + qed + moreover from is_runing have "th \ readys s" + by (simp add:runing_def eq_th) + moreover note eq_cnp eq_cnv ih [of th] + ultimately show ?thesis by auto + next + case False + have eq_wq: "wq (e#s) cs = wq s cs @ [th]" + by (unfold eq_th eq_e wq_def, auto simp:Let_def) + have "th \ readys (e#s)" + proof + assume "th \ readys (e#s)" + hence "\cs. \ waiting (e # s) th cs" by (simp add:readys_def) + from this[rule_format, of cs] have " \ waiting (e # s) th cs" . + hence "th \ set (wq (e#s) cs) \ th = hd (wq (e#s) cs)" + by (simp add:s_waiting_def wq_def) + moreover from eq_wq have "th \ set (wq (e#s) cs)" by auto + ultimately have "th = hd (wq (e#s) cs)" by blast + with eq_wq have "th = hd (wq s cs @ [th])" by simp + hence "th = hd (wq s cs)" using False by auto + with False eq_wq vt_p.wq_distinct [of cs] + show False by (fold eq_e, auto) + qed + moreover from is_runing have "th \ threads (e#s)" + by (unfold eq_e, auto simp:runing_def readys_def eq_th) + moreover have "cntCS (e # s) th = cntCS s th" + apply (unfold cntCS_def holdents_test eq_e step_RAG_p[OF vtp]) + by (auto simp:False) + moreover note eq_cnp eq_cnv ih[of th] + moreover from is_runing have "th \ readys s" + by (simp add:runing_def eq_th) + ultimately show ?thesis by auto + qed + qed + } ultimately show ?thesis by blast + qed + next + case (thread_V thread cs) + from assms vt stp ih thread_V have vtv: "vt (V thread cs # s)" by auto + then interpret vt_v: valid_trace "(V thread cs # s)" by (unfold_locales, simp) + assume eq_e: "e = V thread cs" + and is_runing: "thread \ runing s" + and hold: "holding s thread cs" + from hold obtain rest + where eq_wq: "wq s cs = thread # rest" + by (case_tac "wq s cs", auto simp: wq_def s_holding_def) + have eq_threads: "threads (e#s) = threads s" by (simp add: eq_e) + have eq_set: "set (SOME q. distinct q \ set q = set rest) = set rest" + proof(rule someI2) + from vt_v.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" + by (metis distinct.simps(2) vt_s.wq_distinct) + next + show "\x. distinct x \ set x = set rest \ set x = set rest" + by auto + qed + show ?thesis + proof - + { assume eq_th: "th = thread" + from eq_th have eq_cnp: "cntP (e # s) th = cntP s th" + by (unfold eq_e, simp add:cntP_def count_def) + moreover from eq_th have eq_cnv: "cntV (e#s) th = 1 + cntV s th" + by (unfold eq_e, simp add:cntV_def count_def) + moreover from cntCS_v_dec [OF vtv] + have "cntCS (e # s) thread + 1 = cntCS s thread" + by (simp add:eq_e) + moreover from is_runing have rd_before: "thread \ readys s" + by (unfold runing_def, simp) + moreover have "thread \ readys (e # s)" + proof - + from is_runing + have "thread \ threads (e#s)" + by (unfold eq_e, auto simp:runing_def readys_def) + moreover have "\ cs1. \ waiting (e#s) thread cs1" + proof + fix cs1 + { assume eq_cs: "cs1 = cs" + have "\ waiting (e # s) thread cs1" + proof - + from eq_wq + have "thread \ set (wq (e#s) cs1)" + apply(unfold eq_e wq_def eq_cs s_holding_def) + apply (auto simp:Let_def) + proof - + assume "thread \ set (SOME q. distinct q \ set q = set rest)" + with eq_set have "thread \ set rest" by simp + with vt_v.wq_distinct[of cs] + and eq_wq show False + by (metis distinct.simps(2) vt_s.wq_distinct) + qed + thus ?thesis by (simp add:wq_def s_waiting_def) + qed + } moreover { + assume neq_cs: "cs1 \ cs" + have "\ waiting (e # s) thread cs1" + proof - + from wq_v_neq [OF neq_cs[symmetric]] + have "wq (V thread cs # s) cs1 = wq s cs1" . + moreover have "\ waiting s thread cs1" + proof - + from runing_ready and is_runing + have "thread \ readys s" by auto + thus ?thesis by (simp add:readys_def) + qed + ultimately show ?thesis + by (auto simp:wq_def s_waiting_def eq_e) + qed + } ultimately show "\ waiting (e # s) thread cs1" by blast + qed + ultimately show ?thesis by (simp add:readys_def) + qed + moreover note eq_th ih + ultimately have ?thesis by auto + } moreover { + assume neq_th: "th \ thread" + from neq_th eq_e have eq_cnp: "cntP (e # s) th = cntP s th" + by (simp add:cntP_def count_def) + from neq_th eq_e have eq_cnv: "cntV (e # s) th = cntV s th" + by (simp add:cntV_def count_def) + have ?thesis + proof(cases "th \ set rest") + case False + have "(th \ readys (e # s)) = (th \ readys s)" + apply (insert step_back_vt[OF vtv]) + by (simp add: False eq_e eq_wq neq_th vt_s.readys_v_eq) + moreover have "cntCS (e#s) th = cntCS s th" + apply (insert neq_th, unfold eq_e cntCS_def holdents_test step_RAG_v[OF vtv], auto) + proof - + have "{csa. (Cs csa, Th th) \ RAG s \ csa = cs \ next_th s thread cs th} = + {cs. (Cs cs, Th th) \ RAG s}" + proof - + from False eq_wq + have " next_th s thread cs th \ (Cs cs, Th th) \ RAG s" + apply (unfold next_th_def, auto) + proof - + assume ne: "rest \ []" + and ni: "hd (SOME q. distinct q \ set q = set rest) \ set rest" + and eq_wq: "wq s cs = thread # rest" + from eq_set ni have "hd (SOME q. distinct q \ set q = set rest) \ + set (SOME q. distinct q \ set q = set rest) + " by simp + moreover have "(SOME q. distinct q \ set q = set rest) \ []" + proof(rule someI2) + from vt_s.wq_distinct[ of cs] and eq_wq + show "distinct rest \ set rest = set rest" by auto + next + fix x assume "distinct x \ set x = set rest" + with ne show "x \ []" by auto + qed + ultimately show + "(Cs cs, Th (hd (SOME q. distinct q \ set q = set rest))) \ RAG s" + by auto + qed + thus ?thesis by auto + qed + thus "card {csa. (Cs csa, Th th) \ RAG s \ csa = cs \ next_th s thread cs th} = + card {cs. (Cs cs, Th th) \ RAG s}" by simp + qed + moreover note ih eq_cnp eq_cnv eq_threads + ultimately show ?thesis by auto + next + case True + assume th_in: "th \ set rest" + show ?thesis + proof(cases "next_th s thread cs th") + case False + with eq_wq and th_in have + neq_hd: "th \ hd (SOME q. distinct q \ set q = set rest)" (is "th \ hd ?rest") + by (auto simp:next_th_def) + have "(th \ readys (e # s)) = (th \ readys s)" + proof - + from eq_wq and th_in + have "\ th \ readys s" + apply (auto simp:readys_def s_waiting_def) + apply (rule_tac x = cs in exI, auto) + by (insert vt_s.wq_distinct[of cs], auto simp add: wq_def) + moreover + from eq_wq and th_in and neq_hd + have "\ (th \ readys (e # s))" + apply (auto simp:readys_def s_waiting_def eq_e wq_def Let_def split:list.splits) + by (rule_tac x = cs in exI, auto simp:eq_set) + ultimately show ?thesis by auto + qed + moreover have "cntCS (e#s) th = cntCS s th" + proof - + from eq_wq and th_in and neq_hd + have "(holdents (e # s) th) = (holdents s th)" + apply (unfold eq_e step_RAG_v[OF vtv], + auto simp:next_th_def eq_set s_RAG_def holdents_test wq_def + Let_def cs_holding_def) + by (insert vt_s.wq_distinct[of cs], auto simp:wq_def) + thus ?thesis by (simp add:cntCS_def) + qed + moreover note ih eq_cnp eq_cnv eq_threads + ultimately show ?thesis by auto + next + case True + let ?rest = " (SOME q. distinct q \ set q = set rest)" + let ?t = "hd ?rest" + from True eq_wq th_in neq_th + have "th \ readys (e # s)" + apply (auto simp:eq_e readys_def s_waiting_def wq_def + Let_def next_th_def) + proof - + assume eq_wq: "wq_fun (schs s) cs = thread # rest" + and t_in: "?t \ set rest" + show "?t \ threads s" + proof(rule vt_s.wq_threads) + from eq_wq and t_in + show "?t \ set (wq s cs)" by (auto simp:wq_def) + qed + next + fix csa + assume eq_wq: "wq_fun (schs s) cs = thread # rest" + and t_in: "?t \ set rest" + and neq_cs: "csa \ cs" + and t_in': "?t \ set (wq_fun (schs s) csa)" + show "?t = hd (wq_fun (schs s) csa)" + proof - + { assume neq_hd': "?t \ hd (wq_fun (schs s) csa)" + from vt_s.wq_distinct[of cs] and + eq_wq[folded wq_def] and t_in eq_wq + have "?t \ thread" by auto + with eq_wq and t_in + have w1: "waiting s ?t cs" + by (auto simp:s_waiting_def wq_def) + from t_in' neq_hd' + have w2: "waiting s ?t csa" + by (auto simp:s_waiting_def wq_def) + from vt_s.waiting_unique[OF w1 w2] + and neq_cs have "False" by auto + } thus ?thesis by auto + qed + qed + moreover have "cntP s th = cntV s th + cntCS s th + 1" + proof - + have "th \ readys s" + proof - + from True eq_wq neq_th th_in + show ?thesis + apply (unfold readys_def s_waiting_def, auto) + by (rule_tac x = cs in exI, auto simp add: wq_def) + qed + moreover have "th \ threads s" + proof - + from th_in eq_wq + have "th \ set (wq s cs)" by simp + from vt_s.wq_threads [OF this] + show ?thesis . + qed + ultimately show ?thesis using ih by auto + qed + moreover from True neq_th have "cntCS (e # s) th = 1 + cntCS s th" + apply (unfold cntCS_def holdents_test eq_e step_RAG_v[OF vtv], auto) + proof - + show "card {csa. (Cs csa, Th th) \ RAG s \ csa = cs} = + Suc (card {cs. (Cs cs, Th th) \ RAG s})" + (is "card ?A = Suc (card ?B)") + proof - + have "?A = insert cs ?B" by auto + hence "card ?A = card (insert cs ?B)" by simp + also have "\ = Suc (card ?B)" + proof(rule card_insert_disjoint) + have "?B \ ((\ (x, y). the_cs x) ` RAG s)" + apply (auto simp:image_def) + by (rule_tac x = "(Cs x, Th th)" in bexI, auto) + with vt_s.finite_RAG + show "finite {cs. (Cs cs, Th th) \ RAG s}" by (auto intro:finite_subset) + next + show "cs \ {cs. (Cs cs, Th th) \ RAG s}" + proof + assume "cs \ {cs. (Cs cs, Th th) \ RAG s}" + hence "(Cs cs, Th th) \ RAG s" by simp + with True neq_th eq_wq show False + by (auto simp:next_th_def s_RAG_def cs_holding_def) + qed + qed + finally show ?thesis . + qed + qed + moreover note eq_cnp eq_cnv + ultimately show ?thesis by simp + qed + qed + } ultimately show ?thesis by blast + qed + next + case (thread_set thread prio) + assume eq_e: "e = Set thread prio" + and is_runing: "thread \ runing s" + show ?thesis + proof - + from eq_e have eq_cnp: "cntP (e#s) th = cntP s th" by (simp add:cntP_def count_def) + from eq_e have eq_cnv: "cntV (e#s) th = cntV s th" by (simp add:cntV_def count_def) + have eq_cncs: "cntCS (e#s) th = cntCS s th" + unfolding cntCS_def holdents_test + by (simp add:RAG_set_unchanged eq_e) + from eq_e have eq_readys: "readys (e#s) = readys s" + by (simp add:readys_def cs_waiting_def s_waiting_def wq_def, + auto simp:Let_def) + { assume "th \ thread" + with eq_readys eq_e + have "(th \ readys (e # s) \ th \ threads (e # s)) = + (th \ readys (s) \ th \ threads (s))" + by (simp add:threads.simps) + with eq_cnp eq_cnv eq_cncs ih is_runing + have ?thesis by simp + } moreover { + assume eq_th: "th = thread" + with is_runing ih have " cntP s th = cntV s th + cntCS s th" + by (unfold runing_def, auto) + moreover from eq_th and eq_readys is_runing have "th \ readys (e#s)" + by (simp add:runing_def) + moreover note eq_cnp eq_cnv eq_cncs + ultimately have ?thesis by auto + } ultimately show ?thesis by blast + qed + qed + next + case vt_nil + show ?case + by (unfold cntP_def cntV_def cntCS_def, + auto simp:count_def holdents_test s_RAG_def wq_def cs_holding_def) + qed +qed + +lemma not_thread_cncs: + assumes not_in: "th \ threads s" + shows "cntCS s th = 0" +proof - + from vt not_in show ?thesis + proof(induct arbitrary:th) + case (vt_cons s e th) + interpret vt_s: valid_trace s using vt_cons(1) + by (unfold_locales, simp) + assume vt: "vt s" + and ih: "\th. th \ threads s \ cntCS s th = 0" + and stp: "step s e" + and not_in: "th \ threads (e # s)" + from stp show ?case + proof(cases) + case (thread_create thread prio) + assume eq_e: "e = Create thread prio" + and not_in': "thread \ threads s" + have "cntCS (e # s) th = cntCS s th" + apply (unfold eq_e cntCS_def holdents_test) + by (simp add:RAG_create_unchanged) + moreover have "th \ threads s" + proof - + from not_in eq_e show ?thesis by simp + qed + moreover note ih ultimately show ?thesis by auto + next + case (thread_exit thread) + assume eq_e: "e = Exit thread" + and nh: "holdents s thread = {}" + have eq_cns: "cntCS (e # s) th = cntCS s th" + apply (unfold eq_e cntCS_def holdents_test) + by (simp add:RAG_exit_unchanged) + show ?thesis + proof(cases "th = thread") + case True + have "cntCS s th = 0" by (unfold cntCS_def, auto simp:nh True) + with eq_cns show ?thesis by simp + next + case False + with not_in and eq_e + have "th \ threads s" by simp + from ih[OF this] and eq_cns show ?thesis by simp + qed + next + case (thread_P thread cs) + assume eq_e: "e = P thread cs" + and is_runing: "thread \ runing s" + from assms thread_P ih vt stp thread_P have vtp: "vt (P thread cs#s)" by auto + have neq_th: "th \ thread" + proof - + from not_in eq_e have "th \ threads s" by simp + moreover from is_runing have "thread \ threads s" + by (simp add:runing_def readys_def) + ultimately show ?thesis by auto + qed + hence "cntCS (e # s) th = cntCS s th " + apply (unfold cntCS_def holdents_test eq_e) + by (unfold step_RAG_p[OF vtp], auto) + moreover have "cntCS s th = 0" + proof(rule ih) + from not_in eq_e show "th \ threads s" by simp + qed + ultimately show ?thesis by simp + next + case (thread_V thread cs) + assume eq_e: "e = V thread cs" + and is_runing: "thread \ runing s" + and hold: "holding s thread cs" + have neq_th: "th \ thread" + proof - + from not_in eq_e have "th \ threads s" by simp + moreover from is_runing have "thread \ threads s" + by (simp add:runing_def readys_def) + ultimately show ?thesis by auto + qed + from assms thread_V vt stp ih + have vtv: "vt (V thread cs#s)" by auto + then interpret vt_v: valid_trace "(V thread cs#s)" + by (unfold_locales, simp) + from hold obtain rest + where eq_wq: "wq s cs = thread # rest" + by (case_tac "wq s cs", auto simp: wq_def s_holding_def) + from not_in eq_e eq_wq + have "\ next_th s thread cs th" + apply (auto simp:next_th_def) + proof - + assume ne: "rest \ []" + and ni: "hd (SOME q. distinct q \ set q = set rest) \ threads s" (is "?t \ threads s") + have "?t \ set rest" + proof(rule someI2) + from vt_v.wq_distinct[of cs] and eq_wq + show "distinct rest \ set rest = set rest" + by (metis distinct.simps(2) vt_s.wq_distinct) + next + fix x assume "distinct x \ set x = set rest" with ne + show "hd x \ set rest" by (cases x, auto) + qed + with eq_wq have "?t \ set (wq s cs)" by simp + from vt_s.wq_threads[OF this] and ni + show False + using `hd (SOME q. distinct q \ set q = set rest) \ set (wq s cs)` + ni vt_s.wq_threads by blast + qed + moreover note neq_th eq_wq + ultimately have "cntCS (e # s) th = cntCS s th" + by (unfold eq_e cntCS_def holdents_test step_RAG_v[OF vtv], auto) + moreover have "cntCS s th = 0" + proof(rule ih) + from not_in eq_e show "th \ threads s" by simp + qed + ultimately show ?thesis by simp + next + case (thread_set thread prio) + print_facts + assume eq_e: "e = Set thread prio" + and is_runing: "thread \ runing s" + from not_in and eq_e have "th \ threads s" by auto + from ih [OF this] and eq_e + show ?thesis + apply (unfold eq_e cntCS_def holdents_test) + by (simp add:RAG_set_unchanged) + qed + next + case vt_nil + show ?case + by (unfold cntCS_def, + auto simp:count_def holdents_test s_RAG_def wq_def cs_holding_def) + qed +qed + +end + + +context valid_trace +begin + +lemma dm_RAG_threads: + assumes in_dom: "(Th th) \ Domain (RAG s)" + shows "th \ threads s" +proof - + from in_dom obtain n where "(Th th, n) \ RAG s" by auto + moreover from RAG_target_th[OF this] obtain cs where "n = Cs cs" by auto + ultimately have "(Th th, Cs cs) \ RAG s" by simp + hence "th \ set (wq s cs)" + by (unfold s_RAG_def, auto simp:cs_waiting_def) + from wq_threads [OF this] show ?thesis . +qed + +end + +lemma cp_eq_cpreced: "cp s th = cpreced (wq s) s th" +unfolding cp_def wq_def +apply(induct s rule: schs.induct) +thm cpreced_initial +apply(simp add: Let_def cpreced_initial) +apply(simp add: Let_def) +apply(simp add: Let_def) +apply(simp add: Let_def) +apply(subst (2) schs.simps) +apply(simp add: Let_def) +apply(subst (2) schs.simps) +apply(simp add: Let_def) +done + +context valid_trace +begin + +lemma runing_unique: + assumes runing_1: "th1 \ runing s" + and runing_2: "th2 \ runing s" + shows "th1 = th2" +proof - + from runing_1 and runing_2 have "cp s th1 = cp s th2" + unfolding runing_def + apply(simp) + done + hence eq_max: "Max ((\th. preced th s) ` ({th1} \ dependants (wq s) th1)) = + Max ((\th. preced th s) ` ({th2} \ dependants (wq s) th2))" + (is "Max (?f ` ?A) = Max (?f ` ?B)") + unfolding cp_eq_cpreced + unfolding cpreced_def . + obtain th1' where th1_in: "th1' \ ?A" and eq_f_th1: "?f th1' = Max (?f ` ?A)" + proof - + have h1: "finite (?f ` ?A)" + proof - + have "finite ?A" + proof - + have "finite (dependants (wq s) th1)" + proof- + have "finite {th'. (Th th', Th th1) \ (RAG (wq s))\<^sup>+}" + proof - + let ?F = "\ (x, y). the_th x" + have "{th'. (Th th', Th th1) \ (RAG (wq s))\<^sup>+} \ ?F ` ((RAG (wq s))\<^sup>+)" + apply (auto simp:image_def) + by (rule_tac x = "(Th x, Th th1)" in bexI, auto) + moreover have "finite \" + proof - + from finite_RAG have "finite (RAG s)" . + hence "finite ((RAG (wq s))\<^sup>+)" + apply (unfold finite_trancl) + by (auto simp: s_RAG_def cs_RAG_def wq_def) + thus ?thesis by auto + qed + ultimately show ?thesis by (auto intro:finite_subset) + qed + thus ?thesis by (simp add:cs_dependants_def) + qed + thus ?thesis by simp + qed + thus ?thesis by auto + qed + moreover have h2: "(?f ` ?A) \ {}" + proof - + have "?A \ {}" by simp + thus ?thesis by simp + qed + from Max_in [OF h1 h2] + have "Max (?f ` ?A) \ (?f ` ?A)" . + thus ?thesis + thm cpreced_def + unfolding cpreced_def[symmetric] + unfolding cp_eq_cpreced[symmetric] + unfolding cpreced_def + using that[intro] by (auto) + qed + obtain th2' where th2_in: "th2' \ ?B" and eq_f_th2: "?f th2' = Max (?f ` ?B)" + proof - + have h1: "finite (?f ` ?B)" + proof - + have "finite ?B" + proof - + have "finite (dependants (wq s) th2)" + proof- + have "finite {th'. (Th th', Th th2) \ (RAG (wq s))\<^sup>+}" + proof - + let ?F = "\ (x, y). the_th x" + have "{th'. (Th th', Th th2) \ (RAG (wq s))\<^sup>+} \ ?F ` ((RAG (wq s))\<^sup>+)" + apply (auto simp:image_def) + by (rule_tac x = "(Th x, Th th2)" in bexI, auto) + moreover have "finite \" + proof - + from finite_RAG have "finite (RAG s)" . + hence "finite ((RAG (wq s))\<^sup>+)" + apply (unfold finite_trancl) + by (auto simp: s_RAG_def cs_RAG_def wq_def) + thus ?thesis by auto + qed + ultimately show ?thesis by (auto intro:finite_subset) + qed + thus ?thesis by (simp add:cs_dependants_def) + qed + thus ?thesis by simp + qed + thus ?thesis by auto + qed + moreover have h2: "(?f ` ?B) \ {}" + proof - + have "?B \ {}" by simp + thus ?thesis by simp + qed + from Max_in [OF h1 h2] + have "Max (?f ` ?B) \ (?f ` ?B)" . + thus ?thesis by (auto intro:that) + qed + from eq_f_th1 eq_f_th2 eq_max + have eq_preced: "preced th1' s = preced th2' s" by auto + hence eq_th12: "th1' = th2'" + proof (rule preced_unique) + from th1_in have "th1' = th1 \ (th1' \ dependants (wq s) th1)" by simp + thus "th1' \ threads s" + proof + assume "th1' \ dependants (wq s) th1" + hence "(Th th1') \ Domain ((RAG s)^+)" + apply (unfold cs_dependants_def cs_RAG_def s_RAG_def) + by (auto simp:Domain_def) + hence "(Th th1') \ Domain (RAG s)" by (simp add:trancl_domain) + from dm_RAG_threads[OF this] show ?thesis . + next + assume "th1' = th1" + with runing_1 show ?thesis + by (unfold runing_def readys_def, auto) + qed + next + from th2_in have "th2' = th2 \ (th2' \ dependants (wq s) th2)" by simp + thus "th2' \ threads s" + proof + assume "th2' \ dependants (wq s) th2" + hence "(Th th2') \ Domain ((RAG s)^+)" + apply (unfold cs_dependants_def cs_RAG_def s_RAG_def) + by (auto simp:Domain_def) + hence "(Th th2') \ Domain (RAG s)" by (simp add:trancl_domain) + from dm_RAG_threads[OF this] show ?thesis . + next + assume "th2' = th2" + with runing_2 show ?thesis + by (unfold runing_def readys_def, auto) + qed + qed + from th1_in have "th1' = th1 \ th1' \ dependants (wq s) th1" by simp + thus ?thesis + proof + assume eq_th': "th1' = th1" + from th2_in have "th2' = th2 \ th2' \ dependants (wq s) th2" by simp + thus ?thesis + proof + assume "th2' = th2" thus ?thesis using eq_th' eq_th12 by simp + next + assume "th2' \ dependants (wq s) th2" + with eq_th12 eq_th' have "th1 \ dependants (wq s) th2" by simp + hence "(Th th1, Th th2) \ (RAG s)^+" + by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp) + hence "Th th1 \ Domain ((RAG s)^+)" + apply (unfold cs_dependants_def cs_RAG_def s_RAG_def) + by (auto simp:Domain_def) + hence "Th th1 \ Domain (RAG s)" by (simp add:trancl_domain) + then obtain n where d: "(Th th1, n) \ RAG s" by (auto simp:Domain_def) + from RAG_target_th [OF this] + obtain cs' where "n = Cs cs'" by auto + with d have "(Th th1, Cs cs') \ RAG s" by simp + with runing_1 have "False" + apply (unfold runing_def readys_def s_RAG_def) + by (auto simp:waiting_eq) + thus ?thesis by simp + qed + next + assume th1'_in: "th1' \ dependants (wq s) th1" + from th2_in have "th2' = th2 \ th2' \ dependants (wq s) th2" by simp + thus ?thesis + proof + assume "th2' = th2" + with th1'_in eq_th12 have "th2 \ dependants (wq s) th1" by simp + hence "(Th th2, Th th1) \ (RAG s)^+" + by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp) + hence "Th th2 \ Domain ((RAG s)^+)" + apply (unfold cs_dependants_def cs_RAG_def s_RAG_def) + by (auto simp:Domain_def) + hence "Th th2 \ Domain (RAG s)" by (simp add:trancl_domain) + then obtain n where d: "(Th th2, n) \ RAG s" by (auto simp:Domain_def) + from RAG_target_th [OF this] + obtain cs' where "n = Cs cs'" by auto + with d have "(Th th2, Cs cs') \ RAG s" by simp + with runing_2 have "False" + apply (unfold runing_def readys_def s_RAG_def) + by (auto simp:waiting_eq) + thus ?thesis by simp + next + assume "th2' \ dependants (wq s) th2" + with eq_th12 have "th1' \ dependants (wq s) th2" by simp + hence h1: "(Th th1', Th th2) \ (RAG s)^+" + by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp) + from th1'_in have h2: "(Th th1', Th th1) \ (RAG s)^+" + by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp) + show ?thesis + proof(rule dchain_unique[OF h1 _ h2, symmetric]) + from runing_1 show "th1 \ readys s" by (simp add:runing_def) + from runing_2 show "th2 \ readys s" by (simp add:runing_def) + qed + qed + qed +qed + + +lemma "card (runing s) \ 1" +apply(subgoal_tac "finite (runing s)") +prefer 2 +apply (metis finite_nat_set_iff_bounded lessI runing_unique) +apply(rule ccontr) +apply(simp) +apply(case_tac "Suc (Suc 0) \ card (runing s)") +apply(subst (asm) card_le_Suc_iff) +apply(simp) +apply(auto)[1] +apply (metis insertCI runing_unique) +apply(auto) +done + +end + + +lemma create_pre: + assumes stp: "step s e" + and not_in: "th \ threads s" + and is_in: "th \ threads (e#s)" + obtains prio where "e = Create th prio" +proof - + from assms + show ?thesis + proof(cases) + case (thread_create thread prio) + with is_in not_in have "e = Create th prio" by simp + from that[OF this] show ?thesis . + next + case (thread_exit thread) + with assms show ?thesis by (auto intro!:that) + next + case (thread_P thread) + with assms show ?thesis by (auto intro!:that) + next + case (thread_V thread) + with assms show ?thesis by (auto intro!:that) + next + case (thread_set thread) + with assms show ?thesis by (auto intro!:that) + qed +qed + +context valid_trace +begin + +lemma cnp_cnv_eq: + assumes "th \ threads s" + shows "cntP s th = cntV s th" + using assms + using cnp_cnv_cncs not_thread_cncs by auto + +end + + +lemma eq_RAG: + "RAG (wq s) = RAG s" +by (unfold cs_RAG_def s_RAG_def, auto) + +context valid_trace +begin + +lemma count_eq_dependants: + assumes eq_pv: "cntP s th = cntV s th" + shows "dependants (wq s) th = {}" +proof - + from cnp_cnv_cncs and eq_pv + have "cntCS s th = 0" + by (auto split:if_splits) + moreover have "finite {cs. (Cs cs, Th th) \ RAG s}" + proof - + from finite_holding[of th] show ?thesis + by (simp add:holdents_test) + qed + ultimately have h: "{cs. (Cs cs, Th th) \ RAG s} = {}" + by (unfold cntCS_def holdents_test cs_dependants_def, auto) + show ?thesis + proof(unfold cs_dependants_def) + { assume "{th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ {}" + then obtain th' where "(Th th', Th th) \ (RAG (wq s))\<^sup>+" by auto + hence "False" + proof(cases) + assume "(Th th', Th th) \ RAG (wq s)" + thus "False" by (auto simp:cs_RAG_def) + next + fix c + assume "(c, Th th) \ RAG (wq s)" + with h and eq_RAG show "False" + by (cases c, auto simp:cs_RAG_def) + qed + } thus "{th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} = {}" by auto + qed +qed + +lemma dependants_threads: + shows "dependants (wq s) th \ threads s" +proof + { fix th th' + assume h: "th \ {th'a. (Th th'a, Th th') \ (RAG (wq s))\<^sup>+}" + have "Th th \ Domain (RAG s)" + proof - + from h obtain th' where "(Th th, Th th') \ (RAG (wq s))\<^sup>+" by auto + hence "(Th th) \ Domain ( (RAG (wq s))\<^sup>+)" by (auto simp:Domain_def) + with trancl_domain have "(Th th) \ Domain (RAG (wq s))" by simp + thus ?thesis using eq_RAG by simp + qed + from dm_RAG_threads[OF this] + have "th \ threads s" . + } note hh = this + fix th1 + assume "th1 \ dependants (wq s) th" + hence "th1 \ {th'a. (Th th'a, Th th) \ (RAG (wq s))\<^sup>+}" + by (unfold cs_dependants_def, simp) + from hh [OF this] show "th1 \ threads s" . +qed + +lemma finite_threads: + shows "finite (threads s)" +using vt by (induct) (auto elim: step.cases) + +end + +lemma Max_f_mono: + assumes seq: "A \ B" + and np: "A \ {}" + and fnt: "finite B" + shows "Max (f ` A) \ Max (f ` B)" +proof(rule Max_mono) + from seq show "f ` A \ f ` B" by auto +next + from np show "f ` A \ {}" by auto +next + from fnt and seq show "finite (f ` B)" by auto +qed + +context valid_trace +begin + +lemma cp_le: + assumes th_in: "th \ threads s" + shows "cp s th \ Max ((\ th. (preced th s)) ` threads s)" +proof(unfold cp_eq_cpreced cpreced_def cs_dependants_def) + show "Max ((\th. preced th s) ` ({th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+})) + \ Max ((\th. preced th s) ` threads s)" + (is "Max (?f ` ?A) \ Max (?f ` ?B)") + proof(rule Max_f_mono) + show "{th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ {}" by simp + next + from finite_threads + show "finite (threads s)" . + next + from th_in + show "{th} \ {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ threads s" + apply (auto simp:Domain_def) + apply (rule_tac dm_RAG_threads) + apply (unfold trancl_domain [of "RAG s", symmetric]) + by (unfold cs_RAG_def s_RAG_def, auto simp:Domain_def) + qed +qed + +lemma le_cp: + shows "preced th s \ cp s th" +proof(unfold cp_eq_cpreced preced_def cpreced_def, simp) + show "Prc (priority th s) (last_set th s) + \ Max (insert (Prc (priority th s) (last_set th s)) + ((\th. Prc (priority th s) (last_set th s)) ` dependants (wq s) th))" + (is "?l \ Max (insert ?l ?A)") + proof(cases "?A = {}") + case False + have "finite ?A" (is "finite (?f ` ?B)") + proof - + have "finite ?B" + proof- + have "finite {th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+}" + proof - + let ?F = "\ (x, y). the_th x" + have "{th'. (Th th', Th th) \ (RAG (wq s))\<^sup>+} \ ?F ` ((RAG (wq s))\<^sup>+)" + apply (auto simp:image_def) + by (rule_tac x = "(Th x, Th th)" in bexI, auto) + moreover have "finite \" + proof - + from finite_RAG have "finite (RAG s)" . + hence "finite ((RAG (wq s))\<^sup>+)" + apply (unfold finite_trancl) + by (auto simp: s_RAG_def cs_RAG_def wq_def) + thus ?thesis by auto + qed + ultimately show ?thesis by (auto intro:finite_subset) + qed + thus ?thesis by (simp add:cs_dependants_def) + qed + thus ?thesis by simp + qed + from Max_insert [OF this False, of ?l] show ?thesis by auto + next + case True + thus ?thesis by auto + qed +qed + +lemma max_cp_eq: + shows "Max ((cp s) ` threads s) = Max ((\ th. (preced th s)) ` threads s)" + (is "?l = ?r") +proof(cases "threads s = {}") + case True + thus ?thesis by auto +next + case False + have "?l \ ((cp s) ` threads s)" + proof(rule Max_in) + from finite_threads + show "finite (cp s ` threads s)" by auto + next + from False show "cp s ` threads s \ {}" by auto + qed + then obtain th + where th_in: "th \ threads s" and eq_l: "?l = cp s th" by auto + have "\ \ ?r" by (rule cp_le[OF th_in]) + moreover have "?r \ cp s th" (is "Max (?f ` ?A) \ cp s th") + proof - + have "?r \ (?f ` ?A)" + proof(rule Max_in) + from finite_threads + show " finite ((\th. preced th s) ` threads s)" by auto + next + from False show " (\th. preced th s) ` threads s \ {}" by auto + qed + then obtain th' where + th_in': "th' \ ?A " and eq_r: "?r = ?f th'" by auto + from le_cp [of th'] eq_r + have "?r \ cp s th'" by auto + moreover have "\ \ cp s th" + proof(fold eq_l) + show " cp s th' \ Max (cp s ` threads s)" + proof(rule Max_ge) + from th_in' show "cp s th' \ cp s ` threads s" + by auto + next + from finite_threads + show "finite (cp s ` threads s)" by auto + qed + qed + ultimately show ?thesis by auto + qed + ultimately show ?thesis using eq_l by auto +qed + +lemma max_cp_readys_threads_pre: + assumes np: "threads s \ {}" + shows "Max (cp s ` readys s) = Max (cp s ` threads s)" +proof(unfold max_cp_eq) + show "Max (cp s ` readys s) = Max ((\th. preced th s) ` threads s)" + proof - + let ?p = "Max ((\th. preced th s) ` threads s)" + let ?f = "(\th. preced th s)" + have "?p \ ((\th. preced th s) ` threads s)" + proof(rule Max_in) + from finite_threads show "finite (?f ` threads s)" by simp + next + from np show "?f ` threads s \ {}" by simp + qed + then obtain tm where tm_max: "?f tm = ?p" and tm_in: "tm \ threads s" + by (auto simp:Image_def) + from th_chain_to_ready [OF tm_in] + have "tm \ readys s \ (\th'. th' \ readys s \ (Th tm, Th th') \ (RAG s)\<^sup>+)" . + thus ?thesis + proof + assume "\th'. th' \ readys s \ (Th tm, Th th') \ (RAG s)\<^sup>+ " + then obtain th' where th'_in: "th' \ readys s" + and tm_chain:"(Th tm, Th th') \ (RAG s)\<^sup>+" by auto + have "cp s th' = ?f tm" + proof(subst cp_eq_cpreced, subst cpreced_def, rule Max_eqI) + from dependants_threads finite_threads + show "finite ((\th. preced th s) ` ({th'} \ dependants (wq s) th'))" + by (auto intro:finite_subset) + next + fix p assume p_in: "p \ (\th. preced th s) ` ({th'} \ dependants (wq s) th')" + from tm_max have " preced tm s = Max ((\th. preced th s) ` threads s)" . + moreover have "p \ \" + proof(rule Max_ge) + from finite_threads + show "finite ((\th. preced th s) ` threads s)" by simp + next + from p_in and th'_in and dependants_threads[of th'] + show "p \ (\th. preced th s) ` threads s" + by (auto simp:readys_def) + qed + ultimately show "p \ preced tm s" by auto + next + show "preced tm s \ (\th. preced th s) ` ({th'} \ dependants (wq s) th')" + proof - + from tm_chain + have "tm \ dependants (wq s) th'" + by (unfold cs_dependants_def s_RAG_def cs_RAG_def, auto) + thus ?thesis by auto + qed + qed + with tm_max + have h: "cp s th' = Max ((\th. preced th s) ` threads s)" by simp + show ?thesis + proof (fold h, rule Max_eqI) + fix q + assume "q \ cp s ` readys s" + then obtain th1 where th1_in: "th1 \ readys s" + and eq_q: "q = cp s th1" by auto + show "q \ cp s th'" + apply (unfold h eq_q) + apply (unfold cp_eq_cpreced cpreced_def) + apply (rule Max_mono) + proof - + from dependants_threads [of th1] th1_in + show "(\th. preced th s) ` ({th1} \ dependants (wq s) th1) \ + (\th. preced th s) ` threads s" + by (auto simp:readys_def) + next + show "(\th. preced th s) ` ({th1} \ dependants (wq s) th1) \ {}" by simp + next + from finite_threads + show " finite ((\th. preced th s) ` threads s)" by simp + qed + next + from finite_threads + show "finite (cp s ` readys s)" by (auto simp:readys_def) + next + from th'_in + show "cp s th' \ cp s ` readys s" by simp + qed + next + assume tm_ready: "tm \ readys s" + show ?thesis + proof(fold tm_max) + have cp_eq_p: "cp s tm = preced tm s" + proof(unfold cp_eq_cpreced cpreced_def, rule Max_eqI) + fix y + assume hy: "y \ (\th. preced th s) ` ({tm} \ dependants (wq s) tm)" + show "y \ preced tm s" + proof - + { fix y' + assume hy' : "y' \ ((\th. preced th s) ` dependants (wq s) tm)" + have "y' \ preced tm s" + proof(unfold tm_max, rule Max_ge) + from hy' dependants_threads[of tm] + show "y' \ (\th. preced th s) ` threads s" by auto + next + from finite_threads + show "finite ((\th. preced th s) ` threads s)" by simp + qed + } with hy show ?thesis by auto + qed + next + from dependants_threads[of tm] finite_threads + show "finite ((\th. preced th s) ` ({tm} \ dependants (wq s) tm))" + by (auto intro:finite_subset) + next + show "preced tm s \ (\th. preced th s) ` ({tm} \ dependants (wq s) tm)" + by simp + qed + moreover have "Max (cp s ` readys s) = cp s tm" + proof(rule Max_eqI) + from tm_ready show "cp s tm \ cp s ` readys s" by simp + next + from finite_threads + show "finite (cp s ` readys s)" by (auto simp:readys_def) + next + fix y assume "y \ cp s ` readys s" + then obtain th1 where th1_readys: "th1 \ readys s" + and h: "y = cp s th1" by auto + show "y \ cp s tm" + apply(unfold cp_eq_p h) + apply(unfold cp_eq_cpreced cpreced_def tm_max, rule Max_mono) + proof - + from finite_threads + show "finite ((\th. preced th s) ` threads s)" by simp + next + show "(\th. preced th s) ` ({th1} \ dependants (wq s) th1) \ {}" + by simp + next + from dependants_threads[of th1] th1_readys + show "(\th. preced th s) ` ({th1} \ dependants (wq s) th1) + \ (\th. preced th s) ` threads s" + by (auto simp:readys_def) + qed + qed + ultimately show " Max (cp s ` readys s) = preced tm s" by simp + qed + qed + qed +qed + +text {* (* ccc *) \noindent + Since the current precedence of the threads in ready queue will always be boosted, + there must be one inside it has the maximum precedence of the whole system. +*} +lemma max_cp_readys_threads: + shows "Max (cp s ` readys s) = Max (cp s ` threads s)" +proof(cases "threads s = {}") + case True + thus ?thesis + by (auto simp:readys_def) +next + case False + show ?thesis by (rule max_cp_readys_threads_pre[OF False]) +qed + +end + +lemma eq_holding: "holding (wq s) th cs = holding s th cs" + apply (unfold s_holding_def cs_holding_def wq_def, simp) + done + +lemma f_image_eq: + assumes h: "\ a. a \ A \ f a = g a" + shows "f ` A = g ` A" +proof + show "f ` A \ g ` A" + by(rule image_subsetI, auto intro:h) +next + show "g ` A \ f ` A" + by (rule image_subsetI, auto intro:h[symmetric]) +qed + + +definition detached :: "state \ thread \ bool" + where "detached s th \ (\(\ cs. holding s th cs)) \ (\(\cs. waiting s th cs))" + +lemma detached_test: + shows "detached s th = (Th th \ Field (RAG s))" +apply(simp add: detached_def Field_def) +apply(simp add: s_RAG_def) +apply(simp add: s_holding_abv s_waiting_abv) +apply(simp add: Domain_iff Range_iff) +apply(simp add: wq_def) +apply(auto) +done + +context valid_trace +begin + +lemma detached_intro: + assumes eq_pv: "cntP s th = cntV s th" + shows "detached s th" +proof - + from cnp_cnv_cncs + have eq_cnt: "cntP s th = + cntV s th + (if th \ readys s \ th \ threads s then cntCS s th else cntCS s th + 1)" . + hence cncs_zero: "cntCS s th = 0" + by (auto simp:eq_pv split:if_splits) + with eq_cnt + have "th \ readys s \ th \ threads s" by (auto simp:eq_pv) + thus ?thesis + proof + assume "th \ threads s" + with range_in dm_RAG_threads + show ?thesis + by (auto simp add: detached_def s_RAG_def s_waiting_abv s_holding_abv wq_def Domain_iff Range_iff) + next + assume "th \ readys s" + moreover have "Th th \ Range (RAG s)" + proof - + from card_0_eq [OF finite_holding] and cncs_zero + have "holdents s th = {}" + by (simp add:cntCS_def) + thus ?thesis + apply(auto simp:holdents_test) + apply(case_tac a) + apply(auto simp:holdents_test s_RAG_def) + done + qed + ultimately show ?thesis + by (auto simp add: detached_def s_RAG_def s_waiting_abv s_holding_abv wq_def readys_def) + qed +qed + +lemma detached_elim: + assumes dtc: "detached s th" + shows "cntP s th = cntV s th" +proof - + from cnp_cnv_cncs + have eq_pv: " cntP s th = + cntV s th + (if th \ readys s \ th \ threads s then cntCS s th else cntCS s th + 1)" . + have cncs_z: "cntCS s th = 0" + proof - + from dtc have "holdents s th = {}" + unfolding detached_def holdents_test s_RAG_def + by (simp add: s_waiting_abv wq_def s_holding_abv Domain_iff Range_iff) + thus ?thesis by (auto simp:cntCS_def) + qed + show ?thesis + proof(cases "th \ threads s") + case True + with dtc + have "th \ readys s" + by (unfold readys_def detached_def Field_def Domain_def Range_def, + auto simp:waiting_eq s_RAG_def) + with cncs_z and eq_pv show ?thesis by simp + next + case False + with cncs_z and eq_pv show ?thesis by simp + qed +qed + +lemma detached_eq: + shows "(detached s th) = (cntP s th = cntV s th)" + by (insert vt, auto intro:detached_intro detached_elim) + +end + +text {* + The lemmas in this .thy file are all obvious lemmas, however, they still needs to be derived + from the concise and miniature model of PIP given in PrioGDef.thy. +*} + +lemma eq_dependants: "dependants (wq s) = dependants s" + by (simp add: s_dependants_abv wq_def) + +lemma next_th_unique: + assumes nt1: "next_th s th cs th1" + and nt2: "next_th s th cs th2" + shows "th1 = th2" +using assms by (unfold next_th_def, auto) + +lemma birth_time_lt: "s \ [] \ last_set th s < length s" + apply (induct s, simp) +proof - + fix a s + assume ih: "s \ [] \ last_set th s < length s" + and eq_as: "a # s \ []" + show "last_set th (a # s) < length (a # s)" + proof(cases "s \ []") + case False + from False show ?thesis + by (cases a, auto simp:last_set.simps) + next + case True + from ih [OF True] show ?thesis + by (cases a, auto simp:last_set.simps) + qed +qed + +lemma th_in_ne: "th \ threads s \ s \ []" + by (induct s, auto simp:threads.simps) + +lemma preced_tm_lt: "th \ threads s \ preced th s = Prc x y \ y < length s" + apply (drule_tac th_in_ne) + by (unfold preced_def, auto intro: birth_time_lt) + +lemma inj_the_preced: + "inj_on (the_preced s) (threads s)" + by (metis inj_onI preced_unique the_preced_def) + +lemma tRAG_alt_def: + "tRAG s = {(Th th1, Th th2) | th1 th2. + \ cs. (Th th1, Cs cs) \ RAG s \ (Cs cs, Th th2) \ RAG s}" + by (auto simp:tRAG_def RAG_split wRAG_def hRAG_def) + +lemma tRAG_Field: + "Field (tRAG s) \ Field (RAG s)" + by (unfold tRAG_alt_def Field_def, auto) + +lemma tRAG_ancestorsE: + assumes "x \ ancestors (tRAG s) u" + obtains th where "x = Th th" +proof - + from assms have "(u, x) \ (tRAG s)^+" + by (unfold ancestors_def, auto) + from tranclE[OF this] obtain c where "(c, x) \ tRAG s" by auto + then obtain th where "x = Th th" + by (unfold tRAG_alt_def, auto) + from that[OF this] show ?thesis . +qed + +lemma tRAG_mono: + assumes "RAG s' \ RAG s" + shows "tRAG s' \ tRAG s" + using assms + by (unfold tRAG_alt_def, auto) + +lemma holding_next_thI: + assumes "holding s th cs" + and "length (wq s cs) > 1" + obtains th' where "next_th s th cs th'" +proof - + from assms(1)[folded eq_holding, unfolded cs_holding_def] + have " th \ set (wq s cs) \ th = hd (wq s cs)" . + then obtain rest where h1: "wq s cs = th#rest" + by (cases "wq s cs", auto) + with assms(2) have h2: "rest \ []" by auto + let ?th' = "hd (SOME q. distinct q \ set q = set rest)" + have "next_th s th cs ?th'" using h1(1) h2 + by (unfold next_th_def, auto) + from that[OF this] show ?thesis . +qed + +lemma RAG_tRAG_transfer: + assumes "vt s'" + assumes "RAG s = RAG s' \ {(Th th, Cs cs)}" + and "(Cs cs, Th th'') \ RAG s'" + shows "tRAG s = tRAG s' \ {(Th th, Th th'')}" (is "?L = ?R") +proof - + interpret vt_s': valid_trace "s'" using assms(1) + by (unfold_locales, simp) + interpret rtree: rtree "RAG s'" + proof + show "single_valued (RAG s')" + apply (intro_locales) + by (unfold single_valued_def, + auto intro:vt_s'.unique_RAG) + + show "acyclic (RAG s')" + by (rule vt_s'.acyclic_RAG) + qed + { fix n1 n2 + assume "(n1, n2) \ ?L" + from this[unfolded tRAG_alt_def] + obtain th1 th2 cs' where + h: "n1 = Th th1" "n2 = Th th2" + "(Th th1, Cs cs') \ RAG s" + "(Cs cs', Th th2) \ RAG s" by auto + from h(4) and assms(2) have cs_in: "(Cs cs', Th th2) \ RAG s'" by auto + from h(3) and assms(2) + have "(Th th1, Cs cs') = (Th th, Cs cs) \ + (Th th1, Cs cs') \ RAG s'" by auto + hence "(n1, n2) \ ?R" + proof + assume h1: "(Th th1, Cs cs') = (Th th, Cs cs)" + hence eq_th1: "th1 = th" by simp + moreover have "th2 = th''" + proof - + from h1 have "cs' = cs" by simp + from assms(3) cs_in[unfolded this] rtree.sgv + show ?thesis + by (unfold single_valued_def, auto) + qed + ultimately show ?thesis using h(1,2) by auto + next + assume "(Th th1, Cs cs') \ RAG s'" + with cs_in have "(Th th1, Th th2) \ tRAG s'" + by (unfold tRAG_alt_def, auto) + from this[folded h(1, 2)] show ?thesis by auto + qed + } moreover { + fix n1 n2 + assume "(n1, n2) \ ?R" + hence "(n1, n2) \tRAG s' \ (n1, n2) = (Th th, Th th'')" by auto + hence "(n1, n2) \ ?L" + proof + assume "(n1, n2) \ tRAG s'" + moreover have "... \ ?L" + proof(rule tRAG_mono) + show "RAG s' \ RAG s" by (unfold assms(2), auto) + qed + ultimately show ?thesis by auto + next + assume eq_n: "(n1, n2) = (Th th, Th th'')" + from assms(2, 3) have "(Cs cs, Th th'') \ RAG s" by auto + moreover have "(Th th, Cs cs) \ RAG s" using assms(2) by auto + ultimately show ?thesis + by (unfold eq_n tRAG_alt_def, auto) + qed + } ultimately show ?thesis by auto +qed + +context valid_trace +begin + +lemmas RAG_tRAG_transfer = RAG_tRAG_transfer[OF vt] + +end + +lemma cp_alt_def: + "cp s th = + Max ((the_preced s) ` {th'. Th th' \ (subtree (RAG s) (Th th))})" +proof - + have "Max (the_preced s ` ({th} \ dependants (wq s) th)) = + Max (the_preced s ` {th'. Th th' \ subtree (RAG s) (Th th)})" + (is "Max (_ ` ?L) = Max (_ ` ?R)") + proof - + have "?L = ?R" + by (auto dest:rtranclD simp:cs_dependants_def cs_RAG_def s_RAG_def subtree_def) + thus ?thesis by simp + qed + thus ?thesis by (unfold cp_eq_cpreced cpreced_def, fold the_preced_def, simp) +qed + +lemma cp_gen_alt_def: + "cp_gen s = (Max \ (\x. (the_preced s \ the_thread) ` subtree (tRAG s) x))" + by (auto simp:cp_gen_def) + +lemma tRAG_nodeE: + assumes "(n1, n2) \ tRAG s" + obtains th1 th2 where "n1 = Th th1" "n2 = Th th2" + using assms + by (auto simp: tRAG_def wRAG_def hRAG_def tRAG_def) + +lemma subtree_nodeE: + assumes "n \ subtree (tRAG s) (Th th)" + obtains th1 where "n = Th th1" +proof - + show ?thesis + proof(rule subtreeE[OF assms]) + assume "n = Th th" + from that[OF this] show ?thesis . + next + assume "Th th \ ancestors (tRAG s) n" + hence "(n, Th th) \ (tRAG s)^+" by (auto simp:ancestors_def) + hence "\ th1. n = Th th1" + proof(induct) + case (base y) + from tRAG_nodeE[OF this] show ?case by metis + next + case (step y z) + thus ?case by auto + qed + with that show ?thesis by auto + qed +qed + +lemma tRAG_star_RAG: "(tRAG s)^* \ (RAG s)^*" +proof - + have "(wRAG s O hRAG s)^* \ (RAG s O RAG s)^*" + by (rule rtrancl_mono, auto simp:RAG_split) + also have "... \ ((RAG s)^*)^*" + by (rule rtrancl_mono, auto) + also have "... = (RAG s)^*" by simp + finally show ?thesis by (unfold tRAG_def, simp) +qed + +lemma tRAG_subtree_RAG: "subtree (tRAG s) x \ subtree (RAG s) x" +proof - + { fix a + assume "a \ subtree (tRAG s) x" + hence "(a, x) \ (tRAG s)^*" by (auto simp:subtree_def) + with tRAG_star_RAG[of s] + have "(a, x) \ (RAG s)^*" by auto + hence "a \ subtree (RAG s) x" by (auto simp:subtree_def) + } thus ?thesis by auto +qed + +lemma tRAG_trancl_eq: + "{th'. (Th th', Th th) \ (tRAG s)^+} = + {th'. (Th th', Th th) \ (RAG s)^+}" + (is "?L = ?R") +proof - + { fix th' + assume "th' \ ?L" + hence "(Th th', Th th) \ (tRAG s)^+" by auto + from tranclD[OF this] + obtain z where h: "(Th th', z) \ tRAG s" "(z, Th th) \ (tRAG s)\<^sup>*" by auto + from tRAG_subtree_RAG[of s] and this(2) + have "(z, Th th) \ (RAG s)^*" by (meson subsetCE tRAG_star_RAG) + moreover from h(1) have "(Th th', z) \ (RAG s)^+" using tRAG_alt_def by auto + ultimately have "th' \ ?R" by auto + } moreover + { fix th' + assume "th' \ ?R" + hence "(Th th', Th th) \ (RAG s)^+" by (auto) + from plus_rpath[OF this] + obtain xs where rp: "rpath (RAG s) (Th th') xs (Th th)" "xs \ []" by auto + hence "(Th th', Th th) \ (tRAG s)^+" + proof(induct xs arbitrary:th' th rule:length_induct) + case (1 xs th' th) + then obtain x1 xs1 where Cons1: "xs = x1#xs1" by (cases xs, auto) + show ?case + proof(cases "xs1") + case Nil + from 1(2)[unfolded Cons1 Nil] + have rp: "rpath (RAG s) (Th th') [x1] (Th th)" . + hence "(Th th', x1) \ (RAG s)" by (cases, simp) + then obtain cs where "x1 = Cs cs" + by (unfold s_RAG_def, auto) + from rpath_nnl_lastE[OF rp[unfolded this]] + show ?thesis by auto + next + case (Cons x2 xs2) + from 1(2)[unfolded Cons1[unfolded this]] + have rp: "rpath (RAG s) (Th th') (x1 # x2 # xs2) (Th th)" . + from rpath_edges_on[OF this] + have eds: "edges_on (Th th' # x1 # x2 # xs2) \ RAG s" . + have "(Th th', x1) \ edges_on (Th th' # x1 # x2 # xs2)" + by (simp add: edges_on_unfold) + with eds have rg1: "(Th th', x1) \ RAG s" by auto + then obtain cs1 where eq_x1: "x1 = Cs cs1" by (unfold s_RAG_def, auto) + have "(x1, x2) \ edges_on (Th th' # x1 # x2 # xs2)" + by (simp add: edges_on_unfold) + from this eds + have rg2: "(x1, x2) \ RAG s" by auto + from this[unfolded eq_x1] + obtain th1 where eq_x2: "x2 = Th th1" by (unfold s_RAG_def, auto) + from rg1[unfolded eq_x1] rg2[unfolded eq_x1 eq_x2] + have rt1: "(Th th', Th th1) \ tRAG s" by (unfold tRAG_alt_def, auto) + from rp have "rpath (RAG s) x2 xs2 (Th th)" + by (elim rpath_ConsE, simp) + from this[unfolded eq_x2] have rp': "rpath (RAG s) (Th th1) xs2 (Th th)" . + show ?thesis + proof(cases "xs2 = []") + case True + from rpath_nilE[OF rp'[unfolded this]] + have "th1 = th" by auto + from rt1[unfolded this] show ?thesis by auto + next + case False + from 1(1)[rule_format, OF _ rp' this, unfolded Cons1 Cons] + have "(Th th1, Th th) \ (tRAG s)\<^sup>+" by simp + with rt1 show ?thesis by auto + qed + qed + qed + hence "th' \ ?L" by auto + } ultimately show ?thesis by blast +qed + +lemma tRAG_trancl_eq_Th: + "{Th th' | th'. (Th th', Th th) \ (tRAG s)^+} = + {Th th' | th'. (Th th', Th th) \ (RAG s)^+}" + using tRAG_trancl_eq by auto + +lemma dependants_alt_def: + "dependants s th = {th'. (Th th', Th th) \ (tRAG s)^+}" + by (metis eq_RAG s_dependants_def tRAG_trancl_eq) + +context valid_trace +begin + +lemma count_eq_tRAG_plus: + assumes "cntP s th = cntV s th" + shows "{th'. (Th th', Th th) \ (tRAG s)^+} = {}" + using assms count_eq_dependants dependants_alt_def eq_dependants by auto + +lemma count_eq_RAG_plus: + assumes "cntP s th = cntV s th" + shows "{th'. (Th th', Th th) \ (RAG s)^+} = {}" + using assms count_eq_dependants cs_dependants_def eq_RAG by auto + +lemma count_eq_RAG_plus_Th: + assumes "cntP s th = cntV s th" + shows "{Th th' | th'. (Th th', Th th) \ (RAG s)^+} = {}" + using count_eq_RAG_plus[OF assms] by auto + +lemma count_eq_tRAG_plus_Th: + assumes "cntP s th = cntV s th" + shows "{Th th' | th'. (Th th', Th th) \ (tRAG s)^+} = {}" + using count_eq_tRAG_plus[OF assms] by auto + +end + +lemma tRAG_subtree_eq: + "(subtree (tRAG s) (Th th)) = {Th th' | th'. Th th' \ (subtree (RAG s) (Th th))}" + (is "?L = ?R") +proof - + { fix n + assume h: "n \ ?L" + hence "n \ ?R" + by (smt mem_Collect_eq subsetCE subtree_def subtree_nodeE tRAG_subtree_RAG) + } moreover { + fix n + assume "n \ ?R" + then obtain th' where h: "n = Th th'" "(Th th', Th th) \ (RAG s)^*" + by (auto simp:subtree_def) + from rtranclD[OF this(2)] + have "n \ ?L" + proof + assume "Th th' \ Th th \ (Th th', Th th) \ (RAG s)\<^sup>+" + with h have "n \ {Th th' | th'. (Th th', Th th) \ (RAG s)^+}" by auto + thus ?thesis using subtree_def tRAG_trancl_eq by fastforce + qed (insert h, auto simp:subtree_def) + } ultimately show ?thesis by auto +qed + +lemma threads_set_eq: + "the_thread ` (subtree (tRAG s) (Th th)) = + {th'. Th th' \ (subtree (RAG s) (Th th))}" (is "?L = ?R") + by (auto intro:rev_image_eqI simp:tRAG_subtree_eq) + +lemma cp_alt_def1: + "cp s th = Max ((the_preced s o the_thread) ` (subtree (tRAG s) (Th th)))" +proof - + have "(the_preced s ` the_thread ` subtree (tRAG s) (Th th)) = + ((the_preced s \ the_thread) ` subtree (tRAG s) (Th th))" + by auto + thus ?thesis by (unfold cp_alt_def, fold threads_set_eq, auto) +qed + +lemma cp_gen_def_cond: + assumes "x = Th th" + shows "cp s th = cp_gen s (Th th)" +by (unfold cp_alt_def1 cp_gen_def, simp) + +lemma cp_gen_over_set: + assumes "\ x \ A. \ th. x = Th th" + shows "cp_gen s ` A = (cp s \ the_thread) ` A" +proof(rule f_image_eq) + fix a + assume "a \ A" + from assms[rule_format, OF this] + obtain th where eq_a: "a = Th th" by auto + show "cp_gen s a = (cp s \ the_thread) a" + by (unfold eq_a, simp, unfold cp_gen_def_cond[OF refl[of "Th th"]], simp) +qed + + +context valid_trace +begin + +lemma RAG_threads: + assumes "(Th th) \ Field (RAG s)" + shows "th \ threads s" + using assms + by (metis Field_def UnE dm_RAG_threads range_in vt) + +lemma subtree_tRAG_thread: + assumes "th \ threads s" + shows "subtree (tRAG s) (Th th) \ Th ` threads s" (is "?L \ ?R") +proof - + have "?L = {Th th' |th'. Th th' \ subtree (RAG s) (Th th)}" + by (unfold tRAG_subtree_eq, simp) + also have "... \ ?R" + proof + fix x + assume "x \ {Th th' |th'. Th th' \ subtree (RAG s) (Th th)}" + then obtain th' where h: "x = Th th'" "Th th' \ subtree (RAG s) (Th th)" by auto + from this(2) + show "x \ ?R" + proof(cases rule:subtreeE) + case 1 + thus ?thesis by (simp add: assms h(1)) + next + case 2 + thus ?thesis by (metis ancestors_Field dm_RAG_threads h(1) image_eqI) + qed + qed + finally show ?thesis . +qed + +lemma readys_root: + assumes "th \ readys s" + shows "root (RAG s) (Th th)" +proof - + { fix x + assume "x \ ancestors (RAG s) (Th th)" + hence h: "(Th th, x) \ (RAG s)^+" by (auto simp:ancestors_def) + from tranclD[OF this] + obtain z where "(Th th, z) \ RAG s" by auto + with assms(1) have False + apply (case_tac z, auto simp:readys_def s_RAG_def s_waiting_def cs_waiting_def) + by (fold wq_def, blast) + } thus ?thesis by (unfold root_def, auto) +qed + +lemma readys_in_no_subtree: + assumes "th \ readys s" + and "th' \ th" + shows "Th th \ subtree (RAG s) (Th th')" +proof + assume "Th th \ subtree (RAG s) (Th th')" + thus False + proof(cases rule:subtreeE) + case 1 + with assms show ?thesis by auto + next + case 2 + with readys_root[OF assms(1)] + show ?thesis by (auto simp:root_def) + qed +qed + +lemma not_in_thread_isolated: + assumes "th \ threads s" + shows "(Th th) \ Field (RAG s)" +proof + assume "(Th th) \ Field (RAG s)" + with dm_RAG_threads and range_in assms + show False by (unfold Field_def, blast) +qed + +lemma wf_RAG: "wf (RAG s)" +proof(rule finite_acyclic_wf) + from finite_RAG show "finite (RAG s)" . +next + from acyclic_RAG show "acyclic (RAG s)" . +qed + +lemma sgv_wRAG: "single_valued (wRAG s)" + using waiting_unique + by (unfold single_valued_def wRAG_def, auto) + +lemma sgv_hRAG: "single_valued (hRAG s)" + using holding_unique + by (unfold single_valued_def hRAG_def, auto) + +lemma sgv_tRAG: "single_valued (tRAG s)" + by (unfold tRAG_def, rule single_valued_relcomp, + insert sgv_wRAG sgv_hRAG, auto) + +lemma acyclic_tRAG: "acyclic (tRAG s)" +proof(unfold tRAG_def, rule acyclic_compose) + show "acyclic (RAG s)" using acyclic_RAG . +next + show "wRAG s \ RAG s" unfolding RAG_split by auto +next + show "hRAG s \ RAG s" unfolding RAG_split by auto +qed + +lemma sgv_RAG: "single_valued (RAG s)" + using unique_RAG by (auto simp:single_valued_def) + +lemma rtree_RAG: "rtree (RAG s)" + using sgv_RAG acyclic_RAG + by (unfold rtree_def rtree_axioms_def sgv_def, auto) + +end + +sublocale valid_trace < rtree_RAG: rtree "RAG s" +proof + show "single_valued (RAG s)" + apply (intro_locales) + by (unfold single_valued_def, + auto intro:unique_RAG) + + show "acyclic (RAG s)" + by (rule acyclic_RAG) +qed + +sublocale valid_trace < rtree_s: rtree "tRAG s" +proof(unfold_locales) + from sgv_tRAG show "single_valued (tRAG s)" . +next + from acyclic_tRAG show "acyclic (tRAG s)" . +qed + +sublocale valid_trace < fsbtRAGs : fsubtree "RAG s" +proof - + show "fsubtree (RAG s)" + proof(intro_locales) + show "fbranch (RAG s)" using finite_fbranchI[OF finite_RAG] . + next + show "fsubtree_axioms (RAG s)" + proof(unfold fsubtree_axioms_def) + from wf_RAG show "wf (RAG s)" . + qed + qed +qed + +sublocale valid_trace < fsbttRAGs: fsubtree "tRAG s" +proof - + have "fsubtree (tRAG s)" + proof - + have "fbranch (tRAG s)" + proof(unfold tRAG_def, rule fbranch_compose) + show "fbranch (wRAG s)" + proof(rule finite_fbranchI) + from finite_RAG show "finite (wRAG s)" + by (unfold RAG_split, auto) + qed + next + show "fbranch (hRAG s)" + proof(rule finite_fbranchI) + from finite_RAG + show "finite (hRAG s)" by (unfold RAG_split, auto) + qed + qed + moreover have "wf (tRAG s)" + proof(rule wf_subset) + show "wf (RAG s O RAG s)" using wf_RAG + by (fold wf_comp_self, simp) + next + show "tRAG s \ (RAG s O RAG s)" + by (unfold tRAG_alt_def, auto) + qed + ultimately show ?thesis + by (unfold fsubtree_def fsubtree_axioms_def,auto) + qed + from this[folded tRAG_def] show "fsubtree (tRAG s)" . +qed + +lemma Max_UNION: + assumes "finite A" + and "A \ {}" + and "\ M \ f ` A. finite M" + and "\ M \ f ` A. M \ {}" + shows "Max (\x\ A. f x) = Max (Max ` f ` A)" (is "?L = ?R") + using assms[simp] +proof - + have "?L = Max (\(f ` A))" + by (fold Union_image_eq, simp) + also have "... = ?R" + by (subst Max_Union, simp+) + finally show ?thesis . +qed + +lemma max_Max_eq: + assumes "finite A" + and "A \ {}" + and "x = y" + shows "max x (Max A) = Max ({y} \ A)" (is "?L = ?R") +proof - + have "?R = Max (insert y A)" by simp + also from assms have "... = ?L" + by (subst Max.insert, simp+) + finally show ?thesis by simp +qed + +context valid_trace +begin + +(* ddd *) +lemma cp_gen_rec: + assumes "x = Th th" + shows "cp_gen s x = Max ({the_preced s th} \ (cp_gen s) ` children (tRAG s) x)" +proof(cases "children (tRAG s) x = {}") + case True + show ?thesis + by (unfold True cp_gen_def subtree_children, simp add:assms) +next + case False + hence [simp]: "children (tRAG s) x \ {}" by auto + note fsbttRAGs.finite_subtree[simp] + have [simp]: "finite (children (tRAG s) x)" + by (intro rev_finite_subset[OF fsbttRAGs.finite_subtree], + rule children_subtree) + { fix r x + have "subtree r x \ {}" by (auto simp:subtree_def) + } note this[simp] + have [simp]: "\x\children (tRAG s) x. subtree (tRAG s) x \ {}" + proof - + from False obtain q where "q \ children (tRAG s) x" by blast + moreover have "subtree (tRAG s) q \ {}" by simp + ultimately show ?thesis by blast + qed + have h: "Max ((the_preced s \ the_thread) ` + ({x} \ \(subtree (tRAG s) ` children (tRAG s) x))) = + Max ({the_preced s th} \ cp_gen s ` children (tRAG s) x)" + (is "?L = ?R") + proof - + let "Max (?f ` (?A \ \ (?g ` ?B)))" = ?L + let "Max (_ \ (?h ` ?B))" = ?R + let ?L1 = "?f ` \(?g ` ?B)" + have eq_Max_L1: "Max ?L1 = Max (?h ` ?B)" + proof - + have "?L1 = ?f ` (\ x \ ?B.(?g x))" by simp + also have "... = (\ x \ ?B. ?f ` (?g x))" by auto + finally have "Max ?L1 = Max ..." by simp + also have "... = Max (Max ` (\x. ?f ` subtree (tRAG s) x) ` ?B)" + by (subst Max_UNION, simp+) + also have "... = Max (cp_gen s ` children (tRAG s) x)" + by (unfold image_comp cp_gen_alt_def, simp) + finally show ?thesis . + qed + show ?thesis + proof - + have "?L = Max (?f ` ?A \ ?L1)" by simp + also have "... = max (the_preced s (the_thread x)) (Max ?L1)" + by (subst Max_Un, simp+) + also have "... = max (?f x) (Max (?h ` ?B))" + by (unfold eq_Max_L1, simp) + also have "... =?R" + by (rule max_Max_eq, (simp)+, unfold assms, simp) + finally show ?thesis . + qed + qed thus ?thesis + by (fold h subtree_children, unfold cp_gen_def, simp) +qed + +lemma cp_rec: + "cp s th = Max ({the_preced s th} \ + (cp s o the_thread) ` children (tRAG s) (Th th))" +proof - + have "Th th = Th th" by simp + note h = cp_gen_def_cond[OF this] cp_gen_rec[OF this] + show ?thesis + proof - + have "cp_gen s ` children (tRAG s) (Th th) = + (cp s \ the_thread) ` children (tRAG s) (Th th)" + proof(rule cp_gen_over_set) + show " \x\children (tRAG s) (Th th). \th. x = Th th" + by (unfold tRAG_alt_def, auto simp:children_def) + qed + thus ?thesis by (subst (1) h(1), unfold h(2), simp) + qed +qed + +end + +(* keep *) +lemma next_th_holding: + assumes vt: "vt s" + and nxt: "next_th s th cs th'" + shows "holding (wq s) th cs" +proof - + from nxt[unfolded next_th_def] + obtain rest where h: "wq s cs = th # rest" + "rest \ []" + "th' = hd (SOME q. distinct q \ set q = set rest)" by auto + thus ?thesis + by (unfold cs_holding_def, auto) +qed + +context valid_trace +begin + +lemma next_th_waiting: + assumes nxt: "next_th s th cs th'" + shows "waiting (wq s) th' cs" +proof - + from nxt[unfolded next_th_def] + obtain rest where h: "wq s cs = th # rest" + "rest \ []" + "th' = hd (SOME q. distinct q \ set q = set rest)" by auto + from wq_distinct[of cs, unfolded h] + have dst: "distinct (th # rest)" . + have in_rest: "th' \ set rest" + proof(unfold h, rule someI2) + show "distinct rest \ set rest = set rest" using dst by auto + next + fix x assume "distinct x \ set x = set rest" + with h(2) + show "hd x \ set (rest)" by (cases x, auto) + qed + hence "th' \ set (wq s cs)" by (unfold h(1), auto) + moreover have "th' \ hd (wq s cs)" + by (unfold h(1), insert in_rest dst, auto) + ultimately show ?thesis by (auto simp:cs_waiting_def) +qed + +lemma next_th_RAG: + assumes nxt: "next_th (s::event list) th cs th'" + shows "{(Cs cs, Th th), (Th th', Cs cs)} \ RAG s" + using vt assms next_th_holding next_th_waiting + by (unfold s_RAG_def, simp) + +end + +-- {* A useless definition *} +definition cps:: "state \ (thread \ precedence) set" +where "cps s = {(th, cp s th) | th . th \ threads s}" + +lemma "wq (V th cs # s) cs1 = ttt" + apply (unfold wq_def, auto simp:Let_def) + +end + diff -r 17305a85493d -r c495eb16beb6 CpsG_2.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CpsG_2.thy Wed Jan 27 19:28:42 2016 +0800 @@ -0,0 +1,3557 @@ +theory CpsG +imports PIPDefs +begin + +lemma Max_fg_mono: + assumes "finite A" + and "\ a \ A. f a \ g a" + shows "Max (f ` A) \ Max (g ` A)" +proof(cases "A = {}") + case True + thus ?thesis by auto +next + case False + show ?thesis + proof(rule Max.boundedI) + from assms show "finite (f ` A)" by auto + next + from False show "f ` A \ {}" by auto + next + fix fa + assume "fa \ f ` A" + then obtain a where h_fa: "a \ A" "fa = f a" by auto + show "fa \ Max (g ` A)" + proof(rule Max_ge_iff[THEN iffD2]) + from assms show "finite (g ` A)" by auto + next + from False show "g ` A \ {}" by auto + next + from h_fa have "g a \ g ` A" by auto + moreover have "fa \ g a" using h_fa assms(2) by auto + ultimately show "\a\g ` A. fa \ a" by auto + qed + qed +qed + +lemma Max_f_mono: + assumes seq: "A \ B" + and np: "A \ {}" + and fnt: "finite B" + shows "Max (f ` A) \ Max (f ` B)" +proof(rule Max_mono) + from seq show "f ` A \ f ` B" by auto +next + from np show "f ` A \ {}" by auto +next + from fnt and seq show "finite (f ` B)" by auto +qed + +lemma eq_RAG: + "RAG (wq s) = RAG s" + by (unfold cs_RAG_def s_RAG_def, auto) + +lemma waiting_holding: + assumes "waiting (s::state) th cs" + obtains th' where "holding s th' cs" +proof - + from assms[unfolded s_waiting_def, folded wq_def] + obtain th' where "th' \ set (wq s cs)" "th' = hd (wq s cs)" + by (metis empty_iff hd_in_set list.set(1)) + hence "holding s th' cs" + by (unfold s_holding_def, fold wq_def, auto) + from that[OF this] show ?thesis . +qed + +lemma cp_eq_cpreced: "cp s th = cpreced (wq s) s th" +unfolding cp_def wq_def +apply(induct s rule: schs.induct) +apply(simp add: Let_def cpreced_initial) +apply(simp add: Let_def) +apply(simp add: Let_def) +apply(simp add: Let_def) +apply(subst (2) schs.simps) +apply(simp add: Let_def) +apply(subst (2) schs.simps) +apply(simp add: Let_def) +done + +lemma cp_alt_def: + "cp s th = + Max ((the_preced s) ` {th'. Th th' \ (subtree (RAG s) (Th th))})" +proof - + have "Max (the_preced s ` ({th} \ dependants (wq s) th)) = + Max (the_preced s ` {th'. Th th' \ subtree (RAG s) (Th th)})" + (is "Max (_ ` ?L) = Max (_ ` ?R)") + proof - + have "?L = ?R" + by (auto dest:rtranclD simp:cs_dependants_def cs_RAG_def s_RAG_def subtree_def) + thus ?thesis by simp + qed + thus ?thesis by (unfold cp_eq_cpreced cpreced_def, fold the_preced_def, simp) +qed + +(* ccc *) + + +locale valid_trace = + fixes s + assumes vt : "vt s" + +locale valid_trace_e = valid_trace + + fixes e + assumes vt_e: "vt (e#s)" +begin + +lemma pip_e: "PIP s e" + using vt_e by (cases, simp) + +end + +locale valid_trace_create = valid_trace_e + + fixes th prio + assumes is_create: "e = Create th prio" + +locale valid_trace_exit = valid_trace_e + + fixes th + assumes is_exit: "e = Exit th" + +locale valid_trace_p = valid_trace_e + + fixes th cs + assumes is_p: "e = P th cs" + +locale valid_trace_v = valid_trace_e + + fixes th cs + assumes is_v: "e = V th cs" +begin + definition "rest = tl (wq s cs)" + definition "wq' = (SOME q. distinct q \ set q = set rest)" +end + +locale valid_trace_v_n = valid_trace_v + + assumes rest_nnl: "rest \ []" + +locale valid_trace_v_e = valid_trace_v + + assumes rest_nil: "rest = []" + +locale valid_trace_set= valid_trace_e + + fixes th prio + assumes is_set: "e = Set th prio" + +context valid_trace +begin + +lemma ind [consumes 0, case_names Nil Cons, induct type]: + assumes "PP []" + and "(\s e. valid_trace_e s e \ + PP s \ PIP s e \ PP (e # s))" + shows "PP s" +proof(induct rule:vt.induct[OF vt, case_names Init Step]) + case Init + from assms(1) show ?case . +next + case (Step s e) + show ?case + proof(rule assms(2)) + show "valid_trace_e s e" using Step by (unfold_locales, auto) + next + show "PP s" using Step by simp + next + show "PIP s e" using Step by simp + qed +qed + +lemma vt_moment: "\ t. vt (moment t s)" +proof(induct rule:ind) + case Nil + thus ?case by (simp add:vt_nil) +next + case (Cons s e t) + show ?case + proof(cases "t \ length (e#s)") + case True + from True have "moment t (e#s) = e#s" by simp + thus ?thesis using Cons + by (simp add:valid_trace_def valid_trace_e_def, auto) + next + case False + from Cons have "vt (moment t s)" by simp + moreover have "moment t (e#s) = moment t s" + proof - + from False have "t \ length s" by simp + from moment_app [OF this, of "[e]"] + show ?thesis by simp + qed + ultimately show ?thesis by simp + qed +qed + +lemma finite_threads: + shows "finite (threads s)" +using vt by (induct) (auto elim: step.cases) + +end + +lemma RAG_target_th: "(Th th, x) \ RAG (s::state) \ \ cs. x = Cs cs" + by (unfold s_RAG_def, auto) + +locale valid_moment = valid_trace + + fixes i :: nat + +sublocale valid_moment < vat_moment: valid_trace "(moment i s)" + by (unfold_locales, insert vt_moment, auto) + +lemma waiting_eq: "waiting s th cs = waiting (wq s) th cs" + by (unfold s_waiting_def cs_waiting_def wq_def, auto) + +lemma holding_eq: "holding (s::state) th cs = holding (wq s) th cs" + by (unfold s_holding_def wq_def cs_holding_def, simp) + +lemma runing_ready: + shows "runing s \ readys s" + unfolding runing_def readys_def + by auto + +lemma readys_threads: + shows "readys s \ threads s" + unfolding readys_def + by auto + +lemma wq_v_neq [simp]: + "cs \ cs' \ wq (V thread cs#s) cs' = wq s cs'" + by (auto simp:wq_def Let_def cp_def split:list.splits) + +lemma runing_head: + assumes "th \ runing s" + and "th \ set (wq_fun (schs s) cs)" + shows "th = hd (wq_fun (schs s) cs)" + using assms + by (simp add:runing_def readys_def s_waiting_def wq_def) + +context valid_trace +begin + +lemma runing_wqE: + assumes "th \ runing s" + and "th \ set (wq s cs)" + obtains rest where "wq s cs = th#rest" +proof - + from assms(2) obtain th' rest where eq_wq: "wq s cs = th'#rest" + by (meson list.set_cases) + have "th' = th" + proof(rule ccontr) + assume "th' \ th" + hence "th \ hd (wq s cs)" using eq_wq by auto + with assms(2) + have "waiting s th cs" + by (unfold s_waiting_def, fold wq_def, auto) + with assms show False + by (unfold runing_def readys_def, auto) + qed + with eq_wq that show ?thesis by metis +qed + +end + +context valid_trace_create +begin + +lemma wq_neq_simp [simp]: + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_create wq_def + by (auto simp:Let_def) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" + using assms by simp +end + +context valid_trace_exit +begin + +lemma wq_neq_simp [simp]: + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_exit wq_def + by (auto simp:Let_def) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" + using assms by simp +end + +context valid_trace_p +begin + +lemma wq_neq_simp [simp]: + assumes "cs' \ cs" + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_p wq_def + by (auto simp:Let_def) + +lemma runing_th_s: + shows "th \ runing s" +proof - + from pip_e[unfolded is_p] + show ?thesis by (cases, simp) +qed + +lemma ready_th_s: "th \ readys s" + using runing_th_s + by (unfold runing_def, auto) + +lemma live_th_s: "th \ threads s" + using readys_threads ready_th_s by auto + +lemma live_th_es: "th \ threads (e#s)" + using live_th_s + by (unfold is_p, simp) + +lemma th_not_waiting: + "\ waiting s th c" +proof - + have "th \ readys s" + using runing_ready runing_th_s by blast + thus ?thesis + by (unfold readys_def, auto) +qed + +lemma waiting_neq_th: + assumes "waiting s t c" + shows "t \ th" + using assms using th_not_waiting by blast + +lemma th_not_in_wq: + shows "th \ set (wq s cs)" +proof + assume otherwise: "th \ set (wq s cs)" + from runing_wqE[OF runing_th_s this] + obtain rest where eq_wq: "wq s cs = th#rest" by blast + with otherwise + have "holding s th cs" + by (unfold s_holding_def, fold wq_def, simp) + hence cs_th_RAG: "(Cs cs, Th th) \ RAG s" + by (unfold s_RAG_def, fold holding_eq, auto) + from pip_e[unfolded is_p] + show False + proof(cases) + case (thread_P) + with cs_th_RAG show ?thesis by auto + qed +qed + +lemma wq_es_cs: + "wq (e#s) cs = wq s cs @ [th]" + by (unfold is_p wq_def, auto simp:Let_def) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" +proof(cases "cs' = cs") + case True + show ?thesis using True assms th_not_in_wq + by (unfold True wq_es_cs, auto) +qed (insert assms, simp) + +end + +context valid_trace_v +begin + +lemma wq_neq_simp [simp]: + assumes "cs' \ cs" + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_v wq_def + by (auto simp:Let_def) + +lemma runing_th_s: + shows "th \ runing s" +proof - + from pip_e[unfolded is_v] + show ?thesis by (cases, simp) +qed + +lemma th_not_waiting: + "\ waiting s th c" +proof - + have "th \ readys s" + using runing_ready runing_th_s by blast + thus ?thesis + by (unfold readys_def, auto) +qed + +lemma waiting_neq_th: + assumes "waiting s t c" + shows "t \ th" + using assms using th_not_waiting by blast + +lemma wq_s_cs: + "wq s cs = th#rest" +proof - + from pip_e[unfolded is_v] + show ?thesis + proof(cases) + case (thread_V) + from this(2) show ?thesis + by (unfold rest_def s_holding_def, fold wq_def, + metis empty_iff list.collapse list.set(1)) + qed +qed + +lemma wq_es_cs: + "wq (e#s) cs = wq'" + using wq_s_cs[unfolded wq_def] + by (auto simp:Let_def wq_def rest_def wq'_def is_v, simp) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" +proof(cases "cs' = cs") + case True + show ?thesis + proof(unfold True wq_es_cs wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + using assms[unfolded True wq_s_cs] by auto + qed simp +qed (insert assms, simp) + +end + +context valid_trace_set +begin + +lemma wq_neq_simp [simp]: + shows "wq (e#s) cs' = wq s cs'" + using assms unfolding is_set wq_def + by (auto simp:Let_def) + +lemma wq_distinct_kept: + assumes "distinct (wq s cs')" + shows "distinct (wq (e#s) cs')" + using assms by simp +end + +context valid_trace +begin + +lemma actor_inv: + assumes "PIP s e" + and "\ isCreate e" + shows "actor e \ runing s" + using assms + by (induct, auto) + +lemma isP_E: + assumes "isP e" + obtains cs where "e = P (actor e) cs" + using assms by (cases e, auto) + +lemma isV_E: + assumes "isV e" + obtains cs where "e = V (actor e) cs" + using assms by (cases e, auto) + +lemma wq_distinct: "distinct (wq s cs)" +proof(induct rule:ind) + case (Cons s e) + interpret vt_e: valid_trace_e s e using Cons by simp + show ?case + proof(cases e) + case (Create th prio) + interpret vt_create: valid_trace_create s e th prio + using Create by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_create.wq_distinct_kept) + next + case (Exit th) + interpret vt_exit: valid_trace_exit s e th + using Exit by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_exit.wq_distinct_kept) + next + case (P th cs) + interpret vt_p: valid_trace_p s e th cs using P by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_p.wq_distinct_kept) + next + case (V th cs) + interpret vt_v: valid_trace_v s e th cs using V by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_v.wq_distinct_kept) + next + case (Set th prio) + interpret vt_set: valid_trace_set s e th prio + using Set by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_set.wq_distinct_kept) + qed +qed (unfold wq_def Let_def, simp) + +end + +context valid_trace_e +begin + +text {* + The following lemma shows that only the @{text "P"} + operation can add new thread into waiting queues. + Such kind of lemmas are very obvious, but need to be checked formally. + This is a kind of confirmation that our modelling is correct. +*} + +lemma wq_in_inv: + assumes s_ni: "thread \ set (wq s cs)" + and s_i: "thread \ set (wq (e#s) cs)" + shows "e = P thread cs" +proof(cases e) + -- {* This is the only non-trivial case: *} + case (V th cs1) + have False + proof(cases "cs1 = cs") + case True + show ?thesis + proof(cases "(wq s cs1)") + case (Cons w_hd w_tl) + have "set (wq (e#s) cs) \ set (wq s cs)" + proof - + have "(wq (e#s) cs) = (SOME q. distinct q \ set q = set w_tl)" + using Cons V by (auto simp:wq_def Let_def True split:if_splits) + moreover have "set ... \ set (wq s cs)" + proof(rule someI2) + show "distinct w_tl \ set w_tl = set w_tl" + by (metis distinct.simps(2) local.Cons wq_distinct) + qed (insert Cons True, auto) + ultimately show ?thesis by simp + qed + with assms show ?thesis by auto + qed (insert assms V True, auto simp:wq_def Let_def split:if_splits) + qed (insert assms V, auto simp:wq_def Let_def split:if_splits) + thus ?thesis by auto +qed (insert assms, auto simp:wq_def Let_def split:if_splits) + +lemma wq_out_inv: + assumes s_in: "thread \ set (wq s cs)" + and s_hd: "thread = hd (wq s cs)" + and s_i: "thread \ hd (wq (e#s) cs)" + shows "e = V thread cs" +proof(cases e) +-- {* There are only two non-trivial cases: *} + case (V th cs1) + show ?thesis + proof(cases "cs1 = cs") + case True + have "PIP s (V th cs)" using pip_e[unfolded V[unfolded True]] . + thus ?thesis + proof(cases) + case (thread_V) + moreover have "th = thread" using thread_V(2) s_hd + by (unfold s_holding_def wq_def, simp) + ultimately show ?thesis using V True by simp + qed + qed (insert assms V, auto simp:wq_def Let_def split:if_splits) +next + case (P th cs1) + show ?thesis + proof(cases "cs1 = cs") + case True + with P have "wq (e#s) cs = wq_fun (schs s) cs @ [th]" + by (auto simp:wq_def Let_def split:if_splits) + with s_i s_hd s_in have False + by (metis empty_iff hd_append2 list.set(1) wq_def) + thus ?thesis by simp + qed (insert assms P, auto simp:wq_def Let_def split:if_splits) +qed (insert assms, auto simp:wq_def Let_def split:if_splits) + +end + + +context valid_trace +begin + + +text {* (* ddd *) + The nature of the work is like this: since it starts from a very simple and basic + model, even intuitively very `basic` and `obvious` properties need to derived from scratch. + For instance, the fact + that one thread can not be blocked by two critical resources at the same time + is obvious, because only running threads can make new requests, if one is waiting for + a critical resource and get blocked, it can not make another resource request and get + blocked the second time (because it is not running). + + To derive this fact, one needs to prove by contraction and + reason about time (or @{text "moement"}). The reasoning is based on a generic theorem + named @{text "p_split"}, which is about status changing along the time axis. It says if + a condition @{text "Q"} is @{text "True"} at a state @{text "s"}, + but it was @{text "False"} at the very beginning, then there must exits a moment @{text "t"} + in the history of @{text "s"} (notice that @{text "s"} itself is essentially the history + of events leading to it), such that @{text "Q"} switched + from being @{text "False"} to @{text "True"} and kept being @{text "True"} + till the last moment of @{text "s"}. + + Suppose a thread @{text "th"} is blocked + on @{text "cs1"} and @{text "cs2"} in some state @{text "s"}, + since no thread is blocked at the very beginning, by applying + @{text "p_split"} to these two blocking facts, there exist + two moments @{text "t1"} and @{text "t2"} in @{text "s"}, such that + @{text "th"} got blocked on @{text "cs1"} and @{text "cs2"} + and kept on blocked on them respectively ever since. + + Without lost of generality, we assume @{text "t1"} is earlier than @{text "t2"}. + However, since @{text "th"} was blocked ever since memonent @{text "t1"}, so it was still + in blocked state at moment @{text "t2"} and could not + make any request and get blocked the second time: Contradiction. +*} + +lemma waiting_unique_pre: (* ddd *) + assumes h11: "thread \ set (wq s cs1)" + and h12: "thread \ hd (wq s cs1)" + assumes h21: "thread \ set (wq s cs2)" + and h22: "thread \ hd (wq s cs2)" + and neq12: "cs1 \ cs2" + shows "False" +proof - + let "?Q" = "\ cs s. thread \ set (wq s cs) \ thread \ hd (wq s cs)" + from h11 and h12 have q1: "?Q cs1 s" by simp + from h21 and h22 have q2: "?Q cs2 s" by simp + have nq1: "\ ?Q cs1 []" by (simp add:wq_def) + have nq2: "\ ?Q cs2 []" by (simp add:wq_def) + from p_split [of "?Q cs1", OF q1 nq1] + obtain t1 where lt1: "t1 < length s" + and np1: "\ ?Q cs1 (moment t1 s)" + and nn1: "(\i'>t1. ?Q cs1 (moment i' s))" by auto + from p_split [of "?Q cs2", OF q2 nq2] + obtain t2 where lt2: "t2 < length s" + and np2: "\ ?Q cs2 (moment t2 s)" + and nn2: "(\i'>t2. ?Q cs2 (moment i' s))" by auto + { fix s cs + assume q: "?Q cs s" + have "thread \ runing s" + proof + assume "thread \ runing s" + hence " \cs. \ (thread \ set (wq_fun (schs s) cs) \ + thread \ hd (wq_fun (schs s) cs))" + by (unfold runing_def s_waiting_def readys_def, auto) + from this[rule_format, of cs] q + show False by (simp add: wq_def) + qed + } note q_not_runing = this + { fix t1 t2 cs1 cs2 + assume lt1: "t1 < length s" + and np1: "\ ?Q cs1 (moment t1 s)" + and nn1: "(\i'>t1. ?Q cs1 (moment i' s))" + and lt2: "t2 < length s" + and np2: "\ ?Q cs2 (moment t2 s)" + and nn2: "(\i'>t2. ?Q cs2 (moment i' s))" + and lt12: "t1 < t2" + let ?t3 = "Suc t2" + from lt2 have le_t3: "?t3 \ length s" by auto + from moment_plus [OF this] + obtain e where eq_m: "moment ?t3 s = e#moment t2 s" by auto + have "t2 < ?t3" by simp + from nn2 [rule_format, OF this] and eq_m + have h1: "thread \ set (wq (e#moment t2 s) cs2)" and + h2: "thread \ hd (wq (e#moment t2 s) cs2)" by auto + have "vt (e#moment t2 s)" + proof - + from vt_moment + have "vt (moment ?t3 s)" . + with eq_m show ?thesis by simp + qed + then interpret vt_e: valid_trace_e "moment t2 s" "e" + by (unfold_locales, auto, cases, simp) + have ?thesis + proof - + have "thread \ runing (moment t2 s)" + proof(cases "thread \ set (wq (moment t2 s) cs2)") + case True + have "e = V thread cs2" + proof - + have eq_th: "thread = hd (wq (moment t2 s) cs2)" + using True and np2 by auto + from vt_e.wq_out_inv[OF True this h2] + show ?thesis . + qed + thus ?thesis using vt_e.actor_inv[OF vt_e.pip_e] by auto + next + case False + have "e = P thread cs2" using vt_e.wq_in_inv[OF False h1] . + with vt_e.actor_inv[OF vt_e.pip_e] + show ?thesis by auto + qed + moreover have "thread \ runing (moment t2 s)" + by (rule q_not_runing[OF nn1[rule_format, OF lt12]]) + ultimately show ?thesis by simp + qed + } note lt_case = this + show ?thesis + proof - + { assume "t1 < t2" + from lt_case[OF lt1 np1 nn1 lt2 np2 nn2 this] + have ?thesis . + } moreover { + assume "t2 < t1" + from lt_case[OF lt2 np2 nn2 lt1 np1 nn1 this] + have ?thesis . + } moreover { + assume eq_12: "t1 = t2" + let ?t3 = "Suc t2" + from lt2 have le_t3: "?t3 \ length s" by auto + from moment_plus [OF this] + obtain e where eq_m: "moment ?t3 s = e#moment t2 s" by auto + have lt_2: "t2 < ?t3" by simp + from nn2 [rule_format, OF this] and eq_m + have h1: "thread \ set (wq (e#moment t2 s) cs2)" and + h2: "thread \ hd (wq (e#moment t2 s) cs2)" by auto + from nn1[rule_format, OF lt_2[folded eq_12]] eq_m[folded eq_12] + have g1: "thread \ set (wq (e#moment t1 s) cs1)" and + g2: "thread \ hd (wq (e#moment t1 s) cs1)" by auto + have "vt (e#moment t2 s)" + proof - + from vt_moment + have "vt (moment ?t3 s)" . + with eq_m show ?thesis by simp + qed + then interpret vt_e: valid_trace_e "moment t2 s" "e" + by (unfold_locales, auto, cases, simp) + have "e = V thread cs2 \ e = P thread cs2" + proof(cases "thread \ set (wq (moment t2 s) cs2)") + case True + have "e = V thread cs2" + proof - + have eq_th: "thread = hd (wq (moment t2 s) cs2)" + using True and np2 by auto + from vt_e.wq_out_inv[OF True this h2] + show ?thesis . + qed + thus ?thesis by auto + next + case False + have "e = P thread cs2" using vt_e.wq_in_inv[OF False h1] . + thus ?thesis by auto + qed + moreover have "e = V thread cs1 \ e = P thread cs1" + proof(cases "thread \ set (wq (moment t1 s) cs1)") + case True + have eq_th: "thread = hd (wq (moment t1 s) cs1)" + using True and np1 by auto + from vt_e.wq_out_inv[folded eq_12, OF True this g2] + have "e = V thread cs1" . + thus ?thesis by auto + next + case False + have "e = P thread cs1" using vt_e.wq_in_inv[folded eq_12, OF False g1] . + thus ?thesis by auto + qed + ultimately have ?thesis using neq12 by auto + } ultimately show ?thesis using nat_neq_iff by blast + qed +qed + +text {* + This lemma is a simple corrolary of @{text "waiting_unique_pre"}. +*} + +lemma waiting_unique: + assumes "waiting s th cs1" + and "waiting s th cs2" + shows "cs1 = cs2" + using waiting_unique_pre assms + unfolding wq_def s_waiting_def + by auto + +end + +(* not used *) +text {* + Every thread can only be blocked on one critical resource, + symmetrically, every critical resource can only be held by one thread. + This fact is much more easier according to our definition. +*} +lemma held_unique: + assumes "holding (s::event list) th1 cs" + and "holding s th2 cs" + shows "th1 = th2" + by (insert assms, unfold s_holding_def, auto) + +lemma last_set_lt: "th \ threads s \ last_set th s < length s" + apply (induct s, auto) + by (case_tac a, auto split:if_splits) + +lemma last_set_unique: + "\last_set th1 s = last_set th2 s; th1 \ threads s; th2 \ threads s\ + \ th1 = th2" + apply (induct s, auto) + by (case_tac a, auto split:if_splits dest:last_set_lt) + +lemma preced_unique : + assumes pcd_eq: "preced th1 s = preced th2 s" + and th_in1: "th1 \ threads s" + and th_in2: " th2 \ threads s" + shows "th1 = th2" +proof - + from pcd_eq have "last_set th1 s = last_set th2 s" by (simp add:preced_def) + from last_set_unique [OF this th_in1 th_in2] + show ?thesis . +qed + +lemma preced_linorder: + assumes neq_12: "th1 \ th2" + and th_in1: "th1 \ threads s" + and th_in2: " th2 \ threads s" + shows "preced th1 s < preced th2 s \ preced th1 s > preced th2 s" +proof - + from preced_unique [OF _ th_in1 th_in2] and neq_12 + have "preced th1 s \ preced th2 s" by auto + thus ?thesis by auto +qed + +text {* + The following three lemmas show that @{text "RAG"} does not change + by the happening of @{text "Set"}, @{text "Create"} and @{text "Exit"} + events, respectively. +*} + +lemma RAG_set_unchanged: "(RAG (Set th prio # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + +lemma (in valid_trace_set) + RAG_unchanged: "(RAG (e # s)) = RAG s" + by (unfold is_set RAG_set_unchanged, simp) + +lemma RAG_create_unchanged: "(RAG (Create th prio # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + +lemma (in valid_trace_create) + RAG_unchanged: "(RAG (e # s)) = RAG s" + by (unfold is_create RAG_create_unchanged, simp) + +lemma RAG_exit_unchanged: "(RAG (Exit th # s)) = RAG s" +apply (unfold s_RAG_def s_waiting_def wq_def) +by (simp add:Let_def) + +lemma (in valid_trace_exit) + RAG_unchanged: "(RAG (e # s)) = RAG s" + by (unfold is_exit RAG_exit_unchanged, simp) + +context valid_trace_v +begin + +lemma distinct_rest: "distinct rest" + by (simp add: distinct_tl rest_def wq_distinct) + +lemma holding_cs_eq_th: + assumes "holding s t cs" + shows "t = th" +proof - + from pip_e[unfolded is_v] + show ?thesis + proof(cases) + case (thread_V) + from held_unique[OF this(2) assms] + show ?thesis by simp + qed +qed + +lemma distinct_wq': "distinct wq'" + by (metis (mono_tags, lifting) distinct_rest some_eq_ex wq'_def) + +lemma set_wq': "set wq' = set rest" + by (metis (mono_tags, lifting) distinct_rest rest_def + some_eq_ex wq'_def) + +lemma th'_in_inv: + assumes "th' \ set wq'" + shows "th' \ set rest" + using assms set_wq' by simp + +lemma neq_t_th: + assumes "waiting (e#s) t c" + shows "t \ th" +proof + assume otherwise: "t = th" + show False + proof(cases "c = cs") + case True + have "t \ set wq'" + using assms[unfolded True s_waiting_def, folded wq_def, unfolded wq_es_cs] + by simp + from th'_in_inv[OF this] have "t \ set rest" . + with wq_s_cs[folded otherwise] wq_distinct[of cs] + show ?thesis by simp + next + case False + have "wq (e#s) c = wq s c" using False + by (unfold is_v, simp) + hence "waiting s t c" using assms + by (simp add: cs_waiting_def waiting_eq) + hence "t \ readys s" by (unfold readys_def, auto) + hence "t \ runing s" using runing_ready by auto + with runing_th_s[folded otherwise] show ?thesis by auto + qed +qed + +lemma waiting_esI1: + assumes "waiting s t c" + and "c \ cs" + shows "waiting (e#s) t c" +proof - + have "wq (e#s) c = wq s c" + using assms(2) is_v by auto + with assms(1) show ?thesis + using cs_waiting_def waiting_eq by auto +qed + +lemma holding_esI2: + assumes "c \ cs" + and "holding s t c" + shows "holding (e#s) t c" +proof - + from assms(1) have "wq (e#s) c = wq s c" using is_v by auto + from assms(2)[unfolded s_holding_def, folded wq_def, + folded this, unfolded wq_def, folded s_holding_def] + show ?thesis . +qed + +lemma holding_esI1: + assumes "holding s t c" + and "t \ th" + shows "holding (e#s) t c" +proof - + have "c \ cs" using assms using holding_cs_eq_th by blast + from holding_esI2[OF this assms(1)] + show ?thesis . +qed + +end + +context valid_trace_v_n +begin + +lemma neq_wq': "wq' \ []" +proof (unfold wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) +next + fix x + assume " distinct x \ set x = set rest" + thus "x \ []" using rest_nnl by auto +qed + +definition "taker = hd wq'" + +definition "rest' = tl wq'" + +lemma eq_wq': "wq' = taker # rest'" + by (simp add: neq_wq' rest'_def taker_def) + +lemma next_th_taker: + shows "next_th s th cs taker" + using rest_nnl taker_def wq'_def wq_s_cs + by (auto simp:next_th_def) + +lemma taker_unique: + assumes "next_th s th cs taker'" + shows "taker' = taker" +proof - + from assms + obtain rest' where + h: "wq s cs = th # rest'" + "taker' = hd (SOME q. distinct q \ set q = set rest')" + by (unfold next_th_def, auto) + with wq_s_cs have "rest' = rest" by auto + thus ?thesis using h(2) taker_def wq'_def by auto +qed + +lemma waiting_set_eq: + "{(Th th', Cs cs) |th'. next_th s th cs th'} = {(Th taker, Cs cs)}" + by (smt all_not_in_conv bot.extremum insertI1 insert_subset + mem_Collect_eq next_th_taker subsetI subset_antisym taker_def taker_unique) + +lemma holding_set_eq: + "{(Cs cs, Th th') |th'. next_th s th cs th'} = {(Cs cs, Th taker)}" + using next_th_taker taker_def waiting_set_eq + by fastforce + +lemma holding_taker: + shows "holding (e#s) taker cs" + by (unfold s_holding_def, fold wq_def, unfold wq_es_cs, + auto simp:neq_wq' taker_def) + +lemma waiting_esI2: + assumes "waiting s t cs" + and "t \ taker" + shows "waiting (e#s) t cs" +proof - + have "t \ set wq'" + proof(unfold wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) + next + fix x + assume "distinct x \ set x = set rest" + moreover have "t \ set rest" + using assms(1) cs_waiting_def waiting_eq wq_s_cs by auto + ultimately show "t \ set x" by simp + qed + moreover have "t \ hd wq'" + using assms(2) taker_def by auto + ultimately show ?thesis + by (unfold s_waiting_def, fold wq_def, unfold wq_es_cs, simp) +qed + +lemma waiting_esE: + assumes "waiting (e#s) t c" + obtains "c \ cs" "waiting s t c" + | "c = cs" "t \ taker" "waiting s t cs" "t \ set rest'" +proof(cases "c = cs") + case False + hence "wq (e#s) c = wq s c" using is_v by auto + with assms have "waiting s t c" using cs_waiting_def waiting_eq by auto + from that(1)[OF False this] show ?thesis . +next + case True + from assms[unfolded s_waiting_def True, folded wq_def, unfolded wq_es_cs] + have "t \ hd wq'" "t \ set wq'" by auto + hence "t \ taker" by (simp add: taker_def) + moreover hence "t \ th" using assms neq_t_th by blast + moreover have "t \ set rest" by (simp add: `t \ set wq'` th'_in_inv) + ultimately have "waiting s t cs" + by (metis cs_waiting_def list.distinct(2) list.sel(1) + list.set_sel(2) rest_def waiting_eq wq_s_cs) + show ?thesis using that(2) + using True `t \ set wq'` `t \ taker` `waiting s t cs` eq_wq' by auto +qed + +lemma holding_esI1: + assumes "c = cs" + and "t = taker" + shows "holding (e#s) t c" + by (unfold assms, simp add: holding_taker) + +lemma holding_esE: + assumes "holding (e#s) t c" + obtains "c = cs" "t = taker" + | "c \ cs" "holding s t c" +proof(cases "c = cs") + case True + from assms[unfolded True, unfolded s_holding_def, + folded wq_def, unfolded wq_es_cs] + have "t = taker" by (simp add: taker_def) + from that(1)[OF True this] show ?thesis . +next + case False + hence "wq (e#s) c = wq s c" using is_v by auto + from assms[unfolded s_holding_def, folded wq_def, + unfolded this, unfolded wq_def, folded s_holding_def] + have "holding s t c" . + from that(2)[OF False this] show ?thesis . +qed + +end + + +context valid_trace_v_e +begin + +lemma nil_wq': "wq' = []" +proof (unfold wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) +next + fix x + assume " distinct x \ set x = set rest" + thus "x = []" using rest_nil by auto +qed + +lemma no_taker: + assumes "next_th s th cs taker" + shows "False" +proof - + from assms[unfolded next_th_def] + obtain rest' where "wq s cs = th # rest'" "rest' \ []" + by auto + thus ?thesis using rest_def rest_nil by auto +qed + +lemma waiting_set_eq: + "{(Th th', Cs cs) |th'. next_th s th cs th'} = {}" + using no_taker by auto + +lemma holding_set_eq: + "{(Cs cs, Th th') |th'. next_th s th cs th'} = {}" + using no_taker by auto + +lemma no_holding: + assumes "holding (e#s) taker cs" + shows False +proof - + from wq_es_cs[unfolded nil_wq'] + have " wq (e # s) cs = []" . + from assms[unfolded s_holding_def, folded wq_def, unfolded this] + show ?thesis by auto +qed + +lemma no_waiting: + assumes "waiting (e#s) t cs" + shows False +proof - + from wq_es_cs[unfolded nil_wq'] + have " wq (e # s) cs = []" . + from assms[unfolded s_waiting_def, folded wq_def, unfolded this] + show ?thesis by auto +qed + +lemma waiting_esI2: + assumes "waiting s t c" + shows "waiting (e#s) t c" +proof - + have "c \ cs" using assms + using cs_waiting_def rest_nil waiting_eq wq_s_cs by auto + from waiting_esI1[OF assms this] + show ?thesis . +qed + +lemma waiting_esE: + assumes "waiting (e#s) t c" + obtains "c \ cs" "waiting s t c" +proof(cases "c = cs") + case False + hence "wq (e#s) c = wq s c" using is_v by auto + with assms have "waiting s t c" using cs_waiting_def waiting_eq by auto + from that(1)[OF False this] show ?thesis . +next + case True + from no_waiting[OF assms[unfolded True]] + show ?thesis by auto +qed + +lemma holding_esE: + assumes "holding (e#s) t c" + obtains "c \ cs" "holding s t c" +proof(cases "c = cs") + case True + from no_holding[OF assms[unfolded True]] + show ?thesis by auto +next + case False + hence "wq (e#s) c = wq s c" using is_v by auto + from assms[unfolded s_holding_def, folded wq_def, + unfolded this, unfolded wq_def, folded s_holding_def] + have "holding s t c" . + from that[OF False this] show ?thesis . +qed + +end + +lemma rel_eqI: + assumes "\ x y. (x,y) \ A \ (x,y) \ B" + and "\ x y. (x,y) \ B \ (x, y) \ A" + shows "A = B" + using assms by auto + +lemma in_RAG_E: + assumes "(n1, n2) \ RAG (s::state)" + obtains (waiting) th cs where "n1 = Th th" "n2 = Cs cs" "waiting s th cs" + | (holding) th cs where "n1 = Cs cs" "n2 = Th th" "holding s th cs" + using assms[unfolded s_RAG_def, folded waiting_eq holding_eq] + by auto + +context valid_trace_v +begin + +lemma RAG_es: + "RAG (e # s) = + RAG s - {(Cs cs, Th th)} - + {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'}" (is "?L = ?R") +proof(rule rel_eqI) + fix n1 n2 + assume "(n1, n2) \ ?L" + thus "(n1, n2) \ ?R" + proof(cases rule:in_RAG_E) + case (waiting th' cs') + show ?thesis + proof(cases "rest = []") + case False + interpret h_n: valid_trace_v_n s e th cs + by (unfold_locales, insert False, simp) + from waiting(3) + show ?thesis + proof(cases rule:h_n.waiting_esE) + case 1 + with waiting(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + next + case 2 + with waiting(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + qed + next + case True + interpret h_e: valid_trace_v_e s e th cs + by (unfold_locales, insert True, simp) + from waiting(3) + show ?thesis + proof(cases rule:h_e.waiting_esE) + case 1 + with waiting(1,2) + show ?thesis + by (unfold h_e.waiting_set_eq h_e.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + qed + qed + next + case (holding th' cs') + show ?thesis + proof(cases "rest = []") + case False + interpret h_n: valid_trace_v_n s e th cs + by (unfold_locales, insert False, simp) + from holding(3) + show ?thesis + proof(cases rule:h_n.holding_esE) + case 1 + with holding(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold waiting_eq, auto) + next + case 2 + with holding(1,2) + show ?thesis + by (unfold h_n.waiting_set_eq h_n.holding_set_eq s_RAG_def, + fold holding_eq, auto) + qed + next + case True + interpret h_e: valid_trace_v_e s e th cs + by (unfold_locales, insert True, simp) + from holding(3) + show ?thesis + proof(cases rule:h_e.holding_esE) + case 1 + with holding(1,2) + show ?thesis + by (unfold h_e.waiting_set_eq h_e.holding_set_eq s_RAG_def, + fold holding_eq, auto) + qed + qed + qed +next + fix n1 n2 + assume h: "(n1, n2) \ ?R" + show "(n1, n2) \ ?L" + proof(cases "rest = []") + case False + interpret h_n: valid_trace_v_n s e th cs + by (unfold_locales, insert False, simp) + from h[unfolded h_n.waiting_set_eq h_n.holding_set_eq] + have "((n1, n2) \ RAG s \ (n1 \ Cs cs \ n2 \ Th th) + \ (n1 \ Th h_n.taker \ n2 \ Cs cs)) \ + (n2 = Th h_n.taker \ n1 = Cs cs)" + by auto + thus ?thesis + proof + assume "n2 = Th h_n.taker \ n1 = Cs cs" + with h_n.holding_taker + show ?thesis + by (unfold s_RAG_def, fold holding_eq, auto) + next + assume h: "(n1, n2) \ RAG s \ + (n1 \ Cs cs \ n2 \ Th th) \ (n1 \ Th h_n.taker \ n2 \ Cs cs)" + hence "(n1, n2) \ RAG s" by simp + thus ?thesis + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from h and this(1,2) + have "th' \ h_n.taker \ cs' \ cs" by auto + hence "waiting (e#s) th' cs'" + proof + assume "cs' \ cs" + from waiting_esI1[OF waiting(3) this] + show ?thesis . + next + assume neq_th': "th' \ h_n.taker" + show ?thesis + proof(cases "cs' = cs") + case False + from waiting_esI1[OF waiting(3) this] + show ?thesis . + next + case True + from h_n.waiting_esI2[OF waiting(3)[unfolded True] neq_th', folded True] + show ?thesis . + qed + qed + thus ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case (holding th' cs') + from h this(1,2) + have "cs' \ cs \ th' \ th" by auto + hence "holding (e#s) th' cs'" + proof + assume "cs' \ cs" + from holding_esI2[OF this holding(3)] + show ?thesis . + next + assume "th' \ th" + from holding_esI1[OF holding(3) this] + show ?thesis . + qed + thus ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + qed + qed + next + case True + interpret h_e: valid_trace_v_e s e th cs + by (unfold_locales, insert True, simp) + from h[unfolded h_e.waiting_set_eq h_e.holding_set_eq] + have h_s: "(n1, n2) \ RAG s" "(n1, n2) \ (Cs cs, Th th)" + by auto + from h_s(1) + show ?thesis + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from h_e.waiting_esI2[OF this(3)] + show ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case (holding th' cs') + with h_s(2) + have "cs' \ cs \ th' \ th" by auto + thus ?thesis + proof + assume neq_cs: "cs' \ cs" + from holding_esI2[OF this holding(3)] + show ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + next + assume "th' \ th" + from holding_esI1[OF holding(3) this] + show ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + qed + qed + qed +qed + +end + +lemma step_RAG_v: +assumes vt: + "vt (V th cs#s)" +shows " + RAG (V th cs # s) = + RAG s - {(Cs cs, Th th)} - + {(Th th', Cs cs) |th'. next_th s th cs th'} \ + {(Cs cs, Th th') |th'. next_th s th cs th'}" (is "?L = ?R") +proof - + interpret vt_v: valid_trace_v s "V th cs" + using assms step_back_vt by (unfold_locales, auto) + show ?thesis using vt_v.RAG_es . +qed + +lemma (in valid_trace_create) + th_not_in_threads: "th \ threads s" +proof - + from pip_e[unfolded is_create] + show ?thesis by (cases, simp) +qed + +lemma (in valid_trace_create) + threads_es [simp]: "threads (e#s) = threads s \ {th}" + by (unfold is_create, simp) + +lemma (in valid_trace_exit) + threads_es [simp]: "threads (e#s) = threads s - {th}" + by (unfold is_exit, simp) + +lemma (in valid_trace_p) + threads_es [simp]: "threads (e#s) = threads s" + by (unfold is_p, simp) + +lemma (in valid_trace_v) + threads_es [simp]: "threads (e#s) = threads s" + by (unfold is_v, simp) + +lemma (in valid_trace_v) + th_not_in_rest[simp]: "th \ set rest" +proof + assume otherwise: "th \ set rest" + have "distinct (wq s cs)" by (simp add: wq_distinct) + from this[unfolded wq_s_cs] and otherwise + show False by auto +qed + +lemma (in valid_trace_v) + set_wq_es_cs [simp]: "set (wq (e#s) cs) = set (wq s cs) - {th}" +proof(unfold wq_es_cs wq'_def, rule someI2) + show "distinct rest \ set rest = set rest" + by (simp add: distinct_rest) +next + fix x + assume "distinct x \ set x = set rest" + thus "set x = set (wq s cs) - {th}" + by (unfold wq_s_cs, simp) +qed + +lemma (in valid_trace_exit) + th_not_in_wq: "th \ set (wq s cs)" +proof - + from pip_e[unfolded is_exit] + show ?thesis + by (cases, unfold holdents_def s_holding_def, fold wq_def, + auto elim!:runing_wqE) +qed + +lemma (in valid_trace) wq_threads: + assumes "th \ set (wq s cs)" + shows "th \ threads s" + using assms +proof(induct rule:ind) + case (Nil) + thus ?case by (auto simp:wq_def) +next + case (Cons s e) + interpret vt_e: valid_trace_e s e using Cons by simp + show ?case + proof(cases e) + case (Create th' prio') + interpret vt: valid_trace_create s e th' prio' + using Create by (unfold_locales, simp) + show ?thesis + using Cons.hyps(2) Cons.prems by auto + next + case (Exit th') + interpret vt: valid_trace_exit s e th' + using Exit by (unfold_locales, simp) + show ?thesis + using Cons.hyps(2) Cons.prems vt.th_not_in_wq by auto + next + case (P th' cs') + interpret vt: valid_trace_p s e th' cs' + using P by (unfold_locales, simp) + show ?thesis + using Cons.hyps(2) Cons.prems readys_threads + runing_ready vt.is_p vt.runing_th_s vt_e.wq_in_inv + by fastforce + next + case (V th' cs') + interpret vt: valid_trace_v s e th' cs' + using V by (unfold_locales, simp) + show ?thesis using Cons + using vt.is_v vt.threads_es vt_e.wq_in_inv by blast + next + case (Set th' prio) + interpret vt: valid_trace_set s e th' prio + using Set by (unfold_locales, simp) + show ?thesis using Cons.hyps(2) Cons.prems vt.is_set + by (auto simp:wq_def Let_def) + qed +qed + +context valid_trace +begin + +lemma dm_RAG_threads: + assumes in_dom: "(Th th) \ Domain (RAG s)" + shows "th \ threads s" +proof - + from in_dom obtain n where "(Th th, n) \ RAG s" by auto + moreover from RAG_target_th[OF this] obtain cs where "n = Cs cs" by auto + ultimately have "(Th th, Cs cs) \ RAG s" by simp + hence "th \ set (wq s cs)" + by (unfold s_RAG_def, auto simp:cs_waiting_def) + from wq_threads [OF this] show ?thesis . +qed + +lemma rg_RAG_threads: + assumes "(Th th) \ Range (RAG s)" + shows "th \ threads s" + using assms + by (unfold s_RAG_def cs_waiting_def cs_holding_def, + auto intro:wq_threads) + +end + + + + +lemma preced_v [simp]: "preced th' (V th cs#s) = preced th' s" + by (unfold preced_def, simp) + +lemma (in valid_trace_v) + preced_es: "preced th (e#s) = preced th s" + by (unfold is_v preced_def, simp) + +lemma the_preced_v[simp]: "the_preced (V th cs#s) = the_preced s" +proof + fix th' + show "the_preced (V th cs # s) th' = the_preced s th'" + by (unfold the_preced_def preced_def, simp) +qed + +lemma (in valid_trace_v) + the_preced_es: "the_preced (e#s) = the_preced s" + by (unfold is_v preced_def, simp) + +context valid_trace_p +begin + +lemma not_holding_s_th_cs: "\ holding s th cs" +proof + assume otherwise: "holding s th cs" + from pip_e[unfolded is_p] + show False + proof(cases) + case (thread_P) + moreover have "(Cs cs, Th th) \ RAG s" + using otherwise cs_holding_def + holding_eq th_not_in_wq by auto + ultimately show ?thesis by auto + qed +qed + +lemma waiting_kept: + assumes "waiting s th' cs'" + shows "waiting (e#s) th' cs'" + using assms + by (metis cs_waiting_def hd_append2 list.sel(1) list.set_intros(2) + rotate1.simps(2) self_append_conv2 set_rotate1 + th_not_in_wq waiting_eq wq_es_cs wq_neq_simp) + +lemma holding_kept: + assumes "holding s th' cs'" + shows "holding (e#s) th' cs'" +proof(cases "cs' = cs") + case False + hence "wq (e#s) cs' = wq s cs'" by simp + with assms show ?thesis using cs_holding_def holding_eq by auto +next + case True + from assms[unfolded s_holding_def, folded wq_def] + obtain rest where eq_wq: "wq s cs' = th'#rest" + by (metis empty_iff list.collapse list.set(1)) + hence "wq (e#s) cs' = th'#(rest@[th])" + by (simp add: True wq_es_cs) + thus ?thesis + by (simp add: cs_holding_def holding_eq) +qed + +end + +locale valid_trace_p_h = valid_trace_p + + assumes we: "wq s cs = []" + +locale valid_trace_p_w = valid_trace_p + + assumes wne: "wq s cs \ []" +begin + +definition "holder = hd (wq s cs)" +definition "waiters = tl (wq s cs)" +definition "waiters' = waiters @ [th]" + +lemma wq_s_cs: "wq s cs = holder#waiters" + by (simp add: holder_def waiters_def wne) + +lemma wq_es_cs': "wq (e#s) cs = holder#waiters@[th]" + by (simp add: wq_es_cs wq_s_cs) + +lemma waiting_es_th_cs: "waiting (e#s) th cs" + using cs_waiting_def th_not_in_wq waiting_eq wq_es_cs' wq_s_cs by auto + +lemma RAG_edge: "(Th th, Cs cs) \ RAG (e#s)" + by (unfold s_RAG_def, fold waiting_eq, insert waiting_es_th_cs, auto) + +lemma holding_esE: + assumes "holding (e#s) th' cs'" + obtains "holding s th' cs'" + using assms +proof(cases "cs' = cs") + case False + hence "wq (e#s) cs' = wq s cs'" by simp + with assms show ?thesis + using cs_holding_def holding_eq that by auto +next + case True + with assms show ?thesis + by (metis cs_holding_def holding_eq list.sel(1) list.set_intros(1) that + wq_es_cs' wq_s_cs) +qed + +lemma waiting_esE: + assumes "waiting (e#s) th' cs'" + obtains "th' \ th" "waiting s th' cs'" + | "th' = th" "cs' = cs" +proof(cases "waiting s th' cs'") + case True + have "th' \ th" + proof + assume otherwise: "th' = th" + from True[unfolded this] + show False by (simp add: th_not_waiting) + qed + from that(1)[OF this True] show ?thesis . +next + case False + hence "th' = th \ cs' = cs" + by (metis assms cs_waiting_def holder_def list.sel(1) rotate1.simps(2) + set_ConsD set_rotate1 waiting_eq wq_es_cs wq_es_cs' wq_neq_simp) + with that(2) show ?thesis by metis +qed + +lemma RAG_es: "RAG (e # s) = RAG s \ {(Th th, Cs cs)}" (is "?L = ?R") +proof(rule rel_eqI) + fix n1 n2 + assume "(n1, n2) \ ?L" + thus "(n1, n2) \ ?R" + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from this(3) + show ?thesis + proof(cases rule:waiting_esE) + case 1 + thus ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case 2 + thus ?thesis using waiting(1,2) by auto + qed + next + case (holding th' cs') + from this(3) + show ?thesis + proof(cases rule:holding_esE) + case 1 + with holding(1,2) + show ?thesis by (unfold s_RAG_def, fold holding_eq, auto) + qed + qed +next + fix n1 n2 + assume "(n1, n2) \ ?R" + hence "(n1, n2) \ RAG s \ (n1 = Th th \ n2 = Cs cs)" by auto + thus "(n1, n2) \ ?L" + proof + assume "(n1, n2) \ RAG s" + thus ?thesis + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from waiting_kept[OF this(3)] + show ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case (holding th' cs') + from holding_kept[OF this(3)] + show ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + qed + next + assume "n1 = Th th \ n2 = Cs cs" + thus ?thesis using RAG_edge by auto + qed +qed + +end + +context valid_trace_p_h +begin + +lemma wq_es_cs': "wq (e#s) cs = [th]" + using wq_es_cs[unfolded we] by simp + +lemma holding_es_th_cs: + shows "holding (e#s) th cs" +proof - + from wq_es_cs' + have "th \ set (wq (e#s) cs)" "th = hd (wq (e#s) cs)" by auto + thus ?thesis using cs_holding_def holding_eq by blast +qed + +lemma RAG_edge: "(Cs cs, Th th) \ RAG (e#s)" + by (unfold s_RAG_def, fold holding_eq, insert holding_es_th_cs, auto) + +lemma waiting_esE: + assumes "waiting (e#s) th' cs'" + obtains "waiting s th' cs'" + using assms + by (metis cs_waiting_def event.distinct(15) is_p list.sel(1) + set_ConsD waiting_eq we wq_es_cs' wq_neq_simp wq_out_inv) + +lemma holding_esE: + assumes "holding (e#s) th' cs'" + obtains "cs' \ cs" "holding s th' cs'" + | "cs' = cs" "th' = th" +proof(cases "cs' = cs") + case True + from held_unique[OF holding_es_th_cs assms[unfolded True]] + have "th' = th" by simp + from that(2)[OF True this] show ?thesis . +next + case False + have "holding s th' cs'" using assms + using False cs_holding_def holding_eq by auto + from that(1)[OF False this] show ?thesis . +qed + +lemma RAG_es: "RAG (e # s) = RAG s \ {(Cs cs, Th th)}" (is "?L = ?R") +proof(rule rel_eqI) + fix n1 n2 + assume "(n1, n2) \ ?L" + thus "(n1, n2) \ ?R" + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from this(3) + show ?thesis + proof(cases rule:waiting_esE) + case 1 + thus ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + qed + next + case (holding th' cs') + from this(3) + show ?thesis + proof(cases rule:holding_esE) + case 1 + with holding(1,2) + show ?thesis by (unfold s_RAG_def, fold holding_eq, auto) + next + case 2 + with holding(1,2) show ?thesis by auto + qed + qed +next + fix n1 n2 + assume "(n1, n2) \ ?R" + hence "(n1, n2) \ RAG s \ (n1 = Cs cs \ n2 = Th th)" by auto + thus "(n1, n2) \ ?L" + proof + assume "(n1, n2) \ RAG s" + thus ?thesis + proof(cases rule:in_RAG_E) + case (waiting th' cs') + from waiting_kept[OF this(3)] + show ?thesis using waiting(1,2) + by (unfold s_RAG_def, fold waiting_eq, auto) + next + case (holding th' cs') + from holding_kept[OF this(3)] + show ?thesis using holding(1,2) + by (unfold s_RAG_def, fold holding_eq, auto) + qed + next + assume "n1 = Cs cs \ n2 = Th th" + with holding_es_th_cs + show ?thesis + by (unfold s_RAG_def, fold holding_eq, auto) + qed +qed + +end + +context valid_trace_p +begin + +lemma RAG_es': "RAG (e # s) = (if (wq s cs = []) then RAG s \ {(Cs cs, Th th)} + else RAG s \ {(Th th, Cs cs)})" +proof(cases "wq s cs = []") + case True + interpret vt_p: valid_trace_p_h using True + by (unfold_locales, simp) + show ?thesis by (simp add: vt_p.RAG_es vt_p.we) +next + case False + interpret vt_p: valid_trace_p_w using False + by (unfold_locales, simp) + show ?thesis by (simp add: vt_p.RAG_es vt_p.wne) +qed + +end + +lemma (in valid_trace_v_n) finite_waiting_set: + "finite {(Th th', Cs cs) |th'. next_th s th cs th'}" + by (simp add: waiting_set_eq) + +lemma (in valid_trace_v_n) finite_holding_set: + "finite {(Cs cs, Th th') |th'. next_th s th cs th'}" + by (simp add: holding_set_eq) + +lemma (in valid_trace_v_e) finite_waiting_set: + "finite {(Th th', Cs cs) |th'. next_th s th cs th'}" + by (simp add: waiting_set_eq) + +lemma (in valid_trace_v_e) finite_holding_set: + "finite {(Cs cs, Th th') |th'. next_th s th cs th'}" + by (simp add: holding_set_eq) + +context valid_trace_v +begin + +lemma + finite_RAG_kept: + assumes "finite (RAG s)" + shows "finite (RAG (e#s))" +proof(cases "rest = []") + case True + interpret vt: valid_trace_v_e using True + by (unfold_locales, simp) + show ?thesis using assms + by (unfold RAG_es vt.waiting_set_eq vt.holding_set_eq, simp) +next + case False + interpret vt: valid_trace_v_n using False + by (unfold_locales, simp) + show ?thesis using assms + by (unfold RAG_es vt.waiting_set_eq vt.holding_set_eq, simp) +qed + +end + +context valid_trace_v_e +begin + +lemma + acylic_RAG_kept: + assumes "acyclic (RAG s)" + shows "acyclic (RAG (e#s))" +proof(rule acyclic_subset[OF assms]) + show "RAG (e # s) \ RAG s" + by (unfold RAG_es waiting_set_eq holding_set_eq, auto) +qed + +end + +context valid_trace_v_n +begin + +lemma waiting_taker: "waiting s taker cs" + apply (unfold s_waiting_def, fold wq_def, unfold wq_s_cs taker_def) + using eq_wq' th'_in_inv wq'_def by fastforce + +lemma + acylic_RAG_kept: + assumes "acyclic (RAG s)" + shows "acyclic (RAG (e#s))" +proof - + have "acyclic ((RAG s - {(Cs cs, Th th)} - {(Th taker, Cs cs)}) \ + {(Cs cs, Th taker)})" (is "acyclic (?A \ _)") + proof - + from assms + have "acyclic ?A" + by (rule acyclic_subset, auto) + moreover have "(Th taker, Cs cs) \ ?A^*" + proof + assume otherwise: "(Th taker, Cs cs) \ ?A^*" + hence "(Th taker, Cs cs) \ ?A^+" + by (unfold rtrancl_eq_or_trancl, auto) + from tranclD[OF this] + obtain cs' where h: "(Th taker, Cs cs') \ ?A" + "(Th taker, Cs cs') \ RAG s" + by (unfold s_RAG_def, auto) + from this(2) have "waiting s taker cs'" + by (unfold s_RAG_def, fold waiting_eq, auto) + from waiting_unique[OF this waiting_taker] + have "cs' = cs" . + from h(1)[unfolded this] show False by auto + qed + ultimately show ?thesis by auto + qed + thus ?thesis + by (unfold RAG_es waiting_set_eq holding_set_eq, simp) +qed + +end + +context valid_trace_p_h +begin + +lemma + acylic_RAG_kept: + assumes "acyclic (RAG s)" + shows "acyclic (RAG (e#s))" +proof - + have "acyclic (RAG s \ {(Cs cs, Th th)})" (is "acyclic (?A \ _)") + proof - + from assms + have "acyclic ?A" + by (rule acyclic_subset, auto) + moreover have "(Th th, Cs cs) \ ?A^*" + proof + assume otherwise: "(Th th, Cs cs) \ ?A^*" + hence "(Th th, Cs cs) \ ?A^+" + by (unfold rtrancl_eq_or_trancl, auto) + from tranclD[OF this] + obtain cs' where h: "(Th th, Cs cs') \ RAG s" + by (unfold s_RAG_def, auto) + hence "waiting s th cs'" + by (unfold s_RAG_def, fold waiting_eq, auto) + with th_not_waiting show False by auto + qed + ultimately show ?thesis by auto + qed + thus ?thesis by (unfold RAG_es, simp) +qed + +end + +context valid_trace_p_w +begin + +lemma + acylic_RAG_kept: + assumes "acyclic (RAG s)" + shows "acyclic (RAG (e#s))" +proof - + have "acyclic (RAG s \ {(Th th, Cs cs)})" (is "acyclic (?A \ _)") + proof - + from assms + have "acyclic ?A" + by (rule acyclic_subset, auto) + moreover have "(Cs cs, Th th) \ ?A^*" + proof + assume otherwise: "(Cs cs, Th th) \ ?A^*" + from pip_e[unfolded is_p] + show False + proof(cases) + case (thread_P) + moreover from otherwise have "(Cs cs, Th th) \ ?A^+" + by (unfold rtrancl_eq_or_trancl, auto) + ultimately show ?thesis by auto + qed + qed + ultimately show ?thesis by auto + qed + thus ?thesis by (unfold RAG_es, simp) +qed + +end + +context valid_trace +begin + +lemma finite_RAG: + shows "finite (RAG s)" +proof(induct rule:ind) + case Nil + show ?case + by (auto simp: s_RAG_def cs_waiting_def + cs_holding_def wq_def acyclic_def) +next + case (Cons s e) + interpret vt_e: valid_trace_e s e using Cons by simp + show ?case + proof(cases e) + case (Create th prio) + interpret vt: valid_trace_create s e th prio using Create + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt.RAG_unchanged) + next + case (Exit th) + interpret vt: valid_trace_exit s e th using Exit + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt.RAG_unchanged) + next + case (P th cs) + interpret vt: valid_trace_p s e th cs using P + by (unfold_locales, simp) + show ?thesis using Cons using vt.RAG_es' by auto + next + case (V th cs) + interpret vt: valid_trace_v s e th cs using V + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt.finite_RAG_kept) + next + case (Set th prio) + interpret vt: valid_trace_set s e th prio using Set + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt.RAG_unchanged) + qed +qed + +lemma acyclic_RAG: + shows "acyclic (RAG s)" +proof(induct rule:ind) + case Nil + show ?case + by (auto simp: s_RAG_def cs_waiting_def + cs_holding_def wq_def acyclic_def) +next + case (Cons s e) + interpret vt_e: valid_trace_e s e using Cons by simp + show ?case + proof(cases e) + case (Create th prio) + interpret vt: valid_trace_create s e th prio using Create + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt.RAG_unchanged) + next + case (Exit th) + interpret vt: valid_trace_exit s e th using Exit + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt.RAG_unchanged) + next + case (P th cs) + interpret vt: valid_trace_p s e th cs using P + by (unfold_locales, simp) + show ?thesis + proof(cases "wq s cs = []") + case True + then interpret vt_h: valid_trace_p_h s e th cs + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_h.acylic_RAG_kept) + next + case False + then interpret vt_w: valid_trace_p_w s e th cs + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_w.acylic_RAG_kept) + qed + next + case (V th cs) + interpret vt: valid_trace_v s e th cs using V + by (unfold_locales, simp) + show ?thesis + proof(cases "vt.rest = []") + case True + then interpret vt_e: valid_trace_v_e s e th cs + by (unfold_locales, simp) + show ?thesis by (simp add: Cons.hyps(2) vt_e.acylic_RAG_kept) + next + case False + then interpret vt_n: valid_trace_v_n s e th cs + by (unfold_locales, simp) + show ?thesis by (simp add: Cons.hyps(2) vt_n.acylic_RAG_kept) + qed + next + case (Set th prio) + interpret vt: valid_trace_set s e th prio using Set + by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt.RAG_unchanged) + qed +qed + +lemma wf_RAG: "wf (RAG s)" +proof(rule finite_acyclic_wf) + from finite_RAG show "finite (RAG s)" . +next + from acyclic_RAG show "acyclic (RAG s)" . +qed + +lemma sgv_wRAG: "single_valued (wRAG s)" + using waiting_unique + by (unfold single_valued_def wRAG_def, auto) + +lemma sgv_hRAG: "single_valued (hRAG s)" + using held_unique + by (unfold single_valued_def hRAG_def, auto) + +lemma sgv_tRAG: "single_valued (tRAG s)" + by (unfold tRAG_def, rule single_valued_relcomp, + insert sgv_wRAG sgv_hRAG, auto) + +lemma acyclic_tRAG: "acyclic (tRAG s)" +proof(unfold tRAG_def, rule acyclic_compose) + show "acyclic (RAG s)" using acyclic_RAG . +next + show "wRAG s \ RAG s" unfolding RAG_split by auto +next + show "hRAG s \ RAG s" unfolding RAG_split by auto +qed + +lemma unique_RAG: "\(n, n1) \ RAG s; (n, n2) \ RAG s\ \ n1 = n2" + apply(unfold s_RAG_def, auto, fold waiting_eq holding_eq) + by(auto elim:waiting_unique held_unique) + +lemma sgv_RAG: "single_valued (RAG s)" + using unique_RAG by (auto simp:single_valued_def) + +lemma rtree_RAG: "rtree (RAG s)" + using sgv_RAG acyclic_RAG + by (unfold rtree_def rtree_axioms_def sgv_def, auto) + +end + +sublocale valid_trace < fsbtRAGs : fsubtree "RAG s" +proof - + show "fsubtree (RAG s)" + proof(intro_locales) + show "fbranch (RAG s)" using finite_fbranchI[OF finite_RAG] . + next + show "fsubtree_axioms (RAG s)" + proof(unfold fsubtree_axioms_def) + from wf_RAG show "wf (RAG s)" . + qed + qed +qed + +context valid_trace +begin + +lemma finite_subtree_threads: + "finite {th'. Th th' \ subtree (RAG s) (Th th)}" (is "finite ?A") +proof - + have "?A = the_thread ` {Th th' | th' . Th th' \ subtree (RAG s) (Th th)}" + by (auto, insert image_iff, fastforce) + moreover have "finite {Th th' | th' . Th th' \ subtree (RAG s) (Th th)}" + (is "finite ?B") + proof - + have "?B = (subtree (RAG s) (Th th)) \ {Th th' | th'. True}" + by auto + moreover have "... \ (subtree (RAG s) (Th th))" by auto + moreover have "finite ..." by (simp add: finite_subtree) + ultimately show ?thesis by auto + qed + ultimately show ?thesis by auto +qed + +lemma le_cp: + shows "preced th s \ cp s th" + proof(unfold cp_alt_def, rule Max_ge) + show "finite (the_preced s ` {th'. Th th' \ subtree (RAG s) (Th th)})" + by (simp add: finite_subtree_threads) + next + show "preced th s \ the_preced s ` {th'. Th th' \ subtree (RAG s) (Th th)}" + by (simp add: subtree_def the_preced_def) + qed + +lemma cp_le: + assumes th_in: "th \ threads s" + shows "cp s th \ Max (the_preced s ` threads s)" +proof(unfold cp_alt_def, rule Max_f_mono) + show "finite (threads s)" by (simp add: finite_threads) +next + show " {th'. Th th' \ subtree (RAG s) (Th th)} \ {}" + using subtree_def by fastforce +next + show "{th'. Th th' \ subtree (RAG s) (Th th)} \ threads s" + using assms + by (smt Domain.DomainI dm_RAG_threads mem_Collect_eq + node.inject(1) rtranclD subsetI subtree_def trancl_domain) +qed + +lemma max_cp_eq: + shows "Max ((cp s) ` threads s) = Max (the_preced s ` threads s)" + (is "?L = ?R") +proof - + have "?L \ ?R" + proof(cases "threads s = {}") + case False + show ?thesis + by (rule Max.boundedI, + insert cp_le, + auto simp:finite_threads False) + qed auto + moreover have "?R \ ?L" + by (rule Max_fg_mono, + simp add: finite_threads, + simp add: le_cp the_preced_def) + ultimately show ?thesis by auto +qed + +lemma max_cp_eq_the_preced: + shows "Max ((cp s) ` threads s) = Max (the_preced s ` threads s)" + using max_cp_eq using the_preced_def by presburger + +lemma wf_RAG_converse: + shows "wf ((RAG s)^-1)" +proof(rule finite_acyclic_wf_converse) + from finite_RAG + show "finite (RAG s)" . +next + from acyclic_RAG + show "acyclic (RAG s)" . +qed + +lemma chain_building: + assumes "node \ Domain (RAG s)" + obtains th' where "th' \ readys s" "(node, Th th') \ (RAG s)^+" +proof - + from assms have "node \ Range ((RAG s)^-1)" by auto + from wf_base[OF wf_RAG_converse this] + obtain b where h_b: "(b, node) \ ((RAG s)\)\<^sup>+" "\c. (c, b) \ (RAG s)\" by auto + obtain th' where eq_b: "b = Th th'" + proof(cases b) + case (Cs cs) + from h_b(1)[unfolded trancl_converse] + have "(node, b) \ ((RAG s)\<^sup>+)" by auto + from tranclE[OF this] + obtain n where "(n, b) \ RAG s" by auto + from this[unfolded Cs] + obtain th1 where "waiting s th1 cs" + by (unfold s_RAG_def, fold waiting_eq, auto) + from waiting_holding[OF this] + obtain th2 where "holding s th2 cs" . + hence "(Cs cs, Th th2) \ RAG s" + by (unfold s_RAG_def, fold holding_eq, auto) + with h_b(2)[unfolded Cs, rule_format] + have False by auto + thus ?thesis by auto + qed auto + have "th' \ readys s" + proof - + from h_b(2)[unfolded eq_b] + have "\cs. \ waiting s th' cs" + by (unfold s_RAG_def, fold waiting_eq, auto) + moreover have "th' \ threads s" + proof(rule rg_RAG_threads) + from tranclD[OF h_b(1), unfolded eq_b] + obtain z where "(z, Th th') \ (RAG s)" by auto + thus "Th th' \ Range (RAG s)" by auto + qed + ultimately show ?thesis by (auto simp:readys_def) + qed + moreover have "(node, Th th') \ (RAG s)^+" + using h_b(1)[unfolded trancl_converse] eq_b by auto + ultimately show ?thesis using that by metis +qed + +text {* \noindent + The following is just an instance of @{text "chain_building"}. +*} +lemma th_chain_to_ready: + assumes th_in: "th \ threads s" + shows "th \ readys s \ (\ th'. th' \ readys s \ (Th th, Th th') \ (RAG s)^+)" +proof(cases "th \ readys s") + case True + thus ?thesis by auto +next + case False + from False and th_in have "Th th \ Domain (RAG s)" + by (auto simp:readys_def s_waiting_def s_RAG_def wq_def cs_waiting_def Domain_def) + from chain_building [rule_format, OF this] + show ?thesis by auto +qed + +end + +lemma count_rec1 [simp]: + assumes "Q e" + shows "count Q (e#es) = Suc (count Q es)" + using assms + by (unfold count_def, auto) + +lemma count_rec2 [simp]: + assumes "\Q e" + shows "count Q (e#es) = (count Q es)" + using assms + by (unfold count_def, auto) + +lemma count_rec3 [simp]: + shows "count Q [] = 0" + by (unfold count_def, auto) + +lemma cntP_simp1[simp]: + "cntP (P th cs'#s) th = cntP s th + 1" + by (unfold cntP_def, simp) + +lemma cntP_simp2[simp]: + assumes "th' \ th" + shows "cntP (P th cs'#s) th' = cntP s th'" + using assms + by (unfold cntP_def, simp) + +lemma cntP_simp3[simp]: + assumes "\ isP e" + shows "cntP (e#s) th' = cntP s th'" + using assms + by (unfold cntP_def, cases e, simp+) + +lemma cntV_simp1[simp]: + "cntV (V th cs'#s) th = cntV s th + 1" + by (unfold cntV_def, simp) + +lemma cntV_simp2[simp]: + assumes "th' \ th" + shows "cntV (V th cs'#s) th' = cntV s th'" + using assms + by (unfold cntV_def, simp) + +lemma cntV_simp3[simp]: + assumes "\ isV e" + shows "cntV (e#s) th' = cntV s th'" + using assms + by (unfold cntV_def, cases e, simp+) + +lemma cntP_diff_inv: + assumes "cntP (e#s) th \ cntP s th" + shows "isP e \ actor e = th" +proof(cases e) + case (P th' pty) + show ?thesis + by (cases "(\e. \cs. e = P th cs) (P th' pty)", + insert assms P, auto simp:cntP_def) +qed (insert assms, auto simp:cntP_def) + +lemma cntV_diff_inv: + assumes "cntV (e#s) th \ cntV s th" + shows "isV e \ actor e = th" +proof(cases e) + case (V th' pty) + show ?thesis + by (cases "(\e. \cs. e = V th cs) (V th' pty)", + insert assms V, auto simp:cntV_def) +qed (insert assms, auto simp:cntV_def) + +lemma children_RAG_alt_def: + "children (RAG (s::state)) (Th th) = Cs ` {cs. holding s th cs}" + by (unfold s_RAG_def, auto simp:children_def holding_eq) + +fun the_cs :: "node \ cs" where + "the_cs (Cs cs) = cs" + +lemma holdents_alt_def: + "holdents s th = the_cs ` (children (RAG (s::state)) (Th th))" + by (unfold children_RAG_alt_def holdents_def, simp add: image_image) + +lemma cntCS_alt_def: + "cntCS s th = card (children (RAG s) (Th th))" + apply (unfold children_RAG_alt_def cntCS_def holdents_def) + by (rule card_image[symmetric], auto simp:inj_on_def) + +context valid_trace +begin + +lemma finite_holdents: "finite (holdents s th)" + by (unfold holdents_alt_def, insert finite_children, auto) + +end + +context valid_trace_p_w +begin + +lemma holding_s_holder: "holding s holder cs" + by (unfold s_holding_def, fold wq_def, unfold wq_s_cs, auto) + +lemma holding_es_holder: "holding (e#s) holder cs" + by (unfold s_holding_def, fold wq_def, unfold wq_es_cs wq_s_cs, auto) + +lemma holdents_es: + shows "holdents (e#s) th' = holdents s th'" (is "?L = ?R") +proof - + { fix cs' + assume "cs' \ ?L" + hence h: "holding (e#s) th' cs'" by (auto simp:holdents_def) + have "holding s th' cs'" + proof(cases "cs' = cs") + case True + from held_unique[OF h[unfolded True] holding_es_holder] + have "th' = holder" . + thus ?thesis + by (unfold True holdents_def, insert holding_s_holder, simp) + next + case False + hence "wq (e#s) cs' = wq s cs'" by simp + from h[unfolded s_holding_def, folded wq_def, unfolded this] + show ?thesis + by (unfold s_holding_def, fold wq_def, auto) + qed + hence "cs' \ ?R" by (auto simp:holdents_def) + } moreover { + fix cs' + assume "cs' \ ?R" + hence h: "holding s th' cs'" by (auto simp:holdents_def) + have "holding (e#s) th' cs'" + proof(cases "cs' = cs") + case True + from held_unique[OF h[unfolded True] holding_s_holder] + have "th' = holder" . + thus ?thesis + by (unfold True holdents_def, insert holding_es_holder, simp) + next + case False + hence "wq s cs' = wq (e#s) cs'" by simp + from h[unfolded s_holding_def, folded wq_def, unfolded this] + show ?thesis + by (unfold s_holding_def, fold wq_def, auto) + qed + hence "cs' \ ?L" by (auto simp:holdents_def) + } ultimately show ?thesis by auto +qed + +lemma cntCS_es_th[simp]: "cntCS (e#s) th' = cntCS s th'" + by (unfold cntCS_def holdents_es, simp) + +lemma th_not_ready_es: + shows "th \ readys (e#s)" + using waiting_es_th_cs + by (unfold readys_def, auto) + +end + +context valid_trace_p_h +begin + +lemma th_not_waiting': + "\ waiting (e#s) th cs'" +proof(cases "cs' = cs") + case True + show ?thesis + by (unfold True s_waiting_def, fold wq_def, unfold wq_es_cs', auto) +next + case False + from th_not_waiting[of cs', unfolded s_waiting_def, folded wq_def] + show ?thesis + by (unfold s_waiting_def, fold wq_def, insert False, simp) +qed + +lemma ready_th_es: + shows "th \ readys (e#s)" + using th_not_waiting' + by (unfold readys_def, insert live_th_es, auto) + +lemma holdents_es_th: + "holdents (e#s) th = (holdents s th) \ {cs}" (is "?L = ?R") +proof - + { fix cs' + assume "cs' \ ?L" + hence "holding (e#s) th cs'" + by (unfold holdents_def, auto) + hence "cs' \ ?R" + by (cases rule:holding_esE, auto simp:holdents_def) + } moreover { + fix cs' + assume "cs' \ ?R" + hence "holding s th cs' \ cs' = cs" + by (auto simp:holdents_def) + hence "cs' \ ?L" + proof + assume "holding s th cs'" + from holding_kept[OF this] + show ?thesis by (auto simp:holdents_def) + next + assume "cs' = cs" + thus ?thesis using holding_es_th_cs + by (unfold holdents_def, auto) + qed + } ultimately show ?thesis by auto +qed + +lemma cntCS_es_th: "cntCS (e#s) th = cntCS s th + 1" +proof - + have "card (holdents s th \ {cs}) = card (holdents s th) + 1" + proof(subst card_Un_disjoint) + show "holdents s th \ {cs} = {}" + using not_holding_s_th_cs by (auto simp:holdents_def) + qed (auto simp:finite_holdents) + thus ?thesis + by (unfold cntCS_def holdents_es_th, simp) +qed + +lemma no_holder: + "\ holding s th' cs" +proof + assume otherwise: "holding s th' cs" + from this[unfolded s_holding_def, folded wq_def, unfolded we] + show False by auto +qed + +lemma holdents_es_th': + assumes "th' \ th" + shows "holdents (e#s) th' = holdents s th'" (is "?L = ?R") +proof - + { fix cs' + assume "cs' \ ?L" + hence h_e: "holding (e#s) th' cs'" by (auto simp:holdents_def) + have "cs' \ cs" + proof + assume "cs' = cs" + from held_unique[OF h_e[unfolded this] holding_es_th_cs] + have "th' = th" . + with assms show False by simp + qed + from h_e[unfolded s_holding_def, folded wq_def, unfolded wq_neq_simp[OF this]] + have "th' \ set (wq s cs') \ th' = hd (wq s cs')" . + hence "cs' \ ?R" + by (unfold holdents_def s_holding_def, fold wq_def, auto) + } moreover { + fix cs' + assume "cs' \ ?R" + hence "holding s th' cs'" by (auto simp:holdents_def) + from holding_kept[OF this] + have "holding (e # s) th' cs'" . + hence "cs' \ ?L" + by (unfold holdents_def, auto) + } ultimately show ?thesis by auto +qed + +lemma cntCS_es_th'[simp]: + assumes "th' \ th" + shows "cntCS (e#s) th' = cntCS s th'" + by (unfold cntCS_def holdents_es_th'[OF assms], simp) + +end + +context valid_trace_p +begin + +lemma readys_kept1: + assumes "th' \ th" + and "th' \ readys (e#s)" + shows "th' \ readys s" +proof - + { fix cs' + assume wait: "waiting s th' cs'" + have n_wait: "\ waiting (e#s) th' cs'" + using assms(2)[unfolded readys_def] by auto + have False + proof(cases "cs' = cs") + case False + with n_wait wait + show ?thesis + by (unfold s_waiting_def, fold wq_def, auto) + next + case True + show ?thesis + proof(cases "wq s cs = []") + case True + then interpret vt: valid_trace_p_h + by (unfold_locales, simp) + show ?thesis using n_wait wait waiting_kept by auto + next + case False + then interpret vt: valid_trace_p_w by (unfold_locales, simp) + show ?thesis using n_wait wait waiting_kept by blast + qed + qed + } with assms(2) show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_kept2: + assumes "th' \ th" + and "th' \ readys s" + shows "th' \ readys (e#s)" +proof - + { fix cs' + assume wait: "waiting (e#s) th' cs'" + have n_wait: "\ waiting s th' cs'" + using assms(2)[unfolded readys_def] by auto + have False + proof(cases "cs' = cs") + case False + with n_wait wait + show ?thesis + by (unfold s_waiting_def, fold wq_def, auto) + next + case True + show ?thesis + proof(cases "wq s cs = []") + case True + then interpret vt: valid_trace_p_h + by (unfold_locales, simp) + show ?thesis using n_wait vt.waiting_esE wait by blast + next + case False + then interpret vt: valid_trace_p_w by (unfold_locales, simp) + show ?thesis using assms(1) n_wait vt.waiting_esE wait by auto + qed + qed + } with assms(2) show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_simp [simp]: + assumes "th' \ th" + shows "(th' \ readys (e#s)) = (th' \ readys s)" + using readys_kept1[OF assms] readys_kept2[OF assms] + by metis + +lemma cnp_cnv_cncs_kept: + assumes "cntP s th' = cntV s th' + cntCS s th' + pvD s th'" + shows "cntP (e#s) th' = cntV (e#s) th' + cntCS (e#s) th' + pvD (e#s) th'" +proof(cases "th' = th") + case True + note eq_th' = this + show ?thesis + proof(cases "wq s cs = []") + case True + then interpret vt: valid_trace_p_h by (unfold_locales, simp) + show ?thesis + using assms eq_th' is_p ready_th_s vt.cntCS_es_th vt.ready_th_es pvD_def by auto + next + case False + then interpret vt: valid_trace_p_w by (unfold_locales, simp) + show ?thesis + using add.commute add.left_commute assms eq_th' is_p live_th_s + ready_th_s vt.th_not_ready_es pvD_def + apply (auto) + by (fold is_p, simp) + qed +next + case False + note h_False = False + thus ?thesis + proof(cases "wq s cs = []") + case True + then interpret vt: valid_trace_p_h by (unfold_locales, simp) + show ?thesis using assms + by (insert True h_False pvD_def, auto split:if_splits,unfold is_p, auto) + next + case False + then interpret vt: valid_trace_p_w by (unfold_locales, simp) + show ?thesis using assms + by (insert False h_False pvD_def, auto split:if_splits,unfold is_p, auto) + qed +qed + +end + + +context valid_trace_v (* ccc *) +begin + +lemma holding_th_cs_s: + "holding s th cs" + by (unfold s_holding_def, fold wq_def, unfold wq_s_cs, auto) + +lemma th_ready_s [simp]: "th \ readys s" + using runing_th_s + by (unfold runing_def readys_def, auto) + +lemma th_live_s [simp]: "th \ threads s" + using th_ready_s by (unfold readys_def, auto) + +lemma th_ready_es [simp]: "th \ readys (e#s)" + using runing_th_s neq_t_th + by (unfold is_v runing_def readys_def, auto) + +lemma th_live_es [simp]: "th \ threads (e#s)" + using th_ready_es by (unfold readys_def, auto) + +lemma pvD_th_s[simp]: "pvD s th = 0" + by (unfold pvD_def, simp) + +lemma pvD_th_es[simp]: "pvD (e#s) th = 0" + by (unfold pvD_def, simp) + +lemma cntCS_s_th [simp]: "cntCS s th > 0" +proof - + have "cs \ holdents s th" using holding_th_cs_s + by (unfold holdents_def, simp) + moreover have "finite (holdents s th)" using finite_holdents + by simp + ultimately show ?thesis + by (unfold cntCS_def, + auto intro!:card_gt_0_iff[symmetric, THEN iffD1]) +qed + +end + +context valid_trace_v_n +begin + +lemma not_ready_taker_s[simp]: + "taker \ readys s" + using waiting_taker + by (unfold readys_def, auto) + +lemma taker_live_s [simp]: "taker \ threads s" +proof - + have "taker \ set wq'" by (simp add: eq_wq') + from th'_in_inv[OF this] + have "taker \ set rest" . + hence "taker \ set (wq s cs)" by (simp add: wq_s_cs) + thus ?thesis using wq_threads by auto +qed + +lemma taker_live_es [simp]: "taker \ threads (e#s)" + using taker_live_s threads_es by blast + +lemma taker_ready_es [simp]: + shows "taker \ readys (e#s)" +proof - + { fix cs' + assume "waiting (e#s) taker cs'" + hence False + proof(cases rule:waiting_esE) + case 1 + thus ?thesis using waiting_taker waiting_unique by auto + qed simp + } thus ?thesis by (unfold readys_def, auto) +qed + +lemma neq_taker_th: "taker \ th" + using th_not_waiting waiting_taker by blast + +lemma not_holding_taker_s_cs: + shows "\ holding s taker cs" + using holding_cs_eq_th neq_taker_th by auto + +lemma holdents_es_taker: + "holdents (e#s) taker = holdents s taker \ {cs}" (is "?L = ?R") +proof - + { fix cs' + assume "cs' \ ?L" + hence "holding (e#s) taker cs'" by (auto simp:holdents_def) + hence "cs' \ ?R" + proof(cases rule:holding_esE) + case 2 + thus ?thesis by (auto simp:holdents_def) + qed auto + } moreover { + fix cs' + assume "cs' \ ?R" + hence "holding s taker cs' \ cs' = cs" by (auto simp:holdents_def) + hence "cs' \ ?L" + proof + assume "holding s taker cs'" + hence "holding (e#s) taker cs'" + using holding_esI2 holding_taker by fastforce + thus ?thesis by (auto simp:holdents_def) + next + assume "cs' = cs" + with holding_taker + show ?thesis by (auto simp:holdents_def) + qed + } ultimately show ?thesis by auto +qed + +lemma cntCS_es_taker [simp]: "cntCS (e#s) taker = cntCS s taker + 1" +proof - + have "card (holdents s taker \ {cs}) = card (holdents s taker) + 1" + proof(subst card_Un_disjoint) + show "holdents s taker \ {cs} = {}" + using not_holding_taker_s_cs by (auto simp:holdents_def) + qed (auto simp:finite_holdents) + thus ?thesis + by (unfold cntCS_def, insert holdents_es_taker, simp) +qed + +lemma pvD_taker_s[simp]: "pvD s taker = 1" + by (unfold pvD_def, simp) + +lemma pvD_taker_es[simp]: "pvD (e#s) taker = 0" + by (unfold pvD_def, simp) + +lemma pvD_th_s[simp]: "pvD s th = 0" + by (unfold pvD_def, simp) + +lemma pvD_th_es[simp]: "pvD (e#s) th = 0" + by (unfold pvD_def, simp) + +lemma holdents_es_th: + "holdents (e#s) th = holdents s th - {cs}" (is "?L = ?R") +proof - + { fix cs' + assume "cs' \ ?L" + hence "holding (e#s) th cs'" by (auto simp:holdents_def) + hence "cs' \ ?R" + proof(cases rule:holding_esE) + case 2 + thus ?thesis by (auto simp:holdents_def) + qed (insert neq_taker_th, auto) + } moreover { + fix cs' + assume "cs' \ ?R" + hence "cs' \ cs" "holding s th cs'" by (auto simp:holdents_def) + from holding_esI2[OF this] + have "cs' \ ?L" by (auto simp:holdents_def) + } ultimately show ?thesis by auto +qed + +lemma cntCS_es_th [simp]: "cntCS (e#s) th = cntCS s th - 1" +proof - + have "card (holdents s th - {cs}) = card (holdents s th) - 1" + proof - + have "cs \ holdents s th" using holding_th_cs_s + by (auto simp:holdents_def) + moreover have "finite (holdents s th)" + by (simp add: finite_holdents) + ultimately show ?thesis by auto + qed + thus ?thesis by (unfold cntCS_def holdents_es_th) +qed + +lemma holdents_kept: + assumes "th' \ taker" + and "th' \ th" + shows "holdents (e#s) th' = holdents s th'" (is "?L = ?R") +proof - + { fix cs' + assume h: "cs' \ ?L" + have "cs' \ ?R" + proof(cases "cs' = cs") + case False + hence eq_wq: "wq (e#s) cs' = wq s cs'" by simp + from h have "holding (e#s) th' cs'" by (auto simp:holdents_def) + from this[unfolded s_holding_def, folded wq_def, unfolded eq_wq] + show ?thesis + by (unfold holdents_def s_holding_def, fold wq_def, auto) + next + case True + from h[unfolded this] + have "holding (e#s) th' cs" by (auto simp:holdents_def) + from held_unique[OF this holding_taker] + have "th' = taker" . + with assms show ?thesis by auto + qed + } moreover { + fix cs' + assume h: "cs' \ ?R" + have "cs' \ ?L" + proof(cases "cs' = cs") + case False + hence eq_wq: "wq (e#s) cs' = wq s cs'" by simp + from h have "holding s th' cs'" by (auto simp:holdents_def) + from this[unfolded s_holding_def, folded wq_def, unfolded eq_wq] + show ?thesis + by (unfold holdents_def s_holding_def, fold wq_def, insert eq_wq, simp) + next + case True + from h[unfolded this] + have "holding s th' cs" by (auto simp:holdents_def) + from held_unique[OF this holding_th_cs_s] + have "th' = th" . + with assms show ?thesis by auto + qed + } ultimately show ?thesis by auto +qed + +lemma cntCS_kept [simp]: + assumes "th' \ taker" + and "th' \ th" + shows "cntCS (e#s) th' = cntCS s th'" + by (unfold cntCS_def holdents_kept[OF assms], simp) + +lemma readys_kept1: + assumes "th' \ taker" + and "th' \ readys (e#s)" + shows "th' \ readys s" +proof - + { fix cs' + assume wait: "waiting s th' cs'" + have n_wait: "\ waiting (e#s) th' cs'" + using assms(2)[unfolded readys_def] by auto + have False + proof(cases "cs' = cs") + case False + with n_wait wait + show ?thesis + by (unfold s_waiting_def, fold wq_def, auto) + next + case True + have "th' \ set (th # rest) \ th' \ hd (th # rest)" + using wait[unfolded True s_waiting_def, folded wq_def, unfolded wq_s_cs] . + moreover have "\ (th' \ set rest \ th' \ hd (taker # rest'))" + using n_wait[unfolded True s_waiting_def, folded wq_def, + unfolded wq_es_cs set_wq', unfolded eq_wq'] . + ultimately have "th' = taker" by auto + with assms(1) + show ?thesis by simp + qed + } with assms(2) show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_kept2: + assumes "th' \ taker" + and "th' \ readys s" + shows "th' \ readys (e#s)" +proof - + { fix cs' + assume wait: "waiting (e#s) th' cs'" + have n_wait: "\ waiting s th' cs'" + using assms(2)[unfolded readys_def] by auto + have False + proof(cases "cs' = cs") + case False + with n_wait wait + show ?thesis + by (unfold s_waiting_def, fold wq_def, auto) + next + case True + have "th' \ set rest \ th' \ hd (taker # rest')" + using wait [unfolded True s_waiting_def, folded wq_def, + unfolded wq_es_cs set_wq', unfolded eq_wq'] . + moreover have "\ (th' \ set (th # rest) \ th' \ hd (th # rest))" + using n_wait[unfolded True s_waiting_def, folded wq_def, unfolded wq_s_cs] . + ultimately have "th' = taker" by auto + with assms(1) + show ?thesis by simp + qed + } with assms(2) show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_simp [simp]: + assumes "th' \ taker" + shows "(th' \ readys (e#s)) = (th' \ readys s)" + using readys_kept1[OF assms] readys_kept2[OF assms] + by metis + +lemma cnp_cnv_cncs_kept: + assumes "cntP s th' = cntV s th' + cntCS s th' + pvD s th'" + shows "cntP (e#s) th' = cntV (e#s) th' + cntCS (e#s) th' + pvD (e#s) th'" +proof - + { assume eq_th': "th' = taker" + have ?thesis + apply (unfold eq_th' pvD_taker_es cntCS_es_taker) + by (insert neq_taker_th assms[unfolded eq_th'], unfold is_v, simp) + } moreover { + assume eq_th': "th' = th" + have ?thesis + apply (unfold eq_th' pvD_th_es cntCS_es_th) + by (insert assms[unfolded eq_th'], unfold is_v, simp) + } moreover { + assume h: "th' \ taker" "th' \ th" + have ?thesis using assms + apply (unfold cntCS_kept[OF h], insert h, unfold is_v, simp) + by (fold is_v, unfold pvD_def, simp) + } ultimately show ?thesis by metis +qed + +end + +context valid_trace_v_e +begin + +lemma holdents_es_th: + "holdents (e#s) th = holdents s th - {cs}" (is "?L = ?R") +proof - + { fix cs' + assume "cs' \ ?L" + hence "holding (e#s) th cs'" by (auto simp:holdents_def) + hence "cs' \ ?R" + proof(cases rule:holding_esE) + case 1 + thus ?thesis by (auto simp:holdents_def) + qed + } moreover { + fix cs' + assume "cs' \ ?R" + hence "cs' \ cs" "holding s th cs'" by (auto simp:holdents_def) + from holding_esI2[OF this] + have "cs' \ ?L" by (auto simp:holdents_def) + } ultimately show ?thesis by auto +qed + +lemma cntCS_es_th [simp]: "cntCS (e#s) th = cntCS s th - 1" +proof - + have "card (holdents s th - {cs}) = card (holdents s th) - 1" + proof - + have "cs \ holdents s th" using holding_th_cs_s + by (auto simp:holdents_def) + moreover have "finite (holdents s th)" + by (simp add: finite_holdents) + ultimately show ?thesis by auto + qed + thus ?thesis by (unfold cntCS_def holdents_es_th) +qed + +lemma holdents_kept: + assumes "th' \ th" + shows "holdents (e#s) th' = holdents s th'" (is "?L = ?R") +proof - + { fix cs' + assume h: "cs' \ ?L" + have "cs' \ ?R" + proof(cases "cs' = cs") + case False + hence eq_wq: "wq (e#s) cs' = wq s cs'" by simp + from h have "holding (e#s) th' cs'" by (auto simp:holdents_def) + from this[unfolded s_holding_def, folded wq_def, unfolded eq_wq] + show ?thesis + by (unfold holdents_def s_holding_def, fold wq_def, auto) + next + case True + from h[unfolded this] + have "holding (e#s) th' cs" by (auto simp:holdents_def) + from this[unfolded s_holding_def, folded wq_def, + unfolded wq_es_cs nil_wq'] + show ?thesis by auto + qed + } moreover { + fix cs' + assume h: "cs' \ ?R" + have "cs' \ ?L" + proof(cases "cs' = cs") + case False + hence eq_wq: "wq (e#s) cs' = wq s cs'" by simp + from h have "holding s th' cs'" by (auto simp:holdents_def) + from this[unfolded s_holding_def, folded wq_def, unfolded eq_wq] + show ?thesis + by (unfold holdents_def s_holding_def, fold wq_def, insert eq_wq, simp) + next + case True + from h[unfolded this] + have "holding s th' cs" by (auto simp:holdents_def) + from held_unique[OF this holding_th_cs_s] + have "th' = th" . + with assms show ?thesis by auto + qed + } ultimately show ?thesis by auto +qed + +lemma cntCS_kept [simp]: + assumes "th' \ th" + shows "cntCS (e#s) th' = cntCS s th'" + by (unfold cntCS_def holdents_kept[OF assms], simp) + +lemma readys_kept1: + assumes "th' \ readys (e#s)" + shows "th' \ readys s" +proof - + { fix cs' + assume wait: "waiting s th' cs'" + have n_wait: "\ waiting (e#s) th' cs'" + using assms(1)[unfolded readys_def] by auto + have False + proof(cases "cs' = cs") + case False + with n_wait wait + show ?thesis + by (unfold s_waiting_def, fold wq_def, auto) + next + case True + have "th' \ set (th # rest) \ th' \ hd (th # rest)" + using wait[unfolded True s_waiting_def, folded wq_def, unfolded wq_s_cs] . + hence "th' \ set rest" by auto + with set_wq' have "th' \ set wq'" by metis + with nil_wq' show ?thesis by simp + qed + } thus ?thesis using assms + by (unfold readys_def, auto) +qed + +lemma readys_kept2: + assumes "th' \ readys s" + shows "th' \ readys (e#s)" +proof - + { fix cs' + assume wait: "waiting (e#s) th' cs'" + have n_wait: "\ waiting s th' cs'" + using assms[unfolded readys_def] by auto + have False + proof(cases "cs' = cs") + case False + with n_wait wait + show ?thesis + by (unfold s_waiting_def, fold wq_def, auto) + next + case True + have "th' \ set [] \ th' \ hd []" + using wait[unfolded True s_waiting_def, folded wq_def, + unfolded wq_es_cs nil_wq'] . + thus ?thesis by simp + qed + } with assms show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_simp [simp]: + shows "(th' \ readys (e#s)) = (th' \ readys s)" + using readys_kept1[OF assms] readys_kept2[OF assms] + by metis + +lemma cnp_cnv_cncs_kept: + assumes "cntP s th' = cntV s th' + cntCS s th' + pvD s th'" + shows "cntP (e#s) th' = cntV (e#s) th' + cntCS (e#s) th' + pvD (e#s) th'" +proof - + { + assume eq_th': "th' = th" + have ?thesis + apply (unfold eq_th' pvD_th_es cntCS_es_th) + by (insert assms[unfolded eq_th'], unfold is_v, simp) + } moreover { + assume h: "th' \ th" + have ?thesis using assms + apply (unfold cntCS_kept[OF h], insert h, unfold is_v, simp) + by (fold is_v, unfold pvD_def, simp) + } ultimately show ?thesis by metis +qed + +end + +context valid_trace_v +begin + +lemma cnp_cnv_cncs_kept: + assumes "cntP s th' = cntV s th' + cntCS s th' + pvD s th'" + shows "cntP (e#s) th' = cntV (e#s) th' + cntCS (e#s) th' + pvD (e#s) th'" +proof(cases "rest = []") + case True + then interpret vt: valid_trace_v_e by (unfold_locales, simp) + show ?thesis using assms using vt.cnp_cnv_cncs_kept by blast +next + case False + then interpret vt: valid_trace_v_n by (unfold_locales, simp) + show ?thesis using assms using vt.cnp_cnv_cncs_kept by blast +qed + +end + +context valid_trace_create +begin + +lemma th_not_live_s [simp]: "th \ threads s" +proof - + from pip_e[unfolded is_create] + show ?thesis by (cases, simp) +qed + +lemma th_not_ready_s [simp]: "th \ readys s" + using th_not_live_s by (unfold readys_def, simp) + +lemma th_live_es [simp]: "th \ threads (e#s)" + by (unfold is_create, simp) + +lemma not_waiting_th_s [simp]: "\ waiting s th cs'" +proof + assume "waiting s th cs'" + from this[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp] + have "th \ set (wq s cs')" by auto + from wq_threads[OF this] have "th \ threads s" . + with th_not_live_s show False by simp +qed + +lemma not_holding_th_s [simp]: "\ holding s th cs'" +proof + assume "holding s th cs'" + from this[unfolded s_holding_def, folded wq_def, unfolded wq_neq_simp] + have "th \ set (wq s cs')" by auto + from wq_threads[OF this] have "th \ threads s" . + with th_not_live_s show False by simp +qed + +lemma not_waiting_th_es [simp]: "\ waiting (e#s) th cs'" +proof + assume "waiting (e # s) th cs'" + from this[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp] + have "th \ set (wq s cs')" by auto + from wq_threads[OF this] have "th \ threads s" . + with th_not_live_s show False by simp +qed + +lemma not_holding_th_es [simp]: "\ holding (e#s) th cs'" +proof + assume "holding (e # s) th cs'" + from this[unfolded s_holding_def, folded wq_def, unfolded wq_neq_simp] + have "th \ set (wq s cs')" by auto + from wq_threads[OF this] have "th \ threads s" . + with th_not_live_s show False by simp +qed + +lemma ready_th_es [simp]: "th \ readys (e#s)" + by (simp add:readys_def) + +lemma holdents_th_s: "holdents s th = {}" + by (unfold holdents_def, auto) + +lemma holdents_th_es: "holdents (e#s) th = {}" + by (unfold holdents_def, auto) + +lemma cntCS_th_s [simp]: "cntCS s th = 0" + by (unfold cntCS_def, simp add:holdents_th_s) + +lemma cntCS_th_es [simp]: "cntCS (e#s) th = 0" + by (unfold cntCS_def, simp add:holdents_th_es) + +lemma pvD_th_s [simp]: "pvD s th = 0" + by (unfold pvD_def, simp) + +lemma pvD_th_es [simp]: "pvD (e#s) th = 0" + by (unfold pvD_def, simp) + +lemma holdents_kept: + assumes "th' \ th" + shows "holdents (e#s) th' = holdents s th'" (is "?L = ?R") +proof - + { fix cs' + assume h: "cs' \ ?L" + hence "cs' \ ?R" + by (unfold holdents_def s_holding_def, fold wq_def, + unfold wq_neq_simp, auto) + } moreover { + fix cs' + assume h: "cs' \ ?R" + hence "cs' \ ?L" + by (unfold holdents_def s_holding_def, fold wq_def, + unfold wq_neq_simp, auto) + } ultimately show ?thesis by auto +qed + +lemma cntCS_kept [simp]: + assumes "th' \ th" + shows "cntCS (e#s) th' = cntCS s th'" (is "?L = ?R") + using holdents_kept[OF assms] + by (unfold cntCS_def, simp) + +lemma readys_kept1: + assumes "th' \ th" + and "th' \ readys (e#s)" + shows "th' \ readys s" +proof - + { fix cs' + assume wait: "waiting s th' cs'" + have n_wait: "\ waiting (e#s) th' cs'" + using assms by (auto simp:readys_def) + from wait[unfolded s_waiting_def, folded wq_def] + n_wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp] + have False by auto + } thus ?thesis using assms + by (unfold readys_def, auto) +qed + +lemma readys_kept2: + assumes "th' \ th" + and "th' \ readys s" + shows "th' \ readys (e#s)" +proof - + { fix cs' + assume wait: "waiting (e#s) th' cs'" + have n_wait: "\ waiting s th' cs'" + using assms(2) by (auto simp:readys_def) + from wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp] + n_wait[unfolded s_waiting_def, folded wq_def] + have False by auto + } with assms show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_simp [simp]: + assumes "th' \ th" + shows "(th' \ readys (e#s)) = (th' \ readys s)" + using readys_kept1[OF assms] readys_kept2[OF assms] + by metis + +lemma pvD_kept [simp]: + assumes "th' \ th" + shows "pvD (e#s) th' = pvD s th'" + using assms + by (unfold pvD_def, simp) + +lemma cnp_cnv_cncs_kept: + assumes "cntP s th' = cntV s th' + cntCS s th' + pvD s th'" + shows "cntP (e#s) th' = cntV (e#s) th' + cntCS (e#s) th' + pvD (e#s) th'" +proof - + { + assume eq_th': "th' = th" + have ?thesis using assms + by (unfold eq_th', simp, unfold is_create, simp) + } moreover { + assume h: "th' \ th" + hence ?thesis using assms + by (simp, simp add:is_create) + } ultimately show ?thesis by metis +qed + +end + +context valid_trace_exit +begin + +lemma th_live_s [simp]: "th \ threads s" +proof - + from pip_e[unfolded is_exit] + show ?thesis + by (cases, unfold runing_def readys_def, simp) +qed + +lemma th_ready_s [simp]: "th \ readys s" +proof - + from pip_e[unfolded is_exit] + show ?thesis + by (cases, unfold runing_def, simp) +qed + +lemma th_not_live_es [simp]: "th \ threads (e#s)" + by (unfold is_exit, simp) + +lemma not_holding_th_s [simp]: "\ holding s th cs'" +proof - + from pip_e[unfolded is_exit] + show ?thesis + by (cases, unfold holdents_def, auto) +qed + +lemma cntCS_th_s [simp]: "cntCS s th = 0" +proof - + from pip_e[unfolded is_exit] + show ?thesis + by (cases, unfold cntCS_def, simp) +qed + +lemma not_holding_th_es [simp]: "\ holding (e#s) th cs'" +proof + assume "holding (e # s) th cs'" + from this[unfolded s_holding_def, folded wq_def, unfolded wq_neq_simp] + have "holding s th cs'" + by (unfold s_holding_def, fold wq_def, auto) + with not_holding_th_s + show False by simp +qed + +lemma ready_th_es [simp]: "th \ readys (e#s)" + by (simp add:readys_def) + +lemma holdents_th_s: "holdents s th = {}" + by (unfold holdents_def, auto) + +lemma holdents_th_es: "holdents (e#s) th = {}" + by (unfold holdents_def, auto) + +lemma cntCS_th_es [simp]: "cntCS (e#s) th = 0" + by (unfold cntCS_def, simp add:holdents_th_es) + +lemma pvD_th_s [simp]: "pvD s th = 0" + by (unfold pvD_def, simp) + +lemma pvD_th_es [simp]: "pvD (e#s) th = 0" + by (unfold pvD_def, simp) + +lemma holdents_kept: + assumes "th' \ th" + shows "holdents (e#s) th' = holdents s th'" (is "?L = ?R") +proof - + { fix cs' + assume h: "cs' \ ?L" + hence "cs' \ ?R" + by (unfold holdents_def s_holding_def, fold wq_def, + unfold wq_neq_simp, auto) + } moreover { + fix cs' + assume h: "cs' \ ?R" + hence "cs' \ ?L" + by (unfold holdents_def s_holding_def, fold wq_def, + unfold wq_neq_simp, auto) + } ultimately show ?thesis by auto +qed + +lemma cntCS_kept [simp]: + assumes "th' \ th" + shows "cntCS (e#s) th' = cntCS s th'" (is "?L = ?R") + using holdents_kept[OF assms] + by (unfold cntCS_def, simp) + +lemma readys_kept1: + assumes "th' \ th" + and "th' \ readys (e#s)" + shows "th' \ readys s" +proof - + { fix cs' + assume wait: "waiting s th' cs'" + have n_wait: "\ waiting (e#s) th' cs'" + using assms by (auto simp:readys_def) + from wait[unfolded s_waiting_def, folded wq_def] + n_wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp] + have False by auto + } thus ?thesis using assms + by (unfold readys_def, auto) +qed + +lemma readys_kept2: + assumes "th' \ th" + and "th' \ readys s" + shows "th' \ readys (e#s)" +proof - + { fix cs' + assume wait: "waiting (e#s) th' cs'" + have n_wait: "\ waiting s th' cs'" + using assms(2) by (auto simp:readys_def) + from wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp] + n_wait[unfolded s_waiting_def, folded wq_def] + have False by auto + } with assms show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_simp [simp]: + assumes "th' \ th" + shows "(th' \ readys (e#s)) = (th' \ readys s)" + using readys_kept1[OF assms] readys_kept2[OF assms] + by metis + +lemma pvD_kept [simp]: + assumes "th' \ th" + shows "pvD (e#s) th' = pvD s th'" + using assms + by (unfold pvD_def, simp) + +lemma cnp_cnv_cncs_kept: + assumes "cntP s th' = cntV s th' + cntCS s th' + pvD s th'" + shows "cntP (e#s) th' = cntV (e#s) th' + cntCS (e#s) th' + pvD (e#s) th'" +proof - + { + assume eq_th': "th' = th" + have ?thesis using assms + by (unfold eq_th', simp, unfold is_exit, simp) + } moreover { + assume h: "th' \ th" + hence ?thesis using assms + by (simp, simp add:is_exit) + } ultimately show ?thesis by metis +qed + +end + +context valid_trace_set +begin + +lemma th_live_s [simp]: "th \ threads s" +proof - + from pip_e[unfolded is_set] + show ?thesis + by (cases, unfold runing_def readys_def, simp) +qed + +lemma th_ready_s [simp]: "th \ readys s" +proof - + from pip_e[unfolded is_set] + show ?thesis + by (cases, unfold runing_def, simp) +qed + +lemma th_not_live_es [simp]: "th \ threads (e#s)" + by (unfold is_set, simp) + + +lemma holdents_kept: + shows "holdents (e#s) th' = holdents s th'" (is "?L = ?R") +proof - + { fix cs' + assume h: "cs' \ ?L" + hence "cs' \ ?R" + by (unfold holdents_def s_holding_def, fold wq_def, + unfold wq_neq_simp, auto) + } moreover { + fix cs' + assume h: "cs' \ ?R" + hence "cs' \ ?L" + by (unfold holdents_def s_holding_def, fold wq_def, + unfold wq_neq_simp, auto) + } ultimately show ?thesis by auto +qed + +lemma cntCS_kept [simp]: + shows "cntCS (e#s) th' = cntCS s th'" (is "?L = ?R") + using holdents_kept + by (unfold cntCS_def, simp) + +lemma threads_kept[simp]: + "threads (e#s) = threads s" + by (unfold is_set, simp) + +lemma readys_kept1: + assumes "th' \ readys (e#s)" + shows "th' \ readys s" +proof - + { fix cs' + assume wait: "waiting s th' cs'" + have n_wait: "\ waiting (e#s) th' cs'" + using assms by (auto simp:readys_def) + from wait[unfolded s_waiting_def, folded wq_def] + n_wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp] + have False by auto + } moreover have "th' \ threads s" + using assms[unfolded readys_def] by auto + ultimately show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_kept2: + assumes "th' \ readys s" + shows "th' \ readys (e#s)" +proof - + { fix cs' + assume wait: "waiting (e#s) th' cs'" + have n_wait: "\ waiting s th' cs'" + using assms by (auto simp:readys_def) + from wait[unfolded s_waiting_def, folded wq_def, unfolded wq_neq_simp] + n_wait[unfolded s_waiting_def, folded wq_def] + have False by auto + } with assms show ?thesis + by (unfold readys_def, auto) +qed + +lemma readys_simp [simp]: + shows "(th' \ readys (e#s)) = (th' \ readys s)" + using readys_kept1 readys_kept2 + by metis + +lemma pvD_kept [simp]: + shows "pvD (e#s) th' = pvD s th'" + by (unfold pvD_def, simp) + +lemma cnp_cnv_cncs_kept: + assumes "cntP s th' = cntV s th' + cntCS s th' + pvD s th'" + shows "cntP (e#s) th' = cntV (e#s) th' + cntCS (e#s) th' + pvD (e#s) th'" + using assms + by (unfold is_set, simp, fold is_set, simp) + +end + +context valid_trace +begin + +lemma cnp_cnv_cncs: "cntP s th' = cntV s th' + cntCS s th' + pvD s th'" +proof(induct rule:ind) + case Nil + thus ?case + by (unfold cntP_def cntV_def pvD_def cntCS_def holdents_def + s_holding_def, simp) +next + case (Cons s e) + interpret vt_e: valid_trace_e s e using Cons by simp + show ?case + proof(cases e) + case (Create th prio) + interpret vt_create: valid_trace_create s e th prio + using Create by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_create.cnp_cnv_cncs_kept) + next + case (Exit th) + interpret vt_exit: valid_trace_exit s e th + using Exit by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_exit.cnp_cnv_cncs_kept) + next + case (P th cs) + interpret vt_p: valid_trace_p s e th cs using P by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_p.cnp_cnv_cncs_kept) + next + case (V th cs) + interpret vt_v: valid_trace_v s e th cs using V by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_v.cnp_cnv_cncs_kept) + next + case (Set th prio) + interpret vt_set: valid_trace_set s e th prio + using Set by (unfold_locales, simp) + show ?thesis using Cons by (simp add: vt_set.cnp_cnv_cncs_kept) + qed +qed + +lemma not_thread_holdents: + assumes not_in: "th \ threads s" + shows "holdents s th = {}" +proof - + { fix cs + assume "cs \ holdents s th" + hence "holding s th cs" by (auto simp:holdents_def) + from this[unfolded s_holding_def, folded wq_def] + have "th \ set (wq s cs)" by auto + with wq_threads have "th \ threads s" by auto + with assms + have False by simp + } thus ?thesis by auto +qed + +lemma not_thread_cncs: + assumes not_in: "th \ threads s" + shows "cntCS s th = 0" + using not_thread_holdents[OF assms] + by (simp add:cntCS_def) + +lemma cnp_cnv_eq: + assumes "th \ threads s" + shows "cntP s th = cntV s th" + using assms cnp_cnv_cncs not_thread_cncs pvD_def + by (auto) + +end + + + +end + diff -r 17305a85493d -r c495eb16beb6 Moment.thy.orig --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Moment.thy.orig Wed Jan 27 19:28:42 2016 +0800 @@ -0,0 +1,204 @@ +theory Moment +imports Main +begin + +definition moment :: "nat \ 'a list \ 'a list" +where "moment n s = rev (take n (rev s))" + +definition restm :: "nat \ 'a list \ 'a list" +where "restm n s = rev (drop n (rev s))" + +value "moment 3 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9::int]" +value "moment 2 [5, 4, 3, 2, 1, 0::int]" + +value "restm 3 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9::int]" + +lemma moment_restm_s: "(restm n s) @ (moment n s) = s" + unfolding restm_def moment_def +by (metis append_take_drop_id rev_append rev_rev_ident) + +lemma length_moment_le: + assumes le_k: "k \ length s" + shows "length (moment k s) = k" +using le_k unfolding moment_def by auto + +lemma length_moment_ge: + assumes le_k: "length s \ k" + shows "length (moment k s) = (length s)" +using assms unfolding moment_def by simp + +lemma moment_app [simp]: + assumes ile: "i \ length s" + shows "moment i (s' @ s) = moment i s" +using assms unfolding moment_def by simp + +lemma moment_eq [simp]: "moment (length s) (s' @ s) = s" + unfolding moment_def by simp + +lemma moment_ge [simp]: "length s \ n \ moment n s = s" + by (unfold moment_def, simp) + +lemma moment_zero [simp]: "moment 0 s = []" + by (simp add:moment_def) + +lemma p_split_gen: + "\Q s; \ Q (moment k s)\ \ + (\ i. i < length s \ k \ i \ \ Q (moment i s) \ (\ i' > i. Q (moment i' s)))" +proof (induct s, simp) + fix a s + assume ih: "\Q s; \ Q (moment k s)\ + \ \i i \ \ Q (moment i s) \ (\i'>i. Q (moment i' s))" + and nq: "\ Q (moment k (a # s))" and qa: "Q (a # s)" + have le_k: "k \ length s" + proof - + { assume "length s < k" + hence "length (a#s) \ k" by simp + from moment_ge [OF this] and nq and qa + have "False" by auto + } thus ?thesis by arith + qed + have nq_k: "\ Q (moment k s)" + proof - + have "moment k (a#s) = moment k s" + proof - + from moment_app [OF le_k, of "[a]"] show ?thesis by simp + qed + with nq show ?thesis by simp + qed + show "\i i \ \ Q (moment i (a # s)) \ (\i'>i. Q (moment i' (a # s)))" + proof - + { assume "Q s" + from ih [OF this nq_k] + obtain i where lti: "i < length s" + and nq: "\ Q (moment i s)" + and rst: "\i'>i. Q (moment i' s)" + and lki: "k \ i" by auto + have ?thesis + proof - + from lti have "i < length (a # s)" by auto + moreover have " \ Q (moment i (a # s))" + proof - + from lti have "i \ (length s)" by simp + from moment_app [OF this, of "[a]"] + have "moment i (a # s) = moment i s" by simp + with nq show ?thesis by auto + qed + moreover have " (\i'>i. Q (moment i' (a # s)))" + proof - + { + fix i' + assume lti': "i < i'" + have "Q (moment i' (a # s))" + proof(cases "length (a#s) \ i'") + case True + from True have "moment i' (a#s) = a#s" by simp + with qa show ?thesis by simp + next + case False + from False have "i' \ length s" by simp + from moment_app [OF this, of "[a]"] + have "moment i' (a#s) = moment i' s" by simp + with rst lti' show ?thesis by auto + qed + } thus ?thesis by auto + qed + moreover note lki + ultimately show ?thesis by auto + qed + } moreover { + assume ns: "\ Q s" + have ?thesis + proof - + let ?i = "length s" + have "\ Q (moment ?i (a#s))" + proof - + have "?i \ length s" by simp + from moment_app [OF this, of "[a]"] + have "moment ?i (a#s) = moment ?i s" by simp + moreover have "\ = s" by simp + ultimately show ?thesis using ns by auto + qed + moreover have "\ i' > ?i. Q (moment i' (a#s))" + proof - + { fix i' + assume "i' > ?i" + hence "length (a#s) \ i'" by simp + from moment_ge [OF this] + have " moment i' (a # s) = a # s" . + with qa have "Q (moment i' (a#s))" by simp + } thus ?thesis by auto + qed + moreover have "?i < length (a#s)" by simp + moreover note le_k + ultimately show ?thesis by auto + qed + } ultimately show ?thesis by auto + qed +qed + +lemma p_split: + "\Q s; \ Q []\ \ + (\ i. i < length s \ \ Q (moment i s) \ (\ i' > i. Q (moment i' s)))" +proof - + fix s Q + assume qs: "Q s" and nq: "\ Q []" + from nq have "\ Q (moment 0 s)" by simp + from p_split_gen [of Q s 0, OF qs this] + show "(\ i. i < length s \ \ Q (moment i s) \ (\ i' > i. Q (moment i' s)))" + by auto +qed + +lemma moment_plus_split: + shows "moment (m + i) s = moment m (restm i s) @ moment i s" +unfolding moment_def restm_def +by (metis add.commute rev_append rev_rev_ident take_add) + +lemma moment_prefix: + "(moment i t @ s) = moment (i + length s) (t @ s)" +proof - + from moment_plus_split [of i "length s" "t@s"] + have " moment (i + length s) (t @ s) = moment i (restm (length s) (t @ s)) @ moment (length s) (t @ s)" + by auto + have "moment (i + length s) (t @ s) = moment i t @ moment (length s) (t @ s)" + by (simp add: moment_def) + with moment_app show ?thesis by auto +qed + +lemma moment_plus: + "Suc i \ length s \ moment (Suc i) s = (hd (moment (Suc i) s)) # (moment i s)" +proof(induct s, simp+) + fix a s + assume ih: "Suc i \ length s \ moment (Suc i) s = hd (moment (Suc i) s) # moment i s" + and le_i: "i \ length s" + show "moment (Suc i) (a # s) = hd (moment (Suc i) (a # s)) # moment i (a # s)" + proof(cases "i= length s") + case True + hence "Suc i = length (a#s)" by simp + with moment_eq have "moment (Suc i) (a#s) = a#s" by auto + moreover have "moment i (a#s) = s" + proof - + from moment_app [OF le_i, of "[a]"] + and True show ?thesis by simp + qed + ultimately show ?thesis by auto + next + case False + from False and le_i have lti: "i < length s" by arith + hence les_i: "Suc i \ length s" by arith + show ?thesis + proof - + from moment_app [OF les_i, of "[a]"] + have "moment (Suc i) (a # s) = moment (Suc i) s" by simp + moreover have "moment i (a#s) = moment i s" + proof - + from lti have "i \ length s" by simp + from moment_app [OF this, of "[a]"] show ?thesis by simp + qed + moreover note ih [OF les_i] + ultimately show ?thesis by auto + qed + qed +qed + +end + diff -r 17305a85493d -r c495eb16beb6 Moment.thy~ --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Moment.thy~ Wed Jan 27 19:28:42 2016 +0800 @@ -0,0 +1,225 @@ +theory Moment +imports Main +begin + +definition moment :: "nat \ 'a list \ 'a list" +where "moment n s = rev (take n (rev s))" + +value "moment 3 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9::int]" +value "moment 2 [5, 4, 3, 2, 1, 0::int]" + +(* +lemma length_moment_le: + assumes le_k: "k \ length s" + shows "length (moment k s) = k" +using le_k unfolding moment_def by auto +*) + +(* +lemma length_moment_ge: + assumes le_k: "length s \ k" + shows "length (moment k s) = (length s)" +using assms unfolding moment_def by simp +*) + +lemma moment_app [simp]: + assumes ile: "i \ length s" + shows "moment i (s' @ s) = moment i s" +using assms unfolding moment_def by simp + +lemma moment_eq [simp]: "moment (length s) (s' @ s) = s" + unfolding moment_def by simp + +lemma moment_ge [simp]: "length s \ n \ moment n s = s" + by (unfold moment_def, simp) + +lemma moment_zero [simp]: "moment 0 s = []" + by (simp add:moment_def) + +lemma p_split_gen: + "\Q s; \ Q (moment k s)\ \ + (\ i. i < length s \ k \ i \ \ Q (moment i s) \ (\ i' > i. Q (moment i' s)))" +proof (induct s, simp) + fix a s + assume ih: "\Q s; \ Q (moment k s)\ + \ \i i \ \ Q (moment i s) \ (\i'>i. Q (moment i' s))" + and nq: "\ Q (moment k (a # s))" and qa: "Q (a # s)" + have le_k: "k \ length s" + proof - + { assume "length s < k" + hence "length (a#s) \ k" by simp + from moment_ge [OF this] and nq and qa + have "False" by auto + } thus ?thesis by arith + qed + have nq_k: "\ Q (moment k s)" + proof - + have "moment k (a#s) = moment k s" + proof - + from moment_app [OF le_k, of "[a]"] show ?thesis by simp + qed + with nq show ?thesis by simp + qed + show "\i i \ \ Q (moment i (a # s)) \ (\i'>i. Q (moment i' (a # s)))" + proof - + { assume "Q s" + from ih [OF this nq_k] + obtain i where lti: "i < length s" + and nq: "\ Q (moment i s)" + and rst: "\i'>i. Q (moment i' s)" + and lki: "k \ i" by auto + have ?thesis + proof - + from lti have "i < length (a # s)" by auto + moreover have " \ Q (moment i (a # s))" + proof - + from lti have "i \ (length s)" by simp + from moment_app [OF this, of "[a]"] + have "moment i (a # s) = moment i s" by simp + with nq show ?thesis by auto + qed + moreover have " (\i'>i. Q (moment i' (a # s)))" + proof - + { + fix i' + assume lti': "i < i'" + have "Q (moment i' (a # s))" + proof(cases "length (a#s) \ i'") + case True + from True have "moment i' (a#s) = a#s" by simp + with qa show ?thesis by simp + next + case False + from False have "i' \ length s" by simp + from moment_app [OF this, of "[a]"] + have "moment i' (a#s) = moment i' s" by simp + with rst lti' show ?thesis by auto + qed + } thus ?thesis by auto + qed + moreover note lki + ultimately show ?thesis by auto + qed + } moreover { + assume ns: "\ Q s" + have ?thesis + proof - + let ?i = "length s" + have "\ Q (moment ?i (a#s))" + proof - + have "?i \ length s" by simp + from moment_app [OF this, of "[a]"] + have "moment ?i (a#s) = moment ?i s" by simp + moreover have "\ = s" by simp + ultimately show ?thesis using ns by auto + qed + moreover have "\ i' > ?i. Q (moment i' (a#s))" + proof - + { fix i' + assume "i' > ?i" + hence "length (a#s) \ i'" by simp + from moment_ge [OF this] + have " moment i' (a # s) = a # s" . + with qa have "Q (moment i' (a#s))" by simp + } thus ?thesis by auto + qed + moreover have "?i < length (a#s)" by simp + moreover note le_k + ultimately show ?thesis by auto + qed + } ultimately show ?thesis by auto + qed +qed + +lemma p_split: + "\Q s; \ Q []\ \ + (\ i. i < length s \ \ Q (moment i s) \ (\ i' > i. Q (moment i' s)))" +proof - + fix s Q + assume qs: "Q s" and nq: "\ Q []" + from nq have "\ Q (moment 0 s)" by simp + from p_split_gen [of Q s 0, OF qs this] + show "(\ i. i < length s \ \ Q (moment i s) \ (\ i' > i. Q (moment i' s)))" + by auto +qed + +lemma moment_Suc_tl: + assumes "Suc i \ length s" + shows "tl (moment (Suc i) s) = moment i s" + using assms unfolding moment_def rev_take +by (simp, metis Suc_diff_le diff_Suc_Suc drop_Suc tl_drop) + +lemma moment_plus: + assumes "Suc i \ length s" + shows "(moment (Suc i) s) = (hd (moment (Suc i) s)) # (moment i s)" +proof - + have "(moment (Suc i) s) \ []" + using assms by (auto simp add: moment_def) + hence "(moment (Suc i) s) = (hd (moment (Suc i) s)) # tl (moment (Suc i) s)" + by auto +<<<<<<< local + have "moment (i + length s) (t @ s) = moment i t @ moment (length s) (t @ s)" + by (simp add: moment_def) + with moment_app show ?thesis by auto +qed + +lemma moment_Suc_tl: + assumes "Suc i \ length s" + shows "tl (moment (Suc i) s) = moment i s" + using assms unfolding moment_def rev_take + by (simp, metis Suc_diff_le diff_Suc_Suc drop_Suc tl_drop) + +lemma moment_plus': + assumes "Suc i \ length s" + shows "(moment (Suc i) s) = (hd (moment (Suc i) s)) # (moment i s)" +proof - + have "(moment (Suc i) s) \ []" + using assms length_moment_le by fastforce + hence "(moment (Suc i) s) = (hd (moment (Suc i) s)) # tl (moment (Suc i) s)" + by auto + with moment_Suc_tl[OF assms] + show ?thesis by metis +qed + +lemma moment_plus: + "Suc i \ length s \ moment (Suc i) s = (hd (moment (Suc i) s)) # (moment i s)" +proof(induct s, simp+) + fix a s + assume ih: "Suc i \ length s \ moment (Suc i) s = hd (moment (Suc i) s) # moment i s" + and le_i: "i \ length s" + show "moment (Suc i) (a # s) = hd (moment (Suc i) (a # s)) # moment i (a # s)" + proof(cases "i= length s") + case True + hence "Suc i = length (a#s)" by simp + with moment_eq have "moment (Suc i) (a#s) = a#s" by auto + moreover have "moment i (a#s) = s" + proof - + from moment_app [OF le_i, of "[a]"] + and True show ?thesis by simp + qed + ultimately show ?thesis by auto + next + case False + from False and le_i have lti: "i < length s" by arith + hence les_i: "Suc i \ length s" by arith + show ?thesis + proof - + from moment_app [OF les_i, of "[a]"] + have "moment (Suc i) (a # s) = moment (Suc i) s" by simp + moreover have "moment i (a#s) = moment i s" + proof - + from lti have "i \ length s" by simp + from moment_app [OF this, of "[a]"] show ?thesis by simp + qed + moreover note ih [OF les_i] + ultimately show ?thesis by auto + qed + qed +======= + with moment_Suc_tl[OF assms] + show ?thesis by metis +>>>>>>> other +qed + +end + diff -r 17305a85493d -r c495eb16beb6 Moment_1.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Moment_1.thy Wed Jan 27 19:28:42 2016 +0800 @@ -0,0 +1,896 @@ +theory Moment +imports Main +begin + +fun firstn :: "nat \ 'a list \ 'a list" +where + "firstn 0 s = []" | + "firstn (Suc n) [] = []" | + "firstn (Suc n) (e#s) = e#(firstn n s)" + +lemma upto_map_plus: "map (op + k) [i..j] = [i+k..j+k]" +proof(induct "[i..j]" arbitrary:i j rule:length_induct) + case (1 i j) + thus ?case + proof(cases "i \ j") + case True + hence le_k: "i + k \ j + k" by simp + show ?thesis (is "?L = ?R") + proof - + have "?L = (k + i) # map (op + k) [i + 1..j]" + by (simp add: upto_rec1[OF True]) + moreover have "?R = (i + k) # [i + k + 1..j + k]" + by (simp add: upto_rec1[OF le_k]) + moreover have "map (op + k) [i + 1..j] = [i + k + 1..j + k]" + proof - + have h: "i + k + 1 = (i + 1) + k" by simp + show ?thesis + proof(unfold h, rule 1[rule_format]) + show "length [i + 1..j] < length [i..j]" + using upto_rec1[OF True] by simp + qed simp + qed + ultimately show ?thesis by simp + qed + qed auto +qed + +lemma firstn_alt_def: + "firstn n s = map (\ i. s!(nat i)) [0..(int (min (length s) n)) - 1]" +proof(induct n arbitrary:s) + case (0 s) + thus ?case by auto +next + case (Suc n s) + thus ?case (is "?L = ?R") + proof(cases s) + case Nil + thus ?thesis by simp + next + case (Cons e es) + with Suc + have "?L = e # map (\i. es ! nat i) [0..int (min (length es) n) - 1]" + by simp + also have "... = map (\i. (e # es) ! nat i) [0..int (min (length es) n)]" + (is "?L1 = ?R1") + proof - + have "?R1 = e # map (\i. (e # es) ! nat i) + [1..int (min (length es) n)]" + proof - + have "[0..int (min (length es) n)] = 0#[1..int (min (length es) n)]" + by (simp add: upto.simps) + thus ?thesis by simp + qed + also have "... = ?L1" (is "_#?L2 = _#?R2") + proof - + have "?L2 = ?R2" + proof - + have "map (\i. (e # es) ! nat i) [1..int (min (length es) n)] = + map ((\i. (e # es) ! nat i) \ op + 1) [0..int (min (length es) n) - 1]" + proof - + have "[1..int (min (length es) n)] = + map (op + 1) [0..int (min (length es) n) - 1]" + by (unfold upto_map_plus, simp) + thus ?thesis by simp + qed + also have "... = map (\i. es ! nat i) [0..int (min (length es) n) - 1]" + proof(rule map_cong) + fix x + assume "x \ set [0..int (min (length es) n) - 1]" + thus "((\i. (e # es) ! nat i) \ op + 1) x = es ! nat x" + by (metis atLeastLessThan_iff atLeastLessThan_upto + comp_apply local.Cons nat_0_le nat_int nth_Cons_Suc of_nat_Suc) + qed auto + finally show ?thesis . + qed + thus ?thesis by simp + qed + finally show ?thesis by simp + qed + also have "... = ?R" + by (unfold Cons, simp) + finally show ?thesis . + qed +qed + +fun restn :: "nat \ 'a list \ 'a list" +where "restn n s = rev (firstn (length s - n) (rev s))" + +definition moment :: "nat \ 'a list \ 'a list" +where "moment n s = rev (firstn n (rev s))" + +definition restm :: "nat \ 'a list \ 'a list" +where "restm n s = rev (restn n (rev s))" + +definition from_to :: "nat \ nat \ 'a list \ 'a list" + where "from_to i j s = firstn (j - i) (restn i s)" + +definition down_to :: "nat \ nat \ 'a list \ 'a list" +where "down_to j i s = rev (from_to i j (rev s))" + +value "down_to 6 2 [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]" +value "from_to 2 6 [0, 1, 2, 3, 4, 5, 6, 7]" + +lemma length_eq_elim_l: "\length xs = length ys; xs@us = ys@vs\ \ xs = ys \ us = vs" + by auto + +lemma length_eq_elim_r: "\length us = length vs; xs@us = ys@vs\ \ xs = ys \ us = vs" + by simp + +lemma firstn_nil [simp]: "firstn n [] = []" + by (cases n, simp+) + + +value "from_to 0 2 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] @ + from_to 2 5 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" + +lemma firstn_le: "\ n s'. n \ length s \ firstn n (s@s') = firstn n s" +proof (induct s, simp) + fix a s n s' + assume ih: "\n s'. n \ length s \ firstn n (s @ s') = firstn n s" + and le_n: " n \ length (a # s)" + show "firstn n ((a # s) @ s') = firstn n (a # s)" + proof(cases n, simp) + fix k + assume eq_n: "n = Suc k" + with le_n have "k \ length s" by auto + from ih [OF this] and eq_n + show "firstn n ((a # s) @ s') = firstn n (a # s)" by auto + qed +qed + +lemma firstn_ge [simp]: "\n. length s \ n \ firstn n s = s" +proof(induct s, simp) + fix a s n + assume ih: "\n. length s \ n \ firstn n s = s" + and le: "length (a # s) \ n" + show "firstn n (a # s) = a # s" + proof(cases n) + assume eq_n: "n = 0" with le show ?thesis by simp + next + fix k + assume eq_n: "n = Suc k" + with le have le_k: "length s \ k" by simp + from ih [OF this] have "firstn k s = s" . + from eq_n and this + show ?thesis by simp + qed +qed + +lemma firstn_eq [simp]: "firstn (length s) s = s" + by simp + +lemma firstn_restn_s: "(firstn n (s::'a list)) @ (restn n s) = s" +proof(induct n arbitrary:s, simp) + fix n s + assume ih: "\t. firstn n (t::'a list) @ restn n t = t" + show "firstn (Suc n) (s::'a list) @ restn (Suc n) s = s" + proof(cases s, simp) + fix x xs + assume eq_s: "s = x#xs" + show "firstn (Suc n) s @ restn (Suc n) s = s" + proof - + have "firstn (Suc n) s @ restn (Suc n) s = x # (firstn n xs @ restn n xs)" + proof - + from eq_s have "firstn (Suc n) s = x # firstn n xs" by simp + moreover have "restn (Suc n) s = restn n xs" + proof - + from eq_s have "restn (Suc n) s = rev (firstn (length xs - n) (rev xs @ [x]))" by simp + also have "\ = restn n xs" + proof - + have "(firstn (length xs - n) (rev xs @ [x])) = (firstn (length xs - n) (rev xs))" + by(rule firstn_le, simp) + hence "rev (firstn (length xs - n) (rev xs @ [x])) = + rev (firstn (length xs - n) (rev xs))" by simp + also have "\ = rev (firstn (length (rev xs) - n) (rev xs))" by simp + finally show ?thesis by simp + qed + finally show ?thesis by simp + qed + ultimately show ?thesis by simp + qed with ih eq_s show ?thesis by simp + qed + qed +qed + +lemma moment_restm_s: "(restm n s)@(moment n s) = s" +proof - + have " rev ((firstn n (rev s)) @ (restn n (rev s))) = s" (is "rev ?x = s") + proof - + have "?x = rev s" by (simp only:firstn_restn_s) + thus ?thesis by auto + qed + thus ?thesis + by (auto simp:restm_def moment_def) +qed + +declare restn.simps [simp del] firstn.simps[simp del] + +lemma length_firstn_ge: "length s \ n \ length (firstn n s) = length s" +proof(induct n arbitrary:s, simp add:firstn.simps) + case (Suc k) + assume ih: "\ s. length (s::'a list) \ k \ length (firstn k s) = length s" + and le: "length s \ Suc k" + show ?case + proof(cases s) + case Nil + from Nil show ?thesis by simp + next + case (Cons x xs) + from le and Cons have "length xs \ k" by simp + from ih [OF this] have "length (firstn k xs) = length xs" . + moreover from Cons have "length (firstn (Suc k) s) = Suc (length (firstn k xs))" + by (simp add:firstn.simps) + moreover note Cons + ultimately show ?thesis by simp + qed +qed + +lemma length_firstn_le: "n \ length s \ length (firstn n s) = n" +proof(induct n arbitrary:s, simp add:firstn.simps) + case (Suc k) + assume ih: "\s. k \ length (s::'a list) \ length (firstn k s) = k" + and le: "Suc k \ length s" + show ?case + proof(cases s) + case Nil + from Nil and le show ?thesis by auto + next + case (Cons x xs) + from le and Cons have "k \ length xs" by simp + from ih [OF this] have "length (firstn k xs) = k" . + moreover from Cons have "length (firstn (Suc k) s) = Suc (length (firstn k xs))" + by (simp add:firstn.simps) + ultimately show ?thesis by simp + qed +qed + +lemma app_firstn_restn: + fixes s1 s2 + shows "s1 = firstn (length s1) (s1 @ s2) \ s2 = restn (length s1) (s1 @ s2)" +proof(rule length_eq_elim_l) + have "length s1 \ length (s1 @ s2)" by simp + from length_firstn_le [OF this] + show "length s1 = length (firstn (length s1) (s1 @ s2))" by simp +next + from firstn_restn_s + show "s1 @ s2 = firstn (length s1) (s1 @ s2) @ restn (length s1) (s1 @ s2)" + by metis +qed + + +lemma length_moment_le: + fixes k s + assumes le_k: "k \ length s" + shows "length (moment k s) = k" +proof - + have "length (rev (firstn k (rev s))) = k" + proof - + have "length (rev (firstn k (rev s))) = length (firstn k (rev s))" by simp + also have "\ = k" + proof(rule length_firstn_le) + from le_k show "k \ length (rev s)" by simp + qed + finally show ?thesis . + qed + thus ?thesis by (simp add:moment_def) +qed + +lemma app_moment_restm: + fixes s1 s2 + shows "s1 = restm (length s2) (s1 @ s2) \ s2 = moment (length s2) (s1 @ s2)" +proof(rule length_eq_elim_r) + have "length s2 \ length (s1 @ s2)" by simp + from length_moment_le [OF this] + show "length s2 = length (moment (length s2) (s1 @ s2))" by simp +next + from moment_restm_s + show "s1 @ s2 = restm (length s2) (s1 @ s2) @ moment (length s2) (s1 @ s2)" + by metis +qed + +lemma length_moment_ge: + fixes k s + assumes le_k: "length s \ k" + shows "length (moment k s) = (length s)" +proof - + have "length (rev (firstn k (rev s))) = length s" + proof - + have "length (rev (firstn k (rev s))) = length (firstn k (rev s))" by simp + also have "\ = length s" + proof - + have "\ = length (rev s)" + proof(rule length_firstn_ge) + from le_k show "length (rev s) \ k" by simp + qed + also have "\ = length s" by simp + finally show ?thesis . + qed + finally show ?thesis . + qed + thus ?thesis by (simp add:moment_def) +qed + +lemma length_firstn: "(length (firstn n s) = length s) \ (length (firstn n s) = n)" +proof(cases "n \ length s") + case True + from length_firstn_le [OF True] show ?thesis by auto +next + case False + from False have "length s \ n" by simp + from firstn_ge [OF this] show ?thesis by auto +qed + +lemma firstn_conc: + fixes m n + assumes le_mn: "m \ n" + shows "firstn m s = firstn m (firstn n s)" +proof(cases "m \ length s") + case True + have "s = (firstn n s) @ (restn n s)" by (simp add:firstn_restn_s) + hence "firstn m s = firstn m \" by simp + also have "\ = firstn m (firstn n s)" + proof - + from length_firstn [of n s] + have "m \ length (firstn n s)" + proof + assume "length (firstn n s) = length s" with True show ?thesis by simp + next + assume "length (firstn n s) = n " with le_mn show ?thesis by simp + qed + from firstn_le [OF this, of "restn n s"] + show ?thesis . + qed + finally show ?thesis by simp +next + case False + from False and le_mn have "length s \ n" by simp + from firstn_ge [OF this] show ?thesis by simp +qed + +lemma restn_conc: + fixes i j k s + assumes eq_k: "j + i = k" + shows "restn k s = restn j (restn i s)" +proof - + have "(firstn (length s - k) (rev s)) = + (firstn (length (rev (firstn (length s - i) (rev s))) - j) + (rev (rev (firstn (length s - i) (rev s)))))" + proof - + have "(firstn (length s - k) (rev s)) = + (firstn (length (rev (firstn (length s - i) (rev s))) - j) + (firstn (length s - i) (rev s)))" + proof - + have " (length (rev (firstn (length s - i) (rev s))) - j) = length s - k" + proof - + have "(length (rev (firstn (length s - i) (rev s))) - j) = (length s - i) - j" + proof - + have "(length (rev (firstn (length s - i) (rev s))) - j) = + length ((firstn (length s - i) (rev s))) - j" + by simp + also have "\ = length ((firstn (length (rev s) - i) (rev s))) - j" by simp + also have "\ = (length (rev s) - i) - j" + proof - + have "length ((firstn (length (rev s) - i) (rev s))) = (length (rev s) - i)" + by (rule length_firstn_le, simp) + thus ?thesis by simp + qed + also have "\ = (length s - i) - j" by simp + finally show ?thesis . + qed + with eq_k show ?thesis by auto + qed + moreover have "(firstn (length s - k) (rev s)) = + (firstn (length s - k) (firstn (length s - i) (rev s)))" + proof(rule firstn_conc) + from eq_k show "length s - k \ length s - i" by simp + qed + ultimately show ?thesis by simp + qed + thus ?thesis by simp + qed + thus ?thesis by (simp only:restn.simps) +qed + +(* +value "down_to 2 0 [5, 4, 3, 2, 1, 0]" +value "moment 2 [5, 4, 3, 2, 1, 0]" +*) + +lemma from_to_firstn: "from_to 0 k s = firstn k s" +by (simp add:from_to_def restn.simps) + +lemma moment_app [simp]: + assumes + ile: "i \ length s" + shows "moment i (s'@s) = moment i s" +proof - + have "moment i (s'@s) = rev (firstn i (rev (s'@s)))" by (simp add:moment_def) + moreover have "firstn i (rev (s'@s)) = firstn i (rev s @ rev s')" by simp + moreover have "\ = firstn i (rev s)" + proof(rule firstn_le) + have "length (rev s) = length s" by simp + with ile show "i \ length (rev s)" by simp + qed + ultimately show ?thesis by (simp add:moment_def) +qed + +lemma moment_eq [simp]: "moment (length s) (s'@s) = s" +proof - + have "length s \ length s" by simp + from moment_app [OF this, of s'] + have " moment (length s) (s' @ s) = moment (length s) s" . + moreover have "\ = s" by (simp add:moment_def) + ultimately show ?thesis by simp +qed + +lemma moment_ge [simp]: "length s \ n \ moment n s = s" + by (unfold moment_def, simp) + +lemma moment_zero [simp]: "moment 0 s = []" + by (simp add:moment_def firstn.simps) + +lemma p_split_gen: + "\Q s; \ Q (moment k s)\ \ + (\ i. i < length s \ k \ i \ \ Q (moment i s) \ (\ i' > i. Q (moment i' s)))" +proof (induct s, simp) + fix a s + assume ih: "\Q s; \ Q (moment k s)\ + \ \i i \ \ Q (moment i s) \ (\i'>i. Q (moment i' s))" + and nq: "\ Q (moment k (a # s))" and qa: "Q (a # s)" + have le_k: "k \ length s" + proof - + { assume "length s < k" + hence "length (a#s) \ k" by simp + from moment_ge [OF this] and nq and qa + have "False" by auto + } thus ?thesis by arith + qed + have nq_k: "\ Q (moment k s)" + proof - + have "moment k (a#s) = moment k s" + proof - + from moment_app [OF le_k, of "[a]"] show ?thesis by simp + qed + with nq show ?thesis by simp + qed + show "\i i \ \ Q (moment i (a # s)) \ (\i'>i. Q (moment i' (a # s)))" + proof - + { assume "Q s" + from ih [OF this nq_k] + obtain i where lti: "i < length s" + and nq: "\ Q (moment i s)" + and rst: "\i'>i. Q (moment i' s)" + and lki: "k \ i" by auto + have ?thesis + proof - + from lti have "i < length (a # s)" by auto + moreover have " \ Q (moment i (a # s))" + proof - + from lti have "i \ (length s)" by simp + from moment_app [OF this, of "[a]"] + have "moment i (a # s) = moment i s" by simp + with nq show ?thesis by auto + qed + moreover have " (\i'>i. Q (moment i' (a # s)))" + proof - + { + fix i' + assume lti': "i < i'" + have "Q (moment i' (a # s))" + proof(cases "length (a#s) \ i'") + case True + from True have "moment i' (a#s) = a#s" by simp + with qa show ?thesis by simp + next + case False + from False have "i' \ length s" by simp + from moment_app [OF this, of "[a]"] + have "moment i' (a#s) = moment i' s" by simp + with rst lti' show ?thesis by auto + qed + } thus ?thesis by auto + qed + moreover note lki + ultimately show ?thesis by auto + qed + } moreover { + assume ns: "\ Q s" + have ?thesis + proof - + let ?i = "length s" + have "\ Q (moment ?i (a#s))" + proof - + have "?i \ length s" by simp + from moment_app [OF this, of "[a]"] + have "moment ?i (a#s) = moment ?i s" by simp + moreover have "\ = s" by simp + ultimately show ?thesis using ns by auto + qed + moreover have "\ i' > ?i. Q (moment i' (a#s))" + proof - + { fix i' + assume "i' > ?i" + hence "length (a#s) \ i'" by simp + from moment_ge [OF this] + have " moment i' (a # s) = a # s" . + with qa have "Q (moment i' (a#s))" by simp + } thus ?thesis by auto + qed + moreover have "?i < length (a#s)" by simp + moreover note le_k + ultimately show ?thesis by auto + qed + } ultimately show ?thesis by auto + qed +qed + +lemma p_split: + "\ s Q. \Q s; \ Q []\ \ + (\ i. i < length s \ \ Q (moment i s) \ (\ i' > i. Q (moment i' s)))" +proof - + fix s Q + assume qs: "Q s" and nq: "\ Q []" + from nq have "\ Q (moment 0 s)" by simp + from p_split_gen [of Q s 0, OF qs this] + show "(\ i. i < length s \ \ Q (moment i s) \ (\ i' > i. Q (moment i' s)))" + by auto +qed + +lemma moment_plus: + "Suc i \ length s \ moment (Suc i) s = (hd (moment (Suc i) s)) # (moment i s)" +proof(induct s, simp+) + fix a s + assume ih: "Suc i \ length s \ moment (Suc i) s = hd (moment (Suc i) s) # moment i s" + and le_i: "i \ length s" + show "moment (Suc i) (a # s) = hd (moment (Suc i) (a # s)) # moment i (a # s)" + proof(cases "i= length s") + case True + hence "Suc i = length (a#s)" by simp + with moment_eq have "moment (Suc i) (a#s) = a#s" by auto + moreover have "moment i (a#s) = s" + proof - + from moment_app [OF le_i, of "[a]"] + and True show ?thesis by simp + qed + ultimately show ?thesis by auto + next + case False + from False and le_i have lti: "i < length s" by arith + hence les_i: "Suc i \ length s" by arith + show ?thesis + proof - + from moment_app [OF les_i, of "[a]"] + have "moment (Suc i) (a # s) = moment (Suc i) s" by simp + moreover have "moment i (a#s) = moment i s" + proof - + from lti have "i \ length s" by simp + from moment_app [OF this, of "[a]"] show ?thesis by simp + qed + moreover note ih [OF les_i] + ultimately show ?thesis by auto + qed + qed +qed + +lemma from_to_conc: + fixes i j k s + assumes le_ij: "i \ j" + and le_jk: "j \ k" + shows "from_to i j s @ from_to j k s = from_to i k s" +proof - + let ?ris = "restn i s" + have "firstn (j - i) (restn i s) @ firstn (k - j) (restn j s) = + firstn (k - i) (restn i s)" (is "?x @ ?y = ?z") + proof - + let "firstn (k-j) ?u" = "?y" + let ?rst = " restn (k - j) (restn (j - i) ?ris)" + let ?rst' = "restn (k - i) ?ris" + have "?u = restn (j-i) ?ris" + proof(rule restn_conc) + from le_ij show "j - i + i = j" by simp + qed + hence "?x @ ?y = ?x @ firstn (k-j) (restn (j-i) ?ris)" by simp + moreover have "firstn (k - j) (restn (j - i) (restn i s)) @ ?rst = + restn (j-i) ?ris" by (simp add:firstn_restn_s) + ultimately have "?x @ ?y @ ?rst = ?x @ (restn (j-i) ?ris)" by simp + also have "\ = ?ris" by (simp add:firstn_restn_s) + finally have "?x @ ?y @ ?rst = ?ris" . + moreover have "?z @ ?rst = ?ris" + proof - + have "?z @ ?rst' = ?ris" by (simp add:firstn_restn_s) + moreover have "?rst' = ?rst" + proof(rule restn_conc) + from le_ij le_jk show "k - j + (j - i) = k - i" by auto + qed + ultimately show ?thesis by simp + qed + ultimately have "?x @ ?y @ ?rst = ?z @ ?rst" by simp + thus ?thesis by auto + qed + thus ?thesis by (simp only:from_to_def) +qed + +lemma down_to_conc: + fixes i j k s + assumes le_ij: "i \ j" + and le_jk: "j \ k" + shows "down_to k j s @ down_to j i s = down_to k i s" +proof - + have "rev (from_to j k (rev s)) @ rev (from_to i j (rev s)) = rev (from_to i k (rev s))" + (is "?L = ?R") + proof - + have "?L = rev (from_to i j (rev s) @ from_to j k (rev s))" by simp + also have "\ = ?R" (is "rev ?x = rev ?y") + proof - + have "?x = ?y" by (rule from_to_conc[OF le_ij le_jk]) + thus ?thesis by simp + qed + finally show ?thesis . + qed + thus ?thesis by (simp add:down_to_def) +qed + +lemma restn_ge: + fixes s k + assumes le_k: "length s \ k" + shows "restn k s = []" +proof - + from firstn_restn_s [of k s, symmetric] have "s = (firstn k s) @ (restn k s)" . + hence "length s = length \" by simp + also have "\ = length (firstn k s) + length (restn k s)" by simp + finally have "length s = ..." by simp + moreover from length_firstn_ge and le_k + have "length (firstn k s) = length s" by simp + ultimately have "length (restn k s) = 0" by auto + thus ?thesis by auto +qed + +lemma from_to_ge: "length s \ k \ from_to k j s = []" +proof(simp only:from_to_def) + assume "length s \ k" + from restn_ge [OF this] + show "firstn (j - k) (restn k s) = []" by simp +qed + +(* +value "from_to 2 5 [0, 1, 2, 3, 4]" +value "restn 2 [0, 1, 2, 3, 4]" +*) + +lemma from_to_restn: + fixes k j s + assumes le_j: "length s \ j" + shows "from_to k j s = restn k s" +proof - + have "from_to 0 k s @ from_to k j s = from_to 0 j s" + proof(cases "k \ j") + case True + from from_to_conc True show ?thesis by auto + next + case False + from False le_j have lek: "length s \ k" by auto + from from_to_ge [OF this] have "from_to k j s = []" . + hence "from_to 0 k s @ from_to k j s = from_to 0 k s" by simp + also have "\ = s" + proof - + from from_to_firstn [of k s] + have "\ = firstn k s" . + also have "\ = s" by (rule firstn_ge [OF lek]) + finally show ?thesis . + qed + finally have "from_to 0 k s @ from_to k j s = s" . + moreover have "from_to 0 j s = s" + proof - + have "from_to 0 j s = firstn j s" by (simp add:from_to_firstn) + also have "\ = s" + proof(rule firstn_ge) + from le_j show "length s \ j " by simp + qed + finally show ?thesis . + qed + ultimately show ?thesis by auto + qed + also have "\ = s" + proof - + from from_to_firstn have "\ = firstn j s" . + also have "\ = s" + proof(rule firstn_ge) + from le_j show "length s \ j" by simp + qed + finally show ?thesis . + qed + finally have "from_to 0 k s @ from_to k j s = s" . + moreover have "from_to 0 k s @ restn k s = s" + proof - + from from_to_firstn [of k s] + have "from_to 0 k s = firstn k s" . + thus ?thesis by (simp add:firstn_restn_s) + qed + ultimately have "from_to 0 k s @ from_to k j s = + from_to 0 k s @ restn k s" by simp + thus ?thesis by auto +qed + +lemma down_to_moment: "down_to k 0 s = moment k s" +proof - + have "rev (from_to 0 k (rev s)) = rev (firstn k (rev s))" + using from_to_firstn by metis + thus ?thesis by (simp add:down_to_def moment_def) +qed + +lemma down_to_restm: + assumes le_s: "length s \ j" + shows "down_to j k s = restm k s" +proof - + have "rev (from_to k j (rev s)) = rev (restn k (rev s))" (is "?L = ?R") + proof - + from le_s have "length (rev s) \ j" by simp + from from_to_restn [OF this, of k] show ?thesis by simp + qed + thus ?thesis by (simp add:down_to_def restm_def) +qed + +lemma moment_split: "moment (m+i) s = down_to (m+i) i s @down_to i 0 s" +proof - + have "moment (m + i) s = down_to (m+i) 0 s" using down_to_moment by metis + also have "\ = (down_to (m+i) i s) @ (down_to i 0 s)" + by(rule down_to_conc[symmetric], auto) + finally show ?thesis . +qed + +lemma length_restn: "length (restn i s) = length s - i" +proof(cases "i \ length s") + case True + from length_firstn_le [OF this] have "length (firstn i s) = i" . + moreover have "length s = length (firstn i s) + length (restn i s)" + proof - + have "s = firstn i s @ restn i s" using firstn_restn_s by metis + hence "length s = length \" by simp + thus ?thesis by simp + qed + ultimately show ?thesis by simp +next + case False + hence "length s \ i" by simp + from restn_ge [OF this] have "restn i s = []" . + with False show ?thesis by simp +qed + +lemma length_from_to_in: + fixes i j s + assumes le_ij: "i \ j" + and le_j: "j \ length s" + shows "length (from_to i j s) = j - i" +proof - + have "from_to 0 j s = from_to 0 i s @ from_to i j s" + by (rule from_to_conc[symmetric, OF _ le_ij], simp) + moreover have "length (from_to 0 j s) = j" + proof - + have "from_to 0 j s = firstn j s" using from_to_firstn by metis + moreover have "length \ = j" by (rule length_firstn_le [OF le_j]) + ultimately show ?thesis by simp + qed + moreover have "length (from_to 0 i s) = i" + proof - + have "from_to 0 i s = firstn i s" using from_to_firstn by metis + moreover have "length \ = i" + proof (rule length_firstn_le) + from le_ij le_j show "i \ length s" by simp + qed + ultimately show ?thesis by simp + qed + ultimately show ?thesis by auto +qed + +lemma firstn_restn_from_to: "from_to i (m + i) s = firstn m (restn i s)" +proof(cases "m+i \ length s") + case True + have "restn i s = from_to i (m+i) s @ from_to (m+i) (length s) s" + proof - + have "restn i s = from_to i (length s) s" + by(rule from_to_restn[symmetric], simp) + also have "\ = from_to i (m+i) s @ from_to (m+i) (length s) s" + by(rule from_to_conc[symmetric, OF _ True], simp) + finally show ?thesis . + qed + hence "firstn m (restn i s) = firstn m \" by simp + moreover have "\ = firstn (length (from_to i (m+i) s)) + (from_to i (m+i) s @ from_to (m+i) (length s) s)" + proof - + have "length (from_to i (m+i) s) = m" + proof - + have "length (from_to i (m+i) s) = (m+i) - i" + by(rule length_from_to_in [OF _ True], simp) + thus ?thesis by simp + qed + thus ?thesis by simp + qed + ultimately show ?thesis using app_firstn_restn by metis +next + case False + hence "length s \ m + i" by simp + from from_to_restn [OF this] + have "from_to i (m + i) s = restn i s" . + moreover have "firstn m (restn i s) = restn i s" + proof(rule firstn_ge) + show "length (restn i s) \ m" + proof - + have "length (restn i s) = length s - i" using length_restn by metis + with False show ?thesis by simp + qed + qed + ultimately show ?thesis by simp +qed + +lemma down_to_moment_restm: + fixes m i s + shows "down_to (m + i) i s = moment m (restm i s)" + by (simp add:firstn_restn_from_to down_to_def moment_def restm_def) + +lemma moment_plus_split: + fixes m i s + shows "moment (m + i) s = moment m (restm i s) @ moment i s" +proof - + from moment_split [of m i s] + have "moment (m + i) s = down_to (m + i) i s @ down_to i 0 s" . + also have "\ = down_to (m+i) i s @ moment i s" using down_to_moment by simp + also from down_to_moment_restm have "\ = moment m (restm i s) @ moment i s" + by simp + finally show ?thesis . +qed + +lemma length_restm: "length (restm i s) = length s - i" +proof - + have "length (rev (restn i (rev s))) = length s - i" (is "?L = ?R") + proof - + have "?L = length (restn i (rev s))" by simp + also have "\ = length (rev s) - i" using length_restn by metis + also have "\ = ?R" by simp + finally show ?thesis . + qed + thus ?thesis by (simp add:restm_def) +qed + +lemma moment_prefix: "(moment i t @ s) = moment (i + length s) (t @ s)" +proof - + from moment_plus_split [of i "length s" "t@s"] + have " moment (i + length s) (t @ s) = moment i (restm (length s) (t @ s)) @ moment (length s) (t @ s)" + by auto + with app_moment_restm[of t s] + have "moment (i + length s) (t @ s) = moment i t @ moment (length s) (t @ s)" by simp + with moment_app show ?thesis by auto +qed + +lemma length_down_to_in: + assumes le_ij: "i \ j" + and le_js: "j \ length s" + shows "length (down_to j i s) = j - i" +proof - + have "length (down_to j i s) = length (from_to i j (rev s))" + by (unfold down_to_def, auto) + also have "\ = j - i" + proof(rule length_from_to_in[OF le_ij]) + from le_js show "j \ length (rev s)" by simp + qed + finally show ?thesis . +qed + + +lemma moment_head: + assumes le_it: "Suc i \ length t" + obtains e where "moment (Suc i) t = e#moment i t" +proof - + have "i \ Suc i" by simp + from length_down_to_in [OF this le_it] + have "length (down_to (Suc i) i t) = 1" by auto + then obtain e where "down_to (Suc i) i t = [e]" + apply (cases "(down_to (Suc i) i t)") by auto + moreover have "down_to (Suc i) 0 t = down_to (Suc i) i t @ down_to i 0 t" + by (rule down_to_conc[symmetric], auto) + ultimately have eq_me: "moment (Suc i) t = e#(moment i t)" + by (auto simp:down_to_moment) + from that [OF this] show ?thesis . +qed + +end diff -r 17305a85493d -r c495eb16beb6 draf.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/draf.txt Wed Jan 27 19:28:42 2016 +0800 @@ -0,0 +1,11 @@ +There are low priority threads, +which do not hold any resources, +such thread will not block th. +Theorem 3 does not exclude such threads. + +There are resources, which are not held by any low prioirty threads, +such resources can not cause blockage of th neither. And similiary, +theorem 6 does not exlude them. + +Our one bound excudle them by using a different formaulation. +