Hostname: page-component-cd4964975-g4d8c Total loading time: 0 Render date: 2023-03-29T00:20:06.607Z Has data issue: true Feature Flags: { "useRatesEcommerce": false } hasContentIssue true

Counting and generating lambda terms

Published online by Cambridge University Press:  30 October 2013

Theoretical Computer Science Department, Faculty of Mathematics and Computer Science, Jagiellonian University, ul. Prof. Łojasiewicza 6, 30-348 Kraków, Poland (e-mail:
Theoretical Computer Science Department, Faculty of Mathematics and Computer Science, Jagiellonian University, ul. Prof. Łojasiewicza 6, 30-348 Kraków, Poland; ENS de Lyon, University of Lyon, LIP (UMR 5668 CNRS ENS Lyon UCBL INRIA), 46 allée d'Italie, 69364 Lyon, France (e-mail:
Rights & Permissions[Opens in a new window]


HTML view is not available for this content. However, as you have access to this content, a full PDF is available via the ‘Save PDF’ action button.

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.

Copyright © Cambridge University Press 2013 


Abadi, M., Cardelli, L., Curien, P.-L. & Lévy, J.-J. (1991) Explicit substitutionss. J. Funct. Program. 1 (4), 375416.CrossRefGoogle Scholar
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.CrossRefGoogle Scholar
Curien, P.-L. (2005) Introduction to linear logic and ludics, part I. Corr abs/cs/0501035. Accessed October 17, 2013. Available at: Scholar
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.Google Scholar
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.CrossRefGoogle Scholar
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.CrossRefGoogle Scholar
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.Google Scholar
Flajolet, P. & Sedgewick, R. (2008) Analytic Combinatorics. Cambridge, UK: Cambridge University Press.Google Scholar
Gallier, J. & Snyder, W. (1989) Complete sets of transformations for general E-unification. Theoret. Comput. Sci. 67 (2–3), 203260.CrossRefGoogle Scholar
Graham, R. L., Knuth, D. E. & Patashnik, O. (1989) Concrete Mathematics. Reading, MA: Addison-Wesley.Google Scholar
Hindley, J. R. (2008) M. H. Newman's typability algorithm for lambda-calculus. J. Log. Comput. 18 (2), 229238.CrossRefGoogle Scholar
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.Google Scholar
Kennedy, A. J. & Vytiniotis, D. (2012) Every bit counts: The binary representation of typed data and programs. J. Funct. Program. 22 (4–5), 529573.CrossRefGoogle Scholar
Knuth, D. E. (2006) The Art of Computer Programming, Generating All Trees-History of Combinatorial Generation, Vol. 4 (fascicle 4). Reading, MA: Addison-Wesley.Google Scholar
Lang, W. (2002) On polynomials related to derivatives of the generative functions of the Catalan numbers. Fibonacci Q. 40 (4), 299313.Google Scholar
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.Google Scholar
Lescanne, P. (2013) On counting untyped lambda terms. Theor. Comput. Sci. 474, 8097.CrossRefGoogle Scholar
Li, M. & Vitányi, P. (1997) An Introduction to Kolmogorov Complexity and Its Applications, 2nd ed. Secaucus, NJ: Springer-Verlag.CrossRefGoogle Scholar
Newman, M. H. A. (1943) Stratified systems of logic. Proc. Camb. Phil. Soc. 39, 6983.CrossRefGoogle Scholar
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).Google Scholar
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.CrossRefGoogle Scholar
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.Google Scholar
Stein, al. (2012) Sage Mathematics Software (Version 4.8) (The Sage Development Team). Available at: Accessed October 17, 2013.Google Scholar
The On-Line Encyclopedia of Integer Sequences (OEIS)® Wiki. (2013) Ranking and Unranking Functions. Available at: Accessed June 18, 2013.Google Scholar
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.Google Scholar
Wang, J. (2004, May) The Efficient Generation of Random Programs and Their Applications, MPhil thesis, Honors Thesis, Wellesley College, Wellesley, MA.Google Scholar
Wang, J. (2005) Generating Random Lambda Calculus Terms. Citeseer. Available at: Accessed October 17, 2013.Google Scholar
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.Google Scholar
Submit a response


No Discussions have been published for this article.