Type-Based Allocation Analysis for Co-recursion in Lazy Functional Languages

  • Pedro VasconcelosEmail author
  • Steffen Jost
  • Mário Florido
  • Kevin Hammond
Part of the Lecture Notes in Computer Science book series (LNCS, volume 9032)


This paper presents a novel type-and-effect analysis for predicting upper-bounds on memory allocation costs for co-recursive definitions in a simple lazily-evaluated functional language. We show the soundness of this system against an instrumented variant of Launchbury’s semantics for lazy evaluation which serves as a formal cost model. Our soundness proof requires an intermediate semantics employing indirections. Our proof of correspondence between these semantics that we provide is thus a crucial part of this work.

The analysis has been implemented as an automatic inference system. We demonstrate its effectiveness using several example programs that previously could not be automatically analysed.


Cost Model Operational Semantic Functional Programming Proof Obligation Type Rule 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.


  1. 1.
    Barwise, J., Moss, L.: Vicious Circles. CSLI Publications (1996)Google Scholar
  2. 2.
    Bird, R., Wadler, P.: Introduction to Functional Programming. Prentice-Hall, Englewood Cliffs (1988)Google Scholar
  3. 3.
    Coutts, D.: Stream Fusion: Practical shortcut fusion for coinductive sequence types. PhD thesis, Worcester College, University of Oxford (2010)Google Scholar
  4. 4.
    Simoes, H., Vasconcelos, P., Jost, S., Hammond, K., Florido, M.: Automatic amortised analysis of dynamic memory allocation for lazy functional programs. In: Proc. of ACM Intl. Conf. Func. Programming (ICFP 2012), pp. 165–176. ACM (2012)Google Scholar
  5. 5.
    Sestoft, P.: Deriving a Lazy Abstract Machine. J. Functional Programming 7(3), 231–264 (1997)CrossRefzbMATHMathSciNetGoogle Scholar
  6. 6.
    Launchbury, J.: A Natural Semantics for Lazy Evaluation. In: Proc. POPL 1993: Symp. on Princ. of Prog. Langs., pp. 144–154 (1993)Google Scholar
  7. 7.
    Jost, S., Loidl, H.W., Hammond, K., Scaife, N., Hofmann, M.: “Carbon Credits” for Resource-Bounded Computations Using Amortised Analysis. In: Cavalcanti, A., Dams, D.R. (eds.) FM 2009. LNCS, vol. 5850, pp. 354–369. Springer, Heidelberg (2009)CrossRefGoogle Scholar
  8. 8.
    de la Encina, A., Peña, R.: Proving the Correctness of the STG Machine. In: Arts, T., Mohnen, M. (eds.) IFL 2002. LNCS, vol. 2312, pp. 88–104. Springer, Heidelberg (2002)CrossRefGoogle Scholar
  9. 9.
    Peyton Jones, S.L.: Implementing Lazy Functional Languages on Stock Hardware – the Spineless Tagless G-machine. J. Functional Programming 2(2), 127–202 (1992)CrossRefzbMATHGoogle Scholar
  10. 10.
    Sánchez-Gil, L., Hidalgo-Herrero, M., Ortega-Mallén, Y.: The role of indirections in lazy natural semantics. Technical Report TR-13-13, Departamento de Sistemas Informticos y Computacin, Universidad Complutense de Madrid (2013)Google Scholar
  11. 11.
    de la Encina, A., Peña-Marí, R.: Formally Deriving an STG Machine. In: Proc. 5th International ACM SIGPLAN Conference on Principles and Practice of Declarative Programming, Uppsala, Sweden, August 27-29, pp. 102–112. ACM (2003)Google Scholar
  12. 12.
    de la Encina, A., Peña-Marí, R.: From Natural Semantics to C: a Formal Derivation of two STG Machines. J. Funct. Program. 19(1), 47–94 (2009)CrossRefzbMATHGoogle Scholar
  13. 13.
    Sands, D.: Calculi for Time Analysis of Functional Programs. PhD thesis, Imperial College, University of London (September 1990)Google Scholar
  14. 14.
    Sands, D.: Complexity Analysis for a Lazy Higher-Order Language. In: Jones, N.D. (ed.) ESOP 1990. LNCS, vol. 432, pp. 361–376. Springer, Heidelberg (1990)CrossRefGoogle Scholar
  15. 15.
    Wadler, P.: Strictness Analysis aids Time Analysis. In: Proc. POPL 1988: ACM Symp. on Princ. of Prog. Langs, pp. 119–132 (1988)Google Scholar
  16. 16.
    Wadler, P., Hughes, J.: Projections for Strictness Analysis. In: Kahn, G. (ed.) FPCA 1987. LNCS, vol. 274, pp. 385–407. Springer, Heidelberg (1987)CrossRefGoogle Scholar
  17. 17.
    Wadler, P.: The Essence of Functional Programming. In: Proc. POPL 1992: ACM Symp. on Principles of Prog. Langs., pp. 1–14 (January 1992)Google Scholar
  18. 18.
    Danielsson, N.A.: Lightweight Semiformal Time Complexity Analysis for Purely Functional Data Structures. In: Proc. POPL 2008: Symp. on Principles of Prog. Langs., San Francisco, USA, January 7-12, pp. 133–144. ACM (2008)Google Scholar
  19. 19.
    Turner, D.: Elementary Strong Functional Programming. In: Hartel, P.H., Plasmeijer, R. (eds.) FPLE 1995. LNCS, vol. 1022, pp. 1–13. Springer, Heidelberg (1995)CrossRefGoogle Scholar
  20. 20.
    Hughes, R., Pareto, L., Sabry, A.: Proving the Correctness of Reactive Systems Using Sized Types. In: ACM Symp. on Principles of Prog. Langs (POPL 1996), St. Petersburg Beach, USA, pp. 410–423. ACM (January 1996)Google Scholar
  21. 21.
    Hughes, R., Pareto, L.: Recursion and Dynamic Data Structures in Bounded Space: Towards Embedded ML Programming. In: Proc. 1999 ACM Intl. Conf. on Functional Programming (ICFP 1999), pp. 70–81 (1999)Google Scholar
  22. 22.
    Brady, E., Hammond, K.: A Dependently Typed Framework for Static Analysis of Program Execution Costs. In: Butterfield, A., Grelck, C., Huch, F. (eds.) IFL 2005. LNCS, vol. 4015, pp. 74–90. Springer, Heidelberg (2006)CrossRefGoogle Scholar
  23. 23.
    Pena, R., Segura, C.: A First-Order Functl. Lang. for Reasoning about Heap Consumption. In: Draft Proc. Intl. Workshop on Impl. and Appl. of Functl. Langs. (IFL 2004), pp. 64–80 (2004)Google Scholar
  24. 24.
    Montenegro, M., Pena, R., Segura, C.: An Inference Algorithm for Guaranteeing Safe Destruction. In: Draft Proc. Trends in Functional Programming (TFP 2007), New York, April 2-4 (2007)Google Scholar
  25. 25.
    Talpin, J.P., Jouvelot, P.: Polymorphic type, region and effect inference. J. Funct. Program. 2(3), 245–271 (1992)CrossRefzbMATHMathSciNetGoogle Scholar
  26. 26.
    Nielson, F., Nielson, H.R., Amtoft, T.: Polymorphic subtyping for effect analysis: The algorithm. In: Logical and Operational Methods in the Analysis of Programs and Systems, pp. 207–243 (1996)Google Scholar
  27. 27.
    Nielson, H.R., Nielson, F., Amtoft, T.: Polymorphic subtyping for effect analysis: The static semantics. In: Logical and Operational Methods in the Analysis of Programs and Systems, pp. 141–171 (1996)Google Scholar
  28. 28.
    Damas, L., Milner, R.: Principal type-schemes for functional programs. In: ACM Symp. on Principles of Prog. Langs (POPL 1982), pp. 207–212. ACM, New York (1982)Google Scholar
  29. 29.
    Marlow, S., Jones, S.P.: Making a fast curry: push/enter vs. eval/apply for higher-order languages. In: Proc. of the ACM SIGPLAN 2004 Intl. Conf. on Functional Programming (ICFP 2004), pp. 4–15. ACM Press (January 2004)Google Scholar
  30. 30.
    Hoffmann, J., Aehlig, K., Hofmann, M.: Multivariate Amortized Resource Analysis. ACM Trans. Program. Lang. Syst. 34(3), 14:1–14:62 (2012)Google Scholar
  31. 31.
    Hofmann, M., Jost, S.: Static prediction of heap space usage for first-order functional programs. In: ACM Symp. on Principles of Prog. Langs (POPL 2003), pp. 185–197. ACM (January 2003)Google Scholar
  32. 32.
    Tofte, M., Talpin, J.P.: Region-based memory management. Information and Computation 132(2), 109–176 (1997)CrossRefzbMATHMathSciNetGoogle Scholar
  33. 33.
    Tofte, M., et al.: Programming with regions in the ml kit, IT University of Copenhagen (April 2002),

Copyright information

© Springer-Verlag Berlin Heidelberg 2015

Authors and Affiliations

  • Pedro Vasconcelos
    • 1
    Email author
  • Steffen Jost
    • 2
  • Mário Florido
    • 1
  • Kevin Hammond
    • 3
  1. 1.LIACC, Universidade do PortoPortoPortugal
  2. 2.Ludwig Maximillians UniversitätMunichGermany
  3. 3.University of St AndrewsSt AndrewsUK

Personalised recommendations