Lectures onType Theory
ch:subtyping: ch:subtyping
appendix sectionsolutions

ch:subtyping: ch:subtyping

exercise 8.1.

Put RN:={x:Nat},RT:={x:Top},RC:={x:Nat,color:Bool}. There are six successful ordered pairs. Their complete rule trees are {x}{x}XNat<:NatSReflRN<:RNSRcd. {x}{x}XNat<:TopSTopRN<:RTSRcd. {x}{x}XTop<:TopSReflRT<:RTSRcd. {x}{x,color}XNat<:NatSReflRC<:RNSRcd. The remaining two are {x}{x,color}XNat<:TopSTopRC<:RTSRcd. {x,color}{x,color}XNat<:NatSReflXBool<:BoolSReflRC<:RCSRcd. For RN<:RC and RT<:RC, the target label set {x,color} is not contained in the source label set {x}. Thus the first premise of S-Rcd fails. For RT<:RN, label inclusion holds, but the field premise would be Top<:Nat. Subtype-shape inversion excludes that judgment. These are the remaining three ordered pairs.

Exercise 8.2.

The first judgment holds. Its complete structural derivation is XNat<:TopSTopXNat<:TopSTopTopNat<:NatTopSArr. The first premise is reversed: the target domain Nat is below the source domain Top.

The second judgment would require Top<:Nat both for its reversed domain premise and for its covariant codomain premise. Subtype-shape inversion excludes that judgment. Operationally, an erroneous covariant use would permit g:=λn:Nat.unit:NatTop to be subsumed to TopNat. Then gunit would be assigned type Nat but would reduce to unit, which is not a natural. This application is the preservation counterexample ruled out by domain contravariance.

exercise 8.3.

Recall c={x=0,color=true},ColorPoint={x:Nat,color:Bool},Point={x:Nat}. The typing of the redex, including width subtyping and subsumption, is X0:NatTZeroXtrue:BoolTruec:ColorPointTRcd{x}{x,color}XNat<:NatSReflColorPoint<:PointSRcdc:PointTSubxdom(Point)c.x:NatTProj. Operationally, E-Proj gives c.x0. The reduct has the required type by the leaf X0:NatTZero. Thus the field selected by reduction has exactly the type retained by the subsumed record interface.

Immutability is essential for the depth part of the argument. If writable fields were covariant, an alias of type {q:Nat} could also be used at {q:Top}. A write of true:Bool<:Top through the second alias would leave the first alias reading a Boolean where its type promises a natural. No such write exists for the immutable records of this chapter.

Exercise 8.4.

Prove simultaneously for every closed value form that a derivation v:A cannot have A=Bot, by induction on that typing derivation. A final introduction rule fixes both the outer syntax and a nonbottom result type: vintroduction resultλx:C.tCD{i=vi}iI{i:Ai}iIv1,v2A1×A2inl(v),inr(v)A1+A2unitUnittrue,falseBool0,suc(v)Nat. No one of these outer constructors is Bot. A closed value cannot be a variable, and no elimination rule concludes the typing of one of the listed value syntaxes.

It remains to treat a derivation ending in subsumption: v:CC<:Av:ATSub. If A=Bot, clause Bot-Down of lemma 8.5 forces C=Bot. The first premise is a strictly smaller typing derivation of the same closed value at Bot, contradicting the induction hypothesis. This also explains why all value forms are handled simultaneously: a chain of final subsumptions is peeled until an introduction rule is reached, and bottom cannot appear at either stage.

Exercise 8.5.

Let R={x:Nat,q:Bool},S={q:Bool,r:Unit}. Their common label set is {q} and their union of labels is {x,q,r}. Since the common field types are identical, RS={q:Bool},RS={x:Nat,q:Bool,r:Unit}. The four bound judgments are the following instances of S-Rcd: R<:{q:Bool},S<:{q:Bool},RS<:R,RS<:S. For the first row, the singleton target label set is contained in the corresponding source set; for the second, the target label set is contained in {x,q,r}. Every retained field premise is K<:K for K{Nat,Bool,Unit} and follows by S-Refl. These inclusions and reflexive field trees are all the premises of the four S-Rcd derivations.

