Lesson 6.5 — Subtyping: structural and algorithmic, variance, nominal vs structural¶
Techniques: structural subtyping with top and bottom, records (width, depth, permutation) and functions (contravariant parameters), declarative rules with subsumption vs the algorithmic system with transitivity eliminated (Cardelli 1984/1988 [Car88]; [TAPL, Ch. 15–16]), and subtyping vs coercion [BTCGS91]; variance of generic types — declaration-site (Scala, Kotlin
in/out, C#) vs use-site (Java wildcards [IV02, THEAG04]) — and why mutable containers must be invariant; nominal vs structural subtyping (Java, C#, Swift classes [IPW01] vs TypeScript, Go interfaces, OCaml objects) · Pebble implements: none — Pebble's types are nominal for structs and structural for arrays, with no subtyping (pebble-spec §5); the lab's ★ milestone L4 implements the algorithm and tests it against a brute-force search of the declarative rules · Drills:subtype-query· Prerequisites: Lessons 6.1, 6.2 and 6.4 · Time: 5 hours
Subtyping says that a value of one type can be used wherever a value of another type is expected: a record with more fields where fewer are needed, a function that accepts more where one that accepts less is required, a Dog where an Animal will do. The declarative rules are short and obviously sensible, but one of them — transitivity — makes them useless as an algorithm: to show that a type \(S\) is a subtype of a type \(U\) it lets you guess any intermediate type \(T\). This lesson derives the algorithm that TypeScript, Scala and every structural checker run, proves it equivalent to the rules, extends subtyping to generic types (where the direction of the relation depends on how a type parameter is used), and contrasts subtyping by shape with subtyping by declared name.
1. Problem and motivation¶
Structural subtyping and algorithmic subtyping¶
The problem. A function norm(p: {x: float, y: float}) should accept a point with an extra z field; a callback of type Animal -> int should be usable where Dog -> int is expected. The principle of safe substitution says \(S <: T\) ("\(S\) is a subtype of \(T\)") when every value of type \(S\) can be safely used in every context expecting a \(T\) [TAPL, §15.1]. Cardelli gave the structural rules for records and functions to model inheritance [Car88]; the subsumption rule (\(e : S\) and \(S <: T\) imply \(e : T\)) plugs them into typing. Both the subsumption rule and the transitivity rule of subtyping are not syntax-directed, so an implementation needs an algorithmic system in which they are admissible rather than primitive [TAPL, Ch. 16]. In a bidirectional checker (Lesson 6.4) subsumption is needed in exactly one place, the mode switch. Pebble has no subtyping, so its mode switch compares with equality; the lab's L4 implements the algorithm for a calculus with records and functions.
Variance of generic types¶
The problem. Given Dog <: Animal, is List<Dog> <: List<Animal>? For a read-only list yes (covariance), for a sink that only consumes animals the reverse (contravariance), and for a mutable list neither (invariance): Lesson 6.2 showed covariant mutable arrays are unsound without a run-time check. Languages either let the designer of a type declare each parameter's variance and check the declaration (declaration-site variance: Scala +T/-T, Kotlin and C# out/in) or let the user ask for a covariant or contravariant view at each use (use-site variance: Java's ? extends T and ? super T, from Igarashi and Viroli's variant parametric types [IV02, THEAG04]). Rust infers variance from the type's fields [RUSTC-Variance].
Nominal vs structural subtyping¶
The problem. Is {value: float} meant as meters the same type as {value: float} meant as seconds? Structural type systems say yes: types are compared by shape (TypeScript, OCaml objects, Go interfaces). Nominal systems say no: a type is a name, and \(S <: T\) only if \(S\) is declared to extend \(T\) (Java, C#, Swift classes and protocols; Featherweight Java [IPW01] is the formal model). Nominal typing gives abstraction and cheap checks; structural typing gives flexibility for independently written code. Pebble's structs are nominal (two structs with the same fields are different types) and its arrays structural ([int; 3] equals [int; 3]), with no subtyping between distinct types (pebble-spec §5).
2. Definitions and algorithms¶
Structural subtyping and algorithmic subtyping¶
Definition 6.5.1 (Types with top, bottom and records)
Let \(\mathcal{L}\) be a countably infinite set of labels. The set \(\mathrm{Ty}\) of types of this lesson is given, for labels \(l_1, \dots, l_n \in \mathcal{L}\) and \(n \ge 0\), by the grammar \(\tau ::= \mathsf{int} \mid \mathsf{bool} \mid \top \mid \bot \mid \tau_1 \to \tau_2 \mid \{l_1{:}\tau_1, \dots, l_n{:}\tau_n\}\), where the labels of a record type are pairwise distinct and a record type is identified with the record type that has the same fields in any other order. \(\top\) is the top type, the type of every value; \(\bot\) is the bottom type, which has no values.
TypeScript writes \(\top\) as unknown and \(\bot\) as never; Java's Object is the top of its reference types; Rust writes \(\bot\) as !. An expression of type \(\bot\) never returns: it throws or loops.
Definition 6.5.2 (Declarative subtyping)
Let \(S, T, U, S_1, S_2, T_1, T_2, \tau_1, \tau_2, \ldots\) range over types (Definition 6.5.1), \(l_1, l_2, \ldots\) over labels and \(n, k \ge 0\) over integers, and let \(\{l_i{:}S_i\}_{i=1}^{n}\) abbreviate \(\{l_1{:}S_1, \dots, l_n{:}S_n\}\). The declarative subtyping relation \(<:\) on types is the smallest relation closed under the rules
(S-RcdPerm, reordering fields, is built into the identification of record types.) For a typing context \(\Gamma\) and a term \(e\), the typing rules get the subsumption rule \(\dfrac{\Gamma \vdash e : S \qquad S <: T}{\Gamma \vdash e : T}\) (T-Sub).
Definition 6.5.3 (Algorithmic subtyping)
Let \(S, T, S_1, S_2, T_1, T_2, S_i, T_j\) range over types, \(B\) over the base types \(\{\mathsf{int}, \mathsf{bool}\}\), \(k_1, \dots, k_m\) and \(l_1, \dots, l_n\) over labels and \(m, n \ge 0\) over integers. The algorithmic subtyping relation \(\blacktriangleright S <: T\) is the smallest relation closed under the following rules, of which exactly one applies to any pair of types (checked top to bottom):
No rule applies to any other pair of types.
No rule relates \(\mathsf{int}\) to \(\mathsf{bool}\), a record to an arrow, or \(\top\) to anything but \(\top\): the algorithm answers "no" there.
Algorithm 6.5.4 (Subtype)
- Input: types \(S\) and \(T\) (Definition 6.5.1).
- Output: true iff \(\blacktriangleright S <: T\).
- Precondition: record labels are distinct.
- Postcondition: the result equals the declarative relation (Theorem 6.5.13).
- Invariant: every recursive call is on a pair of strictly smaller types (a subterm of \(S\) and a subterm of \(T\)).
function Subtype(S, T):
if T = ⊤ or S = ⊥: return true # SA-Top, SA-Bot
if S, T are both int or both bool: return true # SA-Base
if S = S1 → S2 and T = T1 → T2: # SA-Arrow
return Subtype(T1, S1) and Subtype(S2, T2) # parameters swap sides
if S = {k_i : S_i} and T = {l_j : T_j}: # SA-Rcd
for each field l_j : T_j of T:
if l_j is not a label of S: return false # width: S may have more, not fewer
if not Subtype(S.field(l_j), T_j): return false # depth, found by label (permutation)
return true
return false
The lab's isSubtype (labs/ch06-bidir/include/bidir/Subtyping.h) and the drill subtype-query implement this algorithm; the drill adds the variance rules below.
Variance of generic types¶
Definition 6.5.5 (Variance; positions)
Let \(C\) be a type constructor with one parameter \(X\), written \(C[X]\), and let \(S, T\) range over types. \(C\) is covariant in \(X\) if \(S <: T\) implies \(C[S] <: C[T]\) for all \(S, T\); contravariant if \(S <: T\) implies \(C[T] <: C[S]\) for all \(S, T\); invariant if \(C[S] <: C[T]\) holds only when \(S <: T\) and \(T <: S\) (for the types of this lesson, only when \(S = T\)); and bivariant if \(C[S] <: C[T]\) for all \(S, T\). Every constructor other than \(\to\) carries a declared variance in \(\{+, -, =\}\) (covariant, contravariant, invariant). The polarity of an occurrence of a type variable \(X\) in a type \(\tau\) is an element of \(\{+, -\}\) defined by induction on \(\tau\): the occurrence \(\tau = X\) has polarity \(+\); in \(\tau = \tau_1 \to \tau_2\), an occurrence inside the parameter \(\tau_1\) has the opposite of its polarity in \(\tau_1\) (flipped: \(+ \leftrightarrow -\)) and an occurrence inside the result \(\tau_2\) keeps its polarity in \(\tau_2\); in \(\tau = D[\tau']\) for a constructor \(D\), an occurrence inside \(\tau'\) keeps its polarity in \(\tau'\) if \(D\) is declared covariant, has it flipped if \(D\) is declared contravariant, and has both polarities if \(D\) is declared invariant. Hence the polarity is the product of the signs along the path from \(\tau\) to the occurrence (\(+ \cdot - = -\)).
Definition 6.5.6 (Declaration-site and use-site variance)
Let \(C\) be a generic type constructor with one parameter \(X\) (Definition 6.5.5) and \(S, T, U\) types. Declaration-site: the declaration class C[+X] (Scala; out X in Kotlin and C#) promises covariance; the checker verifies that \(X\) occurs only in \(+\) positions of the class's members (method results, read-only fields), and dually for -X / in X (\(-\) positions: method parameters). Use-site: every generic type is invariant, and a use may write C<? extends T> (Java) — the type of all C<S> with \(S <: T\), whose members may only be used covariantly — or C<? super T>, the contravariant view. Subtyping between wildcard types is decided by containment (JLS §4.5.1): ? extends U contains ? extends T if \(T <: U\), and contains T itself (so C<T> <: C<? extends U> for every \(T <: U\)); dually, ? super T contains ? super U if \(T <: U\).
Algorithm 6.5.7 (Checking a declaration-site variance annotation)
- Input: a generic class \(C\) with one type parameter \(X\) of declared variance \(v \in \{+, -, =\}\), written \(C[vX]\), and the types of its members.
- Output: the list of occurrences of \(X\) in the members' types whose polarity (Definition 6.5.5) contradicts \(v\).
- Precondition: the declared variances of every other type constructor are known.
- Postcondition: no reported occurrence ⇒ for all types \(S, T\) with \(S <: T\), the subtyping \(C[S] <: C[T]\) (for \(v = +\); \(C[T] <: C[S]\) for \(v = -\)) is safe (Theorem 6.5.15).
- Invariant:
Walk(τ, p)is called with \(p\) the polarity of the position of \(\tau\) in the member's type;badholds the occurrences reported so far.
function CheckVariance(C, v): # flip(+) = −, flip(−) = +
bad ← []
for each member m of C:
if m is a method with parameters P1..Pk and result R:
for each Pi: Walk(Pi, −)
Walk(R, +)
if m is a mutable field of type F: Walk(F, +); Walk(F, −) # read and written
if m is a read-only field of type F: Walk(F, +)
return bad
function Walk(τ, p):
case τ of
X: if v = + and p = −, or v = − and p = +: bad.append(this occurrence)
A → B: Walk(A, flip(p)); Walk(B, p)
D[τ1] with declared variance w:
if w = +: Walk(τ1, p)
if w = −: Walk(τ1, flip(p))
if w = =: Walk(τ1, +); Walk(τ1, −)
otherwise: nothing
The drill's types use List[+T], Sink[-T] and Cell[T] (invariant), and Algorithm 6.5.4 gains one rule per constructor: List[S] <: List[T] if \(S <: T\); Sink[S] <: Sink[T] if \(T <: S\); Cell[S] <: Cell[T] if both.
Nominal vs structural subtyping¶
Definition 6.5.8 (Nominal subtyping)
Let \(\mathcal{C}\) be a finite set of class names containing a distinguished root \(\mathsf{Object}\). A class table \(\mathit{CT}\) assigns to every class name \(C \in \mathcal{C} \setminus \{\mathsf{Object}\}\) a declaration class C extends D implements I1, … { fields; methods } naming its superclass \(\mathrm{super}(C) \in \mathcal{C}\) (the class \(D\) it extends), a set \(\mathrm{ifaces}(C) \subseteq \mathcal{C}\) of interfaces it implements, its fields and its methods; the declared supertypes of \(C\) are \(\{\mathrm{super}(C)\} \cup \mathrm{ifaces}(C)\), and the declared-supertype relation is required to be acyclic. Nominal subtyping \(C <:_{N} D\), for class names \(C, D \in \mathcal{C}\), is the reflexive-transitive closure of the declared-supertype relation [IPW01, §2]. Two classes with identical members but no chain of declared supertypes between them are unrelated. Structural subtyping (Definition 6.5.2) compares members instead of names.
Algorithm 6.5.9 (Nominal subtype test by walking the supertype chain)
- Input: a class table \(\mathit{CT}\) over class names \(\mathcal{C}\) (Definition 6.5.8) and two class names \(C, D \in \mathcal{C}\).
- Output: true iff \(C <:_{N} D\).
- Precondition: the declared-supertype relation is acyclic (checked when classes are declared).
- Postcondition: the result is the closure of Definition 6.5.8.
- Invariant:
cis always a class name with \(C <:_{N}\)c.
3. Worked examples¶
Structural subtyping and algorithmic subtyping¶
Algorithm 6.5.4 on \(S = \{a{:}\mathsf{int},\ f{:}\{x{:}\mathsf{int}\} \to \mathsf{int}\}\) and \(T = \{f{:}\{x{:}\mathsf{int}, y{:}\mathsf{bool}\} \to \top\}\), where \(a, f, x, y\) are labels (the drill's format; indentation = depth):
| call | pair | rule | result |
|---|---|---|---|
| 1 | {a: int, f: {x: int} -> int} <: {f: {x: int, y: bool} -> top} |
SA-Rcd: T's only label f is in S |
yes |
| 2 | · {x: int} -> int <: {x: int, y: bool} -> top |
SA-Arrow | yes |
| 3 | · · {x: int, y: bool} <: {x: int} (parameters swapped) |
SA-Rcd: width | yes |
| 4 | · · · int <: int |
SA-Base | yes |
| 5 | · · int <: top |
SA-Top | yes |
- Width appears twice, in opposite directions: at call 1 \(S\) may have the extra field
a; at call 3 the expected parameter may have the extra fieldy, because the parameters were swapped. A function that needs onlyxcan accept records that also havey. - The same judgment derived declaratively needs S-Trans: e.g. \(S <: \{f{:}\{x{:}\mathsf{int}\} \to \mathsf{int}\}\) by width, then that \(<: T\) by depth with S-Arrow. The algorithm fuses width, depth and permutation into SA-Rcd, so no intermediate type is ever guessed.
Try it
./course drill subtype-query --seed 5 --difficulty medium --solution prints one table like this per query.
Variance of generic types¶
Algorithm 6.5.7 on class Box[+X] { get(): X; set(v: X): unit; map(f: X -> int): Box[int] }:
| member | occurrence | polarity computation | polarity | verdict for +X |
|---|---|---|---|---|
get(): X |
result | result: \(+\) | \(+\) | ok |
set(v: X) |
parameter | parameter: flip \(+\) → \(-\) | \(-\) | error: a covariant X cannot be consumed |
map(f: X -> int) |
inside a parameter of a parameter | parameter: \(-\); parameter of the function type: flip → \(+\) | \(+\) | ok |
map shows why polarity is multiplicative: X is an argument to a callback that Box supplies, so Box produces values of X there. Removing set makes Box safely covariant — the read-only-container case.
Nominal vs structural subtyping¶
With class Meters { value: float } and class Seconds { value: float }, both extending Object: NominalSubtype(Seconds, Meters) walks Seconds → Object without meeting Meters and returns false (javac rejects speed(s, m) in the box of §7); structurally, {value: float} <: {value: float} by SA-Rcd (TypeScript accepts it). Adding a phantom field of a unique literal type ("branding") makes the shapes differ, which is how TypeScript programmers simulate nominal types.
4. Invariants and correctness¶
Structural subtyping and algorithmic subtyping¶
Lemma 6.5.10 (Reflexivity is admissible in the algorithmic system)
\(\blacktriangleright T <: T\) for every type \(T\).
Proof
By induction on \(T\). \(\top\): SA-Top. \(\bot\): SA-Bot. Base types: SA-Base. \(T_1 \to T_2\): by the hypothesis \(\blacktriangleright T_1 <: T_1\) and \(\blacktriangleright T_2 <: T_2\), so SA-Arrow. Records: the label sets are equal (so included) and each field is related to itself by the hypothesis, so SA-Rcd.
Lemma 6.5.11 (Transitivity is admissible in the algorithmic system)
Let \(S, T, U\) be types (Definition 6.5.1). If \(\blacktriangleright S <: U\) and \(\blacktriangleright U <: T\) then \(\blacktriangleright S <: T\).
Proof
By induction on the sum of the sizes of the two derivations \(D_1\) of \(\blacktriangleright S <: U\) and \(D_2\) of \(\blacktriangleright U <: T\), with a case on their last rules.
- If \(D_2\) is SA-Top, then \(T = \top\) and SA-Top concludes. If \(D_1\) is SA-Bot, then \(S = \bot\) and SA-Bot concludes.
- Otherwise \(T \neq \top\) and \(S \neq \bot\). Then \(U \neq \top\): the only rule concluding \(\blacktriangleright \top <: T\) is SA-Top, which needs \(T = \top\). And \(U \neq \bot\): the only rule concluding \(\blacktriangleright S <: \bot\) is SA-Bot, which needs \(S = \bot\). So \(U\) is a base type, an arrow or a record, and both derivations end with the structural rule for \(U\)'s shape; \(S\) and \(T\) have that shape too.
- SA-Base twice: \(S = U = T\), and SA-Base concludes.
- SA-Arrow twice: \(S = S_1 \to S_2\), \(U = U_1 \to U_2\), \(T = T_1 \to T_2\) with subderivations \(\blacktriangleright U_1 <: S_1\), \(\blacktriangleright S_2 <: U_2\), \(\blacktriangleright T_1 <: U_1\) and \(\blacktriangleright U_2 <: T_2\). The induction hypothesis on the pairs \((T_1 <: U_1, U_1 <: S_1)\) and \((S_2 <: U_2, U_2 <: T_2)\) gives \(\blacktriangleright T_1 <: S_1\) and \(\blacktriangleright S_2 <: T_2\), and SA-Arrow concludes.
- SA-Rcd twice: \(T\)'s labels are among \(U\)'s, which are among \(S\)'s. For each label \(l\) of \(T\), \(\blacktriangleright S.l <: U.l\) and \(\blacktriangleright U.l <: T.l\) are subderivations, and the hypothesis gives \(\blacktriangleright S.l <: T.l\); SA-Rcd concludes.
Theorem 6.5.12 (The algorithm terminates)
Let \(S, T\) be types and write \(\lvert \tau \rvert\) for the number of nodes of a type \(\tau\). Algorithm 6.5.4 terminates on the pair \((S, T)\) after at most \(\min(\lvert S \rvert, \lvert T \rvert)\) calls.
Proof
Each recursive call is on a pair \((S', T')\) of immediate subterms, one of \(S\) and one of \(T\), in either order (parameters swap), so \(\lvert S' \rvert + \lvert T' \rvert < \lvert S \rvert + \lvert T \rvert\): the recursion is well founded. The recursion descends both types in lockstep: an arrow's parameter is paired with the other arrow's parameter and its result with the other's result, and a field of \(T\) with the field of \(S\) that has the same label. So each call corresponds to one path from the root of \(T\), and different calls to different paths (labels are distinct); the same holds for \(S\). Hence the calls are at most the nodes of \(T\) and at most the nodes of \(S\).
Theorem 6.5.13 (Soundness and completeness of algorithmic subtyping)
For all types \(S, T\) (Definition 6.5.1): \(\blacktriangleright S <: T\) (Definition 6.5.3) iff \(S <: T\) (Definition 6.5.2).
Proof
Soundness (⇒), by induction on the algorithmic derivation: SA-Top, SA-Bot are S-Top, S-Bot; SA-Base is S-Refl; SA-Arrow is S-Arrow applied to the hypotheses; SA-Rcd is S-RcdDepth (on the fields of \(T\), using the hypotheses) composed by S-Trans with S-RcdWidth (dropping \(S\)'s extra fields), with permutation built in. Completeness (⇐), by induction on the declarative derivation: S-Refl by Lemma 6.5.10; S-Trans by the hypotheses and Lemma 6.5.11; S-Top, S-Bot by SA-Top, SA-Bot; S-Arrow by SA-Arrow on the hypotheses; S-RcdWidth by SA-Rcd with Lemma 6.5.10 on each kept field; S-RcdDepth by SA-Rcd on the hypotheses. [TAPL, §16.1] gives the same proof for its system.
The lab's L4_AgreesWithTheDeclarativeRulesOnAllSmallTypes test checks Theorem 6.5.13 by brute force: it computes the declarative relation as the least fixed point of Definition 6.5.2's rules over the 2 705 types of at most 5 nodes (labels a, b), and compares it with the learner's isSubtype on all 180 625 pairs of types of at most 4 nodes (10 061 of which are subtypes); the Python oracle of the drill passes the same test with generic types added.
What breaks the argument. With a subsumption rule in typing, Theorem 6.5.13 is not enough: the typing rules themselves must be made algorithmic (subsumption only at the mode switch, Lesson 6.4, or at applications, [TAPL, §16.2]). With recursive types, the induction in Theorem 6.5.12 fails (types are infinite), and the algorithm needs a cache of assumed pairs (Amadio–Cardelli).
Variance of generic types¶
Lemma 6.5.14 (Polarity composition)
Let \(X\) be a type variable, \(\tau[X]\) a type in which \(X\) occurs exactly once, at polarity \(p \in \{+, -\}\) (Definition 6.5.5), and for a type \(U\) write \(\tau[U]\) for \(\tau[X]\) with \(U\) substituted for \(X\). Assume that subtyping between applications of a constructor \(D\) with a declared variance follows that variance: for all types \(S, T\), \(D[S] <: D[T]\) iff \(S <: T\) when \(D\) is declared covariant, iff \(T <: S\) when contravariant, and iff \(S = T\) when invariant. Then for all types \(S, T\) with \(S <: T\): \(\tau[S] <: \tau[T]\) if \(p = +\), and \(\tau[T] <: \tau[S]\) if \(p = -\).
Proof
By induction on the position of the occurrence in \(\tau[X]\), with the direction of the claim determined by the polarity at each step. If \(\tau[X] = X\), then \(p = +\) and the claim is the hypothesis \(S <: T\). If \(\tau[X] = \tau_1 \to \tau_2[X]\) with the occurrence in the result, the induction hypothesis on \(\tau_2\) gives the claim for \(\tau_2\) in the direction of \(p\), and S-Arrow (Definition 6.5.2), covariant in the result, preserves the direction. If \(\tau[X] = \tau_1[X] \to \tau_2\) with the occurrence in the parameter, the polarity of the occurrence in \(\tau_1\) is the flip of \(p\) (Definition 6.5.5); the induction hypothesis gives the claim for \(\tau_1\) in the flipped direction, and S-Arrow, contravariant in the parameter, flips it back to the direction of \(p\). If \(\tau[X] = D[\tau'[X]]\) with \(D\) declared covariant or contravariant, the induction hypothesis gives the claim for \(\tau'\), and the assumed rule for \(D\) preserves or flips the direction, matching Definition 6.5.5. A \(D\) declared invariant cannot occur on the path, since then the occurrence would have both polarities, not one \(p \in \{+, -\}\).
Theorem 6.5.15 (Checked declaration-site variance is safe)
Let \(C\) be a generic class with one type parameter \(X\) and declared variance \(+\) (class C[+X]), whose members are methods, each with parameter types \(P_1[X], \dots, P_k[X]\) and with a result type \(R[X]\), and fields, each read-only or mutable and with a type \(F[X]\); assume the subtyping rules of Lemma 6.5.14 for the constructors these types mention. If Algorithm 6.5.7 reports nothing for \(C\), then for all types \(S, T\) with \(S <: T\) and every value \(c\) of type \(C[S]\), every member access on \(c\) that is well typed when \(c\) is typed at \(C[T]\) is also well typed with \(c\) at \(C[S]\), with a result type that is a subtype of the result type at \(C[T]\): using \(c\) as a \(C[T]\) is safe. Dually for declared variance \(-\).
Proof
A member access at \(C[T]\) either calls a method with arguments of types \(P_i[T]\) and result \(R[T]\), or reads a field of type \(F[T]\). Algorithm 6.5.7 guarantees that \(X\) occurs in \(P_i\) only at polarity \(-\) and in \(R\) and \(F\) only at \(+\) (counting nested occurrences with the multiplication of Definition 6.5.5). By Lemma 6.5.14 with \(S <: T\): \(P_i[T] <: P_i[S]\) (arguments acceptable at \(T\) are acceptable at \(S\)), and \(R[S] <: R[T]\), \(F[S] <: F[T]\) (results obtained at \(S\) can be used at \(T\)). So the access type-checks against \(c\)'s real type \(C[S]\) and, by subsumption, returns something usable at \(T\). A mutable field would put \(X\) at both polarities and be rejected, which is the covariant-array problem of Proposition 6.2.11 caught statically.
Nominal vs structural subtyping¶
Proposition 6.5.16 (Nominal subtyping is decided by Algorithm 6.5.9 and implies structural compatibility)
Let \(\mathit{CT}\) be a class table over class names \(\mathcal{C}\) (Definition 6.5.8) and \(C, D \in \mathcal{C}\). Algorithm 6.5.9 returns true on \((C, D)\) iff \(C <:_{N} D\). If every class of \(\mathit{CT}\) inherits every field and method of its declared supertypes (with the same or, for method results, more specific types), then \(C <:_{N} D\) implies that the record of \(C\)'s member signatures is a structural subtype of the record of \(D\)'s; the converse fails.
Proof
Algorithm: by acyclicity the chain \(C, \mathrm{super}(C), \dots\) reaches Object in finitely many steps, and the loop visits exactly the classes \(c\) with \(C <:_{N} c\) along the single-inheritance chain (induction on the number of steps; interfaces add a breadth-first search over a finite DAG). Implication: by induction on the length of the extends chain: each step keeps every inherited member (width) with the same or covariantly refined types (depth), so the member record of \(C\) is a structural subtype of \(D\)'s by Theorem 6.5.13. Converse fails: Meters and Seconds of §3 have equal member signatures but no extends edge.
5. Complexity¶
Write \(\lvert S \rvert\) and \(\lvert T \rvert\) for the numbers of nodes of the two types compared, \(U\) for the number of types in the brute-force universe and \(h\) for the depth of the class hierarchy.
| Technique | Time (worst) | Time (typical) | Space | Variables |
|---|---|---|---|---|
| Algorithmic subtyping (Algorithm 6.5.4) | \(\min(\lvert S \rvert, \lvert T \rvert)\) calls; \(O(\lvert S \rvert \cdot \lvert T \rvert)\) time with list-searched labels, \(O(\lvert S \rvert + \lvert T \rvert)\) with hashed labels | small: types are shallow | \(O(\text{depth})\) | \(\lvert S \rvert, \lvert T \rvert\) type sizes (nodes) |
| Declarative search (with S-Trans) | not an algorithm: unboundedly many intermediates; bounded brute force \(\Theta(U^3)\) per fixed-point round | 60 s in Python, 4 s in C++ for \(U = 2\,705\) | \(\Theta(U^2)\) | \(U\) types in the universe |
| Variance checking (Algorithm 6.5.7) | \(O(\lvert \text{declaration} \rvert)\) | linear | \(O(\text{depth})\) | — |
| Nominal test (Algorithm 6.5.9) | \(O(h)\) per query | \(O(1)\) with a display (HotSpot) | \(O(1)\) | \(h\) hierarchy depth |
Justification. Theorem 6.5.12 bounds the calls of Algorithm 6.5.4; each SA-Rcd step looks up \(T\)'s labels in \(S\), \(O(\lvert S \rvert)\) per label by scanning (so \(O(\lvert S \rvert \cdot \lvert T \rvert)\) over the run) or \(O(1)\) by hashing. The declarative brute force (the lab and drill tests) iterates one-step rules and a Warshall closure, \(\Theta(U^3)\) per round, until no pair is added. Algorithm 6.5.7 walks each member type once. Algorithm 6.5.9 walks at most \(h\) superclasses; HotSpot answers most subtype checks in constant time by storing each class's primary superclasses at fixed depths (a display) and checking one slot [CR02].
Pathological families. Records of records: for \(k \ge 1\) and for labels \(a, b\), the type \(S_k = \{a{:}S_{k-1}, b{:}S_{k-1}\}\) (with \(S_0 = \mathsf{int}\)) has size \(\Theta(2^k)\), and checking \(S_k <: S_k\) visits all of it — linear in the (exponential) size; with hash-consing, a cache of already-proven pairs makes it \(O(k)\). For nominal subtyping with variance, the combination of variance, generics and "expansive" inheritance makes subtyping undecidable: Java's wildcards can encode Turing machines [Gri17].
Scale. TypeScript relates large structural types constantly (every object literal, every union); its checker caches relation results per pair of type ids in isTypeRelatedTo [TSC-Checker] for exactly the reason above.
6. Variants and refinements¶
Structural subtyping and algorithmic subtyping¶
- Subtyping vs coercion [BTCGS91]: interpret \(S <: T\) as a coercion function (dropping fields = building a smaller record) instead of the identity. Coercive subtyping allows
int <: floatwith a real conversion, at the price of a coherence proof (Lesson 6.3, Definition 6.3.5) and run-time copies; inclusive (identity) subtyping needs records whose layout supports width subtyping (offset tables, dictionaries). - Joins and meets [TAPL, §16.3]: the type of
if c then e1 else e2is the least upper bound of the branch types; records have joins (common fields, joined) and arrows have joins using meets of parameters, requiring a meet that may not exist without \(\bot\). - Recursive and equi-recursive types (Amadio–Cardelli 1993): subtyping by coinduction with an assumption set; the algorithm is quadratic with memoization.
- Excess-property checks (TypeScript): a fresh object literal is checked without width subtyping, catching misspelled optional fields — a deliberate exception shown in the box.
Variance of generic types¶
- Variance inference (Rust [RUSTC-Variance], TypeScript's variance measurement for type aliases): compute each parameter's variance from its uses by a fixed point over the declarations; no annotations, less control.
- Use-site variance with capture (Java):
List<? extends Number>is opened toList<CAP#1>with a freshCAP#1 <: Numberat each use; flexible for library users, notoriously hard error messages (the box'sCAP#1). - Method parameter bivariance (TypeScript's method syntax, Eiffel's covariant parameters): unsound on purpose for ergonomics;
strictFunctionTypesrestores contravariance for function-typed properties only.
Nominal vs structural subtyping¶
- Branding / newtypes (TypeScript intersections with a unique tag, Haskell and Rust newtypes): nominal distinctions inside a structural system, at zero run-time cost.
- Structural interfaces over nominal classes (Go interfaces, Scala structural types, TypeScript's classes compared structurally): values are nominally typed, interfaces satisfied by shape; Go checks satisfaction statically and builds method tables (itabs) lazily at run time.
- Nominal with declared variance and wildcards (Java, C#): decidability depends on restricting inheritance patterns [Gri17].
7. In real compilers¶
Structural subtyping and algorithmic subtyping¶
TypeScript's isTypeRelatedTo and structuredTypeRelatedTo in src/compiler/checker.ts [TSC-Checker] implement Algorithm 6.5.4 for object types, with the strictFunctionTypes switch deciding whether function-typed properties are compared contravariantly or bivariantly.
Width, depth, contravariance, top and bottom in tsc 5.9
Reproduce (tsc 5.9.3 via npx):
mkdir -p ts && cat > ts/structural.ts <<'EOF'
type Point = { x: number };
type Point3 = { x: number; y: number; z: number };
const p3: Point3 = { x: 1, y: 2, z: 3 };
const p: Point = p3; // width: extra fields are fine
const q: Point = { x: 1, y: 2 }; // excess-property check on a fresh literal
type Deep = { pos: { x: number } };
const d: Deep = { pos: p3 }; // depth
let takesPoint = (a: Point) => a.x;
let takesPoint3 = (a: Point3) => a.z;
takesPoint3 = takesPoint; // parameter contravariance: accepted
takesPoint = takesPoint3; // rejected with strictFunctionTypes
interface Handler { handle(a: Point): number } // method syntax: bivariant parameters
const h: Handler = { handle: (a: Point3) => a.z }; // accepted even under --strict
const anything: unknown = p3; // unknown is the top type
function fail(): never { throw new Error("no"); } // never is the bottom type
const n: number = fail();
EOF
npx -y -p typescript@5.9 tsc --strict --noEmit ts/structural.ts
Output (complete):
ts/structural.ts(5,26): error TS2353: Object literal may only specify known properties, and 'y' does not exist in type 'Point'.
ts/structural.ts(11,1): error TS2322: Type '(a: Point3) => number' is not assignable to type '(a: Point) => number'.
Types of parameters 'a' and 'a' are incompatible.
Type 'Point' is missing the following properties from type 'Point3': y, z
What to notice: lines 4 and 7 are width and depth (SA-Rcd). Line 10 is SA-Arrow with the parameters swapped (Point3 <: Point), and line 11 fails on exactly that swapped premise ("Types of parameters 'a' and 'a' are incompatible"). Line 13 is accepted although it is the same shape as line 11: method parameters are bivariant, an intentional unsoundness (Lesson 6.2). Lines 14 and 16 use unknown as \(\top\) and never as \(\bot\) (S-Top, S-Bot). Line 5 is not a subtyping failure but TypeScript's excess-property check on a fresh literal (§6).
Variance of generic types¶
javac decides wildcard containment in Types.containsType [JAVAC-Types]; rustc infers variance in variances_of [RUSTC-Variance]; Kotlin checks declaration-site in/out annotations with the polarity rules of Algorithm 6.5.7, reporting a covariant type parameter in an in position [KOTLIN-Generics] (Kotlin's compiler is not runnable in this container).
Use-site variance in javac 21: invariance, ? extends and ? super
Reproduce (javac 21.0.10):
mkdir -p java && cat > java/Variance.java <<'EOF'
import java.util.ArrayList;
import java.util.List;
class Variance {
static double sum(List<? extends Number> xs) { // use-site covariance: read only
double s = 0;
for (Number x : xs) s += x.doubleValue();
return s;
}
static void fill(List<? super Integer> out) { // use-site contravariance: write only
out.add(42);
}
static void run() {
List<Integer> ints = new ArrayList<>();
List<Number> nums = ints; // generics are invariant
List<? extends Number> ro = ints; // fine
ro.add(1); // cannot add to ? extends
fill(new ArrayList<Object>()); // Object is a supertype of Integer
double s = sum(ints);
}
}
EOF
javac -d java java/Variance.java
Output (complete; the container's Picked up JAVA_TOOL_OPTIONS banner removed):
java/Variance.java:15: error: incompatible types: List<Integer> cannot be converted to List<Number>
List<Number> nums = ints; // generics are invariant
^
java/Variance.java:17: error: incompatible types: int cannot be converted to CAP#1
ro.add(1); // cannot add to ? extends
^
where CAP#1 is a fresh type-variable:
CAP#1 extends Number from capture of ? extends Number
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
2 errors
What to notice: List is invariant (line 15): List<Integer> <: List<Number> would allow the unsound store of Proposition 6.2.11, and erased generics cannot check it at run time (Lesson 6.9). ? extends Number is the covariant view: reading gives Number (line 7) but adding fails, because the element type is an unknown CAP#1 <: Number — the set(v: X) error of §3's variance check, found at the use instead of the declaration. ? super Integer is the contravariant view that accepts Integers (line 11) and an ArrayList<Object> (line 18).
Nominal vs structural subtyping¶
javac's Types.isSubtype walks declared supertypes (Algorithm 6.5.9 plus generics); the JVM repeats the test at run time for casts and array stores with HotSpot's fast display-based check [CR02]. TypeScript compares classes structurally, with private members as the one nominal exception.
Same shape, different names: javac 21 vs tsc 5.9, and branding
Reproduce (javac 21.0.10, tsc 5.9.3 via npx):
mkdir -p java ts && cat > java/Nominal.java <<'EOF'
class Nominal {
record Meters(double value) {}
record Seconds(double value) {}
static double speed(Meters m, Seconds s) { return m.value() / s.value(); }
static void run() {
Meters m = new Meters(100);
Seconds s = new Seconds(9.58);
speed(s, m); // same shape, different names: rejected
}
}
EOF
javac -d java java/Nominal.java
cat > ts/nominal.ts <<'EOF'
type Meters = { value: number };
type Seconds = { value: number };
function speed(m: Meters, s: Seconds): number { return m.value / s.value; }
const m: Meters = { value: 100 };
const s: Seconds = { value: 9.58 };
console.log(speed(s, m)); // same shape: accepted, and wrong
type Brand<T, B extends string> = T & { readonly __brand: B };
type SafeMeters = Brand<number, "m">;
type SafeSeconds = Brand<number, "s">;
function safeSpeed(m: SafeMeters, s: SafeSeconds): number { return m / s; }
safeSpeed(9.58 as SafeSeconds, 100 as SafeMeters); // branding simulates nominal types
EOF
npx -y -p typescript@5.9 tsc --strict --noEmit ts/nominal.ts
Output (complete; the container's Picked up JAVA_TOOL_OPTIONS banner removed):
java/Nominal.java:8: error: incompatible types: Seconds cannot be converted to Meters
speed(s, m); // same shape, different names: rejected
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
ts/nominal.ts(11,11): error TS2345: Argument of type 'SafeSeconds' is not assignable to parameter of type 'SafeMeters'.
Type 'SafeSeconds' is not assignable to type '{ readonly __brand: "m"; }'.
Types of property '__brand' are incompatible.
Type '"s"' is not assignable to type '"m"'.
What to notice: Java's records are nominal (Algorithm 6.5.9 finds no path from Seconds to Meters); TypeScript accepts the swapped call on line 6 because the shapes are equal (SA-Rcd), and rejects the branded version only because the phantom __brand fields now differ — a structural check that simulates a nominal one.
8. Comparison¶
| Technique | Power / precision | Speed (asymptotic · practical) | Output / error quality | Implementation effort | Typical use |
|---|---|---|---|---|---|
| Structural subtyping (algorithmic) | Width, depth, permutation, contravariant parameters, top and bottom; equals the declarative rules (Theorem 6.5.13) | \(O(\lvert S \rvert + \lvert T \rvert)\) with hashed labels · cached per type pair in practice | Explains the failing path ("types of parameters … are incompatible") | Low for the algorithm; joins/meets and recursion add effort | TypeScript, OCaml objects, Go interfaces, Scala structural types, the lab's L4 |
| Variance of generics | Covariance for producers, contravariance for consumers, invariance for mutable containers (Theorem 6.5.15) | Linear declaration check; wildcard subtyping undecidable in general [Gri17] | Declaration-site: errors at the declaration; use-site: at each use, with capture variables | Medium (declaration-site), high (use-site with capture) | Scala, Kotlin, C# (declaration-site); Java (use-site); Rust (inferred) |
| Nominal subtyping | Only declared relations; abstraction boundaries enforced (Proposition 6.5.16) | \(O(h)\), \(O(1)\) with displays at run time | "X cannot be converted to Y" names both types | Low | Java, C#, Swift, C++, Kotlin, Rust traits (nominal impls) |
| Structural typing (whole types) | Any matching shape is accepted; unrelated code interoperates | As algorithmic subtyping | Accepts accidental matches (meters for seconds) | Low; branding for nominal needs | TypeScript, Go interfaces, OCaml, Elm records |
Choose structural subtyping where independently written code must interoperate by shape (JavaScript objects, configuration records, interfaces); choose nominal where types carry meaning beyond their shape (units, invariants, abstraction). Choose declaration-site variance when library authors can annotate their types once (Kotlin, Scala, C#); use-site variance only when retrofitting generics onto an invariant language, as Java did.
9. Assessment¶
| Technique | Quiz ids | Drill | Flashcard tag | Exercises |
|---|---|---|---|---|
| Structural subtyping (algorithmic) | subtype-queries, transitivity-elimination |
subtype-query (easy, medium) |
subtyping |
Lab L4 ★ |
| Variance of generics | variance-positions, java-wildcards |
subtype-query (hard: List, Sink, Cell) |
variance |
— |
| Nominal vs structural | nominal-structural, hotspot-display |
subtype-query (records are structural; the drill states it) |
nominal-structural |
— (Pebble's structs are nominal: E3 tests P vs Q with equal fields implicitly through E0401) |
References¶
See the chapter references.