On sets of terms having a given intersection type

Working in a variant of the intersection type assignment system of Coppo, Dezani-Ciancaglini and Venneri [1981], we prove several facts about sets of terms having a given intersection type. Our main result is that every strongly normalizing term M admits a *uniqueness typing*, which is a pair $(\Gamma,A)$ such that 1) $\Gamma \vdash M : A$ 2) $\Gamma \vdash N : A \Longrightarrow M =_{\beta\eta} N$ We also discuss several presentations of intersection type algebras, and the corresponding choices of type assignment rules. Moreover, we show that the set of closed terms with a given type is uniformly separable, and, if infinite, forms an adequate numeral system. The proof of this fact uses an internal version of the B\"ohm-out technique, adapted to terms of a given intersection type.


Introduction
Since their introduction, intersection types have played a role of increasing prominence in programming languages research.From completeness of type assignment [BCD83], to characterization of strongly normalizing (and weakly normalizing) terms [DG03], to syntactic presentations of domain models [AC98], including graph models and filter models [Roc18], to classifications of certain classes of easy terms [CS12], to using types to count resources [KV17], and many others -the theory of intersection types is now a well-established research field of type theory.
Although enormously more expressive than simple types, intersection types enjoy most of the fundamental properties expected from type systems, including stability under substitution and reduction (Subject Reduction Theorem), decidability of type-checking, and some version of principal type theorem.
In the present paper, we prove that, among many intersection types a term may have, there will always be one for which the term is the only inhabitant, up to beta-eta equality.
The paper is organized as follows.First, we review the syntactic theory of intersection types.Next, we discuss the concept of intersection type algebras from several perspectives, with the goal of obtaining a unique representation of every intersection type.This leads us to the alternative formulation of intersection type assignment based on "essential intersection types" introduced by van Bakel [vB95].Here we also establish a technical lemma to be used in the proof of the main result.We then prove the uniqueness typing theorem in a sequence of progressively more general forms.In Section 6, we review notions of separability and adequate numeral systems.In the final section we prove that every intersection type is separable.