Now replace S by S={q:Top,r:Unit}. The recursive definition computes the common field bounds BoolTop=Top,BoolTop=Bool. Hence RS={q:Top},RS={x:Nat,q:Bool,r:Unit}. For example, the q premises of the upper-bound trees are Bool<:Top and Top<:Top, while those of the lower-bound trees are Bool<:Bool and Bool<:Top. The identical-field abbreviation does not apply because its hypothesis asks for literally equal common field types, whereas BoolTop; the global recursive bound calculation is what supplies their join and meet.

exercise 8.6.

Define Colorable:={getColor:UnitBool} and rememberColor:=ΛX<:Colorable.λo:X.o,o.getColor unit. In the context Ω=X<:Colorable,o:X, the observation has the complete derivation (o:X)ΩΩo:XTVarΩ(X)=ColorableΩX<:ColorableSVarΩo:ColorableTSubgetColordom(Colorable)Ωo.getColor:UnitBoolTProjXΩunit:UnitUnitIΩo.getColor unit:BoolTApp. Call the displayed observation derivation Dobs. For the next tree abbreviate bX(o)=o,o.getColor unit. Pair the observation with the original o:X, then introduce the term and type binders: (o:X)ΩΩo:XTVarDobsΩbX(o):X×BoolTPairX<:Colorableλo:X.bX(o):XX×BoolTLamrememberColor:X<:Colorable.XX×BoolTTAbs.

Take the two-method record type and value from the chapter: C:=ColorPointObj,colored:C. The bound premise for instantiation is the width derivation {getColor}{getX,getColor}XUnitBool<:UnitBoolSReflC<:ColorableSRcd. Hence the instantiation and application rules give rememberColor:X<:Colorable.XX×BoolC<:ColorablerememberColor[C]:CC×BoolTTAppcolored:CrememberColor[C] colored:C×BoolTApp. The requested reduction is rememberColor[C] colored(λo:C.o,o.getColor unit) coloredcolored,colored.getColor unitcolored,(λu:Unit.true) unitcolored,true. The first component remains the original two-method record at type C.

Exercise 8.7.

The annotated body is still o,o.getX unit but its local context is now ΩTop=X<:Top,o:X. The variable rule gives ΩTopo:X. To use T-Proj at getX, the derivation in the original term first subsumed o to PointObj={getX:UnitNat}. That step would now require the precise judgment (1)ΩTopX<:PointObj. An induction on a derivation with source X proves the needed variable-source inversion: if Γ(X)=Top and ΓX<:C, then C is X or Top. The S-Var case uses the declared bound; the S-Trans case applies the induction hypothesis to its left premise and then lemma 18.6 to its right premise. Structural rules have nonvariable sources. Since PointObj is neither X nor Top, (1) is the first underivable judgment. Equivalently, after theorem 8.26, the algorithm promotes X to Top and has no successful clause for a record target. Without (1), the receiver cannot be typed at any record interface containing getX, so the projection and the claimed universal typing cannot be rebuilt.

exercise 8.8, exercise 8.9.

Assume ΓP<:Q and begin with a judgment under Γ,X<:Q,Δ. The S-Var case of type substitution has exactly three positions for the looked-up declaration.

If the variable is X, its old conclusion is X<:Q. The bound Q was formed before X, so Q[P/X]=Q. After removing X and transforming the suffix, weaken the premise through that suffix: ΓP<:QΓ,Δ[P/X] ctxΓ,Δ[P/X]P<:Qweakening. This is precisely the substituted instance of X<:Q.

If the looked-up variable Y occurs before X, then Y<:R is a declaration of Γ, and R cannot mention the later X. Lookup is unchanged: (Γ,Δ[P/X])(Y)=RΓ,Δ[P/X]Y<:RSVar. Finally, if Δ=Δ0,Y<:R,Δ1, the transformed context is Γ,Δ0[P/X],Y<:R[P/X],Δ1[P/X]. The corresponding lookup tree is (Γ,Δ[P/X])(Y)=R[P/X]Γ,Δ[P/X]Y<:R[P/X]SVar. These are all possible positions in the ordered context.

For narrowing, suppose the last typing rule is Γ,X<:Q,Δt:Y<:A.BΓ,X<:Q,ΔC<:AΓ,X<:Q,Δt[C]:B[C/Y]TTApp. The simultaneous narrowing induction hypotheses give both premises in the same strengthened context: Γ,X<:P,Δt:Y<:A.B,Γ,X<:P,ΔC<:A. Rebuilding the rule displays both required uses of the induction hypothesis: Γ,X<:P,Δt:Y<:A.BΓ,X<:P,ΔC<:AΓ,X<:P,Δt[C]:B[C/Y]TTApp. The term and its result type are unchanged; only the declaration X<:Q has been narrowed to X<:P.

