Skip to main content
Log in

Type-Based Cost Analysis for Lazy Functional Languages

  • Published:
Journal of Automated Reasoning Aims and scope Submit manuscript

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.

This is a preview of subscription content, log in via an institution to check access.

Access this article

Price excludes VAT (USA)
Tax calculation will be finalised during checkout.

Instant access to the full article PDF.

Institutional subscriptions

Fig. 1
Fig. 2
Fig. 3
Fig. 4
Fig. 5
Fig. 6

Similar content being viewed by others

Notes

  1. The extra cost per node accurately models the operational behaviour, but this is only visible at the lower-level of the intermediate notation (cf. Sect. 3).

  2. Other annotated types are possible. Our whole-program analysis only considers the use of foldl within sum here, leading to the shown annotated type.

  3. Boldface symbols denote possibly-empty sequences of the underlying syntactic categories, e.g. \(\mathbf {x}\) and \(\mathbf {y}\) are sequences of variables.

  4. Depending on the cost model, the encoding could incur some additional operational costs, but these would also be reflected in the analysis output.

  5. A polymorphic system would need to track sharing constraints for type variables alongside the types.

  6. We use the standard notation x : A to denote the singleton context mapping variable x to type A, and a comma between two contexts denotes multiset union. Note that contexts are multimaps, as usual in an affine type system, in order to track each use of a variable.

  7. Note that, because of lazy evaluation, the function could discard the argument and thus the thunk cost need not always be accounted in the application cost. However, the function in this example is strict.

  8. We use the GLPK library: http://www.gnu.org/software/glpk.

  9. This heuristic choice typically lowers cost overestimation by allowing paying ahead as early as possible; cf. last paragraph of Sect. 4.4.

  10. Note that expressing the bound using the length of the first argument of a curried function is not allowed because of the

    figure cv

     rule (cf. Sect. 4.3). This could be overcome simply by un-curring the definition [11].

  11. The reconstruction algorithm may always use the revised rules, since they subsume the previous ones.

  12. Unlike our earlier work [24], we no longer require a technical lemma for replacing such pathological configurations. This is because the revised definition of potential is no longer recursive.

  13. Treating \({\varDelta }'\) as a part of \({\varDelta }\) instead of \({\varGamma }\), in order to preserve \({\varDelta }'\) for the second induction.

  14. Note that it is possible to prepay a location more than once or more than necessary; hence we use truncated subtraction to ensure that the thunk annotation remains non-negative.

  15. This requirement is new, since [29] did not consider potential.

References

  1. Benzinger, R.: Automated higher-order complexity analysis. Theor. Comput. Sci. 318(1–2), 79–103 (2004)

    Article  MathSciNet  MATH  Google Scholar 

  2. Bird, R., Wadler, P.: Introduction to Functional Programming. Prentice Hall, New York (1988)

    Google Scholar 

  3. Coutts, D.: Stream fusion: practical shortcut fusion for coinductive sequence types. Ph.D. thesis, Worcester College, University of Oxford (2010)

  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)

  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)

  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)

  7. Hoffmann, J., Shao, Z.: Type-based amortized resource analysis with integers and arrays. J. Funct. Program. 25, e17 (2015)

    Article  MathSciNet  Google Scholar 

  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)

  9. Hughes, J.: Why functional programming matters. Comput. J. 32(2), 98–107 (1989)

    MathSciNet  Google Scholar 

  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)

  11. Jost, S.: Automated Amortised Analysis. Ph.D. thesis, LMU Munich

  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)

  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)

  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)

  15. Launchbury, J.: A natural semantics for lazy evaluation. In: Proceedings of POPL ’93: Symposium on Principles of Programming Languages, pp. 144–154 (1993)

  16. Okasaki, C.: Purely Functional Data Structures. Cambridge University Press, Cambridge (1998)

    Book  MATH  Google Scholar 

  17. Peyton Jones, S., Marlow, S., Reid, A.: The STG runtime system (revised). Draft paper, Microsoft Research Ltd (1999)

  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)

  19. Pierce, B.C.: Advanced Topics in Types and Programming Languages. MIT Press, Cambridge (2004)

    MATH  Google Scholar 

  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)

  21. Sands, D.: Calculi for Time Analysis of Functional Programs. Ph.D. thesis, Imperial College, University of London (1990)

  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)

  23. Sestoft, P.: Deriving a lazy abstract machine. J. Funct. Program. 7(3), 231–264 (1997)

    Article  MathSciNet  MATH  Google Scholar 

  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)

  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)

  26. Tarjan, R.E.: Amortized computational complexity. SIAM J. Algebraic Discrete Methods 6(2), 306–318 (1985)

    Article  MathSciNet  MATH  Google Scholar 

  27. Tofte, M., Talpin, J.P.: Region-based memory management. Inf. Comput. 132(2), 109–176 (1997)

    Article  MathSciNet  MATH  Google Scholar 

  28. Vasconcelos, P.B.: Space cost analysis using sized types. Ph.D. thesis, School of Computer Science, University of St Andrews (2008)

  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)

  30. Wadler, P.: Strictness analysis aids time analysis. In: Proceedings of POPL ’88: ACM Symposium on Principles of Programming Languages, pp. 119–132 (1988)

  31. Wadler, P.: The essence of functional programming. In: Proceedings of POPL ’92: ACM Symposium on Principles of Programming Languages, pp. 1–14 (1992)

  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)

Download references

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Steffen Jost.

Additional information

This work has been partially supported by EU Grants 288570 (ParaPhrase) and 644235 (RePhrase) under the Seventh Framework and Horizon 2020 Programmes.

Rights and permissions

Reprints and permissions

About this article

Check for updates. Verify currency and authenticity via CrossMark

Cite this article

Jost, S., Vasconcelos, P., Florido, M. et al. Type-Based Cost Analysis for Lazy Functional Languages. J Autom Reasoning 59, 87–120 (2017). https://doi.org/10.1007/s10817-016-9398-9

Download citation

  • Received:

  • Accepted:

  • Published:

  • Issue Date:

  • DOI: https://doi.org/10.1007/s10817-016-9398-9

Keywords

Navigation