Observability = Typability + Inhabitation

12/14/2018 ∙ by Antonio Bucciarelli, et al. ∙ Università di Torino 0

We define an observability property for a calculus with pattern matching which is inspired by the notion of solvability for the lambda-calculus. We prove that observability can be characterized by means of typability and inhabitation in an intersection type system P based on non-idempotent types. We show first that the system P characterizes the set of terms having canonical form, i.e. that a term is typable if and only if it reduces to a canonical form. But the set of observable terms is properly contained in the set of canonical. Thus, typability alone is not sufficient to characterize observability, in contrast to the solvability case for lambda-calculus. We then prove that typability, together with inhabitation, provides a full characterization of observability, in the sense that a term is observable if and only if it is typable and the types of all its arguments are inhabited. We complete the picture by providing an algorithm for the inhabitation problem of P.



There are no comments yet.


page 1

page 2

page 3

page 4

This week in AI

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

1 Introduction

In these last years there has been a growing interest in pattern -calculi [22, 17, 11, 18, 16, 21] which are used to model the pattern-matching primitives of functional programming languages (e.g. OCAML, ML, Haskell) and proof assistants (e.g. Coq, Isabelle). These calculi are extensions of -calculus: abstractions are written as , where is a pattern specifying the expected structure of the argument. In this paper we restrict our attention to pair patterns, which are expressive enough to illustrate the challenging notion of solvability/observability in the framework of pattern -calculi.

We define a pattern calculus with explicit pattern-matching called -calculus. The use of explicit pattern-matching becomes very appropriate to implement different evaluation strategies, thus giving rise to different languages with pattern-matching [11, 12, 1]. In all of them, an application reduces to , where the constructor is an explicit matching, defined by means of suitable reduction rules, which are used to decide if the argument matches the pattern . If the matching is possible, the evaluation proceeds by computing a substitution which is applied to the body . Otherwise, two cases may arise: either a successful matching is not possible at all, and then the term reduces to a failure, denoted by the constant , or pattern matching could potentially become possible after the application of some pertinent substitution to the argument , in which case the reduction is simply blocked. For example, reducing leads to a failure, while reducing leads to a blocking.

We aim to study observability of the -calculus, which corresponds to solvability of -calculus. Let us first recall this last notion: a closed -term is solvable if there is and there are terms such that reduces to the identity. Closed solvable terms represent meaningful programs: if is closed and solvable, then can produce any desired result when applied to a suitable sequence of arguments. The relation between solvability and meaningfulness is also evident in the semantics: it is sound to equate all unsolvable terms, as in Scott’s original model  [23]. This notion can be easily extended to open terms, through the notion of head-context, which does the job of both closing the term and then applying it to an appropriate sequence of arguments. Thus a term is solvable if there is a head-context such that, when is filled by , then is closed and reduces to the identity.

In order to extend the notion of solvability to the pair pattern -calculus, it is clear that pairs have to be taken into account. A relevant question is whether a pair should be considered as meaningful. At least two choices are possible: a lazy semantics considering any pair to be meaningful, or a strict one requiring both of its components to be meaningful. In the operational semantics we supply for the constant is different from : if a term reduces to we do not have any information about its result, but if it reduces to we know at least that it represents a pair. In fact, being a pair is already an observable property, which in particular is sufficient to unblock an explicit matching, independently from the observability of its components. As a consequence, a term is defined to be observable iff there exists a head-context such that is closed and reduces to a pair. Thus for example, the term is always observable, also in case is not observable. Observability turns out to be conservative with respect to the notion of solvability for the -calculus (see Theorem 5).

In this paper we characterize observability for the -calculus through two different and complementary notions related to a type assignment system with non-idempotent intersection types that we call . The first one is typability, that gives the possibility to construct a typing derivation for a given term, and the second one is inhabitation, which gives the possibility to construct a term from a given typing. More precisely, we first supply a notion of canonical form such that reducing a term to some canonical form is a necessary but not a sufficient condition for being observable. In fact, canonical forms may contain blocking explicit matchings, so that we need to guess whether or not there exists a substitution being able to simultaneously unblock all these blocked forms. Our type system characterizes canonical forms: a term has a canonical form if and only if it is typable in system (Theorem 3.2). Types are of the shape , for , where are multisets of types and is a type. The use of multisets to represent the non-idempotent intersection is standard, namely is just a notation for . By using type system we can supply the following characterization of observability: a closed term is observable if and only if is typable in system , let say with a type of the shape (where is a product type), and for all there is a term such that every type in is inhabited by . In fact, if inhabits all the types in , then , resulting from plugging into the head context , reduces to a pair. The extension of this notion to open terms is obtained by suitably adapting the notion of head context.

