Type-inference based deforestation of functional programs

In lazy functional programming modularity is often achieved by using intermediate data structures to combine separate parts of a program. Each intermediate data structure is produced by one part and consumed by another one. Deforestation optimises a functional program by transformation into a program which does not produce such intermediate data structures. In this thesis we present a new method for deforestation, which combines a known method,short cut deforestation, with a new analysis that is based on type inference. Short cut deforestation eliminates an intermediate list by a single, local transformation. In return, short cut deforestation expects both producer and consumer of the intermediate list in a certain form. Whereas the required form of the consumer is generally considered desirable in a well-structured program anyway, the required form of the producer is only a crutch to enable deforestation. Hence only the list-producing functions of the standard libraries were defined in the desired form and short cut deforestation has been confined to compositions of these functions. Here, we present an algorithm which transforms an arbitrary producer into the required form. Starting from the observation that short cut deforestation is based on a parametricity theorem of the second-order typed lambda-calculus, we show how the construction of the required form can be reduced to a type inference problem. Typability for the second-order typed lambda-calculus is undecidable, but we only need to solve a partial type inference problem. For this problem we develop an algorithm based on the well-known Hindley-Milner type inference algorithm. The transformation of a producer often requires inlining of some function definitions. Type inference even indicates which function definitions need to be inlined. However, only limited inlining across module boundaries is practically feasible. Therefore, we extend the previously developed algorithm to split a function definition into a worker definition and a wrapper definition. We only need to inline the small wrapper definition, which transfers all information required for deforestation. The flexibility of type inference allows us to remove intermediate lists which original short cut deforestation cannot remove, even with hand-crafted producers. In contrast to most previous work on deforestation, we give a detailed proof of completeness and semantic correctness of our transformation.

[1]  Cordelia V. Hall Using Hindley-Milner type inference to optimise list representation , 1994, LFP '94.

[2]  Jr. Allen B. Tucker,et al.  The Computer Science and Engineering Handbook , 1997 .

[3]  Warren D. Goldfarb,et al.  The Undecidability of the Second-Order Unification Problem , 1981, Theor. Comput. Sci..

[4]  David Sands,et al.  Improvement theory and its applications , 1999 .

[5]  Thomas Johnsson Efficient compilation of lazy evaluation , 1984, SIGP.

[6]  Andrew D. Gordon A Tutorial on Co-induction and Functional Programming , 1994, Functional Programming.

[7]  Philip Wadler,et al.  Deforestation: Transforming Programs to Eliminate Trees , 1990, Theor. Comput. Sci..

[8]  J. B. Wells Typability and type checking in the second-order Λ-calculus are equivalent and undecidable (Preliminary Draft) , 1993 .

[9]  J. B. Wells,et al.  Typability and type checking in the second-order /spl lambda/-calculus are equivalent and undecidable , 1994, Proceedings Ninth Annual IEEE Symposium on Logic in Computer Science.

[10]  John C. Reynolds,et al.  Polymorphism is not Set-Theoretic , 1984, Semantics of Data Types.

[11]  Enno Ohlebusch,et al.  Term Rewriting Systems , 2002 .

[12]  Mike Paterson,et al.  Linear unification , 1976, STOC '76.

[13]  Simon David Marlow,et al.  Deforestation for higher-order functional programs , 1995 .

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

[15]  Jonathan Rees,et al.  Revised3 report on the algorithmic language scheme , 1986, SIGP.

[16]  Dave Sands,et al.  Computing with Contexts: A simple approach , 1998, HOOTS.

[17]  Alan Mycroft,et al.  Polymorphic Type Schemes and Recursive Definitions , 1984, Symposium on Programming.

[18]  Akihiko Takano,et al.  Program transformation in calculational form , 1998, CSUR.

[19]  Flemming Nielson,et al.  Principles of Program Analysis , 1999, Springer Berlin Heidelberg.

[20]  Olaf Chitil,et al.  Type-Inference Based Short Cut Deforestation (Nearly) without Inlining , 1999, IFL.

