Journal of Automated Reasoning

, Volume 59, Issue 1, pp 87–120 | Cite as

Type-Based Cost Analysis for Lazy Functional Languages

  • Steffen Jost
  • Pedro Vasconcelos
  • Mário Florido
  • Kevin Hammond
Article
  • 169 Downloads

Abstract

We present a static analysis for determining the execution costs of lazily evaluated functional languages, such as Haskell. Time- and space-behaviour of lazy functional languages can be hard to predict, creating a significant barrier to their broader acceptance. This paper applies a type-based analysis employing amortisation and cost effects to statically determine upper bounds on evaluation costs. While amortisation performs well with finite recursive data, we significantly improve the precision of our analysis for co-recursive programs (i.e. dealing with potentially infinite data structures) by tracking self-references. Combining these two approaches gives a fully automatic static analysis for both recursive and co-recursive definitions. The analysis is formally proven correct against an operational semantic that features an exchangeable parametric cost-model. An arbitrary measure can be assigned to all syntactic constructs, allowing to bound, for example, evaluation steps, applications, allocations, etc. Moreover, automatic inference only relies on first-order unification and standard linear programming solving. Our publicly available implementation demonstrates the practicability of our technique on editable non-trivial examples.

Keywords

Automated static analysis Lazy evaluation Corecursion Amortised analysis Type systems Functional programming 

