forked from SSProve/ssprove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg_rhl.v
2728 lines (2566 loc) · 91.3 KB
/
pkg_rhl.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
(** Package Relational Hoar Logic
This file connects packages to the relational Hoare logic and provides
basic crypto-style reasoning notions.
*)
From Coq Require Import Utf8.
From 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 extructures Require Import ord fset fmap.
From Mon Require Import SPropBase.
From 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
pkg_invariants pkg_distr.
Require Import Equations.Prop.DepElim.
From Equations Require Import Equations.
(* Must come after importing Equations.Equations, who knows why. *)
From 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.
Notation " r⊨ ⦃ pre ⦄ c1 ≈ c2 ⦃ post ⦄ " :=
(semantic_judgement _ _ (repr c1) (repr c2) (fromPrePost pre post))
: package_scope.
Theorem rbind_rule :
∀ {A1 A2 B1 B2 : ord_choiceType} {f1 f2} m1 m2
(pre : precond) (mid : postcond A1 A2) (post : postcond B1 B2),
r⊨ ⦃ pre ⦄ m1 ≈ m2 ⦃ mid ⦄ →
(∀ a1 a2,
r⊨ ⦃ λ '(s1, s2), mid (a1, s1) (a2, s2) ⦄ f1 a1 ≈ f2 a2 ⦃ post ⦄
) →
r⊨ ⦃ pre ⦄ bind m1 f1 ≈ bind m2 f2 ⦃ post ⦄.
Proof.
intros A1 A2 B1 B2 f1 f2 m1 m2 pre mid post hm hf.
rewrite !repr_bind.
apply (bind_rule_pp (repr m1) (repr m2) pre mid post hm hf).
Qed.
Lemma get_case :
∀ LA (I : heap_choiceType * heap_choiceType → Prop) ℓ,
INV LA I →
ℓ \in LA →
r⊨ ⦃ λ '(s₀, s₃), I (s₀, s₃) ⦄
x ← get ℓ ;; ret x ≈ x ← get ℓ ;; ret x
⦃ λ '(b₁, s₁) '(b₂, s₂), b₁ = b₂ ∧ I (s₁, s₂) ⦄.
Proof.
intros LA I ℓ hinv hin. intros [s₁ s₂]. simpl.
rewrite /SpecificationMonads.MonoCont_bind /=.
rewrite /SpecificationMonads.MonoCont_order
/SPropMonadicStructures.SProp_op_order
/Morphisms.pointwise_relation /Basics.flip
/SPropMonadicStructures.SProp_order /=.
intuition auto.
assert (get_heap s₁ ℓ = get_heap s₂ ℓ) as Hv.
{ unfold INV in hinv.
specialize (hinv s₁ s₂). destruct hinv as [hinv _].
eapply hinv. all: auto.
}
pose v := (SDistr_unit _ (((get_heap s₁ ℓ), s₁),
((get_heap s₂ ℓ), s₂))).
exists v. split.
- apply SDistr_unit_F_choice_prod_coupling.
reflexivity.
- intros [b₁ s₃] [b₂ s₄]. intro hd.
apply H1. rewrite dunit1E in hd.
assert (
(get_heap s₁ ℓ, s₁, (get_heap s₂ ℓ, s₂)) = (b₁, s₃, (b₂, s₄))
) as e.
{ destruct ((get_heap s₁ ℓ, s₁, (get_heap s₂ ℓ, s₂)) == (b₁, s₃, (b₂, s₄))) eqn:e.
- move: e => /eqP e. assumption.
- rewrite e in hd. cbn in hd.
rewrite order.Order.POrderTheory.ltxx in hd. discriminate.
}
inversion e. subst. intuition auto.
Qed.
Lemma put_case :
∀ {LA} (I : heap_choiceType * heap_choiceType → Prop) l v,
INV LA I →
l \in LA →
r⊨ ⦃ λ '(s0, s3), I (s0, s3) ⦄
putr l v (ret tt) ≈ putr l v (ret tt)
⦃ λ '(b1, s1) '(b2, s2), b1 = b2 ∧ I (s1, s2) ⦄.
Proof.
intros LA I l v hinv hin.
intros [s1 s2]. simpl.
rewrite /SpecificationMonads.MonoCont_bind /=.
rewrite /SpecificationMonads.MonoCont_order /SPropMonadicStructures.SProp_op_order
/Morphisms.pointwise_relation /Basics.flip /SPropMonadicStructures.SProp_order /=.
intuition eauto.
eexists (SDistr_unit _ _).
split.
+ apply SDistr_unit_F_choice_prod_coupling.
reflexivity.
+ intros [b1 s3] [b2 s4].
intros Hd.
apply H1.
unfold SDistr_unit in Hd.
rewrite dunit1E in Hd.
assert ((tt, set_heap s1 l v, (tt, set_heap s2 l v)) = (b1, s3, (b2, s4))) as Heqs.
{ destruct ((tt, set_heap s1 l v, (tt, set_heap s2 l v)) == (b1, s3, (b2, s4))) eqn:Heqd.
- move: Heqd. move /eqP => Heqd. assumption.
- rewrite Heqd in Hd. simpl in Hd.
rewrite order.Order.POrderTheory.ltxx in Hd. discriminate.
}
inversion Heqs.
intuition eauto.
eapply hinv. all: eauto.
Qed.
(* TODO MOVE? *)
Lemma destruct_pair_eq {R : ringType} {A B : eqType} {a b : A} {c d : B} :
((a, c) == (b, d))%:R = (a == b)%:R * (c == d)%:R :> R.
Proof.
destruct (a == b) eqn:ab, (c == d) eqn:cd.
all: cbn; rewrite ab cd /=; try rewrite GRing.mulr1; try rewrite GRing.mulr0; reflexivity.
Qed.
Lemma summable_eq {A : choiceType} {s : A} :
realsum.summable (T:=A) (R:=R) (λ x, (x == s)%:R).
Proof.
match goal with
| |- realsum.summable ?f => eassert (f = _) as Hf end.
{ extensionality x. rewrite eq_sym.
rewrite -dunit1E. reflexivity. }
rewrite Hf. clear Hf.
apply summable_mu.
Qed.
Lemma summable_pair_eq {A : choiceType} {B C : eqType} (f1 f3 : A -> B) (f2 f4 : A -> C)
(h1 : realsum.summable (T:=A) (R:=R) (λ x, (f1 x == f3 x)%:R))
(h2 : realsum.summable (T:=_) (R:=R) (λ x, (f2 x == f4 x)%:R))
:
realsum.summable (T:=_) (R:=R) (λ x, ((f1 x, f2 x) == (f3 x, f4 x))%:R).
Proof.
match goal with
| |- realsum.summable ?f => eassert (f = _) as Hf end.
{ extensionality x.
apply (destruct_pair_eq (a:= f1 x) (b:=f3 x) (c:= f2 x) (d := f4 x)). }
rewrite Hf.
apply realsum.summableM. all: assumption.
Qed.
Lemma psum_exists {R : realType} {A : choiceType} {f : A -> R}
(H : 0 < realsum.psum (T:=A) (R:=R) f) (Hpos : forall x, 0 <= f x) :
exists x, 0 < f x.
Proof.
assert (realsum.psum f ≠ 0) as Hneq.
{ intros Hgt.
rewrite Hgt in H.
rewrite order.Order.POrderTheory.ltxx in H.
auto. }
destruct (realsum.neq0_psum (R:=R) Hneq) as [x Hx].
exists x. specialize (Hpos x).
rewrite order.Order.POrderTheory.le_eqVlt in Hpos.
move: Hpos. move /orP => [H1 | H2].
- move: H1. move /eqP => H1. rewrite -H1.
rewrite order.Order.POrderTheory.ltxx. auto.
- assumption.
Qed.
Lemma pmulr_me (x y : R) : 0 <= y -> (0 < y * x) -> (0 < x).
Proof.
rewrite le0r => /orP[/eqP->|].
- by rewrite GRing.mul0r order.Order.POrderTheory.ltxx.
- intros. by rewrite -(pmulr_rgt0 x b).
Qed.
Lemma ge0_eq {R : realType} {A : eqType} {x y : A} (H : 0 < ((x == y)%:R) :> R) :
x = y.
Proof.
destruct (x == y) eqn:Heq.
- move: Heq. by move /eqP.
- by rewrite order.Order.POrderTheory.ltxx in H.
Qed.
Lemma ne0_eq {R : ringType} {A : eqType} {x y : A} (H : ((x == y)%:R) ≠ (0 : R)) :
x = y.
Proof.
destruct (x == y) eqn:Heq.
- move: Heq. by move /eqP.
- cbn in H. intuition.
Qed.
(* TODO MOVE *)
Lemma dlet_f_equal :
∀ {R : realType} {T U : choiceType} (m : {distr T / R}) (f g : T → {distr U / R}),
f =1 g →
\dlet_(x <- m) f x =1 \dlet_(x <- m) g x.
Proof.
intros R T U m f g e x.
apply functional_extensionality in e. subst.
reflexivity.
Qed.
(* TODO This proof is really the same as cmd_sample_preserve_pre *)
Lemma sampler_case :
∀ {LA} (I : heap_choiceType * heap_choiceType -> Prop) op,
INV LA I →
r⊨ ⦃ λ '(s0, s3), I (s0, s3) ⦄
sampler op [eta ret (A:=Arit op)] ≈ sampler op [eta ret (A:=Arit op)]
⦃ λ '(b1, s1) '(b2, s2), b1 = b2 ∧ I (s1, s2) ⦄.
Proof.
intros LA I op HINV.
intros [s₀ s₁]. hnf. intro P. hnf.
intros [hpre hpost]. simpl.
destruct op as [opA opB].
pose (d :=
SDistr_bind (λ x, SDistr_unit _ ((x, s₀), (x, s₁)))
(Theta_dens.unary_ThetaDens0 _ (ropr (opA ; opB) (λ x : chElement opA, retrFree x)))
).
exists d. split.
- unfold coupling. split.
+ unfold lmg. unfold dfst.
apply distr_ext. intro. simpl.
rewrite dlet_dlet.
simpl.
unfold SDistr_bind, SDistr_unit.
rewrite dlet_dlet.
apply dlet_f_equal. intro.
apply distr_ext. intro.
rewrite dlet_unit. rewrite dlet_unit. simpl. reflexivity.
+ unfold rmg. unfold dsnd.
apply distr_ext. intro. simpl.
rewrite dlet_dlet.
simpl.
unfold SDistr_bind, SDistr_unit.
rewrite dlet_dlet.
apply dlet_f_equal. intro.
apply distr_ext. intro.
rewrite dlet_unit. rewrite dlet_unit. simpl. reflexivity.
- intros [] [] e. subst d. simpl in e.
rewrite SDistr_rightneutral in e. simpl in e.
unfold SDistr_bind, SDistr_unit in e.
rewrite dletE in e.
erewrite eq_psum in e.
2:{
intro. rewrite dunit1E. reflexivity.
}
apply psum_exists in e.
2:{
intro. apply mulr_ge0.
- auto.
- apply ler0n.
}
destruct e as [? e].
apply pmulr_me in e. 2: auto.
apply ge0_eq in e. noconf e.
eapply hpost. intuition auto.
Qed.
(** Syntactic judgment *)
(* It's the same as the semantic one, but we're abstracting it away. *)
Inductive rel_jdg {A B : choiceType} (pre : precond) (post : postcond A B)
(p : raw_code A) (q : raw_code B) :=
_from_sem_jdg : locked (r⊨ ⦃ pre ⦄ p ≈ q ⦃ post ⦄) → rel_jdg pre post p q.
Notation "⊢ ⦃ pre ⦄ c1 ≈ c2 ⦃ post ⦄" :=
(rel_jdg pre post c1 c2)
(format "⊢ ⦃ pre ⦄ '/ ' '[' c1 ']' '/' ≈ '/ ' '[' c2 ']' '/' ⦃ post ⦄")
: package_scope.
Lemma from_sem_jdg :
∀ {A B : choiceType} pre (post : postcond A B) p q,
r⊨ ⦃ pre ⦄ p ≈ q ⦃ post ⦄ →
⊢ ⦃ pre ⦄ p ≈ q ⦃ post ⦄.
Proof.
intros A B pre post p q h.
constructor. rewrite -lock. auto.
Qed.
Lemma to_sem_jdg :
∀ {A B : choiceType} pre (post : postcond A B) p q,
⊢ ⦃ pre ⦄ p ≈ q ⦃ post ⦄ →
r⊨ ⦃ pre ⦄ p ≈ q ⦃ post ⦄.
Proof.
intros A B pre post p q [h].
rewrite -lock in h.
exact h.
Qed.
(** Equivalence of packages in the program logic *)
Definition eq_up_to_inv (E : Interface) (I : precond) (p₀ p₁ : raw_package) :=
∀ (id : ident) (S T : choice_type) (x : S),
(id, (S, T)) \in E →
⊢ ⦃ λ '(s₀, s₁), I (s₀, s₁) ⦄
get_op_default p₀ (id, (S, T)) x ≈ get_op_default p₁ (id, (S, T)) x
⦃ λ '(b₀, s₀) '(b₁, s₁), b₀ = b₁ ∧ I (s₀, s₁) ⦄.
Lemma Pr_eq_empty :
∀ {X Y : ord_choiceType}
{A : pred (X * heap_choiceType)} {B : pred (Y * heap_choiceType)}
Ψ ϕ
(c1 : @FrStP heap_choiceType X) (c2 : @FrStP heap_choiceType Y),
⊨ ⦃ Ψ ⦄ c1 ≈ c2 ⦃ ϕ ⦄ →
Ψ (empty_heap, empty_heap) →
(∀ x y, ϕ x y → (A x) ↔ (B y)) →
\P_[ θ_dens (θ0 c1 empty_heap) ] A =
\P_[ θ_dens (θ0 c2 empty_heap) ] B.
Proof.
intros X Y A B Ψ Φ c1 c2 ? ? ?.
apply (Pr_eq Ψ Φ). all: assumption.
Qed.
Lemma eq_up_to_inv_adversary_link :
∀ {L₀ L₁ LA E} (p₀ p₁ : raw_package) (I : precond) {B} (A : raw_code B)
`{ValidPackage L₀ Game_import E p₀}
`{ValidPackage L₁ Game_import E p₁}
`{@ValidCode LA E B A},
INV LA I →
eq_up_to_inv E I p₀ p₁ →
r⊨ ⦃ I ⦄ code_link A p₀ ≈ code_link A p₁
⦃ λ '(b₀, s₀) '(b₁, s₁), b₀ = b₁ ∧ I (s₀, s₁) ⦄.
Proof.
intros L₀ L₁ LA E p₀ p₁ I B A vp₀ vp₁ vA hLA hp.
induction A in vA |- *.
- cbn - [semantic_judgement].
eapply weaken_rule. 1: apply ret_rule.
intros [h₀ h₁] post.
cbn. unfold SPropMonadicStructures.SProp_op_order.
unfold Basics.flip, SPropMonadicStructures.SProp_order.
intros [HI Hp].
apply Hp. intuition auto.
- cbn - [semantic_judgement lookup_op].
apply inversion_valid_opr in vA as hA. destruct hA as [hi vk].
destruct o as [id [S T]].
eapply from_valid_package in vp₀.
specialize (vp₀ _ hi). simpl in vp₀.
destruct vp₀ as [f₀ [e₀ h₀]].
eapply from_valid_package in vp₁.
specialize (vp₁ _ hi). simpl in vp₁.
destruct vp₁ as [f₁ [e₁ h₁]].
erewrite lookup_op_spec_inv. 2: eauto.
erewrite lookup_op_spec_inv. 2: eauto.
specialize (hp id S T x hi).
erewrite get_op_default_spec in hp. 2: eauto.
erewrite get_op_default_spec in hp. 2: eauto.
rewrite !repr_bind.
eapply bind_rule_pp. 1:{ eapply to_sem_jdg in hp. exact hp. }
cbn - [semantic_judgement].
intros a₀ a₁.
apply pre_hypothesis_rule.
intros s₀ s₁ [? ?]. subst.
eapply pre_weaken_rule. 1: eapply H.
+ eapply vk.
+ cbn. intros s₀' s₁' [? ?]. subst. auto.
- cbn - [semantic_judgement bindrFree].
apply inversion_valid_getr in vA as hA. destruct hA as [hi vk].
match goal with
| |- ⊨ ⦃ ?pre_ ⦄ bindrFree ?m_ ?f1_ ≈ bindrFree _ ?f2_ ⦃ ?post_ ⦄ =>
eapply (bind_rule_pp (f1 := f1_) (f2 := f2_) m_ m_ pre_ _ post_)
end.
+ eapply (get_case LA). all: auto.
+ intros a₀ a₁. cbn - [semantic_judgement].
eapply pre_hypothesis_rule.
intros s₀ s₁ [? ?]. subst a₁.
eapply pre_weaken_rule. 1: eapply H.
* eapply vk.
* cbn. intros s₀' s₁' [? ?]. subst. auto.
- cbn - [semantic_judgement bindrFree].
apply inversion_valid_putr in vA as hA. destruct hA as [hi vk].
match goal with
| |- ⊨ ⦃ ?pre_ ⦄ bindrFree ?m_ ?f1_ ≈ bindrFree _ ?f2_ ⦃ ?post_ ⦄ =>
eapply (bind_rule_pp (f1 := f1_) (f2 := f2_) m_ m_ pre_ _ post_)
end.
+ eapply (@put_case LA). all: auto.
+ intros a₀ a₁. cbn - [semantic_judgement].
eapply pre_hypothesis_rule.
intros s₀ s₁ [? ?]. subst a₁.
eapply pre_weaken_rule. 1: eapply IHA.
* eapply vk.
* cbn. intros s₀' s₁' [? ?]. subst. auto.
- cbn - [semantic_judgement bindrFree].
eapply bind_rule_pp.
+ eapply (@sampler_case LA). auto.
+ intros a₀ a₁. cbn - [semantic_judgement].
eapply pre_hypothesis_rule.
intros s₀ s₁ [? ?]. subst a₁.
eapply pre_weaken_rule. 1: eapply H.
* eapply inversion_valid_sampler. eauto.
* cbn. intros s₀' s₁' [? ?]. subst. auto.
Qed.
Lemma eq_upto_inv_perf_ind :
∀ {L₀ L₁ LA E} (p₀ p₁ : raw_package) (I : precond) (A : raw_package)
`{ValidPackage L₀ Game_import E p₀}
`{ValidPackage L₁ Game_import E p₁}
`{ValidPackage LA E A_export A},
INV' L₀ L₁ I →
I (empty_heap, empty_heap) →
fdisjoint LA L₀ →
fdisjoint LA L₁ →
eq_up_to_inv E I p₀ p₁ →
AdvantageE p₀ p₁ A = 0.
Proof.
intros L₀ L₁ LA E p₀ p₁ I A vp₀ vp₁ vA hI' hIe hd₀ hd₁ hp.
unfold AdvantageE, Pr.
pose r := get_op_default A RUN tt.
assert (hI : INV LA I).
{ unfold INV. intros s₀ s₁. split.
- intros hi l hin. apply hI'.
+ assumption.
+ move: hd₀ => /fdisjointP hd₀. apply hd₀. assumption.
+ move: hd₁ => /fdisjointP hd₁. apply hd₁. assumption.
- intros hi l v hin. apply hI'.
+ assumption.
+ move: hd₀ => /fdisjointP hd₀. apply hd₀. assumption.
+ move: hd₁ => /fdisjointP hd₁. apply hd₁. assumption.
}
unshelve epose proof (eq_up_to_inv_adversary_link p₀ p₁ I r hI hp) as h.
1:{
eapply valid_get_op_default.
- eauto.
- auto_in_fset.
}
assert (
∀ x y : tgt RUN * heap_choiceType,
(let '(b₀, s₀) := x in λ '(b₁, s₁), b₀ = b₁ ∧ I (s₀, s₁)) y →
(fst x == true) ↔ (fst y == true)
) as Ha.
{ intros [b₀ s₀] [b₁ s₁]. simpl.
intros [e ?]. rewrite e. intuition auto.
}
unfold Pr_op.
unshelve epose (rhs := thetaFstd _ (repr (code_link r p₀)) empty_heap).
simpl in rhs.
epose (lhs := Pr_op (A ∘ p₀) RUN tt empty_heap).
assert (lhs = rhs) as he.
{ subst lhs rhs.
unfold Pr_op. unfold Pr_code.
unfold thetaFstd. simpl. apply f_equal2. 2: reflexivity.
apply f_equal. apply f_equal.
rewrite get_op_default_link. reflexivity.
}
unfold lhs in he. unfold Pr_op in he.
rewrite he.
unshelve epose (rhs' := thetaFstd _ (repr (code_link r p₁)) empty_heap).
simpl in rhs'.
epose (lhs' := Pr_op (A ∘ p₁) RUN tt empty_heap).
assert (lhs' = rhs') as e'.
{ subst lhs' rhs'.
unfold Pr_op. unfold Pr_code.
unfold thetaFstd. simpl. apply f_equal2. 2: reflexivity.
apply f_equal. apply f_equal.
rewrite get_op_default_link. reflexivity.
}
unfold lhs' in e'. unfold Pr_op in e'.
rewrite e'.
unfold rhs', rhs.
unfold SDistr_bind. unfold SDistr_unit.
rewrite !dletE.
assert (
∀ x : bool_choiceType * heap_choiceType,
((let '(b, _) := x in dunit (R:=R) (T:=bool_choiceType) b) true) ==
(x.1 == true)%:R
) as h1.
{ intros [b s].
simpl. rewrite dunit1E. apply/eqP. reflexivity.
}
assert (
∀ y,
(λ x : prod_choiceType (tgt RUN) heap_choiceType, (y x) * (let '(b, _) := x in dunit (R:=R) (T:=tgt RUN) b) true) =
(λ x : prod_choiceType (tgt RUN) heap_choiceType, (x.1 == true)%:R * (y x))
) as Hrew.
{ intros y. extensionality x.
destruct x as [x1 x2].
rewrite dunit1E.
simpl. rewrite GRing.mulrC. reflexivity.
}
rewrite !Hrew.
unfold TransformingLaxMorph.rlmm_from_lmla_obligation_1. simpl.
unfold SubDistr.SDistr_obligation_2. simpl.
unfold OrderEnrichedRelativeAdjunctionsExamples.ToTheS_obligation_1.
rewrite !SDistr_rightneutral. simpl.
pose proof (Pr_eq_empty _ _ _ _ h hIe Ha) as Heq.
simpl in Heq.
unfold θ_dens in Heq.
simpl in Heq. unfold pr in Heq.
simpl in Heq.
rewrite Heq.
rewrite /StateTransfThetaDens.unaryStateBeta'_obligation_1.
assert (∀ (x : R), `|x - x| = 0) as Hzero.
{ intros x.
assert (x - x = 0) as H3.
{ apply /eqP. rewrite GRing.subr_eq0. intuition. }
rewrite H3. apply normr0.
}
apply Hzero.
Qed.
Lemma eq_rel_perf_ind :
∀ {L₀ L₁ E} (p₀ p₁ : raw_package) (inv : precond)
`{ValidPackage L₀ Game_import E p₀}
`{ValidPackage L₁ Game_import E p₁},
Invariant L₀ L₁ inv →
eq_up_to_inv E inv p₀ p₁ →
p₀ ≈₀ p₁.
Proof.
intros L₀ L₁ E p₀ p₁ inv v₀ v₁ [? ?] he.
intros LA A vA hd₀ hd₁.
eapply eq_upto_inv_perf_ind. all: eauto.
Qed.
(* Special case where the invariant is equality of state. *)
Corollary eq_rel_perf_ind_eq :
∀ {L₀ L₁ E} (p₀ p₁ : raw_package)
`{ValidPackage L₀ Game_import E p₀}
`{ValidPackage L₁ Game_import E p₁},
eq_up_to_inv E (λ '(h₀, h₁), h₀ = h₁) p₀ p₁ →
p₀ ≈₀ p₁.
Proof.
intros L₀ L₁ E p₀ p₁ v₀ v₁ h.
eapply eq_rel_perf_ind with (λ '(h₀, h₁), h₀ = h₁).
- exact _.
- assumption.
Qed.
(* Special case where the invariant is heap_ignore. *)
Corollary eq_rel_perf_ind_ignore :
∀ L {L₀ L₁ E} (p₀ p₁ : raw_package)
`{ValidPackage L₀ Game_import E p₀}
`{ValidPackage L₁ Game_import E p₁},
fsubset L (L₀ :|: L₁) →
eq_up_to_inv E (heap_ignore L) p₀ p₁ →
p₀ ≈₀ p₁.
Proof.
intros L L₀ L₁ E p₀ p₁ v₀ v₁ hs h.
eapply eq_rel_perf_ind with (heap_ignore L).
- ssprove_invariant.
- assumption.
Qed.
(** Rules for packages *)
(* same as in RulesStateprob.v with `r` at the beginning *)
(* Alternative to rbind_rule *)
Lemma r_bind :
∀ {A₀ A₁ B₀ B₁ : ord_choiceType} m₀ m₁ f₀ f₁
(pre : precond) (mid : postcond A₀ A₁) (post : postcond B₀ B₁),
⊢ ⦃ pre ⦄ m₀ ≈ m₁ ⦃ mid ⦄ →
(∀ a₀ a₁, ⊢ ⦃ λ '(s₀, s₁), mid (a₀, s₀) (a₁, s₁) ⦄ f₀ a₀ ≈ f₁ a₁ ⦃ post ⦄) →
⊢ ⦃ pre ⦄ bind m₀ f₀ ≈ bind m₁ f₁ ⦃ post ⦄.
Proof.
intros A₀ A₁ B₀ B₁ m₀ m₁ f₀ f₁ pre mid post hm hf.
eapply from_sem_jdg. eapply rbind_rule.
- eapply to_sem_jdg. exact hm.
- intros a₀ a₁. eapply to_sem_jdg. eapply hf.
Qed.
(* Pre-condition manipulating rules *)
Theorem rpre_weaken_rule :
∀ {A₀ A₁ : ord_choiceType} {p₀ : raw_code A₀} {p₁ : raw_code A₁}
(pre pre' : precond) post,
⊢ ⦃ pre ⦄ p₀ ≈ p₁ ⦃ post ⦄ →
(∀ s₀ s₁, pre' (s₀, s₁) → pre (s₀, s₁)) →
⊢ ⦃ pre' ⦄ p₀ ≈ p₁ ⦃ post ⦄.
Proof.
intros A₀ A₁ p₀ p₁ pre pre' post he hi.
eapply from_sem_jdg.
eapply pre_weaken_rule.
- eapply to_sem_jdg. eauto.
- eauto.
Qed.
Theorem rpre_hypothesis_rule :
∀ {A₀ A₁ : ord_choiceType} {p₀ : raw_code A₀} {p₁ : raw_code A₁}
(pre : precond) post,
(∀ s₀ s₁,
pre (s₀, s₁) → ⊢ ⦃ λ s, s.1 = s₀ ∧ s.2 = s₁ ⦄ p₀ ≈ p₁ ⦃ post ⦄
) →
⊢ ⦃ pre ⦄ p₀ ≈ p₁ ⦃ post ⦄.
Proof.
intros A₀ A₁ p₀ p₁ pre post h.
eapply from_sem_jdg.
eapply pre_hypothesis_rule.
intros. eapply to_sem_jdg.
apply h. auto.
Qed.
Theorem rpre_strong_hypothesis_rule :
∀ {A₀ A₁ : ord_choiceType} {p₀ : raw_code A₀} {p₁ : raw_code A₁}
(pre : precond) post,
(∀ s₀ s₁, pre (s₀, s₁)) →
⊢ ⦃ λ _, True ⦄ p₀ ≈ p₁ ⦃ post ⦄ →
⊢ ⦃ pre ⦄ p₀ ≈ p₁ ⦃ post ⦄.
Proof.
intros A₀ A₁ p₀ p₁ pre post hs h.
eapply from_sem_jdg.
eapply pre_strong_hypothesis_rule.
- eauto.
- eapply to_sem_jdg. auto.
Qed.
Theorem rpost_weaken_rule :
∀ {A₀ A₁ : ord_choiceType} {p₀ : raw_code A₀} {p₁ : raw_code A₁}
(pre : precond) (post1 post2 : postcond A₀ A₁),
⊢ ⦃ pre ⦄ p₀ ≈ p₁ ⦃ post1 ⦄ →
(∀ a₀ a₁, post1 a₀ a₁ → post2 a₀ a₁) →
⊢ ⦃ pre ⦄ p₀ ≈ p₁ ⦃ post2 ⦄.
Proof.
intros A₀ A₁ p₀ p₁ pre post1 post2 h hi.
eapply from_sem_jdg.
eapply post_weaken_rule.
- eapply to_sem_jdg. eauto.
- eauto.
Qed.
Lemma rreflexivity_rule :
∀ {A : ord_choiceType} (c : raw_code A),
⊢ ⦃ λ '(s₀, s₁), s₀ = s₁ ⦄ c ≈ c ⦃ eq ⦄.
Proof.
intros A c.
eapply from_sem_jdg.
apply (reflexivity_rule (repr c)).
Qed.
(* TODO MOVE? *)
(* bindrFree_and_ret is too constrained *)
Lemma bindrFree_ret :
∀ S P A (m : rFreeF S P A),
bindrFree m (λ x, retrFree x) = m.
Proof.
intros S P A m.
induction m.
- reflexivity.
- cbn. f_equal. extensionality x. auto.
Qed.
Theorem rpost_conclusion_rule :
∀ {A₀ A₁ B : ord_choiceType} {pre : precond}
{c₀ : raw_code A₀} {c₁ : raw_code A₁}
(f₀ : A₀ → B) (f₁ : A₁ → B),
⊢ ⦃ pre ⦄
x₀ ← c₀ ;; ret x₀ ≈ x₁ ← c₁ ;; ret x₁
⦃ λ '(a₀, s₀) '(a₁, s₁), s₀ = s₁ ∧ f₀ a₀ = f₁ a₁ ⦄ →
⊢ ⦃ pre ⦄ x₀ ← c₀ ;; ret (f₀ x₀) ≈ x₁ ← c₁ ;; ret (f₁ x₁) ⦃ eq ⦄.
Proof.
intros A₀ A₁ B pre c₀ c₁ f₀ f₁ h.
eapply from_sem_jdg. eapply to_sem_jdg in h.
rewrite !repr_bind in h. rewrite !repr_bind.
eapply bind_rule_pp.
- simpl (repr (ret _)) in h.
rewrite !bindrFree_ret in h. exact h.
- intros a₀ a₁. eapply to_sem_jdg.
eapply rpre_hypothesis_rule. intros s s' [? e]. subst s'.
rewrite e. eapply rpre_weaken_rule. 1: eapply rreflexivity_rule.
cbn. intros ? ? [? ?]. subst. reflexivity.
Qed.
Theorem rpost_conclusion_rule_cmd :
∀ {A₀ A₁ B : ord_choiceType} {pre : precond}
{c₀ : command A₀} {c₁ : command A₁}
(f₀ : A₀ → B) (f₁ : A₁ → B),
⊢ ⦃ pre ⦄
x₀ ← cmd c₀ ;; ret x₀ ≈
x₁ ← cmd c₁ ;; ret x₁
⦃ λ '(a₀, s₀) '(a₁, s₁), s₀ = s₁ ∧ f₀ a₀ = f₁ a₁ ⦄ →
⊢ ⦃ pre ⦄ x₀ ← cmd c₀ ;; ret (f₀ x₀) ≈ x₁ ← cmd c₁ ;; ret (f₁ x₁) ⦃ eq ⦄.
Proof.
intros A₀ A₁ B pre c₀ c₁ f₀ f₁ h.
eapply from_sem_jdg. eapply to_sem_jdg in h.
rewrite !repr_cmd_bind in h. rewrite !repr_cmd_bind.
eapply bind_rule_pp.
- simpl (repr (ret _)) in h.
rewrite !bindrFree_ret in h. exact h.
- intros a₀ a₁. eapply to_sem_jdg.
eapply rpre_hypothesis_rule. intros s s' [? e]. subst s'.
rewrite e. eapply rpre_weaken_rule. 1: eapply rreflexivity_rule.
cbn. intros ? ? [? ?]. subst. reflexivity.
Qed.
Lemma r_ret :
∀ {A₀ A₁ : ord_choiceType} u₀ u₁ (pre : precond) (post : postcond A₀ A₁),
(∀ s₀ s₁, pre (s₀, s₁) → post (u₀, s₀) (u₁, s₁)) →
⊢ ⦃ pre ⦄ ret u₀ ≈ ret u₁ ⦃ post ⦄.
Proof.
intros A₀ A₁ u₀ u₁ pre post h.
eapply from_sem_jdg. simpl.
eapply weaken_rule. 1: eapply ret_rule.
intros [s₀ s₁] P [hpre hpost]. simpl.
eapply hpost. eapply h. apply hpre.
Qed.
Theorem rif_rule :
∀ {A₀ A₁ : ord_choiceType}
(c₀ c₀' : raw_code A₀) (c₁ c₁' : raw_code A₁)
{b₀ b₁}
{pre : precond} {post : postcond A₀ A₁},
(∀ s, pre s → b₀ = b₁) →
⊢ ⦃ λ s, pre s ∧ b₀ = true ⦄ c₀ ≈ c₁ ⦃ post ⦄ →
⊢ ⦃ λ s, pre s ∧ b₀ = false ⦄ c₀' ≈ c₁' ⦃ post ⦄ →
⊢ ⦃ pre ⦄ if b₀ then c₀ else c₀' ≈ if b₁ then c₁ else c₁' ⦃ post ⦄.
Proof.
intros A₀ A₁ c₀ c₀' c₁ c₁' b₀ b₁ pre post hb ht hf.
eapply from_sem_jdg. eapply to_sem_jdg in ht, hf.
rewrite !repr_if.
eapply if_rule. all: eauto.
Qed.
(* TODO: asymmetric variants of if_rule: if_ruleL and if_ruleR *)
(* skipped for now:
Theorem bounded_do_while_rule *)
(*TODO: asymmetric variants of bounded_do_while --
Rem.: low priority as not useful for our examples *)
Section For_loop_rule.
(* for i = 0 to N : do c *)
Fixpoint for_loop (c : nat → raw_code 'unit) (N : nat) : raw_code 'unit :=
match N with
| 0 => c 0%nat
| S m => for_loop c m ;; c (S m)
end.
Context (I : nat → precond) (N : nat).
Context (c₀ c₁ : nat → raw_code 'unit).
(* hypothesis : *)
(* body maintains the loop invariant I *)
(* to ease the proof we forget about this condition (0 <= n <= N)%nat -> *)
Lemma for_loop_rule :
(∀ i, ⊢ ⦃ I i ⦄ c₀ i ≈ c₁ i ⦃ λ '(_, s₀) '(_, s₁), I i.+1 (s₀,s₁) ⦄) →
⊢ ⦃ I 0%nat ⦄
for_loop c₀ N ≈ for_loop c₁ N
⦃ λ '(_,s₀) '(_,s₁), I N.+1 (s₀,s₁) ⦄.
Proof.
intros h.
induction N as [| n ih].
- simpl. apply h.
- simpl. eapply r_bind.
+ eapply ih.
+ simpl. intros _ _.
eapply rpre_weaken_rule. 1: eapply h.
auto.
Qed.
End For_loop_rule.
Lemma valid_for_loop :
∀ L I c N,
(∀ i, ValidCode L I (c i)) →
ValidCode L I (for_loop c N).
Proof.
intros L I c N h.
induction N. all: simpl.
- eapply h.
- eapply valid_bind. all: eauto.
Qed.
#[export] Hint Extern 1 (ValidCode ?L ?I (for_loop ?c ?N)) =>
eapply valid_for_loop ;
intro
: typeclass_instances ssprove_valid_db.
Lemma rcoupling_eq :
∀ {A : ord_choiceType} (K₀ K₁ : raw_code A) (ψ : precond),
⊢ ⦃ ψ ⦄ K₀ ≈ K₁ ⦃ eq ⦄ →
∀ s₀ s₁,
ψ (s₀, s₁) →
θ_dens (θ0 (repr K₀) s₀) = θ_dens (θ0 (repr K₁) s₁).
Proof.
intros A K₀ K₁ ψ h s₀ s₁ hψ.
eapply to_sem_jdg in h.
eapply coupling_eq. all: eauto.
Qed.
Lemma rrewrite_eqDistrL :
∀ {A₀ A₁ : ord_choiceType} {P Q}
(c₀ c₀' : raw_code A₀) (c₁ : raw_code A₁),
⊢ ⦃ P ⦄ c₀ ≈ c₁ ⦃ Q ⦄ →
(∀ s, θ_dens (θ0 (repr c₀) s) = θ_dens (θ0 (repr c₀') s)) →
⊢ ⦃ P ⦄ c₀' ≈ c₁ ⦃ Q ⦄.
Proof.
intros A₀ A₁ P Q c₀ c₀' c₁ h hθ.
eapply to_sem_jdg in h.
eapply from_sem_jdg.
eapply rewrite_eqDistrL. all: eauto.
Qed.
Lemma rrewrite_eqDistrR :
∀ {A₀ A₁ : ord_choiceType} {P Q}
(c₀ : raw_code A₀) (c₁ c₁' : raw_code A₁),
⊢ ⦃ P ⦄ c₀ ≈ c₁ ⦃ Q ⦄ →
(∀ s, θ_dens (θ0 (repr c₁) s) = θ_dens (θ0 (repr c₁') s)) →
⊢ ⦃ P ⦄ c₀ ≈ c₁' ⦃ Q ⦄.
Proof.
intros A₀ A₁ P Q c₀ c₁ c₁' h hθ.
eapply to_sem_jdg in h.
eapply from_sem_jdg.
eapply rewrite_eqDistrR. all: eauto.
Qed.
Theorem rswap_rule :
∀ {A₀ A₁ : ord_choiceType} {I : precond} {post : postcond A₀ A₁}
(c₀ : raw_code A₀) (c₁ : raw_code A₁),
⊢ ⦃ I ⦄ c₀ ≈ c₁
⦃ λ '(a₀, s₀) '(a₁, s₁), I (s₀, s₁) ∧ post (a₀, s₀) (a₁, s₁) ⦄ →
⊢ ⦃ I ⦄ c₁ ≈ c₀
⦃ λ '(a₁, s₁) '(a₀, s₀), I (s₀, s₁) ∧ post (a₀, s₀) (a₁, s₁) ⦄ →
⊢ ⦃ I ⦄ c₀ ;; c₁ ≈ c₁ ;; c₀
⦃ λ '(a₁, s₁) '(a₀, s₀), I (s₀, s₁) ∧ post (a₀, s₀) (a₁, s₁) ⦄.
Proof.
intros A₀ A₁ I post c₀ c₁ h1 h2.
eapply to_sem_jdg in h1. eapply to_sem_jdg in h2.
eapply from_sem_jdg.
rewrite !repr_bind.
eapply (swap_rule (repr c₀) (repr c₁)). all: auto.
Qed.
(** TW: I guess this to allow going under binders.
We might be better off defining some morphisms on semantic judgments
to use setoid_rewrite.
*)
Theorem rswap_ruleL :
∀ {A₀ A₁ B : ord_choiceType} {pre I : precond} {post : postcond A₁ A₀}
(l : raw_code B) (c₀ : raw_code A₀) (c₁ : raw_code A₁),
⊢ ⦃ pre ⦄ l ≈ l ⦃ λ '(b₀, s₀) '(b₁, s₁), I (s₀, s₁) ⦄ →
⊢ ⦃ I ⦄ c₀ ≈ c₁ ⦃ λ '(a₀, s₀) '(a₁, s₁), I (s₀, s₁) ∧ post (a₁, s₁) (a₀, s₀) ⦄ →
⊢ ⦃ I ⦄ c₁ ≈ c₀ ⦃ λ '(a₁, s₁) '(a₀, s₀), I (s₀, s₁) ∧ post (a₁, s₁) (a₀, s₀) ⦄ →
⊢ ⦃ pre ⦄ l ;; c₀ ;; c₁ ≈ l ;; c₁ ;; c₀ ⦃ post ⦄.
Proof.
intros A₀ A₁ B pre I post l c₀ c₁ hl h0 h1.
eapply to_sem_jdg in h0, h1, hl.
eapply from_sem_jdg.
rewrite !repr_bind.
eapply swap_ruleL. all: eauto.
Qed.
Theorem rswap_ruleR :
∀ {A₀ A₁ B : ord_choiceType} {post : postcond B B}
(c₀ : raw_code A₀) (c₁ : raw_code A₁) (r : A₀ → A₁ → raw_code B),
(∀ b b', b = b' → post b b') →
(∀ a₀ a₁, ⊢ ⦃ λ '(s₁, s₀), s₀ = s₁ ⦄ r a₀ a₁ ≈ r a₀ a₁ ⦃ post ⦄) →
⊢ ⦃ λ '(h₀, h₁), h₀ = h₁ ⦄
a₀ ← c₀ ;; a₁ ← c₁ ;; ret (a₀, a₁) ≈
a₁ ← c₁ ;; a₀ ← c₀ ;; ret (a₀, a₁)
⦃ eq ⦄ →
⊢ ⦃ λ '(h₀, h₁), h₀ = h₁ ⦄
a₀ ← c₀ ;; a₁ ← c₁ ;; r a₀ a₁ ≈
a₁ ← c₁ ;; a₀ ← c₀ ;; r a₀ a₁
⦃ post ⦄.
Proof.
intros A₀ A₁ B post c₀ c₁ r postr hr h.
eapply from_sem_jdg.
repeat setoid_rewrite repr_bind. simpl.
eapply (swap_ruleR (λ a₀ a₁, repr (r a₀ a₁)) (repr c₀) (repr c₁)).
- intros. eapply to_sem_jdg. apply hr.
- apply postr.
- intro s.
unshelve eapply coupling_eq.
+ exact (λ '(h₀, h₁), h₀ = h₁).
+ eapply to_sem_jdg in h. repeat setoid_rewrite repr_bind in h.
apply h.
+ reflexivity.
Qed.
Lemma rsym_pre :
∀ {A₀ A₁ : ord_choiceType} {pre : precond} {post}
{c₀ : raw_code A₀} {c₁ : raw_code A₁},
(∀ h₀ h₁, pre (h₀, h₁) → pre (h₁, h₀)) →
⊢ ⦃ λ '(h₀, h₁), pre (h₁, h₀) ⦄ c₀ ≈ c₁ ⦃ post ⦄ →
⊢ ⦃ pre ⦄ c₀ ≈ c₁ ⦃ post ⦄.
Proof.
intros A₀ A₁ pre post c₀ c₁ pre_sym h.
unshelve eapply rpre_weaken_rule. 2: eassumption.
assumption.
Qed.
Lemma rsymmetry :
∀ {A₀ A₁ : ord_choiceType} {pre : precond} {post}
{c₀ : raw_code A₀} {c₁ : raw_code A₁},
⊢ ⦃ λ '(h₁, h₀), pre (h₀, h₁) ⦄ c₁ ≈ c₀
⦃ λ '(a₁, h₁) '(a₀, h₀), post (a₀, h₀) (a₁, h₁) ⦄ →
⊢ ⦃ pre ⦄ c₀ ≈ c₁ ⦃ post ⦄.
Proof.
intros A₀ A₁ pre post c₀ c₁ h.
eapply from_sem_jdg.
eapply symmetry_rule. eapply to_sem_jdg. auto.
Qed.
Definition spl (o : Op) :=
@callrFree (@ops_StP heap_choiceType) (@ar_StP heap_choiceType) (op_iota o).
Lemma rsamplerC :
∀ {A : ord_choiceType} (o : Op) (c : raw_code A),
⊢ ⦃ λ '(h₀, h₁), h₀ = h₁ ⦄
a ← c ;; r ← (r ← sample o ;; ret r) ;; ret (a, r) ≈
r ← (r ← sample o ;; ret r) ;; a ← c ;; ret (a, r)
⦃ eq ⦄.
Proof.
intros A o c.
eapply rrewrite_eqDistrL.
- eapply rreflexivity_rule.
- intro s.
assert (
repr_sample_c :
repr (r ← (r ← sample o ;; ret r) ;; a ← c ;; ret (a, r)) =
bindrFree (spl o) (λ r, bindrFree (repr c) (λ a, retrFree (a,r)))
).
{ rewrite !repr_bind. f_equal. extensionality r.
rewrite !repr_bind. reflexivity.
}
assert (
repr_c_sample :
repr (a ← c ;; r ← (r ← sample o ;; ret r) ;; ret (a, r)) =
bindrFree (repr c) (λ a, bindrFree (spl o) (λ r, retrFree (a,r)))
).
{ rewrite repr_bind. reflexivity. }
rewrite repr_c_sample repr_sample_c.
pose proof (sample_c_is_c_sample o (repr c) s) as hlp.
unfold sample_c in hlp. unfold c_sample in hlp.
apply hlp.
Qed.
Lemma rsamplerC_sym' :
∀ {A : ord_choiceType} (o : Op) (c : raw_code A),
⊢ ⦃ λ '(h₀, h₁), h₀ = h₁ ⦄
a ← c ;; r ← (r ← sample o ;; ret r) ;; (ret (r, a)) ≈
r ← (r ← sample o ;; ret r) ;; a ← c ;; (ret (r, a))
⦃ eq ⦄.
Proof.
intros A o c.
unshelve eapply rswap_ruleR.
- auto.
- intros a r. apply rsym_pre. 1: auto.
apply rreflexivity_rule.
- apply rsamplerC.
Qed.
Lemma rsamplerC' :
∀ {A : ord_choiceType} (o : Op) (c : raw_code A),
⊢ ⦃ λ '(h₀, h₁), h₀ = h₁ ⦄
r ← (r ← sample o ;; ret r) ;; a ← c ;; ret (r, a) ≈
a ← c ;; r ← (r ← sample o ;; ret r) ;; ret (r, a)
⦃ eq ⦄.
Proof.
intros A o c.
eapply rsymmetry. eapply rsym_pre. 1: auto.
eapply rpost_weaken_rule.
- apply rsamplerC_sym'.
- intros [? ?] [? ?] e. inversion e. intuition auto.
Qed.
(* TODO: generalize the corresponding rule in RulesStateProb.v *)
Theorem rswap_rule_ctx :
∀ {A : ord_choiceType} {I pre} {post Q : postcond A A}
(l r c₀ c₁ : raw_code A),
⊢ ⦃ pre ⦄ l ≈ l ⦃ λ '(a₀, s₀) '(a₁, s₁), I (s₀, s₁) ⦄ →
(∀ a₀ a₁, ⊢ ⦃ λ '(s₁, s₀), Q (a₀,s₀) (a₁,s₁) ⦄ r ≈ r ⦃ post ⦄) →
⊢ ⦃ I ⦄ c₀ ≈ c₁ ⦃ λ '(a₀, s₀) '(a₁, s₁), I (s₀, s₁) ∧ Q (a₀, s₀) (a₁, s₁) ⦄ →
⊢ ⦃ I ⦄ c₁ ≈ c₀ ⦃ λ '(a₁, s₁) '(a₀, s₀), I (s₀, s₁) ∧ Q (a₀, s₀) (a₁, s₁) ⦄ →
⊢ ⦃ pre ⦄ l ;; c₀ ;; c₁ ;; r ≈ l ;; c₁ ;; c₀ ;; r ⦃ post ⦄.
Proof.
intros A I pre post Q l r c₀ c₁ hl hr h₀ h₁.
eapply from_sem_jdg.
rewrite !repr_bind.
eapply swap_rule_ctx.
1:{ eapply to_sem_jdg. exact hl. }
2:{ eapply to_sem_jdg. exact h₀. }
2:{ eapply to_sem_jdg. exact h₁. }
intros a₀ a₁. eapply to_sem_jdg. eapply hr.
Qed.