Test.thy
changeset 38 c89013dca1aa
parent 37 c820ac0f3088
child 39 7ea6b019ce24
equal deleted inserted replaced
37:c820ac0f3088 38:c89013dca1aa
     2 imports Precedence_ord
     2 imports Precedence_ord
     3 begin
     3 begin
     4 
     4 
     5 type_synonym thread = nat -- {* Type for thread identifiers. *}
     5 type_synonym thread = nat -- {* Type for thread identifiers. *}
     6 type_synonym priority = nat  -- {* Type for priorities. *}
     6 type_synonym priority = nat  -- {* Type for priorities. *}
     7 type_synonym cs = nat -- {* Type for critical sections (or critical resources). *}
     7 type_synonym cs = nat -- {* Type for critical sections (or resources). *}
     8 
     8 
     9 datatype event = 
     9 datatype event = 
    10   Create thread priority 
    10   Create thread priority 
    11 | Exit thread 
    11 | Exit thread 
    12 | P thread cs 
    12 | P thread cs 
    96           (|wq_fun = new_wq, cprec_fun = cpreced new_wq (V th cs # s)|))"
    96           (|wq_fun = new_wq, cprec_fun = cpreced new_wq (V th cs # s)|))"
    97 
    97 
    98 definition wq :: "state \<Rightarrow> cs \<Rightarrow> thread list" 
    98 definition wq :: "state \<Rightarrow> cs \<Rightarrow> thread list" 
    99   where "wq s = wq_fun (schs s)"
    99   where "wq s = wq_fun (schs s)"
   100 
   100 
   101 definition cp :: "state \<Rightarrow> thread \<Rightarrow> precedence"
   101 definition cpreced2 :: "state \<Rightarrow> thread \<Rightarrow> precedence"
   102   where "cp s \<equiv> cprec_fun (schs s)"
   102   where "cpreced2 s \<equiv> cprec_fun (schs s)"
       
   103 
       
   104 abbreviation
       
   105   "cpreceds2 s ths \<equiv> {cpreced2 s th | th. th \<in> ths}"
   103 
   106 
   104 definition
   107 definition
   105   "holds2 s \<equiv> holds (wq_fun (schs s))"
   108   "holds2 s \<equiv> holds (wq_fun (schs s))"
   106 
   109 
   107 definition
   110 definition
   111   "RAG2 s \<equiv> RAG (wq_fun (schs s))"
   114   "RAG2 s \<equiv> RAG (wq_fun (schs s))"
   112   
   115   
   113 definition
   116 definition
   114   "dependants2 s \<equiv> dependants (wq_fun (schs s))"
   117   "dependants2 s \<equiv> dependants (wq_fun (schs s))"
   115 
   118 
       
   119 (* ready -> is a thread that is not waiting for any resource *) 
   116 definition readys :: "state \<Rightarrow> thread set"
   120 definition readys :: "state \<Rightarrow> thread set"
   117   where "readys s \<equiv> {th . th \<in> threads s \<and> (\<forall> cs. \<not> waits2 s th cs)}"
   121   where "readys s \<equiv> {th . th \<in> threads s \<and> (\<forall> cs. \<not> waits2 s th cs)}"
   118 
   122 
   119 definition runing :: "state \<Rightarrow> thread set"
   123 definition runing :: "state \<Rightarrow> thread set"
   120   where "runing s \<equiv> {th . th \<in> readys s \<and> cp s th = Max ((cp s) ` (readys s))}"
   124   where "runing s \<equiv> {th . th \<in> readys s \<and> cpreced2 s th = Max (cpreceds2 s (readys s))}"
   121 
   125 
       
   126 (* all resources a thread hols in a state *)
   122 definition holding :: "state \<Rightarrow> thread \<Rightarrow> cs set"
   127 definition holding :: "state \<Rightarrow> thread \<Rightarrow> cs set"
   123   where "holding s th \<equiv> {cs . holds2 s th cs}"
   128   where "holding s th \<equiv> {cs . holds2 s th cs}"
   124 
   129 
   125 abbreviation
   130 abbreviation
   126   "next_to_run ths \<equiv> hd (SOME q::thread list. distinct q \<and> set q = set ths)"
   131   "next_to_run ths \<equiv> hd (SOME q::thread list. distinct q \<and> set q = set ths)"
   127 
   132 
       
   133 lemma exists_distinct:
       
   134   obtains ys where "distinct ys" "set ys = set xs"
       
   135 by (metis List.finite_set finite_distinct_list)
       
   136 
       
   137 lemma next_to_run_set [simp]:
       
   138   "wts \<noteq> [] \<Longrightarrow> next_to_run wts \<in> set wts"
       
   139 apply(rule exists_distinct[of wts])
       
   140 by (metis (mono_tags, lifting) hd_in_set set_empty some_eq_ex)
       
   141 
   128 lemma holding_RAG: 
   142 lemma holding_RAG: 
   129   "holding s th = {cs . (Cs cs, Th th) \<in> RAG2 s}"
   143   "holding s th = {cs . (Cs cs, Th th) \<in> RAG2 s}"
   130 unfolding holding_def
   144 unfolding holding_def RAG2_def RAG_def
   131 unfolding holds2_def
   145 unfolding holds2_def holds_def waits_def
   132 unfolding RAG2_def RAG_def
       
   133 unfolding holds_def waits_def
       
   134 by auto
   146 by auto
   135 
   147 
   136 inductive step :: "state \<Rightarrow> event \<Rightarrow> bool"
   148 inductive step :: "state \<Rightarrow> event \<Rightarrow> bool"
   137   where
   149   where
   138   step_Create: "\<lbrakk>th \<notin> threads s\<rbrakk> \<Longrightarrow> step s (Create th prio)" 
   150   step_Create: "\<lbrakk>th \<notin> threads s\<rbrakk> \<Longrightarrow> step s (Create th prio)" 
   139 | step_Exit: "\<lbrakk>th \<in> runing s; holding s th = {}\<rbrakk> \<Longrightarrow> step s (Exit th)" 
   151 | step_Exit: "\<lbrakk>th \<in> runing s; holding s th = {}\<rbrakk> \<Longrightarrow> step s (Exit th)" 
   140 | step_P: "\<lbrakk>th \<in> runing s;  (Cs cs, Th th)  \<notin> (RAG2 s)^+\<rbrakk> \<Longrightarrow> step s (P th cs)" 
   152 | step_P: "\<lbrakk>th \<in> runing s;  (Cs cs, Th th)  \<notin> (RAG2 s)^+\<rbrakk> \<Longrightarrow> step s (P th cs)" 
   141 | step_V: "\<lbrakk>th \<in> runing s;  holds2 s th cs\<rbrakk> \<Longrightarrow> step s (V th cs)" 
   153 | step_V: "\<lbrakk>th \<in> runing s;  holds2 s th cs\<rbrakk> \<Longrightarrow> step s (V th cs)" 
   142 | step_Set: "\<lbrakk>th \<in> runing s\<rbrakk> \<Longrightarrow> step s (Set th prio)"
   154 | step_Set: "\<lbrakk>th \<in> runing s\<rbrakk> \<Longrightarrow> step s (Set th prio)"
   143 
   155 
       
   156 (* valid states *)
   144 inductive vt :: "state \<Rightarrow> bool"
   157 inductive vt :: "state \<Rightarrow> bool"
   145   where
   158   where
   146   vt_nil[intro]: "vt []" 
   159   vt_nil[intro]: "vt []" 
   147 | vt_cons[intro]: "\<lbrakk>vt s; step s e\<rbrakk> \<Longrightarrow> vt (e#s)"
   160 | vt_cons[intro]: "\<lbrakk>vt s; step s e\<rbrakk> \<Longrightarrow> vt (e#s)"
   148 
   161 
   158 
   171 
   159 lemma wq_distinct_step: 
   172 lemma wq_distinct_step: 
   160   assumes "step s e" "distinct (wq s cs)" 
   173   assumes "step s e" "distinct (wq s cs)" 
   161   shows "distinct (wq (e # s) cs)"
   174   shows "distinct (wq (e # s) cs)"
   162 using assms
   175 using assms
       
   176 unfolding wq_def
   163 apply(induct)
   177 apply(induct)
   164 apply(auto simp add: wq_def Let_def)
   178 apply(auto simp add: RAG2_def RAG_def Let_def)[1]
   165 apply(auto simp add: wq_def Let_def RAG2_def RAG_def holds_def runing_def waits2_def waits_def readys_def)[1]
   179 apply(auto simp add: wq_def Let_def RAG2_def RAG_def holds_def runing_def waits2_def waits_def readys_def)
   166 apply(auto split: list.split)
   180 apply(auto split: list.split)
   167 apply(rule someI2)
   181 apply(rule someI2)
   168 apply(auto)
   182 apply(auto)
   169 done
   183 done
   170 
   184 
   223 by (auto simp add: Let_def)
   237 by (auto simp add: Let_def)
   224 
   238 
   225 lemma RAG_v2:
   239 lemma RAG_v2:
   226 assumes vt:"vt s" "wq s cs = th # wts \<and> wts \<noteq> []"
   240 assumes vt:"vt s" "wq s cs = th # wts \<and> wts \<noteq> []"
   227 shows "RAG2 (V th cs # s) \<subseteq>
   241 shows "RAG2 (V th cs # s) \<subseteq>
   228   RAG2 s - {(Cs cs, Th th)} - {(Th (next_to_run wts), Cs cs)} \<union> {(Cs cs, Th (next_to_run wts))}"
   242   RAG2 s - {(Cs cs, Th th), (Th (next_to_run wts), Cs cs)} \<union> {(Cs cs, Th (next_to_run wts))}"
   229 unfolding RAG2_def RAG_def waits_def holds_def
   243 unfolding RAG2_def RAG_def waits_def holds_def
   230 using assms wq_distinct[OF vt(1), of"cs"]
   244 using assms wq_distinct[OF vt(1), of"cs"]
   231 by (auto simp add: Let_def wq_def)
   245 by (auto simp add: Let_def wq_def)
   232 
   246 
   233 lemma waiting_unique:
   247 lemma waiting_unique:
   234   assumes "vt s"
   248   assumes "vt s"
   235   and "waits2 s th cs1"
   249   and "waits2 s th cs1"
   236   and "waits2 s th cs2"
   250   and "waits2 s th cs2"
   237   shows "cs1 = cs2"
   251   shows "cs1 = cs2"
   238 sorry
   252 using assms
       
   253 apply(induct)
       
   254 apply(simp add: waits2_def waits_def)
       
   255 apply(erule step.cases)
       
   256 apply(auto simp add: Let_def waits2_def waits_def holds_def RAG2_def RAG_def 
       
   257  readys_def runing_def split: if_splits)
       
   258 apply (metis Nil_is_append_conv hd_append2 list.distinct(1) split_list)
       
   259 apply (metis Nil_is_append_conv hd_append2 list.distinct(1) split_list)
       
   260 apply (metis distinct.simps(2) distinct_length_2_or_more list.sel(1) wq_def wq_distinct)
       
   261 by (metis (full_types, hide_lams) distinct.simps(2) distinct_length_2_or_more list.sel(1) wq_def wq_distinct)
       
   262 
       
   263 
       
   264 lemma rtrancl_eq_trancl [simp]:
       
   265   assumes "x \<noteq> y"
       
   266   shows "(x, y) \<in> r\<^sup>* \<longleftrightarrow> (x, y) \<in> r\<^sup>+"
       
   267 using assms by (metis rtrancl_eq_or_trancl) 
   239 
   268 
   240 lemma acyclic_RAG_step: 
   269 lemma acyclic_RAG_step: 
   241   assumes vt: "vt s"
   270   assumes vt: "vt s"
   242   and     stp: "step s e"
   271   and     stp: "step s e"
   243   and     ac: "acyclic (RAG2 s)"
   272   and     ac: "acyclic (RAG2 s)"
   245 using stp vt ac
   274 using stp vt ac
   246 proof (induct)
   275 proof (induct)
   247   case (step_P th s cs)
   276   case (step_P th s cs)
   248   have ac: "acyclic (RAG2 s)" by fact
   277   have ac: "acyclic (RAG2 s)" by fact
   249   have ds: "(Cs cs, Th th) \<notin> (RAG2 s)\<^sup>+" by fact
   278   have ds: "(Cs cs, Th th) \<notin> (RAG2 s)\<^sup>+" by fact
   250   { assume a: "wq s cs = []" -- "case waiting queue is empty"
   279   { assume wq_empty: "wq s cs = []" -- "case waiting queue is empty"
   251     have "(Th th, Cs cs) \<notin> (RAG2 s)\<^sup>*"  
   280     then have "(Th th, Cs cs) \<notin> (RAG2 s)\<^sup>+"
   252     proof
   281     proof (rule_tac notI)
   253       assume "(Th th, Cs cs) \<in> (RAG2 s)\<^sup>*"
   282       assume "(Th th, Cs cs) \<in> (RAG2 s)\<^sup>+"
   254       then have "(Th th, Cs cs) \<in> (RAG2 s)\<^sup>+" by (simp add: rtrancl_eq_or_trancl)
       
   255       then obtain x where "(x, Cs cs) \<in> RAG2 s" using tranclD2 by metis 
   283       then obtain x where "(x, Cs cs) \<in> RAG2 s" using tranclD2 by metis 
   256       with a show False by (auto simp: RAG2_def RAG_def wq_def waits_def)
   284       with wq_empty show False by (auto simp: RAG2_def RAG_def wq_def waits_def)
   257     qed
   285     qed
   258     with acyclic_insert ac
   286     with ac have "acyclic (RAG2 s \<union> {(Cs cs, Th th)})" by simp
   259     have "acyclic (RAG2 s \<union> {(Cs cs, Th th)})" by simp
   287     then have "acyclic (RAG2 (P th cs # s))" using RAG_p1[OF wq_empty] 
   260     then have "acyclic (RAG2 (P th cs # s))" using RAG_p1[OF a] 
       
   261       by (rule acyclic_subset)
   288       by (rule acyclic_subset)
   262   }
   289   }
   263   moreover
   290   moreover
   264   { assume a: "wq s cs \<noteq> []" -- "case waiting queue is not empty"
   291   { assume wq_not_empty: "wq s cs \<noteq> []" -- "case waiting queue is not empty"
   265     from ds have "(Cs cs, Th th) \<notin> (RAG2 s)\<^sup>*" by (metis node.distinct(1) rtranclD)
   292     from ac ds
   266     with acyclic_insert ac 
   293     have "acyclic (RAG2 s \<union> {(Th th, Cs cs)})" by simp
   267     have "acyclic (RAG2 s \<union> {(Th th, Cs cs)})" by auto
   294     then have "acyclic (RAG2 (P th cs # s))" using RAG_p2[OF ds wq_not_empty] 
   268     then have "acyclic (RAG2 (P th cs # s))" using RAG_p2[OF ds a] 
       
   269       by (rule acyclic_subset)
   295       by (rule acyclic_subset)
   270   }
   296   }
   271   ultimately show "acyclic (RAG2 (P th cs # s))" by metis
   297   ultimately show "acyclic (RAG2 (P th cs # s))" by metis
   272 next    
   298 next    
   273   case (step_V th s cs) 
   299   case (step_V th s cs) -- "case for release of a lock" 
   274   have vt: "vt s" by fact
   300   have vt: "vt s" by fact
   275   have ac: "acyclic (RAG2 s)" by fact
   301   have ac: "acyclic (RAG2 s)" by fact
   276   have rn: "th \<in> runing s" by fact
       
   277   have hd: "holds2 s th cs" by fact
   302   have hd: "holds2 s th cs" by fact
   278   from hd have th_in: "th \<in> set (wq s cs)" and
   303   from vt have wq_distinct:"distinct (wq s cs)" by (rule wq_distinct)
   279                eq_hd: "th = hd (wq s cs)" unfolding holds2_def holds_def wq_def by auto
   304   from hd have "th \<in> set (wq s cs)" "th = hd (wq s cs)" unfolding holds2_def holds_def wq_def by auto
   280   then obtain wts where
   305   then obtain wts where eq_wq: "wq s cs = th # wts"  by (cases "wq s cs") (auto)
   281     eq_wq: "wq s cs = th # wts" by (cases "wq s cs") (auto)
   306   -- "case no thread present in the waiting queue to take over"
   282   { assume "wts = []" -- "case no thread present to take over"
   307   { assume "wts = []" 
   283     with eq_wq have "wq s cs = [th]" by simp
   308     with eq_wq have "wq s cs = [th]" by simp
   284     then have "RAG2 (V th cs # s) \<subseteq> RAG2 s - {(Cs cs, Th th)}" by (rule RAG_v1)
   309     then have "RAG2 (V th cs # s) \<subseteq> RAG2 s - {(Cs cs, Th th)}" by (rule RAG_v1)
   285     moreover have "acyclic (RAG2 s - {(Cs cs, Th th)})" using ac by (auto intro: acyclic_subset)
   310     moreover have "acyclic (RAG2 s - {(Cs cs, Th th)})" using ac by (auto intro: acyclic_subset)
   286     ultimately 
   311     ultimately 
   287     have "acyclic (RAG2 (V th cs # s))" by (auto intro: acyclic_subset)
   312     have "acyclic (RAG2 (V th cs # s))" by (auto intro: acyclic_subset)
   288   }
   313   }
   289   moreover
   314   moreover
       
   315   -- "at least one thread present to take over"
   290   { def nth \<equiv> "next_to_run wts"
   316   { def nth \<equiv> "next_to_run wts"
   291     assume aa: "wts \<noteq> []" -- "at least one thread present to take over"
   317     assume wq_not_empty: "wts \<noteq> []" 
   292     with vt eq_wq 
   318     have waits2_cs: "waits2 s nth cs" 
   293     have "RAG2 (V th cs # s) \<subseteq> RAG2 s - {(Cs cs, Th th)} - {(Th nth, Cs cs)} \<union> {(Cs cs, Th nth)}" 
   319       using eq_wq wq_not_empty wq_distinct
   294       unfolding nth_def by (rule_tac RAG_v2) (auto)
   320       unfolding nth_def waits2_def waits_def wq_def[symmetric] by auto
       
   321     have "RAG2 (V th cs # s) \<subseteq> RAG2 s - {(Cs cs, Th th), (Th nth, Cs cs)} \<union> {(Cs cs, Th nth)}" 
       
   322       unfolding nth_def using  vt wq_not_empty eq_wq by (rule_tac RAG_v2) (auto)
   295     moreover 
   323     moreover 
   296     have "acyclic (insert (Cs cs, Th nth) (RAG2 s - {(Cs cs, Th th)} - {(Th nth, Cs cs)}))" 
   324     have "acyclic (RAG2 s - {(Cs cs, Th th), (Th nth, Cs cs)} \<union> {(Cs cs, Th nth)})" 
   297     proof -
   325     proof -
   298       have "acyclic (RAG2 s - {(Cs cs, Th th)} - {(Th nth, Cs cs)})" using ac by (auto intro: acyclic_subset)
   326       have "acyclic (RAG2 s - {(Cs cs, Th th), (Th nth, Cs cs)})" using ac by (auto intro: acyclic_subset)
   299       moreover 
   327       moreover 
   300       have "(Th nth, Cs cs) \<notin> (RAG2 s - {(Cs cs, Th th)} - {(Th nth, Cs cs)})\<^sup>+" 
   328       have "(Th nth, Cs cs) \<notin> (RAG2 s - {(Cs cs, Th th), (Th nth, Cs cs)})\<^sup>+" 
   301         proof 
   329       proof (rule notI)
   302           assume "(Th nth, Cs cs) \<in> (RAG2 s - {(Cs cs, Th th)} - {(Th nth, Cs cs)})\<^sup>+"
   330         assume "(Th nth, Cs cs) \<in> (RAG2 s - {(Cs cs, Th th), (Th nth, Cs cs)})\<^sup>+"
   303           then obtain z where a: "(Th nth, z) \<in> (RAG2 s - {(Cs cs, Th th)} - {(Th nth, Cs cs)})"
   331         then obtain z where a: "(Th nth, z) \<in> (RAG2 s - {(Cs cs, Th th), (Th nth, Cs cs)})"
   304             by (metis converse_tranclE)
   332           by (metis converse_tranclE)
   305           then have "(Th nth, z) \<in> RAG2 s" by simp
   333         then have "(Th nth, z) \<in> RAG2 s" by simp
   306           then obtain cs' where b: "z = Cs cs'" "(Th nth, Cs cs') \<in> RAG2 s"
   334         then obtain cs' where b: "z = Cs cs'" "(Th nth, Cs cs') \<in> RAG2 s"
   307             unfolding RAG2_def RAG_def by auto
   335           unfolding RAG2_def RAG_def by auto
   308           then have "cs = cs'" 
   336         moreover 
   309             apply(rule_tac waiting_unique[OF vt])
   337         have "waits2 s nth cs'" 
   310             using eq_wq aa
   338           using b(2) unfolding RAG2_def RAG_def waits2_def by simp
   311             apply(auto simp add: nth_def RAG2_def RAG_def waits2_def waits_def wq_def)
   339         ultimately have "cs = cs'" 
   312             apply (metis (mono_tags, lifting) `\<And>thesis. (\<And>wts. wq s cs = th # wts \<Longrightarrow> thesis) \<Longrightarrow> thesis` distinct.simps(2) hd_in_set list.inject set_empty2 tfl_some vt wq_def wq_distinct)
   340           by (rule_tac waiting_unique[OF vt waits2_cs])
   313             by (metis (mono_tags, lifting) `\<And>thesis. (\<And>wts. wq s cs = th # wts \<Longrightarrow> thesis) \<Longrightarrow> thesis` distinct.simps(2) hd_in_set list.sel(1) set_empty2 tfl_some vt wq_def wq_distinct)            
   341         then show "False" using a b(1) by simp
   314          then show "False" using a b by simp
   342       qed
   315         qed
       
   316       then have "(Th nth, Cs cs) \<notin> (RAG2 s - {(Cs cs, Th th)} - {(Th nth, Cs cs)})\<^sup>*"
       
   317         by (metis node.distinct(1) rtranclD)
       
   318       ultimately 
   343       ultimately 
   319       show "acyclic (insert (Cs cs, Th nth) (RAG2 s - {(Cs cs, Th th)} - {(Th nth, Cs cs)}))" 
   344       show "acyclic (RAG2 s - {(Cs cs, Th th), (Th nth, Cs cs)} \<union> {(Cs cs, Th nth) })" by simp
   320         by (simp add: acyclic_insert)
       
   321     qed
   345     qed
   322     ultimately have "acyclic (RAG2 (V th cs # s))" by (auto intro: acyclic_subset)
   346     ultimately have "acyclic (RAG2 (V th cs # s))" 
       
   347       by (rule_tac acyclic_subset)
   323   }
   348   }
   324   ultimately show "acyclic (RAG2 (V th cs # s))" by metis
   349   ultimately show "acyclic (RAG2 (V th cs # s))" by metis
   325 qed (simp_all)
   350 qed (simp_all)
   326 
   351 
   327 
   352