Combinatorics of explicit substitutions

04/11/2018 ∙ by Maciej Bendkowski, et al. ∙ Jagiellonian University 0

λυ is an extension of the λ-calculus which internalises the calculus of substitutions. In the current paper, we investigate the combinatorial properties of λυ focusing on the quantitative aspects of substitution resolution. We exhibit an unexpected correspondence between the counting sequence for λυ-terms and famous Catalan numbers. As a by-product, we establish effective sampling schemes for random λυ-terms. We show that typical λυ-terms represent, in a strong sense, non-strict computations in the classic λ-calculus. Moreover, typically almost all substitutions are in fact suspended, i.e. unevaluated, under closures. Consequently, we argue that λυ is an intrinsically non-strict calculus of explicit substitutions. Finally, we investigate the distribution of various redexes governing the substitution resolution in λυ and investigate the quantitative contribution of various substitution primitives.



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

Substitution of terms for variables forms a central concept in various formal calculi with qualifiers, such as predicate logic or different variants of -calculus. Though substitution supports the computational character of -reduction in -calculus, it is usually specified as an external meta-level formalism, see [barendregt1984]. Such an epitheoretic presentation of substitution masks its execution as a single, indivisible calculation step, even though it requires a considerable computational effort to carry out, see [peytonJones1987]. In consequence, the number of -reduction steps required to normalise a -term does not reflect the actual operational cost of normalisation. In order to effectuate substitution resolution, its process needs to be decomposed into a series of fine-grained atomic rewriting steps included as part of the considered calculus.

An early example of such a calculus, internalising the evaluation of substitution, is combinatory logic [CurryFeys1958]; alas, bearing the price of loosing the intuitive, high-level structure of encoded functions, mirrored in the polynomial blow-up of their representation, see [joy1984, joy1985]. Focusing on retaining the basic intuitions behind substitution, various calculi of explicit substitutions highlighting multiple implementation principles of substitution resolution in -calculus were proposed throughout the years, cf. [deBruijn1978, abadi1991, Lescanne1994, DBLP:journals/jar/RoseBL12]. The formalisation of substitution evaluation as a rewriting process provides a formal platform for operational semantics of reduction in -calculus using abstract machines, such as, for instance, the Krivine machine [curien93:categ_combin]. Moreover, with the internalisation of substitution, reduction cost reflects more closely the true computational complexity of executing modern functional programs.

Nonetheless, due to the numerous nuances regarding the evaluation cost of functional programs (e.g. assumed reduction or strictness strategies) supported by a general tradition of considering computational complexity in the framework of Turing machines or RAM models rather than formal calculi, the evaluation cost in term rewriting systems, such as

-calculus or combinatory logic, gains increasing attention only quite recently, see [DBLP:journals/corr/abs-1208-0515, DBLP:conf/icfp/AvanziniLM15, DBLP:journals/corr/AccattoliL16]. The continuing development of automated termination and complexity analysers for both first- and higher-order term rewriting systems echoes the immense practical, and hence also theoretical, demand for complexity analysis frameworks of declarative programming languages, see e.g. [aprove2014, lmcs:749]. In this context, the computational analysis of first-order term rewriting systems seems to most accurately reflect the practical evaluation cost of declarative programs [DBLP:conf/cade/CichonL92, DBLP:journals/jfp/BonfanteCMT01]. Consequently, the average-case performance analysis of abstract rewriting machines executing the declared computations requires a quantitative analysis of their internal calculi. Such investigations provide not only key insight into the quantitative aspects of basic rewriting principles, but also allow to optimise abstract rewriting machines so to reflect the quantitative contribution of various rewriting primitives.

Despite their apparent practical utility, quantitative aspects of term rewriting systems are not well studied. In [DBLP:journals/tcs/ChoppyKS89]

Choppy, Kaplan and Soria provide a quantitative evaluation of a general class of confluent, terminating term rewriting systems in which the term reduction cost (i.e. the number of rewriting steps required to reach the final normal form) is independent of the assumed normalisation strategy. Following a similar, analytic approach, Dershowitz and Lindenstrauss provide an average-time analysis of inference parallelisation in logic programming 


