Advertisement

“Scrap Your Boilerplate” Revolutions

  • Ralf Hinze
  • Andres Löh
Part of the Lecture Notes in Computer Science book series (LNCS, volume 4014)

Abstract

Generic programming allows you to write a function once, and use it many times at different types. Traditionally, generic functions are defined by induction on the structure of types. “Scrap your boilerplate” (SYB) is a radically different approach that dispatches on the structure of values. In previous work, we have shown how to reconcile both approaches using the concept of generic views: many generic functions can be based either on the classical sum-of-products view or on the view that underlies SYB, the so-called ‘spine’ view. One distinct advantage of the spine view is its generality: it is applicable to a large class of data types, including generalised algebraic data types. Its main weakness roots in the value-orientation: one can only define generic functions that consume data (show) but not ones that produce data (read). Furthermore, functions that abstract over type constructors (map, reduce) are out of reach. In this paper, we show how to overcome both limitations. The main technical contributions are the ‘type spine’ view and the ‘lifted spine’ view.

Keywords

Data Type Type Representation Functional Programming Type Constructor Data Constructor 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

References

  1. 1.
    Jansson, P., Jeuring, J.: PolyP—a polytypic programming language extension. In: Conference Record 24th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL 1997), Paris, France, pp. 470–482. ACM Press, New York (1997)CrossRefGoogle Scholar
  2. 2.
    Hinze, R., Jeuring, J.: Generic haskell: Practice and theory. In: Backhouse, R., Gibbons, J. (eds.) Generic Programming. LNCS, vol. 2793, pp. 1–56. Springer, Heidelberg (2003)CrossRefGoogle Scholar
  3. 3.
    Peyton Jones, S., Lämmel, R.: Scrap your boilerplate: A practical approach to generic programming. In: Proceedings of the ACM SIGPLAN Workshop on Types in Language Design and Implementation (TLDI 2003), New Orleans, pp. 26–37 (2003)Google Scholar
  4. 4.
    Hall, C.V., Hammond, K., Peyton Jones, S.L., Wadler, P.L.: Type classes in Haskell. ACM Transactions on Programming Languages and Systems 18(2), 109–138 (1996)CrossRefGoogle Scholar
  5. 5.
    Lämmel, R., Peyton Jones, S.: Scrap your boilerplate with class: extensible generic functions. In: Pierce, B. (ed.) Proceedings of the 2005 International Conference on Functional Programming, Tallinn, Estonia, September 26-28 (2005)Google Scholar
  6. 6.
    Weirich, S.: Type-safe cast: Functional pearl. In: Proceedings of the ACM SIGPLAN International Conference on Functional Programming (ICFP 2000), ACM SIGPLAN Notices, vol. 35(9), pp. 58–67. ACM Press, New York (2000)CrossRefGoogle Scholar
  7. 7.
    Crary, K., Weirich, S., Morrisett, G.: Intensional polymorphism in type-erasure semantics. In: Proceedings of the ACM SIGPLAN International Conference on Functional Programming (ICFP 1998) ACM SIGPLAN Notices, Baltimore, MD, vol. 34(1), pp. 301–312. ACM Press, New York (1999)Google Scholar
  8. 8.
    Cheney, J., Hinze, R.: A lightweight implementation of generics and dynamics. In: Chakravarty, M.M. (ed.) Proceedings of the 2002 ACM SIGPLAN Haskell Workshop, pp. 90–104. ACM Press, New York (2002)CrossRefGoogle Scholar
  9. 9.
    Hinze, R.: A new approach to generic functional programming. In: Reps, T.W. (ed.) Proceedings of the 27th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL 2000), Boston, Massachusetts, January 19-21, pp. 119–132 (2000)Google Scholar
  10. 10.
    Hinze, R.: Generics for the masses. In: Fisher, K. (ed.) Proceedings of the 2004 International Conference on Functional Programming, Snowbird, Utah, September 19-22, 2004, pp. 236–243. ACM Press, New York (2004)Google Scholar
  11. 11.
    Hinze, R., Löh, A., Oliveira, B.C.: Scrap Your Boilerplate reloaded. In: Hagiya, M., Wadler, P. (eds.) FLOPS 2006. LNCS, vol. 3945. Springer, Heidelberg (2006)Google Scholar
  12. 12.
    Xi, H., Chen, C., Chen, G.: Guarded recursive datatype constructors. In: Proceedings of the ACM SIGPLAN-SIGACT symposium on Principles of Programming Languages (POPL 2003), pp. 224–235. ACM Press, New York (2003)CrossRefGoogle Scholar
  13. 13.
    Peyton Jones, S., Washburn, G., Weirich, S.: Wobbly types: Type inference for generalised algebraic data types. Technical Report MS-CIS-05-26, University of Pennsylvania (2004)Google Scholar
  14. 14.
    Wadler, P.: Theorems for free! In: The Fourth International Conference on Functional Programming Languages and Computer Architecture (FPCA 1989), London, UK, pp. 347–359. Addison-Wesley Publishing Company, Reading (1989)CrossRefGoogle Scholar
  15. 15.
    Hughes, R.J.M.: A novel representation of lists and its application to the function “reverse”. Information Processing Letters 22(3), 141–144 (1986)CrossRefGoogle Scholar
  16. 16.
    Bird, R., Meertens, L.: Nested datatypes. In: Jeuring, J. (ed.) MPC 1998. LNCS, vol. 1422, pp. 52–67. Springer, Heidelberg (1998)CrossRefGoogle Scholar
  17. 17.
    Hinze, R.: Functional Pearl: Perfect trees and bit-reversal permutations. Journal of Functional Programming 10(3), 305–317 (2000)MATHCrossRefMathSciNetGoogle Scholar
  18. 18.
    Hutton, G.: Higher-order functions for parsing. Journal of Functional Programming 2(3), 323–343 (1992)MATHCrossRefMathSciNetGoogle Scholar
  19. 19.
    Peyton Jones, S.: Haskell 1998 Language and Libraries. Cambridge University Press, Cambridge (2003)Google Scholar
  20. 20.
    Jones, M.P.: A system of constructor classes: Overloading and implicit higher-order polymorphism. Journal of Functional Programming 5(1), 1–35 (1995)MATHCrossRefGoogle Scholar
  21. 21.
    Hinze, R.: Fun with phantom types. In: Gibbons, J., de Moor, O. (eds.) The Fun of Programming, pp. 245–262. Palgrave Macmillan, Basingstoke (2003)Google Scholar
  22. 22.
    Lämmel, R., Peyton Jones, S.: Scrap more boilerplate: reflection, zips, and generalised casts. In: Fisher, K. (ed.) Proceedings of the 2004 International Conference on Functional Programming, Snowbird, Utah, September 19-22, 2004, pp. 244–255 (2004)Google Scholar
  23. 23.
    Backhouse, R., Jansson, P., Jeuring, J., Meertens, L.: Generic Programming — An Introduction. In: Swierstra, S.D., Henriques, P.R., Oliveira, J.N. (eds.) AFP 1998. LNCS, vol. 1608, pp. 28–115. Springer, Heidelberg (1999)CrossRefGoogle Scholar
  24. 24.
    Hinze, R., Jeuring, J.: Generic haskell: Applications. In: Backhouse, R., Gibbons, J. (eds.) Generic Programming. LNCS, vol. 2793, pp. 57–96. Springer, Heidelberg (2003)CrossRefGoogle Scholar
  25. 25.
    Harper, R., Morrisett, G.: Compiling polymorphism using intensional type analysis. In: 22nd Symposium on Principles of Programming Languages, POPL 1995, pp. 130–141 (1995)Google Scholar
  26. 26.
    Crary, K., Weirich, S.: Flexible type analysis. In: Proceedings ICFP 1999: International Conference on Functional Programming, pp. 233–248. ACM Press, New York (1999)CrossRefGoogle Scholar
  27. 27.
    Trifonov, V., Saha, B., Shao, Z.: Fully reflexive intensional type analysis. In: Proceedings ICFP 2000: International Conference on Functional Programming, pp. 82–93. ACM Press, New York (2000)CrossRefGoogle Scholar
  28. 28.
    Weirich, S.: Encoding intensional type analysis. In: Sands, D. (ed.) ESOP 2001. LNCS, vol. 2028, pp. 92–106. Springer, Heidelberg (2001)CrossRefGoogle Scholar
  29. 29.
    Norell, U., Jansson, P.: Polytypic programming in haskell. In: Trinder, P., Michaelson, G.J., Peña, R. (eds.) IFL 2003. LNCS, vol. 3145, pp. 168–184. Springer, Heidelberg (2004)CrossRefGoogle Scholar
  30. 30.
    Oliveira, B.C., Gibbons, J.: Typecase: A design pattern for type-indexed functions. In: Leijen, D. (ed.) Proceedings of the 2005 ACM SIGPLAN workshop on Haskell, Tallinn, Estonia, pp. 98–109 (2005)Google Scholar
  31. 31.
    Hinze, R., Peyton Jones, S.: Derivable type classes. In: Hutton, G. (ed.) Proceedings of the 2000 ACM SIGPLAN Haskell Workshop. Electronic Notes in Theoretical Computer Science, vol. 41.1. Elsevier Science, Amsterdam (2001); The preliminary proceedings appeared as a University of Nottingham technical reportGoogle Scholar
  32. 32.
    Alimarine, A., Plasmeijer, R.: A generic programming extension for Clean. In: Arts, T., Mohnen, M. (eds.) Proceedings of the 13th International workshop on the Implementation of Functional Languages, IFL 2001, Älvsjö, Sweden, pp. 257–278 (2001)Google Scholar
  33. 33.
    Hinze, R.: Polytypic values possess polykinded types. Science of Computer Programming 43, 129–159 (2002)MATHCrossRefMathSciNetGoogle Scholar
  34. 34.
    Löh, A.: Exploring Generic Haskell. PhD thesis, Utrecht University (2004)Google Scholar
  35. 35.
    Löh, A., Jeuring, J.: The Generic Haskell user’s guide, version 1.42 - Coral release. Technical Report UU-CS-2005-004, Universiteit Utrecht (2005)Google Scholar
  36. 36.
    Hinze, R., Löh, A., Oliveira, B.C.: “Scrap Your Boilerplate” reloaded. Technical Report IAI-TR-2006-2, Institut für Informatik III, Universität Bonn (2006)Google Scholar
  37. 37.
    Jones, M.P.: Type classes with functional dependencies. In: Smolka, G. (ed.) ESOP 2000. LNCS, vol. 1782, pp. 230–244. Springer, Heidelberg (2000)CrossRefGoogle Scholar
  38. 38.
    Hinze, R., Jeuring, J., Löh, A.: Type-indexed data types. Science of Computer Programming 51, 117–151 (2004)MATHCrossRefMathSciNetGoogle Scholar
  39. 39.
    Clarke, D., Löh, A.: Generic Haskell, specifically. In: Gibbons, J., Jeuring, J. (eds.) Proceedings of the IFIP TC2 Working Conference on Generic Programming, Schloss Dagstuhl, pp. 21–48. Kluwer Academic Publishers, Dordrecht (2002)Google Scholar
  40. 40.
    Visser, E.: Language independent traversals for program transformation. In: Jeuring, J. (ed.) Proceedings of the 2nd Workshop on Generic Programming, Ponte de Lima, Portugal, pp. 86–104 (2000); The proceedings appeared as a technical report of Universiteit Utrecht, UU-CS-2000-19Google Scholar
  41. 41.
    Hinze, R., Löh, A.: Open data types and open functions. Technical Report IAI-TR-2006-3, Institut für Informatik III, Universität Bonn (2006)Google Scholar
  42. 42.
    Reig, F.: Generic proofs for combinator-based generic programs. In: Loidl, H.W. (ed.) Trends in Functional Programming, Intellect, vol. 5 (2006)Google Scholar
  43. 43.
    Braun, W., Rem, M.: A logarithmic implementation of flexible arrays. Memorandum MR83/4, Eindhoven University of Technology (1983)Google Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2006

Authors and Affiliations

  • Ralf Hinze
    • 1
  • Andres Löh
    • 1
  1. 1.Institut für Informatik IIIUniversität BonnBonnGermany

Personalised recommendations