-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEquiv.v
More file actions
2477 lines (2093 loc) · 82.7 KB
/
Equiv.v
File metadata and controls
2477 lines (2093 loc) · 82.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
(** * Equiv: Program Equivalence *)
Set Warnings "-notation-overridden,-parsing,-deprecated-hint-without-locality".
From PLF Require Import Maps.
From Coq Require Import Bool.
From Coq Require Import Arith.
From Coq Require Import Init.Nat.
From Coq Require Import PeanoNat. Import Nat.
From Coq Require Import EqNat.
From Coq Require Import Lia.
From Coq Require Import List. Import ListNotations.
From Coq Require Import FunctionalExtensionality.
From PLF Require Export Imp.
Set Default Goal Selector "!".
(** *** Before You Get Started:
- Create a fresh directory for this volume. (Do not try to mix the
files from this volume with files from _Logical Foundations_ in
the same directory: the result will not make you happy.) You
can either start with an empty directory and populate it with
the files listed below, or else download the whole PLF zip file
and unzip it.
- The new directory should contain at least the following files:
- [Imp.v] (make sure it is the one from the PLF distribution,
not the one from LF: they are slightly different);
- [Maps.v] (ditto)
- [Equiv.v] (this file)
- [_CoqProject], containing the following line:
-Q . PLF
- If you see errors like this...
Compiled library PLF.Maps (in file
/Users/.../plf/Maps.vo) makes inconsistent assumptions
over library Coq.Init.Logic
... it may mean something went wrong with the above steps.
Doing "[make clean]" (or manually removing everything except
[.v] and [_CoqProject] files) may help.
- If you are using VSCode with the VSCoq extension, you'll then
want to open a new window in VSCode, click [Open Folder > plf],
and run [make]. If you get an error like "Cannot find a
physical path..." error, it may be because you didn't open plf
directly (you instead opened a folder containing both lf and
plf, for example). *)
(** *** Advice for Working on Exercises:
- Most of the Coq proofs we ask you to do in this chapter are
similar to proofs that we've provided. Before starting to work
on exercises, take the time to work through our proofs (both
informally and in Coq) and make sure you understand them in
detail. This will save you a lot of time.
- The Coq proofs we're doing now are sufficiently complicated that
it is more or less impossible to complete them by random
experimentation or following your nose. You need to start with
an idea about why the property is true and how the proof is
going to go. The best way to do this is to write out at least a
sketch of an informal proof on paper -- one that intuitively
convinces you of the truth of the theorem -- before starting to
work on the formal one. Alternately, grab a friend and try to
convince them that the theorem is true; then try to formalize
your explanation.
- Use automation to save work! The proofs in this chapter can get
pretty long if you try to write out all the cases explicitly. *)
(* ################################################################# *)
(** * Behavioral Equivalence *)
(** In an earlier chapter, we investigated the correctness of a very
simple program transformation: the [optimize_0plus] function. The
programming language we were considering was the first version of
the language of arithmetic expressions -- with no variables -- so
in that setting it was very easy to define what it means for a
program transformation to be correct: it should always yield a
program that evaluates to the same number as the original.
To talk about the correctness of program transformations for the
full Imp language -- in particular, assignment -- we need to
consider the role of mutable state and develop a more
sophisticated notion of correctness, which we'll call _behavioral
equivalence_. *)
(** For example:
- [X + 2] is behaviorally equivalent to [1 + X + 1]
- [X - X] is behaviorally equivalent to [0]
(* TODO *) - [(X - 1) + 1] is _not_ behaviorally equivalent to [X] *)
(* ================================================================= *)
(** ** Definitions *)
(** For [aexp]s and [bexp]s with variables, the definition we want is
clear: Two [aexp]s or [bexp]s are "behaviorally equivalent" if
they evaluate to the same result in every state. *)
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.
(** Here are some simple examples of equivalences of arithmetic
and boolean expressions. *)
Theorem aequiv_example:
aequiv
<{ X - X }>
<{ 0 }>.
Proof.
intros st. simpl. lia.
Qed.
Theorem bequiv_example:
bequiv
<{ X - X = 0 }>
<{ true }>.
Proof.
intros st. unfold beval.
rewrite aequiv_example. reflexivity.
Qed.
(** For commands, the situation is a little more subtle. We
can't simply say "two commands are behaviorally equivalent if they
evaluate to the same ending state whenever they are started in the
same initial state," because some commands, when run in some
starting states, don't terminate in any final state at all!
What we need instead is this: two commands are behaviorally
equivalent if, for any given starting state, they either (1) both
diverge or (2) both terminate in the same final state. A compact
way to express this is "if the first one terminates in a
particular state then so does the second, and vice versa." *)
Definition cequiv (c1 c2 : com) : Prop :=
forall (st st' : state),
(st =[ c1 ]=> st') <-> (st =[ c2 ]=> st').
(** We can also define an asymmetric variant of this relation: We say
that [c1] _refines_ [c2] if they produce the same final states
_when [c1] terminates_ (but [c1] may not terminate in some cases
where [c2] does). *)
Definition refines (c1 c2 : com) : Prop :=
forall (st st' : state),
(st =[ c1 ]=> st') -> (st =[ c2 ]=> st').
(* ================================================================= *)
(** ** Simple Examples *)
(** For examples of command equivalence, let's start by looking at
a trivial program equivalence involving [skip]: *)
Theorem skip_left : forall c,
cequiv
<{ skip; c }>
c.
Proof.
(* WORKED IN CLASS *)
intros c st st'.
split; intros H.
- (* -> *)
inversion H. subst.
inversion H2. subst.
assumption.
- (* <- *)
apply E_Seq with st.
+ apply E_Skip.
+ assumption.
Qed.
(** **** Exercise: 2 stars, standard (skip_right)
Prove that adding a [skip] after a command results in an
equivalent program *)
Theorem skip_right : forall c,
cequiv
<{ c ; skip }>
c.
Proof.
intros c st st'. split; intros H.
- inversion H. subst. inversion H5. subst. assumption.
- apply E_Seq with st'.
* assumption.
* constructor.
Qed.
(** [] *)
(** Similarly, here is a simple equivalence that optimizes [if]
commands: *)
Theorem if_true_simple : forall c1 c2,
cequiv
<{ if true then c1 else c2 end }>
c1.
Proof.
intros c1 c2.
split; intros H.
- (* -> *)
inversion H; subst.
+ assumption.
+ discriminate.
- (* <- *)
apply E_IfTrue.
+ reflexivity.
+ assumption. Qed.
(** Of course, no (human) programmer would write a conditional whose
condition is literally [true]. But they might write one whose
condition is _equivalent_ to true:
_Theorem_: If [b] is equivalent to [true], then [if b then c1
else c2 end] is equivalent to [c1].
_Proof_:
- ([->]) We must show, for all [st] and [st'], that if [st =[
if b then c1 else c2 end ]=> st'] then [st =[ c1 ]=> st'].
Proceed by cases on the rules that could possibly have been
used to show [st =[ if b then c1 else c2 end ]=> st'], namely
[E_IfTrue] and [E_IfFalse].
- Suppose the final rule in the derivation of [st =[ if b
then c1 else c2 end ]=> st'] was [E_IfTrue]. We then have,
by the premises of [E_IfTrue], that [st =[ c1 ]=> st'].
This is exactly what we set out to prove.
- On the other hand, suppose the final rule in the derivation
of [st =[ if b then c1 else c2 end ]=> st'] was [E_IfFalse].
We then know that [beval st b = false] and [st =[ c2 ]=> st'].
Recall that [b] is equivalent to [true], i.e., forall [st],
[beval st b = beval st <{true}> ]. In particular, this means
that [beval st b = true], since [beval st <{true}> = true]. But
this is a contradiction, since [E_IfFalse] requires that
[beval st b = false]. Thus, the final rule could not have
been [E_IfFalse].
- ([<-]) We must show, for all [st] and [st'], that if
[st =[ c1 ]=> st'] then
[st =[ if b then c1 else c2 end ]=> st'].
Since [b] is equivalent to [true], we know that [beval st b] =
[beval st <{true}> ] = [true]. Together with the assumption that
[st =[ c1 ]=> st'], we can apply [E_IfTrue] to derive
[st =[ if b then c1 else c2 end ]=> st']. []
Here is the formal version of this proof: *)
Theorem if_true: forall b c1 c2,
bequiv b <{true}> ->
cequiv
<{ if b then c1 else c2 end }>
c1.
Proof.
intros b c1 c2 Hb.
split; intros H.
- (* -> *)
inversion H; subst.
+ (* b evaluates to true *)
assumption.
+ (* b evaluates to false (contradiction) *)
unfold bequiv in Hb. simpl in Hb.
rewrite Hb in H5.
discriminate.
- (* <- *)
apply E_IfTrue; try assumption.
unfold bequiv in Hb. simpl in Hb.
apply Hb. Qed.
(** **** Exercise: 2 stars, standard, especially useful (if_false) *)
Theorem if_false : forall b c1 c2,
bequiv b <{false}> ->
cequiv
<{ if b then c1 else c2 end }>
c2.
Proof.
intros b c1 c2 Hb. split; intros H.
- inversion H; subst.
* unfold bequiv in Hb. simpl in Hb. rewrite Hb in H5. discriminate H5.
* assumption.
- apply E_IfFalse.
* unfold bequiv in Hb. simpl in Hb. apply Hb.
* assumption.
Qed.
(** [] *)
(** **** Exercise: 3 stars, standard (swap_if_branches)
Show that we can swap the branches of an [if] if we also negate its
condition. *)
Theorem swap_if_branches : forall b c1 c2,
cequiv
<{ if b then c1 else c2 end }>
<{ if ~ b then c2 else c1 end }>.
Proof.
intros b c1 c2. split; intros H.
- inversion H; subst;
[apply E_IfFalse | apply E_IfTrue];
try assumption;
simpl; rewrite H5; reflexivity.
- inversion H; subst; [apply E_IfFalse | apply E_IfTrue]; try assumption;
simpl in H5; [apply negb_true_iff | apply negb_false_iff]; assumption.
Qed.
(** [] *)
(** For [while] loops, we can give a similar pair of theorems. A loop
whose guard is equivalent to [false] is equivalent to [skip],
while a loop whose guard is equivalent to [true] is equivalent to
[while true do skip end] (or any other non-terminating program). *)
(** The first of these facts is easy. *)
Theorem while_false : forall b c,
bequiv b <{false}> ->
cequiv
<{ while b do c end }>
<{ skip }>.
Proof.
intros b c Hb. split; intros H.
- (* -> *)
inversion H; subst.
+ (* E_WhileFalse *)
apply E_Skip.
+ (* E_WhileTrue *)
rewrite Hb in H2. discriminate.
- (* <- *)
inversion H. subst.
apply E_WhileFalse.
apply Hb.
Qed.
(** **** Exercise: 2 stars, advanced, optional (while_false_informal)
Write an informal proof of [while_false].
(* FILL IN HERE *)
_Theorem_: If [b] is equivalent to [false] then [while b do c end] is equivalent to [skip].
_Proof_:
We wish to show that for any starting state [st] and some ending state [st'],
we have that [st =[ while b do c end ]=> st'] and [st =[ skip ]=> st'].
Note that if [b] is equivalent to [false] then we have that [b] evaluates to [false].
We proceed by cases on the rules that could have possibly been used to show
[st =[ while b do c end ]=> st']. Namely, [E_WhileFalse] and [E_WhileTrue].
- Suppose [E_WhileFalse] is the final inference rule in the derivation of [st =[ while b do c end ]=> st'].
We have that by the premises of [E_WhileFalse] we have that [st =[ while b do c end ]=> st].
I.e., [st = st'] by [ceval_deterministic]. Furthermore, by definition [st =[ skip ]=> st] and so,
We have that in the same starting state, both commands end in the same final state which is what we wished to prove.
- Now suppose that [E_WhileTrue] is the final inference rule in the derivation.
We have by the premises of [E_WhileTrue] that [b] evaluates to [true]. However, by assumption [b] evaluates to [false] so this yields a contradiction.
Therefore, [E_WhileTrue] could not be the final inference rule.
Therefore if [b] is equivalent to [false] then [while b do c end] is equivalent to [skip].
*)
(** [] *)
(** To prove the second fact, we need an auxiliary lemma stating that
[while] loops whose guards are equivalent to [true] never
terminate. *)
(** _Lemma_: If [b] is equivalent to [true], then it cannot be
the case that [st =[ while b do c end ]=> st'].
_Proof_: Suppose that [st =[ while b do c end ]=> st']. We show,
by induction on a derivation of [st =[ while b do c end ]=> st'],
that this assumption leads to a contradiction. The only two cases
to consider are [E_WhileFalse] and [E_WhileTrue], the others
are contradictory.
- Suppose [st =[ while b do c end ]=> st'] is proved using rule
[E_WhileFalse]. Then by assumption [beval st b = false]. But
this contradicts the assumption that [b] is equivalent to
[true].
- Suppose [st =[ while b do c end ]=> st'] is proved using rule
[E_WhileTrue]. We must have that:
1. [beval st b = true],
2. there is some [st0] such that [st =[ c ]=> st0] and
[st0 =[ while b do c end ]=> st'],
3. and we are given the induction hypothesis that
[st0 =[ while b do c end ]=> st'] leads to a contradiction,
We obtain a contradiction by 2 and 3. [] *)
Lemma while_true_nonterm : forall b c st st',
bequiv b <{true}> ->
~( st =[ while b do c end ]=> st' ).
Proof.
(* WORKED IN CLASS *)
intros b c st st' Hb.
intros H.
remember <{ while b do c end }> as cw eqn:Heqcw.
induction H;
(* Most rules don't apply; we rule them out by inversion: *)
inversion Heqcw; subst; clear Heqcw.
(* The two interesting cases are the ones for while loops: *)
- (* E_WhileFalse *) (* contradictory -- b is always true! *)
unfold bequiv in Hb.
(* [rewrite] is able to instantiate the quantifier in [st] *)
rewrite Hb in H. discriminate.
- (* E_WhileTrue *) (* immediate from the IH *)
apply IHceval2. reflexivity.
Qed.
(** **** Exercise: 2 stars, standard, optional (while_true_nonterm_informal)
Explain what the lemma [while_true_nonterm] means in English.
(* FILL IN HERE *)
The lemma means that if the condition for the while loop is always true, then the loop won't terminate.
Specifically, it won't evaluate to a final state because it will continue forever without emitting a final state.
*)
(** [] *)
(** **** Exercise: 2 stars, standard, especially useful (while_true)
Prove the following theorem. _Hint_: You'll want to use
[while_true_nonterm] here. *)
Theorem while_true : forall b c,
bequiv b <{true}> ->
cequiv
<{ while b do c end }>
<{ while true do skip end }>.
Proof.
intros b c Hb. split; intros H.
- apply (while_true_nonterm b c st st') in Hb. contradiction.
- assert ((bequiv BTrue <{ true }>)) as HTrue. { unfold bequiv. auto. }
apply (while_true_nonterm BTrue <{ skip }> st st') in HTrue.
apply HTrue in H.
contradiction.
Qed.
(** [] *)
(** A more interesting fact about [while] commands is that any number
of copies of the body can be "unrolled" without changing meaning.
Loop unrolling is an important transformation in any real
compiler: its correctness is of more than academic interest! *)
Theorem loop_unrolling : forall b c,
cequiv
<{ while b do c end }>
<{ if b then c ; while b do c end else skip end }>.
Proof.
(* WORKED IN CLASS *)
intros b c st st'.
split; intros Hce.
- (* -> *)
inversion Hce; subst.
+ (* loop doesn't run *)
apply E_IfFalse.
* assumption.
* apply E_Skip.
+ (* loop runs *)
apply E_IfTrue.
* assumption.
* apply E_Seq with (st' := st'0).
-- assumption.
-- assumption.
- (* <- *)
inversion Hce; subst.
+ (* loop runs *)
inversion H5; subst.
apply E_WhileTrue with (st' := st'0).
* assumption.
* assumption.
* assumption.
+ (* loop doesn't run *)
inversion H5; subst. apply E_WhileFalse. assumption. Qed.
(** **** Exercise: 2 stars, standard, optional (seq_assoc) *)
Theorem seq_assoc : forall c1 c2 c3,
cequiv <{(c1;c2);c3}> <{c1;(c2;c3)}>.
Proof.
intros c1 c2 c3 st st'. split; intros H; inversion H; subst.
- inversion H2. subst.
apply E_Seq with st'1; try assumption.
apply E_Seq with st'0; try assumption.
- inversion H5. subst.
apply E_Seq with st'1.
* apply E_Seq with st'0; assumption.
* assumption.
Qed.
(** [] *)
(** Proving program properties involving assignments is one place
where the fact that program states are treated extensionally
(e.g., [x !-> m x ; m] and [m] are equal maps) comes in handy. *)
Theorem identity_assignment : forall x,
cequiv
<{ x := x }>
<{ skip }>.
Proof.
intros.
split; intro H; inversion H; subst; clear H.
- (* -> *)
rewrite t_update_same.
apply E_Skip.
- (* <- *)
assert (Hx : st' =[ x := x ]=> (x !-> st' x ; st')).
{ apply E_Asgn. reflexivity. }
rewrite t_update_same in Hx.
apply Hx.
Qed.
(** **** Exercise: 2 stars, standard, especially useful (assign_aequiv) *)
Theorem assign_aequiv : forall (X : string) (a : aexp),
aequiv <{ X }> a ->
cequiv <{ skip }> <{ X := a }>.
Proof.
intros X a H st st'.
split; intros H2; inversion H2; subst;
unfold aequiv in H; simpl in H.
- assert (Heval : st' =[ X := a ]=> (X !-> st' X; st')).
{ apply E_Asgn. symmetry. apply H. }
rewrite t_update_same in Heval.
exact Heval.
- rewrite <- (H st). rewrite t_update_same. apply E_Skip.
Qed.
(** [] *)
(** **** Exercise: 2 stars, standard (equiv_classes) *)
(** Given the following programs, group together those that are
equivalent in Imp. Your answer should be given as a list of lists,
where each sub-list represents a group of equivalent programs. For
example, if you think programs (a) through (h) are all equivalent
to each other, but not to (i), your answer should look like this:
[ [prog_a;prog_b;prog_c;prog_d;prog_e;prog_f;prog_g;prog_h] ;
[prog_i] ]
Write down your answer below in the definition of
[equiv_classes]. *)
Definition prog_a : com :=
<{ while X > 0 do
X := X + 1
end }>.
Definition prog_b : com :=
<{ if (X = 0) then
X := X + 1;
Y := 1
else
Y := 0
end;
X := X - Y;
Y := 0 }>.
Definition prog_c : com :=
<{ skip }> .
Definition prog_d : com :=
<{ while X <> 0 do
X := (X * Y) + 1
end }>.
Definition prog_e : com :=
<{ Y := 0 }>.
Definition prog_f : com :=
<{ Y := X + 1;
while X <> Y do
Y := X + 1
end }>.
Definition prog_g : com :=
<{ while true do
skip
end }>.
Definition prog_h : com :=
<{ while X <> X do
X := X + 1
end }>.
Definition prog_i : com :=
<{ while X <> Y do
X := Y + 1
end }>.
Ltac map_equal_assert := apply functional_extensionality; intros x; destruct (string_dec x Y);
[subst; repeat rewrite t_update_eq; reflexivity
| destruct (string_dec x X);
[subst; rewrite t_update_neq; auto | repeat (rewrite t_update_neq; auto)]
].
Theorem equiv_b_e : cequiv prog_b prog_e.
Proof.
unfold prog_b. unfold prog_e.
intros st. split; intros H.
- inversion H. subst. destruct (st X) eqn:HstX.
* inversion H2; subst.
+ eassert (st =[ X := X + 1; Y := 1 ]=> _).
{ eapply E_Seq; eapply E_Asgn; simpl; [rewrite HstX | ..]; simpl; eexists. }
apply (ceval_deterministic _ _ _ _ H8) in H0. subst.
eassert ((Y !-> 1; X !-> 1; st) =[ X := X - Y; Y := 0 ]=> _).
{ eapply E_Seq; eapply E_Asgn; simpl; eexists. }
apply (ceval_deterministic _ _ _ _ H5) in H0. subst.
assert ((Y !-> 0; X !-> 0; Y !-> 1; X !-> 1; st) = (Y !-> 0; st)).
{ map_equal_assert. }
rewrite H0. constructor. reflexivity.
+ simpl in H7. rewrite HstX in H7. discriminate H7.
* inversion H2; subst.
+ simpl in H7. rewrite HstX in H7. discriminate H7.
+ inversion H8. subst. simpl in H5.
eassert ((Y !-> 0; st) =[ X := X - Y; Y := 0 ]=> _).
{ eapply E_Seq; eapply E_Asgn; simpl.
{ rewrite t_update_neq; [.. | discriminate]. rewrite t_update_eq. rewrite sub_0_r. eexists. }
{ eexists. }
}
apply (ceval_deterministic _ _ _ _ H5) in H0. rewrite H0.
assert ((Y !-> 0; X !-> st X; Y !-> 0; st) = (Y !-> 0; st)).
{ map_equal_assert. }
rewrite H1. constructor. reflexivity.
- inversion H. subst. simpl. destruct (st X) eqn:HstX; eapply E_Seq.
* apply E_IfTrue; simpl.
+ rewrite HstX. reflexivity.
+ eapply E_Seq; apply E_Asgn; simpl; [rewrite HstX | ..]; simpl; eexists.
* eapply E_Seq.
+ eapply E_Asgn. simpl. eexists.
+ assert ((Y !-> 0; X !-> 0; Y !-> 1; X !-> 1; st) = (Y !-> 0; st)).
{ map_equal_assert. }
rewrite <- H0. constructor. reflexivity.
* apply E_IfFalse; simpl.
+ rewrite HstX. reflexivity.
+ simpl in H. apply H.
* eapply E_Seq.
+ eapply E_Asgn. simpl. eexists.
+ rewrite t_update_eq. rewrite t_update_neq; [.. | discriminate]. rewrite sub_0_r.
assert ((Y !-> 0; X !-> st X; Y !-> 0; st) = (Y !-> 0; st)).
{ map_equal_assert. }
rewrite <- H0 at 2. constructor. reflexivity.
Qed.
Theorem equiv_c_h : cequiv prog_c prog_h.
Proof.
unfold prog_c. unfold prog_h.
intros st st'. split; intros H; inversion H; subst.
- apply E_WhileFalse. simpl.
rewrite eqb_refl. reflexivity.
- apply E_Skip.
- simpl in H2. rewrite eqb_refl in H2. discriminate H2.
Qed.
Ltac zero_case := match goal with
| Hstx : (?st ?X) = 0 |- _ =>
match goal with
| Hb : beval ?st ?b = true |- _ => simpl in Hb; rewrite Hstx in Hb; simpl in Hb; discriminate Hb
| _ => apply E_WhileFalse; simpl; rewrite Hstx; auto
end
| Hb1 : beval ?st ?b = true, Hb2 : beval ?st ?b = false |- _ => rewrite Hb1 in Hb2; discriminate Hb2
end.
Lemma inc_equiv_a : forall (st st' : state),
(exists n, st X = S n) -> st =[ while X > 0 do X := X + 1 end ]=> st' -> st =[ while X + 1 > 0 do X := X + 1 end ]=> st'.
Proof.
intros st st' Hst H.
remember <{ while X > 0 do X := X + 1 end }> as cw eqn:Heqcw.
induction H; inversion Heqcw; subst; clear Heqcw.
- simpl in H. destruct Hst as [n Hst]. rewrite Hst in H. discriminate H.
- apply E_WhileTrue with st'.
* simpl. destruct Hst as [n Hst]. rewrite Hst. reflexivity.
* assumption.
* apply IHceval2.
+ inversion H0; subst. simpl. destruct Hst as [n0 Hst].
rewrite t_update_eq. exists (S n0). rewrite add_1_r. rewrite Hst.
reflexivity.
+ reflexivity.
Qed.
Lemma inc_equiv_b : forall (st st' : state),
(exists n, st X = S n) -> st =[ while X <> 0 do X := X * Y + 1 end ]=> st' -> st =[ while X * Y + 1 > 0 do X := X * Y + 1 end ]=> st'.
Proof.
intros st st' Hst H.
remember <{ while X <> 0 do X := X * Y + 1 end }> as cw eqn:Heqcw.
induction H; inversion Heqcw; subst; clear Heqcw.
- simpl in H. destruct Hst as [n Hst]. rewrite Hst in H. discriminate H.
- apply E_WhileTrue with st'.
* simpl. destruct Hst as [n Hst]. rewrite Hst.
destruct (st Y); [ rewrite mul_0_r | ..]; reflexivity.
* assumption.
* apply IHceval2.
+ inversion H0; subst. simpl. destruct Hst as [n0 Hst].
rewrite t_update_eq. exists ((st X) * st Y). rewrite add_1_r.
reflexivity.
+ reflexivity.
Qed.
Theorem equiv_a_d : cequiv prog_a prog_d.
Proof.
unfold prog_a. unfold prog_d.
intros st st'. split; intros H.
- destruct (st X) eqn:Hstx.
* inversion H; subst; zero_case.
* assert (exists n', st X = S n') as Hexst. { exists n. assumption. }
apply (inc_equiv_a st st') in Hexst; auto.
assert (bequiv <{ X + 1 > 0 }> <{ true }>) as Htrue.
{ unfold bequiv. intros st0. simpl. destruct (st0 X); reflexivity. }
apply (while_true_nonterm _ <{ X := X + 1 }> st st') in Htrue.
apply Htrue in Hexst. contradiction Hexst.
- destruct (st X) eqn:Hstx.
* inversion H; subst; zero_case.
* assert (exists n', st X = S n') as Hexst. { exists n. assumption. }
apply (inc_equiv_b st st') in Hexst; auto.
assert (bequiv <{ X * Y + 1 > 0 }> <{ true }>) as Htrue.
{ unfold bequiv. intros st0. simpl. destruct (st0 X); destruct (st0 Y); try rewrite mul_0_r; auto. }
apply (while_true_nonterm _ <{ X := X * Y + 1 }> st st') in Htrue.
apply Htrue in Hexst. contradiction Hexst.
Qed.
Lemma prog_f_collapsed : forall st st',
X <> Y ->
st =[ Y := X + 1; while X <> Y do Y := X + 1 end ]=> st' ->
st =[ while X <> X + 1 do Y := X + 1 end ]=> st'.
Proof.
intros st st' Hxy H.
inversion H. subst.
remember <{ while X <> Y do Y := X + 1 end }> as cw eqn:Heqcw.
induction H5; inversion Heqcw; subst; clear Heqcw.
- inversion H2; subst. simpl in H0. rewrite t_update_eq in H0.
rewrite t_update_neq in H0; auto.
apply negb_false_iff in H0. apply eqb_eq in H0.
rewrite add_1_r in H0. apply neq_succ_diag_r in H0.
destruct H0.
- apply IHceval2.
* reflexivity.
* assumption.
* inversion H2. subst. simpl in H2. inversion H5_. subst.
simpl. rewrite t_update_neq; auto. rewrite t_update_shadow.
assumption.
Qed.
Theorem equiv_f_g : cequiv prog_f prog_g.
Proof.
unfold prog_f. unfold prog_g.
intros st st'. split; intros H.
- apply prog_f_collapsed in H; [.. | discriminate].
assert (bequiv <{ X <> X + 1 }> <{ true }>) as Htrue.
{ intros st0. simpl. rewrite negb_true_iff. rewrite add_1_r.
rewrite eqb_neq. apply neq_succ_diag_r. }
apply (while_true_nonterm _ <{ Y := X + 1 }> st st') in Htrue.
contradiction.
- assert (bequiv <{ true }> <{ true }>) as Htrue.
{ intros st0. reflexivity. }
apply (while_true_nonterm _ <{ skip }> st st') in Htrue.
contradiction.
Qed.
Theorem not_equiv_c_e : ~(cequiv prog_c prog_e).
Proof.
unfold prog_c. unfold prog_e. unfold cequiv.
intros Heq. specialize Heq with (Y !-> 3) (Y !-> 0; Y !-> 3).
destruct Heq.
assert ((Y !-> 3) =[ Y := 0 ]=> (Y !-> 0; Y !-> 3)) as He.
{ constructor. reflexivity. }
apply H0 in He. inversion He.
assert (~(forall x, (Y !-> 3) x = (Y !-> 0; Y !-> 3) x)) as Hneq.
{ intros contra. specialize contra with Y. repeat rewrite t_update_eq in contra.
inversion contra.
}
apply Hneq. apply equal_f. apply H1.
Qed.
Theorem not_equiv_f_i : ~(cequiv prog_f prog_i).
Proof.
unfold prog_f. unfold prog_i. unfold cequiv.
intros Heq. specialize Heq with (X !-> 0; Y !-> 0) (X !-> 0; Y !-> 0).
destruct Heq.
assert ((X !-> 0; Y !-> 0) =[ while X <> Y do X := Y + 1 end ]=> (X !-> 0; Y !-> 0)) as Hwhile.
{ apply E_WhileFalse. reflexivity. }
apply H0 in Hwhile.
inversion Hwhile. subst. inversion H3. subst.
simpl in H6.
destruct (string_dec X Y).
- rewrite e in H6. inversion H6; subst.
* assert (~(forall x, (Y !-> 1; Y !-> 0; Y !-> 0) x = (Y !-> 0; Y !-> 0) x)) as Hneq.
{ intros contra. specialize contra with Y. repeat rewrite t_update_eq in contra.
inversion contra. }
apply Hneq. apply equal_f. apply H7.
* simpl in H4. discriminate H4.
- apply (prog_f_collapsed _ _ n) in Hwhile.
assert (bequiv <{ X <> X + 1}> <{ true }>) as Hb.
{ intros st0. simpl. apply negb_true_iff. rewrite add_1_r. rewrite eqb_neq. apply neq_succ_diag_r. }
apply (while_true_nonterm _ <{ Y := X + 1 }> (X !-> 0; Y !-> 0) (X !-> 0; Y !-> 0)) in Hb.
contradiction.
Qed.
Theorem not_equiv_a_c : ~(cequiv prog_a prog_c).
Proof.
unfold prog_a. unfold prog_c. unfold cequiv.
intros Heq. specialize Heq with (st := (X !-> 1)).
edestruct Heq.
eassert ((X !-> 1) =[ skip ]=> _). { apply E_Skip. }
apply H0 in H1.
assert (exists n, (X !-> 1) X = S n). { exists 0. auto. }
apply (inc_equiv_a _ _ H2) in H1.
assert (bequiv <{ X + 1 > 0 }> <{ true }>).
{ intros st0. simpl. destruct (st0 X); reflexivity. }
eapply while_true_nonterm in H3.
apply H3 in H1. auto.
Qed.
Definition equiv_classes : list (list com) :=
[
[prog_a; prog_d];
[prog_b; prog_e];
[prog_c; prog_h];
[prog_f; prog_g];
[prog_i]
].
(* Do not modify the following line: *)
Definition manual_grade_for_equiv_classes : option (nat*string) := None.
(** [] *)
(* ################################################################# *)
(** * Properties of Behavioral Equivalence *)
(** We next consider some fundamental properties of program
equivalence. *)
(* ================================================================= *)
(** ** Behavioral Equivalence Is an Equivalence *)
(** First, let's verify that the equivalences on [aexps], [bexps], and
[com]s really are _equivalences_ -- i.e., that they are reflexive,
symmetric, and transitive. The proofs are all easy. *)
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'. reflexivity. Qed.
Lemma sym_cequiv : forall (c1 c2 : com),
cequiv c1 c2 -> cequiv c2 c1.
Proof.
unfold cequiv. intros c1 c2 H st st'.
rewrite H. reflexivity.
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'.
rewrite H12. apply H23.
Qed.
(* ================================================================= *)
(** ** Behavioral Equivalence Is a Congruence *)
(** Less obviously, behavioral equivalence is also a _congruence_.
That is, the equivalence of two subprograms implies the
equivalence of the larger programs in which they are embedded:
aequiv a a'
-------------------------
cequiv (x := a) (x := a')
cequiv c1 c1'
cequiv c2 c2'
--------------------------
cequiv (c1;c2) (c1';c2')
... and so on for the other forms of commands. *)
(** (Note that we are using the inference rule notation here not
as part of an inductive definition, but simply to write down some
valid implications in a readable format. We prove these
implications below.) *)
(** We will see a concrete example of why these congruence
properties are important in the following section (in the proof of
[fold_constants_com_sound]), but the main idea is that they allow
us to replace a small part of a large program with an equivalent
small part and know that the whole large programs are equivalent
_without_ doing an explicit proof about the parts that didn't
change -- i.e., the "proof burden" of a small change to a large
program is proportional to the size of the change, not the
program! *)
Theorem CAsgn_congruence : forall x a a',
aequiv a a' ->
cequiv <{x := a}> <{x := a'}>.
Proof.
intros x a a' Heqv st st'.
split; intros Hceval.
- (* -> *)
inversion Hceval. subst. apply E_Asgn.
rewrite Heqv. reflexivity.
- (* <- *)
inversion Hceval. subst. apply E_Asgn.
rewrite Heqv. reflexivity. Qed.
(** The congruence property for loops is a little more interesting,
since it requires induction.
_Theorem_: Equivalence is a congruence for [while] -- that is, if
[b] is equivalent to [b'] and [c] is equivalent to [c'], then
[while b do c end] is equivalent to [while b' do c' end].
_Proof_: Suppose [b] is equivalent to [b'] and [c] is
equivalent to [c']. We must show, for every [st] and [st'], that
[st =[ while b do c end ]=> st'] iff [st =[ while b' do c'
end ]=> st']. We consider the two directions separately.
- ([->]) We show that [st =[ while b do c end ]=> st'] implies
[st =[ while b' do c' end ]=> st'], by induction on a
derivation of [st =[ while b do c end ]=> st']. The only
nontrivial cases are when the final rule in the derivation is
[E_WhileFalse] or [E_WhileTrue].
- [E_WhileFalse]: In this case, the form of the rule gives us
[beval st b = false] and [st = st']. But then, since
[b] and [b'] are equivalent, we have [beval st b' =
false], and [E_WhileFalse] applies, giving us
[st =[ while b' do c' end ]=> st'], as required.
- [E_WhileTrue]: The form of the rule now gives us [beval st
b = true], with [st =[ c ]=> st'0] and [st'0 =[ while
b do c end ]=> st'] for some state [st'0], with the
induction hypothesis [st'0 =[ while b' do c' end ]=>
st'].
Since [c] and [c'] are equivalent, we know that [st =[
c' ]=> st'0]. And since [b] and [b'] are equivalent,
we have [beval st b' = true]. Now [E_WhileTrue] applies,
giving us [st =[ while b' do c' end ]=> st'], as
required.
- ([<-]) Similar. [] *)
Theorem CWhile_congruence : forall b b' c c',
bequiv b b' -> cequiv c c' ->
cequiv <{ while b do c end }> <{ while b' do c' end }>.
Proof.
(* WORKED IN CLASS *)
(* We will prove one direction in an "assert"
in order to reuse it for the converse. *)
assert (A: forall (b b' : bexp) (c c' : com) (st st' : state),
bequiv b b' -> cequiv c c' ->
st =[ while b do c end ]=> st' ->
st =[ while b' do c' end ]=> st').
{ unfold bequiv,cequiv.