Lectures onType Theory
Chapter 26
Chapter 26Optional

Corrected Inference for Simple Objects

Prerequisites. Direct starred prerequisites: chapter 4, chapter 13. No later core chapter depends on this route.

Consider W:=λf.λx.f(x with a=3)+f(null with a=3). The second call passes the singleton integer record to f. The first does so exactly when x is empty or consists of one a-field of arbitrary type: update overwrites that field, but it preserves every field at a label ba. One type scheme must therefore describe the empty row and another the singleton-row family. A claim that every typable term has one principal type scheme fails on this term.

This is the correction made by Wand’s 1988 corrigendum to the 1987 simple- object inference system. The repaired result is an effectively generated finite complete set of typings. It is not a principal-type theorem under an unnamed restriction. The later record-concatenation calculus and Rémy’s unitary record calculus will be named separately when they enter.

Rows remember presence

Fix a countable set of labels. A semantic row is a finite partial map from labels to types. Write r for the empty row and r[aτ] for right-biased update. Thus r[aτ](b)={τ,b=a,r(b),ba. The record type with row r is Π(r). Undefined fields are absent; Π(r) is not a product with a hidden arbitrary component.

Definition 26.1 — The corrected row-expression fragment

Type and row expressions are τ,υ::=αIntτυΠ(r),r,s::=ρrr[aτ],σ::=αρ.τ. Type variables α and row variables ρ are disjoint sorts. A substitution maps them to type and row expressions of the corresponding sort and acts homomorphically. Row expressions denote finite partial maps; two rows are equal when their denotations agree at every label. Quantification and instantiation preserve these sorts. A monotype is the empty-quantifier case of a scheme.

A finite family S={θ1,,θk} is a complete set of unifiers for equations E when every θi solves E, and every solution ϕ factors as ϕ=δθi on the variables of E, for some i and some substitution δ. A finite set of type schemes is complete for a term when every typing of the term is a substitution instance of a member of the set.

The distinction between one most general unifier and a finite complete set is load bearing. A unitary problem has one most general solution. A finitary problem may require several incomparable most general branches.

The typing constraints for the record primitives are read directly from their operations. With r fresh at each instantiation, their schemes are null:Π(r),().a:Π(r[aτ])τ,() with a=():Π(r)τΠ(r[aτ]). The row expression r expresses the independent presence information at every label. Update overwrites the a-field instead of equating its old and new types.

Lemma 26.2 — Constraint characterization

For the simply typed lambda calculus with integers, addition, null records, selection, and right-biased update, a substitution solves the syntax-directed constraints of a term exactly when it supplies a typing derivation for that term.

Proof of Lemma 26.2 — Constraint characterization

Proof. Induct on the term. Variables equate an occurrence variable with its binder variable. Application generates te1=te2te1e2, and abstraction generates tλx.e=txte; these are the ordinary Hindley–Milner cases. Integer addition equates both operands and the result with Int. The null, selection, and update cases instantiate the three displayed schemes. In the update case, the fresh input row r is unconstrained at a, while the result is r[aτ]; this is exactly right-biased overwrite. Reading each equation from a derivation gives the forward direction, and applying the corresponding typing rule to a solution gives the reverse direction. ◻

Exercise 26.1

★☆☆ Generate constraints for λr.(r with a=3).a. Solve them and identify the fresh row expression that remains unconstrained before the update.

The invalid decomposition

The original unification step treated update as an injective constructor: r1[aτ1]=r2[aτ2]r1=r2  τ1=τ2. The conclusion is too strong. Put r1=r,r2=r[aBool],τ1=τ2=Int. Both updated rows denote the singleton integer row, while r1r2. The update constructor is not injective in its row argument because the old a-field is immediately overwritten.

The repair determines which of the two input rows already contains a. There are four, not two, possibilities.

Lemma 26.3 — Four-way overwrite decomposition

The equation r1[aτ1]=r2[aτ2] holds exactly when τ1=τ2 and there exist a row r0, with no a-field, and the branch-local field types u1,u2 that occur in one of the following cases: r1r2both presentr0[au1]r0[au2]both absentr0r0right presentr0r0[au2]left presentr0[au1]r0. The four branches form a finite complete cover of the row-shape solutions of (26.3). When the table is used as a solver rule, each displayed ui and r0 is represented by a fresh metavariable; a solution substitution supplies the existential witnesses asserted here.

Proof of Lemma 26.3 — Four-way overwrite decomposition

Proof. Suppose (26.3) holds. At label a, the two updates return τ1 and τ2, so extensional equality gives τ1=τ2. Delete a from both input rows. At every ba, update leaves the old field unchanged; hence the two deletions are equal. Call their common row r0. Each original row either lacks a or has one field ui there, giving exactly the four table rows.

Conversely, in every table row the two inputs agree away from a. Updating both at a with the common type τ1=τ2 therefore makes the rows extensionally equal. Every semantic solution has one of the four presence patterns, so the branches cover all solutions; no two patterns can be merged without forgetting whether a field is present. ◻

Exercise 26.2

★☆☆ For each row of the table in lemma 26.3, choose concrete finite rows and calculate both sides of (26.3) at a and at one label ba.

The shared-variable termination case

Branching alone does not repair termination. If both sides contain the same row variable, repeated decomposition can reproduce a problem of the same size. The representative equation is ρ=ρ[a1τ1][anτn]. It does not ask for an infinite row. It states that ρ already contains the listed fields with the listed final types.

Definition 26.4 — Presence obligations

The obligation has(ρ,a,τ) requires the substitution for ρ to contain an a-field of type τ. The corrected solver handles (26.4) by recording has(ρ,ai,τi)(1in), after retaining only the last obligation for a repeated label. It does not substitute the right side for ρ.

Lemma 26.5 — Shared-variable equivalence

A row substitution solves (26.4) exactly when it satisfies the presence obligations of definition 26.4.

Proof of Lemma 26.5 — Shared-variable equivalence

Proof. Evaluate the update chain from left to right. At every listed label, the right side returns the type written by its last update. Equality with the left side therefore requires that ρ already return that type. At an unlisted label the update chain returns ρ’s own field, so equality is automatic. These are precisely the recorded obligations. ◻

Lemma 26.6 — Termination measure for the corrected row phase

Consider the row-specific phase whose steps are rigid-constructor decomposition, the four-way overwrite split, replacement of a shared-variable equation by canonical presence obligations, and discharge or rejection of one such obligation. Order its states lexicographically by the total number of constructors in active row equations and then by the number of unresolved atomic obligations. Every step in this phase strictly decreases the order.

Proof of Lemma 26.6 — Termination measure for the corrected row phase

Proof. Ordinary decomposition removes one active type or row constructor and adds only its proper subexpressions, so the first component decreases. A four-way overwrite split removes the two compared update heads before solving a branch, which also decreases the first component. The shared-variable case removes its active equation and adds only atomic presence obligations; atomic obligations contribute no constructors to the first component. Discharging or rejecting an atomic obligation leaves the first component fixed and decreases the second. No rule adds an active equation after entering the atomic phase. Lexicographic order on pairs of natural numbers is well founded, so the row-specific phase has no infinite branch. Ordinary variable elimination is outside this phase and uses the original unification algorithm’s halting measure. ◻

Exercise 26.3

★★☆ Show that direct substitution in ρ=ρ[aInt] does not decrease the number of row constructors. Then apply definition 26.4 and calculate the lexicographic measure before and after the special step.

The counterexample has two generators

Retain the full countable label universe of section 26.1. The program mentions only a, but that does not restrict substitutions for a row variable to rows over {a}. Put ra(τ)=r[aτ]. The two type schemes needed for (26.1) are σ0:=(Π(ra(Int))Int)Π(r)Int,σ1:=α.(Π(ra(Int))Int)Π(ra(α))Int. For σ0, x begins empty. For σ1, x begins with an a-field of arbitrary type. Both updates overwrite that field with an integer before f is called.

Proposition 26.7 — No principal scheme for W

In the corrected 1987 row system over its countable label universe, W has no single principal type scheme. The pair {σ0,σ1} is a finite complete set of schemes for W.

Proof of Proposition 26.7 — No principal scheme for W

Proof. Both displayed schemes type W by the update rule. They are incomparable: substitution for the type variable in σ1 cannot turn a present field into absence, while σ0 has no field type to instantiate.

For completeness, consider any typing of W. The two calls to f force the updated row of x to equal ra(Int), the type of the updated null record. At every ba, update preserves x’s old field while ra(Int) has none. Hence x has no field away from a. It is therefore either empty, giving an instance of σ0, or has the single field a:τ, giving an instance of σ1 at τ.

It remains to exclude one generator. A row expression whose instances include both the empty row and a row with an a-field cannot contain an explicit update: an update fixes at least its final label as present. It must expose a row variable ρ. But the label universe contains some ba, so the substitution ρr[bInt] is also an instance. For that input, the first call passes the two-field row {a:Int,b:Int} while the second passes only {a:Int}; the alleged scheme does not type W. Thus no valid scheme generates exactly both families, while the two displayed schemes are complete. ◻

The proposition is the smallest witness against the published principal- types claim. It is not a counterexample to decidable typability.

Theorem 26.8 — Corrigendum boundary: finite complete sets

For every program of Wand’s 1987 simple-object system, the corrected row-equation procedure effectively generates a finite complete set of type schemes. Consequently typability in that system is decidable. The theorem does not assert that the set has one member.

Proof of Theorem 26.8 — Corrigendum boundary: finite complete sets

Proof. This is the corrected result stated on page 132 of Wand’s one-page 1988 corrigendum [Wan88]: the published principal-type claim is withdrawn there, and the replacement is the effective finite complete set constructed by the corrected branching procedure. The imported algorithm replaces invalid decomposition (26.2) by the four cases of lemma 26.3, unions the unifier sets returned by those branches, and treats the shared-variable equations of definition 26.4 separately. By lemma 26.6, the row-specific repair introduces no new infinite branch. The corrigendum’s imported halting argument combines this special treatment with the original measure for the other unification steps. Each equation creates only finitely many branches. The resulting scheme family is finite, every returned scheme types the term, and every typing factors through one returned branch. Those are exactly the soundness and completeness clauses of definition 26.1. The source supplies no theorem selecting one branch as principal. ◻

A separately named successor calculus

Wand’s later record-concatenation calculus starts from a different operation. For semantic records r,r, right-biased concatenation is (rr)(a)={r(a),adom(r),r(a),adom(r). This section alone fixes a finite ordered label set L={a1,,an}. Its separately sorted field descriptors are d::=ϕabsentpresent(τ), where ϕ ranges over descriptor variables. A descriptor substitution maps ϕ only to descriptors. Write ΠL(d1,,dn) for the resulting finite-label record type; this notation is not the row constructor Π(r) of the corrected 1987 system. A concatenation occurrence MN generates fresh descriptor variables and the equations tM=ΠL(f1,,fn),tN=ΠL(g1,,gn),tMN=ΠL(h1,,hn). and, for each i, the disjunction (gi=present(τi)hi=gi)  (gi=absenthi=fi). The disjunction records the same right-bias as the dynamic operation.

Lemma 26.9 — Concatenation constraints are exact

A substitution satisfies (26.7) for every label exactly when its result row is the right-biased concatenation of its two input rows.

Proof of Lemma 26.9 — Concatenation constraints are exact

Proof. Fix ai. If the right row is present there, the first disjunct makes the result equal to that field. If it is absent, the second disjunct copies the left descriptor. These cases are exhaustive and disjoint. Applying the calculation at every label gives equality of finite rows; the reverse direction selects the disjunct dictated by the right row at each label. ◻

Expanding the positive Boolean combination of equations into disjunctive normal form yields finitely many conjunctions. Ordinary unification returns a most general unifier for every solvable conjunction. Their union is a finite complete set, not a single principal solution.

Theorem 26.10 — Finite complete sets for record concatenation

In Wand’s 1991 finite-label record-concatenation calculus, every closed term has an effectively generable finite set of type schemes whose substitution instances are exactly the term’s typings. Typability is decidable.

Proof of Theorem 26.10 — Finite complete sets for record concatenation

Proof. This is Wand’s finite-label theorem for record concatenation. Constraint characterization reduces typing to a positive Boolean formula. Its atoms are ordinary equations and the cases in (26.7). Finite L and finitely many syntax nodes give a finite formula. Expansion into disjunctive normal form gives finitely many unification problems. Unitary first-order unification supplies one most general unifier per solvable disjunct. By lemma 26.9, every returned branch is sound. Conversely, any typing selects one disjunct at each field, so it factors through a returned unifier. The returned set is empty exactly when the term is untypable. ◻

With an unbounded label set, the same paper replaces infinite enumeration by extension constraints and returns pairs consisting of a finite constraint set and a type scheme. That theorem belongs to the successor calculus; it is not a missing clause of the 1988 corrigendum.

Exercise 26.4

★★☆ Take L={a} and generate both disjuncts for λx.λy.(xy).a. Require the result field to be integer. Write the two resulting type schemes and show that neither is an instance of the other.

Rémy’s theorem is not the corrigendum

Rémy assigns every label a presence descriptor in a sorted equational algebra. For record extension and selection, without Wand’s right-biased concatenation operation and without the incompletely formalized recursive object extensions, that algebra has decidable unitary unification. The ordinary Hindley–Milner reduction then gives principal typings.

Thus three claims have three signatures:

system operation boundary inference result
Wand 1987 plus 1988 correction simple objects and overwrite rows finite complete sets; decidable typability
Wand 1991 right-biased record concatenation finite complete sets; decidable typability
Rémy 1991 sorted rows with extension and selection unitary unification and principal typings

The last row cannot be used to replace either of the first two. Adding recursive objects or concatenation changes the equation theory on which unitarity depends.

Exercise 26.5

★☆☆ Classify each claim as belonging to the corrected 1987 system, the 1991 concatenation system, or Rémy’s sorted record calculus: one most general row unifier; a four-way overwrite split; one disjunction per concatenated field; principal typing for extension and selection; finite complete type sets.

The chapter exports lemma 26.3, lemma 26.5, the exact imported result theorem 26.8, and the separately named successor theorem theorem 26.10. It exports no unrestricted principal-type theorem for simple objects and no theorem that transfers Rémy’s unitary result to recursive objects or record concatenation.

Sources.

The counterexample, four overwrite cases, shared-variable termination repair, and finite-complete-set correction are from [Wan88]; the invalid claim being corrected is in [Wan87]. The finite-label concatenation theorem and its exact disjunctive constraints are from [Wan91]. Rémy’s separate unitary boundary is from [Ré91].

Suggested first pass.

Problems exercise 26.6, exercise 26.7, and exercise 26.10.

Exercise 26.6

★☆☆ Derive both σ0 and σ1 for W. At each update, write the input and output descriptor of label a. Identify the step that would be rejected by the invalid decomposition (26.2).

Exercise 26.7

★★☆ Let E contain two overwrite equations at distinct labels a,b. Build the sixteen raw presence branches and give one concrete witness for each. Then add the equation that one selected old field is both absent and present, discard the resulting inconsistent branches, and show how every remaining semantic solution selects a branch. State why this is completeness of a set, not principality of one member.

Exercise 26.8

★★☆ Normalize ρ=ρ[aα][bβ][aγ] to presence obligations. Prove that the obligation for a mentions γ, not α, and that the equation is satisfiable.

Exercise 26.9

★★★ For L={a,b}, generate and solve the constraints for a function that concatenates two records and selects both result fields. List the satisfiable origin choices and compress their unifiers into a finite complete set of type schemes.

Exercise 26.10

★★★ Practical project.corrected-row-unifier Implement the finite one-label overwrite step, including all four presence branches and the shared-variable special case. Maintain the invariant that every returned branch denotes only solutions of the input equation and that every concrete solution selects a returned branch. Run the exact counterexample boundary from proposition 26.7 on a bounded two-label corpus: accept empty and a-only inputs for x, reject inputs that retain a foreign b-field, report that the naive decomposition rejects a valid row equation, and terminate on ρ=ρ[aInt]. Acceptance is the six named PASS lines and final summary in appendix E.

Search the book

Type to search the local edition.