Skip to main content

Counting and generating lambda terms


Lambda calculus is the basis of functional programming and higher order proof assistants. However, little is known about combinatorial properties of lambda terms, in particular, about their asymptotic distribution and random generation. This paper tries to answer questions like: How many terms of a given size are there? What is a ‘typical’ structure of a simply typable 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 aforementioned problems may be later extended to any language with bound 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 generator of lambda terms. Thanks to de Bruijn indices (de Bruijn, N. (1972) Lambda calculus notation with nameless dummies, a tool for automatic formula manipulation, with application to the Church-Rosser theorem. Indagat. Math.34(5), 381–392), we provide three families of formulas for the number of closed lambda terms of a given size and we give four relations between these numbers which have interesting combinatorial interpretations. As a by-product of the counting formulas, we design an algorithm for generating λ-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 typable terms among all terms? (Very small!) How are simply typable lambda terms distributed among all lambda terms? (A typable term almost always starts with an abstraction.) In this paper, abstractions and applications have size 1 and variables have size 0.

Hide All
Abadi, M., Cardelli, L., Curien, P.-L. & Lévy, J.-J. (1991) Explicit substitutionss. J. Funct. Program. 1 (4), 375416.
Bodini, O., Gardy, D. & Gittenberger, B. (2011) Lambda terms of bounded unary height. In Proceedings of the Eighth Workshop on Analytic Algorithmics and Combinatorics (ANALCO 2011), San Francisco, CA. pp. 2332.
Curien, P.-L. (2005) Introduction to linear logic and ludics, part I. Corr abs/cs/0501035. Accessed October 17, 2013. Available at:
Damas, L. & Milner, R. (1982) Principal type-schemes for functional programs. In Conference Record of the Ninth Annual ACM Symposium on Principles of Programming Languages, Albuquerque, NM, January 1982, DeMillo, R. A. (ed). New York, NY: ACM Press, pp. 207212.
David, R., Grygiel, K., Kozik, J., Raffalli, C., Theyssier, G. & Zaionc, M. (2013) Asymptotically almost all λ-terms are strongly normalizing. Log. Meth. Comput. Sci. 9, 130.
de Bruijn, N. (1972) Lambda calculus notation with nameless dummies, a tool for automatic formula manipulation, with application to the Church-Rosser theorem. Indagat. Math. 34 (5), 381392.
Duregård, J., Jansson, P. & Wang, M. (2012) Feat: Functional enumeration of algebraic types. In Proceedings of the 5th ACM SIGPLAN Symposium on Haskell (Haskell 2012), Copenhagen, Denmark, 13 September, Voigtländer, J. (ed). New York, NY: ACM, pp. 6172.
Flajolet, P. & Sedgewick, R. (2008) Analytic Combinatorics. Cambridge, UK: Cambridge University Press.
Gallier, J. & Snyder, W. (1989) Complete sets of transformations for general E-unification. Theoret. Comput. Sci. 67 (2–3), 203260.
Graham, R. L., Knuth, D. E. & Patashnik, O. (1989) Concrete Mathematics. Reading, MA: Addison-Wesley.
Hindley, J. R. (2008) M. H. Newman's typability algorithm for lambda-calculus. J. Log. Comput. 18 (2), 229238.
Jouannaud, J.-P., & Kirchner, C. (1991) Solving equations in abstract algebras: A rule-based survey of unification. In Computational Logic. Essays in Honor of Alan Robinson, Chap. 8, Lassez, J.-L. & Plotkin, G. (eds). Cambridge, MA: MIT Press, pp. 257321.
Kennedy, A. J. & Vytiniotis, D. (2012) Every bit counts: The binary representation of typed data and programs. J. Funct. Program. 22 (4–5), 529573.
Knuth, D. E. (2006) The Art of Computer Programming, Generating All Trees-History of Combinatorial Generation, Vol. 4 (fascicle 4). Reading, MA: Addison-Wesley.
Lang, W. (2002) On polynomials related to derivatives of the generative functions of the Catalan numbers. Fibonacci Q. 40 (4), 299313.
Lescanne, P. (1994) From λσ to λυ, a journey through calculi of explicit substitutions. In Proceedings of the 21st Annual ACM Symposium on Principles of Programming Languages, Portland, OR, Boehm, H. (ed). New York, NY: ACM, pp. 6069.
Lescanne, P. (2013) On counting untyped lambda terms. Theor. Comput. Sci. 474, 8097.
Li, M. & Vitányi, P. (1997) An Introduction to Kolmogorov Complexity and Its Applications, 2nd ed. Secaucus, NJ: Springer-Verlag.
Newman, M. H. A. (1943) Stratified systems of logic. Proc. Camb. Phil. Soc. 39, 6983.
Pałka, M. (2012, May) Testing an Optimising Compiler by Generating Random Lambda Terms. Technical Report, Department of Computer Science and Engineering, Chalmers University of Technology and Göteborg University, Gothenburg, Sweden (Licentiatavhandling).
Pałka, M. H., Claessen, K., Russo, A. & Hughes, J. (2011) Testing an optimising compiler by generating random lambda terms. In Proceedings of the 6th International Workshop on Automation of Software Test (AST'11). New York, NY: ACM, pp. 9197.
Rodriguez, Yakushev A. & Jeuring, J. (2009) Enumerating well-typed terms generically. In Third International Workshop, AAIP, Edinburgh, UK, September 4, Schmid, U., Kitzelmann, E. & Plasmeijer, R. (eds), Lecture Notes in Computer Science, vol. 5812. New York, NY: Springer, pp. 93116.
Stein, al. (2012) Sage Mathematics Software (Version 4.8) (The Sage Development Team). Available at: Accessed October 17, 2013.
The On-Line Encyclopedia of Integer Sequences (OEIS)® Wiki. (2013) Ranking and Unranking Functions. Available at: Accessed June 18, 2013.
Tromp, J. (2006) Binary lambda calculus and combinatory logic. In Kolmogorov Complexity and Applications, Hutter, M., Merkle, W. & Vitányi, P. M. B. (eds), Dagstuhl Seminar Proceedings, vol. 06051. Schloss Dagstuhl, Germany: Internationales Begegnungs- und Forschungszentrum fuer Informatik (IBFI). Available at Accessed October 17, 2013.
Wang, J. (2004, May) The Efficient Generation of Random Programs and Their Applications, MPhil thesis, Honors Thesis, Wellesley College, Wellesley, MA.
Wang, J. (2005) Generating Random Lambda Calculus Terms. Citeseer. Available at: Accessed October 17, 2013.
Yang, X., Chen, Y., Eide, E. & Regehr, J. (2011) Finding and understanding bugs in C compilers. In Proceedings of the 32nd ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI 2011), San Jose, CA, June 4–8, Hall, M. W. & Padua, D. A. (eds). New York, NY: ACM, pp. 283294.
Recommend this journal

Email your librarian or administrator to recommend adding this journal to your organisation's collection.

Journal of Functional Programming
  • ISSN: 0956-7968
  • EISSN: 1469-7653
  • URL: /core/journals/journal-of-functional-programming
Please enter your name
Please enter a valid email address
Who would you like to send this to? *


Full text views

Total number of HTML views: 0
Total number of PDF views: 0 *
Loading metrics...

Abstract views

Total abstract views: 0 *
Loading metrics...

* Views captured on Cambridge Core between <date>. This data will be updated every 24 hours.

Usage data cannot currently be displayed

Counting and generating lambda terms

Submit a response


No Discussions have been published for this article.


Reply to: Submit a response

Your details

Conflicting interests

Do you have any conflicting interests? *