Repository navigation
Expand file tree
/
Copy pathSymbolicExecution.v
More file actions
2252 lines (1910 loc) · 65.9 KB
/
Copy pathSymbolicExecution.v
File metadata and controls
2252 lines (1910 loc) · 65.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(** Formal Reasoning About Programs <http://adam.chlipala.net/frap/>
* Chapter 16*: Symbolic Execution
* Author: Adam Chlipala
* License: https://creativecommons.org/licenses/by-nc-nd/4.0/ *)
Require Import Frap AutomatedTheoremProving.
From Stdlib Require Import FunctionalExtensionality.
(* The last chapter explained (the beginnings of) how to automate proofs in
* simple fragments of first-order logic, one part of the established recipe for
* automating verification of imperative programs. We now turn to the other
* part: symbolic execution and other ways of algorithmically reducing program
* correctness to problems in first-order logic. *)
(** * Our object language *)
(* Let's make a reduced copy of our language from the HoareLogic chapter. We'll
* omit both loops and memory access. Loops can be handled (exercise for the
* reader!) by interpreting a loopy program as a number of related loop-free
* programs, connected in using loop invariants as intermediate preconditions
* and postconditions. We'll return to memory in the SeparationLogic chapter,
* since there are enough nonobvious ideas there to deserve our main
* attention. *)
Inductive exp :=
| Const (n : nat)
| Var (x : string)
| Plus (e1 e2 : exp)
| Minus (e1 e2 : exp)
| Mult (e1 e2 : exp).
Inductive bexp :=
| Equal (e1 e2 : exp)
| Less (e1 e2 : exp).
Definition valuation := fmap var nat.
Definition assertion := valuation -> Prop.
Inductive cmd :=
| Skip
| Assign (x : var) (e : exp)
| Seq (c1 c2 : cmd)
| If_ (be : bexp) (then_ else_ : cmd).
Notation "m $! k" := (match m $? k with Some n => n | None => O end) (at level 30).
Fixpoint eval (e : exp) (v : valuation) : nat :=
match e with
| Const n => n
| Var x => v $! x
| Plus e1 e2 => eval e1 v + eval e2 v
| Minus e1 e2 => eval e1 v - eval e2 v
| Mult e1 e2 => eval e1 v * eval e2 v
end.
Definition beval (b : bexp) (v : valuation) : bool :=
match b with
| Equal e1 e2 => if eval e1 v ==n eval e2 v then true else false
| Less e1 e2 => if eval e2 v <=? eval e1 v then false else true
end.
Inductive exec : valuation -> cmd -> valuation -> Prop :=
| ExSkip : forall v,
exec v Skip v
| ExAssign : forall v x e,
exec v (Assign x e) (v $+ (x, eval e v))
| ExSeq : forall v1 c1 v2 c2 v3,
exec v1 c1 v2
-> exec v2 c2 v3
-> exec v1 (Seq c1 c2) v3
| ExIfTrue : forall v1 b c1 c2 v2,
beval b v1 = true
-> exec v1 c1 v2
-> exec v1 (If_ b c1 c2) v2
| ExIfFalse : forall v1 b c1 c2 v2,
beval b v1 = false
-> exec v1 c2 v2
-> exec v1 (If_ b c1 c2) v2.
(* Now we define a further syntactic language of logical predicates, which we
* will use in program specifications. Recall that, in the HoareLogic chapter,
* we used native Rocq predicates, which is convenient for integration with
* Rocq, but now we want to show how similar functionality can be provided by
* highly automated, standalone programs. There is no good way for functional
* programs in Rocq (technically the language is called "Gallina") to introspect
* into the structure of native predicates, but we need introspection to
* implement our automation. *)
Inductive predicate :=
| Tru
| Fals
| Exp (e : bexp)
| NotExp (e : bexp)
| And (p1 p2 : predicate)
| Or (p1 p2 : predicate)
| Ex (x : var) (p1 : predicate).
(* Note that we make our lives a *little* easy by restricting negation to apply
* to atomic predicates only. However, we also make our lives more than a
* *little* tough by supporting existential quantifiers. Below, we'll see
* examples of how variable binding is often the biggest source of
* administrative headache in mechanized proofs! *)
Fixpoint evalPredicate (p : predicate) (v : valuation) : Prop :=
match p with
| Tru => True
| Fals => False
| Exp e => beval e v = true
| NotExp e => beval e v = false
| And p1 p2 => evalPredicate p1 v /\ evalPredicate p2 v
| Or p1 p2 => evalPredicate p1 v \/ evalPredicate p2 v
| Ex x p1 => exists xv, evalPredicate p1 (v $+ (x, xv))
end.
(* The start of the administrative bonanza: different functions to substitute
* expressions for variables *)
Fixpoint subExp (inThis : exp) (replaceThis : var) (withThis : exp) : exp :=
match inThis with
| Const _ => inThis
| Var x => if x ==v replaceThis then withThis else inThis
| Plus e1 e2 => Plus (subExp e1 replaceThis withThis) (subExp e2 replaceThis withThis)
| Minus e1 e2 => Minus (subExp e1 replaceThis withThis) (subExp e2 replaceThis withThis)
| Mult e1 e2 => Mult (subExp e1 replaceThis withThis) (subExp e2 replaceThis withThis)
end.
Definition subBexp (inThis : bexp) (replaceThis : var) (withThis : exp) : bexp :=
match inThis with
| Equal e1 e2 => Equal (subExp e1 replaceThis withThis) (subExp e2 replaceThis withThis)
| Less e1 e2 => Less (subExp e1 replaceThis withThis) (subExp e2 replaceThis withThis)
end.
Fixpoint subPredicate (inThis : predicate) (replaceThis : var) (withThis : exp) : predicate :=
match inThis with
| Tru | Fals => inThis
| Exp e => Exp (subBexp e replaceThis withThis)
| NotExp e => NotExp (subBexp e replaceThis withThis)
| And p1 p2 => And (subPredicate p1 replaceThis withThis) (subPredicate p2 replaceThis withThis)
| Or p1 p2 => Or (subPredicate p1 replaceThis withThis) (subPredicate p2 replaceThis withThis)
| Ex x p1 => if x ==v replaceThis then
inThis
else
Ex x (subPredicate p1 replaceThis withThis)
end.
(* We must also define which variables appear in which places. *)
Fixpoint varInExp (e : exp) (x : var) : Prop :=
match e with
| Const _ => False
| Var y => y = x
| Plus e1 e2 | Minus e1 e2 | Mult e1 e2 => varInExp e1 x \/ varInExp e2 x
end.
Definition varInBexp (e : bexp) (x : var) : Prop :=
match e with
| Equal e1 e2 | Less e1 e2 => varInExp e1 x \/ varInExp e2 x
end.
Fixpoint varInCmd (c : cmd) (x : var) : Prop :=
match c with
| Skip => False
| Assign y e => y = x \/ varInExp e x
| Seq c1 c2 => varInCmd c1 x \/ varInCmd c2 x
| If_ e c1 c2 => varInBexp e x \/ varInCmd c1 x \/ varInCmd c2 x
end.
Fixpoint varInPredicate (p : predicate) (x : var) : Prop :=
match p with
| Tru | Fals => False
| Exp e | NotExp e => varInBexp e x
| And p1 p2 | Or p1 p2 => varInPredicate p1 x \/ varInPredicate p2 x
| Ex y p1 => y = x \/ varInPredicate p1 x
end.
(* Now the grungiest part of all: *generating fresh variable names*. Following
* a long mathematical tradition, we'll start with one base variable and keep
* adding apostrophes at the end of its name, which get pronounced with
* successively many copies of the word "prime." (The only detail that matters
* here is that we are able to produce a countably infinite sequence of distinct
* variable names.) *)
Definition bumpVar (x : string) : string := x ++ "'".
Fixpoint multibumpVar (n : nat) (x : string) : string :=
match n with
| O => x
| S n' => bumpVar (multibumpVar n' x)
end.
(** * TECHNIQUE #1: Strongest Postconditions *)
(* Finally, here's our first method to automate reducing program correctness to
* queries in first-order logic. A *strongest-postcondition* function
* transforms a precondition into a postcondition guaranteed to be true, after
* running the program in an initial state satisfying the precondition. Note
* the bureaucratic hassle of threading through a record of which temporary
* variable name we should use next. You'll see that such variables are only
* used with "exists" in explaining variable assignment, essentially the same
* way as we built into our Hoare-logic rule for assignment. *)
Fixpoint spost (c : cmd) (nextVar : var) (p : predicate) : predicate * var :=
match c with
| Skip => (p, nextVar)
| Assign x e => (Ex nextVar (And (subPredicate p x (Var nextVar)) (Exp (Equal (Var x) (subExp e x (Var nextVar))))), bumpVar nextVar)
| Seq c1 c2 =>
let (p, nextVar) := spost c1 nextVar p in
spost c2 nextVar p
| If_ e c1 c2 =>
let (p1, nextVar) := spost c1 nextVar (And p (Exp e)) in
let (p2, nextVar) := spost c2 nextVar (And p (NotExp e)) in
(Or p1 p2, nextVar)
end.
(* In fact, all of these rules should look pretty familiar from Hoare logic. *)
(* OK, now we are going to need a host of bureaucratic facts about these
* definitions. Feel free to skip down to the next [Theorem]. *)
Lemma eval_subExp : forall replaceThis withThis v inThis,
eval (subExp inThis replaceThis withThis) v
= eval inThis (v $+ (replaceThis, eval withThis v)).
Proof.
induct inThis; simplify; auto.
cases (x ==v replaceThis); subst; simplify; auto.
Qed.
Local Hint Rewrite eval_subExp.
Lemma eval_subBexp : forall replaceThis withThis v inThis,
beval (subBexp inThis replaceThis withThis) v
= beval inThis (v $+ (replaceThis, eval withThis v)).
Proof.
induct inThis; simplify; auto.
Qed.
Local Hint Rewrite eval_subBexp.
Lemma eval_relevant : forall e v1 v2,
(forall x, varInExp e x -> v1 $! x = v2 $! x)
-> eval e v1 = eval e v2.
Proof.
induct e; simplify; auto.
Qed.
Local Hint Resolve eval_relevant : core.
Lemma beval_relevant : forall e v1 v2,
(forall x, varInBexp e x -> v1 $! x = v2 $! x)
-> beval e v1 = beval e v2.
Proof.
induct e; simplify;
repeat match goal with
| [ |- context[if ?E then _ else _] ] => cases E; simplify
end; auto.
rewrite eval_relevant with (v2 := v1) in n; eauto using eq_sym.
rewrite e in n.
exfalso; eauto.
rewrite eval_relevant with (v2 := v2) in n; eauto using eq_sym.
rewrite e in n.
exfalso; eauto 6 using eq_sym.
rewrite eval_relevant with (v2 := v1) in l0; eauto using eq_sym.
rewrite eval_relevant with (e := e2) (v2 := v1) in l0; eauto using eq_sym.
linear_arithmetic.
rewrite eval_relevant with (v2 := v1) in l0; eauto using eq_sym.
rewrite eval_relevant with (e := e1) (v2 := v1) in l0; eauto using eq_sym.
linear_arithmetic.
Qed.
Local Hint Resolve beval_relevant : core.
Lemma evalPredicate_relevant : forall p v1 v2,
(forall x, varInPredicate p x -> v1 $! x = v2 $! x)
-> evalPredicate p v1 = evalPredicate p v2.
Proof.
induct p; simplify; f_equal; eauto.
apply functional_extensionality; simplify.
apply IHp; simplify.
cases (x1 ==v x); subst; simplify; auto.
Qed.
Local Hint Resolve evalPredicate_relevant : core.
Lemma eval_subPredicate : forall replaceThis withThis inThis,
(forall x, varInPredicate inThis x
-> varInExp withThis x
-> False)
-> forall v, evalPredicate (subPredicate inThis replaceThis withThis) v
= evalPredicate inThis (v $+ (replaceThis, eval withThis v)).
Proof.
induct inThis; simplify; auto.
rewrite IHinThis1; eauto.
rewrite IHinThis2; eauto.
rewrite IHinThis1; eauto.
rewrite IHinThis2; eauto.
cases (x ==v replaceThis); subst; simplify.
f_equal.
apply functional_extensionality; simplify.
f_equal.
maps_equal.
f_equal.
apply functional_extensionality; simplify.
replace (v $+ (replaceThis, eval withThis v) $+ (x, x0))
with (v $+ (x, x0) $+ (replaceThis, eval withThis v)) by maps_equal.
rewrite eval_relevant with (e := withThis) (v2 := v $+ (x, x0)).
apply IHinThis; eauto.
simplify.
assert (x1 <> x) by eauto.
simplify.
trivial.
Qed.
Lemma multibump_plus : forall n2 x n1,
multibumpVar n1 (multibumpVar n2 x) = multibumpVar (n1 + n2) x.
Proof.
induct n1; simplify; equality.
Qed.
Local Hint Rewrite multibump_plus.
Lemma spost_bumps : forall c nextVar p p' nextVar',
spost c nextVar p = (p', nextVar')
-> exists n, nextVar' = multibumpVar n nextVar.
Proof.
induct c; simplify; invert H.
exists 0; trivial.
exists 1; trivial.
specialize (IHc1 nextVar p).
cases (spost c1 nextVar p).
specialize (IHc1 _ _ eq_refl).
invert IHc1.
specialize (IHc2 _ _ _ _ H1).
invert IHc2.
eexists; simplify; eauto.
specialize (IHc1 nextVar (And p (Exp be))).
cases (spost c1 nextVar (And p (Exp be))).
specialize (IHc1 _ _ eq_refl).
invert IHc1.
specialize (IHc2 (multibumpVar x nextVar) (And p (NotExp be))).
cases (spost c2 (multibumpVar x nextVar) (And p (NotExp be))).
specialize (IHc2 _ _ eq_refl).
invert IHc2.
invert H1.
eexists; simplify; eauto.
Qed.
Lemma multibump_bump : forall x n,
multibumpVar n (bumpVar x) = bumpVar (multibumpVar n x).
Proof.
induct n; simplify; equality.
Qed.
Local Hint Rewrite multibump_bump.
Lemma length_bump : forall x,
String.length (bumpVar x) = S (String.length x).
Proof.
induct x; simplify; auto.
Qed.
Local Hint Rewrite length_bump.
Lemma length_multibump : forall n x,
String.length (multibumpVar n x) = n + String.length x.
Proof.
induct n; simplify; auto.
Qed.
Local Hint Rewrite length_multibump.
Lemma multibump_inj : forall x n m,
multibumpVar n x = multibumpVar m x
-> n = m.
Proof.
simplify.
apply (f_equal String.length) in H.
simplify.
linear_arithmetic.
Qed.
Lemma varIn_subExp_fwd : forall x e y e',
varInExp (subExp e' x e) y
-> varInExp e' y \/ varInExp e y.
Proof.
induct e'; simplify; propositional.
cases (x0 ==v x); simplify; subst; auto.
Qed.
Lemma varIn_subBexp_fwd : forall x e y be,
varInBexp (subBexp be x e) y
-> varInBexp be y \/ varInExp e y.
Proof.
induct be; simplify; propositional;
repeat match goal with
| [ H : varInExp _ _ |- _ ] => apply varIn_subExp_fwd in H
end; propositional.
Qed.
Lemma varIn_subPredicate_fwd : forall x e y p,
varInPredicate (subPredicate p x e) y
-> varInPredicate p y \/ varInExp e y.
Proof.
induct p; simplify; propositional;
repeat match goal with
| [ H : varInBexp _ _ |- _ ] => apply varIn_subBexp_fwd in H
end; propositional.
cases (x0 ==v x); simplify; subst; propositional.
Qed.
Lemma spost_avoids : forall c nextVar p p' nextVar',
spost c nextVar p = (p', nextVar')
-> (forall x n, varInCmd c x \/ varInPredicate p x
-> x <> multibumpVar n nextVar)
-> forall n, varInPredicate p' (multibumpVar n nextVar')
-> False.
Proof.
induct c; simplify; invert H; simplify.
specialize (H0 (multibumpVar n nextVar') n).
equality.
propositional.
apply multibump_inj with (n := 0) (m := S n) in H; equality.
apply varIn_subPredicate_fwd in H1.
change (bumpVar (multibumpVar n nextVar)) with (multibumpVar (S n) nextVar) in *.
propositional; eauto.
simplify.
apply multibump_inj with (n := 0) (m := S n) in H; equality.
change (bumpVar (multibumpVar n nextVar)) with (multibumpVar (S n) nextVar) in *; eauto.
apply varIn_subExp_fwd in H.
change (bumpVar (multibumpVar n nextVar)) with (multibumpVar (S n) nextVar) in *.
propositional; eauto.
simplify.
apply multibump_inj with (n := 0) (m := S n) in H1; equality.
specialize (IHc1 nextVar p).
cases (spost c1 nextVar p).
specialize (IHc1 _ _ eq_refl).
specialize (IHc2 _ _ _ _ H3).
apply spost_bumps in Heq; invert Heq.
apply spost_bumps in H3; invert H3.
apply IHc2 with (n := n); simplify; eauto.
propositional; subst; simplify; eauto.
apply IHc1 with (n := n0); simplify; eauto.
propositional; subst; simplify; eauto.
specialize (IHc1 nextVar (And p (Exp be))).
cases (spost c1 nextVar (And p (Exp be))).
specialize (IHc1 _ _ eq_refl).
specialize (IHc2 s (And p (NotExp be))).
cases (spost c2 s (And p (NotExp be))).
invert H3.
specialize (IHc2 _ _ eq_refl).
apply spost_bumps in Heq; invert Heq.
apply spost_bumps in Heq0; invert Heq0.
simplify; propositional.
apply IHc1 with (n := n + x0); simplify; subst; eauto.
propositional; eauto.
apply IHc2 with (n := n); simplify; subst; eauto.
2: rewrite Nat.add_assoc; auto.
propositional; eauto.
Qed.
(* The fundamental soundness property of strongest postcondition! We even
* manage to write it with just one little administrative condition on variable
* names. *)
Theorem spost_sound : forall v c v',
exec v c v'
-> forall nextVar p, evalPredicate p v
-> (forall x n, varInCmd c x \/ varInPredicate p x
-> x <> multibumpVar n nextVar)
(* In other words, none of the temporary variable names
* we might generate is in the command or
* precondition. *)
-> evalPredicate (fst (spost c nextVar p)) v'.
Proof.
induct 1; simplify; auto.
exists (v $! x); simplify; propositional.
rewrite eval_subPredicate; simplify; subst; eauto.
rewrite evalPredicate_relevant with (v2 := v); auto.
simplify.
cases (x0 ==v x); subst; simplify; auto.
cases (x0 ==v nextVar); subst; simplify; auto.
specialize (H0 nextVar 0); simplify; exfalso; eauto.
specialize (H0 x0 0); simplify; eauto.
assert (x <> nextVar) by (specialize (H0 x 0); eauto).
simplify.
match goal with
| [ |- context[eval ?E ?V1 ==n eval ?E ?V2] ] =>
rewrite eval_relevant with (v1 := V2) (v2 := V1);
cases (eval E V1 ==n eval E V1); try equality
end.
simplify.
cases (x0 ==v x); subst; simplify; auto.
cases (x0 ==v nextVar); subst; simplify; auto.
specialize (H0 nextVar 0); simplify; exfalso; eauto.
specialize (IHexec1 nextVar p).
cases (spost c1 nextVar p); simplify; propositional.
apply IHexec2.
apply H3; propositional; eauto.
propositional; subst; eauto.
apply spost_bumps in Heq; invert Heq; simplify; eauto.
eapply spost_avoids; eauto.
propositional; eauto.
specialize (IHexec nextVar (And p (Exp b))).
cases (spost c1 nextVar (And p (Exp b))).
cases (spost c2 s (And p (NotExp b))).
simplify.
left.
apply IHexec; propositional; subst; eauto.
cases (spost c1 nextVar (And p (Exp b))).
specialize (IHexec s (And p (NotExp b))).
cases (spost c2 s (And p (NotExp b))).
simplify.
right.
apply spost_bumps in Heq; invert Heq.
apply IHexec; propositional; subst; simplify; eauto; equality.
Qed.
(* OK, let's see our machinery in action on a few example programs. *)
Example ex1 := Seq (Assign "x" (Const 17)) (Assign "y" (Plus (Var "x") (Const 23))).
Compute fst (spost ex1 "X" Tru).
Example ex2 := Seq
(Seq (Assign "x" (Const 17)) (Assign "y" (Plus (Var "x") (Var "z"))))
(Assign "x" (Mult (Var "y") (Const 3))).
Compute fst (spost ex2 "X" Tru).
Example ex3 := If_ (Equal (Var "x") (Var "y"))
(Assign "z" (Mult (Var "x") (Const 2)))
(Assign "z" (Plus (Var "x") (Var "y"))).
Compute fst (spost ex3 "X" Tru).
(* These next two facts will be useful for automating proof of the side
* condition we included in the soundness theorem. *)
Lemma multibump_first' : forall x0 x n,
multibumpVar n (String x0 x) = String x0 (multibumpVar n x).
Proof.
induction n; simplify; auto.
rewrite IHn.
unfold bumpVar.
simplify.
auto.
Qed.
Lemma multibump_first : forall x0 x n y0 y,
String x0 x = multibumpVar n (String y0 y)
-> x0 <> y0
-> False.
Proof.
simplify.
rewrite multibump_first' in H.
equality.
Qed.
Local Hint Extern 1 False => eapply multibump_first; [ eassumption | equality ] : core.
Theorem ex1_correct : forall v v',
exec v ex1 v'
-> v' $! "y" = 40.
Proof.
simplify.
apply spost_sound with (nextVar := "X") (p := Tru) in H.
simplify.
unfold bumpVar in *; simplify.
first_order.
simplify.
repeat match goal with
| [ H : context[if ?E then _ else _] |- _ ] => cases E; try equality
end.
linear_arithmetic.
simplify.
trivial.
simplify.
propositional; subst; auto.
Qed.
Theorem ex2_correct : forall v v',
exec v ex2 v'
-> v' $! "y" = 17 + v $! "z".
Proof.
simplify.
apply spost_sound with (nextVar := "X") (p := Exp (Equal (Var "z") (Const (v $! "z")))) in H.
simplify.
unfold bumpVar in *; simplify.
first_order.
simplify.
repeat match goal with
| [ H : context[if ?E then _ else _] |- _ ] => cases E; try equality
end.
linear_arithmetic.
simplify.
repeat match goal with
| [ |- context[if ?E then _ else _] ] => cases E; equality
end.
simplify.
propositional; subst; auto.
Qed.
Theorem ex3_correct : forall v v',
exec v ex3 v'
-> v' $! "z" = v $! "x" + v $! "y".
Proof.
simplify.
apply spost_sound with (nextVar := "X")
(p := And (Exp (Equal (Var "x") (Const (v $! "x"))))
(Exp (Equal (Var "y") (Const (v $! "y"))))) in H.
simplify.
unfold bumpVar in *; simplify.
first_order; simplify;
repeat match goal with
| [ H : context[if ?E then _ else _] |- _ ] => cases E; try equality
end; linear_arithmetic.
simplify.
repeat match goal with
| [ |- context[if ?E then _ else _] ] => cases E; try equality
end.
simplify.
propositional; subst; auto.
Qed.
(* We did those proofs moderately manually, because the story isn't over yet.
* We still proved an important implication from the calculated strongest
* postcondition to the postcondition of interest. We want to automate that
* step, too. *)
(** * SHARED TOOL: A Simple First-Order Theorem Prover *)
(* Last chapter, we built a simple automated theorem prover that only
* understands equality on variables. We can wrap it to create an incomplete
* but sound prover for the predicate language we have just defined. *)
(* We will consider all of the conjunctions of *atoms* implied by a larger
* predicate. *)
Record atom := {
Condition : bexp;
Value : bool
}.
(* A *scenario* pairs atoms with existential quantifiers. *)
Record scenario := {
ExVars : list var;
Atoms : list atom
}.
(* Sometimes we want to combine two lists of scenarios into lists of larger
* scenarios, each formed by combining a scenario from each list. This function
* generalizes that process, in the style of Cartesian products. *)
Fixpoint cross {A} (f : A -> A -> A) (ls1 ls2 : list A) : list A :=
match ls1 with
| [] => []
| l1 :: ls1' => map (f l1) ls2 ++ cross f ls1' ls2
end.
(* Now it is not too bad to translate a predicate into a list of flat
* scenarios. *)
Fixpoint scenarios (p : predicate) : list scenario :=
match p with
| Tru => [{| ExVars := []; Atoms := [] |}]
| Fals => [] (* A contradictory situation has no possible scenarios! *)
| Exp e => [{| ExVars := []; Atoms := {| Condition := e; Value := true |} :: [] |}]
| NotExp e => [{| ExVars := []; Atoms := {| Condition := e; Value := false |} :: [] |}]
| And p1 p2 => cross (fun sc1 sc2 => {| ExVars := sc1.(ExVars) ++ sc2.(ExVars);
Atoms := sc1.(Atoms) ++ sc2.(Atoms) |})
(scenarios p1) (scenarios p2)
| Or p1 p2 => scenarios p1 ++ scenarios p2
| Ex x p1 => map (fun sc => {| ExVars := x :: sc.(ExVars);
Atoms := sc.(Atoms) |}) (scenarios p1)
end.
Compute scenarios (fst (spost ex1 "X" Tru)).
Compute scenarios (fst (spost ex2 "X" Tru)).
Compute scenarios (fst (spost ex3 "X" Tru)).
(* While we already defined how to compute which variables appear in a
* predicate, let's now distinguish the *bound* variables that occur as
* immediate arguments to quantifiers. *)
Fixpoint boundVars (p : predicate) : list var :=
match p with
| Tru | Fals | Exp _ | NotExp _ => []
| And p1 p2 | Or p1 p2 => boundVars p1 ++ boundVars p2
| Ex x p1 => x :: boundVars p1
end.
Local Hint Rewrite app_nil_r.
Lemma boundVars_subPredicate : forall x e p,
boundVars (subPredicate p x e) = boundVars p.
Proof.
induct p; simplify; try equality.
cases (x0 ==v x); simplify; equality.
Qed.
Local Hint Rewrite boundVars_subPredicate.
Lemma In_boundVars : forall x p,
In x (boundVars p)
-> varInPredicate p x.
Proof.
induct p; simplify; propositional.
apply in_app_or in H; propositional.
apply in_app_or in H; propositional.
Qed.
Local Hint Resolve In_boundVars : core.
Lemma multibump_zero : forall x,
x = multibumpVar 0 x.
Proof.
trivial.
Qed.
Local Hint Resolve multibump_zero : core.
(* We must characterize the lack of duplicate bound ("exists") variables, with
* the catch that duplicates between branches of "or"s are fine. (Abbreviation
* "wf" by convention stands for "well-formed.") *)
Fixpoint wfPredicate (p : predicate) : Prop :=
match p with
| Tru | Fals | Exp _ | NotExp _ => True
| And p1 p2 => wfPredicate p1 /\ wfPredicate p2
/\ (forall x, In x (boundVars p1) -> varInPredicate p2 x -> False)
/\ (forall x, In x (boundVars p2) -> varInPredicate p1 x -> False)
| Or p1 p2 => wfPredicate p1 /\ wfPredicate p2
| Ex x p1 => wfPredicate p1 /\ ~In x (boundVars p1)
end.
(* Now another sea of lemmas. Meet me at [Theorem]? *)
Lemma varIn_subExp : forall x e' x' e,
varInExp (subExp e x e') x'
-> varInExp e x' \/ varInExp e' x'.
Proof.
induct e; simplify; propositional.
cases (x0 ==v x); subst; simplify; auto.
Qed.
Lemma varIn_subBexp : forall x e' x' e,
varInBexp (subBexp e x e') x'
-> varInBexp e x' \/ varInExp e' x'.
Proof.
induct e; simplify; propositional;
repeat match goal with
| [ H : varInExp _ _ |- _ ] => apply varIn_subExp in H; propositional
end.
Qed.
Lemma sub_wfPredicate : forall x e p,
wfPredicate p
-> (forall y, varInExp e y -> varInPredicate p y -> False)
-> wfPredicate (subPredicate p x e).
Proof.
induct p; simplify; first_order.
apply varIn_subPredicate_fwd in H5; propositional; eauto.
apply varIn_subPredicate_fwd in H5; propositional; eauto.
cases (x0 ==v x); subst; simplify; auto.
first_order.
Qed.
Local Hint Resolve sub_wfPredicate : core.
Lemma spost_binds : forall x c nextVar p,
In x (boundVars (fst (spost c nextVar p)))
-> In x (boundVars p) \/ exists n, x = multibumpVar n nextVar.
Proof.
induct c; simplify; propositional; subst; eauto.
specialize (IHc1 nextVar p).
cases (spost c1 nextVar p); simplify.
specialize (IHc2 s p0).
cases (spost c2 s p0); simplify.
apply spost_bumps in Heq; invert Heq.
propositional.
invert H1.
right; exists (x1 + x0); simplify; trivial.
specialize (IHc1 nextVar (And p (Exp be))).
cases (spost c1 nextVar (And p (Exp be))).
specialize (IHc2 s (And p (NotExp be))).
cases (spost c2 s (And p (NotExp be))).
simplify.
apply in_app_or in H; first_order; subst.
apply spost_bumps in Heq; invert Heq.
right.
exists (x0 + x); simplify; trivial.
Qed.
Lemma spost_uses : forall x c nextVar p,
varInPredicate (fst (spost c nextVar p)) x
-> varInPredicate p x \/ varInCmd c x \/ exists n, x = multibumpVar n nextVar.
Proof.
induct c; simplify; propositional; subst; eauto.
apply varIn_subPredicate_fwd in H; simplify; propositional; subst; eauto.
apply varIn_subExp_fwd in H0; simplify; propositional; subst; eauto.
specialize (IHc1 nextVar p).
cases (spost c1 nextVar p); simplify.
specialize (IHc2 s p0).
cases (spost c2 s p0); simplify.
apply spost_bumps in Heq; invert Heq.
first_order.
simplify.
eauto.
specialize (IHc1 nextVar (And p (Exp be))).
cases (spost c1 nextVar (And p (Exp be))).
specialize (IHc2 s (And p (NotExp be))).
cases (spost c2 s (And p (NotExp be))).
simplify.
first_order.
apply spost_bumps in Heq; invert Heq.
simplify.
eauto.
Qed.
Lemma spost_wf : forall c nextVar p,
wfPredicate p
-> (forall x n, varInCmd c x \/ varInPredicate p x
-> x <> multibumpVar n nextVar)
-> (forall x, varInCmd c x -> In x (boundVars p) -> False)
-> wfPredicate (fst (spost c nextVar p)).
Proof.
induct c; simplify; propositional; eauto.
apply sub_wfPredicate; auto.
simplify; subst; eauto.
apply varIn_subExp in H4; simplify; propositional; subst; eauto.
specialize (IHc1 nextVar p).
cases (spost c1 nextVar p); simplify; propositional.
specialize (spost_bumps _ _ _ _ _ Heq); invert 1.
apply IHc2; propositional; subst; simplify; eauto.
first_order.
eapply spost_avoids; eauto.
propositional; eauto.
simplify.
eassumption.
specialize (spost_binds x0 c1 nextVar p).
rewrite Heq; simplify.
first_order.
specialize (IHc1 nextVar (And p (Exp be))).
cases (spost c1 nextVar (And p (Exp be))); simplify; propositional.
specialize (IHc2 s (And p (NotExp be))).
specialize (spost_bumps _ _ _ _ _ Heq); invert 1.
cases (spost c2 (multibumpVar x nextVar) (And p (NotExp be))); simplify; propositional.
apply H2; propositional; eauto.
apply H3; propositional; subst; simplify; eauto.
Qed.
Local Hint Resolve includes_refl : core.
Lemma Exists_cross : forall A (f : A -> A -> A) (P P1 P2 : A -> Prop) (ls1 ls2 : list A),
Exists P1 ls1
-> Exists P2 ls2
-> (forall l1 l2,
In l1 ls1
-> P1 l1
-> In l2 ls2
-> P2 l2
-> P (f l1 l2))
-> Exists P (cross f ls1 ls2).
Proof.
induct 1; simplify.
apply Exists_app.
left.
apply Exists_map.
apply Exists_exists in H0; invert H0; propositional.
apply Exists_exists; eauto.
apply Exists_app.
auto.
Qed.
Lemma Forall_cross : forall A (f : A -> A -> A) (P P1 P2 : A -> Prop) (ls1 ls2 : list A),
Forall P1 ls1
-> Forall P2 ls2
-> (forall l1 l2,
P1 l1
-> P2 l2
-> P (f l1 l2))
-> Forall P (cross f ls1 ls2).
Proof.
induct 1; simplify; eauto.
apply Forall_app; propositional.
apply Forall_map.
eapply Forall_impl; eauto.
Qed.
Local Hint Resolve in_or_app lookup_Some_dom lookup_None_dom : core.
Lemma includes_join : forall {A B} (m m1 m2 : fmap A B),
m $<= m1
-> m $<= m2
-> m $<= m1 $++ m2.
Proof.
simplify.
apply includes_intro; simplify.
rewrite lookup_join1.
eapply includes_lookup in H1; eassumption.
eauto.
Qed.
Local Hint Resolve includes_join : core.
Lemma scenarios_dom : forall p,
List.Forall (fun sc =>
List.Forall (fun a =>
forall x, varInBexp a.(Condition) x
-> varInPredicate p x) sc.(Atoms)) (scenarios p).
Proof.
induct p; simplify; eauto.
constructor; simplify; auto.
constructor; simplify; auto.
eapply Forall_cross; eauto; simplify.
eapply Forall_app; propositional; eapply Forall_impl; try eassumption; simplify; eauto.
apply Forall_app; propositional; eapply Forall_impl; try eassumption; simplify;
eapply Forall_impl; try eassumption; eauto.
apply Forall_map.
eapply Forall_impl; try eassumption; eauto.
simplify; eapply Forall_impl; try eassumption; eauto.
Qed.
Local Hint Unfold incl : core.
Local Hint Resolve incl_app_app : core.
Lemma scenarios_bound : forall p,
List.Forall (fun sc => incl sc.(ExVars) (boundVars p)) (scenarios p).
Proof.
induct p; simplify; eauto.
eapply Forall_cross; eauto; simplify; eauto.
apply Forall_app; unfold incl in *; propositional.
eapply Forall_impl; try eassumption; simplify; eauto.
eapply Forall_impl; try eassumption; simplify; eauto.
apply Forall_map; simplify.
eapply Forall_impl; try eassumption; simplify.
apply incl_cons; simplify; auto.
apply incl_tl; assumption.
Qed.
Lemma Forall_In : forall A (P : A -> Prop) x ls,
Forall P ls
-> In x ls
-> P x.
Proof.
induct 1; simplify; propositional; subst; auto.
Qed.
(* This next little bit was one of the most surprising detours in the proof. To
* be compatible with assumptions by the AutomatedTheoremProving library, we
* have to make sure valuations include values for all variables that predicates
* might mention; so we zero out any variables not yet included. *)
Fixpoint zeroAll (xs : list var) (v : valuation) : valuation :=