    # Inhabitation for Non-idempotent Intersection Types

The inhabitation problem for intersection types in the lambda-calculus is known to be undecidable. We study the problem in the case of non-idempotent intersection, considering several type assignment systems, which characterize the solvable or the strongly normalizing lambda-terms. We prove the decidability of the inhabitation problem for all the systems considered, by providing sound and complete inhabitation algorithms for them.

## Authors

##### This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

## 2. Preliminaries

##### The λ-calculus

Terms and contexts of the -calculus are generated by the following grammars, respectively:

 t,u,v::=x∣λx.t∣tuC::=□∣λx.C∣Ct∣tC

where ranges over a countable set of variables. As usual, the application symbol associates to the left; to avoid ambiguities in the notation we may use parentheses as e.g. in . We use the notation for the identity function and for , which duplicates its argument. We write to denote the set of free variable of and for the syntactical equality on terms, modulo renaming of bound variables. The notation is used as an abbreviation for . We assume an hygiene condition on variables, i.e. free and bound variables have different names, as well as variables bound by different binders. Given a context and a term , denotes the term obtained by replacing the unique occurrence of in by , thus potentially allowing the capture of free variables of . A context is closing for if is closed, i.e. if .

The -reduction, denoted by , is the contextual closure of the rule:

 (λx.t)u→t{u/x}

where denotes the capture-free replacement of by in . A term of the form is called a -redex. We use to denote the reflexive and transitive closure of , and the transitive, reflexive and symmetric closure of .

##### Normal forms

A term is in -normal form, or just in normal form (nf), when it does not contain any -redex, it has normal form if it can be reduced to a term in normal form, it is strongly normalizing, written , if every -reduction sequence starting from it eventually stops. Terms in normal form () are generated by the following grammar:

 F::=λx.F∣HH::=x∣HF

The notion of head-normal form (hnf) is the syntactical counter part of the well-known notion of solvability for the -calculus, being a term solvable iff there is a closing context for of the shape (called a head-context) such that . A -term is in hnf if it is generated by the following grammar , it has hnf if it -reduces to a term which is in hnf.

 J::=λx.J∣KK::=x∣Kt

For example, is not in hnf but -reduces to which is in hnf, thus has a hnf. The term is solvable while is not.

##### Approximate normal forms

Approximate normal forms  are normal forms in an extended calculus. Let be the -calculus enriched with a constant , and let be the contextual closure of the -reduction plus the following two reduction rules:

 Ωt→Ωλx.Ω→Ω

Normal forms of with respect to are defined through the following grammar:

 a,b,c ::= Ω∣NN ::= λx.N∣LL ::= x∣La

Elements generated by the three grammar rules above are called approximate normal forms, approximate normal forms and approximate normal forms, respectively. E.g. both and are approximate normal forms, while and are not, since they reduce respectively to and .

##### Approximants of a term

Approximate normal forms can be ordered by the smallest contextual order such that , for all . By abuse of notation, we write to compare an approximate normal form with a term when is obtained from by replacing all the occurrences of by arbitrary terms. Let denote the least upper bound w.r.t. . We use the predicate to state that does exist.

The set of approximants of a term is given by:

 A(t)={a | ∃u t→∗βu and a≤u}

It is easy to check that, for every and , . Thus for example, and .

In many well-behaved models, it is possible to relate the interpretation of a term to the interpretation of its approximants, the first example being in . We are going to show such a property, known as the approximation theorem, for the type assignment system presented below.

## 3. Systems characterizing head normalization

In this section we consider the inhabitation problem with respect to an intersection type system for the -calculus that characterizes head-normal forms. The essential feature of this type system that makes its inhabitation problem decidable is the non-idempotency of the intersection. The typing rules are relevant (i.e. the weakening is not allowed), and we represent non-idempotent intersections as multisets of types. Moreover, in order to work with a syntax-directed system, we restrict the set of types to the strict ones .

1. The set of types is defined by the following grammar:

 σ,τ,ρ::=α∣A→τ(types)A,B::=[σi]i∈I(multiset types)

where ranges over a countable set of base types. Multiset types are associated to a finite, possibly empty, set of indices; the empty multiset corresponds to the case and is simply denoted by . To avoid an excessive number of parentheses, we use for example instead of .

2. Typing environments, which are also simply called environments, written , are functions from variables to multiset types, assigning the empty multiset to almost all the variables. We use the symbol to denote the empty typing environment. The domain of , written , is the set of variables whose image is different from . Given environments and , is the environment mapping to , where denotes multiset union; denotes its obvious extension to a non-binary case, where the resulting environment has empty domain in the case . We write for the environment assigning to , and acting as otherwise; is the environment assigning to , for , and to any other variable.

