Lectures onType Theory
ch:mutable-value-semantics: ch:mutable-value-semantics
appendix sectionsolutions

ch:mutable-value-semantics: ch:mutable-value-semantics

exercise 49.1.

Let the representation of p have root p and child locations x,y. Before copying, acc(π,p)={p,x,y}. The recursive copy chooses fresh q,x,y, copies the two scalars, and produces acc(π,q)={q,x,y},acc(π,p)acc(π,q)=. Assignment to q.x changes only π(x), so π(x)=π(x) and p.x is unchanged. A shallow copy that reused x would put x in both accessible sets, contradicting their disjointness.

exercise 49.2.

The book check accepts (&p.x,&p.y) at its first distinct fields, rejects (&p,&p.x) when the first path ends, and accepts (&a[0],&a[1]) at the distinct constant indices. It rejects (&a[i].x,&a[i].y) as soon as it meets the unknown index. Under the published relation, &p&p.x follows by reflexivity and right-field extension. The pairs (&p.x,&p.y) and (&a[0],&a[1]) are unrelated. The published relation also fails to relate the final nested pair: its unknown-index rule relates the array paths, but it has no left congruence for extending both sides. Thus the book and source checks differ on the final pair.

exercise 49.3.

Suppose η1(o)=o, the p-field of π(o) is p, and the x-field of π(p) is x. The lvalue trace begins with η1(o)=oπ(o)=varvoolvovarPSSName. Rule PSS-Struct lifts that step under p, after which π(o)=var[,p,]sΔ(s)(p)=varspovar.plvpvarPSSProp selects the first child. A second congruence step and PSS-Prop, using Δ(sp)(x)=varZ, select xvar. Both qualifier meets are min(var,var)=var, so the displayed T-Inout derives the argument judgment. If o.q.x follows a distinct first-field child q, the book comparison has an apart witness at pq. Tree independence makes the subtrees rooted at p and q disjoint; their selected x-locations are therefore disjoint as well.

exercise 49.4.

Let the ordinary array value have root a. Copying it creates a fresh root a and fresh descendants; the callee’s ordinary parameter maps to a. Let the two access arguments resolve to 1,2. Their callee parameters map directly to those existing locations. By the exercise hypothesis, ηλ is already well formed. The copied set is disjoint from its roots and all caller roots by proposition 49.3; the borrowed sets are disjoint by lemma 49.6. Mutual freshness covers further ordinary parameters. The complete callee frame is well formed. Its pop drops a and its descendants, but neither borrowed location.

exercise 49.5.

If a:[Z], then a[3] is well typed even when the runtime array has length two. Its evaluation reaches a designated out-of-bounds runtime error, one instance of the progress theorem’s error alternative. An overlapping access call is different: the conservative call premise fails, so the program has no typing derivation and is not deferred to runtime.

exercise 49.6.

The premise is a finite small-step execution, from the initial state, of an expression also expressible in the natural semantics. The conclusion is a natural-semantics evaluation whose value equals the pointer-store value after readback. Copy-on-write needs a forward simulation showing that sharing and the uniqueness check preserve the abstract independent-copy result. LLVM lowering needs a typed source-to-IR simulation that relates source steps, allocation, and observable results to IR execution. The source’s semantic correspondence proves neither obligation.

Search the book

Type to search the local edition.