Clowns to the Left of me , Jokers to the Right Dissecting Data Structures

This paper, submitted as a ‘pearl’, introduces a small but useful generalisation to the ‘derivative’ operation on datatypes underlying Huet’s notion of ‘zipper’ (Huet 1997; McBride 2001; Abbott et al. 2005b), giving a concrete representation to one-hole contexts in data which is in mid-transformation. This operator, ‘dissection’, turns a container-like functor into a bifunctor representing a onehole context in which elements to the left of the hole are distinguished in type from elements to its right. I present dissection for polynomial functors, although it is certainly more general, preferring to concentrate here on its diverse applications. For a start, map-like operations over the functor and fold-like operations over the recursive data structure it induces can be expressed by tail recursion alone. Moreover, the derivative is readily recovered from the dissection, along with Huet’s navigation operations. A further special case of dissection, ‘division’, captures the notion of leftmost hole, canonically distinguishing values with no elements from those with at least one. By way of a more practical example, division and dissection are exploited to give a relatively efficient generic algorithm for abstracting all occurrences of one term from another in a first-order syntax. The source code for the paper is available online and compiles with recent extensions to the Glasgow Haskell Compiler.

[1]  Gérard P. Huet,et al.  The Zipper , 1997, Journal of Functional Programming.

[2]  Richard S. Bird,et al.  Algebra of programming , 1997, Prentice Hall International series in computer science.

[3]  Janusz A. Brzozowski,et al.  Derivatives of Regular Expressions , 1964, JACM.

[4]  Jeremy Gibbons Datatype-Generic Programming , 2006, SSDGP.

[5]  Johan Jeuring,et al.  PolyP—a polytypic programming language extension , 1997, POPL '97.

[6]  Ralf Hinze,et al.  Type-indexed data types , 2004, Sci. Comput. Program..

[7]  Conor McBride The Derivative of a Regular Type is its Type of One-Hole Contexts , 2001 .

[8]  Conor McBride Faking it: Simulating dependent types in Haskell , 2002, J. Funct. Program..

[9]  Peter Thiemann,et al.  A Functional Notation for Functional Dependencies , 2001 .

[10]  A. Joyal Foncteurs analytiques et espèces de structures , 1986 .

[11]  M. Barr,et al.  Toposes, Triples and Theories , 1984 .

[12]  Thorsten Altenkirch,et al.  Containers: Constructing strictly positive types , 2005, Theor. Comput. Sci..