3. A typing judgement is a triple either of the form or . The type system is given in Figure 3.

Rules and are self explanatory. Rule can be considered as an auxiliary rule, i.e. it has no logical meaning but just collects together different typings for the same term; remark that it cannot be iterated. Rule has two premises, the one for (resp. ) is called the major (resp. minor) premise. In the case , this rule allows to type a term without giving types to all its subterms, and in particular it allows to type an application whose argument is unsolvable. For example, the judgement turns to be derivable by taking in rule :

 \prooftree\prooftree\justifiesx:[[]→α]⊢x:[]→α\using(var)\prooftree\justifies∅⊢DupDup:[]\using(m)\justifiesx:[[]→α]⊢x(DupDup):α\using(→E)

This feature is shared by all the intersection type systems characterizing solvability.

We write , or simply when is clear from the context, to denote a type derivation in system with conclusion . We call the subject of . A term is said to be -typable if there exists a derivation . By abuse of notation, we omit sometimes the name of the derivation by writing simply to denote the existence of some derivation with conclusion . We extend these notations to judgements of the shape , when we want to reason by induction on the structure of a proof.

One important tool is going to be used in the proofs: The measure of a derivation , written , is the number of rule applications in , except rule .

Note that the definition of the measure of a type derivation reflects the fact that is an auxiliary rule. The notion of measure of a derivation provides an original, combinatorial proof of the fact that typed terms do have hnf. In fact the fundamental subject reduction property holds as follows: if and , then , with the peculiarity that the measure of is strictly smaller than that of whenever the reduction takes place in a subterm of which is typed in . A formal definition of typed positions follows.

• The set of positions of is the set of contexts such that there exists a term verifying , being the subterm of at position .

• Given , the set of typed positions of in is defined by induction on the structure of as follows:

• if is an instance of the axiom.

• if the last rule of is , its subject is and its premise is .

• if the last rule of is , its subject is , and are the major and minor premises of respectively.

• if the last rule of is , and are its premises. A particular case is when , in which case . This coincides with the fact that terms typed by an empty rule are semantically untyped.

We say that the subterm of at position is typed in if is a typed position of in .

• Given (resp. ), we say that is in -normal form, written (resp. ), if for all , implies is not a redex.

Consider the following derivation , where :

 \prooftree\prooftree\prooftree\justifiesx:[σ]⊢x:σ\using(var)\prooftree\prooftree\justifiesy:[α0]⊢y:α0\using(var)\prooftree\justifiesy:[α1]⊢y:α1\using(var)\justifiesy:[α0,α1]⊢y:[α0,α1]\using(m)\justifiesx:[σ],y:[α0,α1]⊢xy:[]→τ\using(→E)\prooftree\justifies∅⊢IDup:[]\using(m)\justifiesx:[σ],y:[α0,α1]⊢xy(IDup):τ\using(→E)

Then,

• .

• .

• is in -normal form (since its only redex is untyped).

1. (Subject reduction and expansion) and imply .

2. (Characterization) is -typable if and only if has hnf.

###### Proof.

See [9, 7]. In particular, the proof of subject reduction is based on the weighted subject reduction property that and imply , where . Moreover, if the reduced redex is typed in , then . ∎

As a matter of fact, the two properties stated in the theorem above may be proved using a semantic shortcut: in  the class of essential type systems is introduced, and it is shown that such systems supply a logical description of the linear relational models of the -calculus, in the sense of . Since the type system is an instance of this class, both statements of Theorem 3 are particular cases of the results proved in .

### 3.1. The key role of approximants

System assigns types to terms without giving types to all their subterms (cf. the rule in case ). So in order to reconstruct all the possible subjects of derivations we need a notation for these untyped subterms, which is supplied by the notion of approximate normal forms introduced in Section 2. As a consequence, some notions previously defined for terms, naturally apply for approximants too. Thus for example, given or , is said to be in -normal form according to Definition 3. But remark that every typing derivation of an approximant is necessarily in normal form. More precisely, if or , then is in normal form. Quite surprisingly, , and do not imply that is in normal form, as the following example shows.

