Skip to main content
Top
Published in: Journal of Automated Reasoning 1/2017

06-01-2017

Type-Based Cost Analysis for Lazy Functional Languages

Authors: Steffen Jost, Pedro Vasconcelos, Mário Florido, Kevin Hammond

Published in: Journal of Automated Reasoning | Issue 1/2017

Log in

Activate our intelligent search to find suitable subject content or patents.

search-config
loading …

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.

Dont have a licence yet? Then find out more about our products and how to get one now:

Springer Professional "Wirtschaft+Technik"

Online-Abonnement

Mit Springer Professional "Wirtschaft+Technik" erhalten Sie Zugriff auf:

  • über 102.000 Bücher
  • über 537 Zeitschriften

aus folgenden Fachgebieten:

  • Automobil + Motoren
  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Elektrotechnik + Elektronik
  • Energie + Nachhaltigkeit
  • Finance + Banking
  • Management + Führung
  • Marketing + Vertrieb
  • Maschinenbau + Werkstoffe
  • Versicherung + Risiko

Jetzt Wissensvorsprung sichern!

Springer Professional "Wirtschaft"

Online-Abonnement

Mit Springer Professional "Wirtschaft" erhalten Sie Zugriff auf:

  • über 67.000 Bücher
  • über 340 Zeitschriften

aus folgenden Fachgebieten:

  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Finance + Banking
  • Management + Führung
  • Marketing + Vertrieb
  • Versicherung + Risiko




Jetzt Wissensvorsprung sichern!

Springer Professional "Technik"

Online-Abonnement

Mit Springer Professional "Technik" erhalten Sie Zugriff auf:

  • über 67.000 Bücher
  • über 390 Zeitschriften

aus folgenden Fachgebieten:

  • Automobil + Motoren
  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Elektrotechnik + Elektronik
  • Energie + Nachhaltigkeit
  • Maschinenbau + Werkstoffe




 

Jetzt Wissensvorsprung sichern!

Footnotes
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.
 
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  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.
 
Literature
2.
go back to reference Bird, R., Wadler, P.: Introduction to Functional Programming. Prentice Hall, New York (1988) Bird, R., Wadler, P.: Introduction to Functional Programming. Prentice Hall, New York (1988)
3.
go back to reference Coutts, D.: Stream fusion: practical shortcut fusion for coinductive sequence types. Ph.D. thesis, Worcester College, University of Oxford (2010) Coutts, D.: Stream fusion: practical shortcut fusion for coinductive sequence types. Ph.D. thesis, Worcester College, University of Oxford (2010)
4.
go back to reference 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) 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.
go back to reference Hoffmann, J., Aehlig, K., Hofmann, M.: Multivariate amortized resource analysis. In: 38th Symposium on Principles of Programming Languages (POPL’11), pp. 357–370 (2011) 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.
go back to reference 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) 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.
go back to reference Hoffmann, J., Shao, Z.: Type-based amortized resource analysis with integers and arrays. J. Funct. Program. 25, e17 (2015)MathSciNetCrossRef Hoffmann, J., Shao, Z.: Type-based amortized resource analysis with integers and arrays. J. Funct. Program. 25, e17 (2015)MathSciNetCrossRef
8.
go back to reference 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) 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.
10.
go back to reference 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) 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.
go back to reference Jost, S.: Automated Amortised Analysis. Ph.D. thesis, LMU Munich Jost, S.: Automated Amortised Analysis. Ph.D. thesis, LMU Munich
12.
go back to reference 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) 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.
go back to reference 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) 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.
go back to reference 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) 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.
go back to reference Launchbury, J.: A natural semantics for lazy evaluation. In: Proceedings of POPL ’93: Symposium on Principles of Programming Languages, pp. 144–154 (1993) Launchbury, J.: A natural semantics for lazy evaluation. In: Proceedings of POPL ’93: Symposium on Principles of Programming Languages, pp. 144–154 (1993)
16.
go back to reference Okasaki, C.: Purely Functional Data Structures. Cambridge University Press, Cambridge (1998)CrossRefMATH Okasaki, C.: Purely Functional Data Structures. Cambridge University Press, Cambridge (1998)CrossRefMATH
17.
go back to reference Peyton Jones, S., Marlow, S., Reid, A.: The STG runtime system (revised). Draft paper, Microsoft Research Ltd (1999) Peyton Jones, S., Marlow, S., Reid, A.: The STG runtime system (revised). Draft paper, Microsoft Research Ltd (1999)
18.
go back to reference 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) 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.
go back to reference Pierce, B.C.: Advanced Topics in Types and Programming Languages. MIT Press, Cambridge (2004)MATH Pierce, B.C.: Advanced Topics in Types and Programming Languages. MIT Press, Cambridge (2004)MATH
20.
go back to reference 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) 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.
go back to reference Sands, D.: Calculi for Time Analysis of Functional Programs. Ph.D. thesis, Imperial College, University of London (1990) Sands, D.: Calculi for Time Analysis of Functional Programs. Ph.D. thesis, Imperial College, University of London (1990)
22.
go back to reference 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) 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)
24.
go back to reference 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) 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.
go back to reference 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) 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)
28.
go back to reference Vasconcelos, P.B.: Space cost analysis using sized types. Ph.D. thesis, School of Computer Science, University of St Andrews (2008) Vasconcelos, P.B.: Space cost analysis using sized types. Ph.D. thesis, School of Computer Science, University of St Andrews (2008)
29.
go back to reference 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) 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.
go back to reference Wadler, P.: Strictness analysis aids time analysis. In: Proceedings of POPL ’88: ACM Symposium on Principles of Programming Languages, pp. 119–132 (1988) Wadler, P.: Strictness analysis aids time analysis. In: Proceedings of POPL ’88: ACM Symposium on Principles of Programming Languages, pp. 119–132 (1988)
31.
go back to reference Wadler, P.: The essence of functional programming. In: Proceedings of POPL ’92: ACM Symposium on Principles of Programming Languages, pp. 1–14 (1992) Wadler, P.: The essence of functional programming. In: Proceedings of POPL ’92: ACM Symposium on Principles of Programming Languages, pp. 1–14 (1992)
32.
go back to reference 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) 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)
Metadata
Title
Type-Based Cost Analysis for Lazy Functional Languages
Authors
Steffen Jost
Pedro Vasconcelos
Mário Florido
Kevin Hammond
Publication date
06-01-2017
Publisher
Springer Netherlands
Published in
Journal of Automated Reasoning / Issue 1/2017
Print ISSN: 0168-7433
Electronic ISSN: 1573-0670
DOI
https://doi.org/10.1007/s10817-016-9398-9

Other articles of this Issue 1/2017

Journal of Automated Reasoning 1/2017 Go to the issue

Premium Partner