Lectures onType Theory
ch:mendler-recursion: ch:mendler-recursion
appendix sectionsolutions

ch:mendler-recursion: ch:mendler-recursion

Exercise 84.1.

Let the result carrier be μMFB and define ϕf(R,rec,inl):=inFBM(inl),ϕf(R,rec,inr(a,r)):=inFBM(inr(f(a),rec(r))). In the cons clause rec:RμMFB and r:R. Mendler beta therefore gives map-nil as the target nil and map-cons as target cons applied to f(a) and the recursively mapped tail.

Exercise 84.2.

The attempted algebra is ϕ:R:Ui(R(μMFT1))(R1)(μMFT1) with ϕ(R,rec,f):=f. The extracted f has domain R, while the expected result has domain μMFT; abstract R cannot be converted to that fixed type. Replacing the clause by λx:μMFT. typechecks, but ignores f. Consequently it cannot expose the negative function needed for out(x)(x) and cannot recreate the self-application cycle.

Exercise 84.3.

Strengthen the claim to every A and induct on t:Term(A). Variables and applications compute directly. In the lambda case the induction hypothesis at Incr(A) applies because lift(id)(zeroV)=zeroV,lift(id)(sucV(a))=sucV(a). Thus lift(id) is pointwise the identity, so rename(id,lam(b))=lam(rename(id,b))=lam(b).

Exercise 84.4.

First inspect z:Incr(A). At zeroV both liftSub(ηA)(z) and ηIncr(A)(z) are var(zeroV). At sucV(a), liftSub(ηA)(sucV(a))=rename(sucV,var(a))=var(sucV(a)). Nested induction on t now settles variables and applications by computation. In the lambda case use the displayed pointwise equality, the induction hypothesis at Incr(A), and congruence for lam.

Exercise 84.5.

At arbitrary R, the recursive placeholder has type rec:RN. In the nonempty layer, f:RR and r:R, so f(r):R and suc(rec(f(r))):N. An unrolled parent instead has type μMFFoo. It cannot be passed to rec because the latter’s domain is the universally quantified R, not the fixed point. This is the exact abstract-domain barrier.

Exercise 84.6.

In τ0=Aτ1 and τ1=Bτ0, each traversal follows an arrow codomain, so the occurrence returning to τ0 is positive. The same argument starts at τ1; property P passes. If the second constraint is changed to τ1=τ0B, the traversal from τ0 to τ1 preserves polarity and the arrow domain reverses it on returning to τ0. Thus a type equal to τ0 contains a negative occurrence of τ0, and property P fails.

Exercise 84.7.

For natural-number sum on lists, use result N: ϕΣ(R,rec,inl)=0,ϕΣ(R,rec,inr(n,r))=n+rec(r). For append to a fixed second list ys:μMFA, use that list type as the result: ϕys(R,rec,inl)=ys,ϕys(R,rec,inr(a,r))=inFAM(inr(a,rec(r))). The beta rule gives the ordinary nil and cons equations. Only the first list is folded; the second is already the result value returned at nil, so no destructor for the first list is needed.

Exercise 84.8.

First prove, by cases on z:Incr(A), subst(liftSub(τ),liftSub(σ)(z))=liftSub(λa.subst(τ,σ(a)))(z). The zero case is reflexivity. The successor case uses renaming–substitution fusion and liftSub(τ)(sucV(b))=rename(sucV,τ(b)). Now strengthen the main claim over the variable type and induct on t. The variable case is the definition, and the application case uses both induction hypotheses. In the lambda case, the induction hypothesis at Incr(A) reduces the goal to the displayed lift equation; congruence for lam closes it.

Exercise 84.9.

For FT(R)=R1, the attempted destructor algebra extracts f:R1 where a result μMFT1 is required; it fails because R is abstract. By contrast, the nonempty Foo layer contains f:RR and r:R, so rec(f(r)) is well typed for rec:RN.

In the source example, put coo0=coo(id) and g=coo(coo0)(noo). Then foo=coo0(g). The histomorphism can unroll g, recover coo0, and form loopFoo(foo)1+loopFoo(coo0(g))1+loopFoo(foo)1+(1+loopFoo(foo)). The catamorphism supplies only RN; the histomorphism additionally supplies the unrolling observation that makes this larger self-containing argument available.

Search the book

Type to search the local edition.