Intersection type assignment system
We work in the system CDV of intersection types without the top element ω.CDV was originally introduced by [CDV81] to obtain a type-theoretic characterization of solvable terms.This system has several variants in the literature.
The modern presentation [BDS13] treats intersection ∩ as a binary type constructor on par with the arrow type →.The set of types generated by these constructors is then imbued with a preorder relation that is used in the subsumption rule.This formulation is convenient for the construction of filter models.
In the original paper [CDV81], intersections and types belonged to different grammar sorts, which were defined by mutual recursion.This formulation is convenient when types are used for syntactic analysis of terms.
Yet another presentation, due to van Bakel [vB95], was introduced in his paper essential intersection types.Here the type assignment rules are restricted as far as possible to remove all redundancies.This system is most convenient for a proof-theoretic analysis of typability, and is the one we will make use of in the proof of our main result.
The equivalence of these systems is shown in [vB95].Rather than reproduce the proof here, we will give a brief review of these systems, which should make the relationship between them clear to the reader.A particular aspect to note is how the choice of type assignment rules relates to the presentation of the underlying intersection type theory.
2.1.Intersection as a type constructor.We begin with the formulation in [BDS13].
Let A be a countable set whose elements are called type atoms.The set of intersection types over A is given by the following grammar: The types are considered together with a preorder generated by the following axioms and rules.This preorder relation will be used in the rules of type assignment.
Let V be a countable set whose elements are called term variables.The set of lambda terms is generated by the grammar We write Ctx for the set of all contexts.
We define the ternary typing relation (− ⊢ − ∶ −) ⊆ Ctx × Λ × T by the following set of inference rules.These include the rules of the simply typed lambda calculus: together with two more rules treating intersection and subsumption: Note that the typability relation on terms is dependent on the type preorder ≤.Below, we will analyze several ways of generating this preorder.
2.2.Intersection type algebras.The following concept is called an "extended abstract type structure" in [AC98].
Definition 2.3.An intersection type preorder (itp) is a structure A = (P, ≤, ∩, ⇒), where (P, ≤) is a preorder, x ∩ y is a maximal lower bound of x and y, ⇒ is as above, and a ⇒ x ∩ y is a maximal lower bound of a ⇒ x and a ⇒ y for all x and y.
If (P, ≤, ∩, ⇒) is an intersection type preorder, then the equivalence relation x ∼ y ∶= x ≤ y and y ≤ x is a congruence with respect to ∩ and ⇒.The quotient P ∼ then has the structure of an intersection type algebra.
At the same time, all of the standard examples, including those below, will indeed be partial orders, with ≤ antisymmetric.For our purposes, it will therefore suffice to restrict attention to intersection type algebras.
Examples 2.4.(1) Let D be a λ-model, combinatory algebra, or a general applicative structure (magma).The powerset (D) carries the structure of an ita, where (2) Every Heyting Algebra is an ita, since Heyting implication is antimonotonic in its first argument, and monotonic and ∧-preserving in the second.(3) Every lattice-ordered group (AKA -group) G = (G, ≤, ∧, ∨, ⋅, e, (−) −1 ) is an ita, by taking the semilattice to be inherited from the order, and defining The distributive law follows since (4) The tropical semiring Z = (Z ∪ {∞}, min, +), with + the semiring product, is an ita.So is the -group Z[⃗ x] of semiring polynomials with variables in ⃗ x and coefficients in Z.
(5) The set of types T can be turned into an ita by taking the quotient of the itp (T, ≤, ∩, →) modulo the relation The algebra of types T = T ∼ is the free ita on the set A. Thus, every "type environment" ρ ∶ A → A, where A is an ita, extends uniquely to an ita homomorphism from T into A.
Moreover, this holds for any set of atoms A. For example, if A = {o}, then every type

Some presentations of free intersection type algebras
We will now review several ways that the free ita on a set of generators can be defined.This will enable us to eventually obtain a much more manageable set of representatives for the equivalence class of a type modulo the relation (2.1).
3.1.Inequational.The most obvious way to get the free ita on a given set A is to do what was just discussed at the end of last section: The carrier of the ita is T ∼, where ≤ is given by the rules of Definition 2.1, and ∼ is (2.1).This tautologically results in a free ita on the set A.

