Generic deriving of generic traversals

Functional programmers have an established tradition of using traversals as a design pattern to work with recursive data structures. The technique is so prolific that a whole host of libraries have been designed to help in the task of automatically providing traversals by analysing the generic structure of data types. More recently, lenses have entered the functional scene and have proved themselves to be a simple and versatile mechanism for working with product types. They make it easy to focus on the salient parts of a data structure in a composable and reusable manner. This paper uses the combination of lenses and traversals to give rise to a library with unprecedented expressivity and flexibility for querying and modifying complex data structures. Furthermore, since lenses and traversals are based on the generic shape of data, this information is used to generate code that is as efficient as hand-optimised versions. The technique leverages the structure of data to produce generic abstractions that are then eliminated by the standard workhorses of modern functional compilers: inlining and specialisation.

[1]  Russell O'Connor Functor is to Lens as Applicative is to Biplate: Introducing Multiplate , 2011, ArXiv.

[2]  Johan Jeuring,et al.  A generic deriving mechanism for Haskell , 2010, Haskell '10.

[3]  Simon L. Peyton Jones,et al.  OutsideIn(X) Modular type inference with local assumptions , 2011, J. Funct. Program..

[4]  Tom Schrijvers,et al.  Understanding idiomatic traversals backwards and forwards , 2013, Haskell '13.

[5]  Simon L. Peyton Jones,et al.  Safe zero-cost coercions for Haskell , 2014, ICFP.

[6]  Simon L. Peyton Jones,et al.  Scrap your boilerplate: a practical design pattern for generic programming , 2003, TLDI '03.

[7]  Simon L. Peyton Jones,et al.  Scrap more boilerplate: reflection, zips, and generalised casts , 2004, ICFP '04.

[8]  Simon L. Peyton Jones,et al.  Closed type families with overlapping equations , 2014, POPL.

[9]  Michael D. Adams,et al.  Template your boilerplate: using template haskell for efficient generic programming , 2012, Haskell '12.

[10]  S. Jones,et al.  A Transformation-Based Optimiser for Haskell , 1998, Sci. Comput. Program..

[11]  Simon L. Peyton Jones,et al.  Secrets of the Glasgow Haskell Compiler inliner , 2002, Journal of Functional Programming.

[12]  Conor McBride,et al.  Applicative programming with effects , 2008, J. Funct. Program..

[13]  Simon L. Peyton Jones,et al.  Generative type abstraction and type-level computation , 2011, POPL '11.

[14]  VytiniotisDimitrios,et al.  Practical type inference for arbitrary-rank types , 2007 .

[15]  Simon L. Peyton Jones,et al.  Practical type inference for arbitrary-rank types , 2007, Journal of Functional Programming.

[16]  Stephanie Weirich,et al.  Visible Type Application , 2016, ESOP.

[17]  Simon L. Peyton Jones,et al.  Giving Haskell a promotion , 2012, TLDI '12.

[18]  Joachim Breitner Beyond correct and fast: Inspection Testing , 2018, ArXiv.

[19]  Ralf Hinze Generics for the masses , 2004, ICFP '04.

[20]  Russell O'Connor,et al.  A representation theorem for second-order functionals , 2015, J. Funct. Program..

[21]  Andres Löh,et al.  True sums of products , 2014, WGP '14.

[22]  Simon L. Peyton Jones,et al.  Associated types with class , 2005, POPL '05.

[23]  Colin Runciman,et al.  Uniform boilerplate and list processing , 2007, Haskell '07.

[24]  José Pedro Magalhães Generic Programming with Multiple Parameters , 2014, FLOPS.

[25]  Jeremy Yallop,et al.  Staged generic programming , 2017, Proc. ACM Program. Lang..

[26]  José Pedro Magalhães Optimisation of Generic Programs Through Inlining , 2012, IFL.

[27]  Simon L. Peyton Jones,et al.  A Reflection on Types , 2016, A List of Successes That Can Change the World.

[28]  José Pedro Magalhães,et al.  Optimizing SYB is easy! , 2014, PEPM '14.

[29]  Ralf Hinze,et al.  A new approach to generic functional programming , 2000, POPL '00.

[30]  Andrew Farmer,et al.  HERMIT: Mechanized Reasoning during Compilation in the Glasgow Haskell Compiler , 2015 .

[31]  Nicolas Wu,et al.  Profunctor Optics: Modular Data Accessors , 2017, Art Sci. Eng. Program..

[32]  Simon L. Peyton Jones,et al.  Understanding functional dependencies via constraint handling rules , 2007, J. Funct. Program..

[33]  José Pedro Magalhães,et al.  Optimizing SYB traversals is easy! , 2015, Sci. Comput. Program..

[34]  Philip Wadler,et al.  How to make ad-hoc polymorphism less ad hoc , 1989, POPL '89.