Unary proﬁle of lambda terms with restricted De Bruijn indices ∗

In this paper we present an average-case analysis of closed lambda terms with restricted values of De Bruijn indices in the size model where each occurrence of a variable contributes one to the size. Given a ﬁxed integer k , a lambda term in which all De Bruijn indices are bounded by k has the following shape: It starts with k De Bruijn levels, forming the so-called hat of the term, to which some number of k -colored Motzkin trees are attached. By means of singularity analysis, we show that the size of this hat is constant on average and that the average number of De Bruijn levels of k -colored Motzkin trees of size n is asymptotically Θ( √ n ) . Combining these two facts, we conclude that, for all k ≥ 1 , the maximal non-empty De Bruijn level in a lambda term with De Bruijn indices at most k and of size n is, on average, also of order √ n . On this basis, we provide the average unary proﬁle of such lambda terms.


Introduction
The last decade has seen an abundance of studies on a quantitative analysis of objects originating from logic and computability theory.From a combinatorial viewpoint, these objects provide intriguing asymptotic and stochastic problems related to counting them and an average-case analysis of their parameters.One of these are lambda terms, central objects of lambda calculus, which are investigated in this paper.Due to a simple combinatorial specification of lambda terms, no knowledge concerning lambda calculus is required to understand statements and proofs of the presented theorems.For more information on lambda calculus we refer a curious reader to Barendregt (1984).
In the literature one can find several different ways to define the size of a lambda term.In this paper we adapt the definition that is probably the most intuitive for combinatorialists, namely every constructor in a term (i.e., each variable, abstraction, and application) contributes one to the term size.This size model gives rise to a challenging and still open problem on the asymptotics of the sequence of the number of closed terms of a given size (for a thorough discussion see Bodini et al. (2013)).The encountered difficulties lead Bodini et al. (2018) to study restricted classes of terms, namely terms with a bounded number of De Bruijn levels and terms with bounded De Bruijn indices (precise definitions of these notions are provided in Section 2).Gittenberger and Larcher (2018) studied some statistical properties of lambda terms with a bounded number of De Bruijn levels.Their results, exhibiting a change in the distribution of leaves within terms, shed some light on reasons for the strange behaviour of the counting sequences.
While the problem of counting terms of a given size is also open for the model where variables do not contribute to the size, David et al. (2013) provided some results concerning typical parameters of closed lambda terms with no additional restrictions.The applied methods, however, seem not to work in the case of the size of each variable being one.
Another way of measuring terms consists in taking into account the reference depth of each variable, i.e., the number of abstractions enclosing it.This approach was motivated by Tromp (2007) and further studied by Grygiel and Lescanne (2015); Bendkowski et al. (2016) with the main focus on enumeration of terms and Bendkowski et al. (2019) describing average values of several parameters in terms.
In this paper, in a similar vein to the work by Gittenberger and Larcher (2018), we perform an averagecase analysis of lambda terms with bounded De Bruijn indices.This class has a significantly weaker restriction compared to restricting the number of De Bruijn levels.On the basis of an empirical investigation on existing Haskell programs and their lambda calculus counterparts, we claim that imposing a bound on De Bruijn indices seems natural, as their values in the vast majority of programs remain small and rarely exceed 20 (Berger (2019)).Our research is hence motivated by getting a better understanding of the structure of lambda terms belonging to this class as well as explaining the structural discrepancies between terms from the two discussed classes.
In the next section we describe lambda terms as combinatorial objects and introduce basic concepts used throughout the paper.In Section 3 we discuss the shape of lambda terms with De Bruijn indices bounded by k (which we call k-indexed lambda terms for brevity) and their decomposition into smaller structures, that is the hat and some attached k-colored Motzkin trees.Our results concerning average sizes of these substructures are presented in the two following sections: First, in Section 4, we show that the size of the hat is constant on average and then, in Section 5, we prove that the average number of De Bruijn levels in terms of size n is asymptotically of order √ n.Section 6 contains the main result of this paper, namely the average unary profile of k-indexed terms.Finally, in the last section, we recall the results by Gittenberger and Larcher (2018) about the distribution of the total number of leaves in k-indexed terms and provide a short conclusion and outlook.

