1 theory Correctness |
|
2 imports PIPBasics |
|
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 sublocale red_extend_highest_gen < red_moment: extend_highest_gen "s" "th" "prio" "tm" "(moment i t)" |
|
131 apply (insert extend_highest_gen_axioms, subst (asm) (1) moment_restm_s [of i t, symmetric]) |
|
132 apply (unfold extend_highest_gen_def extend_highest_gen_axioms_def, clarsimp) |
|
133 by (unfold highest_gen_def, auto dest:step_back_vt_app) |
|
134 |
|
135 context extend_highest_gen |
|
136 begin |
|
137 |
|
138 lemma ind [consumes 0, case_names Nil Cons, induct type]: |
|
139 assumes |
|
140 h0: "R []" |
|
141 and h2: "\<And> e t. \<lbrakk>vt (t@s); step (t@s) e; |
|
142 extend_highest_gen s th prio tm t; |
|
143 extend_highest_gen s th prio tm (e#t); R t\<rbrakk> \<Longrightarrow> R (e#t)" |
|
144 shows "R t" |
|
145 proof - |
|
146 from vt_t extend_highest_gen_axioms show ?thesis |
|
147 proof(induct t) |
|
148 from h0 show "R []" . |
|
149 next |
|
150 case (Cons e t') |
|
151 assume ih: "\<lbrakk>vt (t' @ s); extend_highest_gen s th prio tm t'\<rbrakk> \<Longrightarrow> R t'" |
|
152 and vt_e: "vt ((e # t') @ s)" |
|
153 and et: "extend_highest_gen s th prio tm (e # t')" |
|
154 from vt_e and step_back_step have stp: "step (t'@s) e" by auto |
|
155 from vt_e and step_back_vt have vt_ts: "vt (t'@s)" by auto |
|
156 show ?case |
|
157 proof(rule h2 [OF vt_ts stp _ _ _ ]) |
|
158 show "R t'" |
|
159 proof(rule ih) |
|
160 from et show ext': "extend_highest_gen s th prio tm t'" |
|
161 by (unfold extend_highest_gen_def extend_highest_gen_axioms_def, auto dest:step_back_vt) |
|
162 next |
|
163 from vt_ts show "vt (t' @ s)" . |
|
164 qed |
|
165 next |
|
166 from et show "extend_highest_gen s th prio tm (e # t')" . |
|
167 next |
|
168 from et show ext': "extend_highest_gen s th prio tm t'" |
|
169 by (unfold extend_highest_gen_def extend_highest_gen_axioms_def, auto dest:step_back_vt) |
|
170 qed |
|
171 qed |
|
172 qed |
|
173 |
|
174 |
|
175 lemma th_kept: "th \<in> threads (t @ s) \<and> |
|
176 preced th (t@s) = preced th s" (is "?Q t") |
|
177 proof - |
|
178 show ?thesis |
|
179 proof(induct rule:ind) |
|
180 case Nil |
|
181 from threads_s |
|
182 show ?case |
|
183 by auto |
|
184 next |
|
185 case (Cons e t) |
|
186 interpret h_e: extend_highest_gen _ _ _ _ "(e # t)" using Cons by auto |
|
187 interpret h_t: extend_highest_gen _ _ _ _ t using Cons by auto |
|
188 show ?case |
|
189 proof(cases e) |
|
190 case (Create thread prio) |
|
191 show ?thesis |
|
192 proof - |
|
193 from Cons and Create have "step (t@s) (Create thread prio)" by auto |
|
194 hence "th \<noteq> thread" |
|
195 proof(cases) |
|
196 case thread_create |
|
197 with Cons show ?thesis by auto |
|
198 qed |
|
199 hence "preced th ((e # t) @ s) = preced th (t @ s)" |
|
200 by (unfold Create, auto simp:preced_def) |
|
201 moreover note Cons |
|
202 ultimately show ?thesis |
|
203 by (auto simp:Create) |
|
204 qed |
|
205 next |
|
206 case (Exit thread) |
|
207 from h_e.exit_diff and Exit |
|
208 have neq_th: "thread \<noteq> th" by auto |
|
209 with Cons |
|
210 show ?thesis |
|
211 by (unfold Exit, auto simp:preced_def) |
|
212 next |
|
213 case (P thread cs) |
|
214 with Cons |
|
215 show ?thesis |
|
216 by (auto simp:P preced_def) |
|
217 next |
|
218 case (V thread cs) |
|
219 with Cons |
|
220 show ?thesis |
|
221 by (auto simp:V preced_def) |
|
222 next |
|
223 case (Set thread prio') |
|
224 show ?thesis |
|
225 proof - |
|
226 from h_e.set_diff_low and Set |
|
227 have "th \<noteq> thread" by auto |
|
228 hence "preced th ((e # t) @ s) = preced th (t @ s)" |
|
229 by (unfold Set, auto simp:preced_def) |
|
230 moreover note Cons |
|
231 ultimately show ?thesis |
|
232 by (auto simp:Set) |
|
233 qed |
|
234 qed |
|
235 qed |
|
236 qed |
|
237 |
|
238 text {* |
|
239 According to @{thm th_kept}, thread @{text "th"} has its living status |
|
240 and precedence kept along the way of @{text "t"}. The following lemma |
|
241 shows that this preserved precedence of @{text "th"} remains as the highest |
|
242 along the way of @{text "t"}. |
|
243 |
|
244 The proof goes by induction over @{text "t"} using the specialized |
|
245 induction rule @{thm ind}, followed by case analysis of each possible |
|
246 operations of PIP. All cases follow the same pattern rendered by the |
|
247 generalized introduction rule @{thm "image_Max_eqI"}. |
|
248 |
|
249 The very essence is to show that precedences, no matter whether they |
|
250 are newly introduced or modified, are always lower than the one held |
|
251 by @{term "th"}, which by @{thm th_kept} is preserved along the way. |
|
252 *} |
|
253 lemma max_kept: "Max (the_preced (t @ s) ` (threads (t@s))) = preced th s" |
|
254 proof(induct rule:ind) |
|
255 case Nil |
|
256 from highest_preced_thread |
|
257 show ?case by simp |
|
258 next |
|
259 case (Cons e t) |
|
260 interpret h_e: extend_highest_gen _ _ _ _ "(e # t)" using Cons by auto |
|
261 interpret h_t: extend_highest_gen _ _ _ _ t using Cons by auto |
|
262 show ?case |
|
263 proof(cases e) |
|
264 case (Create thread prio') |
|
265 show ?thesis (is "Max (?f ` ?A) = ?t") |
|
266 proof - |
|
267 -- {* The following is the common pattern of each branch of the case analysis. *} |
|
268 -- {* The major part is to show that @{text "th"} holds the highest precedence: *} |
|
269 have "Max (?f ` ?A) = ?f th" |
|
270 proof(rule image_Max_eqI) |
|
271 show "finite ?A" using h_e.finite_threads by auto |
|
272 next |
|
273 show "th \<in> ?A" using h_e.th_kept by auto |
|
274 next |
|
275 show "\<forall>x\<in>?A. ?f x \<le> ?f th" |
|
276 proof |
|
277 fix x |
|
278 assume "x \<in> ?A" |
|
279 hence "x = thread \<or> x \<in> threads (t@s)" by (auto simp:Create) |
|
280 thus "?f x \<le> ?f th" |
|
281 proof |
|
282 assume "x = thread" |
|
283 thus ?thesis |
|
284 apply (simp add:Create the_preced_def preced_def, fold preced_def) |
|
285 using Create h_e.create_low h_t.th_kept lt_tm preced_leI2 |
|
286 preced_th by force |
|
287 next |
|
288 assume h: "x \<in> threads (t @ s)" |
|
289 from Cons(2)[unfolded Create] |
|
290 have "x \<noteq> thread" using h by (cases, auto) |
|
291 hence "?f x = the_preced (t@s) x" |
|
292 by (simp add:Create the_preced_def preced_def) |
|
293 hence "?f x \<le> Max (the_preced (t@s) ` threads (t@s))" |
|
294 by (simp add: h_t.finite_threads h) |
|
295 also have "... = ?f th" |
|
296 by (metis Cons.hyps(5) h_e.th_kept the_preced_def) |
|
297 finally show ?thesis . |
|
298 qed |
|
299 qed |
|
300 qed |
|
301 -- {* The minor part is to show that the precedence of @{text "th"} |
|
302 equals to preserved one, given by the foregoing lemma @{thm th_kept} *} |
|
303 also have "... = ?t" using h_e.th_kept the_preced_def by auto |
|
304 -- {* Then it follows trivially that the precedence preserved |
|
305 for @{term "th"} remains the maximum of all living threads along the way. *} |
|
306 finally show ?thesis . |
|
307 qed |
|
308 next |
|
309 case (Exit thread) |
|
310 show ?thesis (is "Max (?f ` ?A) = ?t") |
|
311 proof - |
|
312 have "Max (?f ` ?A) = ?f th" |
|
313 proof(rule image_Max_eqI) |
|
314 show "finite ?A" using h_e.finite_threads by auto |
|
315 next |
|
316 show "th \<in> ?A" using h_e.th_kept by auto |
|
317 next |
|
318 show "\<forall>x\<in>?A. ?f x \<le> ?f th" |
|
319 proof |
|
320 fix x |
|
321 assume "x \<in> ?A" |
|
322 hence "x \<in> threads (t@s)" by (simp add: Exit) |
|
323 hence "?f x \<le> Max (?f ` threads (t@s))" |
|
324 by (simp add: h_t.finite_threads) |
|
325 also have "... \<le> ?f th" |
|
326 apply (simp add:Exit the_preced_def preced_def, fold preced_def) |
|
327 using Cons.hyps(5) h_t.th_kept the_preced_def by auto |
|
328 finally show "?f x \<le> ?f th" . |
|
329 qed |
|
330 qed |
|
331 also have "... = ?t" using h_e.th_kept the_preced_def by auto |
|
332 finally show ?thesis . |
|
333 qed |
|
334 next |
|
335 case (P thread cs) |
|
336 with Cons |
|
337 show ?thesis by (auto simp:preced_def the_preced_def) |
|
338 next |
|
339 case (V thread cs) |
|
340 with Cons |
|
341 show ?thesis by (auto simp:preced_def the_preced_def) |
|
342 next |
|
343 case (Set thread prio') |
|
344 show ?thesis (is "Max (?f ` ?A) = ?t") |
|
345 proof - |
|
346 have "Max (?f ` ?A) = ?f th" |
|
347 proof(rule image_Max_eqI) |
|
348 show "finite ?A" using h_e.finite_threads by auto |
|
349 next |
|
350 show "th \<in> ?A" using h_e.th_kept by auto |
|
351 next |
|
352 show "\<forall>x\<in>?A. ?f x \<le> ?f th" |
|
353 proof |
|
354 fix x |
|
355 assume h: "x \<in> ?A" |
|
356 show "?f x \<le> ?f th" |
|
357 proof(cases "x = thread") |
|
358 case True |
|
359 moreover have "the_preced (Set thread prio' # t @ s) thread \<le> the_preced (t @ s) th" |
|
360 proof - |
|
361 have "the_preced (t @ s) th = Prc prio tm" |
|
362 using h_t.th_kept preced_th by (simp add:the_preced_def) |
|
363 moreover have "prio' \<le> prio" using Set h_e.set_diff_low by auto |
|
364 ultimately show ?thesis by (insert lt_tm, auto simp:the_preced_def preced_def) |
|
365 qed |
|
366 ultimately show ?thesis |
|
367 by (unfold Set, simp add:the_preced_def preced_def) |
|
368 next |
|
369 case False |
|
370 then have "?f x = the_preced (t@s) x" |
|
371 by (simp add:the_preced_def preced_def Set) |
|
372 also have "... \<le> Max (the_preced (t@s) ` threads (t@s))" |
|
373 using Set h h_t.finite_threads by auto |
|
374 also have "... = ?f th" by (metis Cons.hyps(5) h_e.th_kept the_preced_def) |
|
375 finally show ?thesis . |
|
376 qed |
|
377 qed |
|
378 qed |
|
379 also have "... = ?t" using h_e.th_kept the_preced_def by auto |
|
380 finally show ?thesis . |
|
381 qed |
|
382 qed |
|
383 qed |
|
384 |
|
385 lemma max_preced: "preced th (t@s) = Max (the_preced (t@s) ` (threads (t@s)))" |
|
386 by (insert th_kept max_kept, auto) |
|
387 |
|
388 text {* |
|
389 The reason behind the following lemma is that: |
|
390 Since @{term "cp"} is defined as the maximum precedence |
|
391 of those threads contained in the sub-tree of node @{term "Th th"} |
|
392 in @{term "RAG (t@s)"}, and all these threads are living threads, and |
|
393 @{term "th"} is also among them, the maximum precedence of |
|
394 them all must be the one for @{text "th"}. |
|
395 *} |
|
396 lemma th_cp_max_preced: |
|
397 "cp (t@s) th = Max (the_preced (t@s) ` (threads (t@s)))" (is "?L = ?R") |
|
398 proof - |
|
399 let ?f = "the_preced (t@s)" |
|
400 have "?L = ?f th" |
|
401 proof(unfold cp_alt_def, rule image_Max_eqI) |
|
402 show "finite {th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}" |
|
403 proof - |
|
404 have "{th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)} = |
|
405 the_thread ` {n . n \<in> subtree (RAG (t @ s)) (Th th) \<and> |
|
406 (\<exists> th'. n = Th th')}" |
|
407 by (smt Collect_cong Setcompr_eq_image mem_Collect_eq the_thread.simps) |
|
408 moreover have "finite ..." by (simp add: vat_t.fsbtRAGs.finite_subtree) |
|
409 ultimately show ?thesis by simp |
|
410 qed |
|
411 next |
|
412 show "th \<in> {th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}" |
|
413 by (auto simp:subtree_def) |
|
414 next |
|
415 show "\<forall>x\<in>{th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}. |
|
416 the_preced (t @ s) x \<le> the_preced (t @ s) th" |
|
417 proof |
|
418 fix th' |
|
419 assume "th' \<in> {th'. Th th' \<in> subtree (RAG (t @ s)) (Th th)}" |
|
420 hence "Th th' \<in> subtree (RAG (t @ s)) (Th th)" by auto |
|
421 moreover have "... \<subseteq> Field (RAG (t @ s)) \<union> {Th th}" |
|
422 by (meson subtree_Field) |
|
423 ultimately have "Th th' \<in> ..." by auto |
|
424 hence "th' \<in> threads (t@s)" |
|
425 proof |
|
426 assume "Th th' \<in> {Th th}" |
|
427 thus ?thesis using th_kept by auto |
|
428 next |
|
429 assume "Th th' \<in> Field (RAG (t @ s))" |
|
430 thus ?thesis using vat_t.not_in_thread_isolated by blast |
|
431 qed |
|
432 thus "the_preced (t @ s) th' \<le> the_preced (t @ s) th" |
|
433 by (metis Max_ge finite_imageI finite_threads image_eqI |
|
434 max_kept th_kept the_preced_def) |
|
435 qed |
|
436 qed |
|
437 also have "... = ?R" by (simp add: max_preced the_preced_def) |
|
438 finally show ?thesis . |
|
439 qed |
|
440 |
|
441 lemma th_cp_max[simp]: "Max (cp (t@s) ` threads (t@s)) = cp (t@s) th" |
|
442 using max_cp_eq th_cp_max_preced the_preced_def vt_t by presburger |
|
443 |
|
444 lemma [simp]: "Max (cp (t@s) ` threads (t@s)) = Max (the_preced (t@s) ` threads (t@s))" |
|
445 by (simp add: th_cp_max_preced) |
|
446 |
|
447 lemma [simp]: "Max (the_preced (t@s) ` threads (t@s)) = the_preced (t@s) th" |
|
448 using max_kept th_kept the_preced_def by auto |
|
449 |
|
450 lemma [simp]: "the_preced (t@s) th = preced th (t@s)" |
|
451 using the_preced_def by auto |
|
452 |
|
453 lemma [simp]: "preced th (t@s) = preced th s" |
|
454 by (simp add: th_kept) |
|
455 |
|
456 lemma [simp]: "cp s th = preced th s" |
|
457 by (simp add: eq_cp_s_th) |
|
458 |
|
459 lemma th_cp_preced [simp]: "cp (t@s) th = preced th s" |
|
460 by (fold max_kept, unfold th_cp_max_preced, simp) |
|
461 |
|
462 lemma preced_less: |
|
463 assumes th'_in: "th' \<in> threads s" |
|
464 and neq_th': "th' \<noteq> th" |
|
465 shows "preced th' s < preced th s" |
|
466 using assms |
|
467 by (metis Max.coboundedI finite_imageI highest not_le order.trans |
|
468 preced_linorder rev_image_eqI threads_s vat_s.finite_threads |
|
469 vat_s.le_cp) |
|
470 |
|
471 section {* The `blocking thread` *} |
|
472 |
|
473 text {* |
|
474 The purpose of PIP is to ensure that the most |
|
475 urgent thread @{term th} is not blocked unreasonably. |
|
476 Therefore, a clear picture of the blocking thread is essential |
|
477 to assure people that the purpose is fulfilled. |
|
478 |
|
479 In this section, we are going to derive a series of lemmas |
|
480 with finally give rise to a picture of the blocking thread. |
|
481 |
|
482 By `blocking thread`, we mean a thread in running state but |
|
483 different from thread @{term th}. |
|
484 *} |
|
485 |
|
486 text {* |
|
487 The following lemmas shows that the @{term cp}-value |
|
488 of the blocking thread @{text th'} equals to the highest |
|
489 precedence in the whole system. |
|
490 *} |
|
491 lemma runing_preced_inversion: |
|
492 assumes runing': "th' \<in> runing (t@s)" |
|
493 shows "cp (t@s) th' = preced th s" (is "?L = ?R") |
|
494 proof - |
|
495 have "?L = Max (cp (t @ s) ` readys (t @ s))" using assms |
|
496 by (unfold runing_def, auto) |
|
497 also have "\<dots> = ?R" |
|
498 by (metis th_cp_max th_cp_preced vat_t.max_cp_readys_threads) |
|
499 finally show ?thesis . |
|
500 qed |
|
501 |
|
502 text {* |
|
503 The following lemma shows how the counting of |
|
504 @{term "P"} and @{term "V"} operations affects |
|
505 the running state of threads in @{term t}. |
|
506 |
|
507 The lemma shows that if a thread's @{term "P"}-count equals |
|
508 its @{term "V"}-count (which means it no longer has any |
|
509 resource in its possession), it can not be in running thread. |
|
510 |
|
511 The proof is by contraction with the assumption @{text "th' \<noteq> th"}. |
|
512 The key is the use of @{thm count_eq_dependants} |
|
513 to derive the emptiness of @{text th'}s @{term dependants}-set |
|
514 from the balance of its @{term P} @{term V} counts. |
|
515 From this, it can be shown @{text th'}s @{term cp}-value |
|
516 equals to its own precedence. |
|
517 |
|
518 On the other hand, since @{text th'} is running, by |
|
519 @{thm runing_preced_inversion}, its @{term cp}-value |
|
520 equals to the precedence of @{term th}. |
|
521 |
|
522 Combining the above two we have that @{text th'} and |
|
523 @{term th} have the same precedence. By uniqueness of precedence, we |
|
524 have @{text "th' = th"}, which is in contradiction with the |
|
525 assumption @{text "th' \<noteq> th"}. |
|
526 *} |
|
527 |
|
528 lemma eq_pv_blocked: (* ddd *) |
|
529 assumes neq_th': "th' \<noteq> th" |
|
530 and eq_pv: "cntP (t@s) th' = cntV (t@s) th'" |
|
531 shows "th' \<notin> runing (t@s)" |
|
532 proof |
|
533 assume otherwise: "th' \<in> runing (t@s)" |
|
534 show False |
|
535 proof - |
|
536 have th'_in: "th' \<in> threads (t@s)" |
|
537 using otherwise readys_threads runing_def by auto |
|
538 have "th' = th" |
|
539 proof(rule preced_unique) |
|
540 -- {* The proof goes like this: |
|
541 it is first shown that the @{term preced}-value of @{term th'} |
|
542 equals to that of @{term th}, then by uniqueness |
|
543 of @{term preced}-values (given by lemma @{thm preced_unique}), |
|
544 @{term th'} equals to @{term th}: *} |
|
545 show "preced th' (t @ s) = preced th (t @ s)" (is "?L = ?R") |
|
546 proof - |
|
547 -- {* Since the counts of @{term th'} are balanced, the subtree |
|
548 of it contains only itself, so, its @{term cp}-value |
|
549 equals its @{term preced}-value: *} |
|
550 have "?L = cp (t@s) th'" |
|
551 by (unfold cp_eq_cpreced cpreced_def count_eq_dependants[OF eq_pv], simp) |
|
552 -- {* Since @{term "th'"} is running, by @{thm runing_preced_inversion}, |
|
553 its @{term cp}-value equals @{term "preced th s"}, |
|
554 which equals to @{term "?R"} by simplification: *} |
|
555 also have "... = ?R" |
|
556 thm runing_preced_inversion |
|
557 using runing_preced_inversion[OF otherwise] by simp |
|
558 finally show ?thesis . |
|
559 qed |
|
560 qed (auto simp: th'_in th_kept) |
|
561 with `th' \<noteq> th` show ?thesis by simp |
|
562 qed |
|
563 qed |
|
564 |
|
565 text {* |
|
566 The following lemma is the extrapolation of @{thm eq_pv_blocked}. |
|
567 It says if a thread, different from @{term th}, |
|
568 does not hold any resource at the very beginning, |
|
569 it will keep hand-emptied in the future @{term "t@s"}. |
|
570 *} |
|
571 lemma eq_pv_persist: (* ddd *) |
|
572 assumes neq_th': "th' \<noteq> th" |
|
573 and eq_pv: "cntP s th' = cntV s th'" |
|
574 shows "cntP (t@s) th' = cntV (t@s) th'" |
|
575 proof(induction rule:ind) -- {* The proof goes by induction. *} |
|
576 -- {* The nontrivial case is for the @{term Cons}: *} |
|
577 case (Cons e t) |
|
578 -- {* All results derived so far hold for both @{term s} and @{term "t@s"}: *} |
|
579 interpret vat_t: extend_highest_gen s th prio tm t using Cons by simp |
|
580 interpret vat_e: extend_highest_gen s th prio tm "(e # t)" using Cons by simp |
|
581 show ?case |
|
582 proof - |
|
583 -- {* It can be proved that @{term cntP}-value of @{term th'} does not change |
|
584 by the happening of event @{term e}: *} |
|
585 have "cntP ((e#t)@s) th' = cntP (t@s) th'" |
|
586 proof(rule ccontr) -- {* Proof by contradiction. *} |
|
587 -- {* Suppose @{term cntP}-value of @{term th'} is changed by @{term e}: *} |
|
588 assume otherwise: "cntP ((e # t) @ s) th' \<noteq> cntP (t @ s) th'" |
|
589 -- {* Then the actor of @{term e} must be @{term th'} and @{term e} |
|
590 must be a @{term P}-event: *} |
|
591 hence "isP e" "actor e = th'" by (auto simp:cntP_diff_inv) |
|
592 with vat_t.actor_inv[OF Cons(2)] |
|
593 -- {* According to @{thm actor_inv}, @{term th'} must be running at |
|
594 the moment @{term "t@s"}: *} |
|
595 have "th' \<in> runing (t@s)" by (cases e, auto) |
|
596 -- {* However, an application of @{thm eq_pv_blocked} to induction hypothesis |
|
597 shows @{term th'} can not be running at moment @{term "t@s"}: *} |
|
598 moreover have "th' \<notin> runing (t@s)" |
|
599 using vat_t.eq_pv_blocked[OF neq_th' Cons(5)] . |
|
600 -- {* Contradiction is finally derived: *} |
|
601 ultimately show False by simp |
|
602 qed |
|
603 -- {* It can also be proved that @{term cntV}-value of @{term th'} does not change |
|
604 by the happening of event @{term e}: *} |
|
605 -- {* The proof follows exactly the same pattern as the case for @{term cntP}-value: *} |
|
606 moreover have "cntV ((e#t)@s) th' = cntV (t@s) th'" |
|
607 proof(rule ccontr) -- {* Proof by contradiction. *} |
|
608 assume otherwise: "cntV ((e # t) @ s) th' \<noteq> cntV (t @ s) th'" |
|
609 hence "isV e" "actor e = th'" by (auto simp:cntV_diff_inv) |
|
610 with vat_t.actor_inv[OF Cons(2)] |
|
611 have "th' \<in> runing (t@s)" by (cases e, auto) |
|
612 moreover have "th' \<notin> runing (t@s)" |
|
613 using vat_t.eq_pv_blocked[OF neq_th' Cons(5)] . |
|
614 ultimately show False by simp |
|
615 qed |
|
616 -- {* Finally, it can be shown that the @{term cntP} and @{term cntV} |
|
617 value for @{term th'} are still in balance, so @{term th'} |
|
618 is still hand-emptied after the execution of event @{term e}: *} |
|
619 ultimately show ?thesis using Cons(5) by metis |
|
620 qed |
|
621 qed (auto simp:eq_pv) |
|
622 |
|
623 text {* |
|
624 By combining @{thm eq_pv_blocked} and @{thm eq_pv_persist}, |
|
625 it can be derived easily that @{term th'} can not be running in the future: |
|
626 *} |
|
627 lemma eq_pv_blocked_persist: |
|
628 assumes neq_th': "th' \<noteq> th" |
|
629 and eq_pv: "cntP s th' = cntV s th'" |
|
630 shows "th' \<notin> runing (t@s)" |
|
631 using assms |
|
632 by (simp add: eq_pv_blocked eq_pv_persist) |
|
633 |
|
634 text {* |
|
635 The following lemma shows the blocking thread @{term th'} |
|
636 must hold some resource in the very beginning. |
|
637 *} |
|
638 lemma runing_cntP_cntV_inv: (* ddd *) |
|
639 assumes is_runing: "th' \<in> runing (t@s)" |
|
640 and neq_th': "th' \<noteq> th" |
|
641 shows "cntP s th' > cntV s th'" |
|
642 using assms |
|
643 proof - |
|
644 -- {* First, it can be shown that the number of @{term P} and |
|
645 @{term V} operations can not be equal for thred @{term th'} *} |
|
646 have "cntP s th' \<noteq> cntV s th'" |
|
647 proof |
|
648 -- {* The proof goes by contradiction, suppose otherwise: *} |
|
649 assume otherwise: "cntP s th' = cntV s th'" |
|
650 -- {* By applying @{thm eq_pv_blocked_persist} to this: *} |
|
651 from eq_pv_blocked_persist[OF neq_th' otherwise] |
|
652 -- {* we have that @{term th'} can not be running at moment @{term "t@s"}: *} |
|
653 have "th' \<notin> runing (t@s)" . |
|
654 -- {* This is obvious in contradiction with assumption @{thm is_runing} *} |
|
655 thus False using is_runing by simp |
|
656 qed |
|
657 -- {* However, the number of @{term V} is always less or equal to @{term P}: *} |
|
658 moreover have "cntV s th' \<le> cntP s th'" using vat_s.cnp_cnv_cncs by auto |
|
659 -- {* Thesis is finally derived by combining the these two results: *} |
|
660 ultimately show ?thesis by auto |
|
661 qed |
|
662 |
|
663 |
|
664 text {* |
|
665 The following lemmas shows the blocking thread @{text th'} must be live |
|
666 at the very beginning, i.e. the moment (or state) @{term s}. |
|
667 |
|
668 The proof is a simple combination of the results above: |
|
669 *} |
|
670 lemma runing_threads_inv: |
|
671 assumes runing': "th' \<in> runing (t@s)" |
|
672 and neq_th': "th' \<noteq> th" |
|
673 shows "th' \<in> threads s" |
|
674 proof(rule ccontr) -- {* Proof by contradiction: *} |
|
675 assume otherwise: "th' \<notin> threads s" |
|
676 have "th' \<notin> runing (t @ s)" |
|
677 proof - |
|
678 from vat_s.cnp_cnv_eq[OF otherwise] |
|
679 have "cntP s th' = cntV s th'" . |
|
680 from eq_pv_blocked_persist[OF neq_th' this] |
|
681 show ?thesis . |
|
682 qed |
|
683 with runing' show False by simp |
|
684 qed |
|
685 |
|
686 text {* |
|
687 The following lemma summarizes several foregoing |
|
688 lemmas to give an overall picture of the blocking thread @{text "th'"}: |
|
689 *} |
|
690 lemma runing_inversion: (* ddd, one of the main lemmas to present *) |
|
691 assumes runing': "th' \<in> runing (t@s)" |
|
692 and neq_th: "th' \<noteq> th" |
|
693 shows "th' \<in> threads s" |
|
694 and "\<not>detached s th'" |
|
695 and "cp (t@s) th' = preced th s" |
|
696 proof - |
|
697 from runing_threads_inv[OF assms] |
|
698 show "th' \<in> threads s" . |
|
699 next |
|
700 from runing_cntP_cntV_inv[OF runing' neq_th] |
|
701 show "\<not>detached s th'" using vat_s.detached_eq by simp |
|
702 next |
|
703 from runing_preced_inversion[OF runing'] |
|
704 show "cp (t@s) th' = preced th s" . |
|
705 qed |
|
706 |
|
707 section {* The existence of `blocking thread` *} |
|
708 |
|
709 text {* |
|
710 Suppose @{term th} is not running, it is first shown that |
|
711 there is a path in RAG leading from node @{term th} to another thread @{text "th'"} |
|
712 in the @{term readys}-set (So @{text "th'"} is an ancestor of @{term th}}). |
|
713 |
|
714 Now, since @{term readys}-set is non-empty, there must be |
|
715 one in it which holds the highest @{term cp}-value, which, by definition, |
|
716 is the @{term runing}-thread. However, we are going to show more: this running thread |
|
717 is exactly @{term "th'"}. |
|
718 *} |
|
719 lemma th_blockedE: (* ddd, the other main lemma to be presented: *) |
|
720 assumes "th \<notin> runing (t@s)" |
|
721 obtains th' where "Th th' \<in> ancestors (RAG (t @ s)) (Th th)" |
|
722 "th' \<in> runing (t@s)" |
|
723 proof - |
|
724 -- {* According to @{thm vat_t.th_chain_to_ready}, either |
|
725 @{term "th"} is in @{term "readys"} or there is path leading from it to |
|
726 one thread in @{term "readys"}. *} |
|
727 have "th \<in> readys (t @ s) \<or> (\<exists>th'. th' \<in> readys (t @ s) \<and> (Th th, Th th') \<in> (RAG (t @ s))\<^sup>+)" |
|
728 using th_kept vat_t.th_chain_to_ready by auto |
|
729 -- {* However, @{term th} can not be in @{term readys}, because otherwise, since |
|
730 @{term th} holds the highest @{term cp}-value, it must be @{term "runing"}. *} |
|
731 moreover have "th \<notin> readys (t@s)" |
|
732 using assms runing_def th_cp_max vat_t.max_cp_readys_threads by auto |
|
733 -- {* So, there must be a path from @{term th} to another thread @{text "th'"} in |
|
734 term @{term readys}: *} |
|
735 ultimately obtain th' where th'_in: "th' \<in> readys (t@s)" |
|
736 and dp: "(Th th, Th th') \<in> (RAG (t @ s))\<^sup>+" by auto |
|
737 -- {* We are going to show that this @{term th'} is running. *} |
|
738 have "th' \<in> runing (t@s)" |
|
739 proof - |
|
740 -- {* We only need to show that this @{term th'} holds the highest @{term cp}-value: *} |
|
741 have "cp (t@s) th' = Max (cp (t@s) ` readys (t@s))" (is "?L = ?R") |
|
742 proof - |
|
743 have "?L = Max ((the_preced (t @ s) \<circ> the_thread) ` subtree (tRAG (t @ s)) (Th th'))" |
|
744 by (unfold cp_alt_def1, simp) |
|
745 also have "... = (the_preced (t @ s) \<circ> the_thread) (Th th)" |
|
746 proof(rule image_Max_subset) |
|
747 show "finite (Th ` (threads (t@s)))" by (simp add: vat_t.finite_threads) |
|
748 next |
|
749 show "subtree (tRAG (t @ s)) (Th th') \<subseteq> Th ` threads (t @ s)" |
|
750 by (metis Range.intros dp trancl_range vat_t.range_in vat_t.subtree_tRAG_thread) |
|
751 next |
|
752 show "Th th \<in> subtree (tRAG (t @ s)) (Th th')" using dp |
|
753 by (unfold tRAG_subtree_eq, auto simp:subtree_def) |
|
754 next |
|
755 show "Max ((the_preced (t @ s) \<circ> the_thread) ` Th ` threads (t @ s)) = |
|
756 (the_preced (t @ s) \<circ> the_thread) (Th th)" (is "Max ?L = _") |
|
757 proof - |
|
758 have "?L = the_preced (t @ s) ` threads (t @ s)" |
|
759 by (unfold image_comp, rule image_cong, auto) |
|
760 thus ?thesis using max_preced the_preced_def by auto |
|
761 qed |
|
762 qed |
|
763 also have "... = ?R" |
|
764 using th_cp_max th_cp_preced th_kept |
|
765 the_preced_def vat_t.max_cp_readys_threads by auto |
|
766 finally show ?thesis . |
|
767 qed |
|
768 -- {* Now, since @{term th'} holds the highest @{term cp} |
|
769 and we have already show it is in @{term readys}, |
|
770 it is @{term runing} by definition. *} |
|
771 with `th' \<in> readys (t@s)` show ?thesis by (simp add: runing_def) |
|
772 qed |
|
773 -- {* It is easy to show @{term th'} is an ancestor of @{term th}: *} |
|
774 moreover have "Th th' \<in> ancestors (RAG (t @ s)) (Th th)" |
|
775 using `(Th th, Th th') \<in> (RAG (t @ s))\<^sup>+` by (auto simp:ancestors_def) |
|
776 ultimately show ?thesis using that by metis |
|
777 qed |
|
778 |
|
779 text {* |
|
780 Now it is easy to see there is always a thread to run by case analysis |
|
781 on whether thread @{term th} is running: if the answer is Yes, the |
|
782 the running thread is obviously @{term th} itself; otherwise, the running |
|
783 thread is the @{text th'} given by lemma @{thm th_blockedE}. |
|
784 *} |
|
785 lemma live: "runing (t@s) \<noteq> {}" |
|
786 proof(cases "th \<in> runing (t@s)") |
|
787 case True thus ?thesis by auto |
|
788 next |
|
789 case False |
|
790 thus ?thesis using th_blockedE by auto |
|
791 qed |
|
792 |
|
793 |
|
794 end |
|
795 end |
|