. More recently, Bendkowski, Grygiel and Zaionc analyse quantitative aspects of normal-order reduction of combinatory logic terms and estimate the asymptotic density of normalising combinators 

[bengryzai2017, BENDKOWSKI_2017]. Alas, due to the intractable, epitheoretic formalisation of substitution in untyped -calculus, its quantitative rewriting aspects have, to our best knowledge, not yet been investigated.

In the following paper we offer a combinatorial perspective on substitution resolution in -calculus and propose a combinatorial analysis of explicit substitutions in -calculus [Lescanne1994]. The paper is structured as follows. In Section 2 we draft the basic characteristics of  required for the reminder of the current paper. Next, we introduce the necessary analytic toolbox used in the quantitative analysis, see Section 3. In Section 4 we enumerate -terms and exhibit the declared correspondence between their corresponding counting sequence and Catalan numbers. Some statistical properties of random -terms are investigated in LABEL:sec:statistical:properties. In LABEL:subsec:strict:substitution:forms we relate the typical form of -terms with the classic, epitheoretic substitution tactic of -calculus. The quantitative impact of substitution suspension is investigated in LABEL:subsec:suspended:substitutions. In LABEL:subsec:redexes we discuss the contribution of various substitution resolution primitives. The final LABEL:sec:conclusions concludes the paper.

2. Preliminaries

2.1. -calculus

In the current subsection we outline the main characteristics of -calculus (lambda-upsilon calculus) required for the reminder of the paper. We refer the curious reader to [Lescanne1994, benaissa_briaud_lescanne_rouyer-degli_1996] for a more detailed exposition.


We choose to outline -calculus following the presentation of [lescanne96] where indices start with 0 instead of [Lescanne1994, benaissa_briaud_lescanne_rouyer-degli_1996] where de Bruijn indices start with 1, as introduced by de Bruijn himself, cf. [deBruijn1972]. Although both conventions are assumed in the context of static, quantitative aspects of -calculus, the former convention seems to be the most recent standard, cf. [gryles2013, gryles2015, BendkowskiGLZ16, GittenbergerGolebiewskiG16].

The computational mechanism of -reduction is usually defined as where the right-hand side denotes the epitheoretic, capture-avoiding substitution of term for variable in . -calculus [Lescanne1994] is a simple, first-order rewriting system internalising substitution resolution of classic -calculus in de Bruijn notation [deBruijn1972]. Its expressions, called ()-terms, consist of indices (for convenience also referred to as variables), abstractions and term application. Terms are also equipped with a new closure operator denoting the ongoing resolution of substitution . Explicit substitutions are fragmented into atomic primitives of shift, denoted as , a unary operator lift, written as , mapping substitutions onto substitutions, and finally a unary slash operator, denoted as , mapping terms onto substitutions. Terms containing closures are called impure whereas terms without them are said to be pure. De Bruijn indices are encoded using a unary base expansion. In other words, n is represented as an -fold application of the successor operator to zero. Figure 0(b) summarises the specification of -terms.

The rewriting rules of -calculus consist of the usual -rule, specified in this framework as and an additional set of (seven) rules governing the resolution of explicit substitutions, see Figure 0(a). Remarkably, these few rewriting rules are sufficient to correctly model -reduction and also preserve strong normalisation of closed -terms, see [benaissa_briaud_lescanne_rouyer-degli_1996]. The simple syntax and rewriting rules of -calculus are not only of theoretical importance, but also of practical interest, used as the foundation of various reduction engines. Let us mention that -calculus and its abstract U-machine executing (strong) -normalisation was successfully used as the main reduction engine in Pollack’s implementation of LEGO, a proof checker of the Calculus of Constructions, the Edinburgh Logical Framework, and also for the Extended Calculus of Constructions [randypollackLEGO].

Example 2.1.

Consider the term . Note that in the de Bruijn notation, is written as . Likewise, the term is denoted as . Certainly, for each term . Note however, that with explicit substitution resolution in , this reduction is fragmented into several reduction steps, as follows:


