-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEquiv.v
More file actions
executable file
·1043 lines (911 loc) · 24.7 KB
/
Copy pathEquiv.v
File metadata and controls
executable file
·1043 lines (911 loc) · 24.7 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
Set Warnings "-notation-overridden,-parsing".
Require Import Maps.
From Coq Require Import Bool.Bool.
From Coq Require Import Arith.Arith.
From Coq Require Import Init.Nat.
From Coq Require Import Arith.PeanoNat. Import Nat.
From Coq Require Import Arith.EqNat.
From Coq Require Import omega.Omega.
From Coq Require Import Lists.List.
From Coq Require Import Logic.FunctionalExtensionality.
Import ListNotations.
Require Import Imp.
Definition aequiv (a1 a2 : aexp) : Prop :=
forall (st : state),
aeval st a1 = aeval st a2.
Definition bequiv (b1 b2 : bexp) : Prop :=
forall (st : state),
beval st b1 = beval st b2.
Theorem aequiv_example: aequiv (X - X) 0.
Proof.
intros st. simpl. omega.
Qed.
Theorem bequiv_example: bequiv (X - X = 0) true.
Proof.
intros st. unfold beval.
rewrite aequiv_example. reflexivity.
Qed.
Definition cequiv (c1 c2 : com) : Prop :=
forall (st st' : state),
(st =[ c1 ]=> st') <-> (st =[ c2 ]=> st').
Theorem skip_left : forall c,
cequiv
(SKIP;; c)
c.
Proof.
split.
- intros.
inversion H. subst.
inversion H2. subst.
assumption.
- intros.
apply E_Seq with st.
+ apply E_Skip.
+ assumption.
Qed.
Theorem skip_right : forall c,
cequiv
(c ;; SKIP)
c.
Proof.
split.
- intros.
inversion H. subst.
inversion H5. subst. assumption.
- intros.
apply E_Seq with st'.
+ assumption.
+ apply E_Skip.
Qed.
Theorem TEST_true_simple : forall c1 c2,
cequiv
(TEST true THEN c1 ELSE c2 FI)
c1.
Proof.
intros c1 c2.
split; intros H.
-
inversion H; subst. assumption. inversion H5.
-
apply E_IfTrue. reflexivity. assumption. Qed.
Theorem TEST_true: forall b c1 c2,
bequiv b BTrue ->
cequiv
(TEST b THEN c1 ELSE c2 FI)
c1.
Proof.
intros b c1 c2 Hb.
split; intros H.
-
inversion H; subst.
+
assumption.
+
unfold bequiv in Hb. simpl in Hb.
rewrite Hb in H5.
inversion H5.
-
apply E_IfTrue; try assumption.
unfold bequiv in Hb. simpl in Hb.
rewrite Hb. reflexivity. Qed.
Theorem TEST_false : forall b c1 c2,
bequiv b BFalse ->
cequiv
(TEST b THEN c1 ELSE c2 FI)
c2.
Proof.
split; intros.
- inversion H0; subst.
+ unfold bequiv in H. simpl in H. rewrite H in H6. discriminate H6.
+ apply H7.
- unfold bequiv in H.
apply E_IfFalse.
+ simpl in H. apply H.
+ apply H0.
Qed.
Search negb.
Theorem swap_if_branches : forall b e1 e2,
cequiv
(TEST b THEN e1 ELSE e2 FI)
(TEST BNot b THEN e2 ELSE e1 FI).
Proof.
split; intros.
- destruct (beval st b) eqn: E.
+ apply E_IfFalse.
* simpl. rewrite E. reflexivity.
* inversion H. subst.
{ apply H6. }
{ rewrite E in H5. discriminate H5. }
+ apply E_IfTrue.
* simpl. rewrite E. reflexivity.
* inversion H. subst.
{ rewrite E in H5. discriminate H5. }
{ apply H6. }
- destruct (beval st b) eqn: E.
+ apply E_IfTrue.
* apply E.
* inversion H; subst.
{ rewrite <- E in H5. simpl in H5. apply no_fixpoint_negb in H5. inversion H5. }
{ apply H6. }
+ apply E_IfFalse.
* apply E.
* inversion H; subst.
{ apply H6. }
{ rewrite <- E in H5. simpl in H5. apply no_fixpoint_negb in H5. inversion H5. }
Qed.
Theorem WHILE_false : forall b c,
bequiv b BFalse ->
cequiv
(WHILE b DO c END)
SKIP.
Proof.
intros b c Hb. split; intros H.
-
inversion H; subst.
+
apply E_Skip.
+
rewrite Hb in H2. inversion H2.
-
inversion H; subst.
apply E_WhileFalse.
rewrite Hb.
reflexivity. Qed.
Lemma WHILE_true_nonterm : forall b c st st',
bequiv b BTrue ->
~( st =[ WHILE b DO c END ]=> st' ).
Proof.
intros b c st st' Hb.
intros H.
remember (WHILE b DO c END)%imp as cw eqn:Heqcw.
induction H;
inversion Heqcw; subst; clear Heqcw.
-
unfold bequiv in Hb.
rewrite Hb in H. inversion H.
-
apply IHceval2. reflexivity. Qed.
Theorem WHILE_true : forall b c,
bequiv b true ->
cequiv
(WHILE b DO c END)
(WHILE true DO SKIP END).
Proof.
split; intros.
- apply WHILE_true_nonterm in H0.
+ inversion H0.
+ apply H.
- apply WHILE_true_nonterm in H0.
+ inversion H0.
+ unfold bequiv. intros. reflexivity.
Qed.
Theorem loop_unrolling : forall b c,
cequiv
(WHILE b DO c END)
(TEST b THEN (c ;; WHILE b DO c END) ELSE SKIP FI).
Proof.
intros b c st st'.
split; intros Hce.
-
inversion Hce; subst.
+
apply E_IfFalse. assumption. apply E_Skip.
+
apply E_IfTrue. assumption.
apply E_Seq with (st' := st'0). assumption. assumption.
-
inversion Hce; subst.
+
inversion H5; subst.
apply E_WhileTrue with (st' := st'0).
assumption. assumption. assumption.
+
inversion H5; subst. apply E_WhileFalse. assumption. Qed.
Theorem seq_assoc : forall c1 c2 c3,
cequiv ((c1;;c2);;c3) (c1;;(c2;;c3)).
Proof.
split; intros.
- inversion H. subst.
inversion H2. subst.
apply (E_Seq c1 (c2;;c3) st st'1 st' H3).
+ apply (E_Seq c2 c3 st'1 st'0 st' H7 H5).
- inversion H. subst.
inversion H5. subst.
specialize (E_Seq c1 c2 st st'0 st'1 H2 H3) as H'.
apply (E_Seq (c1;;c2) c3 st st'1 st' H' H7).
Qed.
Theorem identity_assignment : forall x,
cequiv
(x ::= x)
SKIP.
Proof.
intros.
split; intro H; inversion H; subst.
-
rewrite t_update_same.
apply E_Skip.
-
assert (Hx : st' =[ x ::= x ]=> (x !-> st' x ; st')).
{ apply E_Ass. reflexivity. }
rewrite t_update_same in Hx.
apply Hx.
Qed.
Check E_Ass.
Theorem assign_aequiv : forall (x : string) e,
aequiv x e ->
cequiv SKIP (x ::= e).
Proof.
unfold aequiv. unfold cequiv.
split.
- intros.
inversion H0. subst.
assert (H1: st' = t_update st' x (st' x)).
+ apply functional_extensionality.
intros.
rewrite t_update_same. reflexivity.
+ rewrite H1 in |- * at 2.
apply E_Ass.
rewrite <- H.
reflexivity.
- intros.
inversion H0. subst.
assert (H1: st = t_update st x (aeval st e)).
+ rewrite <- H in |- * at 1.
apply functional_extensionality.
intros.
rewrite t_update_same. reflexivity.
+ rewrite <- H1.
apply E_Skip.
Qed.
Definition prog_a : com :=
(WHILE ~(X <= 0) DO
X ::= X + 1
END)%imp.
Definition prog_b : com :=
(TEST X = 0 THEN
X ::= X + 1;;
Y ::= 1
ELSE
Y ::= 0
FI;;
X ::= X - Y;;
Y ::= 0)%imp.
Definition prog_c : com :=
SKIP%imp.
Definition prog_d : com :=
(WHILE ~(X = 0) DO
X ::= (X * Y) + 1
END)%imp.
Definition prog_e : com :=
(Y ::= 0)%imp.
Definition prog_f : com :=
(Y ::= X + 1;;
WHILE ~(X = Y) DO
Y ::= X + 1
END)%imp.
Definition prog_g : com :=
(WHILE true DO
SKIP
END)%imp.
Definition prog_h : com :=
(WHILE ~(X = X) DO
X ::= X + 1
END)%imp.
Definition prog_i : com :=
(WHILE ~(X = Y) DO
X ::= Y + 1
END)%imp.
Definition equiv_classes : list (list com)
. Admitted.
Definition manual_grade_for_equiv_classes : option (nat*string) := None.
Lemma refl_aequiv : forall (a : aexp), aequiv a a.
Proof.
intros a st. reflexivity. Qed.
Lemma sym_aequiv : forall (a1 a2 : aexp),
aequiv a1 a2 -> aequiv a2 a1.
Proof.
intros a1 a2 H. intros st. symmetry. apply H. Qed.
Lemma trans_aequiv : forall (a1 a2 a3 : aexp),
aequiv a1 a2 -> aequiv a2 a3 -> aequiv a1 a3.
Proof.
unfold aequiv. intros a1 a2 a3 H12 H23 st.
rewrite (H12 st). rewrite (H23 st). reflexivity. Qed.
Lemma refl_bequiv : forall (b : bexp), bequiv b b.
Proof.
unfold bequiv. intros b st. reflexivity. Qed.
Lemma sym_bequiv : forall (b1 b2 : bexp),
bequiv b1 b2 -> bequiv b2 b1.
Proof.
unfold bequiv. intros b1 b2 H. intros st. symmetry. apply H. Qed.
Lemma trans_bequiv : forall (b1 b2 b3 : bexp),
bequiv b1 b2 -> bequiv b2 b3 -> bequiv b1 b3.
Proof.
unfold bequiv. intros b1 b2 b3 H12 H23 st.
rewrite (H12 st). rewrite (H23 st). reflexivity. Qed.
Lemma refl_cequiv : forall (c : com), cequiv c c.
Proof.
unfold cequiv. intros c st st'. apply iff_refl. Qed.
Lemma sym_cequiv : forall (c1 c2 : com),
cequiv c1 c2 -> cequiv c2 c1.
Proof.
unfold cequiv. intros c1 c2 H st st'.
assert (st =[ c1 ]=> st' <-> st =[ c2 ]=> st') as H'.
{ apply H. }
apply iff_sym. assumption.
Qed.
Lemma iff_trans : forall (P1 P2 P3 : Prop),
(P1 <-> P2) -> (P2 <-> P3) -> (P1 <-> P3).
Proof.
intros P1 P2 P3 H12 H23.
inversion H12. inversion H23.
split; intros A.
apply H1. apply H. apply A.
apply H0. apply H2. apply A. Qed.
Lemma trans_cequiv : forall (c1 c2 c3 : com),
cequiv c1 c2 -> cequiv c2 c3 -> cequiv c1 c3.
Proof.
unfold cequiv. intros c1 c2 c3 H12 H23 st st'.
apply iff_trans with (st =[ c2 ]=> st'). apply H12. apply H23. Qed.
Theorem CAss_congruence : forall x a1 a1',
aequiv a1 a1' ->
cequiv (CAss x a1) (CAss x a1').
Proof.
intros x a1 a2 Heqv st st'.
split; intros Hceval.
-
inversion Hceval. subst. apply E_Ass.
rewrite Heqv. reflexivity.
-
inversion Hceval. subst. apply E_Ass.
rewrite Heqv. reflexivity. Qed.
Theorem CWhile_congruence : forall b1 b1' c1 c1',
bequiv b1 b1' -> cequiv c1 c1' ->
cequiv (WHILE b1 DO c1 END) (WHILE b1' DO c1' END).
Proof.
unfold bequiv,cequiv.
intros b1 b1' c1 c1' Hb1e Hc1e st st'.
split; intros Hce.
-
remember (WHILE b1 DO c1 END)%imp as cwhile
eqn:Heqcwhile.
induction Hce; inversion Heqcwhile; subst.
+
apply E_WhileFalse. rewrite <- Hb1e. apply H.
+
apply E_WhileTrue with (st' := st').
* rewrite <- Hb1e. apply H.
*
apply (Hc1e st st'). apply Hce1.
*
apply IHHce2. reflexivity.
-
remember (WHILE b1' DO c1' END)%imp as c'while
eqn:Heqc'while.
induction Hce; inversion Heqc'while; subst.
+
apply E_WhileFalse. rewrite -> Hb1e. apply H.
+
apply E_WhileTrue with (st' := st').
* rewrite -> Hb1e. apply H.
*
apply (Hc1e st st'). apply Hce1.
*
apply IHHce2. reflexivity. Qed.
Theorem CSeq_congruence : forall c1 c1' c2 c2',
cequiv c1 c1' -> cequiv c2 c2' ->
cequiv (c1;;c2) (c1';;c2').
Proof.
split; intros;
inversion H1; subst.
- apply E_Seq with st'0.
+ apply H. apply H4.
+ apply H0. apply H7.
- apply E_Seq with st'0.
+ apply H. apply H4.
+ apply H0. apply H7.
Qed.
Theorem CIf_congruence : forall b b' c1 c1' c2 c2',
bequiv b b' -> cequiv c1 c1' -> cequiv c2 c2' ->
cequiv (TEST b THEN c1 ELSE c2 FI)
(TEST b' THEN c1' ELSE c2' FI).
Proof.
split; intros.
- inversion H2; subst.
+ apply E_IfTrue.
* rewrite (H st) in H8. apply H8.
* rewrite (H0 st) in H9. apply H9.
+ apply E_IfFalse.
* rewrite (H st) in H8. apply H8.
* rewrite (H1 st) in H9. apply H9.
- inversion H2; subst.
+ apply E_IfTrue.
* rewrite <- (H st) in H8. apply H8.
* rewrite <- (H0 st) in H9. apply H9.
+ apply E_IfFalse.
* rewrite <- (H st) in H8. apply H8.
* rewrite <- (H1 st) in H9. apply H9.
Qed.
Example congruence_example:
cequiv
(X ::= 0;;
TEST X = 0
THEN
Y ::= 0
ELSE
Y ::= 42
FI)
(X ::= 0;;
TEST X = 0
THEN
Y ::= X - X
ELSE
Y ::= 42
FI).
Proof.
apply CSeq_congruence.
- apply refl_cequiv.
- apply CIf_congruence.
+ apply refl_bequiv.
+ apply CAss_congruence. unfold aequiv. simpl.
* symmetry. apply minus_diag.
+ apply refl_cequiv.
Qed.
Definition atrans_sound (atrans : aexp -> aexp) : Prop :=
forall (a : aexp),
aequiv a (atrans a).
Definition btrans_sound (btrans : bexp -> bexp) : Prop :=
forall (b : bexp),
bequiv b (btrans b).
Definition ctrans_sound (ctrans : com -> com) : Prop :=
forall (c : com),
cequiv c (ctrans c).
Fixpoint fold_constants_aexp (a : aexp) : aexp :=
match a with
| ANum n => ANum n
| AId x => AId x
| APlus a1 a2 =>
match (fold_constants_aexp a1,
fold_constants_aexp a2)
with
| (ANum n1, ANum n2) => ANum (n1 + n2)
| (a1', a2') => APlus a1' a2'
end
| AMinus a1 a2 =>
match (fold_constants_aexp a1,
fold_constants_aexp a2)
with
| (ANum n1, ANum n2) => ANum (n1 - n2)
| (a1', a2') => AMinus a1' a2'
end
| AMult a1 a2 =>
match (fold_constants_aexp a1,
fold_constants_aexp a2)
with
| (ANum n1, ANum n2) => ANum (n1 * n2)
| (a1', a2') => AMult a1' a2'
end
end.
Example fold_aexp_ex1 :
fold_constants_aexp ((1 + 2) * X)
= (3 * X)%imp.
Proof. reflexivity. Qed.
Compute fold_constants_aexp (1 + 2).
Example fold_aexp_ex2 :
fold_constants_aexp (X - ((0 * 6) + Y))%imp = (X - (0 + Y))%imp.
Proof. reflexivity. Qed.
Fixpoint fold_constants_bexp (b : bexp) : bexp :=
match b with
| BTrue => BTrue
| BFalse => BFalse
| BEq a1 a2 =>
match (fold_constants_aexp a1,
fold_constants_aexp a2) with
| (ANum n1, ANum n2) =>
if n1 =? n2 then BTrue else BFalse
| (a1', a2') =>
BEq a1' a2'
end
| BLe a1 a2 =>
match (fold_constants_aexp a1,
fold_constants_aexp a2) with
| (ANum n1, ANum n2) =>
if n1 <=? n2 then BTrue else BFalse
| (a1', a2') =>
BLe a1' a2'
end
| BNot b1 =>
match (fold_constants_bexp b1) with
| BTrue => BFalse
| BFalse => BTrue
| b1' => BNot b1'
end
| BAnd b1 b2 =>
match (fold_constants_bexp b1,
fold_constants_bexp b2) with
| (BTrue, BTrue) => BTrue
| (BTrue, BFalse) => BFalse
| (BFalse, BTrue) => BFalse
| (BFalse, BFalse) => BFalse
| (b1', b2') => BAnd b1' b2'
end
end.
Example fold_bexp_ex1 :
fold_constants_bexp (true && ~(false && true))%imp
= true.
Proof. reflexivity. Qed.
Example fold_bexp_ex2 :
fold_constants_bexp ((X = Y) && (0 = (2 - (1 + 1))))%imp
= ((X = Y) && true)%imp.
Proof. reflexivity. Qed.
Open Scope imp.
Fixpoint fold_constants_com (c : com) : com :=
match c with
| SKIP =>
SKIP
| x ::= a =>
x ::= (fold_constants_aexp a)
| c1 ;; c2 =>
(fold_constants_com c1) ;; (fold_constants_com c2)
| TEST b THEN c1 ELSE c2 FI =>
match fold_constants_bexp b with
| BTrue => fold_constants_com c1
| BFalse => fold_constants_com c2
| b' => TEST b' THEN fold_constants_com c1
ELSE fold_constants_com c2 FI
end
| WHILE b DO c END =>
match fold_constants_bexp b with
| BTrue => WHILE BTrue DO SKIP END
| BFalse => SKIP
| b' => WHILE b' DO (fold_constants_com c) END
end
end.
Close Scope imp.
Example fold_com_ex1 :
fold_constants_com
(X ::= 4 + 5;;
Y ::= X - 3;;
TEST (X - Y) = (2 + 4) THEN SKIP
ELSE Y ::= 0 FI;;
TEST 0 <= (4 - (2 + 1)) THEN Y ::= 0
ELSE SKIP FI;;
WHILE Y = 0 DO
X ::= X + 1
END)%imp
=
(X ::= 9;;
Y ::= X - 3;;
TEST (X - Y) = 6 THEN SKIP
ELSE Y ::= 0 FI;;
Y ::= 0;;
WHILE Y = 0 DO
X ::= X + 1
END)%imp.
Proof. reflexivity. Qed.
Theorem fold_constants_aexp_sound :
atrans_sound fold_constants_aexp.
Proof.
unfold atrans_sound. intros a. unfold aequiv. intros st.
induction a; simpl;
try reflexivity;
try (destruct (fold_constants_aexp a1);
destruct (fold_constants_aexp a2);
rewrite IHa1; rewrite IHa2; reflexivity). Qed.
Theorem fold_constants_bexp_sound:
btrans_sound fold_constants_bexp.
Proof.
unfold btrans_sound. intros b. unfold bequiv. intros st.
induction b;
try reflexivity.
-
simpl.
remember (fold_constants_aexp a1) as a1' eqn:Heqa1'.
remember (fold_constants_aexp a2) as a2' eqn:Heqa2'.
replace (aeval st a1) with (aeval st a1') by
(subst a1'; rewrite <- fold_constants_aexp_sound; reflexivity).
replace (aeval st a2) with (aeval st a2') by
(subst a2'; rewrite <- fold_constants_aexp_sound; reflexivity).
destruct a1'; destruct a2'; try reflexivity.
simpl. destruct (n =? n0); reflexivity.
- simpl.
remember (fold_constants_aexp a1) as a1' eqn: Hlea1'.
remember (fold_constants_aexp a2) as a2' eqn: Hlea2'.
replace (aeval st a1) with (aeval st a1').
replace (aeval st a2) with (aeval st a2').
destruct a1'; destruct a2'; try reflexivity.
* simpl. destruct (n <=? n0); reflexivity.
* subst. rewrite <- fold_constants_aexp_sound. reflexivity.
* subst. rewrite <- fold_constants_aexp_sound. reflexivity.
-
simpl. remember (fold_constants_bexp b) as b' eqn:Heqb'.
rewrite IHb.
destruct b'; reflexivity.
-
simpl.
remember (fold_constants_bexp b1) as b1' eqn:Heqb1'.
remember (fold_constants_bexp b2) as b2' eqn:Heqb2'.
rewrite IHb1. rewrite IHb2.
destruct b1'; destruct b2'; reflexivity.
Qed.
Theorem fold_constants_com_sound :
ctrans_sound fold_constants_com.
Proof.
unfold ctrans_sound. intros c.
induction c; simpl.
- apply refl_cequiv.
- apply CAss_congruence.
apply fold_constants_aexp_sound.
- apply CSeq_congruence; assumption.
-
assert (bequiv b (fold_constants_bexp b)). {
apply fold_constants_bexp_sound. }
destruct (fold_constants_bexp b) eqn:Heqb;
try (apply CIf_congruence; assumption).
+
apply trans_cequiv with c1; try assumption.
apply TEST_true; assumption.
+
apply trans_cequiv with c2; try assumption.
apply TEST_false; assumption.
- remember (fold_constants_bexp b).
assert (bequiv b b0).
+ rewrite Heqb0. apply fold_constants_bexp_sound.
+ destruct b0; try (apply CWhile_congruence; assumption; assumption).
* apply WHILE_true. assumption.
* apply WHILE_false. assumption.
Qed.
Fixpoint subst_aexp (x : string) (u : aexp) (a : aexp) : aexp :=
match a with
| ANum n =>
ANum n
| AId x' =>
if eqb_string x x' then u else AId x'
| APlus a1 a2 =>
APlus (subst_aexp x u a1) (subst_aexp x u a2)
| AMinus a1 a2 =>
AMinus (subst_aexp x u a1) (subst_aexp x u a2)
| AMult a1 a2 =>
AMult (subst_aexp x u a1) (subst_aexp x u a2)
end.
Example subst_aexp_ex :
subst_aexp X (42 + 53) (Y + X)%imp
= (Y + (42 + 53))%imp.
Proof. reflexivity. Qed.
Definition subst_equiv_property := forall x1 x2 a1 a2,
cequiv (x1 ::= a1;; x2 ::= a2)
(x1 ::= a1;; x2 ::= subst_aexp x1 a1 a2).
Theorem subst_inequiv :
~ subst_equiv_property.
Proof.
unfold subst_equiv_property.
intros Contra.
remember (X ::= X + 1;;
Y ::= X)%imp
as c1.
remember (X ::= X + 1;;
Y ::= X + 1)%imp
as c2.
assert (cequiv c1 c2) by (subst; apply Contra).
remember (Y !-> 1 ; X !-> 1) as st1.
remember (Y !-> 2 ; X !-> 1) as st2.
assert (H1 : empty_st =[ c1 ]=> st1);
assert (H2 : empty_st =[ c2 ]=> st2);
try (subst;
apply E_Seq with (st' := (X !-> 1));
apply E_Ass; reflexivity).
apply H in H1.
assert (Hcontra : st1 = st2)
by (apply (ceval_deterministic c2 empty_st); assumption).
assert (Hcontra' : st1 Y = st2 Y)
by (rewrite Hcontra; reflexivity).
subst. inversion Hcontra'. Qed.
Inductive var_not_used_in_aexp (x : string) : aexp -> Prop :=
| VNUNum : forall n, var_not_used_in_aexp x (ANum n)
| VNUId : forall y, x <> y -> var_not_used_in_aexp x (AId y)
| VNUPlus : forall a1 a2,
var_not_used_in_aexp x a1 ->
var_not_used_in_aexp x a2 ->
var_not_used_in_aexp x (APlus a1 a2)
| VNUMinus : forall a1 a2,
var_not_used_in_aexp x a1 ->
var_not_used_in_aexp x a2 ->
var_not_used_in_aexp x (AMinus a1 a2)
| VNUMult : forall a1 a2,
var_not_used_in_aexp x a1 ->
var_not_used_in_aexp x a2 ->
var_not_used_in_aexp x (AMult a1 a2).
Lemma aeval_weakening : forall x st a ni,
var_not_used_in_aexp x a ->
aeval (x !-> ni ; st) a = aeval st a.
Proof.
intros.
induction H.
- simpl. reflexivity.
- simpl. apply t_update_neq. apply H.
- simpl.
rewrite IHvar_not_used_in_aexp1.
rewrite IHvar_not_used_in_aexp2.
reflexivity.
- simpl.
rewrite IHvar_not_used_in_aexp1.
rewrite IHvar_not_used_in_aexp2.
reflexivity.
- simpl.
rewrite IHvar_not_used_in_aexp1.
rewrite IHvar_not_used_in_aexp2.
reflexivity.
Qed.
Theorem inequiv_exercise:
~ cequiv (WHILE true DO SKIP END) SKIP.
Proof.
Admitted.
Module Himp.
Inductive com : Type :=
| CSkip : com
| CAss : string -> aexp -> com
| CSeq : com -> com -> com
| CIf : bexp -> com -> com -> com
| CWhile : bexp -> com -> com
| CHavoc : string -> com.
Notation "'SKIP'" :=
CSkip : imp_scope.
Notation "X '::=' a" :=
(CAss X a) (at level 60) : imp_scope.
Notation "c1 ;; c2" :=
(CSeq c1 c2) (at level 80, right associativity) : imp_scope.
Notation "'WHILE' b 'DO' c 'END'" :=
(CWhile b c) (at level 80, right associativity) : imp_scope.
Notation "'TEST' e1 'THEN' e2 'ELSE' e3 'FI'" :=
(CIf e1 e2 e3) (at level 80, right associativity) : imp_scope.
Notation "'HAVOC' l" :=
(CHavoc l) (at level 60) : imp_scope.
Reserved Notation "st '=[' c ']=>' st'" (at level 40).
Open Scope imp_scope.
Inductive ceval : com -> state -> state -> Prop :=
| E_Skip : forall st,
st =[ SKIP ]=> st
| E_Ass : forall st a1 n x,
aeval st a1 = n ->
st =[ x ::= a1 ]=> (x !-> n ; st)
| E_Seq : forall c1 c2 st st' st'',
st =[ c1 ]=> st' ->
st' =[ c2 ]=> st'' ->
st =[ c1 ;; c2 ]=> st''
| E_IfTrue : forall st st' b c1 c2,
beval st b = true ->
st =[ c1 ]=> st' ->
st =[ TEST b THEN c1 ELSE c2 FI ]=> st'
| E_IfFalse : forall st st' b c1 c2,
beval st b = false ->
st =[ c2 ]=> st' ->
st =[ TEST b THEN c1 ELSE c2 FI ]=> st'
| E_WhileFalse : forall b st c,
beval st b = false ->
st =[ WHILE b DO c END ]=> st
| E_WhileTrue : forall st st' st'' b c,
beval st b = true ->
st =[ c ]=> st' ->
st' =[ WHILE b DO c END ]=> st'' ->
st =[ WHILE b DO c END ]=> st''
| E_Havoc: forall x st v,
st =[ CHavoc x ]=> (x !-> v; st)
where "st =[ c ]=> st'" := (ceval c st st').
Close Scope imp_scope.
Example havoc_example1 : empty_st =[ (HAVOC X)%imp ]=> (X !-> 0).
Proof.
apply E_Havoc.
Qed.
Example havoc_example2 :
empty_st =[ (SKIP;; HAVOC Z)%imp ]=> (Z !-> 42).
Proof.
apply E_Seq with (empty_st).
- apply E_Skip.
- apply E_Havoc.
Qed.
Definition manual_grade_for_Check_rule_for_HAVOC : option (nat*string) := None.
Definition cequiv (c1 c2 : com) : Prop := forall st st' : state,
st =[ c1 ]=> st' <-> st =[ c2 ]=> st'.
Definition pXY :=
(HAVOC X;; HAVOC Y)%imp.
Definition pYX :=
(HAVOC Y;; HAVOC X)%imp.
Theorem pXY_cequiv_pYX :
cequiv pXY pYX \/ ~cequiv pXY pYX.
Proof.
left.
unfold cequiv.
intros.
split.
- intros.
inversion H. subst.
inversion H2. subst.
inversion H5. subst.
apply E_Seq with (Y !-> v0; st).
+ apply E_Havoc.
+ rewrite t_update_permute.
* apply E_Havoc.
* unfold not.
intros.
discriminate.
- intros.
inversion H. subst.
inversion H2. subst.
inversion H5. subst.
apply E_Seq with (X !-> v0; st).
+ apply E_Havoc.
+ rewrite t_update_permute.
* apply E_Havoc.
* unfold not. intros. discriminate.
Qed.
Definition ptwice :=
(HAVOC X;; HAVOC Y)%imp.
Definition pcopy :=
(HAVOC X;; Y ::= X)%imp.
Theorem ptwice_cequiv_pcopy :
cequiv ptwice pcopy \/ ~cequiv ptwice pcopy.
Proof. Admitted.
Definition p1 : com :=
(WHILE ~ (X = 0) DO
HAVOC Y;;
X ::= X + 1
END)%imp.
Definition p2 : com :=
(WHILE ~ (X = 0) DO
SKIP
END)%imp.
Lemma p1_may_diverge : forall st st', st X <> 0 ->
~ st =[ p1 ]=> st'.
Proof.
Admitted.
Lemma p2_may_diverge : forall st st', st X <> 0 ->
~ st =[ p2 ]=> st'.
Proof.
Admitted.
Theorem p1_p2_equiv : cequiv p1 p2.
Proof. Admitted.
Definition p3 : com :=
(Z ::= 1;;
WHILE ~(X = 0) DO
HAVOC X;;
HAVOC Z
END)%imp.
Definition p4 : com :=
(X ::= 0;;
Z ::= 1)%imp.
Theorem p3_p4_inequiv : ~ cequiv p3 p4.
Proof. Admitted.
Definition p5 : com :=
(WHILE ~(X = 1) DO
HAVOC X