Preliminaries
Let V be a countable set of variables.Lambda terms are defined by the following grammar: A term of the form λx.M is called an abstraction, while a term of the form M N is called an application.For the sake of clarity, we omit some parentheses according to the standard convention, i.e., outermost parentheses are dropped, an application is left-and an abstraction right-associative.By Var(M ) we denote the set of all variables occurring in M .The set FV(M ) of free variables in a term M is defined recursively as follows: A term M is called closed if it contains no free variables, i.e., when FV(M ) = ∅.
Lambda terms have a natural representation by means of finite enriched trees, i.e., rooted trees with additional directed edges (pointers).In order to construct the corresponding enriched tree for a given lambda term, first we construct a Motzkin tree, i.e., a plane rooted tree with each node of out-degree 0, 1, or 2. In this tree each binary node corresponds to an application, each unary node to an abstraction, and each leaf to a variable.Now, for every occurrence of a bound variable x, we add a directed edge from the unary node corresponding to the particular abstraction, labelled with λx, to the variable.Therefore, each unary node of the Motzkin tree carries (zero, one, or more) pointers to leaves taken from the subtree rooted at this unary node; all leaves receiving a pointer from this unary node correspond to the same variable, and each leaf receives at most one pointer.By tree(M ) we denote the Motzkin tree obtained from a lambda term M by removing all pointers (see Grygiel et al. (2013) for a more detailed description).
In what follows, we will be interested only in closed terms.This means that every lambda term we investigate is represented by some Motzkin tree enriched with pointers from unary nodes to leaves in such a way that every leaf receives precisely one pointer.Moreover, terms that are equal up to α-conversion, i.e., up to renaming of bound variables, are considered equivalent.This allows us to apply the De Bruijn notation for lambda terms, which consists in eliminating names of variables and replacing them by positive integers indicating, in the tree representation, the number of unary nodes on the path from a particular variable to its binder.This representation was introduced by De Bruijn (1972), who used the notion of reference depth.In other words, instead of having the set V of variables, we use the set {1, 2, 3, . ..} of De Bruijn indices, where an index n occurring in a term M indicates that the n-th lambda (i.e., unary node) lying on the path from the corresponding leaf to the root in tree(M ) points at this leaf.For every k ∈ N, we say that a lambda term is k-indexed if all of its De Bruijn indices belong to the set {1, 2, . . ., k}.
Let M be a lambda term and v be a vertex in tree(M ).The unary height of v in tree(M ), denoted by h(v), is defined as the number of unary nodes on the path connecting v with the root of tree(M ).For every ∈ N, the -th De Bruijn level of tree(M ) is defined as the set of all vertices v in tree(M ) such that h(v) = .Finally, by the unary profile of a lambda term we define the sequence of numbers of variables in each De Bruijn level of the term.Remark. De Bruijn (1972) introduced the notion of a level exclusively for variables.We extend this concept for all nodes in enriched trees as well as for Motzkin trees, so that all internal nodes are also covered.However, to avoid any confusion, we want to point out that the name De Bruijn level has also been used in slightly different contexts in previous papers so far, as for example by Lescanne and Rouyer-Degli (1995), who speak about subterms of a given term to be at the -th level if they have unary nodes above them and contain only indices at most .
As an example let us consider the term λa.λb.(λc.b)b a λd.(λe.d)d (λf.f a) (see Figure 1).Its De Bruijn notation reads as λ λ(λ2)1 1 λ (λ2)1 (λ13) , and it has three non-empty De Bruijn levels, of which only level 1 is connected.Notice that the 0-th De Bruijn level is empty, as the term is an abstraction.Moreover, this term is k-indexed for every k ≥ 3.
In this paper the size of a lambda term is defined as the total number of its variables, abstractions, and applications, i.e., for any variable x and lambda terms M and N the size is recursively defined as follows: Therefore, the size of a lambda term is equal to the number of all vertices in the corresponding tree.The Having defined the size of lambda terms, we can now look at lambda terms as at objects from a combinatorial class and hence apply standard combinatorial methodology.Given a combinatorial class A of objects and its nonempty subclass A n containing only the objects of A that are of size n, we assume the uniform probability distribution over A n , i.e., every object from A n has the equal probability 1/|A n | to be chosen at random.A parameter χ on the class A is any function from A to N. For example, the size of a hat or the number of non-empty De Bruijn levels of lambda terms are possible instances of parameters.Every parameter χ determines a discrete random variable χ n defined over the discrete probability space A n as follows: where A n,j denotes the class of objects of size n for which the parameter χ is equal to j.This allows us to speak about the expected value (also called average or mean) of the parameter χ for the fixed class.It is well-known that the expected value can be computed by means of generating functions via the formula where A(z, u) is the bivariate generating function associated with |A n,j | n,j≥0 .

