Lectures onType Theory
ch:linear-types: ch:linear-types
appendix sectionsolutions

ch:linear-types: ch:linear-types

exercise 18.1.

Pairwise disjointness supplies the six facts Δ1#Δ2,Δ1#Θ1,Δ1#Θ2,Δ2#Θ1,Δ2#Θ2,Θ1#Θ2. Thus every intermediate union displayed in the problem is defined. Repeated associativity gives ((Δ1Δ2)Θ1)Θ2=Δ1(Δ2(Θ1Θ2)). Commuting the middle two summands and reassociating yields Δ1(Θ1(Δ2Θ2)). For the second equation, both sides are the finite map whose domain is the disjoint union of the same four domains and whose restriction to each domain is the corresponding context. More explicitly, (Δ1Θ1)(Δ2Θ2)=Δ1Θ1Δ2Θ2=Θ2Δ2Θ1Δ1=(Θ2Δ2)(Θ1Δ1). The first line uses the four cross-disjointness facts between the two parenthesized pairs; the last line uses them again after the permutation.

exercise 18.2.

Under the singleton context f:File, the body of e1 has the derivation ;u:1u:1;f:Filereadf:FileBytes;u:1,f:Filelet =u inreadf:FileBytesTOneE. Hence ;f:Filee1:1(FileBytes). Separately, T-Close, T-LVar, and T-LolliE give ;f:Filee2=closef:1. The rejected rule therefore offers the same declaration f:File to both premises and concludes a type for e1e2. Call by value first evaluates the argument e2, removing the live token, and beta reduction then exposes the read in e1. The duplicated assumption is precisely f:File. Removing an explicit contraction rule does not help: the unsplit application rule itself has copied its entire Δ into two premises.

exercise 18.3.

The body derivation is X;p:ABp:ABTLVarX;y:By:BTLVarX;x:Ax:ATLVar;y:B,x:Ayx:BATTensorI;p:ABlet xy=p in yx:BATTensorE. The two singleton premises of T-TensorI are y:B and x:A; their union is the branch context supplied by tensor elimination. Applying T-LolliI discharges p and concludes ;swap:(AB)(BA).

exercise 18.4.

Suppose first that ;λx.xx:A(AA). Inverting T-LolliI gives ;x:Axx:AA. Inverting T-TensorI would split the singleton context as Δ1Δ2=x:A and type one occurrence of x in each premise. Each variable premise requires its entire linear context to be the singleton x:A, so both Δ1 and Δ2 contain x. They are not disjoint, a contradiction.

For λx., implication inversion would require ;x:A:1. The only introduction of is T-OneI, whose linear premise is . No rule removes the unused declaration x:A. Hence neither displayed judgment is derivable.

exercise 18.5.

Define route=λz.λr.case z of inlxinr(xr)inryinl(yr). In either branch, T-TensorI first gives xr:AR or yr:AR. Use T-PlusI2 in the left branch and T-PlusI1 in the right: ;x:A,r:Rinr(xr):(AR)(AR), ;y:A,r:Rinl(yr):(AR)(AR). With scrutinee context z:AA, T-Case takes r:R as the same residual context in both alternatives. Two implications therefore give ;route:(AA)(R((AR)(AR))). The printed occurrence of r is duplicated between alternatives, but only the selected alternative executes; this is exactly why T-Case shares, rather than splits, its residual context.

exercise 18.7.

Suppose the last rule of the first derivation is Γ;Ξ1e1:CBΓ;Ξ2e2:CΓ;Ξ1Ξ2e1e2:BTLolliE, where Ξ1Ξ2=Δ1,x:A. Exclusive membership puts x in exactly one premise. If Ξ1=Ξ1,x:A, the induction hypothesis and the substituent Γ;Δ2v:A give Γ;Ξ1Δ2e1[v/x]:CB. The second premise is unchanged. Reapplying T-LolliE yields Γ;(Ξ1Δ2)Ξ2e1[v/x]e2:B. Pairwise disjointness and split algebra identify its context by (Ξ1Δ2)Ξ2=(Ξ1Ξ2)Δ2=Δ1Δ2. The term is (e1e2)[v/x]. If xΞ2, the symmetric argument substitutes in the argument premise and uses Ξ1(Ξ2Δ2)=(Ξ1Ξ2)Δ2.

exercise 18.9.

Inversion of a typing derivation for the tensor redex supplies pairwise disjoint contexts and premises Γ;Δvv:P,Γ;Δww:Q,Γ;Δe,x:P,y:Qe:C. The redex is typed under (ΔvΔw)Δe: tensor introduction first types vw, and tensor elimination combines that result with the body premise.

