# HG changeset patch # User Christian Urban # Date 1452098066 0 # Node ID b4bcd1edbb6d1b9fb20819f493b32030c996e4ba # Parent b620a2a0806ac7e80551db57df2da6a2e7933089 renamed files diff -r b620a2a0806a -r b4bcd1edbb6d Correctness.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Correctness.thy Wed Jan 06 16:34:26 2016 +0000 @@ -0,0 +1,922 @@ +theory Correctness +imports PIPBasics Implementation +begin + +text {* + The following two auxiliary lemmas are used to reason about @{term Max}. +*} +lemma image_Max_eqI: + assumes "finite B" + and "b \ B" + and "\ x \ B. f x \ f b" + shows "Max (f ` B) = f b" + using assms + using Max_eqI by blast + +lemma image_Max_subset: + assumes "finite A" + and "B \ A" + and "a \ B" + and "Max (f ` A) = f a" + shows "Max (f ` B) = f a" +proof(rule image_Max_eqI) + show "finite B" + using assms(1) assms(2) finite_subset by auto +next + show "a \ B" using assms by simp +next + show "\x\B. f x \ f a" + by (metis Max_ge assms(1) assms(2) assms(4) + finite_imageI image_eqI subsetCE) +qed + +text {* + The following locale @{text "highest_gen"} sets the basic context for our + investigation: supposing thread @{text th} holds the highest @{term cp}-value + in state @{text s}, which means the task for @{text th} is the + most urgent. We want to show that + @{text th} is treated correctly by PIP, which means + @{text th} will not be blocked unreasonably by other less urgent + threads. +*} +locale highest_gen = + fixes s th prio tm + assumes vt_s: "vt s" + and threads_s: "th \ threads s" + and highest: "preced th s = Max ((cp s)`threads s)" + -- {* The internal structure of @{term th}'s precedence is exposed:*} + and preced_th: "preced th s = Prc prio tm" + +-- {* @{term s} is a valid trace, so it will inherit all results derived for + a valid trace: *} +sublocale highest_gen < vat_s: valid_trace "s" + by (unfold_locales, insert vt_s, simp) + +context highest_gen +begin + +text {* + @{term tm} is the time when the precedence of @{term th} is set, so + @{term tm} must be a valid moment index into @{term s}. +*} +lemma lt_tm: "tm < length s" + by (insert preced_tm_lt[OF threads_s preced_th], simp) + +text {* + Since @{term th} holds the highest precedence and @{text "cp"} + is the highest precedence of all threads in the sub-tree of + @{text "th"} and @{text th} is among these threads, + its @{term cp} must equal to its precedence: +*} +lemma eq_cp_s_th: "cp s th = preced th s" (is "?L = ?R") +proof - + have "?L \ ?R" + by (unfold highest, rule Max_ge, + auto simp:threads_s finite_threads) + moreover have "?R \ ?L" + by (unfold vat_s.cp_rec, rule Max_ge, + auto simp:the_preced_def vat_s.fsbttRAGs.finite_children) + ultimately show ?thesis by auto +qed + +(* ccc *) +lemma highest_cp_preced: "cp s th = Max ((\ th'. preced th' s) ` threads s)" + by (fold max_cp_eq, unfold eq_cp_s_th, insert highest, simp) + +lemma highest_preced_thread: "preced th s = Max ((\ th'. preced th' s) ` threads s)" + by (fold eq_cp_s_th, unfold highest_cp_preced, simp) + +lemma highest': "cp s th = Max (cp s ` threads s)" +proof - + from highest_cp_preced max_cp_eq[symmetric] + show ?thesis by simp +qed + +end + +locale extend_highest_gen = highest_gen + + fixes t + assumes vt_t: "vt (t@s)" + and create_low: "Create th' prio' \ set t \ prio' \ prio" + and set_diff_low: "Set th' prio' \ set t \ th' \ th \ prio' \ prio" + and exit_diff: "Exit th' \ set t \ th' \ th" + +sublocale extend_highest_gen < vat_t: valid_trace "t@s" + by (unfold_locales, insert vt_t, simp) + +lemma step_back_vt_app: + assumes vt_ts: "vt (t@s)" + shows "vt s" +proof - + from vt_ts show ?thesis + proof(induct t) + case Nil + from Nil show ?case by auto + next + case (Cons e t) + assume ih: " vt (t @ s) \ vt s" + and vt_et: "vt ((e # t) @ s)" + show ?case + proof(rule ih) + show "vt (t @ s)" + proof(rule step_back_vt) + from vt_et show "vt (e # t @ s)" by simp + qed + qed + qed +qed + + +locale red_extend_highest_gen = extend_highest_gen + + fixes i::nat + +sublocale red_extend_highest_gen < red_moment: extend_highest_gen "s" "th" "prio" "tm" "(moment i t)" + apply (insert extend_highest_gen_axioms, subst (asm) (1) moment_restm_s [of i t, symmetric]) + apply (unfold extend_highest_gen_def extend_highest_gen_axioms_def, clarsimp) + by (unfold highest_gen_def, auto dest:step_back_vt_app) + + +context extend_highest_gen +begin + + lemma ind [consumes 0, case_names Nil Cons, induct type]: + assumes + h0: "R []" + and h2: "\ e t. \vt (t@s); step (t@s) e; + extend_highest_gen s th prio tm t; + extend_highest_gen s th prio tm (e#t); R t\ \ R (e#t)" + shows "R t" +proof - + from vt_t extend_highest_gen_axioms show ?thesis + proof(induct t) + from h0 show "R []" . + next + case (Cons e t') + assume ih: "\vt (t' @ s); extend_highest_gen s th prio tm t'\ \ R t'" + and vt_e: "vt ((e # t') @ s)" + and et: "extend_highest_gen s th prio tm (e # t')" + from vt_e and step_back_step have stp: "step (t'@s) e" by auto + from vt_e and step_back_vt have vt_ts: "vt (t'@s)" by auto + show ?case + proof(rule h2 [OF vt_ts stp _ _ _ ]) + show "R t'" + proof(rule ih) + from et show ext': "extend_highest_gen s th prio tm t'" + by (unfold extend_highest_gen_def extend_highest_gen_axioms_def, auto dest:step_back_vt) + next + from vt_ts show "vt (t' @ s)" . + qed + next + from et show "extend_highest_gen s th prio tm (e # t')" . + next + from et show ext': "extend_highest_gen s th prio tm t'" + by (unfold extend_highest_gen_def extend_highest_gen_axioms_def, auto dest:step_back_vt) + qed + qed +qed + + +lemma th_kept: "th \ threads (t @ s) \ + preced th (t@s) = preced th s" (is "?Q t") +proof - + show ?thesis + proof(induct rule:ind) + case Nil + from threads_s + show ?case + by auto + next + case (Cons e t) + interpret h_e: extend_highest_gen _ _ _ _ "(e # t)" using Cons by auto + interpret h_t: extend_highest_gen _ _ _ _ t using Cons by auto + show ?case + proof(cases e) + case (Create thread prio) + show ?thesis + proof - + from Cons and Create have "step (t@s) (Create thread prio)" by auto + hence "th \ thread" + proof(cases) + case thread_create + with Cons show ?thesis by auto + qed + hence "preced th ((e # t) @ s) = preced th (t @ s)" + by (unfold Create, auto simp:preced_def) + moreover note Cons + ultimately show ?thesis + by (auto simp:Create) + qed + next + case (Exit thread) + from h_e.exit_diff and Exit + have neq_th: "thread \ th" by auto + with Cons + show ?thesis + by (unfold Exit, auto simp:preced_def) + next + case (P thread cs) + with Cons + show ?thesis + by (auto simp:P preced_def) + next + case (V thread cs) + with Cons + show ?thesis + by (auto simp:V preced_def) + next + case (Set thread prio') + show ?thesis + proof - + from h_e.set_diff_low and Set + have "th \ thread" by auto + hence "preced th ((e # t) @ s) = preced th (t @ s)" + by (unfold Set, auto simp:preced_def) + moreover note Cons + ultimately show ?thesis + by (auto simp:Set) + qed + qed + qed +qed + +text {* + According to @{thm th_kept}, thread @{text "th"} has its living status + and precedence kept along the way of @{text "t"}. The following lemma + shows that this preserved precedence of @{text "th"} remains as the highest + along the way of @{text "t"}. + + The proof goes by induction over @{text "t"} using the specialized + induction rule @{thm ind}, followed by case analysis of each possible + operations of PIP. All cases follow the same pattern rendered by the + generalized introduction rule @{thm "image_Max_eqI"}. + + The very essence is to show that precedences, no matter whether they are newly introduced + or modified, are always lower than the one held by @{term "th"}, + which by @{thm th_kept} is preserved along the way. +*} +lemma max_kept: "Max (the_preced (t @ s) ` (threads (t@s))) = preced th s" +proof(induct rule:ind) + case Nil + from highest_preced_thread + show ?case + by (unfold the_preced_def, simp) +next + case (Cons e t) + interpret h_e: extend_highest_gen _ _ _ _ "(e # t)" using Cons by auto + interpret h_t: extend_highest_gen _ _ _ _ t using Cons by auto + show ?case + proof(cases e) + case (Create thread prio') + show ?thesis (is "Max (?f ` ?A) = ?t") + proof - + -- {* The following is the common pattern of each branch of the case analysis. *} + -- {* The major part is to show that @{text "th"} holds the highest precedence: *} + have "Max (?f ` ?A) = ?f th" + proof(rule image_Max_eqI) + show "finite ?A" using h_e.finite_threads by auto + next + show "th \ ?A" using h_e.th_kept by auto + next + show "\x\?A. ?f x \ ?f th" + proof + fix x + assume "x \ ?A" + hence "x = thread \ x \ threads (t@s)" by (auto simp:Create) + thus "?f x \ ?f th" + proof + assume "x = thread" + thus ?thesis + apply (simp add:Create the_preced_def preced_def, fold preced_def) + using Create h_e.create_low h_t.th_kept lt_tm preced_leI2 preced_th by force + next + assume h: "x \ threads (t @ s)" + from Cons(2)[unfolded Create] + have "x \ thread" using h by (cases, auto) + hence "?f x = the_preced (t@s) x" + by (simp add:Create the_preced_def preced_def) + hence "?f x \ Max (the_preced (t@s) ` threads (t@s))" + by (simp add: h_t.finite_threads h) + also have "... = ?f th" + by (metis Cons.hyps(5) h_e.th_kept the_preced_def) + finally show ?thesis . + qed + qed + qed + -- {* The minor part is to show that the precedence of @{text "th"} + equals to preserved one, given by the foregoing lemma @{thm th_kept} *} + also have "... = ?t" using h_e.th_kept the_preced_def by auto + -- {* Then it follows trivially that the precedence preserved + for @{term "th"} remains the maximum of all living threads along the way. *} + finally show ?thesis . + qed + next + case (Exit thread) + show ?thesis (is "Max (?f ` ?A) = ?t") + proof - + have "Max (?f ` ?A) = ?f th" + proof(rule image_Max_eqI) + show "finite ?A" using h_e.finite_threads by auto + next + show "th \ ?A" using h_e.th_kept by auto + next + show "\x\?A. ?f x \ ?f th" + proof + fix x + assume "x \ ?A" + hence "x \ threads (t@s)" by (simp add: Exit) + hence "?f x \ Max (?f ` threads (t@s))" + by (simp add: h_t.finite_threads) + also have "... \ ?f th" + apply (simp add:Exit the_preced_def preced_def, fold preced_def) + using Cons.hyps(5) h_t.th_kept the_preced_def by auto + finally show "?f x \ ?f th" . + qed + qed + also have "... = ?t" using h_e.th_kept the_preced_def by auto + finally show ?thesis . + qed + next + case (P thread cs) + with Cons + show ?thesis by (auto simp:preced_def the_preced_def) + next + case (V thread cs) + with Cons + show ?thesis by (auto simp:preced_def the_preced_def) + next + case (Set thread prio') + show ?thesis (is "Max (?f ` ?A) = ?t") + proof - + have "Max (?f ` ?A) = ?f th" + proof(rule image_Max_eqI) + show "finite ?A" using h_e.finite_threads by auto + next + show "th \ ?A" using h_e.th_kept by auto + next + show "\x\?A. ?f x \ ?f th" + proof + fix x + assume h: "x \ ?A" + show "?f x \ ?f th" + proof(cases "x = thread") + case True + moreover have "the_preced (Set thread prio' # t @ s) thread \ the_preced (t @ s) th" + proof - + have "the_preced (t @ s) th = Prc prio tm" + using h_t.th_kept preced_th by (simp add:the_preced_def) + moreover have "prio' \ prio" using Set h_e.set_diff_low by auto + ultimately show ?thesis by (insert lt_tm, auto simp:the_preced_def preced_def) + qed + ultimately show ?thesis + by (unfold Set, simp add:the_preced_def preced_def) + next + case False + then have "?f x = the_preced (t@s) x" + by (simp add:the_preced_def preced_def Set) + also have "... \ Max (the_preced (t@s) ` threads (t@s))" + using Set h h_t.finite_threads by auto + also have "... = ?f th" by (metis Cons.hyps(5) h_e.th_kept the_preced_def) + finally show ?thesis . + qed + qed + qed + also have "... = ?t" using h_e.th_kept the_preced_def by auto + finally show ?thesis . + qed + qed +qed + +lemma max_preced: "preced th (t@s) = Max (the_preced (t@s) ` (threads (t@s)))" + by (insert th_kept max_kept, auto) + +text {* + The reason behind the following lemma is that: + Since @{term "cp"} is defined as the maximum precedence + of those threads contained in the sub-tree of node @{term "Th th"} + in @{term "RAG (t@s)"}, and all these threads are living threads, and + @{term "th"} is also among them, the maximum precedence of + them all must be the one for @{text "th"}. +*} +lemma th_cp_max_preced: + "cp (t@s) th = Max (the_preced (t@s) ` (threads (t@s)))" (is "?L = ?R") +proof - + let ?f = "the_preced (t@s)" + have "?L = ?f th" + proof(unfold cp_alt_def, rule image_Max_eqI) + show "finite {th'. Th th' \ subtree (RAG (t @ s)) (Th th)}" + proof - + have "{th'. Th th' \ subtree (RAG (t @ s)) (Th th)} = + the_thread ` {n . n \ subtree (RAG (t @ s)) (Th th) \ + (\ th'. n = Th th')}" + by (smt Collect_cong Setcompr_eq_image mem_Collect_eq the_thread.simps) + moreover have "finite ..." by (simp add: vat_t.fsbtRAGs.finite_subtree) + ultimately show ?thesis by simp + qed + next + show "th \ {th'. Th th' \ subtree (RAG (t @ s)) (Th th)}" + by (auto simp:subtree_def) + next + show "\x\{th'. Th th' \ subtree (RAG (t @ s)) (Th th)}. + the_preced (t @ s) x \ the_preced (t @ s) th" + proof + fix th' + assume "th' \ {th'. Th th' \ subtree (RAG (t @ s)) (Th th)}" + hence "Th th' \ subtree (RAG (t @ s)) (Th th)" by auto + moreover have "... \ Field (RAG (t @ s)) \ {Th th}" + by (meson subtree_Field) + ultimately have "Th th' \ ..." by auto + hence "th' \ threads (t@s)" + proof + assume "Th th' \ {Th th}" + thus ?thesis using th_kept by auto + next + assume "Th th' \ Field (RAG (t @ s))" + thus ?thesis using vat_t.not_in_thread_isolated by blast + qed + thus "the_preced (t @ s) th' \ the_preced (t @ s) th" + by (metis Max_ge finite_imageI finite_threads image_eqI + max_kept th_kept the_preced_def) + qed + qed + also have "... = ?R" by (simp add: max_preced the_preced_def) + finally show ?thesis . +qed + +lemma th_cp_max: "cp (t@s) th = Max (cp (t@s) ` threads (t@s))" + using max_cp_eq th_cp_max_preced the_preced_def vt_t by presburger + +lemma th_cp_preced: "cp (t@s) th = preced th s" + by (fold max_kept, unfold th_cp_max_preced, simp) + +lemma preced_less: + assumes th'_in: "th' \ threads s" + and neq_th': "th' \ th" + shows "preced th' s < preced th s" + using assms +by (metis Max.coboundedI finite_imageI highest not_le order.trans + preced_linorder rev_image_eqI threads_s vat_s.finite_threads + vat_s.le_cp) + +text {* + Counting of the number of @{term "P"} and @{term "V"} operations + is the cornerstone of a large number of the following proofs. + The reason is that this counting is quite easy to calculate and + convenient to use in the reasoning. + + The following lemma shows that the counting controls whether + a thread is running or not. +*} + +lemma pv_blocked_pre: + assumes th'_in: "th' \ threads (t@s)" + and neq_th': "th' \ th" + and eq_pv: "cntP (t@s) th' = cntV (t@s) th'" + shows "th' \ runing (t@s)" +proof + assume otherwise: "th' \ runing (t@s)" + show False + proof - + have "th' = th" + proof(rule preced_unique) + show "preced th' (t @ s) = preced th (t @ s)" (is "?L = ?R") + proof - + have "?L = cp (t@s) th'" + by (unfold cp_eq_cpreced cpreced_def count_eq_dependants[OF eq_pv], simp) + also have "... = cp (t @ s) th" using otherwise + by (metis (mono_tags, lifting) mem_Collect_eq + runing_def th_cp_max vat_t.max_cp_readys_threads) + also have "... = ?R" by (metis th_cp_preced th_kept) + finally show ?thesis . + qed + qed (auto simp: th'_in th_kept) + moreover have "th' \ th" using neq_th' . + ultimately show ?thesis by simp + qed +qed + +lemmas pv_blocked = pv_blocked_pre[folded detached_eq] + +lemma runing_precond_pre: + fixes th' + assumes th'_in: "th' \ threads s" + and eq_pv: "cntP s th' = cntV s th'" + and neq_th': "th' \ th" + shows "th' \ threads (t@s) \ + cntP (t@s) th' = cntV (t@s) th'" +proof(induct rule:ind) + case (Cons e t) + interpret vat_t: extend_highest_gen s th prio tm t using Cons by simp + interpret vat_e: extend_highest_gen s th prio tm "(e # t)" using Cons by simp + show ?case + proof(cases e) + case (P thread cs) + show ?thesis + proof - + have "cntP ((e # t) @ s) th' = cntV ((e # t) @ s) th'" + proof - + have "thread \ th'" + proof - + have "step (t@s) (P thread cs)" using Cons P by auto + thus ?thesis + proof(cases) + assume "thread \ runing (t@s)" + moreover have "th' \ runing (t@s)" using Cons(5) + by (metis neq_th' vat_t.pv_blocked_pre) + ultimately show ?thesis by auto + qed + qed with Cons show ?thesis + by (unfold P, simp add:cntP_def cntV_def count_def) + qed + moreover have "th' \ threads ((e # t) @ s)" using Cons by (unfold P, simp) + ultimately show ?thesis by auto + qed + next + case (V thread cs) + show ?thesis + proof - + have "cntP ((e # t) @ s) th' = cntV ((e # t) @ s) th'" + proof - + have "thread \ th'" + proof - + have "step (t@s) (V thread cs)" using Cons V by auto + thus ?thesis + proof(cases) + assume "thread \ runing (t@s)" + moreover have "th' \ runing (t@s)" using Cons(5) + by (metis neq_th' vat_t.pv_blocked_pre) + ultimately show ?thesis by auto + qed + qed with Cons show ?thesis + by (unfold V, simp add:cntP_def cntV_def count_def) + qed + moreover have "th' \ threads ((e # t) @ s)" using Cons by (unfold V, simp) + ultimately show ?thesis by auto + qed + next + case (Create thread prio') + show ?thesis + proof - + have "cntP ((e # t) @ s) th' = cntV ((e # t) @ s) th'" + proof - + have "thread \ th'" + proof - + have "step (t@s) (Create thread prio')" using Cons Create by auto + thus ?thesis using Cons(5) by (cases, auto) + qed with Cons show ?thesis + by (unfold Create, simp add:cntP_def cntV_def count_def) + qed + moreover have "th' \ threads ((e # t) @ s)" using Cons by (unfold Create, simp) + ultimately show ?thesis by auto + qed + next + case (Exit thread) + show ?thesis + proof - + have neq_thread: "thread \ th'" + proof - + have "step (t@s) (Exit thread)" using Cons Exit by auto + thus ?thesis apply (cases) using Cons(5) + by (metis neq_th' vat_t.pv_blocked_pre) + qed + hence "cntP ((e # t) @ s) th' = cntV ((e # t) @ s) th'" using Cons + by (unfold Exit, simp add:cntP_def cntV_def count_def) + moreover have "th' \ threads ((e # t) @ s)" using Cons neq_thread + by (unfold Exit, simp) + ultimately show ?thesis by auto + qed + next + case (Set thread prio') + with Cons + show ?thesis + by (auto simp:cntP_def cntV_def count_def) + qed +next + case Nil + with assms + show ?case by auto +qed + +text {* Changing counting balance to detachedness *} +lemmas runing_precond_pre_dtc = runing_precond_pre + [folded vat_t.detached_eq vat_s.detached_eq] + +lemma runing_precond: + fixes th' + assumes th'_in: "th' \ threads s" + and neq_th': "th' \ th" + and is_runing: "th' \ runing (t@s)" + shows "cntP s th' > cntV s th'" + using assms +proof - + have "cntP s th' \ cntV s th'" + by (metis is_runing neq_th' pv_blocked_pre runing_precond_pre th'_in) + moreover have "cntV s th' \ cntP s th'" using vat_s.cnp_cnv_cncs by auto + ultimately show ?thesis by auto +qed + +lemma moment_blocked_pre: + assumes neq_th': "th' \ th" + and th'_in: "th' \ threads ((moment i t)@s)" + and eq_pv: "cntP ((moment i t)@s) th' = cntV ((moment i t)@s) th'" + shows "cntP ((moment (i+j) t)@s) th' = cntV ((moment (i+j) t)@s) th' \ + th' \ threads ((moment (i+j) t)@s)" +proof - + interpret h_i: red_extend_highest_gen _ _ _ _ _ i + by (unfold_locales) + interpret h_j: red_extend_highest_gen _ _ _ _ _ "i+j" + by (unfold_locales) + interpret h: extend_highest_gen "((moment i t)@s)" th prio tm "moment j (restm i t)" + proof(unfold_locales) + show "vt (moment i t @ s)" by (metis h_i.vt_t) + next + show "th \ threads (moment i t @ s)" by (metis h_i.th_kept) + next + show "preced th (moment i t @ s) = + Max (cp (moment i t @ s) ` threads (moment i t @ s))" + by (metis h_i.th_cp_max h_i.th_cp_preced h_i.th_kept) + next + show "preced th (moment i t @ s) = Prc prio tm" by (metis h_i.th_kept preced_th) + next + show "vt (moment j (restm i t) @ moment i t @ s)" + using moment_plus_split by (metis add.commute append_assoc h_j.vt_t) + next + fix th' prio' + assume "Create th' prio' \ set (moment j (restm i t))" + thus "prio' \ prio" using assms + by (metis Un_iff add.commute h_j.create_low moment_plus_split set_append) + next + fix th' prio' + assume "Set th' prio' \ set (moment j (restm i t))" + thus "th' \ th \ prio' \ prio" + by (metis Un_iff add.commute h_j.set_diff_low moment_plus_split set_append) + next + fix th' + assume "Exit th' \ set (moment j (restm i t))" + thus "th' \ th" + by (metis Un_iff add.commute h_j.exit_diff moment_plus_split set_append) + qed + show ?thesis + by (metis add.commute append_assoc eq_pv h.runing_precond_pre + moment_plus_split neq_th' th'_in) +qed + +lemma moment_blocked_eqpv: + assumes neq_th': "th' \ th" + and th'_in: "th' \ threads ((moment i t)@s)" + and eq_pv: "cntP ((moment i t)@s) th' = cntV ((moment i t)@s) th'" + and le_ij: "i \ j" + shows "cntP ((moment j t)@s) th' = cntV ((moment j t)@s) th' \ + th' \ threads ((moment j t)@s) \ + th' \ runing ((moment j t)@s)" +proof - + from moment_blocked_pre [OF neq_th' th'_in eq_pv, of "j-i"] and le_ij + have h1: "cntP ((moment j t)@s) th' = cntV ((moment j t)@s) th'" + and h2: "th' \ threads ((moment j t)@s)" by auto + moreover have "th' \ runing ((moment j t)@s)" + proof - + interpret h: red_extend_highest_gen _ _ _ _ _ j by (unfold_locales) + show ?thesis + using h.pv_blocked_pre h1 h2 neq_th' by auto + qed + ultimately show ?thesis by auto +qed + +(* The foregoing two lemmas are preparation for this one, but + in long run can be combined. Maybe I am wrong. +*) +lemma moment_blocked: + assumes neq_th': "th' \ th" + and th'_in: "th' \ threads ((moment i t)@s)" + and dtc: "detached (moment i t @ s) th'" + and le_ij: "i \ j" + shows "detached (moment j t @ s) th' \ + th' \ threads ((moment j t)@s) \ + th' \ runing ((moment j t)@s)" +proof - + interpret h_i: red_extend_highest_gen _ _ _ _ _ i by (unfold_locales) + interpret h_j: red_extend_highest_gen _ _ _ _ _ j by (unfold_locales) + have cnt_i: "cntP (moment i t @ s) th' = cntV (moment i t @ s) th'" + by (metis dtc h_i.detached_elim) + from moment_blocked_eqpv[OF neq_th' th'_in cnt_i le_ij] + show ?thesis by (metis h_j.detached_intro) +qed + +lemma runing_preced_inversion: + assumes runing': "th' \ runing (t@s)" + shows "cp (t@s) th' = preced th s" (is "?L = ?R") +proof - + have "?L = Max (cp (t @ s) ` readys (t @ s))" using assms + by (unfold runing_def, auto) + also have "\ = ?R" + by (metis th_cp_max th_cp_preced vat_t.max_cp_readys_threads) + finally show ?thesis . +qed + +text {* + The situation when @{term "th"} is blocked is analyzed by the following lemmas. +*} + +text {* + The following lemmas shows the running thread @{text "th'"}, if it is different from + @{term th}, must be live at the very beginning. By the term {\em the very beginning}, + we mean the moment where the formal investigation starts, i.e. the moment (or state) + @{term s}. +*} + +lemma runing_inversion_0: + assumes neq_th': "th' \ th" + and runing': "th' \ runing (t@s)" + shows "th' \ threads s" +proof - + -- {* The proof is by contradiction: *} + { assume otherwise: "\ ?thesis" + have "th' \ runing (t @ s)" + proof - + -- {* Since @{term "th'"} is running at time @{term "t@s"}, so it exists that time. *} + have th'_in: "th' \ threads (t@s)" using runing' by (simp add:runing_def readys_def) + -- {* However, @{text "th'"} does not exist at very beginning. *} + have th'_notin: "th' \ threads (moment 0 t @ s)" using otherwise + by (metis append.simps(1) moment_zero) + -- {* Therefore, there must be a moment during @{text "t"}, when + @{text "th'"} came into being. *} + -- {* Let us suppose the moment being @{text "i"}: *} + from p_split_gen[OF th'_in th'_notin] + obtain i where lt_its: "i < length t" + and le_i: "0 \ i" + and pre: " th' \ threads (moment i t @ s)" (is "th' \ threads ?pre") + and post: "(\i'>i. th' \ threads (moment i' t @ s))" by (auto) + interpret h_i: red_extend_highest_gen _ _ _ _ _ i by (unfold_locales) + interpret h_i': red_extend_highest_gen _ _ _ _ _ "(Suc i)" by (unfold_locales) + from lt_its have "Suc i \ length t" by auto + -- {* Let us also suppose the event which makes this change is @{text e}: *} + from moment_head[OF this] obtain e where + eq_me: "moment (Suc i) t = e # moment i t" by blast + hence "vt (e # (moment i t @ s))" by (metis append_Cons h_i'.vt_t) + hence "PIP (moment i t @ s) e" by (cases, simp) + -- {* It can be derived that this event @{text "e"}, which + gives birth to @{term "th'"} must be a @{term "Create"}: *} + from create_pre[OF this, of th'] + obtain prio where eq_e: "e = Create th' prio" + by (metis append_Cons eq_me lessI post pre) + have h1: "th' \ threads (moment (Suc i) t @ s)" using post by auto + have h2: "cntP (moment (Suc i) t @ s) th' = cntV (moment (Suc i) t@ s) th'" + proof - + have "cntP (moment i t@s) th' = cntV (moment i t@s) th'" + by (metis h_i.cnp_cnv_eq pre) + thus ?thesis by (simp add:eq_me eq_e cntP_def cntV_def count_def) + qed + show ?thesis + using moment_blocked_eqpv [OF neq_th' h1 h2, of "length t"] lt_its moment_ge + by auto + qed + with `th' \ runing (t@s)` + have False by simp + } thus ?thesis by auto +qed + +text {* + The second lemma says, if the running thread @{text th'} is different from + @{term th}, then this @{text th'} must in the possession of some resources + at the very beginning. + + To ease the reasoning of resource possession of one particular thread, + we used two auxiliary functions @{term cntV} and @{term cntP}, + which are the counters of @{term P}-operations and + @{term V}-operations respectively. + If the number of @{term V}-operation is less than the number of + @{term "P"}-operations, the thread must have some unreleased resource. +*} + +lemma runing_inversion_1: (* ddd *) + assumes neq_th': "th' \ th" + and runing': "th' \ runing (t@s)" + -- {* thread @{term "th'"} is a live on in state @{term "s"} and + it has some unreleased resource. *} + shows "th' \ threads s \ cntV s th' < cntP s th'" +proof - + -- {* The proof is a simple composition of @{thm runing_inversion_0} and + @{thm runing_precond}: *} + -- {* By applying @{thm runing_inversion_0} to assumptions, + it can be shown that @{term th'} is live in state @{term s}: *} + have "th' \ threads s" using runing_inversion_0[OF assms(1,2)] . + -- {* Then the thesis is derived easily by applying @{thm runing_precond}: *} + with runing_precond [OF this neq_th' runing'] show ?thesis by simp +qed + +text {* + The following lemma is just a rephrasing of @{thm runing_inversion_1}: +*} +lemma runing_inversion_2: + assumes runing': "th' \ runing (t@s)" + shows "th' = th \ (th' \ th \ th' \ threads s \ cntV s th' < cntP s th')" +proof - + from runing_inversion_1[OF _ runing'] + show ?thesis by auto +qed + +lemma runing_inversion_3: + assumes runing': "th' \ runing (t@s)" + and neq_th: "th' \ th" + shows "th' \ threads s \ (cntV s th' < cntP s th' \ cp (t@s) th' = preced th s)" + by (metis neq_th runing' runing_inversion_2 runing_preced_inversion) + +lemma runing_inversion_4: + assumes runing': "th' \ runing (t@s)" + and neq_th: "th' \ th" + shows "th' \ threads s" + and "\detached s th'" + and "cp (t@s) th' = preced th s" + apply (metis neq_th runing' runing_inversion_2) + apply (metis neq_th pv_blocked runing' runing_inversion_2 runing_precond_pre_dtc) + by (metis neq_th runing' runing_inversion_3) + + +text {* + Suppose @{term th} is not running, it is first shown that + there is a path in RAG leading from node @{term th} to another thread @{text "th'"} + in the @{term readys}-set (So @{text "th'"} is an ancestor of @{term th}}). + + Now, since @{term readys}-set is non-empty, there must be + one in it which holds the highest @{term cp}-value, which, by definition, + is the @{term runing}-thread. However, we are going to show more: this running thread + is exactly @{term "th'"}. + *} +lemma th_blockedE: (* ddd *) + assumes "th \ runing (t@s)" + obtains th' where "Th th' \ ancestors (RAG (t @ s)) (Th th)" + "th' \ runing (t@s)" +proof - + -- {* According to @{thm vat_t.th_chain_to_ready}, either + @{term "th"} is in @{term "readys"} or there is path leading from it to + one thread in @{term "readys"}. *} + have "th \ readys (t @ s) \ (\th'. th' \ readys (t @ s) \ (Th th, Th th') \ (RAG (t @ s))\<^sup>+)" + using th_kept vat_t.th_chain_to_ready by auto + -- {* However, @{term th} can not be in @{term readys}, because otherwise, since + @{term th} holds the highest @{term cp}-value, it must be @{term "runing"}. *} + moreover have "th \ readys (t@s)" + using assms runing_def th_cp_max vat_t.max_cp_readys_threads by auto + -- {* So, there must be a path from @{term th} to another thread @{text "th'"} in + term @{term readys}: *} + ultimately obtain th' where th'_in: "th' \ readys (t@s)" + and dp: "(Th th, Th th') \ (RAG (t @ s))\<^sup>+" by auto + -- {* We are going to show that this @{term th'} is running. *} + have "th' \ runing (t@s)" + proof - + -- {* We only need to show that this @{term th'} holds the highest @{term cp}-value: *} + have "cp (t@s) th' = Max (cp (t@s) ` readys (t@s))" (is "?L = ?R") + proof - + have "?L = Max ((the_preced (t @ s) \ the_thread) ` subtree (tRAG (t @ s)) (Th th'))" + by (unfold cp_alt_def1, simp) + also have "... = (the_preced (t @ s) \ the_thread) (Th th)" + proof(rule image_Max_subset) + show "finite (Th ` (threads (t@s)))" by (simp add: vat_t.finite_threads) + next + show "subtree (tRAG (t @ s)) (Th th') \ Th ` threads (t @ s)" + by (metis Range.intros dp trancl_range vat_t.range_in vat_t.subtree_tRAG_thread) + next + show "Th th \ subtree (tRAG (t @ s)) (Th th')" using dp + by (unfold tRAG_subtree_eq, auto simp:subtree_def) + next + show "Max ((the_preced (t @ s) \ the_thread) ` Th ` threads (t @ s)) = + (the_preced (t @ s) \ the_thread) (Th th)" (is "Max ?L = _") + proof - + have "?L = the_preced (t @ s) ` threads (t @ s)" + by (unfold image_comp, rule image_cong, auto) + thus ?thesis using max_preced the_preced_def by auto + qed + qed + also have "... = ?R" + using th_cp_max th_cp_preced th_kept + the_preced_def vat_t.max_cp_readys_threads by auto + finally show ?thesis . + qed + -- {* Now, since @{term th'} holds the highest @{term cp} + and we have already show it is in @{term readys}, + it is @{term runing} by definition. *} + with `th' \ readys (t@s)` show ?thesis by (simp add: runing_def) + qed + -- {* It is easy to show @{term th'} is an ancestor of @{term th}: *} + moreover have "Th th' \ ancestors (RAG (t @ s)) (Th th)" + using `(Th th, Th th') \ (RAG (t @ s))\<^sup>+` by (auto simp:ancestors_def) + ultimately show ?thesis using that by metis +qed + +text {* + Now it is easy to see there is always a thread to run by case analysis + on whether thread @{term th} is running: if the answer is Yes, the + the running thread is obviously @{term th} itself; otherwise, the running + thread is the @{text th'} given by lemma @{thm th_blockedE}. +*} +lemma live: "runing (t@s) \ {}" +proof(cases "th \ runing (t@s)") + case True thus ?thesis by auto +next + case False + thus ?thesis using th_blockedE by auto +qed + +end +end + + + diff -r b620a2a0806a -r b4bcd1edbb6d CpsG.thy --- a/CpsG.thy Wed Jan 06 20:46:14 2016 +0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1640 +0,0 @@ -section {* - This file contains lemmas used to guide the recalculation of current precedence - after every system call (or system operation) -*} -theory CpsG -imports PrioG Max RTree -begin - -text {* @{text "the_preced"} is also the same as @{text "preced"}, the only - difference is the order of arguemts. *} -definition "the_preced s th = preced th s" - -lemma inj_the_preced: - "inj_on (the_preced s) (threads s)" - by (metis inj_onI preced_unique the_preced_def) - -text {* @{term "the_thread"} extracts thread out of RAG node. *} -fun the_thread :: "node \ thread" where - "the_thread (Th th) = th" - -text {* The following @{text "wRAG"} is the waiting sub-graph of @{text "RAG"}. *} -definition "wRAG (s::state) = {(Th th, Cs cs) | th cs. waiting s th cs}" - -text {* The following @{text "hRAG"} is the holding sub-graph of @{text "RAG"}. *} -definition "hRAG (s::state) = {(Cs cs, Th th) | th cs. holding s th cs}" - -text {* The following lemma splits @{term "RAG"} graph into the above two sub-graphs. *} -lemma RAG_split: "RAG s = (wRAG s \ hRAG s)" - by (unfold s_RAG_abv wRAG_def hRAG_def s_waiting_abv - s_holding_abv cs_RAG_def, auto) - -text {* - The following @{text "tRAG"} is the thread-graph derived from @{term "RAG"}. - It characterizes the dependency between threads when calculating current - precedences. It is defined as the composition of the above two sub-graphs, - names @{term "wRAG"} and @{term "hRAG"}. - *} -definition "tRAG s = wRAG s O hRAG s" - -(* ccc *) - -definition "cp_gen s x = - Max ((the_preced s \ the_thread) ` subtree (tRAG s) x)" - -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) - find_theorems wf RAG - 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}" - - -text {* (* ddd *) - One beauty of our modelling is that we follow the definitional extension tradition of HOL. - The benefit of such a concise and miniature model is that large number of intuitively - obvious facts are derived as lemmas, rather than asserted as axioms. -*} - -text {* - However, the lemmas in the forthcoming several locales are no longer - obvious. These lemmas show how the current precedences should be recalculated - after every execution step (in our model, every step is represented by an event, - which in turn, represents a system call, or operation). Each operation is - treated in a separate locale. - - The complication of current precedence recalculation comes - because the changing of RAG needs to be taken into account, - in addition to the changing of precedence. - The reason RAG changing affects current precedence is that, - according to the definition, current precedence - of a thread is the maximum of the precedences of its dependants, - where the dependants are defined in terms of RAG. - - Therefore, each operation, lemmas concerning the change of the precedences - and RAG are derived first, so that the lemmas about - current precedence recalculation can be based on. -*} - -text {* (* ddd *) - The following locale @{text "step_set_cps"} investigates the recalculation - after the @{text "Set"} operation. -*} -locale step_set_cps = - fixes s' th prio s - -- {* @{text "s'"} is the system state before the operation *} - -- {* @{text "s"} is the system state after the operation *} - defines s_def : "s \ (Set th prio#s')" - -- {* @{text "s"} is assumed to be a legitimate state, from which - the legitimacy of @{text "s"} can be derived. *} - assumes vt_s: "vt s" - -sublocale step_set_cps < vat_s : valid_trace "s" -proof - from vt_s show "vt s" . -qed - -sublocale step_set_cps < vat_s' : valid_trace "s'" -proof - from step_back_vt[OF vt_s[unfolded s_def]] show "vt s'" . -qed - -context step_set_cps -begin - -text {* (* ddd *) - The following two lemmas confirm that @{text "Set"}-operating only changes the precedence - of the initiating thread. -*} - -lemma eq_preced: - assumes "th' \ th" - shows "preced th' s = preced th' s'" -proof - - from assms show ?thesis - by (unfold s_def, auto simp:preced_def) -qed - -lemma eq_the_preced: - fixes th' - assumes "th' \ th" - shows "the_preced s th' = the_preced s' th'" - using assms - by (unfold the_preced_def, intro eq_preced, simp) - -text {* - The following lemma assures that the resetting of priority does not change the RAG. -*} - -lemma eq_dep: "RAG s = RAG s'" - by (unfold s_def RAG_set_unchanged, auto) - -text {* (* ddd *) - Th following lemma @{text "eq_cp_pre"} says the priority change of @{text "th"} - only affects those threads, which as @{text "Th th"} in their sub-trees. - - The proof of this lemma is simplified by using the alternative definition of @{text "cp"}. -*} - -lemma eq_cp_pre: - fixes th' - assumes nd: "Th th \ subtree (RAG s') (Th th')" - shows "cp s th' = cp s' th'" -proof - - -- {* After unfolding using the alternative definition, elements - affecting the @{term "cp"}-value of threads become explicit. - We only need to prove the following: *} - have "Max (the_preced s ` {th'a. Th th'a \ subtree (RAG s) (Th th')}) = - Max (the_preced s' ` {th'a. Th th'a \ subtree (RAG s') (Th th')})" - (is "Max (?f ` ?S1) = Max (?g ` ?S2)") - proof - - -- {* The base sets are equal. *} - have "?S1 = ?S2" using eq_dep by simp - -- {* The function values on the base set are equal as well. *} - moreover have "\ e \ ?S2. ?f e = ?g e" - proof - fix th1 - assume "th1 \ ?S2" - with nd have "th1 \ th" by (auto) - from eq_the_preced[OF this] - show "the_preced s th1 = the_preced s' th1" . - qed - -- {* Therefore, the image of the functions are equal. *} - ultimately have "(?f ` ?S1) = (?g ` ?S2)" by (auto intro!:f_image_eq) - thus ?thesis by simp - qed - thus ?thesis by (simp add:cp_alt_def) -qed - -text {* - The following lemma shows that @{term "th"} is not in the - sub-tree of any other thread. -*} -lemma th_in_no_subtree: - assumes "th' \ th" - shows "Th th \ subtree (RAG s') (Th th')" -proof - - have "th \ readys s'" - proof - - from step_back_step [OF vt_s[unfolded s_def]] - have "step s' (Set th prio)" . - hence "th \ runing s'" by (cases, simp) - thus ?thesis by (simp add:readys_def runing_def) - qed - find_theorems readys subtree - from vat_s'.readys_in_no_subtree[OF this assms(1)] - show ?thesis by blast -qed - -text {* - By combining @{thm "eq_cp_pre"} and @{thm "th_in_no_subtree"}, - it is obvious that the change of priority only affects the @{text "cp"}-value - of the initiating thread @{text "th"}. -*} -lemma eq_cp: - fixes th' - assumes "th' \ th" - shows "cp s th' = cp s' th'" - by (rule eq_cp_pre[OF th_in_no_subtree[OF assms]]) - -end - -text {* - The following @{text "step_v_cps"} is the locale for @{text "V"}-operation. -*} - -locale step_v_cps = - -- {* @{text "th"} is the initiating thread *} - -- {* @{text "cs"} is the critical resource release by the @{text "V"}-operation *} - fixes s' th cs s -- {* @{text "s'"} is the state before operation*} - defines s_def : "s \ (V th cs#s')" -- {* @{text "s"} is the state after operation*} - -- {* @{text "s"} is assumed to be valid, which implies the validity of @{text "s'"} *} - assumes vt_s: "vt s" - -sublocale step_v_cps < vat_s : valid_trace "s" -proof - from vt_s show "vt s" . -qed - -sublocale step_v_cps < vat_s' : valid_trace "s'" -proof - from step_back_vt[OF vt_s[unfolded s_def]] show "vt s'" . -qed - -context step_v_cps -begin - -lemma ready_th_s': "th \ readys s'" - using step_back_step[OF vt_s[unfolded s_def]] - by (cases, simp add:runing_def) - -lemma ancestors_th: "ancestors (RAG s') (Th th) = {}" -proof - - from vat_s'.readys_root[OF ready_th_s'] - show ?thesis - by (unfold root_def, simp) -qed - -lemma holding_th: "holding s' th cs" -proof - - from vt_s[unfolded s_def] - have " PIP s' (V th cs)" by (cases, simp) - thus ?thesis by (cases, auto) -qed - -lemma edge_of_th: - "(Cs cs, Th th) \ RAG s'" -proof - - from holding_th - show ?thesis - by (unfold s_RAG_def holding_eq, auto) -qed - -lemma ancestors_cs: - "ancestors (RAG s') (Cs cs) = {Th th}" -proof - - have "ancestors (RAG s') (Cs cs) = ancestors (RAG s') (Th th) \ {Th th}" - proof(rule vat_s'.rtree_RAG.ancestors_accum) - from vt_s[unfolded s_def] - have " PIP s' (V th cs)" by (cases, simp) - thus "(Cs cs, Th th) \ RAG s'" - proof(cases) - assume "holding s' th cs" - from this[unfolded holding_eq] - show ?thesis by (unfold s_RAG_def, auto) - qed - qed - from this[unfolded ancestors_th] show ?thesis by simp -qed - -lemma preced_kept: "the_preced s = the_preced s'" - by (auto simp: s_def the_preced_def preced_def) - -end - -text {* - The following @{text "step_v_cps_nt"} is the sub-locale for @{text "V"}-operation, - which represents the case when there is another thread @{text "th'"} - to take over the critical resource released by the initiating thread @{text "th"}. -*} -locale step_v_cps_nt = step_v_cps + - fixes th' - -- {* @{text "th'"} is assumed to take over @{text "cs"} *} - assumes nt: "next_th s' th cs th'" - -context step_v_cps_nt -begin - -text {* - Lemma @{text "RAG_s"} confirms the change of RAG: - two edges removed and one added, as shown by the following diagram. -*} - -(* - RAG before the V-operation - th1 ----| - | - th' ----| - |----> cs -----| - th2 ----| | - | | - th3 ----| | - |------> th - th4 ----| | - | | - th5 ----| | - |----> cs'-----| - th6 ----| - | - th7 ----| - - RAG after the V-operation - th1 ----| - | - |----> cs ----> th' - th2 ----| - | - th3 ----| - - th4 ----| - | - th5 ----| - |----> cs'----> th - th6 ----| - | - th7 ----| -*) - -lemma sub_RAGs': "{(Cs cs, Th th), (Th th', Cs cs)} \ RAG s'" - using next_th_RAG[OF nt] . - -lemma ancestors_th': - "ancestors (RAG s') (Th th') = {Th th, Cs cs}" -proof - - have "ancestors (RAG s') (Th th') = ancestors (RAG s') (Cs cs) \ {Cs cs}" - proof(rule vat_s'.rtree_RAG.ancestors_accum) - from sub_RAGs' show "(Th th', Cs cs) \ RAG s'" by auto - qed - thus ?thesis using ancestors_th ancestors_cs by auto -qed - -lemma RAG_s: - "RAG s = (RAG s' - {(Cs cs, Th th), (Th th', Cs cs)}) \ - {(Cs cs, Th th')}" -proof - - from step_RAG_v[OF vt_s[unfolded s_def], folded s_def] - and nt show ?thesis by (auto intro:next_th_unique) -qed - -lemma subtree_kept: - assumes "th1 \ {th, th'}" - shows "subtree (RAG s) (Th th1) = subtree (RAG s') (Th th1)" (is "_ = ?R") -proof - - let ?RAG' = "(RAG s' - {(Cs cs, Th th), (Th th', Cs cs)})" - let ?RAG'' = "?RAG' \ {(Cs cs, Th th')}" - have "subtree ?RAG' (Th th1) = ?R" - proof(rule subset_del_subtree_outside) - show "Range {(Cs cs, Th th), (Th th', Cs cs)} \ subtree (RAG s') (Th th1) = {}" - proof - - have "(Th th) \ subtree (RAG s') (Th th1)" - proof(rule subtree_refute) - show "Th th1 \ ancestors (RAG s') (Th th)" - by (unfold ancestors_th, simp) - next - from assms show "Th th1 \ Th th" by simp - qed - moreover have "(Cs cs) \ subtree (RAG s') (Th th1)" - proof(rule subtree_refute) - show "Th th1 \ ancestors (RAG s') (Cs cs)" - by (unfold ancestors_cs, insert assms, auto) - qed simp - ultimately have "{Th th, Cs cs} \ subtree (RAG s') (Th th1) = {}" by auto - thus ?thesis by simp - qed - qed - moreover have "subtree ?RAG'' (Th th1) = subtree ?RAG' (Th th1)" - proof(rule subtree_insert_next) - show "Th th' \ subtree (RAG s' - {(Cs cs, Th th), (Th th', Cs cs)}) (Th th1)" - proof(rule subtree_refute) - show "Th th1 \ ancestors (RAG s' - {(Cs cs, Th th), (Th th', Cs cs)}) (Th th')" - (is "_ \ ?R") - proof - - have "?R \ ancestors (RAG s') (Th th')" by (rule ancestors_mono, auto) - moreover have "Th th1 \ ..." using ancestors_th' assms by simp - ultimately show ?thesis by auto - qed - next - from assms show "Th th1 \ Th th'" by simp - qed - qed - ultimately show ?thesis by (unfold RAG_s, simp) -qed - -lemma cp_kept: - assumes "th1 \ {th, th'}" - shows "cp s th1 = cp s' th1" - by (unfold cp_alt_def preced_kept subtree_kept[OF assms], simp) - -end - -locale step_v_cps_nnt = step_v_cps + - assumes nnt: "\ th'. (\ next_th s' th cs th')" - -context step_v_cps_nnt -begin - -lemma RAG_s: "RAG s = RAG s' - {(Cs cs, Th th)}" -proof - - from nnt and step_RAG_v[OF vt_s[unfolded s_def], folded s_def] - show ?thesis by auto -qed - -lemma subtree_kept: - assumes "th1 \ th" - shows "subtree (RAG s) (Th th1) = subtree (RAG s') (Th th1)" -proof(unfold RAG_s, rule subset_del_subtree_outside) - show "Range {(Cs cs, Th th)} \ subtree (RAG s') (Th th1) = {}" - proof - - have "(Th th) \ subtree (RAG s') (Th th1)" - proof(rule subtree_refute) - show "Th th1 \ ancestors (RAG s') (Th th)" - by (unfold ancestors_th, simp) - next - from assms show "Th th1 \ Th th" by simp - qed - thus ?thesis by auto - qed -qed - -lemma cp_kept_1: - assumes "th1 \ th" - shows "cp s th1 = cp s' th1" - by (unfold cp_alt_def preced_kept subtree_kept[OF assms], simp) - -lemma subtree_cs: "subtree (RAG s') (Cs cs) = {Cs cs}" -proof - - { fix n - have "(Cs cs) \ ancestors (RAG s') n" - proof - assume "Cs cs \ ancestors (RAG s') n" - hence "(n, Cs cs) \ (RAG s')^+" by (auto simp:ancestors_def) - from tranclE[OF this] obtain nn where h: "(nn, Cs cs) \ RAG s'" by auto - then obtain th' where "nn = Th th'" - by (unfold s_RAG_def, auto) - from h[unfolded this] have "(Th th', Cs cs) \ RAG s'" . - from this[unfolded s_RAG_def] - have "waiting (wq s') th' cs" by auto - from this[unfolded cs_waiting_def] - have "1 < length (wq s' cs)" - by (cases "wq s' cs", auto) - from holding_next_thI[OF holding_th this] - obtain th' where "next_th s' th cs th'" by auto - with nnt show False by auto - qed - } note h = this - { fix n - assume "n \ subtree (RAG s') (Cs cs)" - hence "n = (Cs cs)" - by (elim subtreeE, insert h, auto) - } moreover have "(Cs cs) \ subtree (RAG s') (Cs cs)" - by (auto simp:subtree_def) - ultimately show ?thesis by auto -qed - -lemma subtree_th: - "subtree (RAG s) (Th th) = subtree (RAG s') (Th th) - {Cs cs}" -find_theorems "subtree" "_ - _" RAG -proof(unfold RAG_s, fold subtree_cs, rule vat_s'.rtree_RAG.subtree_del_inside) - from edge_of_th - show "(Cs cs, Th th) \ edges_in (RAG s') (Th th)" - by (unfold edges_in_def, auto simp:subtree_def) -qed - -lemma cp_kept_2: - shows "cp s th = cp s' th" - by (unfold cp_alt_def subtree_th preced_kept, auto) - -lemma eq_cp: - fixes th' - shows "cp s th' = cp s' th'" - using cp_kept_1 cp_kept_2 - by (cases "th' = th", auto) -end - - -locale step_P_cps = - fixes s' th cs s - defines s_def : "s \ (P th cs#s')" - assumes vt_s: "vt s" - -sublocale step_P_cps < vat_s : valid_trace "s" -proof - from vt_s show "vt s" . -qed - -sublocale step_P_cps < vat_s' : valid_trace "s'" -proof - from step_back_vt[OF vt_s[unfolded s_def]] show "vt s'" . -qed - -context step_P_cps -begin - -lemma readys_th: "th \ readys s'" -proof - - from step_back_step [OF vt_s[unfolded s_def]] - have "PIP s' (P th cs)" . - hence "th \ runing s'" by (cases, simp) - thus ?thesis by (simp add:readys_def runing_def) -qed - -lemma root_th: "root (RAG s') (Th th)" - using readys_root[OF readys_th] . - -lemma in_no_others_subtree: - assumes "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 root_th show ?thesis by (auto simp:root_def) - qed -qed - -lemma preced_kept: "the_preced s = the_preced s'" - by (auto simp: s_def the_preced_def preced_def) - -end - -locale step_P_cps_ne =step_P_cps + - fixes th' - assumes ne: "wq s' cs \ []" - defines th'_def: "th' \ hd (wq s' cs)" - -locale step_P_cps_e =step_P_cps + - assumes ee: "wq s' cs = []" - -context step_P_cps_e -begin - -lemma RAG_s: "RAG s = RAG s' \ {(Cs cs, Th th)}" -proof - - from ee and step_RAG_p[OF vt_s[unfolded s_def], folded s_def] - show ?thesis by auto -qed - -lemma subtree_kept: - assumes "th' \ th" - shows "subtree (RAG s) (Th th') = subtree (RAG s') (Th th')" -proof(unfold RAG_s, rule subtree_insert_next) - from in_no_others_subtree[OF assms] - show "Th th \ subtree (RAG s') (Th th')" . -qed - -lemma cp_kept: - assumes "th' \ th" - shows "cp s th' = cp s' th'" -proof - - have "(the_preced s ` {th'a. Th th'a \ subtree (RAG s) (Th th')}) = - (the_preced s' ` {th'a. Th th'a \ subtree (RAG s') (Th th')})" - by (unfold preced_kept subtree_kept[OF assms], simp) - thus ?thesis by (unfold cp_alt_def, simp) -qed - -end - -context step_P_cps_ne -begin - -lemma RAG_s: "RAG s = RAG s' \ {(Th th, Cs cs)}" -proof - - from step_RAG_p[OF vt_s[unfolded s_def]] and ne - show ?thesis by (simp add:s_def) -qed - -lemma cs_held: "(Cs cs, Th th') \ RAG s'" -proof - - have "(Cs cs, Th th') \ hRAG s'" - proof - - from ne - have " holding s' th' cs" - by (unfold th'_def holding_eq cs_holding_def, auto) - thus ?thesis - by (unfold hRAG_def, auto) - qed - thus ?thesis by (unfold RAG_split, auto) -qed - -lemma tRAG_s: - "tRAG s = tRAG s' \ {(Th th, Th th')}" - using RAG_tRAG_transfer[OF RAG_s cs_held] . - -lemma cp_kept: - assumes "Th th'' \ ancestors (tRAG s) (Th th)" - shows "cp s th'' = cp s' th''" -proof - - have h: "subtree (tRAG s) (Th th'') = subtree (tRAG s') (Th th'')" - proof - - have "Th th' \ subtree (tRAG s') (Th th'')" - proof - assume "Th th' \ subtree (tRAG s') (Th th'')" - thus False - proof(rule subtreeE) - assume "Th th' = Th th''" - from assms[unfolded tRAG_s ancestors_def, folded this] - show ?thesis by auto - next - assume "Th th'' \ ancestors (tRAG s') (Th th')" - moreover have "... \ ancestors (tRAG s) (Th th')" - proof(rule ancestors_mono) - show "tRAG s' \ tRAG s" by (unfold tRAG_s, auto) - qed - ultimately have "Th th'' \ ancestors (tRAG s) (Th th')" by auto - moreover have "Th th' \ ancestors (tRAG s) (Th th)" - by (unfold tRAG_s, auto simp:ancestors_def) - ultimately have "Th th'' \ ancestors (tRAG s) (Th th)" - by (auto simp:ancestors_def) - with assms show ?thesis by auto - qed - qed - from subtree_insert_next[OF this] - have "subtree (tRAG s' \ {(Th th, Th th')}) (Th th'') = subtree (tRAG s') (Th th'')" . - from this[folded tRAG_s] show ?thesis . - qed - show ?thesis by (unfold cp_alt_def1 h preced_kept, simp) -qed - -lemma cp_gen_update_stop: (* ddd *) - assumes "u \ ancestors (tRAG s) (Th th)" - and "cp_gen s u = cp_gen s' u" - and "y \ ancestors (tRAG s) u" - shows "cp_gen s y = cp_gen s' y" - using assms(3) -proof(induct rule:wf_induct[OF vat_s.fsbttRAGs.wf]) - case (1 x) - show ?case (is "?L = ?R") - proof - - from tRAG_ancestorsE[OF 1(2)] - obtain th2 where eq_x: "x = Th th2" by blast - from vat_s.cp_gen_rec[OF this] - have "?L = - Max ({the_preced s th2} \ cp_gen s ` RTree.children (tRAG s) x)" . - also have "... = - Max ({the_preced s' th2} \ cp_gen s' ` RTree.children (tRAG s') x)" - - proof - - from preced_kept have "the_preced s th2 = the_preced s' th2" by simp - moreover have "cp_gen s ` RTree.children (tRAG s) x = - cp_gen s' ` RTree.children (tRAG s') x" - proof - - have "RTree.children (tRAG s) x = RTree.children (tRAG s') x" - proof(unfold tRAG_s, rule children_union_kept) - have start: "(Th th, Th th') \ tRAG s" - by (unfold tRAG_s, auto) - note x_u = 1(2) - show "x \ Range {(Th th, Th th')}" - proof - assume "x \ Range {(Th th, Th th')}" - hence eq_x: "x = Th th'" using RangeE by auto - show False - proof(cases rule:vat_s.rtree_s.ancestors_headE[OF assms(1) start]) - case 1 - from x_u[folded this, unfolded eq_x] vat_s.acyclic_tRAG - show ?thesis by (auto simp:ancestors_def acyclic_def) - next - case 2 - with x_u[unfolded eq_x] - have "(Th th', Th th') \ (tRAG s)^+" by (auto simp:ancestors_def) - with vat_s.acyclic_tRAG show ?thesis by (auto simp:acyclic_def) - qed - qed - qed - moreover have "cp_gen s ` RTree.children (tRAG s) x = - cp_gen s' ` RTree.children (tRAG s) x" (is "?f ` ?A = ?g ` ?A") - proof(rule f_image_eq) - fix a - assume a_in: "a \ ?A" - from 1(2) - show "?f a = ?g a" - proof(cases rule:vat_s.rtree_s.ancestors_childrenE[case_names in_ch out_ch]) - case in_ch - show ?thesis - proof(cases "a = u") - case True - from assms(2)[folded this] show ?thesis . - next - case False - have a_not_in: "a \ ancestors (tRAG s) (Th th)" - proof - assume a_in': "a \ ancestors (tRAG s) (Th th)" - have "a = u" - proof(rule vat_s.rtree_s.ancestors_children_unique) - from a_in' a_in show "a \ ancestors (tRAG s) (Th th) \ - RTree.children (tRAG s) x" by auto - next - from assms(1) in_ch show "u \ ancestors (tRAG s) (Th th) \ - RTree.children (tRAG s) x" by auto - qed - with False show False by simp - qed - from a_in obtain th_a where eq_a: "a = Th th_a" - by (unfold RTree.children_def tRAG_alt_def, auto) - from cp_kept[OF a_not_in[unfolded eq_a]] - have "cp s th_a = cp s' th_a" . - from this [unfolded cp_gen_def_cond[OF eq_a], folded eq_a] - show ?thesis . - qed - next - case (out_ch z) - hence h: "z \ ancestors (tRAG s) u" "z \ RTree.children (tRAG s) x" by auto - show ?thesis - proof(cases "a = z") - case True - from h(2) have zx_in: "(z, x) \ (tRAG s)" by (auto simp:RTree.children_def) - from 1(1)[rule_format, OF this h(1)] - have eq_cp_gen: "cp_gen s z = cp_gen s' z" . - with True show ?thesis by metis - next - case False - from a_in obtain th_a where eq_a: "a = Th th_a" - by (auto simp:RTree.children_def tRAG_alt_def) - have "a \ ancestors (tRAG s) (Th th)" - proof - assume a_in': "a \ ancestors (tRAG s) (Th th)" - have "a = z" - proof(rule vat_s.rtree_s.ancestors_children_unique) - from assms(1) h(1) have "z \ ancestors (tRAG s) (Th th)" - by (auto simp:ancestors_def) - with h(2) show " z \ ancestors (tRAG s) (Th th) \ - RTree.children (tRAG s) x" by auto - next - from a_in a_in' - show "a \ ancestors (tRAG s) (Th th) \ RTree.children (tRAG s) x" - by auto - qed - with False show False by auto - qed - from cp_kept[OF this[unfolded eq_a]] - have "cp s th_a = cp s' th_a" . - from this[unfolded cp_gen_def_cond[OF eq_a], folded eq_a] - show ?thesis . - qed - qed - qed - ultimately show ?thesis by metis - qed - ultimately show ?thesis by simp - qed - also have "... = ?R" - by (fold vat_s'.cp_gen_rec[OF eq_x], simp) - finally show ?thesis . - qed -qed - -lemma cp_up: - assumes "(Th th') \ ancestors (tRAG s) (Th th)" - and "cp s th' = cp s' th'" - and "(Th th'') \ ancestors (tRAG s) (Th th')" - shows "cp s th'' = cp s' th''" -proof - - have "cp_gen s (Th th'') = cp_gen s' (Th th'')" - proof(rule cp_gen_update_stop[OF assms(1) _ assms(3)]) - from assms(2) cp_gen_def_cond[OF refl[of "Th th'"]] - show "cp_gen s (Th th') = cp_gen s' (Th th')" by metis - qed - with cp_gen_def_cond[OF refl[of "Th th''"]] - show ?thesis by metis -qed - -end - -locale step_create_cps = - fixes s' th prio s - defines s_def : "s \ (Create th prio#s')" - assumes vt_s: "vt s" - -sublocale step_create_cps < vat_s: valid_trace "s" - by (unfold_locales, insert vt_s, simp) - -sublocale step_create_cps < vat_s': valid_trace "s'" - by (unfold_locales, insert step_back_vt[OF vt_s[unfolded s_def]], simp) - -context step_create_cps -begin - -lemma RAG_kept: "RAG s = RAG s'" - by (unfold s_def RAG_create_unchanged, auto) - -lemma tRAG_kept: "tRAG s = tRAG s'" - by (unfold tRAG_alt_def RAG_kept, auto) - -lemma preced_kept: - assumes "th' \ th" - shows "the_preced s th' = the_preced s' th'" - by (unfold s_def the_preced_def preced_def, insert assms, auto) - -lemma th_not_in: "Th th \ Field (tRAG s')" -proof - - from vt_s[unfolded s_def] - have "PIP s' (Create th prio)" by (cases, simp) - hence "th \ threads s'" by(cases, simp) - from vat_s'.not_in_thread_isolated[OF this] - have "Th th \ Field (RAG s')" . - with tRAG_Field show ?thesis by auto -qed - -lemma eq_cp: - assumes neq_th: "th' \ th" - shows "cp s th' = cp s' th'" -proof - - have "(the_preced s \ the_thread) ` subtree (tRAG s) (Th th') = - (the_preced s' \ the_thread) ` subtree (tRAG s') (Th th')" - proof(unfold tRAG_kept, rule f_image_eq) - fix a - assume a_in: "a \ subtree (tRAG s') (Th th')" - then obtain th_a where eq_a: "a = Th th_a" - proof(cases rule:subtreeE) - case 2 - from ancestors_Field[OF 2(2)] - and that show ?thesis by (unfold tRAG_alt_def, auto) - qed auto - have neq_th_a: "th_a \ th" - proof - - have "(Th th) \ subtree (tRAG s') (Th th')" - proof - assume "Th th \ subtree (tRAG s') (Th th')" - thus False - proof(cases rule:subtreeE) - case 2 - from ancestors_Field[OF this(2)] - and th_not_in[unfolded Field_def] - show ?thesis by auto - qed (insert assms, auto) - qed - with a_in[unfolded eq_a] show ?thesis by auto - qed - from preced_kept[OF this] - show "(the_preced s \ the_thread) a = (the_preced s' \ the_thread) a" - by (unfold eq_a, simp) - qed - thus ?thesis by (unfold cp_alt_def1, simp) -qed - -lemma children_of_th: "RTree.children (tRAG s) (Th th) = {}" -proof - - { fix a - assume "a \ RTree.children (tRAG s) (Th th)" - hence "(a, Th th) \ tRAG s" by (auto simp:RTree.children_def) - with th_not_in have False - by (unfold Field_def tRAG_kept, auto) - } thus ?thesis by auto -qed - -lemma eq_cp_th: "cp s th = preced th s" - by (unfold vat_s.cp_rec children_of_th, simp add:the_preced_def) - -end - -locale step_exit_cps = - fixes s' th prio s - defines s_def : "s \ Exit th # s'" - assumes vt_s: "vt s" - -sublocale step_exit_cps < vat_s: valid_trace "s" - by (unfold_locales, insert vt_s, simp) - -sublocale step_exit_cps < vat_s': valid_trace "s'" - by (unfold_locales, insert step_back_vt[OF vt_s[unfolded s_def]], simp) - -context step_exit_cps -begin - -lemma preced_kept: - assumes "th' \ th" - shows "the_preced s th' = the_preced s' th'" - by (unfold s_def the_preced_def preced_def, insert assms, auto) - -lemma RAG_kept: "RAG s = RAG s'" - by (unfold s_def RAG_exit_unchanged, auto) - -lemma tRAG_kept: "tRAG s = tRAG s'" - by (unfold tRAG_alt_def RAG_kept, auto) - -lemma th_ready: "th \ readys s'" -proof - - from vt_s[unfolded s_def] - have "PIP s' (Exit th)" by (cases, simp) - hence h: "th \ runing s' \ holdents s' th = {}" by (cases, metis) - thus ?thesis by (unfold runing_def, auto) -qed - -lemma th_holdents: "holdents s' th = {}" -proof - - from vt_s[unfolded s_def] - have "PIP s' (Exit th)" by (cases, simp) - thus ?thesis by (cases, metis) -qed - -lemma th_RAG: "Th th \ Field (RAG s')" -proof - - have "Th th \ Range (RAG s')" - proof - assume "Th th \ Range (RAG s')" - then obtain cs where "holding (wq s') th cs" - by (unfold Range_iff s_RAG_def, auto) - with th_holdents[unfolded holdents_def] - show False by (unfold eq_holding, auto) - qed - moreover have "Th th \ Domain (RAG s')" - proof - assume "Th th \ Domain (RAG s')" - then obtain cs where "waiting (wq s') th cs" - by (unfold Domain_iff s_RAG_def, auto) - with th_ready show False by (unfold readys_def eq_waiting, auto) - qed - ultimately show ?thesis by (auto simp:Field_def) -qed - -lemma th_tRAG: "(Th th) \ Field (tRAG s')" - using th_RAG tRAG_Field[of s'] by auto - -lemma eq_cp: - assumes neq_th: "th' \ th" - shows "cp s th' = cp s' th'" -proof - - have "(the_preced s \ the_thread) ` subtree (tRAG s) (Th th') = - (the_preced s' \ the_thread) ` subtree (tRAG s') (Th th')" - proof(unfold tRAG_kept, rule f_image_eq) - fix a - assume a_in: "a \ subtree (tRAG s') (Th th')" - then obtain th_a where eq_a: "a = Th th_a" - proof(cases rule:subtreeE) - case 2 - from ancestors_Field[OF 2(2)] - and that show ?thesis by (unfold tRAG_alt_def, auto) - qed auto - have neq_th_a: "th_a \ th" - proof - - find_theorems readys subtree s' - from vat_s'.readys_in_no_subtree[OF th_ready assms] - have "(Th th) \ subtree (RAG s') (Th th')" . - with tRAG_subtree_RAG[of s' "Th th'"] - have "(Th th) \ subtree (tRAG s') (Th th')" by auto - with a_in[unfolded eq_a] show ?thesis by auto - qed - from preced_kept[OF this] - show "(the_preced s \ the_thread) a = (the_preced s' \ the_thread) a" - by (unfold eq_a, simp) - qed - thus ?thesis by (unfold cp_alt_def1, simp) -qed - -end - -end - diff -r b620a2a0806a -r b4bcd1edbb6d ExtGG.thy --- a/ExtGG.thy Wed Jan 06 20:46:14 2016 +0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,922 +0,0 @@ -theory ExtGG -imports PrioG CpsG -begin - -text {* - The following two auxiliary lemmas are used to reason about @{term Max}. -*} -lemma image_Max_eqI: - assumes "finite B" - and "b \ B" - and "\ x \ B. f x \ f b" - shows "Max (f ` B) = f b" - using assms - using Max_eqI by blast - -lemma image_Max_subset: - assumes "finite A" - and "B \ A" - and "a \ B" - and "Max (f ` A) = f a" - shows "Max (f ` B) = f a" -proof(rule image_Max_eqI) - show "finite B" - using assms(1) assms(2) finite_subset by auto -next - show "a \ B" using assms by simp -next - show "\x\B. f x \ f a" - by (metis Max_ge assms(1) assms(2) assms(4) - finite_imageI image_eqI subsetCE) -qed - -text {* - The following locale @{text "highest_gen"} sets the basic context for our - investigation: supposing thread @{text th} holds the highest @{term cp}-value - in state @{text s}, which means the task for @{text th} is the - most urgent. We want to show that - @{text th} is treated correctly by PIP, which means - @{text th} will not be blocked unreasonably by other less urgent - threads. -*} -locale highest_gen = - fixes s th prio tm - assumes vt_s: "vt s" - and threads_s: "th \ threads s" - and highest: "preced th s = Max ((cp s)`threads s)" - -- {* The internal structure of @{term th}'s precedence is exposed:*} - and preced_th: "preced th s = Prc prio tm" - --- {* @{term s} is a valid trace, so it will inherit all results derived for - a valid trace: *} -sublocale highest_gen < vat_s: valid_trace "s" - by (unfold_locales, insert vt_s, simp) - -context highest_gen -begin - -text {* - @{term tm} is the time when the precedence of @{term th} is set, so - @{term tm} must be a valid moment index into @{term s}. -*} -lemma lt_tm: "tm < length s" - by (insert preced_tm_lt[OF threads_s preced_th], simp) - -text {* - Since @{term th} holds the highest precedence and @{text "cp"} - is the highest precedence of all threads in the sub-tree of - @{text "th"} and @{text th} is among these threads, - its @{term cp} must equal to its precedence: -*} -lemma eq_cp_s_th: "cp s th = preced th s" (is "?L = ?R") -proof - - have "?L \ ?R" - by (unfold highest, rule Max_ge, - auto simp:threads_s finite_threads) - moreover have "?R \ ?L" - by (unfold vat_s.cp_rec, rule Max_ge, - auto simp:the_preced_def vat_s.fsbttRAGs.finite_children) - ultimately show ?thesis by auto -qed - -(* ccc *) -lemma highest_cp_preced: "cp s th = Max ((\ th'. preced th' s) ` threads s)" - by (fold max_cp_eq, unfold eq_cp_s_th, insert highest, simp) - -lemma highest_preced_thread: "preced th s = Max ((\ th'. preced th' s) ` threads s)" - by (fold eq_cp_s_th, unfold highest_cp_preced, simp) - -lemma highest': "cp s th = Max (cp s ` threads s)" -proof - - from highest_cp_preced max_cp_eq[symmetric] - show ?thesis by simp -qed - -end - -locale extend_highest_gen = highest_gen + - fixes t - assumes vt_t: "vt (t@s)" - and create_low: "Create th' prio' \ set t \ prio' \ prio" - and set_diff_low: "Set th' prio' \ set t \ th' \ th \ prio' \ prio" - and exit_diff: "Exit th' \ set t \ th' \ th" - -sublocale extend_highest_gen < vat_t: valid_trace "t@s" - by (unfold_locales, insert vt_t, simp) - -lemma step_back_vt_app: - assumes vt_ts: "vt (t@s)" - shows "vt s" -proof - - from vt_ts show ?thesis - proof(induct t) - case Nil - from Nil show ?case by auto - next - case (Cons e t) - assume ih: " vt (t @ s) \ vt s" - and vt_et: "vt ((e # t) @ s)" - show ?case - proof(rule ih) - show "vt (t @ s)" - proof(rule step_back_vt) - from vt_et show "vt (e # t @ s)" by simp - qed - qed - qed -qed - - -locale red_extend_highest_gen = extend_highest_gen + - fixes i::nat - -sublocale red_extend_highest_gen < red_moment: extend_highest_gen "s" "th" "prio" "tm" "(moment i t)" - apply (insert extend_highest_gen_axioms, subst (asm) (1) moment_restm_s [of i t, symmetric]) - apply (unfold extend_highest_gen_def extend_highest_gen_axioms_def, clarsimp) - by (unfold highest_gen_def, auto dest:step_back_vt_app) - - -context extend_highest_gen -begin - - lemma ind [consumes 0, case_names Nil Cons, induct type]: - assumes - h0: "R []" - and h2: "\ e t. \vt (t@s); step (t@s) e; - extend_highest_gen s th prio tm t; - extend_highest_gen s th prio tm (e#t); R t\ \ R (e#t)" - shows "R t" -proof - - from vt_t extend_highest_gen_axioms show ?thesis - proof(induct t) - from h0 show "R []" . - next - case (Cons e t') - assume ih: "\vt (t' @ s); extend_highest_gen s th prio tm t'\ \ R t'" - and vt_e: "vt ((e # t') @ s)" - and et: "extend_highest_gen s th prio tm (e # t')" - from vt_e and step_back_step have stp: "step (t'@s) e" by auto - from vt_e and step_back_vt have vt_ts: "vt (t'@s)" by auto - show ?case - proof(rule h2 [OF vt_ts stp _ _ _ ]) - show "R t'" - proof(rule ih) - from et show ext': "extend_highest_gen s th prio tm t'" - by (unfold extend_highest_gen_def extend_highest_gen_axioms_def, auto dest:step_back_vt) - next - from vt_ts show "vt (t' @ s)" . - qed - next - from et show "extend_highest_gen s th prio tm (e # t')" . - next - from et show ext': "extend_highest_gen s th prio tm t'" - by (unfold extend_highest_gen_def extend_highest_gen_axioms_def, auto dest:step_back_vt) - qed - qed -qed - - -lemma th_kept: "th \ threads (t @ s) \ - preced th (t@s) = preced th s" (is "?Q t") -proof - - show ?thesis - proof(induct rule:ind) - case Nil - from threads_s - show ?case - by auto - next - case (Cons e t) - interpret h_e: extend_highest_gen _ _ _ _ "(e # t)" using Cons by auto - interpret h_t: extend_highest_gen _ _ _ _ t using Cons by auto - show ?case - proof(cases e) - case (Create thread prio) - show ?thesis - proof - - from Cons and Create have "step (t@s) (Create thread prio)" by auto - hence "th \ thread" - proof(cases) - case thread_create - with Cons show ?thesis by auto - qed - hence "preced th ((e # t) @ s) = preced th (t @ s)" - by (unfold Create, auto simp:preced_def) - moreover note Cons - ultimately show ?thesis - by (auto simp:Create) - qed - next - case (Exit thread) - from h_e.exit_diff and Exit - have neq_th: "thread \ th" by auto - with Cons - show ?thesis - by (unfold Exit, auto simp:preced_def) - next - case (P thread cs) - with Cons - show ?thesis - by (auto simp:P preced_def) - next - case (V thread cs) - with Cons - show ?thesis - by (auto simp:V preced_def) - next - case (Set thread prio') - show ?thesis - proof - - from h_e.set_diff_low and Set - have "th \ thread" by auto - hence "preced th ((e # t) @ s) = preced th (t @ s)" - by (unfold Set, auto simp:preced_def) - moreover note Cons - ultimately show ?thesis - by (auto simp:Set) - qed - qed - qed -qed - -text {* - According to @{thm th_kept}, thread @{text "th"} has its living status - and precedence kept along the way of @{text "t"}. The following lemma - shows that this preserved precedence of @{text "th"} remains as the highest - along the way of @{text "t"}. - - The proof goes by induction over @{text "t"} using the specialized - induction rule @{thm ind}, followed by case analysis of each possible - operations of PIP. All cases follow the same pattern rendered by the - generalized introduction rule @{thm "image_Max_eqI"}. - - The very essence is to show that precedences, no matter whether they are newly introduced - or modified, are always lower than the one held by @{term "th"}, - which by @{thm th_kept} is preserved along the way. -*} -lemma max_kept: "Max (the_preced (t @ s) ` (threads (t@s))) = preced th s" -proof(induct rule:ind) - case Nil - from highest_preced_thread - show ?case - by (unfold the_preced_def, simp) -next - case (Cons e t) - interpret h_e: extend_highest_gen _ _ _ _ "(e # t)" using Cons by auto - interpret h_t: extend_highest_gen _ _ _ _ t using Cons by auto - show ?case - proof(cases e) - case (Create thread prio') - show ?thesis (is "Max (?f ` ?A) = ?t") - proof - - -- {* The following is the common pattern of each branch of the case analysis. *} - -- {* The major part is to show that @{text "th"} holds the highest precedence: *} - have "Max (?f ` ?A) = ?f th" - proof(rule image_Max_eqI) - show "finite ?A" using h_e.finite_threads by auto - next - show "th \ ?A" using h_e.th_kept by auto - next - show "\x\?A. ?f x \ ?f th" - proof - fix x - assume "x \ ?A" - hence "x = thread \ x \ threads (t@s)" by (auto simp:Create) - thus "?f x \ ?f th" - proof - assume "x = thread" - thus ?thesis - apply (simp add:Create the_preced_def preced_def, fold preced_def) - using Create h_e.create_low h_t.th_kept lt_tm preced_leI2 preced_th by force - next - assume h: "x \ threads (t @ s)" - from Cons(2)[unfolded Create] - have "x \ thread" using h by (cases, auto) - hence "?f x = the_preced (t@s) x" - by (simp add:Create the_preced_def preced_def) - hence "?f x \ Max (the_preced (t@s) ` threads (t@s))" - by (simp add: h_t.finite_threads h) - also have "... = ?f th" - by (metis Cons.hyps(5) h_e.th_kept the_preced_def) - finally show ?thesis . - qed - qed - qed - -- {* The minor part is to show that the precedence of @{text "th"} - equals to preserved one, given by the foregoing lemma @{thm th_kept} *} - also have "... = ?t" using h_e.th_kept the_preced_def by auto - -- {* Then it follows trivially that the precedence preserved - for @{term "th"} remains the maximum of all living threads along the way. *} - finally show ?thesis . - qed - next - case (Exit thread) - show ?thesis (is "Max (?f ` ?A) = ?t") - proof - - have "Max (?f ` ?A) = ?f th" - proof(rule image_Max_eqI) - show "finite ?A" using h_e.finite_threads by auto - next - show "th \ ?A" using h_e.th_kept by auto - next - show "\x\?A. ?f x \ ?f th" - proof - fix x - assume "x \ ?A" - hence "x \ threads (t@s)" by (simp add: Exit) - hence "?f x \ Max (?f ` threads (t@s))" - by (simp add: h_t.finite_threads) - also have "... \ ?f th" - apply (simp add:Exit the_preced_def preced_def, fold preced_def) - using Cons.hyps(5) h_t.th_kept the_preced_def by auto - finally show "?f x \ ?f th" . - qed - qed - also have "... = ?t" using h_e.th_kept the_preced_def by auto - finally show ?thesis . - qed - next - case (P thread cs) - with Cons - show ?thesis by (auto simp:preced_def the_preced_def) - next - case (V thread cs) - with Cons - show ?thesis by (auto simp:preced_def the_preced_def) - next - case (Set thread prio') - show ?thesis (is "Max (?f ` ?A) = ?t") - proof - - have "Max (?f ` ?A) = ?f th" - proof(rule image_Max_eqI) - show "finite ?A" using h_e.finite_threads by auto - next - show "th \ ?A" using h_e.th_kept by auto - next - show "\x\?A. ?f x \ ?f th" - proof - fix x - assume h: "x \ ?A" - show "?f x \ ?f th" - proof(cases "x = thread") - case True - moreover have "the_preced (Set thread prio' # t @ s) thread \ the_preced (t @ s) th" - proof - - have "the_preced (t @ s) th = Prc prio tm" - using h_t.th_kept preced_th by (simp add:the_preced_def) - moreover have "prio' \ prio" using Set h_e.set_diff_low by auto - ultimately show ?thesis by (insert lt_tm, auto simp:the_preced_def preced_def) - qed - ultimately show ?thesis - by (unfold Set, simp add:the_preced_def preced_def) - next - case False - then have "?f x = the_preced (t@s) x" - by (simp add:the_preced_def preced_def Set) - also have "... \ Max (the_preced (t@s) ` threads (t@s))" - using Set h h_t.finite_threads by auto - also have "... = ?f th" by (metis Cons.hyps(5) h_e.th_kept the_preced_def) - finally show ?thesis . - qed - qed - qed - also have "... = ?t" using h_e.th_kept the_preced_def by auto - finally show ?thesis . - qed - qed -qed - -lemma max_preced: "preced th (t@s) = Max (the_preced (t@s) ` (threads (t@s)))" - by (insert th_kept max_kept, auto) - -text {* - The reason behind the following lemma is that: - Since @{term "cp"} is defined as the maximum precedence - of those threads contained in the sub-tree of node @{term "Th th"} - in @{term "RAG (t@s)"}, and all these threads are living threads, and - @{term "th"} is also among them, the maximum precedence of - them all must be the one for @{text "th"}. -*} -lemma th_cp_max_preced: - "cp (t@s) th = Max (the_preced (t@s) ` (threads (t@s)))" (is "?L = ?R") -proof - - let ?f = "the_preced (t@s)" - have "?L = ?f th" - proof(unfold cp_alt_def, rule image_Max_eqI) - show "finite {th'. Th th' \ subtree (RAG (t @ s)) (Th th)}" - proof - - have "{th'. Th th' \ subtree (RAG (t @ s)) (Th th)} = - the_thread ` {n . n \ subtree (RAG (t @ s)) (Th th) \ - (\ th'. n = Th th')}" - by (smt Collect_cong Setcompr_eq_image mem_Collect_eq the_thread.simps) - moreover have "finite ..." by (simp add: vat_t.fsbtRAGs.finite_subtree) - ultimately show ?thesis by simp - qed - next - show "th \ {th'. Th th' \ subtree (RAG (t @ s)) (Th th)}" - by (auto simp:subtree_def) - next - show "\x\{th'. Th th' \ subtree (RAG (t @ s)) (Th th)}. - the_preced (t @ s) x \ the_preced (t @ s) th" - proof - fix th' - assume "th' \ {th'. Th th' \ subtree (RAG (t @ s)) (Th th)}" - hence "Th th' \ subtree (RAG (t @ s)) (Th th)" by auto - moreover have "... \ Field (RAG (t @ s)) \ {Th th}" - by (meson subtree_Field) - ultimately have "Th th' \ ..." by auto - hence "th' \ threads (t@s)" - proof - assume "Th th' \ {Th th}" - thus ?thesis using th_kept by auto - next - assume "Th th' \ Field (RAG (t @ s))" - thus ?thesis using vat_t.not_in_thread_isolated by blast - qed - thus "the_preced (t @ s) th' \ the_preced (t @ s) th" - by (metis Max_ge finite_imageI finite_threads image_eqI - max_kept th_kept the_preced_def) - qed - qed - also have "... = ?R" by (simp add: max_preced the_preced_def) - finally show ?thesis . -qed - -lemma th_cp_max: "cp (t@s) th = Max (cp (t@s) ` threads (t@s))" - using max_cp_eq th_cp_max_preced the_preced_def vt_t by presburger - -lemma th_cp_preced: "cp (t@s) th = preced th s" - by (fold max_kept, unfold th_cp_max_preced, simp) - -lemma preced_less: - assumes th'_in: "th' \ threads s" - and neq_th': "th' \ th" - shows "preced th' s < preced th s" - using assms -by (metis Max.coboundedI finite_imageI highest not_le order.trans - preced_linorder rev_image_eqI threads_s vat_s.finite_threads - vat_s.le_cp) - -text {* - Counting of the number of @{term "P"} and @{term "V"} operations - is the cornerstone of a large number of the following proofs. - The reason is that this counting is quite easy to calculate and - convenient to use in the reasoning. - - The following lemma shows that the counting controls whether - a thread is running or not. -*} - -lemma pv_blocked_pre: - assumes th'_in: "th' \ threads (t@s)" - and neq_th': "th' \ th" - and eq_pv: "cntP (t@s) th' = cntV (t@s) th'" - shows "th' \ runing (t@s)" -proof - assume otherwise: "th' \ runing (t@s)" - show False - proof - - have "th' = th" - proof(rule preced_unique) - show "preced th' (t @ s) = preced th (t @ s)" (is "?L = ?R") - proof - - have "?L = cp (t@s) th'" - by (unfold cp_eq_cpreced cpreced_def count_eq_dependants[OF eq_pv], simp) - also have "... = cp (t @ s) th" using otherwise - by (metis (mono_tags, lifting) mem_Collect_eq - runing_def th_cp_max vat_t.max_cp_readys_threads) - also have "... = ?R" by (metis th_cp_preced th_kept) - finally show ?thesis . - qed - qed (auto simp: th'_in th_kept) - moreover have "th' \ th" using neq_th' . - ultimately show ?thesis by simp - qed -qed - -lemmas pv_blocked = pv_blocked_pre[folded detached_eq] - -lemma runing_precond_pre: - fixes th' - assumes th'_in: "th' \ threads s" - and eq_pv: "cntP s th' = cntV s th'" - and neq_th': "th' \ th" - shows "th' \ threads (t@s) \ - cntP (t@s) th' = cntV (t@s) th'" -proof(induct rule:ind) - case (Cons e t) - interpret vat_t: extend_highest_gen s th prio tm t using Cons by simp - interpret vat_e: extend_highest_gen s th prio tm "(e # t)" using Cons by simp - show ?case - proof(cases e) - case (P thread cs) - show ?thesis - proof - - have "cntP ((e # t) @ s) th' = cntV ((e # t) @ s) th'" - proof - - have "thread \ th'" - proof - - have "step (t@s) (P thread cs)" using Cons P by auto - thus ?thesis - proof(cases) - assume "thread \ runing (t@s)" - moreover have "th' \ runing (t@s)" using Cons(5) - by (metis neq_th' vat_t.pv_blocked_pre) - ultimately show ?thesis by auto - qed - qed with Cons show ?thesis - by (unfold P, simp add:cntP_def cntV_def count_def) - qed - moreover have "th' \ threads ((e # t) @ s)" using Cons by (unfold P, simp) - ultimately show ?thesis by auto - qed - next - case (V thread cs) - show ?thesis - proof - - have "cntP ((e # t) @ s) th' = cntV ((e # t) @ s) th'" - proof - - have "thread \ th'" - proof - - have "step (t@s) (V thread cs)" using Cons V by auto - thus ?thesis - proof(cases) - assume "thread \ runing (t@s)" - moreover have "th' \ runing (t@s)" using Cons(5) - by (metis neq_th' vat_t.pv_blocked_pre) - ultimately show ?thesis by auto - qed - qed with Cons show ?thesis - by (unfold V, simp add:cntP_def cntV_def count_def) - qed - moreover have "th' \ threads ((e # t) @ s)" using Cons by (unfold V, simp) - ultimately show ?thesis by auto - qed - next - case (Create thread prio') - show ?thesis - proof - - have "cntP ((e # t) @ s) th' = cntV ((e # t) @ s) th'" - proof - - have "thread \ th'" - proof - - have "step (t@s) (Create thread prio')" using Cons Create by auto - thus ?thesis using Cons(5) by (cases, auto) - qed with Cons show ?thesis - by (unfold Create, simp add:cntP_def cntV_def count_def) - qed - moreover have "th' \ threads ((e # t) @ s)" using Cons by (unfold Create, simp) - ultimately show ?thesis by auto - qed - next - case (Exit thread) - show ?thesis - proof - - have neq_thread: "thread \ th'" - proof - - have "step (t@s) (Exit thread)" using Cons Exit by auto - thus ?thesis apply (cases) using Cons(5) - by (metis neq_th' vat_t.pv_blocked_pre) - qed - hence "cntP ((e # t) @ s) th' = cntV ((e # t) @ s) th'" using Cons - by (unfold Exit, simp add:cntP_def cntV_def count_def) - moreover have "th' \ threads ((e # t) @ s)" using Cons neq_thread - by (unfold Exit, simp) - ultimately show ?thesis by auto - qed - next - case (Set thread prio') - with Cons - show ?thesis - by (auto simp:cntP_def cntV_def count_def) - qed -next - case Nil - with assms - show ?case by auto -qed - -text {* Changing counting balance to detachedness *} -lemmas runing_precond_pre_dtc = runing_precond_pre - [folded vat_t.detached_eq vat_s.detached_eq] - -lemma runing_precond: - fixes th' - assumes th'_in: "th' \ threads s" - and neq_th': "th' \ th" - and is_runing: "th' \ runing (t@s)" - shows "cntP s th' > cntV s th'" - using assms -proof - - have "cntP s th' \ cntV s th'" - by (metis is_runing neq_th' pv_blocked_pre runing_precond_pre th'_in) - moreover have "cntV s th' \ cntP s th'" using vat_s.cnp_cnv_cncs by auto - ultimately show ?thesis by auto -qed - -lemma moment_blocked_pre: - assumes neq_th': "th' \ th" - and th'_in: "th' \ threads ((moment i t)@s)" - and eq_pv: "cntP ((moment i t)@s) th' = cntV ((moment i t)@s) th'" - shows "cntP ((moment (i+j) t)@s) th' = cntV ((moment (i+j) t)@s) th' \ - th' \ threads ((moment (i+j) t)@s)" -proof - - interpret h_i: red_extend_highest_gen _ _ _ _ _ i - by (unfold_locales) - interpret h_j: red_extend_highest_gen _ _ _ _ _ "i+j" - by (unfold_locales) - interpret h: extend_highest_gen "((moment i t)@s)" th prio tm "moment j (restm i t)" - proof(unfold_locales) - show "vt (moment i t @ s)" by (metis h_i.vt_t) - next - show "th \ threads (moment i t @ s)" by (metis h_i.th_kept) - next - show "preced th (moment i t @ s) = - Max (cp (moment i t @ s) ` threads (moment i t @ s))" - by (metis h_i.th_cp_max h_i.th_cp_preced h_i.th_kept) - next - show "preced th (moment i t @ s) = Prc prio tm" by (metis h_i.th_kept preced_th) - next - show "vt (moment j (restm i t) @ moment i t @ s)" - using moment_plus_split by (metis add.commute append_assoc h_j.vt_t) - next - fix th' prio' - assume "Create th' prio' \ set (moment j (restm i t))" - thus "prio' \ prio" using assms - by (metis Un_iff add.commute h_j.create_low moment_plus_split set_append) - next - fix th' prio' - assume "Set th' prio' \ set (moment j (restm i t))" - thus "th' \ th \ prio' \ prio" - by (metis Un_iff add.commute h_j.set_diff_low moment_plus_split set_append) - next - fix th' - assume "Exit th' \ set (moment j (restm i t))" - thus "th' \ th" - by (metis Un_iff add.commute h_j.exit_diff moment_plus_split set_append) - qed - show ?thesis - by (metis add.commute append_assoc eq_pv h.runing_precond_pre - moment_plus_split neq_th' th'_in) -qed - -lemma moment_blocked_eqpv: - assumes neq_th': "th' \ th" - and th'_in: "th' \ threads ((moment i t)@s)" - and eq_pv: "cntP ((moment i t)@s) th' = cntV ((moment i t)@s) th'" - and le_ij: "i \ j" - shows "cntP ((moment j t)@s) th' = cntV ((moment j t)@s) th' \ - th' \ threads ((moment j t)@s) \ - th' \ runing ((moment j t)@s)" -proof - - from moment_blocked_pre [OF neq_th' th'_in eq_pv, of "j-i"] and le_ij - have h1: "cntP ((moment j t)@s) th' = cntV ((moment j t)@s) th'" - and h2: "th' \ threads ((moment j t)@s)" by auto - moreover have "th' \ runing ((moment j t)@s)" - proof - - interpret h: red_extend_highest_gen _ _ _ _ _ j by (unfold_locales) - show ?thesis - using h.pv_blocked_pre h1 h2 neq_th' by auto - qed - ultimately show ?thesis by auto -qed - -(* The foregoing two lemmas are preparation for this one, but - in long run can be combined. Maybe I am wrong. -*) -lemma moment_blocked: - assumes neq_th': "th' \ th" - and th'_in: "th' \ threads ((moment i t)@s)" - and dtc: "detached (moment i t @ s) th'" - and le_ij: "i \ j" - shows "detached (moment j t @ s) th' \ - th' \ threads ((moment j t)@s) \ - th' \ runing ((moment j t)@s)" -proof - - interpret h_i: red_extend_highest_gen _ _ _ _ _ i by (unfold_locales) - interpret h_j: red_extend_highest_gen _ _ _ _ _ j by (unfold_locales) - have cnt_i: "cntP (moment i t @ s) th' = cntV (moment i t @ s) th'" - by (metis dtc h_i.detached_elim) - from moment_blocked_eqpv[OF neq_th' th'_in cnt_i le_ij] - show ?thesis by (metis h_j.detached_intro) -qed - -lemma runing_preced_inversion: - assumes runing': "th' \ runing (t@s)" - shows "cp (t@s) th' = preced th s" (is "?L = ?R") -proof - - have "?L = Max (cp (t @ s) ` readys (t @ s))" using assms - by (unfold runing_def, auto) - also have "\ = ?R" - by (metis th_cp_max th_cp_preced vat_t.max_cp_readys_threads) - finally show ?thesis . -qed - -text {* - The situation when @{term "th"} is blocked is analyzed by the following lemmas. -*} - -text {* - The following lemmas shows the running thread @{text "th'"}, if it is different from - @{term th}, must be live at the very beginning. By the term {\em the very beginning}, - we mean the moment where the formal investigation starts, i.e. the moment (or state) - @{term s}. -*} - -lemma runing_inversion_0: - assumes neq_th': "th' \ th" - and runing': "th' \ runing (t@s)" - shows "th' \ threads s" -proof - - -- {* The proof is by contradiction: *} - { assume otherwise: "\ ?thesis" - have "th' \ runing (t @ s)" - proof - - -- {* Since @{term "th'"} is running at time @{term "t@s"}, so it exists that time. *} - have th'_in: "th' \ threads (t@s)" using runing' by (simp add:runing_def readys_def) - -- {* However, @{text "th'"} does not exist at very beginning. *} - have th'_notin: "th' \ threads (moment 0 t @ s)" using otherwise - by (metis append.simps(1) moment_zero) - -- {* Therefore, there must be a moment during @{text "t"}, when - @{text "th'"} came into being. *} - -- {* Let us suppose the moment being @{text "i"}: *} - from p_split_gen[OF th'_in th'_notin] - obtain i where lt_its: "i < length t" - and le_i: "0 \ i" - and pre: " th' \ threads (moment i t @ s)" (is "th' \ threads ?pre") - and post: "(\i'>i. th' \ threads (moment i' t @ s))" by (auto) - interpret h_i: red_extend_highest_gen _ _ _ _ _ i by (unfold_locales) - interpret h_i': red_extend_highest_gen _ _ _ _ _ "(Suc i)" by (unfold_locales) - from lt_its have "Suc i \ length t" by auto - -- {* Let us also suppose the event which makes this change is @{text e}: *} - from moment_head[OF this] obtain e where - eq_me: "moment (Suc i) t = e # moment i t" by blast - hence "vt (e # (moment i t @ s))" by (metis append_Cons h_i'.vt_t) - hence "PIP (moment i t @ s) e" by (cases, simp) - -- {* It can be derived that this event @{text "e"}, which - gives birth to @{term "th'"} must be a @{term "Create"}: *} - from create_pre[OF this, of th'] - obtain prio where eq_e: "e = Create th' prio" - by (metis append_Cons eq_me lessI post pre) - have h1: "th' \ threads (moment (Suc i) t @ s)" using post by auto - have h2: "cntP (moment (Suc i) t @ s) th' = cntV (moment (Suc i) t@ s) th'" - proof - - have "cntP (moment i t@s) th' = cntV (moment i t@s) th'" - by (metis h_i.cnp_cnv_eq pre) - thus ?thesis by (simp add:eq_me eq_e cntP_def cntV_def count_def) - qed - show ?thesis - using moment_blocked_eqpv [OF neq_th' h1 h2, of "length t"] lt_its moment_ge - by auto - qed - with `th' \ runing (t@s)` - have False by simp - } thus ?thesis by auto -qed - -text {* - The second lemma says, if the running thread @{text th'} is different from - @{term th}, then this @{text th'} must in the possession of some resources - at the very beginning. - - To ease the reasoning of resource possession of one particular thread, - we used two auxiliary functions @{term cntV} and @{term cntP}, - which are the counters of @{term P}-operations and - @{term V}-operations respectively. - If the number of @{term V}-operation is less than the number of - @{term "P"}-operations, the thread must have some unreleased resource. -*} - -lemma runing_inversion_1: (* ddd *) - assumes neq_th': "th' \ th" - and runing': "th' \ runing (t@s)" - -- {* thread @{term "th'"} is a live on in state @{term "s"} and - it has some unreleased resource. *} - shows "th' \ threads s \ cntV s th' < cntP s th'" -proof - - -- {* The proof is a simple composition of @{thm runing_inversion_0} and - @{thm runing_precond}: *} - -- {* By applying @{thm runing_inversion_0} to assumptions, - it can be shown that @{term th'} is live in state @{term s}: *} - have "th' \ threads s" using runing_inversion_0[OF assms(1,2)] . - -- {* Then the thesis is derived easily by applying @{thm runing_precond}: *} - with runing_precond [OF this neq_th' runing'] show ?thesis by simp -qed - -text {* - The following lemma is just a rephrasing of @{thm runing_inversion_1}: -*} -lemma runing_inversion_2: - assumes runing': "th' \ runing (t@s)" - shows "th' = th \ (th' \ th \ th' \ threads s \ cntV s th' < cntP s th')" -proof - - from runing_inversion_1[OF _ runing'] - show ?thesis by auto -qed - -lemma runing_inversion_3: - assumes runing': "th' \ runing (t@s)" - and neq_th: "th' \ th" - shows "th' \ threads s \ (cntV s th' < cntP s th' \ cp (t@s) th' = preced th s)" - by (metis neq_th runing' runing_inversion_2 runing_preced_inversion) - -lemma runing_inversion_4: - assumes runing': "th' \ runing (t@s)" - and neq_th: "th' \ th" - shows "th' \ threads s" - and "\detached s th'" - and "cp (t@s) th' = preced th s" - apply (metis neq_th runing' runing_inversion_2) - apply (metis neq_th pv_blocked runing' runing_inversion_2 runing_precond_pre_dtc) - by (metis neq_th runing' runing_inversion_3) - - -text {* - Suppose @{term th} is not running, it is first shown that - there is a path in RAG leading from node @{term th} to another thread @{text "th'"} - in the @{term readys}-set (So @{text "th'"} is an ancestor of @{term th}}). - - Now, since @{term readys}-set is non-empty, there must be - one in it which holds the highest @{term cp}-value, which, by definition, - is the @{term runing}-thread. However, we are going to show more: this running thread - is exactly @{term "th'"}. - *} -lemma th_blockedE: (* ddd *) - assumes "th \ runing (t@s)" - obtains th' where "Th th' \ ancestors (RAG (t @ s)) (Th th)" - "th' \ runing (t@s)" -proof - - -- {* According to @{thm vat_t.th_chain_to_ready}, either - @{term "th"} is in @{term "readys"} or there is path leading from it to - one thread in @{term "readys"}. *} - have "th \ readys (t @ s) \ (\th'. th' \ readys (t @ s) \ (Th th, Th th') \ (RAG (t @ s))\<^sup>+)" - using th_kept vat_t.th_chain_to_ready by auto - -- {* However, @{term th} can not be in @{term readys}, because otherwise, since - @{term th} holds the highest @{term cp}-value, it must be @{term "runing"}. *} - moreover have "th \ readys (t@s)" - using assms runing_def th_cp_max vat_t.max_cp_readys_threads by auto - -- {* So, there must be a path from @{term th} to another thread @{text "th'"} in - term @{term readys}: *} - ultimately obtain th' where th'_in: "th' \ readys (t@s)" - and dp: "(Th th, Th th') \ (RAG (t @ s))\<^sup>+" by auto - -- {* We are going to show that this @{term th'} is running. *} - have "th' \ runing (t@s)" - proof - - -- {* We only need to show that this @{term th'} holds the highest @{term cp}-value: *} - have "cp (t@s) th' = Max (cp (t@s) ` readys (t@s))" (is "?L = ?R") - proof - - have "?L = Max ((the_preced (t @ s) \ the_thread) ` subtree (tRAG (t @ s)) (Th th'))" - by (unfold cp_alt_def1, simp) - also have "... = (the_preced (t @ s) \ the_thread) (Th th)" - proof(rule image_Max_subset) - show "finite (Th ` (threads (t@s)))" by (simp add: vat_t.finite_threads) - next - show "subtree (tRAG (t @ s)) (Th th') \ Th ` threads (t @ s)" - by (metis Range.intros dp trancl_range vat_t.range_in vat_t.subtree_tRAG_thread) - next - show "Th th \ subtree (tRAG (t @ s)) (Th th')" using dp - by (unfold tRAG_subtree_eq, auto simp:subtree_def) - next - show "Max ((the_preced (t @ s) \ the_thread) ` Th ` threads (t @ s)) = - (the_preced (t @ s) \ the_thread) (Th th)" (is "Max ?L = _") - proof - - have "?L = the_preced (t @ s) ` threads (t @ s)" - by (unfold image_comp, rule image_cong, auto) - thus ?thesis using max_preced the_preced_def by auto - qed - qed - also have "... = ?R" - using th_cp_max th_cp_preced th_kept - the_preced_def vat_t.max_cp_readys_threads by auto - finally show ?thesis . - qed - -- {* Now, since @{term th'} holds the highest @{term cp} - and we have already show it is in @{term readys}, - it is @{term runing} by definition. *} - with `th' \ readys (t@s)` show ?thesis by (simp add: runing_def) - qed - -- {* It is easy to show @{term th'} is an ancestor of @{term th}: *} - moreover have "Th th' \ ancestors (RAG (t @ s)) (Th th)" - using `(Th th, Th th') \ (RAG (t @ s))\<^sup>+` by (auto simp:ancestors_def) - ultimately show ?thesis using that by metis -qed - -text {* - Now it is easy to see there is always a thread to run by case analysis - on whether thread @{term th} is running: if the answer is Yes, the - the running thread is obviously @{term th} itself; otherwise, the running - thread is the @{text th'} given by lemma @{thm th_blockedE}. -*} -lemma live: "runing (t@s) \ {}" -proof(cases "th \ runing (t@s)") - case True thus ?thesis by auto -next - case False - thus ?thesis using th_blockedE by auto -qed - -end -end - - - diff -r b620a2a0806a -r b4bcd1edbb6d Implementation.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implementation.thy Wed Jan 06 16:34:26 2016 +0000 @@ -0,0 +1,1640 @@ +section {* + This file contains lemmas used to guide the recalculation of current precedence + after every system call (or system operation) +*} +theory Implementation +imports PIPBasics Max RTree +begin + +text {* @{text "the_preced"} is also the same as @{text "preced"}, the only + difference is the order of arguemts. *} +definition "the_preced s th = preced th s" + +lemma inj_the_preced: + "inj_on (the_preced s) (threads s)" + by (metis inj_onI preced_unique the_preced_def) + +text {* @{term "the_thread"} extracts thread out of RAG node. *} +fun the_thread :: "node \ thread" where + "the_thread (Th th) = th" + +text {* The following @{text "wRAG"} is the waiting sub-graph of @{text "RAG"}. *} +definition "wRAG (s::state) = {(Th th, Cs cs) | th cs. waiting s th cs}" + +text {* The following @{text "hRAG"} is the holding sub-graph of @{text "RAG"}. *} +definition "hRAG (s::state) = {(Cs cs, Th th) | th cs. holding s th cs}" + +text {* The following lemma splits @{term "RAG"} graph into the above two sub-graphs. *} +lemma RAG_split: "RAG s = (wRAG s \ hRAG s)" + by (unfold s_RAG_abv wRAG_def hRAG_def s_waiting_abv + s_holding_abv cs_RAG_def, auto) + +text {* + The following @{text "tRAG"} is the thread-graph derived from @{term "RAG"}. + It characterizes the dependency between threads when calculating current + precedences. It is defined as the composition of the above two sub-graphs, + names @{term "wRAG"} and @{term "hRAG"}. + *} +definition "tRAG s = wRAG s O hRAG s" + +(* ccc *) + +definition "cp_gen s x = + Max ((the_preced s \ the_thread) ` subtree (tRAG s) x)" + +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) + find_theorems wf RAG + 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}" + + +text {* (* ddd *) + One beauty of our modelling is that we follow the definitional extension tradition of HOL. + The benefit of such a concise and miniature model is that large number of intuitively + obvious facts are derived as lemmas, rather than asserted as axioms. +*} + +text {* + However, the lemmas in the forthcoming several locales are no longer + obvious. These lemmas show how the current precedences should be recalculated + after every execution step (in our model, every step is represented by an event, + which in turn, represents a system call, or operation). Each operation is + treated in a separate locale. + + The complication of current precedence recalculation comes + because the changing of RAG needs to be taken into account, + in addition to the changing of precedence. + The reason RAG changing affects current precedence is that, + according to the definition, current precedence + of a thread is the maximum of the precedences of its dependants, + where the dependants are defined in terms of RAG. + + Therefore, each operation, lemmas concerning the change of the precedences + and RAG are derived first, so that the lemmas about + current precedence recalculation can be based on. +*} + +text {* (* ddd *) + The following locale @{text "step_set_cps"} investigates the recalculation + after the @{text "Set"} operation. +*} +locale step_set_cps = + fixes s' th prio s + -- {* @{text "s'"} is the system state before the operation *} + -- {* @{text "s"} is the system state after the operation *} + defines s_def : "s \ (Set th prio#s')" + -- {* @{text "s"} is assumed to be a legitimate state, from which + the legitimacy of @{text "s"} can be derived. *} + assumes vt_s: "vt s" + +sublocale step_set_cps < vat_s : valid_trace "s" +proof + from vt_s show "vt s" . +qed + +sublocale step_set_cps < vat_s' : valid_trace "s'" +proof + from step_back_vt[OF vt_s[unfolded s_def]] show "vt s'" . +qed + +context step_set_cps +begin + +text {* (* ddd *) + The following two lemmas confirm that @{text "Set"}-operating only changes the precedence + of the initiating thread. +*} + +lemma eq_preced: + assumes "th' \ th" + shows "preced th' s = preced th' s'" +proof - + from assms show ?thesis + by (unfold s_def, auto simp:preced_def) +qed + +lemma eq_the_preced: + fixes th' + assumes "th' \ th" + shows "the_preced s th' = the_preced s' th'" + using assms + by (unfold the_preced_def, intro eq_preced, simp) + +text {* + The following lemma assures that the resetting of priority does not change the RAG. +*} + +lemma eq_dep: "RAG s = RAG s'" + by (unfold s_def RAG_set_unchanged, auto) + +text {* (* ddd *) + Th following lemma @{text "eq_cp_pre"} says the priority change of @{text "th"} + only affects those threads, which as @{text "Th th"} in their sub-trees. + + The proof of this lemma is simplified by using the alternative definition of @{text "cp"}. +*} + +lemma eq_cp_pre: + fixes th' + assumes nd: "Th th \ subtree (RAG s') (Th th')" + shows "cp s th' = cp s' th'" +proof - + -- {* After unfolding using the alternative definition, elements + affecting the @{term "cp"}-value of threads become explicit. + We only need to prove the following: *} + have "Max (the_preced s ` {th'a. Th th'a \ subtree (RAG s) (Th th')}) = + Max (the_preced s' ` {th'a. Th th'a \ subtree (RAG s') (Th th')})" + (is "Max (?f ` ?S1) = Max (?g ` ?S2)") + proof - + -- {* The base sets are equal. *} + have "?S1 = ?S2" using eq_dep by simp + -- {* The function values on the base set are equal as well. *} + moreover have "\ e \ ?S2. ?f e = ?g e" + proof + fix th1 + assume "th1 \ ?S2" + with nd have "th1 \ th" by (auto) + from eq_the_preced[OF this] + show "the_preced s th1 = the_preced s' th1" . + qed + -- {* Therefore, the image of the functions are equal. *} + ultimately have "(?f ` ?S1) = (?g ` ?S2)" by (auto intro!:f_image_eq) + thus ?thesis by simp + qed + thus ?thesis by (simp add:cp_alt_def) +qed + +text {* + The following lemma shows that @{term "th"} is not in the + sub-tree of any other thread. +*} +lemma th_in_no_subtree: + assumes "th' \ th" + shows "Th th \ subtree (RAG s') (Th th')" +proof - + have "th \ readys s'" + proof - + from step_back_step [OF vt_s[unfolded s_def]] + have "step s' (Set th prio)" . + hence "th \ runing s'" by (cases, simp) + thus ?thesis by (simp add:readys_def runing_def) + qed + find_theorems readys subtree + from vat_s'.readys_in_no_subtree[OF this assms(1)] + show ?thesis by blast +qed + +text {* + By combining @{thm "eq_cp_pre"} and @{thm "th_in_no_subtree"}, + it is obvious that the change of priority only affects the @{text "cp"}-value + of the initiating thread @{text "th"}. +*} +lemma eq_cp: + fixes th' + assumes "th' \ th" + shows "cp s th' = cp s' th'" + by (rule eq_cp_pre[OF th_in_no_subtree[OF assms]]) + +end + +text {* + The following @{text "step_v_cps"} is the locale for @{text "V"}-operation. +*} + +locale step_v_cps = + -- {* @{text "th"} is the initiating thread *} + -- {* @{text "cs"} is the critical resource release by the @{text "V"}-operation *} + fixes s' th cs s -- {* @{text "s'"} is the state before operation*} + defines s_def : "s \ (V th cs#s')" -- {* @{text "s"} is the state after operation*} + -- {* @{text "s"} is assumed to be valid, which implies the validity of @{text "s'"} *} + assumes vt_s: "vt s" + +sublocale step_v_cps < vat_s : valid_trace "s" +proof + from vt_s show "vt s" . +qed + +sublocale step_v_cps < vat_s' : valid_trace "s'" +proof + from step_back_vt[OF vt_s[unfolded s_def]] show "vt s'" . +qed + +context step_v_cps +begin + +lemma ready_th_s': "th \ readys s'" + using step_back_step[OF vt_s[unfolded s_def]] + by (cases, simp add:runing_def) + +lemma ancestors_th: "ancestors (RAG s') (Th th) = {}" +proof - + from vat_s'.readys_root[OF ready_th_s'] + show ?thesis + by (unfold root_def, simp) +qed + +lemma holding_th: "holding s' th cs" +proof - + from vt_s[unfolded s_def] + have " PIP s' (V th cs)" by (cases, simp) + thus ?thesis by (cases, auto) +qed + +lemma edge_of_th: + "(Cs cs, Th th) \ RAG s'" +proof - + from holding_th + show ?thesis + by (unfold s_RAG_def holding_eq, auto) +qed + +lemma ancestors_cs: + "ancestors (RAG s') (Cs cs) = {Th th}" +proof - + have "ancestors (RAG s') (Cs cs) = ancestors (RAG s') (Th th) \ {Th th}" + proof(rule vat_s'.rtree_RAG.ancestors_accum) + from vt_s[unfolded s_def] + have " PIP s' (V th cs)" by (cases, simp) + thus "(Cs cs, Th th) \ RAG s'" + proof(cases) + assume "holding s' th cs" + from this[unfolded holding_eq] + show ?thesis by (unfold s_RAG_def, auto) + qed + qed + from this[unfolded ancestors_th] show ?thesis by simp +qed + +lemma preced_kept: "the_preced s = the_preced s'" + by (auto simp: s_def the_preced_def preced_def) + +end + +text {* + The following @{text "step_v_cps_nt"} is the sub-locale for @{text "V"}-operation, + which represents the case when there is another thread @{text "th'"} + to take over the critical resource released by the initiating thread @{text "th"}. +*} +locale step_v_cps_nt = step_v_cps + + fixes th' + -- {* @{text "th'"} is assumed to take over @{text "cs"} *} + assumes nt: "next_th s' th cs th'" + +context step_v_cps_nt +begin + +text {* + Lemma @{text "RAG_s"} confirms the change of RAG: + two edges removed and one added, as shown by the following diagram. +*} + +(* + RAG before the V-operation + th1 ----| + | + th' ----| + |----> cs -----| + th2 ----| | + | | + th3 ----| | + |------> th + th4 ----| | + | | + th5 ----| | + |----> cs'-----| + th6 ----| + | + th7 ----| + + RAG after the V-operation + th1 ----| + | + |----> cs ----> th' + th2 ----| + | + th3 ----| + + th4 ----| + | + th5 ----| + |----> cs'----> th + th6 ----| + | + th7 ----| +*) + +lemma sub_RAGs': "{(Cs cs, Th th), (Th th', Cs cs)} \ RAG s'" + using next_th_RAG[OF nt] . + +lemma ancestors_th': + "ancestors (RAG s') (Th th') = {Th th, Cs cs}" +proof - + have "ancestors (RAG s') (Th th') = ancestors (RAG s') (Cs cs) \ {Cs cs}" + proof(rule vat_s'.rtree_RAG.ancestors_accum) + from sub_RAGs' show "(Th th', Cs cs) \ RAG s'" by auto + qed + thus ?thesis using ancestors_th ancestors_cs by auto +qed + +lemma RAG_s: + "RAG s = (RAG s' - {(Cs cs, Th th), (Th th', Cs cs)}) \ + {(Cs cs, Th th')}" +proof - + from step_RAG_v[OF vt_s[unfolded s_def], folded s_def] + and nt show ?thesis by (auto intro:next_th_unique) +qed + +lemma subtree_kept: + assumes "th1 \ {th, th'}" + shows "subtree (RAG s) (Th th1) = subtree (RAG s') (Th th1)" (is "_ = ?R") +proof - + let ?RAG' = "(RAG s' - {(Cs cs, Th th), (Th th', Cs cs)})" + let ?RAG'' = "?RAG' \ {(Cs cs, Th th')}" + have "subtree ?RAG' (Th th1) = ?R" + proof(rule subset_del_subtree_outside) + show "Range {(Cs cs, Th th), (Th th', Cs cs)} \ subtree (RAG s') (Th th1) = {}" + proof - + have "(Th th) \ subtree (RAG s') (Th th1)" + proof(rule subtree_refute) + show "Th th1 \ ancestors (RAG s') (Th th)" + by (unfold ancestors_th, simp) + next + from assms show "Th th1 \ Th th" by simp + qed + moreover have "(Cs cs) \ subtree (RAG s') (Th th1)" + proof(rule subtree_refute) + show "Th th1 \ ancestors (RAG s') (Cs cs)" + by (unfold ancestors_cs, insert assms, auto) + qed simp + ultimately have "{Th th, Cs cs} \ subtree (RAG s') (Th th1) = {}" by auto + thus ?thesis by simp + qed + qed + moreover have "subtree ?RAG'' (Th th1) = subtree ?RAG' (Th th1)" + proof(rule subtree_insert_next) + show "Th th' \ subtree (RAG s' - {(Cs cs, Th th), (Th th', Cs cs)}) (Th th1)" + proof(rule subtree_refute) + show "Th th1 \ ancestors (RAG s' - {(Cs cs, Th th), (Th th', Cs cs)}) (Th th')" + (is "_ \ ?R") + proof - + have "?R \ ancestors (RAG s') (Th th')" by (rule ancestors_mono, auto) + moreover have "Th th1 \ ..." using ancestors_th' assms by simp + ultimately show ?thesis by auto + qed + next + from assms show "Th th1 \ Th th'" by simp + qed + qed + ultimately show ?thesis by (unfold RAG_s, simp) +qed + +lemma cp_kept: + assumes "th1 \ {th, th'}" + shows "cp s th1 = cp s' th1" + by (unfold cp_alt_def preced_kept subtree_kept[OF assms], simp) + +end + +locale step_v_cps_nnt = step_v_cps + + assumes nnt: "\ th'. (\ next_th s' th cs th')" + +context step_v_cps_nnt +begin + +lemma RAG_s: "RAG s = RAG s' - {(Cs cs, Th th)}" +proof - + from nnt and step_RAG_v[OF vt_s[unfolded s_def], folded s_def] + show ?thesis by auto +qed + +lemma subtree_kept: + assumes "th1 \ th" + shows "subtree (RAG s) (Th th1) = subtree (RAG s') (Th th1)" +proof(unfold RAG_s, rule subset_del_subtree_outside) + show "Range {(Cs cs, Th th)} \ subtree (RAG s') (Th th1) = {}" + proof - + have "(Th th) \ subtree (RAG s') (Th th1)" + proof(rule subtree_refute) + show "Th th1 \ ancestors (RAG s') (Th th)" + by (unfold ancestors_th, simp) + next + from assms show "Th th1 \ Th th" by simp + qed + thus ?thesis by auto + qed +qed + +lemma cp_kept_1: + assumes "th1 \ th" + shows "cp s th1 = cp s' th1" + by (unfold cp_alt_def preced_kept subtree_kept[OF assms], simp) + +lemma subtree_cs: "subtree (RAG s') (Cs cs) = {Cs cs}" +proof - + { fix n + have "(Cs cs) \ ancestors (RAG s') n" + proof + assume "Cs cs \ ancestors (RAG s') n" + hence "(n, Cs cs) \ (RAG s')^+" by (auto simp:ancestors_def) + from tranclE[OF this] obtain nn where h: "(nn, Cs cs) \ RAG s'" by auto + then obtain th' where "nn = Th th'" + by (unfold s_RAG_def, auto) + from h[unfolded this] have "(Th th', Cs cs) \ RAG s'" . + from this[unfolded s_RAG_def] + have "waiting (wq s') th' cs" by auto + from this[unfolded cs_waiting_def] + have "1 < length (wq s' cs)" + by (cases "wq s' cs", auto) + from holding_next_thI[OF holding_th this] + obtain th' where "next_th s' th cs th'" by auto + with nnt show False by auto + qed + } note h = this + { fix n + assume "n \ subtree (RAG s') (Cs cs)" + hence "n = (Cs cs)" + by (elim subtreeE, insert h, auto) + } moreover have "(Cs cs) \ subtree (RAG s') (Cs cs)" + by (auto simp:subtree_def) + ultimately show ?thesis by auto +qed + +lemma subtree_th: + "subtree (RAG s) (Th th) = subtree (RAG s') (Th th) - {Cs cs}" +find_theorems "subtree" "_ - _" RAG +proof(unfold RAG_s, fold subtree_cs, rule vat_s'.rtree_RAG.subtree_del_inside) + from edge_of_th + show "(Cs cs, Th th) \ edges_in (RAG s') (Th th)" + by (unfold edges_in_def, auto simp:subtree_def) +qed + +lemma cp_kept_2: + shows "cp s th = cp s' th" + by (unfold cp_alt_def subtree_th preced_kept, auto) + +lemma eq_cp: + fixes th' + shows "cp s th' = cp s' th'" + using cp_kept_1 cp_kept_2 + by (cases "th' = th", auto) +end + + +locale step_P_cps = + fixes s' th cs s + defines s_def : "s \ (P th cs#s')" + assumes vt_s: "vt s" + +sublocale step_P_cps < vat_s : valid_trace "s" +proof + from vt_s show "vt s" . +qed + +sublocale step_P_cps < vat_s' : valid_trace "s'" +proof + from step_back_vt[OF vt_s[unfolded s_def]] show "vt s'" . +qed + +context step_P_cps +begin + +lemma readys_th: "th \ readys s'" +proof - + from step_back_step [OF vt_s[unfolded s_def]] + have "PIP s' (P th cs)" . + hence "th \ runing s'" by (cases, simp) + thus ?thesis by (simp add:readys_def runing_def) +qed + +lemma root_th: "root (RAG s') (Th th)" + using readys_root[OF readys_th] . + +lemma in_no_others_subtree: + assumes "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 root_th show ?thesis by (auto simp:root_def) + qed +qed + +lemma preced_kept: "the_preced s = the_preced s'" + by (auto simp: s_def the_preced_def preced_def) + +end + +locale step_P_cps_ne =step_P_cps + + fixes th' + assumes ne: "wq s' cs \ []" + defines th'_def: "th' \ hd (wq s' cs)" + +locale step_P_cps_e =step_P_cps + + assumes ee: "wq s' cs = []" + +context step_P_cps_e +begin + +lemma RAG_s: "RAG s = RAG s' \ {(Cs cs, Th th)}" +proof - + from ee and step_RAG_p[OF vt_s[unfolded s_def], folded s_def] + show ?thesis by auto +qed + +lemma subtree_kept: + assumes "th' \ th" + shows "subtree (RAG s) (Th th') = subtree (RAG s') (Th th')" +proof(unfold RAG_s, rule subtree_insert_next) + from in_no_others_subtree[OF assms] + show "Th th \ subtree (RAG s') (Th th')" . +qed + +lemma cp_kept: + assumes "th' \ th" + shows "cp s th' = cp s' th'" +proof - + have "(the_preced s ` {th'a. Th th'a \ subtree (RAG s) (Th th')}) = + (the_preced s' ` {th'a. Th th'a \ subtree (RAG s') (Th th')})" + by (unfold preced_kept subtree_kept[OF assms], simp) + thus ?thesis by (unfold cp_alt_def, simp) +qed + +end + +context step_P_cps_ne +begin + +lemma RAG_s: "RAG s = RAG s' \ {(Th th, Cs cs)}" +proof - + from step_RAG_p[OF vt_s[unfolded s_def]] and ne + show ?thesis by (simp add:s_def) +qed + +lemma cs_held: "(Cs cs, Th th') \ RAG s'" +proof - + have "(Cs cs, Th th') \ hRAG s'" + proof - + from ne + have " holding s' th' cs" + by (unfold th'_def holding_eq cs_holding_def, auto) + thus ?thesis + by (unfold hRAG_def, auto) + qed + thus ?thesis by (unfold RAG_split, auto) +qed + +lemma tRAG_s: + "tRAG s = tRAG s' \ {(Th th, Th th')}" + using RAG_tRAG_transfer[OF RAG_s cs_held] . + +lemma cp_kept: + assumes "Th th'' \ ancestors (tRAG s) (Th th)" + shows "cp s th'' = cp s' th''" +proof - + have h: "subtree (tRAG s) (Th th'') = subtree (tRAG s') (Th th'')" + proof - + have "Th th' \ subtree (tRAG s') (Th th'')" + proof + assume "Th th' \ subtree (tRAG s') (Th th'')" + thus False + proof(rule subtreeE) + assume "Th th' = Th th''" + from assms[unfolded tRAG_s ancestors_def, folded this] + show ?thesis by auto + next + assume "Th th'' \ ancestors (tRAG s') (Th th')" + moreover have "... \ ancestors (tRAG s) (Th th')" + proof(rule ancestors_mono) + show "tRAG s' \ tRAG s" by (unfold tRAG_s, auto) + qed + ultimately have "Th th'' \ ancestors (tRAG s) (Th th')" by auto + moreover have "Th th' \ ancestors (tRAG s) (Th th)" + by (unfold tRAG_s, auto simp:ancestors_def) + ultimately have "Th th'' \ ancestors (tRAG s) (Th th)" + by (auto simp:ancestors_def) + with assms show ?thesis by auto + qed + qed + from subtree_insert_next[OF this] + have "subtree (tRAG s' \ {(Th th, Th th')}) (Th th'') = subtree (tRAG s') (Th th'')" . + from this[folded tRAG_s] show ?thesis . + qed + show ?thesis by (unfold cp_alt_def1 h preced_kept, simp) +qed + +lemma cp_gen_update_stop: (* ddd *) + assumes "u \ ancestors (tRAG s) (Th th)" + and "cp_gen s u = cp_gen s' u" + and "y \ ancestors (tRAG s) u" + shows "cp_gen s y = cp_gen s' y" + using assms(3) +proof(induct rule:wf_induct[OF vat_s.fsbttRAGs.wf]) + case (1 x) + show ?case (is "?L = ?R") + proof - + from tRAG_ancestorsE[OF 1(2)] + obtain th2 where eq_x: "x = Th th2" by blast + from vat_s.cp_gen_rec[OF this] + have "?L = + Max ({the_preced s th2} \ cp_gen s ` RTree.children (tRAG s) x)" . + also have "... = + Max ({the_preced s' th2} \ cp_gen s' ` RTree.children (tRAG s') x)" + + proof - + from preced_kept have "the_preced s th2 = the_preced s' th2" by simp + moreover have "cp_gen s ` RTree.children (tRAG s) x = + cp_gen s' ` RTree.children (tRAG s') x" + proof - + have "RTree.children (tRAG s) x = RTree.children (tRAG s') x" + proof(unfold tRAG_s, rule children_union_kept) + have start: "(Th th, Th th') \ tRAG s" + by (unfold tRAG_s, auto) + note x_u = 1(2) + show "x \ Range {(Th th, Th th')}" + proof + assume "x \ Range {(Th th, Th th')}" + hence eq_x: "x = Th th'" using RangeE by auto + show False + proof(cases rule:vat_s.rtree_s.ancestors_headE[OF assms(1) start]) + case 1 + from x_u[folded this, unfolded eq_x] vat_s.acyclic_tRAG + show ?thesis by (auto simp:ancestors_def acyclic_def) + next + case 2 + with x_u[unfolded eq_x] + have "(Th th', Th th') \ (tRAG s)^+" by (auto simp:ancestors_def) + with vat_s.acyclic_tRAG show ?thesis by (auto simp:acyclic_def) + qed + qed + qed + moreover have "cp_gen s ` RTree.children (tRAG s) x = + cp_gen s' ` RTree.children (tRAG s) x" (is "?f ` ?A = ?g ` ?A") + proof(rule f_image_eq) + fix a + assume a_in: "a \ ?A" + from 1(2) + show "?f a = ?g a" + proof(cases rule:vat_s.rtree_s.ancestors_childrenE[case_names in_ch out_ch]) + case in_ch + show ?thesis + proof(cases "a = u") + case True + from assms(2)[folded this] show ?thesis . + next + case False + have a_not_in: "a \ ancestors (tRAG s) (Th th)" + proof + assume a_in': "a \ ancestors (tRAG s) (Th th)" + have "a = u" + proof(rule vat_s.rtree_s.ancestors_children_unique) + from a_in' a_in show "a \ ancestors (tRAG s) (Th th) \ + RTree.children (tRAG s) x" by auto + next + from assms(1) in_ch show "u \ ancestors (tRAG s) (Th th) \ + RTree.children (tRAG s) x" by auto + qed + with False show False by simp + qed + from a_in obtain th_a where eq_a: "a = Th th_a" + by (unfold RTree.children_def tRAG_alt_def, auto) + from cp_kept[OF a_not_in[unfolded eq_a]] + have "cp s th_a = cp s' th_a" . + from this [unfolded cp_gen_def_cond[OF eq_a], folded eq_a] + show ?thesis . + qed + next + case (out_ch z) + hence h: "z \ ancestors (tRAG s) u" "z \ RTree.children (tRAG s) x" by auto + show ?thesis + proof(cases "a = z") + case True + from h(2) have zx_in: "(z, x) \ (tRAG s)" by (auto simp:RTree.children_def) + from 1(1)[rule_format, OF this h(1)] + have eq_cp_gen: "cp_gen s z = cp_gen s' z" . + with True show ?thesis by metis + next + case False + from a_in obtain th_a where eq_a: "a = Th th_a" + by (auto simp:RTree.children_def tRAG_alt_def) + have "a \ ancestors (tRAG s) (Th th)" + proof + assume a_in': "a \ ancestors (tRAG s) (Th th)" + have "a = z" + proof(rule vat_s.rtree_s.ancestors_children_unique) + from assms(1) h(1) have "z \ ancestors (tRAG s) (Th th)" + by (auto simp:ancestors_def) + with h(2) show " z \ ancestors (tRAG s) (Th th) \ + RTree.children (tRAG s) x" by auto + next + from a_in a_in' + show "a \ ancestors (tRAG s) (Th th) \ RTree.children (tRAG s) x" + by auto + qed + with False show False by auto + qed + from cp_kept[OF this[unfolded eq_a]] + have "cp s th_a = cp s' th_a" . + from this[unfolded cp_gen_def_cond[OF eq_a], folded eq_a] + show ?thesis . + qed + qed + qed + ultimately show ?thesis by metis + qed + ultimately show ?thesis by simp + qed + also have "... = ?R" + by (fold vat_s'.cp_gen_rec[OF eq_x], simp) + finally show ?thesis . + qed +qed + +lemma cp_up: + assumes "(Th th') \ ancestors (tRAG s) (Th th)" + and "cp s th' = cp s' th'" + and "(Th th'') \ ancestors (tRAG s) (Th th')" + shows "cp s th'' = cp s' th''" +proof - + have "cp_gen s (Th th'') = cp_gen s' (Th th'')" + proof(rule cp_gen_update_stop[OF assms(1) _ assms(3)]) + from assms(2) cp_gen_def_cond[OF refl[of "Th th'"]] + show "cp_gen s (Th th') = cp_gen s' (Th th')" by metis + qed + with cp_gen_def_cond[OF refl[of "Th th''"]] + show ?thesis by metis +qed + +end + +locale step_create_cps = + fixes s' th prio s + defines s_def : "s \ (Create th prio#s')" + assumes vt_s: "vt s" + +sublocale step_create_cps < vat_s: valid_trace "s" + by (unfold_locales, insert vt_s, simp) + +sublocale step_create_cps < vat_s': valid_trace "s'" + by (unfold_locales, insert step_back_vt[OF vt_s[unfolded s_def]], simp) + +context step_create_cps +begin + +lemma RAG_kept: "RAG s = RAG s'" + by (unfold s_def RAG_create_unchanged, auto) + +lemma tRAG_kept: "tRAG s = tRAG s'" + by (unfold tRAG_alt_def RAG_kept, auto) + +lemma preced_kept: + assumes "th' \ th" + shows "the_preced s th' = the_preced s' th'" + by (unfold s_def the_preced_def preced_def, insert assms, auto) + +lemma th_not_in: "Th th \ Field (tRAG s')" +proof - + from vt_s[unfolded s_def] + have "PIP s' (Create th prio)" by (cases, simp) + hence "th \ threads s'" by(cases, simp) + from vat_s'.not_in_thread_isolated[OF this] + have "Th th \ Field (RAG s')" . + with tRAG_Field show ?thesis by auto +qed + +lemma eq_cp: + assumes neq_th: "th' \ th" + shows "cp s th' = cp s' th'" +proof - + have "(the_preced s \ the_thread) ` subtree (tRAG s) (Th th') = + (the_preced s' \ the_thread) ` subtree (tRAG s') (Th th')" + proof(unfold tRAG_kept, rule f_image_eq) + fix a + assume a_in: "a \ subtree (tRAG s') (Th th')" + then obtain th_a where eq_a: "a = Th th_a" + proof(cases rule:subtreeE) + case 2 + from ancestors_Field[OF 2(2)] + and that show ?thesis by (unfold tRAG_alt_def, auto) + qed auto + have neq_th_a: "th_a \ th" + proof - + have "(Th th) \ subtree (tRAG s') (Th th')" + proof + assume "Th th \ subtree (tRAG s') (Th th')" + thus False + proof(cases rule:subtreeE) + case 2 + from ancestors_Field[OF this(2)] + and th_not_in[unfolded Field_def] + show ?thesis by auto + qed (insert assms, auto) + qed + with a_in[unfolded eq_a] show ?thesis by auto + qed + from preced_kept[OF this] + show "(the_preced s \ the_thread) a = (the_preced s' \ the_thread) a" + by (unfold eq_a, simp) + qed + thus ?thesis by (unfold cp_alt_def1, simp) +qed + +lemma children_of_th: "RTree.children (tRAG s) (Th th) = {}" +proof - + { fix a + assume "a \ RTree.children (tRAG s) (Th th)" + hence "(a, Th th) \ tRAG s" by (auto simp:RTree.children_def) + with th_not_in have False + by (unfold Field_def tRAG_kept, auto) + } thus ?thesis by auto +qed + +lemma eq_cp_th: "cp s th = preced th s" + by (unfold vat_s.cp_rec children_of_th, simp add:the_preced_def) + +end + +locale step_exit_cps = + fixes s' th prio s + defines s_def : "s \ Exit th # s'" + assumes vt_s: "vt s" + +sublocale step_exit_cps < vat_s: valid_trace "s" + by (unfold_locales, insert vt_s, simp) + +sublocale step_exit_cps < vat_s': valid_trace "s'" + by (unfold_locales, insert step_back_vt[OF vt_s[unfolded s_def]], simp) + +context step_exit_cps +begin + +lemma preced_kept: + assumes "th' \ th" + shows "the_preced s th' = the_preced s' th'" + by (unfold s_def the_preced_def preced_def, insert assms, auto) + +lemma RAG_kept: "RAG s = RAG s'" + by (unfold s_def RAG_exit_unchanged, auto) + +lemma tRAG_kept: "tRAG s = tRAG s'" + by (unfold tRAG_alt_def RAG_kept, auto) + +lemma th_ready: "th \ readys s'" +proof - + from vt_s[unfolded s_def] + have "PIP s' (Exit th)" by (cases, simp) + hence h: "th \ runing s' \ holdents s' th = {}" by (cases, metis) + thus ?thesis by (unfold runing_def, auto) +qed + +lemma th_holdents: "holdents s' th = {}" +proof - + from vt_s[unfolded s_def] + have "PIP s' (Exit th)" by (cases, simp) + thus ?thesis by (cases, metis) +qed + +lemma th_RAG: "Th th \ Field (RAG s')" +proof - + have "Th th \ Range (RAG s')" + proof + assume "Th th \ Range (RAG s')" + then obtain cs where "holding (wq s') th cs" + by (unfold Range_iff s_RAG_def, auto) + with th_holdents[unfolded holdents_def] + show False by (unfold eq_holding, auto) + qed + moreover have "Th th \ Domain (RAG s')" + proof + assume "Th th \ Domain (RAG s')" + then obtain cs where "waiting (wq s') th cs" + by (unfold Domain_iff s_RAG_def, auto) + with th_ready show False by (unfold readys_def eq_waiting, auto) + qed + ultimately show ?thesis by (auto simp:Field_def) +qed + +lemma th_tRAG: "(Th th) \ Field (tRAG s')" + using th_RAG tRAG_Field[of s'] by auto + +lemma eq_cp: + assumes neq_th: "th' \ th" + shows "cp s th' = cp s' th'" +proof - + have "(the_preced s \ the_thread) ` subtree (tRAG s) (Th th') = + (the_preced s' \ the_thread) ` subtree (tRAG s') (Th th')" + proof(unfold tRAG_kept, rule f_image_eq) + fix a + assume a_in: "a \ subtree (tRAG s') (Th th')" + then obtain th_a where eq_a: "a = Th th_a" + proof(cases rule:subtreeE) + case 2 + from ancestors_Field[OF 2(2)] + and that show ?thesis by (unfold tRAG_alt_def, auto) + qed auto + have neq_th_a: "th_a \ th" + proof - + find_theorems readys subtree s' + from vat_s'.readys_in_no_subtree[OF th_ready assms] + have "(Th th) \ subtree (RAG s') (Th th')" . + with tRAG_subtree_RAG[of s' "Th th'"] + have "(Th th) \ subtree (tRAG s') (Th th')" by auto + with a_in[unfolded eq_a] show ?thesis by auto + qed + from preced_kept[OF this] + show "(the_preced s \ the_thread) a = (the_preced s' \ the_thread) a" + by (unfold eq_a, simp) + qed + thus ?thesis by (unfold cp_alt_def1, simp) +qed + +end + +end + diff -r b620a2a0806a -r b4bcd1edbb6d Journal/Paper.thy --- a/Journal/Paper.thy Wed Jan 06 20:46:14 2016 +0800 +++ b/Journal/Paper.thy Wed Jan 06 16:34:26 2016 +0000 @@ -1,6 +1,8 @@ (*<*) theory Paper -imports "../CpsG" "../ExtGG" "~~/src/HOL/Library/LaTeXsugar" +imports "../Implementation" + "../Correctness" + "~~/src/HOL/Library/LaTeXsugar" begin @@ -19,7 +21,7 @@ Cs ("C") and readys ("ready") and preced ("prec") and - preceds ("precs") and +(* preceds ("precs") and*) cpreced ("cprec") and cp ("cprec") and holdents ("resources") and @@ -361,7 +363,7 @@ We also use the abbreviation \begin{isabelle}\ \ \ \ \ %%% - @{abbrev "preceds s ths"} + ???preceds s ths \end{isabelle} \noindent @@ -898,6 +900,53 @@ However, we are able to combine their two separate bounds into a single theorem improving their bound. + \subsection*{\bf OUTLINE} + + Since @{term "th"} is the most urgent thread, if it is somehow + blocked, people want to know why and wether this blocking is + reasonable. + + @{thm [source] th_blockedE} @{thm th_blockedE} + + if @{term "th"} is blocked, then there is a path leading from + @{term "th"} to @{term "th'"}, which means: + there is a chain of demand leading from @{term th} to @{term th'}. + + %%% in other words + %%% th -> cs1 -> th1 -> cs2 -> th2 -> ... -> csn -> thn -> cs -> th'. + %%% + %%% We says that th is blocked by "th'". + + THEN + + @{thm [source] vat_t.th_chain_to_ready} @{thm vat_t.th_chain_to_ready} + + It is basic propery with non-trival proof. + + THEN + + @{thm [source] max_preced} @{thm max_preced} + + which says @{term "th"} holds the max precedence. + + THEN + + @{thm [source] th_cp_max th_cp_preced th_kept} + @{thm th_cp_max th_cp_preced th_kept} + + THENTHEN + + @{thm [source] runing_inversion_4} @{thm runing_inversion_4} + + which explains what the @{term "th'"} looks like. Now, we have found the + @{term "th'"} which blocks @{term th}, we need to know more about it. + To see what kind of thread can block @{term th}. + + From these two lemmas we can see the correctness of PIP, which is + that: the blockage of th is reasonable and under control. + + \subsection*{END OUTLINE} + In what follows we will describe properties of PIP that allow us to prove Theorem~\ref{mainthm} and, when instructive, briefly describe our argument. It is relatively easy to see that: @@ -915,9 +964,10 @@ \begin{isabelle}\ \ \ \ \ %%% \begin{tabular}{@ {}l} - @{thm[mode=IfThen] waiting_unique[of _ _ "cs\<^sub>1" "cs\<^sub>2"]}\\ - @{thm[mode=IfThen] held_unique[of _ "th\<^sub>1" _ "th\<^sub>2"]}\\ - @{thm[mode=IfThen] runing_unique[of _ "th\<^sub>1" "th\<^sub>2"]} + HERE?? + %@ {thm[mode=IfThen] waiting_unique[of _ _ "cs\<^sub>1" "cs\<^sub>2"]}\\ + %@ {thm[mode=IfThen] held_unique[of _ "th\<^sub>1" _ "th\<^sub>2"]}\\ + %@ {thm[mode=IfThen] runing_unique[of _ "th\<^sub>1" "th\<^sub>2"]} \end{tabular} \end{isabelle} @@ -931,13 +981,13 @@ \begin{isabelle}\ \ \ \ \ %%% \begin{tabular}{@ {}l} - @{text If}~@{thm (prem 1) acyclic_RAG}~@{text "then"}:\\ + HERE?? %@{text If}~@ {thm (prem 1) acyclic_RAG}~@{text "then"}:\\ \hspace{5mm}@{thm (concl) acyclic_RAG}, @{thm (concl) finite_RAG} and @{thm (concl) wf_dep_converse},\\ - \hspace{5mm}@{text "if"}~@{thm (prem 2) dm_RAG_threads}~@{text "then"}~@{thm (concl) dm_RAG_threads} + %\hspace{5mm}@{text "if"}~@ {thm (prem 2) dm_RAG_threads}~@{text "then"}~@{thm (concl) dm_RAG_threads} and\\ - \hspace{5mm}@{text "if"}~@{thm (prem 2) range_in}~@{text "then"}~@{thm (concl) range_in}. + %\hspace{5mm}@{text "if"}~@ {thm (prem 2) range_in}~@{text "then"}~@{thm (concl) range_in}. \end{tabular} \end{isabelle} @@ -1136,7 +1186,7 @@ \begin{isabelle}\ \ \ \ \ %%% \begin{tabular}{@ {}l} - @{thm children_def2} + HERE?? %%@ {thm children_def2} \end{tabular} \end{isabelle} @@ -1146,9 +1196,9 @@ a resource). We can prove the following lemma. \begin{lemma}\label{childrenlem} - @{text "If"} @{thm (prem 1) cp_rec} @{text "then"} + HERE %@{text "If"} @ {thm (prem 1) cp_rec} @{text "then"} \begin{center} - @{thm (concl) cp_rec}. + %@ {thm (concl) cp_rec}. \end{center} \end{lemma} @@ -1184,7 +1234,7 @@ \begin{isabelle}\ \ \ \ \ %%% \begin{tabular}{@ {}l} - @{thm eq_dep},\\ + HERE ?? %@ {thm eq_dep},\\ @{thm eq_cp_th}, and\\ @{thm[mode=IfThen] eq_cp} \end{tabular} @@ -1208,7 +1258,7 @@ \begin{isabelle}\ \ \ \ \ %%% \begin{tabular}{@ {}l} - @{thm eq_dep}, and\\ + HERE %@ {thm eq_dep}, and\\ @{thm[mode=IfThen] eq_cp} \end{tabular} \end{isabelle} @@ -1257,9 +1307,9 @@ % %\begin{isabelle}\ \ \ \ \ %%% %\begin{tabular}{@ {}l} - %@{thm[mode=IfThen] eq_up_self}\\ - %@{text "If"} @{thm (prem 1) eq_up}, @{thm (prem 2) eq_up} and @{thm (prem 3) eq_up}\\ - %@{text "then"} @{thm (concl) eq_up}. + %@ {thm[mode=IfThen] eq_up_self}\\ + %@{text "If"} @ {thm (prem 1) eq_up}, @ {thm (prem 2) eq_up} and @ {thm (prem 3) eq_up}\\ + %@{text "then"} @ {thm (concl) eq_up}. %\end{tabular} %\end{isabelle} % @@ -1330,7 +1380,7 @@ \begin{isabelle}\ \ \ \ \ %%% \begin{tabular}{@ {}l} @{thm RAG_s}\\ - @{thm eq_cp} + HERE %@ {thm eq_cp} \end{tabular} \end{isabelle} @@ -1343,7 +1393,7 @@ \begin{isabelle}\ \ \ \ \ %%% \begin{tabular}{@ {}l} @{thm RAG_s}\\ - @{thm[mode=IfThen] eq_cp} + HERE %@ {thm[mode=IfThen] eq_cp} \end{tabular} \end{isabelle} @@ -1362,8 +1412,9 @@ \begin{isabelle}\ \ \ \ \ %%% \begin{tabular}{@ {}l} %%@ {t hm[mode=IfThen] eq_up_self}\\ - @{text "If"} @{thm (prem 1) eq_up}, @{thm (prem 2) eq_up} and @{thm (prem 3) eq_up}\\ - @{text "then"} @{thm (concl) eq_up}. + HERE + %@{text "If"} @ {thm (prem 1) eq_up}, @ {thm (prem 2) eq_up} and @ {thm (prem 3) eq_up}\\ + %@{text "then"} @ {thm (concl) eq_up}. \end{tabular} \end{isabelle} diff -r b620a2a0806a -r b4bcd1edbb6d PIPBasics.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PIPBasics.thy Wed Jan 06 16:34:26 2016 +0000 @@ -0,0 +1,3051 @@ +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) + +context valid_trace +begin + +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(rule ind, simp add:wq_def) + fix s e + assume h1: "step s e" + and h2: "distinct (wq s cs)" + thus "distinct (wq (e # s) cs)" + proof(induct rule:step.induct, auto simp: wq_def Let_def split:list.splits) + fix thread s + assume h1: "(Cs cs, Th thread) \ (RAG s)\<^sup>+" + and h2: "thread \ set (wq_fun (schs s) cs)" + and h3: "thread \ runing s" + show "False" + proof - + from h3 have "\ cs. thread \ set (wq_fun (schs s) cs) \ + thread = hd ((wq_fun (schs s) cs))" + by (simp add:runing_def readys_def s_waiting_def wq_def) + from this [OF h2] have "thread = hd (wq_fun (schs s) cs)" . + with h2 + have "(Cs cs, Th thread) \ (RAG s)" + by (simp add:s_RAG_def s_holding_def wq_def cs_holding_def) + with h1 show False by auto + qed + next + fix thread s a list + assume dst: "distinct list" + show "distinct (SOME q. distinct q \ set q = set list)" + proof(rule someI2) + from dst show "distinct list \ set list = set list" by auto + next + fix q assume "distinct q \ set q = set list" + thus "distinct q" by auto + qed + qed +qed + +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 - + show ?thesis + proof(cases e) + case (P th cs) + with assms + show ?thesis + by (auto simp:wq_def Let_def split:if_splits) + next + case (Create th prio) + with assms show ?thesis + by (auto simp:wq_def Let_def split:if_splits) + next + case (Exit th) + with assms show ?thesis + by (auto simp:wq_def Let_def split:if_splits) + next + case (Set th prio) + with assms show ?thesis + by (auto simp:wq_def Let_def split:if_splits) + next + case (V th cs) + with vt_e assms show ?thesis + apply (auto simp:wq_def Let_def split:if_splits) + proof - + fix q qs + assume h1: "thread \ set (wq_fun (schs s) cs)" + and h2: "q # qs = wq_fun (schs s) cs" + and h3: "thread \ set (SOME q. distinct q \ set q = set qs)" + and vt: "vt (V th cs # s)" + from h1 and h2[symmetric] have "thread \ set (q # 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 h2[symmetric, 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 h3 show ?thesis by simp + qed + ultimately show "False" by auto + qed + qed +qed + +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 + +(* Wrong: + lemma \thread \ set (wq_fun cs1 s); thread \ set (wq_fun cs2 s)\ \ cs1 = cs2" +*) + +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 lose 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: + 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: + fixes x y z r + 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: + fixes r x y z + 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: + fixes r x y z + 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: +fixes th::thread +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: + fixes th thread cs rest + 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: + fixes th cs s + 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: + fixes th cs s + 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: + fixes s thread cs + 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 + +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 + +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 + +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) + +end diff -r b620a2a0806a -r b4bcd1edbb6d PIPDefs.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PIPDefs.thy Wed Jan 06 16:34:26 2016 +0000 @@ -0,0 +1,614 @@ +chapter {* Definitions *} +(*<*) +theory PIPDefs +imports Precedence_ord Moment +begin +(*>*) + +text {* + In this section, the formal model of Priority Inheritance Protocol (PIP) is presented. + The model is based on Paulson's inductive protocol verification method, where + the state of the system is modelled as a list of events happened so far with the latest + event put at the head. +*} + +text {* + To define events, the identifiers of {\em threads}, + {\em priority} and {\em critical resources } (abbreviated as @{text "cs"}) + need to be represented. All three are represetned using standard + Isabelle/HOL type @{typ "nat"}: +*} + +type_synonym thread = nat -- {* Type for thread identifiers. *} +type_synonym priority = nat -- {* Type for priorities. *} +type_synonym cs = nat -- {* Type for critical sections (or critical resources). *} + +text {* + \noindent + The abstraction of Priority Inheritance Protocol (PIP) is set at the system call level. + Every system call is represented as an event. The format of events is defined + defined as follows: + *} + +datatype event = + Create thread priority | -- {* Thread @{text "thread"} is created with priority @{text "priority"}. *} + Exit thread | -- {* Thread @{text "thread"} finishing its execution. *} + P thread cs | -- {* Thread @{text "thread"} requesting critical resource @{text "cs"}. *} + V thread cs | -- {* Thread @{text "thread"} releasing critical resource @{text "cs"}. *} + Set thread priority -- {* Thread @{text "thread"} resets its priority to @{text "priority"}. *} + + +text {* + As mentioned earlier, in Paulson's inductive method, the states of system are represented as lists of events, + which is defined by the following type @{text "state"}: + *} +type_synonym state = "event list" + + +text {* +\noindent + Resource Allocation Graph (RAG for short) is used extensively in our formal analysis. + The following type @{text "node"} is used to represent nodes in RAG. + *} +datatype node = + Th "thread" | -- {* Node for thread. *} + Cs "cs" -- {* Node for critical resource. *} + +text {* + \noindent + The following function + @{text "threads"} is used to calculate the set of live threads (@{text "threads s"}) + in state @{text "s"}. + *} +fun threads :: "state \ thread set" + where + -- {* At the start of the system, the set of threads is empty: *} + "threads [] = {}" | + -- {* New thread is added to the @{text "threads"}: *} + "threads (Create thread prio#s) = {thread} \ threads s" | + -- {* Finished thread is removed: *} + "threads (Exit thread # s) = (threads s) - {thread}" | + -- {* Other kind of events does not affect the value of @{text "threads"}: *} + "threads (e#s) = threads s" + +text {* + \noindent + The function @{text "threads"} defined above is one of + the so called {\em observation function}s which forms + the very basis of Paulson's inductive protocol verification method. + Each observation function {\em observes} one particular aspect (or attribute) + of the system. For example, the attribute observed by @{text "threads s"} + is the set of threads living in state @{text "s"}. + The protocol being modelled + The decision made the protocol being modelled is based on the {\em observation}s + returned by {\em observation function}s. Since {\observation function}s forms + the very basis on which Paulson's inductive method is based, there will be + a lot of such observation functions introduced in the following. In fact, any function + which takes event list as argument is a {\em observation function}. + *} + +text {* \noindent + Observation @{text "priority th s"} is + the {\em original priority} of thread @{text "th"} in state @{text "s"}. + The {\em original priority} is the priority + assigned to a thread when it is created or when it is reset by system call + (represented by event @{text "Set thread priority"}). +*} + +fun priority :: "thread \ state \ priority" + where + -- {* @{text "0"} is assigned to threads which have never been created: *} + "priority thread [] = 0" | + "priority thread (Create thread' prio#s) = + (if thread' = thread then prio else priority thread s)" | + "priority thread (Set thread' prio#s) = + (if thread' = thread then prio else priority thread s)" | + "priority thread (e#s) = priority thread s" + +text {* + \noindent + Observation @{text "last_set th s"} is the last time when the priority of thread @{text "th"} is set, + observed from state @{text "s"}. + The time in the system is measured by the number of events happened so far since the very beginning. +*} +fun last_set :: "thread \ state \ nat" + where + "last_set thread [] = 0" | + "last_set thread ((Create thread' prio)#s) = + (if (thread = thread') then length s else last_set thread s)" | + "last_set thread ((Set thread' prio)#s) = + (if (thread = thread') then length s else last_set thread s)" | + "last_set thread (_#s) = last_set thread s" + +text {* + \noindent + The {\em precedence} is a notion derived from {\em priority}, where the {\em precedence} of + a thread is the combination of its {\em original priority} and {\em time} the priority is set. + The intention is to discriminate threads with the same priority by giving threads whose priority + is assigned earlier higher precedences, becasue such threads are more urgent to finish. + This explains the following definition: + *} +definition preced :: "thread \ state \ precedence" + where "preced thread s \ Prc (priority thread s) (last_set thread s)" + + +text {* + \noindent + A number of important notions in PIP are represented as the following functions, + defined in terms of the waiting queues of the system, where the waiting queues + , as a whole, is represented by the @{text "wq"} argument of every notion function. + The @{text "wq"} argument is itself a functions which maps every critical resource + @{text "cs"} to the list of threads which are holding or waiting for it. + The thread at the head of this list is designated as the thread which is current + holding the resrouce, which is slightly different from tradition where + all threads in the waiting queue are considered as waiting for the resource. + *} + +consts + holding :: "'b \ thread \ cs \ bool" + waiting :: "'b \ thread \ cs \ bool" + RAG :: "'b \ (node \ node) set" + dependants :: "'b \ thread \ thread set" + +defs (overloaded) + -- {* + \begin{minipage}{0.9\textwidth} + This meaning of @{text "wq"} is reflected in the following definition of @{text "holding wq th cs"}, + where @{text "holding wq th cs"} means thread @{text "th"} is holding the critical + resource @{text "cs"}. This decision is based on @{text "wq"}. + \end{minipage} + *} + + cs_holding_def: + "holding wq thread cs \ (thread \ set (wq cs) \ thread = hd (wq cs))" + -- {* + \begin{minipage}{0.9\textwidth} + In accordance with the definition of @{text "holding wq th cs"}, + a thread @{text "th"} is considered waiting for @{text "cs"} if + it is in the {\em waiting queue} of critical resource @{text "cs"}, but not at the head. + This is reflected in the definition of @{text "waiting wq th cs"} as follows: + \end{minipage} + *} + cs_waiting_def: + "waiting wq thread cs \ (thread \ set (wq cs) \ thread \ hd (wq cs))" + -- {* + \begin{minipage}{0.9\textwidth} + @{text "RAG wq"} generates RAG (a binary relations on @{text "node"}) + out of waiting queues of the system (represented by the @{text "wq"} argument): + \end{minipage} + *} + cs_RAG_def: + "RAG (wq::cs \ thread list) \ + {(Th th, Cs cs) | th cs. waiting wq th cs} \ {(Cs cs, Th th) | cs th. holding wq th cs}" + -- {* + \begin{minipage}{0.9\textwidth} + The following @{text "dependants wq th"} represents the set of threads which are RAGing on + thread @{text "th"} in Resource Allocation Graph @{text "RAG wq"}. + Here, "RAGing" means waiting directly or indirectly on the critical resource. + \end{minipage} + *} + cs_dependants_def: + "dependants (wq::cs \ thread list) th \ {th' . (Th th', Th th) \ (RAG wq)^+}" + + +text {* \noindent + The following + @{text "cpreced s th"} gives the {\em current precedence} of thread @{text "th"} under + state @{text "s"}. The definition of @{text "cpreced"} reflects the basic idea of + Priority Inheritance that the {\em current precedence} of a thread is the precedence + inherited from the maximum of all its dependants, i.e. the threads which are waiting + directly or indirectly waiting for some resources from it. If no such thread exits, + @{text "th"}'s {\em current precedence} equals its original precedence, i.e. + @{text "preced th s"}. + *} + +definition cpreced :: "(cs \ thread list) \ state \ thread \ precedence" + where "cpreced wq s = (\th. Max ((\th'. preced th' s) ` ({th} \ dependants wq th)))" + +text {* + Notice that the current precedence (@{text "cpreced"}) of one thread @{text "th"} can be boosted + (becoming larger than its own precedence) by those threads in + the @{text "dependants wq th"}-set. If one thread get boosted, we say + it inherits the priority (or, more precisely, the precedence) of + its dependants. This is how the word "Inheritance" in + Priority Inheritance Protocol comes. +*} + +(*<*) +lemma + cpreced_def2: + "cpreced wq s th \ Max ({preced th s} \ {preced th' s | th'. th' \ dependants wq th})" + unfolding cpreced_def image_def + apply(rule eq_reflection) + apply(rule_tac f="Max" in arg_cong) + by (auto) +(*>*) + + +text {* \noindent + Assuming @{text "qs"} be the waiting queue of a critical resource, + the following abbreviation "release qs" is the waiting queue after the thread + holding the resource (which is thread at the head of @{text "qs"}) released + the resource: +*} +abbreviation + "release qs \ case qs of + [] => [] + | (_#qs') => (SOME q. distinct q \ set q = set qs')" +text {* \noindent + It can be seen from the definition that the thread at the head of @{text "qs"} is removed + from the return value, and the value @{term "q"} is an reordering of @{text "qs'"}, the + tail of @{text "qs"}. Through this reordering, one of the waiting threads (those in @{text "qs'"} } + is chosen nondeterministically to be the head of the new queue @{text "q"}. + Therefore, this thread is the one who takes over the resource. This is a little better different + from common sense that the thread who comes the earliest should take over. + The intention of this definition is to show that the choice of which thread to take over the + release resource does not affect the correctness of the PIP protocol. +*} + +text {* + The data structure used by the operating system for scheduling is referred to as + {\em schedule state}. It is represented as a record consisting of + a function assigning waiting queue to resources + (to be used as the @{text "wq"} argument in @{text "holding"}, @{text "waiting"} + and @{text "RAG"}, etc) and a function assigning precedence to threads: + *} + +record schedule_state = + wq_fun :: "cs \ thread list" -- {* The function assigning waiting queue. *} + cprec_fun :: "thread \ precedence" -- {* The function assigning precedence. *} + +text {* \noindent + The following two abbreviations (@{text "all_unlocked"} and @{text "initial_cprec"}) + are used to set the initial values of the @{text "wq_fun"} @{text "cprec_fun"} fields + respectively of the @{text "schedule_state"} record by the following function @{text "sch"}, + which is used to calculate the system's {\em schedule state}. + + Since there is no thread at the very beginning to make request, all critical resources + are free (or unlocked). This status is represented by the abbreviation + @{text "all_unlocked"}. + *} +abbreviation + "all_unlocked \ \_::cs. ([]::thread list)" + + +text {* \noindent + The initial current precedence for a thread can be anything, because there is no thread then. + We simply assume every thread has precedence @{text "Prc 0 0"}. + *} + +abbreviation + "initial_cprec \ \_::thread. Prc 0 0" + + +text {* \noindent + The following function @{text "schs"} is used to calculate the system's schedule state @{text "schs s"} + out of the current system state @{text "s"}. It is the central function to model Priority Inheritance: + *} +fun schs :: "state \ schedule_state" + where + -- {* + \begin{minipage}{0.9\textwidth} + Setting the initial value of the @{text "schedule_state"} record (see the explanations above). + \end{minipage} + *} + "schs [] = (| wq_fun = all_unlocked, cprec_fun = initial_cprec |)" | + + -- {* + \begin{minipage}{0.9\textwidth} + \begin{enumerate} + \item @{text "ps"} is the schedule state of last moment. + \item @{text "pwq"} is the waiting queue function of last moment. + \item @{text "pcp"} is the precedence function of last moment (NOT USED). + \item @{text "nwq"} is the new waiting queue function. It is calculated using a @{text "case"} statement: + \begin{enumerate} + \item If the happening event is @{text "P thread cs"}, @{text "thread"} is added to + the end of @{text "cs"}'s waiting queue. + \item If the happening event is @{text "V thread cs"} and @{text "s"} is a legal state, + @{text "th'"} must equal to @{text "thread"}, + because @{text "thread"} is the one currently holding @{text "cs"}. + The case @{text "[] \ []"} may never be executed in a legal state. + the @{text "(SOME q. distinct q \ set q = set qs)"} is used to choose arbitrarily one + thread in waiting to take over the released resource @{text "cs"}. In our representation, + this amounts to rearrange elements in waiting queue, so that one of them is put at the head. + \item For other happening event, the schedule state just does not change. + \end{enumerate} + \item @{text "ncp"} is new precedence function, it is calculated from the newly updated waiting queue + function. The RAGency of precedence function on waiting queue function is the reason to + put them in the same record so that they can evolve together. + \end{enumerate} + + + The calculation of @{text "cprec_fun"} depends on the value of @{text "wq_fun"}. + Therefore, in the following cases, @{text "wq_fun"} is always calculated first, in + the name of @{text "wq"} (if @{text "wq_fun"} is not changed + by the happening event) or @{text "new_wq"} (if the value of @{text "wq_fun"} is changed). + \end{minipage} + *} + "schs (Create th prio # s) = + (let wq = wq_fun (schs s) in + (|wq_fun = wq, cprec_fun = cpreced wq (Create th prio # s)|))" +| "schs (Exit th # s) = + (let wq = wq_fun (schs s) in + (|wq_fun = wq, cprec_fun = cpreced wq (Exit th # s)|))" +| "schs (Set th prio # s) = + (let wq = wq_fun (schs s) in + (|wq_fun = wq, cprec_fun = cpreced wq (Set th prio # s)|))" + -- {* + \begin{minipage}{0.9\textwidth} + Different from the forth coming cases, the @{text "wq_fun"} field of the schedule state + is changed. So, the new value is calculated first, in the name of @{text "new_wq"}. + \end{minipage} + *} +| "schs (P th cs # s) = + (let wq = wq_fun (schs s) in + let new_wq = wq(cs := (wq cs @ [th])) in + (|wq_fun = new_wq, cprec_fun = cpreced new_wq (P th cs # s)|))" +| "schs (V th cs # s) = + (let wq = wq_fun (schs s) in + let new_wq = wq(cs := release (wq cs)) in + (|wq_fun = new_wq, cprec_fun = cpreced new_wq (V th cs # s)|))" + +lemma cpreced_initial: + "cpreced (\ cs. []) [] = (\_. (Prc 0 0))" +apply(simp add: cpreced_def) +apply(simp add: cs_dependants_def cs_RAG_def cs_waiting_def cs_holding_def) +apply(simp add: preced_def) +done + +lemma sch_old_def: + "schs (e#s) = (let ps = schs s in + let pwq = wq_fun ps in + let nwq = case e of + P th cs \ pwq(cs:=(pwq cs @ [th])) | + V th cs \ let nq = case (pwq cs) of + [] \ [] | + (_#qs) \ (SOME q. distinct q \ set q = set qs) + in pwq(cs:=nq) | + _ \ pwq + in let ncp = cpreced nwq (e#s) in + \wq_fun = nwq, cprec_fun = ncp\ + )" +apply(cases e) +apply(simp_all) +done + + +text {* + \noindent + The following @{text "wq"} is a shorthand for @{text "wq_fun"}. + *} +definition wq :: "state \ cs \ thread list" + where "wq s = wq_fun (schs s)" + +text {* \noindent + The following @{text "cp"} is a shorthand for @{text "cprec_fun"}. + *} +definition cp :: "state \ thread \ precedence" + where "cp s \ cprec_fun (schs s)" + +text {* \noindent + Functions @{text "holding"}, @{text "waiting"}, @{text "RAG"} and + @{text "dependants"} still have the + same meaning, but redefined so that they no longer RAG on the + fictitious {\em waiting queue function} + @{text "wq"}, but on system state @{text "s"}. + *} +defs (overloaded) + s_holding_abv: + "holding (s::state) \ holding (wq_fun (schs s))" + s_waiting_abv: + "waiting (s::state) \ waiting (wq_fun (schs s))" + s_RAG_abv: + "RAG (s::state) \ RAG (wq_fun (schs s))" + s_dependants_abv: + "dependants (s::state) \ dependants (wq_fun (schs s))" + + +text {* + The following lemma can be proved easily, and the meaning is obvious. + *} +lemma + s_holding_def: + "holding (s::state) th cs \ (th \ set (wq_fun (schs s) cs) \ th = hd (wq_fun (schs s) cs))" + by (auto simp:s_holding_abv wq_def cs_holding_def) + +lemma s_waiting_def: + "waiting (s::state) th cs \ (th \ set (wq_fun (schs s) cs) \ th \ hd (wq_fun (schs s) cs))" + by (auto simp:s_waiting_abv wq_def cs_waiting_def) + +lemma s_RAG_def: + "RAG (s::state) = + {(Th th, Cs cs) | th cs. waiting (wq s) th cs} \ {(Cs cs, Th th) | cs th. holding (wq s) th cs}" + by (auto simp:s_RAG_abv wq_def cs_RAG_def) + +lemma + s_dependants_def: + "dependants (s::state) th \ {th' . (Th th', Th th) \ (RAG (wq s))^+}" + by (auto simp:s_dependants_abv wq_def cs_dependants_def) + +text {* + The following function @{text "readys"} calculates the set of ready threads. A thread is {\em ready} + for running if it is a live thread and it is not waiting for any critical resource. + *} +definition readys :: "state \ thread set" + where "readys s \ {th . th \ threads s \ (\ cs. \ waiting s th cs)}" + +text {* \noindent + The following function @{text "runing"} calculates the set of running thread, which is the ready + thread with the highest precedence. + *} +definition runing :: "state \ thread set" + where "runing s \ {th . th \ readys s \ cp s th = Max ((cp s) ` (readys s))}" + +text {* \noindent + Notice that the definition of @{text "running"} reflects the preemptive scheduling strategy, + because, if the @{text "running"}-thread (the one in @{text "runing"} set) + lowered its precedence by resetting its own priority to a lower + one, it will lose its status of being the max in @{text "ready"}-set and be superseded. +*} + +text {* \noindent + The following function @{text "holdents s th"} returns the set of resources held by thread + @{text "th"} in state @{text "s"}. + *} +definition holdents :: "state \ thread \ cs set" + where "holdents s th \ {cs . holding s th cs}" + +lemma holdents_test: + "holdents s th = {cs . (Cs cs, Th th) \ RAG s}" +unfolding holdents_def +unfolding s_RAG_def +unfolding s_holding_abv +unfolding wq_def +by (simp) + +text {* \noindent + Observation @{text "cntCS s th"} returns the number of resources held by thread @{text "th"} in + state @{text "s"}: + *} +definition cntCS :: "state \ thread \ nat" + where "cntCS s th = card (holdents s th)" + +text {* \noindent + According to the convention of Paulson's inductive method, + the decision made by a protocol that event @{text "e"} is eligible to happen next under state @{text "s"} + is expressed as @{text "step s e"}. The predicate @{text "step"} is inductively defined as + follows (notice how the decision is based on the {\em observation function}s + defined above, and also notice how a complicated protocol is modeled by a few simple + observations, and how such a kind of simplicity gives rise to improved trust on + faithfulness): + *} +inductive step :: "state \ event \ bool" + where + -- {* + A thread can be created if it is not a live thread: + *} + thread_create: "\thread \ threads s\ \ step s (Create thread prio)" | + -- {* + A thread can exit if it no longer hold any resource: + *} + thread_exit: "\thread \ runing s; holdents s thread = {}\ \ step s (Exit thread)" | + -- {* + \begin{minipage}{0.9\textwidth} + A thread can request for an critical resource @{text "cs"}, if it is running and + the request does not form a loop in the current RAG. The latter condition + is set up to avoid deadlock. The condition also reflects our assumption all threads are + carefully programmed so that deadlock can not happen: + \end{minipage} + *} + thread_P: "\thread \ runing s; (Cs cs, Th thread) \ (RAG s)^+\ \ + step s (P thread cs)" | + -- {* + \begin{minipage}{0.9\textwidth} + A thread can release a critical resource @{text "cs"} + if it is running and holding that resource: + \end{minipage} + *} + thread_V: "\thread \ runing s; holding s thread cs\ \ step s (V thread cs)" | + -- {* + \begin{minipage}{0.9\textwidth} + A thread can adjust its own priority as long as it is current running. + With the resetting of one thread's priority, its precedence may change. + If this change lowered the precedence, according to the definition of @{text "running"} + function, + \end{minipage} + *} + thread_set: "\thread \ runing s\ \ step s (Set thread prio)" + +text {* + In Paulson's inductive method, every protocol is defined by such a @{text "step"} + predicate. For instance, the predicate @{text "step"} given above + defines the PIP protocol. So, it can also be called "PIP". +*} + +abbreviation + "PIP \ step" + + +text {* \noindent + For any protocol defined by a @{text "step"} predicate, + the fact that @{text "s"} is a legal state in + the protocol is expressed as: @{text "vt step s"}, where + the predicate @{text "vt"} can be defined as the following: + *} +inductive vt :: "state \ bool" + where + -- {* Empty list @{text "[]"} is a legal state in any protocol:*} + vt_nil[intro]: "vt []" | + -- {* + \begin{minipage}{0.9\textwidth} + If @{text "s"} a legal state of the protocol defined by predicate @{text "step"}, + and event @{text "e"} is allowed to happen under state @{text "s"} by the protocol + predicate @{text "step"}, then @{text "e#s"} is a new legal state rendered by the + happening of @{text "e"}: + \end{minipage} + *} + vt_cons[intro]: "\vt s; step s e\ \ vt (e#s)" + +text {* \noindent + It is easy to see that the definition of @{text "vt"} is generic. It can be applied to + any specific protocol specified by a @{text "step"}-predicate to get the set of + legal states of that particular protocol. + *} + +text {* + The following are two very basic properties of @{text "vt"}. +*} + +lemma step_back_vt: "vt (e#s) \ vt s" + by(ind_cases "vt (e#s)", simp) + +lemma step_back_step: "vt (e#s) \ step s e" + by(ind_cases "vt (e#s)", simp) + +text {* \noindent + The following two auxiliary functions @{text "the_cs"} and @{text "the_th"} are used to extract + critical resource and thread respectively out of RAG nodes. + *} +fun the_cs :: "node \ cs" + where "the_cs (Cs cs) = cs" + +fun the_th :: "node \ thread" + where "the_th (Th th) = th" + +text {* \noindent + The following predicate @{text "next_th"} describe the next thread to + take over when a critical resource is released. In @{text "next_th s th cs t"}, + @{text "th"} is the thread to release, @{text "t"} is the one to take over. + Notice how this definition is backed up by the @{text "release"} function and its use + in the @{text "V"}-branch of @{text "schs"} function. This @{text "next_th"} function + is not needed for the execution of PIP. It is introduced as an auxiliary function + to state lemmas. The correctness of this definition will be confirmed by + lemmas @{text "step_v_hold_inv"}, @{text " step_v_wait_inv"}, + @{text "step_v_get_hold"} and @{text "step_v_not_wait"}. + *} +definition next_th:: "state \ thread \ cs \ thread \ bool" + where "next_th s th cs t = (\ rest. wq s cs = th#rest \ rest \ [] \ + t = hd (SOME q. distinct q \ set q = set rest))" + +text {* \noindent + The aux function @{text "count Q l"} is used to count the occurrence of situation @{text "Q"} + in list @{text "l"}: + *} +definition count :: "('a \ bool) \ 'a list \ nat" + where "count Q l = length (filter Q l)" + +text {* \noindent + The following observation @{text "cntP s"} returns the number of operation @{text "P"} happened + before reaching state @{text "s"}. + *} +definition cntP :: "state \ thread \ nat" + where "cntP s th = count (\ e. \ cs. e = P th cs) s" + +text {* \noindent + The following observation @{text "cntV s"} returns the number of operation @{text "V"} happened + before reaching state @{text "s"}. + *} +definition cntV :: "state \ thread \ nat" + where "cntV s th = count (\ e. \ cs. e = V th cs) s" +(*<*) + +end +(*>*) + diff -r b620a2a0806a -r b4bcd1edbb6d PrioG.thy --- a/PrioG.thy Wed Jan 06 20:46:14 2016 +0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,3051 +0,0 @@ -theory PrioG -imports PrioGDef -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) - -context valid_trace -begin - -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(rule ind, simp add:wq_def) - fix s e - assume h1: "step s e" - and h2: "distinct (wq s cs)" - thus "distinct (wq (e # s) cs)" - proof(induct rule:step.induct, auto simp: wq_def Let_def split:list.splits) - fix thread s - assume h1: "(Cs cs, Th thread) \ (RAG s)\<^sup>+" - and h2: "thread \ set (wq_fun (schs s) cs)" - and h3: "thread \ runing s" - show "False" - proof - - from h3 have "\ cs. thread \ set (wq_fun (schs s) cs) \ - thread = hd ((wq_fun (schs s) cs))" - by (simp add:runing_def readys_def s_waiting_def wq_def) - from this [OF h2] have "thread = hd (wq_fun (schs s) cs)" . - with h2 - have "(Cs cs, Th thread) \ (RAG s)" - by (simp add:s_RAG_def s_holding_def wq_def cs_holding_def) - with h1 show False by auto - qed - next - fix thread s a list - assume dst: "distinct list" - show "distinct (SOME q. distinct q \ set q = set list)" - proof(rule someI2) - from dst show "distinct list \ set list = set list" by auto - next - fix q assume "distinct q \ set q = set list" - thus "distinct q" by auto - qed - qed -qed - -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 - - show ?thesis - proof(cases e) - case (P th cs) - with assms - show ?thesis - by (auto simp:wq_def Let_def split:if_splits) - next - case (Create th prio) - with assms show ?thesis - by (auto simp:wq_def Let_def split:if_splits) - next - case (Exit th) - with assms show ?thesis - by (auto simp:wq_def Let_def split:if_splits) - next - case (Set th prio) - with assms show ?thesis - by (auto simp:wq_def Let_def split:if_splits) - next - case (V th cs) - with vt_e assms show ?thesis - apply (auto simp:wq_def Let_def split:if_splits) - proof - - fix q qs - assume h1: "thread \ set (wq_fun (schs s) cs)" - and h2: "q # qs = wq_fun (schs s) cs" - and h3: "thread \ set (SOME q. distinct q \ set q = set qs)" - and vt: "vt (V th cs # s)" - from h1 and h2[symmetric] have "thread \ set (q # 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 h2[symmetric, 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 h3 show ?thesis by simp - qed - ultimately show "False" by auto - qed - qed -qed - -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 - -(* Wrong: - lemma \thread \ set (wq_fun cs1 s); thread \ set (wq_fun cs2 s)\ \ cs1 = cs2" -*) - -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 lose 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: - 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: - fixes x y z r - 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: - fixes r x y z - 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: - fixes r x y z - 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: -fixes th::thread -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: - fixes th thread cs rest - 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: - fixes th cs s - 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: - fixes th cs s - 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: - fixes s thread cs - 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 - -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 - -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 - -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) - -end diff -r b620a2a0806a -r b4bcd1edbb6d PrioG.thy~ --- a/PrioG.thy~ Wed Jan 06 20:46:14 2016 +0800 +++ b/PrioG.thy~ Wed Jan 06 16:34:26 2016 +0000 @@ -1,5 +1,5 @@ theory PrioG -imports PrioGDef +imports PrioGDef RTree begin locale valid_trace = @@ -3023,4 +3023,606 @@ 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) + +text {* @{text "the_preced"} is also the same as @{text "preced"}, the only + difference is the order of arguemts. *} +definition "the_preced s th = preced th s" + +lemma inj_the_preced: + "inj_on (the_preced s) (threads s)" + by (metis inj_onI preced_unique the_preced_def) + +text {* @{term "the_thread"} extracts thread out of RAG node. *} +fun the_thread :: "node \ thread" where + "the_thread (Th th) = th" + +text {* The following @{text "wRAG"} is the waiting sub-graph of @{text "RAG"}. *} +definition "wRAG (s::state) = {(Th th, Cs cs) | th cs. waiting s th cs}" + +text {* The following @{text "hRAG"} is the holding sub-graph of @{text "RAG"}. *} +definition "hRAG (s::state) = {(Cs cs, Th th) | th cs. holding s th cs}" + +text {* The following lemma splits @{term "RAG"} graph into the above two sub-graphs. *} +lemma RAG_split: "RAG s = (wRAG s \ hRAG s)" + by (unfold s_RAG_abv wRAG_def hRAG_def s_waiting_abv + s_holding_abv cs_RAG_def, auto) + +text {* + The following @{text "tRAG"} is the thread-graph derived from @{term "RAG"}. + It characterizes the dependency between threads when calculating current + precedences. It is defined as the composition of the above two sub-graphs, + names @{term "wRAG"} and @{term "hRAG"}. + *} +definition "tRAG s = wRAG s O hRAG s" + +(* ccc *) + +definition "cp_gen s x = + Max ((the_preced s \ the_thread) ` subtree (tRAG s) x)" + +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 +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 + +end diff -r b620a2a0806a -r b4bcd1edbb6d PrioGDef.thy --- a/PrioGDef.thy Wed Jan 06 20:46:14 2016 +0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,614 +0,0 @@ -chapter {* Definitions *} -(*<*) -theory PrioGDef -imports Precedence_ord Moment -begin -(*>*) - -text {* - In this section, the formal model of Priority Inheritance Protocol (PIP) is presented. - The model is based on Paulson's inductive protocol verification method, where - the state of the system is modelled as a list of events happened so far with the latest - event put at the head. -*} - -text {* - To define events, the identifiers of {\em threads}, - {\em priority} and {\em critical resources } (abbreviated as @{text "cs"}) - need to be represented. All three are represetned using standard - Isabelle/HOL type @{typ "nat"}: -*} - -type_synonym thread = nat -- {* Type for thread identifiers. *} -type_synonym priority = nat -- {* Type for priorities. *} -type_synonym cs = nat -- {* Type for critical sections (or critical resources). *} - -text {* - \noindent - The abstraction of Priority Inheritance Protocol (PIP) is set at the system call level. - Every system call is represented as an event. The format of events is defined - defined as follows: - *} - -datatype event = - Create thread priority | -- {* Thread @{text "thread"} is created with priority @{text "priority"}. *} - Exit thread | -- {* Thread @{text "thread"} finishing its execution. *} - P thread cs | -- {* Thread @{text "thread"} requesting critical resource @{text "cs"}. *} - V thread cs | -- {* Thread @{text "thread"} releasing critical resource @{text "cs"}. *} - Set thread priority -- {* Thread @{text "thread"} resets its priority to @{text "priority"}. *} - - -text {* - As mentioned earlier, in Paulson's inductive method, the states of system are represented as lists of events, - which is defined by the following type @{text "state"}: - *} -type_synonym state = "event list" - - -text {* -\noindent - Resource Allocation Graph (RAG for short) is used extensively in our formal analysis. - The following type @{text "node"} is used to represent nodes in RAG. - *} -datatype node = - Th "thread" | -- {* Node for thread. *} - Cs "cs" -- {* Node for critical resource. *} - -text {* - \noindent - The following function - @{text "threads"} is used to calculate the set of live threads (@{text "threads s"}) - in state @{text "s"}. - *} -fun threads :: "state \ thread set" - where - -- {* At the start of the system, the set of threads is empty: *} - "threads [] = {}" | - -- {* New thread is added to the @{text "threads"}: *} - "threads (Create thread prio#s) = {thread} \ threads s" | - -- {* Finished thread is removed: *} - "threads (Exit thread # s) = (threads s) - {thread}" | - -- {* Other kind of events does not affect the value of @{text "threads"}: *} - "threads (e#s) = threads s" - -text {* - \noindent - The function @{text "threads"} defined above is one of - the so called {\em observation function}s which forms - the very basis of Paulson's inductive protocol verification method. - Each observation function {\em observes} one particular aspect (or attribute) - of the system. For example, the attribute observed by @{text "threads s"} - is the set of threads living in state @{text "s"}. - The protocol being modelled - The decision made the protocol being modelled is based on the {\em observation}s - returned by {\em observation function}s. Since {\observation function}s forms - the very basis on which Paulson's inductive method is based, there will be - a lot of such observation functions introduced in the following. In fact, any function - which takes event list as argument is a {\em observation function}. - *} - -text {* \noindent - Observation @{text "priority th s"} is - the {\em original priority} of thread @{text "th"} in state @{text "s"}. - The {\em original priority} is the priority - assigned to a thread when it is created or when it is reset by system call - (represented by event @{text "Set thread priority"}). -*} - -fun priority :: "thread \ state \ priority" - where - -- {* @{text "0"} is assigned to threads which have never been created: *} - "priority thread [] = 0" | - "priority thread (Create thread' prio#s) = - (if thread' = thread then prio else priority thread s)" | - "priority thread (Set thread' prio#s) = - (if thread' = thread then prio else priority thread s)" | - "priority thread (e#s) = priority thread s" - -text {* - \noindent - Observation @{text "last_set th s"} is the last time when the priority of thread @{text "th"} is set, - observed from state @{text "s"}. - The time in the system is measured by the number of events happened so far since the very beginning. -*} -fun last_set :: "thread \ state \ nat" - where - "last_set thread [] = 0" | - "last_set thread ((Create thread' prio)#s) = - (if (thread = thread') then length s else last_set thread s)" | - "last_set thread ((Set thread' prio)#s) = - (if (thread = thread') then length s else last_set thread s)" | - "last_set thread (_#s) = last_set thread s" - -text {* - \noindent - The {\em precedence} is a notion derived from {\em priority}, where the {\em precedence} of - a thread is the combination of its {\em original priority} and {\em time} the priority is set. - The intention is to discriminate threads with the same priority by giving threads whose priority - is assigned earlier higher precedences, becasue such threads are more urgent to finish. - This explains the following definition: - *} -definition preced :: "thread \ state \ precedence" - where "preced thread s \ Prc (priority thread s) (last_set thread s)" - - -text {* - \noindent - A number of important notions in PIP are represented as the following functions, - defined in terms of the waiting queues of the system, where the waiting queues - , as a whole, is represented by the @{text "wq"} argument of every notion function. - The @{text "wq"} argument is itself a functions which maps every critical resource - @{text "cs"} to the list of threads which are holding or waiting for it. - The thread at the head of this list is designated as the thread which is current - holding the resrouce, which is slightly different from tradition where - all threads in the waiting queue are considered as waiting for the resource. - *} - -consts - holding :: "'b \ thread \ cs \ bool" - waiting :: "'b \ thread \ cs \ bool" - RAG :: "'b \ (node \ node) set" - dependants :: "'b \ thread \ thread set" - -defs (overloaded) - -- {* - \begin{minipage}{0.9\textwidth} - This meaning of @{text "wq"} is reflected in the following definition of @{text "holding wq th cs"}, - where @{text "holding wq th cs"} means thread @{text "th"} is holding the critical - resource @{text "cs"}. This decision is based on @{text "wq"}. - \end{minipage} - *} - - cs_holding_def: - "holding wq thread cs \ (thread \ set (wq cs) \ thread = hd (wq cs))" - -- {* - \begin{minipage}{0.9\textwidth} - In accordance with the definition of @{text "holding wq th cs"}, - a thread @{text "th"} is considered waiting for @{text "cs"} if - it is in the {\em waiting queue} of critical resource @{text "cs"}, but not at the head. - This is reflected in the definition of @{text "waiting wq th cs"} as follows: - \end{minipage} - *} - cs_waiting_def: - "waiting wq thread cs \ (thread \ set (wq cs) \ thread \ hd (wq cs))" - -- {* - \begin{minipage}{0.9\textwidth} - @{text "RAG wq"} generates RAG (a binary relations on @{text "node"}) - out of waiting queues of the system (represented by the @{text "wq"} argument): - \end{minipage} - *} - cs_RAG_def: - "RAG (wq::cs \ thread list) \ - {(Th th, Cs cs) | th cs. waiting wq th cs} \ {(Cs cs, Th th) | cs th. holding wq th cs}" - -- {* - \begin{minipage}{0.9\textwidth} - The following @{text "dependants wq th"} represents the set of threads which are RAGing on - thread @{text "th"} in Resource Allocation Graph @{text "RAG wq"}. - Here, "RAGing" means waiting directly or indirectly on the critical resource. - \end{minipage} - *} - cs_dependants_def: - "dependants (wq::cs \ thread list) th \ {th' . (Th th', Th th) \ (RAG wq)^+}" - - -text {* \noindent - The following - @{text "cpreced s th"} gives the {\em current precedence} of thread @{text "th"} under - state @{text "s"}. The definition of @{text "cpreced"} reflects the basic idea of - Priority Inheritance that the {\em current precedence} of a thread is the precedence - inherited from the maximum of all its dependants, i.e. the threads which are waiting - directly or indirectly waiting for some resources from it. If no such thread exits, - @{text "th"}'s {\em current precedence} equals its original precedence, i.e. - @{text "preced th s"}. - *} - -definition cpreced :: "(cs \ thread list) \ state \ thread \ precedence" - where "cpreced wq s = (\th. Max ((\th'. preced th' s) ` ({th} \ dependants wq th)))" - -text {* - Notice that the current precedence (@{text "cpreced"}) of one thread @{text "th"} can be boosted - (becoming larger than its own precedence) by those threads in - the @{text "dependants wq th"}-set. If one thread get boosted, we say - it inherits the priority (or, more precisely, the precedence) of - its dependants. This is how the word "Inheritance" in - Priority Inheritance Protocol comes. -*} - -(*<*) -lemma - cpreced_def2: - "cpreced wq s th \ Max ({preced th s} \ {preced th' s | th'. th' \ dependants wq th})" - unfolding cpreced_def image_def - apply(rule eq_reflection) - apply(rule_tac f="Max" in arg_cong) - by (auto) -(*>*) - - -text {* \noindent - Assuming @{text "qs"} be the waiting queue of a critical resource, - the following abbreviation "release qs" is the waiting queue after the thread - holding the resource (which is thread at the head of @{text "qs"}) released - the resource: -*} -abbreviation - "release qs \ case qs of - [] => [] - | (_#qs') => (SOME q. distinct q \ set q = set qs')" -text {* \noindent - It can be seen from the definition that the thread at the head of @{text "qs"} is removed - from the return value, and the value @{term "q"} is an reordering of @{text "qs'"}, the - tail of @{text "qs"}. Through this reordering, one of the waiting threads (those in @{text "qs'"} } - is chosen nondeterministically to be the head of the new queue @{text "q"}. - Therefore, this thread is the one who takes over the resource. This is a little better different - from common sense that the thread who comes the earliest should take over. - The intention of this definition is to show that the choice of which thread to take over the - release resource does not affect the correctness of the PIP protocol. -*} - -text {* - The data structure used by the operating system for scheduling is referred to as - {\em schedule state}. It is represented as a record consisting of - a function assigning waiting queue to resources - (to be used as the @{text "wq"} argument in @{text "holding"}, @{text "waiting"} - and @{text "RAG"}, etc) and a function assigning precedence to threads: - *} - -record schedule_state = - wq_fun :: "cs \ thread list" -- {* The function assigning waiting queue. *} - cprec_fun :: "thread \ precedence" -- {* The function assigning precedence. *} - -text {* \noindent - The following two abbreviations (@{text "all_unlocked"} and @{text "initial_cprec"}) - are used to set the initial values of the @{text "wq_fun"} @{text "cprec_fun"} fields - respectively of the @{text "schedule_state"} record by the following function @{text "sch"}, - which is used to calculate the system's {\em schedule state}. - - Since there is no thread at the very beginning to make request, all critical resources - are free (or unlocked). This status is represented by the abbreviation - @{text "all_unlocked"}. - *} -abbreviation - "all_unlocked \ \_::cs. ([]::thread list)" - - -text {* \noindent - The initial current precedence for a thread can be anything, because there is no thread then. - We simply assume every thread has precedence @{text "Prc 0 0"}. - *} - -abbreviation - "initial_cprec \ \_::thread. Prc 0 0" - - -text {* \noindent - The following function @{text "schs"} is used to calculate the system's schedule state @{text "schs s"} - out of the current system state @{text "s"}. It is the central function to model Priority Inheritance: - *} -fun schs :: "state \ schedule_state" - where - -- {* - \begin{minipage}{0.9\textwidth} - Setting the initial value of the @{text "schedule_state"} record (see the explanations above). - \end{minipage} - *} - "schs [] = (| wq_fun = all_unlocked, cprec_fun = initial_cprec |)" | - - -- {* - \begin{minipage}{0.9\textwidth} - \begin{enumerate} - \item @{text "ps"} is the schedule state of last moment. - \item @{text "pwq"} is the waiting queue function of last moment. - \item @{text "pcp"} is the precedence function of last moment (NOT USED). - \item @{text "nwq"} is the new waiting queue function. It is calculated using a @{text "case"} statement: - \begin{enumerate} - \item If the happening event is @{text "P thread cs"}, @{text "thread"} is added to - the end of @{text "cs"}'s waiting queue. - \item If the happening event is @{text "V thread cs"} and @{text "s"} is a legal state, - @{text "th'"} must equal to @{text "thread"}, - because @{text "thread"} is the one currently holding @{text "cs"}. - The case @{text "[] \ []"} may never be executed in a legal state. - the @{text "(SOME q. distinct q \ set q = set qs)"} is used to choose arbitrarily one - thread in waiting to take over the released resource @{text "cs"}. In our representation, - this amounts to rearrange elements in waiting queue, so that one of them is put at the head. - \item For other happening event, the schedule state just does not change. - \end{enumerate} - \item @{text "ncp"} is new precedence function, it is calculated from the newly updated waiting queue - function. The RAGency of precedence function on waiting queue function is the reason to - put them in the same record so that they can evolve together. - \end{enumerate} - - - The calculation of @{text "cprec_fun"} depends on the value of @{text "wq_fun"}. - Therefore, in the following cases, @{text "wq_fun"} is always calculated first, in - the name of @{text "wq"} (if @{text "wq_fun"} is not changed - by the happening event) or @{text "new_wq"} (if the value of @{text "wq_fun"} is changed). - \end{minipage} - *} - "schs (Create th prio # s) = - (let wq = wq_fun (schs s) in - (|wq_fun = wq, cprec_fun = cpreced wq (Create th prio # s)|))" -| "schs (Exit th # s) = - (let wq = wq_fun (schs s) in - (|wq_fun = wq, cprec_fun = cpreced wq (Exit th # s)|))" -| "schs (Set th prio # s) = - (let wq = wq_fun (schs s) in - (|wq_fun = wq, cprec_fun = cpreced wq (Set th prio # s)|))" - -- {* - \begin{minipage}{0.9\textwidth} - Different from the forth coming cases, the @{text "wq_fun"} field of the schedule state - is changed. So, the new value is calculated first, in the name of @{text "new_wq"}. - \end{minipage} - *} -| "schs (P th cs # s) = - (let wq = wq_fun (schs s) in - let new_wq = wq(cs := (wq cs @ [th])) in - (|wq_fun = new_wq, cprec_fun = cpreced new_wq (P th cs # s)|))" -| "schs (V th cs # s) = - (let wq = wq_fun (schs s) in - let new_wq = wq(cs := release (wq cs)) in - (|wq_fun = new_wq, cprec_fun = cpreced new_wq (V th cs # s)|))" - -lemma cpreced_initial: - "cpreced (\ cs. []) [] = (\_. (Prc 0 0))" -apply(simp add: cpreced_def) -apply(simp add: cs_dependants_def cs_RAG_def cs_waiting_def cs_holding_def) -apply(simp add: preced_def) -done - -lemma sch_old_def: - "schs (e#s) = (let ps = schs s in - let pwq = wq_fun ps in - let nwq = case e of - P th cs \ pwq(cs:=(pwq cs @ [th])) | - V th cs \ let nq = case (pwq cs) of - [] \ [] | - (_#qs) \ (SOME q. distinct q \ set q = set qs) - in pwq(cs:=nq) | - _ \ pwq - in let ncp = cpreced nwq (e#s) in - \wq_fun = nwq, cprec_fun = ncp\ - )" -apply(cases e) -apply(simp_all) -done - - -text {* - \noindent - The following @{text "wq"} is a shorthand for @{text "wq_fun"}. - *} -definition wq :: "state \ cs \ thread list" - where "wq s = wq_fun (schs s)" - -text {* \noindent - The following @{text "cp"} is a shorthand for @{text "cprec_fun"}. - *} -definition cp :: "state \ thread \ precedence" - where "cp s \ cprec_fun (schs s)" - -text {* \noindent - Functions @{text "holding"}, @{text "waiting"}, @{text "RAG"} and - @{text "dependants"} still have the - same meaning, but redefined so that they no longer RAG on the - fictitious {\em waiting queue function} - @{text "wq"}, but on system state @{text "s"}. - *} -defs (overloaded) - s_holding_abv: - "holding (s::state) \ holding (wq_fun (schs s))" - s_waiting_abv: - "waiting (s::state) \ waiting (wq_fun (schs s))" - s_RAG_abv: - "RAG (s::state) \ RAG (wq_fun (schs s))" - s_dependants_abv: - "dependants (s::state) \ dependants (wq_fun (schs s))" - - -text {* - The following lemma can be proved easily, and the meaning is obvious. - *} -lemma - s_holding_def: - "holding (s::state) th cs \ (th \ set (wq_fun (schs s) cs) \ th = hd (wq_fun (schs s) cs))" - by (auto simp:s_holding_abv wq_def cs_holding_def) - -lemma s_waiting_def: - "waiting (s::state) th cs \ (th \ set (wq_fun (schs s) cs) \ th \ hd (wq_fun (schs s) cs))" - by (auto simp:s_waiting_abv wq_def cs_waiting_def) - -lemma s_RAG_def: - "RAG (s::state) = - {(Th th, Cs cs) | th cs. waiting (wq s) th cs} \ {(Cs cs, Th th) | cs th. holding (wq s) th cs}" - by (auto simp:s_RAG_abv wq_def cs_RAG_def) - -lemma - s_dependants_def: - "dependants (s::state) th \ {th' . (Th th', Th th) \ (RAG (wq s))^+}" - by (auto simp:s_dependants_abv wq_def cs_dependants_def) - -text {* - The following function @{text "readys"} calculates the set of ready threads. A thread is {\em ready} - for running if it is a live thread and it is not waiting for any critical resource. - *} -definition readys :: "state \ thread set" - where "readys s \ {th . th \ threads s \ (\ cs. \ waiting s th cs)}" - -text {* \noindent - The following function @{text "runing"} calculates the set of running thread, which is the ready - thread with the highest precedence. - *} -definition runing :: "state \ thread set" - where "runing s \ {th . th \ readys s \ cp s th = Max ((cp s) ` (readys s))}" - -text {* \noindent - Notice that the definition of @{text "running"} reflects the preemptive scheduling strategy, - because, if the @{text "running"}-thread (the one in @{text "runing"} set) - lowered its precedence by resetting its own priority to a lower - one, it will lose its status of being the max in @{text "ready"}-set and be superseded. -*} - -text {* \noindent - The following function @{text "holdents s th"} returns the set of resources held by thread - @{text "th"} in state @{text "s"}. - *} -definition holdents :: "state \ thread \ cs set" - where "holdents s th \ {cs . holding s th cs}" - -lemma holdents_test: - "holdents s th = {cs . (Cs cs, Th th) \ RAG s}" -unfolding holdents_def -unfolding s_RAG_def -unfolding s_holding_abv -unfolding wq_def -by (simp) - -text {* \noindent - Observation @{text "cntCS s th"} returns the number of resources held by thread @{text "th"} in - state @{text "s"}: - *} -definition cntCS :: "state \ thread \ nat" - where "cntCS s th = card (holdents s th)" - -text {* \noindent - According to the convention of Paulson's inductive method, - the decision made by a protocol that event @{text "e"} is eligible to happen next under state @{text "s"} - is expressed as @{text "step s e"}. The predicate @{text "step"} is inductively defined as - follows (notice how the decision is based on the {\em observation function}s - defined above, and also notice how a complicated protocol is modeled by a few simple - observations, and how such a kind of simplicity gives rise to improved trust on - faithfulness): - *} -inductive step :: "state \ event \ bool" - where - -- {* - A thread can be created if it is not a live thread: - *} - thread_create: "\thread \ threads s\ \ step s (Create thread prio)" | - -- {* - A thread can exit if it no longer hold any resource: - *} - thread_exit: "\thread \ runing s; holdents s thread = {}\ \ step s (Exit thread)" | - -- {* - \begin{minipage}{0.9\textwidth} - A thread can request for an critical resource @{text "cs"}, if it is running and - the request does not form a loop in the current RAG. The latter condition - is set up to avoid deadlock. The condition also reflects our assumption all threads are - carefully programmed so that deadlock can not happen: - \end{minipage} - *} - thread_P: "\thread \ runing s; (Cs cs, Th thread) \ (RAG s)^+\ \ - step s (P thread cs)" | - -- {* - \begin{minipage}{0.9\textwidth} - A thread can release a critical resource @{text "cs"} - if it is running and holding that resource: - \end{minipage} - *} - thread_V: "\thread \ runing s; holding s thread cs\ \ step s (V thread cs)" | - -- {* - \begin{minipage}{0.9\textwidth} - A thread can adjust its own priority as long as it is current running. - With the resetting of one thread's priority, its precedence may change. - If this change lowered the precedence, according to the definition of @{text "running"} - function, - \end{minipage} - *} - thread_set: "\thread \ runing s\ \ step s (Set thread prio)" - -text {* - In Paulson's inductive method, every protocol is defined by such a @{text "step"} - predicate. For instance, the predicate @{text "step"} given above - defines the PIP protocol. So, it can also be called "PIP". -*} - -abbreviation - "PIP \ step" - - -text {* \noindent - For any protocol defined by a @{text "step"} predicate, - the fact that @{text "s"} is a legal state in - the protocol is expressed as: @{text "vt step s"}, where - the predicate @{text "vt"} can be defined as the following: - *} -inductive vt :: "state \ bool" - where - -- {* Empty list @{text "[]"} is a legal state in any protocol:*} - vt_nil[intro]: "vt []" | - -- {* - \begin{minipage}{0.9\textwidth} - If @{text "s"} a legal state of the protocol defined by predicate @{text "step"}, - and event @{text "e"} is allowed to happen under state @{text "s"} by the protocol - predicate @{text "step"}, then @{text "e#s"} is a new legal state rendered by the - happening of @{text "e"}: - \end{minipage} - *} - vt_cons[intro]: "\vt s; step s e\ \ vt (e#s)" - -text {* \noindent - It is easy to see that the definition of @{text "vt"} is generic. It can be applied to - any specific protocol specified by a @{text "step"}-predicate to get the set of - legal states of that particular protocol. - *} - -text {* - The following are two very basic properties of @{text "vt"}. -*} - -lemma step_back_vt: "vt (e#s) \ vt s" - by(ind_cases "vt (e#s)", simp) - -lemma step_back_step: "vt (e#s) \ step s e" - by(ind_cases "vt (e#s)", simp) - -text {* \noindent - The following two auxiliary functions @{text "the_cs"} and @{text "the_th"} are used to extract - critical resource and thread respectively out of RAG nodes. - *} -fun the_cs :: "node \ cs" - where "the_cs (Cs cs) = cs" - -fun the_th :: "node \ thread" - where "the_th (Th th) = th" - -text {* \noindent - The following predicate @{text "next_th"} describe the next thread to - take over when a critical resource is released. In @{text "next_th s th cs t"}, - @{text "th"} is the thread to release, @{text "t"} is the one to take over. - Notice how this definition is backed up by the @{text "release"} function and its use - in the @{text "V"}-branch of @{text "schs"} function. This @{text "next_th"} function - is not needed for the execution of PIP. It is introduced as an auxiliary function - to state lemmas. The correctness of this definition will be confirmed by - lemmas @{text "step_v_hold_inv"}, @{text " step_v_wait_inv"}, - @{text "step_v_get_hold"} and @{text "step_v_not_wait"}. - *} -definition next_th:: "state \ thread \ cs \ thread \ bool" - where "next_th s th cs t = (\ rest. wq s cs = th#rest \ rest \ [] \ - t = hd (SOME q. distinct q \ set q = set rest))" - -text {* \noindent - The aux function @{text "count Q l"} is used to count the occurrence of situation @{text "Q"} - in list @{text "l"}: - *} -definition count :: "('a \ bool) \ 'a list \ nat" - where "count Q l = length (filter Q l)" - -text {* \noindent - The following observation @{text "cntP s"} returns the number of operation @{text "P"} happened - before reaching state @{text "s"}. - *} -definition cntP :: "state \ thread \ nat" - where "cntP s th = count (\ e. \ cs. e = P th cs) s" - -text {* \noindent - The following observation @{text "cntV s"} returns the number of operation @{text "V"} happened - before reaching state @{text "s"}. - *} -definition cntV :: "state \ thread \ nat" - where "cntV s th = count (\ e. \ cs. e = V th cs) s" -(*<*) - -end -(*>*) - diff -r b620a2a0806a -r b4bcd1edbb6d PrioGDef.thy~ --- a/PrioGDef.thy~ Wed Jan 06 20:46:14 2016 +0800 +++ b/PrioGDef.thy~ Wed Jan 06 16:34:26 2016 +0000 @@ -1,4 +1,4 @@ - {* Definitions *} +chapter {* Definitions *} (*<*) theory PrioGDef imports Precedence_ord Moment @@ -387,6 +387,8 @@ definition cp :: "state \ thread \ precedence" where "cp s \ cprec_fun (schs s)" +definition "cp_gen s x = Max ((the_preced s \ the_thread) ` subtree (tRAG s) x)" + text {* \noindent Functions @{text "holding"}, @{text "waiting"}, @{text "RAG"} and @{text "dependants"} still have the @@ -585,6 +587,7 @@ *} definition next_th:: "state \ thread \ cs \ thread \ bool" where "next_th s th cs t = (\ rest. wq s cs = th#rest \ rest \ [] \ + t = hd (SOME q. distinct q \ set q = set rest))" text {* \noindent The aux function @{text "count Q l"} is used to count the occurrence of situation @{text "Q"} diff -r b620a2a0806a -r b4bcd1edbb6d README --- a/README Wed Jan 06 20:46:14 2016 +0800 +++ b/README Wed Jan 06 16:34:26 2016 +0000 @@ -4,10 +4,10 @@ Max.thy Some generic facts about Max. Precedence_ord.thy A theory of precedences. Moment.thy The notion of moment. - PrioGDef.thy The formal definition of the PIP-model. - PrioG.thy Basic properties of the PIP-model. - ExtGG.thy The correctness proof of the PIP-model. - CpsG.thy Properties interesting for an implementation. + PIPDefs.thy The formal definition of the PIP-model. + PIPBasics.thy Basic properties of the PIP-model. + Correctness.thy The correctness proof of the PIP-model. + Implementation.thy Properties interesting for an implementation. The repository can be checked using Isabelle 2013-2. diff -r b620a2a0806a -r b4bcd1edbb6d ROOT --- a/ROOT Wed Jan 06 20:46:14 2016 +0800 +++ b/ROOT Wed Jan 06 16:34:26 2016 +0000 @@ -1,7 +1,7 @@ session "PIP" = HOL + theories [document = false, quick_and_dirty] - "CpsG" - "ExtGG" + "Implementation" + "Correctness" "Test" session "Slides2" in "Slides" = PIP + diff -r b620a2a0806a -r b4bcd1edbb6d RTree.thy --- a/RTree.thy Wed Jan 06 20:46:14 2016 +0800 +++ b/RTree.thy Wed Jan 06 16:34:26 2016 +0000 @@ -154,6 +154,7 @@ lemma index_minimize: assumes "P (i::nat)" obtains j where "P j" and "\ k < j. \ P k" +using assms proof - have "\ j. P j \ (\ k < j. \ P k)" using assms diff -r b620a2a0806a -r b4bcd1edbb6d journal.pdf Binary file journal.pdf has changed