Abstract
We present calf, a cost-aware logical framework for studying quantitative aspects of functional programs. Taking inspiration from recent work that reconstructs traditional aspects of programming languages in terms of a modal account of phase distinctions, we argue that the cost structure of programs motivates a phase distinction between intension and extension. Armed with this technology, we contribute a synthetic account of cost structure as a computational effect in which cost-aware programs enjoy an internal noninterference property: input/output behavior cannot depend on cost. As a full-spectrum dependent type theory, calf presents a unified language for programming and specification of both cost and behavior that can be integrated smoothly with existing mathematical libraries available in type theoretic proof assistants.
We evaluate calf as a general framework for cost analysis by implementing two fundamental techniques for algorithm analysis: the method of recurrence relations and physicist’s method for amortized analysis. We deploy these techniques on a variety of case studies: we prove a tight, closed bound for Euclid’s algorithm, verify the amortized complexity of batched queues, and derive tight, closed bounds for the sequential and parallel complexity of merge sort, all fully mechanized in the Agda proof assistant. Lastly we substantiate the soundness of quantitative reasoning in calf by means of a model construction.
Supplemental Material
- Umut A. Acar and Guy E. Blelloch. 2019. Algorithms: Parallel and Sequential. http:www.algorithms-book.comGoogle Scholar
- Thorsten Altenkirch and Ambrus Kaposi. 2016. Normalisation by Evaluation for Dependent Types. In 1st International Conference on Formal Structures for Computation and Deduction (FSCD 2016), Delia Kesner and Brigitte Pientka (Eds.) (Leibniz International Proceedings in Informatics (LIPIcs), Vol. 52). Schloss Dagstuhl–Leibniz-Zentrum fuer Informatik, Dagstuhl, Germany. 6:1–6:16. isbn:978-3-95977-010-1 issn:1868-8969 https://doi.org/10.4230/LIPIcs.FSCD.2016.6 Google Scholar
Cross Ref
- Thorsten Altenkirch and Ambrus Kaposi. 2016. Type Theory in Type Theory Using Quotient Inductive Types. In Proceedings of the 43rd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’16). Association for Computing Machinery, St. Petersburg, FL, USA. 18–29. isbn:978-1-4503-3549-2 https://doi.org/10.1145/2837614.2837638 Google Scholar
Digital Library
- Mathieu Anel and André Joyal. 2021. Topo-logie. In New Spaces in Mathematics: Formal and Conceptual Reflections, Mathieu Anel and Gabriel Catren (Eds.). 1, Cambridge University Press, 155–257. https://doi.org/10.1017/9781108854429.007 Google Scholar
Cross Ref
- Robert Atkey. 2010. Amortised Resource Analysis with Separation Logic. In Programming Languages and Systems, Andrew D. Gordon (Ed.). Springer Berlin Heidelberg, Berlin, Heidelberg. 85–103. isbn:978-3-642-11957-6Google Scholar
- Lars Birkedal, Rasmus Ejlers Møgelberg, Jan Schwinghammer, and Kristian Støvring. 2011. First Steps in Synthetic Guarded Domain Theory: Step-Indexing in the Topos of Trees. In Proceedings of the 2011 IEEE 26th Annual Symposium on Logic in Computer Science. IEEE Computer Society, Washington, DC, USA. 55–64. isbn:978-0-7695-4412-0 https://doi.org/10.1109/LICS.2011.16 arXiv:1208.3596. Google Scholar
Digital Library
- Aleš Bizjak, Hans Bugge Grathwohl, Ranald Clouston, Rasmus E. Møgelberg, and Lars Birkedal. 2016. Guarded Dependent Type Theory with Coinductive Types. In Foundations of Software Science and Computation Structures: 19th International Conference, FOSSACS 2016, Held as Part of the European Joint Conferences on Theory and Practice of Software, ETAPS 2016, Eindhoven, The Netherlands, April 2–8, 2016, Proceedings, Bart Jacobs and Christof Löding (Eds.). Springer Berlin Heidelberg, Berlin, Heidelberg. 20–35. isbn:978-3-662-49630-5 https://doi.org/10.1007/978-3-662-49630-5_2 arXiv:1601.01586. Google Scholar
Cross Ref
- Guy Blelloch and John Greiner. 1995. Parallelism in Sequential Functional Languages. In Proceedings of the Seventh International Conference on Functional Programming Languages and Computer Architecture. Association for Computing Machinery, La Jolla, California, USA. 226–237. isbn:0-89791-719-7 https://doi.org/10.1145/224164.224210 Google Scholar
Digital Library
- Guy E. Blelloch and John Greiner. 1996. A Provable Time and Space Efficient Implementation of NESL. In Proceedings of the First ACM SIGPLAN International Conference on Functional Programming. Association for Computing Machinery, Philadelphia, Pennsylvania, USA. 213–225. isbn:0-89791-770-7 https://doi.org/10.1145/232627.232650 Google Scholar
Digital Library
- Ana Bove and Venanzio Capretta. 2005. Modelling general recursion in type theory. Mathematical Structures in Computer Science, 15, 4 (2005), 671–708. https://doi.org/10.1017/S0960129505004822 Google Scholar
Digital Library
- Edwin Brady. 2013. Idris, a general-purpose dependently typed programming language: Design and implementation. Journal of Functional Programming, 23, 5 (2013), Sept., 552–593. https://doi.org/10.1017/S095679681300018X Google Scholar
Cross Ref
- F. Burton. 1982. An Efficient Functional Implementation of FIFO Queues. Inf. Process. Lett., 14 (1982), 205–206.Google Scholar
Cross Ref
- Kevin Buzzard, Johan Commelin, and Patrick Massot. 2020. Formalising Perfectoid Spaces. In Proceedings of the 9th ACM SIGPLAN International Conference on Certified Programs and Proofs. Association for Computing Machinery, New Orleans, LA, USA. 299–312. isbn:978-1-4503-7097-4 https://doi.org/10.1145/3372885.3373830 Google Scholar
Digital Library
- Venanzio Capretta. 2005. General Recursion via Coinductive Types. Logical Methods in Computer Science, 1, 2 (2005), 1–18. issn:1860-5974Google Scholar
Cross Ref
- Adam Chlipala. 2013. Certified Programming with Dependent Types: A Pragmatic Introduction to the Coq Proof Assistant. The MIT Press. isbn:0-262-02665-1Google Scholar
Digital Library
- R. L. Constable, S. F. Allen, H. M. Bromley, W. R. Cleaveland, J. F. Cremer, R. W. Harper, D. J. Howe, T. B. Knoblock, N. P. Mendler, P. Panangaden, J. T. Sasaki, and S. F. Smith. 1986. Implementing Mathematics with the Nuprl Proof Development System. Prentice-Hall, Inc., Upper Saddle River, NJ, USA. isbn:0-13-451832-2Google Scholar
- Robert L. Constable and Karl Crary. 2002. Computational complexity and induction for partial computable functions in type theory. Cambridge University Press, Cambridge. 164–181. https://doi.org/10.1017/9781316755983.009 Google Scholar
Cross Ref
- Robert L. Constable and Daniel R. Zlatin. 1984. The Type Theory of PL/CV3. ACM Transactions on Programming Languages and Systems, 6, 1 (1984), Jan., 94–117. issn:0164-0925 https://doi.org/10.1145/357233.357238 Google Scholar
Digital Library
- The Coq Development Team. 2016. The Coq Proof Assistant Reference Manual.Google Scholar
- Thierry Coquand. 2019. Canonicity and normalization for dependent type theory. Theoretical Computer Science, 777 (2019), 184–191. issn:0304-3975 https://doi.org/10.1016/j.tcs.2019.01.015 arXiv:1810.09367. In memory of Maurice Nivat, a founding father of Theoretical Computer Science - Part I. Google Scholar
Digital Library
- Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. 2009. Introduction to Algorithms, 3rd Edition. MIT Press. isbn:978-0-262-03384-8 http://mitpress.mit.edu/books/introduction-algorithmsGoogle Scholar
Digital Library
- Karl Crary and Stephanie Weirich. 2000. Resource Bound Certification. In Proceedings of the 27th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages. Association for Computing Machinery, Boston, MA, USA. 184–198. isbn:1-58113-125-9 https://doi.org/10.1145/325694.325716 Google Scholar
Digital Library
- Nils Anders Danielsson. 2008. Lightweight Semiformal Time Complexity Analysis for Purely Functional Data Structures. In Proceedings of the 35th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’08). Association for Computing Machinery, New York, NY, USA. 133–144. isbn:9781595936899 https://doi.org/10.1145/1328438.1328457 Google Scholar
Digital Library
- Norman Danner, Daniel R. Licata, and Ramyaa. 2015. Denotational cost semantics for functional languages with inductive types. In Proceedings of the 20th ACM SIGPLAN International Conference on Functional Programming, ICFP 2015, Vancouver, BC, Canada, September 1-3, 2015, Kathleen Fisher and John H. Reppy (Eds.). Association for Computing Machinery, 140–151. https://doi.org/10.1145/2784731.2784749 Google Scholar
Digital Library
- Ankush Das, Jan Hoffmann, and Frank Pfenning. 2018. Parallel Complexity Analysis with Temporal Session Types. In Proceedings of International Conference on Functional Programming (ICFP 2018), M. Flatt (Ed.). ACM, St. Louis, Missouri, USA. 91:1–91:30.Google Scholar
Digital Library
- Ankush Das, Jan Hoffmann, and Frank Pfenning. 2018. Work Analysis with Resource-Aware Session Types. In Proceedings of 33rd Symposium on Logic in Computer Science (LICS 2018), A. Dawar and E. Grädel (Eds.). Oxford, UK. 305–314.Google Scholar
Digital Library
- Ankush Das and Frank Pfenning. 2020. Rast: A Language for Resource-Aware Session Types. CoRR, abs/2012.13129 (2020), Dec., arxiv:2012.13129 Submitted.Google Scholar
- Rowan Davies and Frank Pfenning. 1999. A Modal Analysis of Staged Computation. J. ACM, 48 (1999), Sept., https://doi.org/10.1145/382780.382785 Google Scholar
Digital Library
- Manuel Eberl. 2015. The Akra-Bazzi theorem and the Master theorem. Archive of Formal Proofs, July, issn:2150-914x https://isa-afp.org/entries/Akra_Bazzi.htmlGoogle Scholar
- Manuel Eberl. 2017. The Median-of-Medians Selection Algorithm. Archive of Formal Proofs, Dec., issn:2150-914x https://isa-afp.org/entries/Median_Of_Medians_Selection.htmlGoogle Scholar
- Manuel Eberl. 2017. The number of comparisons in QuickSort. Archive of Formal Proofs, March, issn:2150-914x https://isa-afp.org/entries/Quick_Sort_Cost.htmlGoogle Scholar
- Marcelo P. Fiore, Andrew M. Pitts, and S. C. Steenkamp. 2021. Quotients, inductive types, and quotient inductive types. arXiv:2101.02994.Google Scholar
- G. Gonthier. 2008. Formal Proof — The Four-Color Theorem. Notices of the AMS, 55, 11 (2008), https://www.ams.org/notices/200811/tx081101382p.pdfGoogle Scholar
- Daniel Gratzer and Jonathan Sterling. 2020. Syntactic categories for dependent type theory: sketching and adequacy. arXiv:2012.10783.Google Scholar
- John Greiner and Guy E. Blelloch. 1999. A Provably Time-Efficient Parallel Implementation of Full Speculation. ACM Transactions on Programming Languages and Systems, 21, 2 (1999), March, 240–285. issn:0164-0925 https://doi.org/10.1145/316686.316690 Google Scholar
Digital Library
- David Gries. 1987. The Science of Programming (1st ed.). Springer-Verlag, Berlin, Heidelberg. isbn:0-387-96480-0Google Scholar
- Jesse Michael Han and Floris van Doorn. 2020. A Formal Proof of the Independence of the Continuum Hypothesis. In Proceedings of the 9th ACM SIGPLAN International Conference on Certified Programs and Proofs. Association for Computing Machinery, New Orleans, LA, USA. 353–366. isbn:978-1-4503-7097-4 https://doi.org/10.1145/3372885.3373826 Google Scholar
Digital Library
- Martin A. T. Handley, Niki Vazou, and Graham Hutton. 2019. Liquidate Your Assets: Reasoning about Resource Usage in Liquid Haskell. Proceedings of the ACM on Programming Languages, 4, POPL (2019), Dec., https://doi.org/10.1145/3371092 Google Scholar
Digital Library
- Robert Harper. 2018. PFPL Supplement: Types and Parallelism. https://www.cs.cmu.edu/~rwh/pfpl/supplements/par.pdfGoogle Scholar
- Robert Harper, John C. Mitchell, and Eugenio Moggi. 1990. Higher-Order Modules and the Phase Distinction. In Proceedings of the 17th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages. Association for Computing Machinery, San Francisco, California, USA. 341–354. isbn:0-89791-343-4 https://doi.org/10.1145/96709.96744 Google Scholar
Digital Library
- Jan Hoffmann, Klaus Aehlig, and Martin Hofmann. 2012. Resource Aware ML. In Computer Aided Verification, P. Madhusudan and Sanjit A. Seshia (Eds.). Springer Berlin Heidelberg, Berlin, Heidelberg. 781–786. isbn:978-3-642-31424-7Google Scholar
- Martin Hofmann. 2000. A Type System for Bounded Space and Functional In-Place Update–Extended Abstract. In Proceedings of the 9th European Symposium on Programming Languages and Systems. Springer-Verlag, Berlin, Heidelberg. 165–179. isbn:3-540-67262-1Google Scholar
Cross Ref
- Martin Hofmann and Steffen Jost. 2003. 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. Association for Computing Machinery, New Orleans, Louisiana, USA. 185–197. isbn:1-58113-628-5 https://doi.org/10.1145/604131.604148 Google Scholar
Digital Library
- Robert Hood and Robert Melville. 1981. Real-time queue operations in pure LISP. Inform. Process. Lett., 13, 2 (1981), 50–54. issn:0020-0190 https://doi.org/10.1016/0020-0190(81)90030-2 Google Scholar
Cross Ref
- Steffen Jost, Kevin Hammond, Hans-Wolfgang Loidl, and Martin Hofmann. 2010. Static determination of quantitative resource usage for higher-order programs. In Proceedings of the 37th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2010, Madrid, Spain, January 17-23, 2010, Manuel V. Hermenegildo and Jens Palsberg (Eds.). Association for Computing Machinery, 223–236. https://doi.org/10.1145/1706299.1706327 Google Scholar
Digital Library
- Ralf Jung, Robbert Krebbers, Jacques-Henri Jourdan, Aleš Bizjak, Lars Birkedal, and Derek Dreyer. 2018. Iris from the ground up: A modular foundation for higher-order concurrent separation logic. Journal of Functional Programming, 28 (2018), e20. https://doi.org/10.1017/S0956796818000151 Google Scholar
Cross Ref
- Ralf Jung, David Swasey, Filip Sieczkowski, Kasper Svendsen, Aaron Turon, Lars Birkedal, and Derek Dreyer. 2015. Iris: Monoids and Invariants As an Orthogonal Basis for Concurrent Reasoning. In POPL ’15: Proceedings of the 42nd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages. Association for Computing Machinery, Mumbai, India. 637–650. isbn:978-1-4503-3300-9 https://doi.org/10.1145/2676726.2676980 Google Scholar
Digital Library
- G. A. Kavvos. 2017. Dual-Context Calculi for Modal Logic. In Proceedings of the 32nd Annual ACM/IEEE Symposium on Logic in Computer Science (LICS). arXiv:1602.04860. arxiv:1602.04860Google Scholar
Cross Ref
- G. A. Kavvos. 2017. On the Semantics of Intensionality and Intensional Recursion. Ph.D. Dissertation. arXiv:1712.09302.Google Scholar
- G. A. Kavvos, Edward Morehouse, Daniel R. Licata, and Norman Danner. 2019. Recurrence Extraction for Functional Programs through Call-by-Push-Value. Proceedings of the ACM on Programming Languages, 4, POPL (2019), Dec., https://doi.org/10.1145/3371083 Google Scholar
Digital Library
- S. C. Kleene. 1943. Recursive predicates and quantifiers. Trans. Amer. Math. Soc., 53 (1943), 41–73. issn:0002-9947 https://doi.org/10.2307/1990131 Google Scholar
Cross Ref
- F. William Lawvere. 1963. Functorial Semantics of Algebraic Theories. Ph.D. Dissertation. Columbia University.Google Scholar
- Daniel K. Lee, Karl Crary, and Robert Harper. 2007. Towards a Mechanized Metatheory of Standard ML. In Proceedings of the 34th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages. Association for Computing Machinery, Nice, France. 173–184. isbn:1-59593-575-4 https://doi.org/10.1145/1190216.1190245 Google Scholar
Digital Library
- Paul Blain Levy. 2004. Call-By-Push-Value: A Functional/Imperative Synthesis (Semantics Structures in Computation, V. 2). Kluwer Academic Publishers, Norwell, MA, USA. isbn:1-4020-1730-8Google Scholar
- Paul Blain Levy. 2006. Call-by-push-value: Decomposing call-by-value and call-by-name. Higher-Order and Symbolic Computation, 19 (2006), 377–414. https://doi.org/10.1007/s10990-006-0480-6 Google Scholar
Digital Library
- Glen Mével, Jacques-Henri Jourdan, and François Pottier. 2019. Time Credits and Time Receipts in Iris. In Programming Languages and Systems, Luís Caires (Ed.). Springer International Publishing, Cham. 3–29. isbn:978-3-030-17184-1Google Scholar
- Tobias Nipkow, Jasmin Blanchette, Manuel Eberl, Alejandro Gómez Londoño, Peter Lammich, Christian Sternagel, Simon Wimmer, and Bohua Zhan. 2021. Functional Algorithms, Verified!. https://functional-algorithms-verified.orgGoogle Scholar
- Yue Niu and Robert Harper. 2020. Cost-Aware Type Theory. arXiv:2011.03660.Google Scholar
- Yue Niu, Jonathan Sterling, Harrison Grodin, and Robert Harper. 2021. agda-calf. https://doi.org/10.1145/3462303 Google Scholar
Digital Library
- Ulf Norell. 2009. Dependently Typed Programming in Agda. In Proceedings of the 4th International Workshop on Types in Language Design and Implementation (TLDI ’09). Association for Computing Machinery, Savannah, GA, USA. 1–2. isbn:978-1-60558-420-1Google Scholar
Cross Ref
- Chris Okasaki. 1998. Purely Functional Data Structures. Cambridge University Press, USA. isbn:0-521-63124-6Google Scholar
Digital Library
- Marco Paviotti, Rasmus Ejlers Møgelberg, and Lars Birkedal. 2015. A Model of PCF in Guarded Type Theory. Electronic Notes in Theoretical Computer Science, 319, Supplement C (2015), 333–349. issn:1571-0661 https://doi.org/10.1016/j.entcs.2015.12.020 The 31st Conference on the Mathematical Foundations of Programming Semantics (MFPS XXXI). Google Scholar
Digital Library
- Pierre-Marie Pédrot and Nicolas Tabareau. 2019. The Fire Triangle: How to Mix Substitution, Dependent Elimination, and Effects. Proceedings of the ACM on Programming Languages, 4, POPL (2019), Dec., https://doi.org/10.1145/3371126 Google Scholar
Digital Library
- Frank Pfenning. 2001. Intensionality, Extensionality, and Proof Irrelevance in Modal Type Theory. In Proceedings of the 16th Annual IEEE Symposium on Logic in Computer Science. IEEE Computer Society, Washington, DC, USA. 221–. issn:1043-6871 http://dl.acm.org/citation.cfm?id=871816.871845Google Scholar
Digital Library
- G.D. Plotkin. 1977. LCF considered as a programming language. Theoretical Computer Science, 5, 3 (1977), 223–255. issn:0304-3975 https://doi.org/10.1016/0304-3975(77)90044-5 Google Scholar
Cross Ref
- Vineet Rajani, Marco Gaboardi, Deepak Garg, and Jan Hoffmann. 2021. A Unifying Type-Theory for Higher-Order (Amortized) Cost Analysis. Proceedings of the ACM on Programming Languages, 5, POPL (2021), Jan., https://doi.org/10.1145/3434308 Google Scholar
Digital Library
- Egbert Rijke, Michael Shulman, and Bas Spitters. 2020. Modalities in homotopy type theory. Logical Methods in Computer Science, Volume 16, Issue 1 (2020), Jan., https://doi.org/10.23638/LMCS-16(1:2)2020 arXiv:1706.07526. Google Scholar
Cross Ref
- Patrick Schultz and David I. Spivak. 2019. Temporal Type Theory (Progress in Computer Science and Applied Logic, Vol. 29). Birkhäuser Basel. isbn:978-3-030-00703-4 issn:2297-0576 https://doi.org/10.1007/978-3-030-00704-1 arXiv:1710.10258. Google Scholar
Cross Ref
- Daniel Spoonhower, Guy E. Blelloch, Robert Harper, and Phillip B. Gibbons. 2008. Space Profiling for Parallel Functional Programs. In Proceedings of the 13th ACM SIGPLAN International Conference on Functional Programming. Association for Computing Machinery, Victoria, BC, Canada. 253–264. isbn:978-1-59593-919-7 https://doi.org/10.1145/1411204.1411240 Google Scholar
Digital Library
- Jonathan Sterling and Carlo Angiuli. 2021. Normalization for Cubical Type Theory. In 2021 36th Annual ACM/IEEE Symposium on Logic in Computer Science (LICS). IEEE Computer Society, Los Alamitos, CA, USA. 1–15. https://doi.org/10.1109/LICS52264.2021.9470719 arXiv:2101.11479. Google Scholar
Digital Library
- Jonathan Sterling and Robert Harper. 2021. Logical Relations as Types: Proof-Relevant Parametricity for Program Modules. J. ACM, 68, 6 (2021), Oct., issn:0004-5411 https://doi.org/10.1145/3474834 arXiv:2010.08599. Google Scholar
Digital Library
- Aaron Stump. 2016. Verified Functional Programming in Agda. Association for Computing Machinery and Morgan & Claypool. isbn:978-1-970001-27-3Google Scholar
Digital Library
- R. Tarjan. 1985. Amortized Computational Complexity. Siam Journal on Algebraic and Discrete Methods, 6 (1985), 306–318.Google Scholar
Digital Library
- Taichi Uemura. 2019. A General Framework for the Semantics of Type Theory. arXiv:1904.04097.Google Scholar
- Sebastian Andreas Ullrich. 2016. Simple Verification of Rust Programs via Functional Purification. Master’s thesis. IPD Snelting.Google Scholar
- Peng Wang, Di Wang, and Adam Chlipala. 2017. TiML: A Functional Language for Practical Complexity Analysis with Invariants. Proceedings of the ACM on Programming Languages, 1, OOPSLA (2017), Oct., https://doi.org/10.1145/3133903 Google Scholar
Digital Library
Index Terms
A cost-aware logical framework
Recommendations
POPLMark reloaded: mechanizing logical relations proofs (invited talk)
CPP 2018: Proceedings of the 7th ACM SIGPLAN International Conference on Certified Programs and ProofsMechanizing formal systems, given via axioms and inference rules, together with proofs about them plays an important role in establishing trust in formal developments. Over the past decade, the POPLMark challenge popularized the use of proof assistants ...
Extensional Higher-Order Paramodulation in Leo-III
AbstractLeo-III is an automated theorem prover for extensional type theory with Henkin semantics and choice. Reasoning with primitive equality is enabled by adapting paramodulation-based proof search to higher-order logic. The prover may cooperate with ...
Soundness and Completeness Proofs by Coinductive Methods
We show how codatatypes can be employed to produce compact, high-level proofs of key results in logic: the soundness and completeness of proof systems for variations of first-order logic. For the classical completeness result, we first establish an ...






Comments