PrioG.thy
changeset 88 83dd5345d5d0
parent 85 d239aa953315
parent 86 2106021bae53
child 89 2056d9f481e2
equal deleted inserted replaced
85:d239aa953315 88:83dd5345d5d0
       
     1 <<<<<<< local
     1 theory Correctness
     2 theory Correctness
     2 imports PIPBasics
     3 imports PIPBasics
     3 begin
     4 begin
     4 
     5 
     5 
     6 
   794 qed
   795 qed
   795 
   796 
   796 
   797 
   797 end
   798 end
   798 end
   799 end
       
   800 =======
       
   801 theory Correctness
       
   802 imports PIPBasics
       
   803 begin
       
   804 
       
   805 
       
   806 text {* 
       
   807   The following two auxiliary lemmas are used to reason about @{term Max}.
       
   808 *}
       
   809 lemma image_Max_eqI: 
       
   810   assumes "finite B"
       
   811   and "b \<in> B"
       
   812   and "\<forall> x \<in> B. f x \<le> f b"
       
   813   shows "Max (f ` B) = f b"
       
   814   using assms
       
   815   using Max_eqI by blast 
       
   816 
       
   817 lemma image_Max_subset:
       
   818   assumes "finite A"
       
   819   and "B \<subseteq> A"
       
   820   and "a \<in> B"
       
   821   and "Max (f ` A) = f a"
       
   822   shows "Max (f ` B) = f a"
       
   823 proof(rule image_Max_eqI)
       
   824   show "finite B"
       
   825     using assms(1) assms(2) finite_subset by auto 
       
   826 next
       
   827   show "a \<in> B" using assms by simp
       
   828 next
       
   829   show "\<forall>x\<in>B. f x \<le> f a"
       
   830     by (metis Max_ge assms(1) assms(2) assms(4) 
       
   831             finite_imageI image_eqI subsetCE) 
       
   832 qed
       
   833 
       
   834 text {*
       
   835   The following locale @{text "highest_gen"} sets the basic context for our
       
   836   investigation: supposing thread @{text th} holds the highest @{term cp}-value
       
   837   in state @{text s}, which means the task for @{text th} is the 
       
   838   most urgent. We want to show that  
       
   839   @{text th} is treated correctly by PIP, which means
       
   840   @{text th} will not be blocked unreasonably by other less urgent
       
   841   threads. 
       
   842 *}
       
   843 locale highest_gen =
       
   844   fixes s th prio tm
       
   845   assumes vt_s: "vt s"
       
   846   and threads_s: "th \<in> threads s"
       
   847   and highest: "preced th s = Max ((cp s)`threads s)"
       
   848   -- {* The internal structure of @{term th}'s precedence is exposed:*}
       
   849   and preced_th: "preced th s = Prc prio tm" 
       
   850 
       
   851 -- {* @{term s} is a valid trace, so it will inherit all results derived for
       
   852       a valid trace: *}
       
   853 sublocale highest_gen < vat_s: valid_trace "s"
       
   854   by (unfold_locales, insert vt_s, simp)
       
   855 
       
   856 context highest_gen
       
   857 begin
       
   858 
       
   859 text {*
       
   860   @{term tm} is the time when the precedence of @{term th} is set, so 
       
   861   @{term tm} must be a valid moment index into @{term s}.
       
   862 *}
       
   863 lemma lt_tm: "tm < length s"
       
   864   by (insert preced_tm_lt[OF threads_s preced_th], simp)
       
   865 
       
   866 text {*
       
   867   Since @{term th} holds the highest precedence and @{text "cp"}
       
   868   is the highest precedence of all threads in the sub-tree of 
       
   869   @{text "th"} and @{text th} is among these threads, 
       
   870   its @{term cp} must equal to its precedence:
       
   871 *}
       
   872 lemma eq_cp_s_th: "cp s th = preced th s" (is "?L = ?R")
       
   873 proof -
       
   874   have "?L \<le> ?R"
       
   875   by (unfold highest, rule Max_ge, 
       
   876         auto simp:threads_s finite_threads)
       
   877   moreover have "?R \<le> ?L"
       
   878     by (unfold vat_s.cp_rec, rule Max_ge, 
       
   879         auto simp:the_preced_def vat_s.fsbttRAGs.finite_children)
       
   880   ultimately show ?thesis by auto
       
   881 qed
       
   882 
       
   883 lemma highest_cp_preced: "cp s th = Max (the_preced s ` threads s)"
       
   884   using eq_cp_s_th highest max_cp_eq the_preced_def by presburger
       
   885   
       
   886 
       
   887 lemma highest_preced_thread: "preced th s = Max (the_preced s ` threads s)"
       
   888   by (fold eq_cp_s_th, unfold highest_cp_preced, simp)
       
   889 
       
   890 lemma highest': "cp s th = Max (cp s ` threads s)"
       
   891   by (simp add: eq_cp_s_th highest)
       
   892 
       
   893 end
       
   894 
       
   895 locale extend_highest_gen = highest_gen + 
       
   896   fixes t 
       
   897   assumes vt_t: "vt (t@s)"
       
   898   and create_low: "Create th' prio' \<in> set t \<Longrightarrow> prio' \<le> prio"
       
   899   and set_diff_low: "Set th' prio' \<in> set t \<Longrightarrow> th' \<noteq> th \<and> prio' \<le> prio"
       
   900   and exit_diff: "Exit th' \<in> set t \<Longrightarrow> th' \<noteq> th"
       
   901 
       
   902 sublocale extend_highest_gen < vat_t: valid_trace "t@s"
       
   903   by (unfold_locales, insert vt_t, simp)
       
   904 
       
   905 lemma step_back_vt_app: 
       
   906   assumes vt_ts: "vt (t@s)" 
       
   907   shows "vt s"
       
   908 proof -
       
   909   from vt_ts show ?thesis
       
   910   proof(induct t)
       
   911     case Nil
       
   912     from Nil show ?case by auto
       
   913   next
       
   914     case (Cons e t)
       
   915     assume ih: " vt (t @ s) \<Longrightarrow> vt s"
       
   916       and vt_et: "vt ((e # t) @ s)"
       
   917     show ?case
       
   918     proof(rule ih)
       
   919       show "vt (t @ s)"
       
   920       proof(rule step_back_vt)
       
   921         from vt_et show "vt (e # t @ s)" by simp
       
   922       qed
       
   923     qed
       
   924   qed
       
   925 qed
       
   926 
       
   927 (* locale red_extend_highest_gen = extend_highest_gen +
       
   928    fixes i::nat
       
   929 *)
       
   930 
       
   931 (*
       
   932 sublocale red_extend_highest_gen <   red_moment: extend_highest_gen "s" "th" "prio" "tm" "(moment i t)"
       
   933   apply (insert extend_highest_gen_axioms, subst (asm) (1) moment_restm_s [of i t, symmetric])
       
   934   apply (unfold extend_highest_gen_def extend_highest_gen_axioms_def, clarsimp)
       
   935   by (unfold highest_gen_def, auto dest:step_back_vt_app)
       
   936 *)
       
   937 
       
   938 context extend_highest_gen
       
   939 begin
       
   940 
       
   941  lemma ind [consumes 0, case_names Nil Cons, induct type]:
       
   942   assumes 
       
   943     h0: "R []"
       
   944   and h2: "\<And> e t. \<lbrakk>vt (t@s); step (t@s) e; 
       
   945                     extend_highest_gen s th prio tm t; 
       
   946                     extend_highest_gen s th prio tm (e#t); R t\<rbrakk> \<Longrightarrow> R (e#t)"
       
   947   shows "R t"
       
   948 proof -
       
   949   from vt_t extend_highest_gen_axioms show ?thesis
       
   950   proof(induct t)
       
   951     from h0 show "R []" .
       
   952   next
       
   953     case (Cons e t')
       
   954     assume ih: "\<lbrakk>vt (t' @ s); extend_highest_gen s th prio tm t'\<rbrakk> \<Longrightarrow> R t'"
       
   955       and vt_e: "vt ((e # t') @ s)"
       
   956       and et: "extend_highest_gen s th prio tm (e # t')"
       
   957     from vt_e and step_back_step have stp: "step (t'@s) e" by auto
       
   958     from vt_e and step_back_vt have vt_ts: "vt (t'@s)" by auto
       
   959     show ?case
       
   960     proof(rule h2 [OF vt_ts stp _ _ _ ])
       
   961       show "R t'"
       
   962       proof(rule ih)
       
   963         from et show ext': "extend_highest_gen s th prio tm t'"
       
   964           by (unfold extend_highest_gen_def extend_highest_gen_axioms_def, auto dest:step_back_vt)
       
   965       next
       
   966         from vt_ts show "vt (t' @ s)" .
       
   967       qed
       
   968     next
       
   969       from et show "extend_highest_gen s th prio tm (e # t')" .
       
   970     next
       
   971       from et show ext': "extend_highest_gen s th prio tm t'"
       
   972           by (unfold extend_highest_gen_def extend_highest_gen_axioms_def, auto dest:step_back_vt)
       
   973     qed
       
   974   qed
       
   975 qed
       
   976 
       
   977 
       
   978 lemma th_kept: "th \<in> threads (t @ s) \<and> 
       
   979                  preced th (t@s) = preced th s" (is "?Q t") 
       
   980 proof -
       
   981   show ?thesis
       
   982   proof(induct rule:ind)
       
   983     case Nil
       
   984     from threads_s
       
   985     show ?case
       
   986       by auto
       
   987   next
       
   988     case (Cons e t)
       
   989     interpret h_e: extend_highest_gen _ _ _ _ "(e # t)" using Cons by auto
       
   990     interpret h_t: extend_highest_gen _ _ _ _ t using Cons by auto
       
   991     show ?case
       
   992     proof(cases e)
       
   993       case (Create thread prio)
       
   994       show ?thesis
       
   995       proof -
       
   996         from Cons and Create have "step (t@s) (Create thread prio)" by auto
       
   997         hence "th \<noteq> thread"
       
   998         proof(cases)
       
   999           case thread_create
       
  1000           with Cons show ?thesis by auto
       
  1001         qed
       
  1002         hence "preced th ((e # t) @ s)  = preced th (t @ s)"
       
  1003           by (unfold Create, auto simp:preced_def)
       
  1004         moreover note Cons
       
  1005         ultimately show ?thesis
       
  1006           by (auto simp:Create)
       
  1007       qed
       
  1008     next
       
  1009       case (Exit thread)
       
  1010       from h_e.exit_diff and Exit
       
  1011       have neq_th: "thread \<noteq> th" by auto
       
  1012       with Cons
       
  1013       show ?thesis
       
  1014         by (unfold Exit, auto simp:preced_def)
       
  1015     next
       
  1016       case (P thread cs)
       
  1017       with Cons
       
  1018       show ?thesis 
       
  1019         by (auto simp:P preced_def)
       
  1020     next
       
  1021       case (V thread cs)
       
  1022       with Cons
       
  1023       show ?thesis 
       
  1024         by (auto simp:V preced_def)
       
  1025     next
       
  1026       case (Set thread prio')
       
  1027       show ?thesis
       
  1028       proof -
       
  1029         from h_e.set_diff_low and Set
       
  1030         have "th \<noteq> thread" by auto
       
  1031         hence "preced th ((e # t) @ s)  = preced th (t @ s)"
       
  1032           by (unfold Set, auto simp:preced_def)
       
  1033         moreover note Cons
       
  1034         ultimately show ?thesis
       
  1035           by (auto simp:Set)
       
  1036       qed
       
  1037     qed
       
  1038   qed
       
  1039 qed
       
  1040 
       
  1041 text {*
       
  1042   According to @{thm th_kept}, thread @{text "th"} has its living status
       
  1043   and precedence kept along the way of @{text "t"}. The following lemma
       
  1044   shows that this preserved precedence of @{text "th"} remains as the highest
       
  1045   along the way of @{text "t"}.
       
  1046 
       
  1047   The proof goes by induction over @{text "t"} using the specialized
       
  1048   induction rule @{thm ind}, followed by case analysis of each possible 
       
  1049   operations of PIP. All cases follow the same pattern rendered by the 
       
  1050   generalized introduction rule @{thm "image_Max_eqI"}. 
       
  1051 
       
  1052   The very essence is to show that precedences, no matter whether they 
       
  1053   are newly introduced or modified, are always lower than the one held 
       
  1054   by @{term "th"}, which by @{thm th_kept} is preserved along the way.
       
  1055 *}
       
  1056 lemma max_kept: "Max (the_preced (t @ s) ` (threads (t@s))) = preced th s"
       
  1057 proof(induct rule:ind)
       
  1058   case Nil
       
  1059   from highest_preced_thread
       
  1060   show ?case by simp
       
  1061 next
       
  1062   case (Cons e t)
       
  1063     interpret h_e: extend_highest_gen _ _ _ _ "(e # t)" using Cons by auto
       
  1064     interpret h_t: extend_highest_gen _ _ _ _ t using Cons by auto
       
  1065   show ?case
       
  1066   proof(cases e)
       
  1067     case (Create thread prio')
       
  1068     show ?thesis (is "Max (?f ` ?A) = ?t")
       
  1069     proof -
       
  1070       -- {* The following is the common pattern of each branch of the case analysis. *}
       
  1071       -- {* The major part is to show that @{text "th"} holds the highest precedence: *}
       
  1072       have "Max (?f ` ?A) = ?f th"
       
  1073       proof(rule image_Max_eqI)
       
  1074         show "finite ?A" using h_e.finite_threads by auto 
       
  1075       next
       
  1076         show "th \<in> ?A" using h_e.th_kept by auto 
       
  1077       next
       
  1078         show "\<forall>x\<in>?A. ?f x \<le> ?f th"
       
  1079         proof 
       
  1080           fix x
       
  1081           assume "x \<in> ?A"
       
  1082           hence "x = thread \<or> x \<in> threads (t@s)" by (auto simp:Create)
       
  1083           thus "?f x \<le> ?f th"
       
  1084           proof
       
  1085             assume "x = thread"
       
  1086             thus ?thesis 
       
  1087               apply (simp add:Create the_preced_def preced_def, fold preced_def)
       
  1088               using Create h_e.create_low h_t.th_kept lt_tm preced_leI2 
       
  1089               preced_th by force
       
  1090           next
       
  1091             assume h: "x \<in> threads (t @ s)"
       
  1092             from Cons(2)[unfolded Create] 
       
  1093             have "x \<noteq> thread" using h by (cases, auto)
       
  1094             hence "?f x = the_preced (t@s) x" 
       
  1095               by (simp add:Create the_preced_def preced_def)
       
  1096             hence "?f x \<le> Max (the_preced (t@s) ` threads (t@s))"
       
  1097               by (simp add: h_t.finite_threads h)
       
  1098             also have "... = ?f th"
       
  1099               by (metis Cons.hyps(5) h_e.th_kept the_preced_def) 
       
  1100             finally show ?thesis .
       
  1101           qed
       
  1102         qed
       
  1103       qed
       
  1104      -- {* The minor part is to show that the precedence of @{text "th"} 
       
  1105            equals to preserved one, given by the foregoing lemma @{thm th_kept} *}
       
  1106       also have "... = ?t" using h_e.th_kept the_preced_def by auto
       
  1107       -- {* Then it follows trivially that the precedence preserved
       
  1108             for @{term "th"} remains the maximum of all living threads along the way. *}
       
  1109       finally show ?thesis .
       
  1110     qed 
       
  1111   next 
       
  1112     case (Exit thread)
       
  1113     show ?thesis (is "Max (?f ` ?A) = ?t")
       
  1114     proof -
       
  1115       have "Max (?f ` ?A) = ?f th"
       
  1116       proof(rule image_Max_eqI)
       
  1117         show "finite ?A" using h_e.finite_threads by auto 
       
  1118       next
       
  1119         show "th \<in> ?A" using h_e.th_kept by auto 
       
  1120       next
       
  1121         show "\<forall>x\<in>?A. ?f x \<le> ?f th"
       
  1122         proof 
       
  1123           fix x
       
  1124           assume "x \<in> ?A"
       
  1125           hence "x \<in> threads (t@s)" by (simp add: Exit) 
       
  1126           hence "?f x \<le> Max (?f ` threads (t@s))" 
       
  1127             by (simp add: h_t.finite_threads) 
       
  1128           also have "... \<le> ?f th" 
       
  1129             apply (simp add:Exit the_preced_def preced_def, fold preced_def)
       
  1130             using Cons.hyps(5) h_t.th_kept the_preced_def by auto
       
  1131           finally show "?f x \<le> ?f th" .
       
  1132         qed
       
  1133       qed
       
  1134       also have "... = ?t" using h_e.th_kept the_preced_def by auto
       
  1135       finally show ?thesis .
       
  1136     qed 
       
  1137   next
       
  1138     case (P thread cs)
       
  1139     with Cons
       
  1140     show ?thesis by (auto simp:preced_def the_preced_def)
       
  1141   next
       
  1142     case (V thread cs)
       
  1143     with Cons
       
  1144     show ?thesis by (auto simp:preced_def the_preced_def)
       
  1145   next 
       
  1146     case (Set thread prio')
       
  1147     show ?thesis (is "Max (?f ` ?A) = ?t")
       
  1148     proof -
       
  1149       have "Max (?f ` ?A) = ?f th"
       
  1150       proof(rule image_Max_eqI)
       
  1151         show "finite ?A" using h_e.finite_threads by auto 
       
  1152       next
       
  1153         show "th \<in> ?A" using h_e.th_kept by auto 
       
  1154       next
       
  1155         show "\<forall>x\<in>?A. ?f x \<le> ?f th"
       
  1156         proof 
       
  1157           fix x
       
  1158           assume h: "x \<in> ?A"
       
  1159           show "?f x \<le> ?f th"
       
  1160           proof(cases "x = thread")
       
  1161             case True
       
  1162             moreover have "the_preced (Set thread prio' # t @ s) thread \<le> the_preced (t @ s) th"
       
  1163             proof -
       
  1164               have "the_preced (t @ s) th = Prc prio tm"  
       
  1165                 using h_t.th_kept preced_th by (simp add:the_preced_def)
       
  1166               moreover have "prio' \<le> prio" using Set h_e.set_diff_low by auto
       
  1167               ultimately show ?thesis by (insert lt_tm, auto simp:the_preced_def preced_def)
       
  1168             qed
       
  1169             ultimately show ?thesis
       
  1170               by (unfold Set, simp add:the_preced_def preced_def)
       
  1171           next
       
  1172             case False
       
  1173             then have "?f x  = the_preced (t@s) x"
       
  1174               by (simp add:the_preced_def preced_def Set)
       
  1175             also have "... \<le> Max (the_preced (t@s) ` threads (t@s))"
       
  1176               using Set h h_t.finite_threads by auto 
       
  1177             also have "... = ?f th" by (metis Cons.hyps(5) h_e.th_kept the_preced_def) 
       
  1178             finally show ?thesis .
       
  1179           qed
       
  1180         qed
       
  1181       qed
       
  1182       also have "... = ?t" using h_e.th_kept the_preced_def by auto
       
  1183       finally show ?thesis .
       
  1184     qed 
       
  1185   qed
       
  1186 qed
       
  1187 
       
  1188 lemma max_preced: "preced th (t@s) = Max (the_preced (t@s) ` (threads (t@s)))"
       
  1189   by (insert th_kept max_kept, auto)
       
  1190 
       
  1191 text {*
       
  1192   The reason behind the following lemma is that:
       
  1193   Since @{term "cp"} is defined as the maximum precedence 
       
  1194   of those threads contained in the sub-tree of node @{term "Th th"} 
       
  1195   in @{term "RAG (t@s)"}, and all these threads are living threads, and 
       
  1196   @{term "th"} is also among them, the maximum precedence of 
       
  1197   them all must be the one for @{text "th"}.
       
  1198 *}
       
  1199 lemma th_cp_max_preced: 
       
  1200   "cp (t@s) th = Max (the_preced (t@s) ` (threads (t@s)))" (is "?L = ?R") 
       
  1201 proof -
       
  1202   let ?f = "the_preced (t@s)"
       
  1203   have "?L = ?f th"
       
  1204   proof(unfold cp_alt_def, rule image_Max_eqI)
       
  1205     show "finite {th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}"
       
  1206     proof -
       
  1207       have "{th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)} = 
       
  1208             the_thread ` {n . n \<in> subtree (RAG (t @ s)) (Th th) \<and>
       
  1209                             (\<exists> th'. n = Th th')}"
       
  1210       by (smt Collect_cong Setcompr_eq_image mem_Collect_eq the_thread.simps)
       
  1211       moreover have "finite ..." by (simp add: vat_t.fsbtRAGs.finite_subtree) 
       
  1212       ultimately show ?thesis by simp
       
  1213     qed
       
  1214   next
       
  1215     show "th \<in> {th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}"
       
  1216       by (auto simp:subtree_def)
       
  1217   next
       
  1218     show "\<forall>x\<in>{th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}.
       
  1219                the_preced (t @ s) x \<le> the_preced (t @ s) th"
       
  1220     proof
       
  1221       fix th'
       
  1222       assume "th' \<in> {th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}"
       
  1223       hence "Th th' \<in> subtree (RAG (t @ s)) (Th th)" by auto
       
  1224       moreover have "... \<subseteq> Field (RAG (t @ s)) \<union> {Th th}"
       
  1225         by (meson subtree_Field)
       
  1226       ultimately have "Th th' \<in> ..." by auto
       
  1227       hence "th' \<in> threads (t@s)" 
       
  1228       proof
       
  1229         assume "Th th' \<in> {Th th}"
       
  1230         thus ?thesis using th_kept by auto 
       
  1231       next
       
  1232         assume "Th th' \<in> Field (RAG (t @ s))"
       
  1233         thus ?thesis using vat_t.not_in_thread_isolated by blast 
       
  1234       qed
       
  1235       thus "the_preced (t @ s) th' \<le> the_preced (t @ s) th"
       
  1236         by (metis Max_ge finite_imageI finite_threads image_eqI 
       
  1237                max_kept th_kept the_preced_def)
       
  1238     qed
       
  1239   qed
       
  1240   also have "... = ?R" by (simp add: max_preced the_preced_def) 
       
  1241   finally show ?thesis .
       
  1242 qed
       
  1243 
       
  1244 lemma th_cp_max[simp]: "Max (cp (t@s) ` threads (t@s)) = cp (t@s) th"
       
  1245   using max_cp_eq th_cp_max_preced the_preced_def vt_t by presburger
       
  1246 
       
  1247 lemma [simp]: "Max (cp (t@s) ` threads (t@s)) = Max (the_preced (t@s) ` threads (t@s))"
       
  1248   by (simp add: th_cp_max_preced)
       
  1249   
       
  1250 lemma [simp]: "Max (the_preced (t@s) ` threads (t@s)) = the_preced (t@s) th"
       
  1251   using max_kept th_kept the_preced_def by auto
       
  1252 
       
  1253 lemma [simp]: "the_preced (t@s) th = preced th (t@s)"
       
  1254   using the_preced_def by auto
       
  1255 
       
  1256 lemma [simp]: "preced th (t@s) = preced th s"
       
  1257   by (simp add: th_kept)
       
  1258 
       
  1259 lemma [simp]: "cp s th = preced th s"
       
  1260   by (simp add: eq_cp_s_th)
       
  1261 
       
  1262 lemma th_cp_preced [simp]: "cp (t@s) th = preced th s"
       
  1263   by (fold max_kept, unfold th_cp_max_preced, simp)
       
  1264 
       
  1265 lemma preced_less:
       
  1266   assumes th'_in: "th' \<in> threads s"
       
  1267   and neq_th': "th' \<noteq> th"
       
  1268   shows "preced th' s < preced th s"
       
  1269   using assms
       
  1270 by (metis Max.coboundedI finite_imageI highest not_le order.trans 
       
  1271     preced_linorder rev_image_eqI threads_s vat_s.finite_threads 
       
  1272     vat_s.le_cp)
       
  1273 
       
  1274 section {* The `blocking thread` *}
       
  1275 
       
  1276 text {* 
       
  1277 
       
  1278   The purpose of PIP is to ensure that the most urgent thread @{term
       
  1279   th} is not blocked unreasonably. Therefore, below, we will derive
       
  1280   properties of the blocking thread. By blocking thread, we mean a
       
  1281   thread in running state t @ s, but is different from thread @{term
       
  1282   th}.
       
  1283 
       
  1284   The first lemmas shows that the @{term cp}-value of the blocking
       
  1285   thread @{text th'} equals to the highest precedence in the whole
       
  1286   system.
       
  1287 
       
  1288 *}
       
  1289 
       
  1290 lemma runing_preced_inversion:
       
  1291   assumes runing': "th' \<in> runing (t @ s)"
       
  1292   shows "cp (t @ s) th' = preced th s" 
       
  1293 proof -
       
  1294   have "cp (t @ s) th' = Max (cp (t @ s) ` readys (t @ s))" 
       
  1295     using assms by (unfold runing_def, auto)
       
  1296   also have "\<dots> = preced th s"
       
  1297     by (metis th_cp_max th_cp_preced vat_t.max_cp_readys_threads) 
       
  1298   finally show ?thesis .
       
  1299 qed
       
  1300 
       
  1301 text {*
       
  1302 
       
  1303   The next lemma shows how the counters for @{term "P"} and @{term
       
  1304   "V"} operations relate to the running threads in the states @{term
       
  1305   s} and @{term "t @ s"}: if a thread's @{term "P"}-count equals its
       
  1306   @{term "V"}-count (which means it no longer has any resource in its
       
  1307   possession), it cannot be a running thread.
       
  1308 
       
  1309   The proof is by contraction with the assumption @{text "th' \<noteq> th"}.
       
  1310   The key is the use of @{thm count_eq_dependants} to derive the
       
  1311   emptiness of @{text th'}s @{term dependants}-set from the balance of
       
  1312   its @{term P} and @{term V} counts.  From this, it can be shown
       
  1313   @{text th'}s @{term cp}-value equals to its own precedence.
       
  1314 
       
  1315   On the other hand, since @{text th'} is running, by @{thm
       
  1316   runing_preced_inversion}, its @{term cp}-value equals to the
       
  1317   precedence of @{term th}.
       
  1318 
       
  1319   Combining the above two results we have that @{text th'} and @{term
       
  1320   th} have the same precedence. By uniqueness of precedences, we have
       
  1321   @{text "th' = th"}, which is in contradiction with the assumption
       
  1322   @{text "th' \<noteq> th"}.
       
  1323 
       
  1324 *} 
       
  1325                       
       
  1326 lemma eq_pv_blocked: (* ddd *)
       
  1327   assumes neq_th': "th' \<noteq> th"
       
  1328   and eq_pv: "cntP (t @ s) th' = cntV (t @ s) th'"
       
  1329   shows "th' \<notin> runing (t @ s)"
       
  1330 proof
       
  1331   assume otherwise: "th' \<in> runing (t @ s)"
       
  1332   show False
       
  1333   proof -
       
  1334     have th'_in: "th' \<in> threads (t @ s)"
       
  1335         using otherwise readys_threads runing_def by auto 
       
  1336     have "th' = th"
       
  1337     proof(rule preced_unique)
       
  1338       -- {* The proof goes like this: 
       
  1339             it is first shown that the @{term preced}-value of @{term th'} 
       
  1340             equals to that of @{term th}, then by uniqueness 
       
  1341             of @{term preced}-values (given by lemma @{thm preced_unique}), 
       
  1342             @{term th'} equals to @{term th}: *}
       
  1343       show "preced th' (t @ s) = preced th (t @ s)" (is "?L = ?R")
       
  1344       proof -
       
  1345         -- {* Since the counts of @{term th'} are balanced, the subtree
       
  1346               of it contains only itself, so, its @{term cp}-value
       
  1347               equals its @{term preced}-value: *}
       
  1348         have "?L = cp (t @ s) th'"
       
  1349           by (unfold cp_eq_cpreced cpreced_def count_eq_dependants[OF eq_pv], simp)
       
  1350         -- {* Since @{term "th'"} is running, by @{thm runing_preced_inversion},
       
  1351               its @{term cp}-value equals @{term "preced th s"}, 
       
  1352               which equals to @{term "?R"} by simplification: *}
       
  1353         also have "... = ?R" 
       
  1354             using runing_preced_inversion[OF otherwise] by simp
       
  1355         finally show ?thesis .
       
  1356       qed
       
  1357     qed (auto simp: th'_in th_kept)
       
  1358     with `th' \<noteq> th` show ?thesis by simp
       
  1359  qed
       
  1360 qed
       
  1361 
       
  1362 text {*
       
  1363   The following lemma is the extrapolation of @{thm eq_pv_blocked}.
       
  1364   It says if a thread, different from @{term th}, 
       
  1365   does not hold any resource at the very beginning,
       
  1366   it will keep hand-emptied in the future @{term "t@s"}.
       
  1367 *}
       
  1368 lemma eq_pv_persist: (* ddd *)
       
  1369   assumes neq_th': "th' \<noteq> th"
       
  1370   and eq_pv: "cntP s th' = cntV s th'"
       
  1371   shows "cntP (t @ s) th' = cntV (t @ s) th'"
       
  1372 proof(induction rule: ind) 
       
  1373   -- {* The nontrivial case is for the @{term Cons}: *}
       
  1374   case (Cons e t)
       
  1375   -- {* All results derived so far hold for both @{term s} and @{term "t@s"}: *}
       
  1376   interpret vat_t: extend_highest_gen s th prio tm t using Cons by simp
       
  1377   interpret vat_e: extend_highest_gen s th prio tm "(e # t)" using Cons by simp
       
  1378   show ?case
       
  1379   proof -
       
  1380     -- {* It can be proved that @{term cntP}-value of @{term th'} does not change
       
  1381           by the happening of event @{term e}: *}
       
  1382     have "cntP ((e#t)@s) th' = cntP (t@s) th'"
       
  1383     proof(rule ccontr) -- {* Proof by contradiction. *}
       
  1384       -- {* Suppose @{term cntP}-value of @{term th'} is changed by @{term e}: *}
       
  1385       assume otherwise: "cntP ((e # t) @ s) th' \<noteq> cntP (t @ s) th'"
       
  1386       -- {* Then the actor of @{term e} must be @{term th'} and @{term e}
       
  1387             must be a @{term P}-event: *}
       
  1388       hence "isP e" "actor e = th'" by (auto simp:cntP_diff_inv) 
       
  1389       with vat_t.actor_inv[OF Cons(2)]
       
  1390       -- {* According to @{thm actor_inv}, @{term th'} must be running at 
       
  1391             the moment @{term "t@s"}: *}
       
  1392       have "th' \<in> runing (t@s)" by (cases e, auto)
       
  1393       -- {* However, an application of @{thm eq_pv_blocked} to induction hypothesis
       
  1394             shows @{term th'} can not be running at moment  @{term "t@s"}: *}
       
  1395       moreover have "th' \<notin> runing (t@s)" 
       
  1396                using vat_t.eq_pv_blocked[OF neq_th' Cons(5)] .
       
  1397       -- {* Contradiction is finally derived: *}
       
  1398       ultimately show False by simp
       
  1399     qed
       
  1400     -- {* It can also be proved that @{term cntV}-value of @{term th'} does not change
       
  1401           by the happening of event @{term e}: *}
       
  1402     -- {* The proof follows exactly the same pattern as the case for @{term cntP}-value: *}
       
  1403     moreover have "cntV ((e#t)@s) th' = cntV (t@s) th'"
       
  1404     proof(rule ccontr) -- {* Proof by contradiction. *}
       
  1405       assume otherwise: "cntV ((e # t) @ s) th' \<noteq> cntV (t @ s) th'"
       
  1406       hence "isV e" "actor e = th'" by (auto simp:cntV_diff_inv) 
       
  1407       with vat_t.actor_inv[OF Cons(2)]
       
  1408       have "th' \<in> runing (t@s)" by (cases e, auto)
       
  1409       moreover have "th' \<notin> runing (t@s)"
       
  1410           using vat_t.eq_pv_blocked[OF neq_th' Cons(5)] .
       
  1411       ultimately show False by simp
       
  1412     qed
       
  1413     -- {* Finally, it can be shown that the @{term cntP} and @{term cntV} 
       
  1414           value for @{term th'} are still in balance, so @{term th'} 
       
  1415           is still hand-emptied after the execution of event @{term e}: *}
       
  1416     ultimately show ?thesis using Cons(5) by metis
       
  1417   qed
       
  1418 qed (auto simp:eq_pv)
       
  1419 
       
  1420 text {*
       
  1421 
       
  1422   By combining @{thm eq_pv_blocked} and @{thm eq_pv_persist}, it can
       
  1423   be derived easily that @{term th'} can not be running in the future:
       
  1424 
       
  1425 *}
       
  1426 
       
  1427 lemma eq_pv_blocked_persist:
       
  1428   assumes neq_th': "th' \<noteq> th"
       
  1429   and eq_pv: "cntP s th' = cntV s th'"
       
  1430   shows "th' \<notin> runing (t @ s)"
       
  1431   using assms
       
  1432   by (simp add: eq_pv_blocked eq_pv_persist) 
       
  1433 
       
  1434 text {*
       
  1435 
       
  1436   The following lemma shows the blocking thread @{term th'} must hold
       
  1437   some resource in the very beginning.
       
  1438 
       
  1439 *}
       
  1440 
       
  1441 lemma runing_cntP_cntV_inv: (* ddd *)
       
  1442   assumes is_runing: "th' \<in> runing (t @ s)"
       
  1443   and neq_th': "th' \<noteq> th"
       
  1444   shows "cntP s th' > cntV s th'"
       
  1445   using assms
       
  1446 proof -
       
  1447   -- {* First, it can be shown that the number of @{term P} and
       
  1448         @{term V} operations can not be equal for thred @{term th'} *}
       
  1449   have "cntP s th' \<noteq> cntV s th'"
       
  1450   proof
       
  1451      -- {* The proof goes by contradiction, suppose otherwise: *}
       
  1452     assume otherwise: "cntP s th' = cntV s th'"
       
  1453     -- {* By applying @{thm  eq_pv_blocked_persist} to this: *}
       
  1454     from eq_pv_blocked_persist[OF neq_th' otherwise] 
       
  1455     -- {* we have that @{term th'} can not be running at moment @{term "t@s"}: *}
       
  1456     have "th' \<notin> runing (t@s)" .
       
  1457     -- {* This is obvious in contradiction with assumption @{thm is_runing}  *}
       
  1458     thus False using is_runing by simp
       
  1459   qed
       
  1460   -- {* However, the number of @{term V} is always less or equal to @{term P}: *}
       
  1461   moreover have "cntV s th' \<le> cntP s th'" using vat_s.cnp_cnv_cncs by auto
       
  1462   -- {* Thesis is finally derived by combining the these two results: *}
       
  1463   ultimately show ?thesis by auto
       
  1464 qed
       
  1465 
       
  1466 
       
  1467 text {*
       
  1468 
       
  1469   The following lemmas shows the blocking thread @{text th'} must be
       
  1470   live at the very beginning, i.e. the moment (or state) @{term s}.
       
  1471   The proof is a  simple combination of the results above:
       
  1472 
       
  1473 *}
       
  1474 
       
  1475 lemma runing_threads_inv: 
       
  1476   assumes runing': "th' \<in> runing (t@s)"
       
  1477   and neq_th': "th' \<noteq> th"
       
  1478   shows "th' \<in> threads s"
       
  1479 proof(rule ccontr) -- {* Proof by contradiction: *}
       
  1480   assume otherwise: "th' \<notin> threads s" 
       
  1481   have "th' \<notin> runing (t @ s)"
       
  1482   proof -
       
  1483     from vat_s.cnp_cnv_eq[OF otherwise]
       
  1484     have "cntP s th' = cntV s th'" .
       
  1485     from eq_pv_blocked_persist[OF neq_th' this]
       
  1486     show ?thesis .
       
  1487   qed
       
  1488   with runing' show False by simp
       
  1489 qed
       
  1490 
       
  1491 text {*
       
  1492 
       
  1493   The following lemma summarises the above lemmas to give an overall
       
  1494   characterisationof the blocking thread @{text "th'"}:
       
  1495 
       
  1496 *}
       
  1497 
       
  1498 lemma runing_inversion: (* ddd, one of the main lemmas to present *)
       
  1499   assumes runing': "th' \<in> runing (t@s)"
       
  1500   and neq_th: "th' \<noteq> th"
       
  1501   shows "th' \<in> threads s"
       
  1502   and    "\<not>detached s th'"
       
  1503   and    "cp (t@s) th' = preced th s"
       
  1504 proof -
       
  1505   from runing_threads_inv[OF assms]
       
  1506   show "th' \<in> threads s" .
       
  1507 next
       
  1508   from runing_cntP_cntV_inv[OF runing' neq_th]
       
  1509   show "\<not>detached s th'" using vat_s.detached_eq by simp
       
  1510 next
       
  1511   from runing_preced_inversion[OF runing']
       
  1512   show "cp (t@s) th' = preced th s" .
       
  1513 qed
       
  1514 
       
  1515 
       
  1516 section {* The existence of `blocking thread` *}
       
  1517 
       
  1518 text {* 
       
  1519 
       
  1520   Suppose @{term th} is not running, it is first shown that there is a
       
  1521   path in RAG leading from node @{term th} to another thread @{text
       
  1522   "th'"} in the @{term readys}-set (So @{text "th'"} is an ancestor of
       
  1523   @{term th}}).
       
  1524 
       
  1525   Now, since @{term readys}-set is non-empty, there must be one in it
       
  1526   which holds the highest @{term cp}-value, which, by definition, is
       
  1527   the @{term runing}-thread. However, we are going to show more: this
       
  1528   running thread is exactly @{term "th'"}.
       
  1529 
       
  1530 *}
       
  1531 
       
  1532 lemma th_blockedE: (* ddd, the other main lemma to be presented: *)
       
  1533   assumes "th \<notin> runing (t@s)"
       
  1534   obtains th' where "Th th' \<in> ancestors (RAG (t @ s)) (Th th)"
       
  1535                     "th' \<in> runing (t@s)"
       
  1536 proof -
       
  1537   -- {* According to @{thm vat_t.th_chain_to_ready}, either 
       
  1538         @{term "th"} is in @{term "readys"} or there is path leading from it to 
       
  1539         one thread in @{term "readys"}. *}
       
  1540   have "th \<in> readys (t @ s) \<or> (\<exists>th'. th' \<in> readys (t @ s) \<and> (Th th, Th th') \<in> (RAG (t @ s))\<^sup>+)" 
       
  1541     using th_kept vat_t.th_chain_to_ready by auto
       
  1542   -- {* However, @{term th} can not be in @{term readys}, because otherwise, since 
       
  1543        @{term th} holds the highest @{term cp}-value, it must be @{term "runing"}. *}
       
  1544   moreover have "th \<notin> readys (t@s)" 
       
  1545     using assms runing_def th_cp_max vat_t.max_cp_readys_threads by auto 
       
  1546   -- {* So, there must be a path from @{term th} to another thread @{text "th'"} in 
       
  1547         term @{term readys}: *}
       
  1548   ultimately obtain th' where th'_in: "th' \<in> readys (t@s)"
       
  1549                           and dp: "(Th th, Th th') \<in> (RAG (t @ s))\<^sup>+" by auto
       
  1550   -- {* We are going to show that this @{term th'} is running. *}
       
  1551   have "th' \<in> runing (t@s)"
       
  1552   proof -
       
  1553     -- {* We only need to show that this @{term th'} holds the highest @{term cp}-value: *}
       
  1554     have "cp (t@s) th' = Max (cp (t@s) ` readys (t@s))" (is "?L = ?R")
       
  1555     proof -
       
  1556       have "?L =  Max ((the_preced (t @ s) \<circ> the_thread) ` subtree (tRAG (t @ s)) (Th th'))"
       
  1557         by (unfold cp_alt_def1, simp)
       
  1558       also have "... = (the_preced (t @ s) \<circ> the_thread) (Th th)"
       
  1559       proof(rule image_Max_subset)
       
  1560         show "finite (Th ` (threads (t@s)))" by (simp add: vat_t.finite_threads)
       
  1561       next
       
  1562         show "subtree (tRAG (t @ s)) (Th th') \<subseteq> Th ` threads (t @ s)"
       
  1563           by (metis Range.intros dp trancl_range vat_t.range_in vat_t.subtree_tRAG_thread) 
       
  1564       next
       
  1565         show "Th th \<in> subtree (tRAG (t @ s)) (Th th')" using dp
       
  1566                     by (unfold tRAG_subtree_eq, auto simp:subtree_def)
       
  1567       next
       
  1568         show "Max ((the_preced (t @ s) \<circ> the_thread) ` Th ` threads (t @ s)) =
       
  1569                       (the_preced (t @ s) \<circ> the_thread) (Th th)" (is "Max ?L = _")
       
  1570         proof -
       
  1571           have "?L = the_preced (t @ s) `  threads (t @ s)" 
       
  1572                      by (unfold image_comp, rule image_cong, auto)
       
  1573           thus ?thesis using max_preced the_preced_def by auto
       
  1574         qed
       
  1575       qed
       
  1576       also have "... = ?R"
       
  1577         using th_cp_max th_cp_preced th_kept 
       
  1578               the_preced_def vat_t.max_cp_readys_threads by auto
       
  1579       finally show ?thesis .
       
  1580     qed 
       
  1581     -- {* Now, since @{term th'} holds the highest @{term cp} 
       
  1582           and we have already show it is in @{term readys},
       
  1583           it is @{term runing} by definition. *}
       
  1584     with `th' \<in> readys (t@s)` show ?thesis by (simp add: runing_def) 
       
  1585   qed
       
  1586   -- {* It is easy to show @{term th'} is an ancestor of @{term th}: *}
       
  1587   moreover have "Th th' \<in> ancestors (RAG (t @ s)) (Th th)" 
       
  1588     using `(Th th, Th th') \<in> (RAG (t @ s))\<^sup>+` by (auto simp:ancestors_def)
       
  1589   ultimately show ?thesis using that by metis
       
  1590 qed
       
  1591 
       
  1592 text {*
       
  1593 
       
  1594   Now it is easy to see there is always a thread to run by case
       
  1595   analysis on whether thread @{term th} is running: if the answer is
       
  1596   yes, the the running thread is obviously @{term th} itself;
       
  1597   otherwise, the running thread is the @{text th'} given by lemma
       
  1598   @{thm th_blockedE}.
       
  1599 
       
  1600 *}
       
  1601 
       
  1602 lemma live: "runing (t@s) \<noteq> {}"
       
  1603 proof(cases "th \<in> runing (t@s)") 
       
  1604   case True thus ?thesis by auto
       
  1605 next
       
  1606   case False
       
  1607   thus ?thesis using th_blockedE by auto
       
  1608 qed
       
  1609 
       
  1610 
       
  1611 end
       
  1612 end
       
  1613 >>>>>>> other