exercise 8.10.

Let Γ=X<:R,R:={x:Nat,q:Bool},P:={x:Top}. The positive query takes source promotion, record comparison, and the top branch in that order. Including every priority guard, its tree is XαPPαTopΓ(X)=RRαP{x}{x,q}NatαTopΓaNat<:TopATopΓaR<:PARcdΓaX<:PAVar. The complete recursive-query list is therefore X<:P,R<:P,Nat<:Top. Since wΓ(R)=3,wΓ(X)=1+wΓ(R)=4,wΓ(P)=2, the sole promotion lowers the pair measure from 4+2=6 to 3+2=5. The record call then lowers it to 1+1=2.

For the reversed query P<:X, equality fails, the target is not top, the source is not bottom or a variable, and the outer constructors are a record and a variable. Thus its complete guard trace is PαX,XαTop,PαBot,P is not a variable,(P,X) matches no common structural form}failure. There is no recursive query, no source promotion, and no algorithmic derivation tree for ΓaP<:X.

Exercise 8.11.

Suppose the two nontrivial record derivations have conclusions Γa{i:Ai}iI<:{j:Bj}jJ,Γa{j:Bj}jJ<:{k:Ck}kK. They supply JI and KJ. Fix a target label K. The three field types named by the two derivations are Ain the first source,Bin the middle record,Cin the final target. Their field premises are ΓaA<:B,ΓaB<:C. Because B is a proper component of the middle record, its weight is strictly smaller than the middle-record weight. The lexicographic transitivity induction therefore applies exactly here and gives ΓaA<:C. Doing this for every K, and composing label inclusions to obtain KI, supplies all premises of A-Rcd for the desired source and target records. If those records are alpha-identical, priority chooses A-Eq; otherwise the record nonidentity guard holds and A-Rcd rebuilds the conclusion.

Exercise 8.12.

Define sub(Γ,A,B) by the following ordered cases. A recursive call returning failure makes the current case return failure; otherwise the named constructor is returned with the recursive trees as premises.

  1. If AαB, return an A-Eq leaf.

  2. If B=Top, return an A-Top leaf.

  3. If A=Bot, return an A-Bot leaf.

  4. If A=X, fail when lookup is undefined; otherwise, with Γ(X)=U, recursively compute D=sub(Γ,U,B) and return AVar(D).

  5. For two arrows, recursively compute the domain tree for B1<:A1 and the codomain tree for A2<:B2, then return A-Arr of them.

  6. For two products or two sums, recursively compare corresponding components in source-to-target order and return A-Prod or A-Sum.

  7. For two records, first fail unless every target label occurs in the source. Recursively compare Aj<:Bj for each target label j and return the finite A-Rcd tree.

  8. For two bounded universals, first fail unless the bounds are alpha-identical. Rename the binders to a common fresh X, recursively compare the bodies under Γ,X<:A, and return A-AllK.

  9. For every other pair of outer forms, return failure.

The order is part of the definition, so reaching each case proves the side-condition that every earlier test failed. In particular the returned A-Top, A-Bot, A-Var, and structural nodes carry exactly the guards printed in the rule table.

Prove soundness of every returned tree by induction on m=wΓ(A)+wΓ(B). The first three branches return leaves whose conclusions and guards check directly. Promotion replaces wΓ(X)=1+wΓ(U) by wΓ(U), so the induction hypothesis says the recursive result is a valid tree for U<:B; adjoining the successful lookup and guards makes a valid A-Var node. Every arrow, product, and sum call is on proper components and hence has smaller measure. Record calls are likewise on field components and there are finitely many of them; the prior label test supplies JI. In the universal branch, the body is the proper summand measured under the same extended context used by the recursive call, so its measure is smaller; the prior equality test supplies the invariant bound required by A-AllK. The induction hypotheses validate all child trees, and the corresponding rule validates the returned parent. Failure returns no tree and imposes no soundness obligation. Thus every possible successful return is a finite derivation checking against the displayed algorithmic rules.

exercise 8.13, exercise 8.15.

