forked from sven--/Software-Foundations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoreStlc.html
3084 lines (2732 loc) · 306 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="coqdoc.css" rel="stylesheet" type="text/css"/>
<title>MoreStlc: More on the Simply Typed Lambda-Calculus</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">MoreStlc<span class="subtitle">More on the Simply Typed Lambda-Calculus</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="lab704"></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. 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 name="lab705"></a><h2 class="section">Numbers</h2>
<div class="paragraph"> </div>
Adding types, constants, and primitive operations for numbers is
easy — just a matter of combining the <span class="inlinecode"><span class="id" type="keyword">Types</span></span> and <span class="inlinecode"><span class="id" type="var">Stlc</span></span>
chapters.
<div class="paragraph"> </div>
<a name="lab706"></a><h2 class="section"><span class="inlinecode"><span class="id" type="keyword">let</span></span>-bindings</h2>
<div class="paragraph"> </div>
When writing a complex expression, it is often useful to give
names to some of its subexpressions: this avoids repetition and
often increases 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" type="keyword">let</span></span> <span class="inlinecode"><span class="id" type="var">x</span>=<span class="id" type="var">t<sub>1</sub></span></span> <span class="inlinecode"><span class="id" type="keyword">in</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> to mean ``evaluate the expression <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> and bind
the name <span class="inlinecode"><span class="id" type="var">x</span></span> to the resulting value while evaluating <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span>.''
<div class="paragraph"> </div>
Our <span class="inlinecode"><span class="id" type="keyword">let</span></span>-binder follows OCaml's in choosing a call-by-value
evaluation order, where the <span class="inlinecode"><span class="id" type="keyword">let</span></span>-bound term must be fully
evaluated before evaluation of the <span class="inlinecode"><span class="id" type="keyword">let</span></span>-body can begin. The
typing rule <span class="inlinecode"><span class="id" type="var">T_Let</span></span> tells us that the type of a <span class="inlinecode"><span class="id" type="keyword">let</span></span> can be
calculated by calculating the type of the <span class="inlinecode"><span class="id" type="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" type="keyword">let</span></span> expression.
<div class="paragraph"> </div>
At this point in the course, it's probably easier simply to look
at the rules defining this new feature as to wade through a lot of
english text conveying the same information. Here they are:
<div class="paragraph"> </div>
Syntax:
<pre>
t ::= 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 style="font-family: arial;">⇒</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 style="font-family: arial;">⇒</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 style="font-family: arial;">⇒</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"><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> , x:T<sub>1</sub> <span style="font-family: arial;">⊢</span> 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"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> 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 name="lab707"></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 pairs 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 style is to take <span class="inlinecode"><span class="id" type="var">fst</span></span> and
<span class="inlinecode"><span class="id" type="var">snd</span></span> — the first- and second-projection operators — as
primitives. Just for fun, let's do our products 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" type="var">t<sub>1</sub></span>,<span class="id" type="var">t<sub>2</sub></span>)</span>, and
projection, written <span class="inlinecode"><span class="id" type="var">t.fst</span></span> for the first projection from <span class="inlinecode"><span class="id" type="var">t</span></span> and
<span class="inlinecode"><span class="id" type="var">t.snd</span></span> for the second projection — plus one new type constructor,
<span class="inlinecode"><span class="id" type="var">T<sub>1</sub></span>×<span class="id" type="var">T<sub>2</sub></span></span>, called the <i>product</i> of <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>.
<div class="paragraph"> </div>
Syntax:
<pre>
t ::= Terms
| ...
| (t,t) pair
| t.fst first projection
| t.snd second projection
v ::= Values
| ...
| (v,v) pair value
T ::= Types
| ...
| T * T product type
</pre>
<div class="paragraph"> </div>
For evaluation, we need several new rules specifying how pairs and
projection behave.
<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_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 style="font-family: arial;">⇒</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 style="font-family: arial;">⇒</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 style="font-family: arial;">⇒</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 style="font-family: arial;">⇒</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 style="font-family: arial;">⇒</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 style="font-family: arial;">⇒</span> v<sub>1</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub> <span style="font-family: arial;">⇒</span> t<sub>1</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Snd1)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub>.snd <span style="font-family: arial;">⇒</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 style="font-family: arial;">⇒</span> v<sub>2</sub></td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
<div class="paragraph"> </div>
Rules <span class="inlinecode"><span class="id" type="var">ST_FstPair</span></span> and <span class="inlinecode"><span class="id" type="var">ST_SndPair</span></span> specify that, when a fully
evaluated pair meets a first or second projection, the result is
the appropriate component. The congruence rules <span class="inlinecode"><span class="id" type="var">ST_Fst1</span></span> and
<span class="inlinecode"><span class="id" type="var">ST_Snd1</span></span> allow reduction to proceed under projections, when the
term being projected from has not yet been fully evaluated.
<span class="inlinecode"><span class="id" type="var">ST_Pair1</span></span> and <span class="inlinecode"><span class="id" type="var">ST_Pair2</span></span> evaluate 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" type="var">v</span></span>
and <span class="inlinecode"><span class="id" type="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" type="var">v</span></span> and <span class="inlinecode"><span class="id" type="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" type="var">v<sub>1</sub></span>,<span class="id" type="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 evaluated before the function
body starts executing.
<div class="paragraph"> </div>
The typing rules for pairs and projections are straightforward.
<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> 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"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> (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"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> t<sub>1</sub> : T<sub>11</sub>*T<sub>12</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"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> t<sub>1</sub>.fst : T<sub>11</sub></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<sub>1</sub> : T<sub>11</sub>*T<sub>12</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"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> t<sub>1</sub>.snd : T<sub>12</sub></td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
The rule <span class="inlinecode"><span class="id" type="var">T_Pair</span></span> says that <span class="inlinecode">(<span class="id" type="var">t<sub>1</sub></span>,<span class="id" type="var">t<sub>2</sub></span>)</span> has type <span class="inlinecode"><span class="id" type="var">T<sub>1</sub></span>×<span class="id" type="var">T<sub>2</sub></span></span> if <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> has
type <span class="inlinecode"><span class="id" type="var">T<sub>1</sub></span></span> and <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> has type <span class="inlinecode"><span class="id" type="var">T<sub>2</sub></span></span>. Conversely, the rules <span class="inlinecode"><span class="id" type="var">T_Fst</span></span>
and <span class="inlinecode"><span class="id" type="var">T_Snd</span></span> tell us that, if <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> has a product type
<span class="inlinecode"><span class="id" type="var">T<sub>11</sub></span>×<span class="id" type="var">T<sub>12</sub></span></span> (i.e., if it will evaluate to a pair), then the types of
the projections from this pair are <span class="inlinecode"><span class="id" type="var">T<sub>11</sub></span></span> and <span class="inlinecode"><span class="id" type="var">T<sub>12</sub></span></span>.
<div class="paragraph"> </div>
<a name="lab708"></a><h2 class="section">Unit</h2>
<div class="paragraph"> </div>
Another handy base type, found especially in languages in
the ML family, is the singleton type <span class="inlinecode"><span class="id" type="var">Unit</span></span>. It has a single element — the term constant <span class="inlinecode"><span class="id" type="var">unit</span></span> (with a small
<span class="inlinecode"><span class="id" type="var">u</span></span>) — and a typing rule making <span class="inlinecode"><span class="id" type="var">unit</span></span> an element of <span class="inlinecode"><span class="id" type="var">Unit</span></span>. We
also add <span class="inlinecode"><span class="id" type="var">unit</span></span> to the set of possible result values of
computations — indeed, <span class="inlinecode"><span class="id" type="var">unit</span></span> is the <i>only</i> possible result of
evaluating an expression of type <span class="inlinecode"><span class="id" type="var">Unit</span></span>.
<div class="paragraph"> </div>
Syntax:
<pre>
t ::= Terms
| ...
| unit unit value
v ::= Values
| ...
| unit unit
T ::= 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"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> 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" type="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" type="var">Unit</span></span> really comes in handy is in richer languages with
various sorts of <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 name="lab709"></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 employees in an accounting
application using 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>,
which describes a set of values drawn from exactly two given types, e.g.
<pre>
Nat + Bool
</pre>
<div class="paragraph"> </div>
We create elements of these types by <i>tagging</i> elements of
the component types. For example, if <span class="inlinecode"><span class="id" type="var">n</span></span> is a <span class="inlinecode"><span class="id" type="var">Nat</span></span> then <span class="inlinecode"><span class="id" type="var">inl</span></span> <span class="inlinecode"><span class="id" type="var">v</span></span>
is an element of <span class="inlinecode"><span class="id" type="var">Nat</span>+<span class="id" type="var">Bool</span></span>; similarly, if <span class="inlinecode"><span class="id" type="var">b</span></span> is a <span class="inlinecode"><span class="id" type="var">Bool</span></span> then
<span class="inlinecode"><span class="id" type="var">inr</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> is a <span class="inlinecode"><span class="id" type="var">Nat</span>+<span class="id" type="var">Bool</span></span>. The names of the tags <span class="inlinecode"><span class="id" type="var">inl</span></span> and <span class="inlinecode"><span class="id" type="var">inr</span></span>
arise from thinking of them as functions
<div class="paragraph"> </div>
<pre>
inl : Nat -> Nat + Bool
inr : Bool -> Nat + Bool
</pre>
<div class="paragraph"> </div>
that "inject" elements of <span class="inlinecode"><span class="id" type="var">Nat</span></span> or <span class="inlinecode"><span class="id" type="var">Bool</span></span> into the left and right
components of the sum type <span class="inlinecode"><span class="id" type="var">Nat</span>+<span class="id" type="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" type="var">inl</span></span> and <span class="inlinecode"><span class="id" type="var">inr</span></span> are keywords, and <span class="inlinecode"><span class="id" type="var">inl</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span> and <span class="inlinecode"><span class="id" type="var">inr</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span> are primitive
syntactic forms, not function applications. This allows us to give
them their own special typing rules.)
<div class="paragraph"> </div>
In general, the elements of a type <span class="inlinecode"><span class="id" type="var">T<sub>1</sub></span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">T<sub>2</sub></span></span> consist of the
elements of <span class="inlinecode"><span class="id" type="var">T<sub>1</sub></span></span> tagged with the token <span class="inlinecode"><span class="id" type="var">inl</span></span>, plus the elements of
<span class="inlinecode"><span class="id" type="var">T<sub>2</sub></span></span> tagged with <span class="inlinecode"><span class="id" type="var">inr</span></span>.
<div class="paragraph"> </div>
One important usage of sums is signaling errors:
<pre>
div : Nat -> Nat -> (Nat + Unit) =
div =
λx:Nat. λy:Nat.
if iszero y then
inr unit
else
inl ...
</pre>
The type <span class="inlinecode"><span class="id" type="var">Nat</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">Unit</span></span> above is in fact isomorphic to <span class="inlinecode"><span class="id" type="var">option</span></span> <span class="inlinecode"><span class="id" type="var">nat</span></span>
in Coq, and we've already seen how to signal errors with options.
<div class="paragraph"> </div>
To <i>use</i> elements of sum types, we introduce a <span class="inlinecode"><span class="id" type="tactic">case</span></span>
construct (a very simplified form of Coq's <span class="inlinecode"><span class="id" type="keyword">match</span></span>) to destruct
them. For example, the following procedure converts a <span class="inlinecode"><span class="id" type="var">Nat</span>+<span class="id" type="var">Bool</span></span>
into a <span class="inlinecode"><span class="id" type="var">Nat</span></span>:
<div class="paragraph"> </div>
<div class="paragraph"> </div>
<pre>
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 ::= Terms
| ...
| inl T t tagging (left)
| inr T t tagging (right)
| case t of case
inl x => t
| inr x => t
v ::= Values
| ...
| inl T v tagged value (left)
| inr T v tagged value (right)
T ::= Types
| ...
| T + T sum type
</pre>
<div class="paragraph"> </div>
Evaluation:
<div class="paragraph"> </div>
<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_Inl)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">inl T t<sub>1</sub> <span style="font-family: arial;">⇒</span> inl T t<sub>1</sub>'</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub> <span style="font-family: arial;">⇒</span> t<sub>1</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_Inr)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">inr T t<sub>1</sub> <span style="font-family: arial;">⇒</span> inr T t<sub>1</sub>'</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t0 <span style="font-family: arial;">⇒</span> t0'</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 t0 of inl x1 ⇒ t<sub>1</sub> | inr x2 ⇒ t<sub>2</sub> <span style="font-family: arial;">⇒</span></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">case t0' of inl x1 ⇒ t<sub>1</sub> | inr x2 ⇒ 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 v0) of inl x1 ⇒ t<sub>1</sub> | inr x2 ⇒ t<sub>2</sub></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: arial;">⇒</span> [x1:=v0]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 v0) of inl x1 ⇒ t<sub>1</sub> | inr x2 ⇒ t<sub>2</sub></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: arial;">⇒</span> [x2:=v0]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"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> 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"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> 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"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> t<sub>1</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"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> inr T<sub>1</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"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> t0 : T<sub>1</sub>+T<sub>2</sub></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: serif; font-size:85%;">Γ</span> , x1:T<sub>1</sub> <span style="font-family: arial;">⊢</span> t<sub>1</sub> : T</td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: serif; font-size:85%;">Γ</span> , x2:T<sub>2</sub> <span style="font-family: arial;">⊢</span> t<sub>2</sub> : T</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"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> case t0 of inl x1 ⇒ t<sub>1</sub> | inr x2 ⇒ t<sub>2</sub> : T</td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
We use the type annotation in <span class="inlinecode"><span class="id" type="var">inl</span></span> and <span class="inlinecode"><span class="id" type="var">inr</span></span> to make the typing
simpler, similarly to what we did for functions. Without this extra
information, the typing rule <span class="inlinecode"><span class="id" type="var">T_Inl</span></span>, for example, would have to
say that, once we have shown that <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> is an element of type <span class="inlinecode"><span class="id" type="var">T<sub>1</sub></span></span>,
we can derive that <span class="inlinecode"><span class="id" type="var">inl</span></span> <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> is an element of <span class="inlinecode"><span class="id" type="var">T<sub>1</sub></span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">T<sub>2</sub></span></span> for <i>any</i>
type T<sub>2</sub>. For example, we could derive both <span class="inlinecode"><span class="id" type="var">inl</span></span> <span class="inlinecode">5</span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" type="var">Nat</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">Nat</span></span>
and <span class="inlinecode"><span class="id" type="var">inl</span></span> <span class="inlinecode">5</span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" type="var">Nat</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">Bool</span></span> (and infinitely many other types).
This 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 rather heavyweight for programmers (and so
real languages adopt other solutions), but it is easy to
understand and formalize.
<div class="paragraph"> </div>
<a name="lab710"></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" type="var">Bool</span></span>, and <i>type constructors</i> like <span class="inlinecode"><span style="font-family: arial;">→</span></span> and <span class="inlinecode">×</span> that
build new types from old ones. Another useful type constructor is
<span class="inlinecode"><span class="id" type="var">List</span></span>. For every type <span class="inlinecode"><span class="id" type="var">T</span></span>, the type <span class="inlinecode"><span class="id" type="var">List</span></span> <span class="inlinecode"><span class="id" type="var">T</span></span> describes
finite-length lists whose elements are drawn from <span class="inlinecode"><span class="id" type="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" type="var">nil</span></span> and cannot appear on <span class="inlinecode"><span class="id" type="var">cons</span></span>, these lists are essentially
identical to those we built in Coq. We use <span class="inlinecode"><span class="id" type="var">lcase</span></span> to destruct
lists, to avoid dealing with questions like "what is the <span class="inlinecode"><span class="id" type="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.
lcase x of nil -> 0
| a::x' -> lcase x' of nil -> a
| b::x'' -> a+b
</pre>
<div class="paragraph"> </div>
<div class="paragraph"> </div>
Syntax:
<pre>
t ::= Terms
| ...
| nil T
| cons t t
| lcase t of nil -> t | x::x -> t
v ::= Values
| ...
| nil T nil value
| cons v v cons value
T ::= 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 style="font-family: arial;">⇒</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 style="font-family: arial;">⇒</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 style="font-family: arial;">⇒</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 style="font-family: arial;">⇒</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 style="font-family: arial;">⇒</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">(lcase t<sub>1</sub> of nil <span style="font-family: arial;">→</span> t<sub>2</sub> | xh::xt <span style="font-family: arial;">→</span> t<sub>3</sub>) <span style="font-family: arial;">⇒</span></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">(lcase t<sub>1</sub>' of nil <span style="font-family: arial;">→</span> t<sub>2</sub> | xh::xt <span style="font-family: arial;">→</span> 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">(lcase nil T of nil <span style="font-family: arial;">→</span> t<sub>2</sub> | xh::xt <span style="font-family: arial;">→</span> t<sub>3</sub>)</td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: arial;">⇒</span> t<sub>2</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </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">(lcase (cons vh vt) of nil <span style="font-family: arial;">→</span> t<sub>2</sub> | xh::xt <span style="font-family: arial;">→</span> t<sub>3</sub>)</td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: arial;">⇒</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"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> nil T : List T</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<sub>1</sub> : T <span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> t<sub>2</sub> : List T</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"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> cons t<sub>1</sub> t<sub>2</sub>: List T</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<sub>1</sub> : List T<sub>1</sub></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> t<sub>2</sub> : T</td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: serif; font-size:85%;">Γ</span> , h:T<sub>1</sub>, t:List T<sub>1</sub> <span style="font-family: arial;">⊢</span> t<sub>3</sub> : T</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"><span style="font-family: serif; font-size:85%;">Γ</span> <span style="font-family: arial;">⊢</span> (lcase t<sub>1</sub> of nil <span style="font-family: arial;">→</span> t<sub>2</sub> | h::t <span style="font-family: arial;">→</span> t<sub>3</sub>) : T</td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
<a name="lab711"></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 might 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>
But this would require quite a bit of work to formalize: we'd have
to introduce a notion of "function definitions" and carry around an
"environment" of such definitions in the definition of the <span class="inlinecode"><span class="id" type="var">step</span></span>
relation.
<div class="paragraph"> </div>
Here is another way that is straightforward to formalize: instead
of writing recursive definitions where the right-hand side can
contain the identifier being defined, we can define a <i>fixed-point
operator</i> that performs the "unfolding" of the recursive definition
in the right-hand side lazily during reduction.
<pre>
fact =
fix
(\f:Nat->Nat.
λx:Nat.
if x=0 then 1 else x * (f (pred x)))
</pre>
<div class="paragraph"> </div>
The intuition is that the higher-order function <span class="inlinecode"><span class="id" type="var">f</span></span> passed
to <span class="inlinecode"><span class="id" type="var">fix</span></span> is a <i>generator</i> for the <span class="inlinecode"><span class="id" type="var">fact</span></span> function: if <span class="inlinecode"><span class="id" type="var">fact</span></span> is
applied to a function that approximates the desired behavior of
<span class="inlinecode"><span class="id" type="var">fact</span></span> up to some number <span class="inlinecode"><span class="id" type="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" type="var">n</span></span>), then it
returns a better approximation to <span class="inlinecode"><span class="id" type="var">fact</span></span> — a function that returns
correct results for inputs up to <span class="inlinecode"><span class="id" type="var">n</span>+1</span>. Applying <span class="inlinecode"><span class="id" type="var">fix</span></span> to this
generator returns its <i>fixed point</i> — a function that gives the
desired behavior for all inputs <span class="inlinecode"><span class="id" type="var">n</span></span>.
<div class="paragraph"> </div>
(The term "fixed point" has exactly the same sense as in ordinary
mathematics, where a fixed point of a function <span class="inlinecode"><span class="id" type="var">f</span></span> is an input <span class="inlinecode"><span class="id" type="var">x</span></span>
such that <span class="inlinecode"><span class="id" type="var">f</span>(<span class="id" type="var">x</span>)</span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">x</span></span>. Here, a fixed point of a function <span class="inlinecode"><span class="id" type="var">F</span></span> of
type (say) <span class="inlinecode">(<span class="id" type="var">Nat</span><span style="font-family: arial;">→</span><span class="id" type="var">Nat</span>)->(<span class="id" type="var">Nat</span><span style="font-family: arial;">→</span><span class="id" type="var">Nat</span>)</span> is a function <span class="inlinecode"><span class="id" type="var">f</span></span> such that <span class="inlinecode"><span class="id" type="var">F</span></span>
<span class="inlinecode"><span class="id" type="var">f</span></span> is behaviorally equivalent to <span class="inlinecode"><span class="id" type="var">f</span></span>.)
<div class="paragraph"> </div>
Syntax:
<pre>
t ::= Terms
| ...
| fix t fixed-point operator
</pre>
Reduction:
<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_Fix1)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">fix t<sub>1</sub> <span style="font-family: arial;">⇒</span> fix t<sub>1</sub>'</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">F = \xf:T<sub>1</sub>.t<sub>2</sub></td>
<td class="infrulenamecol" rowspan="3">
(ST_FixAbs)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">fix F <span style="font-family: arial;">⇒</span> [xf:=fix F]t<sub>2</sub></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>->T<sub>1</sub></td>
<td class="infrulenamecol" rowspan="3">
(T_Fix)
</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> fix t<sub>1</sub> : T<sub>1</sub></td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
Let's see how <span class="inlinecode"><span class="id" type="var">ST_FixAbs</span></span> works by reducing <span class="inlinecode"><span class="id" type="var">fact</span></span> <span class="inlinecode">3</span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">fix</span></span> <span class="inlinecode"><span class="id" type="var">F</span></span> <span class="inlinecode">3</span>,
where <span class="inlinecode"><span class="id" type="var">F</span></span> <span class="inlinecode">=</span> <span class="inlinecode">(\<span class="id" type="var">f</span>.</span> <span class="inlinecode">\<span class="id" type="var">x</span>.</span> <span class="inlinecode"><span class="id" type="keyword">if</span></span> <span class="inlinecode"><span class="id" type="var">x</span>=0</span> <span class="inlinecode"><span class="id" type="keyword">then</span></span> <span class="inlinecode">1</span> <span class="inlinecode"><span class="id" type="keyword">else</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">f</span></span> <span class="inlinecode">(<span class="id" type="var">pred</span></span> <span class="inlinecode"><span class="id" type="var">x</span>)))</span> (we are
omitting type annotations for brevity here).
<pre>
fix F 3
</pre>
<span class="inlinecode"><span style="font-family: arial;">⇒</span></span> <span class="inlinecode"><span class="id" type="var">ST_FixAbs</span></span>
<pre>
(\x. if x=0 then 1 else x * (fix F (pred x))) 3
</pre>
<span class="inlinecode"><span style="font-family: arial;">⇒</span></span> <span class="inlinecode"><span class="id" type="var">ST_AppAbs</span></span>
<pre>
if 3=0 then 1 else 3 * (fix F (pred 3))
</pre>
<span class="inlinecode"><span style="font-family: arial;">⇒</span></span> <span class="inlinecode"><span class="id" type="var">ST_If0_Nonzero</span></span>
<pre>
3 * (fix F (pred 3))
</pre>
<span class="inlinecode"><span style="font-family: arial;">⇒</span></span> <span class="inlinecode"><span class="id" type="var">ST_FixAbs</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">ST_Mult2</span></span>
<pre>
3 * ((\x. if x=0 then 1 else x * (fix F (pred x))) (pred 3))
</pre>
<span class="inlinecode"><span style="font-family: arial;">⇒</span></span> <span class="inlinecode"><span class="id" type="var">ST_PredNat</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">ST_Mult2</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">ST_App2</span></span>
<pre>
3 * ((\x. if x=0 then 1 else x * (fix F (pred x))) 2)
</pre>
<span class="inlinecode"><span style="font-family: arial;">⇒</span></span> <span class="inlinecode"><span class="id" type="var">ST_AppAbs</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">ST_Mult2</span></span>
<pre>
3 * (if 2=0 then 1 else 2 * (fix F (pred 2)))
</pre>
<span class="inlinecode"><span style="font-family: arial;">⇒</span></span> <span class="inlinecode"><span class="id" type="var">ST_If0_Nonzero</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">ST_Mult2</span></span>