[21]  John C. Mitchell,et al.  Foundations for programming languages , 1996, Foundation of computing series.

[22]  J. A. Robinson,et al.  A Machine-Oriented Logic Based on the Resolution Principle , 1965, JACM.

[23]  Simon L. Peyton Jones,et al.  Cheap Deforestation in Practice: An Optimizer for Haskell , 1994, IFIP Congress.

[24]  Philip Wadler,et al.  Once upon a type , 1995, FPCA '95.

[25]  David Sands,et al.  A Foundation for Space-Safe Transformations of Call-by-Need Programs , 1999, HOOTS.

[26]  Tong Zhou,et al.  Improving Programs Which Recurse over Multiple Inductive Structures , 1994, PEPM.

[27]  Joost Engelfriet,et al.  Macro Tree Transducers , 1985, J. Comput. Syst. Sci..

[28]  Jon Fairbairn,et al.  TIM: A simple, lazy abstract machine to execute supercombinatorics , 1987, FPCA.

[29]  Graham Hutton,et al.  Bananas in space: extending fold and unfold to exponential types , 1995, FPCA '95.

[30]  R. Hindley The Principal Type-Scheme of an Object in Combinatory Logic , 1969 .

[31]  Simon L. Peyton Jones,et al.  Implementing lazy functional languages on stock hardware: the Spineless Tagless G-machine , 1992, Journal of Functional Programming.

[32]  Glynn Winskel,et al.  Relational Reasoning about Functions and Nondeterminism , 1999 .

[33]  André L. M. Santos,et al.  Compilation by transformation in non-strict functional languages , 1995 .

[34]  Armin Kühnemann,et al.  Benefits of Tree Transducers for Optimizing Functional Programs , 1998, FSTTCS.

[35]  Matthew Hennessy,et al.  Semantics of programming languages - an elementary introduction using structural operational semantics , 1990 .

[36]  Tim Sheard,et al.  Revisiting catamorphisms over datatypes with embedded functions (or, programs from outer space) , 1996, POPL '96.

[37]  Philip Wadler,et al.  Theorems for free! , 1989, FPCA.

[38]  Paul Hudak,et al.  An Intermediate Meta-Language for Program Transformation , 1998 .

[39]  Andrew M. Pitts,et al.  A Metalanguage for Programming with Bound Names Modulo Renaming , 2000, MPC.

[40]  Harald Ganzinger Increasing Modularity and Language-Independency in Automatically Generated Compilers , 1983, Sci. Comput. Program..

[41]  David Sands,et al.  Proving the Correctness of Recursion-Based Automatic Program Transformations , 1995, Theor. Comput. Sci..

[42]  Jozef Gruska Foundations of Computing , 1997 .

[43]  Simon L. Peyton Jones,et al.  A short cut to deforestation , 1993, FPCA '93.

[44]  Heiko Vogler,et al.  Synthesized and inherited functions - a new computational model for syntax-directed semantics , 1994 .

[45]  Andrew M. Pitts,et al.  A new approach to abstract syntax involving binders , 1999, Proceedings. 14th Symposium on Logic in Computer Science (Cat. No. PR00158).

[46]  Leonidas Fegaras Fusion for Free , 1996 .

[47]  Wei-Ngan Chin,et al.  Parallelization in calculational forms , 1998, POPL '98.

[48]  Haskell B. Curry,et al.  Combinatory Logic, Volume I , 1959 .

[49]  Elmar Eder Properties of Substitutions and Unifications , 1983, GWAI.

[50]  Akihiko Takano,et al.  Shortcut deforestation in calculational form , 1995, FPCA '95.

[51]  Andrew Moran,et al.  Improvement in a lazy context: an operational theory for call-by-need , 1999, POPL '99.

[52]  S. Jones,et al.  A Transformation-Based Optimiser for Haskell , 1998, Sci. Comput. Program..

[53]  Maarten M. Fokkinga,et al.  Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire , 1991, FPCA.

[54]  Olaf Chitil,et al.  Common Subexpressions Are Uncommon in Lazy Functional Languages , 1997, Implementation of Functional Languages.

