261 lemma child_unique: |
256 lemma child_unique: |
262 assumes vt: "vt s" |
257 assumes vt: "vt s" |
263 and ch1: "(Th th, Th th1) \<in> child s" |
258 and ch1: "(Th th, Th th1) \<in> child s" |
264 and ch2: "(Th th, Th th2) \<in> child s" |
259 and ch2: "(Th th, Th th2) \<in> child s" |
265 shows "th1 = th2" |
260 shows "th1 = th2" |
266 proof - |
261 using ch1 ch2 |
267 from ch1 ch2 show ?thesis |
262 proof(unfold child_def, clarsimp) |
268 proof(unfold child_def, clarsimp) |
263 fix cs csa |
269 fix cs csa |
264 assume h1: "(Th th, Cs cs) \<in> depend s" |
270 assume h1: "(Th th, Cs cs) \<in> depend s" |
265 and h2: "(Cs cs, Th th1) \<in> depend s" |
271 and h2: "(Cs cs, Th th1) \<in> depend s" |
266 and h3: "(Th th, Cs csa) \<in> depend s" |
272 and h3: "(Th th, Cs csa) \<in> depend s" |
267 and h4: "(Cs csa, Th th2) \<in> depend s" |
273 and h4: "(Cs csa, Th th2) \<in> depend s" |
268 from unique_depend[OF vt h1 h3] have "cs = csa" by simp |
274 from unique_depend[OF vt h1 h3] have "cs = csa" by simp |
269 with h4 have "(Cs cs, Th th2) \<in> depend s" by simp |
275 with h4 have "(Cs cs, Th th2) \<in> depend s" by simp |
270 from unique_depend[OF vt h2 this] |
276 from unique_depend[OF vt h2 this] |
271 show "th1 = th2" by simp |
277 show "th1 = th2" by simp |
272 qed |
278 qed |
|
279 qed |
|
280 |
|
281 |
|
282 lemma cp_eq_cpreced_f: "cp s = cpreced (wq s) s" |
|
283 proof - |
|
284 from fun_eq_iff |
|
285 have h:"\<And>f g. (\<forall> x. f x = g x) \<Longrightarrow> f = g" by auto |
|
286 show ?thesis |
|
287 proof(rule h) |
|
288 from cp_eq_cpreced show "\<forall>x. cp s x = cpreced (wq s) s x" by auto |
|
289 qed |
|
290 qed |
|
291 |
273 |
292 lemma depend_children: |
274 lemma depend_children: |
293 assumes h: "(Th th1, Th th2) \<in> (depend s)^+" |
275 assumes h: "(Th th1, Th th2) \<in> (depend s)^+" |
294 shows "th1 \<in> children s th2 \<or> (\<exists> th3. th3 \<in> children s th2 \<and> (Th th1, Th th3) \<in> (depend s)^+)" |
276 shows "th1 \<in> children s th2 \<or> (\<exists> th3. th3 \<in> children s th2 \<and> (Th th1, Th th3) \<in> (depend s)^+)" |
295 proof - |
277 proof - |
471 from children_no_dep[OF vt ch2 ch1 this] show ?thesis . |
448 from children_no_dep[OF vt ch2 ch1 this] show ?thesis . |
472 qed |
449 qed |
473 } thus ?thesis by auto |
450 } thus ?thesis by auto |
474 qed |
451 qed |
475 |
452 |
|
453 lemma depend_plus_elim: |
|
454 assumes "vt s" |
|
455 fixes x |
|
456 assumes "(Th x, Th th) \<in> (depend (wq s))\<^sup>+" |
|
457 shows "\<exists>th'\<in>children s th. x = th' \<or> (Th x, Th th') \<in> (depend (wq s))\<^sup>+" |
|
458 using assms(2)[unfolded eq_depend, folded child_depend_eq[OF `vt s`]] |
|
459 apply (unfold children_def) |
|
460 by (metis assms(2) children_def depend_children eq_depend) |
|
461 |
|
462 lemma dependants_expand_pre: |
|
463 assumes "vt s" |
|
464 shows "dependants (wq s) th = (\<Union> th' \<in> children s th. {th'} \<union> dependants (wq s) th')" |
|
465 apply (unfold cs_dependants_def) |
|
466 apply (auto elim!:depend_plus_elim[OF assms]) |
|
467 apply (metis children_def eq_depend mem_Collect_eq set_mp sub_child) |
|
468 apply (unfold children_def, auto) |
|
469 apply (unfold eq_depend, fold child_depend_eq[OF `vt s`]) |
|
470 by (metis trancl.simps) |
|
471 |
|
472 lemma dependants_expand: |
|
473 assumes "vt s" |
|
474 shows "dependants (wq s) th = (\<Union> ((\<lambda> th. {th} \<union> dependants (wq s) th) ` (children s th)))" |
|
475 apply (subst dependants_expand_pre[OF assms]) |
|
476 by simp |
|
477 |
|
478 lemma finite_children: |
|
479 assumes "vt s" |
|
480 shows "finite (children s th)" |
|
481 using children_dependants dependants_threads[OF assms] finite_threads[OF assms] |
|
482 by (metis rev_finite_subset) |
|
483 |
|
484 lemma finite_dependants: |
|
485 assumes "vt s" |
|
486 shows "finite (dependants (wq s) th')" |
|
487 using dependants_threads[OF assms] finite_threads[OF assms] |
|
488 by (metis rev_finite_subset) |
|
489 |
|
490 lemma Max_insert: |
|
491 assumes "finite B" |
|
492 and "B \<noteq> {}" |
|
493 shows "Max ({x} \<union> B) = max x (Max B)" |
|
494 by (metis Max_insert assms insert_is_Un) |
|
495 |
|
496 lemma dependands_expand2: |
|
497 assumes "vt s" |
|
498 shows "dependants (wq s) th = (children s th) \<union> (\<Union>((dependants (wq s)) ` children s th))" |
|
499 by (subst dependants_expand[OF assms]) (auto) |
|
500 |
|
501 abbreviation |
|
502 "preceds s Ths \<equiv> {preced th s| th. th \<in> Ths}" |
|
503 |
|
504 lemma image_compr: |
|
505 "f ` A = {f x | x. x \<in> A}" |
|
506 by auto |
|
507 |
|
508 lemma Un_compr: |
|
509 "{f th | th. R th \<or> Q th} = ({f th | th. R th} \<union> {f th' | th'. Q th'})" |
|
510 by auto |
|
511 |
|
512 lemma in_disj: |
|
513 shows "x \<in> A \<or> (\<exists>y \<in> A. x \<in> Q y) \<longleftrightarrow> (\<exists>y \<in> A. x = y \<or> x \<in> Q y)" |
|
514 by metis |
|
515 |
|
516 lemma UN_exists: |
|
517 shows "(\<Union>x \<in> A. {f y | y. Q y x}) = ({f y | y. (\<exists>x \<in> A. Q y x)})" |
|
518 by auto |
|
519 |
476 lemma cp_rec: |
520 lemma cp_rec: |
477 fixes s th |
521 fixes s th |
478 assumes vt: "vt s" |
522 assumes vt: "vt s" |
479 shows "cp s th = Max ({preced th s} \<union> (cp s ` children s th))" |
523 shows "cp s th = Max ({preced th s} \<union> (cp s ` children s th))" |
480 proof(unfold cp_eq_cpreced_f cpreced_def) |
524 proof(cases "children s th = {}") |
481 let ?f = "(\<lambda>th. preced th s)" |
525 case True |
482 show "Max ((\<lambda>th. preced th s) ` ({th} \<union> dependants (wq s) th)) = |
526 show ?thesis |
483 Max ({preced th s} \<union> (\<lambda>th. Max ((\<lambda>th. preced th s) ` ({th} \<union> dependants (wq s) th))) ` children s th)" |
527 unfolding cp_eq_cpreced cpreced_def |
484 proof(cases " children s th = {}") |
528 by (subst dependants_expand[OF `vt s`]) (simp add: True) |
485 case False |
529 next |
486 have "(\<lambda>th. Max ((\<lambda>th. preced th s) ` ({th} \<union> dependants (wq s) th))) ` children s th = |
530 case False |
487 {Max ((\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')) | th' . th' \<in> children s th}" |
531 show ?thesis (is "?LHS = ?RHS") |
488 (is "?L = ?R") |
532 proof - |
489 by auto |
533 have eq_cp: "cp s = (\<lambda>th. Max (preceds s ({th} \<union> dependants (wq s) th)))" |
490 also have "\<dots> = |
534 by (simp add: fun_eq_iff cp_eq_cpreced cpreced_def Un_compr image_compr[symmetric]) |
491 Max ` {((\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th')) | th' . th' \<in> children s th}" |
535 |
492 (is "_ = Max ` ?C") |
536 have not_emptyness_facts[simp]: |
493 by auto |
537 "dependants (wq s) th \<noteq> {}" "children s th \<noteq> {}" |
494 finally have "Max ?L = Max (Max ` ?C)" by auto |
538 using False dependands_expand2[OF assms] by(auto simp only: Un_empty) |
495 also have "\<dots> = Max (\<Union> ?C)" |
539 |
496 proof(rule Max_Union[symmetric]) |
540 have finiteness_facts[simp]: |
497 from children_dependants[of s th] finite_threads[OF vt] and dependants_threads[OF vt, of th] |
541 "\<And>th. finite (dependants (wq s) th)" "\<And>th. finite (children s th)" |
498 show "finite {(\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th') |th'. th' \<in> children s th}" |
542 by (simp_all add: finite_dependants[OF `vt s`] finite_children[OF `vt s`]) |
499 by (auto simp:finite_subset) |
543 |
500 next |
544 (* expanding definition *) |
501 from False |
545 have "?LHS = Max ({preced th s} \<union> preceds s (dependants (wq s) th))" |
502 show "{(\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th') |th'. th' \<in> children s th} \<noteq> {}" |
546 unfolding eq_cp by (simp add: Un_compr) |
503 by simp |
547 |
504 next |
548 (* moving Max in *) |
505 show "\<And>A. A \<in> {(\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th') |th'. th' \<in> children s th} \<Longrightarrow> |
549 also have "\<dots> = max (Max {preced th s}) (Max (preceds s (dependants (wq s) th)))" |
506 finite A \<and> A \<noteq> {}" |
550 by (simp add: Max_Un) |
507 apply (auto simp:finite_subset) |
551 |
508 proof - |
552 (* expanding dependants *) |
509 fix th' |
553 also have "\<dots> = max (Max {preced th s}) |
510 from finite_threads[OF vt] and dependants_threads[OF vt, of th'] |
554 (Max (preceds s (children s th \<union> \<Union>(dependants (wq s) ` children s th))))" |
511 show "finite ((\<lambda>th. preced th s) ` dependants (wq s) th')" by (auto simp:finite_subset) |
555 by (subst dependands_expand2[OF `vt s`]) (simp) |
512 qed |
556 |
513 qed |
557 (* moving out big Union *) |
514 also have "\<dots> = Max ((\<lambda>th. preced th s) ` dependants (wq s) th)" |
558 also have "\<dots> = max (Max {preced th s}) |
515 (is "Max ?A = Max ?B") |
559 (Max (preceds s (\<Union> ({children s th} \<union> (dependants (wq s) ` children s th)))))" |
516 proof - |
560 by simp |
517 have "?A = ?B" |
561 |
518 proof |
562 (* moving in small union *) |
519 show "\<Union>{(\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th') |th'. th' \<in> children s th} |
563 also have "\<dots> = max (Max {preced th s}) |
520 \<subseteq> (\<lambda>th. preced th s) ` dependants (wq s) th" |
564 (Max (preceds s (\<Union> ((\<lambda>th. {th} \<union> (dependants (wq s) th)) ` children s th))))" |
521 proof |
565 by (simp add: in_disj) |
522 fix x |
566 |
523 assume "x \<in> \<Union>{(\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th') |th'. th' \<in> children s th}" |
567 (* moving in preceds *) |
524 then obtain th' where |
568 also have "\<dots> = max (Max {preced th s}) |
525 th'_in: "th' \<in> children s th" |
569 (Max (\<Union> ((\<lambda>th. preceds s ({th} \<union> (dependants (wq s) th))) ` children s th)))" |
526 and x_in: "x \<in> ?f ` ({th'} \<union> dependants (wq s) th')" by auto |
570 by (simp add: UN_exists) |
527 hence "x = ?f th' \<or> x \<in> (?f ` dependants (wq s) th')" by auto |
571 |
528 thus "x \<in> ?f ` dependants (wq s) th" |
572 (* moving in Max *) |
529 proof |
573 also have "\<dots> = max (Max {preced th s}) |
530 assume "x = preced th' s" |
574 (Max ((\<lambda>th. Max (preceds s ({th} \<union> (dependants (wq s) th)))) ` children s th))" |
531 with th'_in and children_dependants |
575 by (subst Max_Union) (auto simp add: image_image) |
532 show "x \<in> (\<lambda>th. preced th s) ` dependants (wq s) th" by auto |
576 |
533 next |
577 (* folding cp + moving out Max *) |
534 assume "x \<in> (\<lambda>th. preced th s) ` dependants (wq s) th'" |
578 also have "\<dots> = ?RHS" |
535 moreover note th'_in |
579 unfolding eq_cp by (simp add: Max_insert) |
536 ultimately show " x \<in> (\<lambda>th. preced th s) ` dependants (wq s) th" |
580 |
537 by (unfold cs_dependants_def children_def child_def, auto simp:eq_depend) |
581 finally show "?LHS = ?RHS" . |
538 qed |
|
539 qed |
|
540 next |
|
541 show "?f ` dependants (wq s) th |
|
542 \<subseteq> \<Union>{?f ` ({th'} \<union> dependants (wq s) th') |th'. th' \<in> children s th}" |
|
543 proof |
|
544 fix x |
|
545 assume x_in: "x \<in> (\<lambda>th. preced th s) ` dependants (wq s) th" |
|
546 then obtain th' where |
|
547 eq_x: "x = ?f th'" and dp: "(Th th', Th th) \<in> (depend s)^+" |
|
548 by (auto simp:cs_dependants_def eq_depend) |
|
549 from depend_children[OF dp] |
|
550 have "th' \<in> children s th \<or> (\<exists>th3. th3 \<in> children s th \<and> (Th th', Th th3) \<in> (depend s)\<^sup>+)" . |
|
551 thus "x \<in> \<Union>{(\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th') |th'. th' \<in> children s th}" |
|
552 proof |
|
553 assume "th' \<in> children s th" |
|
554 with eq_x |
|
555 show "x \<in> \<Union>{(\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th') |th'. th' \<in> children s th}" |
|
556 by auto |
|
557 next |
|
558 assume "\<exists>th3. th3 \<in> children s th \<and> (Th th', Th th3) \<in> (depend s)\<^sup>+" |
|
559 then obtain th3 where th3_in: "th3 \<in> children s th" |
|
560 and dp3: "(Th th', Th th3) \<in> (depend s)\<^sup>+" by auto |
|
561 show "x \<in> \<Union>{(\<lambda>th. preced th s) ` ({th'} \<union> dependants (wq s) th') |th'. th' \<in> children s th}" |
|
562 proof - |
|
563 from dp3 |
|
564 have "th' \<in> dependants (wq s) th3" |
|
565 by (auto simp:cs_dependants_def eq_depend) |
|
566 with eq_x th3_in show ?thesis by auto |
|
567 qed |
|
568 qed |
|
569 qed |
|
570 qed |
|
571 thus ?thesis by simp |
|
572 qed |
|
573 finally have "Max ((\<lambda>th. preced th s) ` dependants (wq s) th) = Max (?L)" |
|
574 (is "?X = ?Y") by auto |
|
575 moreover have "Max ((\<lambda>th. preced th s) ` ({th} \<union> dependants (wq s) th)) = |
|
576 max (?f th) ?X" |
|
577 proof - |
|
578 have "Max ((\<lambda>th. preced th s) ` ({th} \<union> dependants (wq s) th)) = |
|
579 Max ({?f th} \<union> ?f ` (dependants (wq s) th))" by simp |
|
580 also have "\<dots> = max (Max {?f th}) (Max (?f ` (dependants (wq s) th)))" |
|
581 proof(rule Max_Un, auto) |
|
582 from finite_threads[OF vt] and dependants_threads[OF vt, of th] |
|
583 show "finite ((\<lambda>th. preced th s) ` dependants (wq s) th)" by (auto simp:finite_subset) |
|
584 next |
|
585 assume "dependants (wq s) th = {}" |
|
586 with False and children_dependants show False by auto |
|
587 qed |
|
588 also have "\<dots> = max (?f th) ?X" by simp |
|
589 finally show ?thesis . |
|
590 qed |
|
591 moreover have "Max ({preced th s} \<union> |
|
592 (\<lambda>th. Max ((\<lambda>th. preced th s) ` ({th} \<union> dependants (wq s) th))) ` children s th) = |
|
593 max (?f th) ?Y" |
|
594 proof - |
|
595 have "Max ({preced th s} \<union> |
|
596 (\<lambda>th. Max ((\<lambda>th. preced th s) ` ({th} \<union> dependants (wq s) th))) ` children s th) = |
|
597 max (Max {preced th s}) ?Y" |
|
598 proof(rule Max_Un, auto) |
|
599 from finite_threads[OF vt] dependants_threads[OF vt, of th] children_dependants [of s th] |
|
600 show "finite ((\<lambda>th. Max (insert (preced th s) ((\<lambda>th. preced th s) ` dependants (wq s) th))) ` |
|
601 children s th)" |
|
602 by (auto simp:finite_subset) |
|
603 next |
|
604 assume "children s th = {}" |
|
605 with False show False by auto |
|
606 qed |
|
607 thus ?thesis by simp |
|
608 qed |
|
609 ultimately show ?thesis by auto |
|
610 next |
|
611 case True |
|
612 moreover have "dependants (wq s) th = {}" |
|
613 proof - |
|
614 { fix th' |
|
615 assume "th' \<in> dependants (wq s) th" |
|
616 hence " (Th th', Th th) \<in> (depend s)\<^sup>+" by (simp add:cs_dependants_def eq_depend) |
|
617 from depend_children[OF this] and True |
|
618 have "False" by auto |
|
619 } thus ?thesis by auto |
|
620 qed |
|
621 ultimately show ?thesis by auto |
|
622 qed |
582 qed |
623 qed |
583 qed |
624 |
584 |
625 definition cps:: "state \<Rightarrow> (thread \<times> precedence) set" |
585 definition cps:: "state \<Rightarrow> (thread \<times> precedence) set" |
626 where "cps s = {(th, cp s th) | th . th \<in> threads s}" |
586 where "cps s = {(th, cp s th) | th . th \<in> threads s}" |