Abstract
Functional programmers are strong enthusiasts of modular solutions to programming problems. Since software characteristics such as readability or maintainability are often directly proportional to modularity, this programming style naturally contributes to the beauty of functional programs. Unfortunately, in return of this beauty we often sacrifice efficiency: modular programs rely, at runtime, on the creation, use and elimination of intermediate data structures to connect its components. In this tutorial paper, we study an advanced technique that attempts to retain the best of this two worlds: (i) it allows programmers to implement beautiful, modular programs (ii) it shows how to transform such programs, in a way that can be incorporated in a compiler, into programs that do not construct any intermediate structure.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
Program composition \(( f \mathbin {\circ } g )\; x \) is interpreted as \( f \;( g \; x )\), and is left associative, i.e., \( f \mathbin {\circ } g \mathbin {\circ } h \mathrel {=}( f \mathbin {\circ } g )\mathbin {\circ } h \).
- 2.
Note that type synonyms are declared with the keyword \(\mathbf {type}\) and that new data-types are declared with \(\mathbf {data}\).
- 3.
This function is actually included in the Haskell \( Prelude \).
- 4.
This definition of \( fold \) slightly differs from the definition of \( foldr \mathbin {{:}{:}}( a \rightarrow b \rightarrow b )\rightarrow b \rightarrow [ a ]\rightarrow b \) provided by Haskell, in that we rely on uncurried functions and we have changed the order of the expected arguments. We give this definition here as it will simplify our presentation later.
Also, for simplicity, we have omitted an argument on both sides of the equation \( fold \;( nil , cons )\mathrel {=} f \), that could have equally been given the definition \( fold \;( nil , cons )\; l \mathrel {=} f \; l \).
- 5.
\( uncurry \) takes a function \( f \mathbin {{:}{:}} a \rightarrow b \rightarrow c \) and produces a function \( f' \mathbin {{:}{:}}( a , b )\rightarrow c \).
- 6.
Given two numbers, \( min \) will compute the minimum of both numbers.
- 7.
In order to make it easier for the reader to identify circular definitions, we frame the occurrences of variables that induce them (\( m \) in this case).
- 8.
In the program, we use two anonymous functions that are defined using the symbol \(\lambda \). Defining \(\lambda m \rightarrow Leaf \; m \), for example, is equivalent to defining \( g \; m \mathrel {=} Leaf \; m \).
- 9.
We have used \((\mathbin {\$})\mathbin {{:}{:}}( a \rightarrow b )\rightarrow a \rightarrow b \) in the expression \( pfold _T\;(h_1,h_2)\mathbin {\circ } buildp _T\; g \mathbin {\$} c \) to avoid the use of parenthesis. The same expression could be defined as \(( pfold _T\;(h_1,h_2)\mathbin {\circ } buildp _T\; g )\; c \).
- 10.
Here, we needed to introduce an explicit function composition since one is needed in order to apply the rule. In practice, intermediate structures need to be more informative that the input ones, so the latter must be bigger than the former, and we are forced to define and manipulate intermediate structures. This means that solutions as function compositions are natural ones.
- 11.
For simplicity we shall assume that constructors in a data-type declaration are declared uncurried.
- 12.
By \(s[t := a]\) we denote the replacement of every occurrence of t by a in s.
- 13.
When showing specific instances of fold for concrete data-types, we will write the operations in an algebra \(h_1 \triangledown \cdots \triangledown h_n\) in a tuple \((h_1,\dots ,h_n)\).
- 14.
We denote by \(\bar{ x }\) a tuple of values \((x_1, \cdots , x_{r_i}).\)
- 15.
By left-strict we mean strict on the first argument, that is, \( h \;(\bot , z )\mathrel {=}\bot \).
- 16.
A well known compiler generator toolbox.
References
Abramsky, S., Jung, A.: Domain Theory. In: Handbook of Logic in Computer Science, pp. 1–168. Clarendon Press (1994)
Augusteijn, L.: Sorting morphisms. In: Swierstra, S.D., Oliveira, J.N., Henriques, P.R. (eds.) AFP 1998. LNCS, vol. 1608, pp. 1–27. Springer, Heidelberg (1999). https://doi.org/10.1007/10704973_1
Bird, R.: Using circular programs to eliminate multiple traversals of data. Acta Informatica 21, 239–250 (1984)
Bird, R.: Introduction to Functional Programming using Haskell, 2nd edn. Prentice-Hall, UK (1998)
Bird, R., de Moor, O.: Algebra of Programming. Prentice-Hall Inernational Series in Computer Science, vol. 100. Prentice-Hall, Upper Saddle River (1997)
Cockett, R., Fukushima, T.: About Charity. Technical Report 92/480/18, University of Calgary, June 1992
Cockett, R., Spencer, D.: Strong Categorical Datatypes I. In: Seely, R.A.C., (ed.) International Meeting on Category Theory 1991. Canadian Mathematical Society Conference Proceedings, vol. 13, pp. 141–169 (1991)
Danielsson, N.A., Hughes, J., Jansson, P., Gibbons, J.: Fast and loose reasoning is morally correct. In: Conference Record of the 33rd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2006, pp. 206–217. ACM, New York (2006)
Fernandes, J.P.: Design, Implementation and Calculation of Circular Programs. PhD thesis, Deparment of Informatics, University of Minho, Portugal (2009)
Fernandes, J.P., Pardo, A., Saraiva, J.: A shortcut fusion rule for circular program calculation. In: Proceedings of the ACM SIGPLAN Haskell Workshop, Haskell 2007, pp. 95–106. ACM Press, New York (2007)
Gibbons, J.: Calculating functional programs. In: Backhouse, R., Crole, R., Gibbons, J. (eds.) Algebraic and Coalgebraic Methods in the Mathematics of Program Construction. LNCS, vol. 2297, pp. 151–203. Springer, Heidelberg (2002). https://doi.org/10.1007/3-540-47797-7_5
Gill, A.: Cheap deforestation for non-strict functional languages. PhD thesis, Department of Computing Science, University of Glasgow, UK (1996)
Gill, A., Launchbury, J., Jones, S.L.P.: A short cut to deforestation. In: Conference on Functional Programming Languages and Computer Architecture, pp. 223–232, June 1993
Hughes, J.: Why functional programming matters. Comput. J. 32, 98–107 (1984)
Kastens, U., Pfahler, P., Jung, M.: The eli system. In: Koskimies, K. (ed.) CC 1998. LNCS, vol. 1383, pp. 294–297. Springer, Heidelberg (1998). https://doi.org/10.1007/BFb0026439
Pardo, A.: A calculational approach to recursive programs with effects. PhD thesis, Technische Universität Darmstadt, October 2001
Pardo, A.: Generic Accumulations. In: IFIP WG2.1 Working Conference on Generic Programming, Dagstuhl, Germany, July 2002
Pardo, A., Fernandes, J.P., Saraiva, J.: Shortcut fusion rules for the derivation of circular and higher-order monadic programs. In: Proceedings of the 2009 ACM SIGPLAN Symposium on Partial Evaluation and Program Manipulation, PEPM 2009, pp. 81–90. ACM Press (2009)
Pardo, A., Fernandes, J.P., Saraiva, J.: Shortcut fusion rules for the derivation of circular and higher-order programs. Higher-Order Symb. Comput. 24(1–2), 115–149 (2011). ISSN 1388–3690
Pardo, A., Fernandes, J.P., Saraiva, J.: Multiple intermediate structure deforestation by shortcut fusion. In: Du Bois, A.R., Trinder, P. (eds.) SBLP 2013. LNCS, vol. 8129, pp. 120–134. Springer, Heidelberg (2013). https://doi.org/10.1007/978-3-642-40922-6_9
Pettorossi, A., Skowron, A.: The lambda abstraction strategy for program derivation. In: Fundamenta Informaticae XII, pp. 541–561 (1987)
Jones, S.P. (ed.) Haskell 98 Language and Libraries: The Revised Report. Cambridge University Press (2003). Also in Journal of Functional Programming, 13(1)
Jones, S.P., Hughes, J., Augustsson, L., et al.: Report on the programming language Haskell 98. Technical report, February 1999
Takano, A., Meijer, E.: Shortcut deforestation in calculational form. In: Proceedings of Conference on Functional Programming Languages and Computer Architecture, pp. 306–313. ACM Press (1995)
Voigtländer, J.: Semantics and pragmatics of new shortcut fusion rules. In: Garrigue, J., Hermenegildo, M.V. (eds.) FLOPS 2008. LNCS, vol. 4989, pp. 163–179. Springer, Heidelberg (2008). https://doi.org/10.1007/978-3-540-78969-7_13
Wadler, P.: Theorems for free! In: 4th International Conference on Functional Programming and Computer Architecture, London (1989)
Wadler, P.: Deforestation: transforming programs to eliminate trees. Theoret. Comput. Sci. 73, 231–248 (1990)
Waite, W., Kastens, U., Sloane, A.M.: Generating Software from Specifications. Jones and Bartlett Publishers Inc, USA (2007)
Author information
Authors and Affiliations
Corresponding author
Editor information
Editors and Affiliations
Rights and permissions
Copyright information
© 2019 Springer Nature Switzerland AG
About this chapter
Cite this chapter
Fernandes, J.P., Cunha, J., Saraiva, J., Pardo, A. (2019). Watch Out for that Tree! A Tutorial on Shortcut Deforestation. In: Zsók, V., Porkoláb, Z., Horváth, Z. (eds) Central European Functional Programming School. CEFP 2015. Lecture Notes in Computer Science(), vol 10094. Springer, Cham. https://doi.org/10.1007/978-3-030-28346-9_1
Download citation
DOI: https://doi.org/10.1007/978-3-030-28346-9_1
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-030-28345-2
Online ISBN: 978-3-030-28346-9
eBook Packages: Computer ScienceComputer Science (R0)