Skip to main content Accessibility help

A pattern for almost compositional functions


This paper introduces a pattern for almost compositional functions over recursive data types, and over families of mutually recursive data types. Here “almost compositional” means that for all of the constructors in the type(s), except a limited number of them, the result of the function depends only on the constructor and the results of calling the function on the constructor's arguments. The pattern consists of a generic part constructed once for each data type or family of data types, and a task-specific part. The generic part contains the code for the predictable compositional cases, leaving the interesting work to the task-specific part. Examples of the pattern are given, implemented in dependent type theory with inductive families, in Haskell with generalized algebraic data types and rank-2 polymorphism, and in Java using a variant of the Visitor design pattern. The relationships to the “Scrap Your Boilerplate” approach to generic programming, and to general tree types in dependent type theory, are investigated by reimplementing our operations using those frameworks.

Hide All
Alexandrescu, Andrei. (2001) Modern C++ Design: Generic Programming and Design Patterns Applied. Indianapolis: Addison-Wesley Professional.
Appel, Andrew W. (1997) Modern Compiler Implementation in ML. Cambridge, UK: Cambridge University Press.
Appel, Andrew W. (2002) Modern Compiler Implementation in Java, 2nd ed. Cambridge, UK: Cambridge University Press.
Augustsson, Lennart & Petersson, Kent. (1994) Silly type families.
Bracha, Gilad, Odersky, Martin, Stoutamire, David & Wadler, Philip. (1998) Making the future safe for the past: Adding genericity to the java programming language. In ACM Symposium on Object Oriented Programming: Systems, Languages, and Applications (OOPSLA), Vancouver, BC, Chambers, Craig (ed.), pp. 183–200.
Bringert, Björn. (2006) The Transfer programming language.
Coquand, Catarina & Coquand, Thierry. (September 1999) Structured type theory. Workshop on Logical Frameworks and Meta-languages (LFM'99), Paris, France.
Dybjer, Peter. (1994) Inductive families. Formal Aspect Comput. 6 (4), 440465.
Forsberg, Markus. (September 2007) Three Tools for Language Processing: BNF Converter, Functional Morphology, and Extract. Ph.D. Thesis, Göteborg University and Chalmers University of Technology.
Forsberg, Markus & Ranta, Aarne. (2006) BNF Converter homepage.
Gamma, Erich, Helm, Richard, Johnson, Ralph & Vlissides, John. (1995) Design Patterns: Elements of Reusable Object-Oriented Software. Boston, MA: Addison-Wesley Longman Publishing Co.
Gibbons, Jeremy. (2007) Datatype-generic programming. In Spring School on Datatype-Generic Programming, Backhouse, Roland, Gibbons, Jeremy, Hinze, Ralf & Jeuring, Johan (eds). Lecture Notes in Computer Science, Vol. 4719. Heidelberg: Springer-Verlag.
Gibbons, Jeremy & Oliveira, Bruno C. (July 2006) The essence of the iterator pattern. In Workshop on Mathematically-Structured Functional Programming (MSFP 2006), McBride, Conor & Uustalu, Tarmo (eds). Kuressaare Estonia Swindon, UK: British Computer Society.
Gosling, James, Joy, Bill, Steele, Guy & Bracha, Gilad. (2005) Java Language Specification, 3rd ed. Indianapolis: Addison-Wesley Professional.
Hinze, Ralf. (2004) Generics for the masses. In ICFP '04: Proceedings of the Ninth ACM SIGPLAN International Conference on Functional Programming, Vol. 39. New York: ACM Press, pp. 236243.
Hinze, Ralf, Löh, Andres & Oliveira, Bruno C. D. S. (2006) “Scrap Your Boilerplate” Reloaded. In 8th International Symposium on Functional and Logic Programming (FLOPS 2006), Hagiya, Masami & Wadler, Philip (eds). Lecture Notes in Computer Science, Vol. 3945. Heidelberg: Springer, pp. 1329.
Jansson, Patrik & Jeuring, Johan. (1997) PolyP—A polytypic programming language extension. In Proceedings of the 24th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL). New York: ACM Press, pp. 470482.
Jones, Mark P. (1995) Functional programming with overloading and higher-order polymorphism. In Advanced Functional Programming, First International Spring School on Advanced Functional Programming Techniques-Tutorial Text. London, UK: Springer-Verlag, pp. 97136.
Lämmel, Ralf & Peyton, Jones, Simon. (January 2003) Scrap Your Boilerplate: A practical design pattern for generic programming. In Proceedings of the ACM SIGPLAN Workshop on Types in Language Design and Implementation (TLDI). New Orleans, LA, New York: ACM.
Lee, Lie-Quan, Lumsdaine, Andrew & Siek, Jeremy G. (2002) The Boost Graph Library: User Guide and Reference Manual. Boston, MA: Addison-Wesley Longman Publishing Co.
Leivant, Daniel. (1983) Polymorphic type inference. In POPL'83: Proceedings of the 10th ACM SIGACT-SIGPLAN Symposium on Principles of Programming Languages. New York: ACM Press, pp. 8898.
Martin-Löf, Per. (1984) Intuitionistic Type Theory. Naples, Italy: Bibliopolis.
McBride, Conor & Paterson, Ross. (2008) Applicative programming with effects. J. Funct. Program. 18 (1), 113.
Meijer, Erik, Fokkinga, Maarten & Paterson, Ross. (1991) Functional programming with bananas, lenses, envelopes and barbed wire. In Proceedings of the 5th ACM Conference on Functional Programming Languages and Computer Architecture. New York: Springer-Verlag, pp. 124144.
Mitchell, Neil & O'Rear, Stefan. (August 2007) Data Derive: A User Manual.
Nordström, Bengt, Petersson, Kent & Smith, Jan M. (1990) Programming in Martin-Löf's Type Theory: An Introduction. USA: Oxford University Press.
Norell, Ulf. (2007) Towards a Practical Programming Language Based on Dependent Type Theory. Ph.D. Thesis, Department of Computer Science and Engineering, Chalmers University of Technology, Göteborg, Sweden.
Petersson, Kent & Synek, Dan. (1989) A set constructor for inductive sets in Martin-Löf's type theory. In Category Theory and Computer Science. Lecture Notes in Computer Science, Vol. 389. Heidelberg: Springer, pp. 128140.
Peyton, Jones, Simon. (2003a) The Haskell 98 language. J. Funct. Program. 13 (1), 1146.
Peyton, Jones, Simon. (2003b) The Haskell 98 libraries. J. Funct. Program. 13 (1), 149240.
Peyton, Jones, Simon. (August 2007). The GHC Commentary.
Peyton, Jones, Simon, Vytiniotis, Dimitrios, Weirich, Stephanie & Washburn, Geoffrey. (2006) Simple unification-based type inference for GADTs. In ICFP'06: Proceedings of the Eleventh ACM SIGPLAN International Conference on Functional Programming. New York: ACM Press, pp. 5061.
Peyton, Jones, Simon, Vytiniotis, Dimitrios, Weirich, Stephanie & Shields, Mark. (2007) Practical type inference for arbitrary-rank types. J. Funct. Program. 17 (1), 182.
Ranta, Aarne. (2004) Grammatical framework: A type-theoretical grammar formalism. J. Funct. Program. 14 (2), 145189.
Sarkar, Dipanwita, Waddell, Oscar & Dybvig, Kent R. (2005) EDUCATIONAL PEARL: A Nanopass framework for compiler education. J. Funct. Program. 15 (5), 653667.
Sheard, Tim & Pasalic, Emir. (2004) Two-level types and parameterized modules. J. Funct. Program. 14 (5), 547587.
Sheard, Tim & Peyton, Jones, Simon. (2002) Template meta-programming for Haskell. In Haskell'02: Proceedings of the ACM SIGPLAN workshop on Haskell. New York: ACM Press, pp. 116.
Visser, Joost. (2001) Visitor combination and traversal control. In OOPSLA'01: Proceedings of the 16th ACM SIGPLAN Conference on Object Oriented Programming, Systems, Languages, and Applications, Vol. 36. New York: ACM Press, pp. 270280.
Wadler, Philip. (1990) Deforestation: Transforming programs to eliminate trees. Theor. Comput. Sci. 73 (2), 231248.
Winstanley, Noel, Wallace, Malcom & Meacham, John. (2007) The DrIFT homepage.
Recommend this journal

Email your librarian or administrator to recommend adding this journal to your organisation's collection.

Journal of Functional Programming
  • ISSN: 0956-7968
  • EISSN: 1469-7653
  • URL: /core/journals/journal-of-functional-programming
Please enter your name
Please enter a valid email address
Who would you like to send this to? *


Full text views

Total number of HTML views: 0
Total number of PDF views: 0 *
Loading metrics...

Abstract views

Total abstract views: 0 *
Loading metrics...

* Views captured on Cambridge Core between <date>. This data will be updated every 24 hours.

Usage data cannot currently be displayed

A pattern for almost compositional functions

Submit a response


No Discussions have been published for this article.


Reply to: Submit a response

Your details

Conflicting interests

Do you have any conflicting interests? *