Skip to main content

Random generation of closed simply typed λ-terms: A synergy between logic programming and Boltzmann samplers*


A natural approach to software quality assurance consists in writing unit tests securing programmer-declared code invariants. Throughout the literature, a great body of work has been devoted to tools and techniques automating this labour-intensive process. A prominent example is the successful use of randomness, in particular, random typable λ-terms, in testing functional programming compilers such as the Glasgow Haskell Compiler. Unfortunately, due to the intrinsically difficult combinatorial structure of typable λ-terms, no effective uniform sampling method is known, setting it as a fundamental open problem in the random software testing approach. In this paper, we combine the framework of Boltzmann samplers, a powerful technique of random combinatorial structure generation, with today's Prolog systems offering a synergy between logic variables, unification with occurs check and efficient backtracking. This allows us to develop a novel sampling mechanism able to construct uniformly random closed simply typed λ-terms of up size 120. We apply our techniques to the generation of uniformly random closed simply typed normal forms and design a parallel execution mechanism pushing forward the achievable term size to 140.

Hide All

The first two authors have been partially supported by the Polish National Science Center grant 2013/11/B/ST6/00975. The third author has been supported by NSF grant 1423324. A conference version of the present paper appears in the proceedings of PADL 2017 (PC chairs: Yuliya Lierler and Walid Taha).

Hide All
Barendregt H. P. 1991. Lambda calculi with types. In Handbook of Logic in Computer Science, Vol. 2, Oxford University Press.
Bendkowski M., Grygiel K., Lescanne P. and Zaionc M. 2016. A natural counting of lambda terms. In SOFSEM 2016: Theory and Practice of Computer Science - Proc. of 42nd International Conference on Current Trends in Theory and Practice of Computer Science, Freivalds R. M., Engels G. and Catania B., Eds. Lecture Notes in Computer Science, vol. 9587. Springer, Harrachov, Czech Republic, January 23–28, 2016, 183194.
Bendkowski M., Grygiel K. and Tarau P. 2017. Boltzmann samplers for closed simply-typed lambda terms. In Proc. of Practical Aspects of Declarative Languages – 19th International Symposium, PADL 2017. Paris, France, January 16–17, 2017.
Bodini O., Gardy D. and Gittenberger B. 2011. Lambda terms of bounded unary height. In Proc. of the 8th Workshop on Analytic Algorithmics and Combinatorics (ANALCO), 23–32.
Bodini O., Genitrini A. and Rolin N. 2015. Pointed versus singular Boltzmann samplers: A comparative analysis. Pure Mathematics and Applications 25, 2, 115131.
Claessen K. and Hughes J. 2000. QuickCheck: A lightweight tool for random testing of Haskell programs. In ICFP '00: Proc. of 5th ACM SIGPLAN International Conference on Functional Programming, ACM, New York, NY, USA, 268–279.
David R., Grygiel K., Kozik J., Raffalli C., Theyssier G. and Zaionc M. 2013. Asymptotically almost all λ-terms are strongly normalizing. Logical Methods in Computer Science 9, 1:02, 130.
de Bruijn N. G. 1972. Lambda calculus notation with nameless dummies, a tool for automatic formula manipulation, with application to the Church–Rosser Theorem. Indagationes Mathematicae 34, 381392.
Duchon P., Flajolet P., Louchard G. and Schaeffer G. 2004. Boltzmann samplers for the random generation of combinatorial structures. Combinatorics, Probability and Computing 13, 4–5, 577625.
Flajolet P. and Sedgewick R. 2009. Analytic Combinatorics, 1 ed. Cambridge University Press, New York, NY, USA.
Grygiel K. and Lescanne P. 2013. Counting and generating lambda terms. Journal of Functional Programming 23, 5, 594628.
Grygiel K. and Lescanne P. 2015. Counting and generating terms in the binary lambda calculus. Journal of Functional Programming 25, 125.
Hindley J. R. and Seldin J. P. 2008. Lambda-Calculus and Combinators: An Introduction, vol. 13. Cambridge University Press.
Koopman P., Alimarine A., Tretmans J. and Plasmeijer R. 2003. Gast: Generic automated software testing. In Implementation of Functional Languages: 14th International Workshop, IFL 2002, Madrid, Spain, September 16–18, Springer, Berlin, Heidelberg, 84100.
Koopman P. and Plasmeijer R. 2006. Systematic synthesis of functions. In The University of Nottingham, Nilsson H. (ED.), Selected Papers of the 7th Symposium on Trends in Functional Programming, TFP 2006, Nottingham, UK, 68–83, Intellect Books.
Pałka M. H., Claessen K., Russo A. and Hughes J. 2011. Testing an optimising compiler by generating random lambda terms. In Proc. of the 6th International Workshop on Automation of Software Test, AST'11. ACM, New York, NY, USA, 91–97.
Sloane N. J. A. 2014. The on-line encyclopedia of integer sequences. URL: [Accessed on August 6, 2017].
Tarau P. 2015a. On logic programming representations of lambda terms: de Bruijn indices, compression, type inference, combinatorial generation, normalization. In Proc. 7th International Symposium on Practical Aspects of Declarative Languages PADL'15, E. Pontelli and Son T. C., Eds. Lecture Notes in Computer Science, vol. 8131. Springer, Portland, Oregon, USA, 115–131.
Tarau P. 2015b. On type-directed generation of lambda terms. In 31st International Conference on Logic Programming (ICLP 2015), Technical Communications, De Vos M., Eiter T., Lierler Y. and Toni F., Eds. CEUR, Cork, Ireland. URL:
Wilf H. S. 2006. Generatingfunctionology. A. K. Peters, Ltd., Natick, MA, USA.
Recommend this journal

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

Theory and Practice of Logic Programming
  • ISSN: 1471-0684
  • EISSN: 1475-3081
  • URL: /core/journals/theory-and-practice-of-logic-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: 13 *
Loading metrics...

Abstract views

Total abstract views: 48 *
Loading metrics...

* Views captured on Cambridge Core between 13th October 2017 - 23rd November 2017. This data will be updated every 24 hours.