Uniqueness Types and Destructive Update
Prerequisites. Direct starred prerequisites: none. No later core chapter depends on this route.
Suppose a list cell is consumed exactly once. May a program overwrite the cell? Not yet. A hidden pointer may still reach it. Affine use controls the future use of one name; destructive update needs a statement about every name that can reach the same node. The system in this chapter obtains that statement by typing graphs rather than pretending that source terms are trees.
The graph is therefore the primary object. Conventional typing fixes the types at its node interfaces; uniqueness attributes then constrain the number of references reaching those interfaces. A bounded overwrite becomes admissible only when those two facts meet at the rewritten node.
A term denotes a rooted graph
Let constructors and defined symbols have fixed arities. Expressions and constructor patterns are
Definition 47.1 — Rooted term graph¶
A rooted graph is a pair
Referenced from 2 locations
The denotation
Definition 47.2 — Graph-denotation equality¶
Write
Referenced from 2 locations
The distinction between copying syntax and sharing a root is now visible:
Exercise 47.1¶
Draw both rooted graphs above. Compute the number of incoming references to each constructor node, counting the root as an incoming reference.
Referenced from 4 locations
A case-free function equation
Conventional typing before uniqueness
Conventional types and symbol schemes are
Definition 47.3 — Conventional graph typing¶
A type assignment
Referenced from 2 locations
Proposition 47.4 — Term–graph soundness and completeness¶
Referenced from 3 locations
Proof of Proposition 47.4 — Term–graph soundness and completeness
Proof. For soundness, induct on the expression derivation. A symbol application adds one equation whose incident types are the selected scheme instance. Sharing identifies the variable node with the root of the bound expression; the two premises assign the same
For completeness, decompose the graph denotation by the outer expression constructor. Restrict its node assignment to the component graphs. At a shared or recursive interface the restrictions agree because they are restrictions of one assignment. The induction hypotheses therefore rebuild the corresponding source rule. ◻
Proof of Theorem 47.5 — Conventional subject reduction
Proof. By proposition 47.4, type
Equation generation and the principal conventional type
Assign a fresh type variable to each expression occurrence and graph interface. A symbol occurrence contributes equations equating its incident variables with one fresh instance of its scheme. Sharing contributes an equality between the bound root and every occurrence of the bound variable; a cycle contributes the simultaneous interface equalities; a pattern contributes the constructor equations.
Definition 47.6 — Conventional constraint set¶
Referenced from 2 locations
Proof of Lemma 47.7 — Exact equation generation
Proof. Induct on
Theorem 47.8 — Principal conventional typing¶
If
Referenced from 4 locations
Proof of Theorem 47.8 — Principal conventional typing
Proof. Existence and exactness are lemma 47.7. The factorization property is the most-general-unifier theorem applied to the finite first-order equation set. ◻
Exercise 47.2¶
Generate and solve the equations for
Referenced from 4 locations
Attributes describe aliasing
Every node of a type carries one of two attributes:
Definition 47.9 — Attributed types and correction¶
An attributed data type has the form
Referenced from 2 locations
For lists, a useful scheme is
Exercise 47.3¶
Compute correction for a unique list of unique elements and for a unique list of shared elements. Explain why the element attribute is unchanged and why the first corrected type need not be a constructor result type.
Referenced from 3 locations
The uniqueness judgment has the same surface shape
Definition 47.10 — Reference-sensitive graph typing¶
Extend a graph by one incoming reference to its root. For each reference from an argument position expecting
Referenced from 5 locations
Lemma 47.11 — Contraction matches graph sharing¶
If the conclusion of Contr denotes a graph in which the node for
Referenced from 3 locations
Proof of Lemma 47.11 — Contraction matches graph sharing
Proof. After substitution the two variable roots are one node with at least two incoming references. The graph condition therefore checks
Theorem 47.12 — Uniqueness soundness and graph completeness¶
If
Referenced from 3 locations
Proof of Theorem 47.12 — Uniqueness soundness and graph completeness
Proof. Soundness is induction on the attributed typing derivation. The structural case absent from conventional typing is lemma 47.11. Subsumption composes the local inequalities. Cycle correction accounts for incoming recursive edges.
Completeness cannot in general recover the same syntax, because one graph has many let presentations. Traverse a spanning presentation of the typed graph, insert a let at every node with multiple incoming references, and use contraction with its corrected type. Present strongly connected components by simultaneous
Theorem 47.13 — Uniqueness subject reduction¶
If
Referenced from 4 locations
Proof of Theorem 47.13 — Uniqueness subject reduction
Proof. Transfer the source derivation to a graph typing by theorem 47.12. A well-typed graph-rewrite rule has the same attributed interface at its left and right roots. Substitution of the matched graph for pattern variables preserves every local reference condition; if the replacement increases a reference count, its rule typing has already corrected that interface. Context closure preserves the assignment. Graph completeness then returns a typed presentation
Exercise 47.4¶
Give a derivation for
Referenced from 4 locations
Polymorphic attributes and the exact principal result
Replace concrete attributes by variables
Definition 47.14 — Attribution problem¶
Fix a conventional solution
Referenced from 2 locations
Lemma 47.15 — Effective closure¶
For finite
Referenced from 3 locations
Proof of Lemma 47.15 — Effective closure
Proof. Regard inequalities as edges in a finite graph whose two constants are
Theorem 47.16 — Principal attribution, relative to a conventional solution¶
If
Referenced from 4 locations
Proof of Theorem 47.16 — Principal attribution, relative to a conventional solution
Proof. Every rule contributes only inequalities between its fresh variables and constants. Closure adds exactly their logical consequences. Hence any solution of the generated constraints is a solution of the closure, while the identity symbolic assignment together with that closure is itself a solution. Instantiating its variables produces every concrete attribution. ◻
The qualifier “relative to a conventional solution” cannot be dropped.
Proposition 47.17 — No claimed global principal uniqueness type¶
A most general conventional typing need not admit an attribution even when a proper conventional instance does. Therefore theorem 47.8, theorem 47.16 do not compose into a theorem asserting one principal uniqueness type for every uniqueness-typable expression.
Referenced from 2 locations
Proof of Proposition 47.17 — No claimed global principal uniqueness type
Proof. The higher-order subtype restriction treats type variables differently from their later structured instances. Consequently the source exhibits a most general conventional solution whose lift fails while a proper instance lifts. Attributability therefore need not reflect backward from an instance to its generalization. The effective strategy that attributes only the principal conventional type is sound when it succeeds but may reject an expression whose conventional instance is attributable. ◻
Exercise 47.5¶
For the
Referenced from 3 locations
What destructive update is licensed
An implementation may reuse a constructor node only when the redex interface types that node uniquely and the rewrite preserves its constructor layout. This is an update-eligibility theorem, not a semantics of arbitrary machine writes.
Definition 47.18 — Bounded update eligibility¶
A rewrite occurrence is eligible to overwrite a matched node
has exactly one incoming reference in the root-extended graph;the matched interface assigns
a type with outer attribute ;the replacement constructor fits the implementation’s declared node layout; and
every outgoing edge retained by the replacement has the attributed type checked by the graph rule.
Referenced from 7 locations
The first two conditions exclude hidden aliases; the last two bound what “in-place” means. Eligibility does not change the source rewrite relation. Because the graph denotation retains garbage equations, allocation and overwrite are compared below only at the reachable rewritten interface, not by the full relation
Theorem 47.19 — Eligible overwrite preserves the typed graph interface¶
Replacing an eligible node according to its typed graph-rewrite rule yields the same typed interface as allocating the replacement node and redirecting the unique incoming reference.
Referenced from 3 locations
Proof of Theorem 47.19 — Eligible overwrite preserves the typed graph interface
Proof. There is one incoming reference, so redirection has no other observer. The layout condition makes the overwritten equation well formed, and the edge condition supplies exactly the local premises of definition 47.10. All equations outside the node are unchanged. Both implementations therefore have isomorphic reachable labelled graphs after forgetting physical node identities. ◻
In-situ quicksort.
The 1994 application represents a list segment by a unique spine, while its elements may remain shared. Partition consumes one spine cell at a time and relinks that cell onto one of two accumulator spines. Recursion sorts the two unique partitions; concatenation relinks their spines. No step needs to copy an element or mutate through a shared spine.
For example, after consuming
Exercise 47.6¶
Trace the four-cell example. At each relink, give the unique incoming reference and the retained outgoing edge required by definition 47.18. Then change the input spine to
Referenced from 4 locations
The provenance boundary.
The destructive-update and in-situ quicksort application comes from the 1994 system, which explicitly omits the preservation proof. The graph-semantic soundness and preservation results used here belong to the later 1996 Barendsen–Smetsers system. We use the later theorem only after translating the application’s update eligibility into its frozen graph interface; we do not transfer a theorem to an unspecified compiler or cost model.
Affinity now has a precise contrast. An affine variable may be used at most once syntactically. A unique graph node has one incoming reference semantically. Either property can be useful, but only the second proves the absence of a hidden alias needed by definition 47.18.
Frozen boundary and seminar problems
The proved core contains the selected graph-denotation language, conventional typing, attributed uniqueness typing, graph rewriting, principal conventional inference, and principal attribution relative to a fixed conventional solution. The quicksort application is bounded by definition 47.18. Arrays, concurrency, exceptions, foreign calls, allocator costs, and observational equivalence for a concrete compiler need additional semantics and proofs.
Suggested first pass.
Begin with exercise 47.1, exercise 47.2, exercise 47.4, exercise 47.6. Continue with exercise 47.7. The practical project is an optional second pass.
None of these problems is a prerequisite for a later chapter.
Exercise 47.7¶
Write the root-rewrite case of theorem 47.13 in full. Name the interface substitution, show how every increase in reference count invokes correction, and explain why the conclusion is graph-equivalent rather than syntactically identical to the reduct.
Referenced from 4 locations
Exercise 47.8¶
Design a two-phase inference procedure that first enumerates conventional instances and then solves attribute constraints. Prove soundness of each reported result. Explain why termination of an unrestricted enumeration does not follow from theorem 47.8.
Referenced from 3 locations
Exercise 47.9¶
Practical project.uniqueness-eligibility-checker Implement in Kappa the finite attribute preorder, a reference-count check, and the four conditions of definition 47.18. The observable corpus must accept one unique use, reject duplicated unique use, accept duplicated shared use, accept relinking a unique list spine, reject relinking a shared spine, and reject a constraint closure containing
Sources.
The frozen graph language, conventional and uniqueness systems, and their metatheory are Definitions 3.1–3.7, Propositions 5.7–5.19, Theorem 5.20, and Theorems 7.13, 7.20, 7.23–7.24, 8.11–8.13, and 8.21 of Barendsen and Smetsers [BS96]. The destructive-update and in-situ-quicksort application is [SBvEP94]; its omitted preservation argument is not silently supplied by that paper. Later simplifications of uniqueness typing [dVPA08] are comparisons only and own no theorem stated in this chapter.