For the growing-context trace, put Γn:=X0<:Θ,X1<:X0,,Xn<:Xn1(n0),Tk:=Xk<:Xk1.¬Xk(k1). The chapter’s displayed iteration leaves Γ1X1<:T2. Source promotion reaches the next repeated query: Γ1X1<:T2Γ1X0<:T2Γ1Θ<:T2. The first requested continuation has both S-AllF premises visible at both nested uses: XΓ1X1<:TopSTopΓ2X2<:T3XΓ2,Z2<:X2Z2<:Z2SReflΓ2¬T3<:¬X2SAllFΓ1Θ<:T2SAllF. Its only open recursive premise promotes through every earlier declaration: Γ2X2<:T3Γ2X1<:T3Γ2X0<:T3Γ2Θ<:T3. The second continuation is therefore the following distinct tree: XΓ2X2<:TopSTopΓ3X3<:T4XΓ3,Z3<:X3Z3<:Z3SReflΓ3¬T4<:¬X3SAllFΓ2Θ<:T3SAllF. The outer rule compares the target bound Xi with the unbounded source bound Top; the inner rule reverses the bounds of the two negative abbreviations. The two continuations extend the context from Γ1 to Γ2 and then to Γ3. Each target quantifier supplies a fresh alpha-renamed declaration, so neither enlarged ordered context is textually equal to an earlier one.

Now let ΓS<:T be a closed full-F<: statement. The direct typing reduction is (f:TTop)(f:TTop,a:S)Γ;f:TTop,a:Sf:TTopTVar(a:S)(f:TTop,a:S)Γ;f:TTop,a:Sa:STVarΓS<:TΓ;f:TTop,a:Sa:TTSubΓ;f:TTop,a:Sfa:TopTAppΓ;f:TTopλa:S.fa:STopTLamΓ;λf:TTop.λa:S.fa:(TTop)STopTLam. The displayed T-Sub is the direct derivation’s single use of the input query.

Conversely, first establish the upward arrow-shape fact required in full F<:. Induct on a derivation of A1A2<:C. The conclusion is that C=Top, or that C=C1C2 with C1<:A1 and A2<:C2. Reflexivity, S-Top, and S-Arr give exactly these alternatives. The source of S-Var is a type variable, and S-AllF has a universal source, so neither can be the final rule. We use in parallel the elementary maximality fact ΓTop<:CC=Top, proved by induction on its derivation; only reflexivity, top, and transitivity can occur.

In the transitivity case, write A1A2<:E<:C. The induction hypothesis for the first premise makes E top or an arrow. If E=Top, maximality applied to the second premise makes C=Top. Otherwise let E=E1E2, with E1<:A1 and A2<:E2. Apply the arrow induction hypothesis to the second premise. Its top alternative again concludes C=Top; its arrow alternative gives C=C1C2,C1<:E1<:A1,A2<:E2<:C2. Compose the two chains. This treats transitivity explicitly and exhausts the full-F<: rules.

Now peel final T-Sub steps until the two syntax-directed T-Lam introductions for the displayed annotated lambdas are exposed. Invert the typing of their body fa. Application inversion supplies some domain D such that f is usable at a function type with domain D and a:D. Inversion of the two variable typings through their final subsumption chains gives TTop<:DE,S<:D for some result type E. Apply the just-proved arrow-shape induction to the first judgment: it gives D<:T (and Top<:E). Therefore ΓS<:DΓD<:TΓS<:TSTrans. The term has no free term variables. Its only possible free type variables are those declared by the supplied closed bound context Γ.

Exercise 8.14.

Assume a total decider D for derivability of closed full-F<: subtype statements. On a two-counter-machine instance M, perform these three effective steps:

  1. construct Pierce’s rowing machine R=R(M);

  2. construct the closed subtype statement J=J(R)=J(R(M));

  3. run D(J) and answer “halts” exactly when the decider answers “derivable.”

Correctness in the accepting direction is the chain D(J)=yesJ is derivableR haltsM halts. Because D is total and correct, a negative answer says that J is not derivable. The reverse directions of the same two biconditionals give D(J)=noJ is not derivableR does not haltM does not halt. Thus D would decide both answers to the undecidable machine-halting problem.

One divergent run of one subtype-search strategy proves only that this particular run or strategy fails to terminate on its input. It does not show that the queried judgment is underivable, and still less that no other total algorithm decides all judgments. The two effective reductions and both directions of their biconditionals are what turn a hypothetical total decider into a machine-halting decider; the growing divergent trace alone is not that reduction.

Search the book

Type to search the local edition.