3.2.
Equational.Alternatively, we can make use of the fact that the concept of ita is completely algebraic.Using the equivalence the meet semilattice part of the definition can be captured by the rules of an idempotent commutative semigroup (ICS): The laws concerning → can also be expressed equationally: where the second law expresses anti-monotonicity of ≤ in the first argument, per (3.1).Thus, the free ita can be seen as the set of all terms built from A using the binary operations → and ∩, quotiented by the congruence generated by the equations above.
3.3.Rewriting-theoretic. Next, we could orient the above equations in an effort to obtain a convergent presentation.While some of the rules, especially commutativity, prevent this goal from being fully realized, rewriting theory can offer useful insights into the structure of free itas -including intersection types, see [Sta14].
Of particular interest is the operation of taking the normal form of a type A ∈ T with respect to the distributivity rule: Taking the dist-normal form (DNF) of A ∈ T results in a type expression that can be generated according to the two-phase grammar 3.4.Proof-theoretic.Assuming only the ICS axioms, T obtains the structure of a semilattice.This covers five of the seven rules in Definition 2.1.The subtyping order that results from adding the two remaining rules can be also characterized by the following conditions.A subtype occurrence inside a type expression is called positive if it occurs to the left of an arrow an even number of times, and negative otherwise.It is strictly positive if it never occurs to the left of an arrow.Now, A ≤ B in the free ita iff A ≤ B can be derived via the following axioms and rule: (1) A ≤ B if A = B according to the ICS rules. (2) By straightforward induction on derivations, we can show that the the free ita validates all of the above rules and that, conversely, postulating these rules to all type expressions built from → and ∩ results in an ita.
3.5.Set-theoretic.Finally, it is possible to "bake in" the laws of ICS/semilattice by using finite sets directly in our representation language.
Recall that the free semilattice on the set A is described by the finite powerset of A, where the union of two finite subsets defines the meet in the free semilattice.
Similarly, to construct a meet semilattice with a left-antitone, right-distributive binary operation ⇒, it suffices to interleave taking finite subsets with introducing new elements built with ⇒.Right-distributivity implies that, for any sets X and Y , we should have The elements of the free ita on the set A can therefore be represented by finitely branching trees, defined inductively by the following rule (the base case being obtained at k = 0): As we see, this definition naturally makes a distinction between finite subsets of A(A) -representing intersection types -and the elements themselves, representing arrow and atomic types.This distinction of course reflects the same situation that we encountered with distributivity normal forms in (3.4) and (3.5).
The partial order relation on A(A) can likewise be defined inductively, following the generation of the elements of A(A) themselves: Remark 3.1.The inductive rules generating elements of A(A) above do not yet give unique representatives with respect to the relation (∼) = (≤ ∩ ≥), because some elements x of a set X ⊆ f A(A) can be redundant, in the sense that x ≥ ⋂{y ∈ X y ≠ x}.In this case, we will have X ∼ X ′ , where X ′ = X − {x}.This will also produce elements X→α ∼ X ′ →α.
The expressions could be made completely canonical by removing redundant elements hereditarily from X and from all of its subexpressions.This can be done recursively, which therefore yields an effective procedure for computing the canonical representative of [A] ∼ for every intersection type A. However, we will not need this.
4. The essential intersection type assignment system 4.1.The original CDV type system.The two-layer grammar of types encountered in the previous section is in fact much closer in spirit to the grammar used in the original [CDV81] paper.Accordingly, the rules of type assignment in that system made a distinction between types and sets/intersections.The latter were called sequences, and were considered modulo permutations, which is an early version of the congruence (∼).
The original formulation made it possible to characterize solvable terms using intersection types.This system however is not the optimal choice for our purposes, and an even more minimal formulation has been proposed by van Bakel.4.2.van Bakel's Essential Intersection Types.The following type assignment system closely follows the system of [vB95], with minimal adjustments for consistency.
The system follows a two-layer grammar: We may also write A ∈ X to imply that A = A i for some i.
Every A ∈ T can be written as A = X 1 → ⋯ → X k → α, for some X i ∈ T ∩ and α ∈ A. This α is called the principal atom of A.
The preorder relation on T and T ∩ is defined inductively as in (3.6).This coincides with the usual preorder on intersection types; see [Sta14,(19)].Since every type A ∈ T can be seen as a singleton intersection ⋂{A} ∈ T ∩ , we freely mix both when using the ≤-symbol.This is consistent; for example, A ≤ B ⇐⇒ {A} ≤ {B}.
In particular, if Γ ∶ V ⇀ T ∩ is a context, and A ∈ T, then we write Γ(x) ≤ A if Γ(x) ≤ {A}.By the inductive rules (3.6), this means that B ≤ A for some B ∈ Γ(x).
The essential type assignment system is defined by the following rules.
Among the most attractive features of this systems are: • All types are in distributive normal form.
• The subsumption rule is restricted to variables.
While these are serious restrictions, they do not change the set of typable terms: Proof.See Theorems 4.3-4.5 in [vB95] and the remark that follows.
Corollary 4.2.The full subsumption rule is admissible: The following theorem is probably the most important fact about CDV.We will often make use of it tacitly throughout the rest of the paper.For a proof, see [BDS13, 17.2.15(iii)].
Theorem 4.3.Γ ⊢ M ∶ A for some Γ, A if and only if M is strongly normalizing.
Since the system is completely syntax-directed, the following lemma is also immediate.(For an exact proof, see [BDS13, 14.1.9].) Lemma 4.6 (Thinning).Let Γ be a context, A, B ∈ T. Let M be a beta normal form.
Suppose that the principal atom of B occurs neither in Γ nor in A. Then Recall that the set of beta normal forms can be generated by the following grammar (which is obtained by excluding the redex pattern (λx.)◻ from the language of λ-terms): We proceed by induction on the generation of M according to this grammar.M = xM 1 ⋯M k : By applying the Inversion Lemma k times, we find {X i } 1≤i≤k so that But since the principal atom of B does not occur in A, case analysis on the inductive rule for ≤ implies that we must actually have Γ Moreover, we see that X i occurs in Γ and hence B i,j occurs in Γ, for all i and j.By induction hypothesis, we thus also have By the application rule, Γ ⊢ x ⃗ M ∶ A. M = λx.M ′ : By inversion, we must have A = X → C, where Since X and C are both subexpressions of A, the principal atom of B does not occur in them either.
So the induction hypothesis applies directly, and we get Corollary 4.7.Let Γ and ∆ be contexts and suppose that Γ ⊎ ∆ ⊢ M ∶ A.
If the principal type atoms of ∆ occur neither in Γ nor in A, then Γ ⊢ M ∶ A.
Proof.By induction on ∆, using the previous lemma.

