skip to main content

A Type-Based Approach to Divide-and-Conquer Recursion in Coq

Published:11 January 2023Publication History
Skip Abstract Section

Abstract

This paper proposes a new approach to writing and verifying divide-and-conquer programs in Coq. Extending the rich line of previous work on algebraic approaches to recursion schemes, we present an algebraic approach to divide-and-conquer recursion: recursions are represented as a form of algebra, and from outer recursions, one may initiate inner recursions that can construct data upon which the outer recursions may legally recurse. Termination is enforced entirely by the typing discipline of our recursion schemes. Despite this, our approach requires little from the underlying type system, and can be implemented in System Fω plus a limited form of positive-recursive types. Our implementation of the method in Coq does not rely on structural recursion or on dependent types. The method is demonstrated on several examples, including mergesort, quicksort, Harper’s regular-expression matcher, and others. An indexed version is also derived, implementing a form of divide-and-conquer induction that can be used to reason about functions defined via our method.

References

  1. Pedro Abreu, Benjamin Delaware, Alex Hubers, Christa Jenkins, J. Garrett Morris, and Aaron Stump. 2023. A Type-Based Approach to Divide-And-Conquer Recursion in Coq: POPL'23 Artifact. Zenodo. https://doi.org/10.5281/zenodo.7305612 Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. Reynald Afeldt and David Nowak. 2021. Extending Equational Monadic Reasoning with Monad Transformers. In 26th International Conference on Types for Proofs and Programs (TYPES 2020 ) (Leibniz International Proceedings in Informatics (LIPIcs), Vol. 188 ), Ugo de'Liguoro, Stefano Berardi, and Thorsten Altenkirch (Eds.). Schloss Dagstuhl-Leibniz-Zentrum für Informatik, Dagstuhl, Germany, 2 : 1-2 : 21. https://doi.org/10.4230/LIPIcs.TYPES. 2020.2 Google ScholarGoogle ScholarCross RefCross Ref
  3. Ki Yung Ahn and Tim Sheard. 2011. A Hierarchy of Mendler Style Recursion Combinators: Taming Inductive Datatypes with Negative Occurrences. In Proceedings of the 16th ACM SIGPLAN International Conference on Functional Programming (Tokyo, Japan) ( ICFP '11). ACM, New York, NY, USA, 234-246. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Robert Atkey. 2012. Relational Parametricity for Higher Kinds. In Computer Science Logic (CSL'12)-26th International Workshop/21st Annual Conference of the EACSL, CSL 2012, September 3-6, 2012, Fontainebleau, France (LIPIcs, Vol. 16 ), Patrick Cégielski and Arnaud Durand (Eds.). Schloss Dagstuhl-Leibniz-Zentrum für Informatik, 46-61. https://doi.org/ 10.4230/LIPIcs.CSL. 2012.46 Google ScholarGoogle ScholarCross RefCross Ref
  5. Gilles Barthe, Julien Forest, David Pichardie, and Vlad Rusu. 2006. Defining and Reasoning About Recursive Functions: A Practical Tool for the Coq Proof Assistant. In Functional and Logic Programming, 8th International Symposium, FLOPS 2006, Fuji-Susono, Japan, April 24-26, 2006, Proceedings (Lecture Notes in Computer Science, Vol. 3945 ), Masami Hagiya and Philip Wadler (Eds.). Springer, 114-129. https://doi.org/10.1007/11737414_9 Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Gilles Barthe, Maria João Frade, Eduardo Giménez, Luís Pinto, and Tarmo Uustalu. 2004a. Type-based termination of recursive definitions. Mathematical Structures in Computer Science 14, 1 ( 2004 ), 97-141. https://doi.org/10.1017/S0960129503004122 Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. Gilles Barthe, Maria João Frade, Eduardo Giménez, Luís Pinto, and Tarmo Uustalu. 2004b. Type-based termination of recursive definitions. Math. Struct. Comput. Sci. 14, 1 ( 2004 ), 97-141. https://doi.org/10.1017/S0960129503004122 Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. J. L. Bell. 1993. Hilbert's epsilon-Operator and Classical Logic. Journal of Philosophical Logic 22, 1 (Feb 01 1993 ), 1. http://login.proxy.lib.uiowa.edu/login?url=https://www.proquest.com/scholarly-journals/hilberts-epsilon-operatorclassical-logic/docview/1292914019/se-2 Last updated-2013-02-22. Google ScholarGoogle ScholarCross RefCross Ref
  9. Jean-Philippe Bernardy and Marc Lasson. 2011. Realizability and Parametricity in Pure Type Systems. In Foundations of Software Science and Computational Structures-14th International Conference, FOSSACS 2011, Held as Part of the Joint European Conferences on Theory and Practice of Software, ETAPS 2011, Saarbrücken, Germany, March 26-April 3, 2011. Proceedings (Lecture Notes in Computer Science, Vol. 6604 ), Martin Hofmann (Ed.). Springer, 108-122. https: //doi.org/10.1016/j.tcs. 2006. 12.042 Google ScholarGoogle ScholarCross RefCross Ref
  10. Julian Biendarra, Jasmin Christian Blanchette, Aymeric Bouzy, Martin Desharnais, Mathias Fleury, Johannes Hölzl, Ondrej Kuncar, Andreas Lochbihler, Fabian Meier, Lorenz Panny, Andrei Popescu, Christian Sternagel, René Thiemann, and Dmitriy Traytel. 2017. Foundational (Co)datatypes and (Co)recursion for Higher-Order Logic. In Frontiers of Combining Systems-11th International Symposium, FroCoS 2017, Brasília, Brazil, September 27-29, 2017, Proceedings (Lecture Notes in Computer Science, Vol. 10483 ), Clare Dixon and Marcelo Finger (Eds.). Springer, 3-21. https://doi.org/10.1007/978-3-319-66167-4_1 Google ScholarGoogle ScholarCross RefCross Ref
  11. Richard S. Bird and Oege de Moor. 1997. Algebra of programming. Prentice Hall. Google ScholarGoogle Scholar
  12. Frédéric Blanqui. 2005. Inductive types in the Calculus of Algebraic Constructions. Fundam. Informaticae 65, 1-2 ( 2005 ), 61-86. http://content.iospress.com/articles/fundamenta-informaticae /fi65-1-2-04 Google ScholarGoogle Scholar
  13. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  14. Ana Bove, Alexander Krauss, and Matthieu Sozeau. 2016. Partiality and recursion in interactive theorem provers-an overview. Mathematical Structures in Computer Science 26, 1 ( 2016 ), 38-88. https://doi.org/10.1017/S0960129514000115 Google ScholarGoogle ScholarCross RefCross Ref
  15. Joachim Breitner, Antal Spector-Zabusky, Yao Li, Christine Rizkallah, John Wiegley, Joshua M. Cohen, and Stephanie Weirich. 2021. Ready, Set, Verify! Applying hs-to-coq to real-world Haskell code. J. Funct. Program. 31 ( 2021 ), e5. https://doi.org/10.1017/S0956796820000283 Google ScholarGoogle ScholarCross RefCross Ref
  16. Jonathan Chan and William J. Bowman. 2019. Practical Sized Typing for Coq. CoRR abs/ 1912.05601 ( 2019 ). arXiv: 1912.05601 http://arxiv.org/abs/ 1912.05601 Google ScholarGoogle Scholar
  17. Arthur Charguéraud. 2010. The Optimal Fixed Point Combinator. In Interactive Theorem Proving, Matt Kaufmann and Lawrence C. Paulson (Eds.). Springer Berlin Heidelberg, Berlin, Heidelberg, 195-210. Google ScholarGoogle Scholar
  18. Arthur Charguéraud. 2021. The TLC Coq Library. https://github.com/charguer/tlc Google ScholarGoogle Scholar
  19. Guillaume Claret. 2021. Coq of Ocaml. https://github.com/clarus/coq-of-ocaml. Accessed: 2021-09-09. Google ScholarGoogle Scholar
  20. Robin Cockett and Dwight Spencer. 1992. Strong Categorical Datatypes I. In International Meeting on Category Theory 1991 (Canadian Mathematical Society Proceedings), R. A. G. Seely (Ed.). AMS. Google ScholarGoogle Scholar
  21. Ernesto Copello, Alvaro Tasistro, and Bruno Bianchi. 2014. Case of (Quite) Painless Dependently Typed Programming: Fully Certified Merge Sort in Agda. In Programming Languages-18th Brazilian Symposium, SBLP 2014, Maceio, Brazil, October 2-3, 2014. Proceedings (Lecture Notes in Computer Science, Vol. 8771 ), Fernando Magno Quintão Pereira (Ed.). Springer, 62-76. Google ScholarGoogle ScholarCross RefCross Ref
  22. T. Coquand and G. Huet. 1988. The Calculus of Constructions. Information and Computation 76, 2-3 ( 1988 ), 95-120. Google ScholarGoogle ScholarDigital LibraryDigital Library
  23. Thierry Coquand and Christine Paulin. 1988. Inductively defined types. In COLOG-88, International Conference on Computer Logic, Tallinn, USSR, December 1988, Proceedings (Lecture Notes in Computer Science, Vol. 417 ), Per Martin-Löf and Grigori Mints (Eds.). Springer, 50-66. https://doi.org/10.1007/3-540-52335-9_47 Google ScholarGoogle ScholarCross RefCross Ref
  24. Leonardo Mendonça de Moura, Soonho Kong, Jeremy Avigad, Floris van Doorn, and Jakob von Raumer. 2015. The Lean Theorem Prover (System Description). In Automated Deduction-CADE-25-25th International Conference on Automated Deduction, Berlin, Germany, August 1-7, 2015, Proceedings (Lecture Notes in Computer Science, Vol. 9195 ), Amy P. Felty and Aart Middeldorp (Eds.). Springer, 378-388. https://doi.org/10.1007/978-3-319-21401-6_26 Google ScholarGoogle ScholarCross RefCross Ref
  25. Benjamin Delaware, Bruno C. d. S. Oliveira, and Tom Schrijvers. 2013a. Meta-Theory à La Carte. SIGPLAN Not. 48, 1 (jan 2013 ), 207-218. https://doi.org/10.1145/2480359.2429094 Google ScholarGoogle ScholarDigital LibraryDigital Library
  26. Benjamin Delaware, Steven Keuchel, Tom Schrijvers, and Bruno C.d. S. Oliveira. 2013b. Modular Monadic Meta-Theory. In Proceedings of the 18th ACM SIGPLAN International Conference on Functional Programming (Boston, Massachusetts, USA) ( ICFP '13). Association for Computing Machinery, New York, NY, USA, 319-330. https://doi.org/10.1145/2500365.2500587 Google ScholarGoogle ScholarDigital LibraryDigital Library
  27. Denis Firsov, Richard Blair, and Aaron Stump. 2018. Eficient Mendler-Style Lambda-Encodings in Cedille. In Interactive Theorem Proving-9th International Conference, ITP 2018, Held as Part of the Federated Logic Conference, FloC 2018, Oxford, UK, July 9-12, 2018, Proceedings (Lecture Notes in Computer Science, Vol. 10895 ), Jeremy Avigad and Assia Mahboubi (Eds.). Springer, 235-252. Google ScholarGoogle ScholarCross RefCross Ref
  28. Denis Firsov and Aaron Stump. 2018. Generic derivation of induction for impredicative encodings in Cedille. In Proceedings of the 7th ACM SIGPLAN International Conference on Certified Programs and Proofs, CPP 2018, Los Angeles, CA, USA, January 8-9, 2018, June Andronick and Amy P. Felty (Eds.). ACM, 215-227. Google ScholarGoogle Scholar
  29. Gaëtan Gilbert, Jesper Cockx, Matthieu Sozeau, and Nicolas Tabareau. 2019. Definitional proof-irrelevance without K. Proc. ACM Program. Lang. 3, POPL ( 2019 ), 3 : 1-3 : 28. https://doi.org/10.1145/3290316 Google ScholarGoogle ScholarDigital LibraryDigital Library
  30. Tatsuya Hagino. 1987. A Categorical Programming Language. Ph. D. Dissertation. University of Edinburgh. Google ScholarGoogle Scholar
  31. Robert Harper. 1999. Proof-directed debugging. Journal of Functional Programming 9, 4 ( 1999 ), 463-469. https://doi.org/10. 1017/S0956796899003378 Google ScholarGoogle ScholarDigital LibraryDigital Library
  32. John Hughes, Lars Pareto, and Amr Sabry. 1996. Proving the Correctness of Reactive Systems Using Sized Types. In Proceedings of the 23rd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (St. Petersburg Beach, Florida, USA) ( POPL '96). Association for Computing Machinery, New York, NY, USA, 410-423. https://doi.org/10.1145/237721.240882 Google ScholarGoogle ScholarDigital LibraryDigital Library
  33. Joomy Korkut, Maksim Trifunovski, and Daniel Licata. 2016. Intrinsic Verification of a Regular Expression Matcher. Unpublished, available from Licata's web site. Google ScholarGoogle Scholar
  34. Alexander Krauss. 2010. Partial and Nested Recursive Function Definitions in Higher-order Logic. J. Autom. Reasoning 44, 4 ( 2010 ), 303-336. https://doi.org/10.1007/s10817-009-9157-2 Google ScholarGoogle ScholarDigital LibraryDigital Library
  35. Sava Krstić and John Matthews. 2003. Inductive Invariants for Nested Recursion. In Theorem Proving in Higher Order Logics, David Basin and Burkhart Wolf (Eds.). Springer Berlin Heidelberg, Berlin, Heidelberg, 253-269. Google ScholarGoogle Scholar
  36. K. Rustan M. Leino. 2010. Dafny: An Automatic Program Verifier for Functional Correctness. In Logic for Programming, Artificial Intelligence, and Reasoning-16th International Conference, LPAR-16, Dakar, Senegal, April 25-May 1, 2010, Revised Selected Papers (Lecture Notes in Computer Science, Vol. 6355 ), Edmund M. Clarke and Andrei Voronkov (Eds.). Springer, 348-370. https://doi.org/10.1007/978-3-642-17511-4_20 Google ScholarGoogle ScholarCross RefCross Ref
  37. Standard library Coq. 2009. Sorting/Mergesort.v. Google ScholarGoogle Scholar
  38. Ralph Matthes. 2009. An induction principle for nested datatypes in intensional type theory. J. Funct. Program. 19, 3-4 ( 2009 ), 439-468. https://doi.org/10.1017/S095679680900731X Google ScholarGoogle ScholarDigital LibraryDigital Library
  39. John Matthews. 1999. Recursive Function Definition over Coinductive Types. In Theorem Proving in Higher Order Logics, 12th International Conference, TPHOLs'99, Nice, France, September, 1999, Proceedings (Lecture Notes in Computer Science, Vol. 1690 ), Yves Bertot, Gilles Dowek, André Hirschowitz, Christine Paulin-Mohring, and Laurent Théry (Eds.). Springer, 73-90. https://doi.org/10.1007/3-540-48256-3_6 Google ScholarGoogle ScholarCross RefCross Ref
  40. The Agda development team. 2016. Agda. http://wiki.portal.chalmers.se/agda/pmwiki. php Version 2.5.1. Google ScholarGoogle Scholar
  41. The Coq development team. 2016. The Coq proof assistant reference manual. LogiCal Project. http://coq.inria. fr Version 8.5. Google ScholarGoogle Scholar
  42. Conor McBride. 2002. Elimination with a Motive. In Types for Proofs and Programs, International Workshop, TYPES 2000, Durham, UK, December 8-12, 2000, Selected Papers (Lecture Notes in Computer Science, Vol. 2277 ), Paul Callaghan, Zhaohui Luo, James McKinna, and Robert Pollack (Eds.). Springer, 197-216. Google ScholarGoogle Scholar
  43. N. P. Mendler. 1991. Inductive types and type constraints in the second-order lambda calculus. Annals of Pure and Applied Logic 51, 1 ( 1991 ), 159-172. Google ScholarGoogle ScholarCross RefCross Ref
  44. Neil Mitchell. 2021. Data.List.Extra. https://hackage.haskell.org/package/extra-1.7.10/docs/Data-List-Extra.html Google ScholarGoogle Scholar
  45. Tobias Nipkow, Lawrence C Paulson, and Markus Wenzel. 2002. Isabelle/HOL: a proof assistant for higher-order logic. Vol. 2283. Springer Science & Business Media. Google ScholarGoogle ScholarDigital LibraryDigital Library
  46. Scott Owens and Konrad Slind. 2008. Adapting functional programs to higher order logic. Higher-Order and Symbolic Computation 21, 4 ( 2008 ), 377-409. https://doi.org/10.1007/s10990-008-9038-0 Google ScholarGoogle ScholarDigital LibraryDigital Library
  47. David Salomon and Giovanni Motta. 2009. Handbook of Data Compression. Springer. Google ScholarGoogle ScholarCross RefCross Ref
  48. Matthieu Sozeau. 2006. Subset Coercions in Coq. In Types for Proofs and Programs, International Workshop, TYPES 2006, Nottingham, UK, April 18-21, 2006, Revised Selected Papers (Lecture Notes in Computer Science, Vol. 4502 ), Thorsten Altenkirch and Conor McBride (Eds.). Springer, 237-252. https://doi.org/10.1007/978-3-540-74464-1_16 Google ScholarGoogle ScholarCross RefCross Ref
  49. Matthieu Sozeau, Abhishek Anand, Simon Boulier, Cyril Cohen, Yannick Forster, Fabian Kunze, Gregory Malecha, Nicolas Tabareau, and Théo Winterhalter. 2020. The MetaCoq Project. Journal of Automated Reasoning 64, 5 ( 2020 ), 947-999. https://doi.org/10.1007/s10817-019-09540-0 Google ScholarGoogle ScholarDigital LibraryDigital Library
  50. Matthieu Sozeau and Cyprien Mangin. 2019. Equations reloaded: high-level dependently-typed functional programming and proving in Coq. Proc. ACM Program. Lang. 3, ICFP ( 2019 ), 86 : 1-86 : 29. https://doi.org/10.1145/3341690 Google ScholarGoogle ScholarDigital LibraryDigital Library
  51. Aaron Stump, Christopher Jenkins, Stephan Spahn, and Colin McDonald. 2020. Strong Functional Pearl: Harper's RegularExpression Matcher in Cedille. Proc. ACM Program. Lang. 4, ICFP, Article 122 ( Aug. 2020 ), 25 pages. https://doi.org/10. 1145/3409004 Google ScholarGoogle ScholarDigital LibraryDigital Library
  52. Wouter Swierstra. 2008. Data Types à La Carte. J. Funct. Program. 18, 4 ( July 2008 ), 423-436. Google ScholarGoogle ScholarDigital LibraryDigital Library
  53. Dmitriy Traytel, Andrei Popescu, and Jasmin Christian Blanchette. 2012. Foundational, Compositional (Co)datatypes for Higher-Order Logic: Category Theory Applied to Theorem Proving. In Proceedings of the 27th Annual IEEE Symposium on Logic in Computer Science, LICS 2012, Dubrovnik, Croatia, June 25-28, 2012. IEEE Computer Society, 596-605. https: //doi.org/10.1109/LICS. 2012.75 Google ScholarGoogle ScholarCross RefCross Ref
  54. D. A. Turner. 1995. Elementary Strong Functional Programming. In Proceedings of the First International Symposium on Functional Programming Languages in Education (FPLE '95). Springer-Verlag, Berlin, Heidelberg, 1-13. Google ScholarGoogle ScholarCross RefCross Ref
  55. Tarmo Uustalu and Varmo Vene. 1999. Mendler-style Inductive Types, Categorically. Nordic J. of Computing 6, 3 (Sept. 1999 ), 343-361. Google ScholarGoogle ScholarDigital LibraryDigital Library
  56. Tarmo Uustalu and Varmo Vene. 2011. The Recursion Scheme from the Cofree Recursive Comonad. Electron. Notes Theor. Comput. Sci. 229, 5 ( 2011 ), 135-157. https://doi.org/10.1016/j.entcs. 2011. 02.020 Google ScholarGoogle ScholarCross RefCross Ref
  57. Hongwei Xi. 2002. Dependent Types for Program Termination Verification. Higher-Order and Symbolic Computation 15, 1 (March 2002 ), 91-131. https://doi.org/10.1023/A:1019916231463 Google ScholarGoogle ScholarDigital LibraryDigital Library
  58. Qianchuan Ye and Benjamin Delaware. 2022. Scrap your boilerplate definitions in 10 lines of Ltac!. In The Eighth International Workshop on Coq for Programming Languages. Google ScholarGoogle Scholar

Index Terms

  1. A Type-Based Approach to Divide-and-Conquer Recursion in Coq

          Recommendations

          Comments

          Login options

          Check if you have access through your login credentials or your institution to get full access on this article.

          Sign in

          Full Access

          • Article Metrics

            • Downloads (Last 12 months)600
            • Downloads (Last 6 weeks)27

            Other Metrics

          PDF Format

          View or Download as a PDF file.

          PDF

          eReader

          View online with eReader.

          eReader
          About Cookies On This Site

          We use cookies to ensure that we give you the best experience on our website.

          Learn more

          Got it!