Conceptcore
Subtyping
Also called subtype polymorphism, inclusion polymorphism.
A relation S <: T saying a value of type S can be used wherever a T is expected; functions are contravariant in the argument, and records allow width and depth subtyping.
Subtyping is a relation between types that says a value of type is safe to use wherever a value of type is expected. A type system with subtyping adds one typing rule, subsumption: if a term has type and , then it also has type . So one term has many types, and one function accepts arguments of many types. This is the form of polymorphism behind classes and interfaces in object-oriented languages. Cardelli (1984) gave the first formal rules for records and functions, and the rule for functions surprises many people: it reverses the order on the argument.
In plain words
Suppose a function wants a point with an x field. You can give it a point that also has a y field, because the function never looks at y. So "point with x and y" is a subtype of "point with x". That is subtyping: a more detailed thing can stand in for a less detailed thing. The surprise is with functions. Where a function that accepts any point with x is expected, you can use a function that accepts more, but not one that accepts less. A function that needs y cannot be used where callers may pass a point without y. So for the input of a function the order flips. For the output it does not. Getting these directions wrong lets a program read a field that is not there, and real languages have made this mistake.
The idea
The rules
Add to the simply typed lambda calculus a type and record types with distinct labels, record values and projection . The subtype relation is the smallest relation closed under these rules (Pierce, Chapter 15).
S <: U U <: T
------ S-Refl ---------------- S-Trans -------- S-Top
S <: S S <: T S <: Top
T1 <: S1 S2 <: T2
-------------------- S-Arrow
S1 -> S2 <: T1 -> T2
------------------------------------------------------- S-RcdWidth
{l1:T1, ..., ln:Tn, ..., ln+k:Tn+k} <: {l1:T1, ..., ln:Tn}
for each i: Si <: Ti {k1:S1, ..., kn:Sn} is a permutation of {l1:T1, ..., ln:Tn}
------------------------------------- S-RcdDepth ---------------------------------------------- S-RcdPerm
{l1:S1, ..., ln:Sn} <: {l1:T1, ..., ln:Tn} {k1:S1, ..., kn:Sn} <: {l1:T1, ..., ln:Tn}
Gamma |- t : S S <: T
------------------------ T-Sub
Gamma |- t : T
S-Arrow is contravariant in the argument (the order flips from to the arrow) and covariant in the result. Records are covariant in each field (depth), and a record with more fields is a subtype of one with fewer (width).
Why the arrow rule is right
A function is used at type by a caller that passes a and expects a . The call is safe if can accept any , which needs , and if what returns can serve as a , which needs . Two contravariant positions cancel: in , the type is in a covariant position.
Soundness
Progress and preservation still hold, with two changes in the proof (Pierce, Section 15.3). Canonical forms needs inversion lemmas for subtyping: if , then with and ; if is a subtype of a record type, then is a record type with at least the same labels, and the fields are subtypes. Preservation must allow a step to make the type smaller: after E-AppAbs, the substituted value may have a subtype of the parameter type, and T-Sub covers it.
Algorithmic subtyping
The rules above are not syntax directed. S-Trans has a middle type that does not appear in the conclusion, and T-Sub can be applied at any node. A checker uses equivalent algorithmic rules (Pierce, Chapter 16):
- Drop S-Refl and S-Trans, which are admissible: they can be derived from the others.
- Merge the three record rules into one: if each is some with .
- Drop T-Sub and check subtyping only where types meet: in T-App, require that the argument type is a subtype of the domain, instead of equal to it.
The algorithmic system computes the minimal type of each term, and it accepts a term exactly when the declarative system does. A conditional needs the join (least upper bound) of the types of its branches. For this language with , joins always exist; for records, the join keeps the common labels.
Bounded quantification
System F with subtyping, , has types : a polymorphic function that works for every subtype of . Cardelli and Wegner (1985) proposed it to type functions on records that return their argument with its full type. The full subtype rule for these types compares the bounds contravariantly. Pierce (1994) proved that this makes subtyping in full undecidable, by a reduction from the halting problem for two-counter machines. The kernel , which requires the two bounds to be equal, is decidable. Java generics with wildcards and Scala have related undecidability results.
Other choices
- Inclusion or coercion. In the inclusion view, a value of type simply is a value of type . In the coercion view, comes with a conversion function, as for . Then each subtyping derivation inserts conversions, and the design must be coherent: two derivations of the same judgment must give the same program.
- Structural or nominal. Structural subtyping, as above, follows the shape of types (OCaml objects, TypeScript, Go interfaces). Nominal subtyping holds only between declared names (Java and C# classes), and the checker verifies that each declaration is consistent with the structural rules.
- Recursive types. With recursive types, subtyping is decided on infinite regular trees, by Amadio and Cardelli's rules. Kozen, Palsberg and Schwartzbach (1995) gave an algorithm.
- Rows. Row polymorphism gets much of the effect of width subtyping with type variables instead, and it works with ML-style inference.
Examples
A script with the algorithmic rules checks each subtype claim below. Let and .
| claim | holds | reason |
|---|---|---|
| yes | width | |
| no | has no field | |
| yes | contravariant argument | |
| no | contravariant argument | |
| yes | covariant result | |
| yes | width, then depth with S-Top | |
| yes | two flips cancel | |
| no | two flips cancel |
Subsumption at a call. Let . The application is well typed: the argument has type , T-Sub with gives it type , and T-App gives . It steps to and then to .
Why a covariant argument would be unsound. Let , of type . If the rules allowed , then would be typed . It steps to , which is stuck: the record has no field . The table shows that the algorithm rejects this subtype claim.
Mutable cells must be invariant. Suppose . A function of type may write into the cell, and the owner of the then reads field of it: stuck. Suppose instead . Then a reader reads from a cell that holds only : stuck again. So needs both and . Pierce (Section 15.5) splits a reference into a covariant source and a contravariant sink to recover some flexibility.
Java arrays. Java makes arrays covariant: String[] is a subtype of Object[]. By the argument above, this is unsound for writes. The program Object[] a = new String[1]; a[0] = Integer.valueOf(1); passes the compiler. The JVM keeps the element type with each array and checks every store, so the second statement throws ArrayStoreException. The language is safe only because of this run-time check. Java generics, which came later, are invariant: List<String> is not a subtype of List<Object>.
Where it is used
- Object-oriented languages. A subclass is a subtype of its superclass, so a method that takes a
Shapeaccepts aCircle. Method overriding must follow S-Arrow: Java and C# allow a covariant return type in an override, but not a contravariant parameter type, which they treat as overloading instead. - Variance annotations. Scala (
+A,-A), Kotlin (out,in) and C# (out T,in T) let the programmer declare the variance of a type parameter, and the checker verifies that the parameter appears only in positions of that variance. Java uses wildcards (? extends T,? super T) at use sites instead. - Structural typing. TypeScript checks object types by width and depth, and Go checks that a type has the methods of an interface. TypeScript deliberately accepts unsound method parameter bivariance in some places, for convenience.
- Numeric types. Many languages treat as a coercion.
- Type checking with subtyping is mostly done with bidirectional rules, because full inference with subtyping gives large sets of subtype constraints that are hard to solve and present.
References
- Benjamin C. Pierce, Types and Programming Languages, MIT Press, 2002, Chapter 15 (the rules S-Refl, S-Trans, S-Top, S-Arrow, S-RcdWidth, S-RcdDepth, S-RcdPerm and T-Sub, the inversion lemmas for subtyping and soundness), Chapter 16 (algorithmic subtyping and typing, joins and meets), Chapter 26 and Section 28.5 (bounded quantification and its undecidability)
- Luca Cardelli, A semantics of multiple inheritance, in Semantics of Data Types, LNCS 173, Springer, 1984, pages 51 to 66, the record and function subtyping rules and the soundness of the typing with subsumption
- Luca Cardelli, A semantics of multiple inheritance, Information and Computation 76(2-3), 1988, pages 138 to 164, the journal version of the 1984 paper
- Luca Cardelli and Peter Wegner, On understanding types, data abstraction, and polymorphism, ACM Computing Surveys 17(4), 1985, the classification of polymorphism into parametric, inclusion, overloading and coercion, and bounded quantification
- Benjamin C. Pierce, Bounded quantification is undecidable, Information and Computation 112(1), 1994, pages 131 to 165, the main theorem (subtyping in full F-sub is undecidable), by reduction from the halting problem for two-counter machines; POPL 1992 version
- Dexter Kozen, Jens Palsberg and Michael I. Schwartzbach, Efficient recursive subtyping, Mathematical Structures in Computer Science 5(1), 1995, pages 113 to 125, the O(n^2) algorithm for subtyping of recursive types (POPL 1993 version)
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag GDSKYF Edit this page History Report an error Markdown