-
Notifications
You must be signed in to change notification settings - Fork 0
/
Schorr-Waite.v
1407 lines (1153 loc) · 49.4 KB
/
Schorr-Waite.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
(* ============================================================================
The Schorr-Waite algorithm verified with Bedrock library
(the Bedrock library can be found at http://plv.csail.mit.edu/bedrock/)
Authors: Duckki Oe and Adam Chlipala
Date: 7/15/2013
* ========================================================================= *)
Set Implicit Arguments.
Require Import AutoSep Bags Sets SepPure.
(* ============================================================================
* definitions for abstract object graphs
* ========================================================================= *)
Record obj := {
Address : W;
Mark : bool;
FirstChild : W;
SecondChild : W
}.
Definition mark (o : obj) : obj := {|
Address := Address o;
Mark := true;
FirstChild := FirstChild o;
SecondChild := SecondChild o
|}.
Module Obj_Key.
Definition A := obj.
Theorem eq_dec : forall x y : A, {x = y} + {x <> y}.
decide equality; try apply weq; decide equality.
Qed.
End Obj_Key.
Module Obj_Set := Make(Obj_Key).
Import Obj_Set.
Export Obj_Set.
Definition noNull g := forall o, Address o = 0 -> ~ o %in g.
Definition functional (g : set) :=
forall o1 o2, o1 %in g -> o2 %in g -> Address o1 = Address o2 -> o1 = o2.
Definition inDom (p : W) g := exists o, o %in g /\ Address o = p.
Definition Ptr (p : W) (g : set) := p = 0 \/ inDom p g.
Definition closed (g : set) :=
forall o, o %in g -> Ptr (FirstChild o) g /\ Ptr (SecondChild o) g.
Definition heapInv g := noNull g /\ functional g /\ closed g.
(* ============================================================================
* graph data structure
* ========================================================================= *)
Definition objTag (b : bool) : W := if b then 2 else 0.
Definition objP (o : obj) : HProp :=
(Address o ==*> objTag (Mark o), FirstChild o, SecondChild o)%Sep.
Module Type GRAPH.
Parameter graph : set -> HProp.
Axiom graph_intro : forall g, heapInv g
-> starS objP g ===> graph g.
Axiom graph_elim : forall g, graph g ===> [| heapInv g |] * starS objP g.
End GRAPH.
Module Graph : GRAPH.
Open Scope Sep_scope.
Definition graph (g : set) := [| heapInv g |] * starS objP g.
Theorem graph_intro : forall g, heapInv g
-> starS objP g ===> graph g.
unfold graph; simpl; intros.
eapply Himp_trans; [eapply Himp_star_Emp' | ].
eapply Himp_star_frame; try apply Himp_refl; sepLemma.
Qed.
Theorem graph_elim : forall g, graph g ===> [| heapInv g |] * starS objP g.
unfold graph; simpl; intros; apply Himp_refl.
Qed.
End Graph.
Import Graph.
Export Graph.
(* ============================================================================
* function specification
* ========================================================================= *)
Section reachable.
Variable g : set.
Inductive reachable : W -> obj -> Prop :=
| ReachEq : forall o, o %in g -> Mark o = false
-> reachable (Address o) o
| ReachFirst : forall o o', o %in g -> Mark o = false
-> reachable (FirstChild o) o'
-> reachable (Address o) o'
| ReachSecond : forall o o', o %in g -> Mark o = false
-> reachable (SecondChild o) o'
-> reachable (Address o) o'.
Definition marked (root : W) (g' : set) :=
forall o, reachable root o -> mark o %in g'.
End reachable.
Definition markS := SPEC("root") reserving 2
Al g,
PRE[V] graph g * [| Ptr (V "root") g |]
POST[_] Ex g', graph g' * [| marked g (V "root") g' |].
(* ============================================================================
* Set lemmas
* ========================================================================= *)
Lemma mem_add_set : forall p q s, p %in s -> p %in s %+ q.
sets.
Qed.
Lemma neq_mem_del : forall p q s, p <> q -> p %in s -> p %in s %- q.
sets.
Qed.
Lemma neq_mem_add : forall p q s, p <> q -> p %in s %+ q -> p %in s.
sets.
Qed.
Hint Resolve mem_add_set neq_mem_del.
(* ============================================================================
* graph lemmas
* ========================================================================= *)
Lemma heapInv_noNull : forall g, heapInv g -> noNull g.
inversion 1; tauto.
Qed.
Lemma heapInv_functional : forall g, heapInv g -> functional g.
inversion 1; tauto.
Qed.
Lemma heapInv_closed : forall g, heapInv g -> closed g.
inversion 1; tauto.
Qed.
Hint Immediate heapInv_noNull heapInv_functional heapInv_closed.
Lemma null_Ptr : forall (p : W) g, p = 0 -> Ptr p g.
left; auto.
Qed.
Hint Immediate null_Ptr.
Lemma inDom_Ptr : forall (p : W) g, inDom p g -> Ptr p g.
right; auto.
Qed.
Hint Resolve inDom_Ptr.
Lemma Ptr_FirstChild : forall g x, closed g -> x %in g -> Ptr (FirstChild x) g.
intros; apply H; auto.
Qed.
Lemma Ptr_SecondChild : forall g x, closed g -> x %in g -> Ptr (SecondChild x) g.
intros; apply H; auto.
Qed.
Local Hint Resolve Ptr_FirstChild Ptr_SecondChild.
Lemma mem_inDom : forall o p g, Address o = p -> o %in g -> inDom p g.
red; intros; eauto 3.
Qed.
Local Hint Immediate mem_inDom.
Lemma noNull_contra : forall g o, Address o = 0 -> noNull g -> o %in g -> False.
intros; contradict H1; auto.
Qed.
Ltac ptrs :=
match goal with
| _ => solve [auto]
| H: _ /\ _ |- _ => destruct H
| H: inDom _ _ |- _ => destruct H as [? [] ]
| H: ?o %in ?g, H1: forall _, _ |- _ => specialize (H1 _ H eq_refl)
| |- _ => progress subst
| H: Address ?x = Address ?x |- _ => clear H
| H: Address ?x = Address ?y |- _ => assert (x = y) by auto
end.
Lemma mark_not_in_del : forall g o, o %in g -> functional g
-> ~ mark o %in (g %- o).
intros; intro.
assert (Address o = Address (mark o)) by auto; ptrs.
rewrite <- H3 in *; destruct H1; congruence.
Qed.
Local Hint Resolve mark_not_in_del.
(*=============================================================================
* updMark operator
*===========================================================================*)
Definition updMark g o := (g %- o) %+ mark o.
Infix "%^" := updMark (at level 71, no associativity).
Lemma updMark_neq_mem : forall g o x, x %in g -> o <> x
-> x %in (g %^ o).
unfold "%^"; auto.
Qed.
Lemma updMark_neq_addr_mem : forall g o x, x %in g -> Address o <> Address x
-> x %in (g %^ o).
unfold "%^"; auto.
Qed.
Local Hint Resolve updMark_neq_mem updMark_neq_addr_mem.
Lemma updMark_mark_mem : forall g o, mark o %in (g %^ o).
unfold "%^" in *; intros; sets.
Qed.
Local Hint Resolve updMark_mark_mem.
Lemma updMark_neq_mem' : forall g o x, x %in (g %^ o) -> x <> mark o -> x %in g .
intros; apply neq_mem_add in H; auto.
Qed.
Local Hint Resolve updMark_neq_mem'.
Lemma updMark_noNull : forall g o, noNull g -> Address o <> natToW 0
-> noNull (g %^ o).
red; intros; intro.
destruct (Obj_Key.eq_dec o0 (mark o)); subst; auto.
assert (o0 %in g) by eauto; eapply noNull_contra; eauto.
Qed.
Local Hint Resolve updMark_noNull.
Lemma updMark_functional : forall g o, o %in g -> functional g
-> functional (g %^ o).
unfold functional; intros.
destruct (Obj_Key.eq_dec o1 (mark o)); subst.
destruct (Obj_Key.eq_dec o2 (mark o)); subst; auto.
{
exfalso; simpl in *.
assert (o2 %in g) by eauto 3; repeat ptrs.
apply neq_mem_add in H2; auto; sets.
}
{
destruct (Obj_Key.eq_dec o2 (mark o)); subst.
{
exfalso; simpl in *.
assert (o1 %in g) by eauto 3; repeat ptrs.
apply neq_mem_add in H1; auto; sets.
}
apply neq_mem_add in H1; apply neq_mem_add in H2; auto.
}
Qed.
Local Hint Resolve updMark_functional.
Lemma updMark_inDom : forall w g o, inDom w g -> inDom w (g %^ o).
intros; repeat ptrs.
destruct (Obj_Key.eq_dec x o); subst.
exists (mark o); eauto.
exists x; eauto.
Qed.
Local Hint Resolve updMark_inDom.
Lemma updMark_Ptr : forall w g o, Ptr w g -> Ptr w (g %^ o).
destruct 1; auto.
Qed.
Local Hint Resolve updMark_Ptr.
Lemma updMark_closed : forall g o, o %in g -> closed g -> closed (g %^ o).
red; intros.
destruct (Obj_Key.eq_dec o0 (mark o)); subst; simpl; auto.
apply neq_mem_add in H1; auto.
Qed.
Local Hint Resolve updMark_closed.
(* ============================================================================
* [loop invariant] graph data structure
* ========================================================================= *)
Module Type GRAPH'.
Parameter objects : list W -> set -> HProp.
Parameter graph' : list W -> set -> HProp.
Axiom graph_fwd : forall g, graph g ===> graph' nil g.
Axiom graph_bwd : forall g, graph' nil g ===> graph g.
Axiom graph'_fwd : forall s g,
graph' s g ===> [| heapInv g |] * objects s g.
Axiom graph'_bwd : forall s g,
[| heapInv g |] * objects s g ===> graph' s g.
Definition objects_pick (_ : W) := objects.
Axiom objects_reveal : forall (p : W) s g, p <> 0 -> Ptr p g -> ~ In p s
-> objects_pick p s g
===> Ex o, [| o %in g |] * [| Address o = p |]
* (p ==*> objTag (Mark o), FirstChild o, SecondChild o)
* objects s (g %- o).
Definition objects_filled (_:obj) (_:W) := objects.
Axiom objects_fill : forall o p s g, Address o = p -> o %in g -> ~ In p s
-> objects s (g %- o)
* (Address o ==*> objTag (Mark o), FirstChild o, SecondChild o)
===> objects_filled o p s g.
Definition objects_pushed (o : obj) := objects.
Axiom objects_push : forall o p s g, Address o = p -> o %in g -> heapInv g
-> objects s (g %- o) ===> objects_pushed o (p :: s) g.
Definition objects_popped (_:obj) (_:W) := objects.
Axiom objects_pop : forall o p s g, heapInv g
-> Address o = p -> o %in g -> NoDup (p :: s)
-> objects (p :: s) g
* (Address o ==*> objTag (Mark o), FirstChild o, SecondChild o)
===> objects_popped o p s g.
Axiom objects_mark : forall p s g o, Address o = p -> o %in g -> heapInv g
-> objects (p :: s) g ===> objects (p :: s) (g %^ o).
End GRAPH'.
Module Graph' : GRAPH'.
Open Scope Sep_scope.
Definition object (s : list W) (o : obj) : HProp :=
if In_dec (@weq _) (Address o) s
then Emp
else Address o ==*> objTag (Mark o), FirstChild o, SecondChild o.
Definition objects s := starS (object s).
Definition graph' (s : list W) (g : set) := [| heapInv g |] * objects s g.
Local Hint Resolve Himp_refl.
Theorem graph_fwd : forall g, graph g ===> graph' nil g.
unfold graph'; intros; eapply Himp_trans; [apply graph_elim | ]; auto.
Qed.
Theorem graph_bwd : forall g, graph' nil g ===> graph g.
unfold graph'; sepLemma; eapply Himp_trans; [ | apply graph_intro; auto]; auto.
Qed.
Theorem graph'_fwd : forall s g, graph' s g ===> [| heapInv g |] * objects s g.
unfold graph'; sepLemma.
Qed.
Theorem graph'_bwd : forall s g, [| heapInv g |] * objects s g ===> graph' s g.
unfold graph'; sepLemma.
Qed.
(* --------------------------------------------------------------------------
* accessing a pointer that is not in the stack
* ----------------------------------------------------------------------- *)
Lemma objects_reveal_h : forall o s g, o %in g
-> objects s g ===> object s o * objects s (g %- o).
intros; apply starS_del_fwd; auto.
Qed.
Definition objects_pick (_ : W) := objects.
Theorem objects_reveal : forall (p : W) s g, p <> 0 -> Ptr p g -> ~ In p s
-> objects_pick p s g
===> Ex o, [| o %in g |] * [| Address o = p |]
* (p ==*> objTag (Mark o), FirstChild o, SecondChild o)
* objects s (g %- o).
unfold objects_pick; intros.
destruct H0 as [ | [? [] ] ]; try congruence; subst.
eapply Himp_trans; [apply objects_reveal_h; eauto | ].
unfold object; destruct (In_dec (@weq _) (Address x) s); sepLemma.
Qed.
Lemma object_not_in_stack_bwd : forall o s, ~ In (Address o) s
-> (Address o ==*> objTag (Mark o), FirstChild o, SecondChild o)
===> object s o.
unfold object; intros.
destruct (in_dec (@weq _) (Address o) s); try tauto; auto.
Qed.
Local Hint Resolve object_not_in_stack_bwd.
Definition objects_filled (_:obj) (_:W) := objects.
Local Hint Resolve Himp_star_comm Himp_star_frame.
Local Hint Resolve Himp_trans starS_del_bwd.
Theorem objects_fill : forall o p s g, Address o = p -> o %in g -> ~ In p s
-> objects s (g %- o)
* (Address o ==*> objTag (Mark o), FirstChild o, SecondChild o)
===> objects_filled o p s g.
unfold objects; intros; subst.
eapply Himp_trans; [ | eapply starS_del_bwd]; eauto.
Qed.
(* --------------------------------------------------------------------------
* pushing an object into the stack
* ----------------------------------------------------------------------- *)
Lemma object_head : forall o s, object (Address o :: s) o = Emp.
unfold object; simpl; intros.
destruct (weq (Address o) (Address o)); intuition.
Qed.
Lemma objects_push_h : forall o p s g,
Address o = p -> o %in g -> heapInv g
-> objects s (g %- o) ===> objects (p :: s) g.
intros; subst.
eapply Himp_trans; [ | eapply starS_del_bwd ]; eauto.
rewrite object_head.
eapply Himp_trans; [ | apply Himp_star_Emp' ].
apply starS_weaken; intros.
assert (functional g) by auto.
unfold object; simpl.
destruct (weq (Address o) (Address x)).
solve [repeat ptrs; sets].
destruct (in_dec (@weq _) (Address x) s); auto.
Qed.
Definition objects_pushed (o : obj) := objects.
Theorem objects_push : forall o p s g, Address o = p -> o %in g -> heapInv g
-> objects s (g %- o) ===> objects_pushed o (p :: s) g.
intros; eapply Himp_trans; [ | apply objects_push_h; auto]; sepLemma.
Qed.
(* --------------------------------------------------------------------------
* popping an object out of the stack
* ----------------------------------------------------------------------- *)
Lemma objects_pop_h : forall o p s g, heapInv g
-> o %in g -> Address o = p -> ~ In p s
-> objects (p :: s) g ===> objects s (g %- o).
intros; subst.
eapply Himp_trans; [ eapply starS_del_fwd; eauto| ].
eapply Himp_trans; [apply Himp_star_frame | ].
rewrite object_head; apply Himp_refl.
2: apply Himp_star_Emp.
apply starS_weaken; intros.
assert (functional g) by auto.
unfold object.
destruct (in_dec (@weq _) (Address x) (Address o :: s)),
(in_dec (@weq _) (Address x) s); try solve [sepLemma].
solve [exfalso; inversion_clear i; try tauto; repeat ptrs; sets].
destruct n; try tauto; simpl; auto.
Qed.
Definition objects_popped (_:obj) (_:W) := objects.
Theorem objects_pop : forall o p s g, heapInv g
-> Address o = p -> o %in g -> NoDup (p :: s)
-> objects (p :: s) g
* (Address o ==*> objTag (Mark o), FirstChild o, SecondChild o)
===> objects_popped o p s g.
unfold objects_popped; inversion_clear 4; intros; subst.
eapply Himp_trans; [ | eapply starS_del_bwd; eauto].
eapply Himp_trans; [apply Himp_star_comm | ].
apply Himp_star_frame, objects_pop_h; auto.
Qed.
(* --------------------------------------------------------------------------
* marking an object in the stack
* ----------------------------------------------------------------------- *)
Lemma mark_object_in_stack : forall o s, In (Address o) s
-> object s o = object s (mark o).
unfold object; intros.
destruct (in_dec (@weq _) (Address o) s),
(in_dec (@weq _) (Address (mark o)) s); tauto.
Qed.
Lemma objects_mark_h : forall s g o, heapInv g
-> o %in g -> In (Address o) s
-> objects s g ===> objects s (g %^ o).
intros.
eapply Himp_trans; [apply objects_reveal_h; eauto | ].
eapply Himp_trans; [apply Himp_star_frame; [ | apply Himp_refl] | ].
rewrite mark_object_in_stack by auto; auto.
eapply Himp_trans; [ | apply starS_add_bwd; auto]; auto.
Qed.
Theorem objects_mark : forall p s g o, Address o = p -> o %in g -> heapInv g
-> objects (p :: s) g ===> objects (p :: s) (g %^ o).
intros; apply objects_mark_h; auto; simpl; auto.
Qed.
End Graph'.
Import Graph'.
Export Graph'.
(* ============================================================================
* stack data structure
* ========================================================================= *)
Definition stackInGraph (s : list W) (g : set) :=
List.Forall (fun x => inDom x g) s.
Module Type STACK.
Definition stackTag (b : bool) : W := if b then 2 else 1.
Parameter stack : W -> list W -> set -> W -> HProp.
Parameter stack' : W -> list W -> set -> W -> obj -> W -> W -> HProp.
Axiom stack_pure : forall g s last p,
stack last s g p ===> [| stackInGraph s g |] * stack last s g p.
Axiom stack_nil_fwd : forall last s g (p : W), p = 0
-> stack last s g p ===> [| s = nil |].
Axiom stack_nil_bwd : forall last s g (p : W), p = 0
-> [| s = nil |] ===> stack last s g p.
Axiom stack_cons_fwd : forall last s g (p : W), p <> 0
-> stack last s g p
===> (Ex s', [| s = p :: s' |] * [| p <> 0 |]
* Ex o, [| o %in g |] * [| Address o = p |]
* Ex p1, Ex p2, (p ==*> stackTag (Mark o), p1, p2)
* stack' last s' g p o p1 p2).
Axiom stack_cons_bwd : forall last s g (p : W), p <> 0
-> (Ex s', [| s = p :: s' |] * [| p <> 0 |]
* Ex o, [| o %in g |] * [| Address o = p |]
* Ex p1, Ex p2, (p ==*> stackTag (Mark o), p1, p2)
* stack' last s' g p o p1 p2)
===> stack last s g p.
Axiom stack'_true_fwd : forall last p o p1 p2 s g, Mark o = true
-> stack' last s g p o p1 p2
===> [| p1 = FirstChild o |] * [| last = SecondChild o |] * stack p s g p2.
Axiom stack'_false_fwd : forall last p o p1 p2 s g, Mark o = false
-> stack' last s g p o p1 p2
===> [| last = FirstChild o |] * [| p2 = SecondChild o |] * stack p s g p1.
Axiom stack'_true_bwd : forall last p o p1 p2 s g, Mark o = true
-> [| p1 = FirstChild o |] * [| last = SecondChild o |] * stack p s g p2
===> stack' last s g p o p1 p2.
Axiom stack'_false_bwd : forall last p o p1 p2 s g, Mark o = false
-> [| last = FirstChild o |] * [| p2 = SecondChild o |] * stack p s g p1
===> stack' last s g p o p1 p2.
Axiom stack'_mark_bwd : forall last p o p1 p2 s g,
[| p1 = FirstChild o |] * [| last = SecondChild o |] * stack p s g p2
===> stack' last s g p (mark o) p1 p2.
Axiom stack_mark : forall s q g p o, NoDup (q :: s) -> Address o = q
-> stack q s g p ===> stack q s (g %^ o) p.
End STACK.
Module Stack : STACK.
Open Scope Sep_scope.
Definition stackTag (b : bool) : W := if b then 2 else 1.
Fixpoint stack (last : W) (s : list W) (g : set) (p : W) : HProp :=
match s with
| nil => [| p = 0 |]
| p' :: s => [| p' = p |] * [| p <> 0 |]
* Ex o, [| o %in g |] * [| Address o = p |]
* Ex p1, Ex p2, (p ==*> stackTag (Mark o), p1, p2)
* if Mark o
then [| p1 = FirstChild o |] * [| last = SecondChild o |] * stack p s g p2
else [| last = FirstChild o |] * [| p2 = SecondChild o |] * stack p s g p1
end.
Definition stack' (last : W) s g p (o : obj) (p1 p2 : W) :=
if Mark o
then [| p1 = FirstChild o |] * [| last = SecondChild o |] * stack p s g p2
else [| last = FirstChild o |] * [| p2 = SecondChild o |] * stack p s g p1.
Ltac stack' := match goal with
| [ |- context C[ (Ex o, [| o %in ?g |] * [| Address o = ?p |]
* Ex p1, Ex p2, ?p =*> stackTag (Mark o) * ((?p ^+ $4) =*> p1 * (?p ^+ $8) =*> p2)
* if Mark o then [| p1 = FirstChild o |] * [| ?last = SecondChild o |] * stack ?p ?s ?g p2
else [| ?last = FirstChild o |] * [| p2 = SecondChild o |] * stack ?p ?s ?g p1)%Sep ] ] =>
let E := context C[(Ex o, [| o %in g |] * [| Address o = p |]
* Ex p1, Ex p2, p =*> stackTag (Mark o) * ((p ^+ $4) =*> p1 * (p ^+ $8) =*> p2)
* stack' last s g p o p1 p2)%Sep] in
change E
end.
Local Hint Resolve Himp_refl.
Theorem stack_pure : forall g s last p,
stack last s g p ===> [| stackInGraph s g |] * stack last s g p.
induction s; simpl; intros.
solve [sepLemma; constructor].
sepPure.
destruct (Mark x) eqn:?; simpl; sepPure.
{
eapply Himp_trans.
instantiate (1:=stack p s g x1 * (p ==*> natToW 2, x0, x1)); sepLemma.
eapply Himp_trans; [apply Himp_star_frame; [apply IHs | ] | ]; auto.
stack'; sepLemma; eauto; try apply Forall_cons; eauto.
unfold stack'; rewrite Heqb; sepLemma.
}
{
eapply Himp_trans.
instantiate (1:=stack p s g x0 * (p ==*> natToW 1, x0, x1)); sepLemma.
eapply Himp_trans; [apply Himp_star_frame; [apply IHs | ] | ]; auto.
stack'; sepLemma; eauto; try apply Forall_cons; eauto.
unfold stack'; rewrite Heqb; sepLemma.
}
Qed.
Theorem stack_nil_fwd : forall last s g (p : W), p = 0
-> stack last s g p ===> [| s = nil |].
destruct s; try (simpl; intros; stack'); sepLemma.
Qed.
Theorem stack_nil_bwd : forall last s g (p : W), p = 0
-> [| s = nil |] ===> stack last s g p.
do 2 sepLemma.
Qed.
Theorem stack_cons_fwd : forall last s g (p : W), p <> 0
-> stack last s g p
===> (Ex s', [| s = p :: s' |] * [| p <> 0 |]
* Ex o, [| o %in g |] * [| Address o = p |]
* Ex p1, Ex p2, (p ==*> stackTag (Mark o), p1, p2)
* stack' last s' g p o p1 p2).
destruct s; try (simpl; intros; stack'); sepLemma.
Qed.
Theorem stack_cons_bwd : forall last s g (p : W), p <> 0
-> (Ex s', [| s = p :: s' |] * [| p <> 0 |]
* Ex o, [| o %in g |] * [| Address o = p |]
* Ex p1, Ex p2, (p ==*> stackTag (Mark o), p1, p2)
* stack' last s' g p o p1 p2)
===> stack last s g p.
destruct s; try (simpl; intros; stack'); sepLemma; eauto;
match goal with
| [ H : _ :: _ = _ :: _ |- _ ] => injection H; clear H; subst; sepLemma
end.
Qed.
Theorem stack'_true_fwd : forall last p o p1 p2 s g, Mark o = true
-> stack' last s g p o p1 p2
===> [| p1 = FirstChild o |] * [| last = SecondChild o |] * stack p s g p2.
unfold stack'; intros; destruct (Mark o) eqn:?; sepLemma.
Qed.
Theorem stack'_false_fwd : forall last p o p1 p2 s g, Mark o = false
-> stack' last s g p o p1 p2
===> [| last = FirstChild o |] * [| p2 = SecondChild o |] * stack p s g p1.
unfold stack'; intros; destruct (Mark o) eqn:?; sepLemma.
Qed.
Theorem stack'_true_bwd : forall last p o p1 p2 s g, Mark o = true
-> [| p1 = FirstChild o |] * [| last = SecondChild o |] * stack p s g p2
===> stack' last s g p o p1 p2.
unfold stack'; intros; destruct (Mark o) eqn:?; sepLemma.
Qed.
Theorem stack'_false_bwd : forall last p o p1 p2 s g, Mark o = false
-> [| last = FirstChild o |] * [| p2 = SecondChild o |] * stack p s g p1
===> stack' last s g p o p1 p2.
unfold stack'; intros; destruct (Mark o) eqn:?; sepLemma.
Qed.
Theorem stack'_mark_bwd : forall last p o p1 p2 s g,
[| p1 = FirstChild o |] * [| last = SecondChild o |] * stack p s g p2
===> stack' last s g p (mark o) p1 p2.
unfold stack'; intros; destruct (Mark o) eqn:?; sepLemma.
Qed.
Lemma stack_mark_h : forall s q g p o, ~ In (Address o) s
-> stack q s g p ===> stack q s (g %^ o) p.
induction s; simpl; try tauto; intros; auto.
repeat stack'; sepLemma.
unfold stack'; destruct (Mark x1) eqn:?; sepLemma.
Qed.
Theorem stack_mark : forall s q g p o, NoDup (q :: s) -> Address o = q
-> stack q s g p ===> stack q s (g %^ o) p.
inversion_clear 1; intros; subst; apply stack_mark_h; auto.
Qed.
End Stack.
Import Stack.
(*=============================================================================
* stack with a hole
*===========================================================================*)
Definition hints_stack : TacPackage.
prepare (stack_nil_fwd, stack_cons_fwd, stack'_true_fwd, stack'_false_fwd)
(stack_nil_bwd, stack_cons_bwd, stack'_false_bwd, stack'_true_bwd).
Defined.
Fixpoint stackH (q : W) (last : W) (s : list W) (g : set) (p : W) : HProp :=
match s with
| nil => [| False |]
| p' :: s
=> [| p' = p |] * [| p <> 0 |]
* Ex o, [| o %in g |] * [| Address o = p |]
* if weq q p then
Ex p1, Ex p2, (p ^+ $4) =*> p1 * (p ^+ $8) =*> p2
* stack' last s g p o p1 p2
else
Ex p1, Ex p2, (p ==*> stackTag (Mark o), p1, p2)
* if Mark o
then [| p1 = FirstChild o |] * [| last = SecondChild o |]
* stackH q p s g p2
else [| last = FirstChild o |] * [| p2 = SecondChild o |]
* stackH q p s g p1
end%Sep.
Definition stackH' q last s g p o p1 p2 : HProp :=
(if Mark o
then [| p1 = FirstChild o |] * [| last = SecondChild o |]
* stackH q p s g p2
else [| last = FirstChild o |] * [| p2 = SecondChild o |]
* stackH q p s g p1)%Sep.
Lemma stackH_eq_bwd : forall q last s g p, q = p
-> (Ex s' : list W, [| s = p :: s' |] * [| p <> 0 |]
* Ex o, [| o %in g |] * [| Address o = p |]
* Ex p1, Ex p2, (p ^+ $4) =*> p1 * (p ^+ $8) =*> p2
* stack' last s' g p o p1 p2)
===> stackH q last s g p.
destruct s; intros; subst; simpl; [sepLemma | ].
destruct (weq p p); try congruence; sepLemma; eauto 2.
inversion H; clear H; subst; sepLemma.
Qed.
Ltac fold_stackH' :=
match goal with
| [ |- context C[((Ex o : Obj_Key.A,
[|o %in ?g|] * [|Address o = ?p|] *
((Ex p1 : W,
(Ex p2 : W,
?p =*> stackTag (Mark o) * ((?p ^+ $ (4)) =*> p1 * (?p ^+ $ (8)) =*> p2) *
(if Mark o
then
[|p1 = FirstChild o|] * [|?last = SecondChild o|] *
stackH ?q ?p ?s ?g p2
else
[|?last = FirstChild o|] * [|p2 = SecondChild o|] *
stackH ?q ?p ?s ?g p1))))))%Sep] ] =>
let E := context C[((Ex o : Obj_Key.A,
[|o %in g|] * [|Address o = p|] *
((Ex p1 : W,
(Ex p2 : W,
p =*> stackTag (Mark o) * ((p ^+ $ (4)) =*> p1 * (p ^+ $ (8)) =*> p2) *
stackH' q last s g p o p1 p2)))))%Sep] in
change E
end.
Lemma stackH_neq_fwd : forall q last s g p, q <> p -> In q s
-> stackH q last s g p
===> Ex s', [| s = p :: s' |] * [| p <> 0 |]
* Ex o, [| o %in g |] * [| Address o = p |]
* Ex p1, Ex p2, (p ==*> stackTag (Mark o), p1, p2)
* stackH' q last s' g p o p1 p2.
induction s; intros; subst; simpl; [sepLemma | ].
destruct (weq q p); try congruence; fold_stackH'; sepLemma; eauto.
Qed.
Lemma stackH_neq_bwd : forall q last s g p, q <> p -> In q s
-> (Ex s' : list W, [| s = p :: s' |] * [| p <> 0 |]
* Ex o, [| o %in g |] * [| Address o = p |]
* Ex p1, Ex p2, (p ==*> stackTag (Mark o), p1, p2)
* stackH' q last s' g p o p1 p2)
===> stackH q last s g p.
induction s; intros; subst; simpl; [sepLemma | ].
destruct (weq q p); try congruence; fold_stackH'; sepLemma; eauto.
inversion H1; clear H1; subst; sepLemma.
Qed.
Theorem stackH'_true_fwd : forall q last p o p1 p2 s g, Mark o = true
-> stackH' q last s g p o p1 p2
===> [| p1 = FirstChild o |] * [| last = SecondChild o |]
* stackH q p s g p2.
unfold stackH'; intros; destruct (Mark o) eqn:?; sepLemma.
Qed.
Theorem stackH'_false_fwd : forall q last p o p1 p2 s g, Mark o = false
-> stackH' q last s g p o p1 p2
===> [| last = FirstChild o |] * [| p2 = SecondChild o |]
* stackH q p s g p1.
unfold stackH'; intros; destruct (Mark o) eqn:?; sepLemma.
Qed.
Theorem stackH'_true_bwd : forall q last p o p1 p2 s g, Mark o = true
-> [| p1 = FirstChild o |] * [| last = SecondChild o |] * stackH q p s g p2
===> stackH' q last s g p o p1 p2.
unfold stackH'; intros; destruct (Mark o) eqn:?; sepLemma.
Qed.
Theorem stackH'_false_bwd : forall q last p o p1 p2 s g, Mark o = false
-> [| last = FirstChild o |] * [| p2 = SecondChild o |] * stackH q p s g p1
===> stackH' q last s g p o p1 p2.
unfold stackH'; intros; destruct (Mark o) eqn:?; sepLemma.
Qed.
Ltac stack :=
match goal with
| _ => reflexivity
| _ => congruence
| _ => solve [auto]
| H: _ :: _ = _ :: _ |- _ => inversion H; clear H; subst
| H: functional _, H': Address _ = Address _ |- _
=> progress (apply H in H'; auto)
| H: In _ nil |- _ => solve [inversion H]
| H: In _ (_ :: _) |- _ => inversion H; clear H; subst
end.
Local Hint Resolve Himp_refl.
Lemma stack_reveal_h : forall g o s (p last : W), In (Address o) s
-> o %in g -> Address o <> 0 -> functional g
-> stack last s g p
===> stackH (Address o) last s g p
* Address o =*> stackTag (Mark o).
induction s; [sepLemma | intros].
destruct (weq p (natToW 0)); red; intros; step hints_stack; repeat stack.
{
subst; eapply Himp_trans; [ | apply Himp_star_frame; [ | apply Himp_refl] ].
2: apply stackH_eq_bwd; simpl; auto.
sepLemma.
}
destruct (weq (Address x1) (Address o)); subst; repeat ptrs.
{
eapply Himp_trans.
2: apply Himp_star_frame; [apply stackH_eq_bwd; auto | apply Himp_refl ].
red; intro; step hints_stack; repeat stack.
}
{
eapply Himp_trans; [ | apply Himp_star_frame; [ | apply Himp_refl] ].
2: apply stackH_neq_bwd; simpl; auto.
sepLemma.
(* case split on (Mark x1) to open up stack' *)
destruct (Mark x1) eqn:Hmark; subst; unfold stackH'; step hints_stack;
rewrite Hmark; sepLemma; inversion_clear H;
eapply Himp_trans; try (red; apply IHs); auto.
}
Qed.
Lemma in_stack_inDom : forall s g q, stackInGraph s g -> In q s -> inDom q g.
induction 1; intros; subst; repeat stack.
Qed.
Definition stack_pick (_:W) := stack.
Theorem stack_reveal : forall g s (p last q : W), q <> 0 -> In q s -> heapInv g
-> stack_pick q last s g p
===> Ex o, [| o %in g |] * [| Address o = q |]
* [| In q s |] * q =*> stackTag (Mark o)
* stackH q last s g p.
unfold stack_pick; intros.
eapply Himp_trans; [apply stack_pure | ]; sepPure.
edestruct in_stack_inDom as [? [] ]; eauto 2.
subst; eapply Himp_trans; [apply stack_reveal_h; eauto | ]; sepLemma.
Qed.
Definition stack_filled (_:obj) := stack.
Theorem stack_fill : forall o g s (p last : W), o %in g -> heapInv g
-> stackH (Address o) last s g p * Address o =*> stackTag (Mark o)
===> stack_filled o last s g p.
induction s; unfold stack_filled; simpl; intros; [ solve [sepLemma] | ].
assert (functional g) by auto.
destruct (weq (Address o) p); subst; autorewrite with N.
{
sepLemma; repeat ptrs.
destruct (Mark o) eqn:Heq; repeat step hints_stack; rewrite Heq; sepLemma.
}
{
fold_stackH'; sepLemma.
unfold stackH'; destruct (Mark x1) eqn:Heq; repeat step hints_stack;
rewrite Heq; sepLemma.
}
Qed.
Definition hints : TacPackage.
prepare (graph_fwd, graph'_fwd, objects_reveal,
stack_nil_fwd, stack_cons_fwd, stack'_true_fwd, stack'_false_fwd,
stackH_neq_fwd, stackH'_true_fwd, stackH'_false_fwd,
stack_reveal)
(graph_bwd, graph'_bwd,
objects_fill, objects_push, objects_pop, objects_mark, stack_mark,
stack_nil_bwd, stack_cons_bwd, stack'_false_bwd, stack'_true_bwd,
stack'_mark_bwd,
stackH'_true_bwd, stackH'_false_bwd,
stack_fill).
Defined.
(* ============================================================================
* implementation
* ========================================================================= *)
Definition markedS g s g' := List.Forall (fun p => marked g p g') s.
Definition stayMarked g g' := forall o, o %in g -> Mark o = true -> o %in g'.
Inductive sharing := sharingI.
Inductive pushCase := pushCaseI.
Definition m := bmodule "mark" {{
bfunction "mark"("root", "prev", "tmp") [markS]
"prev" <- 0;;
[ Al g, Al s,
PRE[V] graph' s g * stack (V "root") s g (V "prev")
* [| Ptr (V "root") g |] * [| NoDup s |]
POST[_] Ex g', graph g' * [| stayMarked g g' |]
* [| markedS g (V "root" :: s) g' |] ]
While (0 = 0) {
If ("root" = 0) { "tmp" <- 2 } else { Note [sharing];; "tmp" <-* "root" };;
If ("tmp" = 0) { Note [pushCase];;
"root" *<- 1;;
"tmp" <-* "root"+4;; "root"+4 *<- "prev";;
"prev" <- "root";; "root" <- "tmp"
} else {
If ("prev" = 0) {
Return 0
} else {
"tmp" <-* "prev";;
If ("tmp" <> 2) {
"prev" *<- 2;;
"tmp" <-* "prev"+4;; "prev"+4 *<- "root";;
"root" <-* "prev"+8;; "prev"+8 *<- "tmp"
} else {
"tmp" <-* "prev"+8;; "prev"+8 *<- "root";;
"root" <- "prev";; "prev" <- "tmp"
}
}
}
}
end
}}.
(* ===========================================================================
* list lemmas
* ========================================================================= *)
Section ListLemmas.
Variable A : Type.
Variable eq_dec : forall x y : A, { x = y } + { x <> y }.
Lemma In_split_last : forall (p : A) l, In p l
-> exists x y, l = x ++ p :: y /\ ~ In p y.
induction l; simpl; intros; try tauto.
destruct (In_dec eq_dec p l); eauto.
{
destruct IHl as [? [? [] ] ]; auto; subst.
exists (a :: x), x0; split; simpl; auto.
}
{
destruct H; try tauto; subst.
exists nil, l; simpl; auto.
}
Qed.
End ListLemmas.
(*=============================================================================
* reachable/marked lemmas
*===========================================================================*)
Local Hint Resolve Forall_cons.
Section reachableNoLoops.
Variable g : set.
Inductive pathOf : list W -> W -> obj -> Prop :=
| pathBase o : o %in g -> Mark o = false
-> pathOf (Address o :: nil) (Address o) o
| pathFirst x o l : x %in g -> Mark x = false -> pathOf l (FirstChild x) o
-> pathOf (Address x :: l) (Address x) o
| pathSecond x o l : x %in g -> Mark x = false -> pathOf l (SecondChild x) o
-> pathOf (Address x :: l) (Address x) o.
Local Hint Constructors pathOf reachable.
Lemma pathOf_ex : forall p o, reachable g p o -> exists l, pathOf l p o.
induction 1; eauto; destruct IHreachable; eauto.
Qed.
Lemma pathOf_reachable : forall l p o, pathOf l p o -> reachable g p o.
induction 1; eauto.