Structure of lambda terms with bounded De Bruijn indices
In this section we want to illustrate the asymptotic shape of lambda terms with bounded De Bruijn indices in a very general way, while it will be investigated more thoroughly in the subsequent sections.
In order to set up the generating function for closed lambda terms with bounded De Bruijn indices, we proceed analogously to Bodini et al. (2018).For every k ≥ 1, let G k be the class of k-indexed lambda terms and let G k (z) be the corresponding generating function.In order to write down a formula for G k (z) we define the following auxiliary functions: For j ∈ {0, . . ., k}, let G k,j be the class of unary-binary trees such that every leaf v is labelled by a De Bruijn index m with 1 ≤ m ≤ min{h(v) + j, k}.Note that only the terms in G k,0 are necessarily closed, while this does not have to be the case for terms from G k,j with j ≥ 1.The classes G k,j can be recursively specified, starting from a class Z of atoms, in the following way: Then the classes G k and G k,0 are isomorphic and hence their generating functions coincide.Thus, by translating into generating functions, we directly get (cf.Bodini et al. (2018)) where Bodini et al. (2018, Lemma 5.4) proved that the dominant singularity of G k (z) comes from the innermost radicand, i.e., R k,0 (z), and is equal to ρ k = 1 2 √ k+1 .Furthermore, they provide an asymptotic estimate of the n-th coefficient of G k (z).Before showing this estimate let us define an auxiliary sequence (c j ) j≥1 via and constants C j,k with j ≥ 1 and k ≥ j via These numbers appear both in the announced estimate and throughout this paper.The first values of (c k ) k≥1 and (C j,k ) j≥k are listed in Table 1.
Lemma 3.1 (Bodini et al. (2018, Theorem 5.6)).For any fixed k ≥ 1, let G k (z) be the generating function of the class of k-indexed lambda terms.Then where ρ k = 1 2 √ k+1 and C 1,k is defined as in (3).Remark.In (Bodini et al., 2018, Lemma 5.7) the authors showed the asymptotic decrease of the constant in Lemma 3.1, reading as The result from Lemma 3.1 already gives us a hint that lambda terms with bounded De Bruijn indices behave somewhat treelike.However, in order to get a better intuition why this is the case and how exactly these terms look like, we set up the generating function G k (z) in a different manner, which reflects another way of looking at how the corresponding terms are constructed.Instead of interpreting a lambda term belonging to that class as a structure that involves iterated unary-binary trees, we consider it to be built of leaf-labelled binary trees that are glued together via unary nodes (cf. Figure 2).Thereby, the labels of the leaves correspond to the respective De Bruijn indices.Obviously, this implies that within the whole tree each of the labels belongs to the set {1, . . ., k}.However, in the first k − 1 De Bruijn levels (excluding the 0-th level, which contains no variables) we have a stronger restriction.Since we consider only closed terms, no label (i.e., no De Bruijn index) can exceed the De Bruijn level the respective leaf is located in.Thus, with B(z, w) denoting the bivariate generating function of binary trees where z marks the size (i.e., the total number of nodes) and w marks the number of leaves, and with M k (z) denoting the generating function of Motzkin trees where each leaf can be labelled in k ways (k-colored Motzkin trees in short), we get where Now, let us give an interpretation of Equation ( 4).Each tree representing a lambda term starts with a binary tree, in which all the leaves are replaced by unary nodes to which we add further binary trees.This is necessary for a lambda term to be closed.These newly added binary trees represent the first De Bruijn level.Next, there are two possibilities for each leaf in this level: Either it receives the label 1 or, alternatively, it is replaced with a unary node with a new binary tree attached, which belongs to the next De Bruijn level.In this level the leaves can already be labelled with two different labels (namely 1 or 2), or they can be replaced with unary nodes with new binary trees attached.Starting from the k-th De Bruijn level, the number of possible labellings for the leaves does not increase anymore.Thus, we finally get B(z, k + B(z, k + B(z, k + . ..))), which is exactly the generating function M k (z) of k-colored Motzkin trees given in (5).
Therefore, the enriched tree corresponding to a k-indexed lambda term is constructed as follows (cf. Figure 2): • It starts with the hat consisting of all De Bruijn levels from 0 to k − 1 along with all unary nodes from the k-th level; • To this hat structure we attach k-colored Motzkin trees via unary nodes.
Remark.Note that the glued binary trees in Equation ( 4) constitute the hat of the structure, to which we attach the (comparatively large) k-colored Motzkin trees.We emphasize at this point that the generating functions defined in ( 1) and ( 4) describe indeed the exact same function.The reason to choose the latter way of representing the function G k (z) in this paper is that it gives direct access to the De Bruijn levels, which is advantageous for our purposes.
In the subsequent sections we investigate the structure of these terms in more detail.We prove that, for a fixed k ≥ 1, the hat of a k-indexed lambda term is on average of constant size and that the average number of De Bruijn levels of a term of size n is asymptotically √ n.Finally, we provide its unary profile.

