-
Notifications
You must be signed in to change notification settings - Fork 0
/
Equiv.thy
2266 lines (1988 loc) · 74 KB
/
Equiv.thy
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
theory Equiv
imports Main Imp begin
(* (** * Equiv: Program Equivalence *)
(* Version of 4/29/2010 *)
Require Export Imp.
(** *** Some general advice for the homework
- We've tried to make sure that most of the Coq proofs we ask
you to do are similar to proofs that we've provided. Before
starting to work on the homework problems, take the time to
work through our proofs (both informally, on paper, and in
Coq) and make sure you understand them in detail. This will
save you a lot of time.
- Here's another thing that will save a lot of time. The Coq
proofs we're doing now are sufficiently complicated that it is
more or less impossible to complete them simply by "following
your nose" or random hacking. 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.
- Use automation to save work! Some of the proofs in this
chapter's exercises are pretty long if you try to write out
all the cases explicitly. *)
(* ####################################################### *)
(** * Behavioral equivalence *)
(** In the last 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
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 in the
full Imp language, we need to think about the role of variables
and the state. *)
(* ####################################################### *)
(** ** Definitions *)
(** For [aexp]s and [bexp]s, the definition we want is clear. We say
that 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.
*)
definition aequiv :: "aexp' \<Rightarrow> aexp' \<Rightarrow> bool" where
"aequiv a a' \<equiv> \<forall> st. aeval' st a = aeval' st a'"
definition bequiv :: "bexp' \<Rightarrow> bexp' \<Rightarrow> bool" where
"bequiv b b' \<equiv> \<forall> st. beval' st b = beval' st b'"
(*
(** 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 run in the
same initial state," because some commands (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 both diverge or both
terminate in the same final state. *)
Definition cequiv (c1 c2 : com) : Prop :=
forall (st st':state),
(c1 / st ==> st') <-> (c2 / st ==> st').
*)
definition cequiv :: "com \<Rightarrow> com \<Rightarrow> bool" where
"cequiv c c' \<equiv> \<forall> st st'. ceval c st st' \<longleftrightarrow> ceval c' st st'"
ML {* val setup_tac = (unfold_tac [@{thm cequiv_def},
@{thm aequiv_def},
@{thm bequiv_def}]
@{simpset}) *}
(*
(* ####################################################### *)
(** ** Examples *)
Theorem aequiv_example:
aequiv (AMinus (AId X) (AId X)) (ANum 0).
Proof.
unfold aequiv. intros st. simpl.
apply minus_diag.
Qed.
*)
theorem "aequiv (AMinus' (AId' X) (AId' X)) (ANum' 0)"
by (simp add: aequiv_def)
(*
Theorem bequiv_example:
bequiv (BEq (AMinus (AId X) (AId X)) (ANum 0)) BTrue.
Proof.
unfold bequiv. intros. unfold beval.
rewrite aequiv_example. reflexivity.
Qed.
(** Let's start by looking at some trivial command transformations involving SKIP *)
Theorem skip_left: forall c,
cequiv
(SKIP; c)
c.
Proof.
unfold cequiv. intros c st st'.
split; intros H.
Case "->".
inversion H. subst.
inversion H2. subst.
assumption.
Case "<-".
apply E_Seq with st.
apply E_Skip.
assumption.
Qed.
*)
lemma skip_ident1 : "ceval SKIP st st' \<Longrightarrow> st = st'"
apply (erule ceval.cases)
by auto
lemma skip_ident2 : "ceval SKIP st st' \<Longrightarrow> st' = st"
apply (erule ceval.cases)
by auto
lemma skip_left : "cequiv (SKIP ;; c) c"
apply (simp add: cequiv_def)
apply (intro allI iffI)
apply (erule ceval.cases)
apply auto
proof -
fix sta st'a st''
assume H1: "ceval SKIP sta st'a"
assume H2: "ceval c st'a st''"
have L1: "sta = st'a" using H1 by (simp add: skip_ident1)
have L2: "ceval c sta st''" using H2 L1 by simp
show "ceval c sta st''" using L2 by simp
next
fix st st'
assume H1: "ceval c st st'"
have L1: "ceval SKIP st st" by (rule ceval.intros)
show "ceval (SKIP ;; c) st st'" using H1 L1 by (auto intro: ceval.intros)
qed
theorem skip_right : "cequiv (c ;; SKIP) c"
proof (unfold cequiv_def, intro allI iffI, erule ceval.cases, auto)
fix sta st'a st''
assume H1 : "ceval c sta st'a"
assume H2 : "ceval SKIP st'a st''"
have L1 : "st'a = st''" using H2 by (simp add: skip_ident1)
show "ceval c sta st''" using H1 L1 by simp
next
fix st st'
assume H1 : "ceval c st st'"
have L1 : "ceval SKIP st' st'" by (rule ceval.intros)
show "ceval (c ;; SKIP) st st'" using H1 L1 by (rule ceval.intros)
qed
(* CH: Question! So the above proofs are a little longer
than their equivalents in Coq, but I think that's
just because inversion in Coq automatically wipes
out contradictory goals while I need to call auto
in Isabelle to do the same. Is there a way
to make these shorter? *)
(*
(** **** Exercise: 2 stars *)
Theorem skip_right: forall c,
cequiv
(c; SKIP)
c.
Proof.
(* SOLUTION: *)
unfold cequiv. intros c st st'.
split; intros H.
Case "->".
inversion H. subst.
inversion H5. subst.
assumption.
Case "<-".
apply E_Seq with st'.
assumption. apply E_Skip. Qed.
(** [] *)
*)
theorem "cequiv (IF BTrue' THEN c1 ELSE c2) c1"
by (unfold cequiv_def,
intro allI iffI,
erule ceval.cases, auto intro: ceval.intros)
(*
(** We can also explore transformations that simplify [IFB] commands *)
Theorem IFB_true_simple: forall c1 c2,
cequiv
(IFB BTrue THEN c1 ELSE c2 FI)
c1.
Proof.
intros c1 c2.
split; intros H.
Case "->".
inversion H; subst. assumption. inversion H5.
Case "<-".
apply E_IfTrue. reflexivity. assumption. Qed.
(** Of course, few programmers would be tempted to write a while loop
whose guard is literally [BTrue]. A more interesting case is when
the guard is _equivalent_ to true...
_Theorem_: forall [b] [c1] [c2], if [b] is equivalent to [BTrue],
then [IFB b THEN c1 ELSE c2 FI] is equivalent to [c1].
_Proof_:
- (--->) We must show, for all [st] and [st'], that if [IFB b
THEN c1 ELSE c2 FI / st ==> st'] then [c1 / st ==> st'].
There are two rules that could have been used to show [IFB b
THEN c1 ELSE c2 FI / st ==> st']: [E_IfTrue] and [E_IfFalse].
- Suppose the rule used to show [IFB b THEN c1 ELSE c2 FI /
st ==> st'] was [E_IfTrue]. We then have, by the premises of
[E_IfTrue], that [c1 / st ==> st']. This is exactly what we
set out to prove.
- Suppose the rule used to show [IFB b THEN c1 ELSE c2 FI /
st ==> st'] was [E_IfFalse]. We then know that [beval st b =
false] and [c2 / st ==> st'].
Recall that [b] is equivalent to [BTrue], i.e. forall
[st], [beval st b = beval st BTrue]. In particular, this
means that [beval st b = true], since [beval st BTrue =
true]. But this is a contradiction, since [E_IfFalse]
requires that [beval st b = false]. We therefore conclude
that the final rule could not have been [E_IfFalse].
- (<---) We must show, for all [st] and [st'], that if [c1 / st
==> st'] then [IFB b THEN c1 ELSE c2 FI / st ==> st'].
Since [b] is equivalent to [BTrue], we know that [beval st b] =
[beval st BTrue] = [true]. Together with the assumption that
[c1 / st ==> st'], we can apply [E_IfTrue] to derive [IFB b
THEN c1 ELSE c2 FI / st ==> st']. []
Here is the formal version of this proof: *)
*)
theorem "bequiv b BTrue' \<Longrightarrow> cequiv (IF b THEN c1 ELSE c2) c1"
apply (unfold cequiv_def bequiv_def, intro iffI allI)
apply (erule ceval.cases)
by (auto intro: ceval.intros)
(*
Theorem IFB_true: forall b c1 c2,
bequiv b BTrue ->
cequiv
(IFB b THEN c1 ELSE c2 FI)
c1.
Proof.
intros b c1 c2 Hb.
split; intros H.
Case "->".
inversion H; subst.
SCase "b evaluates to true".
assumption.
SCase "b evaluates to false (contradiction)".
rewrite Hb in H5.
inversion H5.
Case "<-".
apply E_IfTrue; try assumption.
rewrite Hb. reflexivity. Qed.
(* Similarly: *)
*)
theorem "bequiv b BFalse' \<Longrightarrow> cequiv (IF b THEN c1 ELSE c2) c2"
apply (unfold bequiv_def cequiv_def, intro iffI allI)
apply (erule ceval.cases)
by (auto intro: ceval.intros)
(*
(** **** Exercise: 2 stars *)
Theorem IFB_false: forall b c1 c2,
bequiv b BFalse ->
cequiv
(IFB b THEN c1 ELSE c2 FI)
c2.
Proof.
(* SOLUTION: *)
intros b c1 c2 Hb.
split; intros H.
Case "->".
inversion H; subst.
SCase "b evaluates to true (contradiction)".
rewrite Hb in H5.
inversion H5.
SCase "b evaluates to false".
assumption.
Case "<-".
apply E_IfFalse; try assumption.
rewrite Hb. reflexivity. Qed.
(** [] *)
(** For while loops, there is a similar pair of theorems: a loop whose
guard is equivalent to [BFalse] is equivalent to [SKIP], while a
loop whose guard is equivalent to [BTrue] is equivalent to [WHILE
BTrue DO SKIP END] (or any other non-terminating program). The
first of these facts is easy. *)
*)
theorem "bequiv b BFalse' \<Longrightarrow> cequiv (WHILE b DO c END) SKIP"
proof (unfold bequiv_def cequiv_def, intro allI iffI,
erule ceval.cases, auto intro: ceval.intros)
fix st st'
assume H1 : "\<forall>st. \<not> beval' st b"
assume H2 : "ceval SKIP st st'"
have L1 : "st = st'" using H2 apply -
apply (erule ceval.cases)
by auto
show "ceval (WHILE b DO c END) st st'" using H1 L1
by (auto intro: ceval.intros)
qed
(*
Theorem WHILE_false : forall b c,
bequiv b BFalse ->
cequiv
(WHILE b DO c END)
SKIP.
Proof.
intros b c Hb.
unfold cequiv. split; intros.
Case "->".
inversion H; subst.
SCase "E_WhileEnd".
apply E_Skip.
SCase "E_WhileLoop".
rewrite Hb in H2. inversion H2.
Case "<-".
inversion H; subst.
apply E_WhileEnd.
rewrite Hb.
reflexivity. Qed.
(** To prove the second fact, we need an auxiliary lemma stating that
while loops whose guards are equivalent to [BTrue] never
terminate:
_Lemma_: If [b] is equivalent to [BTrue], then it cannot be the
case that [(WHILE b DO c END) / st ==> st'].
_Proof_: Suppose that [(WHILE b DO c END) / st ==> st']. We show,
by induction on a derivation of [(WHILE b DO c END) / st ==> st'],
that this assumption leads to a contradiction.
- Suppose [(WHILE b DO c END) / st ==> st'] is proved using rule
[E_WhileEnd]. Then by assumption [beval st b = false]. But
this contradicts the assumption that [b] is equivalent to
[BTrue].
- Suppose [(WHILE b DO c END) / st ==> st'] is proved using rule
[E_WhileLoop]. Then we are given the induction hypothesis
that [(WHILE b DO c END) / st ==> st'] is contradictory, which
is exactly what we are trying to prove!
- Since these are the only rules that could have been used to
prove [(WHILE b DO c END) / st ==> st'], the other cases of
the induction are immediately contradictory. [] *)
*)
lemma while_true_nonterm : "bequiv b BTrue' \<Longrightarrow> \<not> (ceval (WHILE b DO c END) st st')"
apply auto
done
(*
Lemma WHILE_true_nonterm : forall b c st st',
bequiv b BTrue ->
~( (WHILE b DO c END) / st ==> st' ).
Proof.
intros b c st st' Hb.
intros H.
remember (WHILE b DO c END) as cw.
(ceval_cases (induction H) Case);
(* most rules don't apply, and we can rule them out by inversion *)
inversion Heqcw; subst; clear Heqcw.
Case "E_WhileEnd". (* contradictory -- b is always true! *)
rewrite Hb in H. inversion H.
Case "E_WhileLoop". (* immediate from the IH *)
apply IHceval2. reflexivity. Qed.
(** **** Exercise: 2 stars, optional (WHILE_true_nonterm_informal) *)
(** Explain what the lemma [WHILE_true_nonterm] means in English.
(* SOLUTION: *) [WHILE_true_nonterm] claims that if a [bexp] [b] is
equivalent to [BTrue] (i.e., if [forall st, beval st b = true]),
then it is not possible to construct a derivation [(WHILE b DO c
END) / st ==> st'] for any [st], [st'], or [c].
We can understand this lack of a derivation as nontermination: the
reason a derivation can't be constructed is because the
[E_WhileLoop] rule would need to be applied infinitely many times,
and derivations are finite.
*)
(** [] *)
(** **** Exercise: 2 stars *)
(** You'll want to use [WHILE_true_nonterm] here... *)
*)
theorem "bequiv b BTrue' \<Longrightarrow>
cequiv (WHILE b DO c END)
(WHILE BTrue' DO c END)"
proof (unfold cequiv_def, intro allI conjI iffI)
fix st st'
assume H1: "bequiv b BTrue'"
assume H2: "ceval (WHILE b DO c END) st st'"
have L1: "\<not> (ceval (WHILE b DO c END) st st')" using H1 by (simp add: while_true_nonterm)
show "ceval (WHILE BTrue' DO c END) st st'" using H2 L1 by simp
next
fix st st'
assume H1: "ceval (WHILE BTrue' DO c END) st st'"
have L1: "bequiv BTrue' BTrue'" by (simp add: bequiv_def)
have L2: "\<not> (ceval (WHILE BTrue' DO c END) st st')"
using L1 by (simp add: while_true_nonterm)
show "ceval (WHILE b DO c END) st st'" using L2 H1 by simp
qed
(*
Theorem WHILE_true: forall b c,
bequiv b BTrue ->
cequiv
(WHILE b DO c END)
(WHILE BTrue DO SKIP END).
Proof.
(* SOLUTION: *)
intros b c Hb.
split; intros H.
Case "->".
apply ex_falso_quodlibet.
assert (~((WHILE b DO c END) / st ==> st')) as Hcontra.
apply WHILE_true_nonterm; assumption.
apply Hcontra. apply H.
Case "<-".
apply ex_falso_quodlibet.
assert (~((WHILE BTrue DO SKIP END) / st ==> st')) as Hcontra.
apply WHILE_true_nonterm.
unfold bequiv. reflexivity.
apply Hcontra. apply H. Qed.
(** [] *)
(** **** Exercise: 2 stars (WHILE_false_inf) *)
(** Write an informal proof of WHILE_false.
(* SOLUTION: *)
Theorem: forall [b] [c], if [b] is equivalent to [BFalse],
then [WHILE b DO c END] is equivalent to [SKIP].
Proof:
- (->) We know that [b] is equivalent to [BFalse]. We must
show, for all [st] and [st'], that if [(WHILE b DO c END) /
st ==> st'] then [SKIP / st ==> st'].
There are only two ways we can have [(WHILE b DO c END) /
st ==> st']: using [E_WhileEnd] and [E_WhileLoop].
- Suppose the final rule used to show [(WHILE b DO c END) /
st ==> st'] was [E_WhileEnd]. We then know that [st =
st']; by [E_Skip], we know that [SKIP / st ==> st].
- Suppose the final rule used to show [(WHILE b DO c END) / st
==> st'] was [E_WhileLoop]. But this rule only applies when
[beval st b = true]. However, we are assuming that
that [b] is equivalent to [BFalse], i.e. forall
[st], [beval st b = beval st BFalse = false]. So we have
a contradiction, and the final rule could not have been
[E_WhileLoop] after all.
- (<-) We know that [b] is equivalent to [BFalse]. We must
show, for all [st] and [st'], that if [SKIP / st ==> st']
then [(WHILE b DO c END) / st ==> st'].
[E_Skip] is the only rule that could have proven [SKIP / st
==> st'], so we know that [st' = st]. We must show
that [(WHILE b DO c LOOP) / st ==> st].
Since [b] is equivalent to [BFalse], we know that [beval st
b = false]. By [E_WhileEnd], then, we can derive that
[(WHILE b DO c END) / st ==> st], and we are done.
[]
*)
*)
theorem loop_unrolling : "cequiv (WHILE b DO c END)
(IF b THEN (c ;; WHILE b DO c END) ELSE SKIP)"
apply (unfold cequiv_def, intro allI iffI conjI)
apply (erule ceval.cases, auto intro: ceval.intros)+
done
(*
Theorem loop_unrolling: forall b c,
cequiv
(WHILE b DO c END)
(IFB b THEN (c; WHILE b DO c END) ELSE SKIP FI).
Proof.
(* WORKED IN CLASS *)
unfold cequiv. intros b c st st'.
split; intros Hce.
Case "->".
inversion Hce; subst.
SCase "loop doesn't run".
apply E_IfFalse. assumption. apply E_Skip.
SCase "loop runs".
apply E_IfTrue. assumption.
apply E_Seq with (st' := st'0). assumption. assumption.
Case "<-".
inversion Hce; subst.
SCase "loop runs".
inversion H5; subst.
apply E_WhileLoop with (st' := st'0).
assumption. assumption. assumption.
SCase "loop doesn't run".
inversion H5; subst. apply E_WhileEnd. assumption. Qed.
(** Finally, let's look at simple equivalences involving assignments.
This turns out to be a little tricky.
To start with, we might expect to be able to show that certain
kinds of "useless" assignments can be removed. Most trivially: *)
*)
theorem identity_assignment : "cequiv (x ::= AId' x) SKIP"
apply (unfold cequiv_def, intro conjI iffI allI)
apply (erule ceval.cases, auto intro: ceval.intros)
proof -
fix sta
have L : "update sta x (sta x) = sta"
by (auto simp: update_def intro: ext)
show "ceval SKIP sta (update sta x (sta x))" using L
by (auto intro: ceval.intros)
next
fix st st'
assume H: "ceval SKIP st st'"
have L1: "st = st'" using H apply -
apply (erule ceval.cases)
by auto
have L2: "update st x (st x) = st" by (auto simp: update_def intro: ext)
have L3: "update st x (st x) = st'" using L1 L2 by (simp)
show "ceval (x ::= AId' x) st st'" using L3 by (auto intro: ceval.intros)
qed
(* CH: TODO - clean this proof up, also we're totes being jerks here by using functional extensionality when Coq doesn't have it *)
(*
Theorem identity_assignment_first_try : forall (x:id),
cequiv
(x ::= AId x)
SKIP.
Proof.
unfold cequiv. intros. split; intro H.
Case "->".
inversion H; subst. simpl.
replace (update st x (st x)) with st.
constructor.
(** But here we're stuck. The goal looks reasonable, but in fact it is
not provable! If we look back at the set of lemmas we proved
about [update] in the last chapter, we can see that lemma
[update_same] almost does the job, but not quite: it says that the
original and updated states agree at all values, but this is not
the same thing as saying that they are [eq] in Coq's sense!
*)
Admitted.
(** What is going on here? Recall that our states are just functions
from identifiers to values. For Coq, functions are only equal
when their definitions are syntactically the same (after
reduction). (Otherwise, attempts to apply the [eq] constructor
will fail!) In practice, for functions built up out of [update]
operations, this means that two functions are equal only if they
were constructed using the _same_ [update] operations, applied in
the same order. In the theorem above, the sequence of updates on
the first parameter [cequiv] is one longer than for the second
parameter, so it is no wonder that the equality doesn't hold.
But the problem is quite general. If we try to prove other
"trivial" facts, such as
[cequiv (x ::= APlus (AId x ANum 1) ; x ::= APlus (AId x ANum 1))
(x ::= APlus (AId x ANum 2))]
or
[cequiv (x ::= ANum 1; y ::= ANum 2)
(y ::= ANum 2; x ::= ANum 1)]
we'll get stuck in the same way: we'll have two functions that
behave the same way on all inputs, but cannot be proven to be [eq]
to each other.
The principle we would like to use in these situations is called
_functional extensionality_:
forall x, f x = g x
--------------------
f = g
Although this principle is not derivable in Coq, it turns out
to be safe to add it as an (unproven) _axiom_. *)
Axiom functional_extensionality : forall {X Y: Type} {f g : X -> Y},
(forall (x: X), f x = g x) -> f = g.
(** It has been proven that adding this axiom doesn't introduce any
inconsistencies into Coq. In this way, it is similar to adding
one of the classical logic axioms, such as [excluded_middle].
With the benefit of this axiom we can prove our theorem: *)
Theorem identity_assignment : forall (x:id),
cequiv
(x ::= AId x)
SKIP.
Proof.
unfold cequiv. intros. split; intro H.
Case "->".
inversion H; subst. simpl.
replace (update st x (st x)) with st.
constructor.
apply functional_extensionality. intro. rewrite update_same; reflexivity.
Case "<-".
inversion H; subst.
assert (st' = (update st' x (st' x))).
apply functional_extensionality. intro. rewrite update_same; reflexivity.
rewrite H0 at 2.
constructor. reflexivity.
Qed.
(** A digression on equivalences.
Purists might object to using [functional_extensionality]. In
general, it can be quite dangerous to add axioms, particularly
several at once (as they may be mutually inconsistent). In fact,
[functional_extensionality] and [excluded_middle] can both be
assumed without any problems, but some Coq users prefer to avoid
such "heavyweight" general techniques, and instead craft solutions
for specific problems that stay within Coq's standard logic.
For our particular problem here, rather than extending the
definition of equality to do what we want on functions
representing states, we could instead give an explicit notion of
_equivalence_ on states. For example: *)
Definition stequiv (st1 st2 : state) : Prop :=
forall (x:id), st1 x = st2 x.
Notation "st1 '~' st2" := (stequiv st1 st2) (at level 30).
(** It is easy to prove that [stequiv] is an _equivalence_ (i.e., it
is reflexive, symmetric, and transitive), so it partitions the set
of all states into equivalence classes. *)
(** **** Exercise: 1 star *)
Lemma stequiv_refl : forall (st : state), st ~ st.
Proof.
(* SOLUTION: *)
unfold stequiv. intros. reflexivity. Qed.
(** **** Exercise: 1 star *)
Lemma stequiv_sym : forall (st1 st2 : state), st1 ~ st2 -> st2 ~ st1.
Proof.
(* SOLUTION: *)
unfold stequiv. intros. rewrite H. reflexivity. Qed.
(** **** Exercise: 1 star *)
Lemma stequiv_trans : forall (st1 st2 st3 : state), st1 ~ st2 -> st2 ~ st3 -> st1 ~ st3.
Proof.
(* SOLUTION: *)
unfold stequiv. intros. rewrite H. rewrite H0. reflexivity. Qed.
(** It is then straightforward to show that [aeval] and [beval] behave
uniformly on all members of an equvalence class: *)
(** **** Exercise: 2 stars *)
Lemma stequiv_aeval : forall (st1 st2 : state),
st1 ~ st2 ->
forall (a:aexp), aeval st1 a = aeval st2 a.
Proof.
(* SOLUTION: *)
intros. induction a; simpl; try rewrite IHa1; try rewrite IHa2; try reflexivity.
apply H. Qed.
(** **** Exercise: 2 stars *)
Lemma stequiv_beval : forall (st1 st2 : state),
st1 ~ st2 ->
forall (b:bexp), beval st1 b = beval st2 b.
Proof.
(* SOLUTION: *)
intros.
(bexp_cases (induction b) Case); simpl; try reflexivity.
Case "BEq".
repeat rewrite (stequiv_aeval _ _ H). reflexivity.
Case "BLe".
repeat rewrite (stequiv_aeval _ _ H). reflexivity.
Case "BNot".
rewrite IHb. reflexivity.
Case "BAnd".
rewrite IHb1. rewrite IHb2. reflexivity. Qed.
(** Now we need to redefine [cequal] to use [~] instead of [=].
Unfortunately, it is not so easy to do this in a way that keeps
the definition simple and symmetric. But here is one approach.
We first define a looser variant of [==>] that "folds in"
the notion of equivalence. *)
Reserved Notation "c1 '/' st '==>'' st'" (at level 40, st at level 39).
Inductive ceval' : com -> state -> state -> Prop :=
| E_equiv : forall c st st' st'',
c / st ==> st' ->
st' ~ st'' ->
c / st ==>' st''
where "c1 '/' st '==>'' st'" := (ceval' c1 st st').
(** Now the revised definition of [cequiv'] looks familiar: *)
Definition cequiv' (c1 c2 : com) : Prop :=
forall (st st' : state),
(c1 / st ==>' st') <-> (c2 / st ==>' st').
(** A sanity check shows that the original notion of command equivalence is at least
as strong as this new one. (The converse is not true, naturally.) *)
Lemma cequiv__cequiv' : forall (c1 c2: com),
cequiv c1 c2 -> cequiv' c1 c2.
Proof.
unfold cequiv, cequiv'; split; intros.
inversion H0 ; subst. apply E_equiv with st'0. apply (H st st'0); assumption. assumption.
inversion H0 ; subst. apply E_equiv with st'0. apply (H st st'0). assumption. assumption.
Qed.
(** **** Exercise: 2 stars *)
(** Finally, here is our example once more... (You can complete the proof) *)
Example identity_assign' :
cequiv' SKIP (X ::= AId X).
Proof.
unfold cequiv'. intros. split; intros.
Case "->".
inversion H; subst; clear H. inversion H0; subst. apply E_equiv with (update st'0 X (st'0 X)).
constructor. reflexivity. apply stequiv_trans with st'0. unfold stequiv. intro. apply update_same. reflexivity. assumption.
Case "<-".
(* SOLUTION: *)
inversion H; subst; clear H. inversion H0; subst. simpl in H1. apply E_equiv with st.
constructor. apply stequiv_trans with (update st X (st X)). apply stequiv_sym. unfold stequiv. intro. rewrite update_same; reflexivity.
assumption. Qed.
(** On the whole, this explicit equivalence approach is considerably harder to work with than
relying on functional extensionality. (Coq does have an advanced mechanism called "setoids" that
makes working with equivalences somewhat easier, by allowing them to be registered with the system
so that standard rewriting tactics work for them almost as well as for equalities.)
But it is worth knowing about, because it applies even in situations where the equivalence in
question is *not* over functions. For example, if we chose to represent state mappings
as binary search trees, we would need to use an explicit equivalence of this kind. *)
(** End of digression *)
*)
lemma [simp] : "ceval SKIP st st' \<Longrightarrow> st = st'"
by (erule ceval.cases, auto)
lemma [simp] : "update st x (st x) = st"
by (auto simp: update_def intro: ext)
theorem assign_equiv : "aequiv (AId' x) e \<Longrightarrow> cequiv SKIP (x ::= e)"
proof (unfold cequiv_def, intro conjI iffI allI)
fix st st'
assume H1: "aequiv (AId' x) e"
assume H2: "ceval SKIP st st'"
have L1 : "st = st'" using H2 by simp
have L2 : "aeval' st' e = st' x" using H1 by (auto simp: aequiv_def)
have L3 : "update st x (aeval' st e) = st'" using L1 H1 L2 by (auto)
show "ceval (x ::= e) st st'" using L3 by (auto intro: ceval.intros)
next
fix st st'
assume H1: "aequiv (AId' x) e"
assume H2: "ceval (x ::= e) st st'"
have L1 : "st' = update st x (aeval' st e)" using H2
apply - apply (erule ceval.cases) by auto
have L2 : "update st x (aeval' st e) = st" using H1
by (auto simp: aequiv_def)
show "ceval SKIP st st'" using L1 L2 by (auto intro: ceval.intros)
qed
(* CH: I'll admit the the above is a bit more verbose than the Coq proof,
but most of the text comes from the set up of the Isar proof
which I think is worth it to get the more readable form and
the forwards reasoning. I can probably make it cuter,
but my basic goal was simply to have something nice. *)
(*
(** **** Exercise: 2 stars *)
Theorem assign_aequiv : forall x e,
aequiv (AId x) e ->
cequiv SKIP (x ::= e).
Proof.
(* SOLUTION: *)
unfold cequiv. intros.
split; intros; inversion H0; subst.
Case "->".
assert (st' = update st' x (aeval st' e)).
apply functional_extensionality. intros. unfold aequiv in H. simpl in H. rewrite <- H. rewrite update_same; reflexivity.
rewrite H1 at 2. constructor; reflexivity.
Case "<-".
replace (update st x (aeval st e)) with st. constructor.
apply functional_extensionality. intros. rewrite update_same. reflexivity. apply H. Qed.
(** [] *)
(** **** Exercise: 2 stars *)
Theorem seq_assoc : forall c1 c2 c3,
cequiv ((c1;c2);c3) (c1;(c2;c3)).
Proof.
(* SOLUTION: *)
unfold cequiv. intros c1 c2 c3 st st'.
split; intros Hce.
Case "->".
inversion Hce. subst. inversion H1. subst.
apply E_Seq with (st' := st'1); try assumption.
apply E_Seq with (st' := st'0); try assumption.
Case "<-".
inversion Hce. subst. inversion H4. subst.
apply E_Seq with (st' := st'1); try assumption.
apply E_Seq with (st' := st'0); try assumption. Qed.
(** [] *)
*)
theorem seq_assoc : "cequiv ((c1 ;; c2) ;; c3) (c1 ;; (c2 ;; c3))"
apply (unfold cequiv_def, intro allI iffI conjI)
by (erule ceval.cases, auto intro: ceval.intros)+
(* CH: AUTO POWER!!1one
Enough proofs come up that look just like this that I'd, honestly, like
to write a tactic to handle it.
*)
theorem swap_if_branches : "cequiv (IF b THEN e1 ELSE e2) (IF (BNot' b) THEN e2 ELSE e1)"
apply (unfold cequiv_def, intro allI iffI conjI)
by (erule ceval.cases, auto intro: ceval.intros)+
(* CH: I feel like I'm cheating, but I think the Isar examples above make it clear
that if you want to go through the longer, forward reasoning, you can.
Please comment if you'd like to see things done more explicitly! *)
(*
(** **** Exercise: 3 stars *)
Theorem swap_if_branches: forall b e1 e2,
cequiv
(IFB b THEN e1 ELSE e2 FI)
(IFB BNot b THEN e2 ELSE e1 FI).
Proof.
(* SOLUTION: *)
unfold cequiv. intros b eq e2 st st'.
split; intros Hce.
Case "->".
inversion Hce; subst.
SCase "b is true".
apply E_IfFalse; try assumption.
simpl. rewrite H4. reflexivity.
SCase "b is false".
apply E_IfTrue; try assumption.
simpl. rewrite H4. reflexivity.
Case "<-".
inversion Hce; subst.
SCase "b is false".
simpl in H4. symmetry in H4; apply negb_sym in H4. simpl in H4.
apply E_IfFalse; assumption.
SCase "b is true".
simpl in H4. symmetry in H4; apply negb_sym in H4. simpl in H4.
apply E_IfTrue; assumption. Qed.
(** [] *)
(** **** Exercise: 2 stars, optional *)
(** Which of the following pairs of programs are equivalent? Write
"yes" or "no" for each one.
(a)
[[
WHILE (BLe (ANum 1) (AId X)) DO
X ::= APlus (AId X) (ANum 1)
END
]]
and
[[
WHILE (BLe (ANum 2) (AId X)) DO
X ::= APlus (AId X) (ANum 1)
END
]]
(* SOLUTION: *)
No. (When started in a state where variable X has value 1, the
first program diverges while the second one halts.)
(b)
[[
WHILE BTrue DO
WHILE BFalse DO X ::= APlus (AId X) (ANum 1) END
END
]]
and
[[
WHILE BFalse DO
WHILE BTrue DO X ::= APlus (AId X) (ANum 1) END
END
]]
(* SOLUTION: *)
No. (The first program always diverges; the second always halts.)
[] *)
(* ####################################################### *)
(** ** Program Equivalence is an Equivalence *)
(** The equivalences on [aexps], [bexps], and [com]s are
reflexive, symmetric, and transitive *)
Lemma refl_aequiv : forall (a : aexp), aequiv a a.
Proof.
unfold aequiv. intros a st. reflexivity. Qed.
*)
lemma refl_aequiv : "aequiv a a"
by (auto simp: aequiv_def)
(*
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 sym_aequiv : "aequiv a1 a2 \<Longrightarrow> aequiv a2 a1"
by (auto simp: aequiv_def)
(*
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 trans_aequiv : "aequiv a1 a2 \<Longrightarrow> aequiv a2 a3 \<Longrightarrow> aequiv a1 a3"
by (auto simp: aequiv_def)
(*
Lemma refl_bequiv : forall (b : bexp), bequiv b b.
Proof.
unfold bequiv. intros b st. reflexivity. Qed.
*)
lemma refl_bequiv : "bequiv b b"
by (auto simp: bequiv_def)
(*
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 sym_bequiv : "bequiv b1 b2 \<Longrightarrow> bequiv b2 b2"
by (auto simp: bequiv_def)
(*
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.
*)