-
Notifications
You must be signed in to change notification settings - Fork 0
/
Smallstep.html
2686 lines (2167 loc) · 246 KB
/
Smallstep.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>Smallstep: Small-step Operational Semantics</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">Smallstep<span class="subtitle">Small-step Operational Semantics</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">
The evaluators we have seen so far (e.g., the ones for
<span class="inlinecode"><span class="id" type="var">aexp</span></span>s, <span class="inlinecode"><span class="id" type="var">bexp</span></span>s, and commands) have been formulated in a
"big-step" style — they specify how a given expression can be
evaluated to its final value (or a command plus a store to a final
store) "all in one big step."
<div class="paragraph"> </div>
This style is simple and natural for many purposes — indeed,
Gilles Kahn, who popularized its use, called it <i>natural
semantics</i>. But there are some things it does not do well. In
particular, it does not give us a natural way of talking about
<i>concurrent</i> programming languages, where the "semantics" of a
program — i.e., the essence of how it behaves — is not just
which input states get mapped to which output states, but also
includes the intermediate states that it passes through along the
way, since these states can also be observed by concurrently
executing code.
<div class="paragraph"> </div>
Another shortcoming of the big-step style is more technical, but
critical in some situations. To see the issue, suppose we wanted
to define a variant of Imp where variables could hold <i>either</i>
numbers <i>or</i> lists of numbers (see the <span class="inlinecode"><span class="id" type="var">HoareList</span></span> chapter for
details). In the syntax of this extended language, it will be
possible to write strange expressions like <span class="inlinecode">2</span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">nil</span></span>, and our
semantics for arithmetic expressions will then need to say
something about how such expressions behave. One
possibility (explored in the <span class="inlinecode"><span class="id" type="var">HoareList</span></span> chapter) is to maintain
the convention that every arithmetic expressions evaluates to some
number by choosing some way of viewing a list as a number — e.g.,
by specifying that a list should be interpreted as <span class="inlinecode">0</span> when it
occurs in a context expecting a number. But this is really a bit
of a hack.
<div class="paragraph"> </div>
A much more natural approach is simply to say that the behavior of
an expression like <span class="inlinecode">2+<span class="id" type="var">nil</span></span> is <i>undefined</i> — it doesn't evaluate
to any result at all. And we can easily do this: we just have to
formulate <span class="inlinecode"><span class="id" type="var">aeval</span></span> and <span class="inlinecode"><span class="id" type="var">beval</span></span> as <span class="inlinecode"><span class="id" type="keyword">Inductive</span></span> propositions rather
than Fixpoints, so that we can make them partial functions instead
of total ones.
<div class="paragraph"> </div>
However, now we encounter a serious deficiency. In this language,
a command might <i>fail</i> to map a given starting state to any ending
state for two quite different reasons: either because the
execution gets into an infinite loop or because, at some point,
the program tries to do an operation that makes no sense, such as
adding a number to a list, and none of the evaluation rules can be
applied.
<div class="paragraph"> </div>
These two outcomes — nontermination vs. getting stuck in an
erroneous configuration — are quite different. In particular, we
want to allow the first (permitting the possibility of infinite
loops is the price we pay for the convenience of programming with
general looping constructs like <span class="inlinecode"><span class="id" type="var">while</span></span>) but prevent the
second (which is just wrong), for example by adding some form of
<i>typechecking</i> to the language. Indeed, this will be a major
topic for the rest of the course. As a first step, we need a
different way of presenting the semantics that allows us to
distinguish nontermination from erroneous "stuck states."
<div class="paragraph"> </div>
So, for lots of reasons, we'd like to have a finer-grained way of
defining and reasoning about program behaviors. This is the topic
of the present chapter. We replace the "big-step" <span class="inlinecode"><span class="id" type="var">eval</span></span> relation
with a "small-step" relation that specifies, for a given program,
how the "atomic steps" of computation are performed.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab583"></a><h1 class="section">A Toy Language</h1>
<div class="paragraph"> </div>
To save space in the discussion, let's go back to an
incredibly simple language containing just constants and
addition. (We use single letters — <span class="inlinecode"><span class="id" type="var">C</span></span> and <span class="inlinecode"><span class="id" type="var">P</span></span> — for the
constructor names, for brevity.) At the end of the chapter, we'll
see how to apply the same techniques to the full Imp language.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">tm</span> : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">C</span> : <span class="id" type="var">nat</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span class="comment">(* Constant *)</span><br/>
| <span class="id" type="var">P</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span>. <span class="comment">(* Plus *)</span><br/>
<br/>
</div>
<div class="doc">
Here is a standard evaluator for this language, written in the
same (big-step) style as we've been using up to this point.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">evalF</span> (<span class="id" type="var">t</span> : <span class="id" type="var">tm</span>) : <span class="id" type="var">nat</span> :=<br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">t</span> <span class="id" type="keyword">with</span><br/>
| <span class="id" type="var">C</span> <span class="id" type="var">n</span> ⇒ <span class="id" type="var">n</span><br/>
| <span class="id" type="var">P</span> <span class="id" type="var">a1</span> <span class="id" type="var">a2</span> ⇒ <span class="id" type="var">evalF</span> <span class="id" type="var">a1</span> + <span class="id" type="var">evalF</span> <span class="id" type="var">a2</span><br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
</div>
<div class="doc">
Now, here is the same evaluator, written in exactly the same
style, but formulated as an inductively defined relation. Again,
we use the notation <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode"><span style="font-family: arial;">⇓</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> for "<span class="inlinecode"><span class="id" type="var">t</span></span> evaluates to <span class="inlinecode"><span class="id" type="var">n</span></span>."
<div class="paragraph"> </div>
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(E_Const)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">C n <span style="font-family: arial;">⇓</span> n</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub> <span style="font-family: arial;">⇓</span> n1</td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">t<sub>2</sub> <span style="font-family: arial;">⇓</span> n2</td>
<td class="infrulenamecol" rowspan="3">
(E_Plus)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">P t<sub>1</sub> t<sub>2</sub> <span style="font-family: arial;">⇓</span> n1 + n2</td>
<td></td>
</td>
</table></center>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Reserved Notation</span> " t '<span style="font-family: arial;">⇓</span>' n " (<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 50, <span class="id" type="var">left</span> <span class="id" type="var">associativity</span>).<br/>
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">eval</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">nat</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<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="var">E_Const</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>,<br/>
<span class="id" type="var">C</span> <span class="id" type="var">n</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">n</span><br/>
| <span class="id" type="var">E_Plus</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> <span class="id" type="var">n1</span> <span class="id" type="var">n2</span>, <br/>
<span class="id" type="var">t<sub>1</sub></span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">n1</span> <span style="font-family: arial;">→</span> <br/>
<span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">n2</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">P</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">⇓</span> (<span class="id" type="var">n1</span> + <span class="id" type="var">n2</span>)<br/>
<br/>
<span class="id" type="keyword">where</span> " t '<span style="font-family: arial;">⇓</span>' n " := (<span class="id" type="var">eval</span> <span class="id" type="var">t</span> <span class="id" type="var">n</span>).<br/>
</div>
<br/>
<br/>
</div>
<div class="doc">
Now, here is a small-step version.
<div class="paragraph"> </div>
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(ST_PlusConstConst)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">P (C n1) (C n2) <span style="font-family: arial;">⇒</span> C (n1 + n2)</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub> <span style="font-family: arial;">⇒</span> t<sub>1</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Plus1)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">P t<sub>1</sub> t<sub>2</sub> <span style="font-family: arial;">⇒</span> P t<sub>1</sub>' t<sub>2</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>2</sub> <span style="font-family: arial;">⇒</span> t<sub>2</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Plus2)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">P (C n1) t<sub>2</sub> <span style="font-family: arial;">⇒</span> P (C n1) t<sub>2</sub>'</td>
<td></td>
</td>
</table></center>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Reserved Notation</span> " t '<span style="font-family: arial;">⇒</span>' t' " (<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 40).<br/>
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">step</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">ST_PlusConstConst</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n1</span> <span class="id" type="var">n2</span>,<br/>
<span class="id" type="var">P</span> (<span class="id" type="var">C</span> <span class="id" type="var">n1</span>) (<span class="id" type="var">C</span> <span class="id" type="var">n2</span>) <span style="font-family: arial;">⇒</span> <span class="id" type="var">C</span> (<span class="id" type="var">n1</span> + <span class="id" type="var">n2</span>)<br/>
| <span class="id" type="var">ST_Plus1</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">t<sub>2</sub></span>,<br/>
<span class="id" type="var">t<sub>1</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t<sub>1</sub>'</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">P</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">P</span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">t<sub>2</sub></span><br/>
| <span class="id" type="var">ST_Plus2</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n1</span> <span class="id" type="var">t<sub>2</sub></span> <span class="id" type="var">t<sub>2</sub>'</span>,<br/>
<span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t<sub>2</sub>'</span> <span style="font-family: arial;">→</span> <br/>
<span class="id" type="var">P</span> (<span class="id" type="var">C</span> <span class="id" type="var">n1</span>) <span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">P</span> (<span class="id" type="var">C</span> <span class="id" type="var">n1</span>) <span class="id" type="var">t<sub>2</sub>'</span><br/>
<br/>
<span class="id" type="keyword">where</span> " t '<span style="font-family: arial;">⇒</span>' t' " := (<span class="id" type="var">step</span> <span class="id" type="var">t</span> <span class="id" type="var">t'</span>).<br/>
<br/>
</div>
<div class="doc">
Things to notice:
<div class="paragraph"> </div>
<ul class="doclist">
<li> We are defining just a single reduction step, in which
one <span class="inlinecode"><span class="id" type="var">P</span></span> node is replaced by its value.
<div class="paragraph"> </div>
</li>
<li> Each step finds the <i>leftmost</i> <span class="inlinecode"><span class="id" type="var">P</span></span> node that is ready to
go (both of its operands are constants) and rewrites it in
place. The first rule tells how to rewrite this <span class="inlinecode"><span class="id" type="var">P</span></span> node
itself; the other two rules tell how to find it.
<div class="paragraph"> </div>
</li>
<li> A term that is just a constant cannot take a step.
</li>
</ul>
<div class="paragraph"> </div>
Let's pause and check a couple of examples of reasoning with
the <span class="inlinecode"><span class="id" type="var">step</span></span> relation...
<div class="paragraph"> </div>
If <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> can take a step to <span class="inlinecode"><span class="id" type="var">t<sub>1</sub>'</span></span>, then <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> steps
to <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub>'</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span>:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_step_1</span> : <br/>
<span class="id" type="var">P</span> <br/>
(<span class="id" type="var">P</span> (<span class="id" type="var">C</span> 0) (<span class="id" type="var">C</span> 3))<br/>
(<span class="id" type="var">P</span> (<span class="id" type="var">C</span> 2) (<span class="id" type="var">C</span> 4))<br/>
<span style="font-family: arial;">⇒</span><br/>
<span class="id" type="var">P</span> <br/>
(<span class="id" type="var">C</span> (0 + 3))<br/>
(<span class="id" type="var">P</span> (<span class="id" type="var">C</span> 2) (<span class="id" type="var">C</span> 4)).<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">apply</span> <span class="id" type="var">ST_Plus1</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">ST_PlusConstConst</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
<a name="lab584"></a><h4 class="section">Exercise: 1 star (test_step_2)</h4>
Right-hand sides of sums can take a step only when the
left-hand side is finished: if <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> can take a step to <span class="inlinecode"><span class="id" type="var">t<sub>2</sub>'</span></span>,
then <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode">(<span class="id" type="var">C</span></span> <span class="inlinecode"><span class="id" type="var">n</span>)</span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> steps to <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode">(<span class="id" type="var">C</span></span> <span class="inlinecode"><span class="id" type="var">n</span>)</span>
<span class="inlinecode"><span class="id" type="var">t<sub>2</sub>'</span></span>:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">test_step_2</span> : <br/>
<span class="id" type="var">P</span> <br/>
(<span class="id" type="var">C</span> 0)<br/>
(<span class="id" type="var">P</span> <br/>
(<span class="id" type="var">C</span> 2) <br/>
(<span class="id" type="var">P</span> (<span class="id" type="var">C</span> 0) (<span class="id" type="var">C</span> 3)))<br/>
<span style="font-family: arial;">⇒</span><br/>
<span class="id" type="var">P</span> <br/>
(<span class="id" type="var">C</span> 0)<br/>
(<span class="id" type="var">P</span> <br/>
(<span class="id" type="var">C</span> 2) <br/>
(<span class="id" type="var">C</span> (0 + 3))).<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="lab585"></a><h1 class="section">Relations</h1>
<div class="paragraph"> </div>
We will be using several different step relations, so it is
helpful to generalize a bit and state a few definitions and
theorems about relations in general. (The optional chapter
<span class="inlinecode"><span class="id" type="var">Rel.v</span></span> develops some of these ideas in a bit more detail; it may
be useful if the treatment here is too dense.)
<div class="paragraph"> </div>
A (binary) <i>relation</i> on a set <span class="inlinecode"><span class="id" type="var">X</span></span> is a family of propositions
parameterized by two elements of <span class="inlinecode"><span class="id" type="var">X</span></span> — i.e., a proposition about
pairs of elements of <span class="inlinecode"><span class="id" type="var">X</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">relation</span> (<span class="id" type="var">X</span>: <span class="id" type="keyword">Type</span>) := <span class="id" type="var">X</span><span style="font-family: arial;">→</span><span class="id" type="var">X</span><span style="font-family: arial;">→</span><span class="id" type="keyword">Prop</span>.<br/>
<br/>
</div>
<div class="doc">
Our main examples of such relations in this chapter will be
the single-step and multi-step reduction relations on terms, <span class="inlinecode"><span style="font-family: arial;">⇒</span></span>
and <span class="inlinecode"><span style="font-family: arial;">⇒*</span></span>, but there are many other examples — some that come to
mind are the "equals," "less than," "less than or equal to," and
"is the square of" relations on numbers, and the "prefix of"
relation on lists and strings.
<div class="paragraph"> </div>
One simple property of the <span class="inlinecode"><span style="font-family: arial;">⇒</span></span> relation is that, like the
evaluation relation for our language of Imp programs, it is
<i>deterministic</i>.
<div class="paragraph"> </div>
<i>Theorem</i>: For each <span class="inlinecode"><span class="id" type="var">t</span></span>, there is at most one <span class="inlinecode"><span class="id" type="var">t'</span></span> such that <span class="inlinecode"><span class="id" type="var">t</span></span>
steps to <span class="inlinecode"><span class="id" type="var">t'</span></span> (<span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode"><span style="font-family: arial;">⇒</span></span> <span class="inlinecode"><span class="id" type="var">t'</span></span> is provable). Formally, this is the
same as saying that <span class="inlinecode"><span style="font-family: arial;">⇒</span></span> is deterministic.
<div class="paragraph"> </div>
<i>Proof sketch</i>: We show that if <span class="inlinecode"><span class="id" type="var">x</span></span> steps to both <span class="inlinecode"><span class="id" type="var">y1</span></span> and <span class="inlinecode"><span class="id" type="var">y2</span></span>
then <span class="inlinecode"><span class="id" type="var">y1</span></span> and <span class="inlinecode"><span class="id" type="var">y2</span></span> are equal, by induction on a derivation of
<span class="inlinecode"><span class="id" type="var">step</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">y1</span></span>. There are several cases to consider, depending on
the last rule used in this derivation and in the given derivation
of <span class="inlinecode"><span class="id" type="var">step</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">y2</span></span>.
<div class="paragraph"> </div>
<ul class="doclist">
<li> If both are <span class="inlinecode"><span class="id" type="var">ST_PlusConstConst</span></span>, the result is immediate.
<div class="paragraph"> </div>
</li>
<li> The cases when both derivations end with <span class="inlinecode"><span class="id" type="var">ST_Plus1</span></span> or
<span class="inlinecode"><span class="id" type="var">ST_Plus2</span></span> follow by the induction hypothesis.
<div class="paragraph"> </div>
</li>
<li> It cannot happen that one is <span class="inlinecode"><span class="id" type="var">ST_PlusConstConst</span></span> and the other
is <span class="inlinecode"><span class="id" type="var">ST_Plus1</span></span> or <span class="inlinecode"><span class="id" type="var">ST_Plus2</span></span>, since this would imply that <span class="inlinecode"><span class="id" type="var">x</span></span> has
the form <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> where both <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> and <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> are
constants (by <span class="inlinecode"><span class="id" type="var">ST_PlusConstConst</span></span>) <i>and</i> one of <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> or <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> has
the form <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode">...</span>.
<div class="paragraph"> </div>
</li>
<li> Similarly, it cannot happen that one is <span class="inlinecode"><span class="id" type="var">ST_Plus1</span></span> and the other
is <span class="inlinecode"><span class="id" type="var">ST_Plus2</span></span>, since this would imply that <span class="inlinecode"><span class="id" type="var">x</span></span> has the form
<span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> where <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> has both the form <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> and
the form <span class="inlinecode"><span class="id" type="var">C</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span>. <font size=-2>☐</font>
</li>
</ul>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">deterministic</span> {<span class="id" type="var">X</span>: <span class="id" type="keyword">Type</span>} (<span class="id" type="var">R</span>: <span class="id" type="var">relation</span> <span class="id" type="var">X</span>) :=<br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">x</span> <span class="id" type="var">y1</span> <span class="id" type="var">y2</span> : <span class="id" type="var">X</span>, <span class="id" type="var">R</span> <span class="id" type="var">x</span> <span class="id" type="var">y1</span> <span style="font-family: arial;">→</span> <span class="id" type="var">R</span> <span class="id" type="var">x</span> <span class="id" type="var">y2</span> <span style="font-family: arial;">→</span> <span class="id" type="var">y1</span> = <span class="id" type="var">y2</span>.<br/>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">step_deterministic</span>:<br/>
<span class="id" type="var">deterministic</span> <span class="id" type="var">step</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">unfold</span> <span class="id" type="var">deterministic</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">x</span> <span class="id" type="var">y1</span> <span class="id" type="var">y2</span> <span class="id" type="var">Hy1</span> <span class="id" type="var">Hy2</span>.<br/>
<span class="id" type="tactic">generalize</span> <span class="id" type="tactic">dependent</span> <span class="id" type="var">y2</span>.<br/>
<span class="id" type="tactic">induction</span> <span class="id" type="var">Hy1</span>; <span class="id" type="tactic">intros</span> <span class="id" type="var">y2</span> <span class="id" type="var">Hy2</span>.<br/>
- <span class="comment">(* ST_PlusConstConst *)</span> <span class="id" type="tactic">inversion</span> <span class="id" type="var">Hy2</span>.<br/>
+ <span class="comment">(* ST_PlusConstConst *)</span> <span class="id" type="tactic">reflexivity</span>.<br/>
+ <span class="comment">(* ST_Plus1 *)</span> <span class="id" type="tactic">inversion</span> <span class="id" type="var">H2</span>.<br/>
+ <span class="comment">(* ST_Plus2 *)</span> <span class="id" type="tactic">inversion</span> <span class="id" type="var">H2</span>.<br/>
- <span class="comment">(* ST_Plus1 *)</span> <span class="id" type="tactic">inversion</span> <span class="id" type="var">Hy2</span>.<br/>
+ <span class="comment">(* ST_PlusConstConst *)</span> <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">←</span> <span class="id" type="var">H0</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hy1</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">Hy1</span>.<br/>
+ <span class="comment">(* ST_Plus1 *)</span><br/>
<span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">←</span> (<span class="id" type="var">IHHy1</span> <span class="id" type="var">t<sub>1</sub>'0</span>).<br/>
<span class="id" type="tactic">reflexivity</span>. <span class="id" type="tactic">assumption</span>.<br/>
+ <span class="comment">(* ST_Plus2 *)</span> <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">←</span> <span class="id" type="var">H</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hy1</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">Hy1</span>.<br/>
- <span class="comment">(* ST_Plus2 *)</span> <span class="id" type="tactic">inversion</span> <span class="id" type="var">Hy2</span>.<br/>
+ <span class="comment">(* ST_PlusConstConst *)</span> <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">←</span> <span class="id" type="var">H1</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hy1</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">Hy1</span>.<br/>
+ <span class="comment">(* ST_Plus1 *)</span> <span class="id" type="tactic">inversion</span> <span class="id" type="var">H2</span>.<br/>
+ <span class="comment">(* ST_Plus2 *)</span><br/>
<span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">←</span> (<span class="id" type="var">IHHy1</span> <span class="id" type="var">t<sub>2</sub>'0</span>).<br/>
<span class="id" type="tactic">reflexivity</span>. <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
There is some annoying repetition in this proof.
Each use of <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> <span class="inlinecode"><span class="id" type="var">Hy2</span></span> results in three subcases,
only one of which is relevant (the one which matches the
current case in the induction on <span class="inlinecode"><span class="id" type="var">Hy1</span></span>). The other two
subcases need to be dismissed by finding the contradiction
among the hypotheses and doing inversion on it.
<div class="paragraph"> </div>
There is a tactic called <span class="inlinecode"><span class="id" type="var">solve</span></span> <span class="inlinecode"><span class="id" type="tactic">by</span></span> <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> defined in <span class="inlinecode"><span class="id" type="var">SfLib.v</span></span>
that can be of use in such cases. It will solve the goal if it
can be solved by inverting some hypothesis; otherwise, it fails.
(There are variants <span class="inlinecode"><span class="id" type="var">solve</span></span> <span class="inlinecode"><span class="id" type="tactic">by</span></span> <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> <span class="inlinecode">2</span> and <span class="inlinecode"><span class="id" type="var">solve</span></span> <span class="inlinecode"><span class="id" type="tactic">by</span></span> <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> <span class="inlinecode">3</span>
that work if two or three consecutive inversions will solve the goal.)
<div class="paragraph"> </div>
The example below shows how a proof of the previous theorem can be
simplified using this tactic.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">step_deterministic_alt</span>: <span class="id" type="var">deterministic</span> <span class="id" type="var">step</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">x</span> <span class="id" type="var">y1</span> <span class="id" type="var">y2</span> <span class="id" type="var">Hy1</span> <span class="id" type="var">Hy2</span>.<br/>
<span class="id" type="tactic">generalize</span> <span class="id" type="tactic">dependent</span> <span class="id" type="var">y2</span>.<br/>
<span class="id" type="tactic">induction</span> <span class="id" type="var">Hy1</span>; <span class="id" type="tactic">intros</span> <span class="id" type="var">y2</span> <span class="id" type="var">Hy2</span>; <br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">Hy2</span>; <span class="id" type="tactic">subst</span>; <span class="id" type="tactic">try</span> (<span class="id" type="var">solve</span> <span class="id" type="tactic">by</span> <span class="id" type="tactic">inversion</span>).<br/>
- <span class="comment">(* ST_PlusConstConst *)</span> <span class="id" type="tactic">reflexivity</span>.<br/>
- <span class="comment">(* ST_Plus1 *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHHy1</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H2</span>. <span class="id" type="tactic">rewrite</span> <span class="id" type="var">H2</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
- <span class="comment">(* ST_Plus2 *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHHy1</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H2</span>. <span class="id" type="tactic">rewrite</span> <span class="id" type="var">H2</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
<span class="id" type="keyword">End</span> <span class="id" type="var">SimpleArith1</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab586"></a><h2 class="section">Values</h2>
<div class="paragraph"> </div>
Let's take a moment to slightly generalize the way we state the
definition of single-step reduction.
<div class="paragraph"> </div>
It is useful to think of the <span class="inlinecode"><span style="font-family: arial;">⇒</span></span> relation as defining an
<i>abstract machine</i>:
<div class="paragraph"> </div>
<ul class="doclist">
<li> At any moment, the <i>state</i> of the machine is a term.
<div class="paragraph"> </div>
</li>
<li> A <i>step</i> of the machine is an atomic unit of computation —
here, a single "add" operation.
<div class="paragraph"> </div>
</li>
<li> The <i>halting states</i> of the machine are ones where there is no
more computation to be done.
</li>
</ul>
<div class="paragraph"> </div>
We can then execute a term <span class="inlinecode"><span class="id" type="var">t</span></span> as follows:
<div class="paragraph"> </div>
<ul class="doclist">
<li> Take <span class="inlinecode"><span class="id" type="var">t</span></span> as the starting state of the machine.
<div class="paragraph"> </div>
</li>
<li> Repeatedly use the <span class="inlinecode"><span style="font-family: arial;">⇒</span></span> relation to find a sequence of
machine states, starting with <span class="inlinecode"><span class="id" type="var">t</span></span>, where each state steps to
the next.
<div class="paragraph"> </div>
</li>
<li> When no more reduction is possible, "read out" the final state
of the machine as the result of execution.
</li>
</ul>
<div class="paragraph"> </div>
Intuitively, it is clear that the final states of the
machine are always terms of the form <span class="inlinecode"><span class="id" type="var">C</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> for some <span class="inlinecode"><span class="id" type="var">n</span></span>.
We call such terms <i>values</i>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">value</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
<span class="id" type="var">v_const</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>, <span class="id" type="var">value</span> (<span class="id" type="var">C</span> <span class="id" type="var">n</span>).<br/>
<br/>
</div>
<div class="doc">
Having introduced the idea of values, we can use it in the
definition of the <span class="inlinecode"><span style="font-family: arial;">⇒</span></span> relation to write <span class="inlinecode"><span class="id" type="var">ST_Plus2</span></span> rule in a
slightly more elegant way:
<div class="paragraph"> </div>
<div class="paragraph"> </div>
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(ST_PlusConstConst)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">P (C n1) (C n2) <span style="font-family: arial;">⇒</span> C (n1 + n2)</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub> <span style="font-family: arial;">⇒</span> t<sub>1</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Plus1)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">P t<sub>1</sub> t<sub>2</sub> <span style="font-family: arial;">⇒</span> P t<sub>1</sub>' t<sub>2</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">value v<sub>1</sub></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">t<sub>2</sub> <span style="font-family: arial;">⇒</span> t<sub>2</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Plus2)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">P v<sub>1</sub> t<sub>2</sub> <span style="font-family: arial;">⇒</span> P v<sub>1</sub> t<sub>2</sub>'</td>
<td></td>
</td>
</table></center> Again, the variable names here carry important information:
by convention, <span class="inlinecode"><span class="id" type="var">v<sub>1</sub></span></span> ranges only over values, while <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> and <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span>
range over arbitrary terms. (Given this convention, the explicit
<span class="inlinecode"><span class="id" type="var">value</span></span> hypothesis is arguably redundant. We'll keep it for now,
to maintain a close correspondence between the informal and Coq
versions of the rules, but later on we'll drop it in informal
rules, for the sake of brevity.)
<div class="paragraph"> </div>
Here are the formal rules:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Reserved Notation</span> " t '<span style="font-family: arial;">⇒</span>' t' " (<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 40).<br/>
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">step</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">ST_PlusConstConst</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n1</span> <span class="id" type="var">n2</span>,<br/>
<span class="id" type="var">P</span> (<span class="id" type="var">C</span> <span class="id" type="var">n1</span>) (<span class="id" type="var">C</span> <span class="id" type="var">n2</span>)<br/>
<span style="font-family: arial;">⇒</span> <span class="id" type="var">C</span> (<span class="id" type="var">n1</span> + <span class="id" type="var">n2</span>)<br/>
| <span class="id" type="var">ST_Plus1</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">t<sub>2</sub></span>,<br/>
<span class="id" type="var">t<sub>1</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t<sub>1</sub>'</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">P</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">P</span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">t<sub>2</sub></span><br/>
| <span class="id" type="var">ST_Plus2</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">v<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> <span class="id" type="var">t<sub>2</sub>'</span>,<br/>
<span class="id" type="var">value</span> <span class="id" type="var">v<sub>1</sub></span> <span style="font-family: arial;">→</span> <span class="comment">(* <----- n.b. *)</span><br/>
<span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t<sub>2</sub>'</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">P</span> <span class="id" type="var">v<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">P</span> <span class="id" type="var">v<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub>'</span><br/>
<br/>
<span class="id" type="keyword">where</span> " t '<span style="font-family: arial;">⇒</span>' t' " := (<span class="id" type="var">step</span> <span class="id" type="var">t</span> <span class="id" type="var">t'</span>).<br/>
<br/>
</div>
<div class="doc">
<a name="lab587"></a><h4 class="section">Exercise: 3 stars (redo_determinism)</h4>
As a sanity check on this change, let's re-verify determinism
<div class="paragraph"> </div>
Proof sketch: We must show that if <span class="inlinecode"><span class="id" type="var">x</span></span> steps to both <span class="inlinecode"><span class="id" type="var">y1</span></span> and <span class="inlinecode"><span class="id" type="var">y2</span></span>
then <span class="inlinecode"><span class="id" type="var">y1</span></span> and <span class="inlinecode"><span class="id" type="var">y2</span></span> are equal. Consider the final rules used in
the derivations of <span class="inlinecode"><span class="id" type="var">step</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">y1</span></span> and <span class="inlinecode"><span class="id" type="var">step</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">y2</span></span>.
<div class="paragraph"> </div>
<ul class="doclist">
<li> If both are <span class="inlinecode"><span class="id" type="var">ST_PlusConstConst</span></span>, the result is immediate.
<div class="paragraph"> </div>
</li>
<li> It cannot happen that one is <span class="inlinecode"><span class="id" type="var">ST_PlusConstConst</span></span> and the other
is <span class="inlinecode"><span class="id" type="var">ST_Plus1</span></span> or <span class="inlinecode"><span class="id" type="var">ST_Plus2</span></span>, since this would imply that <span class="inlinecode"><span class="id" type="var">x</span></span> has
the form <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> where both <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> and <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> are
constants (by <span class="inlinecode"><span class="id" type="var">ST_PlusConstConst</span></span>) AND one of <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> or <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> has
the form <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode">...</span>.
<div class="paragraph"> </div>
</li>
<li> Similarly, it cannot happen that one is <span class="inlinecode"><span class="id" type="var">ST_Plus1</span></span> and the other
is <span class="inlinecode"><span class="id" type="var">ST_Plus2</span></span>, since this would imply that <span class="inlinecode"><span class="id" type="var">x</span></span> has the form
<span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> where <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> both has the form <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> and
is a value (hence has the form <span class="inlinecode"><span class="id" type="var">C</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span>).
<div class="paragraph"> </div>
</li>
<li> The cases when both derivations end with <span class="inlinecode"><span class="id" type="var">ST_Plus1</span></span> or
<span class="inlinecode"><span class="id" type="var">ST_Plus2</span></span> follow by the induction hypothesis. <font size=-2>☐</font>
</li>
</ul>
<div class="paragraph"> </div>
Most of this proof is the same as the one above. But to get
maximum benefit from the exercise you should try to write it from
scratch and just use the earlier one if you get stuck.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">step_deterministic</span> :<br/>
<span class="id" type="var">deterministic</span> <span class="id" type="var">step</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="lab588"></a><h2 class="section">Strong Progress and Normal Forms</h2>
<div class="paragraph"> </div>
The definition of single-step reduction for our toy language is
fairly simple, but for a larger language it would be pretty easy
to forget one of the rules and create a situation where some term
cannot take a step even though it has not been completely reduced
to a value. The following theorem shows that we did not, in fact,
make such a mistake here.
<div class="paragraph"> </div>
<i>Theorem</i> (<i>Strong Progress</i>): If <span class="inlinecode"><span class="id" type="var">t</span></span> is a term, then either <span class="inlinecode"><span class="id" type="var">t</span></span>
is a value, or there exists a term <span class="inlinecode"><span class="id" type="var">t'</span></span> such that <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode"><span style="font-family: arial;">⇒</span></span> <span class="inlinecode"><span class="id" type="var">t'</span></span>.
<div class="paragraph"> </div>
<i>Proof</i>: By induction on <span class="inlinecode"><span class="id" type="var">t</span></span>.
<div class="paragraph"> </div>
<ul class="doclist">
<li> Suppose <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">C</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span>. Then <span class="inlinecode"><span class="id" type="var">t</span></span> is a <span class="inlinecode"><span class="id" type="var">value</span></span>.
<div class="paragraph"> </div>
</li>
<li> Suppose <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span>, where (by the IH) <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> is either a
value or can step to some <span class="inlinecode"><span class="id" type="var">t<sub>1</sub>'</span></span>, and where <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> is either a
value or can step to some <span class="inlinecode"><span class="id" type="var">t<sub>2</sub>'</span></span>. We must show <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> is
either a value or steps to some <span class="inlinecode"><span class="id" type="var">t'</span></span>.
<div class="paragraph"> </div>
<ul class="doclist">
<li> If <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> and <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> are both values, then <span class="inlinecode"><span class="id" type="var">t</span></span> can take a step, by
<span class="inlinecode"><span class="id" type="var">ST_PlusConstConst</span></span>.
<div class="paragraph"> </div>
</li>
<li> If <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> is a value and <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> can take a step, then so can <span class="inlinecode"><span class="id" type="var">t</span></span>,
by <span class="inlinecode"><span class="id" type="var">ST_Plus2</span></span>.
<div class="paragraph"> </div>
</li>
<li> If <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> can take a step, then so can <span class="inlinecode"><span class="id" type="var">t</span></span>, by <span class="inlinecode"><span class="id" type="var">ST_Plus1</span></span>. <font size=-2>☐</font>
</li>
</ul>
</li>
</ul>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">strong_progress</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</span>,<br/>
<span class="id" type="var">value</span> <span class="id" type="var">t</span> <span style="font-family: arial;">∨</span> (<span style="font-family: arial;">∃</span><span class="id" type="var">t'</span>, <span class="id" type="var">t</span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t'</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="keyword">Proof</span>.<br/>
<span class="id" type="tactic">induction</span> <span class="id" type="var">t</span>.<br/>
- <span class="comment">(* C *)</span> <span class="id" type="var">left</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">v_const</span>.<br/>
- <span class="comment">(* P *)</span> <span class="id" type="var">right</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">IHt1</span>.<br/>
+ <span class="comment">(* l *)</span> <span class="id" type="tactic">inversion</span> <span class="id" type="var">IHt2</span>.<br/>
× <span class="comment">(* l *)</span> <span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">H0</span>.<br/>
<span style="font-family: arial;">∃</span>(<span class="id" type="var">C</span> (<span class="id" type="var">n</span> + <span class="id" type="var">n0</span>)).<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">ST_PlusConstConst</span>.<br/>
× <span class="comment">(* r *)</span> <span class="id" type="tactic">inversion</span> <span class="id" type="var">H0</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">t'</span> <span class="id" type="var">H1</span>].<br/>
<span style="font-family: arial;">∃</span>(<span class="id" type="var">P</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t'</span>).<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">ST_Plus2</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">H1</span>.<br/>
+ <span class="comment">(* r *)</span> <span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">t'</span> <span class="id" type="var">H0</span>].<br/>
<span style="font-family: arial;">∃</span>(<span class="id" type="var">P</span> <span class="id" type="var">t'</span> <span class="id" type="var">t<sub>2</sub></span>).<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">ST_Plus1</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">H0</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
This important property is called <i>strong progress</i>, because
every term either is a value or can "make progress" by stepping to
some other term. (The qualifier "strong" distinguishes it from a
more refined version that we'll see in later chapters, called
simply "progress.")
<div class="paragraph"> </div>
The idea of "making progress" can be extended to tell us something
interesting about <span class="inlinecode"><span class="id" type="var">value</span></span>s: in this language <span class="inlinecode"><span class="id" type="var">value</span></span>s are exactly
the terms that <i>cannot</i> make progress in this sense.
<div class="paragraph"> </div>
To state this observation formally, let's begin by giving a name
to terms that cannot make progress. We'll call them <i>normal
forms</i>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">normal_form</span> {<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>} (<span class="id" type="var">R</span>:<span class="id" type="var">relation</span> <span class="id" type="var">X</span>) (<span class="id" type="var">t</span>:<span class="id" type="var">X</span>) : <span class="id" type="keyword">Prop</span> :=<br/>
¬ <span style="font-family: arial;">∃</span><span class="id" type="var">t'</span>, <span class="id" type="var">R</span> <span class="id" type="var">t</span> <span class="id" type="var">t'</span>.<br/>
<br/>
</div>
<div class="doc">
This definition actually specifies what it is to be a normal form
for an <i>arbitrary</i> relation <span class="inlinecode"><span class="id" type="var">R</span></span> over an arbitrary set <span class="inlinecode"><span class="id" type="var">X</span></span>, not
just for the particular single-step reduction relation over terms
that we are interested in at the moment. We'll re-use the same
terminology for talking about other relations later in the
course.
<div class="paragraph"> </div>
We can use this terminology to generalize the observation we made
in the strong progress theorem: in this language, normal forms and
values are actually the same thing.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">value_is_nf</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">v</span>,<br/>
<span class="id" type="var">value</span> <span class="id" type="var">v</span> <span style="font-family: arial;">→</span> <span class="id" type="var">normal_form</span> <span class="id" type="var">step</span> <span class="id" type="var">v</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">unfold</span> <span class="id" type="var">normal_form</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">v</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="tactic">intros</span> <span class="id" type="var">contra</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">contra</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">H1</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">nf_is_value</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</span>,<br/>
<span class="id" type="var">normal_form</span> <span class="id" type="var">step</span> <span class="id" type="var">t</span> <span style="font-family: arial;">→</span> <span class="id" type="var">value</span> <span class="id" type="var">t</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>. <span class="comment">(* a corollary of <span class="inlinecode"><span class="id" type="var">strong_progress</span></span>... *)</span><br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">normal_form</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">t</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">assert</span> (<span class="id" type="var">G</span> : <span class="id" type="var">value</span> <span class="id" type="var">t</span> <span style="font-family: arial;">∨</span> <span style="font-family: arial;">∃</span><span class="id" type="var">t'</span>, <span class="id" type="var">t</span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t'</span>).<br/>
{ <span class="comment">(* Proof of assertion *)</span> <span class="id" type="tactic">apply</span> <span class="id" type="var">strong_progress</span>. }<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">G</span>.<br/>
+ <span class="comment">(* l *)</span> <span class="id" type="tactic">apply</span> <span class="id" type="var">H0</span>.<br/>
+ <span class="comment">(* r *)</span> <span class="id" type="tactic">apply</span> <span class="id" type="var">ex_falso_quodlibet</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">assumption</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
<span class="id" type="keyword">Corollary</span> <span class="id" type="var">nf_same_as_value</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</span>,<br/>
<span class="id" type="var">normal_form</span> <span class="id" type="var">step</span> <span class="id" type="var">t</span> <span style="font-family: arial;">↔</span> <span class="id" type="var">value</span> <span class="id" type="var">t</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">split</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">nf_is_value</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">value_is_nf</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
Why is this interesting?
<div class="paragraph"> </div>
Because <span class="inlinecode"><span class="id" type="var">value</span></span> is a syntactic concept — it is defined by looking
at the form of a term — while <span class="inlinecode"><span class="id" type="var">normal_form</span></span> is a semantic one —
it is defined by looking at how the term steps. It is not obvious
that these concepts should coincide!
<div class="paragraph"> </div>
Indeed, we could easily have written the definitions so that they
would not coincide...
</div>
<div class="code code-tight">
<br/>
<br/>
</div>
<div class="doc">
We might, for example, mistakenly define <span class="inlinecode"><span class="id" type="var">value</span></span> so that it
includes some terms that are not finished reducing.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Module</span> <span class="id" type="var">Temp1</span>.<br/>
<span class="comment">(* Open an inner module so we can redefine value and step. *)</span><br/>
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">value</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">v_const</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>, <span class="id" type="var">value</span> (<span class="id" type="var">C</span> <span class="id" type="var">n</span>)<br/>
| <span class="id" type="var">v_funny</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">n2</span>, <span class="comment">(* <---- *)</span><br/>
<span class="id" type="var">value</span> (<span class="id" type="var">P</span> <span class="id" type="var">t<sub>1</sub></span> (<span class="id" type="var">C</span> <span class="id" type="var">n2</span>)).<br/>
<br/>
<span class="id" type="keyword">Reserved Notation</span> " t '<span style="font-family: arial;">⇒</span>' t' " (<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 40).<br/>
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">step</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">ST_PlusConstConst</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n1</span> <span class="id" type="var">n2</span>,<br/>
<span class="id" type="var">P</span> (<span class="id" type="var">C</span> <span class="id" type="var">n1</span>) (<span class="id" type="var">C</span> <span class="id" type="var">n2</span>) <span style="font-family: arial;">⇒</span> <span class="id" type="var">C</span> (<span class="id" type="var">n1</span> + <span class="id" type="var">n2</span>)<br/>
| <span class="id" type="var">ST_Plus1</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">t<sub>2</sub></span>,<br/>
<span class="id" type="var">t<sub>1</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t<sub>1</sub>'</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">P</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">P</span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">t<sub>2</sub></span><br/>
| <span class="id" type="var">ST_Plus2</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">v<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> <span class="id" type="var">t<sub>2</sub>'</span>,<br/>
<span class="id" type="var">value</span> <span class="id" type="var">v<sub>1</sub></span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t<sub>2</sub>'</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">P</span> <span class="id" type="var">v<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">P</span> <span class="id" type="var">v<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub>'</span><br/>
<br/>
<span class="id" type="keyword">where</span> " t '<span style="font-family: arial;">⇒</span>' t' " := (<span class="id" type="var">step</span> <span class="id" type="var">t</span> <span class="id" type="var">t'</span>).<br/>
<br/>
</div>
<div class="doc">
<a name="lab589"></a><h4 class="section">Exercise: 3 stars, advanced (value_not_same_as_normal_form)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">value_not_same_as_normal_form</span> :<br/>
<span style="font-family: arial;">∃</span><span class="id" type="var">v</span>, <span class="id" type="var">value</span> <span class="id" type="var">v</span> <span style="font-family: arial;">∧</span> ¬ <span class="id" type="var">normal_form</span> <span class="id" type="var">step</span> <span class="id" type="var">v</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">