Let . Let , where and . There are type derivations (given on the top) and (given in the bottom) such that is in -normal form but is not in -normal form.

 \prooftree\prooftree\prooftree\justifiesx:[σ1]⊢x:σ1\prooftree\justifies∅⊢Ω:[]\justifiesx:[σ1]⊢xΩ:[α]→α\prooftree\prooftree\prooftree\prooftree\justifiesx:[σ2]⊢x:σ2Πy\justifiesx:[σ2],y:[α]⊢xy:[α]→αΠy\justifiesx:[σ2],y:[α,α]⊢xyy:α\justifiesx:[σ2],y:[α,α]⊢xyy:[α]\justifiesx:[σ1,σ2],y:[α,α]⊢xΩ(xyy):α
 \prooftree\prooftree\prooftree\justifiesx:[σ2]⊢x:σ2\prooftree\prooftree\prooftree\prooftree\justifiesz:[α]⊢z:α\justifies∅⊢I:[α]→αΠy\justifiesy:[α]⊢Iy:α\justifiesy:[α]⊢Iy:[α]\justifiesx:[σ2],y:[α]⊢x(Iy):[α]→α\prooftree\prooftree\prooftree\prooftree\justifiesx:[σ1]⊢x:σ1\prooftree\justifies∅⊢y:[]\justifiesx:[σ1]⊢xy:[α]→αΠy\justifiesx:[σ1],y:[α]⊢xyy:α\justifiesx:[σ1],y:[α]⊢xyy:[α]\justifiesx:[σ1,σ2],y:[α,α]⊢x(Iy)(xyy):α

where denotes the following subderivation

 \prooftree\prooftree\justifiesy:[α]⊢y:α\justifiesy:[α]⊢y:[α]

Given , where is in -normal form, we denote by the least approximate normal form of such that . Formally:

Given , the approximate normal form , called the approximant of , is defined by induction on the structure of as follows:

• If the last rule of is , then , and .

• If the last rule of is , then and follows from , then , being in -normal form.

• If the last rule of is , with premises and , so by induction and . Moreover the hypothesis that is in -normal form implies that is of the form , so that .

• If the last rule of is , then follows from , then . In fact , since , for all .

Remark that in the last case the approximate normal form corresponding to the case is . Coming back to the derivation in Example 3, we have . More generally, given , the approximant can be obtained from by replacing all its maximal subterms untyped in by .

Simple inductions on allow to show the following properties:

1. Let . If (resp. ) then there exists such that (resp. ) and .

2. If or , then there exists and .

Proposition 3.1.2 ensures the completeness of approximants, in the sense that all the typings having subject may also type some approximant of .

On the other hand, it is easy to see that if and is in -normal form, then . Summing up, we get a new proof of the approximation theorem for the relational -model associated to system , in the sense of :

The relational -model induced by system satisfies the approximation theorem, i.e. the interpretation of a term is the union of the interpretations of its approximants.

We are ready to prove that the set of approximate normal forms solving any given instance of the inhabitation problem is finite. This property allows us to design a complete inhabitation algorithm, i.e. one which supplies all solutions. First, we will prove that a derivation in normal form enjoys a sort of subformula property. Let us define the subtype relation as the transitive closure of the following one:

• is a subtype of .

• is a subtype of .

• and are subtypes of .

• is a subtype of for all .

Finally, is a subtype of if .

Let be an approximate normal form. Let . For every subderivation of of the shape , is either a subtype of or a subtype of . Moreover, if is an approximate normal form, then is a subtype of .

###### Proof.

The proof is by induction on . We do not consider the case of rule since the derivation ends in a type .

If ends with , then and , and the property is obviously true, being a subtype of itself. Moreover, is an approximate normal form, and is a subtype of , thus a subtype of .

If ends with , then , , and has the following shape:

 \inferΓ,x:A⊢a′:τΓ⊢λx.a′:A→τ (→I)

By the i.h. the property holds for the derivation with subject . Since both and are subtypes of , we conclude by transitivity of the subtype relation. The moreover statement does not apply to abstractions.

If ends with , then , where and are approximate normal forms, and is an approximate normal form. Then has the following shape:

 \prooftreeΓ⊢a0:A→τ\prooftree(Δi⊢a1:ρi)i∈I\justifiesΔ⊢a1:A\using(m)\justifiesΓ+Δ⊢a0a1:τ\using(→E)

where and . We have that is a subtype of , which in turn is a subtype of , and by the i.h.  is a subtype of , thus a subtype of . For any other subderivation we conclude by the i.h. and the fact that each implies . ∎

This lemma gives to system a quantitative flavour. In fact, define the degree of , written , to be the sum of the cardinalities of all the multisets in and . The following property holds. Let be an approximate normal form. Let . Then the number of typed positions of in which correspond to both bound and free variables (cf. Def. 3) is bounded by .

###### Proof.

Consider a (free or bound) variable of , and let be all the axioms with subject in . By the hygiene convention, is either free or bound in . If is free in , then , and since every axiom corresponds to a typed position of in , the proof is given. If is bound in , then there is a subderivation of ending with rule , with premise and conclusion . By the subformula property (Lemma 3.1), is a subformula of either or , so the cardinality of , which corresponds to the number of bound typed positions of in , is smaller or equal to . ∎