Alpha-rename x,y away from the substituents. Regard the body context as (Δe,y:Q),x:P. Linear substitution with v gives Γ;ΔeΔv,y:Qe[v/x]:C. A second substitution with w gives Γ;(ΔeΔv)Δwe[v/x,w/y]:C. Finally, (ΔeΔv)Δw=(ΔvΔw)Δe by commutativity and associativity of the pairwise-disjoint union. This is the context of the redex, so the tensor root preserves its type.

exercise 18.11.

Rule T-TensorI gives Γ;ΔvΔwvw:PQ. Combining this with the supplied body premise by T-TensorE types the redex under (ΔvΔw)Δe.

For the reduct, first apply linear substitution to x: Γ;Δe,x:P,y:Qe:CΓ;Δvv:PΓ;ΔeΔv,y:Qe[v/x]:C. Then apply it to y: Γ;ΔeΔv,y:Qe[v/x]:CΓ;Δww:QΓ;(ΔeΔv)Δwe[v/x,w/y]:C. After fresh alpha-renaming, the simultaneous substitution is the displayed pair of successive substitutions. The context equation is (ΔeΔv)Δw=(ΔvΔw)Δe=ΔvΔwΔe, so redex and reduct have the required common context and type.

exercise 18.12.

On the left, T-Case uses Δs for its scrutinee and the common residual Δr,p:P,q:Q for both branches. It therefore types the case at C under Δs(Δr,p:P,q:Q). Tensor elimination then combines this body with t:PQ and concludes under ΔtΔsΔr.

On the right, use tensor elimination separately in the alternatives. The left one has premises t:PQ under Δt and e1:C under Δr,p:P,q:Q,x:A, so it concludes under ΔtΔr,x:A. The right alternative similarly concludes under ΔtΔr,y:B. Thus the outer T-Case has scrutinee context Δs and the common residual ΔtΔr. It concludes under Δs(ΔtΔr). The freshness hypotheses prevent either elimination binder from capturing a free variable of s or t, and split commutativity and associativity give Δs(ΔtΔr)=ΔtΔsΔr. Hence both sides have type C under the stated context.

exercise 18.8.

In the first configuration, T-Case places x:A in the scrutinee context. Linear substitution gives Γ;Δ0Θs[v/x]:PQ. The branch premises are unchanged, so rebuilding the case yields Γ;(Δ0Θ)Δrcase s[v/x] of inlpe1inrqe2:C.

In the second configuration, the scrutinee is unchanged. Apply linear substitution to both branch derivations: Γ;ΔrΘ,p:Pe1[v/x]:C,Γ;ΔrΘ,q:Qe2[v/x]:C. Both alternatives therefore have the common residual ΔrΘ, and T-Case gives Γ;Δ0(ΔrΘ)case s of inlpe1[v/x]inrqe2[v/x]:C. If the two residual contexts differed, no single Δr could fill both branch premises of T-Case; the rule would therefore be inapplicable.

exercise 18.6.

For the first term, implication inversion puts one declaration f:File in the body. Tensor inversion would split it between two premises, but inversion of each application readf requires a premise containing f. Both summands would contain the same declaration, contradicting disjointness.

For the second term, implication inversion requires ;f:File:1. Inverting its only possible last rule, T-OneI, requires the empty linear context, so the declaration f cannot be consumed.

For the third term, the application readf consumes f and has type FileBytes. Tensor elimination therefore extends the body by f:File,b:Bytes. Returning b uses T-LVar under the singleton context b:Bytes; the declaration f:File remains. Since there is no linear weakening rule, that branch premise cannot be formed. Thus all three displayed types are underivable.

exercise 18.10.

The scrutinee variable has ;z:11z:11. In the left branch, T-OneE consumes u:1 and a second T-OneE consumes the unit returned by closef; the application of close consumes f:File. Hence ;u:1,f:Filelet =u inlet =closef in :1. The right branch has the identical derivation with v in place of u. Thus T-Case uses f:File as its common residual and gives ;z:11,f:Fileq(z,f):1.

The scrutinee contributes one use of z and zero of f. After masking the branch binders, each branch contributes one use of f: it occurs only as the close argument. The case clause therefore compares equal branch counts and calculates usef(q(z,f))=0+1=1. For the left injection, the complete branch trace is ({h},q(inl,fileh))({h},let = in let =closefileh in )({h},let =closefileh in )(,let = in )(,). The roots are, in order, E-InL, E-One, E-Close, and E-One. Replacing inl and its binder by inr and the right binder gives the symmetric four-step trace. Only the selected printed occurrence of f runs.

exercise 18.14.

