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.
- David Abrahams and Aleksey Gurtovoy. 2004. C++ template metaprogramming: concepts, tools, and techniques from Boost and beyond. Pearson Education.
Google Scholar
- Agda Development Team. 2021. Agda 2.6.2 documentation. https://agda.readthedocs.io/en/v2.6.2/ Accessed [2021/07/10]
Google Scholar
- 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 Scholar
Cross Ref
- Stevan Andjelkovic. 2011. A family of universes for generic programming. Master’s thesis. https://publications.lib.chalmers.se/records/fulltext/146810.pdf
Google Scholar
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Cross Ref
- 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 Scholar
Cross Ref
- Corrado Böhm and Alessandro Berarducci. 1985. Automatic Synthesis of Typed Lambda-Programs on Term Algebras. Theoretical Computer Science, 39 (1985), 135–154.
Google Scholar
Cross Ref
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Cross Ref
- Pierre-Evariste Dagand and Conor McBride. 2012. Elaborating Inductive Definitions.
Google Scholar
- 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 Scholar
Cross Ref
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
- 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 Scholar
Cross Ref
- effectfully. 2016. Deriving eliminators of described data types. http://effectfully.blogspot.com/2016/06/deriving-eliminators-of-described-data.html
Google Scholar
- effectfully. 2016. Descriptions. http://effectfully.blogspot.com/2016/04/descriptions.html
Google Scholar
- effectfully. 2016. Emulating cumulativity in Agda. http://effectfully.blogspot.com/2016/07/cumu.html
Google Scholar
- effectfully. 2020. Generic. https://github.com/effectfully/Generic
Google Scholar
- Lucas Escot and Jesper Cockx. 2022. Generics, a library for datatype-generic programming in Agda. https://doi.org/10.5281/zenodo.6767057
Google Scholar
Digital Library
- Fredrik Nordvall Forsberg. 2013. Inductive-inductive definitions. Ph. D. Dissertation. Swansea University, UK. https://cronfa.swan.ac.uk/Record/cronfa43083 British Library, EThOS
Google Scholar
- 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 Scholar
Cross Ref
- 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 Scholar
- LLC Jane Street Group. 2018. ppxlib’s user manual. https://github.com/ocaml-ppx/ppx_deriving
Google Scholar
- Patrik Jansson and Johan Jeuring. 1997. Polyp - A Polytypic Programming Language. In POPL. 470–482. https://doi.org/10.1145/263699.263763
Google Scholar
Digital Library
- C. Barry Jay. 1995. A Semantics for Shape. Science of Computer Programming, 25, 2-3 (1995), 251–283.
Google Scholar
Digital Library
- Simon Peyton Jones. 2003. Haskell 98 language and libraries: the revised report. Cambridge University Press.
Google Scholar
- 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 Scholar
Digital Library
- Steve Klabnik and Carol Nichols. 2019. The Rust Programming Language (Covers Rust 2018). No Starch Press.
Google Scholar
- Per Martin-Löf. 1984. Intuitionistic type theory (Studies in proof theory, Vol. 1). Bibliopolis. isbn:978-88-7088-228-5
Google Scholar
- 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 Scholar
- Conor McBride. 2013. Dependently typed metaprogramming (in Agda). Lecture Notes.
Google Scholar
- 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 Scholar
Digital Library
- 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 Scholar
Cross Ref
- 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 Scholar
- Bengt Nordström, Kent Petersson, and Jan M Smith. 1990. Programming in Martin-Löf’s type theory. 200, Oxford University Press.
Google Scholar
- 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 Scholar
Digital Library
- Yorick Sijsling. 2016. Generic programming with ornaments and dependent types. Master’s thesis.
Google Scholar
- 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 Scholar
Digital Library
- 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 Scholar
Index Terms
Practical generic programming over a universe of native datatypes
Recommendations
Generic constructors and eliminators from descriptions: type theory as a dependently typed internal DSL
WGP '14: Proceedings of the 10th ACM SIGPLAN workshop on Generic programmingDependently typed languages with an "open" type theory introduce new datatypes using an axiomatic approach. Each new datatype introduces axioms for constructing values of the datatype, and an elimination axiom (which we call the standard eliminator) for ...
Generic zero-cost reuse for dependent types
Dependently typed languages are well known for having a problem with code reuse. Traditional non-indexed algebraic datatypes (e.g. lists) appear alongside a plethora of indexed variations (e.g. vectors). Functions are often rewritten for both non-...
Arity-generic datatype-generic programming
PLPV '10: Proceedings of the 4th ACM SIGPLAN workshop on Programming languages meets program verificationSome programs are doubly-generic. For example, map is datatype-generic in that many different data structures support a mapping operation. A generic programming language like Generic Haskell can use a single definition to generate map for each type. ...






Comments