Skip to Main content Skip to Navigation
Preprints, Working Papers, ...

Counting and generating lambda terms

Abstract : Lambda calculus is the basis of functional programming and higher order proof assistants. However, few is known about combinatorial properties of lambda terms, in particular, about their asymptotic distribution and random generation. Among others, this paper tries to answer questions like: How many terms of a given size are there? What is a ''typical'' structure of a simply typed term? Despite their ostensible simplicity, these questions still remain unanswered, whereas solutions to such problems are essential for testing compilers and optimizing programs whose expected efficiency depends on the size of terms. Our approach toward the afore-mentioned problems may be later extended to any language with bounded variables, i.e., with scopes and declarations. This paper presents two complementary approaches: one, theoretical, uses complex analysis and generating functions, the other, experimental, is based on a computer algebra software, able to handle huge numbers efficiently. Thanks to de Bruijn indices, we provide formulas for the number of closed lambda terms of a given size and show their relevance to recursively defined integer polynomials. Knowledge on the asymptotic behavior of the polynomial coefficients suggests the approach toward the problem of the asymptotic behavior of numbers of closed lambda terms. Indeed, this problem is unamenable to standard generating function methods due to the unusual form of the recurrences. As a by-product of the counting formulas, we design an algorithm for generating lambda terms. Performed tests provide us with experimental data, like the average depth of bound variables and the average number of head lambdas. We also create random generators for various sorts of terms. Thereafter, we conduct experiments that answer questions like: What is the ratio of simply typed terms among all terms? (Very small!) How are simply typed lambda terms distributed among all lambda terms? (A~typed term almost always starts with an abstraction.)
Complete list of metadata
Contributor : Pierre Lescanne Connect in order to contact the contributor
Submitted on : Friday, December 14, 2012 - 11:13:56 PM
Last modification on : Friday, September 10, 2021 - 2:34:03 PM
Long-term archiving on: : Sunday, December 18, 2016 - 2:22:59 AM


Files produced by the author(s)


  • HAL Id : ensl-00740034, version 2
  • ARXIV : 1210.2610


Katarzyna Grygiel, Pierre Lescanne. Counting and generating lambda terms. 2012. ⟨ensl-00740034v2⟩



Record views


Files downloads