-
Notifications
You must be signed in to change notification settings - Fork 0
/
StlcProp.html
1512 lines (1151 loc) · 136 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">Import</span> <a class="idref" href="Maps.html#"><span class="id" type="library">Maps</span></a>.<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Import</span> <span class="id" type="keyword">Types</span>.<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Import</span> <a class="idref" href="Stlc.html#"><span class="id" type="library">Stlc</span></a>.<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Import</span> <a class="idref" href="Smallstep.html#"><span class="id" type="library">Smallstep</span></a>.<br/>
<span class="id" type="keyword">Module</span> <a name="STLCProp"><span class="id" type="module">STLCProp</span></a>.<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="lab576"></a><h1 class="section">Canonical Forms</h1>
<div class="paragraph"> </div>
As we saw for the simple calculus in the <a href="Types.html"><span class="inlineref">Types</span></a> chapter, the
first step in establishing basic properties of reduction and types
is to identify the possible <i>canonical forms</i> (i.e., well-typed
closed values) belonging to each type. For <span class="inlinecode"><span class="id" type="var">Bool</span></span>, these are the boolean
values <span class="inlinecode"><span class="id" type="var">ttrue</span></span> and <span class="inlinecode"><span class="id" type="var">tfalse</span></span>. For arrow types, the canonical forms
are lambda-abstractions.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <a name="STLCProp.canonical_forms_bool"><span class="id" type="lemma">canonical_forms_bool</span></a> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</span>,<br/>
<a class="idref" href="Maps.html#empty"><span class="id" type="definition">empty</span></a> <span style="font-family: arial;">⊢</span> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> ∈ <a class="idref" href="Stlc.html#STLC.TBool"><span class="id" type="constructor">TBool</span></a> <span style="font-family: arial;">→</span><br/>
<a class="idref" href="Stlc.html#STLC.value"><span class="id" type="inductive">value</span></a> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> <span style="font-family: arial;">→</span><br/>
(<a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> = <a class="idref" href="Stlc.html#STLC.ttrue"><span class="id" type="constructor">ttrue</span></a>) <span style="font-family: arial;">∨</span> (<a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> = <a class="idref" href="Stlc.html#STLC.tfalse"><span class="id" type="constructor">tfalse</span></a>).<br/>
<div class="togglescript" id="proofcontrol1" onclick="toggleDisplay('proof1');toggleDisplay('proofcontrol1')"><span class="show"></span></div>
<div class="proofscript" id="proof1" onclick="toggleDisplay('proof1');toggleDisplay('proofcontrol1')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">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/>
</div>
<br/>
<span class="id" type="keyword">Lemma</span> <a name="STLCProp.canonical_forms_fun"><span class="id" type="lemma">canonical_forms_fun</span></a> : <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/>
<a class="idref" href="Maps.html#empty"><span class="id" type="definition">empty</span></a> <span style="font-family: arial;">⊢</span> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> ∈ (<a class="idref" href="Stlc.html#STLC.TArrow"><span class="id" type="constructor">TArrow</span></a> <a class="idref" href="StlcProp.html#T<sub>1</sub>"><span class="id" type="variable">T<sub>1</sub></span></a> <a class="idref" href="StlcProp.html#T<sub>2</sub>"><span class="id" type="variable">T<sub>2</sub></span></a>) <span style="font-family: arial;">→</span><br/>
<a class="idref" href="Stlc.html#STLC.value"><span class="id" type="inductive">value</span></a> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> <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>, <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> = <a class="idref" href="Stlc.html#STLC.tabs"><span class="id" type="constructor">tabs</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> <a class="idref" href="StlcProp.html#T<sub>1</sub>"><span class="id" type="variable">T<sub>1</sub></span></a> <a class="idref" href="StlcProp.html#u"><span class="id" type="variable">u</span></a>.<br/>
<div class="togglescript" id="proofcontrol2" onclick="toggleDisplay('proof2');toggleDisplay('proofcontrol2')"><span class="show"></span></div>
<div class="proofscript" id="proof2" onclick="toggleDisplay('proof2');toggleDisplay('proofcontrol2')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">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">x<sub>0</sub></span>. <span style="font-family: arial;">∃</span><span class="id" type="var">t<sub>0</sub></span>. <span class="id" type="tactic">auto</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
<a name="lab577"></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 a reduction step. The proof is a relatively
straightforward extension of the progress proof we saw in the
<a href="Types.html"><span class="inlineref">Types</span></a> chapter. We'll give the proof in English first, then the
formal version.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <a name="STLCProp.progress"><span class="id" type="lemma">progress</span></a> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</span> <span class="id" type="var">T</span>,<br/>
<a class="idref" href="Maps.html#empty"><span class="id" type="definition">empty</span></a> <span style="font-family: arial;">⊢</span> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> ∈ <a class="idref" href="StlcProp.html#T"><span class="id" type="variable">T</span></a> <span style="font-family: arial;">→</span><br/>
<a class="idref" href="Stlc.html#STLC.value"><span class="id" type="inductive">value</span></a> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> <span style="font-family: arial;">∨</span> <span style="font-family: arial;">∃</span><span class="id" type="var">t'</span>, <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> <span style="font-family: arial;">⇒</span> <a class="idref" href="StlcProp.html#t'"><span class="id" type="variable">t'</span></a>.<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 can see by inspecting the rule 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 is <span class="inlinecode"><span class="id" type="var">T_App</span></span>, then <span class="inlinecode"><span class="id" type="var">t</span></span> has the
form <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> for som e<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>, where 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 a reduction 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 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 a 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 is <span class="inlinecode"><span class="id" type="var">T_If</span></span>, then <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="keyword">if</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span>
<span class="inlinecode"><span class="id" type="keyword">then</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> <span class="inlinecode"><span class="id" type="keyword">else</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>3</sub></span></span>, 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="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> <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> (@<a class="idref" href="Maps.html#empty"><span class="id" type="definition">empty</span></a> <a class="idref" href="Stlc.html#STLC.ty"><span class="id" type="inductive">ty</span></a>) <span class="id" type="keyword">as</span> <span style="font-family: serif; font-size:85%;">Γ</span>.<br/>
<span class="id" type="tactic">induction</span> <span class="id" type="var">Ht</span>; <span class="id" type="tactic">subst</span> <span style="font-family: serif; font-size:85%;">Γ</span>...<br/>
- <span class="comment">(* T_Var *)</span><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="comment">(* T_App *)</span><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="comment">(* t<sub>1</sub> is a value *)</span><br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">IHHt2</span>...<br/>
* <span class="comment">(* t<sub>2</sub> is also a value *)</span><br/>
<span class="id" type="tactic">assert</span> (<span style="font-family: arial;">∃</span><span class="id" type="var">x<sub>0</sub></span> <span class="id" type="var">t<sub>0</sub></span>, <span class="id" type="var">t<sub>1</sub></span> = <a class="idref" href="Stlc.html#STLC.tabs"><span class="id" type="constructor">tabs</span></a> <a class="idref" href="StlcProp.html#x<sub>0</sub>"><span class="id" type="variable">x<sub>0</sub></span></a> <span class="id" type="var">T<sub>11</sub></span> <a class="idref" href="StlcProp.html#t<sub>0</sub>"><span class="id" type="variable">t<sub>0</sub></span></a>).<br/>
<span class="id" type="tactic">eapply</span> <a class="idref" href="StlcProp.html#STLCProp.canonical_forms_fun"><span class="id" type="lemma">canonical_forms_fun</span></a>; <span class="id" type="tactic">eauto</span>.<br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">H<sub>1</sub></span> <span class="id" type="keyword">as</span> [<span class="id" type="var">x<sub>0</sub></span> [<span class="id" type="var">t<sub>0</sub></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">x<sub>0</sub></span>:=<span class="id" type="var">t<sub>2</sub></span>]<span class="id" type="var">t<sub>0</sub></span>)...<br/>
<br/>
* <span class="comment">(* t<sub>2</sub> steps *)</span><br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H<sub>0</sub></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>(<a class="idref" href="Stlc.html#STLC.tapp"><span class="id" type="constructor">tapp</span></a> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub>'</span>)...<br/>
<br/>
+ <span class="comment">(* t<sub>1</sub> steps *)</span><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>(<a class="idref" href="Stlc.html#STLC.tapp"><span class="id" type="constructor">tapp</span></a> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">t<sub>2</sub></span>)...<br/>
<br/>
- <span class="comment">(* T_If *)</span><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="comment">(* t<sub>1</sub> is a value *)</span><br/>
<span class="id" type="tactic">destruct</span> (<a class="idref" href="StlcProp.html#STLCProp.canonical_forms_bool"><span class="id" type="lemma">canonical_forms_bool</span></a> <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="comment">(* t<sub>1</sub> also steps *)</span><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>(<a class="idref" href="Stlc.html#STLC.tif"><span class="id" type="constructor">tif</span></a> <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="lab578"></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> <a name="STLCProp.progress'"><span class="id" type="lemma">progress'</span></a> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</span> <span class="id" type="var">T</span>,<br/>
<a class="idref" href="Maps.html#empty"><span class="id" type="definition">empty</span></a> <span style="font-family: arial;">⊢</span> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> ∈ <a class="idref" href="StlcProp.html#T"><span class="id" type="variable">T</span></a> <span style="font-family: arial;">→</span><br/>
<a class="idref" href="Stlc.html#STLC.value"><span class="id" type="inductive">value</span></a> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> <span style="font-family: arial;">∨</span> <span style="font-family: arial;">∃</span><span class="id" type="var">t'</span>, <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> <span style="font-family: arial;">⇒</span> <a class="idref" href="StlcProp.html#t'"><span class="id" type="variable">t'</span></a>.<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="tactic">induction</span> <span class="id" type="var">t</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 color="rgb(80%,80%,100%)">☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab579"></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 (from 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 <a href="Types.html"><span class="inlineref">Types</span></a> 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, whose definition uses 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. And finally, for this, we need a
careful definition of...
<div class="paragraph"> </div>
</li>
<li> the <i>free variables</i> of a term — i.e., those variables
mentioned in a term and not in the scope of an enclosing
function abstraction binding a variable of the same name.
</li>
</ul>
<div class="paragraph"> </div>
To make Coq happy, we need to formalize the story in the opposite
order...
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab580"></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 class="paragraph"> </div>
Formally:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <a name="STLCProp.appears_free_in"><span class="id" type="inductive">appears_free_in</span></a> : <a class="idref" href="Maps.html#id"><span class="id" type="inductive">id</span></a> <span style="font-family: arial;">→</span> <a class="idref" href="Stlc.html#STLC.tm"><span class="id" type="inductive">tm</span></a> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <a name="STLCProp.afi_var"><span class="id" type="constructor">afi_var</span></a> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span>,<br/>
<a class="idref" href="StlcProp.html#appears_free_in"><span class="id" type="inductive">appears_free_in</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> (<a class="idref" href="Stlc.html#STLC.tvar"><span class="id" type="constructor">tvar</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a>)<br/>
| <a name="STLCProp.afi_app1"><span class="id" type="constructor">afi_app1</span></a> : <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/>
<a class="idref" href="StlcProp.html#appears_free_in"><span class="id" type="inductive">appears_free_in</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> <a class="idref" href="StlcProp.html#t<sub>1</sub>"><span class="id" type="variable">t<sub>1</sub></span></a> <span style="font-family: arial;">→</span> <a class="idref" href="StlcProp.html#appears_free_in"><span class="id" type="inductive">appears_free_in</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> (<a class="idref" href="Stlc.html#STLC.tapp"><span class="id" type="constructor">tapp</span></a> <a class="idref" href="StlcProp.html#t<sub>1</sub>"><span class="id" type="variable">t<sub>1</sub></span></a> <a class="idref" href="StlcProp.html#t<sub>2</sub>"><span class="id" type="variable">t<sub>2</sub></span></a>)<br/>
| <a name="STLCProp.afi_app2"><span class="id" type="constructor">afi_app2</span></a> : <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/>
<a class="idref" href="StlcProp.html#appears_free_in"><span class="id" type="inductive">appears_free_in</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> <a class="idref" href="StlcProp.html#t<sub>2</sub>"><span class="id" type="variable">t<sub>2</sub></span></a> <span style="font-family: arial;">→</span> <a class="idref" href="StlcProp.html#appears_free_in"><span class="id" type="inductive">appears_free_in</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> (<a class="idref" href="Stlc.html#STLC.tapp"><span class="id" type="constructor">tapp</span></a> <a class="idref" href="StlcProp.html#t<sub>1</sub>"><span class="id" type="variable">t<sub>1</sub></span></a> <a class="idref" href="StlcProp.html#t<sub>2</sub>"><span class="id" type="variable">t<sub>2</sub></span></a>)<br/>
| <a name="STLCProp.afi_abs"><span class="id" type="constructor">afi_abs</span></a> : <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/>
<a class="idref" href="StlcProp.html#y"><span class="id" type="variable">y</span></a> ≠ <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> <span style="font-family: arial;">→</span><br/>
<a class="idref" href="StlcProp.html#appears_free_in"><span class="id" type="inductive">appears_free_in</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> <a class="idref" href="StlcProp.html#t<sub>12</sub>"><span class="id" type="variable">t<sub>12</sub></span></a> <span style="font-family: arial;">→</span><br/>
<a class="idref" href="StlcProp.html#appears_free_in"><span class="id" type="inductive">appears_free_in</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> (<a class="idref" href="Stlc.html#STLC.tabs"><span class="id" type="constructor">tabs</span></a> <a class="idref" href="StlcProp.html#y"><span class="id" type="variable">y</span></a> <a class="idref" href="StlcProp.html#T<sub>11</sub>"><span class="id" type="variable">T<sub>11</sub></span></a> <a class="idref" href="StlcProp.html#t<sub>12</sub>"><span class="id" type="variable">t<sub>12</sub></span></a>)<br/>
| <a name="STLCProp.afi_if<sub>1</sub>"><span class="id" type="constructor">afi_if<sub>1</sub></span></a> : <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/>
<a class="idref" href="StlcProp.html#appears_free_in"><span class="id" type="inductive">appears_free_in</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> <a class="idref" href="StlcProp.html#t<sub>1</sub>"><span class="id" type="variable">t<sub>1</sub></span></a> <span style="font-family: arial;">→</span><br/>
<a class="idref" href="StlcProp.html#appears_free_in"><span class="id" type="inductive">appears_free_in</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> (<a class="idref" href="Stlc.html#STLC.tif"><span class="id" type="constructor">tif</span></a> <a class="idref" href="StlcProp.html#t<sub>1</sub>"><span class="id" type="variable">t<sub>1</sub></span></a> <a class="idref" href="StlcProp.html#t<sub>2</sub>"><span class="id" type="variable">t<sub>2</sub></span></a> <a class="idref" href="StlcProp.html#t<sub>3</sub>"><span class="id" type="variable">t<sub>3</sub></span></a>)<br/>
| <a name="STLCProp.afi_if<sub>2</sub>"><span class="id" type="constructor">afi_if<sub>2</sub></span></a> : <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/>
<a class="idref" href="StlcProp.html#appears_free_in"><span class="id" type="inductive">appears_free_in</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> <a class="idref" href="StlcProp.html#t<sub>2</sub>"><span class="id" type="variable">t<sub>2</sub></span></a> <span style="font-family: arial;">→</span><br/>
<a class="idref" href="StlcProp.html#appears_free_in"><span class="id" type="inductive">appears_free_in</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> (<a class="idref" href="Stlc.html#STLC.tif"><span class="id" type="constructor">tif</span></a> <a class="idref" href="StlcProp.html#t<sub>1</sub>"><span class="id" type="variable">t<sub>1</sub></span></a> <a class="idref" href="StlcProp.html#t<sub>2</sub>"><span class="id" type="variable">t<sub>2</sub></span></a> <a class="idref" href="StlcProp.html#t<sub>3</sub>"><span class="id" type="variable">t<sub>3</sub></span></a>)<br/>
| <a name="STLCProp.afi_if<sub>3</sub>"><span class="id" type="constructor">afi_if<sub>3</sub></span></a> : <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/>
<a class="idref" href="StlcProp.html#appears_free_in"><span class="id" type="inductive">appears_free_in</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> <a class="idref" href="StlcProp.html#t<sub>3</sub>"><span class="id" type="variable">t<sub>3</sub></span></a> <span style="font-family: arial;">→</span><br/>
<a class="idref" href="StlcProp.html#appears_free_in"><span class="id" type="inductive">appears_free_in</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> (<a class="idref" href="Stlc.html#STLC.tif"><span class="id" type="constructor">tif</span></a> <a class="idref" href="StlcProp.html#t<sub>1</sub>"><span class="id" type="variable">t<sub>1</sub></span></a> <a class="idref" href="StlcProp.html#t<sub>2</sub>"><span class="id" type="variable">t<sub>2</sub></span></a> <a class="idref" href="StlcProp.html#t<sub>3</sub>"><span class="id" type="variable">t<sub>3</sub></span></a>).<br/>
<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="var">Constructors</span> <a class="idref" href="StlcProp.html#appears_free_in"><span class="id" type="inductive">appears_free_in</span></a>.<br/>
<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> <a name="STLCProp.closed"><span class="id" type="definition">closed</span></a> (<span class="id" type="var">t</span>:<a class="idref" href="Stlc.html#STLC.tm"><span class="id" type="inductive">tm</span></a>) :=<br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">x</span>, ¬ <a class="idref" href="StlcProp.html#STLCProp.appears_free_in"><span class="id" type="inductive">appears_free_in</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab581"></a><h4 class="section">Exercise: 1 star (afi)</h4>
If the definition of <span class="inlinecode"><span class="id" type="var">appears_free_in</span></span> is not crystal clear to
you, it is a good idea to take a piece of paper and write out the
rules in informal inference-rule notation. (Although it is a
rather low-level, technical definition, understanding it is
crucial to understanding substitution and its properties, which
are really the crux of the lambda-calculus.) <font size=-2 color="rgb(80%,80%,100%)">☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab582"></a><h2 class="section">Substitution</h2>
<div class="paragraph"> </div>
To prove that substitution preserves typing, 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> <a name="STLCProp.free_in_context"><span class="id" type="lemma">free_in_context</span></a> : <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/>
<a class="idref" href="StlcProp.html#STLCProp.appears_free_in"><span class="id" type="inductive">appears_free_in</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> <span style="font-family: arial;">→</span><br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> ∈ <a class="idref" href="StlcProp.html#T"><span class="id" type="variable">T</span></a> <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> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> = <a class="idref" href="http://coq.inria.fr/library/Coq.Init.Datatypes.html#Some"><span class="id" type="constructor">Some</span></a> <a class="idref" href="StlcProp.html#T'"><span class="id" type="variable">T'</span></a>.<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>.t12</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">update_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="proofcontrol4" onclick="toggleDisplay('proof4');toggleDisplay('proofcontrol4')"><span class="show"></span></div>
<div class="proofscript" id="proof4" onclick="toggleDisplay('proof4');toggleDisplay('proofcontrol4')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">x</span> <span class="id" type="var">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">H<sub>0</sub></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="tactic">induction</span> <span class="id" type="var">H</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">H<sub>0</sub></span>; <span class="id" type="tactic">eauto</span>].<br/>
- <span class="comment">(* afi_abs *)</span><br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H<sub>1</sub></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">H<sub>7</sub></span>.<br/>
<span class="id" type="tactic">rewrite</span> <a class="idref" href="Maps.html#update_neq"><span class="id" type="lemma">update_neq</span></a> <span class="id" type="keyword">in</span> <span class="id" type="var">H<sub>7</sub></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 (it has no free variables).
<div class="paragraph"> </div>
<a name="lab583"></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> <a name="STLCProp.typable_empty__closed"><span class="id" type="lemma">typable_empty__closed</span></a> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</span> <span class="id" type="var">T</span>,<br/>
<a class="idref" href="Maps.html#empty"><span class="id" type="definition">empty</span></a> <span style="font-family: arial;">⊢</span> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> ∈ <a class="idref" href="StlcProp.html#T"><span class="id" type="variable">T</span></a> <span style="font-family: arial;">→</span><br/>
<a class="idref" href="StlcProp.html#STLCProp.closed"><span class="id" type="definition">closed</span></a> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a>.<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 color="rgb(80%,80%,100%)">☐</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> <a name="STLCProp.context_invariance"><span class="id" type="lemma">context_invariance</span></a> : <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> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> ∈ <a class="idref" href="StlcProp.html#T"><span class="id" type="variable">T</span></a> <span style="font-family: arial;">→</span><br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">x</span>, <a class="idref" href="StlcProp.html#STLCProp.appears_free_in"><span class="id" type="inductive">appears_free_in</span></a> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> <span style="font-family: arial;">→</span> <span style="font-family: serif; font-size:85%;">Γ</span> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> = <span style="font-family: serif; font-size:85%;">Γ'</span> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a>) <span style="font-family: arial;">→</span><br/>
<span style="font-family: serif; font-size:85%;">Γ'</span> <span style="font-family: arial;">⊢</span> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> ∈ <a class="idref" href="StlcProp.html#T"><span class="id" type="variable">T</span></a>.<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 be either <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, note that any variable other
than <span class="inlinecode"><span class="id" type="var">y</span></span> that 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; 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>. But 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 <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span>; hence
the desired result follows from the induction hypotheses.
</li>
</ul>
</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>.<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="tactic">induction</span> <span class="id" type="var">H</span>; <span class="id" type="tactic">intros</span>; <span class="id" type="tactic">auto</span>.<br/>
- <span class="comment">(* T_Var *)</span><br/>
<span class="id" type="tactic">apply</span> <a class="idref" href="Stlc.html#STLC.T_Var"><span class="id" type="constructor">T_Var</span></a>. <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">←</span> <span class="id" type="var">H<sub>0</sub></span>...<br/>
- <span class="comment">(* T_Abs *)</span><br/>
<span class="id" type="tactic">apply</span> <a class="idref" href="Stlc.html#STLC.T_Abs"><span class="id" type="constructor">T_Abs</span></a>.<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">x<sub>1</sub></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">update</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> <a class="idref" href="Maps.html#update"><span class="id" type="definition">update</span></a>. <span class="id" type="tactic">unfold</span> <a class="idref" href="Maps.html#t_update"><span class="id" type="definition">t_update</span></a>. <span class="id" type="tactic">destruct</span> (<a class="idref" href="Maps.html#beq_id"><span class="id" type="definition">beq_id</span></a> <span class="id" type="var">x<sub>0</sub></span> <span class="id" type="var">x<sub>1</sub></span>) <span class="id" type="var">eqn</span>: <span class="id" type="var">Hx0x<sub>1</sub></span>...<br/>
<span class="id" type="tactic">rewrite</span> <a class="idref" href="Maps.html#beq_id_false_iff"><span class="id" type="lemma">beq_id_false_iff</span></a> <span class="id" type="keyword">in</span> <span class="id" type="var">Hx0x<sub>1</sub></span>. <span class="id" type="tactic">auto</span>.<br/>
- <span class="comment">(* T_App *)</span><br/>
<span class="id" type="tactic">apply</span> <a class="idref" href="Stlc.html#STLC.T_App"><span class="id" type="constructor">T_App</span></a> <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> <a name="STLCProp.substitution_preserves_typing"><span class="id" type="lemma">substitution_preserves_typing</span></a> : <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/>
<a class="idref" href="Maps.html#update"><span class="id" type="definition">update</span></a> <span style="font-family: serif; font-size:85%;">Γ</span> <a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a> <a class="idref" href="StlcProp.html#U"><span class="id" type="variable">U</span></a> <span style="font-family: arial;">⊢</span> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> ∈ <a class="idref" href="StlcProp.html#T"><span class="id" type="variable">T</span></a> <span style="font-family: arial;">→</span><br/>
<a class="idref" href="Maps.html#empty"><span class="id" type="definition">empty</span></a> <span style="font-family: arial;">⊢</span> <a class="idref" href="StlcProp.html#v"><span class="id" type="variable">v</span></a> ∈ <a class="idref" href="StlcProp.html#U"><span class="id" type="variable">U</span></a> <span style="font-family: arial;">→</span><br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> [<a class="idref" href="StlcProp.html#x"><span class="id" type="variable">x</span></a>:=<a class="idref" href="StlcProp.html#v"><span class="id" type="variable">v</span></a>]<a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> ∈ <a class="idref" href="StlcProp.html#T"><span class="id" type="variable">T</span></a>.<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>
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 class="paragraph"> </div>
<i>Proof</i>: We show, 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.
<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 <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, from which
<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 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>
One more 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">update</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="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="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="tactic">induction</span> <span class="id" type="var">t</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="comment">(* tvar *)</span><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> (<a class="idref" href="Maps.html#beq_idP"><span class="id" type="axiom">beq_idP</span></a> <span class="id" type="var">x</span> <span class="id" type="var">y</span>) <span class="id" type="keyword">as</span> [<span class="id" type="var">Hxy</span>|<span class="id" type="var">Hxy</span>].<br/>
+ <span class="comment">(* x=y *)</span><br/>
<span class="id" type="tactic">subst</span>.<br/>
<span class="id" type="tactic">rewrite</span> <a class="idref" href="Maps.html#update_eq"><span class="id" type="lemma">update_eq</span></a> <span class="id" type="keyword">in</span> <span class="id" type="var">H<sub>2</sub></span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H<sub>2</sub></span>; <span class="id" type="tactic">subst</span>. <span class="id" type="tactic">clear</span> <span class="id" type="var">H<sub>2</sub></span>.<br/>
<span class="id" type="tactic">eapply</span> <a class="idref" href="StlcProp.html#STLCProp.context_invariance"><span class="id" type="lemma">context_invariance</span></a>... <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> (<a class="idref" href="StlcProp.html#STLCProp.free_in_context"><span class="id" type="lemma">free_in_context</span></a> <span class="id" type="var">_</span> <span class="id" type="var">_</span> <span class="id" type="var">T</span> <a class="idref" href="Maps.html#empty"><span class="id" type="definition">empty</span></a> <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="comment">(* x<>y *)</span><br/>
<span class="id" type="tactic">apply</span> <a class="idref" href="Stlc.html#STLC.T_Var"><span class="id" type="constructor">T_Var</span></a>. <span class="id" type="tactic">rewrite</span> <a class="idref" href="Maps.html#update_neq"><span class="id" type="lemma">update_neq</span></a> <span class="id" type="keyword">in</span> <span class="id" type="var">H<sub>2</sub></span>...<br/>
- <span class="comment">(* tabs *)</span><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> <a class="idref" href="Stlc.html#STLC.T_Abs"><span class="id" type="constructor">T_Abs</span></a>.<br/>
<span class="id" type="tactic">destruct</span> (<a class="idref" href="Maps.html#beq_idP"><span class="id" type="axiom">beq_idP</span></a> <span class="id" type="var">x</span> <span class="id" type="var">y</span>) <span class="id" type="keyword">as</span> [<span class="id" type="var">Hxy</span> | <span class="id" type="var">Hxy</span>].<br/>
+ <span class="comment">(* x=y *)</span><br/>
<span class="id" type="tactic">subst</span>.<br/>
<span class="id" type="tactic">eapply</span> <a class="idref" href="StlcProp.html#STLCProp.context_invariance"><span class="id" type="lemma">context_invariance</span></a>...<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> <a class="idref" href="Maps.html#update"><span class="id" type="definition">update</span></a>, <a class="idref" href="Maps.html#t_update"><span class="id" type="definition">t_update</span></a>.<br/>
<span class="id" type="tactic">destruct</span> (<a class="idref" href="Maps.html#beq_id"><span class="id" type="definition">beq_id</span></a> <span class="id" type="var">y</span> <span class="id" type="var">x</span>) <span class="id" type="var">eqn</span>: <span class="id" type="var">Hyx</span>...<br/>
+ <span class="comment">(* x<>y *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHt</span>. <span class="id" type="tactic">eapply</span> <a class="idref" href="StlcProp.html#STLCProp.context_invariance"><span class="id" type="lemma">context_invariance</span></a>...<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> <a class="idref" href="Maps.html#update"><span class="id" type="definition">update</span></a>, <a class="idref" href="Maps.html#t_update"><span class="id" type="definition">t_update</span></a>.<br/>
<span class="id" type="tactic">destruct</span> (<a class="idref" href="Maps.html#beq_idP"><span class="id" type="axiom">beq_idP</span></a> <span class="id" type="var">y</span> <span class="id" type="var">z</span>) <span class="id" type="keyword">as</span> [<span class="id" type="var">Hyz</span> | <span class="id" type="var">Hyz</span>]; <span class="id" type="tactic">subst</span>; <span class="id" type="tactic">trivial</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">←</span> <a class="idref" href="Maps.html#beq_id_false_iff"><span class="id" type="lemma">beq_id_false_iff</span></a> <span class="id" type="keyword">in</span> <span class="id" type="var">Hxy</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">Hxy</span>...<br/>
<span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
<a name="lab584"></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 a 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
reduction relation preserves types.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <a name="STLCProp.preservation"><span class="id" type="lemma">preservation</span></a> : <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/>
<a class="idref" href="Maps.html#empty"><span class="id" type="definition">empty</span></a> <span style="font-family: arial;">⊢</span> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> ∈ <a class="idref" href="StlcProp.html#T"><span class="id" type="variable">T</span></a> <span style="font-family: arial;">→</span><br/>
<a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> <span style="font-family: arial;">⇒</span> <a class="idref" href="StlcProp.html#t'"><span class="id" type="variable">t'</span></a> <span style="font-family: arial;">→</span><br/>
<a class="idref" href="Maps.html#empty"><span class="id" type="definition">empty</span></a> <span style="font-family: arial;">⊢</span> <a class="idref" href="StlcProp.html#t'"><span class="id" type="variable">t'</span></a> ∈ <a class="idref" href="StlcProp.html#T"><span class="id" type="variable">T</span></a>.<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>.t12</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="proofcontrol7" onclick="toggleDisplay('proof7');toggleDisplay('proofcontrol7')"><span class="show"></span></div>
<div class="proofscript" id="proof7" onclick="toggleDisplay('proof7');toggleDisplay('proofcontrol7')">
<span class="id" type="keyword">Proof</span> <span class="id" type="keyword">with</span> <span class="id" type="tactic">eauto</span>.<br/>
<span class="id" type="var">remember</span> (@<a class="idref" href="Maps.html#empty"><span class="id" type="definition">empty</span></a> <a class="idref" href="Stlc.html#STLC.ty"><span class="id" type="inductive">ty</span></a>) <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="tactic">induction</span> <span class="id" type="var">HT</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="comment">(* T_App *)</span><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="comment">(* ST_AppAbs *)</span><br/>
<span class="id" type="tactic">apply</span> <a class="idref" href="StlcProp.html#STLCProp.substitution_preserves_typing"><span class="id" type="lemma">substitution_preserves_typing</span></a> <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">HT<sub>1</sub></span>...<br/>
<span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
<a name="lab585"></a><h4 class="section">Exercise: 2 stars, recommended (subject_expansion_stlc)</h4>
An exercise in the <a href="Types.html"><span class="inlineref">Types</span></a> 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 color="rgb(80%,80%,100%)">☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab586"></a><h1 class="section">Type Soundness</h1>
<div class="paragraph"> </div>
<a name="lab587"></a><h4 class="section">Exercise: 2 stars, optional (type_soundness)</h4>
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> <a name="STLCProp.stuck"><span class="id" type="definition">stuck</span></a> (<span class="id" type="var">t</span>:<a class="idref" href="Stlc.html#STLC.tm"><span class="id" type="inductive">tm</span></a>) : <span class="id" type="keyword">Prop</span> :=<br/>
(<a class="idref" href="Smallstep.html#normal_form"><span class="id" type="definition">normal_form</span></a> <a class="idref" href="Stlc.html#STLC.step"><span class="id" type="inductive">step</span></a>) <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> <span style="font-family: arial;">∧</span> ¬ <a class="idref" href="Stlc.html#STLC.value"><span class="id" type="inductive">value</span></a> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a>.<br/>
<br/>
<span class="id" type="keyword">Corollary</span> <a name="STLCProp.soundness"><span class="id" type="lemma">soundness</span></a> : <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/>
<a class="idref" href="Maps.html#empty"><span class="id" type="definition">empty</span></a> <span style="font-family: arial;">⊢</span> <a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> ∈ <a class="idref" href="StlcProp.html#T"><span class="id" type="variable">T</span></a> <span style="font-family: arial;">→</span><br/>
<a class="idref" href="StlcProp.html#t"><span class="id" type="variable">t</span></a> <span style="font-family: arial;">⇒*</span> <a class="idref" href="StlcProp.html#t'"><span class="id" type="variable">t'</span></a> <span style="font-family: arial;">→</span><br/>
~(<a class="idref" href="StlcProp.html#STLCProp.stuck"><span class="id" type="definition">stuck</span></a> <a class="idref" href="StlcProp.html#t'"><span class="id" type="variable">t'</span></a>).<br/>
<div class="togglescript" id="proofcontrol8" onclick="toggleDisplay('proof8');toggleDisplay('proofcontrol8')"><span class="show"></span></div>
<div class="proofscript" id="proof8" onclick="toggleDisplay('proof8');toggleDisplay('proofcontrol8')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">t</span> <span class="id" type="var">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> <a class="idref" href="StlcProp.html#STLCProp.stuck"><span class="id" type="definition">stuck</span></a>.<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> <a class="idref" href="Smallstep.html#normal_form"><span class="id" type="definition">normal_form</span></a> <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/>