[55]  Simon L. Peyton Jones,et al.  Once upon a polymorphic type , 1999, POPL '99.

[56]  Wei-Ngan Chin Safe fusion of functional expressions , 1992, LFP '92.

[57]  Philip Wadler,et al.  Deforestation for Higher-Order Functions , 1992, Functional Programming.

[58]  Tim Sheard,et al.  A fold for all seasons , 1993, FPCA '93.

[59]  Alfred V. Aho,et al.  Compilers: Principles, Techniques, and Tools , 1986, Addison-Wesley series in computer science / World student series edition.

[60]  Simon L. Peyton Jones,et al.  Secrets of the Glasgow Haskell Compiler inliner , 2002, Journal of Functional Programming.

[61]  Fritz Henglein,et al.  Type inference with polymorphic recursion , 1993, TOPL.

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

[63]  Olaf Chitil Adding an Optimisation Pass to the Glasgow Haskell Compiler , 1997 .

[64]  Morten Heine Sørensen,et al.  Grammar-Based Data-Flow Analysis to Stop Deforestation , 1994, CAAP.

[65]  Carl A. Gunter Semantics of programming languages: structures and techniques , 1993, Choice Reviews Online.

[66]  Richard S. Bird,et al.  Introduction to functional programming , 1988, Prentice Hall International series in computer science.

[67]  Henk Barendregt,et al.  The Lambda Calculus: Its Syntax and Semantics , 1985 .

[68]  Helmut Seidl Integer Constraints to Stop Deforestation , 1996, ESOP.

[69]  John Launchbury,et al.  A natural semantics for lazy evaluation , 1993, POPL '93.

[70]  David Sands,et al.  Total correctness by local improvement in the transformation of functional programs , 1996, TOPL.

[71]  Geoff W. Hamilton Higher Order Deforestation , 2006, Fundam. Informaticae.

[72]  Trevor Jim What are principal typings and what are they good for? , 1996, POPL '96.

[73]  Geoff W. Hamilton,et al.  Extending Deforestation for First Order functional Programs , 1991, Functional Programming.

[74]  Gerald J. Sussman,et al.  Structure and interpretation of computer programs , 1985, Proceedings of the IEEE.

[75]  Hans Leiß,et al.  Extending the Type Checker of Standard ML by Polymorphic Recursion , 1999, Theor. Comput. Sci..

[76]  Peter G. Harrison,et al.  Functional Programming , 1988 .

[77]  Andrew Donald Gordon,et al.  Functional programming and input/output , 1995 .

[78]  Andrew Pitts,et al.  Semantics and Logics of Computation: Operationally-Based Theories of Program Equivalence , 1997 .

[79]  Wei-Ngan Chin,et al.  Better Consumers for Deforestation (Extended Abstract) , 1995, PLILP.

[80]  Simon L. Peyton Jones,et al.  Unboxed Values as First Class Citizens in a Non-Strict Functional Language , 1991, FPCA.

[81]  S. Purushothaman Iyer,et al.  An Operational Semantics of Sharing in Lazy Evaluation , 1996, Sci. Comput. Program..

[82]  John C. Reynolds,et al.  Types, Abstractions, and Parametric Polymorphism, Part 2 , 1991, MFPS.

[83]  Helmut Seidl,et al.  Constraints to stop higher-order deforestation , 1997, POPL '97.

[84]  Simon L. Peyton Jones,et al.  The Implementation of Functional Programming Languages , 1987 .

[85]  Robin Milner,et al.  Principal type-schemes for functional programs , 1982, POPL '82.

[86]  Franz Baader,et al.  Unification theory , 1986, Decis. Support Syst..

[87]  Didier Parigot,et al.  Declarative Program Transformation: A Deforestation Case-Study , 1999, PPDP.

[88]  Lennart Augustsson,et al.  PH Language Reference Manual, Version 1.0---preliminary , 1995 .

[89]  Jacob B. Schwartz Eliminating intermediate lists in pH , 2000 .

