-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sub.html
2849 lines (2266 loc) · 228 KB
/
Sub.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>Sub: Subtyping</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">Sub<span class="subtitle">Subtyping</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">MoreStlc</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab736"></a><h1 class="section">Concepts</h1>
<div class="paragraph"> </div>
We now turn to the study of <i>subtyping</i>, perhaps the most
characteristic feature of the static type systems of recently
designed programming languages and a key feature needed to support
the object-oriented programming style.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab737"></a><h2 class="section">A Motivating Example</h2>
<div class="paragraph"> </div>
Suppose we are writing a program involving two record types
defined as follows:
<pre>
Person = {name:String, age:Nat}
Student = {name:String, age:Nat, gpa:Nat}
</pre>
<div class="paragraph"> </div>
In the simply typed lamdba-calculus with records, the term
<pre>
(\r:Person. (r.age)+1) {name="Pat",age=21,gpa=1}
</pre>
is not typable: it involves an application of a function that wants
a one-field record to an argument that actually provides two
fields, while the <span class="inlinecode"><span class="id" type="var">T_App</span></span> rule demands that the domain type of the
function being applied must match the type of the argument
precisely.
<div class="paragraph"> </div>
But this is silly: we're passing the function a <i>better</i> argument
than it needs! The only thing the body of the function can
possibly do with its record argument <span class="inlinecode"><span class="id" type="var">r</span></span> is project the field <span class="inlinecode"><span class="id" type="var">age</span></span>
from it: nothing else is allowed by the type, and the presence or
absence of an extra <span class="inlinecode"><span class="id" type="var">gpa</span></span> field makes no difference at all. So,
intuitively, it seems that this function should be applicable to
any record value that has at least an <span class="inlinecode"><span class="id" type="var">age</span></span> field.
<div class="paragraph"> </div>
Looking at the same thing from another point of view, a record with
more fields is "at least as good in any context" as one with just a
subset of these fields, in the sense that any value belonging to
the longer record type can be used <i>safely</i> in any context
expecting the shorter record type. If the context expects
something with the shorter type but we actually give it something
with the longer type, nothing bad will happen (formally, the
program will not get stuck).
<div class="paragraph"> </div>
The general principle at work here is called <i>subtyping</i>. We say
that "<span class="inlinecode"><span class="id" type="var">S</span></span> is a subtype of <span class="inlinecode"><span class="id" type="var">T</span></span>", informally written <span class="inlinecode"><span class="id" type="var">S</span></span> <span class="inlinecode"><:</span> <span class="inlinecode"><span class="id" type="var">T</span></span>, if a
value of type <span class="inlinecode"><span class="id" type="var">S</span></span> can safely be used in any context where a value
of type <span class="inlinecode"><span class="id" type="var">T</span></span> is expected. The idea of subtyping applies not only to
records, but to all of the type constructors in the language —
functions, pairs, etc.
<div class="paragraph"> </div>
<a name="lab738"></a><h2 class="section">Subtyping and Object-Oriented Languages</h2>
<div class="paragraph"> </div>
Subtyping plays a fundamental role in many programming
languages — in particular, it is closely related to the notion of
<i>subclassing</i> in object-oriented languages.
<div class="paragraph"> </div>
An <i>object</i> in Java, C<span class="inlinecode">#</span>, etc. can be thought of as a record,
some of whose fields are functions ("methods") and some of whose
fields are data values ("fields" or "instance variables").
Invoking a method <span class="inlinecode"><span class="id" type="var">m</span></span> of an object <span class="inlinecode"><span class="id" type="var">o</span></span> on some arguments <span class="inlinecode"><span class="id" type="var">a1</span>..<span class="id" type="var">an</span></span>
consists of projecting out the <span class="inlinecode"><span class="id" type="var">m</span></span> field of <span class="inlinecode"><span class="id" type="var">o</span></span> and applying it to
<span class="inlinecode"><span class="id" type="var">a1</span>..<span class="id" type="var">an</span></span>.
<div class="paragraph"> </div>
The type of an object can be given as either a <i>class</i> or an
<i>interface</i>. Both of these provide a description of which methods
and which data fields the object offers.
<div class="paragraph"> </div>
Classes and interfaces are related by the <i>subclass</i> and
<i>subinterface</i> relations. An object belonging to a subclass (or
subinterface) is required to provide all the methods and fields of
one belonging to a superclass (or superinterface), plus possibly
some more.
<div class="paragraph"> </div>
The fact that an object from a subclass (or sub-interface) can be
used in place of one from a superclass (or super-interface)
provides a degree of flexibility that is is extremely handy for
organizing complex libraries. For example, a GUI toolkit like
Java's Swing framework might define an abstract interface
<span class="inlinecode"><span class="id" type="var">Component</span></span> that collects together the common fields and methods
of all objects having a graphical representation that can be
displayed on the screen and that can interact with the user.
Examples of such object would include the buttons, checkboxes, and
scrollbars of a typical GUI. A method that relies only on this
common interface can now be applied to any of these objects.
<div class="paragraph"> </div>
Of course, real object-oriented languages include many other
features besides these. For example, fields can be updated.
Fields and methods can be declared <span class="inlinecode"><span class="id" type="var">private</span></span>. Classes also give
<i>code</i> that is used when constructing objects and implementing
their methods, and the code in subclasses cooperate with code in
superclasses via <i>inheritance</i>. Classes can have static methods
and fields, initializers, etc., etc.
<div class="paragraph"> </div>
To keep things simple here, we won't deal with any of these
issues — in fact, we won't even talk any more about objects or
classes. (There is a lot of discussion in <i>Types and Programming
Languages</i>, if you are interested.) Instead, we'll study the core
concepts behind the subclass / subinterface relation in the
simplified setting of the STLC.
<div class="paragraph"> </div>
<a name="lab739"></a><h3 class="section"> </h3>
Of course, real OO languages have lots of other features...
<div class="paragraph"> </div>
<ul class="doclist">
<li> mutable fields
</li>
<li> <span class="inlinecode"><span class="id" type="var">private</span></span> and other visibility modifiers
</li>
<li> method inheritance
</li>
<li> static components
</li>
<li> etc., etc.
</li>
</ul>
<div class="paragraph"> </div>
We'll ignore all these and focus on core mechanisms.
<div class="paragraph"> </div>
<a name="lab740"></a><h2 class="section">The Subsumption Rule</h2>
<div class="paragraph"> </div>
Our goal for this chapter is to add subtyping to the simply typed
lambda-calculus (with some of the basic extensions from <span class="inlinecode"><span class="id" type="var">MoreStlc</span></span>).
This involves two steps:
<div class="paragraph"> </div>
<ul class="doclist">
<li> Defining a binary <i>subtype relation</i> between types.
<div class="paragraph"> </div>
</li>
<li> Enriching the typing relation to take subtyping into account.
</li>
</ul>
<div class="paragraph"> </div>
The second step is actually very simple. We add just a single rule
to the typing relation: the so-called <i>rule of subsumption</i>:
<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 : S S <: T</td>
<td class="infrulenamecol" rowspan="3">
(T_Sub)
</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 : T</td>
<td></td>
</td>
</table></center> This rule says, intuitively, that it is OK to "forget" some of
what we know about a term. For example, we may know that <span class="inlinecode"><span class="id" type="var">t</span></span> is a record with two
fields (e.g., <span class="inlinecode"><span class="id" type="var">S</span></span> <span class="inlinecode">=</span> <span class="inlinecode">{<span class="id" type="var">x</span>:<span class="id" type="var">A</span><span style="font-family: arial;">→</span><span class="id" type="var">A</span>,</span> <span class="inlinecode"><span class="id" type="var">y</span>:<span class="id" type="var">B</span><span style="font-family: arial;">→</span><span class="id" type="var">B</span>}</span>), but choose to forget about
one of the fields (<span class="inlinecode"><span class="id" type="var">T</span></span> <span class="inlinecode">=</span> <span class="inlinecode">{<span class="id" type="var">y</span>:<span class="id" type="var">B</span><span style="font-family: arial;">→</span><span class="id" type="var">B</span>}</span>) so that we can pass <span class="inlinecode"><span class="id" type="var">t</span></span> to a
function that requires just a single-field record.
<div class="paragraph"> </div>
<a name="lab741"></a><h2 class="section">The Subtype Relation</h2>
<div class="paragraph"> </div>
The first step — the definition of the relation <span class="inlinecode"><span class="id" type="var">S</span></span> <span class="inlinecode"><:</span> <span class="inlinecode"><span class="id" type="var">T</span></span> — is
where all the action is. Let's look at each of the clauses of its
definition.
<div class="paragraph"> </div>
<a name="lab742"></a><h3 class="section">Structural Rules</h3>
<div class="paragraph"> </div>
To start off, we impose two "structural rules" that are
independent of any particular type constructor: a rule of
<i>transitivity</i>, which says intuitively that, if <span class="inlinecode"><span class="id" type="var">S</span></span> is better than
<span class="inlinecode"><span class="id" type="var">U</span></span> and <span class="inlinecode"><span class="id" type="var">U</span></span> is better than <span class="inlinecode"><span class="id" type="var">T</span></span>, then <span class="inlinecode"><span class="id" type="var">S</span></span> is better than <span class="inlinecode"><span class="id" type="var">T</span></span>...
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">S <: U U <: T</td>
<td class="infrulenamecol" rowspan="3">
(S_Trans)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">S <: T</td>
<td></td>
</td>
</table></center> ... and a rule of <i>reflexivity</i>, since certainly any type <span class="inlinecode"><span class="id" type="var">T</span></span> is
as good as itself:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(S_Refl)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">T <: T</td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
<a name="lab743"></a><h3 class="section">Products</h3>
<div class="paragraph"> </div>
Now we consider the individual type constructors, one by one,
beginning with product types. We consider one pair to be "better
than" another if each of its components is.
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">S<sub>1</sub> <: T<sub>1</sub> S<sub>2</sub> <: T<sub>2</sub></td>
<td class="infrulenamecol" rowspan="3">
(S_Prod)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">S<sub>1</sub> × S<sub>2</sub> <: T<sub>1</sub> × T<sub>2</sub></td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
<a name="lab744"></a><h3 class="section">Arrows</h3>
<div class="paragraph"> </div>
Suppose we have two functions <span class="inlinecode"><span class="id" type="var">f</span></span> and <span class="inlinecode"><span class="id" type="var">g</span></span> with these types:
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">f</span> : <span class="id" type="var">C</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Student</span> <br/>
<span class="id" type="var">g</span> : (<span class="id" type="var">C</span><span style="font-family: arial;">→</span><span class="id" type="var">Person</span>) <span style="font-family: arial;">→</span> <span class="id" type="var">D</span>
<div class="paragraph"> </div>
</div>
That is, <span class="inlinecode"><span class="id" type="var">f</span></span> is a function that yields a record of type <span class="inlinecode"><span class="id" type="var">Student</span></span>,
and <span class="inlinecode"><span class="id" type="var">g</span></span> is a (higher-order) function that expects its (function)
argument to yield a record of type <span class="inlinecode"><span class="id" type="var">Person</span></span>. Also suppose, even
though we haven't yet discussed subtyping for records, that
<span class="inlinecode"><span class="id" type="var">Student</span></span> is a subtype of <span class="inlinecode"><span class="id" type="var">Person</span></span>. Then the application <span class="inlinecode"><span class="id" type="var">g</span></span> <span class="inlinecode"><span class="id" type="var">f</span></span> is
safe even though their types do not match up precisely, because
the only thing <span class="inlinecode"><span class="id" type="var">g</span></span> can do with <span class="inlinecode"><span class="id" type="var">f</span></span> is to apply it to some
argument (of type <span class="inlinecode"><span class="id" type="var">C</span></span>); the result will actually be a <span class="inlinecode"><span class="id" type="var">Student</span></span>,
while <span class="inlinecode"><span class="id" type="var">g</span></span> will be expecting a <span class="inlinecode"><span class="id" type="var">Person</span></span>, but this is safe because
the only thing <span class="inlinecode"><span class="id" type="var">g</span></span> can then do is to project out the two fields
that it knows about (<span class="inlinecode"><span class="id" type="var">name</span></span> and <span class="inlinecode"><span class="id" type="var">age</span></span>), and these will certainly
be among the fields that are present.
<div class="paragraph"> </div>
This example suggests that the subtyping rule for arrow types
should say that two arrow types are in the subtype relation if
their results are:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">S<sub>2</sub> <: T<sub>2</sub></td>
<td class="infrulenamecol" rowspan="3">
(S_Arrow_Co)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">S<sub>1</sub> <span style="font-family: arial;">→</span> S<sub>2</sub> <: S<sub>1</sub> <span style="font-family: arial;">→</span> T<sub>2</sub></td>
<td></td>
</td>
</table></center> We can generalize this to allow the arguments of the two arrow
types to be in the subtype relation as well:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">T<sub>1</sub> <: S<sub>1</sub> S<sub>2</sub> <: T<sub>2</sub></td>
<td class="infrulenamecol" rowspan="3">
(S_Arrow)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">S<sub>1</sub> <span style="font-family: arial;">→</span> S<sub>2</sub> <: T<sub>1</sub> <span style="font-family: arial;">→</span> T<sub>2</sub></td>
<td></td>
</td>
</table></center> Notice that the argument types are subtypes "the other way round":
in order to conclude that <span class="inlinecode"><span class="id" type="var">S<sub>1</sub></span><span style="font-family: arial;">→</span><span class="id" type="var">S<sub>2</sub></span></span> to be a subtype of <span class="inlinecode"><span class="id" type="var">T<sub>1</sub></span><span style="font-family: arial;">→</span><span class="id" type="var">T<sub>2</sub></span></span>, it
must be the case that <span class="inlinecode"><span class="id" type="var">T<sub>1</sub></span></span> is a subtype of <span class="inlinecode"><span class="id" type="var">S<sub>1</sub></span></span>. The arrow
constructor is said to be <i>contravariant</i> in its first argument
and <i>covariant</i> in its second.
<div class="paragraph"> </div>
Here is an example that illustrates this:
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">f</span> : <span class="id" type="var">Person</span> <span style="font-family: arial;">→</span> <span class="id" type="var">C</span><br/>
<span class="id" type="var">g</span> : (<span class="id" type="var">Student</span> <span style="font-family: arial;">→</span> <span class="id" type="var">C</span>) <span style="font-family: arial;">→</span> <span class="id" type="var">D</span>
<div class="paragraph"> </div>
</div>
The application <span class="inlinecode"><span class="id" type="var">g</span></span> <span class="inlinecode"><span class="id" type="var">f</span></span> is safe, because the only thing the body of
<span class="inlinecode"><span class="id" type="var">g</span></span> can do with <span class="inlinecode"><span class="id" type="var">f</span></span> is to apply it to some argument of type
<span class="inlinecode"><span class="id" type="var">Student</span></span>. Since <span class="inlinecode"><span class="id" type="var">f</span></span> requires records having (at least) the
fields of a <span class="inlinecode"><span class="id" type="var">Person</span></span>, this will always work. So <span class="inlinecode"><span class="id" type="var">Person</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">C</span></span> is a
subtype of <span class="inlinecode"><span class="id" type="var">Student</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">C</span></span> since <span class="inlinecode"><span class="id" type="var">Student</span></span> is a subtype of
<span class="inlinecode"><span class="id" type="var">Person</span></span>.
<div class="paragraph"> </div>
The intuition is that, if we have a function <span class="inlinecode"><span class="id" type="var">f</span></span> of type <span class="inlinecode"><span class="id" type="var">S<sub>1</sub></span><span style="font-family: arial;">→</span><span class="id" type="var">S<sub>2</sub></span></span>,
then we know that <span class="inlinecode"><span class="id" type="var">f</span></span> accepts elements of type <span class="inlinecode"><span class="id" type="var">S<sub>1</sub></span></span>; clearly, <span class="inlinecode"><span class="id" type="var">f</span></span>
will also accept elements of any subtype <span class="inlinecode"><span class="id" type="var">T<sub>1</sub></span></span> of <span class="inlinecode"><span class="id" type="var">S<sub>1</sub></span></span>. The type of
<span class="inlinecode"><span class="id" type="var">f</span></span> also tells us that it returns elements of type <span class="inlinecode"><span class="id" type="var">S<sub>2</sub></span></span>; we can
also view these results belonging to any supertype <span class="inlinecode"><span class="id" type="var">T<sub>2</sub></span></span> of
<span class="inlinecode"><span class="id" type="var">S<sub>2</sub></span></span>. That is, any function <span class="inlinecode"><span class="id" type="var">f</span></span> of type <span class="inlinecode"><span class="id" type="var">S<sub>1</sub></span><span style="font-family: arial;">→</span><span class="id" type="var">S<sub>2</sub></span></span> can also be
viewed as having type <span class="inlinecode"><span class="id" type="var">T<sub>1</sub></span><span style="font-family: arial;">→</span><span class="id" type="var">T<sub>2</sub></span></span>.
<div class="paragraph"> </div>
<a name="lab745"></a><h3 class="section">Records</h3>
<div class="paragraph"> </div>
What about subtyping for record types?
<div class="paragraph"> </div>
The basic intuition about subtyping for record types is that it is
always safe to use a "bigger" record in place of a "smaller" one.
That is, given a record type, adding extra fields will always
result in a subtype. If some code is expecting a record with
fields <span class="inlinecode"><span class="id" type="var">x</span></span> and <span class="inlinecode"><span class="id" type="var">y</span></span>, it is perfectly safe for it to receive a record
with fields <span class="inlinecode"><span class="id" type="var">x</span></span>, <span class="inlinecode"><span class="id" type="var">y</span></span>, and <span class="inlinecode"><span class="id" type="var">z</span></span>; the <span class="inlinecode"><span class="id" type="var">z</span></span> field will simply be ignored.
For example,
<div class="paragraph"> </div>
<div class="code code-tight">
{<span class="id" type="var">name</span>:<span class="id" type="var">String</span>, <span class="id" type="var">age</span>:<span class="id" type="var">Nat</span>, <span class="id" type="var">gpa</span>:<span class="id" type="var">Nat</span>} <: {<span class="id" type="var">name</span>:<span class="id" type="var">String</span>, <span class="id" type="var">age</span>:<span class="id" type="var">Nat</span>}<br/>
{<span class="id" type="var">name</span>:<span class="id" type="var">String</span>, <span class="id" type="var">age</span>:<span class="id" type="var">Nat</span>} <: {<span class="id" type="var">name</span>:<span class="id" type="var">String</span>}<br/>
{<span class="id" type="var">name</span>:<span class="id" type="var">String</span>} <: {}
<div class="paragraph"> </div>
</div>
This is known as "width subtyping" for records.
<div class="paragraph"> </div>
We can also create a subtype of a record type by replacing the type
of one of its fields with a subtype. If some code is expecting a
record with a field <span class="inlinecode"><span class="id" type="var">x</span></span> of type <span class="inlinecode"><span class="id" type="var">T</span></span>, it will be happy with a record
having a field <span class="inlinecode"><span class="id" type="var">x</span></span> of type <span class="inlinecode"><span class="id" type="var">S</span></span> as long as <span class="inlinecode"><span class="id" type="var">S</span></span> is a subtype of
<span class="inlinecode"><span class="id" type="var">T</span></span>. For example,
<div class="paragraph"> </div>
<div class="code code-tight">
{<span class="id" type="var">x</span>:<span class="id" type="var">Student</span>} <: {<span class="id" type="var">x</span>:<span class="id" type="var">Person</span>}
<div class="paragraph"> </div>
</div>
This is known as "depth subtyping".
<div class="paragraph"> </div>
Finally, although the fields of a record type are written in a
particular order, the order does not really matter. For example,
<div class="paragraph"> </div>
<div class="code code-tight">
{<span class="id" type="var">name</span>:<span class="id" type="var">String</span>,<span class="id" type="var">age</span>:<span class="id" type="var">Nat</span>} <: {<span class="id" type="var">age</span>:<span class="id" type="var">Nat</span>,<span class="id" type="var">name</span>:<span class="id" type="var">String</span>}
<div class="paragraph"> </div>
</div>
This is known as "permutation subtyping".
<div class="paragraph"> </div>
We could formalize these requirements in a single subtyping rule
for records as follows:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">for each jk in j1..jn,</td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule"><span style="font-family: arial;">∃</span>ip in i1..im, such that</td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">jk=ip and Sp <: Tk</td>
<td class="infrulenamecol" rowspan="3">
(S_Rcd)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">{i1:S<sub>1</sub>...im:Sm} <: {j1:T<sub>1</sub>...jn:Tn}</td>
<td></td>
</td>
</table></center> That is, the record on the left should have all the field labels of
the one on the right (and possibly more), while the types of the
common fields should be in the subtype relation. However, this rule
is rather heavy and hard to read. If we like, we can decompose it
into three simpler rules, which can be combined using <span class="inlinecode"><span class="id" type="var">S_Trans</span></span> to
achieve all the same effects.
<div class="paragraph"> </div>
First, adding fields to the end of a record type gives a subtype:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">n > m</td>
<td class="infrulenamecol" rowspan="3">
(S_RcdWidth)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">{i1:T<sub>1</sub>...in:Tn} <: {i1:T<sub>1</sub>...im:Tm}</td>
<td></td>
</td>
</table></center> We can use <span class="inlinecode"><span class="id" type="var">S_RcdWidth</span></span> to drop later fields of a multi-field
record while keeping earlier fields, showing for example that
<span class="inlinecode">{<span class="id" type="var">age</span>:<span class="id" type="var">Nat</span>,<span class="id" type="var">name</span>:<span class="id" type="var">String</span>}</span> <span class="inlinecode"><:</span> <span class="inlinecode">{<span class="id" type="var">name</span>:<span class="id" type="var">String</span>}</span>.
<div class="paragraph"> </div>
Second, we can apply subtyping inside the components of a compound
record type:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">S<sub>1</sub> <: T<sub>1</sub> ... Sn <: Tn</td>
<td class="infrulenamecol" rowspan="3">
(S_RcdDepth)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">{i1:S<sub>1</sub>...in:Sn} <: {i1:T<sub>1</sub>...in:Tn}</td>
<td></td>
</td>
</table></center> For example, we can use <span class="inlinecode"><span class="id" type="var">S_RcdDepth</span></span> and <span class="inlinecode"><span class="id" type="var">S_RcdWidth</span></span> together to
show that <span class="inlinecode">{<span class="id" type="var">y</span>:<span class="id" type="var">Student</span>,</span> <span class="inlinecode"><span class="id" type="var">x</span>:<span class="id" type="var">Nat</span>}</span> <span class="inlinecode"><:</span> <span class="inlinecode">{<span class="id" type="var">y</span>:<span class="id" type="var">Person</span>}</span>.
<div class="paragraph"> </div>
Third, we need to be able to reorder fields. For example, we
might expect that <span class="inlinecode">{<span class="id" type="var">name</span>:<span class="id" type="var">String</span>,</span> <span class="inlinecode"><span class="id" type="var">gpa</span>:<span class="id" type="var">Nat</span>,</span> <span class="inlinecode"><span class="id" type="var">age</span>:<span class="id" type="var">Nat</span>}</span> <span class="inlinecode"><:</span> <span class="inlinecode"><span class="id" type="var">Person</span></span>. We
haven't quite achieved this yet: using just <span class="inlinecode"><span class="id" type="var">S_RcdDepth</span></span> and
<span class="inlinecode"><span class="id" type="var">S_RcdWidth</span></span> we can only drop fields from the <i>end</i> of a record
type. So we need:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">{i1:S<sub>1</sub>...in:Sn} is a permutation of {i1:T<sub>1</sub>...in:Tn}</td>
<td class="infrulenamecol" rowspan="3">
(S_RcdPerm)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">{i1:S<sub>1</sub>...in:Sn} <: {i1:T<sub>1</sub>...in:Tn}</td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
It is worth noting that full-blown language designs may choose not
to adopt all of these subtyping rules. For example, in Java:
<div class="paragraph"> </div>
<ul class="doclist">
<li> A subclass may not change the argument or result types of a
method of its superclass (i.e., no depth subtyping or no arrow
subtyping, depending how you look at it).
<div class="paragraph"> </div>
</li>
<li> Each class has just one superclass ("single inheritance" of
classes).
<div class="paragraph"> </div>
</li>
<li> Each class member (field or method) can be assigned a single
index, adding new indices "on the right" as more members are
added in subclasses (i.e., no permutation for classes).
<div class="paragraph"> </div>
</li>
<li> A class may implement multiple interfaces — so-called "multiple
inheritance" of interfaces (i.e., permutation is allowed for
interfaces).
</li>
</ul>
<div class="paragraph"> </div>
<a name="lab746"></a><h4 class="section">Exercise: 2 stars (arrow_sub_wrong)</h4>
Suppose we had incorrectly defined subtyping as covariant on both
the right and the left of arrow types:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">S<sub>1</sub> <: T<sub>1</sub> S<sub>2</sub> <: T<sub>2</sub></td>
<td class="infrulenamecol" rowspan="3">
(S_Arrow_wrong)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">S<sub>1</sub> <span style="font-family: arial;">→</span> S<sub>2</sub> <: T<sub>1</sub> <span style="font-family: arial;">→</span> T<sub>2</sub></td>
<td></td>
</td>
</table></center> Give a concrete example of functions <span class="inlinecode"><span class="id" type="var">f</span></span> and <span class="inlinecode"><span class="id" type="var">g</span></span> with the following types...
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">f</span> : <span class="id" type="var">Student</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Nat</span><br/>
<span class="id" type="var">g</span> : (<span class="id" type="var">Person</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Nat</span>) <span style="font-family: arial;">→</span> <span class="id" type="var">Nat</span>
<div class="paragraph"> </div>
</div>
... such that the application <span class="inlinecode"><span class="id" type="var">g</span></span> <span class="inlinecode"><span class="id" type="var">f</span></span> will get stuck during
execution.
<div class="paragraph"> </div>
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab747"></a><h3 class="section">Top</h3>
<div class="paragraph"> </div>
Finally, it is natural to give the subtype relation a maximal
element — a type that lies above every other type and is
inhabited by all (well-typed) values. We do this by adding to the
language one new type constant, called <span class="inlinecode"><span class="id" type="var">Top</span></span>, together with a
subtyping rule that places it above every other type in the
subtype relation:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(S_Top)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">S <: Top</td>
<td></td>
</td>
</table></center> The <span class="inlinecode"><span class="id" type="var">Top</span></span> type is an analog of the <span class="inlinecode"><span class="id" type="var">Object</span></span> type in Java and C<span class="inlinecode">#</span>.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab748"></a><h3 class="section">Summary</h3>
<div class="paragraph"> </div>
In summary, we form the STLC with subtyping by starting with the
pure STLC (over some set of base types) and...
<div class="paragraph"> </div>
<ul class="doclist">
<li> adding a base type <span class="inlinecode"><span class="id" type="var">Top</span></span>,
<div class="paragraph"> </div>
</li>
<li> adding the rule of subsumption
<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 : S S <: T</td>
<td class="infrulenamecol" rowspan="3">
(T_Sub)
</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 : T</td>
<td></td>
</td>
</table></center> to the typing relation, and
<div class="paragraph"> </div>
</li>
<li> defining a subtype relation as follows:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">S <: U U <: T</td>
<td class="infrulenamecol" rowspan="3">
(S_Trans)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">S <: T</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(S_Refl)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">T <: T</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(S_Top)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">S <: Top</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">S<sub>1</sub> <: T<sub>1</sub> S<sub>2</sub> <: T<sub>2</sub></td>
<td class="infrulenamecol" rowspan="3">
(S_Prod)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">S<sub>1</sub> × S<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">T<sub>1</sub> <: S<sub>1</sub> S<sub>2</sub> <: T<sub>2</sub></td>
<td class="infrulenamecol" rowspan="3">
(S_Arrow)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">S<sub>1</sub> <span style="font-family: arial;">→</span> S<sub>2</sub> <: T<sub>1</sub> <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">n > m</td>
<td class="infrulenamecol" rowspan="3">
(S_RcdWidth)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">{i1:T<sub>1</sub>...in:Tn} <: {i1:T<sub>1</sub>...im:Tm}</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">S<sub>1</sub> <: T<sub>1</sub> ... Sn <: Tn</td>
<td class="infrulenamecol" rowspan="3">
(S_RcdDepth)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">{i1:S<sub>1</sub>...in:Sn} <: {i1:T<sub>1</sub>...in:Tn}</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">{i1:S<sub>1</sub>...in:Sn} is a permutation of {i1:T<sub>1</sub>...in:Tn}</td>
<td class="infrulenamecol" rowspan="3">
(S_RcdPerm)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">{i1:S<sub>1</sub>...in:Sn} <: {i1:T<sub>1</sub>...in:Tn}</td>
<td></td>
</td>
</table></center>
</li>
</ul>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab749"></a><h2 class="section">Exercises</h2>
<div class="paragraph"> </div>
<a name="lab750"></a><h4 class="section">Exercise: 1 star, optional (subtype_instances_tf_1)</h4>
Suppose we have types <span class="inlinecode"><span class="id" type="var">S</span></span>, <span class="inlinecode"><span class="id" type="var">T</span></span>, <span class="inlinecode"><span class="id" type="var">U</span></span>, and <span class="inlinecode"><span class="id" type="var">V</span></span> with <span class="inlinecode"><span class="id" type="var">S</span></span> <span class="inlinecode"><:</span> <span class="inlinecode"><span class="id" type="var">T</span></span>
and <span class="inlinecode"><span class="id" type="var">U</span></span> <span class="inlinecode"><:</span> <span class="inlinecode"><span class="id" type="var">V</span></span>. Which of the following subtyping assertions
are then true? Write <i>true</i> or <i>false</i> after each one.
(<span class="inlinecode"><span class="id" type="var">A</span></span>, <span class="inlinecode"><span class="id" type="var">B</span></span>, and <span class="inlinecode"><span class="id" type="var">C</span></span> here are base types.)
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" type="var">T</span><span style="font-family: arial;">→</span><span class="id" type="var">S</span></span> <span class="inlinecode"><:</span> <span class="inlinecode"><span class="id" type="var">T</span><span style="font-family: arial;">→</span><span class="id" type="var">S</span></span>
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode"><span class="id" type="var">Top</span><span style="font-family: arial;">→</span><span class="id" type="var">U</span></span> <span class="inlinecode"><:</span> <span class="inlinecode"><span class="id" type="var">S</span><span style="font-family: arial;">→</span><span class="id" type="var">Top</span></span>
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">(<span class="id" type="var">C</span><span style="font-family: arial;">→</span><span class="id" type="var">C</span>)</span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode">(<span class="id" type="var">A</span>×<span class="id" type="var">B</span>)</span> <span class="inlinecode"><:</span> <span class="inlinecode">(<span class="id" type="var">C</span><span style="font-family: arial;">→</span><span class="id" type="var">C</span>)</span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode">(<span class="id" type="var">Top</span>×<span class="id" type="var">B</span>)</span>
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode"><span class="id" type="var">T</span><span style="font-family: arial;">→</span><span class="id" type="var">T</span><span style="font-family: arial;">→</span><span class="id" type="var">U</span></span> <span class="inlinecode"><:</span> <span class="inlinecode"><span class="id" type="var">S</span><span style="font-family: arial;">→</span><span class="id" type="var">S</span><span style="font-family: arial;">→</span><span class="id" type="var">V</span></span>
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">(<span class="id" type="var">T</span><span style="font-family: arial;">→</span><span class="id" type="var">T</span>)<span style="font-family: arial;">→</span><span class="id" type="var">U</span></span> <span class="inlinecode"><:</span> <span class="inlinecode">(<span class="id" type="var">S</span><span style="font-family: arial;">→</span><span class="id" type="var">S</span>)<span style="font-family: arial;">→</span><span class="id" type="var">V</span></span>
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">((<span class="id" type="var">T</span><span style="font-family: arial;">→</span><span class="id" type="var">S</span>)<span style="font-family: arial;">→</span><span class="id" type="var">T</span>)<span style="font-family: arial;">→</span><span class="id" type="var">U</span></span> <span class="inlinecode"><:</span> <span class="inlinecode">((<span class="id" type="var">S</span><span style="font-family: arial;">→</span><span class="id" type="var">T</span>)<span style="font-family: arial;">→</span><span class="id" type="var">S</span>)<span style="font-family: arial;">→</span><span class="id" type="var">V</span></span>
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode"><span class="id" type="var">S</span>×<span class="id" type="var">V</span></span> <span class="inlinecode"><:</span> <span class="inlinecode"><span class="id" type="var">T</span>×<span class="id" type="var">U</span></span>
</li>
</ul>
<div class="paragraph"> </div>
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab751"></a><h4 class="section">Exercise: 2 stars (subtype_order)</h4>
The following types happen to form a linear order with respect to subtyping:
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" type="var">Top</span></span>
</li>
<li> <span class="inlinecode"><span class="id" type="var">Top</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">Student</span></span>
</li>
<li> <span class="inlinecode"><span class="id" type="var">Student</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">Person</span></span>
</li>
<li> <span class="inlinecode"><span class="id" type="var">Student</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">Top</span></span>
</li>
<li> <span class="inlinecode"><span class="id" type="var">Person</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">Student</span></span>
</li>
</ul>
<div class="paragraph"> </div>
Write these types in order from the most specific to the most general.
<div class="paragraph"> </div>
Where does the type <span class="inlinecode"><span class="id" type="var">Top</span><span style="font-family: arial;">→</span><span class="id" type="var">Top</span><span style="font-family: arial;">→</span><span class="id" type="var">Student</span></span> fit into this order?
<div class="paragraph"> </div>
<div class="paragraph"> </div>
<a name="lab752"></a><h4 class="section">Exercise: 1 star (subtype_instances_tf_2)</h4>
Which of the following statements are true? Write <i>true</i> or
<i>false</i> after each one.
<div class="paragraph"> </div>
<div class="code code-tight">
<span style="font-family: arial;">∀</span><span class="id" type="var">S</span> <span class="id" type="var">T</span>,<br/>
<span class="id" type="var">S</span> <: <span class="id" type="var">T</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">S</span><span style="font-family: arial;">→</span><span class="id" type="var">S</span> <: <span class="id" type="var">T</span><span style="font-family: arial;">→</span><span class="id" type="var">T</span><br/>
<br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">S</span>,<br/>
<span class="id" type="var">S</span> <: <span class="id" type="var">A</span><span style="font-family: arial;">→</span><span class="id" type="var">A</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">∃</span><span class="id" type="var">T</span>,<br/>
<span class="id" type="var">S</span> = <span class="id" type="var">T</span><span style="font-family: arial;">→</span><span class="id" type="var">T</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">T</span> <: <span class="id" type="var">A</span><br/>
<br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">S</span> <span class="id" type="var">T<sub>1</sub></span> <span class="id" type="var">T<sub>2</sub></span>,<br/>
(<span class="id" type="var">S</span> <: <span class="id" type="var">T<sub>1</sub></span> <span style="font-family: arial;">→</span> <span class="id" type="var">T<sub>2</sub></span>) <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">∃</span><span class="id" type="var">S<sub>1</sub></span> <span class="id" type="var">S<sub>2</sub></span>,<br/>
<span class="id" type="var">S</span> = <span class="id" type="var">S<sub>1</sub></span> <span style="font-family: arial;">→</span> <span class="id" type="var">S<sub>2</sub></span> <span style="font-family: arial;">∧</span> <span class="id" type="var">T<sub>1</sub></span> <: <span class="id" type="var">S<sub>1</sub></span> <span style="font-family: arial;">∧</span> <span class="id" type="var">S<sub>2</sub></span> <: <span class="id" type="var">T<sub>2</sub></span> <br/>
<br/>
<span style="font-family: arial;">∃</span><span class="id" type="var">S</span>,<br/>
<span class="id" type="var">S</span> <: <span class="id" type="var">S</span><span style="font-family: arial;">→</span><span class="id" type="var">S</span> <br/>
<br/>
<span style="font-family: arial;">∃</span><span class="id" type="var">S</span>,<br/>
<span class="id" type="var">S</span><span style="font-family: arial;">→</span><span class="id" type="var">S</span> <: <span class="id" type="var">S</span> <br/>
<br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">S</span> <span class="id" type="var">T<sub>1</sub></span> <span class="id" type="var">T<sub>2</sub></span>,<br/>
<span class="id" type="var">S</span> <: <span class="id" type="var">T<sub>1</sub></span>×<span class="id" type="var">T<sub>2</sub></span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">∃</span><span class="id" type="var">S<sub>1</sub></span> <span class="id" type="var">S<sub>2</sub></span>,<br/>
<span class="id" type="var">S</span> = <span class="id" type="var">S<sub>1</sub></span>×<span class="id" type="var">S<sub>2</sub></span> <span style="font-family: arial;">∧</span> <span class="id" type="var">S<sub>1</sub></span> <: <span class="id" type="var">T<sub>1</sub></span> <span style="font-family: arial;">∧</span> <span class="id" type="var">S<sub>2</sub></span> <: <span class="id" type="var">T<sub>2</sub></span>
<div class="paragraph"> </div>
</div>
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab753"></a><h4 class="section">Exercise: 1 star (subtype_concepts_tf)</h4>
Which of the following statements are true, and which are false?
<div class="paragraph"> </div>
<ul class="doclist">
<li> There exists a type that is a supertype of every other type.
<div class="paragraph"> </div>
</li>
<li> There exists a type that is a subtype of every other type.
<div class="paragraph"> </div>
</li>
<li> There exists a pair type that is a supertype of every other
pair type.
<div class="paragraph"> </div>
</li>
<li> There exists a pair type that is a subtype of every other
pair type.
<div class="paragraph"> </div>
</li>
<li> There exists an arrow type that is a supertype of every other
arrow type.
<div class="paragraph"> </div>
</li>
<li> There exists an arrow type that is a subtype of every other
arrow type.
<div class="paragraph"> </div>
</li>
<li> There is an infinite descending chain of distinct types in the
subtype relation—-that is, an infinite sequence of types
<span class="inlinecode"><span class="id" type="var">S0</span></span>, <span class="inlinecode"><span class="id" type="var">S<sub>1</sub></span></span>, etc., such that all the <span class="inlinecode"><span class="id" type="var">Si</span></span>'s are different and
each <span class="inlinecode"><span class="id" type="var">S</span>(<span class="id" type="var">i</span>+1)</span> is a subtype of <span class="inlinecode"><span class="id" type="var">Si</span></span>.
<div class="paragraph"> </div>