-
Notifications
You must be signed in to change notification settings - Fork 2
/
SafeJoins.v
1114 lines (1005 loc) · 25.4 KB
/
SafeJoins.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Set Implicit Arguments.
Require Import Coq.Lists.List.
Require Import Aniceto.List.
Require Import Aniceto.Graphs.Graph.
Require Import Aniceto.Graphs.FGraph.
Require Import Tid.
Require Import Aniceto.Graphs.FGraph.
(**
In our analysis we only care about fork and join operations; everything else
is ignored.
*)
Inductive op_type := FORK | JOIN.
Structure op := {
op_t: op_type;
op_src: tid;
op_dst: tid
}.
Module SJ_Notations.
(**
We define a simple notation to wrap up the structure and make it less
verbose.
*)
Notation F x y := ({| op_t := FORK; op_src:=x; op_dst := y |}).
Notation J x y := ({| op_t := JOIN; op_src:=x; op_dst := y |}).
End SJ_Notations.
Section Defs.
(** The type of a trace of forks and joins. *)
Definition trace := list op.
(** A known-graph defined as a list of edges [(x,y)] means that x knows y. *)
Notation known := (list (tid*tid)).
(** Returns the outgoing node in an edge if the edge departs from [t].
Expression [outgoing t e] returns [Some y] if [e = (x, y)],
otherwise it returns [None]. *)
Definition outgoing_of t (e:tid*tid) := let (x,y) := e in if tid_eq_dec x t then Some y else None.
(** Given a known-graph return all the tasks [x] knows. *)
Definition filter_outgoing x := List.omap (outgoing_of x).
(** Copies the knowledge of [x] to [y], such that [y] knows every task
that [x] knows. *)
Definition copy_from (x y:tid) k := map (fun z => (y, z)) (filter_outgoing x k).
(** When [x] forks [y], the [y] copies the knowledge of [x]. *)
Definition fork (x y:tid) (k:known) : known :=
copy_from x y k ++ (x,y) :: k.
(** When [x] joins with [y], [x] copies the knowledge of [y]. *)
Definition join (x y:tid) (k:known) : known :=
copy_from y x k ++ k.
(** Given a fork/join extend the knowledge-graph. *)
Definition eval (o:op) :=
let f := match op_t o with
| FORK => fork
| JOIN => join
end
in
f (op_src o) (op_dst o).
Let fork_incl:
forall x y k,
incl k (fork x y k).
Proof.
intros.
unfold fork.
auto using incl_refl, incl_appr, incl_tl.
Qed.
(** The knowledge graph [k] is a sub-graph of applying a fork to [k]. *)
Lemma in_fork:
forall e x y k,
List.In e k ->
List.In e (fork x y k).
Proof.
intros.
assert (X:incl k (fork x y k)) by eauto using fork_incl.
unfold incl in *; auto.
Qed.
Let join_incl:
forall x y k,
incl k (join x y k).
Proof.
intros.
unfold join.
auto using incl_refl, incl_appr.
Qed.
(** Similarly, the knowledge graph [k] is a sub-graph of applying a join to [k]. *)
Lemma in_join:
forall e x y k,
List.In e k ->
List.In e (join x y k).
Proof.
intros.
assert (incl k (join x y k)) by eauto using join_incl.
unfold incl in *; auto.
Qed.
(** The edges of a knowledge graph grow monotonically with evaluation. *)
Lemma eval_incl:
forall k o,
incl k (eval o k).
Proof.
intros.
unfold eval.
destruct (op_t o); auto using join_incl, fork_incl.
Qed.
Lemma in_eval:
forall e k o,
List.In e k ->
List.In e (eval o k).
Proof.
intros.
assert (Hi: incl k (eval o k)) by auto using eval_incl.
auto.
Qed.
(** Checks if the operation is well-formed given a certain knowledge-graph. *)
Inductive CanCheckOp (k:known) : op -> Prop :=
| can_check_fork:
forall (x y:tid),
x <> y ->
~ Graph.In (Edge k) y ->
CanCheckOp k {| op_t := FORK; op_src:= x; op_dst:= y|}
| can_check_join:
forall x y,
Edge k (x,y) ->
CanCheckOp k {| op_t := JOIN; op_src := x; op_dst:= y|}.
(** Checking well-formedness is decidable. *)
Lemma can_check_op_dec k o:
{ CanCheckOp k o } + { ~ CanCheckOp k o }.
Proof.
destruct o as (t, x, y).
destruct t.
- destruct (in_edge_dec tid_eq_dec y k). {
right.
unfold not; intros Hx.
inversion Hx; subst; simpl in *; clear Hx.
contradiction.
}
destruct (tid_eq_dec x y). {
right.
unfold not; intros Hx.
inversion Hx; subst; simpl in *; clear Hx.
contradiction H1; trivial.
}
auto using can_check_fork.
- destruct (is_edge_dec tid_eq_dec k (x,y)). {
left; auto using can_check_join.
}
right.
unfold not; intros.
inversion H; subst.
contradiction.
Defined.
(** Checks if a reduction step is is well formed. *)
Inductive CheckOp k : op -> known -> Prop :=
| check_fork:
forall (x y:tid),
x <> y ->
~ Graph.In (Edge k) y ->
CheckOp k {| op_t := FORK; op_src:= x; op_dst:= y|} (fork x y k)
| check_join:
forall x y,
Edge k (x,y) ->
CheckOp k {| op_t := JOIN; op_src := x; op_dst:= y|} (join x y k).
(** Checks if a trace is well formed. *)
Inductive Safe : trace -> known -> Prop :=
| safe_nil:
Safe nil nil
| safe_cons:
forall o l k,
CanCheckOp k o ->
Safe l k ->
Safe (o::l) (eval o k).
(** Decidable function that computes if a trace is well formed. *)
Fixpoint is_safe t :=
match t with
| nil => Some nil
| o :: t =>
match is_safe t with
| Some k =>
if can_check_op_dec k o
then Some (eval o k)
else None
| _ => None
end
end.
Lemma can_check_op_to_check_op:
forall o k,
CanCheckOp k o ->
CheckOp k o (eval o k).
Proof.
intros.
inversion H; subst; clear H; subst; unfold eval; simpl in *;
auto using check_fork, check_join.
Qed.
Lemma check_op_to_can_check_op:
forall o k,
CheckOp k o (eval o k) ->
CanCheckOp k o.
Proof.
intros.
inversion H; subst; clear H; subst; unfold eval; simpl in *;
auto using can_check_fork, can_check_join.
Qed.
(** Correctness proof of the predicate [is_safe] and the well-formedness
proposition [Safe], part I. *)
Lemma is_safe_some:
forall t k,
is_safe t = Some k ->
Safe t k.
Proof.
induction t; intros; simpl in *.
- inversion H; subst; auto using safe_nil.
- remember (is_safe t).
symmetry in Heqo.
destruct o. {
destruct (can_check_op_dec l a). {
inversion H; subst.
auto using safe_cons.
}
inversion H.
}
inversion H.
Qed.
(** Correctness proof of the predicate [is_safe] and the well-formedness
proposition [Safe], part II. *)
Lemma safe_to_is_safe:
forall t k,
Safe t k ->
is_safe t = Some k.
Proof.
induction t; intros. {
inversion H.
subst.
auto.
}
inversion H;subst;clear H.
apply IHt in H4.
simpl.
rewrite H4.
destruct (can_check_op_dec k0 a). {
trivial.
}
contradiction.
Qed.
Lemma safe_fun:
forall t k k',
Safe t k ->
Safe t k' ->
k' = k.
Proof.
intros.
apply safe_to_is_safe in H.
apply safe_to_is_safe in H0.
rewrite H in H0.
inversion H0; trivial.
Qed.
Lemma is_safe_none:
forall t k,
is_safe t = None ->
~ Safe t k.
Proof.
intros.
unfold not; intros.
apply safe_to_is_safe in H0.
rewrite H in H0.
inversion H0.
Qed.
Lemma safe_reduces:
forall o t k k',
Safe t k ->
CheckOp k o k' ->
Safe (o::t) k'.
Proof.
intros.
inversion H0; subst; clear H0.
- assert (R: fork x y k = eval {| op_t := FORK; op_src := x; op_dst := y |} k) by auto.
rewrite R.
eauto using can_check_fork, safe_cons.
- assert (R: join x y k = eval {| op_t := JOIN; op_src := x; op_dst := y |} k) by auto.
rewrite R.
eauto using can_check_join, safe_cons.
Qed.
Lemma safe_inv:
forall o t k,
Safe (o::t) k ->
exists k', Safe t k' /\ CheckOp k' o k.
Proof.
intros.
inversion H; subst; clear H.
unfold eval.
inversion H2; subst; simpl in *; clear H2;
eauto using check_fork, check_join.
Qed.
Require Import Aniceto.Graphs.DAG.
Let copy_from_eq:
forall x y z k,
copy_from x y ((x,z) :: k) = (y,z) :: copy_from x y k.
Proof.
intros.
unfold copy_from.
simpl.
destruct (tid_eq_dec x x). {
auto.
}
contradiction n.
apply TID.eq_refl.
Qed.
Let copy_from_neq:
forall x y a b k,
a <> x ->
copy_from x y ((a,b) :: k) = copy_from x y k.
Proof.
intros.
unfold copy_from.
simpl.
destruct (tid_eq_dec a x). {
contradiction H.
}
auto.
Qed.
Let outgoing_of_some:
forall x y,
outgoing_of x (x, y) = Some y.
Proof.
intros.
unfold outgoing_of.
destruct (tid_eq_dec x x).
- auto.
- contradiction n; auto.
Qed.
Let outgoing_of_some_rw:
forall x e y,
outgoing_of x e = Some y ->
e = (x, y).
Proof.
unfold outgoing_of.
intros.
destruct e.
destruct (tid_eq_dec t x); inversion H.
subst; auto.
Qed.
Let in_filter_outgoing:
forall x y k,
List.In (x, y) k ->
List.In y (filter_outgoing x k).
Proof.
intros.
unfold filter_outgoing.
eauto using in_omap_1, outgoing_of_some.
Qed.
Let in_copy_from_1:
forall x y z k,
List.In (x, z) k ->
List.In (y, z) (copy_from x y k).
Proof.
intros.
unfold copy_from.
rewrite in_map_iff.
eauto using in_filter_outgoing.
Qed.
Let in_copy_from_2:
forall x y z k,
List.In (y, z) (copy_from x y k) ->
List.In (x, z) k.
Proof.
intros.
unfold copy_from, filter_outgoing in *.
rewrite in_map_iff in H.
destruct H as (x', (?,Hi)).
inversion H; subst.
apply in_omap_2 in Hi.
destruct Hi as (e,(Hi,R)).
apply outgoing_of_some_rw in R; subst.
assumption.
Qed.
Let copy_from_inv_in:
forall x y a b k,
List.In (x, y) (copy_from a b k) ->
x = b /\ List.In (a, y) k.
Proof.
intros.
destruct (tid_eq_dec x b). {
split; auto.
subst.
eauto using in_copy_from_2.
}
unfold copy_from, filter_outgoing in *.
rewrite in_map_iff in H.
destruct H as (?, (He, ?)).
inversion He; subst.
contradiction n; trivial.
Qed.
Lemma in_fork_1:
forall x y z k,
x <> y ->
~ Graph.In (Edge k) y ->
List.In (y, z) (fork x y k) ->
List.In (x, z) k.
Proof.
intros.
unfold fork in *.
rewrite in_app_iff in H1.
destruct H1.
- eauto using in_copy_from_2.
- destruct H1.
+ inversion H1; subst.
contradiction H; auto.
+ contradiction H0.
eauto using in_left, edge_spec.
Qed.
Lemma in_fork_2:
forall x y k z,
List.In (x,z) k ->
List.In (y,z) (fork x y k).
Proof.
intros.
unfold fork.
rewrite in_app_iff.
eauto using in_copy_from_2.
Qed.
Lemma in_fork_3:
forall k x y z,
y <> z ->
List.In (x, z) (fork x y k) ->
List.In (y, z) (fork x y k).
Proof.
intros.
unfold fork in *.
rewrite in_app_iff in *.
destruct H0.
- apply copy_from_inv_in in H0.
destruct H0; subst.
eauto using in_copy_from_2.
- destruct H0.
+ inversion H0; subst.
contradiction H; trivial.
+ eauto using in_copy_from_2.
Qed.
Lemma fork_inv_in:
forall x y a b k,
List.In (x, y) (fork a b k) ->
(x = b /\ List.In (a, y) k) \/ (a = x /\ b = y) \/ List.In (x, y) k.
Proof.
intros.
unfold fork in *.
rewrite in_app_iff in *.
destruct H.
- apply copy_from_inv_in in H.
intuition.
- destruct H.
+ inversion H.
intuition.
+ auto.
Qed.
Lemma join_inv_in:
forall x y a b k,
List.In (x, y) (join a b k) ->
(x = a /\ List.In (b, y) k) \/ List.In (x, y) k.
Proof.
intros.
unfold join in *.
rewrite in_app_iff in *.
destruct H.
- apply copy_from_inv_in in H.
intuition.
- auto.
Qed.
Lemma in_fork_4:
forall x y z k,
~ Graph.In (FGraph.Edge k) y ->
List.In (z, y) (fork x y k) ->
z = x.
Proof.
intros.
apply fork_inv_in in H0.
destruct H0 as [(?,?)|[(?,?)|?]].
- subst.
contradiction H.
eauto using in_right.
- subst.
trivial.
- contradiction H.
eauto using in_right.
Qed.
Lemma in_fork_5:
forall x y k,
List.In (x, y) (fork x y k).
Proof.
intros.
unfold fork.
rewrite in_app_iff.
right.
auto using in_eq.
Qed.
Lemma in_join_2:
forall x y k z,
List.In (y,z) k ->
List.In (x,z) (join x y k).
Proof.
intros.
unfold join.
rewrite in_app_iff.
eauto using in_copy_from_2.
Qed.
Let copy_from_inv_eq:
forall a b x y k,
List.In (a, b) (copy_from x y k) ->
a = y.
Proof.
induction k as [|(v1,v2)]; intros. {
inversion H.
}
destruct (tid_eq_dec v1 x). {
subst.
rewrite copy_from_eq in *.
destruct H. {
inversion H; subst.
trivial.
}
auto.
}
rewrite copy_from_neq in *; auto.
Qed.
Let reaches_edge_absurd_nil:
forall {A:Type} (x:A),
~ Reaches (Edge nil) x x.
Proof.
intros.
unfold not; intros.
inversion H.
inversion H0.
destruct H1 as (?,(?,(?,?))).
subst.
inversion H3.
inversion H6.
Qed.
Require Import Aniceto.Pair.
Let dag_fork_aux_0:
forall l k x y,
DAG (Edge k) ->
x <> y ->
~ Graph.In (Edge k) y ->
incl l k ->
DAG (Edge (copy_from x y l ++ (x,y)::k)).
Proof.
induction l; intros. {
simpl in *.
apply f_dag_cons; auto using TID.eq_dec.
unfold not; intros X.
contradiction H1; eauto using reaches_to_in_fst.
}
assert (DAG (Edge (copy_from x y l ++ (x,y) ::k))) by
eauto using List.incl_strengthten; clear IHl.
destruct a as (a,b).
destruct (TID.eq_dec a x); rewrite tid_eq_rw in *.
- subst.
rewrite copy_from_eq.
simpl.
assert (y <> b). {
unfold not; intros; subst.
assert (List.In (x,b) k) by (unfold incl in *; auto using in_eq).
assert (Edge k (x,b)) by (unfold Edge; auto).
contradiction H1.
eauto using in_right.
}
apply f_dag_cons; auto using TID.eq_dec.
remember (_ ++ _ :: k) as es.
unfold not; intros.
(* for b to reach y in ES, then it must pass through (x,y) *)
inversion H5 as (w,Hw2).
assert (w = (x,y) :: nil). {
inversion Hw2; subst.
inversion H7 as ((v,?),(?,?)); simpl in *; subst.
rename H9 into E.
remember (_ ++ _) as es.
assert (v = x). {
assert (Hi := E).
apply end_to_edge with (Edge:=Edge es) in Hi; auto.
subst.
unfold Edge in Hi.
rewrite in_app_iff in Hi.
destruct Hi.
- assert (v = y) by eauto; subst.
apply in_copy_from_2 in H9.
assert (List.In (x,y) k); (apply List.incl_strengthten in H2; auto).
contradiction H1.
assert (Edge k (x,y)) by (unfold Edge;auto).
eauto using in_right.
- destruct H9 as [X|X]. {
inversion X; subst; auto.
}
contradiction H1.
assert (Edge k (v,y)) by (unfold Edge;auto).
eauto using in_right.
}
subst.
assert (List.In (x,y) w) by auto using end_in.
apply end_to_append in E.
destruct E as (w', ?); subst.
destruct w'. {
auto.
}
apply walk2_split_app in Hw2.
destruct Hw2.
remember (_ ++ (_ :: k)) as es.
assert (Reaches (Edge es) b x) by eauto using reaches_def.
assert (Reaches (Edge es) x b). {
assert (Edge es (x,b)). {
unfold Edge, incl in *.
subst.
rewrite in_app_iff.
eauto using in_eq, in_cons.
}
auto using edge_to_reaches.
}
assert (n: Reaches (Edge es) b b) by eauto using reaches_trans.
apply H3 in n.
contradiction.
} (* w = (x,y) :: nil *)
subst.
assert (b = x). {
inversion Hw2; subst.
apply starts_with_eq in H6.
auto.
}
subst.
remember (_ ++ (_ :: k)) as es.
assert (n: Reaches (Edge es) x x). {
assert (Edge es (x,x)). {
unfold Edge.
subst.
rewrite in_app_iff.
eauto using in_eq, in_cons.
}
auto using edge_to_reaches.
}
apply H3 in n; contradiction.
- rewrite copy_from_neq; auto.
Qed.
Let dag_fork:
forall k x y,
DAG (Edge k) ->
x <> y ->
~ Graph.In (Edge k) y ->
DAG (Edge (fork x y k)).
Proof.
unfold fork.
intros.
apply dag_fork_aux_0; auto using incl_refl.
Qed.
Let dag_join_aux_0:
forall l k x y,
DAG (Edge k) ->
x <> y ->
Edge k (x,y) ->
incl l k ->
DAG (Edge (copy_from y x l ++ k)).
Proof.
induction l; intros. { auto. }
assert (DAG (Edge (copy_from y x l ++ k))) by
eauto using List.incl_strengthten; clear IHl.
destruct a as (a,b).
destruct (TID.eq_dec a y); rewrite tid_eq_rw in *.
- subst.
rewrite copy_from_eq.
simpl.
assert (x <> b). {
unfold not; intros; subst.
assert (List.In (y,b) k) by (unfold incl in *; auto using in_eq).
assert (Edge k (y,b)) by (unfold Edge; auto).
assert (Reaches (Edge k) y b) by auto using edge_to_reaches.
assert (Reaches (Edge k) b y) by auto using edge_to_reaches.
assert (n: Reaches (Edge k) y y) by eauto using reaches_trans.
apply H in n; contradiction.
}
apply f_dag_cons_reaches; auto using TID.eq_dec.
remember ( _ ++ _ ) as es.
assert (Reaches (Edge es) y b). {
assert (Edge es (y,b)). {
unfold Edge, incl in *.
subst.
rewrite in_app_iff.
eauto using in_eq, in_cons.
}
auto using edge_to_reaches.
}
assert (Reaches (Edge es) x y). {
assert (Edge es (x,y)). {
unfold Edge, incl in *.
subst.
rewrite in_app_iff.
eauto using in_eq, in_cons.
}
auto using edge_to_reaches.
}
eauto using reaches_trans.
- rewrite copy_from_neq; auto.
Qed.
Let dag_join:
forall k x y,
DAG (Edge k) ->
Edge k (x,y) ->
DAG (Edge (join x y k)).
Proof.
intros.
unfold join.
assert (x <> y). {
unfold not; intros; subst.
assert (n: Reaches (Edge k) y y) by auto using edge_to_reaches.
apply H in n.
contradiction.
}
apply dag_join_aux_0; auto using incl_refl.
Qed.
Lemma eval_preserves_dag:
forall k o,
CanCheckOp k o ->
DAG (Edge k) ->
DAG (Edge (eval o k)).
Proof.
intros.
destruct o; auto.
inversion H; subst; simpl; eauto.
Qed.
(**
If the safe-joins graph that yields a trace is well-formed, then
the safe-joins graph is a DAG.
*)
Theorem safe_to_dag:
forall t k,
Safe t k ->
DAG (Edge k).
Proof.
intros.
induction H. {
auto using f_dag_nil.
}
auto using eval_preserves_dag.
Qed.
Notation R_Edge rs k := (Edge (restriction tid_eq_dec rs k)).
Notation R_DAG rs k := (DAG (R_Edge rs k)).
(* Import WellFormed.*)
Let running_infimum:
forall rs k,
restriction tid_eq_dec rs k <> nil ->
R_DAG rs k ->
exists x, Graph.In (R_Edge rs k) x /\
forall y, ~ Reaches (R_Edge rs k) x y.
Proof.
intros.
apply dag_infimum; auto using TID.eq_dec.
Qed.
Let safe_to_r_dag:
forall t k rs,
Safe t k ->
R_DAG rs k.
Proof.
intros.
apply safe_to_dag in H.
apply f_dag_incl with (es:=k); auto using restriction_incl.
Qed.
Let progress_0:
forall t k rs,
Safe t k ->
(restriction tid_eq_dec rs k) <> nil ->
exists x, List.In x rs /\ forall y, Edge k (x,y) -> ~ List.In y rs.
Proof.
intros.
assert (Hx:
exists x, Graph.In (R_Edge rs k) x /\
forall y, ~ Reaches (R_Edge rs k) x y). {
eapply running_infimum; eauto.
}
destruct Hx as (x, (Hin, Hy)).
exists x.
split; eauto using restriction_in_1.
unfold not;
intros y He Hx.
assert (Hz := Hy y); clear Hy.
contradiction Hz; clear Hz.
assert (List.In x rs). {
eauto using restriction_in_1.
}
assert (R_Edge rs k (x,y)). {
unfold Edge.
auto using restriction_in_2.
}
auto using edge_to_reaches.
Qed.
(** If the wait-for-graph is nonempty, then there exists a task
that is not waiting for anyone (i.e., can execute). *)
Theorem progress:
forall t k rs,
Safe t k ->
rs <> nil ->
exists x, List.In x rs /\ forall y, Edge k (x,y) -> ~ List.In y rs.
Proof.
intros.
remember (restriction tid_eq_dec rs k) as l.
destruct l. {
subst.
destruct rs. { contradiction H0; auto. }
exists t0.
assert (List.In t0 (t0::rs)) by auto using in_eq.
split. { auto. }
intros.
unfold not; intros.
assert (List.In (t0, y) (restriction tid_eq_dec (t0::rs) k)) by auto using restriction_in_2.
rewrite <- Heql in *.
inversion H4.
}
assert (restriction tid_eq_dec rs k <> nil). {
intuition.
rewrite H1 in *.
inversion Heql.
}
eauto.
Qed.
Require Import Aniceto.Graphs.FGraph.
(**
If the trace is well-formed and the WFG is a subset of the knowledge graph,
then the WFG is a DAG (deadlock free).
*)
Corollary deadlock_avoidance:
forall t k wfg,
Safe t k ->
(forall e, Edge wfg e -> Edge k e) ->
DAG (Edge wfg).
Proof.
intros.
assert (DAG (Edge k)) by eauto using safe_to_dag.
apply dag_impl with (E:=Edge k); auto.
Qed.
End Defs.
Section Trace.
Import SJ_Notations.
Inductive Edge : trace -> (tid*tid) -> Prop :=
| edge_fork_eq:
forall x y k,
Edge (F x y:: k) (x,y)
| edge_fork_trans:
forall x y z k,
Edge k (x, z) ->
Edge (F x y::k) (y, z)
| edge_join:
forall x y z k,
Edge k (y, z) ->
Edge (J x y::k) (x, z)
| edge_cons:
forall k e e',
Edge k e ->
Edge (e'::k) e.
Inductive Trace: trace -> Prop :=
| trace_nil:
Trace nil
| trace_fork:
forall x y l,
x <> y ->
~ Graph.In (Edge l) y ->
Trace l ->
Trace (F x y:: l)
| trace_join:
forall x y l,
Trace l ->
Edge l (x, y) ->
Trace (J x y :: l).
Lemma edge_to_f_edge:
forall l e k,
Safe l k ->
Edge l e ->
FGraph.Edge k e.
Proof.
induction l; intros;
unfold FGraph.Edge. {
inversion H0.
}
inversion H; subst; clear H.
inversion H0; subst; clear H0.
- unfold eval; simpl.
auto using in_fork_5.
- eapply IHl in H4; eauto.
inversion H3; subst; clear H3.
unfold eval; simpl.
auto using in_fork_2.
- unfold eval; simpl.
eapply IHl in H4; eauto.
eauto using in_join_2.
- apply in_eval.
apply IHl with (k:=k0) in H4; auto.
Qed.
Lemma f_edge_to_edge:
forall l e k,
Safe l k ->
FGraph.Edge k e ->
Edge l e.
Proof.
induction l; intros;
unfold FGraph.Edge in *. {
inversion H; subst.
inversion H0.
}
inversion H; subst; clear H.
destruct e as (x, y).
destruct a as ([], a, b); unfold eval in *; simpl in *.
- apply fork_inv_in in H0.
destruct H0 as [(?,?)|[(?,?)|?]]; subst;