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

ch:recursive-types: ch:recursive-types

exercise 24.1.

Put L=μX.(Unit+Nat×X). The empty list has the derivation unit:Unitinlunit:Unit+Nat×LTInlnil:LTFold. Under n:Nat,xs:L, pairing and right injection give n,xs:Nat×L,inrn,xs:Unit+Nat×L. Rule T-Fold, followed by two lambda rules, therefore derives cons:NatLL. The two applications complete the requested derivation: cons:NatLL3:Natcons3:LLTAppnil:Lcons3nil:LTApp.

To keep the trace within the measure, first expose the two abbreviations: c=λn.λxs.foldL(inrn,xs),C[t]=case (unfoldt) of{inlunil; inrqsndq}. Thus c=cons, and C[] is exactly the displayed list case. Every reduction is C[c3nil]C[(λxs.foldL(inr3,xs))nil]EBetaC[foldL(inr3,nil)]EBetacase (inr3,nil) of {}EUnfoldFoldsnd3,nilECaseInrnil.product elimination The third reduction is the unique use of E-UnfoldFold.

exercise 24.2.

Let T=μX.(Nat×X+Unit). The substitution instance in the fold premise is (Nat×X+Unit)[T/X]=Nat×T+Unit. If v:Nat×T+Unit, the redex is typed by v:Nat×T+UnitfoldTv:TTFoldunfold(foldTv):Nat×T+UnitTUnfold. Rule E-UnfoldFold contracts this term to v, which already has the type on the conclusion. No type-equality rule occurs: the common type is obtained by the displayed capture-avoiding substitution in the premises of T-Fold and T-Unfold.

exercise 24.3.

For D=μX.(XNat), the derivation under x:D is x:Dx:Dx:Dunfoldx:DNatTUnfoldx:Dx:Dx:D(unfoldx)x:NatTApp. Consequently δ:DNat,foldDδ:D, and one application derives ΩD:Nat.

The displayed beta step followed by E-UnfoldFold gives ΩD2ΩD. Induct on k. The case k=0 is the empty trace. If ΩD2kΩD, append the two-step cycle to obtain ΩD2k+2ΩD. Hence a return trace exists for every 2k, and the deterministic term never reaches a value.

For DA=μX.(XA), assume f:AA. Under x:DA, (unfoldx)x:A, so f((unfoldx)x):A and δf:DAA. Thus δf(foldDAδf):A,FixA:(AA)A. For a closed function value v:AA, write qv=δv(foldDAδv). The eager compatible reduction is FixAvqvv((unfold(foldDAδv))(foldDAδv))v(qv). This is the precise unfolding equation. It does not assert that the eager application v(qv) subsequently terminates.

exercise 24.4.

Write L=μX.(Nat×X+Unit),B=Nat×L+Unit. Ignoring duplicate pairs after their first visit, a left-to-right queue run visits pairchildren enqueued after head exposure(L,B)(Nat×L,Nat×L),(Unit,Unit)(Nat×L,Nat×L)(Nat,Nat),(L,L)(Unit,Unit)none(Nat,Nat)none(L,L)(Nat×L,Nat×L),(Unit,Unit). The last two children have already been visited, so the queue empties and the algorithm accepts.

If the right root is B=Bool×L+Unit, the exposed sums still match. Their product children enqueue (Nat,Bool) and (L,L). The first of these is the first rejecting pair: its constructor heads are distinct base types.

exercise 24.5.

Using the chapter’s abbreviations P and B, the run is P12(λm.λn.B(m,n))12PUnroll(λn.B(1,n))2PBetaB(1,2)PBetasucc(P02)PIfSsucc((λm.λn.B(m,n))02)PUnrollsucc((λn.B(0,n))2)PBetasucc(B(0,2))PBetasucc2PIfZ3.PSuccN There are exactly four beta steps.

Call by name has no argument context, so (λx:Nat.0)ΩNat0PBeta. Under the proposed call-by-value mutation, the beta root is unavailable until the argument is a value. The argument context lifts the self-loop ΩNatΩNat to (λx.0)ΩNat(λx.0)ΩNat. Repeating this step gives an infinite sequence, so the application never reaches 0.

exercise 24.6.

Assume F(d)=d. Induction on n gives Fn()Dd: the base is leastness of , and Fn+1()=F(Fn())DF(d)=d uses monotonicity and the fixed-point equation. Therefore nFn()Dd.

