skip to main content

Practical generic programming over a universe of native datatypes

Published:31 August 2022Publication History
Skip Abstract Section

Abstract

Datatype-generic programming makes it possible to define a construction once and apply it to a large class of datatypes.

It is often used to avoid code duplication in languages that encourage the definition of custom datatypes, in particular state-of-the-art dependently typed languages where one can have many variants of the same datatype with different type-level invariants.

In addition to giving access to familiar programming constructions for free, datatype-generic programming in the dependently typed setting also allows for the construction of generic proofs. However, the current interfaces available for this purpose are needlessly hard to use or are limited in the range of datatypes they handle.

In this paper, we describe the design of a library for safe and user-friendly datatype-generic programming in the Agda language. Generic constructions in our library are regular Agda functions over a broad universe of datatypes, yet they can be specialized to native Agda datatypes with a simple one-liner. Furthermore, we provide building blocks so that library designers can too define their own datatype-generic constructions.

References

  1. David Abrahams and Aleksey Gurtovoy. 2004. C++ template metaprogramming: concepts, tools, and techniques from Boost and beyond. Pearson Education. Google ScholarGoogle Scholar
  2. Agda Development Team. 2021. Agda 2.6.2 documentation. https://agda.readthedocs.io/en/v2.6.2/ Accessed [2021/07/10] Google ScholarGoogle Scholar
  3. Thorsten Altenkirch, Conor McBride, and Peter Morris. 2006. Generic Programming with Dependent Types. In Datatype-Generic Programming - International Spring School, SSDGP 2006, Nottingham, UK, April 24-27, 2006, Revised Lectures, Roland Carl Backhouse, Jeremy Gibbons, Ralf Hinze, and Johan Jeuring (Eds.) (Lecture Notes in Computer Science, Vol. 4719). Springer, 209–257. isbn:978-3-540-76785-5 https://doi.org/10.1007/978-3-540-76786-2_4 Google ScholarGoogle ScholarCross RefCross Ref
  4. Stevan Andjelkovic. 2011. A family of universes for generic programming. Master’s thesis. https://publications.lib.chalmers.se/records/fulltext/146810.pdf Google ScholarGoogle Scholar
  5. Marcin Benke, Peter Dybjer, and Patrik Jansson. 2003. Universes for Generic Programs and Proofs in Dependent Type Theory. Nord. J. Comput., 10, 4 (2003), 265–289. Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Jean-Philippe Bernardy, Patrik Jansson, and Ross Paterson. 2010. Parametricity and dependent types. In Proceeding of the 15th ACM SIGPLAN international conference on Functional programming, ICFP 2010, Baltimore, Maryland, USA, September 27-29, 2010, Paul Hudak and Stephanie Weirich (Eds.). ACM, 345–356. isbn:978-1-60558-794-3 https://doi.org/10.1145/1863543.1863592 Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. Richard S. Bird, Oege de Moor, and Paul F. Hoogendijk. 1996. Generic Functional Programming with Types and Relations. Journal of Functional Programming, 6, 1 (1996), 1–28. Google ScholarGoogle ScholarCross RefCross Ref
  8. Edwin C. Brady. 2021. Idris 2: Quantitative Type Theory in Practice. In 35th European Conference on Object-Oriented Programming, ECOOP 2021, July 11-17, 2021, Aarhus, Denmark (Virtual Conference), Anders Møller and Manu Sridharan (Eds.) (LIPIcs, Vol. 194). Schloss Dagstuhl - Leibniz-Zentrum für Informatik. isbn:978-3-95977-190-0 https://doi.org/10.4230/LIPIcs.ECOOP.2021.9 Google ScholarGoogle ScholarCross RefCross Ref
  9. Corrado Böhm and Alessandro Berarducci. 1985. Automatic Synthesis of Typed Lambda-Programs on Term Algebras. Theoretical Computer Science, 39 (1985), 135–154. Google ScholarGoogle ScholarCross RefCross Ref
  10. James Chapman, Pierre Évariste Dagand, Conor McBride, and Peter Morris. 2010. The gentle art of levitation. In Proceeding of the 15th ACM SIGPLAN international conference on Functional programming, ICFP 2010, Baltimore, Maryland, USA, September 27-29, 2010, Paul Hudak and Stephanie Weirich (Eds.). ACM, 3–14. isbn:978-1-60558-794-3 https://doi.org/10.1145/1863543.1863547 Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. David Christiansen and Edwin Brady. 2016. Elaborator reflection: extending Idris in Idris. In Proceedings of the 21st ACM SIGPLAN International Conference on Functional Programming, ICFP 2016, Nara, Japan, September 18-22, 2016, Jacques Garrigue, Gabriele Keller, and Eijiro Sumii (Eds.). ACM, 284–297. isbn:978-1-4503-4219-3 https://doi.org/10.1145/2951913.2951932 Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. Jesper Cockx and Dominique Devriese. 2018. Proof-relevant unification: Dependent pattern matching with only the axioms of your type theory. Journal of Functional Programming, 28 (2018), https://doi.org/10.1017/S095679681800014X Google ScholarGoogle ScholarCross RefCross Ref
  13. Pierre-Evariste Dagand and Conor McBride. 2012. Elaborating Inductive Definitions. Google ScholarGoogle Scholar
  14. Larry Diehl. 2017. Fully Generic Programming over Closed Universes of Inductive-Recursive Types. Ph. D. Dissertation. Portland State University. https://pdxscholar.library.pdx.edu/cgi/viewcontent.cgi?article=4656&context=open_access_etds Google ScholarGoogle ScholarCross RefCross Ref
  15. Larry Diehl and Tim Sheard. 2013. Leveling up dependent types: generic programming over a predicative hierarchy of universes. In Proceedings of the 2013 ACM SIGPLAN workshop on Dependently-typed programming, [email protected] 2013, Boston, Massachusetts, USA, September 24, 2013, Stephanie Weirich (Ed.). ACM, 49–60. isbn:978-1-4503-2384-0 https://doi.org/10.1145/2502409.2502414 Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. Larry Diehl and Tim Sheard. 2014. Generic constructors and eliminators from descriptions: type theory as a dependently typed internal DSL. In Proceedings of the 10th ACM SIGPLAN workshop on Generic programming, WGP 2014, Gothenburg, Sweden, August 31, 2014, José Pedro Magalháes and Tiark Rompf (Eds.). ACM, 3–14. isbn:978-1-4503-3042-8 https://doi.org/10.1145/2633628.2633630 Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. Peter Dybjer and Anton Setzer. 1999. A Finite Axiomatization of Inductive-Recursive Definitions. In Typed Lambda Calculi and Applications, 4th International Conference, TLCA 99, L Aquila, Italy, April 7-9, 1999, Proceedings, Jean-Yves Girard (Ed.) (Lecture Notes in Computer Science, Vol. 1581). Springer, 129–146. isbn:3-540-65763-0 http://link.springer.de/link/service/series/0558/bibs/1581/15810129.htm Google ScholarGoogle Scholar
  18. Peter Dybjer and Anton Setzer. 2003. Induction-recursion and initial algebras. Annals of Pure and Applied Logic, 124, 1-3 (2003), 1–47. https://doi.org/10.1016/S0168-0072(02)00096-9 Google ScholarGoogle ScholarCross RefCross Ref
  19. effectfully. 2016. Deriving eliminators of described data types. http://effectfully.blogspot.com/2016/06/deriving-eliminators-of-described-data.html Google ScholarGoogle Scholar
  20. effectfully. 2016. Descriptions. http://effectfully.blogspot.com/2016/04/descriptions.html Google ScholarGoogle Scholar
  21. effectfully. 2016. Emulating cumulativity in Agda. http://effectfully.blogspot.com/2016/07/cumu.html Google ScholarGoogle Scholar
  22. effectfully. 2020. Generic. https://github.com/effectfully/Generic Google ScholarGoogle Scholar
  23. Lucas Escot and Jesper Cockx. 2022. Generics, a library for datatype-generic programming in Agda. https://doi.org/10.5281/zenodo.6767057 Google ScholarGoogle ScholarDigital LibraryDigital Library
  24. Fredrik Nordvall Forsberg. 2013. Inductive-inductive definitions. Ph. D. Dissertation. Swansea University, UK. https://cronfa.swan.ac.uk/Record/cronfa43083 British Library, EThOS Google ScholarGoogle Scholar
  25. Fredrik Nordvall Forsberg and Anton Setzer. 2010. Inductive-Inductive Definitions. In Computer Science Logic, 24th International Workshop, CSL 2010, 19th Annual Conference of the EACSL, Brno, Czech Republic, August 23-27, 2010. Proceedings, Anuj Dawar and Helmut Veith (Eds.) (Lecture Notes in Computer Science, Vol. 6247). Springer, 454–468. isbn:978-3-642-15204-7 https://doi.org/10.1007/978-3-642-15205-4_35 Google ScholarGoogle ScholarCross RefCross Ref
  26. Ralf Hinze and Johan Jeuring. 2003. Generic Haskell: Practice and Theory. In Generic Programming - Advanced Lectures, Roland Carl Backhouse and Jeremy Gibbons (Eds.) (Lecture Notes in Computer Science, Vol. 2793). Springer, 1–56. isbn:3-540-20194-7 http://springerlink.metapress.com/openurl.asp?genre=article&issn=0302-9743&volume=2793&spage=1 Google ScholarGoogle Scholar
  27. LLC Jane Street Group. 2018. ppxlib’s user manual. https://github.com/ocaml-ppx/ppx_deriving Google ScholarGoogle Scholar
  28. Patrik Jansson and Johan Jeuring. 1997. Polyp - A Polytypic Programming Language. In POPL. 470–482. https://doi.org/10.1145/263699.263763 Google ScholarGoogle ScholarDigital LibraryDigital Library
  29. C. Barry Jay. 1995. A Semantics for Shape. Science of Computer Programming, 25, 2-3 (1995), 251–283. Google ScholarGoogle ScholarDigital LibraryDigital Library
  30. Simon Peyton Jones. 2003. Haskell 98 language and libraries: the revised report. Cambridge University Press. Google ScholarGoogle Scholar
  31. Ambrus Kaposi, András Kovács, and Thorsten Altenkirch. 2019. Constructing quotient inductive-inductive types. Proceedings of the ACM on Programming Languages, 3 (2019), https://dl.acm.org/citation.cfm?id=3290315 Google ScholarGoogle ScholarDigital LibraryDigital Library
  32. Steve Klabnik and Carol Nichols. 2019. The Rust Programming Language (Covers Rust 2018). No Starch Press. Google ScholarGoogle Scholar
  33. Per Martin-Löf. 1984. Intuitionistic type theory (Studies in proof theory, Vol. 1). Bibliopolis. isbn:978-88-7088-228-5 Google ScholarGoogle Scholar
  34. Conor McBride. 2002. Elimination with a Motive. In Types for Proofs and Programs, Paul Callaghan, Zhaohui Luo, James McKinna, Robert Pollack, and Robert Pollack (Eds.). Google ScholarGoogle Scholar
  35. Conor McBride. 2013. Dependently typed metaprogramming (in Agda). Lecture Notes. Google ScholarGoogle Scholar
  36. Conor McBride, Healfdene Goguen, and James McKinna. 2004. A Few Constructions on Constructors. In Types for Proofs and Programs, International Workshop, TYPES 2004, Jouy-en-Josas, France, December 15-18, 2004, Revised Selected Papers, Jean-Christophe Filliâtre, Christine Paulin-Mohring, and Benjamin Werner (Eds.) (Lecture Notes in Computer Science, Vol. 3839). Springer, 186–200. isbn:3-540-31428-8 https://doi.org/10.1007/11617990_12 Google ScholarGoogle ScholarDigital LibraryDigital Library
  37. Peter Morris, Thorsten Altenkirch, and Neil Ghani. 2009. A Universe of Strictly Positive Families. Int. J. Found. Comput. Sci., 20, 1 (2009), 83–107. https://doi.org/10.1142/S0129054109006462 Google ScholarGoogle ScholarCross RefCross Ref
  38. Peter W. J. Morris. 2007. Constructing Universes for Generic Programming. Ph. D. Dissertation. University of Nottingham, UK. http://ethos.bl.uk/OrderDetails.do?uin=uk.bl.ethos.519405 British Library, EThOS Google ScholarGoogle Scholar
  39. Bengt Nordström, Kent Petersson, and Jan M Smith. 1990. Programming in Martin-Löf’s type theory. 200, Oxford University Press. Google ScholarGoogle Scholar
  40. Tim Sheard and Simon L. Peyton Jones. 2002. Template meta-programming for Haskell. SIGPLAN Notices, 37, 12 (2002), 60–75. https://doi.org/10.1145/636517.636528 Google ScholarGoogle ScholarDigital LibraryDigital Library
  41. Yorick Sijsling. 2016. Generic programming with ornaments and dependent types. Master’s thesis. Google ScholarGoogle Scholar
  42. Stephanie Weirich and Chris Casinghino. 2010. Arity-generic datatype-generic programming. In Proceedings of the 4th ACM Workshop Programming Languages meets Program Verification, PLPV 2010, Madrid, Spain, January 19, 2010, Cormac Flanagan and Jean-Christophe Filliâtre (Eds.). ACM, 15–26. isbn:978-1-60558-890-2 https://doi.org/10.1145/1707790.1707799 Google ScholarGoogle ScholarDigital LibraryDigital Library
  43. Pierre Évariste Dagand. 2013. A cosmology of datatypes : reusability and dependent types. Ph. D. Dissertation. University of Strathclyde, Glasgow, UK. http://ethos.bl.uk/OrderDetails.do?uin=uk.bl.ethos.605921 British Library, EThOS Google ScholarGoogle Scholar

Index Terms

  1. Practical generic programming over a universe of native datatypes

      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

      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!