-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpndfs_allred.pvl
1674 lines (1496 loc) · 92.2 KB
/
pndfs_allred.pvl
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
/* * Optimised Parallel Nested DFS (all-red) */
class Worker {
/** ** Thread-local state */
Master master;
int tid; // thread identifier
int nthreads; // total number of threads (in `master`)
seq<int> color; // color set
boolean term; // thread-local termination flag
seq<boolean> observedred; // all states observed red by this worker
seq<boolean> pink; // set of pink states
boolean exploringred; // true if this worker is performing `dfsred`
boolean waiting; // if `exploringred == true`, then `waiting` indicates that the worker is done exploring and waiting for other threads to complete their exploration
int redroot; // if `exploringred == true`, then `redroot` is the root node from which a red DFS is being performed
// inline predicate that helps to connect the state of the master with the state of a worker.
static inline resource MasterState(Worker me, int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot) =
me != null **
Perm(me.master, read) ** me.master != null **
Perm(me.nthreads, read) ** 0 < me.nthreads **
Perm(me.tid, read) ** 0 <= me.tid ** me.tid < me.nthreads **
Perm(me.term, 1/2) **
Perm(me.master.N, read) ** N == me.master.N **
Perm(me.master.G, read) ** G == me.master.G **
Perm(me.master.A, read) ** A == me.master.A **
Perm(me.master.blueroot, read) ** blueroot == me.master.blueroot **
Perm(me.master.threads, read) **
|me.master.threads| == me.nthreads **
me.master.threads[me.tid] == me;
/* ** Auxiliary operations */
context MasterState(this, N, G, A, blueroot);
context Master.AdjacencyMatrix(N, G) && |A| == N;
context Perm(observedred, 1/2) ** |observedred| == N;
context 0 <= s && s < N;
ensures (\forall int v; 0 <= v && v < N && v != s; observedred[v] == \old(observedred[v]));
ensures \old(observedred[s]) ==> observedred[s];
ensures term == \old(term);
void update_redmarkers(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s) {
lock master;
if (master.red[s] && !observedred[s]) {
observedred = Master.UpdateBool(observedred, s, true);
}
unlock master;
}
//void update_redmarkers(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s);
context MasterState(this, N, G, A, blueroot);
context Master.AdjacencyMatrix(N, G) && |A| == N;
context Perm(color, 1/2) ** |color| == N;
context Perm(pink, 1/2) ** |pink| == N;
context Perm(observedred, 1/2) ** |observedred| == N;
context 0 <= s && s < N;
context (\forall int v; 0 <= v && v < N && G[s][v]; color[v] == Master.blue() || color[v] == Master.cyan() || observedred[v]);
context color[s] == Master.cyan() || color[s] == Master.blue();
context A[s] ==> color[s] == Master.cyan();
context Perm(exploringred, 1/2) ** exploringred;
context Perm(waiting, 1/2) ** !waiting;
context Perm(redroot, 1/2);
context 0 <= redroot && redroot < N && A[redroot];
context A[s] ==> s == redroot;
context Master.ExPath(N, G, redroot, s, 1);
ensures (\forall int v; 0 <= v && v < N && v != s; pink[v] == \old(pink[v]));
ensures pink[s];
ensures color == \old(color);
ensures observedred == \old(observedred);
ensures redroot == \old(redroot);
ensures term == \old(term);
void update_setpink(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s) {
lock master;
pink = Master.UpdateBool(pink, s, true);
unlock master;
}
//void update_setpink(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s);
context MasterState(this, N, G, A, blueroot);
context Master.AdjacencyMatrix(N, G) && |A| == N;
context Perm(color, 1/2) ** |color| == N;
context Perm(pink, 1/2) ** |pink| == N;
context Perm(observedred, 1/2) ** |observedred| == N;
context 0 <= s && s < N && !A[s];
context color[s] == Master.blue();
context (\forall int v; 0 <= v && v < N && G[s][v]; (observedred[v] || pink[v]) && color[v] != Master.cyan()); // needed for invariant 2
requires pink[s];
ensures color == \old(color);
ensures term == \old(term);
ensures (\forall int v; 0 <= v && v < N && v != s; pink[v] == \old(pink[v]) && observedred[v] == \old(observedred[v]));
ensures observedred[s] && !pink[s];
void update_setred(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s) {
lock master;
// if there exists a special path which we break by coloring `redroot` red, apply the lemma that finds another special path
assert (\forall int t1; 0 <= t1 && t1 < |master.threads| && Master.ExSpecialPath(N, G, master.threads[t1].color, master.threads[t1].pink, master.red);
Master.lemma_findspec_upd_thr(N, G, master.A, master.threads, master.red, Master.UpdateInt(master.redowner, s, tid), t1, s, master.threads[t1].redroot) &&
(\exists int t2; 0 <= t2 && t2 < |master.threads|; Master.ExSpecialPath(N, G, master.threads[t2].color, Master.UpdateBool(master.threads[t2].pink, s, false), Master.UpdateBool(master.red, s, true)))
);
master.red = Master.UpdateBool(master.red, s, true);
master.redowner = Master.UpdateInt(master.redowner, s, tid); // ghostcode
observedred = Master.UpdateBool(observedred, s, true); // ghostcode
pink = Master.UpdateBool(pink, s, false);
// increases performance (does not make much difference yet)
assert Master.KeyInvariant(N, G, A, master.threads, master.red, blueroot);
unlock master;
}
//void update_setred(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s);
context MasterState(this, N, G, A, blueroot);
context Master.AdjacencyMatrix(N, G) && |A| == N;
context Perm(color, 1/2) ** |color| == N;
context Perm(observedred, 1/2) ** |observedred| == N;
context 0 <= s && s < N;
context (\forall int v; 0 <= v && v < N && G[s][v]; observedred[v]);
context !A[s] && color[s] == Master.cyan();
ensures color == \old(color);
ensures term == \old(term);
ensures (\forall int v; 0 <= v && v < N && v != s; observedred[v] == \old(observedred[v]));
ensures observedred[s];
void update_setred_allred1(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s) {
lock master;
// if there exists a special path which we break by coloring `redroot` red, apply the lemma that finds another special path
assert (\forall int t1; 0 <= t1 && t1 < |master.threads| && Master.ExSpecialPath(N, G, master.threads[t1].color, master.threads[t1].pink, master.red);
Master.lemma_findspec_upd_thr(N, G, master.A, master.threads, master.red, Master.UpdateInt(master.redowner, s, tid), t1, s, master.threads[t1].redroot) &&
(\exists int t2; 0 <= t2 && t2 < |master.threads|; Master.ExSpecialPath(N, G, master.threads[t2].color, Master.UpdateBool(master.threads[t2].pink, s, false), Master.UpdateBool(master.red, s, true)))
);
master.red = Master.UpdateBool(master.red, s, true);
master.redowner = Master.UpdateInt(master.redowner, s, tid); // ghostcode
observedred = Master.UpdateBool(observedred, s, true); // ghostcode
assert Master.KeyInvariant(N, G, A, master.threads, master.red, blueroot);
unlock master;
}
//void update_setred_allred1(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s);
context MasterState(this, N, G, A, blueroot);
context Master.AdjacencyMatrix(N, G) && |A| == N;
context Perm(color, 1/2) ** |color| == N;
context Perm(pink, 1/2) ** |pink| == N;
context Perm(exploringred, 1/2) ** Perm(waiting, 1/2) ** Perm(redroot, 1/2);
context Perm(observedred, 1/2) ** |observedred| == N;
context 0 <= s && s < N;
context !exploringred;
context (\forall int v; 0 <= v && v < N && G[s][v]; observedred[v]);
context A[s] && color[s] == Master.cyan();
context (\forall int v; 0 <= v && v < N; !pink[v]);
context (\forall int v; 0 <= v && v < N && color[v] == Master.cyan(); Master.ExPath(N, G, v, s, 1));
ensures color == \old(color) && pink == \old(pink) && term == \old(term);
ensures (\forall int v; 0 <= v && v < N && v != s; observedred[v] == \old(observedred[v]));
ensures observedred[s];
void update_setred_allred2(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s) {
if (!observedred[s]) {
update_explorered(N, G, A, blueroot, s);
if (!observedred[s]) {
update_setpink(N, G, A, blueroot, s);
assert pink == Master.UpdateBool(\old(pink), s, true);
update_setwaiting(N, G, A, blueroot);
update_setred_await(N, G, A, blueroot);
assert pink == Master.UpdateBool(\old(pink), s, false);
}
}
}
//void update_setred_allred2(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s);
invariant MasterState(this, N, G, A, blueroot);
invariant Master.AdjacencyMatrix(N, G) && |A| == N;
invariant Perm(color, 1/2) ** |color| == N;
invariant Perm(pink, 1/2) ** |pink| == N;
invariant Perm(observedred, 1/2) ** |observedred| == N;
invariant Perm(exploringred, 1/2) ** Perm(waiting, 1/2) ** Perm(redroot, 1/2);
invariant 0 <= redroot && redroot < N;
invariant (\forall int v; 0 <= v && v < N && G[redroot][v]; observedred[v]);
requires exploringred && waiting;
ensures color == \old(color);
ensures redroot == \old(redroot);
ensures term == \old(term);
ensures (\forall int v; 0 <= v && v < N && v != redroot; pink[v] == \old(pink[v]) && observedred[v] == \old(observedred[v]));
ensures observedred[redroot] && !pink[redroot];
ensures !waiting && !exploringred;
void update_setred_await(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot) {
boolean stop = false;
loop_invariant color == \old(color);
loop_invariant redroot == \old(redroot);
loop_invariant term == \old(term);
loop_invariant !stop ==> (exploringred && waiting);
loop_invariant !stop ==> pink == \old(pink);
loop_invariant !stop ==> observedred == \old(observedred);
loop_invariant stop ==> (\forall int v; 0 <= v && v < N && v != redroot; pink[v] == \old(pink[v]));
loop_invariant stop ==> (\forall int v; 0 <= v && v < N && v != redroot; observedred[v] == \old(observedred[v]));
loop_invariant stop ==> (observedred[redroot] && !pink[redroot]);
loop_invariant stop ==> (!waiting && !exploringred);
while (!stop) {
lock master;
// the following if-statement can also be implemented using another while-loop if one does not use PVL to implement the algorithm, but for now this is more convenient
if ((\forall int t; 0 <= t && t < |master.threads| && master.threads[t].exploringred && master.threads[t].redroot == redroot; master.threads[t].waiting)) {
// if `redroot` is not already red but there exists a cycle that includes `redroot`, we can also find a special path using our lemma
assert (!master.red[redroot] && Master.ExPath(N, G, redroot, redroot, 2)) ==> (
Master.lemma_findspec_cycle_thr(N, G, master.A, master.threads, master.red, master.redowner, redroot) &&
(\exists int t; 0 <= t && t < |master.threads|; Master.ExSpecialPath(N, G, master.threads[t].color, master.threads[t].pink, master.red))
);
// if there exists a special path which we break by coloring `redroot` red, apply the lemma that finds another special path
assert (\forall int t1; 0 <= t1 && t1 < |master.threads| && Master.ExSpecialPath(N, G, master.threads[t1].color, master.threads[t1].pink, master.red);
Master.lemma_findspec_upd_thr(N, G, master.A, master.threads, master.red, Master.UpdateInt(master.redowner, redroot, tid), t1, redroot, master.threads[t1].redroot) &&
(\exists int t2; 0 <= t2 && t2 < |master.threads|; Master.ExSpecialPath(N, G, master.threads[t2].color, Master.UpdateBool(master.threads[t2].pink, redroot, false), Master.UpdateBool(master.red, redroot, true)))
);
master.red = Master.UpdateBool(master.red, redroot, true);
master.redowner = Master.UpdateInt(master.redowner, redroot, tid); // ghost
observedred = Master.UpdateBool(observedred, redroot, true); // ghost
pink = Master.UpdateBool(pink, redroot, false);
exploringred = false; // ghost
waiting = false; // ghost
stop = true;
// increases verification performance
assert (\forall int t; 0 <= t && t < |master.threads|;
master.ColorTypes(master.threads[t].color) &&
master.ThreadLocalInvariants(N, G, A, master.threads[t].color, master.threads[t].pink, master.red)
);
// invariant 2; increases performance
assert (\forall int v; 0 <= v && v < N && master.red[v];
(\forall int w; 0 <= w && w < N && G[v][w];
master.red[w] || (master.threads[master.redowner[v]].pink[w] && master.threads[master.redowner[v]].color[w] != Master.cyan())
)
);
// increases verification performance
assert Master.Invariant6(N, G, A, master.red);
assert Master.KeyInvariant(N, G, A, master.threads, master.red, blueroot);
}
unlock master;
}
}
//void update_setred_await(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot);
context MasterState(this, N, G, A, blueroot);
context Master.AdjacencyMatrix(N, G) ** |A| == N;
context Perm(pink, 1/2) ** |pink| == N;
context Perm(color, 1/2) ** |color| == N;
context Perm(exploringred, 1/2) ** !exploringred;
context 0 <= s && s < N;
context Master.ColorTypes(color);
requires !pink[s];
ensures pink == \old(pink);
ensures term == \old(term);
ensures (\forall int v; 0 <= v && v < N && v != s; color[v] == \old(color[v]));
ensures color[s] == Master.cyan();
void update_setcyan(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s) {
lock master;
color = Master.UpdateInt(color, s, Master.cyan());
unlock master;
}
//void update_setcyan(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s);
context MasterState(this, N, G, A, blueroot);
context Master.AdjacencyMatrix(N, G) ** |A| == N;
context Perm(color, 1/2) ** |color| == N;
context Perm(pink, 1/2) ** |pink| == N;
context Perm(observedred, 1/2) ** |observedred| == N;
context 0 <= s && s < N;
context Master.ColorTypes(color);
context (\forall int v; 0 <= v && v < N && G[s][v]; color[v] == Master.blue() || color[v] == Master.cyan() || observedred[v]);
context (\forall int v; 0 <= v && v < N; !pink[v]);
context A[s] ==> observedred[s];
context !pink[s];
requires color[s] == Master.cyan();
ensures (\forall int v; 0 <= v && v < N && v != s; color[v] == \old(color[v]));
ensures color[s] == Master.blue();
ensures pink == \old(pink);
ensures observedred == \old(observedred);
ensures term == \old(term);
void update_setblue(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s) {
lock master;
color = Master.UpdateInt(color, s, Master.blue());
unlock master;
}
//void update_setblue(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s);
/// entire run took 56951 ms (complete)
context MasterState(this, N, G, A, blueroot);
context Master.AdjacencyMatrix(N, G) && |A| == N;
context Perm(pink, 1/2) ** |pink| == N;
context Perm(color, 1/2) ** |color| == N;
context Perm(observedred, 1/2) ** |observedred| == N;
context Perm(waiting, 1/2) ** Perm(redroot, 1/2);
context 0 <= redroot && redroot < N;
context pink[redroot] && A[redroot];
context (\forall int v; 0 <= v && v < N && v != redroot; !pink[v]);
context (\forall int v; 0 <= v && v < N && G[redroot][v]; observedred[v]);
requires !waiting;
ensures waiting;
ensures redroot == \old(redroot);
ensures color == \old(color);
ensures observedred == \old(observedred);
ensures pink == \old(pink);
ensures redroot == \old(redroot);
ensures term == \old(term);
void update_setwaiting(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot) {
lock master;
waiting = true;
unlock master;
}
//void update_setwaiting(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot);
context MasterState(this, N, G, A, blueroot);
context Master.AdjacencyMatrix(N, G) && |A| == N;
context Perm(color, 1/2) ** |color| == N;
context Perm(observedred, 1/2) ** |observedred| == N;
context Perm(exploringred, 1/2) ** Perm(waiting, 1/2) ** Perm(redroot, 1/2);
context 0 <= root && root < N && A[root];
context (\forall int v; 0 <= v && v < N && color[v] == Master.cyan(); Master.ExPath(N, G, v, root, 1));
requires !exploringred && !observedred[root];
ensures color == \old(color);
ensures term == \old(term);
ensures (\forall int v; 0 <= v && v < N && v != root; observedred[v] == \old(observedred[v]));
ensures !observedred[root] ==> (exploringred && !waiting && redroot == root);
ensures observedred[root] ==> (!exploringred && redroot == \old(redroot));
void update_explorered(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int root) {
lock master;
if (master.red[root]) {
observedred = Master.UpdateBool(observedred, root, true);
}
else {
exploringred = true;
waiting = false;
redroot = root;
}
unlock master;
}
//void update_explorered(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int root);
context MasterState(this, N, G, A, blueroot);
ensures !\result ==> (term == \old(term));
ensures \result ==> term;
boolean update_abort(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot) {
boolean aborting = false;
lock master;
if (master.abort) {
term = true;
aborting = true;
}
unlock master;
return aborting;
}
//boolean update_abort(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot);
/* ** DFS red */
yields int c; // explicit witness
yields seq<int> P; // explicit witness
invariant MasterState(this, N, G, A, blueroot);
invariant Master.AdjacencyMatrix(N, G) && |A| == N;
invariant Perm(color, 1/2) ** |color| == N;
invariant Perm(pink, 1/2) ** |pink| == N;
invariant Perm(observedred, 1/2) ** |observedred| == N;
invariant 0 <= s && s < N;
invariant 0 <= root && root < N;
invariant Master.ColorTypes(color);
invariant s == root || color[s] == Master.blue();
invariant color[root] == Master.cyan() && A[root];
invariant Master.Invariant1a(N, G, color, observedred);
invariant Master.Invariant1b(N, G, color, pink, observedred);
invariant (\forall int v; 0 <= v && v < N && G[root][v]; color[v] == Master.blue() || color[v] == Master.cyan() || observedred[v]); // `Next({root}, Blue ∪ Cyan ∪ Red)`
invariant (\forall int v; 0 <= v && v < N; !pink[v] || !observedred[v]); // `Pink ∩ Red = Ø`
invariant Master.Invariant3(N, G, A, color, observedred);
invariant Master.Invariant4(N, G, A, color, pink);
invariant (\forall int v; 0 <= v && v < N && pink[v] && v != root; color[v] == Master.blue()); // `Pink ⊆ Blue ∪ {root}` -- L5 (improved)
invariant (\forall int v; 0 <= v && v < N && v != root && observedred[v]; color[v] != Master.cyan()); // `Red - {root} ⊆ (G - {root}) - Cyan`
invariant (\forall int v; 0 <= v && v < N && color[v] == Master.cyan(); Master.ExPath(N, G, v, root, 1));
invariant (\forall int v; 0 <= v && v < N && pink[v]; Master.ExPath(N, G, root, v, 1));
invariant A[s] ==> s == root;
invariant s == root ==> A[s];
invariant Perm(exploringred, 1/2);
invariant Perm(waiting, 1/2) ** !waiting;
invariant Perm(redroot, 1/2) ** redroot == root;
requires !pink[s];
requires !observedred[s];
requires !term;
requires s == root ==> (\forall int v; 0 <= v && v < N && v != root; !pink[v]);
requires exploringred;
requires Master.ExPath(N, G, root, s, 1);
ensures color == \old(color);
ensures (\forall int v; 0 <= v && v < N && \old(observedred[v]); observedred[v]); // `old(Red) ⊆ Red`
ensures \result == Master.found() || \result == Master.notfound() || \result == Master.aborted(); // return types
ensures \result == Master.notfound() ==> (\forall int v; 0 <= v && v < N; pink[v] == \old(pink[v])); // `¬found ⇒ old(Pink) = Pink`
ensures \result == Master.notfound() ==> observedred[s];
ensures (\result == Master.notfound() && s == root) ==> (\forall int v; 0 <= v && v < N && v != root; !pink[v]);
ensures \result == Master.notfound() ==> (A[s] == !exploringred);
ensures \result == Master.found() ==> (0 <= c && c < N && color[c] == Master.cyan());
ensures \result == Master.found() ==> (1 < |P| && Master.Path(N, G, s, c, P));
ensures \result == Master.aborted() ==> term;
ensures \result != Master.aborted() ==> !term;
int dfsred(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s, int root) {
update_setpink(N, G, A, blueroot, s);
assert A[s] ==> s == root;
int found = Master.notfound();
int t = 0;
assert !term;
// The parallel algorithm should actually traverse the successors of `s` in random order to obtain any speedup.
// However, from a verification point-of-view this does not matter, as long as all successors have been
// considered when the while-loop below terminates.
loop_invariant 0 <= t && t <= N;
loop_invariant color == \old(color);
loop_invariant (\forall int v; 0 <= v && v < N && \old(observedred[v]); observedred[v]); // `old(Red) ⊆ Red`
loop_invariant found == Master.found() || found == Master.notfound() || found == Master.aborted(); // return types
loop_invariant found == Master.notfound() ==> pink[s]; // `s ∈ Pink`
loop_invariant found == Master.notfound() ==> (\forall int v; 0 <= v && v < N && v != s; pink[v] == \old(pink[v])); // `¬found ⇒ old(Pink - {s}) = Pink - {s}`
loop_invariant found == Master.notfound() ==> (\forall int v; 0 <= v && v < t && G[s][v]; observedred[v] || pink[v]); // needed for invariant 2
loop_invariant found == Master.notfound() ==> (\forall int v; 0 <= v && v < t && G[s][v]; color[v] != Master.cyan()); // needed for invariant 2
loop_invariant (found == Master.notfound() && s == root) ==> (\forall int v; 0 <= v && v < N && v != root; !pink[v]);
loop_invariant found == Master.notfound() ==> exploringred;
loop_invariant found == Master.found() ==> (0 <= c && c < N && color[c] == Master.cyan());
loop_invariant found == Master.found() ==> (1 < |P| && Master.Path(N, G, s, c, P));
loop_invariant found == Master.aborted() ==> term;
loop_invariant found != Master.aborted() ==> !term;
while (t < N && found == Master.notfound()) {
// check for global termination
boolean aborting = update_abort(N, G, A, blueroot);
if (aborting) {
found = Master.aborted();
}
if (G[s][t] && found != Master.aborted()) {
if (color[t] == Master.cyan()) {
c = t;
P = seq<int> { s, t };
found = Master.found();
}
else {
if (!pink[t]) {
update_redmarkers(N, G, A, blueroot, t);
if (!observedred[t]) {
assert !A[t];
Master.lemma_edge_path_m(N, G, s, t);
Master.lemma_expath_trans_m(N, G, root, s, t, 1, 1);
found = dfsred(N, G, A, blueroot, t, root) with { c = c; P = P; };
if (found == Master.found()) {
P = seq<int> { s } + P;
}
}
}
}
}
t = t + 1;
}
if (found == Master.notfound()) {
if (A[s]) {
assert s == root;
update_setwaiting(N, G, A, blueroot);
update_setred_await(N, G, A, blueroot);
}
else {
update_setred(N, G, A, blueroot, s);
}
}
return found;
}
//int dfsred(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s, int root);
/* ** DFS blue */
yields int a; // explicit witness
yields seq<int> P; // explicit witness
invariant MasterState(this, N, G, A, blueroot);
invariant Master.AdjacencyMatrix(N, G) && |A| == N;
invariant Perm(color, 1/2) ** |color| == N;
invariant Perm(pink, 1/2) ** |pink| == N;
invariant Perm(observedred, 1/2) ** |observedred| == N;
invariant Perm(exploringred, 1/2);
invariant Perm(waiting, 1/2);
invariant Perm(redroot, 1/2);
invariant 0 <= s && s < N;
invariant 0 <= blueroot && blueroot < N;
invariant Master.ColorTypes(color);
invariant Master.Invariant1b(N, G, color, pink, observedred);
invariant Master.Invariant3(N, G, A, color, observedred);
requires color[s] == Master.white();
requires !observedred[s];
requires (\forall int v; 0 <= v && v < N; !pink[v]); // `Pink = Ø`
requires Master.Invariant1a(N, G, color, observedred);
requires (\forall int v; 0 <= v && v < N && observedred[v]; color[v] != Master.cyan()); // `Red ⊆ G - Cyan`
requires !exploringred;
requires !term;
requires (\forall int v; 0 <= v && v < N && color[v] == Master.cyan(); Master.ExPath(N, G, v, s, 1));
ensures (\forall int v; 0 <= v && v < N && \old(color[v]) == Master.blue(); color[v] == Master.blue()); // `old(Blue) ⊆ Blue`
ensures (\forall int v; 0 <= v && v < N && \old(observedred[v]); observedred[v]); // `old(Red) ⊆ Red`
ensures \result == Master.found() || \result == Master.notfound() || \result == Master.aborted(); // return types
ensures \result == Master.notfound() ==> (\forall int v; 0 <= v && v < N && color[v] == Master.cyan(); \old(color[v]) == Master.cyan());
ensures \result == Master.notfound() ==> (\forall int v; 0 <= v && v < N && \old(color[v]) == Master.cyan(); color[v] == Master.cyan());
ensures \result == Master.notfound() ==> color[s] == Master.blue();
ensures \result == Master.notfound() ==> (\forall int v; 0 <= v && v < N; !pink[v]); // `Pink = Ø`
ensures \result == Master.notfound() ==> Master.Invariant1a(N, G, color, observedred);
ensures \result == Master.notfound() ==> (\forall int v; 0 <= v && v < N && observedred[v]; color[v] != Master.cyan()); // `Red ⊆ G - Cyan`
ensures \result == Master.notfound() ==> !exploringred;
ensures \result == Master.notfound() ==> (\forall int v; 0 <= v && v < N && color[v] == Master.cyan(); Master.ExPath(N, G, v, s, 1));
ensures \result == Master.found() ==> (0 <= a && a < N && A[a]);
ensures \result == Master.found() ==> (Master.Path(N, G, s, a, P) && Master.ExPath(N, G, a, a, 2));
ensures \result == Master.aborted() ==> term;
ensures \result != Master.aborted() ==> !term;
int dfsblue(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s) {
update_setcyan(N, G, A, blueroot, s);
assert Master.ExPath(N, G, s, s, |seq<int> { s }|);
boolean allred = true;
int found = Master.notfound();
int t = 0;
// The parallel algorithm should actually traverse the successors of `s` in random order to obtain any speedup.
// However, from a verification point-of-view this is irrelevant, as long as all successors have been
// considered when the while-loop below terminates.
loop_invariant 0 <= t && t <= N;
loop_invariant (\forall int v; 0 <= v && v < N && \old(color[v]) == Master.blue(); color[v] == Master.blue()); // `old(Blue) ⊆ Blue`
loop_invariant (\forall int v; 0 <= v && v < N && \old(observedred[v]); observedred[v]); // `old(Red) ⊆ Red`
loop_invariant found == Master.found() || found == Master.notfound() || found == Master.aborted(); // return types
loop_invariant found == Master.notfound() ==> (\forall int i; 0 <= i && i < N && i != s; (color[i] == Master.cyan()) == (\old(color[i]) == Master.cyan()));
loop_invariant found == Master.notfound() ==> color[s] == Master.cyan();
loop_invariant found == Master.notfound() ==> (\forall int v; 0 <= v && v < t && G[s][v]; color[v] == Master.cyan() || color[v] == Master.blue() || observedred[v]);
loop_invariant found == Master.notfound() ==> (\forall int v; 0 <= v && v < N; !pink[v]);
loop_invariant found == Master.notfound() ==> Master.Invariant1a(N, G, color, observedred);
loop_invariant found == Master.notfound() ==> (\forall int v; 0 <= v && v < N && observedred[v]; color[v] != Master.cyan()); // `¬found ⇒ Red ⊆ G - Cyan`
loop_invariant found == Master.notfound() ==> !exploringred;
loop_invariant found == Master.notfound() ==> (\forall int v; 0 <= v && v < N && color[v] == Master.cyan(); Master.ExPath(N, G, v, s, 1));
loop_invariant (found == Master.notfound() && allred) ==> (\forall int v; 0 <= v && v < t && G[s][v]; observedred[v]);
loop_invariant found == Master.found() ==> (0 <= a && a < N && A[a]);
loop_invariant found == Master.found() ==> (Master.Path(N, G, s, a, P) && Master.ExPath(N, G, a, a, 2));
loop_invariant found == Master.aborted() ==> term;
loop_invariant found != Master.aborted() ==> !term;
while (t < N && found == Master.notfound()) {
// check for global termination
boolean aborting = update_abort(N, G, A, blueroot);
if (aborting) {
found = Master.aborted();
}
if (G[s][t] && found != Master.aborted()) {
if (color[t] == Master.white()) {
update_redmarkers(N, G, A, blueroot, t);
if (!observedred[t]) {
Master.lemma_nextcyan_m(N, G, color, s, t);
found = dfsblue(N, G, A, blueroot, t) with { a = a; P = P; };
if (found == Master.found()) {
P = seq<int> { s } + P;
}
}
}
if (found == Master.notfound()) {
if (!observedred[t]) {
allred = false; // allred extension
}
}
}
t = t + 1;
}
if (found == Master.notfound()) {
if (allred) {
if (!A[s]) {
update_setred_allred1(N, G, A, blueroot, s); // apply optimisation for non-accepting states
}
else {
update_setred_allred2(N, G, A, blueroot, s); // apply optimisation for accepting states
}
}
else {
if (A[s]) {
update_explorered(N, G, A, blueroot, s);
assert (\forall int v; 0 <= v && v < N && v != s && observedred[v]; color[v] != Master.cyan()); // needed
if (!observedred[s]) {
int c;
seq<int> Q;
found = dfsred(N, G, A, blueroot, s, s) with { c = c; P = Q; };
if (found == Master.found()) {
Master.lemma_cyclefoundhere_m(N, G, color, s, c, Q);
a = s;
P = seq<int> { s };
}
}
}
}
}
if (found == Master.notfound()) {
update_setblue(N, G, A, blueroot, s);
}
return found;
}
//int dfsblue(int N, seq<seq<boolean>> G, seq<boolean> A, int blueroot, int s);
}
class Master {
/** ** Global state */
int N; // total number of states
seq<seq<boolean>> G; // adjacency matrix representation of the input graph
seq<boolean> A; // the (finite) set of accepting states
seq<boolean> red; // set of red states
seq<int> redowner; // for bookkeeping: if `red[v] == true`, then `redowner[v]` contains the rank of the worker that has marked node `v` red
seq<Worker> threads; // worker set
boolean abort; // global termination flag
int blueroot; // the initial state from which all threads perform NDFS
int result; // result of NDFS
resource lock_invariant() =
// graph state
Perm(N, read) ** Perm(G, read) ** AdjacencyMatrix(N, G) **
Perm(A, read) ** |A| == N **
Perm(blueroot, read) ** 0 <= blueroot ** blueroot < N **
Perm(red, 1) ** |red| == N **
Perm(redowner, 1) ** |redowner| == N **
// state of threads
Perm(threads, read) ** 0 < |threads| **
(\forall int t; 0 <= t && t < |threads|;
threads[t] != null
) **
(\forall* int t; 0 <= t && t < |threads|;
Perm(threads[t].master, read) **
Perm(threads[t].tid, read) **
Perm(threads[t].nthreads, read) **
Perm(threads[t].color, 1/2) **
Perm(threads[t].observedred, 1/2) **
Perm(threads[t].pink, 1/2) **
Perm(threads[t].exploringred, 1/2) **
Perm(threads[t].waiting, 1/2) **
Perm(threads[t].term, 1/2) **
Perm(threads[t].redroot, 1/2)
) **
(\forall int t; 0 <= t && t < |threads|;
threads[t].master == this &&
threads[t].tid == t &&
threads[t].nthreads == |threads|
) **
(\forall int t; 0 <= t && t < |threads|;
|threads[t].color| == N &&
|threads[t].observedred| == N &&
|threads[t].pink| == N
) **
// state of red nodes
(\forall int t; 0 <= t && t < |threads|;
(\forall int v; 0 <= v && v < N && threads[t].observedred[v]; red[v])
) **
(\forall int v; 0 <= v && v < N && red[v];
0 <= redowner[v] && redowner[v] < |threads| &&
threads[redowner[v]].observedred[v] &&
threads[redowner[v]].color[v] != white()
) **
// PNDFS invariants
(\forall int t; 0 <= t && t < |threads|;
ColorTypes(threads[t].color) &&
ThreadLocalInvariants(N, G, A, threads[t].color, threads[t].pink, red)
) **
(\forall int v; 0 <= v && v < N && red[v];
(\forall int w; 0 <= w && w < N && G[v][w];
red[w] || (threads[redowner[v]].pink[w] && threads[redowner[v]].color[w] != cyan()) // Invariant 2
)
) **
Invariant6(N, G, A, red) **
KeyInvariant(N, G, A, threads, red, blueroot) **
// `dfsred` worker statusses
(\forall int t; 0 <= t && t < |threads| && threads[t].exploringred; 0 <= threads[t].redroot && threads[t].redroot < N) **
(\forall int t; 0 <= t && t < |threads| && threads[t].exploringred; A[threads[t].redroot]) **
(\forall int t; 0 <= t && t < |threads| && threads[t].exploringred && !threads[t].waiting; !red[threads[t].redroot]) **
(\forall int t; 0 <= t && t < |threads| && threads[t].exploringred && threads[t].waiting; threads[t].pink[threads[t].redroot]) **
(\forall int t; 0 <= t && t < |threads| && threads[t].exploringred && threads[t].waiting; (\forall int v; 0 <= v && v < N && v != threads[t].redroot; !threads[t].pink[v])) **
(\forall int t; 0 <= t && t < |threads| && !threads[t].exploringred; (\forall int v; 0 <= v && v < N; !threads[t].pink[v])) **
(\forall int t; 0 <= t && t < |threads|; (\forall int p; 0 <= p && p < N && threads[t].pink[p] && A[p]; threads[t].exploringred && threads[t].redroot == p)) **
(\forall int t; 0 <= t && t < |threads| && threads[t].exploringred; (\forall int p; 0 <= p && p < N && threads[t].pink[p]; Master.ExPath(N, G, threads[t].redroot, p, 1))) **
(\forall int t; 0 <= t && t < |threads| && threads[t].exploringred; (\forall int c; 0 <= c && c < N && threads[t].color[c] == Master.cyan(); Master.ExPath(N, G, c, threads[t].redroot, 1))) **
(\forall int t; 0 <= t && t < |threads| && threads[t].exploringred && threads[t].waiting; (\forall int v; 0 <= v && v < N && G[threads[t].redroot][v]; red[v])) **
// termination handling
Perm(abort, 1/2) **
(\forall int t; 0 <= t && t < |threads|; threads[t].term ==> abort);
requires 0 < nthreads;
requires AdjacencyMatrix(N_, G_) && |A_| == N_;
requires 0 <= root && root < N_;
ensures Perm(N, read) ** N == N_;
ensures Perm(G, read) ** G == G_;
ensures Perm(A, read) ** A == A_;
ensures AdjacencyMatrix(N, G) && |A| == N;
ensures Perm(blueroot, read) ** blueroot == root;
ensures 0 <= blueroot && blueroot < N;
ensures Perm(threads, read) ** |threads| == nthreads;
ensures Perm(abort, 1/2) ** !abort;
ensures (\forall* int t; 0 <= t && t < nthreads;
Perm(threads[t].master, read) ** threads[t].master == this **
Perm(threads[t].tid, read) ** threads[t].tid == t **
Perm(threads[t].nthreads, read) ** threads[t].nthreads == nthreads **
Perm(threads[t].color, 1/2) ** threads[t].color == NewIntSeq(N) **
Perm(threads[t].observedred, 1/2) ** threads[t].observedred == NewBoolSeq(N) **
Perm(threads[t].pink, 1/2) ** threads[t].pink == NewBoolSeq(N) **
Perm(threads[t].exploringred, 1/2) ** !threads[t].exploringred **
Perm(threads[t].waiting, 1/2) ** !threads[t].waiting **
Perm(threads[t].term, 1/2) ** !threads[t].term **
Perm(threads[t].redroot, 1/2)
);
Master(int N_, seq<seq<boolean>> G_, seq<boolean> A_, int root, int nthreads) {
N = N_;
G = G_;
A = A_;
blueroot = root;
red = NewBoolSeq(N);
redowner = NewIntSeq(N);
threads = seq<Worker> { };
int tid = 0;
// construct the worker set
loop_invariant 0 <= tid && tid <= nthreads;
loop_invariant Perm(N, 1) ** N == N_;
loop_invariant Perm(G, 1) ** G == G_;
loop_invariant Perm(A, 1) ** A == A_;
loop_invariant Perm(red, 1) ** red == NewBoolSeq(N);
loop_invariant Perm(redowner, 1) ** redowner == NewIntSeq(N);
loop_invariant AdjacencyMatrix(N, G) && |A| == N;
loop_invariant 0 <= root && root < N;
loop_invariant Perm(blueroot, 1) ** blueroot == root;
loop_invariant Perm(threads, 1) ** |threads| == tid;
loop_invariant (\forall* int t; 0 <= t && t < tid;
Perm(threads[t].master, 1) ** threads[t].master == this **
Perm(threads[t].tid, 1) ** threads[t].tid == t **
Perm(threads[t].nthreads, 1) ** threads[t].nthreads == nthreads **
Perm(threads[t].color, 1) ** threads[t].color == NewIntSeq(N) **
Perm(threads[t].observedred, 1) ** threads[t].observedred == NewBoolSeq(N) **
Perm(threads[t].pink, 1) ** threads[t].pink == NewBoolSeq(N) **
Perm(threads[t].exploringred, 1) ** !threads[t].exploringred **
Perm(threads[t].waiting, 1) ** !threads[t].waiting **
Perm(threads[t].term, 1) ** !threads[t].term **
Perm(threads[t].redroot, 1)
);
loop_invariant (\forall int t; 0 <= t && t < tid;
ColorTypes(threads[t].color) &&
ThreadLocalInvariants(N, G, A, threads[t].color, threads[t].pink, red)
);
while (tid < nthreads) {
Worker thread = new Worker();
thread.master = this;
thread.tid = tid;
thread.nthreads = nthreads;
thread.color = NewIntSeq(N);
thread.observedred = NewBoolSeq(N);
thread.pink = NewBoolSeq(N);
thread.exploringred = false;
thread.waiting = false;
thread.term = false;
threads = threads + seq<Worker> { thread };
tid = tid + 1;
}
abort = false;
}
/* ** NDFS color codes */
static pure int white() = 0;
static pure int cyan() = 1;
static pure int blue() = 2;
/* ** NDFS return types */
static pure int notfound() = 0;
static pure int found() = 1;
static pure int aborted() = 2;
/** ** Default predicates */
// determines whether `G` is an adjacency matrix of size `N`
static inline pure boolean AdjacencyMatrix(int N, seq<seq<boolean>> G) =
|G| == N && (\forall seq<boolean> e; e in G; |e| == N);
// determines whether the given color set has the proper colors
static inline pure boolean ColorTypes(seq<int> color) =
(\forall int i; 0 <= i && i < |color|; color[i] == white() || color[i] == cyan() || color[i] == blue());
// determines whether `P` is a path from `x` to `y` in the graph `G`.
requires AdjacencyMatrix(N, G);
static pure boolean Path(int N, seq<seq<boolean>> G, int x, int y, seq<int> P) =
0 <= x && x < N && 0 <= y && y < N &&
0 < |P| && P[0] == x && P[|P| - 1] == y &&
(\forall int j; 0 <= j && j < |P|; 0 <= P[j] && P[j] < N) &&
(\forall int j; 0 <= j && j < |P| - 1; G[P[j]][P[j + 1]]);
// Shorthand notation for paths.
static pure inline boolean Path(int N, seq<seq<boolean>> G, seq<int> P) = Path(N, G, P[0], P[|P|-1], P);
// existential quantification over paths in `G` of length at least `len`.
requires AdjacencyMatrix(N, G);
static pure boolean ExPath(int N, seq<seq<boolean>> G, int x, int y, int len) =
(\exists seq<int> P; len <= |P|; Path(N, G, x, y, P));
// A _special path_ is defined to be a path `P` starting with a pink node and ending with a cyan node, so that all states on `P` are not red.
requires AdjacencyMatrix(N, G) && |color| == N && |pink| == N && |red| == N;
requires 1 < |P| && Path(N, G, P);
static pure boolean SpecialPath(int N, seq<seq<boolean>> G, seq<int> P, seq<int> color, seq<boolean> pink, seq<boolean> red) =
pink[P[0]] && color[P[|P| - 1]] == cyan() && (\forall int i; 0 <= i && i < |P|; !red[P[i]]);
// Existential quantification of "special" paths
requires AdjacencyMatrix(N, G) && |color| == N && |pink| == N && |red| == N;
static pure boolean ExSpecialPath(int N, seq<seq<boolean>> G, seq<int> color, seq<boolean> pink, seq<boolean> red) =
(\exists seq<int> P; 1 < |P| && Path(N, G, P); SpecialPath(N, G, P, color, pink, red));
// Determines the existence of an accepting cycle from `root` to the accepting state `a`
requires AdjacencyMatrix(N, G) && |A| == N;
requires 0 <= root && root < N;
requires 0 <= a && a < N && A[a];
static pure boolean ExAccCycle(int N, seq<seq<boolean>> G, seq<boolean> A, int root, int a) =
ExPath(N, G, root, a, 1) && ExPath(N, G, a, a, 2);
/* ** Default operations */
// some workaround definitions
static inline pure boolean GetBool(seq<boolean> xs, int i) = xs[i];
static inline pure int GetInt(seq<int> xs, int i) = xs[i];
requires 0 <= i && i < |xs|;
ensures |\result| == |xs|;
ensures \result[i] == v;
ensures (\forall int j; 0 <= j && j < |xs| && j != i; \result[j] == xs[j]);
pure static seq<int> UpdateInt(seq<int> xs, int i, int v) =
0 < i ? seq<int> { head(xs) } + UpdateInt(tail(xs), i - 1, v) : seq<int> { v } + tail(xs);
requires 0 <= i && i < |xs|;
ensures |\result| == |xs|;
ensures \result[i] == v;
ensures (\forall int j; 0 <= j && j < |xs| && j != i; \result[j] == xs[j]);
pure static seq<boolean> UpdateBool(seq<boolean> xs, int i, boolean v) =
0 < i ? seq<boolean> { head(xs) } + UpdateBool(tail(xs), i - 1, v) : seq<boolean> { v } + tail(xs);
// constructs a fresh color set containing `n` white colors.
requires 0 <= n;
ensures |\result| == n;
ensures (\forall int i; 0 <= i && i < n; \result[i] == white());
pure static seq<int> NewIntSeq(int n) =
0 < n ? seq<int> { white() } + NewIntSeq(n - 1) : seq<int> { };
// constructs a fresh color set containing `n` white colors.
requires 0 <= n;
ensures |\result| == n;
ensures (\forall int i; 0 <= i && i < n; !\result[i]);
pure static seq<boolean> NewBoolSeq(int n) =
0 < n ? seq<boolean> { false } + NewBoolSeq(n - 1) : seq<boolean> { };
// skips the first `n` elements of the integer sequence `xs`.
requires 0 <= n && n <= |xs|;
ensures |\result| == |xs| - n;
ensures (\forall int i; 0 <= i && i < |xs| - n; xs[n + i] == \result[i]);
pure static seq<int> SkipInt(seq<int> xs, int n) =
0 < n ? SkipInt(tail(xs), n - 1) : xs;
// takes the first `n` elements of the integer sequence `xs`.
requires 0 <= n && n <= |xs|;
ensures |\result| == n;
ensures (\forall int i; 0 <= i && i < n; xs[i] == \result[i]);
pure static seq<int> TakeInt(seq<int> xs, int n) =
0 < n ? seq<int> { head(xs) } + TakeInt(tail(xs), n - 1) : seq<int> { };
// Helper method to extract a color matrix from a set of workers.
requires 0 <= j && j <= |threads|;
requires (\forall* int i; 0 <= i && i < |threads|; threads[i] != null ** Perm(threads[i].color, 1/2));
ensures |\result| == |threads| - j;
ensures (\forall int i; 0 <= i && i < |\result|; \result[i] == threads[j + i].color);
static pure seq<seq<int>> ExtractColorsHelper(seq<Worker> threads, int j) =
j < |threads| ?
seq<seq<int>> { threads[j].color } + ExtractColorsHelper(threads, j + 1) :
seq<seq<int>> { };
// Helper method to extract a pink matrix from a set of workers.
requires 0 <= j && j <= |threads|;
requires (\forall* int i; 0 <= i && i < |threads|; threads[i] != null ** Perm(threads[i].pink, 1/2));
ensures |\result| == |threads| - j;
ensures (\forall int i; 0 <= i && i < |\result|; \result[i] == threads[j + i].pink);
static pure seq<seq<boolean>> ExtractPinkHelper(seq<Worker> threads, int j) =
j < |threads| ?
seq<seq<boolean>> { threads[j].pink } + ExtractPinkHelper(threads, j + 1) :
seq<seq<boolean>> { };
// Gives a matrix representation of the colors of the given set of workers.
requires (\forall* int i; 0 <= i && i < |threads|; threads[i] != null ** Perm(threads[i].color, 1/2));
ensures |\result| == |threads|;
ensures (\forall int i; 0 <= i && i < |threads|; \result[i] == threads[i].color);
static pure seq<seq<int>> ExtractColors(seq<Worker> threads) = ExtractColorsHelper(threads, 0);
// Gives a matrix representation of the pink colorings of the given set of workers.
requires (\forall* int i; 0 <= i && i < |threads|; threads[i] != null ** Perm(threads[i].pink, 1/2));
ensures |\result| == |threads|;
ensures (\forall int i; 0 <= i && i < |threads|; \result[i] == threads[i].pink);
static pure seq<seq<boolean>> ExtractPink(seq<Worker> threads) = ExtractPinkHelper(threads, 0);
/* ** Standard properties of paths */
requires AdjacencyMatrix(N, G) && Path(N, G, x, y, P);
ensures \result && ExPath(N, G, x, y, |P|);
pure static boolean lemma_expath(int N, seq<seq<boolean>> G, int x, int y, seq<int> P) = true;
requires AdjacencyMatrix(N, G) && Path(N, G, x, y, P);
ensures ExPath(N, G, x, y, |P|);
static void lemma_expath_m(int N, seq<seq<boolean>> G, int x, int y, seq<int> P) {
assert lemma_expath(N, G, x, y, P);
}
requires AdjacencyMatrix(N, G);
requires Path(N, G, s, t, P);
requires Path(N, G, t, u, Q);
ensures \result && Path(N, G, s, u, P + tail(Q));
pure static boolean lemma_path_trans(int N, seq<seq<boolean>> G, int s, int t, int u, seq<int> P, seq<int> Q) = true;
requires AdjacencyMatrix(N, G);
requires ExPath(N, G, s, t, len1);
requires ExPath(N, G, t, u, len2);
ensures \result && ExPath(N, G, s, u, len1 + len2 - 1);
pure static boolean lemma_expath_trans(int N, seq<seq<boolean>> G, int s, int t, int u, int len1, int len2) =
(\forall seq<int> P; Path(N, G, s, t, P);
(\forall seq<int> Q; Path(N, G, t, u, Q);
lemma_path_trans(N, G, s, t, u, P, Q)));
requires AdjacencyMatrix(N, G);
requires ExPath(N, G, s, t, len1);
requires ExPath(N, G, t, u, len2);
ensures ExPath(N, G, s, u, len1 + len2 - 1);
static void lemma_expath_trans_m(int N, seq<seq<boolean>> G, int s, int t, int u, int len1, int len2) {
assert lemma_expath_trans(N, G, s, t, u, len1, len2);
}
requires AdjacencyMatrix(N, G);
requires 0 <= s && s < N;
requires 0 <= t && t < N;
requires G[s][t];
ensures \result && ExPath(N, G, s, t, 2);
pure static boolean lemma_edge_path(int N, seq<seq<boolean>> G, int s, int t) =
Path(N, G, s, t, seq<int> { s, t });
requires AdjacencyMatrix(N, G);
requires 0 <= s && s < N;
requires 0 <= t && t < N;
requires G[s][t];
ensures ExPath(N, G, s, t, 2);
static void lemma_edge_path_m(int N, seq<seq<boolean>> G, int s, int t) {
assert lemma_edge_path(N, G, s, t);
}
requires AdjacencyMatrix(N, G) && |color| == N && |pink| == N && |red| == N;
requires 1 < |P| && Path(N, G, P);
requires SpecialPath(N, G, P, color, pink, red);
ensures ExSpecialPath(N, G, color, pink, red);
ensures \result;
static pure boolean lemma_specialpath_ex(int N, seq<seq<boolean>> G, seq<int> color, seq<boolean> pink, seq<boolean> red, seq<int> P) = true;
/* ** Auxiliary lemmas */
// Lemma: If `P` is an `(x,y)`-path, `x` is blue and all successors of blue states are blue, then `y` must consequently also be blue.
requires AdjacencyMatrix(N, G) && |C1| == N;
requires Path(N, G, x, y, P);
requires C1[x] == blue();
requires (\forall int v; 0 <= v && v < N && C1[v] == blue(); (\forall int w; 0 <= w && w < N && G[v][w]; C1[w] == blue())); // `Next(Blue, Blue)`
ensures \result && C1[y] == blue();