[90]  Masato Takeichi,et al.  An Extension Of The Acid Rain Theorem , 1996 .

[91]  J. Y. Girard,et al.  Interpretation fonctionelle et elimination des coupures dans l'aritmetique d'ordre superieur , 1972 .

[92]  Simon L. Peyton Jones,et al.  Lazy functional state threads , 1994, PLDI '94.

[93]  Peter Sestoft,et al.  Deriving a lazy abstract machine , 1997, Journal of Functional Programming.

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

[95]  Jean-Pierre Jouannaud,et al.  Rewrite Systems , 1991, Handbook of Theoretical Computer Science, Volume B: Formal Models and Sematics.

[96]  John Launchbury,et al.  Warm fusion: deriving build-catas from recursive definitions , 1995, FPCA '95.

[97]  Mark P. Jones,et al.  Functional Programming with Overloading and Higher-Order Polymorphism , 1995, Advanced Functional Programming.

[98]  Peter Lee,et al.  A Fresh Look at Combinator Graph Reduction (Or, Having a TIGRE by the Tail) , 1989 .

[99]  Leonidas Fegaras Efficient Optimization of Iterative Queries , 1993, DBPL.

[100]  Masato Takeichi,et al.  Deriving structural hylomorphisms from recursive definitions , 1996, ICFP '96.

[101]  Glynn Winskel,et al.  The formal semantics of programming languages - an introduction , 1993, Foundation of computing series.

[102]  Andrew John Gill,et al.  Cheap deforestation for non-strict functional languages , 1996 .

[103]  Masato Takeichi,et al.  Towards Manipulation of Mutually Recursive Functions , 1998, Fuji International Symposium on Functional and Logic Programming.

[104]  Simon L. Peyton Jones,et al.  Formally based profiling for higher-order functional languages , 1997, TOPL.

[105]  Robin Milner,et al.  A Theory of Type Polymorphism in Programming , 1978, J. Comput. Syst. Sci..

[106]  Kwangkeun Yi,et al.  Proofs about a folklore let-polymorphic type inference algorithm , 1998, TOPL.

[107]  John C. Reynolds,et al.  Types, Abstraction and Parametric Polymorphism , 1983, IFIP Congress.

[108]  Jerzy Tiuryn,et al.  Type reconstruction in the presence of polymorphic recursion , 1993, TOPL.

[109]  Alberto Martelli,et al.  An Efficient Unification Algorithm , 1982, TOPL.

[110]  John Hughes,et al.  Why Functional Programming Matters , 1989, Comput. J..

[111]  Wei-Ngan Chin,et al.  Safe fusion of functional expressions II: Further improvements , 1994, Journal of Functional Programming.

[112]  Helmut Seidl,et al.  Constraints to Stop Deforestation , 1998, Sci. Comput. Program..

[113]  Masato Takeichi,et al.  A calculational fusion system HYLO , 1997, Algorithmic Languages and Calculi.

[114]  Andrew M. Pitts,et al.  Parametric polymorphism and operational equivalence , 2000, Mathematical Structures in Computer Science.

[115]  John C. Reynolds,et al.  Towards a theory of type structure , 1974, Symposium on Programming.

[116]  Armin Kühnemann,et al.  Comparison of Deforestation Techniques for Functional Programs and for Tree Transducers , 1999, Fuji International Symposium on Functional and Logic Programming.

[117]  S. Abramsky The lazy lambda calculus , 1990 .

[118]  Thomas Johnsson,et al.  Attribute grammars as a functional programming paradigm , 1987, FPCA.

[119]  Olaf Chitil,et al.  Typer inference builds a short cut to deforestation , 1999, ICFP '99.

[120]  Zoltán Fülöp On attributed tree transducers , 1981, Acta Cybern..

[121]  Didier Parigot,et al.  Attribute Grammars and Functional Programming Deforestation , 1997, SAS.

[122]  Ralf Hinze,et al.  Haskell 98 — A Non−strict‚ Purely Functional Language , 1999 .