-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVPHL.v
3518 lines (3282 loc) · 113 KB
/
VPHL.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
(** * VPHL: A Verified Probabilistic Hoare Logic *)
Require Export PrImp.
Require Export Coq.Program.Equality.
Require Export Coq.Logic.FunctionalExtensionality.
(** ** Hoare Expressions
Hexps are the fundamental assertions of our Hoare Logic.
**)
Inductive hexp : Type :=
| HEq : bexp -> R -> hexp
| HLt : bexp -> R -> hexp
| HGt : bexp -> R -> hexp
| HAnd : hexp -> hexp -> hexp
| HOr : hexp -> hexp -> hexp.
(** Useful Abbreviations *)
Definition HLe b r: hexp := HOr (HLt b r) (HEq b r).
Definition HGe b r: hexp := HOr (HGt b r) (HEq b r).
Definition HNe b r: hexp := HOr (HLt b r) (HGt b r).
Fixpoint HNot hx :=
match hx with
| HEq b p => HNe b p
| HLt b p => HGe b p
| HGt b p => HLe b p
| HAnd h1 h2 => HOr (HNot h1) (HNot h2)
| HOr h1 h2 => HAnd (HNot h1) (HNot h2)
end.
Definition HIf P1 P2 : hexp := HOr (HNot P1) P2.
Definition HTrue y := HEq (BId y) 1.
Definition HFalse y := HEq (BId y) 0.
(** *** Evaluation *)
Fixpoint heval (dst : dstate) (h: hexp) {struct h}: Prop :=
match h with
| HEq b p => Prb b in dst = p
| HLt b p => Prb b in dst < p
| HGt b p => Prb b in dst > p
| HAnd h1 h2 => (heval dst h1) /\ (heval dst h2)
| HOr h1 h2 => (heval dst h1) \/ (heval dst h2)
end.
Lemma HGe_simpl: forall mst b p, heval mst (HGe b p) = (Prb b in mst >= p).
Proof. reflexivity. Qed.
Lemma HLe_simpl: forall mst b p, heval mst (HLe b p) = (Prb b in mst <= p).
Proof. reflexivity. Qed.
Lemma HNe_simpl: forall mst b p, heval mst (HNe b p) <-> (Prb b in mst <> p).
Proof. unfold not. split; intros.
simpl in *. lra.
simpl. lra.
Qed.
Definition h_implies (P Q : hexp) : Prop :=
forall dst, heval dst P -> heval dst Q.
Notation "P ->> Q" :=
(h_implies P Q) (at level 80) : hoare_spec_scope.
Open Scope hoare_spec_scope.
Notation "P <<->> Q" :=
(P ->> Q /\ Q ->> P) (at level 80) : hoare_spec_scope.
(** ** Hoare Triples *)
Definition hoare_triple
(P:hexp) (c:com) (Q:hexp) : Prop :=
forall dst dst',
c / dst || dst' ->
heval dst P ->
heval dst' Q.
Notation "{{ P }} c {{ Q }}" :=
(hoare_triple P c Q) (at level 90, c at next level)
: hoare_spec_scope.
Theorem hoare_post_true : forall (P Q : hexp) c,
(forall dst, heval dst Q) ->
{{P}} c {{Q}}.
Proof.
intros P Q c H. unfold hoare_triple.
intros dst dst' mcom HP.
apply H. Qed.
Theorem hoare_pre_false : forall (P Q : hexp) c,
(forall dst, ~(heval dst P)) ->
{{P}} c {{Q}}.
Proof.
intros P Q c H. unfold hoare_triple.
intros dst dst' mcom HP.
unfold not in H. apply H in HP.
inversion HP. Qed.
(** ** Hoare Consequence Rules: Largely preserved from Software Foundations *)
Theorem hoare_consequence_pre : forall (P P' Q : hexp) c,
{{P'}} c {{Q}} ->
P ->> P' ->
{{P}} c {{Q}}.
Proof.
intros P P' Q c Hhoare Himp.
intros dst dst' Hc HP. apply (Hhoare dst dst').
assumption. apply Himp. assumption. Qed.
Theorem hoare_consequence_post : forall (P Q Q' : hexp) c,
{{P}} c {{Q'}} ->
Q' ->> Q ->
{{P}} c {{Q}}.
Proof.
intros P Q Q' c Hhoare Himp.
intros dst dst' Hc HP.
apply Himp.
apply (Hhoare dst dst').
assumption. assumption. Qed.
Theorem hoare_consequence : forall (P P' Q Q' : hexp) c,
{{P'}} c {{Q'}} ->
P ->> P' ->
Q' ->> Q ->
{{P}} c {{Q}}.
Proof.
intros P P' Q Q' c Hht HPP' HQ'Q.
apply hoare_consequence_pre with (P' := P').
apply hoare_consequence_post with (Q' := Q').
assumption. assumption. assumption. Qed.
(** ** Restricted Assertions
Certain Restricted Hoare Assertions enable us to reason from distributions
to sub-distributions, and vice-versa
*)
Inductive non_disjunctive : hexp -> Prop :=
| nd_eq : forall b p, non_disjunctive (HEq b p)
| nd_lt : forall b p, non_disjunctive (HLt b p)
| nd_gt : forall b p, non_disjunctive (HGt b p)
| nd_conj : forall h1 h2,
non_disjunctive h1 ->
non_disjunctive h2 ->
non_disjunctive (HAnd h1 h2).
(** Lemma 4.1 *)
Lemma non_disjunctive_combine: forall P dst1 dst2 p v,
non_disjunctive P ->
heval dst1 P ->
heval dst2 P ->
heval (Combine p v dst1 dst2) P.
Proof.
intros.
induction H as [ | | | P1 P2 nd1 IH1 nd2 IH2]; simpl.
+ simpl in *.
rewrite H0, H1; lra.
+ apply scale_lt; assumption.
+ apply scale_gt; assumption.
+ destruct H0, H1.
split.
apply IH1; assumption.
apply IH2; assumption.
Qed.
Inductive non_probabilistic : hexp -> Prop :=
| np_0 : forall b, non_probabilistic (HEq b 0)
| np_1 : forall b, non_probabilistic (HEq b 1)
| np_conj : forall h1 h2,
non_probabilistic h1 ->
non_probabilistic h2 ->
non_probabilistic (HAnd h1 h2)
| np_disj : forall h1 h2,
non_probabilistic h1 ->
non_probabilistic h2 ->
non_probabilistic (HOr h1 h2).
(** Lemma 4.2 *)
Lemma non_probabilistic_split : forall P p v dst1 dst2,
non_probabilistic P ->
heval (Combine p v dst1 dst2) P ->
(heval dst1 P /\ heval dst2 P).
Proof.
intros P p v dst1 dst2 np.
induction np.
+ apply pr_split_0.
+ apply pr_split_1.
+ simpl; intuition.
+ simpl; intuition.
Qed.
Inductive deterministic : hexp -> Prop :=
| det_lit : forall b, deterministic (HEq b 1)
| det_conj : forall h1 h2,
deterministic h1 ->
deterministic h2 ->
deterministic (HAnd h1 h2).
(** Lemma 4.3 (1) *)
Lemma deterministic_combine : forall P p v dst1 dst2,
deterministic P ->
heval dst1 P ->
heval dst2 P ->
heval (Combine p v dst1 dst2) P.
Proof.
intros P p v dst1 dst2 np.
induction np.
+ apply pr_combine.
+ simpl; intuition.
Qed.
(** Lemma 4.3 (2) *)
Lemma deterministic_split : forall P p v dst1 dst2,
deterministic P ->
heval (Combine p v dst1 dst2) P ->
heval dst1 P /\ heval dst2 P.
Proof.
intros P p v dst1 dst2 np.
induction np.
+ apply pr_split_1.
+ simpl; intuition.
Qed.
(* ####################################################### *)
(** * DST Equivalence:
Since every distribution over states functions as a probability measure
over assertions, we should have a notion of distribution equivalence.
Note that since dsts are represented in tree form, and represent multisets of
states and weights, the actual representations of equivalent dsts can vary widely
(eg. any distribution can be expressed via an arbitrarily large tree).
**)
Definition dst_equiv (dst dst' : dstate) : Prop :=
forall b, Prb b in dst = Prb b in dst'.
Notation "a '==' b" := (dst_equiv a b) (at level 50).
Lemma dst_equiv_eq : forall st1 st2,
Unit st1 == Unit st2 ->
st1 = st2.
Proof.
intros.
apply injective_projections.
+ apply functional_extensionality.
intros.
specialize (H (BEq (AId x) (ANum (fst st1 x)))).
simpl in H.
rewrite <- beq_nat_refl in H.
destruct (beq_nat (fst st2 x) (fst st1 x)) eqn:Eq.
apply beq_nat_true in Eq.
symmetry.
assumption.
lra.
+ apply functional_extensionality.
intros.
specialize (H (BId x)).
simpl in H.
destruct (snd st1 x), (snd st2 x); trivial; lra.
Qed.
Lemma dst_equiv_refl : forall dst, dst == dst.
Proof. unfold dst_equiv. reflexivity. Qed.
Lemma dst_equiv_symm : forall dst1 dst2, dst1 == dst2 -> dst2 == dst1.
Proof. unfold dst_equiv. auto. Qed.
Lemma dst_equiv_trans : forall dst1 dst2 dst3,
dst1 == dst2 -> dst2 == dst3 -> dst1 == dst3.
Proof. unfold dst_equiv. intros. rewrite H. apply H0. Qed.
(* Necessary? *)
Lemma dst_equiv_split : forall b p v dst11 dst10 dst21 dst20,
(Combine p v dst11 dst10) == (Combine p v dst21 dst20) ->
Prb b in dst11 = 1 ->
Prb b in dst10 = 0 ->
Prb b in dst21 = 1 ->
Prb b in dst20 = 0 ->
dst11 == dst21 /\ dst10 == dst20.
Proof.
intros.
unfold dst_equiv in *.
split.
+ intros.
specialize (H (BAnd b0 b)).
simpl in *.
rewrite pr_conj_1_r in H; trivial.
rewrite pr_conj_0_r in H; trivial.
rewrite pr_conj_1_r in H; trivial.
rewrite pr_conj_0_r in H; trivial.
rewrite Rmult_0_r in H.
rewrite 2 Rplus_0_r in H.
apply Rmult_eq_reg_l with (r:=p); lra.
+ intros.
specialize (H (BAnd b0 (BNot b))).
apply prb_complement_1 in H0.
apply prb_complement_0 in H1.
apply prb_complement_1 in H2.
apply prb_complement_0 in H3.
simpl in H.
rewrite pr_conj_0_r in H; trivial.
rewrite pr_conj_1_r in H; trivial.
rewrite pr_conj_0_r in H; trivial.
rewrite pr_conj_1_r in H; trivial.
rewrite Rmult_0_r in H.
rewrite 2 Rplus_0_l in H.
apply Rmult_eq_reg_l with (r:=(1-p)); lra.
Qed.
Lemma dst_equiv_combine : forall p v dst11 dst12 dst21 dst22,
dst11 == dst21 ->
dst12 == dst22 ->
(Combine p v dst11 dst12) == (Combine p v dst21 dst22).
Proof. unfold dst_equiv. intros. simpl. rewrite H, H0. reflexivity. Qed.
Lemma dst_equiv_unit_split : forall st p v dst1 dst2,
(Combine p v dst1 dst2) == (Unit st) <->
dst1 == (Unit st) /\ dst2 == (Unit st).
Proof.
unfold dst_equiv.
simpl.
split; intros.
+ split.
- intros.
specialize (H b).
destruct (beval b st) eqn:Eq.
apply sum_to_1 in H; try lra; try apply pr_normality.
apply sum_to_0 in H; try lra; try apply pr_normality.
- intros.
specialize (H b).
destruct (beval b st) eqn:Eq.
apply sum_to_1 in H; try lra; try apply pr_normality.
apply sum_to_0 in H; try lra; try apply pr_normality.
+ destruct H as [H1 H2].
rewrite H1, H2.
lra.
Qed.
(**
The following lemmas assume we have the proof terms v'.
We could give the proof terms explicitly i.e. one_minus_p p v
*)
(** Lemma 2.1 Distribution Commutativity *)
Lemma dst_comm : forall p v v' dst1 dst2,
(Combine p v dst1 dst2) == (Combine (1-p) v' dst2 dst1).
Proof. unfold dst_equiv. simpl. intros. lra. Qed.
(** Lemma 2.2 Distribution Associativity *)
Lemma dst_assoc : forall p q v v' w w' dst1 dst2 dst3,
Combine q w (Combine p v dst1 dst2) dst3 ==
Combine (p*q) w' dst1 (Combine (q*(1-p)/(1-p*q)) v' dst2 dst3).
Proof.
unfold dst_equiv.
intros.
simpl.
rewrite ! Rmult_plus_distr_l.
replace (q * (p * (Prb b in dst1))) with (p * q * (Prb b in dst1)) by lra.
unfold Rdiv.
rewrite <- 3 Rmult_assoc.
rewrite Rinv_r_simpl_m. 2:lra.
rewrite <- Rplus_assoc.
rewrite <- Rmult_assoc.
assert (H:(1 - p * q) * (1 - q * (1 - p) * / (1 - p * q)) = (1-q)).
2: rewrite H; reflexivity.
unfold Rminus.
rewrite ! Rmult_plus_distr_l.
rewrite <- ! Ropp_mult_distr_l_reverse.
rewrite <- ! Rmult_assoc.
rewrite Rinv_r_simpl_m. 2:lra.
rewrite ! Rmult_1_r.
lra.
Qed.
(** Lemma 2.3 Distribution Merge *)
Lemma dst_merge : forall p q r u v w u' v' w' dst1 dst2 dst3 dst4,
Combine p u (Combine q v dst1 dst2) (Combine r w dst3 dst4) ==
Combine (p*q + (1-p)*r) u'
(Combine ((p*q)/(p*q + (1-p)*r)) v' dst1 dst3)
(Combine (p*(1-q)/ (1 - (p*q + (1-p)*r))) w' dst2 dst4).
Proof.
unfold dst_equiv; intros.
simpl.
unfold Rdiv.
rewrite ! Rmult_plus_distr_l.
rewrite <- ! Rmult_assoc.
rewrite <- ! Rplus_assoc.
assert (H: (p * q + (1 - p) * r) * p * q * / (p * q + (1 - p) * r) = p * q).
rewrite Rmult_comm.
rewrite <- ! Rmult_assoc.
rewrite <- Rinv_l_sym; lra.
rewrite H. clear H.
assert (H: (1 - (p * q + (1 - p) * r)) * p * (1 - q) * / (1 - (p * q + (1 - p) * r)) = p * (1-q)).
rewrite Rmult_comm.
rewrite <- ! Rmult_assoc.
rewrite <- Rinv_l_sym; lra.
rewrite H. clear H.
assert (H: (p * q + (1 - p) * r) * (1 - p * q * / (p * q + (1 - p) * r)) = (1-p) * r).
unfold Rminus.
rewrite ! Rmult_plus_distr_l.
rewrite Rmult_1_r.
rewrite <- ! Ropp_mult_distr_l_reverse.
rewrite <- ! Rmult_assoc.
rewrite Rmult_comm with (r2:=/ (p * q + (1 + - p) * r)).
rewrite <- ! Rmult_assoc.
rewrite <- Rinv_l_sym; lra.
rewrite H. clear H.
assert(H: (1 - (p * q + (1 - p) * r)) *
(1 - p * (1 - q) * / (1 - (p * q + (1 - p) * r))) = (1-p)*(1-r)).
unfold Rminus.
rewrite ! Rmult_plus_distr_l.
rewrite Rmult_1_r.
rewrite <- ! Ropp_mult_distr_l_reverse.
rewrite <- ! Rmult_assoc.
rewrite Rinv_r_simpl_m; lra.
rewrite H. clear H.
lra.
Qed.
(** Updating at the root or leaves is equivalent *)
Lemma prob_update_equiv : forall dst x p v,
prob_update dst x p v ==
Combine p v (dist_update dst x (ANum 1)) (dist_update dst x (ANum 0)).
Proof.
unfold dst_equiv; simpl.
intros.
induction dst.
+ simpl. reflexivity.
+ simpl.
rewrite IHdst1, IHdst2.
lra.
Qed.
Lemma prob_update_b_equiv : forall dst y p v,
prob_update_b dst y p v ==
Combine p v (dist_update_b dst y BTrue) (dist_update_b dst y BFalse).
Proof.
unfold dst_equiv; simpl.
intros.
induction dst.
+ simpl. reflexivity.
+ simpl.
rewrite IHdst1, IHdst2.
lra.
Qed.
(** We can equivalently define dst_equiv over assertions, rather than probabilities. *)
Definition dst_equiv_hexp (dst dst': dstate) : Prop :=
forall P, heval dst P <-> heval dst' P.
Theorem dst_equiv_iff : forall dst dst',
dst_equiv dst dst' <-> dst_equiv_hexp dst dst'.
Proof.
split; intros.
+ induction P; simpl in *; try solve [rewrite H; reflexivity].
- rewrite IHP1, IHP2.
reflexivity.
- rewrite IHP1, IHP2.
reflexivity.
+ unfold dst_equiv.
intros.
destruct H with (P:=(HEq b (Prb b in dst'))) as [ignore solution].
apply solution.
reflexivity.
Qed.
Theorem dst_comm_hexp : forall dst1 dst2 p v v',
dst_equiv_hexp (Combine p v dst1 dst2) (Combine (1-p) v' dst2 dst1).
Proof.
intros.
apply dst_equiv_iff.
apply dst_comm.
Qed.
(** ** The Hoare Skip Rule *)
Theorem hoare_skip : forall P,
{{P}} Skip {{P}}.
Proof.
intros P dst dst' step pre.
apply skip_equiv in step.
subst.
assumption.
Qed.
(** ** The Hoare Sequence Rule *)
Theorem hoare_seq : forall P Q R c1 c2,
{{P}} c1 {{Q}} ->
{{Q}} c2 {{R}} ->
{{P}} c1 ; c2 {{R}}.
Proof.
intros P Q R c1 c2 hoare1 hoare2 dst dst' step pre.
apply seq_equiv in step.
destruct step as (dst0 & step1 & step2).
apply (hoare2 dst0 dst' step2).
apply (hoare1 dst dst0 step1 pre).
Qed.
(** * Hoare Assignment **)
(** First, we substitute a for X thoughout an arithmetic expression. *)
Fixpoint aexp_sub x a ax {struct ax} : aexp :=
match ax with
| ANum n => ANum n
| AId x' => if beq_aid x x' then a else AId x'
| APlus a1 a2 => APlus (aexp_sub x a a1) (aexp_sub x a a2)
| AMinus a1 a2 => AMinus (aexp_sub x a a1) (aexp_sub x a a2)
| AMult a1 a2 => AMult (aexp_sub x a a1) (aexp_sub x a a2)
end.
Fixpoint bexp_sub x a bx {struct bx} : bexp :=
match bx with
| BTrue => BTrue
| BFalse => BFalse
| BId y => BId y
| BEq a1 a2 => BEq (aexp_sub x a a1) (aexp_sub x a a2)
| BLe a1 a2 => BLe (aexp_sub x a a1) (aexp_sub x a a2)
| BNot b1 => BNot (bexp_sub x a b1)
| BAnd b1 b2 => BAnd (bexp_sub x a b1) (bexp_sub x a b2)
end.
Fixpoint hexp_sub x a hx {struct hx} : hexp :=
match hx with
| HEq b p => HEq (bexp_sub x a b) p
| HLt b p => HLt (bexp_sub x a b) p
| HGt b p => HGt (bexp_sub x a b) p
| HAnd h1 h2 => HAnd (hexp_sub x a h1) (hexp_sub x a h2)
| HOr h1 h2 => HOr (hexp_sub x a h1) (hexp_sub x a h2)
end.
Theorem a_sub_eq : forall x a ax st,
aeval (aexp_sub x a ax) st = aeval ax (update st x (aeval a st)).
Proof.
intros.
generalize dependent ax.
induction a.
+ simpl.
induction ax.
- simpl. reflexivity.
- simpl.
unfold update.
destruct (beq_aid x a).
reflexivity.
reflexivity.
- simpl in *. rewrite IHax1. rewrite IHax2. reflexivity.
- simpl in *. rewrite IHax1. rewrite IHax2. reflexivity.
- simpl in *. rewrite IHax1. rewrite IHax2. reflexivity.
+ induction ax; try (solve [simpl; rewrite IHax1; rewrite IHax2; reflexivity]).
- simpl. reflexivity.
- simpl.
unfold update.
destruct (beq_aid x a0).
reflexivity.
reflexivity.
+ induction ax; try (solve [simpl; rewrite IHax1; rewrite IHax2; reflexivity]).
- simpl. reflexivity.
- simpl in *.
destruct (beq_aid x a) eqn:eq; trivial.
+ induction ax; try (solve [simpl; rewrite IHax1; rewrite IHax2; reflexivity]).
- simpl. reflexivity.
- simpl in *.
destruct (beq_aid x a) eqn:eq; trivial.
+ induction ax; try (solve [simpl; rewrite IHax1; rewrite IHax2; reflexivity]).
- simpl. reflexivity.
- simpl in *.
destruct (beq_aid x a) eqn:eq; trivial.
Qed.
Theorem b_sub_eq : forall x a bx st,
beval (bexp_sub x a bx) st = beval bx (update st x (aeval a st)).
Proof.
intros.
induction bx; simpl; trivial.
+ rewrite 2 a_sub_eq; reflexivity.
+ rewrite 2 a_sub_eq; reflexivity.
+ rewrite IHbx; reflexivity.
+ rewrite IHbx1, IHbx2; reflexivity.
Qed.
Lemma pr_sub: forall x a b dst,
Prb bexp_sub x a b in dst = Prb b in dist_update dst x a.
Proof.
intros.
induction dst.
+ simpl. rewrite b_sub_eq. reflexivity.
+ simpl in *. rewrite IHdst1, IHdst2. reflexivity.
Qed.
Theorem h_sub_eq : forall x a hx dst,
heval dst (hexp_sub x a hx) =
heval (dist_update dst x a) hx.
Proof.
intros.
induction hx.
+ induction dst; simpl.
- rewrite b_sub_eq; reflexivity.
- rewrite 2 pr_sub; reflexivity.
+ induction dst; simpl.
- rewrite b_sub_eq; reflexivity.
- rewrite 2 pr_sub; reflexivity.
+ induction dst; simpl.
- rewrite b_sub_eq; reflexivity.
- rewrite 2 pr_sub; reflexivity.
+ simpl. rewrite IHhx1, IHhx2. reflexivity.
+ simpl. rewrite IHhx1, IHhx2. reflexivity.
Qed.
(** ** The Hoare Assignment Rule *)
Theorem hoare_asgn : forall Q x a,
{{hexp_sub x a Q}} (x ::= a) {{Q}}.
Proof.
intros Q x a dst dst' step pre.
apply assign_equiv in step.
subst.
rewrite <- h_sub_eq.
assumption.
Qed.
(** The Related Equivalence Rule *)
Lemma update_equiv : forall dst1 dst2 x n,
dst_equiv dst1 dst2 ->
dst_equiv (dist_update dst1 x n) (dist_update dst2 x n).
Proof.
intros.
rewrite dst_equiv_iff in *.
unfold dst_equiv_hexp.
intros.
rewrite <- 2 h_sub_eq.
apply H.
Qed.
(** * Boolean Assignment
This follows the same structure as above *)
Fixpoint bexp_sub_b y b bx {struct bx} : bexp :=
match bx with
| BTrue => BTrue
| BFalse => BFalse
| BId y' => if beq_bid y y' then b else BId y'
| BEq a1 a2 => BEq a1 a2
| BLe a1 a2 => BLe a1 a2
| BNot b1 => BNot (bexp_sub_b y b b1)
| BAnd b1 b2 => BAnd (bexp_sub_b y b b1) (bexp_sub_b y b b2)
end.
Fixpoint hexp_sub_b y b hx {struct hx} : hexp :=
match hx with
| HEq b' p => HEq (bexp_sub_b y b b') p
| HLt b' p => HLt (bexp_sub_b y b b') p
| HGt b' p => HGt (bexp_sub_b y b b') p
| HAnd h1 h2 => HAnd (hexp_sub_b y b h1) (hexp_sub_b y b h2)
| HOr h1 h2 => HOr (hexp_sub_b y b h1) (hexp_sub_b y b h2)
end.
Theorem b_sub_eq_b : forall y b bx st,
beval (bexp_sub_b y b bx) st = beval bx (update_b st y (beval b st)).
Proof.
intros.
induction bx; simpl; trivial.
+ destruct (beq_bid y b0); trivial.
+ rewrite 2 update_non_interference; reflexivity.
+ rewrite 2 update_non_interference; reflexivity.
+ rewrite IHbx; reflexivity.
+ rewrite IHbx1, IHbx2; reflexivity.
Qed.
Lemma pr_sub_b: forall y b bx dst,
Prb bexp_sub_b y b bx in dst = Prb bx in dist_update_b dst y b.
Proof.
intros.
induction dst.
+ simpl; rewrite b_sub_eq_b; reflexivity.
+ simpl in *; rewrite IHdst1, IHdst2; reflexivity.
Qed.
Theorem h_sub_eq_b : forall y b hx dst,
heval dst (hexp_sub_b y b hx) =
heval (dist_update_b dst y b) hx.
Proof.
intros.
induction hx.
+ induction dst; simpl.
- rewrite b_sub_eq_b; reflexivity.
- rewrite 2 pr_sub_b; reflexivity.
+ induction dst; simpl.
- rewrite b_sub_eq_b; reflexivity.
- rewrite 2 pr_sub_b; reflexivity.
+ induction dst; simpl.
- rewrite b_sub_eq_b; reflexivity.
- rewrite 2 pr_sub_b; reflexivity.
+ simpl. rewrite IHhx1, IHhx2. reflexivity.
+ simpl. rewrite IHhx1, IHhx2. reflexivity.
Qed.
(** ** The Hoare Boolean Assignment Rule *)
Theorem hoare_asgn_b : forall Q y b,
{{hexp_sub_b y b Q}} (y :== b) {{Q}}.
Proof.
intros Q y b dst dst' step pre.
apply bassign_equiv in step.
subst.
rewrite <- h_sub_eq_b.
assumption.
Qed.
(** The Related Equivalence Rule *)
Lemma update_equiv_b : forall dst1 dst2 y b,
dst_equiv dst1 dst2 ->
dst_equiv (dist_update_b dst1 y b) (dist_update_b dst2 y b).
Proof.
intros.
rewrite dst_equiv_iff in *.
unfold dst_equiv_hexp.
intros.
rewrite <- 2 h_sub_eq_b.
apply H.
Qed.
(** * Hoare Toss *)
(** ** Hoare Toss Preliminaries: X-freeness
Let's introduce the idea of X-freeness - a given variable appears nowhere
in our Hoare Assertions *)
Inductive ax_id_free: aexp -> aid -> Prop :=
| Af_num : forall n x, ax_id_free (ANum n) x
| Af_id : forall x' x, x<>x' -> ax_id_free (AId x') x
| Af_plus : forall a1 a2 x,
ax_id_free a1 x -> ax_id_free a2 x -> ax_id_free (APlus a1 a2) x
| Af_minus : forall a1 a2 x,
ax_id_free a1 x -> ax_id_free a2 x -> ax_id_free (AMinus a1 a2) x
| Af_mult : forall a1 a2 x,
ax_id_free a1 x -> ax_id_free a2 x -> ax_id_free (AMult a1 a2) x.
Inductive bx_id_free: bexp -> aid -> Prop :=
| Bf_true : forall x, bx_id_free BTrue x
| Bf_false : forall x, bx_id_free BFalse x
| Bf_id : forall y x, bx_id_free (BId y) x
| Bf_eq : forall a1 a2 x,
ax_id_free a1 x -> ax_id_free a2 x -> bx_id_free (BEq a1 a2) x
| Bf_le : forall a1 a2 x,
ax_id_free a1 x -> ax_id_free a2 x -> bx_id_free (BLe a1 a2) x
| Bf_not: forall b x, bx_id_free b x -> bx_id_free (BNot b) x
| Bf_and : forall b1 b2 x,
bx_id_free b1 x -> bx_id_free b2 x -> bx_id_free (BAnd b1 b2) x.
Inductive hx_id_free: hexp -> aid -> Prop :=
| Hf_eq : forall b p x, bx_id_free b x -> hx_id_free (HEq b p) x
| Hf_lt : forall b p x, bx_id_free b x -> hx_id_free (HLt b p) x
| Hf_gt : forall b p x, bx_id_free b x -> hx_id_free (HGt b p) x
| Hf_and : forall h1 h2 x,
hx_id_free h1 x -> hx_id_free h2 x -> hx_id_free (HAnd h1 h2) x
| Hf_or : forall h1 h2 x,
hx_id_free h1 x -> hx_id_free h2 x -> hx_id_free (HOr h1 h2) x.
(** This wind up being a useful weaker notion of freeness that allows us to use
earlier results relating to variable substitution. *)
Definition free (Q : hexp) (x : aid) : Prop := forall a, hexp_sub x a Q = Q.
(** We need to show that the weaker form follows from the stronger one. *)
Lemma a_free_eq: forall a x ax,
ax_id_free ax x -> aexp_sub x a ax = ax.
Proof.
intros.
induction H; simpl.
+ reflexivity.
+ apply beq_aid_false_iff in H.
rewrite H; reflexivity.
+ rewrite IHax_id_free1, IHax_id_free2; reflexivity.
+ rewrite IHax_id_free1, IHax_id_free2; reflexivity.
+ rewrite IHax_id_free1, IHax_id_free2; reflexivity.
Qed.
Lemma b_free_eq: forall a x bx,
bx_id_free bx x -> bexp_sub x a bx = bx.
Proof.
intros.
induction H; simpl; trivial.
+ eapply a_free_eq in H.
eapply a_free_eq in H0.
rewrite H, H0. reflexivity.
+ eapply a_free_eq in H.
eapply a_free_eq in H0.
rewrite H, H0. reflexivity.
+ rewrite IHbx_id_free. reflexivity.
+ rewrite IHbx_id_free1, IHbx_id_free2. reflexivity.
Qed.
(** These two implication of the above will prove useful shortly. *)
Lemma a_free_imp : forall a x st n,
ax_id_free a x ->
aeval a (update st x n) = aeval a st.
Proof.
intros.
apply a_free_eq with (a:=ANum n) in H.
replace n with (aeval (ANum n) st) by reflexivity.
rewrite <- a_sub_eq.
rewrite H.
reflexivity.
Qed.
Lemma b_free_imp : forall b x st n,
bx_id_free b x ->
beval b (update st x n) = beval b st.
Proof.
intros.
apply b_free_eq with (a:=ANum n) in H.
replace n with (aeval (ANum n) st) by reflexivity.
rewrite <- b_sub_eq.
rewrite H.
reflexivity.
Qed.
Lemma free_eq: forall Q x,
hx_id_free Q x -> free Q x.
Proof.
unfold free.
intros.
induction H; simpl.
+ eapply b_free_eq in H.
rewrite H. reflexivity.
+ eapply b_free_eq in H.
rewrite H. reflexivity.
+ eapply b_free_eq in H.
rewrite H. reflexivity.
+ rewrite IHhx_id_free1, IHhx_id_free2. reflexivity.
+ rewrite IHhx_id_free1, IHhx_id_free2. reflexivity.
Qed.
(** A simple consequence of freeness and the hoare_assignment rule : *)
Lemma hoare_asgn_free : forall Q x a,
free Q x ->
{{ Q }} (x ::= a) {{ Q }}.
Proof.
intros Q x a freeQ dst dst' step pre.
apply assign_equiv in step.
subst.
rewrite <- h_sub_eq.
unfold free in freeQ.
rewrite freeQ.
assumption.
Qed.
(** Conditionals allow us to insert boolean expressions throughout our assertions *)
Fixpoint bicondition01 (hx:hexp) (x:aid) (p:R) : hexp :=
match hx with
| HEq b p' => HAnd (HEq (BAnd b (BEq (AId x) (ANum 1%nat))) (p*p'))
(HEq (BAnd b (BEq (AId x) (ANum 0%nat))) ((1-p)*p'))
| HLt b p' => HAnd (HLt (BAnd b (BEq (AId x) (ANum 1%nat))) (p*p'))
(HLt (BAnd b (BEq (AId x) (ANum 0%nat))) ((1-p)*p'))
| HGt b p' => HAnd (HGt (BAnd b (BEq (AId x) (ANum 1%nat))) (p*p'))
(HGt (BAnd b (BEq (AId x) (ANum 0%nat))) ((1-p)*p'))
| HAnd P1 P2 => HAnd (bicondition01 P1 x p) (bicondition01 P2 x p)
| HOr P1 P2 => HOr (bicondition01 P1 x p) (bicondition01 P2 x p)
end.
(** Independence rules for tossing a free variable *)
Lemma prob_update_1 : forall dst x b r p v,
bx_id_free b x ->
Prb b in dst = r ->
Prb BAnd b (BEq (AId x) (ANum 1)) in prob_update dst x p v = p * r.
Proof.
intros.
generalize dependent r.
induction dst.
intros.
+ simpl in *.
apply b_free_eq with (a := ANum 1) in H.
replace 1%nat with (aeval (ANum 1%nat) t) by reflexivity.
rewrite <- b_sub_eq.
rewrite H.
simpl.
rewrite beq_aid_refl.
rewrite <- beq_nat_refl.
simpl.
rewrite andb_true_r.
rewrite andb_false_r.
rewrite H0.
lra.
+ intros.
simpl in *.
erewrite IHdst1 by reflexivity.
erewrite IHdst2 by reflexivity.
rewrite <- 2 Rmult_assoc.
rewrite Rmult_comm with (r1:=p0) (r2:=p).
rewrite Rmult_comm with (r1:=(1-p0)) (r2:=p).
rewrite 2 Rmult_assoc.
rewrite <- Rmult_plus_distr_l.
rewrite H0.
reflexivity.
Qed.
Lemma prob_update_0 : forall dst x b r p v,
bx_id_free b x ->
Prb b in dst = r ->
Prb BAnd b (BEq (AId x) (ANum 0)) in prob_update dst x p v = (1-p) * r.
Proof.
intros.
generalize dependent r.
induction dst.
intros.
+ simpl in *.
apply b_free_eq with (a := ANum 0) in H.
replace 0%nat with (aeval (ANum 0%nat) t) by reflexivity.
rewrite <- b_sub_eq.
rewrite H.
simpl.
rewrite beq_aid_refl.
rewrite <- beq_nat_refl.
simpl.
rewrite andb_true_r.
rewrite andb_false_r.
rewrite H0.
lra.
+ intros.
simpl in *.
erewrite IHdst1 by reflexivity.
erewrite IHdst2 by reflexivity.
rewrite <- 2 Rmult_assoc.
rewrite Rmult_comm with (r1:=p0) (r2:=(1-p)).
rewrite Rmult_comm with (r1:=(1-p0)) (r2:=(1-p)).
rewrite 2 Rmult_assoc.
rewrite <- Rmult_plus_distr_l.
rewrite H0.
reflexivity.
Qed.
(** ** The Hoare Toss Rule *)
Theorem hoare_toss : forall Q x p v, hx_id_free Q x ->
{{ Q }} x $=(p,v) {{ bicondition01 Q x p }}.
Proof.
intros Q x p v hfQ dst dst' step pre.
apply toss_equiv in step; subst.
specialize (prob_update_0 dst x). intros pu0.
specialize (prob_update_1 dst x). intros pu1.
induction Q.
+ simpl in *.
inversion hfQ; subst.
rewrite pu0 with (r:=(Prb b in dst)); trivial.
rewrite pu1 with (r:=(Prb b in dst)); trivial.
split; reflexivity.
+ simpl in *.
inversion hfQ; subst.
rewrite pu0 with (r:=(Prb b in dst)); trivial.
rewrite pu1 with (r:=(Prb b in dst)); trivial.
split; apply Rmult_lt_compat_l; trivial; lra.
+ simpl in *.
inversion hfQ; subst.
rewrite pu0 with (r:=(Prb b in dst)); trivial.
rewrite pu1 with (r:=(Prb b in dst)); trivial.
split; apply Rmult_gt_compat_l; trivial; lra.
+ simpl.
inversion hfQ; subst.
destruct pre.
split.
apply IHQ1; assumption.
apply IHQ2; assumption.
+ simpl.
inversion hfQ; subst.
destruct pre.
left. apply IHQ1; assumption.
right. apply IHQ2; assumption.
Qed.
(** * BToss *)
(** This follows the same structure as above - though the boolean coin toss
operation is the important one in this case. *)
Inductive bx_bid_free: bexp -> bid -> Prop :=
| Bf_true_b : forall y, bx_bid_free BTrue y
| Bf_false_b : forall y, bx_bid_free BFalse y
| Bf_id_b : forall y' y, y<>y' -> bx_bid_free (BId y') y
| Bf_eq_b : forall a1 a2 y, bx_bid_free (BEq a1 a2) y
| Bf_le_b : forall a1 a2 y, bx_bid_free (BLe a1 a2) y
| Bf_not_b: forall b y, bx_bid_free b y -> bx_bid_free (BNot b) y
| Bf_and_b : forall b1 b2 y,
bx_bid_free b1 y -> bx_bid_free b2 y -> bx_bid_free (BAnd b1 b2) y.