PrioG.thy~
changeset 97 c7ba70dc49bd
parent 96 4805c6333fef
parent 93 524bd3caa6b6
child 98 382293d415f3
equal deleted inserted replaced
96:4805c6333fef 97:c7ba70dc49bd
     1 theory PrioG
       
     2 imports CpsG
       
     3 begin
       
     4 
       
     5 
       
     6 text {* 
       
     7   The following two auxiliary lemmas are used to reason about @{term Max}.
       
     8 *}
       
     9 lemma image_Max_eqI: 
       
    10   assumes "finite B"
       
    11   and "b \<in> B"
       
    12   and "\<forall> x \<in> B. f x \<le> f b"
       
    13   shows "Max (f ` B) = f b"
       
    14   using assms
       
    15   using Max_eqI by blast 
       
    16 
       
    17 lemma image_Max_subset:
       
    18   assumes "finite A"
       
    19   and "B \<subseteq> A"
       
    20   and "a \<in> B"
       
    21   and "Max (f ` A) = f a"
       
    22   shows "Max (f ` B) = f a"
       
    23 proof(rule image_Max_eqI)
       
    24   show "finite B"
       
    25     using assms(1) assms(2) finite_subset by auto 
       
    26 next
       
    27   show "a \<in> B" using assms by simp
       
    28 next
       
    29   show "\<forall>x\<in>B. f x \<le> f a"
       
    30     by (metis Max_ge assms(1) assms(2) assms(4) 
       
    31             finite_imageI image_eqI subsetCE) 
       
    32 qed
       
    33 
       
    34 text {*
       
    35   The following locale @{text "highest_gen"} sets the basic context for our
       
    36   investigation: supposing thread @{text th} holds the highest @{term cp}-value
       
    37   in state @{text s}, which means the task for @{text th} is the 
       
    38   most urgent. We want to show that  
       
    39   @{text th} is treated correctly by PIP, which means
       
    40   @{text th} will not be blocked unreasonably by other less urgent
       
    41   threads. 
       
    42 *}
       
    43 locale highest_gen =
       
    44   fixes s th prio tm
       
    45   assumes vt_s: "vt s"
       
    46   and threads_s: "th \<in> threads s"
       
    47   and highest: "preced th s = Max ((cp s)`threads s)"
       
    48   -- {* The internal structure of @{term th}'s precedence is exposed:*}
       
    49   and preced_th: "preced th s = Prc prio tm" 
       
    50 
       
    51 -- {* @{term s} is a valid trace, so it will inherit all results derived for
       
    52       a valid trace: *}
       
    53 sublocale highest_gen < vat_s: valid_trace "s"
       
    54   by (unfold_locales, insert vt_s, simp)
       
    55 
       
    56 context highest_gen
       
    57 begin
       
    58 
       
    59 text {*
       
    60   @{term tm} is the time when the precedence of @{term th} is set, so 
       
    61   @{term tm} must be a valid moment index into @{term s}.
       
    62 *}
       
    63 lemma lt_tm: "tm < length s"
       
    64   by (insert preced_tm_lt[OF threads_s preced_th], simp)
       
    65 
       
    66 text {*
       
    67   Since @{term th} holds the highest precedence and @{text "cp"}
       
    68   is the highest precedence of all threads in the sub-tree of 
       
    69   @{text "th"} and @{text th} is among these threads, 
       
    70   its @{term cp} must equal to its precedence:
       
    71 *}
       
    72 lemma eq_cp_s_th: "cp s th = preced th s" (is "?L = ?R")
       
    73 proof -
       
    74   have "?L \<le> ?R"
       
    75   by (unfold highest, rule Max_ge, 
       
    76         auto simp:threads_s finite_threads)
       
    77   moreover have "?R \<le> ?L"
       
    78     by (unfold vat_s.cp_rec, rule Max_ge, 
       
    79         auto simp:the_preced_def vat_s.fsbttRAGs.finite_children)
       
    80   ultimately show ?thesis by auto
       
    81 qed
       
    82 
       
    83 lemma highest_cp_preced: "cp s th = Max (the_preced s ` threads s)"
       
    84   using eq_cp_s_th highest max_cp_eq the_preced_def by presburger
       
    85   
       
    86 
       
    87 lemma highest_preced_thread: "preced th s = Max (the_preced s ` threads s)"
       
    88   by (fold eq_cp_s_th, unfold highest_cp_preced, simp)
       
    89 
       
    90 lemma highest': "cp s th = Max (cp s ` threads s)"
       
    91   by (simp add: eq_cp_s_th highest)
       
    92 
       
    93 end
       
    94 
       
    95 locale extend_highest_gen = highest_gen + 
       
    96   fixes t 
       
    97   assumes vt_t: "vt (t@s)"
       
    98   and create_low: "Create th' prio' \<in> set t \<Longrightarrow> prio' \<le> prio"
       
    99   and set_diff_low: "Set th' prio' \<in> set t \<Longrightarrow> th' \<noteq> th \<and> prio' \<le> prio"
       
   100   and exit_diff: "Exit th' \<in> set t \<Longrightarrow> th' \<noteq> th"
       
   101 
       
   102 sublocale extend_highest_gen < vat_t: valid_trace "t@s"
       
   103   by (unfold_locales, insert vt_t, simp)
       
   104 
       
   105 lemma step_back_vt_app: 
       
   106   assumes vt_ts: "vt (t@s)" 
       
   107   shows "vt s"
       
   108 proof -
       
   109   from vt_ts show ?thesis
       
   110   proof(induct t)
       
   111     case Nil
       
   112     from Nil show ?case by auto
       
   113   next
       
   114     case (Cons e t)
       
   115     assume ih: " vt (t @ s) \<Longrightarrow> vt s"
       
   116       and vt_et: "vt ((e # t) @ s)"
       
   117     show ?case
       
   118     proof(rule ih)
       
   119       show "vt (t @ s)"
       
   120       proof(rule step_back_vt)
       
   121         from vt_et show "vt (e # t @ s)" by simp
       
   122       qed
       
   123     qed
       
   124   qed
       
   125 qed
       
   126 
       
   127 (* locale red_extend_highest_gen = extend_highest_gen +
       
   128    fixes i::nat
       
   129 *)
       
   130 
       
   131 (*
       
   132 sublocale red_extend_highest_gen <   red_moment: extend_highest_gen "s" "th" "prio" "tm" "(moment i t)"
       
   133   apply (insert extend_highest_gen_axioms, subst (asm) (1) moment_restm_s [of i t, symmetric])
       
   134   apply (unfold extend_highest_gen_def extend_highest_gen_axioms_def, clarsimp)
       
   135   by (unfold highest_gen_def, auto dest:step_back_vt_app)
       
   136 *)
       
   137 
       
   138 context extend_highest_gen
       
   139 begin
       
   140 
       
   141  lemma ind [consumes 0, case_names Nil Cons, induct type]:
       
   142   assumes 
       
   143     h0: "R []"
       
   144   and h2: "\<And> e t. \<lbrakk>vt (t@s); step (t@s) e; 
       
   145                     extend_highest_gen s th prio tm t; 
       
   146                     extend_highest_gen s th prio tm (e#t); R t\<rbrakk> \<Longrightarrow> R (e#t)"
       
   147   shows "R t"
       
   148 proof -
       
   149   from vt_t extend_highest_gen_axioms show ?thesis
       
   150   proof(induct t)
       
   151     from h0 show "R []" .
       
   152   next
       
   153     case (Cons e t')
       
   154     assume ih: "\<lbrakk>vt (t' @ s); extend_highest_gen s th prio tm t'\<rbrakk> \<Longrightarrow> R t'"
       
   155       and vt_e: "vt ((e # t') @ s)"
       
   156       and et: "extend_highest_gen s th prio tm (e # t')"
       
   157     from vt_e and step_back_step have stp: "step (t'@s) e" by auto
       
   158     from vt_e and step_back_vt have vt_ts: "vt (t'@s)" by auto
       
   159     show ?case
       
   160     proof(rule h2 [OF vt_ts stp _ _ _ ])
       
   161       show "R t'"
       
   162       proof(rule ih)
       
   163         from et show ext': "extend_highest_gen s th prio tm t'"
       
   164           by (unfold extend_highest_gen_def extend_highest_gen_axioms_def, auto dest:step_back_vt)
       
   165       next
       
   166         from vt_ts show "vt (t' @ s)" .
       
   167       qed
       
   168     next
       
   169       from et show "extend_highest_gen s th prio tm (e # t')" .
       
   170     next
       
   171       from et show ext': "extend_highest_gen s th prio tm t'"
       
   172           by (unfold extend_highest_gen_def extend_highest_gen_axioms_def, auto dest:step_back_vt)
       
   173     qed
       
   174   qed
       
   175 qed
       
   176 
       
   177 
       
   178 lemma th_kept: "th \<in> threads (t @ s) \<and> 
       
   179                  preced th (t@s) = preced th s" (is "?Q t") 
       
   180 proof -
       
   181   show ?thesis
       
   182   proof(induct rule:ind)
       
   183     case Nil
       
   184     from threads_s
       
   185     show ?case
       
   186       by auto
       
   187   next
       
   188     case (Cons e t)
       
   189     interpret h_e: extend_highest_gen _ _ _ _ "(e # t)" using Cons by auto
       
   190     interpret h_t: extend_highest_gen _ _ _ _ t using Cons by auto
       
   191     show ?case
       
   192     proof(cases e)
       
   193       case (Create thread prio)
       
   194       show ?thesis
       
   195       proof -
       
   196         from Cons and Create have "step (t@s) (Create thread prio)" by auto
       
   197         hence "th \<noteq> thread"
       
   198         proof(cases)
       
   199           case thread_create
       
   200           with Cons show ?thesis by auto
       
   201         qed
       
   202         hence "preced th ((e # t) @ s)  = preced th (t @ s)"
       
   203           by (unfold Create, auto simp:preced_def)
       
   204         moreover note Cons
       
   205         ultimately show ?thesis
       
   206           by (auto simp:Create)
       
   207       qed
       
   208     next
       
   209       case (Exit thread)
       
   210       from h_e.exit_diff and Exit
       
   211       have neq_th: "thread \<noteq> th" by auto
       
   212       with Cons
       
   213       show ?thesis
       
   214         by (unfold Exit, auto simp:preced_def)
       
   215     next
       
   216       case (P thread cs)
       
   217       with Cons
       
   218       show ?thesis 
       
   219         by (auto simp:P preced_def)
       
   220     next
       
   221       case (V thread cs)
       
   222       with Cons
       
   223       show ?thesis 
       
   224         by (auto simp:V preced_def)
       
   225     next
       
   226       case (Set thread prio')
       
   227       show ?thesis
       
   228       proof -
       
   229         from h_e.set_diff_low and Set
       
   230         have "th \<noteq> thread" by auto
       
   231         hence "preced th ((e # t) @ s)  = preced th (t @ s)"
       
   232           by (unfold Set, auto simp:preced_def)
       
   233         moreover note Cons
       
   234         ultimately show ?thesis
       
   235           by (auto simp:Set)
       
   236       qed
       
   237     qed
       
   238   qed
       
   239 qed
       
   240 
       
   241 text {*
       
   242   According to @{thm th_kept}, thread @{text "th"} has its living status
       
   243   and precedence kept along the way of @{text "t"}. The following lemma
       
   244   shows that this preserved precedence of @{text "th"} remains as the highest
       
   245   along the way of @{text "t"}.
       
   246 
       
   247   The proof goes by induction over @{text "t"} using the specialized
       
   248   induction rule @{thm ind}, followed by case analysis of each possible 
       
   249   operations of PIP. All cases follow the same pattern rendered by the 
       
   250   generalized introduction rule @{thm "image_Max_eqI"}. 
       
   251 
       
   252   The very essence is to show that precedences, no matter whether they 
       
   253   are newly introduced or modified, are always lower than the one held 
       
   254   by @{term "th"}, which by @{thm th_kept} is preserved along the way.
       
   255 *}
       
   256 lemma max_kept: "Max (the_preced (t @ s) ` (threads (t@s))) = preced th s"
       
   257 proof(induct rule:ind)
       
   258   case Nil
       
   259   from highest_preced_thread
       
   260   show ?case by simp
       
   261 next
       
   262   case (Cons e t)
       
   263     interpret h_e: extend_highest_gen _ _ _ _ "(e # t)" using Cons by auto
       
   264     interpret h_t: extend_highest_gen _ _ _ _ t using Cons by auto
       
   265   show ?case
       
   266   proof(cases e)
       
   267     case (Create thread prio')
       
   268     show ?thesis (is "Max (?f ` ?A) = ?t")
       
   269     proof -
       
   270       -- {* The following is the common pattern of each branch of the case analysis. *}
       
   271       -- {* The major part is to show that @{text "th"} holds the highest precedence: *}
       
   272       have "Max (?f ` ?A) = ?f th"
       
   273       proof(rule image_Max_eqI)
       
   274         show "finite ?A" using h_e.finite_threads by auto 
       
   275       next
       
   276         show "th \<in> ?A" using h_e.th_kept by auto 
       
   277       next
       
   278         show "\<forall>x\<in>?A. ?f x \<le> ?f th"
       
   279         proof 
       
   280           fix x
       
   281           assume "x \<in> ?A"
       
   282           hence "x = thread \<or> x \<in> threads (t@s)" by (auto simp:Create)
       
   283           thus "?f x \<le> ?f th"
       
   284           proof
       
   285             assume "x = thread"
       
   286             thus ?thesis 
       
   287               apply (simp add:Create the_preced_def preced_def, fold preced_def)
       
   288               using Create h_e.create_low h_t.th_kept lt_tm preced_leI2 
       
   289               preced_th by force
       
   290           next
       
   291             assume h: "x \<in> threads (t @ s)"
       
   292             from Cons(2)[unfolded Create] 
       
   293             have "x \<noteq> thread" using h by (cases, auto)
       
   294             hence "?f x = the_preced (t@s) x" 
       
   295               by (simp add:Create the_preced_def preced_def)
       
   296             hence "?f x \<le> Max (the_preced (t@s) ` threads (t@s))"
       
   297               by (simp add: h_t.finite_threads h)
       
   298             also have "... = ?f th"
       
   299               by (metis Cons.hyps(5) h_e.th_kept the_preced_def) 
       
   300             finally show ?thesis .
       
   301           qed
       
   302         qed
       
   303       qed
       
   304      -- {* The minor part is to show that the precedence of @{text "th"} 
       
   305            equals to preserved one, given by the foregoing lemma @{thm th_kept} *}
       
   306       also have "... = ?t" using h_e.th_kept the_preced_def by auto
       
   307       -- {* Then it follows trivially that the precedence preserved
       
   308             for @{term "th"} remains the maximum of all living threads along the way. *}
       
   309       finally show ?thesis .
       
   310     qed 
       
   311   next 
       
   312     case (Exit thread)
       
   313     show ?thesis (is "Max (?f ` ?A) = ?t")
       
   314     proof -
       
   315       have "Max (?f ` ?A) = ?f th"
       
   316       proof(rule image_Max_eqI)
       
   317         show "finite ?A" using h_e.finite_threads by auto 
       
   318       next
       
   319         show "th \<in> ?A" using h_e.th_kept by auto 
       
   320       next
       
   321         show "\<forall>x\<in>?A. ?f x \<le> ?f th"
       
   322         proof 
       
   323           fix x
       
   324           assume "x \<in> ?A"
       
   325           hence "x \<in> threads (t@s)" by (simp add: Exit) 
       
   326           hence "?f x \<le> Max (?f ` threads (t@s))" 
       
   327             by (simp add: h_t.finite_threads) 
       
   328           also have "... \<le> ?f th" 
       
   329             apply (simp add:Exit the_preced_def preced_def, fold preced_def)
       
   330             using Cons.hyps(5) h_t.th_kept the_preced_def by auto
       
   331           finally show "?f x \<le> ?f th" .
       
   332         qed
       
   333       qed
       
   334       also have "... = ?t" using h_e.th_kept the_preced_def by auto
       
   335       finally show ?thesis .
       
   336     qed 
       
   337   next
       
   338     case (P thread cs)
       
   339     with Cons
       
   340     show ?thesis by (auto simp:preced_def the_preced_def)
       
   341   next
       
   342     case (V thread cs)
       
   343     with Cons
       
   344     show ?thesis by (auto simp:preced_def the_preced_def)
       
   345   next 
       
   346     case (Set thread prio')
       
   347     show ?thesis (is "Max (?f ` ?A) = ?t")
       
   348     proof -
       
   349       have "Max (?f ` ?A) = ?f th"
       
   350       proof(rule image_Max_eqI)
       
   351         show "finite ?A" using h_e.finite_threads by auto 
       
   352       next
       
   353         show "th \<in> ?A" using h_e.th_kept by auto 
       
   354       next
       
   355         show "\<forall>x\<in>?A. ?f x \<le> ?f th"
       
   356         proof 
       
   357           fix x
       
   358           assume h: "x \<in> ?A"
       
   359           show "?f x \<le> ?f th"
       
   360           proof(cases "x = thread")
       
   361             case True
       
   362             moreover have "the_preced (Set thread prio' # t @ s) thread \<le> the_preced (t @ s) th"
       
   363             proof -
       
   364               have "the_preced (t @ s) th = Prc prio tm"  
       
   365                 using h_t.th_kept preced_th by (simp add:the_preced_def)
       
   366               moreover have "prio' \<le> prio" using Set h_e.set_diff_low by auto
       
   367               ultimately show ?thesis by (insert lt_tm, auto simp:the_preced_def preced_def)
       
   368             qed
       
   369             ultimately show ?thesis
       
   370               by (unfold Set, simp add:the_preced_def preced_def)
       
   371           next
       
   372             case False
       
   373             then have "?f x  = the_preced (t@s) x"
       
   374               by (simp add:the_preced_def preced_def Set)
       
   375             also have "... \<le> Max (the_preced (t@s) ` threads (t@s))"
       
   376               using Set h h_t.finite_threads by auto 
       
   377             also have "... = ?f th" by (metis Cons.hyps(5) h_e.th_kept the_preced_def) 
       
   378             finally show ?thesis .
       
   379           qed
       
   380         qed
       
   381       qed
       
   382       also have "... = ?t" using h_e.th_kept the_preced_def by auto
       
   383       finally show ?thesis .
       
   384     qed 
       
   385   qed
       
   386 qed
       
   387 
       
   388 lemma max_preced: "preced th (t@s) = Max (the_preced (t@s) ` (threads (t@s)))"
       
   389   by (insert th_kept max_kept, auto)
       
   390 
       
   391 text {*
       
   392   The reason behind the following lemma is that:
       
   393   Since @{term "cp"} is defined as the maximum precedence 
       
   394   of those threads contained in the sub-tree of node @{term "Th th"} 
       
   395   in @{term "RAG (t@s)"}, and all these threads are living threads, and 
       
   396   @{term "th"} is also among them, the maximum precedence of 
       
   397   them all must be the one for @{text "th"}.
       
   398 *}
       
   399 lemma th_cp_max_preced: 
       
   400   "cp (t@s) th = Max (the_preced (t@s) ` (threads (t@s)))" (is "?L = ?R") 
       
   401 proof -
       
   402   let ?f = "the_preced (t@s)"
       
   403   have "?L = ?f th"
       
   404   proof(unfold cp_alt_def, rule image_Max_eqI)
       
   405     show "finite {th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}"
       
   406     proof -
       
   407       have "{th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)} = 
       
   408             the_thread ` {n . n \<in> subtree (RAG (t @ s)) (Th th) \<and>
       
   409                             (\<exists> th'. n = Th th')}"
       
   410       by (smt Collect_cong Setcompr_eq_image mem_Collect_eq the_thread.simps)
       
   411       moreover have "finite ..." by (simp add: vat_t.fsbtRAGs.finite_subtree) 
       
   412       ultimately show ?thesis by simp
       
   413     qed
       
   414   next
       
   415     show "th \<in> {th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}"
       
   416       by (auto simp:subtree_def)
       
   417   next
       
   418     show "\<forall>x\<in>{th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}.
       
   419                the_preced (t @ s) x \<le> the_preced (t @ s) th"
       
   420     proof
       
   421       fix th'
       
   422       assume "th' \<in> {th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}"
       
   423       hence "Th th' \<in> subtree (RAG (t @ s)) (Th th)" by auto
       
   424       moreover have "... \<subseteq> Field (RAG (t @ s)) \<union> {Th th}"
       
   425         by (meson subtree_Field)
       
   426       ultimately have "Th th' \<in> ..." by auto
       
   427       hence "th' \<in> threads (t@s)" 
       
   428       proof
       
   429         assume "Th th' \<in> {Th th}"
       
   430         thus ?thesis using th_kept by auto 
       
   431       next
       
   432         assume "Th th' \<in> Field (RAG (t @ s))"
       
   433         thus ?thesis using vat_t.not_in_thread_isolated by blast 
       
   434       qed
       
   435       thus "the_preced (t @ s) th' \<le> the_preced (t @ s) th"
       
   436         by (metis Max_ge finite_imageI finite_threads image_eqI 
       
   437                max_kept th_kept the_preced_def)
       
   438     qed
       
   439   qed
       
   440   also have "... = ?R" by (simp add: max_preced the_preced_def) 
       
   441   finally show ?thesis .
       
   442 qed
       
   443 
       
   444 lemma th_cp_max[simp]: "Max (cp (t@s) ` threads (t@s)) = cp (t@s) th"
       
   445   using max_cp_eq th_cp_max_preced the_preced_def vt_t by presburger
       
   446 
       
   447 lemma [simp]: "Max (cp (t@s) ` threads (t@s)) = Max (the_preced (t@s) ` threads (t@s))"
       
   448   by (simp add: th_cp_max_preced)
       
   449   
       
   450 lemma [simp]: "Max (the_preced (t@s) ` threads (t@s)) = the_preced (t@s) th"
       
   451   using max_kept th_kept the_preced_def by auto
       
   452 
       
   453 lemma [simp]: "the_preced (t@s) th = preced th (t@s)"
       
   454   using the_preced_def by auto
       
   455 
       
   456 lemma [simp]: "preced th (t@s) = preced th s"
       
   457   by (simp add: th_kept)
       
   458 
       
   459 lemma [simp]: "cp s th = preced th s"
       
   460   by (simp add: eq_cp_s_th)
       
   461 
       
   462 lemma th_cp_preced [simp]: "cp (t@s) th = preced th s"
       
   463   by (fold max_kept, unfold th_cp_max_preced, simp)
       
   464 
       
   465 lemma preced_less:
       
   466   assumes th'_in: "th' \<in> threads s"
       
   467   and neq_th': "th' \<noteq> th"
       
   468   shows "preced th' s < preced th s"
       
   469   using assms
       
   470 by (metis Max.coboundedI finite_imageI highest not_le order.trans 
       
   471     preced_linorder rev_image_eqI threads_s vat_s.finite_threads 
       
   472     vat_s.le_cp)
       
   473 
       
   474 section {* The `blocking thread` *}
       
   475 
       
   476 text {* 
       
   477   The purpose of PIP is to ensure that the most 
       
   478   urgent thread @{term th} is not blocked unreasonably. 
       
   479   Therefore, a clear picture of the blocking thread is essential 
       
   480   to assure people that the purpose is fulfilled. 
       
   481   
       
   482   In this section, we are going to derive a series of lemmas 
       
   483   with finally give rise to a picture of the blocking thread. 
       
   484 
       
   485   By `blocking thread`, we mean a thread in running state but 
       
   486   different from thread @{term th}.
       
   487 *}
       
   488 
       
   489 text {*
       
   490   The following lemmas shows that the @{term cp}-value 
       
   491   of the blocking thread @{text th'} equals to the highest
       
   492   precedence in the whole system.
       
   493 *}
       
   494 lemma runing_preced_inversion:
       
   495   assumes runing': "th' \<in> runing (t@s)"
       
   496   shows "cp (t@s) th' = preced th s" (is "?L = ?R")
       
   497 proof -
       
   498   have "?L = Max (cp (t @ s) ` readys (t @ s))" using assms
       
   499       by (unfold runing_def, auto)
       
   500   also have "\<dots> = ?R"
       
   501       by (metis th_cp_max th_cp_preced vat_t.max_cp_readys_threads) 
       
   502   finally show ?thesis .
       
   503 qed
       
   504 
       
   505 text {*
       
   506 
       
   507   The following lemma shows how the counters for @{term "P"} and
       
   508   @{term "V"} operations relate to the running threads in the states
       
   509   @{term s} and @{term "t @ s"}.  The lemma shows that if a thread's
       
   510   @{term "P"}-count equals its @{term "V"}-count (which means it no
       
   511   longer has any resource in its possession), it cannot be a running
       
   512   thread.
       
   513 
       
   514   The proof is by contraction with the assumption @{text "th' \<noteq> th"}.
       
   515   The key is the use of @{thm count_eq_dependants} to derive the
       
   516   emptiness of @{text th'}s @{term dependants}-set from the balance of
       
   517   its @{term P} and @{term V} counts.  From this, it can be shown
       
   518   @{text th'}s @{term cp}-value equals to its own precedence.
       
   519 
       
   520   On the other hand, since @{text th'} is running, by @{thm
       
   521   runing_preced_inversion}, its @{term cp}-value equals to the
       
   522   precedence of @{term th}.
       
   523 
       
   524   Combining the above two resukts we have that @{text th'} and @{term
       
   525   th} have the same precedence. By uniqueness of precedences, we have
       
   526   @{text "th' = th"}, which is in contradiction with the assumption
       
   527   @{text "th' \<noteq> th"}.
       
   528 
       
   529 *} 
       
   530                       
       
   531 lemma eq_pv_blocked: (* ddd *)
       
   532   assumes neq_th': "th' \<noteq> th"
       
   533   and eq_pv: "cntP (t@s) th' = cntV (t@s) th'"
       
   534   shows "th' \<notin> runing (t@s)"
       
   535 proof
       
   536   assume otherwise: "th' \<in> runing (t@s)"
       
   537   show False
       
   538   proof -
       
   539     have th'_in: "th' \<in> threads (t@s)"
       
   540         using otherwise readys_threads runing_def by auto 
       
   541     have "th' = th"
       
   542     proof(rule preced_unique)
       
   543       -- {* The proof goes like this: 
       
   544             it is first shown that the @{term preced}-value of @{term th'} 
       
   545             equals to that of @{term th}, then by uniqueness 
       
   546             of @{term preced}-values (given by lemma @{thm preced_unique}), 
       
   547             @{term th'} equals to @{term th}: *}
       
   548       show "preced th' (t @ s) = preced th (t @ s)" (is "?L = ?R")
       
   549       proof -
       
   550         -- {* Since the counts of @{term th'} are balanced, the subtree
       
   551               of it contains only itself, so, its @{term cp}-value
       
   552               equals its @{term preced}-value: *}
       
   553         have "?L = cp (t@s) th'"
       
   554           by (unfold cp_eq_cpreced cpreced_def count_eq_dependants[OF eq_pv], simp)
       
   555         -- {* Since @{term "th'"} is running, by @{thm runing_preced_inversion},
       
   556               its @{term cp}-value equals @{term "preced th s"}, 
       
   557               which equals to @{term "?R"} by simplification: *}
       
   558         also have "... = ?R" 
       
   559         thm runing_preced_inversion
       
   560             using runing_preced_inversion[OF otherwise] by simp
       
   561         finally show ?thesis .
       
   562       qed
       
   563     qed (auto simp: th'_in th_kept)
       
   564     with `th' \<noteq> th` show ?thesis by simp
       
   565  qed
       
   566 qed
       
   567 
       
   568 text {*
       
   569   The following lemma is the extrapolation of @{thm eq_pv_blocked}.
       
   570   It says if a thread, different from @{term th}, 
       
   571   does not hold any resource at the very beginning,
       
   572   it will keep hand-emptied in the future @{term "t@s"}.
       
   573 *}
       
   574 lemma eq_pv_persist: (* ddd *)
       
   575   assumes neq_th': "th' \<noteq> th"
       
   576   and eq_pv: "cntP s th' = cntV s th'"
       
   577   shows "cntP (t@s) th' = cntV (t@s) th'"
       
   578 proof(induction rule:ind) -- {* The proof goes by induction. *}
       
   579   -- {* The nontrivial case is for the @{term Cons}: *}
       
   580   case (Cons e t)
       
   581   -- {* All results derived so far hold for both @{term s} and @{term "t@s"}: *}
       
   582   interpret vat_t: extend_highest_gen s th prio tm t using Cons by simp
       
   583   interpret vat_e: extend_highest_gen s th prio tm "(e # t)" using Cons by simp
       
   584   show ?case
       
   585   proof -
       
   586     -- {* It can be proved that @{term cntP}-value of @{term th'} does not change
       
   587           by the happening of event @{term e}: *}
       
   588     have "cntP ((e#t)@s) th' = cntP (t@s) th'"
       
   589     proof(rule ccontr) -- {* Proof by contradiction. *}
       
   590       -- {* Suppose @{term cntP}-value of @{term th'} is changed by @{term e}: *}
       
   591       assume otherwise: "cntP ((e # t) @ s) th' \<noteq> cntP (t @ s) th'"
       
   592       -- {* Then the actor of @{term e} must be @{term th'} and @{term e}
       
   593             must be a @{term P}-event: *}
       
   594       hence "isP e" "actor e = th'" by (auto simp:cntP_diff_inv) 
       
   595       with vat_t.actor_inv[OF Cons(2)]
       
   596       -- {* According to @{thm actor_inv}, @{term th'} must be running at 
       
   597             the moment @{term "t@s"}: *}
       
   598       have "th' \<in> runing (t@s)" by (cases e, auto)
       
   599       -- {* However, an application of @{thm eq_pv_blocked} to induction hypothesis
       
   600             shows @{term th'} can not be running at moment  @{term "t@s"}: *}
       
   601       moreover have "th' \<notin> runing (t@s)" 
       
   602                using vat_t.eq_pv_blocked[OF neq_th' Cons(5)] .
       
   603       -- {* Contradiction is finally derived: *}
       
   604       ultimately show False by simp
       
   605     qed
       
   606     -- {* It can also be proved that @{term cntV}-value of @{term th'} does not change
       
   607           by the happening of event @{term e}: *}
       
   608     -- {* The proof follows exactly the same pattern as the case for @{term cntP}-value: *}
       
   609     moreover have "cntV ((e#t)@s) th' = cntV (t@s) th'"
       
   610     proof(rule ccontr) -- {* Proof by contradiction. *}
       
   611       assume otherwise: "cntV ((e # t) @ s) th' \<noteq> cntV (t @ s) th'"
       
   612       hence "isV e" "actor e = th'" by (auto simp:cntV_diff_inv) 
       
   613       with vat_t.actor_inv[OF Cons(2)]
       
   614       have "th' \<in> runing (t@s)" by (cases e, auto)
       
   615       moreover have "th' \<notin> runing (t@s)"
       
   616           using vat_t.eq_pv_blocked[OF neq_th' Cons(5)] .
       
   617       ultimately show False by simp
       
   618     qed
       
   619     -- {* Finally, it can be shown that the @{term cntP} and @{term cntV} 
       
   620           value for @{term th'} are still in balance, so @{term th'} 
       
   621           is still hand-emptied after the execution of event @{term e}: *}
       
   622     ultimately show ?thesis using Cons(5) by metis
       
   623   qed
       
   624 qed (auto simp:eq_pv)
       
   625 
       
   626 text {*
       
   627   By combining @{thm  eq_pv_blocked} and @{thm eq_pv_persist},
       
   628   it can be derived easily that @{term th'} can not be running in the future:
       
   629 *}
       
   630 lemma eq_pv_blocked_persist:
       
   631   assumes neq_th': "th' \<noteq> th"
       
   632   and eq_pv: "cntP s th' = cntV s th'"
       
   633   shows "th' \<notin> runing (t@s)"
       
   634   using assms
       
   635   by (simp add: eq_pv_blocked eq_pv_persist) 
       
   636 
       
   637 text {*
       
   638   The following lemma shows the blocking thread @{term th'}
       
   639   must hold some resource in the very beginning. 
       
   640 *}
       
   641 lemma runing_cntP_cntV_inv: (* ddd *)
       
   642   assumes is_runing: "th' \<in> runing (t@s)"
       
   643   and neq_th': "th' \<noteq> th"
       
   644   shows "cntP s th' > cntV s th'"
       
   645   using assms
       
   646 proof -
       
   647   -- {* First, it can be shown that the number of @{term P} and
       
   648         @{term V} operations can not be equal for thred @{term th'} *}
       
   649   have "cntP s th' \<noteq> cntV s th'"
       
   650   proof
       
   651      -- {* The proof goes by contradiction, suppose otherwise: *}
       
   652     assume otherwise: "cntP s th' = cntV s th'"
       
   653     -- {* By applying @{thm  eq_pv_blocked_persist} to this: *}
       
   654     from eq_pv_blocked_persist[OF neq_th' otherwise] 
       
   655     -- {* we have that @{term th'} can not be running at moment @{term "t@s"}: *}
       
   656     have "th' \<notin> runing (t@s)" .
       
   657     -- {* This is obvious in contradiction with assumption @{thm is_runing}  *}
       
   658     thus False using is_runing by simp
       
   659   qed
       
   660   -- {* However, the number of @{term V} is always less or equal to @{term P}: *}
       
   661   moreover have "cntV s th' \<le> cntP s th'" using vat_s.cnp_cnv_cncs by auto
       
   662   -- {* Thesis is finally derived by combining the these two results: *}
       
   663   ultimately show ?thesis by auto
       
   664 qed
       
   665 
       
   666 
       
   667 text {*
       
   668   The following lemmas shows the blocking thread @{text th'} must be live 
       
   669   at the very beginning, i.e. the moment (or state) @{term s}. 
       
   670 
       
   671   The proof is a  simple combination of the results above:
       
   672 *}
       
   673 lemma runing_threads_inv: 
       
   674   assumes runing': "th' \<in> runing (t@s)"
       
   675   and neq_th': "th' \<noteq> th"
       
   676   shows "th' \<in> threads s"
       
   677 proof(rule ccontr) -- {* Proof by contradiction: *}
       
   678   assume otherwise: "th' \<notin> threads s" 
       
   679   have "th' \<notin> runing (t @ s)"
       
   680   proof -
       
   681     from vat_s.cnp_cnv_eq[OF otherwise]
       
   682     have "cntP s th' = cntV s th'" .
       
   683     from eq_pv_blocked_persist[OF neq_th' this]
       
   684     show ?thesis .
       
   685   qed
       
   686   with runing' show False by simp
       
   687 qed
       
   688 
       
   689 text {*
       
   690   The following lemma summarizes several foregoing 
       
   691   lemmas to give an overall picture of the blocking thread @{text "th'"}:
       
   692 *}
       
   693 lemma runing_inversion: (* ddd, one of the main lemmas to present *)
       
   694   assumes runing': "th' \<in> runing (t@s)"
       
   695   and neq_th: "th' \<noteq> th"
       
   696   shows "th' \<in> threads s"
       
   697   and    "\<not>detached s th'"
       
   698   and    "cp (t@s) th' = preced th s"
       
   699 proof -
       
   700   from runing_threads_inv[OF assms]
       
   701   show "th' \<in> threads s" .
       
   702 next
       
   703   from runing_cntP_cntV_inv[OF runing' neq_th]
       
   704   show "\<not>detached s th'" using vat_s.detached_eq by simp
       
   705 next
       
   706   from runing_preced_inversion[OF runing']
       
   707   show "cp (t@s) th' = preced th s" .
       
   708 qed
       
   709 
       
   710 section {* The existence of `blocking thread` *}
       
   711 
       
   712 text {* 
       
   713   Suppose @{term th} is not running, it is first shown that
       
   714   there is a path in RAG leading from node @{term th} to another thread @{text "th'"} 
       
   715   in the @{term readys}-set (So @{text "th'"} is an ancestor of @{term th}}).
       
   716 
       
   717   Now, since @{term readys}-set is non-empty, there must be
       
   718   one in it which holds the highest @{term cp}-value, which, by definition, 
       
   719   is the @{term runing}-thread. However, we are going to show more: this running thread
       
   720   is exactly @{term "th'"}.
       
   721      *}
       
   722 lemma th_blockedE: (* ddd, the other main lemma to be presented: *)
       
   723   assumes "th \<notin> runing (t@s)"
       
   724   obtains th' where "Th th' \<in> ancestors (RAG (t @ s)) (Th th)"
       
   725                     "th' \<in> runing (t@s)"
       
   726 proof -
       
   727   -- {* According to @{thm vat_t.th_chain_to_ready}, either 
       
   728         @{term "th"} is in @{term "readys"} or there is path leading from it to 
       
   729         one thread in @{term "readys"}. *}
       
   730   have "th \<in> readys (t @ s) \<or> (\<exists>th'. th' \<in> readys (t @ s) \<and> (Th th, Th th') \<in> (RAG (t @ s))\<^sup>+)" 
       
   731     using th_kept vat_t.th_chain_to_ready by auto
       
   732   -- {* However, @{term th} can not be in @{term readys}, because otherwise, since 
       
   733        @{term th} holds the highest @{term cp}-value, it must be @{term "runing"}. *}
       
   734   moreover have "th \<notin> readys (t@s)" 
       
   735     using assms runing_def th_cp_max vat_t.max_cp_readys_threads by auto 
       
   736   -- {* So, there must be a path from @{term th} to another thread @{text "th'"} in 
       
   737         term @{term readys}: *}
       
   738   ultimately obtain th' where th'_in: "th' \<in> readys (t@s)"
       
   739                           and dp: "(Th th, Th th') \<in> (RAG (t @ s))\<^sup>+" by auto
       
   740   -- {* We are going to show that this @{term th'} is running. *}
       
   741   have "th' \<in> runing (t@s)"
       
   742   proof -
       
   743     -- {* We only need to show that this @{term th'} holds the highest @{term cp}-value: *}
       
   744     have "cp (t@s) th' = Max (cp (t@s) ` readys (t@s))" (is "?L = ?R")
       
   745     proof -
       
   746       have "?L =  Max ((the_preced (t @ s) \<circ> the_thread) ` subtree (tRAG (t @ s)) (Th th'))"
       
   747         by (unfold cp_alt_def1, simp)
       
   748       also have "... = (the_preced (t @ s) \<circ> the_thread) (Th th)"
       
   749       proof(rule image_Max_subset)
       
   750         show "finite (Th ` (threads (t@s)))" by (simp add: vat_t.finite_threads)
       
   751       next
       
   752         show "subtree (tRAG (t @ s)) (Th th') \<subseteq> Th ` threads (t @ s)"
       
   753           by (metis Range.intros dp trancl_range vat_t.range_in vat_t.subtree_tRAG_thread) 
       
   754       next
       
   755         show "Th th \<in> subtree (tRAG (t @ s)) (Th th')" using dp
       
   756                     by (unfold tRAG_subtree_eq, auto simp:subtree_def)
       
   757       next
       
   758         show "Max ((the_preced (t @ s) \<circ> the_thread) ` Th ` threads (t @ s)) =
       
   759                       (the_preced (t @ s) \<circ> the_thread) (Th th)" (is "Max ?L = _")
       
   760         proof -
       
   761           have "?L = the_preced (t @ s) `  threads (t @ s)" 
       
   762                      by (unfold image_comp, rule image_cong, auto)
       
   763           thus ?thesis using max_preced the_preced_def by auto
       
   764         qed
       
   765       qed
       
   766       also have "... = ?R"
       
   767         using th_cp_max th_cp_preced th_kept 
       
   768               the_preced_def vat_t.max_cp_readys_threads by auto
       
   769       finally show ?thesis .
       
   770     qed 
       
   771     -- {* Now, since @{term th'} holds the highest @{term cp} 
       
   772           and we have already show it is in @{term readys},
       
   773           it is @{term runing} by definition. *}
       
   774     with `th' \<in> readys (t@s)` show ?thesis by (simp add: runing_def) 
       
   775   qed
       
   776   -- {* It is easy to show @{term th'} is an ancestor of @{term th}: *}
       
   777   moreover have "Th th' \<in> ancestors (RAG (t @ s)) (Th th)" 
       
   778     using `(Th th, Th th') \<in> (RAG (t @ s))\<^sup>+` by (auto simp:ancestors_def)
       
   779   ultimately show ?thesis using that by metis
       
   780 qed
       
   781 
       
   782 text {*
       
   783   Now it is easy to see there is always a thread to run by case analysis
       
   784   on whether thread @{term th} is running: if the answer is Yes, the 
       
   785   the running thread is obviously @{term th} itself; otherwise, the running
       
   786   thread is the @{text th'} given by lemma @{thm th_blockedE}.
       
   787 *}
       
   788 lemma live: "runing (t@s) \<noteq> {}"
       
   789 proof(cases "th \<in> runing (t@s)") 
       
   790   case True thus ?thesis by auto
       
   791 next
       
   792   case False
       
   793   thus ?thesis using th_blockedE by auto
       
   794 qed
       
   795 
       
   796 
       
   797 end
       
   798 end
       
   799 =======
       
   800 theory Correctness
       
   801 imports PIPBasics
       
   802 begin
       
   803 
       
   804 
       
   805 text {* 
       
   806   The following two auxiliary lemmas are used to reason about @{term Max}.
       
   807 *}
       
   808 lemma image_Max_eqI: 
       
   809   assumes "finite B"
       
   810   and "b \<in> B"
       
   811   and "\<forall> x \<in> B. f x \<le> f b"
       
   812   shows "Max (f ` B) = f b"
       
   813   using assms
       
   814   using Max_eqI by blast 
       
   815 
       
   816 lemma image_Max_subset:
       
   817   assumes "finite A"
       
   818   and "B \<subseteq> A"
       
   819   and "a \<in> B"
       
   820   and "Max (f ` A) = f a"
       
   821   shows "Max (f ` B) = f a"
       
   822 proof(rule image_Max_eqI)
       
   823   show "finite B"
       
   824     using assms(1) assms(2) finite_subset by auto 
       
   825 next
       
   826   show "a \<in> B" using assms by simp
       
   827 next
       
   828   show "\<forall>x\<in>B. f x \<le> f a"
       
   829     by (metis Max_ge assms(1) assms(2) assms(4) 
       
   830             finite_imageI image_eqI subsetCE) 
       
   831 qed
       
   832 
       
   833 text {*
       
   834   The following locale @{text "highest_gen"} sets the basic context for our
       
   835   investigation: supposing thread @{text th} holds the highest @{term cp}-value
       
   836   in state @{text s}, which means the task for @{text th} is the 
       
   837   most urgent. We want to show that  
       
   838   @{text th} is treated correctly by PIP, which means
       
   839   @{text th} will not be blocked unreasonably by other less urgent
       
   840   threads. 
       
   841 *}
       
   842 locale highest_gen =
       
   843   fixes s th prio tm
       
   844   assumes vt_s: "vt s"
       
   845   and threads_s: "th \<in> threads s"
       
   846   and highest: "preced th s = Max ((cp s)`threads s)"
       
   847   -- {* The internal structure of @{term th}'s precedence is exposed:*}
       
   848   and preced_th: "preced th s = Prc prio tm" 
       
   849 
       
   850 -- {* @{term s} is a valid trace, so it will inherit all results derived for
       
   851       a valid trace: *}
       
   852 sublocale highest_gen < vat_s: valid_trace "s"
       
   853   by (unfold_locales, insert vt_s, simp)
       
   854 
       
   855 context highest_gen
       
   856 begin
       
   857 
       
   858 text {*
       
   859   @{term tm} is the time when the precedence of @{term th} is set, so 
       
   860   @{term tm} must be a valid moment index into @{term s}.
       
   861 *}
       
   862 lemma lt_tm: "tm < length s"
       
   863   by (insert preced_tm_lt[OF threads_s preced_th], simp)
       
   864 
       
   865 text {*
       
   866   Since @{term th} holds the highest precedence and @{text "cp"}
       
   867   is the highest precedence of all threads in the sub-tree of 
       
   868   @{text "th"} and @{text th} is among these threads, 
       
   869   its @{term cp} must equal to its precedence:
       
   870 *}
       
   871 lemma eq_cp_s_th: "cp s th = preced th s" (is "?L = ?R")
       
   872 proof -
       
   873   have "?L \<le> ?R"
       
   874   by (unfold highest, rule Max_ge, 
       
   875         auto simp:threads_s finite_threads)
       
   876   moreover have "?R \<le> ?L"
       
   877     by (unfold vat_s.cp_rec, rule Max_ge, 
       
   878         auto simp:the_preced_def vat_s.fsbttRAGs.finite_children)
       
   879   ultimately show ?thesis by auto
       
   880 qed
       
   881 
       
   882 lemma highest_cp_preced: "cp s th = Max (the_preced s ` threads s)"
       
   883   using eq_cp_s_th highest max_cp_eq the_preced_def by presburger
       
   884   
       
   885 
       
   886 lemma highest_preced_thread: "preced th s = Max (the_preced s ` threads s)"
       
   887   by (fold eq_cp_s_th, unfold highest_cp_preced, simp)
       
   888 
       
   889 lemma highest': "cp s th = Max (cp s ` threads s)"
       
   890   by (simp add: eq_cp_s_th highest)
       
   891 
       
   892 end
       
   893 
       
   894 locale extend_highest_gen = highest_gen + 
       
   895   fixes t 
       
   896   assumes vt_t: "vt (t@s)"
       
   897   and create_low: "Create th' prio' \<in> set t \<Longrightarrow> prio' \<le> prio"
       
   898   and set_diff_low: "Set th' prio' \<in> set t \<Longrightarrow> th' \<noteq> th \<and> prio' \<le> prio"
       
   899   and exit_diff: "Exit th' \<in> set t \<Longrightarrow> th' \<noteq> th"
       
   900 
       
   901 sublocale extend_highest_gen < vat_t: valid_trace "t@s"
       
   902   by (unfold_locales, insert vt_t, simp)
       
   903 
       
   904 lemma step_back_vt_app: 
       
   905   assumes vt_ts: "vt (t@s)" 
       
   906   shows "vt s"
       
   907 proof -
       
   908   from vt_ts show ?thesis
       
   909   proof(induct t)
       
   910     case Nil
       
   911     from Nil show ?case by auto
       
   912   next
       
   913     case (Cons e t)
       
   914     assume ih: " vt (t @ s) \<Longrightarrow> vt s"
       
   915       and vt_et: "vt ((e # t) @ s)"
       
   916     show ?case
       
   917     proof(rule ih)
       
   918       show "vt (t @ s)"
       
   919       proof(rule step_back_vt)
       
   920         from vt_et show "vt (e # t @ s)" by simp
       
   921       qed
       
   922     qed
       
   923   qed
       
   924 qed
       
   925 
       
   926 (* locale red_extend_highest_gen = extend_highest_gen +
       
   927    fixes i::nat
       
   928 *)
       
   929 
       
   930 (*
       
   931 sublocale red_extend_highest_gen <   red_moment: extend_highest_gen "s" "th" "prio" "tm" "(moment i t)"
       
   932   apply (insert extend_highest_gen_axioms, subst (asm) (1) moment_restm_s [of i t, symmetric])
       
   933   apply (unfold extend_highest_gen_def extend_highest_gen_axioms_def, clarsimp)
       
   934   by (unfold highest_gen_def, auto dest:step_back_vt_app)
       
   935 *)
       
   936 
       
   937 context extend_highest_gen
       
   938 begin
       
   939 
       
   940  lemma ind [consumes 0, case_names Nil Cons, induct type]:
       
   941   assumes 
       
   942     h0: "R []"
       
   943   and h2: "\<And> e t. \<lbrakk>vt (t@s); step (t@s) e; 
       
   944                     extend_highest_gen s th prio tm t; 
       
   945                     extend_highest_gen s th prio tm (e#t); R t\<rbrakk> \<Longrightarrow> R (e#t)"
       
   946   shows "R t"
       
   947 proof -
       
   948   from vt_t extend_highest_gen_axioms show ?thesis
       
   949   proof(induct t)
       
   950     from h0 show "R []" .
       
   951   next
       
   952     case (Cons e t')
       
   953     assume ih: "\<lbrakk>vt (t' @ s); extend_highest_gen s th prio tm t'\<rbrakk> \<Longrightarrow> R t'"
       
   954       and vt_e: "vt ((e # t') @ s)"
       
   955       and et: "extend_highest_gen s th prio tm (e # t')"
       
   956     from vt_e and step_back_step have stp: "step (t'@s) e" by auto
       
   957     from vt_e and step_back_vt have vt_ts: "vt (t'@s)" by auto
       
   958     show ?case
       
   959     proof(rule h2 [OF vt_ts stp _ _ _ ])
       
   960       show "R t'"
       
   961       proof(rule ih)
       
   962         from et show ext': "extend_highest_gen s th prio tm t'"
       
   963           by (unfold extend_highest_gen_def extend_highest_gen_axioms_def, auto dest:step_back_vt)
       
   964       next
       
   965         from vt_ts show "vt (t' @ s)" .
       
   966       qed
       
   967     next
       
   968       from et show "extend_highest_gen s th prio tm (e # t')" .
       
   969     next
       
   970       from et show ext': "extend_highest_gen s th prio tm t'"
       
   971           by (unfold extend_highest_gen_def extend_highest_gen_axioms_def, auto dest:step_back_vt)
       
   972     qed
       
   973   qed
       
   974 qed
       
   975 
       
   976 
       
   977 lemma th_kept: "th \<in> threads (t @ s) \<and> 
       
   978                  preced th (t@s) = preced th s" (is "?Q t") 
       
   979 proof -
       
   980   show ?thesis
       
   981   proof(induct rule:ind)
       
   982     case Nil
       
   983     from threads_s
       
   984     show ?case
       
   985       by auto
       
   986   next
       
   987     case (Cons e t)
       
   988     interpret h_e: extend_highest_gen _ _ _ _ "(e # t)" using Cons by auto
       
   989     interpret h_t: extend_highest_gen _ _ _ _ t using Cons by auto
       
   990     show ?case
       
   991     proof(cases e)
       
   992       case (Create thread prio)
       
   993       show ?thesis
       
   994       proof -
       
   995         from Cons and Create have "step (t@s) (Create thread prio)" by auto
       
   996         hence "th \<noteq> thread"
       
   997         proof(cases)
       
   998           case thread_create
       
   999           with Cons show ?thesis by auto
       
  1000         qed
       
  1001         hence "preced th ((e # t) @ s)  = preced th (t @ s)"
       
  1002           by (unfold Create, auto simp:preced_def)
       
  1003         moreover note Cons
       
  1004         ultimately show ?thesis
       
  1005           by (auto simp:Create)
       
  1006       qed
       
  1007     next
       
  1008       case (Exit thread)
       
  1009       from h_e.exit_diff and Exit
       
  1010       have neq_th: "thread \<noteq> th" by auto
       
  1011       with Cons
       
  1012       show ?thesis
       
  1013         by (unfold Exit, auto simp:preced_def)
       
  1014     next
       
  1015       case (P thread cs)
       
  1016       with Cons
       
  1017       show ?thesis 
       
  1018         by (auto simp:P preced_def)
       
  1019     next
       
  1020       case (V thread cs)
       
  1021       with Cons
       
  1022       show ?thesis 
       
  1023         by (auto simp:V preced_def)
       
  1024     next
       
  1025       case (Set thread prio')
       
  1026       show ?thesis
       
  1027       proof -
       
  1028         from h_e.set_diff_low and Set
       
  1029         have "th \<noteq> thread" by auto
       
  1030         hence "preced th ((e # t) @ s)  = preced th (t @ s)"
       
  1031           by (unfold Set, auto simp:preced_def)
       
  1032         moreover note Cons
       
  1033         ultimately show ?thesis
       
  1034           by (auto simp:Set)
       
  1035       qed
       
  1036     qed
       
  1037   qed
       
  1038 qed
       
  1039 
       
  1040 text {*
       
  1041   According to @{thm th_kept}, thread @{text "th"} has its living status
       
  1042   and precedence kept along the way of @{text "t"}. The following lemma
       
  1043   shows that this preserved precedence of @{text "th"} remains as the highest
       
  1044   along the way of @{text "t"}.
       
  1045 
       
  1046   The proof goes by induction over @{text "t"} using the specialized
       
  1047   induction rule @{thm ind}, followed by case analysis of each possible 
       
  1048   operations of PIP. All cases follow the same pattern rendered by the 
       
  1049   generalized introduction rule @{thm "image_Max_eqI"}. 
       
  1050 
       
  1051   The very essence is to show that precedences, no matter whether they 
       
  1052   are newly introduced or modified, are always lower than the one held 
       
  1053   by @{term "th"}, which by @{thm th_kept} is preserved along the way.
       
  1054 *}
       
  1055 lemma max_kept: "Max (the_preced (t @ s) ` (threads (t@s))) = preced th s"
       
  1056 proof(induct rule:ind)
       
  1057   case Nil
       
  1058   from highest_preced_thread
       
  1059   show ?case by simp
       
  1060 next
       
  1061   case (Cons e t)
       
  1062     interpret h_e: extend_highest_gen _ _ _ _ "(e # t)" using Cons by auto
       
  1063     interpret h_t: extend_highest_gen _ _ _ _ t using Cons by auto
       
  1064   show ?case
       
  1065   proof(cases e)
       
  1066     case (Create thread prio')
       
  1067     show ?thesis (is "Max (?f ` ?A) = ?t")
       
  1068     proof -
       
  1069       -- {* The following is the common pattern of each branch of the case analysis. *}
       
  1070       -- {* The major part is to show that @{text "th"} holds the highest precedence: *}
       
  1071       have "Max (?f ` ?A) = ?f th"
       
  1072       proof(rule image_Max_eqI)
       
  1073         show "finite ?A" using h_e.finite_threads by auto 
       
  1074       next
       
  1075         show "th \<in> ?A" using h_e.th_kept by auto 
       
  1076       next
       
  1077         show "\<forall>x\<in>?A. ?f x \<le> ?f th"
       
  1078         proof 
       
  1079           fix x
       
  1080           assume "x \<in> ?A"
       
  1081           hence "x = thread \<or> x \<in> threads (t@s)" by (auto simp:Create)
       
  1082           thus "?f x \<le> ?f th"
       
  1083           proof
       
  1084             assume "x = thread"
       
  1085             thus ?thesis 
       
  1086               apply (simp add:Create the_preced_def preced_def, fold preced_def)
       
  1087               using Create h_e.create_low h_t.th_kept lt_tm preced_leI2 
       
  1088               preced_th by force
       
  1089           next
       
  1090             assume h: "x \<in> threads (t @ s)"
       
  1091             from Cons(2)[unfolded Create] 
       
  1092             have "x \<noteq> thread" using h by (cases, auto)
       
  1093             hence "?f x = the_preced (t@s) x" 
       
  1094               by (simp add:Create the_preced_def preced_def)
       
  1095             hence "?f x \<le> Max (the_preced (t@s) ` threads (t@s))"
       
  1096               by (simp add: h_t.finite_threads h)
       
  1097             also have "... = ?f th"
       
  1098               by (metis Cons.hyps(5) h_e.th_kept the_preced_def) 
       
  1099             finally show ?thesis .
       
  1100           qed
       
  1101         qed
       
  1102       qed
       
  1103      -- {* The minor part is to show that the precedence of @{text "th"} 
       
  1104            equals to preserved one, given by the foregoing lemma @{thm th_kept} *}
       
  1105       also have "... = ?t" using h_e.th_kept the_preced_def by auto
       
  1106       -- {* Then it follows trivially that the precedence preserved
       
  1107             for @{term "th"} remains the maximum of all living threads along the way. *}
       
  1108       finally show ?thesis .
       
  1109     qed 
       
  1110   next 
       
  1111     case (Exit thread)
       
  1112     show ?thesis (is "Max (?f ` ?A) = ?t")
       
  1113     proof -
       
  1114       have "Max (?f ` ?A) = ?f th"
       
  1115       proof(rule image_Max_eqI)
       
  1116         show "finite ?A" using h_e.finite_threads by auto 
       
  1117       next
       
  1118         show "th \<in> ?A" using h_e.th_kept by auto 
       
  1119       next
       
  1120         show "\<forall>x\<in>?A. ?f x \<le> ?f th"
       
  1121         proof 
       
  1122           fix x
       
  1123           assume "x \<in> ?A"
       
  1124           hence "x \<in> threads (t@s)" by (simp add: Exit) 
       
  1125           hence "?f x \<le> Max (?f ` threads (t@s))" 
       
  1126             by (simp add: h_t.finite_threads) 
       
  1127           also have "... \<le> ?f th" 
       
  1128             apply (simp add:Exit the_preced_def preced_def, fold preced_def)
       
  1129             using Cons.hyps(5) h_t.th_kept the_preced_def by auto
       
  1130           finally show "?f x \<le> ?f th" .
       
  1131         qed
       
  1132       qed
       
  1133       also have "... = ?t" using h_e.th_kept the_preced_def by auto
       
  1134       finally show ?thesis .
       
  1135     qed 
       
  1136   next
       
  1137     case (P thread cs)
       
  1138     with Cons
       
  1139     show ?thesis by (auto simp:preced_def the_preced_def)
       
  1140   next
       
  1141     case (V thread cs)
       
  1142     with Cons
       
  1143     show ?thesis by (auto simp:preced_def the_preced_def)
       
  1144   next 
       
  1145     case (Set thread prio')
       
  1146     show ?thesis (is "Max (?f ` ?A) = ?t")
       
  1147     proof -
       
  1148       have "Max (?f ` ?A) = ?f th"
       
  1149       proof(rule image_Max_eqI)
       
  1150         show "finite ?A" using h_e.finite_threads by auto 
       
  1151       next
       
  1152         show "th \<in> ?A" using h_e.th_kept by auto 
       
  1153       next
       
  1154         show "\<forall>x\<in>?A. ?f x \<le> ?f th"
       
  1155         proof 
       
  1156           fix x
       
  1157           assume h: "x \<in> ?A"
       
  1158           show "?f x \<le> ?f th"
       
  1159           proof(cases "x = thread")
       
  1160             case True
       
  1161             moreover have "the_preced (Set thread prio' # t @ s) thread \<le> the_preced (t @ s) th"
       
  1162             proof -
       
  1163               have "the_preced (t @ s) th = Prc prio tm"  
       
  1164                 using h_t.th_kept preced_th by (simp add:the_preced_def)
       
  1165               moreover have "prio' \<le> prio" using Set h_e.set_diff_low by auto
       
  1166               ultimately show ?thesis by (insert lt_tm, auto simp:the_preced_def preced_def)
       
  1167             qed
       
  1168             ultimately show ?thesis
       
  1169               by (unfold Set, simp add:the_preced_def preced_def)
       
  1170           next
       
  1171             case False
       
  1172             then have "?f x  = the_preced (t@s) x"
       
  1173               by (simp add:the_preced_def preced_def Set)
       
  1174             also have "... \<le> Max (the_preced (t@s) ` threads (t@s))"
       
  1175               using Set h h_t.finite_threads by auto 
       
  1176             also have "... = ?f th" by (metis Cons.hyps(5) h_e.th_kept the_preced_def) 
       
  1177             finally show ?thesis .
       
  1178           qed
       
  1179         qed
       
  1180       qed
       
  1181       also have "... = ?t" using h_e.th_kept the_preced_def by auto
       
  1182       finally show ?thesis .
       
  1183     qed 
       
  1184   qed
       
  1185 qed
       
  1186 
       
  1187 lemma max_preced: "preced th (t@s) = Max (the_preced (t@s) ` (threads (t@s)))"
       
  1188   by (insert th_kept max_kept, auto)
       
  1189 
       
  1190 text {*
       
  1191   The reason behind the following lemma is that:
       
  1192   Since @{term "cp"} is defined as the maximum precedence 
       
  1193   of those threads contained in the sub-tree of node @{term "Th th"} 
       
  1194   in @{term "RAG (t@s)"}, and all these threads are living threads, and 
       
  1195   @{term "th"} is also among them, the maximum precedence of 
       
  1196   them all must be the one for @{text "th"}.
       
  1197 *}
       
  1198 lemma th_cp_max_preced: 
       
  1199   "cp (t@s) th = Max (the_preced (t@s) ` (threads (t@s)))" (is "?L = ?R") 
       
  1200 proof -
       
  1201   let ?f = "the_preced (t@s)"
       
  1202   have "?L = ?f th"
       
  1203   proof(unfold cp_alt_def, rule image_Max_eqI)
       
  1204     show "finite {th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}"
       
  1205     proof -
       
  1206       have "{th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)} = 
       
  1207             the_thread ` {n . n \<in> subtree (RAG (t @ s)) (Th th) \<and>
       
  1208                             (\<exists> th'. n = Th th')}"
       
  1209       by (smt Collect_cong Setcompr_eq_image mem_Collect_eq the_thread.simps)
       
  1210       moreover have "finite ..." by (simp add: vat_t.fsbtRAGs.finite_subtree) 
       
  1211       ultimately show ?thesis by simp
       
  1212     qed
       
  1213   next
       
  1214     show "th \<in> {th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}"
       
  1215       by (auto simp:subtree_def)
       
  1216   next
       
  1217     show "\<forall>x\<in>{th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}.
       
  1218                the_preced (t @ s) x \<le> the_preced (t @ s) th"
       
  1219     proof
       
  1220       fix th'
       
  1221       assume "th' \<in> {th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}"
       
  1222       hence "Th th' \<in> subtree (RAG (t @ s)) (Th th)" by auto
       
  1223       moreover have "... \<subseteq> Field (RAG (t @ s)) \<union> {Th th}"
       
  1224         by (meson subtree_Field)
       
  1225       ultimately have "Th th' \<in> ..." by auto
       
  1226       hence "th' \<in> threads (t@s)" 
       
  1227       proof
       
  1228         assume "Th th' \<in> {Th th}"
       
  1229         thus ?thesis using th_kept by auto 
       
  1230       next
       
  1231         assume "Th th' \<in> Field (RAG (t @ s))"
       
  1232         thus ?thesis using vat_t.not_in_thread_isolated by blast 
       
  1233       qed
       
  1234       thus "the_preced (t @ s) th' \<le> the_preced (t @ s) th"
       
  1235         by (metis Max_ge finite_imageI finite_threads image_eqI 
       
  1236                max_kept th_kept the_preced_def)
       
  1237     qed
       
  1238   qed
       
  1239   also have "... = ?R" by (simp add: max_preced the_preced_def) 
       
  1240   finally show ?thesis .
       
  1241 qed
       
  1242 
       
  1243 lemma th_cp_max[simp]: "Max (cp (t@s) ` threads (t@s)) = cp (t@s) th"
       
  1244   using max_cp_eq th_cp_max_preced the_preced_def vt_t by presburger
       
  1245 
       
  1246 lemma [simp]: "Max (cp (t@s) ` threads (t@s)) = Max (the_preced (t@s) ` threads (t@s))"
       
  1247   by (simp add: th_cp_max_preced)
       
  1248   
       
  1249 lemma [simp]: "Max (the_preced (t@s) ` threads (t@s)) = the_preced (t@s) th"
       
  1250   using max_kept th_kept the_preced_def by auto
       
  1251 
       
  1252 lemma [simp]: "the_preced (t@s) th = preced th (t@s)"
       
  1253   using the_preced_def by auto
       
  1254 
       
  1255 lemma [simp]: "preced th (t@s) = preced th s"
       
  1256   by (simp add: th_kept)
       
  1257 
       
  1258 lemma [simp]: "cp s th = preced th s"
       
  1259   by (simp add: eq_cp_s_th)
       
  1260 
       
  1261 lemma th_cp_preced [simp]: "cp (t@s) th = preced th s"
       
  1262   by (fold max_kept, unfold th_cp_max_preced, simp)
       
  1263 
       
  1264 lemma preced_less:
       
  1265   assumes th'_in: "th' \<in> threads s"
       
  1266   and neq_th': "th' \<noteq> th"
       
  1267   shows "preced th' s < preced th s"
       
  1268   using assms
       
  1269 by (metis Max.coboundedI finite_imageI highest not_le order.trans 
       
  1270     preced_linorder rev_image_eqI threads_s vat_s.finite_threads 
       
  1271     vat_s.le_cp)
       
  1272 
       
  1273 section {* The `blocking thread` *}
       
  1274 
       
  1275 text {* 
       
  1276 
       
  1277   The purpose of PIP is to ensure that the most urgent thread @{term
       
  1278   th} is not blocked unreasonably. Therefore, below, we will derive
       
  1279   properties of the blocking thread. By blocking thread, we mean a
       
  1280   thread in running state t @ s, but is different from thread @{term
       
  1281   th}.
       
  1282 
       
  1283   The first lemmas shows that the @{term cp}-value of the blocking
       
  1284   thread @{text th'} equals to the highest precedence in the whole
       
  1285   system.
       
  1286 
       
  1287 *}
       
  1288 
       
  1289 lemma runing_preced_inversion:
       
  1290   assumes runing': "th' \<in> runing (t @ s)"
       
  1291   shows "cp (t @ s) th' = preced th s" 
       
  1292 proof -
       
  1293   have "cp (t @ s) th' = Max (cp (t @ s) ` readys (t @ s))" 
       
  1294     using assms by (unfold runing_def, auto)
       
  1295   also have "\<dots> = preced th s"
       
  1296     by (metis th_cp_max th_cp_preced vat_t.max_cp_readys_threads) 
       
  1297   finally show ?thesis .
       
  1298 qed
       
  1299 
       
  1300 text {*
       
  1301 
       
  1302   The next lemma shows how the counters for @{term "P"} and @{term
       
  1303   "V"} operations relate to the running threads in the states @{term
       
  1304   s} and @{term "t @ s"}: if a thread's @{term "P"}-count equals its
       
  1305   @{term "V"}-count (which means it no longer has any resource in its
       
  1306   possession), it cannot be a running thread.
       
  1307 
       
  1308   The proof is by contraction with the assumption @{text "th' \<noteq> th"}.
       
  1309   The key is the use of @{thm count_eq_dependants} to derive the
       
  1310   emptiness of @{text th'}s @{term dependants}-set from the balance of
       
  1311   its @{term P} and @{term V} counts.  From this, it can be shown
       
  1312   @{text th'}s @{term cp}-value equals to its own precedence.
       
  1313 
       
  1314   On the other hand, since @{text th'} is running, by @{thm
       
  1315   runing_preced_inversion}, its @{term cp}-value equals to the
       
  1316   precedence of @{term th}.
       
  1317 
       
  1318   Combining the above two results we have that @{text th'} and @{term
       
  1319   th} have the same precedence. By uniqueness of precedences, we have
       
  1320   @{text "th' = th"}, which is in contradiction with the assumption
       
  1321   @{text "th' \<noteq> th"}.
       
  1322 
       
  1323 *} 
       
  1324                       
       
  1325 lemma eq_pv_blocked: (* ddd *)
       
  1326   assumes neq_th': "th' \<noteq> th"
       
  1327   and eq_pv: "cntP (t @ s) th' = cntV (t @ s) th'"
       
  1328   shows "th' \<notin> runing (t @ s)"
       
  1329 proof
       
  1330   assume otherwise: "th' \<in> runing (t @ s)"
       
  1331   show False
       
  1332   proof -
       
  1333     have th'_in: "th' \<in> threads (t @ s)"
       
  1334         using otherwise readys_threads runing_def by auto 
       
  1335     have "th' = th"
       
  1336     proof(rule preced_unique)
       
  1337       -- {* The proof goes like this: 
       
  1338             it is first shown that the @{term preced}-value of @{term th'} 
       
  1339             equals to that of @{term th}, then by uniqueness 
       
  1340             of @{term preced}-values (given by lemma @{thm preced_unique}), 
       
  1341             @{term th'} equals to @{term th}: *}
       
  1342       show "preced th' (t @ s) = preced th (t @ s)" (is "?L = ?R")
       
  1343       proof -
       
  1344         -- {* Since the counts of @{term th'} are balanced, the subtree
       
  1345               of it contains only itself, so, its @{term cp}-value
       
  1346               equals its @{term preced}-value: *}
       
  1347         have "?L = cp (t @ s) th'"
       
  1348           by (unfold cp_eq_cpreced cpreced_def count_eq_dependants[OF eq_pv], simp)
       
  1349         -- {* Since @{term "th'"} is running, by @{thm runing_preced_inversion},
       
  1350               its @{term cp}-value equals @{term "preced th s"}, 
       
  1351               which equals to @{term "?R"} by simplification: *}
       
  1352         also have "... = ?R" 
       
  1353             using runing_preced_inversion[OF otherwise] by simp
       
  1354         finally show ?thesis .
       
  1355       qed
       
  1356     qed (auto simp: th'_in th_kept)
       
  1357     with `th' \<noteq> th` show ?thesis by simp
       
  1358  qed
       
  1359 qed
       
  1360 
       
  1361 text {*
       
  1362   The following lemma is the extrapolation of @{thm eq_pv_blocked}.
       
  1363   It says if a thread, different from @{term th}, 
       
  1364   does not hold any resource at the very beginning,
       
  1365   it will keep hand-emptied in the future @{term "t@s"}.
       
  1366 *}
       
  1367 lemma eq_pv_persist: (* ddd *)
       
  1368   assumes neq_th': "th' \<noteq> th"
       
  1369   and eq_pv: "cntP s th' = cntV s th'"
       
  1370   shows "cntP (t @ s) th' = cntV (t @ s) th'"
       
  1371 proof(induction rule: ind) 
       
  1372   -- {* The nontrivial case is for the @{term Cons}: *}
       
  1373   case (Cons e t)
       
  1374   -- {* All results derived so far hold for both @{term s} and @{term "t@s"}: *}
       
  1375   interpret vat_t: extend_highest_gen s th prio tm t using Cons by simp
       
  1376   interpret vat_e: extend_highest_gen s th prio tm "(e # t)" using Cons by simp
       
  1377   show ?case
       
  1378   proof -
       
  1379     -- {* It can be proved that @{term cntP}-value of @{term th'} does not change
       
  1380           by the happening of event @{term e}: *}
       
  1381     have "cntP ((e#t)@s) th' = cntP (t@s) th'"
       
  1382     proof(rule ccontr) -- {* Proof by contradiction. *}
       
  1383       -- {* Suppose @{term cntP}-value of @{term th'} is changed by @{term e}: *}
       
  1384       assume otherwise: "cntP ((e # t) @ s) th' \<noteq> cntP (t @ s) th'"
       
  1385       -- {* Then the actor of @{term e} must be @{term th'} and @{term e}
       
  1386             must be a @{term P}-event: *}
       
  1387       hence "isP e" "actor e = th'" by (auto simp:cntP_diff_inv) 
       
  1388       with vat_t.actor_inv[OF Cons(2)]
       
  1389       -- {* According to @{thm actor_inv}, @{term th'} must be running at 
       
  1390             the moment @{term "t@s"}: *}
       
  1391       have "th' \<in> runing (t@s)" by (cases e, auto)
       
  1392       -- {* However, an application of @{thm eq_pv_blocked} to induction hypothesis
       
  1393             shows @{term th'} can not be running at moment  @{term "t@s"}: *}
       
  1394       moreover have "th' \<notin> runing (t@s)" 
       
  1395                using vat_t.eq_pv_blocked[OF neq_th' Cons(5)] .
       
  1396       -- {* Contradiction is finally derived: *}
       
  1397       ultimately show False by simp
       
  1398     qed
       
  1399     -- {* It can also be proved that @{term cntV}-value of @{term th'} does not change
       
  1400           by the happening of event @{term e}: *}
       
  1401     -- {* The proof follows exactly the same pattern as the case for @{term cntP}-value: *}
       
  1402     moreover have "cntV ((e#t)@s) th' = cntV (t@s) th'"
       
  1403     proof(rule ccontr) -- {* Proof by contradiction. *}
       
  1404       assume otherwise: "cntV ((e # t) @ s) th' \<noteq> cntV (t @ s) th'"
       
  1405       hence "isV e" "actor e = th'" by (auto simp:cntV_diff_inv) 
       
  1406       with vat_t.actor_inv[OF Cons(2)]
       
  1407       have "th' \<in> runing (t@s)" by (cases e, auto)
       
  1408       moreover have "th' \<notin> runing (t@s)"
       
  1409           using vat_t.eq_pv_blocked[OF neq_th' Cons(5)] .
       
  1410       ultimately show False by simp
       
  1411     qed
       
  1412     -- {* Finally, it can be shown that the @{term cntP} and @{term cntV} 
       
  1413           value for @{term th'} are still in balance, so @{term th'} 
       
  1414           is still hand-emptied after the execution of event @{term e}: *}
       
  1415     ultimately show ?thesis using Cons(5) by metis
       
  1416   qed
       
  1417 qed (auto simp:eq_pv)
       
  1418 
       
  1419 text {*
       
  1420 
       
  1421   By combining @{thm eq_pv_blocked} and @{thm eq_pv_persist}, it can
       
  1422   be derived easily that @{term th'} can not be running in the future:
       
  1423 
       
  1424 *}
       
  1425 
       
  1426 lemma eq_pv_blocked_persist:
       
  1427   assumes neq_th': "th' \<noteq> th"
       
  1428   and eq_pv: "cntP s th' = cntV s th'"
       
  1429   shows "th' \<notin> runing (t @ s)"
       
  1430   using assms
       
  1431   by (simp add: eq_pv_blocked eq_pv_persist) 
       
  1432 
       
  1433 text {*
       
  1434 
       
  1435   The following lemma shows the blocking thread @{term th'} must hold
       
  1436   some resource in the very beginning.
       
  1437 
       
  1438 *}
       
  1439 
       
  1440 lemma runing_cntP_cntV_inv: (* ddd *)
       
  1441   assumes is_runing: "th' \<in> runing (t @ s)"
       
  1442   and neq_th': "th' \<noteq> th"
       
  1443   shows "cntP s th' > cntV s th'"
       
  1444   using assms
       
  1445 proof -
       
  1446   -- {* First, it can be shown that the number of @{term P} and
       
  1447         @{term V} operations can not be equal for thred @{term th'} *}
       
  1448   have "cntP s th' \<noteq> cntV s th'"
       
  1449   proof
       
  1450      -- {* The proof goes by contradiction, suppose otherwise: *}
       
  1451     assume otherwise: "cntP s th' = cntV s th'"
       
  1452     -- {* By applying @{thm  eq_pv_blocked_persist} to this: *}
       
  1453     from eq_pv_blocked_persist[OF neq_th' otherwise] 
       
  1454     -- {* we have that @{term th'} can not be running at moment @{term "t@s"}: *}
       
  1455     have "th' \<notin> runing (t@s)" .
       
  1456     -- {* This is obvious in contradiction with assumption @{thm is_runing}  *}
       
  1457     thus False using is_runing by simp
       
  1458   qed
       
  1459   -- {* However, the number of @{term V} is always less or equal to @{term P}: *}
       
  1460   moreover have "cntV s th' \<le> cntP s th'" using vat_s.cnp_cnv_cncs by auto
       
  1461   -- {* Thesis is finally derived by combining the these two results: *}
       
  1462   ultimately show ?thesis by auto
       
  1463 qed
       
  1464 
       
  1465 
       
  1466 text {*
       
  1467 
       
  1468   The following lemmas shows the blocking thread @{text th'} must be
       
  1469   live at the very beginning, i.e. the moment (or state) @{term s}.
       
  1470   The proof is a  simple combination of the results above:
       
  1471 
       
  1472 *}
       
  1473 
       
  1474 lemma runing_threads_inv: 
       
  1475   assumes runing': "th' \<in> runing (t@s)"
       
  1476   and neq_th': "th' \<noteq> th"
       
  1477   shows "th' \<in> threads s"
       
  1478 proof(rule ccontr) -- {* Proof by contradiction: *}
       
  1479   assume otherwise: "th' \<notin> threads s" 
       
  1480   have "th' \<notin> runing (t @ s)"
       
  1481   proof -
       
  1482     from vat_s.cnp_cnv_eq[OF otherwise]
       
  1483     have "cntP s th' = cntV s th'" .
       
  1484     from eq_pv_blocked_persist[OF neq_th' this]
       
  1485     show ?thesis .
       
  1486   qed
       
  1487   with runing' show False by simp
       
  1488 qed
       
  1489 
       
  1490 text {*
       
  1491 
       
  1492   The following lemma summarises the above lemmas to give an overall
       
  1493   characterisationof the blocking thread @{text "th'"}:
       
  1494 
       
  1495 *}
       
  1496 
       
  1497 lemma runing_inversion: (* ddd, one of the main lemmas to present *)
       
  1498   assumes runing': "th' \<in> runing (t@s)"
       
  1499   and neq_th: "th' \<noteq> th"
       
  1500   shows "th' \<in> threads s"
       
  1501   and    "\<not>detached s th'"
       
  1502   and    "cp (t@s) th' = preced th s"
       
  1503 proof -
       
  1504   from runing_threads_inv[OF assms]
       
  1505   show "th' \<in> threads s" .
       
  1506 next
       
  1507   from runing_cntP_cntV_inv[OF runing' neq_th]
       
  1508   show "\<not>detached s th'" using vat_s.detached_eq by simp
       
  1509 next
       
  1510   from runing_preced_inversion[OF runing']
       
  1511   show "cp (t@s) th' = preced th s" .
       
  1512 qed
       
  1513 
       
  1514 
       
  1515 section {* The existence of `blocking thread` *}
       
  1516 
       
  1517 text {* 
       
  1518 
       
  1519   Suppose @{term th} is not running, it is first shown that there is a
       
  1520   path in RAG leading from node @{term th} to another thread @{text
       
  1521   "th'"} in the @{term readys}-set (So @{text "th'"} is an ancestor of
       
  1522   @{term th}}).
       
  1523 
       
  1524   Now, since @{term readys}-set is non-empty, there must be one in it
       
  1525   which holds the highest @{term cp}-value, which, by definition, is
       
  1526   the @{term runing}-thread. However, we are going to show more: this
       
  1527   running thread is exactly @{term "th'"}.
       
  1528 
       
  1529 *}
       
  1530 
       
  1531 lemma th_blockedE: (* ddd, the other main lemma to be presented: *)
       
  1532   assumes "th \<notin> runing (t@s)"
       
  1533   obtains th' where "Th th' \<in> ancestors (RAG (t @ s)) (Th th)"
       
  1534                     "th' \<in> runing (t@s)"
       
  1535 proof -
       
  1536   -- {* According to @{thm vat_t.th_chain_to_ready}, either 
       
  1537         @{term "th"} is in @{term "readys"} or there is path leading from it to 
       
  1538         one thread in @{term "readys"}. *}
       
  1539   have "th \<in> readys (t @ s) \<or> (\<exists>th'. th' \<in> readys (t @ s) \<and> (Th th, Th th') \<in> (RAG (t @ s))\<^sup>+)" 
       
  1540     using th_kept vat_t.th_chain_to_ready by auto
       
  1541   -- {* However, @{term th} can not be in @{term readys}, because otherwise, since 
       
  1542        @{term th} holds the highest @{term cp}-value, it must be @{term "runing"}. *}
       
  1543   moreover have "th \<notin> readys (t@s)" 
       
  1544     using assms runing_def th_cp_max vat_t.max_cp_readys_threads by auto 
       
  1545   -- {* So, there must be a path from @{term th} to another thread @{text "th'"} in 
       
  1546         term @{term readys}: *}
       
  1547   ultimately obtain th' where th'_in: "th' \<in> readys (t@s)"
       
  1548                           and dp: "(Th th, Th th') \<in> (RAG (t @ s))\<^sup>+" by auto
       
  1549   -- {* We are going to show that this @{term th'} is running. *}
       
  1550   have "th' \<in> runing (t@s)"
       
  1551   proof -
       
  1552     -- {* We only need to show that this @{term th'} holds the highest @{term cp}-value: *}
       
  1553     have "cp (t@s) th' = Max (cp (t@s) ` readys (t@s))" (is "?L = ?R")
       
  1554     proof -
       
  1555       have "?L =  Max ((the_preced (t @ s) \<circ> the_thread) ` subtree (tRAG (t @ s)) (Th th'))"
       
  1556         by (unfold cp_alt_def1, simp)
       
  1557       also have "... = (the_preced (t @ s) \<circ> the_thread) (Th th)"
       
  1558       proof(rule image_Max_subset)
       
  1559         show "finite (Th ` (threads (t@s)))" by (simp add: vat_t.finite_threads)
       
  1560       next
       
  1561         show "subtree (tRAG (t @ s)) (Th th') \<subseteq> Th ` threads (t @ s)"
       
  1562           by (metis Range.intros dp trancl_range vat_t.range_in vat_t.subtree_tRAG_thread) 
       
  1563       next
       
  1564         show "Th th \<in> subtree (tRAG (t @ s)) (Th th')" using dp
       
  1565                     by (unfold tRAG_subtree_eq, auto simp:subtree_def)
       
  1566       next
       
  1567         show "Max ((the_preced (t @ s) \<circ> the_thread) ` Th ` threads (t @ s)) =
       
  1568                       (the_preced (t @ s) \<circ> the_thread) (Th th)" (is "Max ?L = _")
       
  1569         proof -
       
  1570           have "?L = the_preced (t @ s) `  threads (t @ s)" 
       
  1571                      by (unfold image_comp, rule image_cong, auto)
       
  1572           thus ?thesis using max_preced the_preced_def by auto
       
  1573         qed
       
  1574       qed
       
  1575       also have "... = ?R"
       
  1576         using th_cp_max th_cp_preced th_kept 
       
  1577               the_preced_def vat_t.max_cp_readys_threads by auto
       
  1578       finally show ?thesis .
       
  1579     qed 
       
  1580     -- {* Now, since @{term th'} holds the highest @{term cp} 
       
  1581           and we have already show it is in @{term readys},
       
  1582           it is @{term runing} by definition. *}
       
  1583     with `th' \<in> readys (t@s)` show ?thesis by (simp add: runing_def) 
       
  1584   qed
       
  1585   -- {* It is easy to show @{term th'} is an ancestor of @{term th}: *}
       
  1586   moreover have "Th th' \<in> ancestors (RAG (t @ s)) (Th th)" 
       
  1587     using `(Th th, Th th') \<in> (RAG (t @ s))\<^sup>+` by (auto simp:ancestors_def)
       
  1588   ultimately show ?thesis using that by metis
       
  1589 qed
       
  1590 
       
  1591 text {*
       
  1592 
       
  1593   Now it is easy to see there is always a thread to run by case
       
  1594   analysis on whether thread @{term th} is running: if the answer is
       
  1595   yes, the the running thread is obviously @{term th} itself;
       
  1596   otherwise, the running thread is the @{text th'} given by lemma
       
  1597   @{thm th_blockedE}.
       
  1598 
       
  1599 *}
       
  1600 
       
  1601 lemma live: "runing (t@s) \<noteq> {}"
       
  1602 proof(cases "th \<in> runing (t@s)") 
       
  1603   case True thus ?thesis by auto
       
  1604 next
       
  1605   case False
       
  1606   thus ?thesis using th_blockedE by auto
       
  1607 qed
       
  1608 
       
  1609 
       
  1610 end
       
  1611 end