-
Notifications
You must be signed in to change notification settings - Fork 0
/
Records.html
1181 lines (1000 loc) · 154 KB
/
Records.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>Records: Adding Records to 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">Records<span class="subtitle">Adding Records to STLC</span></h1>
<div class="code code-tight">
</div>
<div class="doc">
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Export</span> <span class="id" type="var">Stlc</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab798"></a><h1 class="section">Adding Records</h1>
<div class="paragraph"> </div>
We saw in chapter <span class="inlinecode"><span class="id" type="var">MoreStlc</span></span> how records can be treated as syntactic
sugar for nested uses of products. This is fine for simple
examples, but the encoding is informal (in reality, if we really
treated records this way, it would be carried out in the parser,
which we are eliding here), and anyway it is not very efficient.
So it is also interesting to see how records can be treated as
first-class citizens of the language.
<div class="paragraph"> </div>
Recall the informal definitions we gave before:
<div class="paragraph"> </div>
<div class="paragraph"> </div>
Syntax:
<pre>
t ::= Terms:
| ...
| {i1=t<sub>1</sub>, ..., in=tn} record
| t.i projection
v ::= Values:
| ...
| {i1=v<sub>1</sub>, ..., in=vn} record value
T ::= Types:
| ...
| {i1:T<sub>1</sub>, ..., in:Tn} record type
</pre>
Reduction:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">ti <span style="font-family: arial;">⇒</span> ti' (ST_Rcd)</td>
<td class="infrulenamecol" rowspan="3">
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">{i1=v<sub>1</sub>, ..., im=vm, in=tn, ...} <span style="font-family: arial;">⇒</span> {i1=v<sub>1</sub>, ..., im=vm, in=tn', ...}</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub> <span style="font-family: arial;">⇒</span> t<sub>1</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Proj1)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub>.i <span style="font-family: arial;">⇒</span> t<sub>1</sub>'.i</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(ST_ProjRcd)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">{..., i=vi, ...}.i <span style="font-family: arial;">⇒</span> vi</td>
<td></td>
</td>
</table></center> Typing:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> t<sub>1</sub> : T<sub>1</sub> ... <span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> tn : Tn</td>
<td class="infrulenamecol" rowspan="3">
(T_Rcd)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> {i1=t<sub>1</sub>, ..., in=tn} : {i1:T<sub>1</sub>, ..., in:Tn}</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> t : {..., i:Ti, ...}</td>
<td class="infrulenamecol" rowspan="3">
(T_Proj)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> t.i : Ti</td>
<td></td>
</td>
</table></center>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab799"></a><h1 class="section">Formalizing Records</h1>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Module</span> <span class="id" type="var">STLCExtendedRecords</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab800"></a><h3 class="section">Syntax and Operational Semantics</h3>
<div class="paragraph"> </div>
The most obvious way to formalize the syntax of record types would
be this:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Module</span> <span class="id" type="var">FirstTry</span>.<br/>
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">alist</span> (<span class="id" type="var">X</span> : <span class="id" type="keyword">Type</span>) := <span class="id" type="var">list</span> (<span class="id" type="var">id</span> × <span class="id" type="var">X</span>).<br/>
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">ty</span> : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">TBase</span> : <span class="id" type="var">id</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ty</span><br/>
| <span class="id" type="var">TArrow</span> : <span class="id" type="var">ty</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ty</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ty</span><br/>
| <span class="id" type="var">TRcd</span> : (<span class="id" type="var">alist</span> <span class="id" type="var">ty</span>) <span style="font-family: arial;">→</span> <span class="id" type="var">ty</span>.<br/>
<br/>
</div>
<div class="doc">
Unfortunately, we encounter here a limitation in Coq: this type
does not automatically give us the induction principle we expect
the induction hypothesis in the <span class="inlinecode"><span class="id" type="var">TRcd</span></span> case doesn't give us
any information about the <span class="inlinecode"><span class="id" type="var">ty</span></span> elements of the list, making it
useless for the proofs we want to do.
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* Check ty_ind. <br/>
====><br/>
ty_ind : <br/>
forall P : ty -> Prop,<br/>
(forall i : id, P (TBase i)) -><br/>
(forall t : ty, P t -> forall t0 : ty, P t0 -> P (TArrow t t0)) -><br/>
(forall a : alist ty, P (TRcd a)) -> <span class="comment">(* ??? *)</span><br/>
forall t : ty, P t<br/>
*)</span><br/>
<br/>
<span class="id" type="keyword">End</span> <span class="id" type="var">FirstTry</span>.<br/>
<br/>
</div>
<div class="doc">
It is possible to get a better induction principle out of Coq, but
the details of how this is done are not very pretty, and it is not
as intuitive to use as the ones Coq generates automatically for
simple <span class="inlinecode"><span class="id" type="keyword">Inductive</span></span> definitions.
<div class="paragraph"> </div>
Fortunately, there is a different way of formalizing records that
is, in some ways, even simpler and more natural: instead of using
the existing <span class="inlinecode"><span class="id" type="var">list</span></span> type, we can essentially include its
constructors ("nil" and "cons") in the syntax of types.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">ty</span> : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">TBase</span> : <span class="id" type="var">id</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ty</span><br/>
| <span class="id" type="var">TArrow</span> : <span class="id" type="var">ty</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ty</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ty</span><br/>
| <span class="id" type="var">TRNil</span> : <span class="id" type="var">ty</span><br/>
| <span class="id" type="var">TRCons</span> : <span class="id" type="var">id</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ty</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ty</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ty</span>.<br/>
<br/>
<span class="id" type="keyword">Tactic Notation</span> "T_cases" <span class="id" type="var">tactic</span>(<span class="id" type="var">first</span>) <span class="id" type="var">ident</span>(<span class="id" type="var">c</span>) :=<br/>
<span class="id" type="var">first</span>;<br/>
[ <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "TBase" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "TArrow"<br/>
| <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "TRNil" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "TRCons" ].<br/>
<br/>
</div>
<div class="doc">
Similarly, at the level of terms, we have constructors <span class="inlinecode"><span class="id" type="var">trnil</span></span>
the empty record — and <span class="inlinecode"><span class="id" type="var">trcons</span></span>, which adds a single field to
the front of a list of fields.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">tm</span> : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">tvar</span> : <span class="id" type="var">id</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span><br/>
| <span class="id" type="var">tapp</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span><br/>
| <span class="id" type="var">tabs</span> : <span class="id" type="var">id</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ty</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span><br/>
<span class="comment">(* records *)</span><br/>
| <span class="id" type="var">tproj</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">id</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span><br/>
| <span class="id" type="var">trnil</span> : <span class="id" type="var">tm</span><br/>
| <span class="id" type="var">trcons</span> : <span class="id" type="var">id</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span>.<br/>
<br/>
<span class="id" type="keyword">Tactic Notation</span> "t_cases" <span class="id" type="var">tactic</span>(<span class="id" type="var">first</span>) <span class="id" type="var">ident</span>(<span class="id" type="var">c</span>) :=<br/>
<span class="id" type="var">first</span>;<br/>
[ <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "tvar" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "tapp" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "tabs"<br/>
| <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "tproj" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "trnil" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "trcons" ].<br/>
<br/>
</div>
<div class="doc">
Some variables, for examples...
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> <span class="id" type="var">a</span> := (<span class="id" type="var">Id</span> 0).<br/>
<span class="id" type="keyword">Notation</span> <span class="id" type="var">f</span> := (<span class="id" type="var">Id</span> 1).<br/>
<span class="id" type="keyword">Notation</span> <span class="id" type="var">g</span> := (<span class="id" type="var">Id</span> 2).<br/>
<span class="id" type="keyword">Notation</span> <span class="id" type="var">l</span> := (<span class="id" type="var">Id</span> 3).<br/>
<span class="id" type="keyword">Notation</span> <span class="id" type="var">A</span> := (<span class="id" type="var">TBase</span> (<span class="id" type="var">Id</span> 4)).<br/>
<span class="id" type="keyword">Notation</span> <span class="id" type="var">B</span> := (<span class="id" type="var">TBase</span> (<span class="id" type="var">Id</span> 5)).<br/>
<span class="id" type="keyword">Notation</span> <span class="id" type="var">k</span> := (<span class="id" type="var">Id</span> 6).<br/>
<span class="id" type="keyword">Notation</span> <span class="id" type="var">i1</span> := (<span class="id" type="var">Id</span> 7).<br/>
<span class="id" type="keyword">Notation</span> <span class="id" type="var">i2</span> := (<span class="id" type="var">Id</span> 8).<br/>
<br/>
</div>
<div class="doc">
<span class="inlinecode">{</span> <span class="inlinecode"><span class="id" type="var">i1</span>:<span class="id" type="var">A</span></span> <span class="inlinecode">}</span>
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* Check (TRCons i1 A TRNil). *)</span><br/>
<br/>
</div>
<div class="doc">
<span class="inlinecode">{</span> <span class="inlinecode"><span class="id" type="var">i1</span>:<span class="id" type="var">A</span><span style="font-family: arial;">→</span><span class="id" type="var">B</span>,</span> <span class="inlinecode"><span class="id" type="var">i2</span>:<span class="id" type="var">A</span></span> <span class="inlinecode">}</span>
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* Check (TRCons i1 (TArrow A B) <br/>
(TRCons i2 A TRNil)). *)</span><br/>
<br/>
</div>
<div class="doc">
<a name="lab801"></a><h3 class="section">Well-Formedness</h3>
<div class="paragraph"> </div>
Generalizing our abstract syntax for records (from lists to the
nil/cons presentation) introduces the possibility of writing
strange types like this
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">weird_type</span> := <span class="id" type="var">TRCons</span> <span class="id" type="var">X</span> <span class="id" type="var">A</span> <span class="id" type="var">B</span>.<br/>
<br/>
</div>
<div class="doc">
where the "tail" of a record type is not actually a record type!
<div class="paragraph"> </div>
We'll structure our typing judgement so that no ill-formed types
like <span class="inlinecode"><span class="id" type="var">weird_type</span></span> are assigned to terms. To support this, we
define <span class="inlinecode"><span class="id" type="var">record_ty</span></span> and <span class="inlinecode"><span class="id" type="var">record_tm</span></span>, which identify record types
and terms, and <span class="inlinecode"><span class="id" type="var">well_formed_ty</span></span> which rules out the ill-formed
types.
<div class="paragraph"> </div>
First, a type is a record type if it is built with just <span class="inlinecode"><span class="id" type="var">TRNil</span></span>
and <span class="inlinecode"><span class="id" type="var">TRCons</span></span> at the outermost level.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">record_ty</span> : <span class="id" type="var">ty</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">RTnil</span> : <br/>
<span class="id" type="var">record_ty</span> <span class="id" type="var">TRNil</span><br/>
| <span class="id" type="var">RTcons</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">i</span> <span class="id" type="var">T<sub>1</sub></span> <span class="id" type="var">T<sub>2</sub></span>,<br/>
<span class="id" type="var">record_ty</span> (<span class="id" type="var">TRCons</span> <span class="id" type="var">i</span> <span class="id" type="var">T<sub>1</sub></span> <span class="id" type="var">T<sub>2</sub></span>).<br/>
<br/>
</div>
<div class="doc">
Similarly, a term is a record term if it is built with <span class="inlinecode"><span class="id" type="var">trnil</span></span>
and <span class="inlinecode"><span class="id" type="var">trcons</span></span>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">record_tm</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">rtnil</span> :<br/>
<span class="id" type="var">record_tm</span> <span class="id" type="var">trnil</span><br/>
| <span class="id" type="var">rtcons</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">i</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>,<br/>
<span class="id" type="var">record_tm</span> (<span class="id" type="var">trcons</span> <span class="id" type="var">i</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>).<br/>
<br/>
</div>
<div class="doc">
Note that <span class="inlinecode"><span class="id" type="var">record_ty</span></span> and <span class="inlinecode"><span class="id" type="var">record_tm</span></span> are not recursive — they
just check the outermost constructor. The <span class="inlinecode"><span class="id" type="var">well_formed_ty</span></span>
property, on the other hand, verifies that the whole type is well
formed in the sense that the tail of every record (the second
argument to <span class="inlinecode"><span class="id" type="var">TRCons</span></span>) is a record.
<div class="paragraph"> </div>
Of course, we should also be concerned about ill-formed terms, not
just types; but typechecking can rules those out without the help
of an extra <span class="inlinecode"><span class="id" type="var">well_formed_tm</span></span> definition because it already
examines the structure of terms. LATER : should they fill in part of this as an exercise? We
didn't give rules for it above
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">well_formed_ty</span> : <span class="id" type="var">ty</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">wfTBase</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">i</span>,<br/>
<span class="id" type="var">well_formed_ty</span> (<span class="id" type="var">TBase</span> <span class="id" type="var">i</span>)<br/>
| <span class="id" type="var">wfTArrow</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">T<sub>1</sub></span> <span class="id" type="var">T<sub>2</sub></span>,<br/>
<span class="id" type="var">well_formed_ty</span> <span class="id" type="var">T<sub>1</sub></span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">well_formed_ty</span> <span class="id" type="var">T<sub>2</sub></span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">well_formed_ty</span> (<span class="id" type="var">TArrow</span> <span class="id" type="var">T<sub>1</sub></span> <span class="id" type="var">T<sub>2</sub></span>)<br/>
| <span class="id" type="var">wfTRNil</span> :<br/>
<span class="id" type="var">well_formed_ty</span> <span class="id" type="var">TRNil</span><br/>
| <span class="id" type="var">wfTRCons</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">i</span> <span class="id" type="var">T<sub>1</sub></span> <span class="id" type="var">T<sub>2</sub></span>,<br/>
<span class="id" type="var">well_formed_ty</span> <span class="id" type="var">T<sub>1</sub></span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">well_formed_ty</span> <span class="id" type="var">T<sub>2</sub></span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">record_ty</span> <span class="id" type="var">T<sub>2</sub></span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">well_formed_ty</span> (<span class="id" type="var">TRCons</span> <span class="id" type="var">i</span> <span class="id" type="var">T<sub>1</sub></span> <span class="id" type="var">T<sub>2</sub></span>).<br/>
<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="var">Constructors</span> <span class="id" type="var">record_ty</span> <span class="id" type="var">record_tm</span> <span class="id" type="var">well_formed_ty</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab802"></a><h3 class="section">Substitution</h3>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="tactic">subst</span> (<span class="id" type="var">x</span>:<span class="id" type="var">id</span>) (<span class="id" type="var">s</span>:<span class="id" type="var">tm</span>) (<span class="id" type="var">t</span>:<span class="id" type="var">tm</span>) : <span class="id" type="var">tm</span> :=<br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">t</span> <span class="id" type="keyword">with</span><br/>
| <span class="id" type="var">tvar</span> <span class="id" type="var">y</span> ⇒ <span class="id" type="keyword">if</span> <span class="id" type="var">eq_id_dec</span> <span class="id" type="var">x</span> <span class="id" type="var">y</span> <span class="id" type="keyword">then</span> <span class="id" type="var">s</span> <span class="id" type="keyword">else</span> <span class="id" type="var">t</span><br/>
| <span class="id" type="var">tabs</span> <span class="id" type="var">y</span> <span class="id" type="var">T</span> <span class="id" type="var">t<sub>1</sub></span> ⇒ <span class="id" type="var">tabs</span> <span class="id" type="var">y</span> <span class="id" type="var">T</span> (<span class="id" type="keyword">if</span> <span class="id" type="var">eq_id_dec</span> <span class="id" type="var">x</span> <span class="id" type="var">y</span> <span class="id" type="keyword">then</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="keyword">else</span> (<span class="id" type="tactic">subst</span> <span class="id" type="var">x</span> <span class="id" type="var">s</span> <span class="id" type="var">t<sub>1</sub></span>))<br/>
| <span class="id" type="var">tapp</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> ⇒ <span class="id" type="var">tapp</span> (<span class="id" type="tactic">subst</span> <span class="id" type="var">x</span> <span class="id" type="var">s</span> <span class="id" type="var">t<sub>1</sub></span>) (<span class="id" type="tactic">subst</span> <span class="id" type="var">x</span> <span class="id" type="var">s</span> <span class="id" type="var">t<sub>2</sub></span>)<br/>
| <span class="id" type="var">tproj</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">i</span> ⇒ <span class="id" type="var">tproj</span> (<span class="id" type="tactic">subst</span> <span class="id" type="var">x</span> <span class="id" type="var">s</span> <span class="id" type="var">t<sub>1</sub></span>) <span class="id" type="var">i</span><br/>
| <span class="id" type="var">trnil</span> ⇒ <span class="id" type="var">trnil</span><br/>
| <span class="id" type="var">trcons</span> <span class="id" type="var">i</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">tr1</span> ⇒ <span class="id" type="var">trcons</span> <span class="id" type="var">i</span> (<span class="id" type="tactic">subst</span> <span class="id" type="var">x</span> <span class="id" type="var">s</span> <span class="id" type="var">t<sub>1</sub></span>) (<span class="id" type="tactic">subst</span> <span class="id" type="var">x</span> <span class="id" type="var">s</span> <span class="id" type="var">tr1</span>)<br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
<span class="id" type="keyword">Notation</span> "'[' x ':=' s ']' t" := (<span class="id" type="tactic">subst</span> <span class="id" type="var">x</span> <span class="id" type="var">s</span> <span class="id" type="var">t</span>) (<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 20).<br/>
<br/>
</div>
<div class="doc">
<a name="lab803"></a><h3 class="section">Reduction</h3>
<div class="paragraph"> </div>
Next we define the values of our language. A record is a value if
all of its fields are.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">value</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">v_abs</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span> <span class="id" type="var">T<sub>11</sub></span> <span class="id" type="var">t<sub>12</sub></span>,<br/>
<span class="id" type="var">value</span> (<span class="id" type="var">tabs</span> <span class="id" type="var">x</span> <span class="id" type="var">T<sub>11</sub></span> <span class="id" type="var">t<sub>12</sub></span>)<br/>
| <span class="id" type="var">v_rnil</span> : <span class="id" type="var">value</span> <span class="id" type="var">trnil</span><br/>
| <span class="id" type="var">v_rcons</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">i</span> <span class="id" type="var">v<sub>1</sub></span> <span class="id" type="var">vr</span>,<br/>
<span class="id" type="var">value</span> <span class="id" type="var">v<sub>1</sub></span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">value</span> <span class="id" type="var">vr</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">value</span> (<span class="id" type="var">trcons</span> <span class="id" type="var">i</span> <span class="id" type="var">v<sub>1</sub></span> <span class="id" type="var">vr</span>).<br/>
<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="var">Constructors</span> <span class="id" type="var">value</span>.<br/>
<br/>
</div>
<div class="doc">
Utility functions for extracting one field from record type or
term:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">Tlookup</span> (<span class="id" type="var">i</span>:<span class="id" type="var">id</span>) (<span class="id" type="var">Tr</span>:<span class="id" type="var">ty</span>) : <span class="id" type="var">option</span> <span class="id" type="var">ty</span> :=<br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">Tr</span> <span class="id" type="keyword">with</span><br/>
| <span class="id" type="var">TRCons</span> <span class="id" type="var">i'</span> <span class="id" type="var">T</span> <span class="id" type="var">Tr'</span> ⇒ <span class="id" type="keyword">if</span> <span class="id" type="var">eq_id_dec</span> <span class="id" type="var">i</span> <span class="id" type="var">i'</span> <span class="id" type="keyword">then</span> <span class="id" type="var">Some</span> <span class="id" type="var">T</span> <span class="id" type="keyword">else</span> <span class="id" type="var">Tlookup</span> <span class="id" type="var">i</span> <span class="id" type="var">Tr'</span><br/>
| <span class="id" type="var">_</span> ⇒ <span class="id" type="var">None</span><br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">tlookup</span> (<span class="id" type="var">i</span>:<span class="id" type="var">id</span>) (<span class="id" type="var">tr</span>:<span class="id" type="var">tm</span>) : <span class="id" type="var">option</span> <span class="id" type="var">tm</span> :=<br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">tr</span> <span class="id" type="keyword">with</span><br/>
| <span class="id" type="var">trcons</span> <span class="id" type="var">i'</span> <span class="id" type="var">t</span> <span class="id" type="var">tr'</span> ⇒ <span class="id" type="keyword">if</span> <span class="id" type="var">eq_id_dec</span> <span class="id" type="var">i</span> <span class="id" type="var">i'</span> <span class="id" type="keyword">then</span> <span class="id" type="var">Some</span> <span class="id" type="var">t</span> <span class="id" type="keyword">else</span> <span class="id" type="var">tlookup</span> <span class="id" type="var">i</span> <span class="id" type="var">tr'</span><br/>
| <span class="id" type="var">_</span> ⇒ <span class="id" type="var">None</span><br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
</div>
<div class="doc">
The <span class="inlinecode"><span class="id" type="var">step</span></span> function uses the term-level lookup function (for the
projection rule), while the type-level lookup is needed for
<span class="inlinecode"><span class="id" type="var">has_type</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Reserved Notation</span> "t<sub>1</sub> '<span style="font-family: arial;">⇒</span>' t<sub>2</sub>" (<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 40).<br/>
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">step</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">ST_AppAbs</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span> <span class="id" type="var">T<sub>11</sub></span> <span class="id" type="var">t<sub>12</sub></span> <span class="id" type="var">v<sub>2</sub></span>,<br/>
<span class="id" type="var">value</span> <span class="id" type="var">v<sub>2</sub></span> <span style="font-family: arial;">→</span><br/>
(<span class="id" type="var">tapp</span> (<span class="id" type="var">tabs</span> <span class="id" type="var">x</span> <span class="id" type="var">T<sub>11</sub></span> <span class="id" type="var">t<sub>12</sub></span>) <span class="id" type="var">v<sub>2</sub></span>) <span style="font-family: arial;">⇒</span> ([<span class="id" type="var">x</span>:=<span class="id" type="var">v<sub>2</sub></span>]<span class="id" type="var">t<sub>12</sub></span>)<br/>
| <span class="id" type="var">ST_App1</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">t<sub>2</sub></span>,<br/>
<span class="id" type="var">t<sub>1</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t<sub>1</sub>'</span> <span style="font-family: arial;">→</span><br/>
(<span class="id" type="var">tapp</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>) <span style="font-family: arial;">⇒</span> (<span class="id" type="var">tapp</span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">t<sub>2</sub></span>)<br/>
| <span class="id" type="var">ST_App2</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">v<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> <span class="id" type="var">t<sub>2</sub>'</span>,<br/>
<span class="id" type="var">value</span> <span class="id" type="var">v<sub>1</sub></span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t<sub>2</sub>'</span> <span style="font-family: arial;">→</span><br/>
(<span class="id" type="var">tapp</span> <span class="id" type="var">v<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>) <span style="font-family: arial;">⇒</span> (<span class="id" type="var">tapp</span> <span class="id" type="var">v<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub>'</span>)<br/>
| <span class="id" type="var">ST_Proj1</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">i</span>,<br/>
<span class="id" type="var">t<sub>1</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t<sub>1</sub>'</span> <span style="font-family: arial;">→</span><br/>
(<span class="id" type="var">tproj</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">i</span>) <span style="font-family: arial;">⇒</span> (<span class="id" type="var">tproj</span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">i</span>)<br/>
| <span class="id" type="var">ST_ProjRcd</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">tr</span> <span class="id" type="var">i</span> <span class="id" type="var">vi</span>,<br/>
<span class="id" type="var">value</span> <span class="id" type="var">tr</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">tlookup</span> <span class="id" type="var">i</span> <span class="id" type="var">tr</span> = <span class="id" type="var">Some</span> <span class="id" type="var">vi</span> <span style="font-family: arial;">→</span><br/>
(<span class="id" type="var">tproj</span> <span class="id" type="var">tr</span> <span class="id" type="var">i</span>) <span style="font-family: arial;">⇒</span> <span class="id" type="var">vi</span><br/>
| <span class="id" type="var">ST_Rcd_Head</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">i</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">tr2</span>,<br/>
<span class="id" type="var">t<sub>1</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t<sub>1</sub>'</span> <span style="font-family: arial;">→</span><br/>
(<span class="id" type="var">trcons</span> <span class="id" type="var">i</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">tr2</span>) <span style="font-family: arial;">⇒</span> (<span class="id" type="var">trcons</span> <span class="id" type="var">i</span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">tr2</span>)<br/>
| <span class="id" type="var">ST_Rcd_Tail</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">i</span> <span class="id" type="var">v<sub>1</sub></span> <span class="id" type="var">tr2</span> <span class="id" type="var">tr2'</span>,<br/>
<span class="id" type="var">value</span> <span class="id" type="var">v<sub>1</sub></span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">tr2</span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">tr2'</span> <span style="font-family: arial;">→</span><br/>
(<span class="id" type="var">trcons</span> <span class="id" type="var">i</span> <span class="id" type="var">v<sub>1</sub></span> <span class="id" type="var">tr2</span>) <span style="font-family: arial;">⇒</span> (<span class="id" type="var">trcons</span> <span class="id" type="var">i</span> <span class="id" type="var">v<sub>1</sub></span> <span class="id" type="var">tr2'</span>)<br/>
<br/>
<span class="id" type="keyword">where</span> "t<sub>1</sub> '<span style="font-family: arial;">⇒</span>' t<sub>2</sub>" := (<span class="id" type="var">step</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>).<br/>
<br/>
<span class="id" type="keyword">Tactic Notation</span> "step_cases" <span class="id" type="var">tactic</span>(<span class="id" type="var">first</span>) <span class="id" type="var">ident</span>(<span class="id" type="var">c</span>) :=<br/>
<span class="id" type="var">first</span>;<br/>
[ <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "ST_AppAbs" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "ST_App1" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "ST_App2"<br/>
| <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "ST_Proj1" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "ST_ProjRcd"<br/>
| <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "ST_Rcd_Head" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "ST_Rcd_Tail" ].<br/>
<br/>
<span class="id" type="keyword">Notation</span> <span class="id" type="var">multistep</span> := (<span class="id" type="var">multi</span> <span class="id" type="var">step</span>).<br/>
<span class="id" type="keyword">Notation</span> "t<sub>1</sub> '<span style="font-family: arial;">⇒*</span>' t<sub>2</sub>" := (<span class="id" type="var">multistep</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="tactic">at</span> <span class="id" type="var">level</span> 40).<br/>
<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="var">Constructors</span> <span class="id" type="var">step</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab804"></a><h3 class="section">Typing</h3>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">context</span> := <span class="id" type="var">partial_map</span> <span class="id" type="var">ty</span>.<br/>
<br/>
</div>
<div class="doc">
Next we define the typing rules. These are nearly direct
transcriptions of the inference rules shown above. The only major
difference is the use of <span class="inlinecode"><span class="id" type="var">well_formed_ty</span></span>. In the informal
presentation we used a grammar that only allowed well formed
record types, so we didn't have to add a separate check.
<div class="paragraph"> </div>
We'd like to set things up so that that whenever <span class="inlinecode"><span class="id" type="var">has_type</span></span> <span class="inlinecode"><span style="font-family: serif; font-size:85%;">Γ</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span>
<span class="inlinecode"><span class="id" type="var">T</span></span> holds, we also have <span class="inlinecode"><span class="id" type="var">well_formed_ty</span></span> <span class="inlinecode"><span class="id" type="var">T</span></span>. That is, <span class="inlinecode"><span class="id" type="var">has_type</span></span>
never assigns ill-formed types to terms. In fact, we prove this
theorem below.
<div class="paragraph"> </div>
However, we don't want to clutter the definition of <span class="inlinecode"><span class="id" type="var">has_type</span></span>
with unnecessary uses of <span class="inlinecode"><span class="id" type="var">well_formed_ty</span></span>. Instead, we place
<span class="inlinecode"><span class="id" type="var">well_formed_ty</span></span> checks only where needed - where an inductive
call to <span class="inlinecode"><span class="id" type="var">has_type</span></span> won't already be checking the well-formedness
of a type.
<div class="paragraph"> </div>
For example, we check <span class="inlinecode"><span class="id" type="var">well_formed_ty</span></span> <span class="inlinecode"><span class="id" type="var">T</span></span> in the <span class="inlinecode"><span class="id" type="var">T_Var</span></span> case,
because there is no inductive <span class="inlinecode"><span class="id" type="var">has_type</span></span> call that would
enforce this. Similarly, in the <span class="inlinecode"><span class="id" type="var">T_Abs</span></span> case, we require a
proof of <span class="inlinecode"><span class="id" type="var">well_formed_ty</span></span> <span class="inlinecode"><span class="id" type="var">T<sub>11</sub></span></span> because the inductive call to
<span class="inlinecode"><span class="id" type="var">has_type</span></span> only guarantees that <span class="inlinecode"><span class="id" type="var">T<sub>12</sub></span></span> is well-formed.
<div class="paragraph"> </div>
In the rules you must write, the only necessary <span class="inlinecode"><span class="id" type="var">well_formed_ty</span></span>
check comes in the <span class="inlinecode"><span class="id" type="var">tnil</span></span> case.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Reserved Notation</span> "Gamma '<span style="font-family: arial;">⊢</span>' t '∈' T" (<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 40).<br/>
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">has_type</span> : <span class="id" type="var">context</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ty</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">T_Var</span> : <span style="font-family: arial;">∀</span><span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">x</span> <span class="id" type="var">T</span>,<br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">x</span> = <span class="id" type="var">Some</span> <span class="id" type="var">T</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">well_formed_ty</span> <span class="id" type="var">T</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> (<span class="id" type="var">tvar</span> <span class="id" type="var">x</span>) ∈ <span class="id" type="var">T</span><br/>
| <span class="id" type="var">T_Abs</span> : <span style="font-family: arial;">∀</span><span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">x</span> <span class="id" type="var">T<sub>11</sub></span> <span class="id" type="var">T<sub>12</sub></span> <span class="id" type="var">t<sub>12</sub></span>,<br/>
<span class="id" type="var">well_formed_ty</span> <span class="id" type="var">T<sub>11</sub></span> <span style="font-family: arial;">→</span><br/>
(<span class="id" type="var">extend</span> <span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">x</span> <span class="id" type="var">T<sub>11</sub></span>) <span style="font-family: arial;">⊢</span> <span class="id" type="var">t<sub>12</sub></span> ∈ <span class="id" type="var">T<sub>12</sub></span> <span style="font-family: arial;">→</span> <br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> (<span class="id" type="var">tabs</span> <span class="id" type="var">x</span> <span class="id" type="var">T<sub>11</sub></span> <span class="id" type="var">t<sub>12</sub></span>) ∈ (<span class="id" type="var">TArrow</span> <span class="id" type="var">T<sub>11</sub></span> <span class="id" type="var">T<sub>12</sub></span>)<br/>
| <span class="id" type="var">T_App</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">T<sub>1</sub></span> <span class="id" type="var">T<sub>2</sub></span> <span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>,<br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> <span class="id" type="var">t<sub>1</sub></span> ∈ (<span class="id" type="var">TArrow</span> <span class="id" type="var">T<sub>1</sub></span> <span class="id" type="var">T<sub>2</sub></span>) <span style="font-family: arial;">→</span> <br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> <span class="id" type="var">t<sub>2</sub></span> ∈ <span class="id" type="var">T<sub>1</sub></span> <span style="font-family: arial;">→</span> <br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> (<span class="id" type="var">tapp</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>2</sub></span><br/>
<span class="comment">(* records: *)</span><br/>
| <span class="id" type="var">T_Proj</span> : <span style="font-family: arial;">∀</span><span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">i</span> <span class="id" type="var">t</span> <span class="id" type="var">Ti</span> <span class="id" type="var">Tr</span>,<br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> <span class="id" type="var">t</span> ∈ <span class="id" type="var">Tr</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">Tlookup</span> <span class="id" type="var">i</span> <span class="id" type="var">Tr</span> = <span class="id" type="var">Some</span> <span class="id" type="var">Ti</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> (<span class="id" type="var">tproj</span> <span class="id" type="var">t</span> <span class="id" type="var">i</span>) ∈ <span class="id" type="var">Ti</span><br/>
| <span class="id" type="var">T_RNil</span> : <span style="font-family: arial;">∀</span><span style="font-family: serif; font-size:85%;">Γ</span>,<br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> <span class="id" type="var">trnil</span> ∈ <span class="id" type="var">TRNil</span><br/>
| <span class="id" type="var">T_RCons</span> : <span style="font-family: arial;">∀</span><span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">i</span> <span class="id" type="var">t</span> <span class="id" type="var">T</span> <span class="id" type="var">tr</span> <span class="id" type="var">Tr</span>,<br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> <span class="id" type="var">t</span> ∈ <span class="id" type="var">T</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> <span class="id" type="var">tr</span> ∈ <span class="id" type="var">Tr</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">record_ty</span> <span class="id" type="var">Tr</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">record_tm</span> <span class="id" type="var">tr</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> (<span class="id" type="var">trcons</span> <span class="id" type="var">i</span> <span class="id" type="var">t</span> <span class="id" type="var">tr</span>) ∈ (<span class="id" type="var">TRCons</span> <span class="id" type="var">i</span> <span class="id" type="var">T</span> <span class="id" type="var">Tr</span>)<br/>
<br/>
<span class="id" type="keyword">where</span> "Gamma '<span style="font-family: arial;">⊢</span>' t '∈' T" := (<span class="id" type="var">has_type</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/>
<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="var">Constructors</span> <span class="id" type="var">has_type</span>.<br/>
<br/>
<span class="id" type="keyword">Tactic Notation</span> "has_type_cases" <span class="id" type="var">tactic</span>(<span class="id" type="var">first</span>) <span class="id" type="var">ident</span>(<span class="id" type="var">c</span>) :=<br/>
<span class="id" type="var">first</span>;<br/>
[ <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "T_Var" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "T_Abs" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "T_App"<br/>
| <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "T_Proj" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "T_RNil" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "T_RCons" ].<br/>
<br/>
</div>
<div class="doc">
<a name="lab805"></a><h2 class="section">Examples</h2>
<div class="paragraph"> </div>
<a name="lab806"></a><h4 class="section">Exercise: 2 stars (examples)</h4>
Finish the proofs.
<div class="paragraph"> </div>
Feel free to use Coq's automation features in this proof.
However, if you are not confident about how the type system works,
you may want to carry out the proof first using the basic
features (<span class="inlinecode"><span class="id" type="tactic">apply</span></span> instead of <span class="inlinecode"><span class="id" type="tactic">eapply</span></span>, in particular) and then
perhaps compress it using automation.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">typing_example_2</span> : <br/>
<span class="id" type="var">empty</span> <span style="font-family: arial;">⊢</span> <br/>
(<span class="id" type="var">tapp</span> (<span class="id" type="var">tabs</span> <span class="id" type="var">a</span> (<span class="id" type="var">TRCons</span> <span class="id" type="var">i1</span> (<span class="id" type="var">TArrow</span> <span class="id" type="var">A</span> <span class="id" type="var">A</span>)<br/>
(<span class="id" type="var">TRCons</span> <span class="id" type="var">i2</span> (<span class="id" type="var">TArrow</span> <span class="id" type="var">B</span> <span class="id" type="var">B</span>)<br/>
<span class="id" type="var">TRNil</span>))<br/>
(<span class="id" type="var">tproj</span> (<span class="id" type="var">tvar</span> <span class="id" type="var">a</span>) <span class="id" type="var">i2</span>))<br/>
(<span class="id" type="var">trcons</span> <span class="id" type="var">i1</span> (<span class="id" type="var">tabs</span> <span class="id" type="var">a</span> <span class="id" type="var">A</span> (<span class="id" type="var">tvar</span> <span class="id" type="var">a</span>)) <br/>
(<span class="id" type="var">trcons</span> <span class="id" type="var">i2</span> (<span class="id" type="var">tabs</span> <span class="id" type="var">a</span> <span class="id" type="var">B</span> (<span class="id" type="var">tvar</span> <span class="id" type="var">a</span>))<br/>
<span class="id" type="var">trnil</span>))) ∈<br/>
(<span class="id" type="var">TArrow</span> <span class="id" type="var">B</span> <span class="id" type="var">B</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
</div>
<div class="doc">
Before starting to prove this fact (or the one above!), make sure
you understand what it is saying.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">typing_nonexample</span> : <br/>
¬ <span style="font-family: arial;">∃</span><span class="id" type="var">T</span>,<br/>
(<span class="id" type="var">extend</span> <span class="id" type="var">empty</span> <span class="id" type="var">a</span> (<span class="id" type="var">TRCons</span> <span class="id" type="var">i2</span> (<span class="id" type="var">TArrow</span> <span class="id" type="var">A</span> <span class="id" type="var">A</span>) <br/>
<span class="id" type="var">TRNil</span>)) <span style="font-family: arial;">⊢</span><br/>
(<span class="id" type="var">trcons</span> <span class="id" type="var">i1</span> (<span class="id" type="var">tabs</span> <span class="id" type="var">a</span> <span class="id" type="var">B</span> (<span class="id" type="var">tvar</span> <span class="id" type="var">a</span>)) (<span class="id" type="var">tvar</span> <span class="id" type="var">a</span>)) ∈<br/>
<span class="id" type="var">T</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">typing_nonexample_2</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">y</span>,<br/>
¬ <span style="font-family: arial;">∃</span><span class="id" type="var">T</span>,<br/>
(<span class="id" type="var">extend</span> <span class="id" type="var">empty</span> <span class="id" type="var">y</span> <span class="id" type="var">A</span>) <span style="font-family: arial;">⊢</span><br/>
(<span class="id" type="var">tapp</span> (<span class="id" type="var">tabs</span> <span class="id" type="var">a</span> (<span class="id" type="var">TRCons</span> <span class="id" type="var">i1</span> <span class="id" type="var">A</span> <span class="id" type="var">TRNil</span>)<br/>
(<span class="id" type="var">tproj</span> (<span class="id" type="var">tvar</span> <span class="id" type="var">a</span>) <span class="id" type="var">i1</span>))<br/>
(<span class="id" type="var">trcons</span> <span class="id" type="var">i1</span> (<span class="id" type="var">tvar</span> <span class="id" type="var">y</span>) (<span class="id" type="var">trcons</span> <span class="id" type="var">i2</span> (<span class="id" type="var">tvar</span> <span class="id" type="var">y</span>) <span class="id" type="var">trnil</span>))) ∈<br/>
<span class="id" type="var">T</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab807"></a><h2 class="section">Properties of Typing</h2>
<div class="paragraph"> </div>
The proofs of progress and preservation for this system are
essentially the same as for the pure simply typed lambda-calculus,
but we need to add some technical lemmas involving records.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab808"></a><h3 class="section">Well-Formedness</h3>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">wf_rcd_lookup</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">i</span> <span class="id" type="var">T</span> <span class="id" type="var">Ti</span>,<br/>
<span class="id" type="var">well_formed_ty</span> <span class="id" type="var">T</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">Tlookup</span> <span class="id" type="var">i</span> <span class="id" type="var">T</span> = <span class="id" type="var">Some</span> <span class="id" type="var">Ti</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">well_formed_ty</span> <span class="id" type="var">Ti</span>.<br/>
<span class="id" type="keyword">Proof</span> <span class="id" type="keyword">with</span> <span class="id" type="tactic">eauto</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">i</span> <span class="id" type="var">T</span>.<br/>
<span class="id" type="var">T_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">T</span>) <span class="id" type="var">Case</span>; <span class="id" type="tactic">intros</span>; <span class="id" type="tactic">try</span> <span class="id" type="var">solve</span> <span class="id" type="tactic">by</span> <span class="id" type="tactic">inversion</span>.<br/>
<span class="id" type="var">Case</span> "TRCons".<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">unfold</span> <span class="id" type="var">Tlookup</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H0</span>.<br/>
<span class="id" type="tactic">destruct</span> (<span class="id" type="var">eq_id_dec</span> <span class="id" type="var">i</span> <span class="id" type="var">i0</span>)...<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H0</span>. <span class="id" type="tactic">subst</span>... <span class="id" type="keyword">Qed</span>.<br/>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">step_preserves_record_tm</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">tr</span> <span class="id" type="var">tr'</span>,<br/>
<span class="id" type="var">record_tm</span> <span class="id" type="var">tr</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">tr</span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">tr'</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">record_tm</span> <span class="id" type="var">tr'</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">tr</span> <span class="id" type="var">tr'</span> <span class="id" type="var">Hrt</span> <span class="id" type="var">Hstp</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">Hrt</span>; <span class="id" type="tactic">subst</span>; <span class="id" type="tactic">inversion</span> <span class="id" type="var">Hstp</span>; <span class="id" type="tactic">subst</span>; <span class="id" type="tactic">auto</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">has_type__wf</span> : <span style="font-family: arial;">∀</span><span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">t</span> <span class="id" type="var">T</span>,<br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> <span class="id" type="var">t</span> ∈ <span class="id" type="var">T</span> <span style="font-family: arial;">→</span> <span class="id" type="var">well_formed_ty</span> <span class="id" type="var">T</span>.<br/>
<span class="id" type="keyword">Proof</span> <span class="id" type="keyword">with</span> <span class="id" type="tactic">eauto</span>.<br/>
<span class="id" type="tactic">intros</span> <span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">t</span> <span class="id" type="var">T</span> <span class="id" type="var">Htyp</span>.<br/>
<span class="id" type="var">has_type_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">Htyp</span>) <span class="id" type="var">Case</span>...<br/>
<span class="id" type="var">Case</span> "T_App".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">IHHtyp1</span>...<br/>
<span class="id" type="var">Case</span> "T_Proj".<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">wf_rcd_lookup</span>...<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab809"></a><h3 class="section">Field Lookup</h3>
<div class="paragraph"> </div>
Lemma: If <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">v</span></span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" type="var">T</span></span> and <span class="inlinecode"><span class="id" type="var">Tlookup</span></span> <span class="inlinecode"><span class="id" type="var">i</span></span> <span class="inlinecode"><span class="id" type="var">T</span></span> returns <span class="inlinecode"><span class="id" type="var">Some</span></span> <span class="inlinecode"><span class="id" type="var">Ti</span></span>,
then <span class="inlinecode"><span class="id" type="var">tlookup</span></span> <span class="inlinecode"><span class="id" type="var">i</span></span> <span class="inlinecode"><span class="id" type="var">v</span></span> returns <span class="inlinecode"><span class="id" type="var">Some</span></span> <span class="inlinecode"><span class="id" type="var">ti</span></span> for some term <span class="inlinecode"><span class="id" type="var">ti</span></span> such
that <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">ti</span></span> <span class="inlinecode">∈</span> <span class="inlinecode"><span class="id" type="var">Ti</span></span>.
<div class="paragraph"> </div>
Proof: By induction on the typing derivation <span class="inlinecode"><span class="id" type="var">Htyp</span></span>. Since
<span class="inlinecode"><span class="id" type="var">Tlookup</span></span> <span class="inlinecode"><span class="id" type="var">i</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">Some</span></span> <span class="inlinecode"><span class="id" type="var">Ti</span></span>, <span class="inlinecode"><span class="id" type="var">T</span></span> must be a record type, this and
the fact that <span class="inlinecode"><span class="id" type="var">v</span></span> is a value eliminate most cases by inspection,
leaving only the <span class="inlinecode"><span class="id" type="var">T_RCons</span></span> case.
<div class="paragraph"> </div>
If the last step in the typing derivation is by <span class="inlinecode"><span class="id" type="var">T_RCons</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">trcons</span></span> <span class="inlinecode"><span class="id" type="var">i0</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode"><span class="id" type="var">tr</span></span> and <span class="inlinecode"><span class="id" type="var">T</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">TRCons</span></span> <span class="inlinecode"><span class="id" type="var">i0</span></span> <span class="inlinecode"><span class="id" type="var">T</span></span> <span class="inlinecode"><span class="id" type="var">Tr</span></span> for some <span class="inlinecode"><span class="id" type="var">i0</span></span>,
<span class="inlinecode"><span class="id" type="var">t</span></span>, <span class="inlinecode"><span class="id" type="var">tr</span></span>, <span class="inlinecode"><span class="id" type="var">T</span></span> and <span class="inlinecode"><span class="id" type="var">Tr</span></span>.
<div class="paragraph"> </div>
This leaves two possiblities to consider - either <span class="inlinecode"><span class="id" type="var">i0</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">i</span></span> or
not.
<div class="paragraph"> </div>
<ul class="doclist">
<li> If <span class="inlinecode"><span class="id" type="var">i</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">i0</span></span>, then since <span class="inlinecode"><span class="id" type="var">Tlookup</span></span> <span class="inlinecode"><span class="id" type="var">i</span></span> <span class="inlinecode">(<span class="id" type="var">TRCons</span></span> <span class="inlinecode"><span class="id" type="var">i0</span></span> <span class="inlinecode"><span class="id" type="var">T</span></span> <span class="inlinecode"><span class="id" type="var">Tr</span>)</span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">Some</span></span>
<span class="inlinecode"><span class="id" type="var">Ti</span></span> we have <span class="inlinecode"><span class="id" type="var">T</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">Ti</span></span>. It follows that <span class="inlinecode"><span class="id" type="var">t</span></span> itself satisfies
the theorem.
<div class="paragraph"> </div>
</li>
<li> On the other hand, suppose <span class="inlinecode"><span class="id" type="var">i</span></span> <span class="inlinecode">≠</span> <span class="inlinecode"><span class="id" type="var">i0</span></span>. Then
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">Tlookup</span> <span class="id" type="var">i</span> <span class="id" type="var">T</span> = <span class="id" type="var">Tlookup</span> <span class="id" type="var">i</span> <span class="id" type="var">Tr</span>
<div class="paragraph"> </div>
</div>
and
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">tlookup</span> <span class="id" type="var">i</span> <span class="id" type="var">t</span> = <span class="id" type="var">tlookup</span> <span class="id" type="var">i</span> <span class="id" type="var">tr</span>,
<div class="paragraph"> </div>
</div>
so the result follows from the induction hypothesis. <font size=-2>☐</font>
</li>
</ul>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">lookup_field_in_value</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">v</span> <span class="id" type="var">T</span> <span class="id" type="var">i</span> <span class="id" type="var">Ti</span>,<br/>
<span class="id" type="var">value</span> <span class="id" type="var">v</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">empty</span> <span style="font-family: arial;">⊢</span> <span class="id" type="var">v</span> ∈ <span class="id" type="var">T</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">Tlookup</span> <span class="id" type="var">i</span> <span class="id" type="var">T</span> = <span class="id" type="var">Some</span> <span class="id" type="var">Ti</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">∃</span><span class="id" type="var">ti</span>, <span class="id" type="var">tlookup</span> <span class="id" type="var">i</span> <span class="id" type="var">v</span> = <span class="id" type="var">Some</span> <span class="id" type="var">ti</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">empty</span> <span style="font-family: arial;">⊢</span> <span class="id" type="var">ti</span> ∈ <span class="id" type="var">Ti</span>.<br/>
<span class="id" type="keyword">Proof</span> <span class="id" type="keyword">with</span> <span class="id" type="tactic">eauto</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">v</span> <span class="id" type="var">T</span> <span class="id" type="var">i</span> <span class="id" type="var">Ti</span> <span class="id" type="var">Hval</span> <span class="id" type="var">Htyp</span> <span class="id" type="var">Hget</span>.<br/>
<span class="id" type="var">remember</span> (@<span class="id" type="var">empty</span> <span class="id" type="var">ty</span>) <span class="id" type="keyword">as</span> <span style="font-family: serif; font-size:85%;">Γ</span>.<br/>
<span class="id" type="var">has_type_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">Htyp</span>) <span class="id" type="var">Case</span>; <span class="id" type="tactic">subst</span>; <span class="id" type="tactic">try</span> <span class="id" type="var">solve</span> <span class="id" type="tactic">by</span> <span class="id" type="tactic">inversion</span>...<br/>
<span class="id" type="var">Case</span> "T_RCons".<br/>
<span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hget</span>. <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">destruct</span> (<span class="id" type="var">eq_id_dec</span> <span class="id" type="var">i</span> <span class="id" type="var">i0</span>).<br/>
<span class="id" type="var">SCase</span> "i is first".<br/>
<span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">Hget</span>. <span class="id" type="tactic">subst</span>.<br/>
<span style="font-family: arial;">∃</span><span class="id" type="var">t</span>...<br/>
<span class="id" type="var">SCase</span> "get tail".<br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">IHHtyp2</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">vi</span> [<span class="id" type="var">Hgeti</span> <span class="id" type="var">Htypi</span>]]...<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">Hval</span>... <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab810"></a><h3 class="section">Progress</h3>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="tactic">progress</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t</span> <span class="id" type="var">T</span>, <br/>
<span class="id" type="var">empty</span> <span style="font-family: arial;">⊢</span> <span class="id" type="var">t</span> ∈ <span class="id" type="var">T</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">value</span> <span class="id" type="var">t</span> <span style="font-family: arial;">∨</span> <span style="font-family: arial;">∃</span><span class="id" type="var">t'</span>, <span class="id" type="var">t</span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t'</span>.<br/>
<span class="id" type="keyword">Proof</span> <span class="id" type="keyword">with</span> <span class="id" type="tactic">eauto</span>.<br/>
<span class="comment">(* Theorem: Suppose empty |- t : T. Then either<br/>
1. t is a value, or<br/>
2. t ==> t' for some t'.<br/>
Proof: By induction on the given typing derivation. *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">t</span> <span class="id" type="var">T</span> <span class="id" type="var">Ht</span>.<br/>
<span class="id" type="var">remember</span> (@<span class="id" type="var">empty</span> <span class="id" type="var">ty</span>) <span class="id" type="keyword">as</span> <span style="font-family: serif; font-size:85%;">Γ</span>.<br/>
<span class="id" type="tactic">generalize</span> <span class="id" type="tactic">dependent</span> <span class="id" type="var">HeqGamma</span>.<br/>
<span class="id" type="var">has_type_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">Ht</span>) <span class="id" type="var">Case</span>; <span class="id" type="tactic">intros</span> <span class="id" type="var">HeqGamma</span>; <span class="id" type="tactic">subst</span>.<br/>
<span class="id" type="var">Case</span> "T_Var".<br/>
<span class="comment">(* The final rule in the given typing derivation cannot be <span class="inlinecode"><span class="id" type="var">T_Var</span></span>,<br/>
since it can never be the case that <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">x</span></span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" type="var">T</span></span> (since the<br/>
context is empty). *)</span><br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">Case</span> "T_Abs".<br/>
<span class="comment">(* If the <span class="inlinecode"><span class="id" type="var">T_Abs</span></span> rule was the last used, then <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">tabs</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 class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span>,<br/>
which is a value. *)</span><br/>
<span class="id" type="var">left</span>...<br/>
<span class="id" type="var">Case</span> "T_App".<br/>
<span class="comment">(* If the last rule applied was T_App, then <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span>, and we know <br/>
from the form of the rule that<br/>
<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<sub>1</sub></span></span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" type="var">T<sub>1</sub></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><br/>
<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<sub>2</sub></span></span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" type="var">T<sub>1</sub></span></span><br/>
By the induction hypothesis, each of t<sub>1</sub> and t<sub>2</sub> either is a value <br/>
or can take a step. *)</span><br/>
<span class="id" type="var">right</span>.<br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">IHHt1</span>; <span class="id" type="tactic">subst</span>...<br/>
<span class="id" type="var">SCase</span> "t<sub>1</sub> is a value".<br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">IHHt2</span>; <span class="id" type="tactic">subst</span>...<br/>
<span class="id" type="var">SSCase</span> "t<sub>2</sub> is a value".<br/>
<span class="comment">(* If both <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> and <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> are values, then we know that <br/>
<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">tabs</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 class="inlinecode"><span class="id" type="var">t<sub>12</sub></span></span>, since abstractions are the only values<br/>
that can have an arrow type. But <br/>
<span class="inlinecode">(<span class="id" type="var">tabs</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 class="inlinecode"><span class="id" type="var">t<sub>12</sub></span>)</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">x</span>:=<span class="id" type="var">t<sub>2</sub></span>]<span class="id" type="var">t<sub>12</sub></span></span> by <span class="inlinecode"><span class="id" type="var">ST_AppAbs</span></span>. *)</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">try</span> (<span class="id" type="var">solve</span> <span class="id" type="tactic">by</span> <span class="id" type="tactic">inversion</span>).<br/>
<span style="font-family: arial;">∃</span>([<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>)...<br/>
<span class="id" type="var">SSCase</span> "t<sub>2</sub> steps".<br/>
<span class="comment">(* If <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> is a value and <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> <span class="inlinecode"><span style="font-family: arial;">⇒</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub>'</span></span>, then <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> <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 class="id" type="var">t<sub>2</sub>'</span></span> <br/>
by <span class="inlinecode"><span class="id" type="var">ST_App2</span></span>. *)</span><br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">H0</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">t<sub>2</sub>'</span> <span class="id" type="var">Hstp</span>]. <span style="font-family: arial;">∃</span>(<span class="id" type="var">tapp</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub>'</span>)...<br/>
<span class="id" type="var">SCase</span> "t<sub>1</sub> steps".<br/>
<span class="comment">(* Finally, If <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></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>, then <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> <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 class="id" type="var">t<sub>2</sub></span></span> by <span class="inlinecode"><span class="id" type="var">ST_App1</span></span>. *)</span><br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">Hstp</span>]. <span style="font-family: arial;">∃</span>(<span class="id" type="var">tapp</span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">t<sub>2</sub></span>)...<br/>
<span class="id" type="var">Case</span> "T_Proj".<br/>
<span class="comment">(* If the last rule in the given derivation is <span class="inlinecode"><span class="id" type="var">T_Proj</span></span>, then <br/>
<span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">tproj</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode"><span class="id" type="var">i</span></span> and<br/>
<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">TRcd</span></span> <span class="inlinecode"><span class="id" type="var">Tr</span>)</span><br/>
By the IH, <span class="inlinecode"><span class="id" type="var">t</span></span> either is a value or takes a step. *)</span><br/>
<span class="id" type="var">right</span>. <span class="id" type="tactic">destruct</span> <span class="id" type="var">IHHt</span>...<br/>
<span class="id" type="var">SCase</span> "rcd is value".<br/>
<span class="comment">(* If <span class="inlinecode"><span class="id" type="var">t</span></span> is a value, then we may use lemma<br/>
<span class="inlinecode"><span class="id" type="var">lookup_field_in_value</span></span> to show <span class="inlinecode"><span class="id" type="var">tlookup</span></span> <span class="inlinecode"><span class="id" type="var">i</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">Some</span></span> <span class="inlinecode"><span class="id" type="var">ti</span></span> for<br/>
some <span class="inlinecode"><span class="id" type="var">ti</span></span> which gives us <span class="inlinecode"><span class="id" type="var">tproj</span></span> <span class="inlinecode"><span class="id" type="var">i</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode"><span style="font-family: arial;">⇒</span></span> <span class="inlinecode"><span class="id" type="var">ti</span></span> by <span class="inlinecode"><span class="id" type="var">ST_ProjRcd</span></span><br/>
*)</span><br/>
<span class="id" type="tactic">destruct</span> (<span class="id" type="var">lookup_field_in_value</span> <span class="id" type="var">_</span> <span class="id" type="var">_</span> <span class="id" type="var">_</span> <span class="id" type="var">_</span> <span class="id" type="var">H0</span> <span class="id" type="var">Ht</span> <span class="id" type="var">H</span>) <span class="id" type="keyword">as</span> [<span class="id" type="var">ti</span> [<span class="id" type="var">Hlkup</span> <span class="id" type="var">_</span>]].<br/>
<span style="font-family: arial;">∃</span><span class="id" type="var">ti</span>...<br/>
<span class="id" type="var">SCase</span> "rcd_steps".<br/>
<span class="comment">(* On the other hand, 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>, then <span class="inlinecode"><span class="id" type="var">tproj</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode"><span class="id" type="var">i</span></span> <span class="inlinecode"><span style="font-family: arial;">⇒</span></span> <span class="inlinecode"><span class="id" type="var">tproj</span></span> <span class="inlinecode"><span class="id" type="var">t'</span></span> <span class="inlinecode"><span class="id" type="var">i</span></span><br/>
by <span class="inlinecode"><span class="id" type="var">ST_Proj1</span></span>. *)</span><br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">H0</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">t'</span> <span class="id" type="var">Hstp</span>]. <span style="font-family: arial;">∃</span>(<span class="id" type="var">tproj</span> <span class="id" type="var">t'</span> <span class="id" type="var">i</span>)...<br/>
<span class="id" type="var">Case</span> "T_RNil".<br/>
<span class="comment">(* If the last rule in the given derivation is <span class="inlinecode"><span class="id" type="var">T_RNil</span></span>, then <br/>
<span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">trnil</span></span>, which is a value. *)</span><br/>
<span class="id" type="var">left</span>...<br/>
<span class="id" type="var">Case</span> "T_RCons".<br/>
<span class="comment">(* If the last rule is <span class="inlinecode"><span class="id" type="var">T_RCons</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">trcons</span></span> <span class="inlinecode"><span class="id" type="var">i</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode"><span class="id" type="var">tr</span></span> and<br/>
<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><br/>
<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">tr</span></span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" type="var">Tr</span></span><br/>
By the IH, each of <span class="inlinecode"><span class="id" type="var">t</span></span> and <span class="inlinecode"><span class="id" type="var">tr</span></span> either is a value or can take<br/>
a step. *)</span><br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">IHHt1</span>...<br/>
<span class="id" type="var">SCase</span> "head is a value".<br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">IHHt2</span>; <span class="id" type="tactic">try</span> <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">SSCase</span> "tail is a value".<br/>
<span class="comment">(* If <span class="inlinecode"><span class="id" type="var">t</span></span> and <span class="inlinecode"><span class="id" type="var">tr</span></span> are both values, then <span class="inlinecode"><span class="id" type="var">trcons</span></span> <span class="inlinecode"><span class="id" type="var">i</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode"><span class="id" type="var">tr</span></span><br/>
is a value as well. *)</span><br/>
<span class="id" type="var">left</span>...<br/>
<span class="id" type="var">SSCase</span> "tail steps".<br/>
<span class="comment">(* If <span class="inlinecode"><span class="id" type="var">t</span></span> is a value and <span class="inlinecode"><span class="id" type="var">tr</span></span> <span class="inlinecode"><span style="font-family: arial;">⇒</span></span> <span class="inlinecode"><span class="id" type="var">tr'</span></span>, then <br/>
<span class="inlinecode"><span class="id" type="var">trcons</span></span> <span class="inlinecode"><span class="id" type="var">i</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode"><span class="id" type="var">tr</span></span> <span class="inlinecode"><span style="font-family: arial;">⇒</span></span> <span class="inlinecode"><span class="id" type="var">trcons</span></span> <span class="inlinecode"><span class="id" type="var">i</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode"><span class="id" type="var">tr'</span></span> by <br/>
<span class="inlinecode"><span class="id" type="var">ST_Rcd_Tail</span></span>. *)</span><br/>
<span class="id" type="var">right</span>. <span class="id" type="tactic">destruct</span> <span class="id" type="var">H2</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">tr'</span> <span class="id" type="var">Hstp</span>].<br/>
<span style="font-family: arial;">∃</span>(<span class="id" type="var">trcons</span> <span class="id" type="var">i</span> <span class="id" type="var">t</span> <span class="id" type="var">tr'</span>)...<br/>
<span class="id" type="var">SCase</span> "head steps".<br/>
<span class="comment">(* 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>, then <br/>
<span class="inlinecode"><span class="id" type="var">trcons</span></span> <span class="inlinecode"><span class="id" type="var">i</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode"><span class="id" type="var">tr</span></span> <span class="inlinecode"><span style="font-family: arial;">⇒</span></span> <span class="inlinecode"><span class="id" type="var">trcons</span></span> <span class="inlinecode"><span class="id" type="var">i</span></span> <span class="inlinecode"><span class="id" type="var">t'</span></span> <span class="inlinecode"><span class="id" type="var">tr</span></span> <br/>
by <span class="inlinecode"><span class="id" type="var">ST_Rcd_Head</span></span>. *)</span><br/>
<span class="id" type="var">right</span>. <span class="id" type="tactic">destruct</span> <span class="id" type="var">H1</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">t'</span> <span class="id" type="var">Hstp</span>].<br/>
<span style="font-family: arial;">∃</span>(<span class="id" type="var">trcons</span> <span class="id" type="var">i</span> <span class="id" type="var">t'</span> <span class="id" type="var">tr</span>)... <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab811"></a><h3 class="section">Context Invariance</h3>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">appears_free_in</span> : <span class="id" type="var">id</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">afi_var</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span>,<br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> (<span class="id" type="var">tvar</span> <span class="id" type="var">x</span>)<br/>
| <span class="id" type="var">afi_app1</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>,<br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> <span class="id" type="var">t<sub>1</sub></span> <span style="font-family: arial;">→</span> <span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> (<span class="id" type="var">tapp</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>)<br/>
| <span class="id" type="var">afi_app2</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>,<br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> <span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">→</span> <span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> (<span class="id" type="var">tapp</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>)<br/>
| <span class="id" type="var">afi_abs</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span> <span class="id" type="var">y</span> <span class="id" type="var">T<sub>11</sub></span> <span class="id" type="var">t<sub>12</sub></span>,<br/>
<span class="id" type="var">y</span> ≠ <span class="id" type="var">x</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> <span class="id" type="var">t<sub>12</sub></span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> (<span class="id" type="var">tabs</span> <span class="id" type="var">y</span> <span class="id" type="var">T<sub>11</sub></span> <span class="id" type="var">t<sub>12</sub></span>)<br/>
| <span class="id" type="var">afi_proj</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span> <span class="id" type="var">t</span> <span class="id" type="var">i</span>,<br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> <span class="id" type="var">t</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> (<span class="id" type="var">tproj</span> <span class="id" type="var">t</span> <span class="id" type="var">i</span>)<br/>
| <span class="id" type="var">afi_rhead</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span> <span class="id" type="var">i</span> <span class="id" type="var">ti</span> <span class="id" type="var">tr</span>,<br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> <span class="id" type="var">ti</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> (<span class="id" type="var">trcons</span> <span class="id" type="var">i</span> <span class="id" type="var">ti</span> <span class="id" type="var">tr</span>)<br/>
| <span class="id" type="var">afi_rtail</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span> <span class="id" type="var">i</span> <span class="id" type="var">ti</span> <span class="id" type="var">tr</span>,<br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> <span class="id" type="var">tr</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> (<span class="id" type="var">trcons</span> <span class="id" type="var">i</span> <span class="id" type="var">ti</span> <span class="id" type="var">tr</span>).<br/>
<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="var">Constructors</span> <span class="id" type="var">appears_free_in</span>.<br/>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">context_invariance</span> : <span style="font-family: arial;">∀</span><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: serif; font-size:85%;">Γ'</span> <span class="id" type="var">t</span> <span class="id" type="var">S</span>,<br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> <span class="id" type="var">t</span> ∈ <span class="id" type="var">S</span> <span style="font-family: arial;">→</span><br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">x</span>, <span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> <span class="id" type="var">t</span> <span style="font-family: arial;">→</span> <span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">x</span> = <span style="font-family: serif; font-size:85%;">Γ'</span> <span class="id" type="var">x</span>) <span style="font-family: arial;">→</span><br/>
<span style="font-family: serif; font-size:85%;">Γ'</span> <span style="font-family: arial;">⊢</span> <span class="id" type="var">t</span> ∈ <span class="id" type="var">S</span>.<br/>
<span class="id" type="keyword">Proof</span> <span class="id" type="keyword">with</span> <span class="id" type="tactic">eauto</span>.<br/>
<span class="id" type="tactic">intros</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="var">has_type_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">H</span>) <span class="id" type="var">Case</span>; <br/>
<span class="id" type="tactic">intros</span> <span style="font-family: serif; font-size:85%;">Γ'</span> <span class="id" type="var">Heqv</span>...<br/>
<span class="id" type="var">Case</span> "T_Var".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">T_Var</span>... <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">←</span> <span class="id" type="var">Heqv</span>...<br/>
<span class="id" type="var">Case</span> "T_Abs".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">T_Abs</span>... <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">y</span> <span class="id" type="var">Hafi</span>.<br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">extend</span>. <span class="id" type="tactic">destruct</span> (<span class="id" type="var">eq_id_dec</span> <span class="id" type="var">x</span> <span class="id" type="var">y</span>)...<br/>
<span class="id" type="var">Case</span> "T_App".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">T_App</span> <span class="id" type="keyword">with</span> <span class="id" type="var">T<sub>1</sub></span>...<br/>
<span class="id" type="var">Case</span> "T_RCons".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">T_RCons</span>... <span class="id" type="keyword">Qed</span>.<br/>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">free_in_context</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span> <span class="id" type="var">t</span> <span class="id" type="var">T</span> <span style="font-family: serif; font-size:85%;">Γ</span>,<br/>
<span class="id" type="var">appears_free_in</span> <span class="id" type="var">x</span> <span class="id" type="var">t</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> <span class="id" type="var">t</span> ∈ <span class="id" type="var">T</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">∃</span><span class="id" type="var">T'</span>, <span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">x</span> = <span class="id" type="var">Some</span> <span class="id" type="var">T'</span>.<br/>
<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">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">Hafi</span> <span class="id" type="var">Htyp</span>.<br/>
<span class="id" type="var">has_type_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">Htyp</span>) <span class="id" type="var">Case</span>; <span class="id" type="tactic">inversion</span> <span class="id" type="var">Hafi</span>; <span class="id" type="tactic">subst</span>...<br/>
<span class="id" type="var">Case</span> "T_Abs".<br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">IHHtyp</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">T'</span> <span class="id" type="var">Hctx</span>]... <span style="font-family: arial;">∃</span><span class="id" type="var">T'</span>.<br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">extend</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hctx</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">neq_id</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hctx</span>...<br/>