Uniqueness Typing
Definition 5.1.Let M ∈ Λ.A uniqueness typing for M is a pair In this section, we will show that every strongly normalizing term admits a uniqueness typing in CDV.
(2) N β is the set of all β-normal forms.
(3) N βη is the set of all βη-normal forms.(4) SN is the set of all strongly normalizing terms.
We will now establish the following progression of claims.
Proposition 5.3.For every M ∈ N β there exists a context Γ and an intersection type Corollary 5.4.For every M ∈ N β there exists a context Γ and an intersection type Theorem 5.5.For every M ∈ SN there exists a context Γ and an intersection type Proof of Proposition 5.3.Let M be given.
For every subterm N of M , let α N be a fresh type atom.We shall again make use of the following grammar for beta normal forms: We proceed by induction on the generation of M according to this grammar.
Case 1. M = xM 1 ⋯M k , k ≥ 0: (This includes the base case M = x.)By induction hypothesis, there exist Γ 1 , . . ., Let α be the unique type atom associated to the current subterm.Put We consider the possible shapes of N according to (5.1).If N = λy.N ′ , then applying the inversion lemma to Γ ⊢ N ∶ A yields that A must be a function type X → A ′ , contradicting that A = α is an atom.
Thus N is an application: N = yN 1 ⋯N l .Applying inversion to Γ ⊢ N ∶ A a sufficient number of times now yields that However, the only element in the context Γ which contains the atom A = α is By applying Lemma 4.6 to this judgment with y ∶ B being x ∶ A 1 → ⋅ ⋅ ⋅ → A k → α, followed by Corollary 4.7 with ∆ being ⊎ j≠i Γ j , we find Γ i ⊢ N i ∶ A i .By induction hypothesis, we have x occurs in M ′ , then by the free variable lemma, Γ ′ (x) is defined.
(4) Recursively check whether M i is convertible with N i .Notice, however, that the term N is not presented to us with a code; rather, the steps above must be applied to a pure variable ν ∈ V, such that, in the event that ν gets replaced with an actual N ∈ Λ(Γ, A), we will have ∆⌜M ⌝ν[ν ∶= N ] equal K if M = N , and F otherwise.
Let us now elaborate the above steps.By Theorem 4.3, there is no loss of generality in assuming that M, N ∈ N β .
(1) To check whether the head variable of N is v, we apply ν to a sequence of terms ⃗ X.Each X i , once substituted for a variable at a particular position, will capture all possible arguments of x k+i , and will produce a tuple from which the index ⌜k + i⌝ = c k+i of x k+i can be easily extracted.
Those x i with i ≤ k will have already been substituted by such terms; their occurrence in N will therefore allow ⌜i⌝ to be extracted effectively as well.
(2) Once v is confirmed as the head variable, we will need to compare the Böhm rank of M and N , namely, whether the difference in their lambda prefix matches the difference in the number of terms applied to the head variable.Since both terms were assumed to be typable, there are finitely many possibilities for the arity of N , and a separator can be effectively constructed to separate M from N based on whether their arity is different.
(3) If the previous two steps are successful, the algorithm will implicitly η-expand M to match the arity of A exactly.Specifically, by Inversion Lemma, from Γ ⊢ M ∶ A we conclude that the length of the lambda prefix of M is bounded by the arity of A, so that l ≤ a(A).
This allows us to replace M with its eta-expansion without affecting typability: (Note that this eta expansion of M does yet result in M and N having the same lambda prefix.The issue is that, the head variable v of N is to be replaced by a tupler that is designed to capture the maximum number of arguments that v could possibly have, a(v), while M might be using a declaration for v with a lower arity.)(4) Nevertheless, the terms X i that are (were) substituted for x i in N allow easy access to the children N 1 , . . ., N m ′ -or rather, to their substitution instances N σ j , where We will therefore be able to extract these instances and recursively invoke the algorithm on M j and N σ j .Notice that the above procedure is effective in Γ, A, and M .It remains to argue why this procedure is guaranteed to terminate.Notation 7.1.Let M ∈ N β , A ∈ T. The heights of M and A are defined recursively by The construction is proved correct by induction on the pair ( M , A ), ordered lexicographically.Notice that every recursive call decrements the height of M , until a variable is reached.After that, every recursive call decrements the height of the type A until an atom is reached.At that point, the recursion stops.
More precisely, once the algorithm reaches a variable in M , there are only a finite number of levels that need to be checked to determine whether N is an eta-expansion of M .This is due to the following observation.
Lemma 7.2.Let X ↠ η x be an eta expansion of x that is in beta normal form.
Proof.That X ≤ A is a straightforward induction on X.
Base case: By applying the Inversion Lemma k times, we find that Applying inversion k times more, we find Finally, before spelling out the above procedure explicitly, let us immediately point out what is at once a simplification and a generalization of it.
Instead of assuming that N ∈ Λ(Γ, A), the only hypothesis we actually need about N is that N ∈ Λ(Γ, B) for some B, and the arities of all subterms of N are uniformly bounded by a constant.This observation means that the local separator for M actually works on a bigger domain than Λ(Γ, A). (However, it is uniform only on that domain.) With this insight, we can modify the Böhm-out proof above to always use tuples of the same length.