Note that in the final reduction step of (1) we obtain . The additional shift operator guarantees that (potential) free indices are aptly incremented so to avoid potential variable captures. If is closed, i.e. each variable in is bound, then resolves simply to , as intended.

(a) Rewriting rules.
(b) Terms of -calculus. Note that de Bruijn indices are encoded in unary base using a successor operator .
Figure 1. The -calculus rewriting system.

3. Analytic toolbox

We base our quantitative analysis of -terms on techniques borrowed from analytic combinatorics, in particular singularity analysis developed by Flajolet and Odlyzko [FlajoletOdlyzko1990]. We refer the unfamiliar reader to [Wilf2006, flajolet09] for a thorough introduction to (multivariate) generating functions and analytic combinatorics.


Our arguments follow standard applications of singularity analysis to (multivariate) systems of generating functions corresponding to algebraic structures. For the reader’s convenience we offer a high-level, though limited to the subject of our interest, outline of this process in the following section.

3.1. Singularity analysis

Interested in the quantitative properties of -terms, for instance their asymptotic enumeration or parameter analysis, we typically take the following general approach. We start the analysis with establishing a formal, unambiguous context-free specification describing the structures of our interest. Next, using symbolic methods [flajolet09, Part A. Symbolic Methods] we convert the specification into a system of generating functions, i.e. formal power series in which the coefficient standing by , written as , denotes the number of structures (objects) of size . Interested in parameter analysis, so obtained generating functions become bivariate and take the form where , also written as , stands for the number of structures of size for which the investigated parameter takes value ; for instance, denotes the number of terms of size with exactly occurrences of a specific redex pattern. In this context, variable corresponds to the size of specified structures whereas is said to mark the investigated parameter quantities.

When the obtained system of generating functions admits an analytic solution (i.e. obtained formal power series are also analytic at the complex plane origin) we can investigate the quantitative properties of respective coefficient sequences, and so also enumerated combinatorial structures, by examining the analytic properties of associated generating functions. The location of their singularities, in particular so-called dominant singularities dictates the main, exponential growth rate factor of the investigated coefficient sequence.

Theorem 3.1 (Exponential growth formula [flajolet09, Theorem IV.7]).

If is analytic at the origin and is the modulus of a singularity nearest to the origin in the sense that


then the coefficient satisfies


Generating functions considered in the current paper are algebraic, i.e. are branches of polynomial equations in form of . Since cannot be unambiguously defined as an analytic functions near the origin, the main source of singularities encountered during our analysis are roots of radicand expressions involved in the closed-form, analytic formulae defining studied generating functions. The following classic result due to Pringsheim facilities the inspection of such singularities.

Theorem 3.2 (Pringsheim [flajolet09, Theorem IV.6]).

If is representable at the origin by a series expansion that has non-negative coefficients and radius of convergence , then the point is a singularity of .

A detailed singularity analysis of algebraic generating functions, involving an examination of the type of dominant singularities follows as a consequence of the Puiseux series expansion for algebraic generating functions.

Theorem 3.3 (Newton, Puiseux [flajolet09, Theorem VII.7]).

Let be a branch of an algebraic function . Then in a circular neighbourhood of a singularity slit along a ray emanating from , admits a fractional Newton-Puiseux series expansion that is locally convergent and of the form


where and .

With available Puiseux series, the complete asymptotic expansion of sub-exponential growth rate factors associated with coefficient sequences of investigated algebraic generating functions can be accessed using the following standard function scale.

Theorem 3.4 (Standard function scale [flajolet09, Theorem VI.1]).

Let . Then, admits for large a complete asymptotic expansion in form of


where is the Euler Gamma function defined as


3.2. Parameter analysis

Consider a random variable

denoting a certain parameter quantity of a (uniformly) random -term of size . In order to analyse the limit behaviour of as

tends to infinity, we utilise the moment techniques of multivariate generating functions 