Average size of a hat
In this section we focus on the size of the hat of k-indexed lambda terms.We prove that the average size of a hat is asymptotically constant, i.e., it does not depend on the size of a term.This implies that on average the number of k-colored Motzkin trees in the decomposition described in the previous section is also constant.
Theorem 4.1.For k ≥ 1, let X n,k be the size of the hat of a random k-indexed lambda term of size n.Then, as n tends to infinity, with (c j ) j≥1 and C j,k defined in (2) and (3).
Proof: To prove this theorem we use the well-known approach of marking the parameter of interest in the generating function with a second variable u and then investigating the bivariate generating function.
Remember that throughout this whole paper the quantity k is a fixed constant which is independent of n, and thus when expanding the radicands R k,j the uniformity of the error terms in k is not an issue.Moreover, as a further consequence, the set of singularities of the generating function G k (z) is finite, since the radicand R k,j is singular if and only if one of the radicands R k,j−1 , R k,j−2 , . .., R k,0 , is zero.Thus, there exists a δ > 0 such that ρ k is the only singularity for |z| < ρ k +δ and therefore all requirements for the use of singularity are fulfilled.So, let G k (z, u) be the generating function for k-indexed lambda terms with z marking the size of terms and u marking the size of their hats.The average size of a hat is hence given by .
Since we want to mark by u all the nodes that belong to the hat, we get where B(z, w) is the function defined in (5).This gives Therefore, the derivatives can also be recursively defined via and we get R k,m (z, 1) . (6) Expanding the radicands R k,j around z = ρ k yields (see Bodini et al. (2018)) Plugging this into Equation ( 6) gives where A k and B k are constants depending on k with Since A k is not important for the result, we skip computing its exact value.By inserting (7) into the formula for B k and after some simplifications, we obtain By singularity analysis applied to we immediately obtain, as n tends to infinity, Finally, by ( 8) and Lemma 3.1, we get In Figure 3 we list average sizes of hats of k-indexed lambda terms for k up to 10.Since the hat of a k-indexed lambda term, for any k ≥ 1, is constant on average, such a term has on average a finite number of unary nodes in the k-th De Bruijn level.Therefore, we arrive at the following conclusion.Corollary 4.2.For every k ≥ 1, the average number of k-colored Motzkin trees in the decomposition (see the description in the end of Section 3) of k-indexed lambda terms is constant.