Clearly, the property of being observable is undecidable, exactly as the solvability property for -calculus. More precisely, the property of having canonical form is undecidable, since the -terms that are typable in system , characterizing terms having canonical form, are exactly the solvable ones. But our characterization of observability through the inhabitation property of does not add a further level of undecidability: in fact we prove that inhabitation for system is decidable, by designing a sound and complete inhabitation algorithm for it. The inhabitation algorithm presented here is a non trivial extension of the one given in [8, 9] for the -calculus, the difficulty of the extension being due to the explicit pattern matching and to the use of structural information of patterns in the typing rules, which is the one provided by the form/structure of the patterns.

Relation with -calculus

Let us recall the existing characterizations of solvability for the -calculus:


reduces to the identity for an appropriate head-context ;


has a head-normal form;


can be typed in a suitable intersection type system.

Statement (1) is the definition of solvability, Statement (2) (resp. (3)) is known as the syntactical (resp. logical) characterization of solvability. The syntactical characterization, i.e. (2) (1) has been proved in an untyped setting using the standardization theorem (see [4]). The logical characterization, i.e. (3) (1), uses the syntactical one: it is performed by building an intersection type assignment system characterizing terms having head-normal form (see for example [14]). Then the implications (3) (2) corresponds to the soundness of the type system (proved by means of a subject reduction property), while (2) (3) states its completeness (proved by subject expansion).

Traditional systems in the literature characterizing solvability for -calculus are [5, 19], where intersection is idempotent. Exactly the same results hold for non-idempotent intersection types, for example for the type system  [13, 8], which is a restriction of to -terms.

How does the particular case of the lambda calculus fit in the “observability = typability + inhabitation” frame? We address this issue in the following digression. Let be a type which is peculiar to some set of “observable” terms, in the sense that any closed term of type reduces to an observable (in the present work, the observables are all the pairs, in the case of the lambda calculus, the unique observable is the identity) . Then a type of the form may be viewed as a certificate, establishing that, by applying a closed term to a sequence of closed arguments , one gets a term that reduces to an observable. This is summarized by the slogan “observability = typability + inhabitation”. In the case of the call-by-name lambda calculus, however, typability alone already guarantees observability. The mismatch is only apparent, though: any closed, head-normal term of the lambda calculus may be assigned a type of the form where all the ’s are empty except the one corresponding to the head variable, which is of the shape (). The problems of finding inhabitants of the empty type and of are both trivial. Hence, “observability = typability + inhabitation” does hold for the lambda-calculus, too, but the “inhabitation” part is trivial in that particular case. This is due, of course, to the fact that the head-normalizable terms of the lambda calculus coincide with both the solvable terms and the typable ones.

But in other settings, a term may be both typable and non observable, the types of (some of) its arguments being non-inhabited. Theorem 5 provides such an example for .

Related work

This work is an expanded and revised version of [10]. The main novelties are:

  • The reduction relation on is smaller. In particular, a reduction rule allowing to unfold nested explicit substitutions of the form has been removed.

  • Accordingly, the current version of the type system is much simpler, since ensuring subject reduction for the removed rule was particularly troublesome. In particular, the use of idempotent/persistent information on the structure of patterns was needed.

  • The rationale behind the removed rule was that our inhabitation algorithm only produces normal forms without nested substitutions. We leave the inhabitation algorithm unchanged, and we circumvent the problem by showing that, whenever a (normal) term inhabits a given type, then there exists a (normal) term without nested substitutions inhabiting the same type (Lemma 4).

Non-idempotent intersection types are also used in [7] to derive strong normalization of a call-by-name calculus with constructors, pattern matching and fixpoints. A similar result can be found in [3], where the completeness proof of the (strong) call-by-need strategy in [2] is extended to the case of constructors.

Organization of the paper.

Section 2 introduces the pattern calculus and its main properties. Section 3 presents the type system and proves a characterization of terms having canonical forms by means of typability. Section 4 presents a sound and complete algorithm for the inhabitation problem associated with our typing system. Section 5 shows a complete characterization of observability using the inhabitation result and the typability notion. Finally, Section 6 concludes by discussing some future work.

2 The Pair Pattern Calculus

We now introduce the -calculus, a generalization of the -calculus where abstraction is extended to patterns and terms to pairs. Pattern matching is specified by means of an explicit operation. Reduction is performed only if the argument matches the abstracted pattern. Terms and contexts of the -calculus are defined by means of the following grammars:

where range over a countable set of variables, and every pattern is linear, i.e. every variable appears at most once in . We denote by the identity function and by the auto applicative function . As usual we use the abbreviation for , , . Remark that every -term is a -term.

