skip to main content
10.1145/604174.604179acmconferencesArticle/Chapter ViewAbstractPublication PagespoplConference Proceedingsconference-collections
Article

Scrap your boilerplate: a practical design pattern for generic programming

Published:18 January 2003Publication History

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.

References

  1. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  2. 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 ScholarGoogle Scholar
  3. R. Bird and R. Paterson. De Bruijn notation as a nested datatype. Journal of Functional Programming, 9(1):77--91, Jan. 1999. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  5. ACM Conference on Functional Programming and Computer Architecture (FPCA'93), Cophenhagen, 1993. ACM. ISBN 0-89791-595-X.Google ScholarGoogle Scholar
  6. 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 ScholarGoogle Scholar
  7. 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 ScholarGoogle Scholar
  8. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  9. F. Henderson. Dynamic type class casts proposal. Email to the haskell mailing list, Oct. 1999.Google ScholarGoogle Scholar
  10. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  11. 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 ScholarGoogle Scholar
  12. P. Hudak. Phil's proposal for restricted type classes. Email to the haskell mailing list, June 1991.Google ScholarGoogle Scholar
  13. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  14. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  15. 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 ScholarGoogle ScholarCross RefCross Ref
  16. A. Kfoury. Type reconstruction in finite rank fragments of second-order lambda calculus. Information and Computation, 98(2):228--257, June 1992. Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  18. 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 ScholarGoogle Scholar
  19. J. Launchbury and S. Peyton Jones. State in Haskell. Lisp and Symbolic Computation, 8(4):293--342, Dec. 1995. Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. X. Leroy and M. Mauny. Dynamics in ML. In Hughes {13}. Google ScholarGoogle ScholarDigital LibraryDigital Library
  21. K. Lieberherr. Adaptive Object-Oriented Software: The Demeter Method with Propagation Patterns. PWS Publishing Company, Boston, 1996. Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. E. Meijer, M. Fokkinga, and R. Paterson. Functional Programming with Bananas, Lenses, Envelopes, and Barbed Wire. In Hughes {13}, pages 124--144. Google ScholarGoogle Scholar
  23. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  24. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  25. C. Okasaki. Purely functional data structures. Cambridge University Press, 1998. Google ScholarGoogle ScholarDigital LibraryDigital Library
  26. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  27. S. Peyton Jones. Restricted overloading. Email to the fplangc mailing list, Dec. 1990.Google ScholarGoogle Scholar
  28. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  29. T. Sheard and L. Fegaras. A fold for all seasons. In FPCA93 {5}, pages 233--242. ISBN 0-89791-595-X. Google ScholarGoogle Scholar
  30. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  31. M. Shields and S. Peyton Jones. Putting "putting type annotations to work" to work. In preparation, 2002.Google ScholarGoogle Scholar
  32. 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 ScholarGoogle Scholar
  33. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  34. J. Visser. Visitor combination and traversal control. In OOPSLA 2001 Conference Proceedings: Object-Oriented Programming Systems, Languages, and Applications. ACM Press, 2001. Google ScholarGoogle ScholarDigital LibraryDigital Library
  35. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  36. S. Weirich. Type-safe cast. In ACM SIGPLAN International Conference on Functional Programming (ICFP'00), pages 58--67, Montreal, Sept. 2000. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  37. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  38. N. Winstanley. A type-sensitive preprocessor for Haskell. In Glasgow Workshop on Functional Programming, Ullapool, 1997.Google ScholarGoogle Scholar

Index Terms

  1. Scrap your boilerplate: a practical design pattern for generic programming

          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
          • Published in

            cover image ACM Conferences
            TLDI '03: Proceedings of the 2003 ACM SIGPLAN international workshop on Types in languages design and implementation
            January 2003
            144 pages
            ISBN:1581136498
            DOI:10.1145/604174

            Copyright © 2003 ACM

            Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]

            Publisher

            Association for Computing Machinery

            New York, NY, United States

            Publication History

            • Published: 18 January 2003

            Permissions

            Request permissions about this article.

            Request Permissions

            Check for updates

            Qualifiers

            • Article

            Acceptance Rates

            TLDI '03 Paper Acceptance Rate11of26submissions,42%Overall Acceptance Rate11of26submissions,42%

            Upcoming Conference

            POPL '25

          PDF Format

          View or Download as a PDF file.

          PDF

          eReader

          View online with eReader.

          eReader