Average number of De Bruijn levels
In order to determine the average number of De Bruijn levels of k-indexed lambda terms, we first compute the average number of De Bruijn levels of k-colored Motzkin trees.To this end, we use the following result by Drmota et al. (2014).The notation A(z) B(z) used therein means that [z n ]A(z) ≤ [z n ]B(z) for every n ≥ 0.
Let T [0] (z) be a power series with 0 T [0] (z) T (z) such that T [0] (z) is analytic at z = z 0 , and let T [k] (z), k ≥ 1 be iteratively defined by Let H n be an integer valued random variable that is defined by Lemma 5.2.The average number of De Bruijn levels of a k-colored Motzkin tree of size n is asymptotically equal to πn 2k + √ k .
Proof: For k ≥ 1 and h ≥ 0, the generating function M and hence . Let us notice that F k (z, t) satisfies the assumptions of Lemma 5.1.Indeed, the function M k (z), with a square-root singularity at z fulfilling all necessary conditions.Furthermore, the function M [0] k (z) enumerates all k-colored Motzkin trees with only one (the 0-th) De Bruijn level.These trees are binary trees with k possible labels for each node, thus thus the average number of De Brujin levels of k-colored Motzkin trees is asymptotically equal to Corollary 5.3.For every k ≥ 1, the average number of De Bruijn levels of a k-indexed lambda term of size n is Θ( √ n).
Proof: By Corollary 4.2, the number of k-colored Motzkin trees in the decomposition of lambda terms is constant on average.Therefore, the size of a largest such tree in the decomposition of a k-indexed lambda term of size n is asymptotically Θ(n).Since the average number of De Bruijn levels of k-colored Motzkin trees of size asymptotic to n is Θ( √ n), the same is true for k-indexed lambda terms, which have just k levels more than a longest (in terms of De Bruijn levels) k-colored Motzkin tree in their decomposition.

