Control-Flow Analysis of Higher-Order Languages Olin

Total Page:16

File Type:pdf, Size:1020Kb

Control-Flow Analysis of Higher-Order Languages Olin Control-Flow Analysis of Higher-Order Languages or Taming Lambda Olin Shivers May 1991 CMU-CS-91-145 School of Computer Science Carnegie Mellon University Pittsburgh, PA 15213 Submitted in partial ful®llment of the requirements for the degree of Doctor of Philosophy. c 1991 Olin Shivers This research was sponsored by the Of®ce of Naval Research under Contract N00014-84-K-0415. The views and conclusions contained in this document are those of the author and should not be interpreted as representing the of®cial policies, either expressed or implied, of ONR or the U.S. Government. Keywords: data-¯ow analysis, Scheme, LISP, ML, CPS, type recovery, higher-order functions, functional programming, optimising compilers, denotational semantics, non- standard abstract semantic interpretations Abstract Programs written in powerful, higher-order languages like Scheme, ML, and Common Lisp should run as fast as their FORTRAN and C counterparts. They should, but they don't. A major reason is the level of optimisation applied to these two classes of languages. Many FORTRAN and C compilers employ an arsenal of sophisticated global optimisations that depend upon data-¯ow analysis: common-subexpression elimination, loop-invariant detection, induction-variable elimination, and many, many more. Compilers for higher- order languages do not provide these optimisations. Without them, Scheme, LISP and ML compilers are doomed to produce code that runs slower than their FORTRAN and C counterparts. The problem is the lack of an explicit control-¯ow graph at compile time, something which traditional data-¯ow analysis techniques require. In this dissertation, I present a technique for recovering the control-¯ow graph of a Scheme program at compile time. I give examples of how this information can be used to perform several data-¯ow analysis optimisations, including copy propagation, induction-variable elimination, useless-variable elimination, and type recovery. The analysis is de®ned in terms of a non-standard semantic interpretation. The denotational semantics is carefully developed, and several theorems establishing the correctness of the semantics and the implementing algorithms are proven. iii iv To my parents, Julia and Olin. v vi Contents Acknowledgments xi 1 Introduction 1 1.1 My Thesis : :: :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 1 1.2 Structure of the Dissertation ::: :: :: :: :: ::: :: :: :: :: :: 2 1.3 Flow Analysis and Higher-Order Languages : :: ::: :: :: :: :: :: 3 2 Basic Techniques: CPS and NSAS 7 2.1 CPS : ::: :: :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 7 2.2 Control Flow in CPS :: :: ::: :: :: :: :: ::: :: :: :: :: :: 11 2.3 Non-Standard Abstract Semantic Interpretation : ::: :: :: :: :: :: 12 3 Control Flow I: Informal Semantics 15 3.1 Notation :: :: :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 15 3.2 Syntax ::: :: :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 16 3.3 Standard Semantics :: :: ::: :: :: :: :: ::: :: :: :: :: :: 17 3.4 Exact Control-Flow Semantics :: :: :: :: :: ::: :: :: :: :: :: 23 3.5 Abstract Control-Flow Semantics :: :: :: :: ::: :: :: :: :: :: 26 3.6 0CFA : ::: :: :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 31 3.7 Other Abstractions : :: :: ::: :: :: :: :: ::: :: :: :: :: :: 33 3.8 Semantic Extensions :: :: ::: :: :: :: :: ::: :: :: :: :: :: 34 4 Control Flow II: Detailed Semantics 41 4.1 Patching the Equations : :: ::: :: :: :: :: ::: :: :: :: :: :: 42 4.2 Abstraction Functions : :: ::: :: :: :: :: ::: :: :: :: :: :: 53 4.3 Existence :: :: :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 57 4.4 Properties of the Semantic Functions :: :: :: ::: :: :: :: :: :: 61 vii 5 Implementation I 79 5.1 The Basic Algorithm :: :: ::: :: :: :: :: ::: :: :: :: :: :: 79 5.2 Exploiting Monotonicity :: ::: :: :: :: :: ::: :: :: :: :: :: 80 5.3 Time-Stamp Approximations :: :: :: :: :: ::: :: :: :: :: :: 81 5.4 Particulars of My Implementation :: :: :: :: ::: :: :: :: :: :: 84 6 Implementation II 85 6.1 Preliminaries :: :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 85 6.2 The Basic Algorithm :: :: ::: :: :: :: :: ::: :: :: :: :: :: 86 6.3 The Aggressive-Cutoff Algorithm :: :: :: :: ::: :: :: :: :: :: 87 6.4 The Time-Stamp Algorithm ::: :: :: :: :: ::: :: :: :: :: :: 90 7 Applications I 93 7.1 Induction-Variable Elimination : :: :: :: :: ::: :: :: :: :: :: 93 7.2 Useless-Variable Elimination :: :: :: :: :: ::: :: :: :: :: :: 98 7.3 Constant Propagation : :: ::: :: :: :: :: ::: :: :: :: :: :: 101 8 The Environment Problem and Re¯ow Analysis 103 8.1 Limits of Simple Control-Flow Analysis : :: :: ::: :: :: :: :: :: 103 8.2 Re¯ow Analysis and Temporal Distinctions : :: ::: :: :: :: :: :: 105 9 Applications II: Type Recovery 107 9.1 Type Recovery : :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 107 9.2 Quantity-based Analysis :: ::: :: :: :: :: ::: :: :: :: :: :: 112 9.3 Exact Type Recovery : :: ::: :: :: :: :: ::: :: :: :: :: :: 112 9.4 Approximate Type Recovery ::: :: :: :: :: ::: :: :: :: :: :: 119 9.5 Implementation :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 127 9.6 Discussion and Speculation ::: :: :: :: :: ::: :: :: :: :: :: 128 9.7 Related Work : :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 133 10 Applications III: Super- 135 10.1 Copy Propagation : :: :: ::: :: :: :: :: ::: :: :: :: :: :: 135 10.2 Lambda Propagation :: :: ::: :: :: :: :: ::: :: :: :: :: :: 137 10.3 Cleaning Up :: :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 141 10.4 A Uni®ed View :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 141 viii 11 Future Work 143 11.1 Theory ::: :: :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 143 11.2 Other Optimisations :: :: ::: :: :: :: :: ::: :: :: :: :: :: 144 11.3 Extensions : :: :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 146 11.4 Implementation :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 149 12 Related Work 151 12.1 Early Abstract Interpretation: the Cousots and Mycroft :: :: :: :: :: 151 12.2 Hudak ::: :: :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 152 12.3 Higher-Order Analyses: Deutsch and Harrison : ::: :: :: :: :: :: 153 13 Conclusions 155 Appendices A 1CFA Code Listing 157 A.1 Preliminaries :: :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 157 ::: :: :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: A.2 1cfa.t 161 B Analysis Examples 173 B.1 Puzzle example :: :: :: ::: :: :: :: :: ::: :: :: :: :: :: 173 B.2 Factorial example : :: :: ::: :: :: :: :: ::: :: :: :: :: :: 176 Bibliography 179 ix x Acknowledgments It is a great pleasure to thank the people who helped me with my graduate career. First, I thank the members of my thesis committee: Peter Lee, Allen Newell, John Reynolds, Jeannette Wing, and Paul Hudak. Peter Lee, my thesis advisor and good friend, taught me how to do research, taught me how to write, helped me publish papers, and kept me up-to-date on the latest developments in Formula-1 Grand Prix. I suppose that I recursively owe a debt to Uwe Pleban for teaching Peter all the things he taught me. Allen Newell was my research advisor for my ®rst four years at CMU, and co-advised me with Peter after I picked up my thesis topic. He taught me about having ªa bright blue ¯ame for science.º Allen sheltered me during the period when I was drifting in research limbo; I'm reasonably certain that had I chosen a different advisor, I'd have become an ex-Ph.D.-candidate pretty rapidly. Allen believed in me Ð certainly more than I did. John Reynolds taught (drilled/hammered/beat into) me the importance of formal rigour in my work. Doing mathematics with John is an exhilarating experience, if you have lots of stamina and don't mind chalk dust or feelings of inadequacy. John's emphasis on formal methods rescued my dissertation from being just another pile of hacks. Jeannette Wing was my contact with the programming-systems world while I was of®cially an AI student. She supported my control-¯ow analysis research in its early stages. She also has an eagle eye for poor writing and a deep appreciation for edged weapons. Paul Hudak's work in¯uenced me a great deal. His papers on abstract semantic interpretations caused me to consider applying the whole approach to my problem. It goes without saying that all of these ®ve scholars are dedicated researchers, else they would not have attained their current positions. HoweverÐand this is not a given in academeÐthey also have a deep committment to their students' welfare and development. I was very, very lucky to be trained in the craft of science under the tutelage of these individuals. xi In the summer of 1984, Forest Baskett hired the entire T group to come to DEC's Western Research Laboratory and write a Scheme compiler. I was going to do the data-¯ow analysis module of the compiler; it would be fair to say that I am now ®nally ®nishing my 1984 summer job. The compiler Forest hired us to write turned into the ORBIT Scheme compiler, and this dissertation is the third one to come out of that summer project (so far). Both Forest and DEC have a record of sponsoring grad students for summer research projects that are interesting longshots, and I'd like to acknowledge their foresight, scope and generosity. If one is going to do research on advanced programming languages, it certainly helps to be friends with someone like Jonathan Rees. In 1982, Jonathan was the other guy at Yale who'd read the Sussman/Steele papers. We agreed that hacking Scheme and spinning great plans was much more fun than interminable theory classes. Jonathan's deep understanding of computer languages and good systems taste has in¯uenced me repeatedly. Norman Adams, like Jonathan a member of the T project, contributed his technical and
Recommended publications
  • Catamorphic Approach to Program Analysis
    Catamorphic Approach to Program Analysis Mizuhito OGAWA1,2, Zhenjiang HU1,2, Isao SASANO1, Masato TAKEICHI1 1 The University of Tokyo 2 PRESTO 21, Japan Science and Technology Corporation {mizuhito,hu,sasano,takeichi}@ipl.t.u-tokyo.ac.jp Abstract. This paper proposes a new framework for program analysis that regards them as maximum marking problems: mark the codes of a program under a certain condition such that the number of marked nodes is maximum. We show that if one can develop an efficient checking program (in terms of a finite catamorphism) whether marked programs are correctly marked, then an efficient and incremental marking algorithm to analyze control flow of structured programs (say in C or Java with limited numbers of gotos) is obtained for free. To describe catamorphisms on control flow graphs, we present an algebraic construction, called SP Term, for graphs with bounded tree width. The transformation to SP Terms is done efficiently in linear time. We demonstrate that our framework is powerful to describe various kinds of control flow analyses. Especially, for analyses in which some optimality is required (or expected), our approach has a big advantage. Keywords: Catamorphism, Control Flow Graphs, Control Flow Analysis, Functional Programming, Linear Time Algorithm, Program Optimization, Bounded Tree Width 1 Introduction While program transformation plays an important role in optimizing compiler and software engi- neering, implementing program transformation requires various kinds of program analyses to extract program fragments satisfying a certain property; these fragments will then be replaced by equivalent but more efficient constructs. As a simple example, consider the program in Fig.
    [Show full text]
  • Control-Flow Analysis of Functional Programs
    Control-flow analysis of functional programs JAN MIDTGAARD Department of Computer Science, Aarhus University We present a survey of control-flow analysis of functional programs, which has been the subject of extensive investigation throughout the past 30 years. Analyses of the control flow of functional programs have been formulated in multiple settings and have led to many different approximations, starting with the seminal works of Jones, Shivers, and Sestoft. In this paper, we survey control-flow analysis of functional programs by structuring the multitude of formulations and approximations and comparing them. Categories and Subject Descriptors: D.3.2 [Programming Languages]: Language Classifica- tions—Applicative languages; F.3.1 [Logics and Meanings of Programs]: Specifying and Ver- ifying and Reasoning about Programs General Terms: Languages, Theory, Verification Additional Key Words and Phrases: Control-flow analysis, higher-order functions 1. INTRODUCTION Since the introduction of high-level languages and compilers, much work has been devoted to approximating, at compile time, which values the variables of a given program may denote at run time. The problem has been named data-flow analysis or just flow analysis. In a language without higher-order functions, the operator of a function call is apparent from the text of the program: it is a lexically visible identifier and therefore the called function is available at compile time. One can thus base an analysis for such a language on the textual structure of the program, since it determines the exact control flow of the program, e.g., as a flow chart. On the other hand, in a language with higher-order functions, the operator of a function call may not be apparent from the text of the program: it can be the result of a computation and therefore the called function may not be available until run time.
    [Show full text]
  • Refunctionalization of Abstract Abstract Machines Bridging the Gap Between Abstract Abstract Machines and Abstract Definitional Interpreters (Functional Pearl)
    105 Refunctionalization of Abstract Abstract Machines Bridging the Gap between Abstract Abstract Machines and Abstract Definitional Interpreters (Functional Pearl) GUANNAN WEI, Purdue University, USA JAMES DECKER, Purdue University, USA TIARK ROMPF, Purdue University, USA Abstracting abstract machines is a systematic methodology for constructing sound static analyses for higher- order languages, by deriving small-step abstract abstract machines (AAMs) that perform abstract interpretation from abstract machines that perform concrete evaluation. Darais et al. apply the same underlying idea to monadic definitional interpreters, and obtain monadic abstract definitional interpreters (ADIs) that perform abstract interpretation in big-step style using monads. Yet, the relation between small-step abstract abstract machines and big-step abstract definitional interpreters is not well studied. In this paper, we explain their functional correspondence and demonstrate how to systematically transform small-step abstract abstract machines into big-step abstract definitional interpreters. Building on known semantic interderivation techniques from the concrete evaluation setting, the transformations include lin- earization, lightweight fusion, disentanglement, refunctionalization, and the left inverse of the CPS transform. Linearization expresses nondeterministic choice through first-order data types, after which refunctional- ization transforms the first-order data types that represent continuations into higher-order functions. The refunctionalized AAM is an abstract interpreter written in continuation-passing style (CPS) with two layers of continuations, which can be converted back to direct style with delimited control operators. Based on the known correspondence between delimited control and monads, we demonstrate that the explicit use of monads in abstract definitional interpreters is optional. All transformations properly handle the collecting semantics and nondeterminism of abstract interpreta- tion.
    [Show full text]
  • The Complexity of Flow Analysis in Higher-Order Languages
    The Complexity of Flow Analysis in Higher-Order Languages David Van Horn arXiv:1311.4733v1 [cs.PL] 19 Nov 2013 The Complexity of Flow Analysis in Higher-Order Languages A Dissertation Presented to The Faculty of the Graduate School of Arts and Sciences Brandeis University Mitchom School of Computer Science In Partial Fulfillment of the Requirements for the Degree Doctor of Philosophy by David Van Horn August, 2009 This dissertation, directed and approved by David Van Horn’s committee, has been accepted and approved by the Graduate Faculty of Brandeis University in partial fulfillment of the requirements for the degree of: DOCTOR OF PHILOSOPHY Adam B. Jaffe, Dean of Arts and Sciences Dissertation Committee: Harry G. Mairson, Brandeis University, Chair Olivier Danvy, University of Aarhus Timothy J. Hickey, Brandeis University Olin Shivers, Northeastern University c David Van Horn, 2009 Licensed under the Academic Free License version 3.0. in memory of William Gordon Mercer July 22, 1927–October 8, 2007 Acknowledgments Harry taught me so much, not the least of which was a compelling kind of science. It is fairly obvious that I am not uninfluenced by Olivier Danvy and Olin Shivers and that I do not regret their influence upon me. My family provided their own weird kind of emotional support and humor. I gratefully acknowledge the support of the following people, groups, and institu- tions, in no particular order: Matthew Goldfield. Jan Midtgaard. Fritz Henglein. Matthew Might. Ugo Dal Lago. Chung-chieh Shan. Kazushige Terui. Christian Skalka. Shriram Krishnamurthi. Michael Sperber. David McAllester. Mitchell Wand. Damien Sereni. Jean-Jacques Levy.´ Julia Lawall.
    [Show full text]
  • N350.Doc ISOSTD ISO Template Version 3.0 1997-02-07 © ISO/IEC ISO/IEC PDTR 15942
    ISO/IEC JTC1 /SC 22 N 350 Date: 98-08-17 ISO/IEC PDTR 15942 ISO/IEC JTC 1/SC22/WG9 Secretariat: ANSI Working Draft 3.6 - Programming Languages - Guide for the Use of the Ada Programming Language in High Integrity Systems Document type: Technical Report type 3 Document subtype: Not applicable Document stage: (20) Preparatory Document language: E H:\ada\hrg\v36\n350.doc ISOSTD ISO Template Version 3.0 1997-02-07 © ISO/IEC ISO/IEC PDTR 15942 Contents 1 Scope ........................................................................................................................................................................1 1.1 Within the scope ..............................................................................................................................................1 1.2 Out of scope.....................................................................................................................................................2 2 Verification Techniques ..........................................................................................................................................2 2.1 Traceability.......................................................................................................................................................2 2.2 Reviews.............................................................................................................................................................3 2.3 Analysis ............................................................................................................................................................3
    [Show full text]
  • Control-Flow Analysis of Functional Programs
    BRICS RS-07-18 J. Midtgaard: Control-Flow Analysis of Functional Programs BRICS Basic Research in Computer Science Control-Flow Analysis of Functional Programs Jan Midtgaard BRICS Report Series RS-07-18 ISSN 0909-0878 December 2007 Copyright c 2007, Jan Midtgaard. BRICS, Department of Computer Science University of Aarhus. All rights reserved. Reproduction of all or part of this work is permitted for educational or research use on condition that this copyright notice is included in any copy. See back inner page for a list of recent BRICS Report Series publications. Copies may be obtained by contacting: BRICS Department of Computer Science University of Aarhus IT-parken, Aabogade 34 DK–8200 Aarhus N Denmark Telephone: +45 8942 9300 Telefax: +45 8942 5601 Internet: [email protected] BRICS publications are in general accessible through the World Wide Web and anonymous FTP through these URLs: http://www.brics.dk ftp://ftp.brics.dk This document in subdirectory RS/07/18/ Control-flow analysis of functional programs Jan Midtgaard BRICS Department of Computer Science University of Aarhus∗ November 16, 2007 Abstract We present a survey of control-flow analysis of functional programs, which has been the subject of extensive investigation throughout the past 25 years. Analyses of the control flow of functional programs have been formulated in multiple settings and have led to many different approxima- tions, starting with the seminal works of Jones, Shivers, and Sestoft. In this paper we survey control-flow analysis of functional programs by struc- turing the multitude of formulations and approximations and comparing them. ∗IT-parken, Aabogade 34, DK-8200 Aarhus N, Denmark.
    [Show full text]
  • Research Summary
    Research Summary Franklyn Turbak Department of Computer Science Wellesley College August 1, 2001 My main research area is the design, analysis, and implementation of expressive programming languages. I also work on pedagogical aspects and applications of programming and of programming languages. This document summarizes my research and publications in these areas. Much of the research described here was undertaken as part of the Church Project1, a group of programming language researchers investigating applications of formal systems in programming language design, analysis, and implementation. I was a co-founder of the Church Project in Septem- ber, 1995, and have been working closely with its members ever since. My research with the Church Project is supported with funding from an NSF Experimental Software Systems grant, an individ- ual grant that is part of a larger collaborative grant with Boston University, Boston College, and Stevens Institute of Technology. Some of my research was conducted with colleagues and students at my home institution, Wellesley College. I believe it is important for undergraduate computer science students to experi- ence the ¯eld outside their coursework, so I encourage my students to become involved in research and independent study projects. Although the technical prerequisites for programming language research set a fairly high bar for undergraduates, I have managed to involve undergraduates in several projects related to programming languages (see Sections 4.3 and 6). This summary is organized into sections according to research themes. In each section, I present the context of my research, describe my publications and contributions, and, where relevant, de- scribe future research plans.
    [Show full text]
  • A Study of Control and Type-Flow Analyses for Higher-Order Programming Languages
    A Study of Control and Type-Flow Analyses for Higher-Order Programming Languages Gabriel Petrov Capstone Final Report for BSc (Honours) in Mathematical, Computational and Statistical Sciences Supervised by: Ilya Sergey AY 2020/2021 i ii Acknowledgements I can scarcely put into words how grateful I am to the following peo- ple, for shaping my experiences during my undergraduate education and helping me become the person I am today. To my first computer science professor, Prof Aquinas Hobor, for in- spiring me to pursue a journey in the area, despite the fact that prior to his class I didn’t even know it was a field. Thank-you, for I have so far found that journey to be nothing short of incredible. To my capstone advisor, Prof Ilya Sergey, without whom this thesis would not exist, for his gracious counsel, for his many classes, for de- scribing the field of computer science as a forever incomplete jigsaw puz- zle in sophomore year, and for helping me place the first few pieces down in their correct spaces. To Tram and all of my friends - Adair, Alaukik, Alvin, Karolina, Leyli, Michael, Max, Ryan and more - for far too many moments to count. To my sisters and my parents, the former, for growing up far faster than I could’ve thought possible and the latter for never failing to enable me in my endeavours, for always maintaining their quiet, but unyielding support. I know I don’t rely on it often, but its presence is a comfort. Finally, I am grateful to a Chuhu, whose smile somewhere out there never ceases to make the world shine a little more warmly.
    [Show full text]
  • Eliminating Dead Code on Recursive Data ?
    Eliminating Dead Code on Recursive Data ? Yanhong A. Liu, Scott D. Stoller Computer Science Department, State University of New York at Stony Brook Stony Brook, NY 11794, USA E-mail: fliu, [email protected] Abstract This paper describes a powerful method for dead code analysis and elimination in the presence of recursive data constructions. We describe partially dead recursive data using liveness patterns based on general regular tree grammars extended with the notion of live and dead, and we formulate the analysis as computing liveness patterns at all program points based on constraints constructed from the program and programming language semantics. The analysis yields the most precise program- based grammars that satisfy the constraints. The analysis algorithm takes cubic time in terms of the size of the program in the worst case but is very e±cient in practice, as shown by our prototype implementation. The analysis results are used to identify and eliminate dead code. The framework for representing and analyzing properties of recursive data structures using general regular tree grammars applies to other analyses as well. Key words: Dead-code elimination, Recursive data structures, Regular-tree grammars, Constraints, Program analysis, Slicing 1 Introduction Dead computations produce values that never get used [1]. While program- mers are not likely to write code that performs dead computations, such code appears often as the result of program optimization, modi¯cation, and reuse [45,1]. There are also other programming activities that do not ex- plicitly involve live or dead code but rely on similar notions. Examples are program slicing [64,50], specialization [50], incrementalization [39,38], and ? This work was supported in part by NSF under grant CCR-9711253 and ONR under grants N00014-99-1-0132, N00014-99-1-0358, and N00014-01-1-0109.
    [Show full text]
  • Constraints to Stop Deforestation
    Science of Computer Programming ELSEVIER Science of Computer Programming 32 (1998) 73-107 Constraints to stop deforestation H. Seidl”,*, M.H. Sm-ensenb aFB IV - Informatik. Universitiit Trier, D-54286 Trier, Germany bDepartment of Computer Science, University of Copenhagen, Universitetsparken I, DK-2IOO Copenhagen 0. Denmark Abstract Wadler’s deforestation algorithm eliminates intermediate data structures from functional pro- grams. To be suitable for inclusion in a compiler, deforestation must terminate on all programs. Several techniques exist to ensure termination of deforestation on all first-order programs, but general techniques for higher-order programs were introduced only recently first by Hamilton and then by Marlow. We present a new technique for ensuring termination of deforestation on all higher-order pro- grams that allows useful transformation steps prohibited in Hamilton’s and Marlow’s techniques. The technique uses a constraint-based higher-order control-flow analysis. We also relate our technique to previous approaches to termination of first- and higher-order deforestation in some detail. @ 1998 Elsevier Science B.V. All rights reserved. Keywords: Deforestation; Intermediate data structures; Higher-order functional programs; Termination detection; Constraint-based program analysis; Integer constraints 1. Introduction Lazy, higher-order, functional programming languages lend themselves to an elegant style of programming which uses intermediate data structures [31]. However, this style also leads to inefficient programs. Example 1. Consider the following program: letrec a=Axs,ys. case xs of [I-ys; (t:ts)-+t:a tsys in Aus,us, ws. a (a us us) ws Program syntax is introduced officially in Section 2; here we introduce some terminol- ogy useful for the informal introduction in the present section.
    [Show full text]
  • Lecture Notes: Control Flow Analysis for Functional Languages
    Lecture Notes: Control Flow Analysis for Functional Languages 15-819O: Program Analysis Jonathan Aldrich [email protected] Lecture 10 1 Analysis of Functional Programs We now consider analysis of functional programs. Consider an idealized functional language, similar to the core of Scheme or ML, based on the lambda calculus. We can define a grammar for expressions in the language as follows: e :: λx.e | x | e1 e2 | n | e e | ::: The grammar includes a definition of an anonymous function λx.e, where x is the function argument and e is the body of the function. The function can include variables x or function calls e1 e2, where e1 is the func- tion to be invoked and e2 is passed to that function as an argument. (In an imperative language this would more typically be written e1pe2q but we follow the functional convention here). We evaluate a function call pλx.eq v with some value v as the argument by substituting the argument v for all occurrences of x in the body e of the function. For example, pλx.x 1q 3 evaluates to 3 1, which of course evaluates further to 4. 1 A more interesting execution example would be pλf:f 3qpλx.x 1q. This first evaluates by substituting the argument for f, yielding pλx.x 1q 3. Then we invoke the function getting 3 1 which again evaluates to 4. Let us consider an analysis such as constant propagation applied to this language. Because functional languages are not based on statements but rather expressions, it is appropriate to consider not just the values of vari- ables, but also the values that expressions evaluate to.
    [Show full text]
  • Hybrid Eager and Lazy Evaluation For
    Hybrid Eager and Lazy Evaluation for Efficient Compilation of Haskell by Jan-Willem Maessen Submitted to the Department of Electrical Engineering and Computer Science in partial fulfillment of the requirements for the degree of Doctor of Philosophy in Computer Science at the MASSACHUSETTS INSTITUTE OF TECHNOLOGY June 2002 c Massachusetts Institute of Technology 2002. All rights reserved. Author........................................................................... Department of Electrical Engineering and Computer Science 17 May 2002 Certified by . Arvind Charles and Jennifer Johnson Professor of Computer Science and Engineering Thesis Supervisor Accepted by . Arthur C. Smith Chairman, Department Committee on Graduate Students Hybrid Eager and Lazy Evaluation for Efficient Compilation of Haskell by Jan-Willem Maessen Submitted to the Department of Electrical Engineering and Computer Science on 17 May 2002, in partial fulfillment of the requirements for the degree of Doctor of Philosophy in Computer Science Abstract The advantage of a non-strict, purely functional language such as Haskell lies in its clean equational semantics. However, lazy implementations of Haskell fall short: they cannot express tail recursion gracefully without annotation. We describe resource-bounded hybrid evaluation, a mixture of strict and lazy evaluation, and its realization in Eager Haskell. From the programmer’s perspective, Eager Haskell is simply another implementation of Haskell with the same clean equational semantics. Iteration can be expressed using tail recursion, without the need to resort to program annotations. Under hybrid evaluation, computations are ordinarily executed in program order just as in a strict functional language. When particular stack, heap, or time bounds are exceeded, suspensions are generated for all outstanding computations. These suspensions are re-started in a demand-driven fashion from the root.
    [Show full text]