Lectures onType Theory
ch:containers-ornaments: ch:containers-ornaments
appendix sectionsolutions

ch:containers-ornaments: ch:containers-ornaments

Exercise 81.1.

The unique shape is :1 and the contents function f:Fin(2)X is f(0)=a and f(1)=b. Hence the element is (,[a,b]). Container action leaves the shape fixed: cmapCBin(h,(,[a,b]))=(,[h(a),h(b)]). This is one binary-node layer: its contents label two recursive positions. A complete tree arises only after taking an appropriate fixed point of the layer operator.

Exercise 81.2.

Let mS(n)=n1 and mP(n,i)=suc(i). If i:Fin(n1), then suc(i):Fin(n). At shape 0 the target has shape 0 and no positions. At shape 3, mX((3,[x0,x1,x2]))=(2,[x1,x2]). A nonempty target at source shape 0 would require a function from some inhabited target-position type to Fin(0), which is impossible.

Exercise 81.3.

Represent F by the sum of the zero-position unit container and the one-position container with shape A; represent G by one shape with two positions. Composition gives FG either zero positions or two positions indexed by a chosen a:A. Product gives GF a pair of F-shapes and therefore 0, 1, or 2 positions. In particular the shape (nilShape,consShape(a)) of GF has one position, whereas no shape of FG does. Thus the two polynomial operators are not identified by reassociation.

Exercise 81.4.

Set jS(j)P(j,)010suc(k)A1n(suc(k),a,)=k. For u=(a,h) in the successor fiber, its unique recursive child is h():X(k). Replacing the last equation by n(suc(k),a,)=suc(k) makes the child have type X(suc(k)). The recursive index no longer descends from the result index, so the layer no longer describes the vector constructor.

Exercise 81.5.

The derivative is (A+X×X)=0+(1×X+X×1)X+X. For fork(l,r) the left-hole context stores inl(r) and plugs x as fork(x,r); the right-hole context stores inr(l) and plugs x as fork(l,x). These are the two summands of the product rule, each storing the sibling belonging to the undifferentiated factor.

Exercise 81.6.

The result has type Vec(A,m+n). Induct on xs. At vnil both sides compute to forget(ys). At vcons(a,zs), both sides expose the head a and the induction hypothesis identifies their tails: forget(vappend(vcons(a,zs),ys))a::forget(vappend(zs,ys))=IHa::append(forget(zs),forget(ys)). The forgetful map erases the result index m+n while retaining the list spine.

Exercise 81.7.

A binary-node layer has two recursive positions. A leaf layer has none. A forgetful morphism from the alleged refinement back to the binary layer would need a position map from each of those two target positions to a position of the leaf source. Its codomain is empty, so no such function exists. For a family map that distinguishes the missing right child, the induced map to the pullback is not surjective and hence is not an equivalence. Thus the proposal violates the cartesian clause of definition 81.16; selecting only the left child does not repair the missing right position.

Exercise 81.8.

Induct on the relation witness. At 0 the input list is nil, list map is nil, and RB(0,nil) is immediate. At a successor, write xs=cons(a,zs). The induction hypothesis gives RB(n,mapList(f,zs)); the successor/cons clause yields RB(suc(n),cons(f(a),mapList(f,zs))). The ML type is (’a -> ’b) -> ’a list -> ’b list. It does not state that the output length equals the input length; that fact remains the displayed logical relation.

Exercise 81.9.

Reverse has shape component nn and position component rn(i)=n1i. Its self-composite has shape component the identity and position component irn(rn(i))=n1(n1i)=i(i:Fin(n)). Hence for every (n,f) its extension is (n,λi.f(i)), pointwise equal to (n,f).

Exercise 81.10.

Write L=1+A×X and R=1+B×X. The product rule gives (L×R)(A×R)+(L×B). An element inl(a,r) is a hole in the L recursive position and plugs x as (inr(a,x),r). An element inr(l,b) is a hole in the R recursive position and plugs x as (l,inr(b,x)). For example, inl(a,inl) and inr(inr(a,x0),b) inhabit the two summands.

Exercise 81.11.

Take A=1. Finite lists form the initial algebra of F(X)=1+1×X. The type containing one finite list for each natural length together with one infinite stream is also closed under the same one-step equation: the extra element is sent to the successor branch with itself as tail. Thus a bare fixed-point equivalence does not choose between them. Initiality distinguishes finite lists by the unique algebra morphism out; finality distinguishes the possibly infinite solution by the unique coalgebra morphism in.

Search the book

Type to search the local edition.