-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoreStlc.html
2737 lines (2461 loc) · 506 KB
/
MoreStlc.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="common/css/sf.css" rel="stylesheet" type="text/css" />
<title>MoreStlc: More on the Simply Typed Lambda-Calculus</title>
<link href="common/jquery-ui/jquery-ui.css" rel="stylesheet">
<script src="common/jquery-ui/external/jquery/jquery.js"></script>
<script src="common/jquery-ui/jquery-ui.js"></script>
<script src="common/toggleproofs.js"></script>
<link href="common/css/plf.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="page">
<div id="header">
<div id='logoinheader'><a href='https://softwarefoundations.cis.upenn.edu'>
<img src='common/media/image/sf_logo_sm.png' alt='Software Foundations Logo'></a></div>
<div class='booktitleinheader'><a href='index.html'>Volume 2: Programming Language Foundations</a></div>
<ul id='menu'>
<li class='section_name'><a href='toc.html'>Table of Contents</a></li>
<li class='section_name'><a href='coqindex.html'>Index</a></li>
<li class='section_name'><a href='deps.html'>Roadmap</a></li>
</ul>
</div>
<div id="main">
<h1 class="libtitle">MoreStlc<span class="subtitle">More on the Simply Typed Lambda-Calculus</span></h1>
<div class="code">
<span class="id" title="keyword">Set</span> <span class="id" title="var">Warnings</span> "-notation-overridden,-parsing,-deprecated-hint-without-locality".<br/>
<span class="id" title="keyword">From</span> <span class="id" title="var">PLF</span> <span class="id" title="keyword">Require</span> <span class="id" title="keyword">Import</span> <span class="id" title="library">Maps</span>.<br/>
<span class="id" title="keyword">From</span> <span class="id" title="var">PLF</span> <span class="id" title="keyword">Require</span> <span class="id" title="keyword">Import</span> <a class="idref" href="Types.html#"><span class="id" title="library">Types</span></a>.<br/>
<span class="id" title="keyword">From</span> <span class="id" title="var">PLF</span> <span class="id" title="keyword">Require</span> <span class="id" title="keyword">Import</span> <a class="idref" href="Smallstep.html#"><span class="id" title="library">Smallstep</span></a>.<br/>
<span class="id" title="keyword">From</span> <span class="id" title="var">PLF</span> <span class="id" title="keyword">Require</span> <span class="id" title="keyword">Import</span> <a class="idref" href="Stlc.html#"><span class="id" title="library">Stlc</span></a>.<br/>
</div>
<div class="doc">
<a id="lab265"></a><h1 class="section">Simple Extensions to STLC</h1>
<div class="paragraph"> </div>
The simply typed lambda-calculus has enough structure to make its
theoretical properties interesting, but it is not much of a
programming language!
<div class="paragraph"> </div>
In this chapter, we begin to close the gap with real-world
languages by introducing a number of familiar features that have
straightforward treatments at the level of typing.
<div class="paragraph"> </div>
<a id="lab266"></a><h2 class="section">Numbers</h2>
<div class="paragraph"> </div>
As we saw in exercise <span class="inlinecode"><span class="id" title="var">stlc_arith</span></span> at the end of the <span class="inlinecode"><span class="id" title="var">StlcProp</span></span>
chapter, adding types, constants, and primitive operations for
natural numbers is easy -- basically just a matter of combining
the <a href="Types.html"><span class="inlineref">Types</span></a> and <a href="Stlc.html"><span class="inlineref">Stlc</span></a> chapters. Adding more realistic
numeric types like machine integers and floats is also
straightforward, though of course the specifications of the
numeric primitives become more fiddly.
<div class="paragraph"> </div>
<a id="lab267"></a><h2 class="section">Let Bindings</h2>
<div class="paragraph"> </div>
When writing a complex expression, it is useful to be able
to give names to some of its subexpressions to avoid repetition
and increase readability. Most languages provide one or more ways
of doing this. In OCaml (and Coq), for example, we can write <span class="inlinecode"><span class="id" title="keyword">let</span></span>
<span class="inlinecode"><span class="id" title="var">x</span>=<span class="id" title="var">t<sub>1</sub></span></span> <span class="inlinecode"><span class="id" title="keyword">in</span></span> <span class="inlinecode"><span class="id" title="var">t<sub>2</sub></span></span> to mean "reduce the expression <span class="inlinecode"><span class="id" title="var">t<sub>1</sub></span></span> to a value and
bind the name <span class="inlinecode"><span class="id" title="var">x</span></span> to this value while reducing <span class="inlinecode"><span class="id" title="var">t<sub>2</sub></span></span>."
<div class="paragraph"> </div>
Our <span class="inlinecode"><span class="id" title="keyword">let</span></span>-binder follows OCaml in choosing a standard
<i>call-by-value</i> evaluation order, where the <span class="inlinecode"><span class="id" title="keyword">let</span></span>-bound term must
be fully reduced before reduction of the <span class="inlinecode"><span class="id" title="keyword">let</span></span>-body can begin.
The typing rule <span class="inlinecode"><span class="id" title="var">T_Let</span></span> tells us that the type of a <span class="inlinecode"><span class="id" title="keyword">let</span></span> can be
calculated by calculating the type of the <span class="inlinecode"><span class="id" title="keyword">let</span></span>-bound term,
extending the context with a binding with this type, and in this
enriched context calculating the type of the body (which is then
the type of the whole <span class="inlinecode"><span class="id" title="keyword">let</span></span> expression).
<div class="paragraph"> </div>
At this point in the book, it's probably easier simply to look at
the rules defining this new feature than to wade through a lot of
English text conveying the same information. Here they are:
<div class="paragraph"> </div>
Syntax:
<pre>
t <span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>:</span>:</span>=</span> Terms
| ... (other terms same as before)
| let x=t in t let-binding
</pre>
<div class="paragraph"> </div>
<div class="paragraph"> </div>
Reduction:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> t<sub>1</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Let1)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">let x=t<sub>1</sub> in t<sub>2</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> let x=t<sub>1</sub>' in t<sub>2</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(ST_LetValue)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">let x=v<sub>1</sub> in t<sub>2</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> [x:=v<sub>1</sub>]t<sub>2</sub></td>
<td></td>
</td>
</table></center> Typing:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ t<sub>1</sub> ∈ T<sub>1</sub> x<span class="nowrap"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>⊢</span><span style='font-size:90%;'>></span></span></span></span></span>T<sub>1</sub>; Gamma ⊢ t<sub>2</sub> ∈ T<sub>2</sub></td>
<td class="infrulenamecol" rowspan="3">
(T_Let)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ let x=t<sub>1</sub> in t<sub>2</sub> ∈ T<sub>2</sub></td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
<a id="lab268"></a><h2 class="section">Pairs</h2>
<div class="paragraph"> </div>
Our functional programming examples in Coq have made
frequent use of <i>pairs</i> of values. The type of such a pair is
called a <i>product type</i>.
<div class="paragraph"> </div>
The formalization of pairs is almost too simple to be worth
discussing. However, let's look briefly at the various parts of
the definition to emphasize the common pattern.
<div class="paragraph"> </div>
In Coq, the primitive way of extracting the components of a pair
is <i>pattern matching</i>. An alternative is to take <span class="inlinecode"><span class="id" title="var">fst</span></span> and
<span class="inlinecode"><span class="id" title="var">snd</span></span> -- the first- and second-projection operators -- as
primitives. Just for fun, let's do our pairs this way. For
example, here's how we'd write a function that takes a pair of
numbers and returns the pair of their sum and difference:
<pre>
\x : Nat*Nat,
let sum = x.fst + x.snd in
let diff = x.fst - x.snd in
(sum,diff)
</pre>
<div class="paragraph"> </div>
Adding pairs to the simply typed lambda-calculus, then, involves
adding two new forms of term -- pairing, written <span class="inlinecode">(<span class="id" title="var">t<sub>1</sub></span>,<span class="id" title="var">t<sub>2</sub></span>)</span>, and
projection, written <span class="inlinecode"><span class="id" title="var">t.fst</span></span> for the first projection from <span class="inlinecode"><span class="id" title="var">t</span></span> and
<span class="inlinecode"><span class="id" title="var">t.snd</span></span> for the second projection -- plus one new type constructor,
<span class="inlinecode"><span class="id" title="var">T<sub>1</sub></span>×<span class="id" title="var">T<sub>2</sub></span></span>, called the <i>product</i> of <span class="inlinecode"><span class="id" title="var">T<sub>1</sub></span></span> and <span class="inlinecode"><span class="id" title="var">T<sub>2</sub></span></span>.
<div class="paragraph"> </div>
Syntax:
<pre>
t <span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>:</span>:</span>=</span> Terms
| ...
| (t,t) pair
| t.fst first projection
| t.snd second projection
v <span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>:</span>:</span>=</span> Values
| ...
| (v,v) pair value
T <span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>:</span>:</span>=</span> Types
| ...
| T * T product type
</pre>
<div class="paragraph"> </div>
For reduction, we need several new rules specifying how pairs and
projection behave.
<div class="paragraph"> </div>
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> t<sub>1</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Pair1)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">(t<sub>1</sub>,t<sub>2</sub>) <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> (t<sub>1</sub>',t<sub>2</sub>)</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>2</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> t<sub>2</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Pair2)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">(v<sub>1</sub>,t<sub>2</sub>) <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> (v<sub>1</sub>,t<sub>2</sub>')</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> t<sub>1</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Fst1)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub>.fst <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> t<sub>1</sub>'.fst</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(ST_FstPair)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">(v<sub>1</sub>,v<sub>2</sub>).fst <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> v<sub>1</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> t<sub>1</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Snd1)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub>.snd <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> t<sub>1</sub>'.snd</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(ST_SndPair)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">(v<sub>1</sub>,v<sub>2</sub>).snd <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> v<sub>2</sub></td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
Rules <span class="inlinecode"><span class="id" title="var">ST_FstPair</span></span> and <span class="inlinecode"><span class="id" title="var">ST_SndPair</span></span> say that, when a fully
reduced pair meets a first or second projection, the result is
the appropriate component. The congruence rules <span class="inlinecode"><span class="id" title="var">ST_Fst1</span></span> and
<span class="inlinecode"><span class="id" title="var">ST_Snd1</span></span> allow reduction to proceed under projections, when the
term being projected from has not yet been fully reduced.
<span class="inlinecode"><span class="id" title="var">ST_Pair1</span></span> and <span class="inlinecode"><span class="id" title="var">ST_Pair2</span></span> reduce the parts of pairs: first the
left part, and then -- when a value appears on the left -- the right
part. The ordering arising from the use of the metavariables <span class="inlinecode"><span class="id" title="var">v</span></span>
and <span class="inlinecode"><span class="id" title="var">t</span></span> in these rules enforces a left-to-right evaluation
strategy for pairs. (Note the implicit convention that
metavariables like <span class="inlinecode"><span class="id" title="var">v</span></span> and <span class="inlinecode"><span class="id" title="var">v<sub>1</sub></span></span> can only denote values.) We've
also added a clause to the definition of values, above, specifying
that <span class="inlinecode">(<span class="id" title="var">v<sub>1</sub></span>,<span class="id" title="var">v<sub>2</sub></span>)</span> is a value. The fact that the components of a pair
value must themselves be values ensures that a pair passed as an
argument to a function will be fully reduced before the function
body starts executing.
<div class="paragraph"> </div>
The typing rules for pairs and projections are straightforward.
<div class="paragraph"> </div>
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ t<sub>1</sub> ∈ T<sub>1</sub> Gamma ⊢ t<sub>2</sub> ∈ T<sub>2</sub></td>
<td class="infrulenamecol" rowspan="3">
(T_Pair)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ (t<sub>1</sub>,t<sub>2</sub>) ∈ T<sub>1</sub>*T<sub>2</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ t<sub>0</sub> ∈ T<sub>1</sub>*T<sub>2</sub></td>
<td class="infrulenamecol" rowspan="3">
(T_Fst)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ t<sub>0</sub>.fst ∈ T<sub>1</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ t<sub>0</sub> ∈ T<sub>1</sub>*T<sub>2</sub></td>
<td class="infrulenamecol" rowspan="3">
(T_Snd)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ t<sub>0</sub>.snd ∈ T<sub>2</sub></td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
<span class="inlinecode"><span class="id" title="var">T_Pair</span></span> says that <span class="inlinecode">(<span class="id" title="var">t<sub>1</sub></span>,<span class="id" title="var">t<sub>2</sub></span>)</span> has type <span class="inlinecode"><span class="id" title="var">T<sub>1</sub></span>×<span class="id" title="var">T<sub>2</sub></span></span> if <span class="inlinecode"><span class="id" title="var">t<sub>1</sub></span></span> has
type <span class="inlinecode"><span class="id" title="var">T<sub>1</sub></span></span> and <span class="inlinecode"><span class="id" title="var">t<sub>2</sub></span></span> has type <span class="inlinecode"><span class="id" title="var">T<sub>2</sub></span></span>. Conversely, <span class="inlinecode"><span class="id" title="var">T_Fst</span></span> and <span class="inlinecode"><span class="id" title="var">T_Snd</span></span>
tell us that, if <span class="inlinecode"><span class="id" title="var">t<sub>0</sub></span></span> has a product type <span class="inlinecode"><span class="id" title="var">T<sub>1</sub></span>×<span class="id" title="var">T<sub>2</sub></span></span> (i.e., if it
will reduce to a pair), then the types of the projections from
this pair are <span class="inlinecode"><span class="id" title="var">T<sub>1</sub></span></span> and <span class="inlinecode"><span class="id" title="var">T<sub>2</sub></span></span>.
<div class="paragraph"> </div>
<a id="lab269"></a><h2 class="section">Unit</h2>
<div class="paragraph"> </div>
Another handy base type, found especially in functional languages,
is the singleton type <span class="inlinecode"><span class="id" title="var">Unit</span></span>. It has a single element -- the term constant <span class="inlinecode"><span class="id" title="var">unit</span></span> (with a small
<span class="inlinecode"><span class="id" title="var">u</span></span>) -- and a typing rule making <span class="inlinecode"><span class="id" title="var">unit</span></span> an element of <span class="inlinecode"><span class="id" title="var">Unit</span></span>. We
also add <span class="inlinecode"><span class="id" title="var">unit</span></span> to the set of possible values -- indeed, <span class="inlinecode"><span class="id" title="var">unit</span></span> is
the <i>only</i> possible result of reducing an expression of type
<span class="inlinecode"><span class="id" title="var">Unit</span></span>.
<div class="paragraph"> </div>
Syntax:
<pre>
t <span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>:</span>:</span>=</span> Terms
| ... (other terms same as before)
| unit unit
v <span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>:</span>:</span>=</span> Values
| ...
| unit unit value
T <span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>:</span>:</span>=</span> Types
| ...
| Unit unit type
</pre>
Typing:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(T_Unit)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ unit ∈ Unit</td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
It may seem a little strange to bother defining a type that
has just one element -- after all, wouldn't every computation
living in such a type be trivial?
<div class="paragraph"> </div>
This is a fair question, and indeed in the STLC the <span class="inlinecode"><span class="id" title="var">Unit</span></span> type is
not especially critical (though we'll see two uses for it below).
Where <span class="inlinecode"><span class="id" title="var">Unit</span></span> really comes in handy is in richer languages with
<i>side effects</i> -- e.g., assignment statements that mutate
variables or pointers, exceptions and other sorts of nonlocal
control structures, etc. In such languages, it is convenient to
have a type for the (trivial) result of an expression that is
evaluated only for its effect.
<div class="paragraph"> </div>
<a id="lab270"></a><h2 class="section">Sums</h2>
<div class="paragraph"> </div>
Many programs need to deal with values that can take two distinct
forms. For example, we might identify students in a university
database using <i>either</i> their name <i>or</i> their id number. A search
function might return <i>either</i> a matching value <i>or</i> an error code.
<div class="paragraph"> </div>
These are specific examples of a binary <i>sum type</i> (sometimes called
a <i>disjoint union</i>), which describes a set of values drawn from
one of two given types, e.g.:
<pre>
Nat + Bool
</pre>
We create elements of these types by <i>tagging</i> elements of
the component types. For example, if <span class="inlinecode"><span class="id" title="var">n</span></span> is a <span class="inlinecode"><span class="id" title="var">Nat</span></span> then <span class="inlinecode"><span class="id" title="var">inl</span></span> <span class="inlinecode"><span class="id" title="var">n</span></span>
is an element of <span class="inlinecode"><span class="id" title="var">Nat</span>+<span class="id" title="var">Bool</span></span>; similarly, if <span class="inlinecode"><span class="id" title="var">b</span></span> is a <span class="inlinecode"><span class="id" title="var">Bool</span></span> then
<span class="inlinecode"><span class="id" title="var">inr</span></span> <span class="inlinecode"><span class="id" title="var">b</span></span> is a <span class="inlinecode"><span class="id" title="var">Nat</span>+<span class="id" title="var">Bool</span></span>. The names of the tags <span class="inlinecode"><span class="id" title="var">inl</span></span> and <span class="inlinecode"><span class="id" title="var">inr</span></span>
arise from thinking of them as functions
<pre>
inl ∈ Nat <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span> Nat + Bool
inr ∈ Bool <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span> Nat + Bool
</pre>
that "inject" elements of <span class="inlinecode"><span class="id" title="var">Nat</span></span> or <span class="inlinecode"><span class="id" title="var">Bool</span></span> into the left and right
components of the sum type <span class="inlinecode"><span class="id" title="var">Nat</span>+<span class="id" title="var">Bool</span></span>. (But note that we don't
actually treat them as functions in the way we formalize them:
<span class="inlinecode"><span class="id" title="var">inl</span></span> and <span class="inlinecode"><span class="id" title="var">inr</span></span> are keywords, and <span class="inlinecode"><span class="id" title="var">inl</span></span> <span class="inlinecode"><span class="id" title="var">t</span></span> and <span class="inlinecode"><span class="id" title="var">inr</span></span> <span class="inlinecode"><span class="id" title="var">t</span></span> are primitive
syntactic forms, not function applications.)
<div class="paragraph"> </div>
In general, the elements of a type <span class="inlinecode"><span class="id" title="var">T<sub>1</sub></span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" title="var">T<sub>2</sub></span></span> consist of the
elements of <span class="inlinecode"><span class="id" title="var">T<sub>1</sub></span></span> tagged with the token <span class="inlinecode"><span class="id" title="var">inl</span></span>, plus the elements of
<span class="inlinecode"><span class="id" title="var">T<sub>2</sub></span></span> tagged with <span class="inlinecode"><span class="id" title="var">inr</span></span>.
<div class="paragraph"> </div>
As we've seen in Coq programming, one important use of sums is
signaling errors:
<pre>
div ∈ Nat <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span> Nat <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span> (Nat + Unit)
div =
\x:Nat, \y:Nat,
if iszero y then
inr unit
else
inl ...
</pre>
The type <span class="inlinecode"><span class="id" title="var">Nat</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" title="var">Unit</span></span> above is in fact isomorphic to <span class="inlinecode"><span class="id" title="var">option</span></span>
<span class="inlinecode"><span class="id" title="var">nat</span></span> in Coq -- i.e., it's easy to write functions that translate
back and forth.
<div class="paragraph"> </div>
To <i>use</i> elements of sum types, we introduce a <span class="inlinecode"><span class="id" title="tactic">case</span></span>
construct (a very simplified form of Coq's <span class="inlinecode"><span class="id" title="keyword">match</span></span>) to destruct
them. For example, the following procedure converts a <span class="inlinecode"><span class="id" title="var">Nat</span>+<span class="id" title="var">Bool</span></span>
into a <span class="inlinecode"><span class="id" title="var">Nat</span></span>:
<div class="paragraph"> </div>
<pre>
getNat ∈ Nat+Bool <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span> Nat
getNat =
\x:Nat+Bool,
case x of
inl n => n
| inr b => if b then 1 else 0
</pre>
<div class="paragraph"> </div>
More formally...
<div class="paragraph"> </div>
Syntax:
<pre>
t <span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>:</span>:</span>=</span> Terms
| ... (other terms same as before)
| inl T t tagging (left)
| inr T t tagging (right)
| case t of case
inl x => t
| inr x => t
v <span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>:</span>:</span>=</span> Values
| ...
| inl T v tagged value (left)
| inr T v tagged value (right)
T <span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>:</span>:</span>=</span> Types
| ...
| T + T sum type
</pre>
<div class="paragraph"> </div>
Reduction:
<div class="paragraph"> </div>
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> t<sub>1</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Inl)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">inl T<sub>2</sub> t<sub>1</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> inl T<sub>2</sub> t<sub>1</sub>'</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>2</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> t<sub>2</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Inr)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">inr T<sub>1</sub> t<sub>2</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> inr T<sub>1</sub> t<sub>2</sub>'</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>0</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> t<sub>0</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Case)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">case t<sub>0</sub> of inl x<sub>1</sub> => t<sub>1</sub> | inr x<sub>2</sub> => t<sub>2</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">case t<sub>0</sub>' of inl x<sub>1</sub> => t<sub>1</sub> | inr x<sub>2</sub> => t<sub>2</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(ST_CaseInl)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">case (inl T<sub>2</sub> v<sub>1</sub>) of inl x<sub>1</sub> => t<sub>1</sub> | inr x<sub>2</sub> => t<sub>2</sub></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> [x<sub>1</sub>:=v<sub>1</sub>]t<sub>1</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(ST_CaseInr)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">case (inr T<sub>1</sub> v<sub>2</sub>) of inl x<sub>1</sub> => t<sub>1</sub> | inr x<sub>2</sub> => t<sub>2</sub></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> [x<sub>2</sub>:=v<sub>2</sub>]t<sub>2</sub></td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
Typing:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ t<sub>1</sub> ∈ T<sub>1</sub></td>
<td class="infrulenamecol" rowspan="3">
(T_Inl)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ inl T<sub>2</sub> t<sub>1</sub> ∈ T<sub>1</sub> + T<sub>2</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ t<sub>2</sub> ∈ T<sub>2</sub></td>
<td class="infrulenamecol" rowspan="3">
(T_Inr)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ inr T<sub>1</sub> t<sub>2</sub> ∈ T<sub>1</sub> + T<sub>2</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ t<sub>0</sub> ∈ T<sub>1</sub>+T<sub>2</sub></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">x<sub>1</sub><span class="nowrap"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>⊢</span><span style='font-size:90%;'>></span></span></span></span></span>T<sub>1</sub>; Gamma ⊢ t<sub>1</sub> ∈ T<sub>3</sub></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">x<sub>2</sub><span class="nowrap"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>⊢</span><span style='font-size:90%;'>></span></span></span></span></span>T<sub>2</sub>; Gamma ⊢ t<sub>2</sub> ∈ T<sub>3</sub></td>
<td class="infrulenamecol" rowspan="3">
(T_Case)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ case t<sub>0</sub> of inl x<sub>1</sub> => t<sub>1</sub> | inr x<sub>2</sub> => t<sub>2</sub> ∈ T<sub>3</sub></td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
We use the type annotations on <span class="inlinecode"><span class="id" title="var">inl</span></span> and <span class="inlinecode"><span class="id" title="var">inr</span></span> to make the typing
relation deterministic (each term has at most one type), as we
did for functions.
<div class="paragraph"> </div>
Without this extra information, the typing rule <span class="inlinecode"><span class="id" title="var">T_Inl</span></span>, for
example, would have to say that, once we have shown that <span class="inlinecode"><span class="id" title="var">t<sub>1</sub></span></span> is
an element of type <span class="inlinecode"><span class="id" title="var">T<sub>1</sub></span></span>, we can derive that <span class="inlinecode"><span class="id" title="var">inl</span></span> <span class="inlinecode"><span class="id" title="var">t<sub>1</sub></span></span> is an element
of <span class="inlinecode"><span class="id" title="var">T<sub>1</sub></span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" title="var">T<sub>2</sub></span></span> for <i>any</i> type <span class="inlinecode"><span class="id" title="var">T<sub>2</sub></span></span>. For example, we could derive both
<span class="inlinecode"><span class="id" title="var">inl</span></span> <span class="inlinecode">5</span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" title="var">Nat</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" title="var">Nat</span></span> and <span class="inlinecode"><span class="id" title="var">inl</span></span> <span class="inlinecode">5</span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" title="var">Nat</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" title="var">Bool</span></span> (and infinitely many
other types). This peculiarity (technically, a failure of
uniqueness of types) would mean that we cannot build a
typechecking algorithm simply by "reading the rules from bottom to
top" as we could for all the other features seen so far.
<div class="paragraph"> </div>
There are various ways to deal with this difficulty. One simple
one -- which we've adopted here -- forces the programmer to
explicitly annotate the "other side" of a sum type when performing
an injection. This is a bit heavy for programmers (so real
languages adopt other solutions), but it is easy to understand and
formalize.
<div class="paragraph"> </div>
<a id="lab271"></a><h2 class="section">Lists</h2>
<div class="paragraph"> </div>
The typing features we have seen can be classified into
<i>base types</i> like <span class="inlinecode"><span class="id" title="var">Bool</span></span>, and <i>type constructors</i> like <span class="inlinecode">→</span> and
<span class="inlinecode">×</span> that build new types from old ones. Another useful type
constructor is <span class="inlinecode"><span class="id" title="var">List</span></span>. For every type <span class="inlinecode"><span class="id" title="var">T</span></span>, the type <span class="inlinecode"><span class="id" title="var">List</span></span> <span class="inlinecode"><span class="id" title="var">T</span></span>
describes finite-length lists whose elements are drawn from <span class="inlinecode"><span class="id" title="var">T</span></span>.
<div class="paragraph"> </div>
In principle, we could encode lists using pairs, sums and
<i>recursive</i> types. But giving semantics to recursive types is
non-trivial. Instead, we'll just discuss the special case of lists
directly.
<div class="paragraph"> </div>
Below we give the syntax, semantics, and typing rules for lists.
Except for the fact that explicit type annotations are mandatory
on <span class="inlinecode"><span class="id" title="var">nil</span></span> and cannot appear on <span class="inlinecode"><span class="id" title="var">cons</span></span>, these lists are essentially
identical to those we built in Coq. We use <span class="inlinecode"><span class="id" title="tactic">case</span></span>, rather than
<span class="inlinecode"><span class="id" title="var">head</span></span> and <span class="inlinecode"><span class="id" title="var">tail</span></span> operators, to destruct lists, to avoid dealing
with questions like "what is the <span class="inlinecode"><span class="id" title="var">head</span></span> of the empty list?"
<div class="paragraph"> </div>
For example, here is a function that calculates the sum of
the first two elements of a list of numbers:
<pre>
\x:List Nat,
case x of nil => 0
| a::x' => case x' of nil => a
| b::x'' => a+b
</pre>
<div class="paragraph"> </div>
<div class="paragraph"> </div>
Syntax:
<pre>
t <span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>:</span>:</span>=</span> Terms
| ...
| nil T
| cons t t
| case t of nil => t
| x::x => t
v <span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>:</span>:</span>=</span> Values
| ...
| nil T nil value
| cons v v cons value
T <span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>:</span>:</span>=</span> Types
| ...
| List T list of Ts
</pre>
<div class="paragraph"> </div>
Reduction:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> t<sub>1</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Cons1)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">cons t<sub>1</sub> t<sub>2</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> cons t<sub>1</sub>' t<sub>2</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>2</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> t<sub>2</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Cons2)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">cons v<sub>1</sub> t<sub>2</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> cons v<sub>1</sub> t<sub>2</sub>'</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> t<sub>1</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Lcase1)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">(case t<sub>1</sub> of nil => t<sub>2</sub> | xh::xt => t<sub>3</sub>) <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">(case t<sub>1</sub>' of nil => t<sub>2</sub> | xh::xt => t<sub>3</sub>)</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(ST_LcaseNil)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">(case nil T<sub>1</sub> of nil => t<sub>2</sub> | xh::xt => t<sub>3</sub>)</td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> t<sub>2</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(ST_LcaseCons)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">(case (cons vh vt) of nil => t<sub>2</sub> | xh::xt => t<sub>3</sub>)</td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> [xh:=vh,xt:=vt]t<sub>3</sub></td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
Typing:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(T_Nil)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ nil T<sub>1</sub> ∈ List T<sub>1</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ t<sub>1</sub> ∈ T<sub>1</sub> Gamma ⊢ t<sub>2</sub> ∈ List T<sub>1</sub></td>
<td class="infrulenamecol" rowspan="3">
(T_Cons)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ cons t<sub>1</sub> t<sub>2</sub> ∈ List T<sub>1</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ t<sub>1</sub> ∈ List T<sub>1</sub></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ t<sub>2</sub> ∈ T<sub>2</sub></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">(h<span class="nowrap"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>⊢</span><span style='font-size:90%;'>></span></span></span></span></span>T<sub>1</sub>; t<span class="nowrap"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>⊢</span><span style='font-size:90%;'>></span></span></span></span></span>List T<sub>1</sub>; Gamma) ⊢ t<sub>3</sub> ∈ T<sub>2</sub></td>
<td class="infrulenamecol" rowspan="3">
(T_Lcase)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">Gamma ⊢ (case t<sub>1</sub> of nil => t<sub>2</sub> | h::t => t<sub>3</sub>) ∈ T<sub>2</sub></td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
<a id="lab272"></a><h2 class="section">General Recursion</h2>
<div class="paragraph"> </div>
Another facility found in most programming languages (including
Coq) is the ability to define recursive functions. For example,
we would like to be able to define the factorial function like
this:
<pre>
fact = \x:Nat,
if x=0 then 1 else x * (fact (pred x)))
</pre>
Note that the right-hand side of this binder mentions the variable
being bound -- something that is not allowed by our formalization of
<span class="inlinecode"><span class="id" title="keyword">let</span></span> above.
<div class="paragraph"> </div>
Directly formalizing this "recursive definition" mechanism is possible,
but it requires some extra effort: in particular, we'd have to
pass around an "environment" of recursive function definitions in
the definition of the <span class="inlinecode"><span class="id" title="var">step</span></span> relation.
<div class="paragraph"> </div>
Here is another way of presenting recursive functions that is
a bit more verbose but equally powerful and much more straightforward
to formalize: instead of writing recursive definitions, we will define
a <i>fixed-point operator</i> called <span class="inlinecode"><span class="id" title="keyword">fix</span></span> that performs the "unfolding"
of the recursive definition in the right-hand side as needed, during
reduction.
<div class="paragraph"> </div>
For example, instead of
<pre>
fact = \x:Nat,
if x=0 then 1 else x * (fact (pred x)))
</pre>
we will write:
<pre>
fact =
fix
(\f:Nat<span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span>Nat,
\x:Nat,
if x=0 then 1 else x * (f (pred x)))
</pre>
<div class="paragraph"> </div>
We can derive the latter from the former as follows:
<div class="paragraph"> </div>
<ul class="doclist">
<li> In the right-hand side of the definition of <span class="inlinecode"><span class="id" title="var">fact</span></span>, replace
recursive references to <span class="inlinecode"><span class="id" title="var">fact</span></span> by a fresh variable <span class="inlinecode"><span class="id" title="var">f</span></span>.
<div class="paragraph"> </div>
</li>
<li> Add an abstraction binding <span class="inlinecode"><span class="id" title="var">f</span></span> at the front, with an
appropriate type annotation. (Since we are using <span class="inlinecode"><span class="id" title="var">f</span></span> in place
of <span class="inlinecode"><span class="id" title="var">fact</span></span>, which had type <span class="inlinecode"><span class="id" title="var">Nat</span>→<span class="id" title="var">Nat</span></span>, we should require <span class="inlinecode"><span class="id" title="var">f</span></span>
to have the same type.) The new abstraction has type
<span class="inlinecode">(<span class="id" title="var">Nat</span>→<span class="id" title="var">Nat</span>)</span> <span class="inlinecode">→</span> <span class="inlinecode">(<span class="id" title="var">Nat</span>→<span class="id" title="var">Nat</span>)</span>.
<div class="paragraph"> </div>
</li>
<li> Apply <span class="inlinecode"><span class="id" title="keyword">fix</span></span> to this abstraction. This application has
type <span class="inlinecode"><span class="id" title="var">Nat</span>→<span class="id" title="var">Nat</span></span>.
<div class="paragraph"> </div>
</li>
<li> Use all of this as the right-hand side of an ordinary
<span class="inlinecode"><span class="id" title="keyword">let</span></span>-binding for <span class="inlinecode"><span class="id" title="var">fact</span></span>.
</li>
</ul>
<div class="paragraph"> </div>
The intuition is that the higher-order function <span class="inlinecode"><span class="id" title="var">f</span></span> passed
to <span class="inlinecode"><span class="id" title="keyword">fix</span></span> is a <i>generator</i> for the <span class="inlinecode"><span class="id" title="var">fact</span></span> function: if <span class="inlinecode"><span class="id" title="var">f</span></span> is
applied to a function that "approximates" the desired behavior of
<span class="inlinecode"><span class="id" title="var">fact</span></span> up to some number <span class="inlinecode"><span class="id" title="var">n</span></span> (that is, a function that returns
correct results on inputs less than or equal to <span class="inlinecode"><span class="id" title="var">n</span></span> but we don't
care what it does on inputs greater than <span class="inlinecode"><span class="id" title="var">n</span></span>), then <span class="inlinecode"><span class="id" title="var">f</span></span> returns a
slightly better approximation to <span class="inlinecode"><span class="id" title="var">fact</span></span> -- a function that returns
correct results for inputs up to <span class="inlinecode"><span class="id" title="var">n</span>+1</span>. Applying <span class="inlinecode"><span class="id" title="keyword">fix</span></span> to this
generator returns its <i>fixed point</i>, which is a function that
gives the desired behavior for all inputs <span class="inlinecode"><span class="id" title="var">n</span></span>.
<div class="paragraph"> </div>
(The term "fixed point" is used here in exactly the same sense as
in ordinary mathematics, where a fixed point of a function <span class="inlinecode"><span class="id" title="var">f</span></span> is
an input <span class="inlinecode"><span class="id" title="var">x</span></span> such that <span class="inlinecode"><span class="id" title="var">f</span>(<span class="id" title="var">x</span>)</span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" title="var">x</span></span>. Here, a fixed point of a
function <span class="inlinecode"><span class="id" title="var">F</span></span> of type <span class="inlinecode">(<span class="id" title="var">Nat</span>→<span class="id" title="var">Nat</span>)<span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span>(<span class="id" title="var">Nat</span>→<span class="id" title="var">Nat</span>)</span> is a function <span class="inlinecode"><span class="id" title="var">f</span></span> of
type <span class="inlinecode"><span class="id" title="var">Nat</span>→<span class="id" title="var">Nat</span></span> such that <span class="inlinecode"><span class="id" title="var">F</span></span> <span class="inlinecode"><span class="id" title="var">f</span></span> behaves the same as <span class="inlinecode"><span class="id" title="var">f</span></span>.)
<div class="paragraph"> </div>
Syntax:
<pre>
t <span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>:</span>:</span>=</span> Terms
| ...
| fix t fixed-point operator
</pre>
Reduction:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:6%;'><span style='letter-spacing:-.2em;'>-</span><span style='letter-spacing:-.2em;'>-</span></span>></span></span> t<sub>1</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Fix1)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">