-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibSepReference.v
More file actions
4292 lines (3450 loc) · 131 KB
/
LibSepReference.v
File metadata and controls
4292 lines (3450 loc) · 131 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
(** * LibSepReference: Appendix - The Full Construction *)
(** This file provides an end-to-end construction of a weakest-precondition
style characteristic formula generator (the function named [wpgen] in
chapter [WPgen]), for a core programming language with programs assumed
to be in A-normal form, including support for records with up to 3 fields. *)
Set Implicit Arguments.
From SLF Require Export LibCore.
From SLF Require Export LibSepTLCbuffer LibSepVar LibSepFmap.
From SLF Require LibSepSimpl.
Module Fmap := LibSepFmap. (* Short name for Fmap module. *)
(* ################################################################# *)
(** * Imports *)
(* ================================================================= *)
(** ** Extensionality Axioms *)
(** These standard extensionality axioms may also be found in
the [LibAxiom.v] file associated with the TLC library. *)
Axiom functional_extensionality : forall A B (f g:A->B),
(forall x, f x = g x) ->
f = g.
Axiom propositional_extensionality : forall (P Q:Prop),
(P <-> Q) ->
P = Q.
(* ================================================================= *)
(** ** Variables *)
(** The file [LibSepVar.v], whoses definitions are imported in the header to
the present file, defines the type [var] as an alias for [string].
It also provides the boolean function [var_eq x y] to compare two variables,
and the tactic [case_var] to perform case analysis on expressions of the
form [if var_eq x y then .. else ..] that appear in the goal. *)
(* ================================================================= *)
(** ** Finite Maps *)
(** The file [LibSepFmap.v], which is bound by the short name [Fmap] in the
header, provides a formalization of finite maps. These maps are used to
represent heaps in the semantics. The library provides a tactic called
[fmap_disjoint] to automate disjointness proofs, and a tactic called
[fmap_eq] for proving equalities between heaps modulo associativity and
commutativity. Without these two tactics, proofs involving finite maps
would be much more tedious and fragile. *)
(* ################################################################# *)
(** * Source Language *)
(* ================================================================= *)
(** ** Syntax *)
(** The grammar of primitive operations includes a number of operations. *)
Inductive prim : Type :=
| val_ref : prim
| val_get : prim
| val_set : prim
| val_free : prim
| val_neg : prim
| val_opp : prim
| val_eq : prim
| val_add : prim
| val_neq : prim
| val_sub : prim
| val_mul : prim
| val_div : prim
| val_mod : prim
| val_rand : prim
| val_le : prim
| val_lt : prim
| val_ge : prim
| val_gt : prim
| val_ptr_add : prim.
(** Locations are defined as natural numbers. *)
Definition loc : Type := nat.
(** The null location corresponds to address zero. *)
Definition null : loc := 0%nat.
(** The grammar of closed values includes includes basic values such as [int]
and [bool], but also locations, closures. It also includes two special
values, [val_uninit] used in the formalization of arrays, and [val_error]
used for stating semantics featuring error-propagation. *)
Inductive val : Type :=
| val_unit : val
| val_bool : bool -> val
| val_int : int -> val
| val_loc : loc -> val
| val_prim : prim -> val
| val_fun : var -> trm -> val
| val_fix : var -> var -> trm -> val
| val_uninit : val
| val_error : val
(** The grammar of terms includes values, variables, functions, applications,
sequence, let-bindings, and conditions. Sequences are redundant with
let-bindings, but are useful in practice to avoid binding dummy names,
and serve on numerous occasion as a warm-up before proving results on
let-bindings. *)
with trm : Type :=
| trm_val : val -> trm
| trm_var : var -> trm
| trm_fun : var -> trm -> trm
| trm_fix : var -> var -> trm -> trm
| trm_app : trm -> trm -> trm
| trm_seq : trm -> trm -> trm
| trm_let : var -> trm -> trm -> trm
| trm_if : trm -> trm -> trm -> trm.
(** A state, a.k.a. [heap], consists of a finite map from location to values.
Records and arrays are represented as sets of consecutive cells, preceeded
by a header field describing the length of the block. *)
Definition heap : Type := fmap loc val.
(* ================================================================= *)
(** ** Rocq Tweaks *)
(** [h1 \u h2] is a notation for union of two heaps. *)
Notation "h1 \u h2" := (Fmap.union h1 h2)
(at level 37, right associativity).
(** Implicit types associated with meta-variables. *)
Implicit Types f : var.
Implicit Types b : bool.
Implicit Types p : loc.
Implicit Types n : int.
Implicit Types v w r vf vx : val.
Implicit Types t : trm.
Implicit Types h s : heap.
(** The types of values and terms are inhabited. *)
Global Instance Inhab_val : Inhab val.
Proof using. apply (Inhab_of_val val_unit). Qed.
Global Instance Inhab_trm : Inhab trm.
Proof using. apply (Inhab_of_val (trm_val val_unit)). Qed.
(** Coercions to improve conciseness in the statment of evaluation rules. *)
Coercion val_bool : bool >-> val.
Coercion val_int : Z >-> val.
Coercion val_loc : loc >-> val.
Coercion val_prim : prim >-> val.
Coercion trm_val : val >-> trm.
Coercion trm_var : var >-> trm.
Coercion trm_app : trm >-> Funclass.
(* ================================================================= *)
(** ** Values and Substitutions *)
(** The predicate [trm_is_val t] asserts that [t] is a value. *)
Definition trm_is_val (t:trm) : Prop :=
match t with trm_val v => True | _ => False end.
(** The standard capture-avoiding substitution, written [subst x v t]. *)
Fixpoint subst (y:var) (v':val) (t:trm) : trm :=
let aux t := subst y v' t in
let if_y_eq x t1 t2 := if var_eq x y then t1 else t2 in
match t with
| trm_val v => trm_val v
| trm_var x => if_y_eq x (trm_val v') t
| trm_fun x t1 => trm_fun x (if_y_eq x t1 (aux t1))
| trm_fix f x t1 => trm_fix f x (if_y_eq f t1 (if_y_eq x t1 (aux t1)))
| trm_app t1 t2 => trm_app (aux t1) (aux t2)
| trm_seq t1 t2 => trm_seq (aux t1) (aux t2)
| trm_let x t1 t2 => trm_let x (aux t1) (if_y_eq x t2 (aux t2))
| trm_if t0 t1 t2 => trm_if (aux t0) (aux t1) (aux t2)
end.
(* ================================================================= *)
(** ** Small-Step Semantics *)
(** The judgment [step s t s' t'] asserts that the configuration [(s,t)]
can take one reduction step towards the program configuration [(s',t')]. *)
Inductive step : heap -> trm -> heap -> trm -> Prop :=
(* Context rules *)
| step_seq_ctx : forall s1 s2 t1 t1' t2,
step s1 t1 s2 t1' ->
step s1 (trm_seq t1 t2) s2 (trm_seq t1' t2)
| step_let_ctx : forall s1 s2 x t1 t1' t2,
step s1 t1 s2 t1' ->
step s1 (trm_let x t1 t2) s2 (trm_let x t1' t2)
| step_app_arg1 : forall s1 s2 t1 t1' t2,
step s1 t1 s2 t1' ->
step s1 (trm_app t1 t2) s2 (trm_app t1' t2)
| step_app_arg2 : forall s1 s2 v1 t2 t2',
step s1 t2 s2 t2' ->
step s1 (trm_app v1 t2) s2 (trm_app v1 t2')
(* Reductions *)
| step_fun : forall s x t1,
step s (trm_fun x t1) s (val_fun x t1)
| step_fix : forall s f x t1,
step s (trm_fix f x t1) s (val_fix f x t1)
| step_app_fun : forall s v1 v2 x t1,
v1 = val_fun x t1 ->
step s (trm_app v1 v2) s (subst x v2 t1)
| step_app_fix : forall s v1 v2 f x t1,
v1 = val_fix f x t1 ->
step s (trm_app v1 v2) s (subst x v2 (subst f v1 t1))
| step_if : forall s b t1 t2,
step s (trm_if (val_bool b) t1 t2) s (if b then t1 else t2)
| step_seq : forall s t2 v1,
step s (trm_seq v1 t2) s t2
| step_let : forall s x t2 v1,
step s (trm_let x v1 t2) s (subst x v1 t2)
(* Unary operations *)
| step_neg : forall s b,
step s (val_neg (val_bool b)) s (val_bool (neg b))
| step_opp : forall s n,
step s (val_opp (val_int n)) s (val_int (- n))
| step_rand : forall s n n1,
0 <= n1 < n ->
step s (val_rand (val_int n)) s (val_int n1)
(* Binary operations *)
| step_eq : forall s v1 v2,
step s (val_eq v1 v2) s (val_bool (isTrue (v1 = v2)))
| step_neq : forall s v1 v2,
step s (val_neq v1 v2) s (val_bool (isTrue (v1 <> v2)))
| step_add : forall s n1 n2,
step s (val_add (val_int n1) (val_int n2)) s (val_int (n1 + n2))
| step_sub : forall s n1 n2,
step s (val_sub (val_int n1) (val_int n2)) s (val_int (n1 - n2))
| step_mul : forall s n1 n2,
step s (val_mul (val_int n1) (val_int n2)) s (val_int (n1 * n2))
| step_div : forall s n1 n2,
n2 <> 0 ->
step s (val_div (val_int n1) (val_int n2)) s (Z.quot n1 n2)
| step_mod : forall s n1 n2,
n2 <> 0 ->
step s (val_mod (val_int n1) (val_int n2)) s (Z.rem n1 n2)
| step_le : forall s n1 n2,
step s (val_le (val_int n1) (val_int n2)) s (val_bool (isTrue (n1 <= n2)))
| step_lt : forall s n1 n2,
step s (val_lt (val_int n1) (val_int n2)) s (val_bool (isTrue (n1 < n2)))
| step_ge : forall s n1 n2,
step s (val_ge (val_int n1) (val_int n2)) s (val_bool (isTrue (n1 >= n2)))
| step_gt : forall s n1 n2,
step s (val_gt (val_int n1) (val_int n2)) s (val_bool (isTrue (n1 > n2)))
| step_ptr_add : forall s p1 p2 n,
(p2:int) = p1 + n ->
step s (val_ptr_add (val_loc p1) (val_int n)) s (val_loc p2)
(* Heap operations *)
| step_ref : forall s v p,
~ Fmap.indom s p ->
step s (val_ref v) (Fmap.update s p v) (val_loc p)
| step_get : forall s p,
Fmap.indom s p ->
step s (val_get (val_loc p)) s (Fmap.read s p)
| step_set : forall s p v,
Fmap.indom s p ->
step s (val_set (val_loc p) v) (Fmap.update s p v) val_unit
| step_free : forall s p,
Fmap.indom s p ->
step s (val_free (val_loc p)) (Fmap.remove s p) val_unit.
(** The judgment [steps s t s' t'] corresponds to the reflexive-transitive
closure of [step]. Concretely, this judgment asserts that the configuration
[(s,t)] can reduce in zero, one, or several steps to [(s',t')]. *)
Inductive steps : heap -> trm -> heap -> trm -> Prop :=
| steps_refl : forall s t,
steps s t s t
| steps_step : forall s1 s2 s3 t1 t2 t3,
step s1 t1 s2 t2 ->
steps s2 t2 s3 t3 ->
steps s1 t1 s3 t3.
Lemma steps_of_step : forall s1 s2 t1 t2,
step s1 t1 s2 t2 ->
steps s1 t1 s2 t2.
Proof using. introv M. applys steps_step M. applys steps_refl. Qed.
Lemma steps_trans : forall s1 s2 s3 t1 t2 t3,
steps s1 t1 s2 t2 ->
steps s2 t2 s3 t3 ->
steps s1 t1 s3 t3.
Proof using. introv M1. induction M1; introv M2. { auto. } { constructors*. } Qed.
(** The predicate [reducible s t] asserts that the configuration [(s,t)]
can take a step. *)
Definition reducible (s:heap) (t:trm) : Prop :=
exists s' t', step s t s' t'.
(** The predicate [notstuck s t] asserts that [t] is a value or is reducible. *)
Definition notstuck (s:heap) (t:trm) : Prop :=
trm_is_val t \/ reducible s t.
(* ================================================================= *)
(** ** Omni-Big-Step Semantics of primitive operations *)
(** Evaluation rules for unary operations are captured by the predicate
[evalunop op v1 P], which asserts that [op v1] evaluates to a value
[v2] satisfying [P]. *)
Inductive evalunop : prim -> val -> (val->Prop) -> Prop :=
| evalunop_neg : forall b1,
evalunop val_neg (val_bool b1) (= val_bool (neg b1))
| evalunop_opp : forall n1,
evalunop val_opp (val_int n1) (= val_int (- n1))
| evalunop_rand : forall n,
n > 0 ->
evalunop val_rand (val_int n) (fun r => exists n1, r = val_int n1 /\ 0 <= n1 < n).
(** Evaluation rules for binary operations are captured by the predicate
[redupop op v1 v2 P], which asserts that [op v1 v2] evaluates to a
value [v3] satisfying [P]. *)
Inductive evalbinop : val -> val -> val -> (val->Prop) -> Prop :=
| evalbinop_eq : forall v1 v2,
evalbinop val_eq v1 v2 (= val_bool (isTrue (v1 = v2)))
| evalbinop_neq : forall v1 v2,
evalbinop val_neq v1 v2 (= val_bool (isTrue (v1 <> v2)))
| evalbinop_add : forall n1 n2,
evalbinop val_add (val_int n1) (val_int n2) (= val_int (n1 + n2))
| evalbinop_sub : forall n1 n2,
evalbinop val_sub (val_int n1) (val_int n2) (= val_int (n1 - n2))
| evalbinop_mul : forall n1 n2,
evalbinop val_mul (val_int n1) (val_int n2) (= val_int (n1 * n2))
| evalbinop_div : forall n1 n2,
n2 <> 0 ->
evalbinop val_div (val_int n1) (val_int n2) (= val_int (Z.quot n1 n2))
| evalbinop_mod : forall n1 n2,
n2 <> 0 ->
evalbinop val_mod (val_int n1) (val_int n2) (= val_int (Z.rem n1 n2))
| evalbinop_le : forall n1 n2,
evalbinop val_le (val_int n1) (val_int n2) (= val_bool (isTrue (n1 <= n2)))
| evalbinop_lt : forall n1 n2,
evalbinop val_lt (val_int n1) (val_int n2) (= val_bool (isTrue (n1 < n2)))
| evalbinop_ge : forall n1 n2,
evalbinop val_ge (val_int n1) (val_int n2) (= val_bool (isTrue (n1 >= n2)))
| evalbinop_gt : forall n1 n2,
evalbinop val_gt (val_int n1) (val_int n2) (= val_bool (isTrue (n1 > n2)))
| evalbinop_ptr_add : forall p1 p2 n,
(p2:int) = p1 + n ->
evalbinop val_ptr_add (val_loc p1) (val_int n) (= val_loc p2).
(* ================================================================= *)
(** ** Omni-Big-Step Semantics *)
Section Eval.
(** The predicate [purepost s P] converts a predicate [P:val->Prop] into
a postcondition of type [val->heap->Prop] that holds in the state [s]. *)
Definition purepost (s:heap) (P:val->Prop) : val->heap->Prop :=
fun v s' => P v /\ s' = s.
Definition purepostin (s:heap) (P:val->Prop) (Q:val->heap->Prop) : Prop :=
(* equivalent to [purepost S P ===> Q] *)
forall v, P v -> Q v s.
(** Omni-Big-step evaluation judgement, written [eval s t Q]. *)
Implicit Types Q : val->heap->Prop.
Inductive eval : heap -> trm -> (val->heap->Prop) -> Prop :=
| eval_val : forall s v Q,
Q v s ->
eval s (trm_val v) Q
| eval_fun : forall s x t1 Q,
Q (val_fun x t1) s ->
eval s (trm_fun x t1) Q
| eval_fix : forall s f x t1 Q,
Q (val_fix f x t1) s ->
eval s (trm_fix f x t1) Q
| eval_app_arg1 : forall s1 t1 t2 Q1 Q,
~ trm_is_val t1 ->
eval s1 t1 Q1 ->
(forall v1 s2, Q1 v1 s2 -> eval s2 (trm_app v1 t2) Q) ->
eval s1 (trm_app t1 t2) Q
| eval_app_arg2 : forall s1 v1 t2 Q1 Q,
~ trm_is_val t2 ->
eval s1 t2 Q1 ->
(forall v2 s2, Q1 v2 s2 -> eval s2 (trm_app v1 v2) Q) ->
eval s1 (trm_app v1 t2) Q
| eval_app_fun : forall s1 v1 v2 x t1 Q,
v1 = val_fun x t1 ->
eval s1 (subst x v2 t1) Q ->
eval s1 (trm_app v1 v2) Q
| eval_app_fix : forall s v1 v2 f x t1 Q,
v1 = val_fix f x t1 ->
eval s (subst x v2 (subst f v1 t1)) Q ->
eval s (trm_app v1 v2) Q
| eval_seq : forall Q1 s t1 t2 Q,
eval s t1 Q1 ->
(forall v1 s2, Q1 v1 s2 -> eval s2 t2 Q) ->
eval s (trm_seq t1 t2) Q
| eval_let : forall Q1 s x t1 t2 Q,
eval s t1 Q1 ->
(forall v1 s2, Q1 v1 s2 -> eval s2 (subst x v1 t2) Q) ->
eval s (trm_let x t1 t2) Q
| eval_if : forall s (b:bool) t1 t2 Q,
eval s (if b then t1 else t2) Q ->
eval s (trm_if (val_bool b) t1 t2) Q
| eval_unop : forall op s v1 P Q,
evalunop op v1 P ->
purepostin s P Q ->
eval s (op v1) Q
| eval_binop : forall op s v1 v2 P Q,
evalbinop op v1 v2 P ->
purepostin s P Q ->
eval s (op v1 v2) Q
| eval_ref : forall s v Q,
(forall p, ~ Fmap.indom s p ->
Q (val_loc p) (Fmap.update s p v)) ->
eval s (val_ref v) Q
| eval_get : forall s p Q,
Fmap.indom s p ->
Q (Fmap.read s p) s ->
eval s (val_get (val_loc p)) Q
| eval_set : forall s p v Q,
Fmap.indom s p ->
Q val_unit (Fmap.update s p v) ->
eval s (val_set (val_loc p) v) Q
| eval_free : forall s p Q,
Fmap.indom s p ->
Q val_unit (Fmap.remove s p) ->
eval s (val_free (val_loc p)) Q.
(** Specialized rule for values, to instantiate the postcondition *)
Lemma eval_val_minimal : forall s v,
eval s (trm_val v) (fun v' s' => (v' = v) /\ (s' = s)).
Proof using. intros. applys* eval_val. Qed.
(** Specialized evaluation rules for selected operations, to avoid the
indirection via [eval_unop] and [eval_binop] in the course. *)
Lemma eval_add : forall s n1 n2 Q,
Q (val_int (n1 + n2)) s ->
eval s (val_add (val_int n1) (val_int n2)) Q.
Proof using.
intros. applys* eval_binop.
{ applys* evalbinop_add. }
{ intros ? ->. auto. }
Qed.
Lemma eval_div : forall s n1 n2 Q,
n2 <> 0 ->
Q (val_int (Z.quot n1 n2)) s ->
eval s (val_div (val_int n1) (val_int n2)) Q.
Proof using.
intros. applys* eval_binop.
{ applys* evalbinop_div. }
{ intros ? ->. auto. }
Qed.
Lemma eval_rand : forall s n Q,
n > 0 ->
(forall n1, 0 <= n1 < n -> Q n1 s) ->
eval s (val_rand (val_int n)) Q.
Proof using.
intros. applys* eval_unop.
{ applys* evalunop_rand. }
{ intros ? (?&->&?). auto. }
Qed.
(** Derived rule for reasoning about an applications, without need to
perform a case analysis on whether the entities are already values. *)
Lemma eval_app_arg1' : forall s1 t1 t2 Q1 Q,
eval s1 t1 Q1 ->
(forall v1 s2, Q1 v1 s2 -> eval s2 (trm_app v1 t2) Q) ->
eval s1 (trm_app t1 t2) Q.
Proof using.
introv M1 M2. tests C1: (trm_is_val t1).
{ destruct t1; tryfalse. inverts M1. applys* M2. }
{ applys* eval_app_arg1. }
Qed.
Lemma eval_app_arg2' : forall s1 v1 t2 Q1 Q,
eval s1 t2 Q1 ->
(forall v2 s2, eval s2 (trm_app v1 v2) Q) ->
eval s1 (trm_app v1 t2) Q.
Proof using.
introv M1 M2. tests C1: (trm_is_val t2).
{ destruct t2; tryfalse. inverts M1. applys* M2. }
{ applys* eval_app_arg2. }
Qed.
(** [eval_like t1 t2] asserts that [t2] evaluates like [t1]. In particular,
this relation hold whenever [t2] reduces in small-step to [t1]. *)
Definition eval_like (t1 t2:trm) : Prop :=
forall s Q, eval s t1 Q -> eval s t2 Q.
End Eval.
(* ################################################################# *)
(** * Heap Predicates *)
(** We next define heap predicates and establish their properties. All this
material is wrapped in a module, allowing us to instantiate the functor from
chapter [LibSepSimpl] that defines the tactic [xsimpl]. *)
Module SepSimplArgs.
(* ================================================================= *)
(** ** Hprop and Entailment *)
Declare Scope hprop_scope.
Open Scope hprop_scope.
(** The type of heap predicates is named [hprop]. *)
Definition hprop := heap -> Prop.
(** Implicit types for meta-variables. *)
Implicit Types P : Prop.
Implicit Types H : hprop.
Implicit Types Q : val->hprop.
(** Entailment for heap predicates, written [H1 ==> H2]. This entailment
is linear. *)
Definition himpl (H1 H2:hprop) : Prop :=
forall h, H1 h -> H2 h.
Notation "H1 ==> H2" := (himpl H1 H2) (at level 55) : hprop_scope.
(** Entailment between postconditions, written [Q1 ===> Q2]. *)
Definition qimpl A (Q1 Q2:A->hprop) : Prop :=
forall (v:A), Q1 v ==> Q2 v.
Notation "Q1 ===> Q2" := (qimpl Q1 Q2) (at level 55) : hprop_scope.
(* ================================================================= *)
(** ** Definition of Heap Predicates *)
(** The core heap predicates are defined next, together with the
associated notation:
- [\[]] denotes the empty heap predicate
- [\[P]] denotes a pure fact
- [\Top] denotes the true heap predicate (affine)
- [p ~~> v] denotes a singleton heap
- [H1 \* H2] denotes the separating conjunction
- [Q1 \*+ H2] denotes the separating conjunction extending a postcondition
- [\exists x, H] denotes an existential quantifier
- [\forall x, H] denotes a universal quantifier
- [H1 \-* H2] denotes a magic wand between heap predicates
- [Q1 \--* Q2] denotes a magic wand between postconditions. *)
Definition hempty : hprop :=
fun h => (h = Fmap.empty).
Definition hsingle (p:loc) (v:val) : hprop :=
fun h => (h = Fmap.single p v).
Definition hstar (H1 H2 : hprop) : hprop :=
fun h => exists h1 h2, H1 h1
/\ H2 h2
/\ Fmap.disjoint h1 h2
/\ h = Fmap.union h1 h2.
Definition hexists A (J:A->hprop) : hprop :=
fun h => exists x, J x h.
Definition hforall (A : Type) (J : A -> hprop) : hprop :=
fun h => forall x, J x h.
Notation "\[]" := (hempty)
(at level 0) : hprop_scope.
Notation "p '~~>' v" := (hsingle p v) (at level 32) : hprop_scope.
Notation "H1 '\*' H2" := (hstar H1 H2)
(at level 41, right associativity) : hprop_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 ']'") : hprop_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 ']'") : hprop_scope.
(** The remaining operators are defined in terms of the preivous above,
rather than directly as functions over heaps. Doing so reduces the
amount of proofs, by allowing to better leverage the tactic [xsimpl]. *)
Definition hpure (P:Prop) : hprop :=
\exists (p:P), \[].
Definition htop : hprop :=
\exists (H:hprop), H.
Definition hwand (H1 H2 : hprop) : hprop :=
\exists H0, H0 \* hpure ((H1 \* H0) ==> H2).
Definition qwand A (Q1 Q2:A->hprop) : hprop :=
\forall x, hwand (Q1 x) (Q2 x).
Notation "\[ P ]" := (hpure P)
(at level 0, format "\[ P ]") : hprop_scope.
Notation "\Top" := (htop) : hprop_scope.
Notation "Q \*+ H" := (fun x => hstar (Q x) H)
(at level 40) : hprop_scope.
Notation "H1 \-* H2" := (hwand H1 H2)
(at level 43, right associativity) : hprop_scope.
Notation "Q1 \--* Q2" := (qwand Q1 Q2)
(at level 43) : hprop_scope.
(* ================================================================= *)
(** ** Basic Properties of Separation Logic Operators *)
(* ----------------------------------------------------------------- *)
(** *** Tactic for Automation *)
(** We set up [auto] to process goals of the form [h1 = h2] by calling
[fmap_eq], which proves equality modulo associativity and commutativity. *)
#[global] Hint Extern 1 (_ = _ :> heap) => fmap_eq.
(** We also set up [auto] to process goals of the form [Fmap.disjoint h1 h2]
by calling the tactic [fmap_disjoint], which essentially normalizes all
disjointness goals and hypotheses, split all conjunctions, and invokes
proof search with a base of hints specified in [LibSepFmap.v]. *)
Import Fmap.DisjointHints.
Tactic Notation "fmap_disjoint_pre" :=
subst; rew_disjoint; jauto_set.
#[global] Hint Extern 1 (Fmap.disjoint _ _) => fmap_disjoint_pre.
(* ----------------------------------------------------------------- *)
(** *** Properties of [himpl] and [qimpl] *)
Lemma himpl_refl : forall H,
H ==> H.
Proof using. introv M. auto. Qed.
#[global] Hint Resolve himpl_refl.
Lemma himpl_trans : forall H2 H1 H3,
(H1 ==> H2) ->
(H2 ==> H3) ->
(H1 ==> H3).
Proof using. introv M1 M2. unfolds* himpl. Qed.
Lemma himpl_trans_r : forall H2 H1 H3,
H2 ==> H3 ->
H1 ==> H2 ->
H1 ==> H3.
Proof using. introv M1 M2. applys* himpl_trans M2 M1. Qed.
Lemma himpl_antisym : forall H1 H2,
(H1 ==> H2) ->
(H2 ==> H1) ->
(H1 = H2).
Proof using. introv M1 M2. applys pred_ext_1. intros h. iff*. Qed.
Lemma hprop_op_comm : forall (op:hprop->hprop->hprop),
(forall H1 H2, op H1 H2 ==> op H2 H1) ->
(forall H1 H2, op H1 H2 = op H2 H1).
Proof using. introv M. intros. applys himpl_antisym; applys M. Qed.
Lemma qimpl_refl : forall A (Q:A->hprop),
Q ===> Q.
Proof using. intros. unfolds*. Qed.
#[global] Hint Resolve qimpl_refl.
(* ----------------------------------------------------------------- *)
(** *** Properties of [hempty] *)
Lemma hempty_intro :
\[] Fmap.empty.
Proof using. unfolds*. Qed.
Lemma hempty_inv : forall h,
\[] h ->
h = Fmap.empty.
Proof using. auto. Qed.
(* ----------------------------------------------------------------- *)
(** *** Properties of [hstar] *)
Lemma hstar_intro : forall H1 H2 h1 h2,
H1 h1 ->
H2 h2 ->
Fmap.disjoint h1 h2 ->
(H1 \* H2) (Fmap.union h1 h2).
Proof using. intros. exists~ h1 h2. Qed.
Lemma hstar_inv : forall H1 H2 h,
(H1 \* H2) h ->
exists h1 h2, H1 h1 /\ H2 h2 /\ Fmap.disjoint h1 h2 /\ h = Fmap.union h1 h2.
Proof using. introv M. applys M. Qed.
Lemma hstar_comm : forall H1 H2,
H1 \* H2 = H2 \* H1.
Proof using.
applys hprop_op_comm. unfold hprop, hstar. intros H1 H2.
intros h (h1&h2&M1&M2&D&U). rewrite~ Fmap.union_comm_of_disjoint in U.
exists* h2 h1.
Qed.
Lemma hstar_assoc : forall H1 H2 H3,
(H1 \* H2) \* H3 = H1 \* (H2 \* H3).
Proof using.
intros H1 H2 H3. applys himpl_antisym; intros h.
{ intros (h'&h3&(h1&h2&M3&M4&D'&U')&M2&D&U). subst h'.
exists h1 (h2 \+ h3). splits~. { applys* hstar_intro. } }
{ intros (h1&h'&M1&(h2&h3&M3&M4&D'&U')&D&U). subst h'.
exists (h1 \+ h2) h3. splits~. { applys* hstar_intro. } }
Qed.
Lemma hstar_hempty_l : forall H,
\[] \* H = H.
Proof using.
intros. applys himpl_antisym; intros h.
{ intros (h1&h2&M1&M2&D&U). forwards E: hempty_inv M1. subst.
rewrite~ Fmap.union_empty_l. }
{ intros M. exists (Fmap.empty:heap) h. splits~. { applys hempty_intro. } }
Qed.
Lemma hstar_hempty_r : forall H,
H \* \[] = H.
Proof using.
applys neutral_r_of_comm_neutral_l. applys~ hstar_comm. applys~ hstar_hempty_l.
Qed.
Lemma hstar_hexists : forall A (J:A->hprop) H,
(hexists J) \* H = hexists (fun x => (J x) \* H).
Proof using.
intros. applys himpl_antisym; intros h.
{ intros (h1&h2&(x&M1)&M2&D&U). exists~ x h1 h2. }
{ intros (x&(h1&h2&M1&M2&D&U)). exists h1 h2. splits~. { exists~ x. } }
Qed.
Lemma hstar_hforall : forall H A (J:A->hprop),
(hforall J) \* H ==> hforall (J \*+ H).
Proof using.
intros. intros h M. destruct M as (h1&h2&M1&M2&D&U). intros x. exists~ h1 h2.
Qed.
Lemma himpl_frame_l : forall H2 H1 H1',
H1 ==> H1' ->
(H1 \* H2) ==> (H1' \* H2).
Proof using. introv W (h1&h2&?). exists* h1 h2. Qed.
Lemma himpl_frame_r : forall H1 H2 H2',
H2 ==> H2' ->
(H1 \* H2) ==> (H1 \* H2').
Proof using.
introv M. do 2 rewrite (@hstar_comm H1). applys~ himpl_frame_l.
Qed.
Lemma himpl_frame_lr : forall H1 H1' H2 H2',
H1 ==> H1' ->
H2 ==> H2' ->
(H1 \* H2) ==> (H1' \* H2').
Proof using.
introv M1 M2. applys himpl_trans. applys~ himpl_frame_l M1. applys~ himpl_frame_r.
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_r M2. applys himpl_frame_l M1.
Qed.
Lemma himpl_hstar_trans_r : forall H1 H2 H3 H4,
H1 ==> H2 ->
H3 \* H2 ==> H4 ->
H3 \* H1 ==> H4.
Proof using.
introv M1 M2. applys himpl_trans_r M2. applys himpl_frame_r M1.
Qed.
(* ----------------------------------------------------------------- *)
(** *** Properties of [hpure] *)
Lemma hpure_intro : forall P,
P ->
\[P] Fmap.empty.
Proof using. introv M. exists M. unfolds*. Qed.
Lemma hpure_inv : forall P h,
\[P] h ->
P /\ h = Fmap.empty.
Proof using. introv (p&M). split~. Qed.
Lemma hstar_hpure_l : forall P H h,
(\[P] \* H) h = (P /\ H h).
Proof using.
intros. apply prop_ext. unfold hpure.
rewrite hstar_hexists. rewrite* hstar_hempty_l.
iff (p&M) (p&M). { split~. } { exists~ p. }
Qed.
Lemma hstar_hpure_r : forall P H h,
(H \* \[P]) h = (H h /\ P).
Proof using.
intros. rewrite hstar_comm. rewrite hstar_hpure_l. apply* prop_ext.
Qed.
Lemma himpl_hstar_hpure_r : forall P H H',
P ->
(H ==> H') ->
H ==> (\[P] \* H').
Proof using. introv HP W. intros h K. rewrite* hstar_hpure_l. Qed.
Lemma hpure_inv_hempty : forall P h,
\[P] h ->
P /\ \[] h.
Proof using.
introv M. rewrite <- hstar_hpure_l. rewrite~ hstar_hempty_r.
Qed.
Lemma hpure_intro_hempty : forall P h,
\[] h ->
P ->
\[P] h.
Proof using.
introv M N. rewrite <- (hstar_hempty_l \[P]). rewrite~ hstar_hpure_r.
Qed.
Lemma himpl_hempty_hpure : forall P,
P ->
\[] ==> \[P].
Proof using. introv HP. intros h Hh. applys* hpure_intro_hempty. Qed.
Lemma himpl_hstar_hpure_l : forall P H H',
(P -> H ==> H') ->
(\[P] \* H) ==> H'.
Proof using.
introv W Hh. rewrite hstar_hpure_l in Hh. applys* W.
Qed.
Lemma hempty_eq_hpure_true :
\[] = \[True].
Proof using.
applys himpl_antisym; intros h M.
{ applys* hpure_intro_hempty. }
{ forwards*: hpure_inv_hempty M. }
Qed.
Lemma hfalse_hstar_any : forall H,
\[False] \* H = \[False].
Proof using.
intros. applys himpl_antisym; intros h; rewrite hstar_hpure_l; intros M.
{ false*. } { lets: hpure_inv_hempty M. false*. }
Qed.
(* ----------------------------------------------------------------- *)
(** *** Properties of [hexists] *)
Lemma hexists_intro : forall A (x:A) (J:A->hprop) h,
J x h ->
(hexists J) h.
Proof using. intros. exists~ x. Qed.
Lemma hexists_inv : forall A (J:A->hprop) h,
(hexists J) h ->
exists x, J x h.
Proof using. intros. destruct H as [x H]. exists~ x. Qed.
Lemma himpl_hexists_l : forall A H (J:A->hprop),
(forall x, J x ==> H) ->
(hexists J) ==> H.
Proof using. introv W. intros h (x&Hh). applys* W. Qed.
Lemma himpl_hexists_r : forall A (x:A) H J,
(H ==> J x) ->
H ==> (hexists J).
Proof using. introv W. intros h. exists x. apply~ W. Qed.
Lemma himpl_hexists : forall A (J1 J2:A->hprop),
J1 ===> J2 ->
hexists J1 ==> hexists J2.
Proof using.
introv W. applys himpl_hexists_l. intros x. applys himpl_hexists_r W.
Qed.
(* ----------------------------------------------------------------- *)
(** *** Properties of [hforall] *)
Lemma hforall_intro : forall A (J:A->hprop) h,
(forall x, J x h) ->
(hforall J) h.
Proof using. introv M. applys* M. Qed.
Lemma hforall_inv : forall A (J:A->hprop) h,
(hforall J) h ->
forall x, J x h.
Proof using. introv M. applys* M. Qed.
Lemma himpl_hforall_r : forall A (J:A->hprop) H,
(forall x, H ==> J x) ->
H ==> (hforall J).
Proof using. introv M. intros h Hh x. apply~ M. Qed.
Lemma himpl_hforall_l : forall A x (J:A->hprop) H,
(J x ==> H) ->
(hforall J) ==> H.
Proof using. introv M. intros h Hh. apply~ M. Qed.
Lemma hforall_specialize : forall A (x:A) (J:A->hprop),
(hforall J) ==> (J x).
Proof using. intros. applys* himpl_hforall_l x. Qed.
Lemma himpl_hforall : forall A (J1 J2:A->hprop),
J1 ===> J2 ->
hforall J1 ==> hforall J2.
Proof using.
introv W. applys himpl_hforall_r. intros x. applys himpl_hforall_l W.
Qed.
(* ----------------------------------------------------------------- *)
(** *** Properties of [hwand] *)
Lemma hwand_equiv : forall H0 H1 H2,
(H0 ==> H1 \-* H2) <-> (H1 \* H0 ==> H2).
Proof using.
unfold hwand. iff M.
{ rewrite hstar_comm. applys himpl_hstar_trans_l (rm M).
rewrite hstar_hexists. applys himpl_hexists_l. intros H.
rewrite (hstar_comm H). rewrite hstar_assoc.
rewrite (hstar_comm H H1). applys~ himpl_hstar_hpure_l. }
{ applys himpl_hexists_r H0.
rewrite <- (hstar_hempty_r H0) at 1.
applys himpl_frame_r. applys himpl_hempty_hpure M. }
Qed.
Lemma himpl_hwand_r : forall H1 H2 H3,
H2 \* H1 ==> H3 ->
H1 ==> (H2 \-* H3).
Proof using. introv M. rewrite~ hwand_equiv. Qed.
Lemma himpl_hwand_r_inv : forall H1 H2 H3,
H1 ==> (H2 \-* H3) ->
H2 \* H1 ==> H3.
Proof using. introv M. rewrite~ <- hwand_equiv. Qed.
Lemma hwand_cancel : forall H1 H2,
H1 \* (H1 \-* H2) ==> H2.
Proof using. intros. applys himpl_hwand_r_inv. applys himpl_refl. Qed.
Arguments hwand_cancel : clear implicits.
Lemma himpl_hempty_hwand_same : forall H,
\[] ==> (H \-* H).
Proof using. intros. apply himpl_hwand_r. rewrite~ hstar_hempty_r. Qed.
Lemma hwand_hempty_l : forall H,
(\[] \-* H) = H.
Proof using.
intros. applys himpl_antisym.
{ rewrite <- hstar_hempty_l at 1. applys hwand_cancel. }
{ rewrite hwand_equiv. rewrite~ hstar_hempty_l. }
Qed.
Lemma hwand_hpure_l : forall P H,
P ->
(\[P] \-* H) = H.
Proof using.
introv HP. applys himpl_antisym.
{ lets K: hwand_cancel \[P] H. applys himpl_trans_r K.
applys* himpl_hstar_hpure_r. }
{ rewrite hwand_equiv. applys* himpl_hstar_hpure_l. }
Qed.
Lemma hwand_curry : forall H1 H2 H3,