-
Notifications
You must be signed in to change notification settings - Fork 1
/
Types.html
1450 lines (1175 loc) · 97.2 KB
/
Types.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>Types: Type Systems</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">Types<span class="subtitle">Type Systems</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">Smallstep</span>.<br/>
<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="var">Constructors</span> <span class="id" type="var">multi</span>.<br/>
<br/>
</div>
<div class="doc">
Our next major topic is <i>type systems</i> — static program
analyses that classify expressions according to the "shapes" of
their results. We'll begin with a typed version of a very simple
language with just booleans and numbers, to introduce the basic
ideas of types, typing rules, and the fundamental theorems about
type systems: <i>type preservation</i> and <i>progress</i>. Then we'll move
on to the <i>simply typed lambda-calculus</i>, which lives at the core
of every modern functional programming language (including
Coq).
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab626"></a><h1 class="section">Typed Arithmetic Expressions</h1>
<div class="paragraph"> </div>
To motivate the discussion of type systems, let's begin as
usual with an extremely simple toy language. We want it to have
the potential for programs "going wrong" because of runtime type
errors, so we need something a tiny bit more complex than the
language of constants and addition that we used in chapter
<span class="inlinecode"><span class="id" type="var">Smallstep</span></span>: a single kind of data (just numbers) is too simple,
but just two kinds (numbers and booleans) already gives us enough
material to tell an interesting story.
<div class="paragraph"> </div>
The language definition is completely routine. The only thing to
notice is that we are <i>not</i> using the <span class="inlinecode"><span class="id" type="var">asnum</span></span>/<span class="inlinecode"><span class="id" type="var">aslist</span></span> trick that
we used in chapter <span class="inlinecode"><span class="id" type="var">HoareList</span></span> to make all the operations total by
forcibly coercing the arguments to <span class="inlinecode">+</span> (for example) into numbers.
Instead, we simply let terms get stuck if they try to use an
operator with the wrong kind of operands: the <span class="inlinecode"><span class="id" type="var">step</span></span> relation
doesn't relate them to anything.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab627"></a><h2 class="section">Syntax</h2>
<div class="paragraph"> </div>
Informally:
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">t</span> ::= <span class="id" type="var">true</span><br/>
| <span class="id" type="var">false</span><br/>
| <span class="id" type="keyword">if</span> <span class="id" type="var">t</span> <span class="id" type="keyword">then</span> <span class="id" type="var">t</span> <span class="id" type="keyword">else</span> <span class="id" type="var">t</span><br/>
| 0<br/>
| <span class="id" type="var">succ</span> <span class="id" type="var">t</span><br/>
| <span class="id" type="var">pred</span> <span class="id" type="var">t</span><br/>
| <span class="id" type="var">iszero</span> <span class="id" type="var">t</span>
<div class="paragraph"> </div>
</div>
Formally:
</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">ttrue</span> : <span class="id" type="var">tm</span><br/>
| <span class="id" type="var">tfalse</span> : <span class="id" type="var">tm</span><br/>
| <span class="id" type="var">tif</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 style="font-family: arial;">→</span> <span class="id" type="var">tm</span><br/>
| <span class="id" type="var">tzero</span> : <span class="id" type="var">tm</span><br/>
| <span class="id" type="var">tsucc</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span><br/>
| <span class="id" type="var">tpred</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span><br/>
| <span class="id" type="var">tiszero</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span>.<br/>
<br/>
</div>
<div class="doc">
<i>Values</i> are <span class="inlinecode"><span class="id" type="var">true</span></span>, <span class="inlinecode"><span class="id" type="var">false</span></span>, and numeric values...
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">bvalue</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">bv_true</span> : <span class="id" type="var">bvalue</span> <span class="id" type="var">ttrue</span><br/>
| <span class="id" type="var">bv_false</span> : <span class="id" type="var">bvalue</span> <span class="id" type="var">tfalse</span>.<br/>
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">nvalue</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">nv_zero</span> : <span class="id" type="var">nvalue</span> <span class="id" type="var">tzero</span><br/>
| <span class="id" type="var">nv_succ</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</span>, <span class="id" type="var">nvalue</span> <span class="id" type="var">t</span> <span style="font-family: arial;">→</span> <span class="id" type="var">nvalue</span> (<span class="id" type="var">tsucc</span> <span class="id" type="var">t</span>).<br/>
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">value</span> (<span class="id" type="var">t</span>:<span class="id" type="var">tm</span>) := <span class="id" type="var">bvalue</span> <span class="id" type="var">t</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">nvalue</span> <span class="id" type="var">t</span>.<br/>
<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">Hint</span> <span class="id" type="var">Constructors</span> <span class="id" type="var">bvalue</span> <span class="id" type="var">nvalue</span>.<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="keyword">Unfold</span> <span class="id" type="var">value</span>.<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="keyword">Unfold</span> <span class="id" type="var">extend</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
<a name="lab628"></a><h2 class="section">Operational Semantics</h2>
<div class="paragraph"> </div>
Informally:
<div class="paragraph"> </div>
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(ST_IfTrue)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">if true then t<sub>1</sub> else t<sub>2</sub> <span style="font-family: arial;">⇒</span> t<sub>1</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(ST_IfFalse)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">if false then t<sub>1</sub> else t<sub>2</sub> <span style="font-family: arial;">⇒</span> t<sub>2</sub></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_If)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">if t<sub>1</sub> then t<sub>2</sub> else t<sub>3</sub> <span style="font-family: arial;">⇒</span></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">if t<sub>1</sub>' then t<sub>2</sub> else t<sub>3</sub></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_Succ)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">succ t<sub>1</sub> <span style="font-family: arial;">⇒</span> succ t<sub>1</sub>'</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(ST_PredZero)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">pred 0 <span style="font-family: arial;">⇒</span> 0</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">numeric value v<sub>1</sub></td>
<td class="infrulenamecol" rowspan="3">
(ST_PredSucc)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">pred (succ v<sub>1</sub>) <span style="font-family: arial;">⇒</span> v<sub>1</sub></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_Pred)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">pred t<sub>1</sub> <span style="font-family: arial;">⇒</span> pred t<sub>1</sub>'</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(ST_IszeroZero)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">iszero 0 <span style="font-family: arial;">⇒</span> true</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">numeric value v<sub>1</sub></td>
<td class="infrulenamecol" rowspan="3">
(ST_IszeroSucc)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">iszero (succ v<sub>1</sub>) <span style="font-family: arial;">⇒</span> false</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_Iszero)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">iszero t<sub>1</sub> <span style="font-family: arial;">⇒</span> iszero t<sub>1</sub>'</td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
Formally:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Reserved Notation</span> "t<sub>1</sub> '<span style="font-family: arial;">⇒</span>' t<sub>2</sub>" (<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_IfTrue</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>,<br/>
(<span class="id" type="var">tif</span> <span class="id" type="var">ttrue</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">t<sub>1</sub></span><br/>
| <span class="id" type="var">ST_IfFalse</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>,<br/>
(<span class="id" type="var">tif</span> <span class="id" type="var">tfalse</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">t<sub>2</sub></span><br/>
| <span class="id" type="var">ST_If</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> <span class="id" type="var">t<sub>3</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">tif</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">t<sub>3</sub></span>) <span style="font-family: arial;">⇒</span> (<span class="id" type="var">tif</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">t<sub>3</sub></span>)<br/>
| <span class="id" type="var">ST_Succ</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>,<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">tsucc</span> <span class="id" type="var">t<sub>1</sub></span>) <span style="font-family: arial;">⇒</span> (<span class="id" type="var">tsucc</span> <span class="id" type="var">t<sub>1</sub>'</span>)<br/>
| <span class="id" type="var">ST_PredZero</span> :<br/>
(<span class="id" type="var">tpred</span> <span class="id" type="var">tzero</span>) <span style="font-family: arial;">⇒</span> <span class="id" type="var">tzero</span><br/>
| <span class="id" type="var">ST_PredSucc</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t<sub>1</sub></span>,<br/>
<span class="id" type="var">nvalue</span> <span class="id" type="var">t<sub>1</sub></span> <span style="font-family: arial;">→</span><br/>
(<span class="id" type="var">tpred</span> (<span class="id" type="var">tsucc</span> <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><br/>
| <span class="id" type="var">ST_Pred</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>,<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">tpred</span> <span class="id" type="var">t<sub>1</sub></span>) <span style="font-family: arial;">⇒</span> (<span class="id" type="var">tpred</span> <span class="id" type="var">t<sub>1</sub>'</span>)<br/>
| <span class="id" type="var">ST_IszeroZero</span> :<br/>
(<span class="id" type="var">tiszero</span> <span class="id" type="var">tzero</span>) <span style="font-family: arial;">⇒</span> <span class="id" type="var">ttrue</span><br/>
| <span class="id" type="var">ST_IszeroSucc</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t<sub>1</sub></span>,<br/>
<span class="id" type="var">nvalue</span> <span class="id" type="var">t<sub>1</sub></span> <span style="font-family: arial;">→</span><br/>
(<span class="id" type="var">tiszero</span> (<span class="id" type="var">tsucc</span> <span class="id" type="var">t<sub>1</sub></span>)) <span style="font-family: arial;">⇒</span> <span class="id" type="var">tfalse</span><br/>
| <span class="id" type="var">ST_Iszero</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>,<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">tiszero</span> <span class="id" type="var">t<sub>1</sub></span>) <span style="font-family: arial;">⇒</span> (<span class="id" type="var">tiszero</span> <span class="id" type="var">t<sub>1</sub>'</span>)<br/>
<br/>
<span class="id" type="keyword">where</span> "t<sub>1</sub> '<span style="font-family: arial;">⇒</span>' t<sub>2</sub>" := (<span class="id" type="var">step</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>).<br/>
<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">Tactic Notation</span> "step_cases" <span class="id" type="var">tactic</span>(<span class="id" type="var">first</span>) <span class="id" type="var">ident</span>(<span class="id" type="var">c</span>) :=<br/>
<span class="id" type="var">first</span>;<br/>
[ <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "ST_IfTrue" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "ST_IfFalse" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "ST_If" <br/>
| <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "ST_Succ" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "ST_PredZero"<br/>
| <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "ST_PredSucc" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "ST_Pred" <br/>
| <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "ST_IszeroZero" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "ST_IszeroSucc"<br/>
| <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "ST_Iszero" ].<br/>
<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="var">Constructors</span> <span class="id" type="var">step</span>.<br/>
</div>
</div>
<div class="doc">
Notice that the <span class="inlinecode"><span class="id" type="var">step</span></span> relation doesn't care about whether
expressions make global sense — it just checks that the operation
in the <i>next</i> reduction step is being applied to the right kinds
of operands.
<div class="paragraph"> </div>
For example, the term <span class="inlinecode"><span class="id" type="var">succ</span></span> <span class="inlinecode"><span class="id" type="var">true</span></span> (i.e., <span class="inlinecode"><span class="id" type="var">tsucc</span></span> <span class="inlinecode"><span class="id" type="var">ttrue</span></span> in the
formal syntax) cannot take a step, but the almost as obviously
nonsensical term
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">succ</span> (<span class="id" type="keyword">if</span> <span class="id" type="var">true</span> <span class="id" type="keyword">then</span> <span class="id" type="var">true</span> <span class="id" type="keyword">else</span> <span class="id" type="var">true</span>)
<div class="paragraph"> </div>
</div>
can take a step (once, before becoming stuck).
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab629"></a><h2 class="section">Normal Forms and Values</h2>
<div class="paragraph"> </div>
The first interesting thing about the <span class="inlinecode"><span class="id" type="var">step</span></span> relation in this
language is that the strong progress theorem from the Smallstep
chapter fails! That is, there are terms that are normal
forms (they can't take a step) but not values (because we have not
included them in our definition of possible "results of
evaluation"). Such terms are <i>stuck</i>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> <span class="id" type="var">step_normal_form</span> := (<span class="id" type="var">normal_form</span> <span class="id" type="var">step</span>).<br/>
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">stuck</span> (<span class="id" type="var">t</span>:<span class="id" type="var">tm</span>) : <span class="id" type="keyword">Prop</span> :=<br/>
<span class="id" type="var">step_normal_form</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/>
<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="keyword">Unfold</span> <span class="id" type="var">stuck</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab630"></a><h4 class="section">Exercise: 2 stars (some_term_is_stuck)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Example</span> <span class="id" type="var">some_term_is_stuck</span> :<br/>
<span style="font-family: arial;">∃</span><span class="id" type="var">t</span>, <span class="id" type="var">stuck</span> <span class="id" type="var">t</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="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
However, although values and normal forms are not the same in this
language, the former set is included in the latter. This is
important because it shows we did not accidentally define things
so that some value could still take a step.
<div class="paragraph"> </div>
<a name="lab631"></a><h4 class="section">Exercise: 3 stars, advanced (value_is_nf)</h4>
Hint: You will reach a point in this proof where you need to
use an induction to reason about a term that is known to be a
numeric value. This induction can be performed either over the
term itself or over the evidence that it is a numeric value. The
proof goes through in either case, but you will find that one way
is quite a bit shorter than the other. For the sake of the
exercise, try to complete the proof both ways.
</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">t</span>,<br/>
<span class="id" type="var">value</span> <span class="id" type="var">t</span> <span style="font-family: arial;">→</span> <span class="id" type="var">step_normal_form</span> <span class="id" type="var">t</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="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab632"></a><h4 class="section">Exercise: 3 stars, optional (step_deterministic)</h4>
Using <span class="inlinecode"><span class="id" type="var">value_is_nf</span></span>, we can show that the <span class="inlinecode"><span class="id" type="var">step</span></span> relation is
also deterministic...
</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> <span class="id" type="keyword">with</span> <span class="id" type="tactic">eauto</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="lab633"></a><h2 class="section">Typing</h2>
<div class="paragraph"> </div>
The next critical observation about this language is that,
although there are stuck terms, they are all "nonsensical", mixing
booleans and numbers in a way that we don't even <i>want</i> to have a
meaning. We can easily exclude such ill-typed terms by defining a
<i>typing relation</i> that relates terms to the types (either numeric
or boolean) of their final results.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">ty</span> : <span class="id" type="keyword">Type</span> := <br/>
| <span class="id" type="var">TBool</span> : <span class="id" type="var">ty</span><br/>
| <span class="id" type="var">TNat</span> : <span class="id" type="var">ty</span>.<br/>
<br/>
</div>
<div class="doc">
In informal notation, the typing relation is often written
<span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T</span></span>, pronounced "<span class="inlinecode"><span class="id" type="var">t</span></span> has type <span class="inlinecode"><span class="id" type="var">T</span></span>." The <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> symbol is
called a "turnstile". (Below, we're going to see richer typing
relations where an additional "context" argument is written to the
left of the turnstile. Here, the context is always empty.) <center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(T_True)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: arial;">⊢</span> true ∈ Bool</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(T_False)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: arial;">⊢</span> false ∈ Bool</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: arial;">⊢</span> t<sub>1</sub> ∈ Bool <span style="font-family: arial;">⊢</span> t<sub>2</sub> ∈ T <span style="font-family: arial;">⊢</span> t<sub>3</sub> ∈ T</td>
<td class="infrulenamecol" rowspan="3">
(T_If)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: arial;">⊢</span> if t<sub>1</sub> then t<sub>2</sub> else t<sub>3</sub> ∈ T</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(T_Zero)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: arial;">⊢</span> 0 ∈ Nat</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: arial;">⊢</span> t<sub>1</sub> ∈ Nat</td>
<td class="infrulenamecol" rowspan="3">
(T_Succ)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: arial;">⊢</span> succ t<sub>1</sub> ∈ Nat</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: arial;">⊢</span> t<sub>1</sub> ∈ Nat</td>
<td class="infrulenamecol" rowspan="3">
(T_Pred)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: arial;">⊢</span> pred t<sub>1</sub> ∈ Nat</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: arial;">⊢</span> t<sub>1</sub> ∈ Nat</td>
<td class="infrulenamecol" rowspan="3">
(T_IsZero)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: arial;">⊢</span> iszero t<sub>1</sub> ∈ Bool</td>
<td></td>
</td>
</table></center>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Reserved Notation</span> "'<span style="font-family: arial;">⊢</span>' t '∈' 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">has_type</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ty</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">T_True</span> : <br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">ttrue</span> ∈ <span class="id" type="var">TBool</span><br/>
| <span class="id" type="var">T_False</span> : <br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">tfalse</span> ∈ <span class="id" type="var">TBool</span><br/>
| <span class="id" type="var">T_If</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">t<sub>3</sub></span> <span class="id" type="var">T</span>,<br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">t<sub>1</sub></span> ∈ <span class="id" type="var">TBool</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">t<sub>2</sub></span> ∈ <span class="id" type="var">T</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">t<sub>3</sub></span> ∈ <span class="id" type="var">T</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">tif</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">t<sub>3</sub></span> ∈ <span class="id" type="var">T</span><br/>
| <span class="id" type="var">T_Zero</span> : <br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">tzero</span> ∈ <span class="id" type="var">TNat</span><br/>
| <span class="id" type="var">T_Succ</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t<sub>1</sub></span>,<br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">t<sub>1</sub></span> ∈ <span class="id" type="var">TNat</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">tsucc</span> <span class="id" type="var">t<sub>1</sub></span> ∈ <span class="id" type="var">TNat</span><br/>
| <span class="id" type="var">T_Pred</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t<sub>1</sub></span>,<br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">t<sub>1</sub></span> ∈ <span class="id" type="var">TNat</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">tpred</span> <span class="id" type="var">t<sub>1</sub></span> ∈ <span class="id" type="var">TNat</span><br/>
| <span class="id" type="var">T_Iszero</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t<sub>1</sub></span>,<br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">t<sub>1</sub></span> ∈ <span class="id" type="var">TNat</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">tiszero</span> <span class="id" type="var">t<sub>1</sub></span> ∈ <span class="id" type="var">TBool</span><br/>
<br/>
<span class="id" type="keyword">where</span> "'<span style="font-family: arial;">⊢</span>' t '∈' T" := (<span class="id" type="var">has_type</span> <span class="id" type="var">t</span> <span class="id" type="var">T</span>).<br/>
<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">Tactic Notation</span> "has_type_cases" <span class="id" type="var">tactic</span>(<span class="id" type="var">first</span>) <span class="id" type="var">ident</span>(<span class="id" type="var">c</span>) :=<br/>
<span class="id" type="var">first</span>;<br/>
[ <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "T_True" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "T_False" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "T_If"<br/>
| <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "T_Zero" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "T_Succ" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "T_Pred"<br/>
| <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "T_Iszero" ].<br/>
<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="var">Constructors</span> <span class="id" type="var">has_type</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
<a name="lab634"></a><h3 class="section">Examples</h3>
<div class="paragraph"> </div>
It's important to realize that the typing relation is a
<i>conservative</i> (or <i>static</i>) approximation: it does not calculate
the type of the normal form of a term.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">has_type_1</span> : <br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">tif</span> <span class="id" type="var">tfalse</span> <span class="id" type="var">tzero</span> (<span class="id" type="var">tsucc</span> <span class="id" type="var">tzero</span>) ∈ <span class="id" type="var">TNat</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">apply</span> <span class="id" type="var">T_If</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">T_False</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">T_Zero</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">T_Succ</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">T_Zero</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
(Since we've included all the constructors of the typing relation
in the hint database, the <span class="inlinecode"><span class="id" type="tactic">auto</span></span> tactic can actually find this
proof automatically.)
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">has_type_not</span> : <br/>
¬ (<span style="font-family: arial;">⊢</span> <span class="id" type="var">tif</span> <span class="id" type="var">tfalse</span> <span class="id" type="var">tzero</span> <span class="id" type="var">ttrue</span> ∈ <span class="id" type="var">TBool</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">Contra</span>. <span class="id" type="var">solve</span> <span class="id" type="tactic">by</span> <span class="id" type="tactic">inversion</span> 2. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
<a name="lab635"></a><h4 class="section">Exercise: 1 star, optional (succ_hastype_nat__hastype_nat)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Example</span> <span class="id" type="var">succ_hastype_nat__hastype_nat</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</span>,<br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">tsucc</span> <span class="id" type="var">t</span> ∈ <span class="id" type="var">TNat</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">t</span> ∈ <span class="id" type="var">TNat</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="lab636"></a><h2 class="section">Canonical forms</h2>
<div class="paragraph"> </div>
The following two lemmas capture the basic property that defines
the shape of well-typed values. They say that the definition of value
and the typing relation agree.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">bool_canonical</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</span>,<br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">t</span> ∈ <span class="id" type="var">TBool</span> <span style="font-family: arial;">→</span> <span class="id" type="var">value</span> <span class="id" type="var">t</span> <span style="font-family: arial;">→</span> <span class="id" type="var">bvalue</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">intros</span> <span class="id" type="var">t</span> <span class="id" type="var">HT</span> <span class="id" type="var">HV</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">HV</span>; <span class="id" type="tactic">auto</span>.<br/>
<br/>
<span class="id" type="tactic">induction</span> <span class="id" type="var">H</span>; <span class="id" type="tactic">inversion</span> <span class="id" type="var">HT</span>; <span class="id" type="tactic">auto</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">nat_canonical</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</span>,<br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">t</span> ∈ <span class="id" type="var">TNat</span> <span style="font-family: arial;">→</span> <span class="id" type="var">value</span> <span class="id" type="var">t</span> <span style="font-family: arial;">→</span> <span class="id" type="var">nvalue</span> <span class="id" type="var">t</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">intros</span> <span class="id" type="var">t</span> <span class="id" type="var">HT</span> <span class="id" type="var">HV</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">HV</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">inversion</span> <span class="id" type="var">HT</span>.<br/>
<br/>
<span class="id" type="tactic">auto</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
<a name="lab637"></a><h2 class="section">Progress</h2>
<div class="paragraph"> </div>
The typing relation enjoys two critical properties. The first is
that well-typed normal forms are values (i.e., not stuck).
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="tactic">progress</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</span> <span class="id" type="var">T</span>,<br/>
<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><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/>
<br/>
</div>
<div class="doc">
<a name="lab638"></a><h4 class="section">Exercise: 3 stars (finish_progress)</h4>
Complete the formal proof of the <span class="inlinecode"><span class="id" type="tactic">progress</span></span> property. (Make sure
you understand the informal proof fragment in the following
exercise before starting — this will save you a lot of time.)
</div>
<div class="code code-tight">
<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> <span class="id" type="keyword">with</span> <span class="id" type="tactic">auto</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">t</span> <span class="id" type="var">T</span> <span class="id" type="var">HT</span>.<br/>
<span class="id" type="var">has_type_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">HT</span>) <span class="id" type="var">Case</span>...<br/>
<span class="comment">(* The cases that were obviously values, like T_True and<br/>
T_False, were eliminated immediately by auto *)</span><br/>
<span class="id" type="var">Case</span> "T_If".<br/>
<span class="id" type="var">right</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">IHHT1</span>; <span class="id" type="tactic">clear</span> <span class="id" type="var">IHHT1</span>.<br/>
<span class="id" type="var">SCase</span> "t<sub>1</sub> is a value".<br/>
<span class="id" type="tactic">apply</span> (<span class="id" type="var">bool_canonical</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">HT1</span>) <span class="id" type="keyword">in</span> <span class="id" type="var">H</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">clear</span> <span class="id" type="var">H</span>.<br/>
<span style="font-family: arial;">∃</span><span class="id" type="var">t<sub>2</sub></span>...<br/>
<span style="font-family: arial;">∃</span><span class="id" type="var">t<sub>3</sub></span>...<br/>
<span class="id" type="var">SCase</span> "t<sub>1</sub> can take a step".<br/>
<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<sub>1</sub>'</span> <span class="id" type="var">H1</span>].<br/>
<span style="font-family: arial;">∃</span>(<span class="id" type="var">tif</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">t<sub>3</sub></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">
</div>
<br/>
</div>
<div class="doc">
<a name="lab639"></a><h4 class="section">Exercise: 3 stars, advanced (finish_progress_informal)</h4>
Complete the corresponding informal proof:
<div class="paragraph"> </div>
<i>Theorem</i>: If <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T</span></span>, then either <span class="inlinecode"><span class="id" type="var">t</span></span> is a value or else
<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> for some <span class="inlinecode"><span class="id" type="var">t'</span></span>.
<div class="paragraph"> </div>
<i>Proof</i>: By induction on a derivation of <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T</span></span>.
<div class="paragraph"> </div>
<ul class="doclist">
<li> If the last rule in the derivation is <span class="inlinecode"><span class="id" type="var">T_If</span></span>, then <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="keyword">if</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span>
<span class="inlinecode"><span class="id" type="keyword">then</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> <span class="inlinecode"><span class="id" type="keyword">else</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>3</sub></span></span>, with <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">Bool</span></span>, <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T</span></span> and <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>3</sub></span></span>
<span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T</span></span>. By the IH, either <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> is a value or else <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> can step
to some <span class="inlinecode"><span class="id" type="var">t<sub>1</sub>'</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> is a value, then by the canonical forms lemmas
and the fact that <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">Bool</span></span> we have that <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span>
is a <span class="inlinecode"><span class="id" type="var">bvalue</span></span> — i.e., it is either <span class="inlinecode"><span class="id" type="var">true</span></span> or <span class="inlinecode"><span class="id" type="var">false</span></span>.
If <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">true</span></span>, then <span class="inlinecode"><span class="id" type="var">t</span></span> steps to <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> by <span class="inlinecode"><span class="id" type="var">ST_IfTrue</span></span>,
while if <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">false</span></span>, then <span class="inlinecode"><span class="id" type="var">t</span></span> steps to <span class="inlinecode"><span class="id" type="var">t<sub>3</sub></span></span> by
<span class="inlinecode"><span class="id" type="var">ST_IfFalse</span></span>. Either way, <span class="inlinecode"><span class="id" type="var">t</span></span> can step, which is what
we wanted to show.
<div class="paragraph"> </div>
</li>
<li> If <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> itself can take a step, then, by <span class="inlinecode"><span class="id" type="var">ST_If</span></span>, so can
<span class="inlinecode"><span class="id" type="var">t</span></span>.
</li>
</ul>
</li>
</ul>
<div class="paragraph"> </div>
<span class="comment">(* FILL IN HERE *)</span><br/>
<font size=-2>☐</font>
<div class="paragraph"> </div>
This is more interesting than the strong progress theorem that we
saw in the Smallstep chapter, where <i>all</i> normal forms were
values. Here, a term can be stuck, but only if it is ill
typed.
<div class="paragraph"> </div>
<a name="lab640"></a><h4 class="section">Exercise: 1 star (step_review)</h4>
Quick review. Answer <i>true</i> or <i>false</i>. In this language...
<div class="paragraph"> </div>
<ul class="doclist">
<li> Every well-typed normal form is a value.
<div class="paragraph"> </div>
</li>
<li> Every value is a normal form.
<div class="paragraph"> </div>
</li>
<li> The single-step evaluation relation is
a partial function (i.e., it is deterministic).
<div class="paragraph"> </div>
</li>
<li> The single-step evaluation relation is a <i>total</i> function.
</li>
</ul>
<div class="paragraph"> </div>
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab641"></a><h2 class="section">Type Preservation</h2>
<div class="paragraph"> </div>
The second critical property of typing is that, when a well-typed
term takes a step, the result is also a well-typed term.
<div class="paragraph"> </div>
This theorem is often called the <i>subject reduction</i> property,
because it tells us what happens when the "subject" of the typing
relation is reduced. This terminology comes from thinking of
typing statements as sentences, where the term is the subject and
the type is the predicate.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">preservation</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</span> <span class="id" type="var">t'</span> <span class="id" type="var">T</span>,<br/>
<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><br/>
<span class="id" type="var">t</span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t'</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">⊢</span> <span class="id" type="var">t'</span> ∈ <span class="id" type="var">T</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab642"></a><h4 class="section">Exercise: 2 stars (finish_preservation)</h4>
Complete the formal proof of the <span class="inlinecode"><span class="id" type="var">preservation</span></span> property. (Again,
make sure you understand the informal proof fragment in the
following exercise first.)
</div>
<div class="code code-tight">
<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> <span class="id" type="keyword">with</span> <span class="id" type="tactic">auto</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">t</span> <span class="id" type="var">t'</span> <span class="id" type="var">T</span> <span class="id" type="var">HT</span> <span class="id" type="var">HE</span>.<br/>
<span class="id" type="tactic">generalize</span> <span class="id" type="tactic">dependent</span> <span class="id" type="var">t'</span>.<br/>
<span class="id" type="var">has_type_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">HT</span>) <span class="id" type="var">Case</span>; <br/>
<span class="comment">(* every case needs to introduce a couple of things *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">t'</span> <span class="id" type="var">HE</span>; <br/>
<span class="comment">(* and we can deal with several impossible<br/>
cases all at once *)</span><br/>
<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="id" type="var">Case</span> "T_If". <span class="id" type="tactic">inversion</span> <span class="id" type="var">HE</span>; <span class="id" type="tactic">subst</span>; <span class="id" type="tactic">clear</span> <span class="id" type="var">HE</span>.<br/>
<span class="id" type="var">SCase</span> "ST_IFTrue". <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="var">SCase</span> "ST_IfFalse". <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="var">SCase</span> "ST_If". <span class="id" type="tactic">apply</span> <span class="id" type="var">T_If</span>; <span class="id" type="tactic">try</span> <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHHT1</span>; <span class="id" type="tactic">assumption</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab643"></a><h4 class="section">Exercise: 3 stars, advanced (finish_preservation_informal)</h4>
Complete the following proof:
<div class="paragraph"> </div>
<i>Theorem</i>: If <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T</span></span> and <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>, then <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t'</span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T</span></span>.
<div class="paragraph"> </div>
<i>Proof</i>: By induction on a derivation of <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T</span></span>.
<div class="paragraph"> </div>
<ul class="doclist">
<li> If the last rule in the derivation is <span class="inlinecode"><span class="id" type="var">T_If</span></span>, then <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="keyword">if</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span>
<span class="inlinecode"><span class="id" type="keyword">then</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> <span class="inlinecode"><span class="id" type="keyword">else</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>3</sub></span></span>, with <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">Bool</span></span>, <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T</span></span> and <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>3</sub></span></span>
<span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T</span></span>.
<div class="paragraph"> </div>
Inspecting the rules for the small-step reduction relation and
remembering that <span class="inlinecode"><span class="id" type="var">t</span></span> has the form <span class="inlinecode"><span class="id" type="keyword">if</span></span> <span class="inlinecode">...</span>, we see that the
only ones that could have been used to prove <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> are
<span class="inlinecode"><span class="id" type="var">ST_IfTrue</span></span>, <span class="inlinecode"><span class="id" type="var">ST_IfFalse</span></span>, or <span class="inlinecode"><span class="id" type="var">ST_If</span></span>.
<div class="paragraph"> </div>
<ul class="doclist">
<li> If the last rule was <span class="inlinecode"><span class="id" type="var">ST_IfTrue</span></span>, then <span class="inlinecode"><span class="id" type="var">t'</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span>. But we
know that <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T</span></span>, so we are done.
<div class="paragraph"> </div>
</li>
<li> If the last rule was <span class="inlinecode"><span class="id" type="var">ST_IfFalse</span></span>, then <span class="inlinecode"><span class="id" type="var">t'</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">t<sub>3</sub></span></span>. But we
know that <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>3</sub></span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T</span></span>, so we are done.
<div class="paragraph"> </div>
</li>