This property has a very important corollary. Given a pair , the number of approximate normal forms such that and is finite.

###### Proof.

By Property 3.1, if , then the number of typed positions of variables of in is bounded by . By definition of , is the only untyped subterm of , then gives an upper bound for the number of all positions of variables of . But the number of approximate normal forms with a bounded number of variable positions is finite, so the number of such approximate normal forms is finite too. ∎

### 3.2. The inhabitation algorithm

An instance of the inhabitation problem is a pair , and solving such an instance consists in deciding whether there exists a term such that is derivable.

We find convenient to present inhabitation algorithms as deductive systems, proving judgements of the form , whose intended meaning is that there exists a derivation such that . In this spirit, a run of the algorithm on the input is a proof of the judgement , for some approximate normal form , which is called the output of that run. Hence, running the algorithm on corresponds to searching a proof of , for some (unknown) . As usual, when writing we mean that there exists a proof of that judgement.

When facing a problem , in case , there exist two possibilities. The first one consists in guessing that the last rule used in a derivation solving the problem is , and trying to solve the problem for a fresh variable . The second one consists in guessing that the last rule is , choosing a head variable among those in the domain of having a type of the form (if any), and trying to construct the arguments using the resources still avaliable in .

This gives the algorithm in Figure 4. The two alternatives described above are implemented by rule and rule respectively. Once the head variable has been chosen, by , rule allows to construct its argument one by one, using rule . All the branches of any given run of the algorithm stop on a rule, or on a rule with , or fail (no rule applies).

More precisely, the algorithm uses three forms of judgements:

• meaning that the approximate normal form solves the inhabitation problem .

• , meaning that the approximate normal form solves the inhabitation problem .

• meaning that the approximate normal form , headed by a variable of type , solves the inhabitation problem .

We prove termination, soundness and completeness of the algorithm presented in Figure 4. Completeness, in particular, is obtained thanks to a non-deterministic behaviour: given an environment and a type , different runs can be chosen, each one constructing a judgement of the form . By collecting all such possible runs we recover all the approximate normal forms such that there exists a derivation , with .

Some comments on the rules of the algorithm are in order. Rule looks for an approximate normal form, when the input type is an arrow. Rule applies the approximation theorem 3.1; notice that in the particular case it gives , where denotes the environment having empty domain. Rule is self-explaining, rule is based on the property that, if an approximate normal form is an application , then must be an approximate normal form, and its type is a subtype of a type assigned to its head variable. Rule selects a head variable and launches the search for a suitable approximate normal form.

1. Let and . Given input , the algorithm succeeds with two different runs, generating respectively the following deduction trees:

(1.1)

(1.2)

Remark that the type does not correspond to the simple type , which represent the data types of Church numerals and so has an infinite number of inhabitants. In system there is no common type for all the Church numerals, since the numeral has type (among others, all of degree ).

2. Let and . Given input , a succesful run of the algorithm is:

3. Given input , where each is a base type, then the unique possible run consists in starting with rule , then rule , then the algorithm stops since no other rule can be applied. Then is empty.

It follows from Example 3.2.1 that the algorithm is not an obvious extension of the classical inhabitation algorithm for simple types [3, 16], and cannot be conservative with respect to it, since the two algorithms take input types belonging to different grammars.

In order to show that the inhabitation algorithm terminates, we define a measure on types and environments, as follows:

The measure is then extended to the judgements of the algorithm:

[Termination] The inhabitation algorithm for system terminates.

###### Proof.

Given input , every run of the algorithm is a deduction tree labelled with the rules of the algorithm (as in Example 3.2), where a node is a child of iff there exists an instance of a rule having as conclusion and as one of their premises. We associate to every rule application the measure of its conclusion, and we will prove that at every recursive call the measure decreases. The proof is by induction on the deduction tree.

If the last applied rule is , then the proof is trivial. If the last rule is or , then the proof is straightforward by application of the i.h. and the definition of the measure. So let us assume that the last rule is , with premise and conclusion . Then the measure of the premise is , while that of the conclusion is , which is strictly bigger. Let us assume that the last rule be , with premises and , and conclusion . The measure of the conclusion is , while the measures of the premises are respectively and . Also taking into account that either or can be empty, both the premises have a measure strictly smaller than then measure of the conclusion.

Hence any particular run of the algorithm terminates.

Since any instance (resp. or ) gives rise to a finite number of possible runs, as in Example 3.2, we conclude. ∎

The definition below gives the intended meaning of the three components of the inhabitation algorithm:

• ,

• ,

• .

Remark that