ABSTRACT
We describe a design pattern for writing programs that traverse data structures built from rich mutually-recursive data types. Such programs often have a great deal of "boilerplate" code that simply walks the structure, hiding a small amount of "real" code that constitutes the reason for the traversal.Our technique allows most of this boilerplate to be written once and for all, or even generated mechanically, leaving the programmer free to concentrate on the important part of the algorithm. These generic programs are much more adaptive when faced with data structure evolution because they contain many fewer lines of type-specific code.Our approach is simple to understand, reasonably efficient, and it handles all the data types found in conventional functional programming languages. It makes essential use of rank-2 polymorphism, an extension found in some implementations of Haskell. Further it relies on a simple type-safe cast operator.
- M. Abadi, L. Cardelli, B. Pierce, and G. Plotkin. Dynamic typing in a statically-typed language. In 16th ACM Conference on Principles of Programming Languages, pages 213--227, Jan. 1989. Google ScholarDigital Library
- M. Abadi, L. Cardelli, B. Pierce, and D. Remy. Dynamic typing in polymorphic languages. In Proceedings of the 1992 ACM Workshop on ML and its Applications, pages 92--103, San Francisco, June 1992.Google Scholar
- R. Bird and R. Paterson. De Bruijn notation as a nested datatype. Journal of Functional Programming, 9(1):77--91, Jan. 1999. Google ScholarDigital Library
- D. Clarke and A. Löh. Generic Haskell, Specifically. In J. Gibbons and J. Jeuring, editors, Proc. of the IFIP TC2 Working Conference on Generic Programming. Kluwer Academic Publishers, 2002. To appear. Google ScholarDigital Library
- ACM Conference on Functional Programming and Computer Architecture (FPCA'93), Cophenhagen, 1993. ACM. ISBN 0-89791-595-X.Google Scholar
- A. Gill, J. Launchbury, and S. Peyton Jones. A short cut to deforestation. In FPCA93 {5}, pages 223--232. ISBN 0-89791-595-X. Google Scholar
- N. Glew. Type dispatch for named hierarchical types. In Proceedings of the Fourth ACM SIGPLAN International Conference on Functional Programming (ICFP'99), volume 34.9 of ACM Sigplan Notices, pages 172--182, N.Y., Sept. 27--29 1999. ACM Press. Google Scholar
- R. Harper and G. Morrisett. Compiling polymorphism using intensional type analysis. In 22nd ACM Symposium on Principles of Programming Languages (POPL'95), pages 130--141. ACM, Jan. 1995. Google ScholarDigital Library
- F. Henderson. Dynamic type class casts proposal. Email to the haskell mailing list, Oct. 1999.Google Scholar
- R. Hinze. A New Approach to Generic Functional Programming. In T. Reps, editor, Proceedings of the 27th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, Boston, Massachusetts, January 19--21, pages 119--132, Jan. 2000. Google ScholarDigital Library
- R. Hinze and S. Peyton Jones. Derivable type classes. In G. Hutton, editor, Proceedings of the 2000 Haskell Workshop, Montreal, number NOTTCS-TR-00-1 in Technical Reports, Sept. 2000.Google Scholar
- P. Hudak. Phil's proposal for restricted type classes. Email to the haskell mailing list, June 1991.Google Scholar
- R. Hughes, editor. ACM Conference on Functional Programming and Computer Architecture (FPCA'91), volume 523 of Lecture Notes in Computer Science, Boston, 1991. Springer Verlag. Google ScholarDigital Library
- P. Jansson and J. Jeuring. PolyP - a polytypic programming language extension. In 24th ACM Symposium on Principles of Programming Languages (POPL'97), pages 470--482, Paris, Jan. 1997. ACM. Google ScholarDigital Library
- J. Jeuring and P. Jansson. Polytypic programming. In J. Launchbury, E. Meijer, and T. Sheard, editors, 2nd Int. School on Advanced Functional Programming, Olympia, WA, USA, 26--30 Aug 1996, volume 1129 of Lecture Notes in Computer Science, pages 68--114. Springer-Verlag, Berlin, 1996. Google ScholarCross Ref
- A. Kfoury. Type reconstruction in finite rank fragments of second-order lambda calculus. Information and Computation, 98(2):228--257, June 1992. Google ScholarDigital Library
- R. Lämmel and J. Visser. Typed Combinators for Generic Traversal. In Proc. Practical Aspects of Declarative Programming PADL 2002, volume 2257 of LNCS, pages 137--154. Springer-Verlag, Jan. 2002. Google ScholarDigital Library
- R. Lämmel, J. Visser, and J. Kort. Dealing with Large Bananas. In J. Jeuring, editor, Proceedings of WGP'2000, Technical Report, Universiteit Utrecht, pages 46--59, July 2000.Google Scholar
- J. Launchbury and S. Peyton Jones. State in Haskell. Lisp and Symbolic Computation, 8(4):293--342, Dec. 1995. Google ScholarDigital Library
- X. Leroy and M. Mauny. Dynamics in ML. In Hughes {13}. Google ScholarDigital Library
- K. Lieberherr. Adaptive Object-Oriented Software: The Demeter Method with Propagation Patterns. PWS Publishing Company, Boston, 1996. Google ScholarDigital Library
- E. Meijer, M. Fokkinga, and R. Paterson. Functional Programming with Bananas, Lenses, Envelopes, and Barbed Wire. In Hughes {13}, pages 124--144. Google Scholar
- E. Meijer and J. Jeuring. Merging Monads and Folds for Functional Programming. In J. Jeuring and E. Meijer, editors, Advanced Functional Programming, volume 925 of Lecture Notes in Computer Science, pages 228--266. Springer Verlag, 1995. Google ScholarDigital Library
- M. Odersky and K. Läufer. Putting type annotations to work. In 23rd ACM Symposium on Principles of Programming Languages (POPL'96), pages 54--67. ACM, St Petersburg Beach, Florida, Jan. 1996. Google ScholarDigital Library
- C. Okasaki. Purely functional data structures. Cambridge University Press, 1998. Google ScholarDigital Library
- J. Palsberg and B. Jay. The essence of the visitor pattern. In Proceedings 22nd Annual International Computer Software and Applications Conference (COMPSAC'98), pages 9--15, Aug. 1998. Google ScholarDigital Library
- S. Peyton Jones. Restricted overloading. Email to the fplangc mailing list, Dec. 1990.Google Scholar
- T. Sheard. Generic unification via Two-Level types and parameterized modules. In ACM SIGPLAN International Conference on Functional Programming (ICFP'01), volume 36, 10 of ACM SIGPLAN notices, pages 86--97, Florence, Sept. 2001. ACM. Google ScholarDigital Library
- T. Sheard and L. Fegaras. A fold for all seasons. In FPCA93 {5}, pages 233--242. ISBN 0-89791-595-X. Google Scholar
- T. Sheard and S. Peyton Jones. Template meta-programming for Haskell. In M. Chakravarty, editor, Proceedings of the 2002 Haskell Workshop, Pittsburgh, Oct. 2002. Google ScholarDigital Library
- M. Shields and S. Peyton Jones. Putting "putting type annotations to work" to work. In preparation, 2002.Google Scholar
- S. Swierstra, P. Alcocer, and J. Saraiva. Designing and implementing combinator languages. In S. Swierstra, P. Henriques, and J. Oliveira, editors, Advanced Functional Programming, Third International School, AFP '98, volume 1608 of Lecture Notes in Computer Science, pages 150--206, Braga, Portugal, Sept. 1999. Springer Verlag.Google Scholar
- E. Visser, Z.-e.-A. Benaissa, and A. Tolmach. Building program optimizers with rewriting strategies. In ACM SIGPLAN International Conference on Functional Programming (ICFP'98), volume 34(1) of ACM SIGPLAN Notices, pages 13--26, Baltimore, 1998. ACM. Google ScholarDigital Library
- J. Visser. Visitor combination and traversal control. In OOPSLA 2001 Conference Proceedings: Object-Oriented Programming Systems, Languages, and Applications. ACM Press, 2001. Google ScholarDigital Library
- M. Wallace and C. Runciman. Haskell and XML: Generic combinators or type-based translation. In ACM SIGPLAN International Conference on Functional Programming (ICFP'99), pages 148--159, Paris, Sept. 1999. ACM. Google ScholarDigital Library
- S. Weirich. Type-safe cast. In ACM SIGPLAN International Conference on Functional Programming (ICFP'00), pages 58--67, Montreal, Sept. 2000. ACM. Google ScholarDigital Library
- S. Weirich. Higher-order intensional type analysis. In D. L. Métayer, editor, Programming Languages and Systems: 11th European Symposium on Programming (ESOP 2002), Grenoble, France, number 2305 in Lecture Notes in Computer Science, pages 98--114. Springer Verlag, 2002. Google ScholarDigital Library
- N. Winstanley. A type-sensitive preprocessor for Haskell. In Glasgow Workshop on Functional Programming, Ullapool, 1997.Google Scholar
Index Terms
- Scrap your boilerplate: a practical design pattern for generic programming
Recommendations
Scrap your boilerplate: a practical design pattern for generic programming
We describe a design pattern for writing programs that traverse data structures built from rich mutually-recursive data types. Such programs often have a great deal of "boilerplate" code that simply walks the structure, hiding a small amount of "real" ...
Scrap more boilerplate: reflection, zips, and generalised casts
ICFP '04: Proceedings of the ninth ACM SIGPLAN international conference on Functional programmingWriting boilerplate code is a royal pain. Generic programming promises to alleviate this pain by allowing the programmer to write a generic "recipe" for boilerplate code, and use that recipe in many places. In earlier work we introduced the "Scrap your ...
Scrap more boilerplate: reflection, zips, and generalised casts
ICFP '04Writing boilerplate code is a royal pain. Generic programming promises to alleviate this pain by allowing the programmer to write a generic "recipe" for boilerplate code, and use that recipe in many places. In earlier work we introduced the "Scrap your ...
Comments