Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
skip to main content
research-article
Open access

Story of Your Lazy Function’s Life: A Bidirectional Demand Semantics for Mechanized Cost Analysis of Lazy Programs

Published: 15 August 2024 Publication History

Abstract

Lazy evaluation is a powerful tool that enables better compositionality and potentially better performance in functional programming, but it is challenging to analyze its computation cost. Existing works either require manually annotating sharing, or rely on separation logic to reason about heaps of mutable cells. In this paper, we propose a bidirectional demand semantics that allows for extrinsic reasoning about the computation cost of lazy programs without relying on special program logics. To show the effectiveness of our approach, we apply the demand semantics to a variety of case studies including insertion sort, selection sort, Okasaki's banker's queue, and the implicit queue. We formally prove that the banker's queue and the implicit queue are both amortized and persistent using the Rocq Prover (formerly known as Coq). We also propose the reverse physicist's method, a novel variant of the classical physicist's method, which enables mechanized, modular and compositional reasoning about amortization and persistence with the demand semantics.

References

[1]
Andrew W. Appel, Andrew Tolmach, and Michael Clarkson. 2023. Verified Functional Algorithms. Electronic textbook. Version 1.5.4. http://www.cis.upenn.edu/ bcpierce/sf
[2]
Zena M. Ariola, Matthias Felleisen, John Maraist, Martin Odersky, and Philip Wadler. 1995. The Call-by-Need Lambda Calculus. In Conference Record of POPL’95: 22nd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, San Francisco, California, USA, January 23-25, 1995, Ron K. Cytron and Peter Lee (Eds.). ACM Press, 233–246. https://doi.org/10.1145/199448.199507
[3]
Frédéric Besson and Evgeny Makarov. 2023. Micromega: solvers for arithmetic goals over ordered rings. https://coq.inria.fr/doc/v8.17/refman/addendum/micromega.html
[4]
Bror Bjerner and S. Holmström. 1989. A Composition Approach to Time Analysis of First Order Lazy Functional Programs. In Proceedings of the fourth international conference on Functional programming languages and computer architecture, FPCA 1989, London, UK, September 11-13, 1989, Joseph E. Stoy (Ed.). ACM, 157–165. https://doi.org/10.1145/99370.99382
[5]
Joachim Breitner, Antal Spector-Zabusky, Yao Li, Christine Rizkallah, John Wiegley, Joshua M. Cohen, and Stephanie Weirich. 2021. Ready, Set, Verify! Applying hs-to-coqm to real-world Haskell code. J. Funct. Program., 31 (2021), e5. https://doi.org/10.1017/S0956796820000283
[6]
Koen Claessen. 2020. Finger trees explained anew, and slightly simplified (functional pearl). In Proceedings of the 13th ACM SIGPLAN International Symposium on Haskell, Haskell@ICFP 2020, Virtual Event, USA, August 7, 2020, Tom Schrijvers (Ed.). ACM, 31–38. https://doi.org/10.1145/3406088.3409026
[7]
Stephen A. Cook. 1971. The Complexity of Theorem-Proving Procedures. In Proceedings of the Third Annual ACM Symposium on Theory of Computing (STOC ’71). Association for Computing Machinery, New York, NY, USA. 151–158. isbn:9781450374644 https://doi.org/10.1145/800157.805047
[8]
Joseph W. Cutler, Daniel R. Licata, and Norman Danner. 2020. Denotational recurrence extraction for amortized analysis. Proc. ACM Program. Lang., 4, ICFP (2020), 97:1–97:29. https://doi.org/10.1145/3408979
[9]
Nils Anders Danielsson. 2008. Lightweight semiformal time complexity analysis for purely functional data structures. In Proceedings of the 35th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2008, San Francisco, California, USA, January 7-12, 2008, George C. Necula and Philip Wadler (Eds.). ACM, 133–144. https://doi.org/10.1145/1328438.1328457
[10]
Norman Danner, Jennifer Paykin, and James S. Royer. 2013. A static cost analysis for a higher-order language. In Proceedings of the 7th Workshop on Programming languages meets program verification, PLPV 2013, Rome, Italy, January 22, 2013, Matthew Might, David Van Horn, Andreas Abel, and Tim Sheard (Eds.). ACM, 25–34. https://doi.org/10.1145/2428116.2428123
[11]
Danny Dubé and Marc Feeley. 2002. A demand-driven adaptive type analysis. In Proceedings of the Seventh ACM SIGPLAN International Conference on Functional Programming (ICFP ’02), Pittsburgh, Pennsylvania, USA, October 4-6, 2002, Mitchell Wand and Simon L. Peyton Jones (Eds.). ACM, 84–97. https://doi.org/10.1145/581478.581487
[12]
Robert Ennals and Simon Peyton Jones. 2003. Optimistic Evaluation: An Adaptive Evaluation Strategy for Non-Strict Programs. In Proceedings of the Eighth ACM SIGPLAN International Conference on Functional Programming (ICFP ’03). Association for Computing Machinery, New York, NY, USA. 287–298. isbn:1581137567 https://doi.org/10.1145/944705.944731
[13]
Leandro Facchinetti, Zachary Palmer, and Scott F. Smith. 2019. Higher-order Demand-driven Program Analysis. ACM Trans. Program. Lang. Syst., 41, 3 (2019), 14:1–14:53. https://doi.org/10.1145/3310340
[14]
Kenneth Foner, Hengchu Zhang, and Leonidas Lampropoulos. 2018. Keep your laziness in check. Proc. ACM Program. Lang., 2, ICFP (2018), 102:1–102:30. https://doi.org/10.1145/3236797
[15]
Yannick Forster and Gert Smolka. 2017. Weak Call-by-Value Lambda Calculus as a Model of Computation in Coq. In Interactive Theorem Proving. Springer International Publishing, 189–206. https://doi.org/10.1007/978-3-319-66107-0_13
[16]
Lennard Gäher and Fabian Kunze. 2021. Mechanising Complexity Theory: The Cook-Levin Theorem in Coq. In 12th International Conference on Interactive Theorem Proving (ITP 2021), Liron Cohen and Cezary Kaliszyk (Eds.) (Leibniz International Proceedings in Informatics (LIPIcs), Vol. 193). Schloss Dagstuhl – Leibniz-Zentrum für Informatik, Dagstuhl, Germany. 20:1–20:18. isbn:978-3-95977-188-7 issn:1868-8969 https://doi.org/10.4230/LIPIcs.ITP.2021.20
[17]
Kimball Germane, Jay McCarthy, Michael D. Adams, and Matthew Might. 2019. Demand Control-Flow Analysis. In Verification, Model Checking, and Abstract Interpretation - 20th International Conference, VMCAI 2019, Cascais, Portugal, January 13-15, 2019, Proceedings, Constantin Enea and Ruzica Piskac (Eds.) (Lecture Notes in Computer Science, Vol. 11388). Springer, 226–246. https://doi.org/10.1007/978-3-030-11245-5_11
[18]
Harrison Grodin and Robert Harper. 2023. Amortized Analysis via Coinduction. CoRR, abs/2303.16048 (2023), arXiv:2303.16048. arxiv:2303.16048
[19]
Jörgen Gustavsson and David Sands. 2001. Possibilities and Limitations of Call-by-Need Space Improvement. In Proceedings of the Sixth ACM SIGPLAN International Conference on Functional Programming (ICFP ’01), Firenze (Florence), Italy, September 3-5, 2001, Benjamin C. Pierce (Ed.). ACM, 265–276. https://doi.org/10.1145/507635.507667
[20]
Jennifer Hackett and Graham Hutton. 2019. Call-by-need is clairvoyant call-by-value. Proc. ACM Program. Lang., 3, ICFP (2019), 114:1–114:23. https://doi.org/10.1145/3341718
[21]
Martin A. T. Handley, Niki Vazou, and Graham Hutton. 2020. Liquidate your assets: reasoning about resource usage in Liquid Haskell. Proc. ACM Program. Lang., 4, POPL (2020), 24:1–24:27. https://doi.org/10.1145/3371092
[22]
Ralf Hinze and Ross Paterson. 2006. Finger Trees: A Simple General-Purpose Data Structure. J. Funct. Program., 16, 2 (2006), 197–217. https://doi.org/10.1017/S0956796805005769
[23]
Jan Hoffmann, Klaus Aehlig, and Martin Hofmann. 2012. Multivariate amortized resource analysis. ACM Trans. Program. Lang. Syst., 34, 3 (2012), 14:1–14:62. https://doi.org/10.1145/2362389.2362393
[24]
Susan Horwitz, Thomas W. Reps, and Shmuel Sagiv. 1995. Demand Interprocedural Dataflow Analysis. In Proceedings of the Third ACM SIGSOFT Symposium on Foundations of Software Engineering, SIGSOFT 1995, Washington, DC, USA, October 10-13, 1995, Gail E. Kaiser (Ed.). ACM, 104–115. https://doi.org/10.1145/222124.222146
[25]
John Hughes. 1989. Why Functional Programming Matters. Comput. J., 32, 2 (1989), 98–107. https://doi.org/10.1093/comjnl/32.2.98
[26]
Simon Peyton Jones and Will Partain. 1993. Measuring the effectiveness of a simple strictness analyser. In Proceedings of the 1993 Glasgow Workshop on Functional Programming, Ayr, Scotland, UK, July 5-7, 1993, John T. O’Donnell and Kevin Hammond (Eds.) (Workshops in Computing). Springer, 201–221. https://doi.org/10.1007/978-1-4471-3236-3_17
[27]
Ralf Jung, Robbert Krebbers, Jacques-Henri Jourdan, Ales Bizjak, Lars Birkedal, and Derek Dreyer. 2018. Iris from the ground up: A modular foundation for higher-order concurrent separation logic. J. Funct. Program., 28 (2018), e20. https://doi.org/10.1017/S0956796818000151
[28]
Haim Kaplan and Robert Endre Tarjan. 1995. Persistent lists with catenation via recursive slow-down. In Proceedings of the Twenty-Seventh Annual ACM Symposium on Theory of Computing, 29 May-1 June 1995, Las Vegas, Nevada, USA, Frank Thomson Leighton and Allan Borodin (Eds.). ACM, 93–102. https://doi.org/10.1145/225058.225090
[29]
John Launchbury. 1993. A Natural Semantics for Lazy Evaluation. In Conference Record of the Twentieth Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, Charleston, South Carolina, USA, January 1993, Mary S. Van Deusen and Bernard Lang (Eds.). ACM Press, 144–154. https://doi.org/10.1145/158511.158618
[30]
Paul Blain Levy. 2001. Call-by-push-value. Ph. D. Dissertation. Queen Mary University of London, UK. https://ethos.bl.uk/OrderDetails.do?uin=uk.bl.ethos.369233
[31]
Yao Li, Li-yao Xia, and Stephanie Weirich. 2021. Reasoning about the garden of forking paths. Proc. ACM Program. Lang., 5, ICFP (2021), 1–28. https://doi.org/10.1145/3473585
[32]
Jay A. McCarthy, Burke Fetscher, Max S. New, Daniel Feltey, and Robert Bruce Findler. 2018. A Coq library for internal verification of running-times. Sci. Comput. Program., 164 (2018), 49–65. https://doi.org/10.1016/J.SCICO.2017.05.001
[33]
Glen Mével, Jacques-Henri Jourdan, and François Pottier. 2019. Time Credits and Time Receipts in Iris. In Programming Languages and Systems - 28th European Symposium on Programming, ESOP 2019, Held as Part of the European Joint Conferences on Theory and Practice of Software, ETAPS 2019, Prague, Czech Republic, April 6-11, 2019, Proceedings, Luís Caires (Ed.) (Lecture Notes in Computer Science, Vol. 11423). Springer, 3–29. https://doi.org/10.1007/978-3-030-17184-1_1
[34]
Andrew Moran and David Sands. 1999. Improvement in a Lazy Context: An Operational Theory for Call-by-Need. In POPL ’99, Proceedings of the 26th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, San Antonio, TX, USA, January 20-22, 1999, Andrew W. Appel and Alex Aiken (Eds.). ACM, 43–56. https://doi.org/10.1145/292540.292547
[35]
Yue Niu, Jonathan Sterling, Harrison Grodin, and Robert Harper. 2022. A cost-aware logical framework. Proc. ACM Program. Lang., 6, POPL (2022), 1–31. https://doi.org/10.1145/3498670
[36]
Peter W. O’Hearn. 2020. Incorrectness logic. Proc. ACM Program. Lang., 4, POPL (2020), 10:1–10:32. https://doi.org/10.1145/3371078
[37]
Chris Okasaki. 1999. Purely functional data structures. Cambridge University Press. isbn:978-0-521-66350-2
[38]
Zachary Palmer, Theodore Park, Scott F. Smith, and Shiwei Weng. 2020. Higher-order demand-driven symbolic evaluation. Proc. ACM Program. Lang., 4, ICFP (2020), 102:1–102:28. https://doi.org/10.1145/3408984
[39]
Pierre-Marie Pédrot and Nicolas Tabareau. 2020. The fire triangle: how to mix substitution, dependent elimination, and effects. Proc. ACM Program. Lang., 4, POPL (2020), 58:1–58:28. https://doi.org/10.1145/3371126
[40]
François Pottier, Armaël Guéneau, Jacques-Henri Jourdan, and Glen Mével. 2024. Thunks and Debits in Separation Logic with Time Credits. Proc. ACM Program. Lang., 8, POPL (2024), https://hal.science/hal-04238691/file/main.pdf
[41]
Vineet Rajani, Marco Gaboardi, Deepak Garg, and Jan Hoffmann. 2021. A unifying type-theory for higher-order (amortized) cost analysis. Proc. ACM Program. Lang., 5, POPL (2021), 1–28. https://doi.org/10.1145/3434308
[42]
Amr Sabry and Matthias Felleisen. 1992. Reasoning About Programs in Continuation-Passing Style. In Proceedings of the Conference on Lisp and Functional Programming, LFP 1992, San Francisco, California, USA, 22-24 June 1992. ACM, 288–298. https://doi.org/10.1145/141471.141563
[43]
David Sands. 1996. Total Correctness by Local Improvement in the Transformation of Functional Programs. ACM Trans. Program. Lang. Syst., 18, 2 (1996), 175–234. https://doi.org/10.1145/227699.227716
[44]
David Sands. 1997. From SOS Rules to Proof Principles: An Operational Metatheory for Functional Languages. In Conference Record of POPL’97: The 24th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, Papers Presented at the Symposium, Paris, France, 15-17 January 1997, Peter Lee, Fritz Henglein, and Neil D. Jones (Eds.). ACM Press, 428–441. https://doi.org/10.1145/263699.263760
[45]
Ilya Sergey, Simon Peyton Jones, and Dimitrios Vytiniotis. 2014. Theory and practice of demand analysis in Haskell. https://www.microsoft.com/en-us/research/publication/theory-practice-demand-analysis-haskell/ Unpublished draft
[46]
Ilya Sergey, Dimitrios Vytiniotis, Simon L. Peyton Jones, and Joachim Breitner. 2017. Modular, higher order cardinality analysis in theory and practice. J. Funct. Program., 27 (2017), e11. https://doi.org/10.1017/S0956796817000016
[47]
Simon Spies, Lennard Gäher, Joseph Tassarotti, Ralf Jung, Robbert Krebbers, Lars Birkedal, and Derek Dreyer. 2022. Later credits: resourceful reasoning for the later modality. Proc. ACM Program. Lang., 6, ICFP (2022), 283–311. https://doi.org/10.1145/3547631
[48]
Robert Endre Tarjan. 1985. Amortized computational complexity. SIAM Journal on Algebraic Discrete Methods, 6, 2 (1985), 306–318.
[49]
Niki Vazou. 2016. Liquid Haskell: Haskell as a Theorem Prover. Ph. D. Dissertation. University of California, San Diego, USA. http://www.escholarship.org/uc/item/8dm057ws
[50]
Philip Wadler and R. J. M. Hughes. 1987. Projections for strictness analysis. In Functional Programming Languages and Computer Architecture, Portland, Oregon, USA, September 14-16, 1987, Proceedings, Gilles Kahn (Ed.) (Lecture Notes in Computer Science, Vol. 274). Springer, 385–407. https://doi.org/10.1007/3-540-18317-5_21
[51]
Li-yao Xia, Laura Israel, Maite Kramarz, Nicholas Coltharp, Koen Claessen, Stephanie Weirich, and Yao Li. 2024. Story of Your Lazy Function’s Life: A Bidirectional Demand Semantics for Mechanized Cost Analysis of Lazy Programs (Artifact). https://doi.org/10.5281/zenodo.11493754

Recommendations

Comments

Information & Contributors

Information

Published In

cover image Proceedings of the ACM on Programming Languages
Proceedings of the ACM on Programming Languages  Volume 8, Issue ICFP
August 2024
1031 pages
EISSN:2475-1421
DOI:10.1145/3554318
Issue’s Table of Contents
This work is licensed under a Creative Commons Attribution International 4.0 License.

Publisher

Association for Computing Machinery

New York, NY, United States

Publication History

Published: 15 August 2024
Published in PACMPL Volume 8, Issue ICFP

Permissions

Request permissions for this article.

Check for updates

Badges

Author Tags

  1. amortized analysis
  2. computation cost
  3. formal verification
  4. lazy evaluation

Qualifiers

  • Research-article

Funding Sources

  • NSF (National Science Foundation)

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • 0
    Total Citations
  • 238
    Total Downloads
  • Downloads (Last 12 months)238
  • Downloads (Last 6 weeks)114
Reflects downloads up to 10 Nov 2024

Other Metrics

Citations

View Options

View options

PDF

View or Download as a PDF file.

PDF

eReader

View online with eReader.

eReader

Get Access

Login options

Full Access

Media

Figures

Other

Tables

Share

Share

Share this Publication link

Share on social media