One affine weakening gives the body premise ;a:1;f:Filea:1WAff. Rule T-LolliI therefore types λf.:File1. Independently, T-Open, T-OneI, and T-LolliE type open:File under the empty context. A final T-LolliE derives ;adrop:1.

For fresh h, evaluation is (,drop)({h},(λf.)fileh)({h},). The strictly linear cleanup conclusion says that a returned unit has empty live set. The affine trace ends with live set {h}, so that conclusion does not transfer to affine typing. This is not a counterexample to theorem 18.20: its premise is the strictly linear ownership judgment He:A, whereas the displayed derivation uses a and its additional weakening rule.

exercise 36.15.

Use distinct assumptions f1,f2:File before contraction. The body has the derivation shape ;f1:Filerclosef1:1;f2:Filerclosef2:1;f1:File,f2:Filerlet =closef1 inclosef2:1TOneE. Rule C-Rel identifies f1,f2 with f, producing the body printed in badClose; T-LolliI discharges f. The closed argument open supplies the final T-LolliE premise.

For fresh h, the complete relevant trace is (,badClose)({h},(λf.let =closef inclosef)fileh)({h},let =closefileh inclosefileh)(,let = inclosefileh)(,closefileh). The last configuration is stuck because h is no longer live.

Affine typing preserves configurations and at-most-one ownership, but cleanup fails by the drop trace. Relevant typing destroys one-owner immediately and configuration preservation at the first close; therefore iterated progress also fails. Its no-weakening fact still implies Hrv:1H=, so the trace above is not a cleanup counterexample: it never returns unit. Unrestricted typing admits both counterexamples, so one-owner, configuration preservation/progress, and cleanup all fail there.

exercise 18.13.

The identity needs no structural rule: T-LVar followed by T-LolliI derives it already in (and the same tree is available in every regime). It uses zero weakenings and zero contractions.

The discarded function needs exactly one W-Aff, as in exercise 18.14; its inclusion-minimal regime is affine, and it is also derivable unrestrictedly. It is not linear or relevant: inversion of the implication would require a derivation of with x:A still in the linear context, while neither regime has weakening.

For λx.xx, start with two variable leaves under x1:A and x2:A, combine them by T-TensorI, and use one C-Rel to identify x1,x2 with x. Thus its minimal regime is relevant, with one contraction and no weakening. For λx.(xx)x, tensor introduction first uses three fresh declarations x1,x2,x3:A. Contract x1,x2 and then contract the resulting declaration with x3. After the two capture-avoiding identifications the body is (xx)x. Its minimal regime is again relevant, now with two contractions and no weakening. Both copying terms are also unrestrictedly derivable.

Neither copying term is affine or linear. Inverting their tensor trees forces two, respectively three, leaves whose contexts all contain the one declaration x:A; disjoint splitting cannot supply those leaves, and affine weakening cannot create a second occurrence. Hence the classifications and the stated rule counts are minimal.

exercise 18.15.

Let g:!(AA) be linear. Bang elimination consumes g and places f:AA in the unrestricted context. Under the additional linear declaration x:A, the innermost application is f:AA;f:AAf:AA;x:Ax:Af:AA;x:Afx:ATLolliE. Apply the same unrestricted f once to fx and once to f(fx). These are three T-UVar leaves for f, each with empty linear context, and one T-LVar leaf for x. Implication introduction discharges x; bang elimination rebuilds the body; the outer implication introduction discharges g. Hence ;thrice!:!(AA)(AA). The derivation stores only the permission to reuse f, not the numeral three: the same T-UVar rule could occur any finite number of times.

For the unwrapped term, inversion of the outer and inner T-LolliI rules leaves a single linear declaration f:AA for the nested application. Inversion of its two outer T-LolliE rules would have to partition that singleton among three function premises, each of which ends in T-LVar for f. Pairwise disjoint context splitting cannot do so. Thus the displayed linear type of the unwrapped thrice is underivable.

exercise 36.8.

Removing x is absence: the precontext no longer contains it, so neither a term nor a type may mention it. Marking x by zero is contemplation: the shared precontext retains x:S, but a nonzero run-time subject cannot consume it. This is the case justified by zero-erasure.

Permitting an unused unit-priced input is discardability. It needs the ordered weakening rule and its factorization, splitting, and zero-reflection conditions; the rigid zero annotation alone does not justify it. Postulating that all proof inhabitants are equal is proof irrelevance. It needs an equality rule or semantic principle and follows neither from absence nor from erasure. Thus the four changes realize, respectively, clauses 1–4 of proposition 36.33; only the second is the zero-use mechanism of the erasure theorem.

Search the book

Type to search the local edition.