Safe fusion of functional expressions

Large functional programs are often constructed by decomposing each big task into smaller tasks which can be performed by simpler functions. This hierarchical style of developing programs has been found to improve programmers' productivity because smaller functions are easier to construct and reuse. However, programs written in this way tend to be less efficient. Unnecessary intermediate data structures may be created. More function invocations may be required. To reduce such performance penalties, Wadler proposed a transformation algorithm, called deforestation, which could automatically fuse certain composed expressions together in order to eliminate intermediate tree-like data structures. However, his technique is only applicable to a subset of first-order expressions. This paper will generalise the deforestation technique to make it applicable to all first-order and higher-order functional programs. Our generalisation is made possible by the adoption of a model for safe fusion which views each function as a producer and its parameters as consumers. Through this model, static program properties are proposed to classify producers and consumers as either safe or unsafe. This classification is used to identify sub-terms that can be safely fused/eliminated. We present the generalised transformation algorithm as a set of syntax-directed rewrite rules, illustrate it with examples, and provide an outline of its termination proof.

[1]  Richard C. Waters,et al.  Automatic transformation of series expressions into loops , 1991, TOPL.

[2]  Martin S. Feather,et al.  A System for Assisting Program Transformation , 1982, TOPL.

[3]  John Darlington,et al.  A Transformation System for Developing Recursive Programs , 1977, J. ACM.

[4]  Philip Wadler Listlessness is better than laziness II: composing listless functions , 1985, Programs as Data Objects.

[5]  Charles Consel,et al.  Binding time analysis for high order untyped functional languages , 1990, LISP and Functional Programming.

[6]  Philip Wadler,et al.  Deforestation: Transforming Programs to Eliminate Trees , 1988, Theoretical Computer Science.

[7]  Wei-Ngan Chin,et al.  Automatic methods for program transformation , 1990 .

[8]  W.-N. Chin Generalising Deforestation for All First-Order Functional Programs , 1991, JTASPEFT/WSA.

[9]  Philip Wadler,et al.  Listlessness is better than laziness: Lazy evaluation and garbage collection at compile-time , 1984, LFP '84.

[10]  Lennart Augustsson,et al.  Compiling Pattern Matching , 1985, FPCA.

[11]  Valentin F. Turchin,et al.  The concept of a supercompiler , 1986, TOPL.

[12]  Carsten Kehler Holst,et al.  Finiteness Analysis , 1991, Conference on Functional Programming Languages and Computer Architecture.

[13]  Peter Sestoft,et al.  An experiment in partial evaluation: the generation of a compiler generator , 1985, SIGP.

[14]  Philip Wadler,et al.  When Will Deforestation Stop , 1988 .