Construction of ∆.
The term ∆ will be defined in terms of a number of auxiliary functions, the most important of which recurses through the syntactic tree of ⌜M ⌝ carrying along the context Γ to keep track of the free variables as they are substituted into the second term N .
Some of the following combinators are defined by the specification they must satisfy.In all cases, the specifications are met by simple functional programs, easily implemented in a language like Haskell.By Church's Thesis, these terms are all λ-definable.
For example, the first two terms are actually ternary functions, whose implicit first argument takes as input a Church numeral c n = ⌜n⌝.They can be defined by Iter = I, Apps = λnf x.n⟨x⟩f ; then Iter n = Iter⌜n⌝ and Apps n = Apps⌜n⌝.
Iter n F X = F n (X) For the next part of the definition, we will fix a bound b ∈ N representing the maximum arity of any type subexpression in A, and therefore also the maximum length of an abstraction sequence or application sequence inside an inhabitant of A.
So let b ∈ N be fixed.
The following combinators will help us compute the Böhm rank of a term N without looking at its code.The Böhm rank is the quantity n − l, where N = λx 1 . . .x l .yN 1 ⋯N n .
The term X i will be the tupler substituted for the context variable x i in N ; it will contain ⌜i⌝ as the first element of the tuple, so that we can easily extract the index of the variable that created it, allowing us to compare head variables of M and N .