Hostname: page-component-586b7cd67f-g8jcs Total loading time: 0 Render date: 2024-11-28T21:26:53.827Z Has data issue: false hasContentIssue false

A pattern for almost compositional functions

Published online by Cambridge University Press:  02 September 2008

BJÖRN BRINGERT
Affiliation:
Department of Computer Science and Engineering, Chalmers University of Technology and University of GothenburgSE-412 96, Göteborg, Sweden (e-mail: bringert@chalmers.se, aarne@chalmers.se)
AARNE RANTA
Affiliation:
Department of Computer Science and Engineering, Chalmers University of Technology and University of GothenburgSE-412 96, Göteborg, Sweden (e-mail: bringert@chalmers.se, aarne@chalmers.se)
Rights & Permissions [Opens in a new window]

Abstract

Core share and HTML view are not available for this content. However, as you have access to this content, a full PDF is available via the ‘Save PDF’ action button.

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.

Type
Articles
Copyright
Copyright © Cambridge University Press 2008

References

Alexandrescu, Andrei. (2001) Modern C++ Design: Generic Programming and Design Patterns Applied. Indianapolis: Addison-Wesley Professional.Google Scholar
Appel, Andrew W. (1997) Modern Compiler Implementation in ML. Cambridge, UK: Cambridge University Press.CrossRefGoogle Scholar
Appel, Andrew W. (2002) Modern Compiler Implementation in Java, 2nd ed. Cambridge, UK: Cambridge University Press.CrossRefGoogle Scholar
Augustsson, Lennart & Petersson, Kent. (1994) Silly type families. http://www.cs.pdx.edu/sheard/papers/silly.pdf.Google Scholar
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.CrossRefGoogle Scholar
Coquand, Catarina & Coquand, Thierry. (September 1999) Structured type theory. Workshop on Logical Frameworks and Meta-languages (LFM'99), Paris, France.Google Scholar
Dybjer, Peter. (1994) Inductive families. Formal Aspect Comput. 6 (4), 440465.CrossRefGoogle Scholar
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.Google Scholar
Forsberg, Markus & Ranta, Aarne. (2006) BNF Converter homepage. http://www.cs.chalmers.se/markus/BNFC/.Google Scholar
Gamma, Erich, Helm, Richard, Johnson, Ralph & Vlissides, John. (1995) Design Patterns: Elements of Reusable Object-Oriented Software. Boston, MA: Addison-Wesley Longman Publishing Co.Google Scholar
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.Google Scholar
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.Google Scholar
Gosling, James, Joy, Bill, Steele, Guy & Bracha, Gilad. (2005) Java Language Specification, 3rd ed. Indianapolis: Addison-Wesley Professional.Google Scholar
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.CrossRefGoogle Scholar
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.Google Scholar
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.CrossRefGoogle Scholar
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.Google Scholar
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.Google Scholar
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.Google Scholar
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.CrossRefGoogle Scholar
Martin-Löf, Per. (1984) Intuitionistic Type Theory. Naples, Italy: Bibliopolis.Google Scholar
McBride, Conor & Paterson, Ross. (2008) Applicative programming with effects. J. Funct. Program. 18 (1), 113.CrossRefGoogle Scholar
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.Google Scholar
Mitchell, Neil & O'Rear, Stefan. (August 2007) Data Derive: A User Manual. http://www.cs.york.ac.uk/fp/darcs/derive/derive.htm.Google Scholar
Nordström, Bengt, Petersson, Kent & Smith, Jan M. (1990) Programming in Martin-Löf's Type Theory: An Introduction. USA: Oxford University Press. http://www.cs.chalmers.se/Cs/Research/Logic/book/.Google Scholar
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.Google Scholar
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.CrossRefGoogle Scholar
Peyton, Jones, Simon. (2003a) The Haskell 98 language. J. Funct. Program. 13 (1), 1146.Google Scholar
Peyton, Jones, Simon. (2003b) The Haskell 98 libraries. J. Funct. Program. 13 (1), 149240.Google Scholar
Peyton, Jones, Simon. (August 2007). The GHC Commentary. http://hackage.haskell.org/trac/ghc/wiki/Commentary.Google Scholar
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.CrossRefGoogle Scholar
Peyton, Jones, Simon, Vytiniotis, Dimitrios, Weirich, Stephanie & Shields, Mark. (2007) Practical type inference for arbitrary-rank types. J. Funct. Program. 17 (1), 182.Google Scholar
Ranta, Aarne. (2004) Grammatical framework: A type-theoretical grammar formalism. J. Funct. Program. 14 (2), 145189.CrossRefGoogle Scholar
Sarkar, Dipanwita, Waddell, Oscar & Dybvig, Kent R. (2005) EDUCATIONAL PEARL: A Nanopass framework for compiler education. J. Funct. Program. 15 (5), 653667.CrossRefGoogle Scholar
Sheard, Tim & Pasalic, Emir. (2004) Two-level types and parameterized modules. J. Funct. Program. 14 (5), 547587.CrossRefGoogle Scholar
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.Google Scholar
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.CrossRefGoogle Scholar
Wadler, Philip. (1990) Deforestation: Transforming programs to eliminate trees. Theor. Comput. Sci. 73 (2), 231248.CrossRefGoogle Scholar
Winstanley, Noel, Wallace, Malcom & Meacham, John. (2007) The DrIFT homepage. http://repetae.net/john/computer/haskell/DrIFT/.Google Scholar
Submit a response

Discussions

No Discussions have been published for this article.