-
Notifications
You must be signed in to change notification settings - Fork 1
/
Equiv.html
2911 lines (2344 loc) · 272 KB
/
Equiv.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="coqdoc.css" rel="stylesheet" type="text/css"/>
<title>Equiv: Program Equivalence</title>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<div id="page">
<div id="header">
</div>
<div id="main">
<h1 class="libtitle">Equiv<span class="subtitle">Program Equivalence</span></h1>
<div class="code code-tight">
</div>
<div class="doc">
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Export</span> <span class="id" type="var">Imp</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab456"></a><h3 class="section">Some general advice for working on exercises:</h3>
<div class="paragraph"> </div>
<ul class="doclist">
<li> 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.
<div class="paragraph"> </div>
</li>
<li> The Coq proofs we're doing now are sufficiently complicated that
it is more or less impossible to complete them simply 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.
<div class="paragraph"> </div>
</li>
<li> 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.
</li>
</ul>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab457"></a><h1 class="section">Behavioral Equivalence</h1>
<div class="paragraph"> </div>
In the last chapter, we investigated the correctness of a very
simple program transformation: the <span class="inlinecode"><span class="id" type="var">optimize_0plus</span></span> 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 <i>means</i> for a
program transformation to be correct: it should always yield a
program that evaluates to the same number as the original.
<div class="paragraph"> </div>
To go further and talk about the correctness of program
transformations in the full Imp language, we need to consider the
role of variables and state.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab458"></a><h2 class="section">Definitions</h2>
<div class="paragraph"> </div>
For <span class="inlinecode"><span class="id" type="var">aexp</span></span>s and <span class="inlinecode"><span class="id" type="var">bexp</span></span>s with variables, the definition we want is
clear. We say
that two <span class="inlinecode"><span class="id" type="var">aexp</span></span>s or <span class="inlinecode"><span class="id" type="var">bexp</span></span>s are <i>behaviorally equivalent</i> if they
evaluate to the same result <i>in every state</i>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">aequiv</span> (<span class="id" type="var">a1</span> <span class="id" type="var">a2</span> : <span class="id" type="var">aexp</span>) : <span class="id" type="keyword">Prop</span> :=<br/>
<span style="font-family: arial;">∀</span>(<span class="id" type="var">st</span>:<span class="id" type="var">state</span>), <br/>
<span class="id" type="var">aeval</span> <span class="id" type="var">st</span> <span class="id" type="var">a1</span> = <span class="id" type="var">aeval</span> <span class="id" type="var">st</span> <span class="id" type="var">a2</span>.<br/>
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">bequiv</span> (<span class="id" type="var">b1</span> <span class="id" type="var">b2</span> : <span class="id" type="var">bexp</span>) : <span class="id" type="keyword">Prop</span> :=<br/>
<span style="font-family: arial;">∀</span>(<span class="id" type="var">st</span>:<span class="id" type="var">state</span>), <br/>
<span class="id" type="var">beval</span> <span class="id" type="var">st</span> <span class="id" type="var">b1</span> = <span class="id" type="var">beval</span> <span class="id" type="var">st</span> <span class="id" type="var">b2</span>.<br/>
<br/>
</div>
<div class="doc">
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 (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. A compact way to express this
is "if the first one terminates in a particular state then so does
the second, and vice versa."
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">cequiv</span> (<span class="id" type="var">c1</span> <span class="id" type="var">c2</span> : <span class="id" type="var">com</span>) : <span class="id" type="keyword">Prop</span> :=<br/>
<span style="font-family: arial;">∀</span>(<span class="id" type="var">st</span> <span class="id" type="var">st'</span> : <span class="id" type="var">state</span>), <br/>
(<span class="id" type="var">c1</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st'</span>) <span style="font-family: arial;">↔</span> (<span class="id" type="var">c2</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st'</span>).<br/>
<br/>
</div>
<div class="doc">
<a name="lab459"></a><h4 class="section">Exercise: 2 stars (equiv_classes)</h4>
<div class="paragraph"> </div>
Given the following programs, group together those that are
equivalent in <span class="inlinecode"><span class="id" type="var">Imp</span></span>. 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: {a,b,c,d,e,f,g,h} {i}.
<div class="paragraph"> </div>
(a)
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">WHILE</span> <span class="id" type="var">X</span> > 0 <span class="id" type="var">DO</span><br/>
<span class="id" type="var">X</span> ::= <span class="id" type="var">X</span> + 1<br/>
<span class="id" type="var">END</span>
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
(b)
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">IFB</span> <span class="id" type="var">X</span> = 0 <span class="id" type="var">THEN</span><br/>
<span class="id" type="var">X</span> ::= <span class="id" type="var">X</span> + 1;;<br/>
<span class="id" type="var">Y</span> ::= 1<br/>
<span class="id" type="var">ELSE</span><br/>
<span class="id" type="var">Y</span> ::= 0<br/>
<span class="id" type="var">FI</span>;;<br/>
<span class="id" type="var">X</span> ::= <span class="id" type="var">X</span> - <span class="id" type="var">Y</span>;;<br/>
<span class="id" type="var">Y</span> ::= 0
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
(c)
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">SKIP</span>
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
(d)
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">WHILE</span> <span class="id" type="var">X</span> ≠ 0 <span class="id" type="var">DO</span><br/>
<span class="id" type="var">X</span> ::= <span class="id" type="var">X</span> × <span class="id" type="var">Y</span> + 1<br/>
<span class="id" type="var">END</span>
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
(e)
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">Y</span> ::= 0
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
(f)
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">Y</span> ::= <span class="id" type="var">X</span> + 1;;<br/>
<span class="id" type="var">WHILE</span> <span class="id" type="var">X</span> ≠ <span class="id" type="var">Y</span> <span class="id" type="var">DO</span><br/>
<span class="id" type="var">Y</span> ::= <span class="id" type="var">X</span> + 1<br/>
<span class="id" type="var">END</span>
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
(g)
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">WHILE</span> <span class="id" type="var">TRUE</span> <span class="id" type="var">DO</span><br/>
<span class="id" type="var">SKIP</span><br/>
<span class="id" type="var">END</span>
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
(h)
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">WHILE</span> <span class="id" type="var">X</span> ≠ <span class="id" type="var">X</span> <span class="id" type="var">DO</span><br/>
<span class="id" type="var">X</span> ::= <span class="id" type="var">X</span> + 1<br/>
<span class="id" type="var">END</span>
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
(i)
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">WHILE</span> <span class="id" type="var">X</span> ≠ <span class="id" type="var">Y</span> <span class="id" type="var">DO</span><br/>
<span class="id" type="var">X</span> ::= <span class="id" type="var">Y</span> + 1<br/>
<span class="id" type="var">END</span>
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
<span class="comment">(* FILL IN HERE *)</span><br/>
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab460"></a><h2 class="section">Examples</h2>
<div class="paragraph"> </div>
Here are some simple examples of equivalences of arithmetic
and boolean expressions.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">aequiv_example</span>:<br/>
<span class="id" type="var">aequiv</span> (<span class="id" type="var">AMinus</span> (<span class="id" type="var">AId</span> <span class="id" type="var">X</span>) (<span class="id" type="var">AId</span> <span class="id" type="var">X</span>)) (<span class="id" type="var">ANum</span> 0).<br/>
<div class="togglescript" id="proofcontrol1" onclick="toggleDisplay('proof1');toggleDisplay('proofcontrol1')"><span class="show"></span></div>
<div class="proofscript" id="proof1" onclick="toggleDisplay('proof1');toggleDisplay('proofcontrol1')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">st</span>. <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">omega</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">bequiv_example</span>:<br/>
<span class="id" type="var">bequiv</span> (<span class="id" type="var">BEq</span> (<span class="id" type="var">AMinus</span> (<span class="id" type="var">AId</span> <span class="id" type="var">X</span>) (<span class="id" type="var">AId</span> <span class="id" type="var">X</span>)) (<span class="id" type="var">ANum</span> 0)) <span class="id" type="var">BTrue</span>.<br/>
<div class="togglescript" id="proofcontrol2" onclick="toggleDisplay('proof2');toggleDisplay('proofcontrol2')"><span class="show"></span></div>
<div class="proofscript" id="proof2" onclick="toggleDisplay('proof2');toggleDisplay('proofcontrol2')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">st</span>. <span class="id" type="tactic">unfold</span> <span class="id" type="var">beval</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">aequiv_example</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
For examples of command equivalence, let's start by looking at
some trivial program transformations involving <span class="inlinecode"><span class="id" type="var">SKIP</span></span>:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">skip_left</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">c</span>,<br/>
<span class="id" type="var">cequiv</span> <br/>
(<span class="id" type="var">SKIP</span>;; <span class="id" type="var">c</span>) <br/>
<span class="id" type="var">c</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* WORKED IN CLASS *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span>.<br/>
<span class="id" type="tactic">split</span>; <span class="id" type="tactic">intros</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">Case</span> "<span style="font-family: arial;">→</span>".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">subst</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H2</span>. <span class="id" type="tactic">subst</span>.<br/>
<span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="var">Case</span> "<span style="font-family: arial;">←</span>".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">E_Seq</span> <span class="id" type="keyword">with</span> <span class="id" type="var">st</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">E_Skip</span>.<br/>
<span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab461"></a><h4 class="section">Exercise: 2 stars (skip_right)</h4>
Prove that adding a SKIP after a command results in an equivalent
program
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">skip_right</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">c</span>,<br/>
<span class="id" type="var">cequiv</span> <br/>
(<span class="id" type="var">c</span>;; <span class="id" type="var">SKIP</span>) <br/>
<span class="id" type="var">c</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
Similarly, here is a simple transformations that simplifies <span class="inlinecode"><span class="id" type="var">IFB</span></span>
commands:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">IFB_true_simple</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">c1</span> <span class="id" type="var">c2</span>,<br/>
<span class="id" type="var">cequiv</span> <br/>
(<span class="id" type="var">IFB</span> <span class="id" type="var">BTrue</span> <span class="id" type="var">THEN</span> <span class="id" type="var">c1</span> <span class="id" type="var">ELSE</span> <span class="id" type="var">c2</span> <span class="id" type="var">FI</span>) <br/>
<span class="id" type="var">c1</span>.<br/>
<div class="togglescript" id="proofcontrol3" onclick="toggleDisplay('proof3');toggleDisplay('proofcontrol3')"><span class="show"></span></div>
<div class="proofscript" id="proof3" onclick="toggleDisplay('proof3');toggleDisplay('proofcontrol3')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">c1</span> <span class="id" type="var">c2</span>.<br/>
<span class="id" type="tactic">split</span>; <span class="id" type="tactic">intros</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">Case</span> "<span style="font-family: arial;">→</span>".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>; <span class="id" type="tactic">subst</span>. <span class="id" type="tactic">assumption</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">H5</span>.<br/>
<span class="id" type="var">Case</span> "<span style="font-family: arial;">←</span>".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">E_IfTrue</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="tactic">assumption</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
Of course, few programmers would be tempted to write a conditional
whose guard is literally <span class="inlinecode"><span class="id" type="var">BTrue</span></span>. A more interesting case is when
the guard is <i>equivalent</i> to true:
<div class="paragraph"> </div>
<i>Theorem</i>: If <span class="inlinecode"><span class="id" type="var">b</span></span> is equivalent to <span class="inlinecode"><span class="id" type="var">BTrue</span></span>, then <span class="inlinecode"><span class="id" type="var">IFB</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">THEN</span></span> <span class="inlinecode"><span class="id" type="var">c1</span></span>
<span class="inlinecode"><span class="id" type="var">ELSE</span></span> <span class="inlinecode"><span class="id" type="var">c2</span></span> <span class="inlinecode"><span class="id" type="var">FI</span></span> is equivalent to <span class="inlinecode"><span class="id" type="var">c1</span></span>.
<a name="lab462"></a><h2 class="section"> </h2>
<div class="paragraph"> </div>
<i>Proof</i>:
<div class="paragraph"> </div>
<ul class="doclist">
<li> (<span class="inlinecode"><span style="font-family: arial;">→</span></span>) We must show, for all <span class="inlinecode"><span class="id" type="var">st</span></span> and <span class="inlinecode"><span class="id" type="var">st'</span></span>, that if <span class="inlinecode"><span class="id" type="var">IFB</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span>
<span class="inlinecode"><span class="id" type="var">THEN</span></span> <span class="inlinecode"><span class="id" type="var">c1</span></span> <span class="inlinecode"><span class="id" type="var">ELSE</span></span> <span class="inlinecode"><span class="id" type="var">c2</span></span> <span class="inlinecode"><span class="id" type="var">FI</span></span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span> then <span class="inlinecode"><span class="id" type="var">c1</span></span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span>.
<div class="paragraph"> </div>
Proceed by cases on the rules that could possibly have been
used to show <span class="inlinecode"><span class="id" type="var">IFB</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">THEN</span></span> <span class="inlinecode"><span class="id" type="var">c1</span></span> <span class="inlinecode"><span class="id" type="var">ELSE</span></span> <span class="inlinecode"><span class="id" type="var">c2</span></span> <span class="inlinecode"><span class="id" type="var">FI</span></span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span>, namely
<span class="inlinecode"><span class="id" type="var">E_IfTrue</span></span> and <span class="inlinecode"><span class="id" type="var">E_IfFalse</span></span>.
<div class="paragraph"> </div>
<ul class="doclist">
<li> Suppose the final rule rule in the derivation of <span class="inlinecode"><span class="id" type="var">IFB</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">THEN</span></span>
<span class="inlinecode"><span class="id" type="var">c1</span></span> <span class="inlinecode"><span class="id" type="var">ELSE</span></span> <span class="inlinecode"><span class="id" type="var">c2</span></span> <span class="inlinecode"><span class="id" type="var">FI</span></span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span> was <span class="inlinecode"><span class="id" type="var">E_IfTrue</span></span>. We then have, by
the premises of <span class="inlinecode"><span class="id" type="var">E_IfTrue</span></span>, that <span class="inlinecode"><span class="id" type="var">c1</span></span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span>. This is
exactly what we set out to prove.
<div class="paragraph"> </div>
</li>
<li> On the other hand, suppose the final rule in the derivation
of <span class="inlinecode"><span class="id" type="var">IFB</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">THEN</span></span> <span class="inlinecode"><span class="id" type="var">c1</span></span> <span class="inlinecode"><span class="id" type="var">ELSE</span></span> <span class="inlinecode"><span class="id" type="var">c2</span></span> <span class="inlinecode"><span class="id" type="var">FI</span></span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span> was <span class="inlinecode"><span class="id" type="var">E_IfFalse</span></span>.
We then know that <span class="inlinecode"><span class="id" type="var">beval</span></span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">false</span></span> and <span class="inlinecode"><span class="id" type="var">c2</span></span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span>.
<div class="paragraph"> </div>
Recall that <span class="inlinecode"><span class="id" type="var">b</span></span> is equivalent to <span class="inlinecode"><span class="id" type="var">BTrue</span></span>, i.e. forall <span class="inlinecode"><span class="id" type="var">st</span></span>,
<span class="inlinecode"><span class="id" type="var">beval</span></span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">beval</span></span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span class="id" type="var">BTrue</span></span>. In particular, this means
that <span class="inlinecode"><span class="id" type="var">beval</span></span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">true</span></span>, since <span class="inlinecode"><span class="id" type="var">beval</span></span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span class="id" type="var">BTrue</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">true</span></span>. But
this is a contradiction, since <span class="inlinecode"><span class="id" type="var">E_IfFalse</span></span> requires that
<span class="inlinecode"><span class="id" type="var">beval</span></span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">false</span></span>. Thus, the final rule could not have
been <span class="inlinecode"><span class="id" type="var">E_IfFalse</span></span>.
<div class="paragraph"> </div>
</li>
</ul>
</li>
<li> (<span class="inlinecode"><span style="font-family: arial;">←</span></span>) We must show, for all <span class="inlinecode"><span class="id" type="var">st</span></span> and <span class="inlinecode"><span class="id" type="var">st'</span></span>, that if <span class="inlinecode"><span class="id" type="var">c1</span></span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span>
<span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span> then <span class="inlinecode"><span class="id" type="var">IFB</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">THEN</span></span> <span class="inlinecode"><span class="id" type="var">c1</span></span> <span class="inlinecode"><span class="id" type="var">ELSE</span></span> <span class="inlinecode"><span class="id" type="var">c2</span></span> <span class="inlinecode"><span class="id" type="var">FI</span></span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span>.
<div class="paragraph"> </div>
Since <span class="inlinecode"><span class="id" type="var">b</span></span> is equivalent to <span class="inlinecode"><span class="id" type="var">BTrue</span></span>, we know that <span class="inlinecode"><span class="id" type="var">beval</span></span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> =
<span class="inlinecode"><span class="id" type="var">beval</span></span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span class="id" type="var">BTrue</span></span> = <span class="inlinecode"><span class="id" type="var">true</span></span>. Together with the assumption that
<span class="inlinecode"><span class="id" type="var">c1</span></span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span>, we can apply <span class="inlinecode"><span class="id" type="var">E_IfTrue</span></span> to derive <span class="inlinecode"><span class="id" type="var">IFB</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">THEN</span></span>
<span class="inlinecode"><span class="id" type="var">c1</span></span> <span class="inlinecode"><span class="id" type="var">ELSE</span></span> <span class="inlinecode"><span class="id" type="var">c2</span></span> <span class="inlinecode"><span class="id" type="var">FI</span></span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span>. <font size=-2>☐</font>
</li>
</ul>
<div class="paragraph"> </div>
Here is the formal version of this proof:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">IFB_true</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">b</span> <span class="id" type="var">c1</span> <span class="id" type="var">c2</span>,<br/>
<span class="id" type="var">bequiv</span> <span class="id" type="var">b</span> <span class="id" type="var">BTrue</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">cequiv</span> <br/>
(<span class="id" type="var">IFB</span> <span class="id" type="var">b</span> <span class="id" type="var">THEN</span> <span class="id" type="var">c1</span> <span class="id" type="var">ELSE</span> <span class="id" type="var">c2</span> <span class="id" type="var">FI</span>) <br/>
<span class="id" type="var">c1</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">b</span> <span class="id" type="var">c1</span> <span class="id" type="var">c2</span> <span class="id" type="var">Hb</span>.<br/>
<span class="id" type="tactic">split</span>; <span class="id" type="tactic">intros</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">Case</span> "<span style="font-family: arial;">→</span>".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>; <span class="id" type="tactic">subst</span>.<br/>
<span class="id" type="var">SCase</span> "b evaluates to true".<br/>
<span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="var">SCase</span> "b evaluates to false (contradiction)".<br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">bequiv</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hb</span>. <span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hb</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">Hb</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H5</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H5</span>.<br/>
<span class="id" type="var">Case</span> "<span style="font-family: arial;">←</span>".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">E_IfTrue</span>; <span class="id" type="tactic">try</span> <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">bequiv</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hb</span>. <span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hb</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">Hb</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab463"></a><h4 class="section">Exercise: 2 stars (IFB_false)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">IFB_false</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">b</span> <span class="id" type="var">c1</span> <span class="id" type="var">c2</span>,<br/>
<span class="id" type="var">bequiv</span> <span class="id" type="var">b</span> <span class="id" type="var">BFalse</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">cequiv</span> <br/>
(<span class="id" type="var">IFB</span> <span class="id" type="var">b</span> <span class="id" type="var">THEN</span> <span class="id" type="var">c1</span> <span class="id" type="var">ELSE</span> <span class="id" type="var">c2</span> <span class="id" type="var">FI</span>) <br/>
<span class="id" type="var">c2</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab464"></a><h4 class="section">Exercise: 3 stars (swap_if_branches)</h4>
Show that we can swap the branches of an IF by negating its
condition
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">swap_if_branches</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">b</span> <span class="id" type="var">e1</span> <span class="id" type="var">e2</span>,<br/>
<span class="id" type="var">cequiv</span><br/>
(<span class="id" type="var">IFB</span> <span class="id" type="var">b</span> <span class="id" type="var">THEN</span> <span class="id" type="var">e1</span> <span class="id" type="var">ELSE</span> <span class="id" type="var">e2</span> <span class="id" type="var">FI</span>)<br/>
(<span class="id" type="var">IFB</span> <span class="id" type="var">BNot</span> <span class="id" type="var">b</span> <span class="id" type="var">THEN</span> <span class="id" type="var">e2</span> <span class="id" type="var">ELSE</span> <span class="id" type="var">e1</span> <span class="id" type="var">FI</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab465"></a><h2 class="section"> </h2>
<div class="paragraph"> </div>
For <span class="inlinecode"><span class="id" type="var">WHILE</span></span> loops, we can give a similar pair of theorems. A loop
whose guard is equivalent to <span class="inlinecode"><span class="id" type="var">BFalse</span></span> is equivalent to <span class="inlinecode"><span class="id" type="var">SKIP</span></span>,
while a loop whose guard is equivalent to <span class="inlinecode"><span class="id" type="var">BTrue</span></span> is equivalent to
<span class="inlinecode"><span class="id" type="var">WHILE</span></span> <span class="inlinecode"><span class="id" type="var">BTrue</span></span> <span class="inlinecode"><span class="id" type="var">DO</span></span> <span class="inlinecode"><span class="id" type="var">SKIP</span></span> <span class="inlinecode"><span class="id" type="var">END</span></span> (or any other non-terminating program).
The first of these facts is easy.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">WHILE_false</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">b</span> <span class="id" type="var">c</span>,<br/>
<span class="id" type="var">bequiv</span> <span class="id" type="var">b</span> <span class="id" type="var">BFalse</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">cequiv</span><br/>
(<span class="id" type="var">WHILE</span> <span class="id" type="var">b</span> <span class="id" type="var">DO</span> <span class="id" type="var">c</span> <span class="id" type="var">END</span>)<br/>
<span class="id" type="var">SKIP</span>.<br/>
<div class="togglescript" id="proofcontrol4" onclick="toggleDisplay('proof4');toggleDisplay('proofcontrol4')"><span class="show"></span></div>
<div class="proofscript" id="proof4" onclick="toggleDisplay('proof4');toggleDisplay('proofcontrol4')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">b</span> <span class="id" type="var">c</span> <span class="id" type="var">Hb</span>. <span class="id" type="tactic">split</span>; <span class="id" type="tactic">intros</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">Case</span> "<span style="font-family: arial;">→</span>".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>; <span class="id" type="tactic">subst</span>.<br/>
<span class="id" type="var">SCase</span> "E_WhileEnd".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">E_Skip</span>.<br/>
<span class="id" type="var">SCase</span> "E_WhileLoop".<br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">Hb</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H2</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">H2</span>.<br/>
<span class="id" type="var">Case</span> "<span style="font-family: arial;">←</span>".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>; <span class="id" type="tactic">subst</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">E_WhileEnd</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">Hb</span>.<br/>
<span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
<a name="lab466"></a><h4 class="section">Exercise: 2 stars, advanced, optional (WHILE_false_informal)</h4>
Write an informal proof of <span class="inlinecode"><span class="id" type="var">WHILE_false</span></span>.
<div class="paragraph"> </div>
<span class="comment">(* FILL IN HERE *)</span><br/>
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab467"></a><h2 class="section"> </h2>
To prove the second fact, we need an auxiliary lemma stating that
<span class="inlinecode"><span class="id" type="var">WHILE</span></span> loops whose guards are equivalent to <span class="inlinecode"><span class="id" type="var">BTrue</span></span> never
terminate:
<div class="paragraph"> </div>
<i>Lemma</i>: If <span class="inlinecode"><span class="id" type="var">b</span></span> is equivalent to <span class="inlinecode"><span class="id" type="var">BTrue</span></span>, then it cannot be the
case that <span class="inlinecode">(<span class="id" type="var">WHILE</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">DO</span></span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode"><span class="id" type="var">END</span>)</span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span>.
<div class="paragraph"> </div>
<i>Proof</i>: Suppose that <span class="inlinecode">(<span class="id" type="var">WHILE</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">DO</span></span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode"><span class="id" type="var">END</span>)</span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span>. We show,
by induction on a derivation of <span class="inlinecode">(<span class="id" type="var">WHILE</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">DO</span></span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode"><span class="id" type="var">END</span>)</span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span>,
that this assumption leads to a contradiction.
<div class="paragraph"> </div>
<ul class="doclist">
<li> Suppose <span class="inlinecode">(<span class="id" type="var">WHILE</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">DO</span></span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode"><span class="id" type="var">END</span>)</span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span> is proved using rule
<span class="inlinecode"><span class="id" type="var">E_WhileEnd</span></span>. Then by assumption <span class="inlinecode"><span class="id" type="var">beval</span></span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">false</span></span>. But
this contradicts the assumption that <span class="inlinecode"><span class="id" type="var">b</span></span> is equivalent to
<span class="inlinecode"><span class="id" type="var">BTrue</span></span>.
<div class="paragraph"> </div>
</li>
<li> Suppose <span class="inlinecode">(<span class="id" type="var">WHILE</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">DO</span></span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode"><span class="id" type="var">END</span>)</span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span> is proved using rule
<span class="inlinecode"><span class="id" type="var">E_WhileLoop</span></span>. Then we are given the induction hypothesis
that <span class="inlinecode">(<span class="id" type="var">WHILE</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">DO</span></span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode"><span class="id" type="var">END</span>)</span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span> is contradictory, which
is exactly what we are trying to prove!
<div class="paragraph"> </div>
</li>
<li> Since these are the only rules that could have been used to
prove <span class="inlinecode">(<span class="id" type="var">WHILE</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">DO</span></span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode"><span class="id" type="var">END</span>)</span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">st'</span></span>, the other cases of
the induction are immediately contradictory. <font size=-2>☐</font>
</li>
</ul>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">WHILE_true_nonterm</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">b</span> <span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span>,<br/>
<span class="id" type="var">bequiv</span> <span class="id" type="var">b</span> <span class="id" type="var">BTrue</span> <span style="font-family: arial;">→</span><br/>
~( (<span class="id" type="var">WHILE</span> <span class="id" type="var">b</span> <span class="id" type="var">DO</span> <span class="id" type="var">c</span> <span class="id" type="var">END</span>) / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st'</span> ).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* WORKED IN CLASS *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">b</span> <span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span> <span class="id" type="var">Hb</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">remember</span> (<span class="id" type="var">WHILE</span> <span class="id" type="var">b</span> <span class="id" type="var">DO</span> <span class="id" type="var">c</span> <span class="id" type="var">END</span>) <span class="id" type="keyword">as</span> <span class="id" type="var">cw</span> <span class="id" type="var">eqn</span>:<span class="id" type="var">Heqcw</span>.<br/>
<span class="id" type="var">ceval_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">H</span>) <span class="id" type="var">Case</span>;<br/>
<span class="comment">(* Most rules don't apply, and we can rule them out <br/>
by inversion *)</span><br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">Heqcw</span>; <span class="id" type="tactic">subst</span>; <span class="id" type="tactic">clear</span> <span class="id" type="var">Heqcw</span>.<br/>
<span class="comment">(* The two interesting cases are the ones for WHILE loops: *)</span><br/>
<span class="id" type="var">Case</span> "E_WhileEnd". <span class="comment">(* contradictory -- b is always true! *)</span><br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">bequiv</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hb</span>.<br/>
<span class="comment">(* <span class="inlinecode"><span class="id" type="tactic">rewrite</span></span> is able to instantiate the quantifier in <span class="inlinecode"><span class="id" type="var">st</span></span> *)</span><br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">Hb</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">Case</span> "E_WhileLoop". <span class="comment">(* immediate from the IH *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHceval2</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab468"></a><h4 class="section">Exercise: 2 stars, optional (WHILE_true_nonterm_informal)</h4>
Explain what the lemma <span class="inlinecode"><span class="id" type="var">WHILE_true_nonterm</span></span> means in English.
<div class="paragraph"> </div>
<span class="comment">(* FILL IN HERE *)</span><br/>
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab469"></a><h4 class="section">Exercise: 2 stars (WHILE_true)</h4>
Prove the following theorem. <i>Hint</i>: You'll want to use
<span class="inlinecode"><span class="id" type="var">WHILE_true_nonterm</span></span> here.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">WHILE_true</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">b</span> <span class="id" type="var">c</span>,<br/>
<span class="id" type="var">bequiv</span> <span class="id" type="var">b</span> <span class="id" type="var">BTrue</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">cequiv</span> <br/>
(<span class="id" type="var">WHILE</span> <span class="id" type="var">b</span> <span class="id" type="var">DO</span> <span class="id" type="var">c</span> <span class="id" type="var">END</span>)<br/>
(<span class="id" type="var">WHILE</span> <span class="id" type="var">BTrue</span> <span class="id" type="var">DO</span> <span class="id" type="var">SKIP</span> <span class="id" type="var">END</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">loop_unrolling</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">b</span> <span class="id" type="var">c</span>,<br/>
<span class="id" type="var">cequiv</span><br/>
(<span class="id" type="var">WHILE</span> <span class="id" type="var">b</span> <span class="id" type="var">DO</span> <span class="id" type="var">c</span> <span class="id" type="var">END</span>)<br/>
(<span class="id" type="var">IFB</span> <span class="id" type="var">b</span> <span class="id" type="var">THEN</span> (<span class="id" type="var">c</span>;; <span class="id" type="var">WHILE</span> <span class="id" type="var">b</span> <span class="id" type="var">DO</span> <span class="id" type="var">c</span> <span class="id" type="var">END</span>) <span class="id" type="var">ELSE</span> <span class="id" type="var">SKIP</span> <span class="id" type="var">FI</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* WORKED IN CLASS *)</span><br/>
<div class="togglescript" id="proofcontrol5" onclick="toggleDisplay('proof5');toggleDisplay('proofcontrol5')"><span class="show"></span></div>
<div class="proofscript" id="proof5" onclick="toggleDisplay('proof5');toggleDisplay('proofcontrol5')">
<span class="id" type="tactic">intros</span> <span class="id" type="var">b</span> <span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span>.<br/>
<span class="id" type="tactic">split</span>; <span class="id" type="tactic">intros</span> <span class="id" type="var">Hce</span>.<br/>
<span class="id" type="var">Case</span> "<span style="font-family: arial;">→</span>".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">Hce</span>; <span class="id" type="tactic">subst</span>.<br/>
<span class="id" type="var">SCase</span> "loop doesn't run".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">E_IfFalse</span>. <span class="id" type="tactic">assumption</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">E_Skip</span>.<br/>
<span class="id" type="var">SCase</span> "loop runs".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">E_IfTrue</span>. <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">E_Seq</span> <span class="id" type="keyword">with</span> (<span class="id" type="var">st'</span> := <span class="id" type="var">st'0</span>). <span class="id" type="tactic">assumption</span>. <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="var">Case</span> "<span style="font-family: arial;">←</span>".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">Hce</span>; <span class="id" type="tactic">subst</span>.<br/>
<span class="id" type="var">SCase</span> "loop runs".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H5</span>; <span class="id" type="tactic">subst</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">E_WhileLoop</span> <span class="id" type="keyword">with</span> (<span class="id" type="var">st'</span> := <span class="id" type="var">st'0</span>).<br/>
<span class="id" type="tactic">assumption</span>. <span class="id" type="tactic">assumption</span>. <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="var">SCase</span> "loop doesn't run".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H5</span>; <span class="id" type="tactic">subst</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">E_WhileEnd</span>. <span class="id" type="tactic">assumption</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
<a name="lab470"></a><h4 class="section">Exercise: 2 stars, optional (seq_assoc)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">seq_assoc</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">c1</span> <span class="id" type="var">c2</span> <span class="id" type="var">c3</span>,<br/>
<span class="id" type="var">cequiv</span> ((<span class="id" type="var">c1</span>;;<span class="id" type="var">c2</span>);;<span class="id" type="var">c3</span>) (<span class="id" type="var">c1</span>;;(<span class="id" type="var">c2</span>;;<span class="id" type="var">c3</span>)).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab471"></a><h2 class="section">The Functional Equivalence Axiom</h2>
<div class="paragraph"> </div>
Finally, let's look at simple equivalences involving assignments.
For example, we might expect to be able to show that <span class="inlinecode"><span class="id" type="var">X</span></span> <span class="inlinecode">::=</span> <span class="inlinecode"><span class="id" type="var">AId</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span>
is equivalent to <span class="inlinecode"><span class="id" type="var">SKIP</span></span>. However, when we try to show it, we get
stuck in an interesting way.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">identity_assignment_first_try</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">X</span>:<span class="id" type="var">id</span>),<br/>
<span class="id" type="var">cequiv</span> (<span class="id" type="var">X</span> ::= <span class="id" type="var">AId</span> <span class="id" type="var">X</span>) <span class="id" type="var">SKIP</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span>. <span class="id" type="tactic">split</span>; <span class="id" type="tactic">intro</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">Case</span> "<span style="font-family: arial;">→</span>".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>; <span class="id" type="tactic">subst</span>. <span class="id" type="tactic">simpl</span>.<br/>
<span class="id" type="tactic">replace</span> (<span class="id" type="var">update</span> <span class="id" type="var">st</span> <span class="id" type="var">X</span> (<span class="id" type="var">st</span> <span class="id" type="var">X</span>)) <span class="id" type="keyword">with</span> <span class="id" type="var">st</span>.<br/>
<span class="id" type="var">constructor</span>.<br/>
<span class="comment">(* Stuck... *)</span> <span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
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
<span class="inlinecode"><span class="id" type="var">update</span></span> in the last chapter, we can see that lemma <span class="inlinecode"><span class="id" type="var">update_same</span></span>
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 <span class="inlinecode">=</span> in Coq's sense!
<div class="paragraph"> </div>
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, modulo
simplification. (This is the only way we can legally apply the
<span class="inlinecode"><span class="id" type="var">refl_equal</span></span> constructor of the inductively defined proposition
<span class="inlinecode"><span class="id" type="var">eq</span></span>!) In practice, for functions built up by repeated uses of the
<span class="inlinecode"><span class="id" type="var">update</span></span> operation, this means that two functions can be proven
equal only if they were constructed using the <i>same</i> <span class="inlinecode"><span class="id" type="var">update</span></span>
operations, applied in the same order. In the theorem above, the
sequence of updates on the first parameter <span class="inlinecode"><span class="id" type="var">cequiv</span></span> is one longer
than for the second parameter, so it is no wonder that the
equality doesn't hold.
<div class="paragraph"> </div>
<a name="lab472"></a><h2 class="section"> </h2>
This problem is actually quite general. If we try to prove other
simple facts, such as
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">cequiv</span> (<span class="id" type="var">X</span> ::= <span class="id" type="var">X</span> + 1;;<br/>
<span class="id" type="var">X</span> ::= <span class="id" type="var">X</span> + 1)<br/>
(<span class="id" type="var">X</span> ::= <span class="id" type="var">X</span> + 2)
<div class="paragraph"> </div>
</div>
or
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">cequiv</span> (<span class="id" type="var">X</span> ::= 1;; <span class="id" type="var">Y</span> ::= 2)<br/>
(<span class="id" type="var">y</span> ::= 2;; <span class="id" type="var">X</span> ::= 1)<br/>
<div class="paragraph"> </div>
</div>
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 <span class="inlinecode"><span class="id" type="var">eq</span></span>
to each other.
<div class="paragraph"> </div>
The reasoning principle we would like to use in these situations
is called <i>functional extensionality</i>:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: arial;">∀</span>x, f x = g x</td>
<td class="infrulenamecol" rowspan="3">
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">f = g</td>
<td></td>
</td>
</table></center> Although this principle is not derivable in Coq's built-in logic,
it is safe to add it as an additional <i>axiom</i>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Axiom</span> <span class="id" type="var">functional_extensionality</span> : <span style="font-family: arial;">∀</span>{<span class="id" type="var">X</span> <span class="id" type="var">Y</span>: <span class="id" type="keyword">Type</span>} {<span class="id" type="var">f</span> <span class="id" type="var">g</span> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Y</span>},<br/>
(<span style="font-family: arial;">∀</span>(<span class="id" type="var">x</span>: <span class="id" type="var">X</span>), <span class="id" type="var">f</span> <span class="id" type="var">x</span> = <span class="id" type="var">g</span> <span class="id" type="var">x</span>) <span style="font-family: arial;">→</span> <span class="id" type="var">f</span> = <span class="id" type="var">g</span>.<br/>
<br/>
</div>
<div class="doc">
It can be shown 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 <span class="inlinecode"><span class="id" type="var">excluded_middle</span></span>.)
<div class="paragraph"> </div>
With the benefit of this axiom we can prove our theorem.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">identity_assignment</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">X</span>:<span class="id" type="var">id</span>),<br/>
<span class="id" type="var">cequiv</span><br/>
(<span class="id" type="var">X</span> ::= <span class="id" type="var">AId</span> <span class="id" type="var">X</span>)<br/>
<span class="id" type="var">SKIP</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span>. <span class="id" type="tactic">split</span>; <span class="id" type="tactic">intro</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">Case</span> "<span style="font-family: arial;">→</span>".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>; <span class="id" type="tactic">subst</span>. <span class="id" type="tactic">simpl</span>.<br/>
<span class="id" type="tactic">replace</span> (<span class="id" type="var">update</span> <span class="id" type="var">st</span> <span class="id" type="var">X</span> (<span class="id" type="var">st</span> <span class="id" type="var">X</span>)) <span class="id" type="keyword">with</span> <span class="id" type="var">st</span>.<br/>
<span class="id" type="var">constructor</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">functional_extensionality</span>. <span class="id" type="tactic">intro</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">update_same</span>; <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">Case</span> "<span style="font-family: arial;">←</span>".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>; <span class="id" type="tactic">subst</span>.<br/>
<span class="id" type="tactic">assert</span> (<span class="id" type="var">st'</span> = (<span class="id" type="var">update</span> <span class="id" type="var">st'</span> <span class="id" type="var">X</span> (<span class="id" type="var">st'</span> <span class="id" type="var">X</span>))).<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">functional_extensionality</span>. <span class="id" type="tactic">intro</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">update_same</span>; <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">H0</span> <span class="id" type="tactic">at</span> 2.<br/>
<span class="id" type="var">constructor</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab473"></a><h4 class="section">Exercise: 2 stars (assign_aequiv)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">assign_aequiv</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">X</span> <span class="id" type="var">e</span>,<br/>
<span class="id" type="var">aequiv</span> (<span class="id" type="var">AId</span> <span class="id" type="var">X</span>) <span class="id" type="var">e</span> <span style="font-family: arial;">→</span> <br/>
<span class="id" type="var">cequiv</span> <span class="id" type="var">SKIP</span> (<span class="id" type="var">X</span> ::= <span class="id" type="var">e</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab474"></a><h1 class="section">Properties of Behavioral Equivalence</h1>
<div class="paragraph"> </div>
We now turn to developing some of the properties of the program
equivalences we have defined.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab475"></a><h2 class="section">Behavioral Equivalence is an Equivalence</h2>
<div class="paragraph"> </div>
First, we verify that the equivalences on <span class="inlinecode"><span class="id" type="var">aexps</span></span>, <span class="inlinecode"><span class="id" type="var">bexps</span></span>, and
<span class="inlinecode"><span class="id" type="var">com</span></span>s really are <i>equivalences</i> — i.e., that they are reflexive,
symmetric, and transitive. The proofs are all easy.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">refl_aequiv</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">a</span> : <span class="id" type="var">aexp</span>), <span class="id" type="var">aequiv</span> <span class="id" type="var">a</span> <span class="id" type="var">a</span>.<br/>
<div class="togglescript" id="proofcontrol6" onclick="toggleDisplay('proof6');toggleDisplay('proofcontrol6')"><span class="show"></span></div>
<div class="proofscript" id="proof6" onclick="toggleDisplay('proof6');toggleDisplay('proofcontrol6')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">a</span> <span class="id" type="var">st</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">sym_aequiv</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">a1</span> <span class="id" type="var">a2</span> : <span class="id" type="var">aexp</span>), <br/>
<span class="id" type="var">aequiv</span> <span class="id" type="var">a1</span> <span class="id" type="var">a2</span> <span style="font-family: arial;">→</span> <span class="id" type="var">aequiv</span> <span class="id" type="var">a2</span> <span class="id" type="var">a1</span>.<br/>
<div class="togglescript" id="proofcontrol7" onclick="toggleDisplay('proof7');toggleDisplay('proofcontrol7')"><span class="show"></span></div>
<div class="proofscript" id="proof7" onclick="toggleDisplay('proof7');toggleDisplay('proofcontrol7')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">a1</span> <span class="id" type="var">a2</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">st</span>. <span class="id" type="tactic">symmetry</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">H</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">trans_aequiv</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">a1</span> <span class="id" type="var">a2</span> <span class="id" type="var">a3</span> : <span class="id" type="var">aexp</span>), <br/>
<span class="id" type="var">aequiv</span> <span class="id" type="var">a1</span> <span class="id" type="var">a2</span> <span style="font-family: arial;">→</span> <span class="id" type="var">aequiv</span> <span class="id" type="var">a2</span> <span class="id" type="var">a3</span> <span style="font-family: arial;">→</span> <span class="id" type="var">aequiv</span> <span class="id" type="var">a1</span> <span class="id" type="var">a3</span>.<br/>
<div class="togglescript" id="proofcontrol8" onclick="toggleDisplay('proof8');toggleDisplay('proofcontrol8')"><span class="show"></span></div>
<div class="proofscript" id="proof8" onclick="toggleDisplay('proof8');toggleDisplay('proofcontrol8')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">aequiv</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">a1</span> <span class="id" type="var">a2</span> <span class="id" type="var">a3</span> <span class="id" type="var">H12</span> <span class="id" type="var">H23</span> <span class="id" type="var">st</span>.<br/>
<span class="id" type="tactic">rewrite</span> (<span class="id" type="var">H12</span> <span class="id" type="var">st</span>). <span class="id" type="tactic">rewrite</span> (<span class="id" type="var">H23</span> <span class="id" type="var">st</span>). <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">refl_bequiv</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">b</span> : <span class="id" type="var">bexp</span>), <span class="id" type="var">bequiv</span> <span class="id" type="var">b</span> <span class="id" type="var">b</span>.<br/>
<div class="togglescript" id="proofcontrol9" onclick="toggleDisplay('proof9');toggleDisplay('proofcontrol9')"><span class="show"></span></div>
<div class="proofscript" id="proof9" onclick="toggleDisplay('proof9');toggleDisplay('proofcontrol9')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">bequiv</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">b</span> <span class="id" type="var">st</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">sym_bequiv</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">b1</span> <span class="id" type="var">b2</span> : <span class="id" type="var">bexp</span>), <br/>
<span class="id" type="var">bequiv</span> <span class="id" type="var">b1</span> <span class="id" type="var">b2</span> <span style="font-family: arial;">→</span> <span class="id" type="var">bequiv</span> <span class="id" type="var">b2</span> <span class="id" type="var">b1</span>.<br/>
<div class="togglescript" id="proofcontrol10" onclick="toggleDisplay('proof10');toggleDisplay('proofcontrol10')"><span class="show"></span></div>
<div class="proofscript" id="proof10" onclick="toggleDisplay('proof10');toggleDisplay('proofcontrol10')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">bequiv</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">b1</span> <span class="id" type="var">b2</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">st</span>. <span class="id" type="tactic">symmetry</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">H</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">trans_bequiv</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">b1</span> <span class="id" type="var">b2</span> <span class="id" type="var">b3</span> : <span class="id" type="var">bexp</span>), <br/>
<span class="id" type="var">bequiv</span> <span class="id" type="var">b1</span> <span class="id" type="var">b2</span> <span style="font-family: arial;">→</span> <span class="id" type="var">bequiv</span> <span class="id" type="var">b2</span> <span class="id" type="var">b3</span> <span style="font-family: arial;">→</span> <span class="id" type="var">bequiv</span> <span class="id" type="var">b1</span> <span class="id" type="var">b3</span>.<br/>
<div class="togglescript" id="proofcontrol11" onclick="toggleDisplay('proof11');toggleDisplay('proofcontrol11')"><span class="show"></span></div>
<div class="proofscript" id="proof11" onclick="toggleDisplay('proof11');toggleDisplay('proofcontrol11')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">bequiv</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">b1</span> <span class="id" type="var">b2</span> <span class="id" type="var">b3</span> <span class="id" type="var">H12</span> <span class="id" type="var">H23</span> <span class="id" type="var">st</span>.<br/>
<span class="id" type="tactic">rewrite</span> (<span class="id" type="var">H12</span> <span class="id" type="var">st</span>). <span class="id" type="tactic">rewrite</span> (<span class="id" type="var">H23</span> <span class="id" type="var">st</span>). <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">refl_cequiv</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">c</span> : <span class="id" type="var">com</span>), <span class="id" type="var">cequiv</span> <span class="id" type="var">c</span> <span class="id" type="var">c</span>.<br/>
<div class="togglescript" id="proofcontrol12" onclick="toggleDisplay('proof12');toggleDisplay('proofcontrol12')"><span class="show"></span></div>
<div class="proofscript" id="proof12" onclick="toggleDisplay('proof12');toggleDisplay('proofcontrol12')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">cequiv</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">iff_refl</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">sym_cequiv</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">c1</span> <span class="id" type="var">c2</span> : <span class="id" type="var">com</span>), <br/>
<span class="id" type="var">cequiv</span> <span class="id" type="var">c1</span> <span class="id" type="var">c2</span> <span style="font-family: arial;">→</span> <span class="id" type="var">cequiv</span> <span class="id" type="var">c2</span> <span class="id" type="var">c1</span>.<br/>
<div class="togglescript" id="proofcontrol13" onclick="toggleDisplay('proof13');toggleDisplay('proofcontrol13')"><span class="show"></span></div>
<div class="proofscript" id="proof13" onclick="toggleDisplay('proof13');toggleDisplay('proofcontrol13')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">cequiv</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">c1</span> <span class="id" type="var">c2</span> <span class="id" type="var">H</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span>.<br/>
<span class="id" type="tactic">assert</span> (<span class="id" type="var">c1</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st'</span> <span style="font-family: arial;">↔</span> <span class="id" type="var">c2</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st'</span>) <span class="id" type="keyword">as</span> <span class="id" type="var">H'</span>.<br/>
<span class="id" type="var">SCase</span> "Proof of assertion". <span class="id" type="tactic">apply</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">iff_sym</span>. <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
</div>