-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpkg_invariants.v
1607 lines (1400 loc) · 46.1 KB
/
pkg_invariants.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
(** Invariants on state
These can be very useful when proving program equivalences.
*)
From Coq Require Import Utf8.
From SSProve.Relational Require Import OrderEnrichedCategory
OrderEnrichedRelativeMonadExamples.
Set Warnings "-ambiguous-paths,-notation-overridden,-notation-incompatible-format".
From mathcomp Require Import ssrnat ssreflect ssrfun ssrbool ssrnum eqtype
choice reals distr seq all_algebra fintype realsum.
Set Warnings "ambiguous-paths,notation-overridden,notation-incompatible-format".
From HB Require Import structures.
From extructures Require Import ord fset fmap.
From SSProve.Mon Require Import SPropBase.
From SSProve.Crypt Require Import Prelude Axioms ChoiceAsOrd SubDistr Couplings
RulesStateProb UniformStateProb UniformDistrLemmas StateTransfThetaDens
StateTransformingLaxMorph choice_type pkg_core_definition pkg_notation
pkg_tactics pkg_composition pkg_heap pkg_semantics pkg_lookup pkg_advantage.
Require Import Equations.Prop.DepElim.
From Equations Require Import Equations.
(* Must come after importing Equations.Equations, who knows why. *)
From SSProve.Crypt Require Import FreeProbProg.
Import Num.Theory.
Set Equations With UIP.
Set Equations Transparent.
Import SPropNotations.
Import PackageNotation.
Import RSemanticNotation.
Set Bullet Behavior "Strict Subproofs".
Set Default Goal Selector "!".
Set Primitive Projections.
#[local] Open Scope rsemantic_scope.
#[local] Open Scope fset.
#[local] Open Scope fset_scope.
#[local] Open Scope type_scope.
#[local] Open Scope package_scope.
#[local] Open Scope ring_scope.
#[local] Open Scope real_scope.
Definition precond := heap * heap → Prop.
Definition postcond A B := (A * heap) → (B * heap) → Prop.
Definition INV (L : {fset Location})
(I : heap_choiceType * heap_choiceType → Prop) :=
∀ s1 s2,
(I (s1, s2) → ∀ l, l \in L → get_heap s1 l = get_heap s2 l) ∧
(I (s1, s2) → ∀ l v, l \in L → I (set_heap s1 l v, set_heap s2 l v)).
Definition INV' (L1 L2 : {fset Location})
(I : heap_choiceType * heap_choiceType → Prop) :=
∀ s1 s2,
(I (s1, s2) → ∀ l, l \notin L1 → l \notin L2 →
get_heap s1 l = get_heap s2 l) ∧
(I (s1, s2) → ∀ l v, l \notin L1 → l \notin L2 →
I (set_heap s1 l v, set_heap s2 l v)).
Lemma INV'_to_INV (L L1 L2 : {fset Location})
(I : heap_choiceType * heap_choiceType → Prop)
(HINV' : INV' L1 L2 I)
(Hdisjoint1 : fdisjoint L L1) (Hdisjoint2 : fdisjoint L L2) :
INV L I.
Proof.
unfold INV.
intros s1 s2. split.
- intros hi l hin.
apply HINV'.
+ assumption.
+ move: Hdisjoint1. move /fdisjointP => Hdisjoint1.
apply Hdisjoint1. assumption.
+ move: Hdisjoint2. move /fdisjointP => Hdisjoint2.
apply Hdisjoint2. assumption.
- intros hi l v hin.
apply HINV'.
+ assumption.
+ move: Hdisjoint1. move /fdisjointP => Hdisjoint1.
apply Hdisjoint1. assumption.
+ move: Hdisjoint2. move /fdisjointP => Hdisjoint2.
apply Hdisjoint2. assumption.
Qed.
Class Invariant L₀ L₁ inv := {
inv_INV' : INV' L₀ L₁ inv ;
inv_empty : inv (empty_heap, empty_heap)
}.
Create HintDb ssprove_invariant.
#[export] Hint Extern 100 =>
shelve
: ssprove_invariant.
Ltac ssprove_invariant :=
(unshelve typeclasses eauto with ssprove_invariant) ; shelve_unifiable.
Lemma Invariant_eq :
∀ L₀ L₁,
Invariant L₀ L₁ (λ '(s₀, s₁), s₀ = s₁).
Proof.
split.
- intros s₀ s₁. split.
+ intro e. rewrite e. auto.
+ intro e. rewrite e. auto.
- reflexivity.
Qed.
#[export] Hint Extern 10 (Invariant _ _ (λ '(s₀, s₁), s₀ = s₁)) =>
eapply Invariant_eq
: typeclass_instances ssprove_invariant.
Definition heap_ignore (L : {fset Location}) : precond :=
λ '(h₀, h₁),
∀ (ℓ : Location), ℓ \notin L → get_heap h₀ ℓ = get_heap h₁ ℓ.
Arguments heap_ignore : simpl never.
Lemma heap_ignore_empty :
∀ L,
heap_ignore L (empty_heap, empty_heap).
Proof.
intros L ℓ hℓ. reflexivity.
Qed.
Lemma INV'_heap_ignore :
∀ L L₀ L₁,
fsubset L (L₀ :|: L₁) →
INV' L₀ L₁ (heap_ignore L).
Proof.
intros L L₀ L₁ hs h₀ h₁. split.
- intros hh ℓ n₀ n₁.
eapply hh.
apply /negP. intro h.
eapply injectSubset in h. 2: eauto.
rewrite in_fsetU in h. move: h => /orP [h | h].
+ rewrite h in n₀. discriminate.
+ rewrite h in n₁. discriminate.
- intros h ℓ v n₀ n₁ ℓ' n.
destruct (ℓ' != ℓ) eqn:e.
+ rewrite get_set_heap_neq. 2: auto.
rewrite get_set_heap_neq. 2: auto.
apply h. auto.
+ move: e => /eqP e. subst.
rewrite !get_set_heap_eq. reflexivity.
Qed.
Lemma Invariant_heap_ignore :
∀ L L₀ L₁,
fsubset L (L₀ :|: L₁) →
Invariant L₀ L₁ (heap_ignore L).
Proof.
intros L L₀ L₁ h. split.
- apply INV'_heap_ignore. auto.
- apply heap_ignore_empty.
Qed.
#[export] Hint Extern 10 (Invariant _ _ (heap_ignore _)) =>
eapply Invariant_heap_ignore
: (* typeclass_instances *) ssprove_invariant.
(* Not-really-symmetric (in use) conjunction of invariants *)
Definition inv_conj (inv inv' : precond) :=
λ s, inv s ∧ inv' s.
Notation "I ⋊ J" :=
(inv_conj I J) (at level 19, left associativity) : package_scope.
Arguments inv_conj : simpl never.
Class SemiInvariant (L₀ L₁ : {fset Location}) (sinv : precond) := {
sinv_set :
∀ s₀ s₁ ℓ v,
ℓ \notin L₀ →
ℓ \notin L₁ →
sinv (s₀, s₁) →
sinv (set_heap s₀ ℓ v, set_heap s₁ ℓ v) ;
sinv_empty : sinv (empty_heap, empty_heap)
}.
Lemma Invariant_inv_conj :
∀ L₀ L₁ inv sinv,
Invariant L₀ L₁ inv →
SemiInvariant L₀ L₁ sinv →
Invariant L₀ L₁ (inv ⋊ sinv).
Proof.
intros L₀ L₁ inv sinv [his hie] [hss hse]. split.
- intros s₀ s₁. specialize (his s₀ s₁). destruct his. split.
+ intros []. eauto.
+ intros [] ℓ v h₀ h₁. split. all: eauto.
- split. all: eauto.
Qed.
#[export] Hint Extern 10 (Invariant _ _ (_ ⋊ _)) =>
eapply Invariant_inv_conj
: typeclass_instances ssprove_invariant.
Definition couple_lhs ℓ ℓ' (R : _ → _ → Prop) : precond :=
λ '(s₀, s₁), R (get_heap s₀ ℓ) (get_heap s₀ ℓ').
Lemma SemiInvariant_couple_lhs :
∀ L₀ L₁ ℓ ℓ' (R : _ → _ → Prop),
ℓ \in L₀ :|: L₁ →
ℓ' \in L₀ :|: L₁ →
R (get_heap empty_heap ℓ) (get_heap empty_heap ℓ') →
SemiInvariant L₀ L₁ (couple_lhs ℓ ℓ' R).
Proof.
intros L₀ L₁ ℓ ℓ' h hℓ hℓ' he. split.
- intros s₀ s₁ l v hl₀ hl₁ ?.
assert (hl : l \notin L₀ :|: L₁).
{ rewrite in_fsetU. rewrite (negbTE hl₀) (negbTE hl₁). reflexivity. }
unfold couple_lhs.
rewrite !get_set_heap_neq.
+ auto.
+ apply /negP => /eqP e. subst. rewrite hℓ' in hl. discriminate.
+ apply /negP => /eqP e. subst. rewrite hℓ in hl. discriminate.
- simpl. auto.
Qed.
Arguments couple_lhs : simpl never.
#[export] Hint Extern 10 (SemiInvariant _ _ (couple_lhs _ _ _)) =>
eapply SemiInvariant_couple_lhs
: (* typeclass_instances *) ssprove_invariant.
Definition couple_rhs ℓ ℓ' (R : _ → _ → Prop) : precond :=
λ '(s₀, s₁), R (get_heap s₁ ℓ) (get_heap s₁ ℓ').
Lemma SemiInvariant_couple_rhs :
∀ L₀ L₁ ℓ ℓ' (R : _ → _ → Prop),
ℓ \in L₀ :|: L₁ →
ℓ' \in L₀ :|: L₁ →
R (get_heap empty_heap ℓ) (get_heap empty_heap ℓ') →
SemiInvariant L₀ L₁ (couple_rhs ℓ ℓ' R).
Proof.
intros L₀ L₁ ℓ ℓ' h hℓ hℓ' he. split.
- intros s₀ s₁ l v hl₀ hl₁ ?.
assert (hl : l \notin L₀ :|: L₁).
{ rewrite in_fsetU. rewrite (negbTE hl₀) (negbTE hl₁). reflexivity. }
unfold couple_rhs.
rewrite !get_set_heap_neq.
+ auto.
+ apply /negP => /eqP e. subst. rewrite hℓ' in hl. discriminate.
+ apply /negP => /eqP e. subst. rewrite hℓ in hl. discriminate.
- simpl. auto.
Qed.
Arguments couple_rhs : simpl never.
#[export] Hint Extern 10 (SemiInvariant _ _ (couple_rhs _ _ _)) =>
eapply SemiInvariant_couple_rhs
: (* typeclass_instances *) ssprove_invariant.
(* TODO triple_lhs, or even better, something more generic *)
Definition triple_rhs ℓ₁ ℓ₂ ℓ₃ (R : _ → _ → _ → Prop) : precond :=
λ '(s₀, s₁), R (get_heap s₁ ℓ₁) (get_heap s₁ ℓ₂) (get_heap s₁ ℓ₃).
Lemma SemiInvariant_triple_rhs :
∀ L₀ L₁ ℓ₁ ℓ₂ ℓ₃ (R : _ → _ → _ → Prop),
ℓ₁ \in L₀ :|: L₁ →
ℓ₂ \in L₀ :|: L₁ →
ℓ₃ \in L₀ :|: L₁ →
R (get_heap empty_heap ℓ₁) (get_heap empty_heap ℓ₂) (get_heap empty_heap ℓ₃) →
SemiInvariant L₀ L₁ (triple_rhs ℓ₁ ℓ₂ ℓ₃ R).
Proof.
intros L₀ L₁ ℓ₁ ℓ₂ ℓ₃ R h₁ h₂ h₃ he. split.
- intros s₀ s₁ ℓ v hℓ₀ hℓ₁ ?.
assert (hℓ : ℓ \notin L₀ :|: L₁).
{ rewrite in_fsetU. rewrite (negbTE hℓ₀) (negbTE hℓ₁). reflexivity. }
unfold triple_rhs.
rewrite !get_set_heap_neq.
+ auto.
+ apply /negP => /eqP e. subst. rewrite h₃ in hℓ. discriminate.
+ apply /negP => /eqP e. subst. rewrite h₂ in hℓ. discriminate.
+ apply /negP => /eqP e. subst. rewrite h₁ in hℓ. discriminate.
- simpl. auto.
Qed.
Arguments triple_rhs : simpl never.
#[export] Hint Extern 10 (SemiInvariant _ _ (triple_rhs _ _ _ _)) =>
eapply SemiInvariant_triple_rhs
: (* typeclass_instances *) ssprove_invariant.
Inductive side := lhs | rhs.
Definition choose_heap s₀ s₁ (s : side) : heap :=
match s with
| lhs => s₀
| rhs => s₁
end.
Lemma choose_heap_same :
∀ s si,
choose_heap s s si = s.
Proof.
intros s si.
destruct si.
all: reflexivity.
Qed.
Fixpoint locRel (l : list (Location * side)) :=
match l with
| (ℓ, _) :: l => ℓ → locRel l
| [::] => Prop
end.
Fixpoint heapLocRel (s₀ s₁ : heap) l (R : locRel l) : Prop :=
match l return locRel l → Prop with
| (ℓ, s) :: l =>
λ R, heapLocRel s₀ s₁ l (R (get_heap (choose_heap s₀ s₁ s) ℓ))
| [::] => λ R, R
end R.
Definition loc_rel (l : list (Location * side)) (R : locRel l) : precond :=
λ '(s₀, s₁), heapLocRel s₀ s₁ l R.
Lemma SemiInvariant_loc_rel :
∀ L₀ L₁ l (R : locRel l),
List.forallb (λ '(ℓ,_), ℓ \in L₀ :|: L₁) l →
heapLocRel empty_heap empty_heap l R →
SemiInvariant L₀ L₁ (loc_rel l R).
Proof.
intros L₀ L₁ l R h he. split.
- intros s₀ s₁ ℓ v hℓ₀ hℓ₁ hh.
assert (hℓ : ℓ \notin L₀ :|: L₁).
{ rewrite in_fsetU. rewrite (negbTE hℓ₀) (negbTE hℓ₁). reflexivity. }
unfold loc_rel.
induction l as [| [ℓ' si] l ih] in s₀, s₁, R, hh, h |- *.
+ assumption.
+ simpl. apply ih.
* simpl in h. move: h => /andP [_ h]. assumption.
* simpl in h. move: h => /andP [h _].
destruct si. all: simpl.
all: rewrite !get_set_heap_neq.
1,3: assumption.
-- apply /negP => /eqP e. subst. rewrite h in hℓ. discriminate.
-- apply /negP => /eqP e. subst. rewrite h in hℓ. discriminate.
- simpl. assumption.
Qed.
Arguments loc_rel : simpl never.
#[export] Hint Extern 10 (SemiInvariant _ _ (loc_rel _ _)) =>
eapply SemiInvariant_loc_rel
: (* typeclass_instances *) ssprove_invariant.
Definition get_pre_cond ℓ (pre : precond) :=
∀ s₀ s₁, pre (s₀, s₁) → get_heap s₀ ℓ = get_heap s₁ ℓ.
Lemma get_pre_cond_heap_ignore :
∀ (ℓ : Location) (L : {fset Location}),
ℓ \notin L →
get_pre_cond ℓ (heap_ignore L).
Proof.
intros ℓ L hℓ s₀ s₁ h. apply h. auto.
Qed.
#[export] Hint Extern 10 (get_pre_cond _ (heap_ignore _)) =>
apply get_pre_cond_heap_ignore
: ssprove_invariant.
Lemma get_pre_cond_conj :
∀ ℓ (pre spre : precond),
get_pre_cond ℓ pre →
get_pre_cond ℓ (pre ⋊ spre).
Proof.
intros ℓ pre spre h s₀ s₁ []. apply h. auto.
Qed.
#[export] Hint Extern 10 (get_pre_cond _ (_ ⋊ _)) =>
apply get_pre_cond_conj
: ssprove_invariant.
Definition put_pre_cond ℓ v (pre : precond) :=
∀ s₀ s₁, pre (s₀, s₁) → pre (set_heap s₀ ℓ v, set_heap s₁ ℓ v).
Lemma put_pre_cond_heap_ignore :
∀ ℓ v L,
put_pre_cond ℓ v (heap_ignore L).
Proof.
intros ℓ v L s₀ s₁ h ℓ' hn.
destruct (ℓ' != ℓ) eqn:e.
- rewrite get_set_heap_neq. 2: auto.
rewrite get_set_heap_neq. 2: auto.
apply h. auto.
- move: e => /eqP e. subst.
rewrite !get_set_heap_eq. reflexivity.
Qed.
#[export] Hint Extern 10 (put_pre_cond _ _ (heap_ignore _)) =>
apply put_pre_cond_heap_ignore
: ssprove_invariant.
Lemma put_pre_cond_conj :
∀ ℓ v pre spre,
put_pre_cond ℓ v pre →
put_pre_cond ℓ v spre →
put_pre_cond ℓ v (pre ⋊ spre).
Proof.
intros ℓ v pre spre h hs.
intros s₀ s₁ []. split. all: auto.
Qed.
#[export] Hint Extern 10 (put_pre_cond _ _ (_ ⋊ _)) =>
apply put_pre_cond_conj
: ssprove_invariant.
Lemma put_pre_cond_couple_lhs :
∀ ℓ v ℓ₀ ℓ₁ h,
ℓ₀ != ℓ →
ℓ₁ != ℓ →
put_pre_cond ℓ v (couple_lhs ℓ₀ ℓ₁ h).
Proof.
intros ℓ v ℓ₀ ℓ₁ h n₀ n₁ s₀ s₁ hc.
unfold couple_lhs in *.
rewrite !get_set_heap_neq. all: auto.
Qed.
#[export] Hint Extern 10 (put_pre_cond _ _ (couple_lhs _ _ _)) =>
apply put_pre_cond_couple_lhs
: ssprove_invariant.
Lemma put_pre_cond_couple_rhs :
∀ ℓ v ℓ₀ ℓ₁ h,
ℓ₀ != ℓ →
ℓ₁ != ℓ →
put_pre_cond ℓ v (couple_rhs ℓ₀ ℓ₁ h).
Proof.
intros ℓ v ℓ₀ ℓ₁ h n₀ n₁ s₀ s₁ hc.
unfold couple_rhs in *.
rewrite !get_set_heap_neq. all: auto.
Qed.
#[export] Hint Extern 10 (put_pre_cond _ _ (couple_rhs _ _ _)) =>
apply put_pre_cond_couple_rhs
: ssprove_invariant.
Lemma put_pre_cond_triple_rhs :
∀ ℓ v ℓ₁ ℓ₂ ℓ₃ h,
ℓ₁ != ℓ →
ℓ₂ != ℓ →
ℓ₃ != ℓ →
put_pre_cond ℓ v (triple_rhs ℓ₁ ℓ₂ ℓ₃ h).
Proof.
intros ℓ v ℓ₁ ℓ₂ ℓ₃ h n₁ n₂ n₃ s₀ s₁ hc.
unfold triple_rhs in *.
rewrite !get_set_heap_neq. all: auto.
Qed.
#[export] Hint Extern 10 (put_pre_cond _ _ (triple_rhs _ _ _ _)) =>
apply put_pre_cond_triple_rhs
: ssprove_invariant.
(* TODO MOVE *)
Lemma notin_cons :
∀ (T : eqType) (y : T) (s : seq T) (x : T),
(x \notin y :: s) = (x != y) && (x \notin s).
Proof.
intros T y s x.
rewrite in_cons.
rewrite Bool.negb_orb. reflexivity.
Qed.
Lemma put_pre_cond_loc_rel :
∀ ℓ v l (R : locRel l),
ℓ \notin (map fst l) →
put_pre_cond ℓ v (loc_rel l R).
Proof.
intros ℓ v l R h s₀ s₁ hc.
unfold loc_rel in *.
induction l as [| [ℓ' si] l ih].
- assumption.
- simpl. simpl in h. simpl in hc.
rewrite notin_cons in h.
move: h => /andP [hn h].
apply ih.
+ assumption.
+ destruct si.
all: rewrite !get_set_heap_neq.
1,3: auto.
all: rewrite eq_sym.
all: auto.
Qed.
#[export] Hint Extern 10 (put_pre_cond _ _ (loc_rel _ _)) =>
apply put_pre_cond_loc_rel
: ssprove_invariant.
(** Predicates on invariants
The idea is to use them as side-conditions for rules.
*)
Class Syncs ℓ pre :=
is_tracking : ∀ s₀ s₁, pre (s₀, s₁) → get_heap s₀ ℓ = get_heap s₁ ℓ.
Class Couples_lhs ℓ ℓ' R pre :=
is_coupling_lhs : ∀ s, pre s → couple_lhs ℓ ℓ' R s.
Class Couples_rhs ℓ ℓ' R pre :=
is_coupling_rhs : ∀ s, pre s → couple_rhs ℓ ℓ' R s.
Class Triple_rhs ℓ₁ ℓ₂ ℓ₃ R pre :=
is_triple_rhs : ∀ s, pre s → triple_rhs ℓ₁ ℓ₂ ℓ₃ R s.
Class LocRel l R pre :=
has_loc_rel : ∀ s, pre s → loc_rel l R s.
Lemma Syncs_eq :
∀ ℓ, Syncs ℓ (λ '(s₀, s₁), s₀ = s₁).
Proof.
intros ℓ s₀ s₁ e. subst. reflexivity.
Qed.
#[export] Hint Extern 10 (Syncs _ (λ '(s₀, s₁), s₀ = s₁)) =>
apply Syncs_eq
: typeclass_instances ssprove_invariant.
Lemma Syncs_heap_ignore :
∀ ℓ L,
ℓ \notin L →
Syncs ℓ (heap_ignore L).
Proof.
intros ℓ L hn s₀ s₁ h.
apply h. auto.
Qed.
#[export] Hint Extern 10 (Syncs _ (heap_ignore _)) =>
apply Syncs_heap_ignore
: typeclass_instances ssprove_invariant.
Lemma Syncs_conj :
∀ ℓ (pre spre : precond),
Syncs ℓ pre →
Syncs ℓ (pre ⋊ spre).
Proof.
intros ℓ pre spre hpre s₀ s₁ [].
apply hpre. auto.
Qed.
#[export] Hint Extern 10 (Syncs _ (_ ⋊ _)) =>
apply Syncs_conj
: typeclass_instances ssprove_invariant.
Lemma Couples_couple_lhs :
∀ ℓ ℓ' R,
Couples_lhs ℓ ℓ' R (couple_lhs ℓ ℓ' R).
Proof.
intros ℓ ℓ' R s h. auto.
Qed.
#[export] Hint Extern 10 (Couples_lhs _ _ _ (couple_lhs _ _ _)) =>
eapply Couples_couple_lhs
: typeclass_instances ssprove_invariant.
Lemma Couples_couple_rhs :
∀ ℓ ℓ' R,
Couples_rhs ℓ ℓ' R (couple_rhs ℓ ℓ' R).
Proof.
intros ℓ ℓ' R s h. auto.
Qed.
#[export] Hint Extern 10 (Couples_rhs _ _ _ (couple_rhs _ _ _)) =>
eapply Couples_couple_rhs
: typeclass_instances ssprove_invariant.
Lemma Triple_triple_rhs :
∀ ℓ₁ ℓ₂ ℓ₃ R,
Triple_rhs ℓ₁ ℓ₂ ℓ₃ R (triple_rhs ℓ₁ ℓ₂ ℓ₃ R).
Proof.
intros ℓ₁ ℓ₂ ℓ₃ R s h. auto.
Qed.
#[export] Hint Extern 10 (Triple_rhs _ _ _ _ (triple_rhs _ _ _ _)) =>
eapply Triple_triple_rhs
: typeclass_instances ssprove_invariant.
Lemma LocRel_loc_rel :
∀ l R,
LocRel l R (loc_rel l R).
Proof.
intros l R s h. auto.
Qed.
#[export] Hint Extern 10 (LocRel _ _ (loc_rel _ _)) =>
eapply LocRel_loc_rel
: typeclass_instances ssprove_invariant.
Lemma Couples_lhs_conj_right :
∀ ℓ ℓ' R (pre spre : precond),
Couples_lhs ℓ ℓ' R spre →
Couples_lhs ℓ ℓ' R (pre ⋊ spre).
Proof.
intros ℓ ℓ' R pre spre h s [].
apply h. auto.
Qed.
Lemma Couples_lhs_conj_left :
∀ ℓ ℓ' R (pre spre : precond),
Couples_lhs ℓ ℓ' R pre →
Couples_lhs ℓ ℓ' R (pre ⋊ spre).
Proof.
intros ℓ ℓ' R pre spre h s [].
apply h. auto.
Qed.
#[export] Hint Extern 9 (Couples_lhs _ _ _ (_ ⋊ _)) =>
eapply Couples_lhs_conj_right
: typeclass_instances ssprove_invariant.
#[export] Hint Extern 11 (Couples_lhs _ _ _ (_ ⋊ _)) =>
eapply Couples_lhs_conj_left
: typeclass_instances ssprove_invariant.
Lemma Couples_rhs_conj_right :
∀ ℓ ℓ' R (pre spre : precond),
Couples_rhs ℓ ℓ' R spre →
Couples_rhs ℓ ℓ' R (pre ⋊ spre).
Proof.
intros ℓ ℓ' R pre spre h s [].
apply h. auto.
Qed.
Lemma Couples_rhs_conj_left :
∀ ℓ ℓ' R (pre spre : precond),
Couples_rhs ℓ ℓ' R pre →
Couples_rhs ℓ ℓ' R (pre ⋊ spre).
Proof.
intros ℓ ℓ' R pre spre h s [].
apply h. auto.
Qed.
#[export] Hint Extern 9 (Couples_rhs _ _ _ (_ ⋊ _)) =>
eapply Couples_rhs_conj_right
: typeclass_instances ssprove_invariant.
#[export] Hint Extern 11 (Couples_rhs _ _ _ (_ ⋊ _)) =>
eapply Couples_rhs_conj_left
: typeclass_instances ssprove_invariant.
Lemma Triple_rhs_conj_right :
∀ ℓ₁ ℓ₂ ℓ₃ R (pre spre : precond),
Triple_rhs ℓ₁ ℓ₂ ℓ₃ R spre →
Triple_rhs ℓ₁ ℓ₂ ℓ₃ R (pre ⋊ spre).
Proof.
intros ℓ₁ ℓ₂ ℓ₃ R pre spre h s [].
apply h. auto.
Qed.
Lemma Triple_rhs_conj_left :
∀ ℓ₁ ℓ₂ ℓ₃ R (pre spre : precond),
Triple_rhs ℓ₁ ℓ₂ ℓ₃ R pre →
Triple_rhs ℓ₁ ℓ₂ ℓ₃ R (pre ⋊ spre).
Proof.
intros ℓ₁ ℓ₂ ℓ₃ R pre spre h s [].
apply h. auto.
Qed.
#[export] Hint Extern 9 (Triple_rhs _ _ _ _ (_ ⋊ _)) =>
eapply Triple_rhs_conj_right
: typeclass_instances ssprove_invariant.
#[export] Hint Extern 11 (Triple_rhs _ _ _ _ (_ ⋊ _)) =>
eapply Triple_rhs_conj_left
: typeclass_instances ssprove_invariant.
Lemma LocRel_conj_right :
∀ l R (pre spre : precond),
LocRel l R spre →
LocRel l R (pre ⋊ spre).
Proof.
intros l R pre spre h s [].
apply h. auto.
Qed.
Lemma LocRel_conj_left :
∀ l R (pre spre : precond),
LocRel l R pre →
LocRel l R (pre ⋊ spre).
Proof.
intros l R pre spre h s [].
apply h. auto.
Qed.
#[export] Hint Extern 9 (LocRel _ _ (_ ⋊ _)) =>
eapply LocRel_conj_right
: typeclass_instances ssprove_invariant.
#[export] Hint Extern 11 (LocRel _ _ (_ ⋊ _)) =>
eapply LocRel_conj_left
: typeclass_instances ssprove_invariant.
Definition rem_lhs ℓ v : precond :=
λ '(s₀, s₁), get_heap s₀ ℓ = v.
Definition rem_rhs ℓ v : precond :=
λ '(s₀, s₁), get_heap s₁ ℓ = v.
Class Remembers_lhs ℓ v pre :=
is_remembering_lhs : ∀ s₀ s₁, pre (s₀, s₁) → rem_lhs ℓ v (s₀, s₁).
Class Remembers_rhs ℓ v pre :=
is_remembering_rhs : ∀ s₀ s₁, pre (s₀, s₁) → rem_rhs ℓ v (s₀, s₁).
Lemma Remembers_lhs_rem_lhs :
∀ ℓ v,
Remembers_lhs ℓ v (rem_lhs ℓ v).
Proof.
intros ℓ v. intros s₀ s₁ h. auto.
Qed.
#[export] Hint Extern 10 (Remembers_lhs _ _ (rem_lhs _ _)) =>
eapply Remembers_lhs_rem_lhs
: typeclass_instances ssprove_invariant.
Lemma Remembers_rhs_rem_rhs :
∀ ℓ v,
Remembers_rhs ℓ v (rem_rhs ℓ v).
Proof.
intros ℓ v. intros s₀ s₁ h. auto.
Qed.
#[export] Hint Extern 10 (Remembers_rhs _ _ (rem_rhs _ _)) =>
eapply Remembers_rhs_rem_rhs
: typeclass_instances ssprove_invariant.
Lemma Remembers_lhs_conj_right :
∀ ℓ v (pre spre : precond),
Remembers_lhs ℓ v spre →
Remembers_lhs ℓ v (pre ⋊ spre).
Proof.
intros ℓ v pre spre h.
intros s₀ s₁ []. apply h. auto.
Qed.
Lemma Remembers_lhs_conj_left :
∀ ℓ v (pre spre : precond),
Remembers_lhs ℓ v pre →
Remembers_lhs ℓ v (pre ⋊ spre).
Proof.
intros ℓ v pre spre h.
intros s₀ s₁ []. apply h. auto.
Qed.
#[export] Hint Extern 9 (Remembers_lhs _ _ (_ ⋊ _)) =>
eapply Remembers_lhs_conj_right
: typeclass_instances ssprove_invariant.
#[export] Hint Extern 11 (Remembers_lhs _ _ (_ ⋊ _)) =>
eapply Remembers_lhs_conj_left
: typeclass_instances ssprove_invariant.
Lemma Remembers_rhs_conj_right :
∀ ℓ v (pre spre : precond),
Remembers_rhs ℓ v spre →
Remembers_rhs ℓ v (pre ⋊ spre).
Proof.
intros ℓ v pre spre h.
intros s₀ s₁ []. apply h. auto.
Qed.
Lemma Remembers_rhs_conj_left :
∀ ℓ v (pre spre : precond),
Remembers_rhs ℓ v pre →
Remembers_rhs ℓ v (pre ⋊ spre).
Proof.
intros ℓ v pre spre h.
intros s₀ s₁ []. apply h. auto.
Qed.
#[export] Hint Extern 9 (Remembers_rhs _ _ (_ ⋊ _)) =>
eapply Remembers_rhs_conj_right
: typeclass_instances ssprove_invariant.
#[export] Hint Extern 11 (Remembers_rhs _ _ (_ ⋊ _)) =>
eapply Remembers_rhs_conj_left
: typeclass_instances ssprove_invariant.
Lemma Remembers_lhs_from_tracked_rhs :
∀ ℓ v pre,
Remembers_rhs ℓ v pre →
Syncs ℓ pre →
Remembers_lhs ℓ v pre.
Proof.
intros ℓ v pre hr ht.
intros s₀ s₁ hpre. simpl.
specialize (hr _ _ hpre). specialize (ht _ _ hpre).
rewrite ht. apply hr.
Qed.
Lemma Remembers_rhs_from_tracked_lhs :
∀ ℓ v pre,
Remembers_lhs ℓ v pre →
Syncs ℓ pre →
Remembers_rhs ℓ v pre.
Proof.
intros ℓ v pre hr ht.
intros s₀ s₁ hpre. simpl.
specialize (hr _ _ hpre). specialize (ht _ _ hpre).
rewrite -ht. apply hr.
Qed.
Lemma put_pre_cond_rem_lhs :
∀ ℓ v ℓ' v',
ℓ' != ℓ →
put_pre_cond ℓ v (rem_lhs ℓ' v').
Proof.
intros ℓ v ℓ' v' hn s₀ s₁ hc.
unfold rem_lhs in *.
rewrite get_set_heap_neq. all: auto.
Qed.
#[export] Hint Extern 10 (put_pre_cond _ _ (rem_lhs _ _)) =>
apply put_pre_cond_rem_lhs
: ssprove_invariant.
Lemma put_pre_cond_rem_rhs :
∀ ℓ v ℓ' v',
ℓ' != ℓ →
put_pre_cond ℓ v (rem_rhs ℓ' v').
Proof.
intros ℓ v ℓ' v' hn s₀ s₁ hc.
unfold rem_rhs in *.
rewrite get_set_heap_neq. all: auto.
Qed.
#[export] Hint Extern 10 (put_pre_cond _ _ (rem_rhs _ _)) =>
apply put_pre_cond_rem_rhs
: ssprove_invariant.
(** Dually to rem_lhs/rem_rhs we create "invariants" to represent a deviation
of invariant, or a deficit which will need to be paid later to restore
the proper invariant.
*)
Definition set_lhs ℓ v (pre : precond) : precond :=
λ '(s₀, s₁),
∃ s₀', pre (s₀', s₁) ∧ s₀ = set_heap s₀' ℓ v.
Arguments set_lhs : simpl never.
Definition set_rhs ℓ v (pre : precond) : precond :=
λ '(s₀, s₁),
∃ s₁', pre (s₀, s₁') ∧ s₁ = set_heap s₁' ℓ v.
Arguments set_rhs : simpl never.
Lemma restore_set_lhs :
∀ ℓ v pre s₀ s₁,
set_lhs ℓ v pre (s₀, s₁) →
(∀ s₀', pre (s₀', s₁) → pre (set_heap s₀' ℓ v, s₁)) →
pre (s₀, s₁).
Proof.
intros ℓ v pre s₀ s₁ h hpre.
destruct h as [? [? ?]]. subst.
eapply hpre. auto.
Qed.
Lemma restore_set_rhs :
∀ ℓ v pre s₀ s₁,
set_rhs ℓ v pre (s₀, s₁) →
(∀ s₁', pre (s₀, s₁') → pre (s₀, set_heap s₁' ℓ v)) →
pre (s₀, s₁).
Proof.
intros ℓ v pre s₀ s₁ h hpre.
destruct h as [? [? ?]]. subst.
eapply hpre. auto.
Qed.
(** Representation of affectations in a heap
They can be interpreted as updates or as read data depending on the context.
*)
Inductive heap_val :=
| hpv_l (ℓ : Location) (v : ℓ)
| hpv_r (ℓ : Location) (v : ℓ).
Definition loc_val_pair (ℓ : Location) (v : ℓ) : ∑ ℓ : Location, ℓ :=
(ℓ ; v).
Definition heap_val_eq : rel heap_val :=
λ u v,
match u, v with
| hpv_l ℓ v, hpv_l ℓ' v' => loc_val_pair ℓ v == (ℓ' ; v')
| hpv_r ℓ v, hpv_r ℓ' v' => loc_val_pair ℓ v == (ℓ' ; v')
| _, _ => false
end.
Lemma heap_val_eqP : Equality.axiom heap_val_eq.
Proof.
intros u v.
destruct u, v. all: simpl. 2,3: constructor. 2,3: discriminate.
all: unfold loc_val_pair.
all: destruct eq_op eqn:e.
all: move: e => /eqP e. all: noconf e.
all: constructor.
all: try reflexivity.
all: intro h. all: inversion h. all: contradiction.
Qed.
(*Canonical heap_val_eqMixin := EqMixin heap_val_eqP.
Canonical heap_val_eqType :=
Eval hnf in EqType heap_val heap_val_eqMixin. *)
Definition heap_val_hasDecEq := hasDecEq.Build heap_val heap_val_eqP.
HB.instance Definition _ := heap_val_hasDecEq.
Derive NoConfusion for heap_val.
Fixpoint update_pre (l : list heap_val) (pre : precond) :=
match l with
| hpv_l ℓ v :: l => set_lhs ℓ v (update_pre l pre)
| hpv_r ℓ v :: l => set_rhs ℓ v (update_pre l pre)
| [::] => pre
end.
Fixpoint update_heaps (l : list heap_val) s₀ s₁ :=
match l with
| hpv_l ℓ v :: l =>
let '(s₀, s₁) := update_heaps l s₀ s₁ in
(set_heap s₀ ℓ v, s₁)
| hpv_r ℓ v :: l =>
let '(s₀, s₁) := update_heaps l s₀ s₁ in
(s₀, set_heap s₁ ℓ v)
| [::] => (s₀, s₁)
end.
Lemma update_pre_spec :
∀ l pre s₀ s₁,
update_pre l pre (s₀, s₁) →
∃ s₀' s₁', pre (s₀', s₁') ∧ (s₀, s₁) = update_heaps l s₀' s₁'.
Proof.
intros l pre s₀ s₁ h.
induction l as [| [] l ih] in pre, s₀, s₁, h |- *.
- simpl in h. simpl.
eexists _, _. intuition eauto.
- simpl in h. simpl.
destruct h as [s [h ?]]. subst.
eapply ih in h. destruct h as [? [? [? e]]].
eexists _, _. split. 1: eauto.
rewrite -e. reflexivity.
- simpl in h. simpl.
destruct h as [s [h ?]]. subst.
eapply ih in h. destruct h as [? [? [? e]]].
eexists _, _. split. 1: eauto.
rewrite -e. reflexivity.
Qed.
Definition is_hpv_l u :=
match u with
| hpv_l _ _ => true
| _ => false
end.
Definition is_hpv_r u :=
match u with
| hpv_r _ _ => true
| _ => false
end.
Lemma update_heaps_filter_l :
∀ l s₀ s₁,
(update_heaps (filter is_hpv_l l) s₀ s₁).1 =
(update_heaps l s₀ s₁).1.
Proof.
intros l s₀ s₁.
induction l as [| [] l ih] in s₀, s₁ |- *.
- reflexivity.
- simpl. destruct update_heaps eqn:e1.
destruct (update_heaps l s₀ s₁) eqn:e2.
simpl. specialize (ih s₀ s₁).
rewrite e2 e1 in ih. simpl in ih. subst. reflexivity.
- simpl. destruct update_heaps eqn:e1.
destruct (update_heaps l s₀ s₁) eqn:e2.
simpl. specialize (ih s₀ s₁).
rewrite e2 e1 in ih. simpl in ih. auto.
Qed.
Lemma update_heaps_filter_r :
∀ l s₀ s₁,
(update_heaps (filter is_hpv_r l) s₀ s₁).2 =
(update_heaps l s₀ s₁).2.
Proof.
intros l s₀ s₁.
induction l as [| [] l ih] in s₀, s₁ |- *.
- reflexivity.
- simpl. destruct update_heaps eqn:e1.
destruct (update_heaps l s₀ s₁) eqn:e2.
simpl. specialize (ih s₀ s₁).
rewrite e2 e1 in ih. simpl in ih. auto.