-
Notifications
You must be signed in to change notification settings - Fork 11
/
StlcProp.html
1487 lines (1126 loc) · 121 KB
/
StlcProp.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>StlcProp: Properties of STLC</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">StlcProp<span class="subtitle">Properties of STLC</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">Stlc</span>.<br/>
<br/>
<span class="id" type="keyword">Module</span> <span class="id" type="var">STLCProp</span>.<br/>
<span class="id" type="keyword">Import</span> <span class="id" type="var">STLC</span>.<br/>
<br/>
</div>
<div class="doc">
In this chapter, we develop the fundamental theory of the Simply
Typed Lambda Calculus — in particular, the type safety
theorem.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab680"></a><h1 class="section">Canonical Forms</h1>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">canonical_forms_bool</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</span>,<br/>
<span class="id" type="var">empty</span> <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><br/>
<span class="id" type="var">value</span> <span class="id" type="var">t</span> <span style="font-family: arial;">→</span><br/>
(<span class="id" type="var">t</span> = <span class="id" type="var">ttrue</span>) <span style="font-family: arial;">∨</span> (<span class="id" type="var">t</span> = <span class="id" type="var">tfalse</span>).<br/>
<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">HVal</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">HVal</span>; <span class="id" type="tactic">intros</span>; <span class="id" type="tactic">subst</span>; <span class="id" type="tactic">try</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/>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">canonical_forms_fun</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</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">empty</span> <span style="font-family: arial;">⊢</span> <span class="id" type="var">t</span> ∈ (<span class="id" type="var">TArrow</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><br/>
<span class="id" type="var">value</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">x</span> <span class="id" type="var">u</span>, <span class="id" type="var">t</span> = <span class="id" type="var">tabs</span> <span class="id" type="var">x</span> <span class="id" type="var">T<sub>1</sub></span> <span class="id" type="var">u</span>.<br/>
<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">T<sub>1</sub></span> <span class="id" type="var">T<sub>2</sub></span> <span class="id" type="var">HT</span> <span class="id" type="var">HVal</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">HVal</span>; <span class="id" type="tactic">intros</span>; <span class="id" type="tactic">subst</span>; <span class="id" type="tactic">try</span> <span class="id" type="tactic">inversion</span> <span class="id" type="var">HT</span>; <span class="id" type="tactic">subst</span>; <span class="id" type="tactic">auto</span>.<br/>
<span style="font-family: arial;">∃</span><span class="id" type="var">x0</span>. <span style="font-family: arial;">∃</span><span class="id" type="var">t0</span>. <span class="id" type="tactic">auto</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab681"></a><h1 class="section">Progress</h1>
<div class="paragraph"> </div>
As before, the <i>progress</i> theorem tells us that closed, well-typed
terms are not stuck: either a well-typed term is a value, or it
can take an evaluation step. The proof is a relatively
straightforward extension of the progress proof we saw in the
<span class="inlinecode"><span class="id" type="keyword">Types</span></span> chapter.
</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 class="id" type="var">empty</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><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">
<i>Proof</i>: by induction on the 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> The last rule of the derivation cannot be <span class="inlinecode"><span class="id" type="var">T_Var</span></span>, since a
variable is never well typed in an empty context.
<div class="paragraph"> </div>
</li>
<li> The <span class="inlinecode"><span class="id" type="var">T_True</span></span>, <span class="inlinecode"><span class="id" type="var">T_False</span></span>, and <span class="inlinecode"><span class="id" type="var">T_Abs</span></span> cases are trivial, since in
each of these cases we know immediately that <span class="inlinecode"><span class="id" type="var">t</span></span> is a value.
<div class="paragraph"> </div>
</li>
<li> If the last rule of the derivation was <span class="inlinecode"><span class="id" type="var">T_App</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>1</sub></span></span>
<span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span>, and we know that <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 also well typed in the
empty context; in particular, there exists a type <span class="inlinecode"><span class="id" type="var">T<sub>2</sub></span></span> such 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">T<sub>2</sub></span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></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>2</sub></span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T<sub>2</sub></span></span>. By the induction
hypothesis, either <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> is a value or it can take an evaluation
step.
<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, we now consider <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span>, which by the other
induction hypothesis must also either be a value or take an
evaluation step.
<div class="paragraph"> </div>
<ul class="doclist">
<li> Suppose <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> is a value. Since <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> is a value with an
arrow type, it must be a lambda abstraction; hence <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> can take a step by <span class="inlinecode"><span class="id" type="var">ST_AppAbs</span></span>.
<div class="paragraph"> </div>
</li>
<li> Otherwise, <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> can take a step, and hence so can <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> by <span class="inlinecode"><span class="id" type="var">ST_App2</span></span>.
<div class="paragraph"> </div>
</li>
</ul>
</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<sub>1</sub></span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> by <span class="inlinecode"><span class="id" type="var">ST_App1</span></span>.
<div class="paragraph"> </div>
</li>
</ul>
</li>
<li> If the last rule of the derivation was <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>, where <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> has type <span class="inlinecode"><span class="id" type="var">Bool</span></span>. By the IH, <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span>
either is a value or takes a step.
<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 since it has type <span class="inlinecode"><span class="id" type="var">Bool</span></span> it must be
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 it is <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>; otherwise it steps to <span class="inlinecode"><span class="id" type="var">t<sub>3</sub></span></span>.
<div class="paragraph"> </div>
</li>
<li> Otherwise, <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> takes a step, and therefore so does <span class="inlinecode"><span class="id" type="var">t</span></span> (by
<span class="inlinecode"><span class="id" type="var">ST_If</span></span>).
</li>
</ul>
</li>
</ul>
</div>
<div class="code code-tight">
<br/>
<div class="togglescript" id="proofcontrol1" onclick="toggleDisplay('proof1');toggleDisplay('proofcontrol1')"><span class="show"></span></div>
<div class="proofscript" id="proof1" onclick="toggleDisplay('proof1');toggleDisplay('proofcontrol1')">
<span class="id" type="keyword">Proof</span> <span class="id" type="keyword">with</span> <span class="id" type="tactic">eauto</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">remember</span> (@<span class="id" type="var">empty</span> <span class="id" type="var">ty</span>) <span class="id" type="keyword">as</span> <span style="font-family: serif; font-size:85%;">Γ</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>; <span class="id" type="tactic">subst</span> <span style="font-family: serif; font-size:85%;">Γ</span>...<br/>
<span class="id" type="var">Case</span> "T_Var".<br/>
<span class="comment">(* contradictory: variables cannot be typed in an <br/>
empty context *)</span><br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>.<br/>
<br/>
<span class="id" type="var">Case</span> "T_App".<br/>
<span class="comment">(* <span class="inlinecode"><span class="id" type="var">t</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>. Proceed by cases on whether <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> is a <br/>
value or steps... *)</span><br/>
<span class="id" type="var">right</span>. <span class="id" type="tactic">destruct</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">destruct</span> <span class="id" type="var">IHHt2</span>...<br/>
<span class="id" type="var">SSCase</span> "t<sub>2</sub> is also a value".<br/>
<span class="id" type="tactic">assert</span> (<span style="font-family: arial;">∃</span><span class="id" type="var">x0</span> <span class="id" type="var">t0</span>, <span class="id" type="var">t<sub>1</sub></span> = <span class="id" type="var">tabs</span> <span class="id" type="var">x0</span> <span class="id" type="var">T<sub>11</sub></span> <span class="id" type="var">t0</span>).<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">canonical_forms_fun</span>; <span class="id" type="tactic">eauto</span>.<br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">H1</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">x0</span> [<span class="id" type="var">t0</span> <span class="id" type="var">Heq</span>]]. <span class="id" type="tactic">subst</span>.<br/>
<span style="font-family: arial;">∃</span>([<span class="id" type="var">x0</span>:=<span class="id" type="var">t<sub>2</sub></span>]<span class="id" type="var">t0</span>)...<br/>
<br/>
<span class="id" type="var">SSCase</span> "t<sub>2</sub> steps".<br/>
<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<sub>2</sub>'</span> <span class="id" type="var">Hstp</span>]. <span style="font-family: arial;">∃</span>(<span class="id" type="var">tapp</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub>'</span>)...<br/>
<br/>
<span class="id" type="var">SCase</span> "t<sub>1</sub> steps".<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">Hstp</span>]. <span style="font-family: arial;">∃</span>(<span class="id" type="var">tapp</span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">t<sub>2</sub></span>)...<br/>
<br/>
<span class="id" type="var">Case</span> "T_If".<br/>
<span class="id" type="var">right</span>. <span class="id" type="tactic">destruct</span> <span class="id" type="var">IHHt1</span>...<br/>
<br/>
<span class="id" type="var">SCase</span> "t<sub>1</sub> is a value".<br/>
<span class="id" type="tactic">destruct</span> (<span class="id" type="var">canonical_forms_bool</span> <span class="id" type="var">t<sub>1</sub></span>); <span class="id" type="tactic">subst</span>; <span class="id" type="tactic">eauto</span>.<br/>
<br/>
<span class="id" type="var">SCase</span> "t<sub>1</sub> also steps".<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">Hstp</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="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
<a name="lab682"></a><h4 class="section">Exercise: 3 stars, optional (progress_from_term_ind)</h4>
Show that progress can also be proved by induction on terms
instead of induction on typing derivations.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">progress'</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</span> <span class="id" type="var">T</span>,<br/>
<span class="id" type="var">empty</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><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/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">t</span>.<br/>
<span class="id" type="var">t_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">t</span>) <span class="id" type="var">Case</span>; <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="tactic">auto</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="lab683"></a><h1 class="section">Preservation</h1>
<div class="paragraph"> </div>
The other half of the type soundness property is the preservation
of types during reduction. For this, we need to develop some
technical machinery for reasoning about variables and
substitution. Working from top to bottom (the high-level property
we are actually interested in to the lowest-level technical lemmas
that are needed by various cases of the more interesting proofs),
the story goes like this:
<div class="paragraph"> </div>
<ul class="doclist">
<li> The <i>preservation theorem</i> is proved by induction on a typing
derivation, pretty much as we did in the <span class="inlinecode"><span class="id" type="keyword">Types</span></span> chapter. The
one case that is significantly different is the one for the
<span class="inlinecode"><span class="id" type="var">ST_AppAbs</span></span> rule, which is defined using the substitution
operation. To see that this step preserves typing, we need to
know that the substitution itself does. So we prove a...
<div class="paragraph"> </div>
</li>
<li> <i>substitution lemma</i>, stating that substituting a (closed)
term <span class="inlinecode"><span class="id" type="var">s</span></span> for a variable <span class="inlinecode"><span class="id" type="var">x</span></span> in a term <span class="inlinecode"><span class="id" type="var">t</span></span> preserves the type
of <span class="inlinecode"><span class="id" type="var">t</span></span>. The proof goes by induction on the form of <span class="inlinecode"><span class="id" type="var">t</span></span> and
requires looking at all the different cases in the definition
of substitition. This time, the tricky cases are the ones for
variables and for function abstractions. In both cases, we
discover that we need to take a term <span class="inlinecode"><span class="id" type="var">s</span></span> that has been shown
to be well-typed in some context <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> and consider the same
term <span class="inlinecode"><span class="id" type="var">s</span></span> in a slightly different context <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span></span>. For this
we prove a...
<div class="paragraph"> </div>
</li>
<li> <i>context invariance</i> lemma, showing that typing is preserved
under "inessential changes" to the context <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> — in
particular, changes that do not affect any of the free
variables of the term. For this, we need a careful definition
of
<div class="paragraph"> </div>
</li>
<li> the <i>free variables</i> of a term — i.e., the variables occuring
in the term that are not in the scope of a function
abstraction that binds them.
</li>
</ul>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab684"></a><h2 class="section">Free Occurrences</h2>
<div class="paragraph"> </div>
A variable <span class="inlinecode"><span class="id" type="var">x</span></span> <i>appears free in</i> a term <i>t</i> if <span class="inlinecode"><span class="id" type="var">t</span></span> contains some
occurrence of <span class="inlinecode"><span class="id" type="var">x</span></span> that is not under an abstraction labeled <span class="inlinecode"><span class="id" type="var">x</span></span>. For example:
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" type="var">y</span></span> appears free, but <span class="inlinecode"><span class="id" type="var">x</span></span> does not, in <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">T</span><span style="font-family: arial;">→</span><span class="id" type="var">U</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">y</span></span>
</li>
<li> both <span class="inlinecode"><span class="id" type="var">x</span></span> and <span class="inlinecode"><span class="id" type="var">y</span></span> appear free in <span class="inlinecode">(\<span class="id" type="var">x</span>:<span class="id" type="var">T</span><span style="font-family: arial;">→</span><span class="id" type="var">U</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">y</span>)</span> <span class="inlinecode"><span class="id" type="var">x</span></span>
</li>
<li> no variables appear free in <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">T</span><span style="font-family: arial;">→</span><span class="id" type="var">U</span>.</span> <span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">T</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">y</span></span>
</li>
</ul>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">appears_free_in</span> : <span class="id" type="var">id</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">afi_var</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span>,<br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> (<span class="id" type="var">tvar</span> <span class="id" type="var">x</span>)<br/>
| <span class="id" type="var">afi_app1</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</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">appears_free_in</span> <span class="id" type="var">x</span> <span class="id" type="var">t<sub>1</sub></span> <span style="font-family: arial;">→</span> <span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> (<span class="id" type="var">tapp</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">afi_app2</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</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">appears_free_in</span> <span class="id" type="var">x</span> <span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">→</span> <span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> (<span class="id" type="var">tapp</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">afi_abs</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span> <span class="id" type="var">y</span> <span class="id" type="var">T<sub>11</sub></span> <span class="id" type="var">t<sub>12</sub></span>,<br/>
<span class="id" type="var">y</span> ≠ <span class="id" type="var">x</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> <span class="id" type="var">t<sub>12</sub></span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> (<span class="id" type="var">tabs</span> <span class="id" type="var">y</span> <span class="id" type="var">T<sub>11</sub></span> <span class="id" type="var">t<sub>12</sub></span>)<br/>
| <span class="id" type="var">afi_if1</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</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">appears_free_in</span> <span class="id" type="var">x</span> <span class="id" type="var">t<sub>1</sub></span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</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">afi_if2</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</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">appears_free_in</span> <span class="id" type="var">x</span> <span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</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">afi_if3</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</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">appears_free_in</span> <span class="id" type="var">x</span> <span class="id" type="var">t<sub>3</sub></span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</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/>
<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')">
<br/>
<span class="id" type="keyword">Tactic Notation</span> "afi_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> "afi_var"<br/>
| <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "afi_app1" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "afi_app2" <br/>
| <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "afi_abs" <br/>
| <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "afi_if1" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "afi_if2" <br/>
| <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "afi_if3" ].<br/>
<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="var">Constructors</span> <span class="id" type="var">appears_free_in</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
A term in which no variables appear free is said to be <i>closed</i>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">closed</span> (<span class="id" type="var">t</span>:<span class="id" type="var">tm</span>) :=<br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">x</span>, ¬ <span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> <span class="id" type="var">t</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab685"></a><h2 class="section">Substitution</h2>
<div class="paragraph"> </div>
We first need a technical lemma connecting free variables and
typing contexts. If a variable <span class="inlinecode"><span class="id" type="var">x</span></span> appears free in a term <span class="inlinecode"><span class="id" type="var">t</span></span>,
and if we know <span class="inlinecode"><span class="id" type="var">t</span></span> is well typed in context <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span>, then it must
be the case that <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> assigns a type to <span class="inlinecode"><span class="id" type="var">x</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">free_in_context</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span> <span class="id" type="var">t</span> <span class="id" type="var">T</span> <span style="font-family: serif; font-size:85%;">Γ</span>,<br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> <span class="id" type="var">t</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: serif; font-size:85%;">Γ</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><br/>
<span style="font-family: arial;">∃</span><span class="id" type="var">T'</span>, <span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">x</span> = <span class="id" type="var">Some</span> <span class="id" type="var">T'</span>.<br/>
<br/>
</div>
<div class="doc">
<i>Proof</i>: We show, by induction on the proof that <span class="inlinecode"><span class="id" type="var">x</span></span> appears free
in <span class="inlinecode"><span class="id" type="var">t</span></span>, that, for all contexts <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span>, if <span class="inlinecode"><span class="id" type="var">t</span></span> is well typed
under <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span>, then <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> assigns some type to <span class="inlinecode"><span class="id" type="var">x</span></span>.
<div class="paragraph"> </div>
<ul class="doclist">
<li> If the last rule used was <span class="inlinecode"><span class="id" type="var">afi_var</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">x</span></span>, and from
the assumption that <span class="inlinecode"><span class="id" type="var">t</span></span> is well typed under <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> we have
immediately that <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> assigns a type to <span class="inlinecode"><span class="id" type="var">x</span></span>.
<div class="paragraph"> </div>
</li>
<li> If the last rule used was <span class="inlinecode"><span class="id" type="var">afi_app1</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>1</sub></span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> and <span class="inlinecode"><span class="id" type="var">x</span></span>
appears free in <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span>. Since <span class="inlinecode"><span class="id" type="var">t</span></span> is well typed under <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span>,
we can see from the typing rules that <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> must also be, and
the IH then tells us that <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> assigns <span class="inlinecode"><span class="id" type="var">x</span></span> a type.
<div class="paragraph"> </div>
</li>
<li> Almost all the other cases are similar: <span class="inlinecode"><span class="id" type="var">x</span></span> appears free in a
subterm of <span class="inlinecode"><span class="id" type="var">t</span></span>, and since <span class="inlinecode"><span class="id" type="var">t</span></span> is well typed under <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span>, we
know the subterm of <span class="inlinecode"><span class="id" type="var">t</span></span> in which <span class="inlinecode"><span class="id" type="var">x</span></span> appears is well typed
under <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> as well, and the IH gives us exactly the
conclusion we want.
<div class="paragraph"> </div>
</li>
<li> The only remaining case is <span class="inlinecode"><span class="id" type="var">afi_abs</span></span>. In this case <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">=</span>
<span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub>.t<sub>12</sub></span></span>, and <span class="inlinecode"><span class="id" type="var">x</span></span> appears free in <span class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span>; we also know that
<span class="inlinecode"><span class="id" type="var">x</span></span> is different from <span class="inlinecode"><span class="id" type="var">y</span></span>. The difference from the previous
cases is that whereas <span class="inlinecode"><span class="id" type="var">t</span></span> is well typed under <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span>, its
body <span class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span> is well typed under <span class="inlinecode">(<span style="font-family: serif; font-size:85%;">Γ</span>,</span> <span class="inlinecode"><span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span>)</span>, so the IH
allows us to conclude that <span class="inlinecode"><span class="id" type="var">x</span></span> is assigned some type by the
extended context <span class="inlinecode">(<span style="font-family: serif; font-size:85%;">Γ</span>,</span> <span class="inlinecode"><span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span>)</span>. To conclude that <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span>
assigns a type to <span class="inlinecode"><span class="id" type="var">x</span></span>, we appeal to lemma <span class="inlinecode"><span class="id" type="var">extend_neq</span></span>, noting
that <span class="inlinecode"><span class="id" type="var">x</span></span> and <span class="inlinecode"><span class="id" type="var">y</span></span> are different variables.
</li>
</ul>
</div>
<div class="code code-tight">
<br/>
<div class="togglescript" id="proofcontrol3" onclick="toggleDisplay('proof3');toggleDisplay('proofcontrol3')"><span class="show"></span></div>
<div class="proofscript" id="proof3" onclick="toggleDisplay('proof3');toggleDisplay('proofcontrol3')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">x</span> <span class="id" type="var">t</span> <span class="id" type="var">T</span> <span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">H</span> <span class="id" type="var">H0</span>. <span class="id" type="tactic">generalize</span> <span class="id" type="tactic">dependent</span> <span style="font-family: serif; font-size:85%;">Γ</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">afi_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">H</span>) <span class="id" type="var">Case</span>; <br/>
<span class="id" type="tactic">intros</span>; <span class="id" type="tactic">try</span> <span class="id" type="var">solve</span> [<span class="id" type="tactic">inversion</span> <span class="id" type="var">H0</span>; <span class="id" type="tactic">eauto</span>].<br/>
<span class="id" type="var">Case</span> "afi_abs".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H1</span>; <span class="id" type="tactic">subst</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHappears_free_in</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H7</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">extend_neq</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H7</span>; <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
Next, we'll need the fact that any term <span class="inlinecode"><span class="id" type="var">t</span></span> which is well typed in
the empty context is closed — that is, it has no free variables.
<div class="paragraph"> </div>
<a name="lab686"></a><h4 class="section">Exercise: 2 stars, optional (typable_empty__closed)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Corollary</span> <span class="id" type="var">typable_empty__closed</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</span> <span class="id" type="var">T</span>, <br/>
<span class="id" type="var">empty</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><br/>
<span class="id" type="var">closed</span> <span class="id" type="var">t</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
Sometimes, when we have a proof <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> <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>, we will need to
replace <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> by a different context <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span></span>. When is it safe
to do this? Intuitively, it must at least be the case that
<span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span></span> assigns the same types as <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> to all the variables
that appear free in <span class="inlinecode"><span class="id" type="var">t</span></span>. In fact, this is the only condition that
is needed.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">context_invariance</span> : <span style="font-family: arial;">∀</span><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: serif; font-size:85%;">Γ'</span> <span class="id" type="var">t</span> <span class="id" type="var">T</span>,<br/>
<span style="font-family: serif; font-size:85%;">Γ</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><br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">x</span>, <span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> <span class="id" type="var">t</span> <span style="font-family: arial;">→</span> <span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">x</span> = <span style="font-family: serif; font-size:85%;">Γ'</span> <span class="id" type="var">x</span>) <span style="font-family: arial;">→</span><br/>
<span style="font-family: serif; font-size:85%;">Γ'</span> <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">
<i>Proof</i>: By induction on the derivation of <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> <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 was <span class="inlinecode"><span class="id" type="var">T_Var</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">x</span></span>
and <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">T</span></span>. By assumption, <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">T</span></span> as well, and
hence <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span></span> <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> by <span class="inlinecode"><span class="id" type="var">T_Var</span></span>.
<div class="paragraph"> </div>
</li>
<li> If the last rule was <span class="inlinecode"><span class="id" type="var">T_Abs</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">y</span>:<span class="id" type="var">T<sub>11</sub></span>.</span> <span class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span>, with <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>11</sub></span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">T<sub>12</sub></span></span> and <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span>,</span> <span class="inlinecode"><span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span></span> <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T<sub>12</sub></span></span>. The induction
hypothesis is that for any context <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ''</span></span>, if <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span>,</span>
<span class="inlinecode"><span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span></span> and <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ''</span></span> assign the same types to all the free
variables in <span class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span>, then <span class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span> has type <span class="inlinecode"><span class="id" type="var">T<sub>12</sub></span></span> under <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ''</span></span>.
Let <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span></span> be a context which agrees with <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> on the
free variables in <span class="inlinecode"><span class="id" type="var">t</span></span>; we must show <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span></span> <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span>.</span> <span class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span> <span class="inlinecode">∈</span>
<span class="inlinecode"><span class="id" type="var">T<sub>11</sub></span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">T<sub>12</sub></span></span>.
<div class="paragraph"> </div>
By <span class="inlinecode"><span class="id" type="var">T_Abs</span></span>, it suffices to show that <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span>,</span> <span class="inlinecode"><span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span></span> <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span> <span class="inlinecode">∈</span>
<span class="inlinecode"><span class="id" type="var">T<sub>12</sub></span></span>. By the IH (setting <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ''</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span>,</span> <span class="inlinecode"><span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span></span>), it
suffices to show that <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span>,</span> <span class="inlinecode"><span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span></span> and <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span>,</span> <span class="inlinecode"><span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span></span> agree
on all the variables that appear free in <span class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span>.
<div class="paragraph"> </div>
Any variable occurring free in <span class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span> must either be <span class="inlinecode"><span class="id" type="var">y</span></span>, or
some other variable. <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span>,</span> <span class="inlinecode"><span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span></span> and <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span>,</span> <span class="inlinecode"><span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span></span>
clearly agree on <span class="inlinecode"><span class="id" type="var">y</span></span>. Otherwise, we note that any variable
other than <span class="inlinecode"><span class="id" type="var">y</span></span> which occurs free in <span class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span> also occurs free in
<span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">=</span> <span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span>.</span> <span class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span>, and by assumption <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> and <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span></span>
agree on all such variables, and hence so do <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span>,</span> <span class="inlinecode"><span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span></span>
and <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span>,</span> <span class="inlinecode"><span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span></span>.
<div class="paragraph"> </div>
</li>
<li> If the last rule was <span class="inlinecode"><span class="id" type="var">T_App</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>1</sub></span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span>, with <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> <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">T<sub>2</sub></span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">T</span></span> and <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</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<sub>2</sub></span></span>. One induction
hypothesis states that for all contexts <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span></span>, if <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span></span>
agrees with <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> on the free variables in <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span>, then <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span>
has type <span class="inlinecode"><span class="id" type="var">T<sub>2</sub></span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">T</span></span> under <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span></span>; there is a similar IH for
<span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span>. We must show that <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> also has type <span class="inlinecode"><span class="id" type="var">T</span></span> under
<span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span></span>, given the assumption that <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span></span> agrees with
<span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> on all the free variables in <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>. By <span class="inlinecode"><span class="id" type="var">T_App</span></span>, it
suffices to show that <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> each have the same type
under <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span></span> as under <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span>. However, we note that all
free variables in <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> are also free in <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 similarly
for free variables in <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span>; hence the desired result follows
by the two IHs.
</li>
</ul>
</div>
<div class="code code-tight">
<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> <span class="id" type="keyword">with</span> <span class="id" type="tactic">eauto</span>.<br/>
<span class="id" type="tactic">intros</span>.<br/>
<span class="id" type="tactic">generalize</span> <span class="id" type="tactic">dependent</span> <span style="font-family: serif; font-size:85%;">Γ'</span>.<br/>
<span class="id" type="var">has_type_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">H</span>) <span class="id" type="var">Case</span>; <span class="id" type="tactic">intros</span>; <span class="id" type="tactic">auto</span>.<br/>
<span class="id" type="var">Case</span> "T_Var".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">T_Var</span>. <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">←</span> <span class="id" type="var">H0</span>...<br/>
<span class="id" type="var">Case</span> "T_Abs".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">T_Abs</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHhas_type</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">x1</span> <span class="id" type="var">Hafi</span>.<br/>
<span class="comment">(* the only tricky step... the <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span></span> we use to <br/>
instantiate is <span class="inlinecode"><span class="id" type="var">extend</span></span> <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">T<sub>11</sub></span></span> *)</span><br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">extend</span>. <span class="id" type="tactic">destruct</span> (<span class="id" type="var">eq_id_dec</span> <span class="id" type="var">x0</span> <span class="id" type="var">x1</span>)...<br/>
<span class="id" type="var">Case</span> "T_App".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">T_App</span> <span class="id" type="keyword">with</span> <span class="id" type="var">T<sub>11</sub></span>...<br/>
<span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
Now we come to the conceptual heart of the proof that reduction
preserves types — namely, the observation that <i>substitution</i>
preserves types.
<div class="paragraph"> </div>
Formally, the so-called <i>Substitution Lemma</i> says this: suppose we
have a term <span class="inlinecode"><span class="id" type="var">t</span></span> with a free variable <span class="inlinecode"><span class="id" type="var">x</span></span>, and suppose we've been
able to assign a type <span class="inlinecode"><span class="id" type="var">T</span></span> to <span class="inlinecode"><span class="id" type="var">t</span></span> under the assumption that <span class="inlinecode"><span class="id" type="var">x</span></span> has
some type <span class="inlinecode"><span class="id" type="var">U</span></span>. Also, suppose that we have some other term <span class="inlinecode"><span class="id" type="var">v</span></span> and
that we've shown that <span class="inlinecode"><span class="id" type="var">v</span></span> has type <span class="inlinecode"><span class="id" type="var">U</span></span>. Then, since <span class="inlinecode"><span class="id" type="var">v</span></span> satisfies
the assumption we made about <span class="inlinecode"><span class="id" type="var">x</span></span> when typing <span class="inlinecode"><span class="id" type="var">t</span></span>, we should be
able to substitute <span class="inlinecode"><span class="id" type="var">v</span></span> for each of the occurrences of <span class="inlinecode"><span class="id" type="var">x</span></span> in <span class="inlinecode"><span class="id" type="var">t</span></span>
and obtain a new term that still has type <span class="inlinecode"><span class="id" type="var">T</span></span>.
<div class="paragraph"> </div>
<i>Lemma</i>: If <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span>,<span class="id" type="var">x</span>:<span class="id" type="var">U</span></span> <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 style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">v</span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">U</span></span>, then <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> <span class="inlinecode"><span style="font-family: arial;">⊢</span></span>
<span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">v</span>]<span class="id" type="var">t</span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">substitution_preserves_typing</span> : <span style="font-family: arial;">∀</span><span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">x</span> <span class="id" type="var">U</span> <span class="id" type="var">t</span> <span class="id" type="var">v</span> <span class="id" type="var">T</span>,<br/>
<span class="id" type="var">extend</span> <span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">x</span> <span class="id" type="var">U</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><br/>
<span class="id" type="var">empty</span> <span style="font-family: arial;">⊢</span> <span class="id" type="var">v</span> ∈ <span class="id" type="var">U</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> [<span class="id" type="var">x</span>:=<span class="id" type="var">v</span>]<span class="id" type="var">t</span> ∈ <span class="id" type="var">T</span>.<br/>
<br/>
</div>
<div class="doc">
One technical subtlety in the statement of the lemma is that we
assign <span class="inlinecode"><span class="id" type="var">v</span></span> the type <span class="inlinecode"><span class="id" type="var">U</span></span> in the <i>empty</i> context — in other words,
we assume <span class="inlinecode"><span class="id" type="var">v</span></span> is closed. This assumption considerably simplifies
the <span class="inlinecode"><span class="id" type="var">T_Abs</span></span> case of the proof (compared to assuming <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">v</span></span> <span class="inlinecode">∈</span>
<span class="inlinecode"><span class="id" type="var">U</span></span>, which would be the other reasonable assumption at this point)
because the context invariance lemma then tells us that <span class="inlinecode"><span class="id" type="var">v</span></span> has
type <span class="inlinecode"><span class="id" type="var">U</span></span> in any context at all — we don't have to worry about
free variables in <span class="inlinecode"><span class="id" type="var">v</span></span> clashing with the variable being introduced
into the context by <span class="inlinecode"><span class="id" type="var">T_Abs</span></span>.
<div class="paragraph"> </div>
<i>Proof</i>: We prove, by induction on <span class="inlinecode"><span class="id" type="var">t</span></span>, that, for all <span class="inlinecode"><span class="id" type="var">T</span></span> and
<span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span>, if <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span>,<span class="id" type="var">x</span>:<span class="id" type="var">U</span></span> <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 style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">v</span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">U</span></span>, then <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> <span class="inlinecode"><span style="font-family: arial;">⊢</span></span>
<span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">v</span>]<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 <span class="inlinecode"><span class="id" type="var">t</span></span> is a variable, there are two cases to consider, depending
on whether <span class="inlinecode"><span class="id" type="var">t</span></span> is <span class="inlinecode"><span class="id" type="var">x</span></span> or some other variable.
<div class="paragraph"> </div>
<ul class="doclist">
<li> If <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">x</span></span>, then from the fact that <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span>,</span> <span class="inlinecode"><span class="id" type="var">x</span>:<span class="id" type="var">U</span></span> <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T</span></span> we
conclude that <span class="inlinecode"><span class="id" type="var">U</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">T</span></span>. We must show that <span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">v</span>]<span class="id" type="var">x</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">v</span></span> has
type <span class="inlinecode"><span class="id" type="var">T</span></span> under <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span>, given the assumption that <span class="inlinecode"><span class="id" type="var">v</span></span> has
type <span class="inlinecode"><span class="id" type="var">U</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">T</span></span> under the empty context. This follows from
context invariance: if a closed term has type <span class="inlinecode"><span class="id" type="var">T</span></span> in the
empty context, it has that type in any context.
<div class="paragraph"> </div>
</li>
<li> If <span class="inlinecode"><span class="id" type="var">t</span></span> is some variable <span class="inlinecode"><span class="id" type="var">y</span></span> that is not equal to <span class="inlinecode"><span class="id" type="var">x</span></span>, then
we need only note that <span class="inlinecode"><span class="id" type="var">y</span></span> has the same type under <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span>,</span>
<span class="inlinecode"><span class="id" type="var">x</span>:<span class="id" type="var">U</span></span> as under <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span>.
<div class="paragraph"> </div>
</li>
</ul>
</li>
<li> If <span class="inlinecode"><span class="id" type="var">t</span></span> is an abstraction <span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span>.</span> <span class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span>, then the IH tells us,
for all <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span></span> and <span class="inlinecode"><span class="id" type="var">T'</span></span>, that if <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span>,<span class="id" type="var">x</span>:<span class="id" type="var">U</span></span> <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>12</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">v</span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">U</span></span>, then <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ'</span></span> <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">v</span>]<span class="id" type="var">t<sub>12</sub></span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T'</span></span>.
<div class="paragraph"> </div>
The substitution in the conclusion behaves differently,
depending on whether <span class="inlinecode"><span class="id" type="var">x</span></span> and <span class="inlinecode"><span class="id" type="var">y</span></span> are the same variable name.
<div class="paragraph"> </div>
First, suppose <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">y</span></span>. Then, by the definition of
substitution, <span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">v</span>]<span class="id" type="var">t</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">t</span></span>, so we just need to show <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> <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>. But we know <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span>,<span class="id" type="var">x</span>:<span class="id" type="var">U</span></span> <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 since the
variable <span class="inlinecode"><span class="id" type="var">y</span></span> does not appear free in <span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span>.</span> <span class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span>, the
context invariance lemma yields <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> <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>
Second, suppose <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode">≠</span> <span class="inlinecode"><span class="id" type="var">y</span></span>. We know <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span>,<span class="id" type="var">x</span>:<span class="id" type="var">U</span>,<span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span></span> <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span> <span class="inlinecode">∈</span>
<span class="inlinecode"><span class="id" type="var">T<sub>12</sub></span></span> by inversion of the typing relation, and <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span>,<span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span>,<span class="id" type="var">x</span>:<span class="id" type="var">U</span></span>
<span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T<sub>12</sub></span></span> follows from this by the context invariance
lemma, so the IH applies, giving us <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span>,<span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span></span> <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">v</span>]<span class="id" type="var">t<sub>12</sub></span></span> <span class="inlinecode">∈</span>
<span class="inlinecode"><span class="id" type="var">T<sub>12</sub></span></span>. By <span class="inlinecode"><span class="id" type="var">T_Abs</span></span>, <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span>.</span> <span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">v</span>]<span class="id" type="var">t<sub>12</sub></span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T<sub>11</sub></span><span style="font-family: arial;">→</span><span class="id" type="var">T<sub>12</sub></span></span>, and
by the definition of substitution (noting that <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode">≠</span> <span class="inlinecode"><span class="id" type="var">y</span></span>),
<span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> <span class="inlinecode"><span style="font-family: arial;">⊢</span></span> <span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span>.</span> <span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">v</span>]<span class="id" type="var">t<sub>12</sub></span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">T<sub>11</sub></span><span style="font-family: arial;">→</span><span class="id" type="var">T<sub>12</sub></span></span> as required.
<div class="paragraph"> </div>
</li>
<li> If <span class="inlinecode"><span class="id" type="var">t</span></span> is an application <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>, the result follows
straightforwardly from the definition of substitution and the
induction hypotheses.
<div class="paragraph"> </div>
</li>
<li> The remaining cases are similar to the application case.
</li>
</ul>
<div class="paragraph"> </div>
Another technical note: This proof is a rare case where an
induction on terms, rather than typing derivations, yields a
simpler argument. The reason for this is that the assumption
<span class="inlinecode"><span class="id" type="var">extend</span></span> <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">U</span></span> <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> is not completely generic, in
the sense that one of the "slots" in the typing relation — namely
the context — is not just a variable, and this means that Coq's
native induction tactic does not give us the induction hypothesis
that we want. It is possible to work around this, but the needed
generalization is a little tricky. The term <span class="inlinecode"><span class="id" type="var">t</span></span>, on the other
hand, <i>is</i> completely generic.
</div>
<div class="code code-tight">
<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> <span class="id" type="keyword">with</span> <span class="id" type="tactic">eauto</span>.<br/>
<span class="id" type="tactic">intros</span> <span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">x</span> <span class="id" type="var">U</span> <span class="id" type="var">t</span> <span class="id" type="var">v</span> <span class="id" type="var">T</span> <span class="id" type="var">Ht</span> <span class="id" type="var">Ht'</span>.<br/>
<span class="id" type="tactic">generalize</span> <span class="id" type="tactic">dependent</span> <span style="font-family: serif; font-size:85%;">Γ</span>. <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">t_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">t</span>) <span class="id" type="var">Case</span>; <span class="id" type="tactic">intros</span> <span class="id" type="var">T</span> <span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">H</span>;<br/>
<span class="comment">(* in each case, we'll want to get at the derivation of 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">simpl</span>...<br/>
<span class="id" type="var">Case</span> "tvar".<br/>
<span class="id" type="tactic">rename</span> <span class="id" type="var">i</span> <span class="id" type="var">into</span> <span class="id" type="var">y</span>. <span class="id" type="tactic">destruct</span> (<span class="id" type="var">eq_id_dec</span> <span class="id" type="var">x</span> <span class="id" type="var">y</span>).<br/>
<span class="id" type="var">SCase</span> "x=y".<br/>
<span class="id" type="tactic">subst</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">extend_eq</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H2</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H2</span>; <span class="id" type="tactic">subst</span>. <span class="id" type="tactic">clear</span> <span class="id" type="var">H2</span>.<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">context_invariance</span>... <span class="id" type="tactic">intros</span> <span class="id" type="var">x</span> <span class="id" type="var">Hcontra</span>.<br/>
<span class="id" type="tactic">destruct</span> (<span class="id" type="var">free_in_context</span> <span class="id" type="var">_</span> <span class="id" type="var">_</span> <span class="id" type="var">T</span> <span class="id" type="var">empty</span> <span class="id" type="var">Hcontra</span>) <span class="id" type="keyword">as</span> [<span class="id" type="var">T'</span> <span class="id" type="var">HT'</span>]...<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">HT'</span>.<br/>
<span class="id" type="var">SCase</span> "x≠y".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">T_Var</span>. <span class="id" type="tactic">rewrite</span> <span class="id" type="var">extend_neq</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H2</span>...<br/>
<span class="id" type="var">Case</span> "tabs".<br/>
<span class="id" type="tactic">rename</span> <span class="id" type="var">i</span> <span class="id" type="var">into</span> <span class="id" type="var">y</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">T_Abs</span>.<br/>
<span class="id" type="tactic">destruct</span> (<span class="id" type="var">eq_id_dec</span> <span class="id" type="var">x</span> <span class="id" type="var">y</span>).<br/>
<span class="id" type="var">SCase</span> "x=y".<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">context_invariance</span>...<br/>
<span class="id" type="tactic">subst</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">x</span> <span class="id" type="var">Hafi</span>. <span class="id" type="tactic">unfold</span> <span class="id" type="var">extend</span>.<br/>
<span class="id" type="tactic">destruct</span> (<span class="id" type="var">eq_id_dec</span> <span class="id" type="var">y</span> <span class="id" type="var">x</span>)...<br/>
<span class="id" type="var">SCase</span> "x≠y".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHt</span>. <span class="id" type="tactic">eapply</span> <span class="id" type="var">context_invariance</span>...<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">z</span> <span class="id" type="var">Hafi</span>. <span class="id" type="tactic">unfold</span> <span class="id" type="var">extend</span>.<br/>
<span class="id" type="tactic">destruct</span> (<span class="id" type="var">eq_id_dec</span> <span class="id" type="var">y</span> <span class="id" type="var">z</span>)...<br/>
<span class="id" type="tactic">subst</span>. <span class="id" type="tactic">rewrite</span> <span class="id" type="var">neq_id</span>...<br/>
<span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
The substitution lemma can be viewed as a kind of "commutation"
property. Intuitively, it says that substitution and typing can
be done in either order: we can either assign types to the terms
<span class="inlinecode"><span class="id" type="var">t</span></span> and <span class="inlinecode"><span class="id" type="var">v</span></span> separately (under suitable contexts) and then combine
them using substitution, or we can substitute first and then
assign a type to <span class="inlinecode"></span> <span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">v</span>]</span> <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode"></span> — the result is the same either
way.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab687"></a><h2 class="section">Main Theorem</h2>
<div class="paragraph"> </div>
We now have the tools we need to prove preservation: if a closed
term <span class="inlinecode"><span class="id" type="var">t</span></span> has type <span class="inlinecode"><span class="id" type="var">T</span></span>, and takes an evaluation step to <span class="inlinecode"><span class="id" type="var">t'</span></span>, then <span class="inlinecode"><span class="id" type="var">t'</span></span>
is also a closed term with type <span class="inlinecode"><span class="id" type="var">T</span></span>. In other words, the small-step
evaluation relation preserves types.
</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 class="id" type="var">empty</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><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 class="id" type="var">empty</span> <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">
<i>Proof</i>: by induction on the 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> We can immediately rule out <span class="inlinecode"><span class="id" type="var">T_Var</span></span>, <span class="inlinecode"><span class="id" type="var">T_Abs</span></span>, <span class="inlinecode"><span class="id" type="var">T_True</span></span>, and
<span class="inlinecode"><span class="id" type="var">T_False</span></span> as the final rules in the derivation, since in each of
these cases <span class="inlinecode"><span class="id" type="var">t</span></span> cannot take a step.
<div class="paragraph"> </div>
</li>
<li> If the last rule in the derivation was <span class="inlinecode"><span class="id" type="var">T_App</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>1</sub></span></span>
<span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span>. There are three cases to consider, one for each rule that
could have been used to show that <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> takes a step to <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> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> takes a step by <span class="inlinecode"><span class="id" type="var">ST_App1</span></span>, with <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> stepping to
<span class="inlinecode"><span class="id" type="var">t<sub>1</sub>'</span></span>, then by the IH <span class="inlinecode"><span class="id" type="var">t<sub>1</sub>'</span></span> has the same type as <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span>, and
hence <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> has the same type as <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 class="paragraph"> </div>
</li>
<li> The <span class="inlinecode"><span class="id" type="var">ST_App2</span></span> case is similar.
<div class="paragraph"> </div>
</li>
<li> If <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> takes a step by <span class="inlinecode"><span class="id" type="var">ST_AppAbs</span></span>, then <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">x</span>:<span class="id" type="var">T<sub>11</sub>.t<sub>12</sub></span></span> and <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">x</span>:=<span class="id" type="var">t<sub>2</sub></span>]<span class="id" type="var">t<sub>12</sub></span></span>; the
desired result now follows from the fact that substitution
preserves types.
<div class="paragraph"> </div>
</li>
</ul>
</li>
<li> If the last rule in the derivation was <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>, and there are again three cases depending on
how <span class="inlinecode"><span class="id" type="var">t</span></span> steps.
<div class="paragraph"> </div>
<ul class="doclist">
<li> If <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> or <span class="inlinecode"><span class="id" type="var">t<sub>3</sub></span></span>, the result is immediate, since
<span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> and <span class="inlinecode"><span class="id" type="var">t<sub>3</sub></span></span> have the same type as <span class="inlinecode"><span class="id" type="var">t</span></span>.
<div class="paragraph"> </div>
</li>
<li> Otherwise, <span class="inlinecode"><span class="id" type="var">t</span></span> steps by <span class="inlinecode"><span class="id" type="var">ST_If</span></span>, and the desired conclusion
follows directly from the induction hypothesis.
</li>
</ul>
</li>
</ul>
</div>
<div class="code code-tight">
<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> <span class="id" type="keyword">with</span> <span class="id" type="tactic">eauto</span>.<br/>
<span class="id" type="var">remember</span> (@<span class="id" type="var">empty</span> <span class="id" type="var">ty</span>) <span class="id" type="keyword">as</span> <span style="font-family: serif; font-size:85%;">Γ</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="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="id" type="tactic">intros</span> <span class="id" type="var">t'</span> <span class="id" type="var">HE</span>; <span class="id" type="tactic">subst</span> <span style="font-family: serif; font-size:85%;">Γ</span>; <span class="id" type="tactic">subst</span>; <br/>
<span class="id" type="tactic">try</span> <span class="id" type="var">solve</span> [<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">auto</span>].<br/>
<span class="id" type="var">Case</span> "T_App".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">HE</span>; <span class="id" type="tactic">subst</span>...<br/>
<span class="comment">(* Most of the cases are immediate by induction, <br/>
and <span class="inlinecode"><span class="id" type="tactic">eauto</span></span> takes care of them *)</span><br/>
<span class="id" type="var">SCase</span> "ST_AppAbs".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">substitution_preserves_typing</span> <span class="id" type="keyword">with</span> <span class="id" type="var">T<sub>11</sub></span>...<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">HT1</span>...<br/>
<span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
<a name="lab688"></a><h4 class="section">Exercise: 2 stars (subject_expansion_stlc)</h4>
An exercise in the <span class="inlinecode"><span class="id" type="keyword">Types</span></span> chapter asked about the subject
expansion property for the simple language of arithmetic and
boolean expressions. Does this property hold for STLC? That is,
is it always the case that, if <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> and <span class="inlinecode"><span class="id" type="var">has_type</span></span> <span class="inlinecode"><span class="id" type="var">t'</span></span> <span class="inlinecode"><span class="id" type="var">T</span></span>,
then <span class="inlinecode"><span class="id" type="var">empty</span></span> <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>? If so, prove it. If not, give a
counter-example not involving conditionals.
<div class="paragraph"> </div>
<span class="comment">(* FILL IN HERE *)</span><br/>
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab689"></a><h1 class="section">Type Soundness</h1>
<div class="paragraph"> </div>
<a name="lab690"></a><h4 class="section">Exercise: 2 stars, optional (type_soundness)</h4>
<div class="paragraph"> </div>
Put progress and preservation together and show that a well-typed
term can <i>never</i> reach a stuck state.
</div>
<div class="code code-tight">
<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">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/>
<br/>
<span class="id" type="keyword">Corollary</span> <span class="id" type="var">soundness</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 class="id" type="var">empty</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> <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 class="id" type="var">stuck</span> <span class="id" type="var">t'</span>).<br/>
<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">t'</span> <span class="id" type="var">T</span> <span class="id" type="var">Hhas_type</span> <span class="id" type="var">Hmulti</span>. <span class="id" type="tactic">unfold</span> <span class="id" type="var">stuck</span>.<br/>
<span class="id" type="tactic">intros</span> [<span class="id" type="var">Hnf</span> <span class="id" type="var">Hnot_val</span>]. <span class="id" type="tactic">unfold</span> <span class="id" type="var">normal_form</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hnf</span>.<br/>
<span class="id" type="tactic">induction</span> <span class="id" type="var">Hmulti</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab691"></a><h1 class="section">Uniqueness of Types</h1>
<div class="paragraph"> </div>
<a name="lab692"></a><h4 class="section">Exercise: 3 stars (types_unique)</h4>
Another pleasant property of the STLC is that types are
unique: a given term (in a given context) has at most one
type. Formalize this statement and prove it.
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
</div>
<div class="doc">
<font size=-2>☐</font>