Lectures onType Theory
ch:gradual-typing: ch:gradual-typing
appendix sectionsolutions

ch:gradual-typing: ch:gradual-typing

exercise 23.1.

Rule C-UnkR gives 2c?, and C-UnkL gives ?cN. In a derivation of 2cN, the last rule cannot be an unknown rule because neither endpoint is ?; it cannot be a base rule because the two base constructors differ; and it cannot be C-Arr. Hence no such derivation exists. An attempted proof of transitivity has no case combining C-UnkR with C-UnkL: their middle unknown supplies no equality between the two outside constructors.

exercise 23.2.

For the inner application, g:? matches ?? and 0:N is consistent with ?. Thus g:?(g0)1:?,λg:?.(g0)1:??. The argument has type NN, which is consistent with the outer domain ?, so the complete term has type ? by G-App. Replacing n:N by n:2 changes the argument type to 22 but does not reject the program: C-UnkR still gives 22c?. That application-consistency premise is where the comparison is postponed.

exercise 23.3.

The first elaboration is ((λx:?.x)0)1(????1,f(λx:?.x))(?N1,a0):?. For the second term put u:=(????2,f(λy:?.y))(?N2,a0). The inner instance of I-App gives u:?. The outer instance gives the complete target (22223,f(λx:2.x))(2?3,au):2. All four casts remain present; the two arrow identities are not optimized away.

exercise 23.4.

Since gnd(NN)=??, E-Ground gives ?NNpv???p(??NNpv). The outer term is an injection tagged ??, and its payload is the displayed function wrapper. Both forms occur in the value grammar, so no further reduction is required to obtain a value.

exercise 23.5.

Direct injection of every non-unknown A would require the value clause ?Apv rather than the clause restricted to G. Projection would likewise compare arbitrary stored and demanded type expressions: B?q(?Apv) would return v when A=B and blame otherwise. Canonical forms at ? would then expose an arbitrary syntactic type tag A, not one of the three ground tags. In particular, the present proof that all dynamic functions carry the single tag ?? would no longer apply; this mutation changes both the value invariant and the reduction table.

exercise 23.10.

Inversion of the redex typing supplies v:A1B1, w:A2, A1cA2, and B1cB2. The reduct has the tree Put u=v(A1A2p¯w). The two stages are v:A1B1w:A2A2cA1A1A2p¯w:A1TCastu:B1TApp. Then u:B1B1cB2B2B1pu:B2TCast. If the domain cast is reversed to A2A1p¯, its operand would have to have type A1, but the available premise is w:A2. Thus the inner T-Cast instance, and consequently the application, cannot be formed.

exercise 23.6.

For positive safety, P-Arr requires the opposite-polarity domain premise ?N, which is N-Unk, and the codomain premise N+?, which is P-Unk. Hence NN+??. For negative safety, N-Arr requires N+? in the domain and ?N in the codomain. The same two unknown rules give ??NN.

exercise 23.13.

With the mutated wrapper rule, the first application in example 23.25 becomes ?Np(i(N?pd)). The stored tag of d is 2, so E-Mismatch produces blamep, which then propagates. The false line in the label-preservation proof is the wrapper case: inversion of A1B1+A2B2 supplies A2A1 for a cast labeled p¯. Relabelling that cast by p would instead require the unsupported positive premise A2+A1.

exercise 23.7.

Write the precise function type as BC and its less precise type as ?. Matching changes from fun(BC)=BC to fun(?)=??. The component precision goals are Bty? and Cty?, both instances of Pr-Unk. If the precise argument has type D with DcB, its less precise mate has a type D with DtyD. The less precise application requires only Dc?, supplied by C-UnkR. Thus G-App gives result type ?, and Pr-Unk relates C to that result.

exercise 23.8.

The lambda in r has type ??, matching returns the same arrow, and true:2 with 2c? by C-UnkR. Therefore G-App derives r:?.

A derivation for r would type its lambda as NN and matching would return NN. Inverting its final G-App would therefore require 2cN. Inversion of consistency rules shows that no rule has these two distinct base endpoints. Only the direction from a typed precise term to its less precise mate remains valid; typing r cannot be used to infer typing of r.

exercise 23.12.

The common inner call elaborates completely to uC:=(????i,f(λx:?.x))(?Ni,a0):?. Keeping the two fresh labels at the outer source position, the two complete elaborations are a=(2222o,f(λy:2.y))(2?o,auC):2,a=(????o,f(λy:?.y))(??o,auC):?. Thus every one of the six inserted casts is shown: the common inner pair and the two outer pairs. Reducing the common subterm gives uCz, where z=?Ni,a0. The precise outer argument cast then gives 2?o,azblameo,a. The surrounding application propagates this blame, so ablameo,a:2. On the other side, the unknown identity casts and arrow wrapper all contract without changing the payload, so az:?. The final results are related by Cz:?2ty?blameo,a:2Cz:?CPrBlame. Thus equality of observations would fail, while error approximation holds.

exercise 23.9.

By lemma 23.32, i:NNCi:NN. Apply CPr-CastR using NNc?? and NNty?? to obtain the required iCw. For the argument, instantiate CPr-CastR with S=U=N and V=?: its term premise is reflexive numeral precision, while its remaining premises are Nc? and Nty?. Hence 0:NCz:?.

The applications are related by CPr-App. Their right reduction is wz?Np(i(N?p¯z))EWrapApp,?Np(i0)EProject,?Np0EBeta. The left application i0 reduces to 0. Reflexive numeral precision and CPr-CastR relate this 0:N to the final injection at ?.

exercise 23.11.

Clause 1 begins with successful evaluation of the more precise program. Finite simulation and value catch-up cannot introduce left blame, so the less precise program must reach a related value. Clause 2 begins with success of the less precise program. The more precise program may contain a check removed on the right, so its permitted additional outcome is blame.

The false reverse clause would assert that right success always implies left success. In the displayed esrce, the right elaboration returns the tagged natural z, while the left outer Boolean projection reaches blameo,a. Hence the blame alternative is necessary. In the actual proof, operational trichotomy also presents the possible case aω. Infinite simulation would then force aω, contradicting the assumed finite reduction of a to a value. This is the exact step that replaces the false normalization claim.

exercise 23.14.

Let i=λn:N.n and g=λf:NN.(f0)i. The inner application in g elaborates to (NNNNi,ff)(NNi,a0). The complete outer elaboration is ((NN)N(NN)No,fgC)(NNNNo,ai), where gC is g with the displayed inner target body.

Each arrow identity is a wrapper. Applying the outer wrapper first inserts an identity arrow cast on its argument and an identity base cast on its result. Erasure removes all of these casts, so that step leaves the erased term gi unchanged. The ensuing beta step erases to (i0); applying the inner arrow wrapper and reducing its two base identity casts again leave that erasure unchanged; the inner beta step erases to 0. Finally the surrounding result identity casts reduce to 0 and erase to 0 throughout. Thus the only non-stuttering erasure steps are gii00, exactly the ordinary call-by-value reduction of the static source term.

Search the book

Type to search the local edition.