For the counterexample, take the pointed omega-chain C={0D1DDω} and define G(n)=0(n<ω),G(ω)=ω. The map is monotone: all finite inputs have the same image, and that image is below G(ω). It is not continuous, since G(nn)=G(ω)=ω0=nG(n). Thus monotonicity alone does not justify the join calculation in the fixed- point proof.

exercise 24.7.

Let a0= and ai+1=cons(0,ai). Its lub z is the infinite all-zero list. The images form Dinr(0,)Dinr(0,cons(0,))D. Using the componentwise product order and the lifting lub, iout(ai)=↑inr(0,iai)=↑inr(0,z)=out(z).

A chain in L that reaches a finite word ending in nil is constant from that stage onward: such a total finite word has no strict extension in the partial-list order. Equivalently, if the lub reveals nil after a finite prefix, compactness of that finite total word puts the same observation in some chain member, after which the chain is constant.

Now take a chain in (1+N×L). If it is constantly bottom, its image under in is constantly . Otherwise it has a first nonbottom member. The separated-sum tag cannot subsequently change. For the left tag the chain is constantly inl(), and the image is constantly nil. For the right tag discreteness of N fixes one head n, while the tails form a chain (di); hence in(iinr(n,di))=cons(n,idi)=icons(n,di). These exhaustive cases prove that in preserves omega-chain lubs. The same cases prove monotonicity, so it is continuous.

exercise 24.8.

Suppose the final typing rule has premise Γ,x:Ae:A, let ηRΓγ, and put F(d)=[[e]]η[xd],q=fix x:A.e[γ]. We prove Fk()RAq by induction on k. Bottom is related to every closed term by admissibility’s bottom clause. At the inductive step, the exact environment invariant is η[xFk()]RΓ,x:Aγ[xq]. The body case of the fundamental induction therefore gives Fk+1()RAe[γ,q/x]. But P-Unroll gives qe[γ,q/x]. Finite anti-reduction on the term argument turns the preceding judgment into Fk+1()RAq.

Finally, the predicate ddRAq is admissible by lemma 24.37: it contains bottom and is closed under lubs of omega-chains. Applying the lub clause to the Kleene chain gives lfp(F)=kFk()RAq, which is exactly the P-Fix conclusion.

exercise 24.9.

For recursive values, suppose foldvn+1μX.Afoldw. The defining clause gives vnA[μX.A/X]w. At target index zero the value relation is universal. At target m+1n+1, we have mn, so the induction hypothesis gives vmA[μX.A/X]w, and the recursive clause restores foldvm+1μX.Afoldw.

For arrows, suppose fn+1ABg. Again index zero is immediate. If m+1n+1, take any jm+1 and any ajAb. Since also jn+1, the original arrow clause already gives faEjBgb. This is precisely the arrow clause at m+1.

For the copier, retain the chapter’s abbreviations h=foldθ,r=(unfoldh)h,g=λxs.copyBodyrxs. The promised two steps are r=(unfold(foldθ))hθhg, first by E-UnfoldFold, then by call-by-value beta. For a cons value, the remaining administrative betas expose caseList (conskys) of {}consk(gys). This prefix evaluates r to g, unfolds the folded input by E-UnfoldFold, and then uses the right sum-case root. The structural induction hypothesis is used exactly on gysys; compatible closure lifts that sequence through the argument position of consk[], equivalently through the pair, right-injection, and fold constructor contexts. Thus consk(gys)conskys.

At an arbitrary n, fundamental reflexivity with equal empty substitutions gives vEnListNatv. Structural convergence gives copyvv. Apply the two-sided anti-reduction clause with this left prefix and an empty right prefix to obtain copyvEnListNatv.

exercise 24.10.

The classifications are:

  1. Preservation of the type of ΩD is a safety claim. It constrains every finite reduct but does not assert that a value appears.

  2. A division procedure that returns the quotient whenever it returns with a nonzero divisor satisfies partial correctness. Termination is not part of that implication.

  3. The assertion that every recursive call to plus returns is a termination claim.

  4. The assertion that every finite observation of the all-zero lazy list reveals another cons cell is productivity.

  5. The assertion that plus terminates on all numeral inputs and returns their mathematical sum is totality: termination and partial correctness are both present.

The examples are deliberately not interchangeable. In particular, ΩD is safe without terminating, while productivity describes all finite tail observations of a domain element rather than return of an eager infinite value.

