-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibSepSimpl.v
More file actions
2034 lines (1625 loc) · 66.4 KB
/
LibSepSimpl.v
File metadata and controls
2034 lines (1625 loc) · 66.4 KB
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
(** * LibSepSimpl: Appendix - Simplification Tactic for Entailments *)
Set Implicit Arguments.
From SLF Require Export LibCore.
From SLF Require Export LibSepTLCbuffer.
(* ################################################################# *)
(** * A Functor for Separation Logic Entailment *)
(** This file consists of a functor that provides a tactic for simplifying
entailment relations. This tactic is somewhat generic in that it can
be used for several variants of Separation Logic, hence the use of a
functor to implement the tactic with respect to abstract definitions of
heap predicates.
The file provides a number of lemmas that hold in any variant of
Separation Logic satisfying the requirements of the functor.
It also provides the following key tactics:
- [xsimpl] simplifies heap entailments.
- [xpull] is a restricted version of [xsimpl] that only act over the
left-hand side of the entailment, leaving the right-hand side unmodified.
- [xchange] performs transitivity steps in entailments, it enables
replacing a subset of the heap predicates on the right-hand side with
another set of heap predicates entailed by the former. *)
(** Bonus: the tactic [rew_heap] normalizes heap predicate expressions;
it is not used in the course. *)
(* ################################################################# *)
(** * Assumptions of the functor *)
Module Type XsimplParams.
(* ================================================================= *)
(** ** Operators *)
(** The notion of heap predicate and entailment must be provided. *)
Parameter hprop : Type.
Parameter himpl : hprop -> hprop -> Prop.
Definition qimpl A (Q1 Q2:A->hprop) :=
forall r, himpl (Q1 r) (Q2 r).
(** The core operators of Separation Logic must be provided. *)
Parameter hempty : hprop.
Parameter hstar : hprop -> hprop -> hprop.
Parameter hpure : Prop -> hprop.
Parameter htop : hprop.
Parameter hgc : hprop.
Parameter hwand : hprop -> hprop -> hprop.
Parameter qwand : forall A, (A->hprop) -> (A->hprop) -> hprop.
Parameter hexists : forall A, (A->hprop) -> hprop.
Parameter hforall : forall A, (A->hprop) -> hprop.
(** The predicate [haffine] must be provided. For a fully linear logic, use
the always-false predicate. For a fully affine logic, use the always-true
predicate. *)
Parameter haffine : hprop -> Prop.
(* ================================================================= *)
(** ** Notation *)
(** The following notation is used for stating the required properties. *)
Declare Scope heap_scope.
Notation "H1 ==> H2" := (himpl H1 H2)
(at level 55) : heap_scope.
Notation "Q1 ===> Q2" := (qimpl Q1 Q2)
(at level 55) : heap_scope.
Notation "\[]" := (hempty)
(at level 0) : heap_scope.
Notation "\[ P ]" := (hpure P)
(at level 0, format "\[ P ]") : heap_scope.
Notation "\Top" := (htop) : heap_scope.
Notation "\GC" := (hgc) : heap_scope.
Notation "H1 '\*' H2" := (hstar H1 H2)
(at level 41, right associativity) : heap_scope.
Notation "Q \*+ H" := (fun x => hstar (Q x) H)
(at level 40) : heap_scope.
Notation "H1 \-* H2" := (hwand H1 H2)
(at level 43, right associativity) : heap_scope.
Notation "Q1 \--* Q2" := (qwand Q1 Q2)
(at level 43) : heap_scope.
Notation "'\exists' x1 .. xn , H" :=
(hexists (fun x1 => .. (hexists (fun xn => H)) ..))
(at level 39, x1 binder, H at level 50, right associativity,
format "'[' '\exists' '/ ' x1 .. xn , '/ ' H ']'") : heap_scope.
Notation "'\forall' x1 .. xn , H" :=
(hforall (fun x1 => .. (hforall (fun xn => H)) ..))
(at level 39, x1 binder, H at level 50, right associativity,
format "'[' '\forall' '/ ' x1 .. xn , '/ ' H ']'") : heap_scope.
Local Open Scope heap_scope.
(* ================================================================= *)
(** ** Properties Assumed by the Functor *)
(** The following properties must be satisfied. *)
Implicit Types P : Prop.
Implicit Types H : hprop.
(** Entailment must be an order. *)
Parameter himpl_refl : forall H,
H ==> H.
Parameter himpl_trans : forall H2 H1 H3,
(H1 ==> H2) ->
(H2 ==> H3) ->
(H1 ==> H3).
Parameter himpl_antisym : forall H1 H2,
(H1 ==> H2) ->
(H2 ==> H1) ->
(H1 = H2).
(** The star and the empty heap predicate must form a commutative monoid. *)
Parameter hstar_hempty_l : forall H,
\[] \* H = H.
Parameter hstar_hempty_r : forall H,
H \* \[] = H.
Parameter hstar_comm : forall H1 H2,
H1 \* H2 = H2 \* H1.
Parameter hstar_assoc : forall H1 H2 H3,
(H1 \* H2) \* H3 = H1 \* (H2 \* H3).
(** The frame property for entailment must hold. *)
Parameter himpl_frame_lr : forall H1 H1' H2 H2',
H1 ==> H1' ->
H2 ==> H2' ->
(H1 \* H2) ==> (H1' \* H2').
Parameter himpl_hstar_trans_l : forall H1 H2 H3 H4,
H1 ==> H2 ->
H2 \* H3 ==> H4 ->
H1 \* H3 ==> H4.
(** Characterization of hpure *)
Parameter himpl_hempty_hpure : forall P,
P ->
\[] ==> \[P].
Parameter himpl_hstar_hpure_l : forall P H H',
(P -> H ==> H') ->
(\[P] \* H) ==> H'.
(** Characterization of [hexists] *)
Parameter himpl_hexists_l : forall A H (J:A->hprop),
(forall x, J x ==> H) ->
(hexists J) ==> H.
Parameter himpl_hexists_r : forall A (x:A) H J,
(H ==> J x) ->
H ==> (hexists J).
Parameter hstar_hexists : forall A (J:A->hprop) H,
(hexists J) \* H = hexists (fun x => (J x) \* H).
(** Characterization of [hforall] *)
Parameter himpl_hforall_r : forall A (J:A->hprop) H,
(forall x, H ==> J x) ->
H ==> (hforall J).
Parameter hstar_hforall : forall H A (J:A->hprop),
(hforall J) \* H ==> hforall (J \*+ H).
(** Characterization of [hwand] *)
Parameter hwand_equiv : forall H0 H1 H2,
(H0 ==> H1 \-* H2) <-> (H1 \* H0 ==> H2).
Parameter hwand_curry_eq : forall H1 H2 H3,
(H1 \* H2) \-* H3 = H1 \-* (H2 \-* H3).
Parameter hwand_hempty_l : forall H,
(\[] \-* H) = H.
(** Characterization of [qwand] *)
Parameter qwand_equiv : forall H A (Q1 Q2:A->hprop),
H ==> (Q1 \--* Q2) <-> (Q1 \*+ H) ===> Q2.
Parameter hwand_cancel : forall H1 H2,
H1 \* (H1 \-* H2) ==> H2.
Parameter qwand_specialize : forall A (x:A) (Q1 Q2:A->hprop),
(Q1 \--* Q2) ==> (Q1 x \-* Q2 x).
(** Characterization of [htop] *)
Parameter himpl_htop_r : forall H,
H ==> \Top.
Parameter hstar_htop_htop :
\Top \* \Top = \Top.
(** Characterization of [hgc] *)
Parameter haffine_hempty :
haffine \[].
Parameter himpl_hgc_r : forall H,
haffine H ->
H ==> \GC.
Parameter hstar_hgc_hgc :
\GC \* \GC = \GC.
End XsimplParams.
(* ################################################################# *)
(** * Body of the Functor *)
(** When all the assumptions of the functor are statisfied, all the lemmas
stated below hold, and all the tactics defined below may be used. *)
Module XsimplSetup (HP : XsimplParams).
Import HP.
Local Open Scope heap_scope.
Implicit Types H : hprop.
Implicit Types P : Prop.
#[global]
Hint Resolve himpl_refl.
(* ================================================================= *)
(** ** Properties of [himpl] *)
Lemma himpl_of_eq : forall H1 H2,
H1 = H2 ->
H1 ==> H2.
Proof. intros. subst. applys~ himpl_refl. Qed.
Lemma himpl_of_eq_sym : forall H1 H2,
H1 = H2 ->
H2 ==> H1.
Proof. intros. subst. applys~ himpl_refl. Qed.
Lemma himpl_hstar_trans_l : forall H1 H2 H3 H4,
H1 ==> H2 ->
H2 \* H3 ==> H4 ->
H1 \* H3 ==> H4.
Proof using.
introv M1 M2. applys himpl_trans M2. applys* himpl_frame_lr M1.
Qed.
(* ================================================================= *)
(** ** Properties of [qimpl] *)
Lemma qimpl_refl : forall A (Q:A->hprop),
Q ===> Q.
Proof using. intros. hnfs*. Qed.
#[global] Hint Resolve qimpl_refl.
Lemma qimpl_trans : forall A (Q2 Q1 Q3:A->hprop),
(Q1 ===> Q2) ->
(Q2 ===> Q3) ->
(Q1 ===> Q3).
Proof using. introv M1 M2. intros v. applys* himpl_trans. Qed.
Lemma qimpl_antisym : forall A (Q1 Q2:A->hprop),
(Q1 ===> Q2) ->
(Q2 ===> Q1) ->
(Q1 = Q2).
Proof using. introv M1 M2. apply fun_ext_1. intros v. applys* himpl_antisym. Qed.
(* ================================================================= *)
(** ** Properties of [hstar] *)
Lemma hstar_comm_assoc : forall H1 H2 H3,
H1 \* H2 \* H3 = H2 \* H1 \* H3.
Proof using.
intros. rewrite <- hstar_assoc.
rewrite (@hstar_comm H1 H2). rewrite~ hstar_assoc.
Qed.
(* ================================================================= *)
(** ** Representation Predicates *)
(** The arrow notation typically takes the form [x ~> R x],
to indicate that [X] is the logical value that describes the
heap structure [x], according to the representation predicate [R].
It is just a notation for the heap predicate [R X x]. *)
Definition repr (A:Type) (S:A->hprop) (x:A) : hprop :=
S x.
Notation "x '~>' S" := (repr S x)
(at level 33, no associativity) : heap_scope.
Lemma repr_eq : forall (A:Type) (S:A->hprop) (x:A),
(x ~> S) = (S x).
Proof using. auto. Qed.
(** [x ~> Id X] holds when [x] is equal to [X] in the empty heap.
[Id] is called the identity representation predicate. *)
Definition Id {A:Type} (X:A) (x:A) :=
\[ X = x ].
(** [xrepr_clean] simplifies instances of
[p ~> (fun _ => _)] by unfolding the arrow,
but only when the body does not captures
mklocally bound variables. This tactic should
normally not be used directly *)
Ltac xrepr_clean_core tt :=
repeat match goal with |- context C [?p ~> ?E] =>
match E with (fun _ => _) =>
let E' := eval cbv beta in (E p) in
let G' := context C [E'] in
let G := match goal with |- ?G => G end in
change G with G' end end.
Tactic Notation "xrepr_clean" :=
xrepr_clean_core tt.
Lemma repr_id : forall A (x X:A),
(x ~> Id X) = \[X = x].
Proof using. intros. unfold Id. xrepr_clean. auto. Qed.
(* ================================================================= *)
(** ** [rew_heap] Tactic to Normalize Expressions with [hstar] *)
(** [rew_heap] removes empty heap predicates, and normalizes w.r.t.
associativity of star.
[rew_heap_assoc] only normalizes w.r.t. associativity.
(It should only be used internally for tactic implementation. *)
Lemma star_post_empty : forall B (Q:B->hprop),
Q \*+ \[] = Q.
Proof using. extens. intros. rewrite* hstar_hempty_r. Qed.
#[global]
Hint Rewrite hstar_hempty_l hstar_hempty_r
hstar_assoc star_post_empty hwand_hempty_l : rew_heap.
Tactic Notation "rew_heap" :=
autorewrite with rew_heap.
Tactic Notation "rew_heap" "in" "*" :=
autorewrite with rew_heap in *.
Tactic Notation "rew_heap" "in" hyp(H) :=
autorewrite with rew_heap in H.
Tactic Notation "rew_heap" "~" :=
rew_heap; auto_tilde.
Tactic Notation "rew_heap" "~" "in" "*" :=
rew_heap in *; auto_tilde.
Tactic Notation "rew_heap" "~" "in" hyp(H) :=
rew_heap in H; auto_tilde.
Tactic Notation "rew_heap" "*" :=
rew_heap; auto_star.
Tactic Notation "rew_heap" "*" "in" "*" :=
rew_heap in *; auto_star.
Tactic Notation "rew_heap" "*" "in" hyp(H) :=
rew_heap in H; auto_star.
#[global]
Hint Rewrite hstar_assoc : rew_heap_assoc.
Tactic Notation "rew_heap_assoc" :=
autorewrite with rew_heap_assoc.
(* ================================================================= *)
(** ** Auxiliary Tactics Used by [xpull] and [xsimpl] *)
Ltac remove_empty_heaps_from H :=
match H with context[ ?H1 \* \[] ] =>
match is_evar_as_bool H1 with
| false => rewrite (@hstar_hempty_r H1)
| true => let X := fresh in
set (X := H1);
rewrite (@hstar_hempty_r X);
subst X
end end.
Ltac remove_empty_heaps_haffine tt :=
repeat match goal with |- haffine ?H => remove_empty_heaps_from H end.
Ltac remove_empty_heaps_left tt :=
repeat match goal with |- ?H1 ==> _ => remove_empty_heaps_from H1 end.
Ltac remove_empty_heaps_right tt :=
repeat match goal with |- _ ==> ?H2 => remove_empty_heaps_from H2 end.
(* ================================================================= *)
(** ** Tactics [xsimpl] and [xpull] for Heap Entailments *)
(** The implementation of the tactics is fairly involved. The high-level
specification of the tactic appears in the last appendix of:
http://www.chargueraud.org/research/2020/seq_seplogic/seq_seplogic.pdf . *)
(* ----------------------------------------------------------------- *)
(** *** [xaffine] placeholder *)
Ltac xaffine_core tt := (* to be generalized lated *)
try solve [ assumption | apply haffine_hempty ].
Tactic Notation "xaffine" :=
xaffine_core tt.
(* ----------------------------------------------------------------- *)
(** *** Hints for tactics such as [xsimpl] *)
Inductive Xsimpl_hint : list Boxer -> Type :=
| xsimpl_hint : forall (L:list Boxer), Xsimpl_hint L.
Ltac xsimpl_hint_put L :=
let H := fresh "Hint" in
generalize (xsimpl_hint L); intros H.
Ltac xsimpl_hint_next cont :=
match goal with H: Xsimpl_hint ((boxer ?x)::?L) |- _ =>
clear H; xsimpl_hint_put L; cont x end.
Ltac xsimpl_hint_remove tt :=
match goal with H: Xsimpl_hint _ |- _ => clear H end.
(* ----------------------------------------------------------------- *)
(** *** Lemmas [hstars_reorder_..] to flip an iterated [hstar]. *)
(** [hstars_flip tt] applies to a goal of the form [H1 \* .. \* Hn \* \[]= ?H]
and instantiates [H] with [Hn \* ... \* H1 \* \[]].
If [n > 9], the maximum arity supported, the tactic unifies [H] with
the original LHS. *)
Lemma hstars_flip_0 :
\[] = \[].
Proof using. auto. Qed.
Lemma hstars_flip_1 : forall H1,
H1 \* \[] = H1 \* \[].
Proof using. auto. Qed.
Lemma hstars_flip_2 : forall H1 H2,
H1 \* H2 \* \[] = H2 \* H1 \* \[].
Proof using. intros. rew_heap. rewrite (hstar_comm H2). rew_heap~. Qed.
Lemma hstars_flip_3 : forall H1 H2 H3,
H1 \* H2 \* H3 \* \[] = H3 \* H2 \* H1 \* \[].
Proof using. intros. rewrite <- (hstars_flip_2 H1). rew_heap. rewrite (hstar_comm H3). rew_heap~. Qed.
Lemma hstars_flip_4 : forall H1 H2 H3 H4,
H1 \* H2 \* H3 \* H4 \* \[] = H4 \* H3 \* H2 \* H1 \* \[].
Proof using. intros. rewrite <- (hstars_flip_3 H1). rew_heap. rewrite (hstar_comm H4). rew_heap~. Qed.
Lemma hstars_flip_5 : forall H1 H2 H3 H4 H5,
H1 \* H2 \* H3 \* H4 \* H5 \* \[] = H5 \* H4 \* H3 \* H2 \* H1 \* \[].
Proof using. intros. rewrite <- (hstars_flip_4 H1). rew_heap. rewrite (hstar_comm H5). rew_heap~. Qed.
Lemma hstars_flip_6 : forall H1 H2 H3 H4 H5 H6,
H1 \* H2 \* H3 \* H4 \* H5 \* H6 \* \[]
= H6 \* H5 \* H4 \* H3 \* H2 \* H1 \* \[].
Proof using. intros. rewrite <- (hstars_flip_5 H1). rew_heap. rewrite (hstar_comm H6). rew_heap~. Qed.
Lemma hstars_flip_7 : forall H1 H2 H3 H4 H5 H6 H7,
H1 \* H2 \* H3 \* H4 \* H5 \* H6 \* H7 \* \[]
= H7 \* H6 \* H5 \* H4 \* H3 \* H2 \* H1 \* \[].
Proof using. intros. rewrite <- (hstars_flip_6 H1). rew_heap. rewrite (hstar_comm H7). rew_heap~. Qed.
Lemma hstars_flip_8 : forall H1 H2 H3 H4 H5 H6 H7 H8,
H1 \* H2 \* H3 \* H4 \* H5 \* H6 \* H7 \* H8 \* \[]
= H8 \* H7 \* H6 \* H5 \* H4 \* H3 \* H2 \* H1 \* \[].
Proof using. intros. rewrite <- (hstars_flip_7 H1). rew_heap. rewrite (hstar_comm H8). rew_heap~. Qed.
Lemma hstars_flip_9 : forall H1 H2 H3 H4 H5 H6 H7 H8 H9,
H1 \* H2 \* H3 \* H4 \* H5 \* H6 \* H7 \* H8 \* H9 \* \[]
= H9 \* H8 \* H7 \* H6 \* H5 \* H4 \* H3 \* H2 \* H1 \* \[].
Proof using. intros. rewrite <- (hstars_flip_8 H1). rew_heap. rewrite (hstar_comm H9). rew_heap~. Qed.
Ltac hstars_flip_lemma i :=
match number_to_nat i with
| 0%nat => constr:(hstars_flip_0)
| 1%nat => constr:(hstars_flip_1)
| 2%nat => constr:(hstars_flip_2)
| 3%nat => constr:(hstars_flip_3)
| 4%nat => constr:(hstars_flip_4)
| 5%nat => constr:(hstars_flip_5)
| 6%nat => constr:(hstars_flip_6)
| 7%nat => constr:(hstars_flip_7)
| 8%nat => constr:(hstars_flip_8)
| 9%nat => constr:(hstars_flip_9)
| _ => constr:(hstars_flip_1) (* unsupported *)
end.
Ltac hstars_arity i Hs :=
match Hs with
| \[] => constr:(i)
| ?H1 \* ?H2 => hstars_arity (S i) H2
end.
Ltac hstars_flip_arity tt :=
match goal with |- ?HL = ?HR => hstars_arity 0%nat HL end.
Ltac hstars_flip tt :=
let i := hstars_flip_arity tt in
let L := hstars_flip_lemma i in
eapply L.
(* ----------------------------------------------------------------- *)
(** *** Lemmas [hstars_pick_...] to extract hyps in depth. *)
(** [hstars_search Hs test] applies to an expression [Hs]
of the form either [H1 \* ... \* Hn \* \[]]
or [H1 \* ... \* Hn]. It invokes the function [test i Hi]
for each of the [Hi] in turn until the tactic succeeds.
In the particular case of invoking [test n Hn] when there
is no trailing [\[]], the call is of the form [test (hstars_last n) Hn]
where [hstars_last] is an identity tag. *)
Definition hstars_last (A:Type) (X:A) := X.
Ltac hstars_search Hs test :=
let rec aux i Hs :=
first [ match Hs with ?H \* _ => test i H end
| match Hs with _ \* ?Hs' => aux (S i) Hs' end
| match Hs with ?H => test (hstars_last i) H end ] in
aux 1%nat Hs.
(** [hstars_pick_lemma i] returns one of the lemma below,
which enables reordering in iterated stars, by extracting
the i-th item to bring it to the front. *)
Lemma hstars_pick_1 : forall H1 H,
H1 \* H = H1 \* H.
Proof using. auto. Qed.
Lemma hstars_pick_2 : forall H1 H2 H,
H1 \* H2 \* H = H2 \* H1 \* H.
Proof using. intros. rewrite~ (hstar_comm_assoc H1). Qed.
Lemma hstars_pick_3 : forall H1 H2 H3 H,
H1 \* H2 \* H3 \* H = H3 \* H1 \* H2 \* H.
Proof using. intros. rewrite~ (hstar_comm_assoc H2). applys hstars_pick_2. Qed.
Lemma hstars_pick_4 : forall H1 H2 H3 H4 H,
H1 \* H2 \* H3 \* H4 \* H = H4 \* H1 \* H2 \* H3 \* H.
Proof using. intros. rewrite~ (hstar_comm_assoc H3). applys hstars_pick_3. Qed.
Lemma hstars_pick_5 : forall H1 H2 H3 H4 H5 H,
H1 \* H2 \* H3 \* H4 \* H5 \* H = H5 \* H1 \* H2 \* H3 \* H4 \* H.
Proof using. intros. rewrite~ (hstar_comm_assoc H4). applys hstars_pick_4. Qed.
Lemma hstars_pick_6 : forall H1 H2 H3 H4 H5 H6 H,
H1 \* H2 \* H3 \* H4 \* H5 \* H6 \* H
= H6 \* H1 \* H2 \* H3 \* H4 \* H5 \* H.
Proof using. intros. rewrite~ (hstar_comm_assoc H5). applys hstars_pick_5. Qed.
Lemma hstars_pick_7 : forall H1 H2 H3 H4 H5 H6 H7 H,
H1 \* H2 \* H3 \* H4 \* H5 \* H6 \* H7 \* H
= H7 \* H1 \* H2 \* H3 \* H4 \* H5 \* H6 \* H.
Proof using. intros. rewrite~ (hstar_comm_assoc H6). applys hstars_pick_6. Qed.
Lemma hstars_pick_8 : forall H1 H2 H3 H4 H5 H6 H7 H8 H,
H1 \* H2 \* H3 \* H4 \* H5 \* H6 \* H7 \* H8 \* H
= H8 \* H1 \* H2 \* H3 \* H4 \* H5 \* H6 \* H7 \* H.
Proof using. intros. rewrite~ (hstar_comm_assoc H7). applys hstars_pick_7. Qed.
Lemma hstars_pick_9 : forall H1 H2 H3 H4 H5 H6 H7 H8 H9 H,
H1 \* H2 \* H3 \* H4 \* H5 \* H6 \* H7 \* H8 \* H9 \* H
= H9 \* H1 \* H2 \* H3 \* H4 \* H5 \* H6 \* H7 \* H8 \* H.
Proof using. intros. rewrite~ (hstar_comm_assoc H8). applys hstars_pick_8. Qed.
Lemma hstars_pick_last_1 : forall H1,
H1 = H1.
Proof using. auto. Qed.
Lemma hstars_pick_last_2 : forall H1 H2,
H1 \* H2 = H2 \* H1.
Proof using. intros. rewrite~ (hstar_comm). Qed.
Lemma hstars_pick_last_3 : forall H1 H2 H3,
H1 \* H2 \* H3 = H3 \* H1 \* H2.
Proof using. intros. rewrite~ (hstar_comm H2). applys hstars_pick_2. Qed.
Lemma hstars_pick_last_4 : forall H1 H2 H3 H4,
H1 \* H2 \* H3 \* H4 = H4 \* H1 \* H2 \* H3.
Proof using. intros. rewrite~ (hstar_comm H3). applys hstars_pick_3. Qed.
Lemma hstars_pick_last_5 : forall H1 H2 H3 H4 H5,
H1 \* H2 \* H3 \* H4 \* H5 = H5 \* H1 \* H2 \* H3 \* H4.
Proof using. intros. rewrite~ (hstar_comm H4). applys hstars_pick_4. Qed.
Lemma hstars_pick_last_6 : forall H1 H2 H3 H4 H5 H6,
H1 \* H2 \* H3 \* H4 \* H5 \* H6
= H6 \* H1 \* H2 \* H3 \* H4 \* H5.
Proof using. intros. rewrite~ (hstar_comm H5). applys hstars_pick_5. Qed.
Lemma hstars_pick_last_7 : forall H1 H2 H3 H4 H5 H6 H7,
H1 \* H2 \* H3 \* H4 \* H5 \* H6 \* H7
= H7 \* H1 \* H2 \* H3 \* H4 \* H5 \* H6.
Proof using. intros. rewrite~ (hstar_comm H6). applys hstars_pick_6. Qed.
Lemma hstars_pick_last_8 : forall H1 H2 H3 H4 H5 H6 H7 H8,
H1 \* H2 \* H3 \* H4 \* H5 \* H6 \* H7 \* H8
= H8 \* H1 \* H2 \* H3 \* H4 \* H5 \* H6 \* H7.
Proof using. intros. rewrite~ (hstar_comm H7). applys hstars_pick_7. Qed.
Lemma hstars_pick_last_9 : forall H1 H2 H3 H4 H5 H6 H7 H8 H9,
H1 \* H2 \* H3 \* H4 \* H5 \* H6 \* H7 \* H8 \* H9
= H9 \* H1 \* H2 \* H3 \* H4 \* H5 \* H6 \* H7 \* H8.
Proof using. intros. rewrite~ (hstar_comm H8). applys hstars_pick_8. Qed.
Ltac hstars_pick_lemma i :=
let unsupported tt := fail 100 "hstars_pick supports only arity up to 9" in
match i with
| hstars_last ?j => match number_to_nat j with
| 1%nat => constr:(hstars_pick_last_1)
| 2%nat => constr:(hstars_pick_last_2)
| 3%nat => constr:(hstars_pick_last_3)
| 4%nat => constr:(hstars_pick_last_4)
| 5%nat => constr:(hstars_pick_last_5)
| 6%nat => constr:(hstars_pick_last_6)
| 7%nat => constr:(hstars_pick_last_7)
| 8%nat => constr:(hstars_pick_last_8)
| 9%nat => constr:(hstars_pick_last_9)
| _ => unsupported tt
end
| ?j => match number_to_nat j with
| 1%nat => constr:(hstars_pick_1)
| 2%nat => constr:(hstars_pick_2)
| 3%nat => constr:(hstars_pick_3)
| 4%nat => constr:(hstars_pick_4)
| 5%nat => constr:(hstars_pick_5)
| 6%nat => constr:(hstars_pick_6)
| 7%nat => constr:(hstars_pick_7)
| 8%nat => constr:(hstars_pick_8)
| 9%nat => constr:(hstars_pick_9)
| _ => unsupported tt
end
end.
(* ----------------------------------------------------------------- *)
(** *** Documentation for the Tactic [xsimpl] *)
(** ... doc for [xsimpl] to update
At the end, there remains a heap entailment with a simplified
LHS and a simplified RHS, with items not cancelled out.
At this point, if the goal is of the form [H ==> \GC] or [H ==> \Top] or
[H ==> ?H'] for some evar [H'], then [xsimpl] solves the goal.
Else, it leaves whatever remains.
For the cancellation part, [xsimpl] cancels out [H] from the LHS
with [H'] from the RHS if either [H'] is syntactically equal to [H],
or if [H] and [H'] both have the form [x ~> ...] for the same [x].
Note that, in case of a mismatch with [x ~> R X] on the LHS and
[x ~> R' X'] on the RHS, [xsimpl] will produce a goal of the form
[(x ~> R X) = (x ~> R' X')] which will likely be unsolvable.
It is the user's responsability to perform the appropriate conversion
steps prior to calling [xsimpl].
Remark: the reason for the special treatment of [x ~> ...] is that
it is very useful to be able to automatically cancel out
[x ~> R X] from the LHS with [x ~> R ?Y], for some evar [?Y] which
typically is introduced from an existential, e.g. [\exists Y, x ~> R Y].
Remark: importantly, [xsimpl] does not attempt any simplification on
a representation predicate of the form [?x ~> ...], when the [?x]
is an uninstantiated evar. Such situation may arise for example
with the following RHS: [\exists p, (r ~> Ref p) \* (p ~> Ref 3)].
As a special feature, [xsimpl] may be provided optional arguments
for instantiating the existentials (instead of introducing evars).
These optional arguments need to be given in left-right order,
and are used on a first-match basis: the head value is used if its
type matches the type expected by the existential, else an evar
is introduced for that existential. *)
(** [Xsimpl (Hla, Hlw, Hlt) (Hra, Hrg, Hrt)] is interepreted as
the entailment [Hla \* Hlw \* Hlt ==> Hra \* Hrg \* Hrt] where
- [Hla] denotes "cleaned up" items from the left hand side
- [Hlw] denotes the [H1 \-* H2] and [Q1 \--* Q2] items from the left hand side
- [Hlt] denotes the remaining items to process items from the left hand side
- [Hra] denotes "cleaned up" items from the right hand side
- [Hrg] denotes the [\GC] and [\Top] items from the right hand side
- [Hrt] denotes the remaining items to process from the right hand side
Note: we assume that all items consist of iterated hstars, and are
always terminated by an empty heap. *)
Definition Xsimpl (HL HR:hprop*hprop*hprop) :=
let '(Hla,Hlw,Hlt) := HL in
let '(Hra,Hrg,Hrt) := HR in
Hla \* Hlw \* Hlt ==> Hra \* Hrg \* Hrt.
(** [protect X] is use to prevent [xsimpl] from investigating inside [X] *)
Definition protect (A:Type) (X:A) : A := X.
(** Auxiliary lemmas to prove lemmas for [xsimpl] implementation. *)
Lemma Xsimpl_trans_l : forall Hla1 Hlw1 Hlt1 Hla2 Hlw2 Hlt2 HR,
Xsimpl (Hla2,Hlw2,Hlt2) HR ->
Hla1 \* Hlw1 \* Hlt1 ==> Hla2 \* Hlw2 \* Hlt2 ->
Xsimpl (Hla1,Hlw1,Hlt1) HR.
Proof using.
introv M1 E. destruct HR as [[Hra Hrg] Hrt]. unfolds Xsimpl.
applys* himpl_trans M1.
Qed.
Lemma Xsimpl_trans_r : forall Hra1 Hrg1 Hrt1 Hra2 Hrg2 Hrt2 HL,
Xsimpl HL (Hra2,Hrg2,Hrt2) ->
Hra2 \* Hrg2 \* Hrt2 ==> Hra1 \* Hrg1 \* Hrt1 ->
Xsimpl HL (Hra1,Hrg1,Hrt1).
Proof using.
introv M1 E. destruct HL as [[Hla Hlw] Hlt]. unfolds Xsimpl.
applys* himpl_trans M1.
Qed.
Lemma Xsimpl_trans : forall Hla1 Hlw1 Hlt1 Hla2 Hlw2 Hlt2 Hra1 Hrg1 Hrt1 Hra2 Hrg2 Hrt2,
Xsimpl (Hla2,Hlw2,Hlt2) (Hra2,Hrg2,Hrt2) ->
(Hla2 \* Hlw2 \* Hlt2 ==> Hra2 \* Hrg2 \* Hrt2 ->
Hla1 \* Hlw1 \* Hlt1 ==> Hra1 \* Hrg1 \* Hrt1) ->
Xsimpl (Hla1,Hlw1,Hlt1) (Hra1,Hrg1,Hrt1).
Proof using. introv M1 E. unfolds Xsimpl. eauto. Qed.
(* ----------------------------------------------------------------- *)
(** *** Basic cancellation tactic used to establish lemmas used by [xsimpl] *)
Lemma hstars_simpl_start : forall H1 H2,
H1 \* \[] ==> \[] \* H2 \* \[] ->
H1 ==> H2.
Proof using. introv M. rew_heap~ in *. Qed.
Lemma hstars_simpl_keep : forall H1 Ha H Ht,
H1 ==> (H \* Ha) \* Ht ->
H1 ==> Ha \* H \* Ht.
Proof using. introv M. rew_heap in *. rewrite~ hstar_comm_assoc. Qed.
Lemma hstars_simpl_cancel : forall H1 Ha H Ht,
H1 ==> Ha \* Ht ->
H \* H1 ==> Ha \* H \* Ht.
Proof using. introv M. rewrite hstar_comm_assoc. applys~ himpl_frame_lr. Qed.
Lemma hstars_simpl_pick_lemma : forall H1 H1' H2,
H1 = H1' ->
H1' ==> H2 ->
H1 ==> H2.
Proof using. introv M. subst~. Qed.
Ltac hstars_simpl_pick i :=
(* Remark: the [hstars_pick_last] lemmas should never be needed here *)
let L := hstars_pick_lemma i in
eapply hstars_simpl_pick_lemma; [ apply L | ].
Ltac hstars_simpl_start tt :=
match goal with |- ?H1 ==> ?H2 => idtac end;
applys hstars_simpl_start;
rew_heap_assoc.
Ltac hstars_simpl_step tt :=
match goal with |- ?Hl ==> ?Ha \* ?H \* ?H2 =>
first [
hstars_search Hl ltac:(fun i H' =>
match H' with H => hstars_simpl_pick i end);
apply hstars_simpl_cancel
| apply hstars_simpl_keep ]
end.
Ltac hstars_simpl_post tt :=
rew_heap; try apply himpl_refl.
Ltac hstars_simpl_core tt :=
hstars_simpl_start tt;
repeat (hstars_simpl_step tt);
hstars_simpl_post tt.
Tactic Notation "hstars_simpl" :=
hstars_simpl_core tt.
(* ----------------------------------------------------------------- *)
(** *** Transition lemmas *)
(** Transition lemmas to start the process *)
Lemma xpull_protect : forall H1 H2,
H1 ==> protect H2 ->
H1 ==> H2.
Proof using. auto. Qed.
Lemma xsimpl_start : forall H1 H2,
Xsimpl (\[], \[], (H1 \* \[])) (\[], \[], (H2 \* \[])) ->
H1 ==> H2.
Proof using. introv M. unfolds Xsimpl. rew_heap~ in *. Qed.
(* Note: [repeat rewrite hstar_assoc] after applying this lemma *)
(** Transition lemmas for LHS extraction operations *)
Ltac xsimpl_l_start M :=
introv M;
match goal with HR: hprop*hprop*hprop |- _ =>
destruct HR as [[Hra Hrg] Hrt]; unfolds Xsimpl end.
Ltac xsimpl_l_start' M :=
xsimpl_l_start M; applys himpl_trans (rm M); hstars_simpl.
Lemma xsimpl_l_hempty : forall Hla Hlw Hlt HR,
Xsimpl (Hla, Hlw, Hlt) HR ->
Xsimpl (Hla, Hlw, (\[] \* Hlt)) HR.
Proof using. xsimpl_l_start' M. Qed.
Lemma xsimpl_l_hpure : forall P Hla Hlw Hlt HR,
(P -> Xsimpl (Hla, Hlw, Hlt) HR) ->
Xsimpl (Hla, Hlw, (\[P] \* Hlt)) HR.
Proof using.
xsimpl_l_start M. rewrite hstars_pick_3. applys* himpl_hstar_hpure_l.
Qed.
Lemma xsimpl_l_hexists : forall A (J:A->hprop) Hla Hlw Hlt HR,
(forall x, Xsimpl (Hla, Hlw, (J x \* Hlt)) HR) ->
Xsimpl (Hla, Hlw, (hexists J \* Hlt)) HR.
Proof using.
xsimpl_l_start M. rewrite hstars_pick_3. rewrite hstar_hexists.
applys* himpl_hexists_l. intros. rewrite~ <- hstars_pick_3.
Qed.
Lemma xsimpl_l_acc_wand : forall H Hla Hlw Hlt HR,
Xsimpl (Hla, (H \* Hlw), Hlt) HR ->
Xsimpl (Hla, Hlw, (H \* Hlt)) HR.
Proof using. xsimpl_l_start' M. Qed.
Lemma xsimpl_l_acc_other : forall H Hla Hlw Hlt HR,
Xsimpl ((H \* Hla), Hlw, Hlt) HR ->
Xsimpl (Hla, Hlw, (H \* Hlt)) HR.
Proof using. xsimpl_l_start' M. Qed.
(** Transition lemmas for LHS cancellation operations
---Hlt is meant to be empty there *)
Lemma xsimpl_l_cancel_hwand_hempty : forall H2 Hla Hlw Hlt HR,
Xsimpl (Hla, Hlw, (H2 \* Hlt)) HR ->
Xsimpl (Hla, ((\[] \-* H2) \* Hlw), Hlt) HR.
Proof using. xsimpl_l_start' M. Qed.
Lemma xsimpl_l_cancel_hwand : forall H1 H2 Hla Hlw Hlt HR,
Xsimpl (\[], Hlw, (Hla \* H2 \* Hlt)) HR ->
Xsimpl ((H1 \* Hla), ((H1 \-* H2) \* Hlw), Hlt) HR.
Proof using. xsimpl_l_start' M. applys~ hwand_cancel. Qed.
Lemma xsimpl_l_cancel_qwand : forall A (x:A) (Q1 Q2:A->hprop) Hla Hlw Hlt HR,
Xsimpl (\[], Hlw, (Hla \* Q2 x \* Hlt)) HR ->
Xsimpl ((Q1 x \* Hla), ((Q1 \--* Q2) \* Hlw), Hlt) HR.
Proof using.
xsimpl_l_start' M. rewrite hstar_comm. applys himpl_hstar_trans_l.
applys qwand_specialize x. rewrite hstar_comm. applys hwand_cancel.
Qed.
Lemma xsimpl_l_keep_wand : forall H Hla Hlw Hlt HR,
Xsimpl ((H \* Hla), Hlw, Hlt) HR ->
Xsimpl (Hla, (H \* Hlw), Hlt) HR.
Proof using. xsimpl_l_start' M. Qed.
Lemma xsimpl_l_hwand_reorder : forall H1 H1' H2 Hla Hlw Hlt HR,
H1 = H1' ->
Xsimpl (Hla, ((H1' \-* H2) \* Hlw), Hlt) HR ->
Xsimpl (Hla, ((H1 \-* H2) \* Hlw), Hlt) HR.
Proof using. intros. subst*. Qed.
Lemma xsimpl_l_cancel_hwand_hstar : forall H1 H2 H3 Hla Hlw Hlt HR,
Xsimpl (Hla, Hlw, ((H2 \-* H3) \* Hlt)) HR ->
Xsimpl ((H1 \* Hla), (((H1 \* H2) \-* H3) \* Hlw), Hlt) HR.
Proof using.
xsimpl_l_start' M. rewrite hwand_curry_eq. applys hwand_cancel.
Qed.
Lemma xsimpl_l_cancel_hwand_hstar_hempty : forall H2 H3 Hla Hlw Hlt HR,
Xsimpl (Hla, Hlw, ((H2 \-* H3) \* Hlt)) HR ->
Xsimpl (Hla, (((\[] \* H2) \-* H3) \* Hlw), Hlt) HR.
Proof using. xsimpl_l_start' M. Qed.
(** Transition lemmas for RHS extraction operations *)
Ltac xsimpl_r_start M :=
introv M;
match goal with HL: hprop*hprop*hprop |- _ =>
destruct HL as [[Hla Hlw] Hlt]; unfolds Xsimpl end.
Ltac xsimpl_r_start' M :=
xsimpl_r_start M; applys himpl_trans (rm M); hstars_simpl.
Lemma xsimpl_r_hempty : forall Hra Hrg Hrt HL,
Xsimpl HL (Hra, Hrg, Hrt) ->
Xsimpl HL (Hra, Hrg, (\[] \* Hrt)).
Proof using. xsimpl_r_start' M. Qed.
Lemma xsimpl_r_hwand_same : forall H Hra Hrg Hrt HL,
Xsimpl HL (Hra, Hrg, Hrt) ->
Xsimpl HL (Hra, Hrg, ((H \-* H) \* Hrt)).
Proof using. xsimpl_r_start' M. rewrite hwand_equiv. rew_heap~. Qed.
Lemma xsimpl_r_hpure : forall P Hra Hrg Hrt HL,
P ->
Xsimpl HL (Hra, Hrg, Hrt) ->
Xsimpl HL (Hra, Hrg, (\[P] \* Hrt)).
Proof using.
introv HP. xsimpl_r_start' M. applys* himpl_hempty_hpure.
Qed.
Lemma xsimpl_r_hexists : forall A (x:A) (J:A->hprop) Hra Hrg Hrt HL,
Xsimpl HL (Hra, Hrg, (J x \* Hrt)) ->
Xsimpl HL (Hra, Hrg, (hexists J \* Hrt)).
Proof using. xsimpl_r_start' M. applys* himpl_hexists_r. Qed.
Lemma xsimpl_r_id : forall A (x X:A) Hra Hrg Hrt HL,
(X = x) ->
Xsimpl HL (Hra, Hrg, Hrt) ->
Xsimpl HL (Hra, Hrg, (x ~> Id X \* Hrt)).
Proof using.
introv ->. xsimpl_r_start' M. rewrite repr_id.
applys* himpl_hempty_hpure.
Qed.
Lemma xsimpl_r_id_unify : forall A (x:A) Hra Hrg Hrt HL,
Xsimpl HL (Hra, Hrg, Hrt) ->
Xsimpl HL (Hra, Hrg, (x ~> Id x \* Hrt)).
Proof using. introv M. applys~ xsimpl_r_id. Qed.
Lemma xsimpl_r_keep : forall H Hra Hrg Hrt HL,
Xsimpl HL ((H \* Hra), Hrg, Hrt) ->
Xsimpl HL (Hra, Hrg, (H \* Hrt)).
Proof using. xsimpl_r_start' M. Qed.
(** Transition lemmas for [\Top] and [\GC] cancellation *)
(* [H] meant to be [\GC] or [\Top] *)
Lemma xsimpl_r_hgc_or_htop : forall H Hra Hrg Hrt HL,
Xsimpl HL (Hra, (H \* Hrg), Hrt) ->
Xsimpl HL (Hra, Hrg, (H \* Hrt)).
Proof using. xsimpl_r_start' M. Qed.
Lemma xsimpl_r_htop_replace_hgc : forall Hra Hrg Hrt HL,
Xsimpl HL (Hra, (\Top \* Hrg), Hrt) ->
Xsimpl HL (Hra, (\GC \* Hrg), (\Top \* Hrt)).
Proof using. xsimpl_r_start' M. applys himpl_hgc_r. xaffine. Qed.
Lemma xsimpl_r_hgc_drop : forall Hra Hrg Hrt HL,
Xsimpl HL (Hra, Hrg, Hrt) ->
Xsimpl HL (Hra, Hrg, (\GC \* Hrt)).
Proof using. xsimpl_r_start' M. applys himpl_hgc_r. xaffine. Qed.
Lemma xsimpl_r_htop_drop : forall Hra Hrg Hrt HL,
Xsimpl HL (Hra, Hrg, Hrt) ->
Xsimpl HL (Hra, Hrg, (\Top \* Hrt)).
Proof using. xsimpl_r_start' M. applys himpl_htop_r. Qed.
(** Transition lemmas for LHS/RHS cancellation
--- meant to be applied when Hlw and Hlt are empty *)
Ltac xsimpl_lr_start M :=
introv M; unfolds Xsimpl; rew_heap in *.
Ltac xsimpl_lr_start' M :=
xsimpl_lr_start M; hstars_simpl;
try (applys himpl_trans (rm M); hstars_simpl).
Lemma xsimpl_lr_cancel_same : forall H Hla Hlw Hlt Hra Hrg Hrt,
Xsimpl (Hla, Hlw, Hlt) (Hra, Hrg, Hrt) ->
Xsimpl ((H \* Hla), Hlw, Hlt) (Hra, Hrg, (H \* Hrt)).
Proof using. xsimpl_lr_start' M. Qed.