The operator is called an explicit matching. The constant denotes the failure of the matching operation. Free and bound variables of terms are defined as expected, in particular and . A term is closed if . We write iff . As usual, terms are considered modulo -conversion. Given a context and a term , denotes the term obtained by replacing the unique occurrence of in by , thus possibly capturing some free variables of .

The reduction relation of the -calculus, denoted by , is the contextual closure of the following rewriting rules:

where denotes the substitution of all the free occurrences of in by . By -conversion, and without loss of generality, no reduction rule captures free variables. Thus for example, in rule , the bound and free variables of the term are supposed to be disjoint, so that the variables of , which are bound in , cannot be free in . The reflexive and transitive closure of is written .

The rule triggers the pattern operation while rule performs substitution, rules and implement (successful or unsuccessful) pattern matching. Rule prevents bad applications and rules deal with propagation of failure. Rules pushes head explicit matchings out, and may seem unnecessary. Indeed the calculus would be also confluent without it, but it is particularly useful for the design of the inhabitation algorithm (see Section 4).


The reduction relation is confluent.

The proof is given in the next subsection.

Normal forms are terms without occurrences of redexes; they are formally defined by the following grammar:


A term is an -normal form if and only if is a -normal form, i.e. if no rewriting rule is applicable to a subterm of .

We define a term to be normalizing if it reduces to a normal form.

Let us notice that in a language like , where there is an explicit notion of failure, normalizing terms are not interesting from a computation point of view, since is a normal form, but cannot be considered as the result of a computation. If we want to formalize the notion of programs yielding a result, terms reducing to cannot be taken into consideration. Remark however that is not operationally equivalent to , according to the idea that a pair can be always observed, and so it can be considered as a result of a computation. This suggests a notion of reduction which is lazy w.r.t. pairs, i.e. that never reduces inside pairs.

Therefore, a syntactical class of terms which is particularly interesting from an operational point of view is that of canonical forms. Canonical forms (resp. pure canonical forms ) can be formalized by the following grammar:

where the notion of pure canonical form, i.e. of canonical form without nested matchings, is a technical notion that will be useful in the sequel. A term is in canonical form (or it is canonical), written , if it is generated by , and it has a canonical form if it reduces to a term in . Note that -canonical forms cannot be closed. Also, remark that the of a term is not unique, e.g. both and are s of . It is worth noticing that but neither nor . We will prove that observable terms are strictly contained in the canonical ones.


  • The term is both in normal and canonical form.

  • The term is in normal form, but not in canonical form.

  • The term is in canonical form, but not in normal form.

  • The term is in canonical form, but not in pure canonical form.

  • The term is in pure canonical form.

  • The term is not in canonical form.

  • The term is in pure canonical form.

We end this section by stating a lemma about -reduction that will be useful in next Section.


Every infinite -reduction sequence contains an infinite number of -reduction steps.

It is sufficient to show that the reduction system without the rule , that we call , is terminating. Indeed, let denote a (possibly empty) list of explicit matchings. Then implies , where is the -uple whose first component is the number of applications of the form in (rules ), the second one is the sum of the sizes of patterns in (rules ), and the third one is the sum of the sizes of subterms occurring to the right of explicit matching operators. These tuples are ordered lexicographically.

2.1 The Confluence Proof

In order to show confluence of our reduction system we first simplify the system by erasing just one rule in such a way that confluence of holds if confluence of deprived from holds. This last statement is proved by applying the decreasing diagram technique [24]. We just change the name/order of the rules to make easier the application of the decreasing technique.

We define . We write iff or .


For all , if and , then there exists s.t. and .

By induction on , we only show the most significant cases:

The following lemma can be found for example in [4].


Let and be two reduction relations. Suppose for any such that and , there exists verifying and . Then and commute, i.e.  if and , s.t. and .

By Lemma 2.1 and 2.1 we obtain.


The reduction relations and commute.


The reduction relation is confluent.

We use the decreasing diagram technique [24]. For that, we first order the reduction rules of the system by letting iff . We write if . Given a set of natural numbers we write if every -reduction step in the sequence verifies , i.e. if for every -reduction step in the sequence such that . The system is said to be decreasing iff for any such that and , there exists such that and .

We now show that the system is decreasing.

  • We consider the cases and . We only show the interesting ones.

    All of them verify the decreasingness property.

  • The cases and are straightforward.

  • We consider the cases and . We only show the interesting ones.

    All of them verify the decreasingness property.

  • The interesting case and is the following.

    All of them verify the decreasingness property.

  • All the remaining cases are straightforward.

Lemma (Hindley-Rosen)

Let and be two confluent reduction relations which commute. Then is confluent.

Lemma 2. The reduction relation is confluent.

Since is trivially confluent, is confluent by Lemma 2.1 and and commute by Corollary 2.1, then turns out to be confluent by Lemma 2.1, which concludes the proof.

