PrioG.thy
changeset 63 b620a2a0806a
parent 62 031d2ae9c9b8
equal deleted inserted replaced
62:031d2ae9c9b8 63:b620a2a0806a
     1 theory PrioG
     1 theory PrioG
     2 imports PrioGDef 
     2 imports PrioGDef 
     3 begin
     3 begin
       
     4 
       
     5 locale valid_trace = 
       
     6   fixes s
       
     7   assumes vt : "vt s"
       
     8 
       
     9 locale valid_trace_e = valid_trace +
       
    10   fixes e
       
    11   assumes vt_e: "vt (e#s)"
       
    12 begin
       
    13 
       
    14 lemma pip_e: "PIP s e"
       
    15   using vt_e by (cases, simp)  
       
    16 
       
    17 end
     4 
    18 
     5 lemma runing_ready: 
    19 lemma runing_ready: 
     6   shows "runing s \<subseteq> readys s"
    20   shows "runing s \<subseteq> readys s"
     7   unfolding runing_def readys_def
    21   unfolding runing_def readys_def
     8   by auto 
    22   by auto 
    14 
    28 
    15 lemma wq_v_neq:
    29 lemma wq_v_neq:
    16    "cs \<noteq> cs' \<Longrightarrow> wq (V thread cs#s) cs' = wq s cs'"
    30    "cs \<noteq> cs' \<Longrightarrow> wq (V thread cs#s) cs' = wq s cs'"
    17   by (auto simp:wq_def Let_def cp_def split:list.splits)
    31   by (auto simp:wq_def Let_def cp_def split:list.splits)
    18 
    32 
    19 lemma wq_distinct: "vt s \<Longrightarrow> distinct (wq s cs)"
    33 context valid_trace
    20 proof(erule_tac vt.induct, simp add:wq_def)
    34 begin
       
    35 
       
    36 lemma ind [consumes 0, case_names Nil Cons, induct type]:
       
    37   assumes "PP []"
       
    38      and "(\<And>s e. valid_trace s \<Longrightarrow> valid_trace (e#s) \<Longrightarrow>
       
    39                    PP s \<Longrightarrow> PIP s e \<Longrightarrow> PP (e # s))"
       
    40      shows "PP s"
       
    41 proof(rule vt.induct[OF vt])
       
    42   from assms(1) show "PP []" .
       
    43 next
       
    44   fix s e
       
    45   assume h: "vt s" "PP s" "PIP s e"
       
    46   show "PP (e # s)"
       
    47   proof(cases rule:assms(2))
       
    48     from h(1) show v1: "valid_trace s" by (unfold_locales, simp)
       
    49   next
       
    50     from h(1,3) have "vt (e#s)" by auto
       
    51     thus "valid_trace (e # s)" by (unfold_locales, simp)
       
    52   qed (insert h, auto)
       
    53 qed
       
    54 
       
    55 lemma wq_distinct: "distinct (wq s cs)"
       
    56 proof(rule ind, simp add:wq_def)
    21   fix s e
    57   fix s e
    22   assume h1: "step s e"
    58   assume h1: "step s e"
    23   and h2: "distinct (wq s cs)"
    59   and h2: "distinct (wq s cs)"
    24   thus "distinct (wq (e # s) cs)"
    60   thus "distinct (wq (e # s) cs)"
    25   proof(induct rule:step.induct, auto simp: wq_def Let_def split:list.splits)
    61   proof(induct rule:step.induct, auto simp: wq_def Let_def split:list.splits)
    49       thus "distinct q" by auto
    85       thus "distinct q" by auto
    50     qed
    86     qed
    51   qed
    87   qed
    52 qed
    88 qed
    53 
    89 
       
    90 end
       
    91 
       
    92 
       
    93 context valid_trace_e
       
    94 begin
       
    95 
    54 text {*
    96 text {*
    55   The following lemma shows that only the @{text "P"}
    97   The following lemma shows that only the @{text "P"}
    56   operation can add new thread into waiting queues. 
    98   operation can add new thread into waiting queues. 
    57   Such kind of lemmas are very obvious, but need to be checked formally.
    99   Such kind of lemmas are very obvious, but need to be checked formally.
    58   This is a kind of confirmation that our modelling is correct.
   100   This is a kind of confirmation that our modelling is correct.
    59 *}
   101 *}
    60 
   102 
    61 lemma block_pre: 
   103 lemma block_pre: 
    62   fixes thread cs s
   104   assumes s_ni: "thread \<notin>  set (wq s cs)"
    63   assumes vt_e: "vt (e#s)"
       
    64   and s_ni: "thread \<notin>  set (wq s cs)"
       
    65   and s_i: "thread \<in> set (wq (e#s) cs)"
   105   and s_i: "thread \<in> set (wq (e#s) cs)"
    66   shows "e = P thread cs"
   106   shows "e = P thread cs"
    67 proof -
   107 proof -
    68   show ?thesis
   108   show ?thesis
    69   proof(cases e)
   109   proof(cases e)
    83     case (Set th prio)
   123     case (Set th prio)
    84     with assms show ?thesis
   124     with assms show ?thesis
    85       by (auto simp:wq_def Let_def split:if_splits)
   125       by (auto simp:wq_def Let_def split:if_splits)
    86   next
   126   next
    87     case (V th cs)
   127     case (V th cs)
    88     with assms show ?thesis
   128     with vt_e assms show ?thesis
    89       apply (auto simp:wq_def Let_def split:if_splits)
   129       apply (auto simp:wq_def Let_def split:if_splits)
    90     proof -
   130     proof -
    91       fix q qs
   131       fix q qs
    92       assume h1: "thread \<notin> set (wq_fun (schs s) cs)"
   132       assume h1: "thread \<notin> set (wq_fun (schs s) cs)"
    93         and h2: "q # qs = wq_fun (schs s) cs"
   133         and h2: "q # qs = wq_fun (schs s) cs"
    96       from h1 and h2[symmetric] have "thread \<notin> set (q # qs)" by simp
   136       from h1 and h2[symmetric] have "thread \<notin> set (q # qs)" by simp
    97       moreover have "thread \<in> set qs"
   137       moreover have "thread \<in> set qs"
    98       proof -
   138       proof -
    99         have "set (SOME q. distinct q \<and> set q = set qs) = set qs"
   139         have "set (SOME q. distinct q \<and> set q = set qs) = set qs"
   100         proof(rule someI2)
   140         proof(rule someI2)
   101           from wq_distinct [OF step_back_vt[OF vt], of cs]
   141           from wq_distinct [of cs]
   102           and h2[symmetric, folded wq_def]
   142           and h2[symmetric, folded wq_def]
   103           show "distinct qs \<and> set qs = set qs" by auto
   143           show "distinct qs \<and> set qs = set qs" by auto
   104         next
   144         next
   105           fix x assume "distinct x \<and> set x = set qs"
   145           fix x assume "distinct x \<and> set x = set qs"
   106           thus "set x = set qs" by auto
   146           thus "set x = set qs" by auto
   110       ultimately show "False" by auto
   150       ultimately show "False" by auto
   111       qed
   151       qed
   112   qed
   152   qed
   113 qed
   153 qed
   114 
   154 
       
   155 end
       
   156 
   115 text {*
   157 text {*
   116   The following lemmas is also obvious and shallow. It says
   158   The following lemmas is also obvious and shallow. It says
   117   that only running thread can request for a critical resource 
   159   that only running thread can request for a critical resource 
   118   and that the requested resource must be one which is
   160   and that the requested resource must be one which is
   119   not current held by the thread.
   161   not current held by the thread.
   124 apply (ind_cases "vt ((P thread cs)#s)")
   166 apply (ind_cases "vt ((P thread cs)#s)")
   125 apply (ind_cases "step s (P thread cs)")
   167 apply (ind_cases "step s (P thread cs)")
   126 by auto
   168 by auto
   127 
   169 
   128 lemma abs1:
   170 lemma abs1:
   129   fixes e es
       
   130   assumes ein: "e \<in> set es"
   171   assumes ein: "e \<in> set es"
   131   and neq: "hd es \<noteq> hd (es @ [x])"
   172   and neq: "hd es \<noteq> hd (es @ [x])"
   132   shows "False"
   173   shows "False"
   133 proof -
   174 proof -
   134   from ein have "es \<noteq> []" by auto
   175   from ein have "es \<noteq> []" by auto
   139 lemma q_head: "Q (hd es) \<Longrightarrow> hd es = hd [th\<leftarrow>es . Q th]"
   180 lemma q_head: "Q (hd es) \<Longrightarrow> hd es = hd [th\<leftarrow>es . Q th]"
   140   by (cases es, auto)
   181   by (cases es, auto)
   141 
   182 
   142 inductive_cases evt_cons: "vt (a#s)"
   183 inductive_cases evt_cons: "vt (a#s)"
   143 
   184 
       
   185 context valid_trace_e
       
   186 begin
       
   187 
   144 lemma abs2:
   188 lemma abs2:
   145   assumes vt: "vt (e#s)"
   189   assumes inq: "thread \<in> set (wq s cs)"
   146   and inq: "thread \<in> set (wq s cs)"
       
   147   and nh: "thread = hd (wq s cs)"
   190   and nh: "thread = hd (wq s cs)"
   148   and qt: "thread \<noteq> hd (wq (e#s) cs)"
   191   and qt: "thread \<noteq> hd (wq (e#s) cs)"
   149   and inq': "thread \<in> set (wq (e#s) cs)"
   192   and inq': "thread \<in> set (wq (e#s) cs)"
   150   shows "False"
   193   shows "False"
   151 proof -
   194 proof -
   152   from assms show "False"
   195   from vt_e assms show "False"
   153     apply (cases e)
   196     apply (cases e)
   154     apply ((simp split:if_splits add:Let_def wq_def)[1])+
   197     apply ((simp split:if_splits add:Let_def wq_def)[1])+
   155     apply (insert abs1, fast)[1]
   198     apply (insert abs1, fast)[1]
   156     apply (auto simp:wq_def simp:Let_def split:if_splits list.splits)
   199     apply (auto simp:wq_def simp:Let_def split:if_splits list.splits)
   157   proof -
   200   proof -
   159     assume vt: "vt (V th cs # s)"
   202     assume vt: "vt (V th cs # s)"
   160       and th_in: "thread \<in> set (SOME q. distinct q \<and> set q = set qs)"
   203       and th_in: "thread \<in> set (SOME q. distinct q \<and> set q = set qs)"
   161       and eq_wq: "wq_fun (schs s) cs = thread # qs"
   204       and eq_wq: "wq_fun (schs s) cs = thread # qs"
   162     show "False"
   205     show "False"
   163     proof -
   206     proof -
   164       from wq_distinct[OF step_back_vt[OF vt], of cs]
   207       from wq_distinct[of cs]
   165         and eq_wq[folded wq_def] have "distinct (thread#qs)" by simp
   208         and eq_wq[folded wq_def] have "distinct (thread#qs)" by simp
   166       moreover have "thread \<in> set qs"
   209       moreover have "thread \<in> set qs"
   167       proof -
   210       proof -
   168         have "set (SOME q. distinct q \<and> set q = set qs) = set qs"
   211         have "set (SOME q. distinct q \<and> set q = set qs) = set qs"
   169         proof(rule someI2)
   212         proof(rule someI2)
   170           from wq_distinct [OF step_back_vt[OF vt], of cs]
   213           from wq_distinct [of cs]
   171           and eq_wq [folded wq_def]
   214           and eq_wq [folded wq_def]
   172           show "distinct qs \<and> set qs = set qs" by auto
   215           show "distinct qs \<and> set qs = set qs" by auto
   173         next
   216         next
   174           fix x assume "distinct x \<and> set x = set qs"
   217           fix x assume "distinct x \<and> set x = set qs"
   175           thus "set x = set qs" by auto
   218           thus "set x = set qs" by auto
   179       ultimately show ?thesis by auto
   222       ultimately show ?thesis by auto
   180     qed
   223     qed
   181   qed
   224   qed
   182 qed
   225 qed
   183 
   226 
   184 lemma vt_moment: "\<And> t. \<lbrakk>vt s\<rbrakk> \<Longrightarrow> vt (moment t s)"
   227 end
   185 proof(induct s, simp)
   228 
   186   fix a s t
   229 context valid_trace
   187   assume h: "\<And>t.\<lbrakk>vt s\<rbrakk> \<Longrightarrow> vt (moment t s)"
   230 begin
   188     and vt_a: "vt (a # s)"
   231 
   189   show "vt (moment t (a # s))"
   232 lemma vt_moment: "\<And> t. vt (moment t s)"
   190   proof(cases "t \<ge> length (a#s)")
   233 proof(induct rule:ind)
       
   234   case Nil
       
   235   thus ?case by (simp add:vt_nil)
       
   236 next
       
   237   case (Cons s e t)
       
   238   show ?case
       
   239   proof(cases "t \<ge> length (e#s)")
   191     case True
   240     case True
   192     from True have "moment t (a#s) = a#s" by simp
   241     from True have "moment t (e#s) = e#s" by simp
   193     with vt_a show ?thesis by simp
   242     thus ?thesis using Cons
       
   243       by (simp add:valid_trace_def)
   194   next
   244   next
   195     case False
   245     case False
   196     hence le_t1: "t \<le> length s" by simp
   246     from Cons have "vt (moment t s)" by simp
   197     from vt_a have "vt s"
   247     moreover have "moment t (e#s) = moment t s"
   198       by (erule_tac evt_cons, simp)
       
   199     from h [OF this] have "vt (moment t s)" .
       
   200     moreover have "moment t (a#s) = moment t s"
       
   201     proof -
   248     proof -
   202       from moment_app [OF le_t1, of "[a]"] 
   249       from False have "t \<le> length s" by simp
       
   250       from moment_app [OF this, of "[e]"] 
   203       show ?thesis by simp
   251       show ?thesis by simp
   204     qed
   252     qed
   205     ultimately show ?thesis by auto
   253     ultimately show ?thesis by simp
   206   qed
   254   qed
   207 qed
   255 qed
   208 
   256 
   209 (* Wrong:
   257 (* Wrong:
   210     lemma \<lbrakk>thread \<in> set (wq_fun cs1 s); thread \<in> set (wq_fun cs2 s)\<rbrakk> \<Longrightarrow> cs1 = cs2"
   258     lemma \<lbrakk>thread \<in> set (wq_fun cs1 s); thread \<in> set (wq_fun cs2 s)\<rbrakk> \<Longrightarrow> cs1 = cs2"
   242   in blocked state at moment @{text "t2"} and could not
   290   in blocked state at moment @{text "t2"} and could not
   243   make any request and get blocked the second time: Contradiction.
   291   make any request and get blocked the second time: Contradiction.
   244 *}
   292 *}
   245 
   293 
   246 lemma waiting_unique_pre:
   294 lemma waiting_unique_pre:
   247   fixes cs1 cs2 s thread
   295   assumes h11: "thread \<in> set (wq s cs1)"
   248   assumes vt: "vt s"
       
   249   and h11: "thread \<in> set (wq s cs1)"
       
   250   and h12: "thread \<noteq> hd (wq s cs1)"
   296   and h12: "thread \<noteq> hd (wq s cs1)"
   251   assumes h21: "thread \<in> set (wq s cs2)"
   297   assumes h21: "thread \<in> set (wq s cs2)"
   252   and h22: "thread \<noteq> hd (wq s cs2)"
   298   and h22: "thread \<noteq> hd (wq s cs2)"
   253   and neq12: "cs1 \<noteq> cs2"
   299   and neq12: "cs1 \<noteq> cs2"
   254   shows "False"
   300   shows "False"
   280       obtain e where eq_m: "moment ?t3 s = e#moment t2 s" by auto
   326       obtain e where eq_m: "moment ?t3 s = e#moment t2 s" by auto
   281       have "t2 < ?t3" by simp
   327       have "t2 < ?t3" by simp
   282       from nn2 [rule_format, OF this] and eq_m
   328       from nn2 [rule_format, OF this] and eq_m
   283       have h1: "thread \<in> set (wq (e#moment t2 s) cs2)" and
   329       have h1: "thread \<in> set (wq (e#moment t2 s) cs2)" and
   284         h2: "thread \<noteq> hd (wq (e#moment t2 s) cs2)" by auto
   330         h2: "thread \<noteq> hd (wq (e#moment t2 s) cs2)" by auto
   285       have vt_e: "vt (e#moment t2 s)"
   331       have "vt (e#moment t2 s)"
   286       proof -
   332       proof -
   287         from vt_moment [OF vt]
   333         from vt_moment 
   288         have "vt (moment ?t3 s)" .
   334         have "vt (moment ?t3 s)" .
   289         with eq_m show ?thesis by simp
   335         with eq_m show ?thesis by simp
   290       qed
   336       qed
       
   337       then interpret vt_e: valid_trace_e "moment t2 s" "e"
       
   338         by (unfold_locales, auto, cases, simp)
   291       have ?thesis
   339       have ?thesis
   292       proof(cases "thread \<in> set (wq (moment t2 s) cs2)")
   340       proof(cases "thread \<in> set (wq (moment t2 s) cs2)")
   293         case True
   341         case True
   294         from True and np2 have eq_th: "thread = hd (wq (moment t2 s) cs2)"
   342         from True and np2 have eq_th: "thread = hd (wq (moment t2 s) cs2)"
   295           by auto
   343           by auto 
   296           thm abs2
   344         from vt_e.abs2 [OF True eq_th h2 h1]
   297         from abs2 [OF vt_e True eq_th h2 h1]
       
   298         show ?thesis by auto
   345         show ?thesis by auto
   299       next
   346       next
   300         case False
   347         case False
   301         from block_pre [OF vt_e False h1]
   348         from vt_e.block_pre[OF False h1]
   302         have "e = P thread cs2" .
   349         have "e = P thread cs2" .
   303         with vt_e have "vt ((P thread cs2)# moment t2 s)" by simp
   350         with vt_e.vt_e have "vt ((P thread cs2)# moment t2 s)" by simp
   304         from p_pre [OF this] have "thread \<in> runing (moment t2 s)" by simp
   351         from p_pre [OF this] have "thread \<in> runing (moment t2 s)" by simp
   305         with runing_ready have "thread \<in> readys (moment t2 s)" by auto
   352         with runing_ready have "thread \<in> readys (moment t2 s)" by auto
   306         with nn1 [rule_format, OF lt12]
   353         with nn1 [rule_format, OF lt12]
   307         show ?thesis  by (simp add:readys_def wq_def s_waiting_def, auto)
   354         show ?thesis  by (simp add:readys_def wq_def s_waiting_def, auto)
   308       qed
   355       qed
   314       obtain e where eq_m: "moment ?t3 s = e#moment t1 s" by auto
   361       obtain e where eq_m: "moment ?t3 s = e#moment t1 s" by auto
   315       have lt_t3: "t1 < ?t3" by simp
   362       have lt_t3: "t1 < ?t3" by simp
   316       from nn1 [rule_format, OF this] and eq_m
   363       from nn1 [rule_format, OF this] and eq_m
   317       have h1: "thread \<in> set (wq (e#moment t1 s) cs1)" and
   364       have h1: "thread \<in> set (wq (e#moment t1 s) cs1)" and
   318         h2: "thread \<noteq> hd (wq (e#moment t1 s) cs1)" by auto
   365         h2: "thread \<noteq> hd (wq (e#moment t1 s) cs1)" by auto
   319       have vt_e: "vt  (e#moment t1 s)"
   366       have "vt  (e#moment t1 s)"
   320       proof -
   367       proof -
   321         from vt_moment [OF vt]
   368         from vt_moment
   322         have "vt (moment ?t3 s)" .
   369         have "vt (moment ?t3 s)" .
   323         with eq_m show ?thesis by simp
   370         with eq_m show ?thesis by simp
   324       qed
   371       qed
       
   372       then interpret vt_e: valid_trace_e "moment t1 s" e
       
   373         by (unfold_locales, auto, cases, auto)
   325       have ?thesis
   374       have ?thesis
   326       proof(cases "thread \<in> set (wq (moment t1 s) cs1)")
   375       proof(cases "thread \<in> set (wq (moment t1 s) cs1)")
   327         case True
   376         case True
   328         from True and np1 have eq_th: "thread = hd (wq (moment t1 s) cs1)"
   377         from True and np1 have eq_th: "thread = hd (wq (moment t1 s) cs1)"
   329           by auto
   378           by auto
   330         from abs2 [OF vt_e True eq_th h2 h1]
   379         from vt_e.abs2 True eq_th h2 h1
   331         show ?thesis by auto
   380         show ?thesis by auto
   332       next
   381       next
   333         case False
   382         case False
   334         from block_pre [OF vt_e False h1]
   383         from vt_e.block_pre [OF False h1]
   335         have "e = P thread cs1" .
   384         have "e = P thread cs1" .
   336         with vt_e have "vt ((P thread cs1)# moment t1 s)" by simp
   385         with vt_e.vt_e have "vt ((P thread cs1)# moment t1 s)" by simp
   337         from p_pre [OF this] have "thread \<in> runing (moment t1 s)" by simp
   386         from p_pre [OF this] have "thread \<in> runing (moment t1 s)" by simp
   338         with runing_ready have "thread \<in> readys (moment t1 s)" by auto
   387         with runing_ready have "thread \<in> readys (moment t1 s)" by auto
   339         with nn2 [rule_format, OF lt12]
   388         with nn2 [rule_format, OF lt12]
   340         show ?thesis  by (simp add:readys_def wq_def s_waiting_def, auto)
   389         show ?thesis  by (simp add:readys_def wq_def s_waiting_def, auto)
   341       qed
   390       qed
   349       from nn1 [rule_format, OF this] and eq_m
   398       from nn1 [rule_format, OF this] and eq_m
   350       have h1: "thread \<in> set (wq (e#moment t1 s) cs1)" and
   399       have h1: "thread \<in> set (wq (e#moment t1 s) cs1)" and
   351         h2: "thread \<noteq> hd (wq (e#moment t1 s) cs1)" by auto
   400         h2: "thread \<noteq> hd (wq (e#moment t1 s) cs1)" by auto
   352       have vt_e: "vt (e#moment t1 s)"
   401       have vt_e: "vt (e#moment t1 s)"
   353       proof -
   402       proof -
   354         from vt_moment [OF vt]
   403         from vt_moment
   355         have "vt (moment ?t3 s)" .
   404         have "vt (moment ?t3 s)" .
   356         with eq_m show ?thesis by simp
   405         with eq_m show ?thesis by simp
   357       qed
   406       qed
       
   407       then interpret vt_e: valid_trace_e "moment t1 s" e
       
   408         by (unfold_locales, auto, cases, auto)
   358       have ?thesis
   409       have ?thesis
   359       proof(cases "thread \<in> set (wq (moment t1 s) cs1)")
   410       proof(cases "thread \<in> set (wq (moment t1 s) cs1)")
   360         case True
   411         case True
   361         from True and np1 have eq_th: "thread = hd (wq (moment t1 s) cs1)"
   412         from True and np1 have eq_th: "thread = hd (wq (moment t1 s) cs1)"
   362           by auto
   413           by auto
   363         from abs2 [OF vt_e True eq_th h2 h1]
   414         from vt_e.abs2 [OF True eq_th h2 h1]
   364         show ?thesis by auto
   415         show ?thesis by auto
   365       next
   416       next
   366         case False
   417         case False
   367         from block_pre [OF vt_e False h1]
   418         from vt_e.block_pre [OF False h1]
   368         have eq_e1: "e = P thread cs1" .
   419         have eq_e1: "e = P thread cs1" .
   369         have lt_t3: "t1 < ?t3" by simp
   420         have lt_t3: "t1 < ?t3" by simp
   370         with eqt12 have "t2 < ?t3" by simp
   421         with eqt12 have "t2 < ?t3" by simp
   371         from nn2 [rule_format, OF this] and eq_m and eqt12
   422         from nn2 [rule_format, OF this] and eq_m and eqt12
   372         have h1: "thread \<in> set (wq (e#moment t2 s) cs2)" and
   423         have h1: "thread \<in> set (wq (e#moment t2 s) cs2)" and
   375         proof(cases "thread \<in> set (wq (moment t2 s) cs2)")
   426         proof(cases "thread \<in> set (wq (moment t2 s) cs2)")
   376           case True
   427           case True
   377           from True and np2 have eq_th: "thread = hd (wq (moment t2 s) cs2)"
   428           from True and np2 have eq_th: "thread = hd (wq (moment t2 s) cs2)"
   378             by auto
   429             by auto
   379           from vt_e and eqt12 have "vt (e#moment t2 s)" by simp 
   430           from vt_e and eqt12 have "vt (e#moment t2 s)" by simp 
   380           from abs2 [OF this True eq_th h2 h1]
   431           then interpret vt_e2: valid_trace_e "moment t2 s" e
       
   432             by (unfold_locales, auto, cases, auto)
       
   433           from vt_e2.abs2 [OF True eq_th h2 h1]
   381           show ?thesis .
   434           show ?thesis .
   382         next
   435         next
   383           case False
   436           case False
   384           have vt_e: "vt (e#moment t2 s)"
   437           have "vt (e#moment t2 s)"
   385           proof -
   438           proof -
   386             from vt_moment [OF vt] eqt12
   439             from vt_moment eqt12
   387             have "vt (moment (Suc t2) s)" by auto
   440             have "vt (moment (Suc t2) s)" by auto
   388             with eq_m eqt12 show ?thesis by simp
   441             with eq_m eqt12 show ?thesis by simp
   389           qed
   442           qed
   390           from block_pre [OF vt_e False h1]
   443           then interpret vt_e2: valid_trace_e "moment t2 s" e
       
   444             by (unfold_locales, auto, cases, auto)
       
   445           from vt_e2.block_pre [OF False h1]
   391           have "e = P thread cs2" .
   446           have "e = P thread cs2" .
   392           with eq_e1 neq12 show ?thesis by auto
   447           with eq_e1 neq12 show ?thesis by auto
   393         qed
   448         qed
   394       qed
   449       qed
   395     } ultimately show ?thesis by arith
   450     } ultimately show ?thesis by arith
   399 text {*
   454 text {*
   400   This lemma is a simple corrolary of @{text "waiting_unique_pre"}.
   455   This lemma is a simple corrolary of @{text "waiting_unique_pre"}.
   401 *}
   456 *}
   402 
   457 
   403 lemma waiting_unique:
   458 lemma waiting_unique:
   404   fixes s cs1 cs2
   459   assumes "waiting s th cs1"
   405   assumes "vt s"
       
   406   and "waiting s th cs1"
       
   407   and "waiting s th cs2"
   460   and "waiting s th cs2"
   408   shows "cs1 = cs2"
   461   shows "cs1 = cs2"
   409 using waiting_unique_pre assms
   462 using waiting_unique_pre assms
   410 unfolding wq_def s_waiting_def
   463 unfolding wq_def s_waiting_def
   411 by auto
   464 by auto
       
   465 
       
   466 end
   412 
   467 
   413 (* not used *)
   468 (* not used *)
   414 text {*
   469 text {*
   415   Every thread can only be blocked on one critical resource, 
   470   Every thread can only be blocked on one critical resource, 
   416   symmetrically, every critical resource can only be held by one thread. 
   471   symmetrically, every critical resource can only be held by one thread. 
   417   This fact is much more easier according to our definition. 
   472   This fact is much more easier according to our definition. 
   418 *}
   473 *}
   419 lemma held_unique:
   474 lemma held_unique:
   420   fixes s::"state"
   475   assumes "holding (s::event list) th1 cs"
   421   assumes "holding s th1 cs"
       
   422   and "holding s th2 cs"
   476   and "holding s th2 cs"
   423   shows "th1 = th2"
   477   shows "th1 = th2"
   424 using assms
   478  by (insert assms, unfold s_holding_def, auto)
   425 unfolding s_holding_def
       
   426 by auto
       
   427 
   479 
   428 
   480 
   429 lemma last_set_lt: "th \<in> threads s \<Longrightarrow> last_set th s < length s"
   481 lemma last_set_lt: "th \<in> threads s \<Longrightarrow> last_set th s < length s"
   430   apply (induct s, auto)
   482   apply (induct s, auto)
   431   by (case_tac a, auto split:if_splits)
   483   by (case_tac a, auto split:if_splits)
   640 proof -
   692 proof -
   641   fix t c 
   693   fix t c 
   642   assume vt: "vt (V th cs # s)"
   694   assume vt: "vt (V th cs # s)"
   643     and nw: "\<not> waiting (wq (V th cs # s)) t c"
   695     and nw: "\<not> waiting (wq (V th cs # s)) t c"
   644     and wt: "waiting (wq s) t c"
   696     and wt: "waiting (wq s) t c"
       
   697   from vt interpret vt_v: valid_trace_e s "V th cs" 
       
   698     by  (cases, unfold_locales, simp)
   645   show "next_th s th cs t \<and> cs = c"
   699   show "next_th s th cs t \<and> cs = c"
   646   proof(cases "cs = c")
   700   proof(cases "cs = c")
   647     case False
   701     case False
   648     with nw wt show ?thesis
   702     with nw wt show ?thesis
   649       by (auto simp:cs_waiting_def wq_def Let_def)
   703       by (auto simp:cs_waiting_def wq_def Let_def)
   657       assume t_in: "t \<in> set list"
   711       assume t_in: "t \<in> set list"
   658         and t_ni: "t \<notin> set (SOME q. distinct q \<and> set q = set list)"
   712         and t_ni: "t \<notin> set (SOME q. distinct q \<and> set q = set list)"
   659         and eq_wq: "wq_fun (schs s) cs = a # list"
   713         and eq_wq: "wq_fun (schs s) cs = a # list"
   660       have " set (SOME q. distinct q \<and> set q = set list) = set list"
   714       have " set (SOME q. distinct q \<and> set q = set list) = set list"
   661       proof(rule someI2)
   715       proof(rule someI2)
   662         from wq_distinct[OF step_back_vt[OF vt], of cs] and eq_wq[folded wq_def]
   716         from vt_v.wq_distinct[of cs] and eq_wq[folded wq_def]
   663         show "distinct list \<and> set list = set list" by auto
   717         show "distinct list \<and> set list = set list" by auto
   664       next
   718       next
   665         show "\<And>x. distinct x \<and> set x = set list \<Longrightarrow> set x = set list"
   719         show "\<And>x. distinct x \<and> set x = set list \<Longrightarrow> set x = set list"
   666           by auto
   720           by auto
   667       qed
   721       qed
   671       assume t_in: "t \<in> set list"
   725       assume t_in: "t \<in> set list"
   672         and t_ni: "t \<notin> set (SOME q. distinct q \<and> set q = set list)"
   726         and t_ni: "t \<notin> set (SOME q. distinct q \<and> set q = set list)"
   673         and eq_wq: "wq_fun (schs s) cs = a # list"
   727         and eq_wq: "wq_fun (schs s) cs = a # list"
   674       have " set (SOME q. distinct q \<and> set q = set list) = set list"
   728       have " set (SOME q. distinct q \<and> set q = set list) = set list"
   675       proof(rule someI2)
   729       proof(rule someI2)
   676         from wq_distinct[OF step_back_vt[OF vt], of cs] and eq_wq[folded wq_def]
   730         from vt_v.wq_distinct[of cs] and eq_wq[folded wq_def]
   677         show "distinct list \<and> set list = set list" by auto
   731         show "distinct list \<and> set list = set list" by auto
   678       next
   732       next
   679         show "\<And>x. distinct x \<and> set x = set list \<Longrightarrow> set x = set list"
   733         show "\<And>x. distinct x \<and> set x = set list \<Longrightarrow> set x = set list"
   680           by auto
   734           by auto
   681       qed
   735       qed
   702 lemma step_v_release:
   756 lemma step_v_release:
   703   "\<lbrakk>vt (V th cs # s); holding (wq (V th cs # s)) th cs\<rbrakk> \<Longrightarrow> False"
   757   "\<lbrakk>vt (V th cs # s); holding (wq (V th cs # s)) th cs\<rbrakk> \<Longrightarrow> False"
   704 proof -
   758 proof -
   705   assume vt: "vt (V th cs # s)"
   759   assume vt: "vt (V th cs # s)"
   706     and hd: "holding (wq (V th cs # s)) th cs"
   760     and hd: "holding (wq (V th cs # s)) th cs"
       
   761   from vt interpret vt_v: valid_trace_e s "V th cs"
       
   762     by (cases, unfold_locales, simp+)
   707   from step_back_step [OF vt] and hd
   763   from step_back_step [OF vt] and hd
   708   show "False"
   764   show "False"
   709   proof(cases)
   765   proof(cases)
   710     assume "holding (wq (V th cs # s)) th cs" and "holding s th cs"
   766     assume "holding (wq (V th cs # s)) th cs" and "holding s th cs"
   711     thus ?thesis
   767     thus ?thesis
   717         "wq_fun (schs s) cs = hd (SOME q. distinct q \<and> set q = set list) # list"
   773         "wq_fun (schs s) cs = hd (SOME q. distinct q \<and> set q = set list) # list"
   718       and hd_in: "hd (SOME q. distinct q \<and> set q = set list)
   774       and hd_in: "hd (SOME q. distinct q \<and> set q = set list)
   719             \<in> set (SOME q. distinct q \<and> set q = set list)"
   775             \<in> set (SOME q. distinct q \<and> set q = set list)"
   720       have "set (SOME q. distinct q \<and> set q = set list) = set list"
   776       have "set (SOME q. distinct q \<and> set q = set list) = set list"
   721       proof(rule someI2)
   777       proof(rule someI2)
   722         from wq_distinct[OF step_back_vt[OF vt], of cs] and eq_wq
   778         from vt_v.wq_distinct[of cs] and eq_wq
   723         show "distinct list \<and> set list = set list" by auto
   779         show "distinct list \<and> set list = set list" by auto
   724       next
   780       next
   725         show "\<And>x. distinct x \<and> set x = set list \<Longrightarrow> set x = set list"
   781         show "\<And>x. distinct x \<and> set x = set list \<Longrightarrow> set x = set list"
   726           by auto
   782           by auto
   727       qed
   783       qed
   728       moreover have "distinct  (hd (SOME q. distinct q \<and> set q = set list) # list)"
   784       moreover have "distinct  (hd (SOME q. distinct q \<and> set q = set list) # list)"
   729       proof -
   785       proof -
   730         from wq_distinct[OF step_back_vt[OF vt], of cs] and eq_wq
   786         from vt_v.wq_distinct[of cs] and eq_wq
   731         show ?thesis by auto
   787         show ?thesis by auto
   732       qed
   788       qed
   733       moreover note eq_wq and hd_in
   789       moreover note eq_wq and hd_in
   734       ultimately show "False" by auto
   790       ultimately show "False" by auto
   735     qed
   791     qed
   745   assume vt: "vt (V th cs # s)"
   801   assume vt: "vt (V th cs # s)"
   746     and eq_wq[folded wq_def]: " wq_fun (schs s) cs = th # rest"
   802     and eq_wq[folded wq_def]: " wq_fun (schs s) cs = th # rest"
   747     and nrest: "rest \<noteq> []"
   803     and nrest: "rest \<noteq> []"
   748     and ni: "hd (SOME q. distinct q \<and> set q = set rest)
   804     and ni: "hd (SOME q. distinct q \<and> set q = set rest)
   749             \<notin> set (SOME q. distinct q \<and> set q = set rest)"
   805             \<notin> set (SOME q. distinct q \<and> set q = set rest)"
       
   806   from vt interpret vt_v: valid_trace_e s "V th cs"
       
   807     by (cases, unfold_locales, simp+)
   750   have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
   808   have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
   751   proof(rule someI2)
   809   proof(rule someI2)
   752     from wq_distinct[OF step_back_vt[OF vt], of cs] and eq_wq
   810     from vt_v.wq_distinct[of cs] and eq_wq
   753     show "distinct rest \<and> set rest = set rest" by auto
   811     show "distinct rest \<and> set rest = set rest" by auto
   754   next
   812   next
   755     fix x assume "distinct x \<and> set x = set rest"
   813     fix x assume "distinct x \<and> set x = set rest"
   756     hence "set x = set rest" by auto
   814     hence "set x = set rest" by auto
   757     with nrest
   815     with nrest
   789 proof -
   847 proof -
   790   fix t c
   848   fix t c
   791   let ?s' = "(V th cs # s)"
   849   let ?s' = "(V th cs # s)"
   792   assume vt: "vt ?s'" 
   850   assume vt: "vt ?s'" 
   793     and wt: "waiting (wq ?s') t c"
   851     and wt: "waiting (wq ?s') t c"
       
   852   from vt interpret vt_v: valid_trace_e s "V th cs"
       
   853     by (cases, unfold_locales, simp+)
   794   show "waiting (wq s) t c"
   854   show "waiting (wq s) t c"
   795   proof(cases "c = cs")
   855   proof(cases "c = cs")
   796     case False
   856     case False
   797     assume neq_cs: "c \<noteq> cs"
   857     assume neq_cs: "c \<noteq> cs"
   798     hence "waiting (wq ?s') t c = waiting (wq s) t c"
   858     hence "waiting (wq ?s') t c = waiting (wq s) t c"
   807       assume not_in: "t \<notin> set list"
   867       assume not_in: "t \<notin> set list"
   808         and is_in: "t \<in> set (SOME q. distinct q \<and> set q = set list)"
   868         and is_in: "t \<in> set (SOME q. distinct q \<and> set q = set list)"
   809         and eq_wq: "wq_fun (schs s) cs = a # list"
   869         and eq_wq: "wq_fun (schs s) cs = a # list"
   810       have "set (SOME q. distinct q \<and> set q = set list) = set list"
   870       have "set (SOME q. distinct q \<and> set q = set list) = set list"
   811       proof(rule someI2)
   871       proof(rule someI2)
   812         from wq_distinct [OF step_back_vt[OF vt], of cs]
   872         from vt_v.wq_distinct [of cs]
   813         and eq_wq[folded wq_def]
   873         and eq_wq[folded wq_def]
   814         show "distinct list \<and> set list = set list" by auto
   874         show "distinct list \<and> set list = set list" by auto
   815       next
   875       next
   816         fix x assume "distinct x \<and> set x = set list"
   876         fix x assume "distinct x \<and> set x = set list"
   817         thus "set x = set list" by auto
   877         thus "set x = set list" by auto
   825         apply (unfold wq_def, auto simp:Let_def cs_waiting_def)
   885         apply (unfold wq_def, auto simp:Let_def cs_waiting_def)
   826       proof -
   886       proof -
   827         assume " t \<in> set (SOME q. distinct q \<and> set q = set list)"
   887         assume " t \<in> set (SOME q. distinct q \<and> set q = set list)"
   828         moreover have "\<dots> = set list" 
   888         moreover have "\<dots> = set list" 
   829         proof(rule someI2)
   889         proof(rule someI2)
   830           from wq_distinct [OF step_back_vt[OF vt], of cs]
   890           from vt_v.wq_distinct [of cs]
   831             and eq_wq[folded wq_def]
   891             and eq_wq[folded wq_def]
   832           show "distinct list \<and> set list = set list" by auto
   892           show "distinct list \<and> set list = set list" by auto
   833         next
   893         next
   834           fix x assume "distinct x \<and> set x = set list" 
   894           fix x assume "distinct x \<and> set x = set list" 
   835           thus "set x = set list" by auto
   895           thus "set x = set list" by auto
   836         qed
   896         qed
   837         ultimately show "t \<in> set list" by simp
   897         ultimately show "t \<in> set list" by simp
   838       qed
   898       qed
   839       with eq_wq and wq_distinct [OF step_back_vt[OF vt], of cs, unfolded wq_def]
   899       with eq_wq and vt_v.wq_distinct [of cs, unfolded wq_def]
   840       show False by auto
   900       show False by auto
   841     qed
   901     qed
   842   qed
   902   qed
   843 qed
   903 qed
   844 
   904 
   883 
   943 
   884 
   944 
   885 lemma RAG_target_th: "(Th th, x) \<in> RAG (s::state) \<Longrightarrow> \<exists> cs. x = Cs cs"
   945 lemma RAG_target_th: "(Th th, x) \<in> RAG (s::state) \<Longrightarrow> \<exists> cs. x = Cs cs"
   886   by (unfold s_RAG_def, auto)
   946   by (unfold s_RAG_def, auto)
   887 
   947 
       
   948 context valid_trace
       
   949 begin
       
   950 
   888 text {*
   951 text {*
   889   The following lemma shows that @{text "RAG"} is acyclic.
   952   The following lemma shows that @{text "RAG"} is acyclic.
   890   The overall structure is by induction on the formation of @{text "vt s"}
   953   The overall structure is by induction on the formation of @{text "vt s"}
   891   and then case analysis on event @{text "e"}, where the non-trivial cases 
   954   and then case analysis on event @{text "e"}, where the non-trivial cases 
   892   for those for @{text "V"} and @{text "P"} events.
   955   for those for @{text "V"} and @{text "P"} events.
   893 *}
   956 *}
   894 lemma acyclic_RAG: 
   957 lemma acyclic_RAG:
   895   fixes s
       
   896   assumes vt: "vt s"
       
   897   shows "acyclic (RAG s)"
   958   shows "acyclic (RAG s)"
   898 using assms
   959 using vt
   899 proof(induct)
   960 proof(induct)
   900   case (vt_cons s e)
   961   case (vt_cons s e)
       
   962   interpret vt_s: valid_trace s using vt_cons(1)
       
   963     by (unfold_locales, simp)
   901   assume ih: "acyclic (RAG s)"
   964   assume ih: "acyclic (RAG s)"
   902     and stp: "step s e"
   965     and stp: "step s e"
   903     and vt: "vt s"
   966     and vt: "vt s"
   904   show ?case
   967   show ?case
   905   proof(cases e)
   968   proof(cases e)
   947           obtain cs' where eq_x: "x = Cs cs'" by auto
  1010           obtain cs' where eq_x: "x = Cs cs'" by auto
   948           with th_d have "(Th ?th', Cs cs') \<in> ?A" by simp
  1011           with th_d have "(Th ?th', Cs cs') \<in> ?A" by simp
   949           hence wt_th': "waiting s ?th' cs'"
  1012           hence wt_th': "waiting s ?th' cs'"
   950             unfolding s_RAG_def s_waiting_def cs_waiting_def wq_def by simp
  1013             unfolding s_RAG_def s_waiting_def cs_waiting_def wq_def by simp
   951           hence "cs' = cs"
  1014           hence "cs' = cs"
   952           proof(rule waiting_unique [OF vt])
  1015           proof(rule vt_s.waiting_unique)
   953             from eq_wq wq_distinct[OF vt, of cs]
  1016             from eq_wq vt_s.wq_distinct[of cs]
   954             show "waiting s ?th' cs" 
  1017             show "waiting s ?th' cs" 
   955               apply (unfold s_waiting_def wq_def, auto)
  1018               apply (unfold s_waiting_def wq_def, auto)
   956             proof -
  1019             proof -
   957               assume hd_in: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> set rest"
  1020               assume hd_in: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> set rest"
   958                 and eq_wq: "wq_fun (schs s) cs = th # rest"
  1021                 and eq_wq: "wq_fun (schs s) cs = th # rest"
   959               have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
  1022               have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
   960               proof(rule someI2)
  1023               proof(rule someI2)
   961                 from wq_distinct[OF vt, of cs] and eq_wq
  1024                 from vt_s.wq_distinct[of cs] and eq_wq
   962                 show "distinct rest \<and> set rest = set rest" unfolding wq_def by auto
  1025                 show "distinct rest \<and> set rest = set rest" unfolding wq_def by auto
   963               next
  1026               next
   964                 fix x assume "distinct x \<and> set x = set rest"
  1027                 fix x assume "distinct x \<and> set x = set rest"
   965                 with False show "x \<noteq> []" by auto
  1028                 with False show "x \<noteq> []" by auto
   966               qed
  1029               qed
   967               hence "hd (SOME q. distinct q \<and> set q = set rest) \<in> 
  1030               hence "hd (SOME q. distinct q \<and> set q = set rest) \<in> 
   968                 set (SOME q. distinct q \<and> set q = set rest)" by auto
  1031                 set (SOME q. distinct q \<and> set q = set rest)" by auto
   969               moreover have "\<dots> = set rest" 
  1032               moreover have "\<dots> = set rest" 
   970               proof(rule someI2)
  1033               proof(rule someI2)
   971                 from wq_distinct[OF vt, of cs] and eq_wq
  1034                 from vt_s.wq_distinct[of cs] and eq_wq
   972                 show "distinct rest \<and> set rest = set rest" unfolding wq_def by auto
  1035                 show "distinct rest \<and> set rest = set rest" unfolding wq_def by auto
   973               next
  1036               next
   974                 show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
  1037                 show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
   975               qed
  1038               qed
   976               moreover note hd_in
  1039               moreover note hd_in
   978             next
  1041             next
   979               assume hd_in: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> set rest"
  1042               assume hd_in: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> set rest"
   980                 and eq_wq: "wq s cs = hd (SOME q. distinct q \<and> set q = set rest) # rest"
  1043                 and eq_wq: "wq s cs = hd (SOME q. distinct q \<and> set q = set rest) # rest"
   981               have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
  1044               have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
   982               proof(rule someI2)
  1045               proof(rule someI2)
   983                 from wq_distinct[OF vt, of cs] and eq_wq
  1046                 from vt_s.wq_distinct[of cs] and eq_wq
   984                 show "distinct rest \<and> set rest = set rest" by auto
  1047                 show "distinct rest \<and> set rest = set rest" by auto
   985               next
  1048               next
   986                 fix x assume "distinct x \<and> set x = set rest"
  1049                 fix x assume "distinct x \<and> set x = set rest"
   987                 with False show "x \<noteq> []" by auto
  1050                 with False show "x \<noteq> []" by auto
   988               qed
  1051               qed
   989               hence "hd (SOME q. distinct q \<and> set q = set rest) \<in> 
  1052               hence "hd (SOME q. distinct q \<and> set q = set rest) \<in> 
   990                 set (SOME q. distinct q \<and> set q = set rest)" by auto
  1053                 set (SOME q. distinct q \<and> set q = set rest)" by auto
   991               moreover have "\<dots> = set rest" 
  1054               moreover have "\<dots> = set rest" 
   992               proof(rule someI2)
  1055               proof(rule someI2)
   993                 from wq_distinct[OF vt, of cs] and eq_wq
  1056                 from vt_s.wq_distinct[of cs] and eq_wq
   994                 show "distinct rest \<and> set rest = set rest" by auto
  1057                 show "distinct rest \<and> set rest = set rest" by auto
   995               next
  1058               next
   996                 show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
  1059                 show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
   997               qed
  1060               qed
   998               moreover note hd_in
  1061               moreover note hd_in
  1064       by (auto simp: s_RAG_def cs_waiting_def 
  1127       by (auto simp: s_RAG_def cs_waiting_def 
  1065         cs_holding_def wq_def acyclic_def)
  1128         cs_holding_def wq_def acyclic_def)
  1066 qed
  1129 qed
  1067 
  1130 
  1068 
  1131 
  1069 lemma finite_RAG: 
  1132 lemma finite_RAG:
  1070   fixes s
       
  1071   assumes vt: "vt s"
       
  1072   shows "finite (RAG s)"
  1133   shows "finite (RAG s)"
  1073 proof -
  1134 proof -
  1074   from vt show ?thesis
  1135   from vt show ?thesis
  1075   proof(induct)
  1136   proof(induct)
  1076     case (vt_cons s e)
  1137     case (vt_cons s e)
       
  1138     interpret vt_s: valid_trace s using vt_cons(1)
       
  1139       by (unfold_locales, simp)
  1077     assume ih: "finite (RAG s)"
  1140     assume ih: "finite (RAG s)"
  1078       and stp: "step s e"
  1141       and stp: "step s e"
  1079       and vt: "vt s"
  1142       and vt: "vt s"
  1080     show ?case
  1143     show ?case
  1081     proof(cases e)
  1144     proof(cases e)
  1143 qed
  1206 qed
  1144 
  1207 
  1145 text {* Several useful lemmas *}
  1208 text {* Several useful lemmas *}
  1146 
  1209 
  1147 lemma wf_dep_converse: 
  1210 lemma wf_dep_converse: 
  1148   fixes s
       
  1149   assumes vt: "vt s"
       
  1150   shows "wf ((RAG s)^-1)"
  1211   shows "wf ((RAG s)^-1)"
  1151 proof(rule finite_acyclic_wf_converse)
  1212 proof(rule finite_acyclic_wf_converse)
  1152   from finite_RAG [OF vt]
  1213   from finite_RAG 
  1153   show "finite (RAG s)" .
  1214   show "finite (RAG s)" .
  1154 next
  1215 next
  1155   from acyclic_RAG[OF vt]
  1216   from acyclic_RAG
  1156   show "acyclic (RAG s)" .
  1217   show "acyclic (RAG s)" .
  1157 qed
  1218 qed
  1158 
  1219 
       
  1220 end
       
  1221 
  1159 lemma hd_np_in: "x \<in> set l \<Longrightarrow> hd l \<in> set l"
  1222 lemma hd_np_in: "x \<in> set l \<Longrightarrow> hd l \<in> set l"
  1160 by (induct l, auto)
  1223   by (induct l, auto)
  1161 
  1224 
  1162 lemma th_chasing: "(Th th, Cs cs) \<in> RAG (s::state) \<Longrightarrow> \<exists> th'. (Cs cs, Th th') \<in> RAG s"
  1225 lemma th_chasing: "(Th th, Cs cs) \<in> RAG (s::state) \<Longrightarrow> \<exists> th'. (Cs cs, Th th') \<in> RAG s"
  1163   by (auto simp:s_RAG_def s_holding_def cs_holding_def cs_waiting_def wq_def dest:hd_np_in)
  1226   by (auto simp:s_RAG_def s_holding_def cs_holding_def cs_waiting_def wq_def dest:hd_np_in)
  1164 
  1227 
       
  1228 context valid_trace
       
  1229 begin
       
  1230 
  1165 lemma wq_threads: 
  1231 lemma wq_threads: 
  1166   fixes s cs
  1232   assumes h: "th \<in> set (wq s cs)"
  1167   assumes vt: "vt s"
       
  1168   and h: "th \<in> set (wq s cs)"
       
  1169   shows "th \<in> threads s"
  1233   shows "th \<in> threads s"
  1170 proof -
  1234 proof -
  1171  from vt and h show ?thesis
  1235  from vt and h show ?thesis
  1172   proof(induct arbitrary: th cs)
  1236   proof(induct arbitrary: th cs)
  1173     case (vt_cons s e)
  1237     case (vt_cons s e)
       
  1238     interpret vt_s: valid_trace s
       
  1239       using vt_cons(1) by (unfold_locales, auto)
  1174     assume ih: "\<And>th cs. th \<in> set (wq s cs) \<Longrightarrow> th \<in> threads s"
  1240     assume ih: "\<And>th cs. th \<in> set (wq s cs) \<Longrightarrow> th \<in> threads s"
  1175       and stp: "step s e"
  1241       and stp: "step s e"
  1176       and vt: "vt s"
  1242       and vt: "vt s"
  1177       and h: "th \<in> set (wq (e # s) cs)"
  1243       and h: "th \<in> set (wq (e # s) cs)"
  1178     show ?case
  1244     show ?case
  1225                 apply (auto simp:Let_def wq_def split:if_splits)
  1291                 apply (auto simp:Let_def wq_def split:if_splits)
  1226               proof -
  1292               proof -
  1227                 assume th_in: "th \<in> set (SOME q. distinct q \<and> set q = set rest)"
  1293                 assume th_in: "th \<in> set (SOME q. distinct q \<and> set q = set rest)"
  1228                 have "set (SOME q. distinct q \<and> set q = set rest) = set rest" 
  1294                 have "set (SOME q. distinct q \<and> set q = set rest) = set rest" 
  1229                 proof(rule someI2)
  1295                 proof(rule someI2)
  1230                   from wq_distinct[OF vt, of cs'] and eq_wq[folded wq_def]
  1296                   from vt_s.wq_distinct[of cs'] and eq_wq[folded wq_def]
  1231                   show "distinct rest \<and> set rest = set rest" by auto
  1297                   show "distinct rest \<and> set rest = set rest" by auto
  1232                 next
  1298                 next
  1233                   show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest"
  1299                   show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest"
  1234                     by auto
  1300                     by auto
  1235                 qed
  1301                 qed
  1262     case vt_nil
  1328     case vt_nil
  1263     thus ?case by (auto simp:wq_def)
  1329     thus ?case by (auto simp:wq_def)
  1264   qed
  1330   qed
  1265 qed
  1331 qed
  1266 
  1332 
  1267 lemma range_in: "\<lbrakk>vt s; (Th th) \<in> Range (RAG (s::state))\<rbrakk> \<Longrightarrow> th \<in> threads s"
  1333 lemma range_in: "\<lbrakk>(Th th) \<in> Range (RAG (s::state))\<rbrakk> \<Longrightarrow> th \<in> threads s"
  1268   apply(unfold s_RAG_def cs_waiting_def cs_holding_def)
  1334   apply(unfold s_RAG_def cs_waiting_def cs_holding_def)
  1269   by (auto intro:wq_threads)
  1335   by (auto intro:wq_threads)
  1270 
  1336 
  1271 lemma readys_v_eq:
  1337 lemma readys_v_eq:
  1272   fixes th thread cs rest
  1338   fixes th thread cs rest
  1273   assumes vt: "vt s"
  1339   assumes neq_th: "th \<noteq> thread"
  1274   and neq_th: "th \<noteq> thread"
       
  1275   and eq_wq: "wq s cs = thread#rest"
  1340   and eq_wq: "wq s cs = thread#rest"
  1276   and not_in: "th \<notin>  set rest"
  1341   and not_in: "th \<notin>  set rest"
  1277   shows "(th \<in> readys (V thread cs#s)) = (th \<in> readys s)"
  1342   shows "(th \<in> readys (V thread cs#s)) = (th \<in> readys s)"
  1278 proof -
  1343 proof -
  1279   from assms show ?thesis
  1344   from assms show ?thesis
  1290        assume th_nin: "th \<notin> set rest"
  1355        assume th_nin: "th \<notin> set rest"
  1291         and th_in: "th \<in> set (SOME q. distinct q \<and> set q = set rest)"
  1356         and th_in: "th \<in> set (SOME q. distinct q \<and> set q = set rest)"
  1292         and eq_wq: "wq_fun (schs s) cs = thread # rest"
  1357         and eq_wq: "wq_fun (schs s) cs = thread # rest"
  1293       have "set (SOME q. distinct q \<and> set q = set rest) = set rest"
  1358       have "set (SOME q. distinct q \<and> set q = set rest) = set rest"
  1294       proof(rule someI2)
  1359       proof(rule someI2)
  1295         from wq_distinct[OF vt, of cs, unfolded wq_def] and eq_wq[unfolded wq_def]
  1360         from wq_distinct[of cs, unfolded wq_def] and eq_wq[unfolded wq_def]
  1296         show "distinct rest \<and> set rest = set rest" by auto
  1361         show "distinct rest \<and> set rest = set rest" by auto
  1297       next
  1362       next
  1298         show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
  1363         show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest" by auto
  1299       qed
  1364       qed
  1300       with th_nin th_in show False by auto
  1365       with th_nin th_in show False by auto
  1306   by chasing out-going edges, it is always possible to reach a node representing a ready
  1371   by chasing out-going edges, it is always possible to reach a node representing a ready
  1307   thread. In this lemma, it is the @{text "th'"}.
  1372   thread. In this lemma, it is the @{text "th'"}.
  1308 *}
  1373 *}
  1309 
  1374 
  1310 lemma chain_building:
  1375 lemma chain_building:
  1311   assumes vt: "vt s"
       
  1312   shows "node \<in> Domain (RAG s) \<longrightarrow> (\<exists> th'. th' \<in> readys s \<and> (node, Th th') \<in> (RAG s)^+)"
  1376   shows "node \<in> Domain (RAG s) \<longrightarrow> (\<exists> th'. th' \<in> readys s \<and> (node, Th th') \<in> (RAG s)^+)"
  1313 proof -
  1377 proof -
  1314   from wf_dep_converse [OF vt]
  1378   from wf_dep_converse
  1315   have h: "wf ((RAG s)\<inverse>)" .
  1379   have h: "wf ((RAG s)\<inverse>)" .
  1316   show ?thesis
  1380   show ?thesis
  1317   proof(induct rule:wf_induct [OF h])
  1381   proof(induct rule:wf_induct [OF h])
  1318     fix x
  1382     fix x
  1319     assume ih [rule_format]: 
  1383     assume ih [rule_format]: 
  1340         proof(cases "th' \<in> readys s")
  1404         proof(cases "th' \<in> readys s")
  1341           case True
  1405           case True
  1342           from True and th'_d show ?thesis by auto
  1406           from True and th'_d show ?thesis by auto
  1343         next
  1407         next
  1344           case False
  1408           case False
  1345           from th'_d and range_in [OF vt] have "th' \<in> threads s" by auto
  1409           from th'_d and range_in  have "th' \<in> threads s" by auto
  1346           with False have "Th th' \<in> Domain (RAG s)" 
  1410           with False have "Th th' \<in> Domain (RAG s)" 
  1347             by (auto simp:readys_def wq_def s_waiting_def s_RAG_def cs_waiting_def Domain_def)
  1411             by (auto simp:readys_def wq_def s_waiting_def s_RAG_def cs_waiting_def Domain_def)
  1348           from ih [OF th'_d this]
  1412           from ih [OF th'_d this]
  1349           obtain th'' where 
  1413           obtain th'' where 
  1350             th''_r: "th'' \<in> readys s" and 
  1414             th''_r: "th'' \<in> readys s" and 
  1360 
  1424 
  1361 text {* \noindent
  1425 text {* \noindent
  1362   The following is just an instance of @{text "chain_building"}.
  1426   The following is just an instance of @{text "chain_building"}.
  1363 *}
  1427 *}
  1364 lemma th_chain_to_ready:
  1428 lemma th_chain_to_ready:
  1365   fixes s th
  1429   assumes th_in: "th \<in> threads s"
  1366   assumes vt: "vt s"
       
  1367   and th_in: "th \<in> threads s"
       
  1368   shows "th \<in> readys s \<or> (\<exists> th'. th' \<in> readys s \<and> (Th th, Th th') \<in> (RAG s)^+)"
  1430   shows "th \<in> readys s \<or> (\<exists> th'. th' \<in> readys s \<and> (Th th, Th th') \<in> (RAG s)^+)"
  1369 proof(cases "th \<in> readys s")
  1431 proof(cases "th \<in> readys s")
  1370   case True
  1432   case True
  1371   thus ?thesis by auto
  1433   thus ?thesis by auto
  1372 next
  1434 next
  1373   case False
  1435   case False
  1374   from False and th_in have "Th th \<in> Domain (RAG s)" 
  1436   from False and th_in have "Th th \<in> Domain (RAG s)" 
  1375     by (auto simp:readys_def s_waiting_def s_RAG_def wq_def cs_waiting_def Domain_def)
  1437     by (auto simp:readys_def s_waiting_def s_RAG_def wq_def cs_waiting_def Domain_def)
  1376   from chain_building [rule_format, OF vt this]
  1438   from chain_building [rule_format, OF this]
  1377   show ?thesis by auto
  1439   show ?thesis by auto
  1378 qed
  1440 qed
       
  1441 
       
  1442 end
  1379 
  1443 
  1380 lemma waiting_eq: "waiting s th cs = waiting (wq s) th cs"
  1444 lemma waiting_eq: "waiting s th cs = waiting (wq s) th cs"
  1381   by  (unfold s_waiting_def cs_waiting_def wq_def, auto)
  1445   by  (unfold s_waiting_def cs_waiting_def wq_def, auto)
  1382 
  1446 
  1383 lemma holding_eq: "holding (s::state) th cs = holding (wq s) th cs"
  1447 lemma holding_eq: "holding (s::state) th cs = holding (wq s) th cs"
  1384   by (unfold s_holding_def wq_def cs_holding_def, simp)
  1448   by (unfold s_holding_def wq_def cs_holding_def, simp)
  1385 
  1449 
  1386 lemma holding_unique: "\<lbrakk>holding (s::state) th1 cs; holding s th2 cs\<rbrakk> \<Longrightarrow> th1 = th2"
  1450 lemma holding_unique: "\<lbrakk>holding (s::state) th1 cs; holding s th2 cs\<rbrakk> \<Longrightarrow> th1 = th2"
  1387   by (unfold s_holding_def cs_holding_def, auto)
  1451   by (unfold s_holding_def cs_holding_def, auto)
  1388 
  1452 
  1389 lemma unique_RAG: "\<lbrakk>vt s; (n, n1) \<in> RAG s; (n, n2) \<in> RAG s\<rbrakk> \<Longrightarrow> n1 = n2"
  1453 context valid_trace
       
  1454 begin
       
  1455 
       
  1456 lemma unique_RAG: "\<lbrakk>(n, n1) \<in> RAG s; (n, n2) \<in> RAG s\<rbrakk> \<Longrightarrow> n1 = n2"
  1390   apply(unfold s_RAG_def, auto, fold waiting_eq holding_eq)
  1457   apply(unfold s_RAG_def, auto, fold waiting_eq holding_eq)
  1391   by(auto elim:waiting_unique holding_unique)
  1458   by(auto elim:waiting_unique holding_unique)
  1392 
  1459 
       
  1460 end
       
  1461 
       
  1462 
  1393 lemma trancl_split: "(a, b) \<in> r^+ \<Longrightarrow> \<exists> c. (a, c) \<in> r"
  1463 lemma trancl_split: "(a, b) \<in> r^+ \<Longrightarrow> \<exists> c. (a, c) \<in> r"
  1394 by (induct rule:trancl_induct, auto)
  1464 by (induct rule:trancl_induct, auto)
  1395 
  1465 
       
  1466 context valid_trace
       
  1467 begin
       
  1468 
  1396 lemma dchain_unique:
  1469 lemma dchain_unique:
  1397   assumes vt: "vt s"
  1470   assumes th1_d: "(n, Th th1) \<in> (RAG s)^+"
  1398   and th1_d: "(n, Th th1) \<in> (RAG s)^+"
       
  1399   and th1_r: "th1 \<in> readys s"
  1471   and th1_r: "th1 \<in> readys s"
  1400   and th2_d: "(n, Th th2) \<in> (RAG s)^+"
  1472   and th2_d: "(n, Th th2) \<in> (RAG s)^+"
  1401   and th2_r: "th2 \<in> readys s"
  1473   and th2_r: "th2 \<in> readys s"
  1402   shows "th1 = th2"
  1474   shows "th1 = th2"
  1403 proof -
  1475 proof -
  1404   { assume neq: "th1 \<noteq> th2"
  1476   { assume neq: "th1 \<noteq> th2"
  1405     hence "Th th1 \<noteq> Th th2" by simp
  1477     hence "Th th1 \<noteq> Th th2" by simp
  1406     from unique_chain [OF _ th1_d th2_d this] and unique_RAG [OF vt]
  1478     from unique_chain [OF _ th1_d th2_d this] and unique_RAG 
  1407     have "(Th th1, Th th2) \<in> (RAG s)\<^sup>+ \<or> (Th th2, Th th1) \<in> (RAG s)\<^sup>+" by auto
  1479     have "(Th th1, Th th2) \<in> (RAG s)\<^sup>+ \<or> (Th th2, Th th1) \<in> (RAG s)\<^sup>+" by auto
  1408     hence "False"
  1480     hence "False"
  1409     proof
  1481     proof
  1410       assume "(Th th1, Th th2) \<in> (RAG s)\<^sup>+"
  1482       assume "(Th th1, Th th2) \<in> (RAG s)\<^sup>+"
  1411       from trancl_split [OF this]
  1483       from trancl_split [OF this]
  1425         by (auto simp:readys_def wq_def s_RAG_def s_waiting_def cs_waiting_def)
  1497         by (auto simp:readys_def wq_def s_RAG_def s_waiting_def cs_waiting_def)
  1426       with th2_r show ?thesis by auto
  1498       with th2_r show ?thesis by auto
  1427     qed
  1499     qed
  1428   } thus ?thesis by auto
  1500   } thus ?thesis by auto
  1429 qed
  1501 qed
       
  1502 
       
  1503 end
  1430              
  1504              
  1431 
  1505 
  1432 lemma step_holdents_p_add:
  1506 lemma step_holdents_p_add:
  1433   fixes th cs s
  1507   fixes th cs s
  1434   assumes vt: "vt (P th cs#s)"
  1508   assumes vt: "vt (P th cs#s)"
  1448   from assms show ?thesis
  1522   from assms show ?thesis
  1449   unfolding  holdents_test step_RAG_p[OF vt] by auto
  1523   unfolding  holdents_test step_RAG_p[OF vt] by auto
  1450 qed
  1524 qed
  1451 
  1525 
  1452 
  1526 
  1453 lemma finite_holding:
  1527 lemma (in valid_trace) finite_holding :
  1454   fixes s th cs
       
  1455   assumes vt: "vt s"
       
  1456   shows "finite (holdents s th)"
  1528   shows "finite (holdents s th)"
  1457 proof -
  1529 proof -
  1458   let ?F = "\<lambda> (x, y). the_cs x"
  1530   let ?F = "\<lambda> (x, y). the_cs x"
  1459   from finite_RAG [OF vt]
  1531   from finite_RAG 
  1460   have "finite (RAG s)" .
  1532   have "finite (RAG s)" .
  1461   hence "finite (?F `(RAG s))" by simp
  1533   hence "finite (?F `(RAG s))" by simp
  1462   moreover have "{cs . (Cs cs, Th th) \<in> RAG s} \<subseteq> \<dots>" 
  1534   moreover have "{cs . (Cs cs, Th th) \<in> RAG s} \<subseteq> \<dots>" 
  1463   proof -
  1535   proof -
  1464     { have h: "\<And> a A f. a \<in> A \<Longrightarrow> f a \<in> f ` A" by auto
  1536     { have h: "\<And> a A f. a \<in> A \<Longrightarrow> f a \<in> f ` A" by auto
  1474 lemma cntCS_v_dec: 
  1546 lemma cntCS_v_dec: 
  1475   fixes s thread cs
  1547   fixes s thread cs
  1476   assumes vtv: "vt (V thread cs#s)"
  1548   assumes vtv: "vt (V thread cs#s)"
  1477   shows "(cntCS (V thread cs#s) thread + 1) = cntCS s thread"
  1549   shows "(cntCS (V thread cs#s) thread + 1) = cntCS s thread"
  1478 proof -
  1550 proof -
       
  1551   from vtv interpret vt_s: valid_trace s
       
  1552     by (cases, unfold_locales, simp)
       
  1553   from vtv interpret vt_v: valid_trace "V thread cs#s"
       
  1554      by (unfold_locales, simp)
  1479   from step_back_step[OF vtv]
  1555   from step_back_step[OF vtv]
  1480   have cs_in: "cs \<in> holdents s thread" 
  1556   have cs_in: "cs \<in> holdents s thread" 
  1481     apply (cases, unfold holdents_test s_RAG_def, simp)
  1557     apply (cases, unfold holdents_test s_RAG_def, simp)
  1482     by (unfold cs_holding_def s_holding_def wq_def, auto)
  1558     by (unfold cs_holding_def s_holding_def wq_def, auto)
  1483   moreover have cs_not_in: 
  1559   moreover have cs_not_in: 
  1484     "(holdents (V thread cs#s) thread) = holdents s thread - {cs}"
  1560     "(holdents (V thread cs#s) thread) = holdents s thread - {cs}"
  1485     apply (insert wq_distinct[OF step_back_vt[OF vtv], of cs])
  1561     apply (insert vt_s.wq_distinct[of cs])
  1486     apply (unfold holdents_test, unfold step_RAG_v[OF vtv],
  1562     apply (unfold holdents_test, unfold step_RAG_v[OF vtv],
  1487             auto simp:next_th_def)
  1563             auto simp:next_th_def)
  1488   proof -
  1564   proof -
  1489     fix rest
  1565     fix rest
  1490     assume dst: "distinct (rest::thread list)"
  1566     assume dst: "distinct (rest::thread list)"
  1534   have "holdents s thread = insert cs (holdents (V thread cs#s) thread)"
  1610   have "holdents s thread = insert cs (holdents (V thread cs#s) thread)"
  1535     by auto
  1611     by auto
  1536   moreover have "card \<dots> = 
  1612   moreover have "card \<dots> = 
  1537                     Suc (card ((holdents (V thread cs#s) thread) - {cs}))"
  1613                     Suc (card ((holdents (V thread cs#s) thread) - {cs}))"
  1538   proof(rule card_insert)
  1614   proof(rule card_insert)
  1539     from finite_holding [OF vtv]
  1615     from vt_v.finite_holding
  1540     show " finite (holdents (V thread cs # s) thread)" .
  1616     show " finite (holdents (V thread cs # s) thread)" .
  1541   qed
  1617   qed
  1542   moreover from cs_not_in 
  1618   moreover from cs_not_in 
  1543   have "cs \<notin> (holdents (V thread cs#s) thread)" by auto
  1619   have "cs \<notin> (holdents (V thread cs#s) thread)" by auto
  1544   ultimately show ?thesis by (simp add:cntCS_def)
  1620   ultimately show ?thesis by (simp add:cntCS_def)
  1545 qed 
  1621 qed 
  1546 
  1622 
       
  1623 context valid_trace
       
  1624 begin
       
  1625 
  1547 text {* (* ddd *) \noindent
  1626 text {* (* ddd *) \noindent
  1548   The relationship between @{text "cntP"}, @{text "cntV"} and @{text "cntCS"} 
  1627   The relationship between @{text "cntP"}, @{text "cntV"} and @{text "cntCS"} 
  1549   of one particular thread. 
  1628   of one particular thread. 
  1550 *} 
  1629 *} 
  1551 
  1630 
  1552 lemma cnp_cnv_cncs:
  1631 lemma cnp_cnv_cncs:
  1553   fixes s th
       
  1554   assumes vt: "vt s"
       
  1555   shows "cntP s th = cntV s th + (if (th \<in> readys s \<or> th \<notin> threads s) 
  1632   shows "cntP s th = cntV s th + (if (th \<in> readys s \<or> th \<notin> threads s) 
  1556                                        then cntCS s th else cntCS s th + 1)"
  1633                                        then cntCS s th else cntCS s th + 1)"
  1557 proof -
  1634 proof -
  1558   from vt show ?thesis
  1635   from vt show ?thesis
  1559   proof(induct arbitrary:th)
  1636   proof(induct arbitrary:th)
  1560     case (vt_cons s e)
  1637     case (vt_cons s e)
       
  1638     interpret vt_s: valid_trace s using vt_cons(1) by (unfold_locales, simp)
  1561     assume vt: "vt s"
  1639     assume vt: "vt s"
  1562     and ih: "\<And>th. cntP s th  = cntV s th +
  1640     and ih: "\<And>th. cntP s th  = cntV s th +
  1563                (if (th \<in> readys s \<or> th \<notin> threads s) then cntCS s th else cntCS s th + 1)"
  1641                (if (th \<in> readys s \<or> th \<notin> threads s) then cntCS s th else cntCS s th + 1)"
  1564     and stp: "step s e"
  1642     and stp: "step s e"
  1565     from stp show ?case
  1643     from stp show ?case
  1569         and not_in: "thread \<notin> threads s"
  1647         and not_in: "thread \<notin> threads s"
  1570       show ?thesis
  1648       show ?thesis
  1571       proof -
  1649       proof -
  1572         { fix cs 
  1650         { fix cs 
  1573           assume "thread \<in> set (wq s cs)"
  1651           assume "thread \<in> set (wq s cs)"
  1574           from wq_threads [OF vt this] have "thread \<in> threads s" .
  1652           from vt_s.wq_threads [OF this] have "thread \<in> threads s" .
  1575           with not_in have "False" by simp
  1653           with not_in have "False" by simp
  1576         } with eq_e have eq_readys: "readys (e#s) = readys s \<union> {thread}"
  1654         } with eq_e have eq_readys: "readys (e#s) = readys s \<union> {thread}"
  1577           by (auto simp:readys_def threads.simps s_waiting_def 
  1655           by (auto simp:readys_def threads.simps s_waiting_def 
  1578             wq_def cs_waiting_def Let_def)
  1656             wq_def cs_waiting_def Let_def)
  1579         from eq_e have eq_cnp: "cntP (e#s) th = cntP s th" by (simp add:cntP_def count_def)
  1657         from eq_e have eq_cnp: "cntP (e#s) th = cntP s th" by (simp add:cntP_def count_def)
  1630       case (thread_P thread cs)
  1708       case (thread_P thread cs)
  1631       assume eq_e: "e = P thread cs"
  1709       assume eq_e: "e = P thread cs"
  1632         and is_runing: "thread \<in> runing s"
  1710         and is_runing: "thread \<in> runing s"
  1633         and no_dep: "(Cs cs, Th thread) \<notin> (RAG s)\<^sup>+"
  1711         and no_dep: "(Cs cs, Th thread) \<notin> (RAG s)\<^sup>+"
  1634       from thread_P vt stp ih  have vtp: "vt (P thread cs#s)" by auto
  1712       from thread_P vt stp ih  have vtp: "vt (P thread cs#s)" by auto
       
  1713       then interpret vt_p: valid_trace "(P thread cs#s)"
       
  1714         by (unfold_locales, simp)
  1635       show ?thesis 
  1715       show ?thesis 
  1636       proof -
  1716       proof -
  1637         { have hh: "\<And> A B C. (B = C) \<Longrightarrow> (A \<and> B) = (A \<and> C)" by blast
  1717         { have hh: "\<And> A B C. (B = C) \<Longrightarrow> (A \<and> B) = (A \<and> C)" by blast
  1638           assume neq_th: "th \<noteq> thread"
  1718           assume neq_th: "th \<noteq> thread"
  1639           with eq_e
  1719           with eq_e
  1677                   Suc (card {cs. (Cs cs, Th thread) \<in> RAG s})" (is "card ?L = Suc (card ?R)")
  1757                   Suc (card {cs. (Cs cs, Th thread) \<in> RAG s})" (is "card ?L = Suc (card ?R)")
  1678                 proof -
  1758                 proof -
  1679                   have "?L = insert cs ?R" by auto
  1759                   have "?L = insert cs ?R" by auto
  1680                   moreover have "card \<dots> = Suc (card (?R - {cs}))" 
  1760                   moreover have "card \<dots> = Suc (card (?R - {cs}))" 
  1681                   proof(rule card_insert)
  1761                   proof(rule card_insert)
  1682                     from finite_holding [OF vt, of thread]
  1762                     from vt_s.finite_holding [of thread]
  1683                     show " finite {cs. (Cs cs, Th thread) \<in> RAG s}"
  1763                     show " finite {cs. (Cs cs, Th thread) \<in> RAG s}"
  1684                       by (unfold holdents_test, simp)
  1764                       by (unfold holdents_test, simp)
  1685                   qed
  1765                   qed
  1686                   moreover have "?R - {cs} = ?R"
  1766                   moreover have "?R - {cs} = ?R"
  1687                   proof -
  1767                   proof -
  1716                   by (simp add:s_waiting_def wq_def)
  1796                   by (simp add:s_waiting_def wq_def)
  1717                 moreover from eq_wq have "th \<in> set (wq (e#s) cs)" by auto
  1797                 moreover from eq_wq have "th \<in> set (wq (e#s) cs)" by auto
  1718                 ultimately have "th = hd (wq (e#s) cs)" by blast
  1798                 ultimately have "th = hd (wq (e#s) cs)" by blast
  1719                 with eq_wq have "th = hd (wq s cs @ [th])" by simp
  1799                 with eq_wq have "th = hd (wq s cs @ [th])" by simp
  1720                 hence "th = hd (wq s cs)" using False by auto
  1800                 hence "th = hd (wq s cs)" using False by auto
  1721                 with False eq_wq wq_distinct [OF vtp, of cs]
  1801                 with False eq_wq vt_p.wq_distinct [of cs]
  1722                 show False by (fold eq_e, auto)
  1802                 show False by (fold eq_e, auto)
  1723               qed
  1803               qed
  1724               moreover from is_runing have "th \<in> threads (e#s)" 
  1804               moreover from is_runing have "th \<in> threads (e#s)" 
  1725                 by (unfold eq_e, auto simp:runing_def readys_def eq_th)
  1805                 by (unfold eq_e, auto simp:runing_def readys_def eq_th)
  1726               moreover have "cntCS (e # s) th = cntCS s th"
  1806               moreover have "cntCS (e # s) th = cntCS s th"
  1735         } ultimately show ?thesis by blast
  1815         } ultimately show ?thesis by blast
  1736       qed
  1816       qed
  1737     next
  1817     next
  1738       case (thread_V thread cs)
  1818       case (thread_V thread cs)
  1739       from assms vt stp ih thread_V have vtv: "vt (V thread cs # s)" by auto
  1819       from assms vt stp ih thread_V have vtv: "vt (V thread cs # s)" by auto
       
  1820       then interpret vt_v: valid_trace "(V thread cs # s)" by (unfold_locales, simp)
  1740       assume eq_e: "e = V thread cs"
  1821       assume eq_e: "e = V thread cs"
  1741         and is_runing: "thread \<in> runing s"
  1822         and is_runing: "thread \<in> runing s"
  1742         and hold: "holding s thread cs"
  1823         and hold: "holding s thread cs"
  1743       from hold obtain rest 
  1824       from hold obtain rest 
  1744         where eq_wq: "wq s cs = thread # rest"
  1825         where eq_wq: "wq s cs = thread # rest"
  1745         by (case_tac "wq s cs", auto simp: wq_def s_holding_def)
  1826         by (case_tac "wq s cs", auto simp: wq_def s_holding_def)
  1746       have eq_threads: "threads (e#s) = threads s" by (simp add: eq_e)
  1827       have eq_threads: "threads (e#s) = threads s" by (simp add: eq_e)
  1747       have eq_set: "set (SOME q. distinct q \<and> set q = set rest) = set rest"
  1828       have eq_set: "set (SOME q. distinct q \<and> set q = set rest) = set rest"
  1748       proof(rule someI2)
  1829       proof(rule someI2)
  1749         from wq_distinct[OF step_back_vt[OF vtv], of cs] and eq_wq
  1830         from vt_v.wq_distinct[of cs] and eq_wq
  1750         show "distinct rest \<and> set rest = set rest" by auto
  1831         show "distinct rest \<and> set rest = set rest"
       
  1832           by (metis distinct.simps(2) vt_s.wq_distinct)
  1751       next
  1833       next
  1752         show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest"
  1834         show "\<And>x. distinct x \<and> set x = set rest \<Longrightarrow> set x = set rest"
  1753           by auto
  1835           by auto
  1754       qed
  1836       qed
  1755       show ?thesis
  1837       show ?thesis
  1780                     apply(unfold eq_e wq_def eq_cs s_holding_def)
  1862                     apply(unfold eq_e wq_def eq_cs s_holding_def)
  1781                     apply (auto simp:Let_def)
  1863                     apply (auto simp:Let_def)
  1782                   proof -
  1864                   proof -
  1783                     assume "thread \<in> set (SOME q. distinct q \<and> set q = set rest)"
  1865                     assume "thread \<in> set (SOME q. distinct q \<and> set q = set rest)"
  1784                     with eq_set have "thread \<in> set rest" by simp
  1866                     with eq_set have "thread \<in> set rest" by simp
  1785                     with wq_distinct[OF step_back_vt[OF vtv], of cs]
  1867                     with vt_v.wq_distinct[of cs]
  1786                     and eq_wq show False by auto
  1868                     and eq_wq show False
       
  1869                         by (metis distinct.simps(2) vt_s.wq_distinct)
  1787                   qed
  1870                   qed
  1788                   thus ?thesis by (simp add:wq_def s_waiting_def)
  1871                   thus ?thesis by (simp add:wq_def s_waiting_def)
  1789                 qed
  1872                 qed
  1790               } moreover {
  1873               } moreover {
  1791                 assume neq_cs: "cs1 \<noteq> cs"
  1874                 assume neq_cs: "cs1 \<noteq> cs"
  1817           have ?thesis
  1900           have ?thesis
  1818           proof(cases "th \<in> set rest")
  1901           proof(cases "th \<in> set rest")
  1819             case False
  1902             case False
  1820             have "(th \<in> readys (e # s)) = (th \<in> readys s)"
  1903             have "(th \<in> readys (e # s)) = (th \<in> readys s)"
  1821               apply (insert step_back_vt[OF vtv])
  1904               apply (insert step_back_vt[OF vtv])
  1822               by (unfold eq_e, rule readys_v_eq [OF _ neq_th eq_wq False], auto)
  1905               by (simp add: False eq_e eq_wq neq_th vt_s.readys_v_eq)
  1823             moreover have "cntCS (e#s) th = cntCS s th"
  1906             moreover have "cntCS (e#s) th = cntCS s th"
  1824               apply (insert neq_th, unfold eq_e cntCS_def holdents_test step_RAG_v[OF vtv], auto)
  1907               apply (insert neq_th, unfold eq_e cntCS_def holdents_test step_RAG_v[OF vtv], auto)
  1825               proof -
  1908               proof -
  1826                 have "{csa. (Cs csa, Th th) \<in> RAG s \<or> csa = cs \<and> next_th s thread cs th} =
  1909                 have "{csa. (Cs csa, Th th) \<in> RAG s \<or> csa = cs \<and> next_th s thread cs th} =
  1827                       {cs. (Cs cs, Th th) \<in> RAG s}"
  1910                       {cs. (Cs cs, Th th) \<in> RAG s}"
  1836                     from eq_set ni have "hd (SOME q. distinct q \<and> set q = set rest) \<notin> 
  1919                     from eq_set ni have "hd (SOME q. distinct q \<and> set q = set rest) \<notin> 
  1837                                   set (SOME q. distinct q \<and> set q = set rest)
  1920                                   set (SOME q. distinct q \<and> set q = set rest)
  1838                                   " by simp
  1921                                   " by simp
  1839                     moreover have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
  1922                     moreover have "(SOME q. distinct q \<and> set q = set rest) \<noteq> []"
  1840                     proof(rule someI2)
  1923                     proof(rule someI2)
  1841                       from wq_distinct[OF step_back_vt[OF vtv], of cs] and eq_wq
  1924                       from vt_s.wq_distinct[ of cs] and eq_wq
  1842                       show "distinct rest \<and> set rest = set rest" by auto
  1925                       show "distinct rest \<and> set rest = set rest" by auto
  1843                     next
  1926                     next
  1844                       fix x assume "distinct x \<and> set x = set rest"
  1927                       fix x assume "distinct x \<and> set x = set rest"
  1845                       with ne show "x \<noteq> []" by auto
  1928                       with ne show "x \<noteq> []" by auto
  1846                     qed
  1929                     qed
  1868               proof -
  1951               proof -
  1869                 from eq_wq and th_in
  1952                 from eq_wq and th_in
  1870                 have "\<not> th \<in> readys s"
  1953                 have "\<not> th \<in> readys s"
  1871                   apply (auto simp:readys_def s_waiting_def)
  1954                   apply (auto simp:readys_def s_waiting_def)
  1872                   apply (rule_tac x = cs in exI, auto)
  1955                   apply (rule_tac x = cs in exI, auto)
  1873                   by (insert wq_distinct[OF step_back_vt[OF vtv], of cs], auto simp add: wq_def)
  1956                   by (insert vt_s.wq_distinct[of cs], auto simp add: wq_def)
  1874                 moreover 
  1957                 moreover 
  1875                 from eq_wq and th_in and neq_hd
  1958                 from eq_wq and th_in and neq_hd
  1876                 have "\<not> (th \<in> readys (e # s))"
  1959                 have "\<not> (th \<in> readys (e # s))"
  1877                   apply (auto simp:readys_def s_waiting_def eq_e wq_def Let_def split:list.splits)
  1960                   apply (auto simp:readys_def s_waiting_def eq_e wq_def Let_def split:list.splits)
  1878                   by (rule_tac x = cs in exI, auto simp:eq_set)
  1961                   by (rule_tac x = cs in exI, auto simp:eq_set)
  1883                 from eq_wq and  th_in and neq_hd
  1966                 from eq_wq and  th_in and neq_hd
  1884                 have "(holdents (e # s) th) = (holdents s th)"
  1967                 have "(holdents (e # s) th) = (holdents s th)"
  1885                   apply (unfold eq_e step_RAG_v[OF vtv], 
  1968                   apply (unfold eq_e step_RAG_v[OF vtv], 
  1886                          auto simp:next_th_def eq_set s_RAG_def holdents_test wq_def
  1969                          auto simp:next_th_def eq_set s_RAG_def holdents_test wq_def
  1887                                    Let_def cs_holding_def)
  1970                                    Let_def cs_holding_def)
  1888                   by (insert wq_distinct[OF step_back_vt[OF vtv], of cs], auto simp:wq_def)
  1971                   by (insert vt_s.wq_distinct[of cs], auto simp:wq_def)
  1889                 thus ?thesis by (simp add:cntCS_def)
  1972                 thus ?thesis by (simp add:cntCS_def)
  1890               qed
  1973               qed
  1891               moreover note ih eq_cnp eq_cnv eq_threads
  1974               moreover note ih eq_cnp eq_cnv eq_threads
  1892               ultimately show ?thesis by auto
  1975               ultimately show ?thesis by auto
  1893             next
  1976             next
  1900                         Let_def next_th_def)
  1983                         Let_def next_th_def)
  1901               proof -
  1984               proof -
  1902                 assume eq_wq: "wq_fun (schs s) cs = thread # rest"
  1985                 assume eq_wq: "wq_fun (schs s) cs = thread # rest"
  1903                   and t_in: "?t \<in> set rest"
  1986                   and t_in: "?t \<in> set rest"
  1904                 show "?t \<in> threads s"
  1987                 show "?t \<in> threads s"
  1905                 proof(rule wq_threads[OF step_back_vt[OF vtv]])
  1988                 proof(rule vt_s.wq_threads)
  1906                   from eq_wq and t_in
  1989                   from eq_wq and t_in
  1907                   show "?t \<in> set (wq s cs)" by (auto simp:wq_def)
  1990                   show "?t \<in> set (wq s cs)" by (auto simp:wq_def)
  1908                 qed
  1991                 qed
  1909               next
  1992               next
  1910                 fix csa
  1993                 fix csa
  1913                   and neq_cs: "csa \<noteq> cs"
  1996                   and neq_cs: "csa \<noteq> cs"
  1914                   and t_in': "?t \<in>  set (wq_fun (schs s) csa)"
  1997                   and t_in': "?t \<in>  set (wq_fun (schs s) csa)"
  1915                 show "?t = hd (wq_fun (schs s) csa)"
  1998                 show "?t = hd (wq_fun (schs s) csa)"
  1916                 proof -
  1999                 proof -
  1917                   { assume neq_hd': "?t \<noteq> hd (wq_fun (schs s) csa)"
  2000                   { assume neq_hd': "?t \<noteq> hd (wq_fun (schs s) csa)"
  1918                     from wq_distinct[OF step_back_vt[OF vtv], of cs] and 
  2001                     from vt_s.wq_distinct[of cs] and 
  1919                     eq_wq[folded wq_def] and t_in eq_wq
  2002                     eq_wq[folded wq_def] and t_in eq_wq
  1920                     have "?t \<noteq> thread" by auto
  2003                     have "?t \<noteq> thread" by auto
  1921                     with eq_wq and t_in
  2004                     with eq_wq and t_in
  1922                     have w1: "waiting s ?t cs"
  2005                     have w1: "waiting s ?t cs"
  1923                       by (auto simp:s_waiting_def wq_def)
  2006                       by (auto simp:s_waiting_def wq_def)
  1924                     from t_in' neq_hd'
  2007                     from t_in' neq_hd'
  1925                     have w2: "waiting s ?t csa"
  2008                     have w2: "waiting s ?t csa"
  1926                       by (auto simp:s_waiting_def wq_def)
  2009                       by (auto simp:s_waiting_def wq_def)
  1927                     from waiting_unique[OF step_back_vt[OF vtv] w1 w2]
  2010                     from vt_s.waiting_unique[OF w1 w2]
  1928                     and neq_cs have "False" by auto
  2011                     and neq_cs have "False" by auto
  1929                   } thus ?thesis by auto
  2012                   } thus ?thesis by auto
  1930                 qed
  2013                 qed
  1931               qed
  2014               qed
  1932               moreover have "cntP s th = cntV s th + cntCS s th + 1"
  2015               moreover have "cntP s th = cntV s th + cntCS s th + 1"
  1940                 qed
  2023                 qed
  1941                 moreover have "th \<in> threads s"
  2024                 moreover have "th \<in> threads s"
  1942                 proof -
  2025                 proof -
  1943                   from th_in eq_wq
  2026                   from th_in eq_wq
  1944                   have "th \<in> set (wq s cs)" by simp
  2027                   have "th \<in> set (wq s cs)" by simp
  1945                   from wq_threads [OF step_back_vt[OF vtv] this] 
  2028                   from vt_s.wq_threads [OF this] 
  1946                   show ?thesis .
  2029                   show ?thesis .
  1947                 qed
  2030                 qed
  1948                 ultimately show ?thesis using ih by auto
  2031                 ultimately show ?thesis using ih by auto
  1949               qed
  2032               qed
  1950               moreover from True neq_th have "cntCS (e # s) th = 1 + cntCS s th"
  2033               moreover from True neq_th have "cntCS (e # s) th = 1 + cntCS s th"
  1959                   also have "\<dots> = Suc (card ?B)"
  2042                   also have "\<dots> = Suc (card ?B)"
  1960                   proof(rule card_insert_disjoint)
  2043                   proof(rule card_insert_disjoint)
  1961                     have "?B \<subseteq> ((\<lambda> (x, y). the_cs x) ` RAG s)" 
  2044                     have "?B \<subseteq> ((\<lambda> (x, y). the_cs x) ` RAG s)" 
  1962                       apply (auto simp:image_def)
  2045                       apply (auto simp:image_def)
  1963                       by (rule_tac x = "(Cs x, Th th)" in bexI, auto)
  2046                       by (rule_tac x = "(Cs x, Th th)" in bexI, auto)
  1964                     with finite_RAG[OF step_back_vt[OF vtv]]
  2047                     with vt_s.finite_RAG
  1965                     show "finite {cs. (Cs cs, Th th) \<in> RAG s}" by (auto intro:finite_subset)
  2048                     show "finite {cs. (Cs cs, Th th) \<in> RAG s}" by (auto intro:finite_subset)
  1966                   next
  2049                   next
  1967                     show "cs \<notin> {cs. (Cs cs, Th th) \<in> RAG s}"
  2050                     show "cs \<notin> {cs. (Cs cs, Th th) \<in> RAG s}"
  1968                     proof
  2051                     proof
  1969                       assume "cs \<in> {cs. (Cs cs, Th th) \<in> RAG s}"
  2052                       assume "cs \<in> {cs. (Cs cs, Th th) \<in> RAG s}"
  2020         auto simp:count_def holdents_test s_RAG_def wq_def cs_holding_def)
  2103         auto simp:count_def holdents_test s_RAG_def wq_def cs_holding_def)
  2021   qed
  2104   qed
  2022 qed
  2105 qed
  2023 
  2106 
  2024 lemma not_thread_cncs:
  2107 lemma not_thread_cncs:
  2025   fixes th s
  2108   assumes not_in: "th \<notin> threads s" 
  2026   assumes vt: "vt s"
       
  2027   and not_in: "th \<notin> threads s" 
       
  2028   shows "cntCS s th = 0"
  2109   shows "cntCS s th = 0"
  2029 proof -
  2110 proof -
  2030   from vt not_in show ?thesis
  2111   from vt not_in show ?thesis
  2031   proof(induct arbitrary:th)
  2112   proof(induct arbitrary:th)
  2032     case (vt_cons s e th)
  2113     case (vt_cons s e th)
       
  2114     interpret vt_s: valid_trace s using vt_cons(1)
       
  2115        by (unfold_locales, simp)
  2033     assume vt: "vt s"
  2116     assume vt: "vt s"
  2034       and ih: "\<And>th. th \<notin> threads s \<Longrightarrow> cntCS s th = 0"
  2117       and ih: "\<And>th. th \<notin> threads s \<Longrightarrow> cntCS s th = 0"
  2035       and stp: "step s e"
  2118       and stp: "step s e"
  2036       and not_in: "th \<notin> threads (e # s)"
  2119       and not_in: "th \<notin> threads (e # s)"
  2037     from stp show ?case
  2120     from stp show ?case
  2095         from not_in eq_e have "th \<notin> threads s" by simp
  2178         from not_in eq_e have "th \<notin> threads s" by simp
  2096         moreover from is_runing have "thread \<in> threads s"
  2179         moreover from is_runing have "thread \<in> threads s"
  2097           by (simp add:runing_def readys_def)
  2180           by (simp add:runing_def readys_def)
  2098         ultimately show ?thesis by auto
  2181         ultimately show ?thesis by auto
  2099       qed
  2182       qed
  2100       from assms thread_V vt stp ih have vtv: "vt (V thread cs#s)" by auto
  2183       from assms thread_V vt stp ih 
       
  2184       have vtv: "vt (V thread cs#s)" by auto
       
  2185       then interpret vt_v: valid_trace "(V thread cs#s)"
       
  2186         by (unfold_locales, simp)
  2101       from hold obtain rest 
  2187       from hold obtain rest 
  2102         where eq_wq: "wq s cs = thread # rest"
  2188         where eq_wq: "wq s cs = thread # rest"
  2103         by (case_tac "wq s cs", auto simp: wq_def s_holding_def)
  2189         by (case_tac "wq s cs", auto simp: wq_def s_holding_def)
  2104       from not_in eq_e eq_wq
  2190       from not_in eq_e eq_wq
  2105       have "\<not> next_th s thread cs th"
  2191       have "\<not> next_th s thread cs th"
  2107       proof -
  2193       proof -
  2108         assume ne: "rest \<noteq> []"
  2194         assume ne: "rest \<noteq> []"
  2109           and ni: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> threads s" (is "?t \<notin> threads s")
  2195           and ni: "hd (SOME q. distinct q \<and> set q = set rest) \<notin> threads s" (is "?t \<notin> threads s")
  2110         have "?t \<in> set rest"
  2196         have "?t \<in> set rest"
  2111         proof(rule someI2)
  2197         proof(rule someI2)
  2112           from wq_distinct[OF step_back_vt[OF vtv], of cs] and eq_wq
  2198           from vt_v.wq_distinct[of cs] and eq_wq
  2113           show "distinct rest \<and> set rest = set rest" by auto
  2199           show "distinct rest \<and> set rest = set rest"
       
  2200             by (metis distinct.simps(2) vt_s.wq_distinct) 
  2114         next
  2201         next
  2115           fix x assume "distinct x \<and> set x = set rest" with ne
  2202           fix x assume "distinct x \<and> set x = set rest" with ne
  2116           show "hd x \<in> set rest" by (cases x, auto)
  2203           show "hd x \<in> set rest" by (cases x, auto)
  2117         qed
  2204         qed
  2118         with eq_wq have "?t \<in> set (wq s cs)" by simp
  2205         with eq_wq have "?t \<in> set (wq s cs)" by simp
  2119         from wq_threads[OF step_back_vt[OF vtv], OF this] and ni
  2206         from vt_s.wq_threads[OF this] and ni
  2120         show False by auto
  2207         show False
       
  2208           using `hd (SOME q. distinct q \<and> set q = set rest) \<in> set (wq s cs)` 
       
  2209             ni vt_s.wq_threads by blast 
  2121       qed
  2210       qed
  2122       moreover note neq_th eq_wq
  2211       moreover note neq_th eq_wq
  2123       ultimately have "cntCS (e # s) th  = cntCS s th"
  2212       ultimately have "cntCS (e # s) th  = cntCS s th"
  2124         by (unfold eq_e cntCS_def holdents_test step_RAG_v[OF vtv], auto)
  2213         by (unfold eq_e cntCS_def holdents_test step_RAG_v[OF vtv], auto)
  2125       moreover have "cntCS s th = 0"
  2214       moreover have "cntCS s th = 0"
  2144       by (unfold cntCS_def, 
  2233       by (unfold cntCS_def, 
  2145         auto simp:count_def holdents_test s_RAG_def wq_def cs_holding_def)
  2234         auto simp:count_def holdents_test s_RAG_def wq_def cs_holding_def)
  2146   qed
  2235   qed
  2147 qed
  2236 qed
  2148 
  2237 
       
  2238 end
       
  2239 
  2149 lemma eq_waiting: "waiting (wq (s::state)) th cs = waiting s th cs"
  2240 lemma eq_waiting: "waiting (wq (s::state)) th cs = waiting s th cs"
  2150   by (auto simp:s_waiting_def cs_waiting_def wq_def)
  2241   by (auto simp:s_waiting_def cs_waiting_def wq_def)
  2151 
  2242 
       
  2243 context valid_trace
       
  2244 begin
       
  2245 
  2152 lemma dm_RAG_threads:
  2246 lemma dm_RAG_threads:
  2153   fixes th s
  2247   assumes in_dom: "(Th th) \<in> Domain (RAG s)"
  2154   assumes vt: "vt s"
       
  2155   and in_dom: "(Th th) \<in> Domain (RAG s)"
       
  2156   shows "th \<in> threads s"
  2248   shows "th \<in> threads s"
  2157 proof -
  2249 proof -
  2158   from in_dom obtain n where "(Th th, n) \<in> RAG s" by auto
  2250   from in_dom obtain n where "(Th th, n) \<in> RAG s" by auto
  2159   moreover from RAG_target_th[OF this] obtain cs where "n = Cs cs" by auto
  2251   moreover from RAG_target_th[OF this] obtain cs where "n = Cs cs" by auto
  2160   ultimately have "(Th th, Cs cs) \<in> RAG s" by simp
  2252   ultimately have "(Th th, Cs cs) \<in> RAG s" by simp
  2161   hence "th \<in> set (wq s cs)"
  2253   hence "th \<in> set (wq s cs)"
  2162     by (unfold s_RAG_def, auto simp:cs_waiting_def)
  2254     by (unfold s_RAG_def, auto simp:cs_waiting_def)
  2163   from wq_threads [OF vt this] show ?thesis .
  2255   from wq_threads [OF this] show ?thesis .
  2164 qed
  2256 qed
       
  2257 
       
  2258 end
  2165 
  2259 
  2166 lemma cp_eq_cpreced: "cp s th = cpreced (wq s) s th"
  2260 lemma cp_eq_cpreced: "cp s th = cpreced (wq s) s th"
  2167 unfolding cp_def wq_def
  2261 unfolding cp_def wq_def
  2168 apply(induct s rule: schs.induct)
  2262 apply(induct s rule: schs.induct)
  2169 thm cpreced_initial
  2263 thm cpreced_initial
  2175 apply(simp add: Let_def)
  2269 apply(simp add: Let_def)
  2176 apply(subst (2) schs.simps)
  2270 apply(subst (2) schs.simps)
  2177 apply(simp add: Let_def)
  2271 apply(simp add: Let_def)
  2178 done
  2272 done
  2179 
  2273 
  2180 (* FIXME: NOT NEEDED *)
  2274 context valid_trace
       
  2275 begin
       
  2276 
  2181 lemma runing_unique:
  2277 lemma runing_unique:
  2182   fixes th1 th2 s
  2278   assumes runing_1: "th1 \<in> runing s"
  2183   assumes vt: "vt s"
       
  2184   and runing_1: "th1 \<in> runing s"
       
  2185   and runing_2: "th2 \<in> runing s"
  2279   and runing_2: "th2 \<in> runing s"
  2186   shows "th1 = th2"
  2280   shows "th1 = th2"
  2187 proof -
  2281 proof -
  2188   from runing_1 and runing_2 have "cp s th1 = cp s th2"
  2282   from runing_1 and runing_2 have "cp s th1 = cp s th2"
  2189     unfolding runing_def
  2283     unfolding runing_def
  2208             have "{th'. (Th th', Th th1) \<in> (RAG (wq s))\<^sup>+} \<subseteq> ?F ` ((RAG (wq s))\<^sup>+)"
  2302             have "{th'. (Th th', Th th1) \<in> (RAG (wq s))\<^sup>+} \<subseteq> ?F ` ((RAG (wq s))\<^sup>+)"
  2209               apply (auto simp:image_def)
  2303               apply (auto simp:image_def)
  2210               by (rule_tac x = "(Th x, Th th1)" in bexI, auto)
  2304               by (rule_tac x = "(Th x, Th th1)" in bexI, auto)
  2211             moreover have "finite \<dots>"
  2305             moreover have "finite \<dots>"
  2212             proof -
  2306             proof -
  2213               from finite_RAG[OF vt] have "finite (RAG s)" .
  2307               from finite_RAG have "finite (RAG s)" .
  2214               hence "finite ((RAG (wq s))\<^sup>+)"
  2308               hence "finite ((RAG (wq s))\<^sup>+)"
  2215                 apply (unfold finite_trancl)
  2309                 apply (unfold finite_trancl)
  2216                 by (auto simp: s_RAG_def cs_RAG_def wq_def)
  2310                 by (auto simp: s_RAG_def cs_RAG_def wq_def)
  2217               thus ?thesis by auto
  2311               thus ?thesis by auto
  2218             qed
  2312             qed
  2252             have "{th'. (Th th', Th th2) \<in> (RAG (wq s))\<^sup>+} \<subseteq> ?F ` ((RAG (wq s))\<^sup>+)"
  2346             have "{th'. (Th th', Th th2) \<in> (RAG (wq s))\<^sup>+} \<subseteq> ?F ` ((RAG (wq s))\<^sup>+)"
  2253               apply (auto simp:image_def)
  2347               apply (auto simp:image_def)
  2254               by (rule_tac x = "(Th x, Th th2)" in bexI, auto)
  2348               by (rule_tac x = "(Th x, Th th2)" in bexI, auto)
  2255             moreover have "finite \<dots>"
  2349             moreover have "finite \<dots>"
  2256             proof -
  2350             proof -
  2257               from finite_RAG[OF vt] have "finite (RAG s)" .
  2351               from finite_RAG have "finite (RAG s)" .
  2258               hence "finite ((RAG (wq s))\<^sup>+)"
  2352               hence "finite ((RAG (wq s))\<^sup>+)"
  2259                 apply (unfold finite_trancl)
  2353                 apply (unfold finite_trancl)
  2260                 by (auto simp: s_RAG_def cs_RAG_def wq_def)
  2354                 by (auto simp: s_RAG_def cs_RAG_def wq_def)
  2261               thus ?thesis by auto
  2355               thus ?thesis by auto
  2262             qed
  2356             qed
  2287       assume "th1' \<in> dependants (wq s) th1"
  2381       assume "th1' \<in> dependants (wq s) th1"
  2288       hence "(Th th1') \<in> Domain ((RAG s)^+)"
  2382       hence "(Th th1') \<in> Domain ((RAG s)^+)"
  2289         apply (unfold cs_dependants_def cs_RAG_def s_RAG_def)
  2383         apply (unfold cs_dependants_def cs_RAG_def s_RAG_def)
  2290         by (auto simp:Domain_def)
  2384         by (auto simp:Domain_def)
  2291       hence "(Th th1') \<in> Domain (RAG s)" by (simp add:trancl_domain)
  2385       hence "(Th th1') \<in> Domain (RAG s)" by (simp add:trancl_domain)
  2292       from dm_RAG_threads[OF vt this] show ?thesis .
  2386       from dm_RAG_threads[OF this] show ?thesis .
  2293     next
  2387     next
  2294       assume "th1' = th1"
  2388       assume "th1' = th1"
  2295       with runing_1 show ?thesis
  2389       with runing_1 show ?thesis
  2296         by (unfold runing_def readys_def, auto)
  2390         by (unfold runing_def readys_def, auto)
  2297     qed
  2391     qed
  2302       assume "th2' \<in> dependants (wq s) th2"
  2396       assume "th2' \<in> dependants (wq s) th2"
  2303       hence "(Th th2') \<in> Domain ((RAG s)^+)"
  2397       hence "(Th th2') \<in> Domain ((RAG s)^+)"
  2304         apply (unfold cs_dependants_def cs_RAG_def s_RAG_def)
  2398         apply (unfold cs_dependants_def cs_RAG_def s_RAG_def)
  2305         by (auto simp:Domain_def)
  2399         by (auto simp:Domain_def)
  2306       hence "(Th th2') \<in> Domain (RAG s)" by (simp add:trancl_domain)
  2400       hence "(Th th2') \<in> Domain (RAG s)" by (simp add:trancl_domain)
  2307       from dm_RAG_threads[OF vt this] show ?thesis .
  2401       from dm_RAG_threads[OF this] show ?thesis .
  2308     next
  2402     next
  2309       assume "th2' = th2"
  2403       assume "th2' = th2"
  2310       with runing_2 show ?thesis
  2404       with runing_2 show ?thesis
  2311         by (unfold runing_def readys_def, auto)
  2405         by (unfold runing_def readys_def, auto)
  2312     qed
  2406     qed
  2364       hence h1: "(Th th1', Th th2) \<in> (RAG s)^+"
  2458       hence h1: "(Th th1', Th th2) \<in> (RAG s)^+"
  2365         by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp)
  2459         by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp)
  2366       from th1'_in have h2: "(Th th1', Th th1) \<in> (RAG s)^+"
  2460       from th1'_in have h2: "(Th th1', Th th1) \<in> (RAG s)^+"
  2367         by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp)
  2461         by (unfold cs_dependants_def s_RAG_def cs_RAG_def, simp)
  2368       show ?thesis
  2462       show ?thesis
  2369       proof(rule dchain_unique[OF vt h1 _ h2, symmetric])
  2463       proof(rule dchain_unique[OF h1 _ h2, symmetric])
  2370         from runing_1 show "th1 \<in> readys s" by (simp add:runing_def)
  2464         from runing_1 show "th1 \<in> readys s" by (simp add:runing_def)
  2371         from runing_2 show "th2 \<in> readys s" by (simp add:runing_def) 
  2465         from runing_2 show "th2 \<in> readys s" by (simp add:runing_def) 
  2372       qed
  2466       qed
  2373     qed
  2467     qed
  2374   qed
  2468   qed
  2375 qed
  2469 qed
  2376 
  2470 
  2377 
  2471 
  2378 lemma "vt s \<Longrightarrow> card (runing s) \<le> 1"
  2472 lemma "card (runing s) \<le> 1"
  2379 apply(subgoal_tac "finite (runing s)")
  2473 apply(subgoal_tac "finite (runing s)")
  2380 prefer 2
  2474 prefer 2
  2381 apply (metis finite_nat_set_iff_bounded lessI runing_unique)
  2475 apply (metis finite_nat_set_iff_bounded lessI runing_unique)
  2382 apply(rule ccontr)
  2476 apply(rule ccontr)
  2383 apply(simp)
  2477 apply(simp)
  2387 apply(auto)[1]
  2481 apply(auto)[1]
  2388 apply (metis insertCI runing_unique)
  2482 apply (metis insertCI runing_unique)
  2389 apply(auto) 
  2483 apply(auto) 
  2390 done
  2484 done
  2391 
  2485 
       
  2486 end
       
  2487 
       
  2488 
  2392 lemma create_pre:
  2489 lemma create_pre:
  2393   assumes stp: "step s e"
  2490   assumes stp: "step s e"
  2394   and not_in: "th \<notin> threads s"
  2491   and not_in: "th \<notin> threads s"
  2395   and is_in: "th \<in> threads (e#s)"
  2492   and is_in: "th \<in> threads (e#s)"
  2396   obtains prio where "e = Create th prio"
  2493   obtains prio where "e = Create th prio"
  2445   ultimately have eq_me: "moment (Suc i) t = e#(moment i t)"
  2542   ultimately have eq_me: "moment (Suc i) t = e#(moment i t)"
  2446     by (auto simp:down_to_moment)
  2543     by (auto simp:down_to_moment)
  2447   from that [OF this] show ?thesis .
  2544   from that [OF this] show ?thesis .
  2448 qed
  2545 qed
  2449 
  2546 
       
  2547 context valid_trace
       
  2548 begin
       
  2549 
  2450 lemma cnp_cnv_eq:
  2550 lemma cnp_cnv_eq:
  2451   fixes th s
  2551   assumes "th \<notin> threads s"
  2452   assumes "vt s"
       
  2453   and "th \<notin> threads s"
       
  2454   shows "cntP s th = cntV s th"
  2552   shows "cntP s th = cntV s th"
  2455  by (simp add: assms(1) assms(2) cnp_cnv_cncs not_thread_cncs)
  2553   using assms
       
  2554   using cnp_cnv_cncs not_thread_cncs by auto
       
  2555 
       
  2556 end
       
  2557 
  2456 
  2558 
  2457 lemma eq_RAG: 
  2559 lemma eq_RAG: 
  2458   "RAG (wq s) = RAG s"
  2560   "RAG (wq s) = RAG s"
  2459 by (unfold cs_RAG_def s_RAG_def, auto)
  2561 by (unfold cs_RAG_def s_RAG_def, auto)
  2460 
  2562 
       
  2563 context valid_trace
       
  2564 begin
       
  2565 
  2461 lemma count_eq_dependants:
  2566 lemma count_eq_dependants:
  2462   assumes vt: "vt s"
  2567   assumes eq_pv: "cntP s th = cntV s th"
  2463   and eq_pv: "cntP s th = cntV s th"
       
  2464   shows "dependants (wq s) th = {}"
  2568   shows "dependants (wq s) th = {}"
  2465 proof -
  2569 proof -
  2466   from cnp_cnv_cncs[OF vt] and eq_pv
  2570   from cnp_cnv_cncs and eq_pv
  2467   have "cntCS s th = 0" 
  2571   have "cntCS s th = 0" 
  2468     by (auto split:if_splits)
  2572     by (auto split:if_splits)
  2469   moreover have "finite {cs. (Cs cs, Th th) \<in> RAG s}"
  2573   moreover have "finite {cs. (Cs cs, Th th) \<in> RAG s}"
  2470   proof -
  2574   proof -
  2471     from finite_holding[OF vt, of th] show ?thesis
  2575     from finite_holding[of th] show ?thesis
  2472       by (simp add:holdents_test)
  2576       by (simp add:holdents_test)
  2473   qed
  2577   qed
  2474   ultimately have h: "{cs. (Cs cs, Th th) \<in> RAG s} = {}"
  2578   ultimately have h: "{cs. (Cs cs, Th th) \<in> RAG s} = {}"
  2475     by (unfold cntCS_def holdents_test cs_dependants_def, auto)
  2579     by (unfold cntCS_def holdents_test cs_dependants_def, auto)
  2476   show ?thesis
  2580   show ?thesis
  2490     } thus "{th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} = {}" by auto
  2594     } thus "{th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} = {}" by auto
  2491   qed
  2595   qed
  2492 qed
  2596 qed
  2493 
  2597 
  2494 lemma dependants_threads:
  2598 lemma dependants_threads:
  2495   fixes s th
       
  2496   assumes vt: "vt s"
       
  2497   shows "dependants (wq s) th \<subseteq> threads s"
  2599   shows "dependants (wq s) th \<subseteq> threads s"
  2498 proof
  2600 proof
  2499   { fix th th'
  2601   { fix th th'
  2500     assume h: "th \<in> {th'a. (Th th'a, Th th') \<in> (RAG (wq s))\<^sup>+}"
  2602     assume h: "th \<in> {th'a. (Th th'a, Th th') \<in> (RAG (wq s))\<^sup>+}"
  2501     have "Th th \<in> Domain (RAG s)"
  2603     have "Th th \<in> Domain (RAG s)"
  2503       from h obtain th' where "(Th th, Th th') \<in> (RAG (wq s))\<^sup>+" by auto
  2605       from h obtain th' where "(Th th, Th th') \<in> (RAG (wq s))\<^sup>+" by auto
  2504       hence "(Th th) \<in> Domain ( (RAG (wq s))\<^sup>+)" by (auto simp:Domain_def)
  2606       hence "(Th th) \<in> Domain ( (RAG (wq s))\<^sup>+)" by (auto simp:Domain_def)
  2505       with trancl_domain have "(Th th) \<in> Domain (RAG (wq s))" by simp
  2607       with trancl_domain have "(Th th) \<in> Domain (RAG (wq s))" by simp
  2506       thus ?thesis using eq_RAG by simp
  2608       thus ?thesis using eq_RAG by simp
  2507     qed
  2609     qed
  2508     from dm_RAG_threads[OF vt this]
  2610     from dm_RAG_threads[OF this]
  2509     have "th \<in> threads s" .
  2611     have "th \<in> threads s" .
  2510   } note hh = this
  2612   } note hh = this
  2511   fix th1 
  2613   fix th1 
  2512   assume "th1 \<in> dependants (wq s) th"
  2614   assume "th1 \<in> dependants (wq s) th"
  2513   hence "th1 \<in> {th'a. (Th th'a, Th th) \<in> (RAG (wq s))\<^sup>+}"
  2615   hence "th1 \<in> {th'a. (Th th'a, Th th) \<in> (RAG (wq s))\<^sup>+}"
  2514     by (unfold cs_dependants_def, simp)
  2616     by (unfold cs_dependants_def, simp)
  2515   from hh [OF this] show "th1 \<in> threads s" .
  2617   from hh [OF this] show "th1 \<in> threads s" .
  2516 qed
  2618 qed
  2517 
  2619 
  2518 lemma finite_threads:
  2620 lemma finite_threads:
  2519   assumes vt: "vt s"
       
  2520   shows "finite (threads s)"
  2621   shows "finite (threads s)"
  2521 using vt
  2622 using vt by (induct) (auto elim: step.cases)
  2522 by (induct) (auto elim: step.cases)
  2623 
       
  2624 end
  2523 
  2625 
  2524 lemma Max_f_mono:
  2626 lemma Max_f_mono:
  2525   assumes seq: "A \<subseteq> B"
  2627   assumes seq: "A \<subseteq> B"
  2526   and np: "A \<noteq> {}"
  2628   and np: "A \<noteq> {}"
  2527   and fnt: "finite B"
  2629   and fnt: "finite B"
  2532   from np show "f ` A \<noteq> {}" by auto
  2634   from np show "f ` A \<noteq> {}" by auto
  2533 next
  2635 next
  2534   from fnt and seq show "finite (f ` B)" by auto
  2636   from fnt and seq show "finite (f ` B)" by auto
  2535 qed
  2637 qed
  2536 
  2638 
       
  2639 context valid_trace
       
  2640 begin
       
  2641 
  2537 lemma cp_le:
  2642 lemma cp_le:
  2538   assumes vt: "vt s"
  2643   assumes th_in: "th \<in> threads s"
  2539   and th_in: "th \<in> threads s"
       
  2540   shows "cp s th \<le> Max ((\<lambda> th. (preced th s)) ` threads s)"
  2644   shows "cp s th \<le> Max ((\<lambda> th. (preced th s)) ` threads s)"
  2541 proof(unfold cp_eq_cpreced cpreced_def cs_dependants_def)
  2645 proof(unfold cp_eq_cpreced cpreced_def cs_dependants_def)
  2542   show "Max ((\<lambda>th. preced th s) ` ({th} \<union> {th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+}))
  2646   show "Max ((\<lambda>th. preced th s) ` ({th} \<union> {th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+}))
  2543          \<le> Max ((\<lambda>th. preced th s) ` threads s)"
  2647          \<le> Max ((\<lambda>th. preced th s) ` threads s)"
  2544     (is "Max (?f ` ?A) \<le> Max (?f ` ?B)")
  2648     (is "Max (?f ` ?A) \<le> Max (?f ` ?B)")
  2545   proof(rule Max_f_mono)
  2649   proof(rule Max_f_mono)
  2546     show "{th} \<union> {th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} \<noteq> {}" by simp
  2650     show "{th} \<union> {th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} \<noteq> {}" by simp
  2547   next
  2651   next
  2548     from finite_threads [OF vt]
  2652     from finite_threads
  2549     show "finite (threads s)" .
  2653     show "finite (threads s)" .
  2550   next
  2654   next
  2551     from th_in
  2655     from th_in
  2552     show "{th} \<union> {th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} \<subseteq> threads s"
  2656     show "{th} \<union> {th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} \<subseteq> threads s"
  2553       apply (auto simp:Domain_def)
  2657       apply (auto simp:Domain_def)
  2554       apply (rule_tac dm_RAG_threads[OF vt])
  2658       apply (rule_tac dm_RAG_threads)
  2555       apply (unfold trancl_domain [of "RAG s", symmetric])
  2659       apply (unfold trancl_domain [of "RAG s", symmetric])
  2556       by (unfold cs_RAG_def s_RAG_def, auto simp:Domain_def)
  2660       by (unfold cs_RAG_def s_RAG_def, auto simp:Domain_def)
  2557   qed
  2661   qed
  2558 qed
  2662 qed
  2559 
  2663 
  2560 lemma le_cp:
  2664 lemma le_cp:
  2561   assumes vt: "vt s"
       
  2562   shows "preced th s \<le> cp s th"
  2665   shows "preced th s \<le> cp s th"
  2563 proof(unfold cp_eq_cpreced preced_def cpreced_def, simp)
  2666 proof(unfold cp_eq_cpreced preced_def cpreced_def, simp)
  2564   show "Prc (priority th s) (last_set th s)
  2667   show "Prc (priority th s) (last_set th s)
  2565     \<le> Max (insert (Prc (priority th s) (last_set th s))
  2668     \<le> Max (insert (Prc (priority th s) (last_set th s))
  2566             ((\<lambda>th. Prc (priority th s) (last_set th s)) ` dependants (wq s) th))"
  2669             ((\<lambda>th. Prc (priority th s) (last_set th s)) ` dependants (wq s) th))"
  2577           have "{th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} \<subseteq> ?F ` ((RAG (wq s))\<^sup>+)"
  2680           have "{th'. (Th th', Th th) \<in> (RAG (wq s))\<^sup>+} \<subseteq> ?F ` ((RAG (wq s))\<^sup>+)"
  2578             apply (auto simp:image_def)
  2681             apply (auto simp:image_def)
  2579             by (rule_tac x = "(Th x, Th th)" in bexI, auto)
  2682             by (rule_tac x = "(Th x, Th th)" in bexI, auto)
  2580           moreover have "finite \<dots>"
  2683           moreover have "finite \<dots>"
  2581           proof -
  2684           proof -
  2582             from finite_RAG[OF vt] have "finite (RAG s)" .
  2685             from finite_RAG have "finite (RAG s)" .
  2583             hence "finite ((RAG (wq s))\<^sup>+)"
  2686             hence "finite ((RAG (wq s))\<^sup>+)"
  2584               apply (unfold finite_trancl)
  2687               apply (unfold finite_trancl)
  2585               by (auto simp: s_RAG_def cs_RAG_def wq_def)
  2688               by (auto simp: s_RAG_def cs_RAG_def wq_def)
  2586             thus ?thesis by auto
  2689             thus ?thesis by auto
  2587           qed
  2690           qed
  2597     thus ?thesis by auto
  2700     thus ?thesis by auto
  2598   qed
  2701   qed
  2599 qed
  2702 qed
  2600 
  2703 
  2601 lemma max_cp_eq: 
  2704 lemma max_cp_eq: 
  2602   assumes vt: "vt s"
       
  2603   shows "Max ((cp s) ` threads s) = Max ((\<lambda> th. (preced th s)) ` threads s)"
  2705   shows "Max ((cp s) ` threads s) = Max ((\<lambda> th. (preced th s)) ` threads s)"
  2604   (is "?l = ?r")
  2706   (is "?l = ?r")
  2605 proof(cases "threads s = {}")
  2707 proof(cases "threads s = {}")
  2606   case True
  2708   case True
  2607   thus ?thesis by auto
  2709   thus ?thesis by auto
  2608 next
  2710 next
  2609   case False
  2711   case False
  2610   have "?l \<in> ((cp s) ` threads s)"
  2712   have "?l \<in> ((cp s) ` threads s)"
  2611   proof(rule Max_in)
  2713   proof(rule Max_in)
  2612     from finite_threads[OF vt] 
  2714     from finite_threads
  2613     show "finite (cp s ` threads s)" by auto
  2715     show "finite (cp s ` threads s)" by auto
  2614   next
  2716   next
  2615     from False show "cp s ` threads s \<noteq> {}" by auto
  2717     from False show "cp s ` threads s \<noteq> {}" by auto
  2616   qed
  2718   qed
  2617   then obtain th 
  2719   then obtain th 
  2618     where th_in: "th \<in> threads s" and eq_l: "?l = cp s th" by auto
  2720     where th_in: "th \<in> threads s" and eq_l: "?l = cp s th" by auto
  2619   have "\<dots> \<le> ?r" by (rule cp_le[OF vt th_in])
  2721   have "\<dots> \<le> ?r" by (rule cp_le[OF th_in])
  2620   moreover have "?r \<le> cp s th" (is "Max (?f ` ?A) \<le> cp s th")
  2722   moreover have "?r \<le> cp s th" (is "Max (?f ` ?A) \<le> cp s th")
  2621   proof -
  2723   proof -
  2622     have "?r \<in> (?f ` ?A)"
  2724     have "?r \<in> (?f ` ?A)"
  2623     proof(rule Max_in)
  2725     proof(rule Max_in)
  2624       from finite_threads[OF vt]
  2726       from finite_threads
  2625       show " finite ((\<lambda>th. preced th s) ` threads s)" by auto
  2727       show " finite ((\<lambda>th. preced th s) ` threads s)" by auto
  2626     next
  2728     next
  2627       from False show " (\<lambda>th. preced th s) ` threads s \<noteq> {}" by auto
  2729       from False show " (\<lambda>th. preced th s) ` threads s \<noteq> {}" by auto
  2628     qed
  2730     qed
  2629     then obtain th' where 
  2731     then obtain th' where 
  2630       th_in': "th' \<in> ?A " and eq_r: "?r = ?f th'" by auto
  2732       th_in': "th' \<in> ?A " and eq_r: "?r = ?f th'" by auto
  2631     from le_cp [OF vt, of th']  eq_r
  2733     from le_cp [of th']  eq_r
  2632     have "?r \<le> cp s th'" by auto
  2734     have "?r \<le> cp s th'" by auto
  2633     moreover have "\<dots> \<le> cp s th"
  2735     moreover have "\<dots> \<le> cp s th"
  2634     proof(fold eq_l)
  2736     proof(fold eq_l)
  2635       show " cp s th' \<le> Max (cp s ` threads s)"
  2737       show " cp s th' \<le> Max (cp s ` threads s)"
  2636       proof(rule Max_ge)
  2738       proof(rule Max_ge)
  2637         from th_in' show "cp s th' \<in> cp s ` threads s"
  2739         from th_in' show "cp s th' \<in> cp s ` threads s"
  2638           by auto
  2740           by auto
  2639       next
  2741       next
  2640         from finite_threads[OF vt]
  2742         from finite_threads
  2641         show "finite (cp s ` threads s)" by auto
  2743         show "finite (cp s ` threads s)" by auto
  2642       qed
  2744       qed
  2643     qed
  2745     qed
  2644     ultimately show ?thesis by auto
  2746     ultimately show ?thesis by auto
  2645   qed
  2747   qed
  2646   ultimately show ?thesis using eq_l by auto
  2748   ultimately show ?thesis using eq_l by auto
  2647 qed
  2749 qed
  2648 
  2750 
  2649 lemma max_cp_readys_threads_pre:
  2751 lemma max_cp_readys_threads_pre:
  2650   assumes vt: "vt s"
  2752   assumes np: "threads s \<noteq> {}"
  2651   and np: "threads s \<noteq> {}"
       
  2652   shows "Max (cp s ` readys s) = Max (cp s ` threads s)"
  2753   shows "Max (cp s ` readys s) = Max (cp s ` threads s)"
  2653 proof(unfold max_cp_eq[OF vt])
  2754 proof(unfold max_cp_eq)
  2654   show "Max (cp s ` readys s) = Max ((\<lambda>th. preced th s) ` threads s)"
  2755   show "Max (cp s ` readys s) = Max ((\<lambda>th. preced th s) ` threads s)"
  2655   proof -
  2756   proof -
  2656     let ?p = "Max ((\<lambda>th. preced th s) ` threads s)" 
  2757     let ?p = "Max ((\<lambda>th. preced th s) ` threads s)" 
  2657     let ?f = "(\<lambda>th. preced th s)"
  2758     let ?f = "(\<lambda>th. preced th s)"
  2658     have "?p \<in> ((\<lambda>th. preced th s) ` threads s)"
  2759     have "?p \<in> ((\<lambda>th. preced th s) ` threads s)"
  2659     proof(rule Max_in)
  2760     proof(rule Max_in)
  2660       from finite_threads[OF vt] show "finite (?f ` threads s)" by simp
  2761       from finite_threads show "finite (?f ` threads s)" by simp
  2661     next
  2762     next
  2662       from np show "?f ` threads s \<noteq> {}" by simp
  2763       from np show "?f ` threads s \<noteq> {}" by simp
  2663     qed
  2764     qed
  2664     then obtain tm where tm_max: "?f tm = ?p" and tm_in: "tm \<in> threads s"
  2765     then obtain tm where tm_max: "?f tm = ?p" and tm_in: "tm \<in> threads s"
  2665       by (auto simp:Image_def)
  2766       by (auto simp:Image_def)
  2666     from th_chain_to_ready [OF vt tm_in]
  2767     from th_chain_to_ready [OF tm_in]
  2667     have "tm \<in> readys s \<or> (\<exists>th'. th' \<in> readys s \<and> (Th tm, Th th') \<in> (RAG s)\<^sup>+)" .
  2768     have "tm \<in> readys s \<or> (\<exists>th'. th' \<in> readys s \<and> (Th tm, Th th') \<in> (RAG s)\<^sup>+)" .
  2668     thus ?thesis
  2769     thus ?thesis
  2669     proof
  2770     proof
  2670       assume "\<exists>th'. th' \<in> readys s \<and> (Th tm, Th th') \<in> (RAG s)\<^sup>+ "
  2771       assume "\<exists>th'. th' \<in> readys s \<and> (Th tm, Th th') \<in> (RAG s)\<^sup>+ "
  2671       then obtain th' where th'_in: "th' \<in> readys s" 
  2772       then obtain th' where th'_in: "th' \<in> readys s" 
  2672         and tm_chain:"(Th tm, Th th') \<in> (RAG s)\<^sup>+" by auto
  2773         and tm_chain:"(Th tm, Th th') \<in> (RAG s)\<^sup>+" by auto
  2673       have "cp s th' = ?f tm"
  2774       have "cp s th' = ?f tm"
  2674       proof(subst cp_eq_cpreced, subst cpreced_def, rule Max_eqI)
  2775       proof(subst cp_eq_cpreced, subst cpreced_def, rule Max_eqI)
  2675         from dependants_threads[OF vt] finite_threads[OF vt]
  2776         from dependants_threads finite_threads
  2676         show "finite ((\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th'))" 
  2777         show "finite ((\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th'))" 
  2677           by (auto intro:finite_subset)
  2778           by (auto intro:finite_subset)
  2678       next
  2779       next
  2679         fix p assume p_in: "p \<in> (\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')"
  2780         fix p assume p_in: "p \<in> (\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')"
  2680         from tm_max have " preced tm s = Max ((\<lambda>th. preced th s) ` threads s)" .
  2781         from tm_max have " preced tm s = Max ((\<lambda>th. preced th s) ` threads s)" .
  2681         moreover have "p \<le> \<dots>"
  2782         moreover have "p \<le> \<dots>"
  2682         proof(rule Max_ge)
  2783         proof(rule Max_ge)
  2683           from finite_threads[OF vt]
  2784           from finite_threads
  2684           show "finite ((\<lambda>th. preced th s) ` threads s)" by simp
  2785           show "finite ((\<lambda>th. preced th s) ` threads s)" by simp
  2685         next
  2786         next
  2686           from p_in and th'_in and dependants_threads[OF vt, of th']
  2787           from p_in and th'_in and dependants_threads[of th']
  2687           show "p \<in> (\<lambda>th. preced th s) ` threads s"
  2788           show "p \<in> (\<lambda>th. preced th s) ` threads s"
  2688             by (auto simp:readys_def)
  2789             by (auto simp:readys_def)
  2689         qed
  2790         qed
  2690         ultimately show "p \<le> preced tm s" by auto
  2791         ultimately show "p \<le> preced tm s" by auto
  2691       next
  2792       next
  2708         show "q \<le> cp s th'"
  2809         show "q \<le> cp s th'"
  2709           apply (unfold h eq_q)
  2810           apply (unfold h eq_q)
  2710           apply (unfold cp_eq_cpreced cpreced_def)
  2811           apply (unfold cp_eq_cpreced cpreced_def)
  2711           apply (rule Max_mono)
  2812           apply (rule Max_mono)
  2712         proof -
  2813         proof -
  2713           from dependants_threads [OF vt, of th1] th1_in
  2814           from dependants_threads [of th1] th1_in
  2714           show "(\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1) \<subseteq> 
  2815           show "(\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1) \<subseteq> 
  2715                  (\<lambda>th. preced th s) ` threads s"
  2816                  (\<lambda>th. preced th s) ` threads s"
  2716             by (auto simp:readys_def)
  2817             by (auto simp:readys_def)
  2717         next
  2818         next
  2718           show "(\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1) \<noteq> {}" by simp
  2819           show "(\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1) \<noteq> {}" by simp
  2719         next
  2820         next
  2720           from finite_threads[OF vt] 
  2821           from finite_threads 
  2721           show " finite ((\<lambda>th. preced th s) ` threads s)" by simp
  2822           show " finite ((\<lambda>th. preced th s) ` threads s)" by simp
  2722         qed
  2823         qed
  2723       next
  2824       next
  2724         from finite_threads[OF vt]
  2825         from finite_threads
  2725         show "finite (cp s ` readys s)" by (auto simp:readys_def)
  2826         show "finite (cp s ` readys s)" by (auto simp:readys_def)
  2726       next
  2827       next
  2727         from th'_in
  2828         from th'_in
  2728         show "cp s th' \<in> cp s ` readys s" by simp
  2829         show "cp s th' \<in> cp s ` readys s" by simp
  2729       qed
  2830       qed
  2739           proof -
  2840           proof -
  2740             { fix y'
  2841             { fix y'
  2741               assume hy' : "y' \<in> ((\<lambda>th. preced th s) ` dependants (wq s) tm)"
  2842               assume hy' : "y' \<in> ((\<lambda>th. preced th s) ` dependants (wq s) tm)"
  2742               have "y' \<le> preced tm s"
  2843               have "y' \<le> preced tm s"
  2743               proof(unfold tm_max, rule Max_ge)
  2844               proof(unfold tm_max, rule Max_ge)
  2744                 from hy' dependants_threads[OF vt, of tm]
  2845                 from hy' dependants_threads[of tm]
  2745                 show "y' \<in> (\<lambda>th. preced th s) ` threads s" by auto
  2846                 show "y' \<in> (\<lambda>th. preced th s) ` threads s" by auto
  2746               next
  2847               next
  2747                 from finite_threads[OF vt] 
  2848                 from finite_threads
  2748                 show "finite ((\<lambda>th. preced th s) ` threads s)" by simp
  2849                 show "finite ((\<lambda>th. preced th s) ` threads s)" by simp
  2749               qed
  2850               qed
  2750             } with hy show ?thesis by auto
  2851             } with hy show ?thesis by auto
  2751           qed
  2852           qed
  2752         next
  2853         next
  2753           from dependants_threads[OF vt, of tm] finite_threads[OF vt]
  2854           from dependants_threads[of tm] finite_threads
  2754           show "finite ((\<lambda>th. preced th s) ` ({tm} \<union> dependants (wq s) tm))"
  2855           show "finite ((\<lambda>th. preced th s) ` ({tm} \<union> dependants (wq s) tm))"
  2755             by (auto intro:finite_subset)
  2856             by (auto intro:finite_subset)
  2756         next
  2857         next
  2757           show "preced tm s \<in> (\<lambda>th. preced th s) ` ({tm} \<union> dependants (wq s) tm)"
  2858           show "preced tm s \<in> (\<lambda>th. preced th s) ` ({tm} \<union> dependants (wq s) tm)"
  2758             by simp
  2859             by simp
  2759         qed 
  2860         qed 
  2760         moreover have "Max (cp s ` readys s) = cp s tm"
  2861         moreover have "Max (cp s ` readys s) = cp s tm"
  2761         proof(rule Max_eqI)
  2862         proof(rule Max_eqI)
  2762           from tm_ready show "cp s tm \<in> cp s ` readys s" by simp
  2863           from tm_ready show "cp s tm \<in> cp s ` readys s" by simp
  2763         next
  2864         next
  2764           from finite_threads[OF vt]
  2865           from finite_threads
  2765           show "finite (cp s ` readys s)" by (auto simp:readys_def)
  2866           show "finite (cp s ` readys s)" by (auto simp:readys_def)
  2766         next
  2867         next
  2767           fix y assume "y \<in> cp s ` readys s"
  2868           fix y assume "y \<in> cp s ` readys s"
  2768           then obtain th1 where th1_readys: "th1 \<in> readys s"
  2869           then obtain th1 where th1_readys: "th1 \<in> readys s"
  2769             and h: "y = cp s th1" by auto
  2870             and h: "y = cp s th1" by auto
  2770           show "y \<le> cp s tm"
  2871           show "y \<le> cp s tm"
  2771             apply(unfold cp_eq_p h)
  2872             apply(unfold cp_eq_p h)
  2772             apply(unfold cp_eq_cpreced cpreced_def tm_max, rule Max_mono)
  2873             apply(unfold cp_eq_cpreced cpreced_def tm_max, rule Max_mono)
  2773           proof -
  2874           proof -
  2774             from finite_threads[OF vt]
  2875             from finite_threads
  2775             show "finite ((\<lambda>th. preced th s) ` threads s)" by simp
  2876             show "finite ((\<lambda>th. preced th s) ` threads s)" by simp
  2776           next
  2877           next
  2777             show "(\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1) \<noteq> {}"
  2878             show "(\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1) \<noteq> {}"
  2778               by simp
  2879               by simp
  2779           next
  2880           next
  2780             from dependants_threads[OF vt, of th1] th1_readys
  2881             from dependants_threads[of th1] th1_readys
  2781             show "(\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1) 
  2882             show "(\<lambda>th. preced th s) ` ({th1} \<union> dependants (wq s) th1) 
  2782                     \<subseteq> (\<lambda>th. preced th s) ` threads s"
  2883                     \<subseteq> (\<lambda>th. preced th s) ` threads s"
  2783               by (auto simp:readys_def)
  2884               by (auto simp:readys_def)
  2784           qed
  2885           qed
  2785         qed
  2886         qed
  2792 text {* (* ccc *) \noindent
  2893 text {* (* ccc *) \noindent
  2793   Since the current precedence of the threads in ready queue will always be boosted,
  2894   Since the current precedence of the threads in ready queue will always be boosted,
  2794   there must be one inside it has the maximum precedence of the whole system. 
  2895   there must be one inside it has the maximum precedence of the whole system. 
  2795 *}
  2896 *}
  2796 lemma max_cp_readys_threads:
  2897 lemma max_cp_readys_threads:
  2797   assumes vt: "vt s"
       
  2798   shows "Max (cp s ` readys s) = Max (cp s ` threads s)"
  2898   shows "Max (cp s ` readys s) = Max (cp s ` threads s)"
  2799 proof(cases "threads s = {}")
  2899 proof(cases "threads s = {}")
  2800   case True
  2900   case True
  2801   thus ?thesis 
  2901   thus ?thesis 
  2802     by (auto simp:readys_def)
  2902     by (auto simp:readys_def)
  2803 next
  2903 next
  2804   case False
  2904   case False
  2805   show ?thesis by (rule max_cp_readys_threads_pre[OF vt False])
  2905   show ?thesis by (rule max_cp_readys_threads_pre[OF False])
  2806 qed
  2906 qed
  2807 
  2907 
       
  2908 end
  2808 
  2909 
  2809 lemma eq_holding: "holding (wq s) th cs = holding s th cs"
  2910 lemma eq_holding: "holding (wq s) th cs = holding s th cs"
  2810   apply (unfold s_holding_def cs_holding_def wq_def, simp)
  2911   apply (unfold s_holding_def cs_holding_def wq_def, simp)
  2811   done
  2912   done
  2812 
  2913 
  2834 apply(simp add: Domain_iff Range_iff)
  2935 apply(simp add: Domain_iff Range_iff)
  2835 apply(simp add: wq_def)
  2936 apply(simp add: wq_def)
  2836 apply(auto)
  2937 apply(auto)
  2837 done
  2938 done
  2838 
  2939 
       
  2940 context valid_trace
       
  2941 begin
       
  2942 
  2839 lemma detached_intro:
  2943 lemma detached_intro:
  2840   fixes s th
  2944   assumes eq_pv: "cntP s th = cntV s th"
  2841   assumes vt: "vt s"
       
  2842   and eq_pv: "cntP s th = cntV s th"
       
  2843   shows "detached s th"
  2945   shows "detached s th"
  2844 proof -
  2946 proof -
  2845  from cnp_cnv_cncs[OF vt]
  2947  from cnp_cnv_cncs
  2846   have eq_cnt: "cntP s th =
  2948   have eq_cnt: "cntP s th =
  2847     cntV s th + (if th \<in> readys s \<or> th \<notin> threads s then cntCS s th else cntCS s th + 1)" .
  2949     cntV s th + (if th \<in> readys s \<or> th \<notin> threads s then cntCS s th else cntCS s th + 1)" .
  2848   hence cncs_zero: "cntCS s th = 0"
  2950   hence cncs_zero: "cntCS s th = 0"
  2849     by (auto simp:eq_pv split:if_splits)
  2951     by (auto simp:eq_pv split:if_splits)
  2850   with eq_cnt
  2952   with eq_cnt
  2851   have "th \<in> readys s \<or> th \<notin> threads s" by (auto simp:eq_pv)
  2953   have "th \<in> readys s \<or> th \<notin> threads s" by (auto simp:eq_pv)
  2852   thus ?thesis
  2954   thus ?thesis
  2853   proof
  2955   proof
  2854     assume "th \<notin> threads s"
  2956     assume "th \<notin> threads s"
  2855     with range_in[OF vt] dm_RAG_threads[OF vt]
  2957     with range_in dm_RAG_threads
  2856     show ?thesis
  2958     show ?thesis
  2857       by (auto simp add: detached_def s_RAG_def s_waiting_abv s_holding_abv wq_def Domain_iff Range_iff)
  2959       by (auto simp add: detached_def s_RAG_def s_waiting_abv s_holding_abv wq_def Domain_iff Range_iff)
  2858   next
  2960   next
  2859     assume "th \<in> readys s"
  2961     assume "th \<in> readys s"
  2860     moreover have "Th th \<notin> Range (RAG s)"
  2962     moreover have "Th th \<notin> Range (RAG s)"
  2861     proof -
  2963     proof -
  2862       from card_0_eq [OF finite_holding [OF vt]] and cncs_zero
  2964       from card_0_eq [OF finite_holding] and cncs_zero
  2863       have "holdents s th = {}"
  2965       have "holdents s th = {}"
  2864         by (simp add:cntCS_def)
  2966         by (simp add:cntCS_def)
  2865       thus ?thesis
  2967       thus ?thesis
  2866         apply(auto simp:holdents_test)
  2968         apply(auto simp:holdents_test)
  2867         apply(case_tac a)
  2969         apply(case_tac a)
  2872       by (auto simp add: detached_def s_RAG_def s_waiting_abv s_holding_abv wq_def readys_def)
  2974       by (auto simp add: detached_def s_RAG_def s_waiting_abv s_holding_abv wq_def readys_def)
  2873   qed
  2975   qed
  2874 qed
  2976 qed
  2875 
  2977 
  2876 lemma detached_elim:
  2978 lemma detached_elim:
  2877   fixes s th
  2979   assumes dtc: "detached s th"
  2878   assumes vt: "vt s"
       
  2879   and dtc: "detached s th"
       
  2880   shows "cntP s th = cntV s th"
  2980   shows "cntP s th = cntV s th"
  2881 proof -
  2981 proof -
  2882   from cnp_cnv_cncs[OF vt]
  2982   from cnp_cnv_cncs
  2883   have eq_pv: " cntP s th =
  2983   have eq_pv: " cntP s th =
  2884     cntV s th + (if th \<in> readys s \<or> th \<notin> threads s then cntCS s th else cntCS s th + 1)" .
  2984     cntV s th + (if th \<in> readys s \<or> th \<notin> threads s then cntCS s th else cntCS s th + 1)" .
  2885   have cncs_z: "cntCS s th = 0"
  2985   have cncs_z: "cntCS s th = 0"
  2886   proof -
  2986   proof -
  2887     from dtc have "holdents s th = {}"
  2987     from dtc have "holdents s th = {}"
  2902     with cncs_z and eq_pv show ?thesis by simp
  3002     with cncs_z and eq_pv show ?thesis by simp
  2903   qed
  3003   qed
  2904 qed
  3004 qed
  2905 
  3005 
  2906 lemma detached_eq:
  3006 lemma detached_eq:
  2907   fixes s th
       
  2908   assumes vt: "vt s"
       
  2909   shows "(detached s th) = (cntP s th = cntV s th)"
  3007   shows "(detached s th) = (cntP s th = cntV s th)"
  2910   by (insert vt, auto intro:detached_intro detached_elim)
  3008   by (insert vt, auto intro:detached_intro detached_elim)
       
  3009 
       
  3010 end
  2911 
  3011 
  2912 text {* 
  3012 text {* 
  2913   The lemmas in this .thy file are all obvious lemmas, however, they still needs to be derived
  3013   The lemmas in this .thy file are all obvious lemmas, however, they still needs to be derived
  2914   from the concise and miniature model of PIP given in PrioGDef.thy.
  3014   from the concise and miniature model of PIP given in PrioGDef.thy.
  2915 *}
  3015 *}
  2921   assumes nt1: "next_th s th cs th1"
  3021   assumes nt1: "next_th s th cs th1"
  2922   and nt2: "next_th s th cs th2"
  3022   and nt2: "next_th s th cs th2"
  2923   shows "th1 = th2"
  3023   shows "th1 = th2"
  2924 using assms by (unfold next_th_def, auto)
  3024 using assms by (unfold next_th_def, auto)
  2925 
  3025 
  2926  
  3026 lemma birth_time_lt:  "s \<noteq> [] \<Longrightarrow> last_set th s < length s"
       
  3027   apply (induct s, simp)
       
  3028 proof -
       
  3029   fix a s
       
  3030   assume ih: "s \<noteq> [] \<Longrightarrow> last_set th s < length s"
       
  3031     and eq_as: "a # s \<noteq> []"
       
  3032   show "last_set th (a # s) < length (a # s)"
       
  3033   proof(cases "s \<noteq> []")
       
  3034     case False
       
  3035     from False show ?thesis
       
  3036       by (cases a, auto simp:last_set.simps)
       
  3037   next
       
  3038     case True
       
  3039     from ih [OF True] show ?thesis
       
  3040       by (cases a, auto simp:last_set.simps)
       
  3041   qed
       
  3042 qed
       
  3043 
       
  3044 lemma th_in_ne: "th \<in> threads s \<Longrightarrow> s \<noteq> []"
       
  3045   by (induct s, auto simp:threads.simps)
       
  3046 
       
  3047 lemma preced_tm_lt: "th \<in> threads s \<Longrightarrow> preced th s = Prc x y \<Longrightarrow> y < length s"
       
  3048   apply (drule_tac th_in_ne)
       
  3049   by (unfold preced_def, auto intro: birth_time_lt)
       
  3050 
  2927 end
  3051 end