PIPBasics.thy
changeset 115 74fc1eae4605
parent 114 81c6ede5cd11
child 116 a7441db6f4e1
equal deleted inserted replaced
114:81c6ede5cd11 115:74fc1eae4605
   163 apply(subst (2) schs.simps)
   163 apply(subst (2) schs.simps)
   164 apply(simp add: Let_def)
   164 apply(simp add: Let_def)
   165 apply(subst (2) schs.simps)
   165 apply(subst (2) schs.simps)
   166 apply(simp add: Let_def)
   166 apply(simp add: Let_def)
   167 done
   167 done
   168 
       
   169 text {*
       
   170   The following lemmas is an alternative definition of @{term cp},
       
   171   which is based on the notion of subtrees in @{term RAG} and 
       
   172   is handy to use once the abstract theory of {\em relational graph}
       
   173   (and specifically {\em relational tree} and {\em relational forest})
       
   174   are in place.
       
   175 *}
       
   176 lemma cp_alt_def:
       
   177   "cp s th =  
       
   178            Max ((the_preced s) ` {th'. Th th' \<in> (subtree (RAG s) (Th th))})"
       
   179 proof -
       
   180   have "Max (the_preced s ` ({th} \<union> dependants (wq s) th)) =
       
   181         Max (the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)})" 
       
   182           (is "Max (_ ` ?L) = Max (_ ` ?R)")
       
   183   proof -
       
   184     have "?L = ?R" 
       
   185     by (auto dest:rtranclD simp:cs_dependants_def cs_RAG_def s_RAG_def subtree_def)
       
   186     thus ?thesis by simp
       
   187   qed
       
   188   thus ?thesis by (unfold cp_eq cpreced_def, fold the_preced_def, simp)
       
   189 qed
       
   190 
   168 
   191 text {*
   169 text {*
   192   The following @{text "children_RAG_alt_def"} relates
   170   The following @{text "children_RAG_alt_def"} relates
   193   @{term children} in @{term RAG} to the notion of @{term holding}.
   171   @{term children} in @{term RAG} to the notion of @{term holding}.
   194   It is a technical lemmas used to prove the two following lemmas.
   172   It is a technical lemmas used to prove the two following lemmas.
   591 lemma threads_set_eq: 
   569 lemma threads_set_eq: 
   592    "the_thread ` (subtree (tRAG s) (Th th)) = 
   570    "the_thread ` (subtree (tRAG s) (Th th)) = 
   593                   {th'. Th th' \<in> (subtree (RAG s) (Th th))}" (is "?L = ?R")
   571                   {th'. Th th' \<in> (subtree (RAG s) (Th th))}" (is "?L = ?R")
   594    by (auto intro:rev_image_eqI simp:tRAG_subtree_eq)
   572    by (auto intro:rev_image_eqI simp:tRAG_subtree_eq)
   595 
   573 
       
   574 text {*
       
   575   The following lemmas is an alternative definition of @{term cp},
       
   576   which is based on the notion of subtrees in @{term RAG} and 
       
   577   is handy to use once the abstract theory of {\em relational graph}
       
   578   (and specifically {\em relational tree} and {\em relational forest})
       
   579   are in place.
       
   580 *}
       
   581 lemma cp_alt_def:
       
   582   "cp s th =  
       
   583            Max ((the_preced s) ` {th'. Th th' \<in> (subtree (RAG s) (Th th))})"
       
   584 proof -
       
   585   have "Max (the_preced s ` ({th} \<union> dependants (wq s) th)) =
       
   586         Max (the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)})" 
       
   587           (is "Max (_ ` ?L) = Max (_ ` ?R)")
       
   588   proof -
       
   589     have "?L = ?R" 
       
   590     by (auto dest:rtranclD simp:cs_dependants_def cs_RAG_def s_RAG_def subtree_def)
       
   591     thus ?thesis by simp
       
   592   qed
       
   593   thus ?thesis by (unfold cp_eq cpreced_def, fold the_preced_def, simp)
       
   594 qed
       
   595 
       
   596 text {*
       
   597   The following is another definition of @{term cp}.
       
   598 *}
       
   599 lemma cp_alt_def1: 
       
   600   "cp s th = Max ((the_preced s o the_thread) ` (subtree (tRAG s) (Th th)))"
       
   601 proof -
       
   602   have "(the_preced s ` the_thread ` subtree (tRAG s) (Th th)) =
       
   603        ((the_preced s \<circ> the_thread) ` subtree (tRAG s) (Th th))"
       
   604        by auto
       
   605   thus ?thesis by (unfold cp_alt_def, fold threads_set_eq, auto)
       
   606 qed
       
   607 
   596 lemma RAG_tRAG_transfer:
   608 lemma RAG_tRAG_transfer:
   597   assumes  "RAG s' = RAG s \<union> {(Th th, Cs cs)}"
   609   assumes  "RAG s' = RAG s \<union> {(Th th, Cs cs)}"
   598   and "(Cs cs, Th th'') \<in> RAG s"
   610   and "(Cs cs, Th th'') \<in> RAG s"
   599   shows "tRAG s' = tRAG s \<union> {(Th th, Th th'')}" (is "?L = ?R")
   611   shows "tRAG s' = tRAG s \<union> {(Th th, Th th'')}" (is "?L = ?R")
   600 proof -
   612 proof -
   929       show ?thesis by simp
   941       show ?thesis by simp
   930     qed
   942     qed
   931     ultimately show ?thesis by simp
   943     ultimately show ?thesis by simp
   932   qed
   944   qed
   933 qed
   945 qed
       
   946 
       
   947 text {*
       
   948   The following two lemmas are fundamental, because they assure
       
   949   that the numbers of both living and ready threads are finite.
       
   950 *}
       
   951 
       
   952 lemma finite_threads:
       
   953   shows "finite (threads s)"
       
   954   using vt by (induct) (auto elim: step.cases)
       
   955 
       
   956 lemma  finite_readys: "finite (readys s)"
       
   957   using finite_threads readys_threads rev_finite_subset by blast
       
   958 
   934 end
   959 end
   935 
   960 
   936 text {*
   961 text {*
   937   The following locale @{text "valid_moment"} is to inherit the properties 
   962   The following locale @{text "valid_moment"} is to inherit the properties 
   938   derived on any valid state to the prefix of it, with length @{text "i"}.
   963   derived on any valid state to the prefix of it, with length @{text "i"}.
   963   by (unfold_locales, simp)
   988   by (unfold_locales, simp)
   964 
   989 
   965 section {* Waiting queues are distinct *}
   990 section {* Waiting queues are distinct *}
   966 
   991 
   967 text {*
   992 text {*
   968   This section proofs that every waiting queue in the system
   993   This section proves that every waiting queue in the system
   969   is distinct, given in lemma @{text wq_distinct}.
   994   is distinct, given in lemma @{text wq_distinct}.
   970 
   995 
   971   The proof is split into the locales for events (or operations),
   996   The proof is split into the locales for events (or operations),
   972   all contain a lemma named @{text "wq_distinct_kept"} to show that
   997   all contain a lemma named @{text "wq_distinct_kept"} to show that
   973   the distinctiveness is preserved by the respective operation. All lemmas
   998   the distinctiveness is preserved by the respective operation. All lemmas
  1476   finally show ?thesis .
  1501   finally show ?thesis .
  1477 qed
  1502 qed
  1478 
  1503 
  1479 end
  1504 end
  1480 
  1505 
  1481 section {* The change of @{term RAG} *}
  1506 section {* The formation of @{term RAG} *}
  1482 
  1507 
  1483 text {*
  1508 text {*
  1484   The whole of PIP resides on the understanding of the formation
  1509   The whole of PIP resides on the understanding of the formation
  1485   of @{term RAG}. In this section, we are going to investigate how
  1510   of @{term RAG}. We are going to show that @{term RAG}
  1486   @{term RAG} is changed with the execution of every event (or operation).
  1511   forms a finite branching forest. The formalization is divided 
  1487   The effect of every event on @{text RAG} is derived in its respective
  1512   into a series of subsections.
       
  1513 *}
       
  1514 
       
  1515 subsection {* The change of @{term RAG} with each step of execution *}
       
  1516 
       
  1517 text {*
       
  1518   The very essence to prove that @{term RAG} has a certain property is to 
       
  1519   show that this property is preserved as an invariant by the execution 
       
  1520   of the system, and the basis for such kind of invariant proofs is to 
       
  1521   show how @{term RAG} is changed with the execution of every execution step
       
  1522   (or event, or system operation). In this subsection,
       
  1523   the effect of every event on @{text RAG} is derived in its respective
  1488   locale named @{text "RAG_es"} with all lemmas before auxiliary. 
  1524   locale named @{text "RAG_es"} with all lemmas before auxiliary. 
  1489 
  1525 
  1490   The derivation of these @{text "RAG_es"}s forms the very basis 
  1526   These derived @{text "RAG_es"}s constitute the foundation 
  1491   to show the well-formedness of @{term RAG}, 
  1527   to show the various well-formed properties of @{term RAG},  
  1492   for example, @{term RAG} is finite, acyclic, and single-valued, etc.
  1528   for example, @{term RAG} is finite, acyclic, and single-valued, etc.
  1493 
       
  1494 *}
  1529 *}
  1495 
  1530 
  1496 text {*
  1531 text {*
  1497   The following three lemmas show that @{text "RAG"} does not change
  1532   The following three lemmas show that @{text "RAG"} does not change
  1498   by the happening of @{text "Set"}, @{text "Create"} and @{text "Exit"}
  1533   by the happening of @{text "Set"}, @{text "Create"} and @{text "Exit"}
  2268   show ?thesis by (simp add: vt_p.RAG_es vt_p.wne) 
  2303   show ?thesis by (simp add: vt_p.RAG_es vt_p.wne) 
  2269 qed
  2304 qed
  2270 
  2305 
  2271 end
  2306 end
  2272 
  2307 
  2273 section {* RAG is finite *}
  2308 subsection {* RAG is finite *}
  2274 
  2309 
  2275 context valid_trace_v
  2310 context valid_trace_v
  2276 begin
  2311 begin
  2277 
  2312 
  2278 lemma 
  2313 lemma 
  2335       by (unfold_locales, simp)
  2370       by (unfold_locales, simp)
  2336     show ?thesis using Cons by simp
  2371     show ?thesis using Cons by simp
  2337   qed
  2372   qed
  2338 qed
  2373 qed
  2339 end
  2374 end
  2340 
       
  2341 section {* RAG is acyclic *}
       
  2342 
  2375 
  2343 subsection {* Uniqueness of waiting *}
  2376 subsection {* Uniqueness of waiting *}
  2344 
  2377 
  2345 text {*
  2378 text {*
  2346   {\em Uniqueness of waiting} means that 
  2379   {\em Uniqueness of waiting} means that 
  2719   thus ?thesis by (unfold RAG_es, simp)
  2752   thus ?thesis by (unfold RAG_es, simp)
  2720 qed
  2753 qed
  2721 
  2754 
  2722 end
  2755 end
  2723 
  2756 
       
  2757 
       
  2758 subsection {* RAG is acyclic *}
       
  2759 
  2724 context valid_trace
  2760 context valid_trace
  2725 begin
  2761 begin
  2726 
  2762 
  2727 text {* 
  2763 text {* 
  2728   With these @{text "acylic_RAG_kept"}-lemmas proved, 
  2764   With these @{text "acylic_RAG_kept"}-lemmas proved, 
  2791   qed
  2827   qed
  2792 qed
  2828 qed
  2793 
  2829 
  2794 end
  2830 end
  2795 
  2831 
  2796 section {* RAG is single-valued *}
  2832 subsection {* RAG is single-valued *}
  2797 
  2833 
  2798 text {*
  2834 text {*
  2799   The proof that @{term RAG} is single-valued makes use of 
  2835   The proof that @{term RAG} is single-valued makes use of 
  2800   @{text "unique_waiting"} and @{thm held_unique} and the
  2836   @{text "unique_waiting"} and @{thm held_unique} and the
  2801   proof itself is very simple:
  2837   proof itself is very simple:
  2811 lemma sgv_RAG: "single_valued (RAG s)"
  2847 lemma sgv_RAG: "single_valued (RAG s)"
  2812   using unique_RAG by (auto simp:single_valued_def)
  2848   using unique_RAG by (auto simp:single_valued_def)
  2813 
  2849 
  2814 end
  2850 end
  2815 
  2851 
  2816 section {* RAG is well-founded *}
  2852 subsection {* RAG is well-founded *}
  2817 
  2853 
  2818 text {*
  2854 text {*
  2819   In this section, it is proved that both @{term RAG} and 
  2855   In this section, it is proved that both @{term RAG} and 
  2820   its converse @{term "(RAG s)^-1"} are well-founded.
  2856   its converse @{term "(RAG s)^-1"} are well-founded.
  2821   The proof is very simple with the help of
  2857   The proof is very simple with the help of
  2842   show "acyclic (RAG s)" .
  2878   show "acyclic (RAG s)" .
  2843 qed
  2879 qed
  2844 
  2880 
  2845 end
  2881 end
  2846 
  2882 
  2847 section {* RAG forms a finite-branching forest (or tree) *}
  2883 subsection {* RAG forms a finite-branching forest (or tree) *}
  2848 
  2884 
  2849 text {*
  2885 text {*
  2850   With all the well-formedness proofs about @{term RAG} in place, 
  2886   With all the well-formedness proofs about @{term RAG} in place, 
  2851   it is easy to show.
  2887   it is easy to show.
  2852 *}
  2888 *}
  2874       from wf_RAG show "wf (RAG s)" .
  2910       from wf_RAG show "wf (RAG s)" .
  2875     qed
  2911     qed
  2876   qed
  2912   qed
  2877 qed
  2913 qed
  2878 
  2914 
  2879 
  2915 subsection {* Derived properties for sub-graphs of RAG *}
  2880 section {* Derived properties for sub-graphs of RAG *}
       
  2881 
  2916 
  2882 context valid_trace
  2917 context valid_trace
  2883 begin
  2918 begin
  2884 
  2919 
  2885 lemma acyclic_tRAG: "acyclic (tRAG s)"
  2920 lemma acyclic_tRAG: "acyclic (tRAG s)"
  2950 qed
  2985 qed
  2951 
  2986 
  2952 section {* Chain to readys *}
  2987 section {* Chain to readys *}
  2953 
  2988 
  2954 text {*
  2989 text {*
  2955   In this section, a more complete view of @{term RAG} and @{term threads}
  2990   In this section, based on the just derived
  2956   are given. 
  2991   properties about the shape of @{term RAG}, 
       
  2992   a more complete view of the relationship 
       
  2993   between threads is established.
  2957 *}
  2994 *}
  2958 
  2995 
  2959 context valid_trace
  2996 context valid_trace
  2960 begin
  2997 begin
  2961 
  2998 
  2962 text {*
  2999 text {*
  2963   The first lemma is technical, which says out of any node in the domain 
  3000   The first lemma is technical, which says out of any node 
  2964   of @{term RAG}, there is a path leading to a ready thread.
  3001   in the domain of @{term RAG},
       
  3002   (no matter whether it is thread node or resource node)  
       
  3003   there is a path leading to a ready thread.
  2965 *}
  3004 *}
  2966 
  3005 
  2967 lemma chain_building:
  3006 lemma chain_building:
  2968   assumes "node \<in> Domain (RAG s)"
  3007   assumes "node \<in> Domain (RAG s)"
  2969   obtains th' where "th' \<in> readys s" "(node, Th th') \<in> (RAG s)^+"
  3008   obtains th' where "th' \<in> readys s" "(node, Th th') \<in> (RAG s)^+"
  3274   } thus ?thesis by auto
  3313   } thus ?thesis by auto
  3275 qed
  3314 qed
  3276 
  3315 
  3277 end
  3316 end
  3278 
  3317 
  3279 (* ccc *)
       
  3280 section {* Relating @{term cp} and @{term the_preced} and @{term preced} *}
  3318 section {* Relating @{term cp} and @{term the_preced} and @{term preced} *}
  3281 
  3319 
       
  3320 text {*
       
  3321  @{term cp} of a thread is defined to be the maximum of 
       
  3322  the @{term preced}-values (precedences) of all threads in 
       
  3323  its subtree given by @{term RAG}. Therefore, there exits 
       
  3324  a relationship between @{term cp} and @{term preced} (and 
       
  3325  also its variation @{term "the_preced"}) to be explored, 
       
  3326  and this is the target of this section.
       
  3327 *}
       
  3328 
  3282 context valid_trace
  3329 context valid_trace
  3283 begin
  3330 begin
  3284 
  3331 
  3285 lemma finite_threads:
  3332 text {*
  3286   shows "finite (threads s)"
  3333   Since @{term cp} is the maximum of all @{term preced}s in its subtree, 
  3287   using vt by (induct) (auto elim: step.cases)
  3334   which includes itself, it is not difficult to show
  3288 
  3335   that the one thread's precedence is less or equal to its
  3289 lemma  finite_readys: "finite (readys s)"
  3336   @{text cp}-value:
  3290   using finite_threads readys_threads rev_finite_subset by blast
  3337 *}
  3291 
  3338 
  3292 lemma le_cp:
  3339 lemma le_cp:
  3293   shows "preced th s \<le> cp s th"
  3340   shows "preced th s \<le> cp s th"
  3294   proof(unfold cp_alt_def, rule Max_ge)
  3341   proof(unfold cp_alt_def, rule Max_ge)
  3295     show "finite (the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)})"
  3342     show "finite (the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)})"
  3296       by (simp add: finite_subtree_threads)
  3343       by (simp add: finite_subtree_threads)
  3297   next
  3344   next
  3298     show "preced th s \<in> the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)}"
  3345     show "preced th s \<in> the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)}"
  3299       by (simp add: subtree_def the_preced_def)   
  3346       by (simp add: subtree_def the_preced_def)   
  3300   qed
  3347   qed
       
  3348 
       
  3349 text {*
       
  3350   Since @{term cp} is the maximum precedence of its subtree,
       
  3351   and the subtree is only a subset of the set of all threads,
       
  3352   it can be shown that @{text cp} is less or equal to the maximum of
       
  3353   all threads:
       
  3354 *}
  3301 
  3355 
  3302 lemma cp_le:
  3356 lemma cp_le:
  3303   assumes th_in: "th \<in> threads s"
  3357   assumes th_in: "th \<in> threads s"
  3304   shows "cp s th \<le> Max (the_preced s ` threads s)"
  3358   shows "cp s th \<le> Max (the_preced s ` threads s)"
  3305 proof(unfold cp_alt_def, rule Max_f_mono)
  3359 proof(unfold cp_alt_def, rule Max_f_mono)
  3312     using assms
  3366     using assms
  3313     by (smt Domain.DomainI dm_RAG_threads mem_Collect_eq 
  3367     by (smt Domain.DomainI dm_RAG_threads mem_Collect_eq 
  3314         node.inject(1) rtranclD subsetI subtree_def trancl_domain) 
  3368         node.inject(1) rtranclD subsetI subtree_def trancl_domain) 
  3315 qed
  3369 qed
  3316 
  3370 
       
  3371 text {*
       
  3372   Since the @{term cp}-value of each individual thread is less or equal to the 
       
  3373   maximum precedence value of all threads (shown in lemma @{thm cp_le}),
       
  3374   it is easy to derive further that maximum @{term "cp"}-value of
       
  3375   all threads is also less or equal to the latter.
       
  3376 
       
  3377   On the other hand, since the precedence value of each individual 
       
  3378   thread is less of equal to its own @{term cp}-value (shown in lemma @{thm le_cp}),
       
  3379   it is easy to show that the maximum of the former is less or equal to the 
       
  3380   maximum of the latter. 
       
  3381 
       
  3382   By combining these two perspectives, we get the following equality 
       
  3383   between the two maximums:
       
  3384 *}
       
  3385 
  3317 lemma max_cp_eq: 
  3386 lemma max_cp_eq: 
  3318   shows "Max ((cp s) ` threads s) = Max (the_preced s ` threads s)"
  3387   shows "Max ((cp s) ` threads s) = Max (the_preced s ` threads s)"
  3319   (is "?L = ?R")
  3388   (is "?L = ?R")
  3320 proof -
  3389 proof -
  3321   have "?L \<le> ?R" 
  3390   have "?L \<le> ?R" 
  3332         simp add: le_cp the_preced_def)
  3401         simp add: le_cp the_preced_def)
  3333   ultimately show ?thesis by auto
  3402   ultimately show ?thesis by auto
  3334 qed
  3403 qed
  3335 
  3404 
  3336 text {* (* ddd *) \noindent
  3405 text {* (* ddd *) \noindent
  3337   Since the current precedence of the threads in ready queue will always be boosted,
  3406   According to @{thm threads_alt_def} and @{thm readys_subtree_disjoint} , 
  3338   there must be one inside it has the maximum precedence of the whole system. 
  3407   the set of @{term threads} can be partitioned into subtrees of the 
  3339 *}
  3408   threads in @{term readys}, and also because  @{term cp}-value of a thread is 
  3340 lemma max_cp_readys_threads:
  3409   the maximum precedence of its own subtree, by applying 
  3341   shows "Max (cp s ` readys s) = Max (cp s ` threads s)" (is "?L = ?R")
  3410   the absorbing property of @{term Max} (lemma @{thm Max_UNION}) 
       
  3411   over the union of subtrees, the following equation can be derived:
       
  3412 *}
       
  3413 
       
  3414 lemma max_cp_readys_max_preced:
       
  3415   shows "Max (cp s ` readys s) = Max (the_preced s ` threads s)" (is "?L = ?R")
  3342 proof(cases "readys s = {}")
  3416 proof(cases "readys s = {}")
  3343   case False
  3417   case False
  3344   have "?R = Max (the_preced s ` threads s)" by (unfold max_cp_eq, simp)
  3418   have "?R = 
  3345   also have "... = 
  3419     Max (the_preced s ` (\<Union>th\<in>readys s. {th'. Th th' \<in> subtree (RAG s) (Th th)}))"
  3346     Max (the_preced s ` (\<Union>th\<in>readys s. {th'. Th th' \<in> subtree (RAG s) (Th th)}))" 
  3420     by (simp add: threads_alt_def)  
  3347          by (unfold threads_alt_def, simp)
       
  3348   also have "... = 
  3421   also have "... = 
  3349     Max ((\<Union>th\<in>readys s. the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)}))"
  3422     Max ((\<Union>th\<in>readys s. the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)}))"
  3350           by (unfold image_UN, simp)
  3423           by (unfold image_UN, simp)
  3351   also have "... = 
  3424   also have "... = 
  3352     Max (Max ` (\<lambda>th. the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)}) ` readys s)" 
  3425     Max (Max ` (\<lambda>th. the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)}) ` readys s)" 
  3353   proof(rule Max_UNION)
  3426   proof(rule Max_UNION)
  3354     show "\<forall>M\<in>(\<lambda>x. the_preced s ` 
  3427     show "\<forall>M\<in>(\<lambda>x. the_preced s ` 
  3355                     {th'. Th th' \<in> subtree (RAG s) (Th x)}) ` readys s. finite M"
  3428                     {th'. Th th' \<in> subtree (RAG s) (Th x)}) ` readys s. finite M"
  3356                         using finite_subtree_threads by auto
  3429                         using finite_subtree_threads by auto
  3357   qed (auto simp:False subtree_def finite_readys)
  3430   qed (auto simp:False subtree_def finite_readys)
  3358   also have "... =  
  3431   also have "... = 
  3359     Max ((Max \<circ> (\<lambda>th. the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)})) ` readys s)" 
  3432      Max ((Max \<circ> (\<lambda>th. the_preced s ` {th'. Th th' \<in> subtree (RAG s) (Th th)})) ` 
  3360       by (unfold image_comp, simp)
  3433                         readys s)" 
       
  3434       by (simp add: image_comp)
  3361   also have "... = ?L" (is "Max (?f ` ?A) = Max (?g ` ?A)")
  3435   also have "... = ?L" (is "Max (?f ` ?A) = Max (?g ` ?A)")
  3362   proof -
  3436   proof -
  3363     have "(?f ` ?A) = (?g ` ?A)"
  3437     have "(?f ` ?A) = (?g ` ?A)"
  3364     proof(rule f_image_eq)
  3438     proof(rule f_image_eq)
  3365       fix th1 
  3439       fix th1 
  3370     thus ?thesis by simp
  3444     thus ?thesis by simp
  3371   qed
  3445   qed
  3372   finally show ?thesis by simp
  3446   finally show ?thesis by simp
  3373 qed (auto simp:threads_alt_def)
  3447 qed (auto simp:threads_alt_def)
  3374 
  3448 
  3375 end
  3449 text {*
       
  3450   The following lemma is simply a corollary of @{thm max_cp_readys_max_preced}
       
  3451   and @{thm max_cp_eq}:
       
  3452 *}
       
  3453 lemma max_cp_readys_threads:
       
  3454   shows "Max (cp s ` readys s) = Max (cp s ` threads s)" 
       
  3455     using max_cp_readys_max_preced max_cp_eq by auto
       
  3456 
       
  3457 end
       
  3458 
  3376 
  3459 
  3377 section {* Relating @{term cntP}, @{term cntV}, @{term cntCS} and @{term pvD} *}
  3460 section {* Relating @{term cntP}, @{term cntV}, @{term cntCS} and @{term pvD} *}
       
  3461 
       
  3462 text {*
       
  3463   As explained in the section where @{term "cntP"},
       
  3464   @{term "cntV"} and @{term "cntCS"} are defined, 
       
  3465   we need to establish a equation (in lemma @{text "cnp_cnv_cncs"}) 
       
  3466   so that the last can be calculated out of the first two. 
       
  3467 
       
  3468   While the calculation of @{term "cntV"} and @{term "cntCS"}
       
  3469   are relatively simple, the calculation of @{term "cntCS"} and 
       
  3470   @{term "pvD"} are complicated, because their definitions
       
  3471   involve a number of other functions such as @{term holdents}, @{term readys}, 
       
  3472   and @{term threads}. To prove  @{text "cnp_cnv_cncs"}, 
       
  3473   we need to investigate how the values of these functions
       
  3474   are changed with the execution of each kind of system operation.
       
  3475   Following conventions, such investigation are divided into 
       
  3476   locales corresponding to system operations.
       
  3477 *}
  3378 
  3478 
  3379 context valid_trace_p_w
  3479 context valid_trace_p_w
  3380 begin
  3480 begin
  3381 
  3481 
  3382 lemma holding_s_holder: "holding s holder cs"
  3482 lemma holding_s_holder: "holding s holder cs"
  4631 section {* Corollaries of @{thm valid_trace.cnp_cnv_cncs} *}
  4731 section {* Corollaries of @{thm valid_trace.cnp_cnv_cncs} *}
  4632 
  4732 
  4633 context valid_trace
  4733 context valid_trace
  4634 begin
  4734 begin
  4635 
  4735 
       
  4736 text {*
       
  4737   The following two lemmas are purely technical, which says
       
  4738   a non-living thread can not hold any resource.
       
  4739 *}
  4636 lemma not_thread_holdents:
  4740 lemma not_thread_holdents:
  4637   assumes not_in: "th \<notin> threads s" 
  4741   assumes not_in: "th \<notin> threads s" 
  4638   shows "holdents s th = {}"
  4742   shows "holdents s th = {}"
  4639 proof -
  4743 proof -
  4640   { fix cs
  4744   { fix cs
  4651 lemma not_thread_cncs:
  4755 lemma not_thread_cncs:
  4652   assumes not_in: "th \<notin> threads s" 
  4756   assumes not_in: "th \<notin> threads s" 
  4653   shows "cntCS s th = 0"
  4757   shows "cntCS s th = 0"
  4654   using not_thread_holdents[OF assms]
  4758   using not_thread_holdents[OF assms]
  4655   by (simp add:cntCS_def)
  4759   by (simp add:cntCS_def)
       
  4760 
       
  4761 text {*
       
  4762   Starting from the following @{text cnp_cnv_eq}, all 
       
  4763   lemmas in this section concern the meaning of 
       
  4764   condition @{prop "cntP s th = cntV s th"}, i.e.
       
  4765   when the numbers of resource requesting and resource releasing
       
  4766   are equal.
       
  4767 *}
  4656 
  4768 
  4657 lemma cnp_cnv_eq:
  4769 lemma cnp_cnv_eq:
  4658   assumes "th \<notin> threads s"
  4770   assumes "th \<notin> threads s"
  4659   shows "cntP s th = cntV s th"
  4771   shows "cntP s th = cntV s th"
  4660   using assms cnp_cnv_cncs not_thread_cncs pvD_def
  4772   using assms cnp_cnv_cncs not_thread_cncs pvD_def
  4697       by (auto simp:children_def)
  4809       by (auto simp:children_def)
  4698     with eq_pv_children[OF assms]
  4810     with eq_pv_children[OF assms]
  4699     show False by simp
  4811     show False by simp
  4700 qed
  4812 qed
  4701 
  4813 
       
  4814 lemma count_eq_RAG_plus_Th:
       
  4815   assumes "cntP s th = cntV s th"
       
  4816   shows "{Th th' | th'. (Th th', Th th) \<in> (RAG s)^+} = {}"
       
  4817   using count_eq_RAG_plus[OF assms] by auto
       
  4818 
  4702 lemma eq_pv_dependants:
  4819 lemma eq_pv_dependants:
  4703   assumes eq_pv: "cntP s th = cntV s th"
  4820   assumes eq_pv: "cntP s th = cntV s th"
  4704   shows "dependants s th = {}"
  4821   shows "dependants s th = {}"
  4705 proof -
  4822 proof -
  4706   from count_eq_RAG_plus[OF assms, folded dependants_alt_def1]
  4823   from count_eq_RAG_plus[OF assms, folded dependants_alt_def1]
  4710 lemma count_eq_tRAG_plus:
  4827 lemma count_eq_tRAG_plus:
  4711   assumes "cntP s th = cntV s th"
  4828   assumes "cntP s th = cntV s th"
  4712   shows "{th'. (Th th', Th th) \<in> (tRAG s)^+} = {}"
  4829   shows "{th'. (Th th', Th th) \<in> (tRAG s)^+} = {}"
  4713   using assms eq_pv_dependants dependants_alt_def eq_dependants by auto 
  4830   using assms eq_pv_dependants dependants_alt_def eq_dependants by auto 
  4714 
  4831 
  4715 lemma count_eq_RAG_plus_Th:
       
  4716   assumes "cntP s th = cntV s th"
       
  4717   shows "{Th th' | th'. (Th th', Th th) \<in> (RAG s)^+} = {}"
       
  4718   using count_eq_RAG_plus[OF assms] by auto
       
  4719 
       
  4720 lemma count_eq_tRAG_plus_Th:
  4832 lemma count_eq_tRAG_plus_Th:
  4721   assumes "cntP s th = cntV s th"
  4833   assumes "cntP s th = cntV s th"
  4722   shows "{Th th' | th'. (Th th', Th th) \<in> (tRAG s)^+} = {}"
  4834   shows "{Th th' | th'. (Th th', Th th) \<in> (tRAG s)^+} = {}"
  4723    using count_eq_tRAG_plus[OF assms] by auto
  4835    using count_eq_tRAG_plus[OF assms] by auto
  4724 
  4836 
  4725 end
  4837 end
  4726 
  4838 
       
  4839 subsection {* A notion @{text detached} and its relation with @{term cntP} 
       
  4840   and @{term cntV} *}
       
  4841 
  4727 definition detached :: "state \<Rightarrow> thread \<Rightarrow> bool"
  4842 definition detached :: "state \<Rightarrow> thread \<Rightarrow> bool"
  4728   where "detached s th \<equiv> (\<not>(\<exists> cs. holding s th cs)) \<and> (\<not>(\<exists>cs. waiting s th cs))"
  4843   where "detached s th \<equiv> (\<not>(\<exists> cs. holding s th cs)) \<and> (\<not>(\<exists>cs. waiting s th cs))"
       
  4844 
       
  4845 text {*
       
  4846   The following lemma shows that a thread is detached means 
       
  4847   it is isolated from @{term RAG}:
       
  4848 *}
  4729 
  4849 
  4730 lemma detached_test:
  4850 lemma detached_test:
  4731   shows "detached s th = (Th th \<notin> Field (RAG s))"
  4851   shows "detached s th = (Th th \<notin> Field (RAG s))"
  4732 apply(simp add: detached_def Field_def)
  4852 apply(simp add: detached_def Field_def)
  4733 apply(simp add: s_RAG_def)
  4853 apply(simp add: s_RAG_def)
  4797   shows "(detached s th) = (cntP s th = cntV s th)"
  4917   shows "(detached s th) = (cntP s th = cntV s th)"
  4798   by (insert vt, auto intro:detached_intro detached_elim)
  4918   by (insert vt, auto intro:detached_intro detached_elim)
  4799 
  4919 
  4800 end
  4920 end
  4801 
  4921 
  4802 section {* Recursive definition of @{term "cp"} *}
       
  4803 
       
  4804 lemma cp_alt_def1: 
       
  4805   "cp s th = Max ((the_preced s o the_thread) ` (subtree (tRAG s) (Th th)))"
       
  4806 proof -
       
  4807   have "(the_preced s ` the_thread ` subtree (tRAG s) (Th th)) =
       
  4808        ((the_preced s \<circ> the_thread) ` subtree (tRAG s) (Th th))"
       
  4809        by auto
       
  4810   thus ?thesis by (unfold cp_alt_def, fold threads_set_eq, auto)
       
  4811 qed
       
  4812 
       
  4813 lemma cp_gen_def_cond: 
       
  4814   assumes "x = Th th"
       
  4815   shows "cp s th = cp_gen s (Th th)"
       
  4816 by (unfold cp_alt_def1 cp_gen_def, simp)
       
  4817 
       
  4818 lemma cp_gen_over_set:
       
  4819   assumes "\<forall> x \<in> A. \<exists> th. x = Th th"
       
  4820   shows "cp_gen s ` A = (cp s \<circ> the_thread) ` A"
       
  4821 proof(rule f_image_eq)
       
  4822   fix a
       
  4823   assume "a \<in> A"
       
  4824   from assms[rule_format, OF this]
       
  4825   obtain th where eq_a: "a = Th th" by auto
       
  4826   show "cp_gen s a = (cp s \<circ> the_thread) a"
       
  4827     by  (unfold eq_a, simp, unfold cp_gen_def_cond[OF refl[of "Th th"]], simp)
       
  4828 qed
       
  4829 
       
  4830 
       
  4831 context valid_trace
       
  4832 begin
       
  4833 (* ddd *)
       
  4834 lemma cp_gen_rec:
       
  4835   assumes "x = Th th"
       
  4836   shows "cp_gen s x = Max ({the_preced s th} \<union> (cp_gen s) ` children (tRAG s) x)"
       
  4837 proof(cases "children (tRAG s) x = {}")
       
  4838   case True
       
  4839   show ?thesis
       
  4840     by (unfold True cp_gen_def subtree_children, simp add:assms)
       
  4841 next
       
  4842   case False
       
  4843   hence [simp]: "children (tRAG s) x \<noteq> {}" by auto
       
  4844   note fsbttRAGs.finite_subtree[simp]
       
  4845   have [simp]: "finite (children (tRAG s) x)"
       
  4846      by (intro rev_finite_subset[OF fsbttRAGs.finite_subtree], 
       
  4847             rule children_subtree)
       
  4848   { fix r x
       
  4849     have "subtree r x \<noteq> {}" by (auto simp:subtree_def)
       
  4850   } note this[simp]
       
  4851   have [simp]: "\<exists>x\<in>children (tRAG s) x. subtree (tRAG s) x \<noteq> {}"
       
  4852   proof -
       
  4853     from False obtain q where "q \<in> children (tRAG s) x" by blast
       
  4854     moreover have "subtree (tRAG s) q \<noteq> {}" by simp
       
  4855     ultimately show ?thesis by blast
       
  4856   qed
       
  4857   have h: "Max ((the_preced s \<circ> the_thread) `
       
  4858                 ({x} \<union> \<Union>(subtree (tRAG s) ` children (tRAG s) x))) =
       
  4859         Max ({the_preced s th} \<union> cp_gen s ` children (tRAG s) x)"
       
  4860                      (is "?L = ?R")
       
  4861   proof -
       
  4862     let "Max (?f ` (?A \<union> \<Union> (?g ` ?B)))" = ?L
       
  4863     let "Max (_ \<union> (?h ` ?B))" = ?R
       
  4864     let ?L1 = "?f ` \<Union>(?g ` ?B)"
       
  4865     have eq_Max_L1: "Max ?L1 = Max (?h ` ?B)"
       
  4866     proof -
       
  4867       have "?L1 = ?f ` (\<Union> x \<in> ?B.(?g x))" by simp
       
  4868       also have "... =  (\<Union> x \<in> ?B. ?f ` (?g x))" by auto
       
  4869       finally have "Max ?L1 = Max ..." by simp
       
  4870       also have "... = Max (Max ` (\<lambda>x. ?f ` subtree (tRAG s) x) ` ?B)"
       
  4871         by (subst Max_UNION, simp+)
       
  4872       also have "... = Max (cp_gen s ` children (tRAG s) x)"
       
  4873           by (unfold image_comp cp_gen_alt_def, simp)
       
  4874       finally show ?thesis .
       
  4875     qed
       
  4876     show ?thesis
       
  4877     proof -
       
  4878       have "?L = Max (?f ` ?A \<union> ?L1)" by simp
       
  4879       also have "... = max (the_preced s (the_thread x)) (Max ?L1)"
       
  4880             by (subst Max_Un, simp+)
       
  4881       also have "... = max (?f x) (Max (?h ` ?B))"
       
  4882         by (unfold eq_Max_L1, simp)
       
  4883       also have "... =?R"
       
  4884         by (rule max_Max_eq, (simp)+, unfold assms, simp)
       
  4885       finally show ?thesis .
       
  4886     qed
       
  4887   qed  thus ?thesis 
       
  4888           by (fold h subtree_children, unfold cp_gen_def, simp) 
       
  4889 qed
       
  4890 
       
  4891 lemma cp_rec:
       
  4892   "cp s th = Max ({the_preced s th} \<union> 
       
  4893                      (cp s o the_thread) ` children (tRAG s) (Th th))"
       
  4894 proof -
       
  4895   have "Th th = Th th" by simp
       
  4896   note h =  cp_gen_def_cond[OF this] cp_gen_rec[OF this]
       
  4897   show ?thesis 
       
  4898   proof -
       
  4899     have "cp_gen s ` children (tRAG s) (Th th) = 
       
  4900                 (cp s \<circ> the_thread) ` children (tRAG s) (Th th)"
       
  4901     proof(rule cp_gen_over_set)
       
  4902       show " \<forall>x\<in>children (tRAG s) (Th th). \<exists>th. x = Th th"
       
  4903         by (unfold tRAG_alt_def, auto simp:children_def)
       
  4904     qed
       
  4905     thus ?thesis by (subst (1) h(1), unfold h(2), simp)
       
  4906   qed
       
  4907 qed
       
  4908 end
       
  4909 
       
  4910 section {* Other properties useful in Implementation.thy or Correctness.thy *}
  4922 section {* Other properties useful in Implementation.thy or Correctness.thy *}
  4911 
  4923 
  4912 context valid_trace_e 
  4924 context valid_trace_e 
  4913 begin
  4925 begin
  4914 
  4926