3 The Type System

In this section we present a type system for the -calculus, and we show that it characterizes terms having canonical form, i.e. a term is typable if and only if it has canonical form. The set of types is generated by the following grammar:

where is a (possibly empty) finite set of indices, and a multiset is an unordered list of (not necessarily different) elements. The arrow type constructor is right associative. We consider a unique type constant , which can be assigned to any pair.

We write to denote multiset union (i.e. taking multiplicities into account), and to denote multiset membership. The product operation on multisets is defined as follows:

The first line of the product operation corresponds to the (structural) type of a pair whose components are untyped (see Figure 1 for details).

Remark that , the multiset inclusion being strict for example in the following case: .

Typing environments, written , are functions from variables to multiset types, assigning the empty multiset to almost all the variables. The domain of , written , is the set of variables whose image is different from . We may write iff .


Given typing environments , we write for the environment which maps to . If , the resulting environment is the one having an empty domain. Note that and are just particular cases of the previous general definition. When we may write instead of . The notation is used for the environment whose domain is , defined as expected; is the environment assigning to , for , and to any other variable; denotes the environment such that , if , otherwise. We also assume that is identical to .

The type assignment system (cf. Figure 1) is a set of typing rules assigning strict types of to terms of . It uses an auxiliary system assigning multiset types to patterns. We write (resp. ) to denote a typing derivation ending in the sequent (resp. ), in which case (resp. ) is called the subject of . By abuse of notation, (resp. ) also denotes the existence of some typing derivation ending in this sequent, in which case (resp. ) is said to be typable. The measure of a typing derivation , written , is the number of typing rules in .

Figure 1: The type assignment system .

Rules () and () are those used for -calculus in [8, 13]. Linearity of patterns is guaranteed by the clause in rule . Rule with is essential to type erasing functions such as for example . The rule allows to type a pair like without assigning types to their components, thus will be typable too, whereas will not. This choice reflects the fact that every pair is canonical, so any kind of pair needs to be typed. Rules and type pairs having just one typed component, whereas standard typed calculi with pairs (e.g. [11]) requires both components to be typed. Moreover, this standard policy can be easily recovered from ours by typing a pair whose components are both typed using and successively.

Rule () is obvious and corresponds to the usual rule for typing abstraction in -calculus. Rule () is the more subtle one: in order to type , on one hand we need to type , and on the other one, we need to check that and can be assigned the same types. Notice also that in the process of assigning a type to a term , some subterms of may be left untyped when using rules or . Typically, this happens when contains occurrences of non typable terms, like in . A first elementary property of the type system is that head occurrences are always typed:


If is typable, then is typable.

Let , then it is easy to prove, by induction on , that the occurrence of filling the hole of is always typed.

In order to preserve the relevance of the system, proved below, we need to collect all the environments used in the premises typing and respectively. In fact, to be relevant means that only the used premises are registered in the typing environments. This property, formally stated in the following lemma, will be an important technical tool used to develop the inhabitation algorithm.

Lemma (Relevance)

  • If , then .

  • If , then .

By induction on the typing derivations.

3.1 On the Typing of Patterns

The system features two kinds of typings: those of the form , for terms, and those of the form , for patterns. These are, of course, fundamentally dissymetric notions: is undecidable and non deterministic (a given term may have several types in a given environement), whereas is decidable and deterministic. As a matter of fact, in the typing rules of , the unique type such that could have been denoted by , as we do for variables, being defined as in Lemma 3.1 below. However, we decided to keep the typing judgements since they allow for a clearer formulation of the typing rules of .

A characterization of the typings of patterns is provided in Lemma 3.1. Some preliminary definitions are given below.

Given two patterns and , we say that occurs in if

  • either

  • or and either occurs in or occurs occurs in .

Remark that, by linearity of patterns, at most one of the conditions in the second item above may hold.

As a matter of notation, if is a multiset type, let us denote by (resp. ) the multiset type (resp. ).

If occurs in and is a multiset type, the multiset type is defined as follows:

Given an environement , a pattern and two variables , we say that the pattern is structural in w.r.t. , written , if occurs in and .

Typings of patterns can be characterized as follows:


For all environement and pattern , if and only if and

(): If , then by Lemma 3. We prove that by induction on . If then either and , or and . If , then the last rule of its type derivation is

and by the induction hypothesis and , for . Observe that:

  • the domains of and are disjoint, by linearity of .

  • for all , , for .

  • if then and , for .

  • if , then and for some , and are empty, , and .

By the observations above, we conclude that


(): the proof is by induction on , again. In the case the environement is partitioned in , .


  • Let and . Then,

    • and

    Hence: .

  • Let and . Then,