References

  1. 1.
    Benzinger, R.: Automated higher-order complexity analysis. Theor. Comput. Sci. 318(1–2), 79–103 (2004)MathSciNetCrossRefMATHGoogle Scholar
  2. 2.
    Bird, R., Wadler, P.: Introduction to Functional Programming. Prentice Hall, New York (1988)Google Scholar
  3. 3.
    Coutts, D.: Stream fusion: practical shortcut fusion for coinductive sequence types. Ph.D. thesis, Worcester College, University of Oxford (2010)Google Scholar
  4. 4.
    Danielsson, N.A.: Lightweight semiformal time complexity analysis for purely functional data structures. Proceedings of POPL 2008: 35th ACM SIGPLAN-SIGACT symposium on principles of programming languages. San Francisco, California, pp. 133–144. ACM, San Francisco (2008)Google Scholar
  5. 5.
    Hoffmann, J., Aehlig, K., Hofmann, M.: Multivariate amortized resource analysis. In: 38th Symposium on Principles of Programming Languages (POPL’11), pp. 357–370 (2011)Google Scholar
  6. 6.
    Hoffmann, J., Shao, Z.: Automatic static cost analysis for parallel programs. In: Proceedings of the 24th European Symposium on Programming (ESOP’15), pp. 132–157. Springer (2015)Google Scholar
  7. 7.
    Hoffmann, J., Shao, Z.: Type-based amortized resource analysis with integers and arrays. J. Funct. Program. 25, e17 (2015)MathSciNetCrossRefGoogle Scholar
  8. 8.
    Hofmann, M., Jost, S.: Static prediction of heap space usage for first-order functional programs. In: Proceedings of the 30th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL), pp. 185–197. ACM, ACM Press, New Orleans (2003)Google Scholar
  9. 9.
    Hughes, J.: Why functional programming matters. Comput. J. 32(2), 98–107 (1989)MathSciNetGoogle Scholar
  10. 10.
    Hughes, J., Pareto, L.: Recursion and dynamic data structures in bounded space: towards embedded ML programming. In: Proceedings of 1999 ACM International Conference on Functional Programming (ICFP ’99), ACM Sigplan Notices, vol. 34, pp. 70–81. ACM Press (1999)Google Scholar
  11. 11.
    Jost, S.: Automated Amortised Analysis. Ph.D. thesis, LMU MunichGoogle Scholar
  12. 12.
    Jost, S., Loidl, H.W., Hammond, K., Hofmann, M.: Static determination of quantitative resource usage for higher-order programs. In: Proceedings of ACM Symposium on Principles of Programming Languages (POPL), pp. 223–236 (2010)Google Scholar
  13. 13.
    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: Formal Methods. Lecture Notes in Computer Science, vol. 5850, pp. 354–369. Springer, Heidelberg (2009)Google Scholar
  14. 14.
    La Encina, A., Peña, R.: Proving the correctness of the STG machine. In: Proceedings of IFL ’01: Implementation of Functional Programming Languages, Stockholm, Sweden, Sept. 24–26, 2001, pp. 88–104. Springer LNCS 2312 (2002)Google Scholar
  15. 15.
    Launchbury, J.: A natural semantics for lazy evaluation. In: Proceedings of POPL ’93: Symposium on Principles of Programming Languages, pp. 144–154 (1993)Google Scholar
  16. 16.
    Okasaki, C.: Purely Functional Data Structures. Cambridge University Press, Cambridge (1998)CrossRefMATHGoogle Scholar
  17. 17.
    Peyton Jones, S., Marlow, S., Reid, A.: The STG runtime system (revised). Draft paper, Microsoft Research Ltd (1999)Google Scholar
  18. 18.
    Peyton Jones, S., Augustsson, L., Boutel, B., Burton, F., Fasel, J., Gordon, A., Hammond, K., Hughes, R., Hudak, P., Johnsson, T., Jones, M., Peterson, J., Reid, A., Wadler, P. (eds.): Report on the Non-Strict Functional Language, Haskell (Haskell98). Technical report, Yale University (1999)Google Scholar
  19. 19.
    Pierce, B.C.: Advanced Topics in Types and Programming Languages. MIT Press, Cambridge (2004)MATHGoogle Scholar
  20. 20.
    Reistad, B., Gifford, D.: Static dependent costs for estimating execution time. In: Proceedings of ACM Conference on Lisp and Functional Programming, pp. 65–78. ACM Press, Orlando, Florida, June 27–29 (1994)Google Scholar
  21. 21.
    Sands, D.: Calculi for Time Analysis of Functional Programs. Ph.D. thesis, Imperial College, University of London (1990)Google Scholar
  22. 22.
    Sands, D.: Complexity analysis for a lazy higher-order language. In: Proceedings of ESOP ’90: European Symposium on Programming, Copenhagen, Denmark, Springer LNCS 432, pp. 361–376 (1990)Google Scholar
  23. 23.
    Sestoft, P.: Deriving a lazy abstract machine. J. Funct. Program. 7(3), 231–264 (1997)MathSciNetCrossRefMATHGoogle Scholar
  24. 24.
    Simões, H., Jost, S., Vasconcelos, P., Florido, M., Hammond, K.: Automatic amortised analysis of dynamic memory allocation for lazy functional programs. In: Proceedings of the 17th ACM SIGPLAN International Conference on Functional Programming (ICFP’12), pp. 165–176. ACM (2012)Google Scholar
  25. 25.
    Talpin, J.P., Jouvelot, P.: The type and effect discipline. In: Scedrov, A. (ed.) Proceedings of 1992 Logics in Computer Science Conference, vol. 111, pp. 245–271. IEEE (1992)Google Scholar
  26. 26.
    Tarjan, R.E.: Amortized computational complexity. SIAM J. Algebraic Discrete Methods 6(2), 306–318 (1985)MathSciNetCrossRefMATHGoogle Scholar
  27. 27.
    Tofte, M., Talpin, J.P.: Region-based memory management. Inf. Comput. 132(2), 109–176 (1997)MathSciNetCrossRefMATHGoogle Scholar
  28. 28.
    Vasconcelos, P.B.: Space cost analysis using sized types. Ph.D. thesis, School of Computer Science, University of St Andrews (2008)Google Scholar
  29. 29.
    Vasconcelos, P.B., Jost, S., Florido, M., Hammond, K.: Type-based allocation analysis for co-recursion in lazy functional languages. In: Programming Languages and Systems: Proceedings of 24th European Symposium on Programming, ESOP 2015, Held as Part of the European Joint Conferences on Theory and Practice of Software, ETAPS 2015, London, UK, April 11–18, pp. 787–811 (2015)Google Scholar
  30. 30.
    Wadler, P.: Strictness analysis aids time analysis. In: Proceedings of POPL ’88: ACM Symposium on Principles of Programming Languages, pp. 119–132 (1988)Google Scholar
  31. 31.
    Wadler, P.: The essence of functional programming. In: Proceedings of POPL ’92: ACM Symposium on Principles of Programming Languages, pp. 1–14 (1992)Google Scholar
  32. 32.
    Wadler, P., Hughes, J.: Projections for strictness analysis. In: Proceedings of FPCA’87: International Conference on Functional Programming Language and Computer Architecture, Springer LNCS 274, pp. 385–407 (1987)Google Scholar

Copyright information

© Springer Science+Business Media Dordrecht 2017

Authors and Affiliations

  1. 1.LMU MunichMunichGermany
  2. 2.LIACC, DCC/Faculdade de CiênciasUniversidade do PortoPortoPortugal
  3. 3.University of St AndrewsSt AndrewsUK

Personalised recommendations