[flajolet09, Chapter 3]. In particular, if is a bivariate generating function associated with where marks the considered parameter quantities, then the expectation takes the form


Consequently, the limit mean and, similarly, all higher moments can be accessed using techniques of singularity analysis. Although such a direct approach allows to investigate all the limit moments of

(in particular its mean and variance) it is usually more convenient to study the associated probability generating function

instead, defined as


With at hand, it is possible to readily access the limit distribution of . In the current paper we focus primarily on continuous, Gaussian limit distributions associated with various redexes in -calculus. The following Quasi-powers theorem due to Hwang [hwang1998convergence]

provides means to obtain a limit Gaussian distribution and establishes the rate at which intermediate distributions converge to the final limit distribution.

Theorem 3.5 (Quasi-powers theorem, see [flajolet09, Theorem IX.8]).


be a sequence of non-negative discrete random variables (supported by

) with probability generating functions . Assume that, uniformly in a fixed complex neighbourhood of , for sequences , there holds


where and are analytic at and .

Assume finally that satisfies the following variability condition:


Then, the distribution is, after standardisation, asymptotically Gaussian with speed of convergence of order :



is the standard normal distribution function


The limit expectation and variance satisfy


4. Counting -terms

In current section we begin the enumeration of -terms. For that purpose, we impose on them a size notion such that the size of a -term, denoted as , is equal to the total number of constructors (in the associated term algebra, see Figure 0(b)) of which it is built. Figure 2 provides the recursive definition of term size.

Figure 2. Natural size notion for -terms.

Such a size notion, in which all building constructors contribute equal weight one to the overall term size was introduced in [Bendkowski2016] as the so-called natural size notion. Likewise, we also refer to the size notion assumed in the current paper as natural.

Certainly, our choice is arbitrary and, in principle, different size measures can be assumed, cf. [gryles2015, Bendkowski2016, GittenbergerGolebiewskiG16]. For convenience, we choose the natural size notion thus avoiding the obfuscating (though still manageable) technical difficulties arising in the analysis of general size model frameworks, see e.g. [GittenbergerGolebiewskiG16]. Moreover, our particular choice exhibits unexpected consequences and hence is, arguably, interesting on its own, see creftypecap 4.1.

Equipped with a size notion ensuring that for each the total number of -terms of size is finite, we can proceed with our enumerative analysis. Surprisingly, the counting sequence corresponding to -terms in the natural size notion corresponds also to the celebrated sequence of Catalan numbers111see

Proposition 4.1.

Let and denote the generating functions corresponding to -terms
and substitutions, respectively. Then,


In consequence


hence also


Consider the formal specification (2) for -terms. Let be the generating function corresponding to de Bruijn indices. Note that following symbolic methods, the generating functions , , and give rise to the system


Note that is an independent variable in (18). We can therefore solve the equation and find that . Substituting this expression for in the equations defining and we obtain


System (19) admits two solutions, i.e.


both with agreeing signs.

In order to determine the correct pair of generating functions we invoke the fact that, by their construction, both and are non-negative integers for all . Consequently, the declared pair (15) is the analytic solution of (20). At this point, we notice that both the generating functions in (15) resemble the famous generating function corresponding to Catalan numbers, see e.g. [Wilf2006, Section 2.3]. Indeed


In this form, we can readily relate Catalan numbers with respective coefficients of and , see (16). From (21) we obtain . The number corresponds thus to for all with the initial . Furthermore, given we note that corresponds to the partial sum of Catalan numbers222see up to (exclusively). ∎

The correspondence exhibited in creftypecap 4.1 witnesses the existence of a bijection between -terms of size and, for instance, plane binary trees with inner nodes. In what follows we provide an alternative, constructive proof of this fact.

4.1. Bijection between -terms and plane binary trees

Let denote the set of plane binary trees (i.e. binary trees in which we distinguish the order of subtrees). Consider the map defined as in LABEL:fig:bijection. Note that, for convenience, we omit drawing leafs. Consequently, nodes in LABEL:fig:bijection with a single or no subtrees are to be understood as having implicit leafs attached to vacuous branches.