Unary profile
In this section, we determine the mean unary profile of a random lambda term of some large size, i.e., the asymptotic number of variables in each De Bruijn level of the term.
In the forthcoming proof, we will make use of the following technical results.
Lemma 6.1 (Gittenberger (1999, Lemma 3.4)).Let γ be a Hankel contour truncated at K ∈ R + , i.e., t ≤ K for all t ∈ γ.Then we have, for α, β > 0, Lemma 6.2.Let ε > 0 and γ Proof: The closer to ρ k an argument of the function γ z → √ is, the greater its modulus gets.Thus, we set z = ρ k 1 + log 2 n n + i n , which is the closest point to ρ k on γ, and we get Plugging in the asymptotic formulas for a and b directly yields the desired result.Now we are in the position to prove the main theorem of this section.Theorem 6.3.Let α > 0 be a fixed real number.The expected number of variables at De Bruijn level α √ n in a k-indexed lambda term of size n is asymptotically equal to Proof: To prove this theorem we use again the approach that we mark the parameter of interest and investigate the thereby obtained bivariate generating function.However, in this case we have to face a different problem that arises due to the fact that we want to mark the number of leaves in a level proportional to √ n, while n tends to infinity, leading to infinitely many nested radicals.Thus, we cannot apply the transfer theorems directly in order to get the asymptotics for the coefficients of the generating functions, but instead we write the coefficients by means of Cauchy's integral formula and use a suitable integration contour.Fortunately, we will be able to show that solely finitely many of the occurring radicands are different, while the great majority describes the exact same function, which then provides simplifications that allow us to calculate the integral along the chosen curve asymptotically.
So, first let U k, (z, u) be the bivariate generating function for k-indexed terms with z marking the size and u marking the number of leaves in the (k + )-th De Bruijn level, where ≥ 1.Then we have Applying formulas for B(z, w) and M k (z) given in (5) yields where Furthermore, we have Given the De Bruijn level = α √ n with α > 0, we are interested in estimating In order to make further computations easier, let us notice that Q k,j (z, 1) = z + Q k,0 (z, 1) for j ∈ {1, . . ., }, i.e., all these radicands describe the same function.Indeed, let us first notice that the above holds for j = 1, since Next, by ( 5), we can notice that . By iteration we obtain the result for j ∈ {3, . . ., }.
For z = ρ k 1 + t n we get the expansions where (c j ) j≥1 and (b k,j ) k,j≥1 are as before (see ( 2) and ( 7)).
Let ε > 0. We have where as an integration path we choose a truncated Hankel contour γ 1 ∪ γ 2 ∪ γ 3 encircling the dominant singularity ρ k and a circular arc γ 4 : We start by estimating the integral along γ 1 .To this end, we apply the substitution z Fig. 4: Contour of integration: γ1 plotted with a solid line, γ2 and γ3 with dashed lines, and γ4 with a dotted line.
where γ 1 denotes the transformed curve and we use the expansions given in (9): Since the number of binary nodes differs only by 1 from the number of leaves, and the remaining nodes (that are neither binary nodes nor leaves) have to be unary nodes, we can state the following corollary.
Corollary 7.2.Let Y n be the total number of binary nodes in a random closed lambda term of size n with De Bruijn index at most k, and let Z n be the total number of unary nodes, respectively.Then with X n being defined as in Theorem 7.1.
Remark.Thus, it is an immediate observation that on average each lambda binds √ k leaves in lambda terms with De Bruijn indices being at most k.
By calculating the asymptotic number of individual constructors that occur in k-colored Motzkin trees, we get exactly the same results as in Theorem 7.1 (and therefore also as in Corollary 7.2).Furthermore, the height and the profile of these k-colored Motzkin trees are also very similar to that of lambda terms with De Bruijn indices at most k.Thus, k-indexed lambda terms are very much alike k-colored Motzkin trees.However, their counting sequences differ significantly (by a factor C 1,k /2) due to the restrictions on labelling leaves in hats of the terms.So, there are way more k-colored Motzkin trees than k-indexed lambda terms.Nevertheless the great majority of them exhibits the same structural properties.
This leads to the conjecture that the problem of generating random lambda terms could be solved by means of generating random k-colored Motzkin trees and finding a suitable algorithm for repairing their hats.The resulting generation would not be perfectly uniform, but potentially very close to the uniform one and it would definitively be an interesting future topic to investigate.

Fig. 1 :
Fig. 1: Left: The enriched tree of λa.λb.(λc.b)b a λd.(λe.d)d (λf.f a) .Right: Its counterpart in the De Bruijn notation (where all leaves are labeled with their respective De Bruijn indices) along with its decomposition into De Bruijn levels.term in Figure 1 is of size 19.It has 7 variables, 6 applications, and 6 abstractions.Having defined the size of lambda terms, we can now look at lambda terms as at objects from a combinatorial class and hence apply standard combinatorial methodology.Given a combinatorial class A of objects and its nonempty subclass A n containing only the objects of A that are of size n, we assume the uniform probability distribution over A n , i.e., every object from A n has the equal probability 1/|A n | to be chosen at random.A parameter χ on the class A is any function from A to N. For example, the size of a hat or the number of non-empty De Bruijn levels of lambda terms are possible instances of parameters.Every parameter χ determines a discrete random variable χ n defined over the discrete probability space A n as follows:

Fig. 2 :
Fig. 2: A lambda term from G3 decomposed into the hat and three subterms represented by 3-colored Motzkin trees.
of k-colored Motzkin trees with at most h De Bruijn levels fulfills ρ k + b k,j −t/n + O(|t|/n) ρ k + b k,j −t/n 1 First values of the sequences (c k ) k≥1 and (C j,k ) k≥j for j ∈ {1, 2, 3, 4}.