Disjoint Intersections, Merge Elaboration, and Coherence
Prerequisites. Direct starred prerequisites: chapter 9. No later core chapter depends on this route.
The term 1,,2 offers two integers. If an integer is demanded, a type-directed compiler may select either component: (1,,2:𝖨𝗇𝗍)⇝𝜋1(1,2)or(1,,2:𝖨𝗇𝗍)⇝𝜋2(1,2). The target programs evaluate to 1 and 2. Type preservation alone does not choose between them. A coherent merge calculus must exclude this overlap and must also prevent ambiguity introduced indirectly by subtyping.
This chapter fixes the top-free variant of the calculus 𝜆𝑖 of Oliveira, Shi, and Alpuim. In particular, there is no 𝖳𝗈𝗉 type and no appeal to either of that paper’s two notions of top-like type. This choice is part of every statement below.
The source and its pair target
The running merge is 1,,(𝜆𝑥.𝑥:𝖨𝗇𝗍→𝖨𝗇𝗍). Its two components have incompatible outer forms, so an integer demand selects the left branch and a function demand selects the right branch.
Source types and terms are 𝐴,𝐵,𝐶::=𝖨𝗇𝗍∣𝐴→𝐵∣𝐴×𝐵∣𝐴&𝐵,𝑒::=𝑛∣𝑥∣𝜆𝑥.𝑒∣𝑒1𝑒2∣(𝑒1,𝑒2)∣𝜋𝑖𝑒∣𝑒1,,𝑒2∣(𝑒:𝐴),𝑖∈{1,2}. An ordinary type is a type whose outer constructor is not intersection: 𝖨𝗇𝗍𝗈𝗋𝖽𝗂𝗇𝖺𝗋𝗒,𝐴→𝐵𝗈𝗋𝖽𝗂𝗇𝖺𝗋𝗒,𝐴×𝐵𝗈𝗋𝖽𝗂𝗇𝖺𝗋𝗒. Contexts are finite lists of declarations with distinct variables.
The target is the simply typed call-by-value lambda calculus with integers and products: 𝑇,𝑈::=𝖨𝗇𝗍∣𝑇→𝑈∣𝑇×𝑈,𝑀,𝑁::=𝑛∣𝑥∣𝜆𝑥.𝑀∣𝑀𝑁∣(𝑀,𝑁)∣𝜋𝑖𝑀,𝑉,𝑊::=𝑛∣𝜆𝑥.𝑀∣(𝑉,𝑊),𝐸::=[]∣𝐸𝑀∣𝑉𝐸∣(𝐸,𝑀)∣(𝑉,𝐸)∣𝜋𝑖𝐸. Its typing rules are
𝑥:𝑇∈Γ
Γ⊢𝑥:𝑇
DT-Var
Γ⊢𝑛:𝖨𝗇𝗍
DT-Int
Γ,𝑥:𝑇⊢𝑀:𝑈
Γ⊢𝜆𝑥.𝑀:𝑇→𝑈
DT-Lam
Γ⊢𝑀:𝑇→𝑈Γ⊢𝑁:𝑇
Γ⊢𝑀𝑁:𝑈
DT-App
Γ⊢𝑀:𝑇Γ⊢𝑁:𝑈
Γ⊢(𝑀,𝑁):𝑇×𝑈
DT-Pair
Γ⊢𝑀:𝑇1×𝑇2
Γ⊢𝜋𝑖𝑀:𝑇𝑖
DT-Proj
Root contraction and compatible call-by-value reduction are (𝜆𝑥.𝑀)𝑉⇝0𝑀[𝑉/𝑥],𝜋𝑖(𝑉1,𝑉2)⇝0𝑉𝑖,𝑀⇝0𝑁𝐸[𝑀]⟼𝐸[𝑁]. Source types are erased to target types by |𝖨𝗇𝗍|=𝖨𝗇𝗍,|𝐴→𝐵|=|𝐴|→|𝐵|,|𝐴×𝐵|=|𝐴|×|𝐵|,|𝐴&𝐵|=|𝐴|×|𝐵|. Context erasure applies this operation to every declaration.
The target distinguishes a source product from a source intersection only through the elaboration that constructs it. A product is an explicit pair. An intersection is implemented by a pair whose projections are inserted by subtyping.
The judgment 𝐴<:𝐵⇝𝑐 means that source subtyping constructs a target coercion 𝑐:|𝐴|→|𝐵|. It is generated by
𝖨𝗇𝗍<:𝖨𝗇𝗍⇝𝜆𝑥.𝑥
S-Int
𝐵1<:𝐴1⇝𝑐1𝐴2<:𝐵2⇝𝑐2
𝐴1→𝐴2<:𝐵1→𝐵2⇝𝜆𝑓.𝜆𝑥.𝑐2(𝑓(𝑐1𝑥))
S-Arr
𝐴1<:𝐵1⇝𝑐1𝐴2<:𝐵2⇝𝑐2
𝐴1×𝐴2<:𝐵1×𝐵2⇝𝜆𝑝.(𝑐1(𝜋1𝑝),𝑐2(𝜋2𝑝))
S-Prod
𝐴<:𝐵1⇝𝑐1𝐴<:𝐵2⇝𝑐2
𝐴<:𝐵1&𝐵2⇝𝜆𝑥.(𝑐1𝑥,𝑐2𝑥)
S-&R
𝐴1<:𝐵⇝𝑐𝐵𝗈𝗋𝖽𝗂𝗇𝖺𝗋𝗒
𝐴1&𝐴2<:𝐵⇝𝜆𝑝.𝑐(𝜋1𝑝)
S-&L_1
𝐴2<:𝐵⇝𝑐𝐵𝗈𝗋𝖽𝗂𝗇𝖺𝗋𝗒
𝐴1&𝐴2<:𝐵⇝𝜆𝑝.𝑐(𝜋2𝑝)
S-&L_2
Write 𝐴<:𝐵 when some 𝑐 makes the elaborating judgment derivable. The ordinary-type premises force a right intersection to be decomposed by S-&R before either left projection can apply.
Worked calculations display target coercions in beta-eta normal form. The syntactic uniqueness and coherence statements compare the raw coercions generated by the rules before this presentation convention is applied.
For example, 𝖨𝗇𝗍&(𝖨𝗇𝗍→𝖨𝗇𝗍)<:𝖨𝗇𝗍⇝𝜆𝑝.𝜋1𝑝, whereas the function demand derives the coercion 𝜆𝑝.𝜋2𝑝. No search preference is involved.
Proof. Use rule induction. Rule S-Int is target identity. For S-Arr, the induction hypotheses type 𝑐1:|𝐵1|→|𝐴1| and 𝑐2:|𝐴2|→|𝐵2|; therefore the displayed eta-expansion maps a function 𝑓:|𝐴1|→|𝐴2| to one of type |𝐵1|→|𝐵2|. Rule S-Prod applies the two coercions to the corresponding projections. Rule S-&R pairs two results obtained from the same input. The two left rules project the indicated component before applying their induction-hypothesis coercion. These are all rules. ◻
Disjointness is absence of a common demand
Merely requiring 𝐴≮:𝐵 and 𝐵≮:𝐴 is too weak. The types 𝖨𝗇𝗍&(𝖨𝗇𝗍→𝖨𝗇𝗍)and𝖨𝗇𝗍&(𝖨𝗇𝗍×𝖨𝗇𝗍) are incomparable, but both can answer an integer demand. Merging values of these types would restore the original ambiguity one level down.
Two top-free types are disjoint, written 𝐴∗𝐵, when they have no common supertype: 𝐴∗𝐵:=¬∃𝐶.𝐴<:𝐶∧𝐵<:𝐶. The witness 𝐶 ranges over raw types generated by definition 21.1; it need not itself satisfy well-formedness. Source typing admits only well-formed types, but this unrestricted witness makes the structural decision theorem below complete. An ill-formed intersection may therefore witness a common function domain without becoming a type of a term. Well-formedness is structural for integers, arrows, and products. Its intersection clause is
Γ⊢𝐴𝗍𝗒𝗉𝖾Γ⊢𝐵𝗍𝗒𝗉𝖾𝐴∗𝐵
Γ⊢𝐴&𝐵𝗍𝗒𝗉𝖾
WF-&
The type grammar has no 𝖳𝗈𝗉; hence (21.1) does not require an exception for top-like common supertypes.
Write 𝐴∗𝖺𝐵 for the least symmetric relation generated by the following clauses.
An intersection distributes: 𝐴1∗𝖺𝐵𝐴2∗𝖺𝐵𝐴1&𝐴2∗𝖺𝐵,𝐴∗𝖺𝐵1𝐴∗𝖺𝐵2𝐴∗𝖺𝐵1&𝐵2.
Functions are disjoint exactly when their result types are: 𝐴2∗𝖺𝐵2𝐴1→𝐴2∗𝖺𝐵1→𝐵2.
Products are disjoint when either corresponding component is: 𝐴1∗𝖺𝐵1𝐴1×𝐴2∗𝖺𝐵1×𝐵2,𝐴2∗𝖺𝐵2𝐴1×𝐴2∗𝖺𝐵1×𝐵2.
Two nonintersection types with distinct outer constructors are algorithmically disjoint.
The procedure first distributes intersections, then compares the remaining outer constructors. In the product case it succeeds if either recursive call succeeds; in the function case it inspects only results.
Function domains do not decide disjointness. Although 𝖨𝗇𝗍∗(𝖨𝗇𝗍×𝖨𝗇𝗍), the functions 𝖨𝗇𝗍→𝖨𝗇𝗍and(𝖨𝗇𝗍×𝖨𝗇𝗍)→𝖨𝗇𝗍 share the supertype (𝖨𝗇𝗍&(𝖨𝗇𝗍×𝖨𝗇𝗍))→𝖨𝗇𝗍. Contravariance creates the common domain.
An ordinary leaf of a type is an ordinary type obtained by repeatedly choosing one component of an outer intersection. Every raw type has an ordinary leaf. For raw types 𝑋,𝐶, one has 𝑋<:𝐶 if and only if 𝑋<:𝐷 for every ordinary leaf 𝐷 of 𝐶.
Proof. Induct on 𝐶. If 𝐶 is ordinary, its only ordinary leaf is 𝐶, so both implications are identities. If 𝐶=𝐶1&𝐶2, its ordinary leaves are those of 𝐶1 together with those of 𝐶2. In the forward direction, inversion fixes the last rule as S-&R; its two premises and the induction hypotheses give subtyping to every leaf. In the reverse direction, the induction hypotheses give 𝑋<:𝐶1 and 𝑋<:𝐶2, and S-&R gives 𝑋<:𝐶. The same induction, choosing either component in the intersection case, proves existence of a leaf. ◻
Proof of Theorem 21.8 — Decision of simple disjointness
Proof. For soundness, induct on algorithmic disjointness and suppose that 𝐶 is a common supertype. Choose an ordinary leaf 𝐷 of 𝐶. By lemma 21.7, both inputs subtype 𝐷. In the function case, inversion fixes 𝐷 as an arrow and gives a result type common to both results. In either product case, inversion fixes 𝐷 as a product and gives common supertypes in both coordinates. Distinct ordinary outer constructors cannot both subtype 𝐷, because no subtyping rule changes an ordinary outer constructor.
For distribution through 𝐴1&𝐴2, the derivation 𝐴1&𝐴2<:𝐷 selects one component because 𝐷 is ordinary. That component and 𝐵 both subtype 𝐷, contradicting the corresponding premise 𝐴𝑖∗𝐵. Exchanging the two arguments of ∗ gives right distribution: semantic disjointness is symmetric by (21.1), and ∗𝖺 was defined as a symmetric relation.
For completeness, induct on the total size of the pair. Suppose first that 𝐴=𝐴1&𝐴2 and algorithmic disjointness fails. One component 𝐴𝑖 then fails against 𝐵. By the induction hypothesis, 𝐴𝑖 and 𝐵 have a common supertype 𝐶. For each ordinary leaf 𝐷 of 𝐶, lemma 21.7 gives 𝐴𝑖<:𝐷 and 𝐵<:𝐷. Rule S-&L𝑖 gives 𝐴1&𝐴2<:𝐷; rebuilding with lemma 21.7 makes 𝐶 a common supertype of 𝐴 and 𝐵. Exchanging the two inputs proves the case in which 𝐵 is an intersection.
It remains to compare ordinary heads. Equal integer heads have the common supertype 𝖨𝗇𝗍, so semantic disjointness excludes that case. Distinct ordinary heads use the axiom. Two arrows can fail the algorithm only when their results have a common supertype 𝐶; then (𝐴1&𝐵1)→𝐶 is a raw common supertype of the arrows, whether or not its domain intersection is well formed, contrary to semantic disjointness. Two products can fail only when both coordinate pairs have common supertypes 𝐶1,𝐶2; then 𝐶1×𝐶2 is common to the products. These cases exhaust the grammar. Every recursive call removes an outer constructor from at least one input, so it strictly reduces total pair size. ◻
★★☆ Run definition 21.6 on 𝖨𝗇𝗍and𝖨𝗇𝗍→𝖨𝗇𝗍,𝖨𝗇𝗍×𝖨𝗇𝗍and(𝖨𝗇𝗍→𝖨𝗇𝗍)×𝖨𝗇𝗍, and on two arrows with the same result. Give the common supertype in every rejected case.
Disjointness removes ambiguity between the components of a well-formed intersection. It does not choose the type of an unannotated application such as (𝗌𝗎𝖼𝖼,,𝗂𝖽)(3,,(𝜆𝑥.𝑥)). The program could demand an integer or a function at intermediate points. The source therefore synthesizes a unique type and requires annotations at the remaining choice points.
The running merge synthesizes 𝖨𝗇𝗍&(𝖨𝗇𝗍→𝖨𝗇𝗍): Γ⊢1⇒𝖨𝗇𝗍⇝1Γ⊢𝜆𝑥.𝑥⇐𝖨𝗇𝗍→𝖨𝗇𝗍⇝𝜆𝑥.𝑥Γ⊢(𝜆𝑥.𝑥:𝖨𝗇𝗍→𝖨𝗇𝗍)⇒𝖨𝗇𝗍→𝖨𝗇𝗍⇝𝜆𝑥.𝑥I−Ann𝖨𝗇𝗍∗(𝖨𝗇𝗍→𝖨𝗇𝗍)Γ⊢1,,(𝜆𝑥.𝑥:𝖨𝗇𝗍→𝖨𝗇𝗍)⇒𝖨𝗇𝗍&(𝖨𝗇𝗍→𝖨𝗇𝗍)⇝(1,𝜆𝑥.𝑥)I−Merge. Checking this term against 𝖨𝗇𝗍 appends the unique coercion 𝜆𝑝.𝜋1𝑝, so its target is (𝜆𝑝.𝜋1𝑝)(1,𝜆𝑥.𝑥).
Proof. Induct on the total number of constructors in 𝐴 and 𝐵. If 𝐵=𝐵1&𝐵2, the ordinary premises exclude both left rules, so both derivations end in S-&R; the induction hypotheses identify the two component coercions. If 𝐴=𝐴1&𝐴2 and 𝐵 is ordinary, the last rule is one of the two left rules. Both derivations cannot choose different projections by lemma 21.5; when they choose the same projection, the induction hypothesis identifies their premises. For equal ordinary heads, inversion fixes S-Int, S-Arr, or S-Prod, and the component induction hypotheses identify every subcoercion. Distinct ordinary heads have no derivation. Thus every possible last-rule pair has equal target syntax up to bound names. ◻
Proof. Induct on 𝑒. A variable has its unique context entry and an integer has type 𝖨𝗇𝗍. Pair and merge types are fixed by the two induction hypotheses. An annotation writes its result type. For application, the function induction hypothesis fixes the whole arrow and hence its codomain; checking the argument does not choose a result type. Projection is fixed by the synthesized product. A bare lambda has no synthesis rule. These cases cover the source grammar. ◻
Proof of Theorem 21.12 — Elaboration preserves types
Proof. Use rule induction on the combined bidirectional judgment. Variables, integers, pairs, applications, projections, and lambdas use the corresponding target typing rule and the induction hypotheses. Rule I-Merge creates a target pair of type |𝐴|×|𝐵|=|𝐴&𝐵|. Rule I-Ann uses its checking induction hypothesis. In I-Sub, lemma 21.3 gives 𝑐:|𝐴|→|𝐵|, so target application types 𝑐𝑀 at |𝐵|. ◻
Proof of Theorem 21.13 — Coherence of the frozen elaboration
Proof. Induct on the first derivation, proving both clauses together. Every synthesis rule is determined by the outer source term. Its mate must use the same rule, and the induction hypotheses identify its premise elaborations. In I-App, lemma 21.11 fixes the function’s arrow domain before the checking induction hypothesis is applied to the argument. In I-Merge, the source syntax fixes left and right order; disjointness is a proposition and contributes no target term.
For checking, a lambda must use I-Lam; alpha-rename both target binders to one fresh variable and apply the body induction hypothesis. A nonlambda must use I-Sub. Unique synthesis fixes its intermediate type, the synthesis induction hypothesis fixes its target term, and lemma 21.10 fixes the inserted coercion. Thus the two applications are alpha equal. No other last rules are possible. ◻
If ⋅⊢𝑒⇐𝐴⇝𝑀, then target evaluation of 𝑀 either continues or ends in a target value of type |𝐴|; it never gets stuck. Any other derivation of the same checking judgment produces the same target program up to alpha equality and hence the same value.
Proof of Corollary 21.14 — Safety and deterministic meaning
Proof. Target typing follows from theorem 21.12. For preservation of one target step, induct on its evaluation context. At a root step, the substitution lemma proves the beta case, and inversion of DT-Pair proves the two projection cases. Every context case rebuilds the same typing rule around the induction hypothesis.
For progress, induct on a closed target typing derivation. The variable case is impossible. Integers and lambdas are values. A pair either is a value or one of its components steps in the displayed left-to-right contexts. In an application, the function steps, the argument steps after the function is a value, or arrow canonical forms make the function a lambda and beta contraction applies. A projection first steps its scrutinee; when the scrutinee is a value, product canonical forms make it a pair and projection contraction applies. Iteration of these two lemmas gives target safety. If ⋅⊢𝑒⇐𝐴⇝𝑁 is another derivation, theorem 21.13 makes 𝑀 and 𝑁 alpha equal; deterministic call-by-value evaluation preserves alpha equality at every step. ◻
What the side conditions exclude
Drop the disjointness premises from both I-Merge and WF-&. Then ⋅⊢1,,2⇒𝖨𝗇𝗍&𝖨𝗇𝗍⇝(1,2). Both left subtyping rules apply when the result is checked against 𝖨𝗇𝗍: 𝜆𝑝.𝜋1𝑝and𝜆𝑝.𝜋2𝑝. Their applications to (1,2) evaluate to different integers. This counterexample removes only the two occurrences of the same disjointness condition; the syntax, target, evaluation order, and annotation discipline remain unchanged.
Annotations remove a different ambiguity. Even when both merges are disjoint, the source term ((𝜆𝑥.𝑥:𝖨𝗇𝗍→𝖨𝗇𝗍),,(𝜆𝑥.𝑥:(𝖨𝗇𝗍→𝖨𝗇𝗍)→(𝖨𝗇𝗍→𝖨𝗇𝗍)))(1,,(𝜆𝑥.𝑥:𝖨𝗇𝗍→𝖨𝗇𝗍)). does not synthesize: its function position has an intersection type rather than a unique arrow. Annotating that position with the intended arrow makes the demanded projection explicit. Disjointness chooses a component once a type is demanded; it does not infer which demand the programmer intended.
★☆☆ Write the two complete elaboration derivations for (1,,2:𝖨𝗇𝗍) after deleting the disjointness premises from I-Merge and WF-&. Reduce both target terms to values.
★★☆ Delete the ordinary-target premise from the two left subtyping rules. For a well-formed 𝐴&𝐵, construct a subtype query with an intersection target for which a left rule and S-&R produce different target coercions. Show that both coercions typecheck, and identify the first metatheorem in this chapter that fails.
★★☆ Assume 𝐴,𝐵,𝐶 are pairwise disjoint. Derive coercions from (𝐴&𝐵)&𝐶 and 𝐴&(𝐵&𝐶) to 𝐵. The target pair shapes differ; explain why theorem 21.13 does not identify programs whose source types are merely isomorphic.
The theorem boundary is now exact. It covers the top-free grammar, well-formed disjoint intersections, the displayed coercive subtyping rules, and the bidirectional annotation discipline. It does not cover unrestricted merges, union types, polymorphism, either top-like variant, or later disjoint-intersection calculi.
Sources.
The calculus, simple-disjointness definition, algorithmic characterization, pair elaboration, and coherence boundary follow [OSA16]. The authors study three variants; this chapter deliberately uses only their top-free one. The pinned paper artifact and later thesis in the local dossier are corroborating material, not sources for a stronger theorem.
★★☆ Prove directly from (21.1) that two arrow types are disjoint exactly when their result types are disjoint. In the reverse direction, use the intersection of the two domains to build a common domain.
★★☆ Construct incomparable types 𝐴,𝐵 with a common ordinary supertype 𝐶. Choose closed terms of types 𝐴,𝐵 whose coercions to 𝐶 produce different observations. This must refute the weaker test 𝐴≮:𝐵∧𝐵≮:𝐴, not merely repeat 𝖨𝗇𝗍&𝖨𝗇𝗍.
★★★ Reconstruct lemma 21.10 as a last-rule comparison table. For every pair of potentially overlapping rules, record whether the pair is excluded by ordinary targets, by lemma 21.5, or by outer-constructor inversion.
★★★Practical project.disjoint-merge-checker Implement the frozen type grammar, algorithmic disjointness, coercive subtyping, merge formation, and ordinary-target contributor selection. Maintain the invariant that an accepted ordinary demand has at most one contributing branch. On the named corpus, accept 𝖨𝗇𝗍&(𝖨𝗇𝗍→𝖨𝗇𝗍), reject 𝖨𝗇𝗍&𝖨𝗇𝗍, classify arrows by their results, and print the two distinct projections obtained by deliberately disabling the disjointness check. Include the nested-product demand 𝖨𝗇𝗍×(𝖨𝗇𝗍&(𝖨𝗇𝗍→𝖨𝗇𝗍))<:𝖨𝗇𝗍×𝖨𝗇𝗍, which must select the left branch of a disjoint merge with 𝖨𝗇𝗍→𝖨𝗇𝗍. Also test distribution through an intersection on each side of algorithmic disjointness. Acceptance is the eight named PASS lines and final summary recorded in appendix E.