exercise 24.11.

Inverting a typed Pr-Unroll redex gives Γ,p:Pe:P,Γfix p:P.e:P. Term substitution with the second judgment in the first yields Γe[fix p:P.e/p]:P, which is the type of the reduct. This proves preservation for the root.

Preservation says only that a step retains its type. For ωP=fix p:P.p, Pr-Unroll is the self-loop ωPωP, so normalization fails despite preservation. At P=0, rule Pr-Fix derives the closed inhabitant ω0:0. Thus the syntactic consistency statement “there is no closed term of type 0” is already false even though evaluation produces no empty-type value.

exercise 24.12.

At index zero every pair of closed values of the same type is related. A list fold at index n+1 consumes one index and compares its sum payload at index n.

For (v0,v1), index one reaches payload index zero, so the different inl and inr payloads are still related. At index two the fold clause reaches the sum clause at index one; the tags differ, so the pair is not related. The least index is 2.

For (v1,v2), index two follows fold at 2right sum at 1product at 101Nat1, and the last judgment is false. Index one again stops at the universal payload relation, so the least index is 2.

For (v3,v4), index two compares the common outer head 0 and their tails at list index one, which consumes to payload index zero; hence they are related. At index three the clauses traverse outer fold at 3right sum and product at 20=0,tail fold at 2right sum and product at 112. The final natural clause fails. Thus the least distinguishing index is 3.

exercise 24.13.

For p=, strict case analysis makes Fp the constant-bottom map, so Fpi()= for every i and μΦ()=. For p=0, Fp is the constant-zero map: F00()=,F0i()=0(i1),μΦ(0)=0. For p=k+1, Fp=succ. Because this operation is strict, every iterate starting at bottom is bottom, and therefore μΦ(k+1)=.

It remains to check preservation of chain lubs by the resulting parameter map. A chain in the flat domain either remains bottom, or reaches one fixed numeral and is constant thereafter. In the first case both sides of the continuity equation are bottom. If the numeral is zero, the image chain is eventually zero and has lub zero. If it is positive, the image chain is constantly bottom. In all cases μΦ(ipi)=iμΦ(pi), which directly verifies the conclusion of the parameterized fixed-point lemma for this Φ.

exercise 24.14.

For a closed list value v, call by value gives K[v]=(λxs.ΩD)vΩD, after which the two-step cycle repeats forever. For the copied argument, the argument position is evaluated first. Structural convergence supplies K[copyv](λxs.ΩD)vΩD, so this term also diverges.

For every numeral m, neither plugged term satisfies m. The contextual biconditional of theorem 24.51 is therefore falsefalse, and is true. The stronger assertion that both terms converge is false. Observational equivalence preserves any numeral that is produced; it does not manufacture convergence inside a diverging context.

exercise 24.15.

The body of ΩNat denotes the identity map on N. Its Kleene chain is Did()=Did2()=D, so [[ΩNat]]=. If this closed typed term converged to a numeral n, the operational-to-denotational direction of adequacy would give =n, impossible in the flat domain. Progress and determinism leave an infinite reduction, which is the operational self-loop already displayed.

For addition, its semantic functional is H(f)(m)(n)=caseN(m,n,ksucc(f(k)(n))). The third finite approximant already gives H3()(2)(1)=succ(H2()(1)(1))=succ(2)=3. Later approximants agree there, so [[plus21]]=3. The denotational-to- operational direction of adequacy recovers plus213. Conversely, applying the operational- to-denotational direction to the chapter’s displayed run recovers the same semantic equality. The two uses are logically distinct.

exercise 24.16.

Put B=Unit+Nat×L. From p:B, the iso-recursive derivation is p:BfoldLp:LTFoldunfold(foldLp):BTUnfold. Since p is a value, unfold(foldLp)pEUnfoldFold. This is an object-language operational contraction.

For equi-recursive equality, initialize the worklist by (L,B). Exposing the left root gives B, so the matching sum heads enqueue (Unit,Unit) and (Nat×L,Nat×L). The product pair enqueues (Nat,Nat) and (L,L). Exposing (L,L) only recreates already visited pairs, so the worklist empties and the algorithm accepts LμB. This is a metalevel type-equality decision on finite graphs. It is neither a term reduction nor a premise used by T-Fold, T-Unfold, or E-UnfoldFold in the iso-recursive calculus.

Search the book

Type to search the local edition.