Investigating Minimally Strict Functions in Functional Programming

Total Page:16

File Type:pdf, Size:1020Kb

Investigating Minimally Strict Functions in Functional Programming Investigating Minimally Strict Functions in Functional Programming Dissertation zur Erlangung des akademischen Grades Doktor-Ingenieur (Dr.-Ing.) der Technischen Fakultät der Christian-Albrechts-Universität zu Kiel Jan Christiansen Kiel 2011 1. Gutachter Prof. Dr. Rudolf Berghammer 2. Gutachter Priv.-Doz. Dr. Frank Huch Datum der mündlichen Prüfung 12. Januar 2012 ii Contents 1 Introduction1 2 Preliminaries9 2.1 Introduction to Haskell............................9 2.1.1 Algebraic Data Types.........................9 2.1.2 Functions............................... 11 2.1.3 Types.................................. 15 2.1.4 Parametric Polymorphism...................... 16 2.1.5 Higher-Order............................. 18 2.1.6 Type Classes.............................. 21 2.2 Denotation of a Simple Functional Language............... 24 3 Non-Strict Evaluation 33 3.1 Advantages of Non-Strict Evaluation.................... 34 3.2 Unnecessarily Strict Functions........................ 36 4 Mathematical Model of Minimally Strict Functions 43 4.1 Least Strict Functions............................. 43 4.2 Sequential and Demanded Positions.................... 49 4.3 Minimally Strict Functions.......................... 65 4.3.1 Sufficiency of the Criterion..................... 69 4.3.2 Necessity of the Criterion...................... 75 5 Implementation of Sloth 83 5.1 Enumerating Test Cases........................... 84 5.2 Checking Test Cases............................. 90 5.3 Presenting Counter-Examples........................ 95 5.4 Identifying Sequential Positions....................... 97 6 Minimally Strict Polymorphic Functions 101 6.1 Introduction.................................. 102 6.2 Free Theorems................................. 104 6.3 Less Strict Functions on Lists........................ 108 6.4 Less Strict Functions in the Presence of seq................ 114 6.5 Minimally Strict Functions on Lists..................... 122 6.6 Generalization................................. 123 iii Contents 7 Case Studies 127 7.1 Deriving a Less Strict Implementation................... 127 7.2 Peano Multiplication............................. 133 7.3 Binary Arithmetics.............................. 136 7.4 The split Package............................... 144 7.4.1 The Function splitWhen....................... 146 7.4.2 The Function insertBlanks...................... 153 7.5 Reversing Lists................................ 157 8 Conclusion 165 8.1 Summary.................................... 165 8.2 Future Work.................................. 168 8.2.1 Functional Programming...................... 168 8.2.2 Functional-Logic Programming................... 171 A Proofs from Chapter4 173 A.1 Proofs from Section 4.2............................ 173 A.2 Proofs from Section 4.3.1........................... 175 A.3 Proofs from Section 4.3.2........................... 179 B Proofs from Chapter6 187 B.1 Proofs from Section 6.3............................ 187 iv Acknowledgments First of all I am very grateful to Olaf Chitil for introducing me to his excellent idea of checking whether a function is unnecessarily strict. Furthermore, I want to thank Rudolf Berghammer for his support, for tolerating my occasional loose tongue, and for not loosing patience with me. I am indebted to Fabian Reck for being an excellent officemate and a wine expert, and for always lending me an ear. Besides, I want to thank Daniel Seidel for being a great workmate and host and for even becoming a good friend. I owe thank to Janis Voigtländer for introducing me to the idea of free theorems and for collaborating with me in general. With respect to the work at hand I want to thank Ole Cordsen, Nikita Danilenko, Sebastian Fischer, Hauke Fuhrmann, Frank Kupke, Björn Peemöller, Matthias Quednau, Fabian Reck, Daniel Seidel, and Gabriel Wicke for reading parts of this thesis. I want to thank the “gang from the seventh floor”, which, besides some members already mentioned, includes Bernd Braßel, Stefan Bolus, Michael Hanus, Thomas Heß, Frank Huch, Ina Pfannschmidt, Peter Pichol, and Ulrike Pollakowski for providing a nice working environment and sharing lunch. In particular, I would like to single out the benefits that we all enjoy by having a great secretary like Ulrike. Moreover, thanks to the countless number of students for giving me the opportunity to teach them the basic parts of functional programming in several first term practical courses. Also, I would like to thank the easily countable number of students that took my advanced courses. Finally, a very big thanks to the team of the cafetaria of the “Studentenwerk Schleswig-Holstein” without whose constant supply with coffee and sugar in the form of muffins and donuts this work could not have been finished. v 1 Introduction In this thesis we consider the non-strict, functional programming language Haskell (Peyton Jones 2003). First of all, functional programing languages belong to the class of declarative programming languages. While in an imperative programming language the programmer specifies a procedure to get a solution to a problem, in a declarative language the programmer instead specifies the structure of a solution. In other words, while in an imperative language we define how a solution is computed, in a declarative language we rather define what is computed. Besides, Haskell has a quite strong type system, which is able to catch some bugs at compile-time, while these bugs do not emerge until run-time in a language with a weaker type system or no type system at all. And, finally, features like higher-order functions and overload- ing allow programmers to reuse code, which results in programs that are smaller and easier to maintain. However, even with a comparatively high-level language like Haskell, program- mers may write programs that are not optimal with respect to a certain criterion. Obviously, the most important criterion is correctness. That is, we prefer a pro- gram that meets our specifications over a program that does not. There are several approaches that assist programmers in writing correct Haskell programs like prop- erty-based testing (Claessen and Hughes 2000) or even automatic proving (Sonnex et al. 2011). Nonetheless, there are other important criteria to rate programs. For example, we could classify several implementations of the same algorithm in one language by means of run-time or memory consumption. This thesis investigates another criterion, namely, rating Haskell programs by means of strictness. Intuitively, in contrast to a strict programming language, in a non-strict program- ming language like Haskell a function only inspects the part of its argument that is needed to yield a certain part of its result. In the extreme case of a constant function, for example, the function does not evaluate its argument at all as the result of the function does not depend on the argument. Nevertheless, we can still define a con- stant function in Haskell that unnecessarily inspects its argument. More generally speaking, in Haskell a function definition rather non-declaratively specifies how an argument is inspected. There are often several implementations of functions with the same basic behavior that inspect different parts of their argument. While in the case of a constant function it is quite easy to observe that it does not have to inspect its argument, we will see that in general it is more difficult to observe whether a function can yield the same result by inspecting a smaller part of its argument. To rate functions with respect to strictness, we consider the following definition. A function f is less strict than a function g if the function f never inspects a larger part of its argument than the function g does, to yield a certain result. In addition, there exists a result such that f inspects a strictly smaller part of its argument than g does. We call a function f with these properties less strict than g. This “definition” is quite informal, and we will later give a formal definition of the less-strict relation by 1 1 Introduction means of a denotational semantics. For now this intuitive view should be sufficient for this introduction and will be helpful later on. As an example for the interest in less strict functions, let us consider the standard Haskell function partition. Waldmann(2000) as well as Russell(2000) have stated that the implementation of this function was more strict than necessary. The function partition takes a predicate — a unary funtion that yields a Boolean value — and a list — an ordered collection of values of equal type — and yields a pair — a container for two values. The pair holds two lists where the first list contains the elements that satisfy the predicate and the second list contains the elements that do not. It has been observed that partition, at that time, was unnecessarily strict because the implementation did not comply with an abstract specification given in the Haskell language report (Peyton Jones 2003). The language report specifies the behavior of partition by means of the standard Haskell function filter. The function filter takes a list and a predicate and yields a list of all elements that satisfy the predicate. The behavior of partition is specified by the following equivalence where p is an arbitrary predicate and xs an arbitrary list. Furthermore, the symbol ≡ denotes that the left-hand and the right-hand side yield the same results when evaluated. partition p xs ≡ (filter p xs, filter (not ◦ p) xs) The application of a function is denoted by juxta-position, for example,
Recommended publications
  • Parametric Polymorphism and Operational Improvement
    Parametric Polymorphism and Operational Improvement JENNIFER HACKETT, University of Nottingham, UK GRAHAM HUTTON, University of Nottingham, UK Parametricity, in both operational and denotational forms, has long been a useful tool for reasoning about program correctness. However, there is as yet no comparable technique for reasoning about program improve- ment, that is, when one program uses fewer resources than another. Existing theories of parametricity cannot be used to address this problem as they are agnostic with regard to resource usage. This article addresses this problem by presenting a new operational theory of parametricity that is sensitive to time costs, which can be used to reason about time improvement properties. We demonstrate the applicability of our theory by showing how it can be used to prove that a number of well-known program fusion techniques are time improvements, including fixed point fusion, map fusion and short cut fusion. CCS Concepts: • Software and its engineering → Functional languages; Polymorphism; • Theory of computation → Operational semantics; Additional Key Words and Phrases: parametricity, improvement theory ACM Reference Format: Jennifer Hackett and Graham Hutton. 2018. Parametric Polymorphism and Operational Improvement. Proc. ACM Program. Lang. 2, ICFP, Article 68 (September 2018), 24 pages. https://doi.org/10.1145/3236763 68 1 INTRODUCTION Parametric polymorphism is everywhere. In typed functional languages, many if not most of the built-in and user-defined functions are parametric. Because of this ubiquity, we must carefully study the properties of parametric functions. Chief among these properties is the abstraction theorem [Reynolds 1983], which shows that any well-typed term must satisfy a property that can be derived uniformly from its type.
    [Show full text]
  • Projections for Strictness Analysis
    Projections for Strictness Analysis Philip Wadler Programming Research Group, Oxford University and Programming Methodology Group, Chalmers University, GSteborg R.J.M. Hughes Department of Computer Science, University of Glasgow Abstract Contexts have been proposed as a means of performing strictness analysis on non-flat do- mains. Roughly speaking, a eontezt describes how much a sub-expression will be evaluated by the surrounding program. This paper shows how contexts can be represented using the notion of projection from domain theory. This is clearer than the previous explanation of contexts in terms of continuations. In addition, this paper describes finite dornaine of contexts over the non-flat list domain. This means that recursive context equations can be solved using standard fixpoint techniques, instead of the algebraic manipulation previously used. Praises of lazy functional languages have been widely sung, and so have some curses. One reason for praise is that laziness supports programming styles that are inconvenient or impossible otherwise [Joh87,Hug84,Wad85a]. One reason for cursing is that laziness hinders efficient implementation. Still, acceptable efficiency for lazy languages is at last being achieved. This is done by means of graph reduction [Pey87], as found in the G-machine [Aug84,Joh84] and the Ponder implementation [FW86], among others. The essential trick is to evaluate an expression immediately, when this is safe, rather than to construct a graph. Strictness analysis can reveal more places where this optimisation is safe. In the Ponder implementation, strictness analysis speeds up some programs by a factor of two or more. In addition, strictness analysis may enable other optimisations, such as destructive updating of arrays [HB85].
    [Show full text]
  • Foundations of Functional Programming
    Foundations of Functional Programming Computer Science Tripos Part IB Easter Term Lawrence C Paulson Computer Laboratory University of Cambridge [email protected] Copyright © 2021 by Lawrence C. Paulson Contents 1 Introduction 1 2 Equality and Normalization 6 3 Encoding Data in the λ-Calculus 11 4 Writing Recursive Functions in the λ-calculus 16 5 The λ-Calculus and Computation Theory 23 6 ISWIM: The λ-calculus as a Programming Language 29 7 Lazy Evaluation via Combinators 39 8 Compiling Methods Using Combinators 45 i ii 1 1 Introduction This course is concerned with the λ-calculus and its close relative, combinatory logic. The λ-calculus is important to functional programming and to computer science generally: 1. Variable binding and scoping in block-structured languages can be mod- elled. 2. Several function calling mechanisms — call-by-name, call-by-value, and call-by-need — can be modelled. The latter two are also known as strict evaluation and lazy evaluation. 3. The λ-calculus is Turing universal, and is probably the most natural model of computation. Church’s Thesis asserts that the ‘computable’ functions are precisely those that can be represented in the λ-calculus. 4. All the usual data structures of functional programming, including infinite lists, can be represented. Computation on infinite objects can be defined formally in the λ-calculus. 5. Its notions of confluence (Church-Rosser property), termination, and nor- mal form apply generally in rewriting theory. 6. Lisp, one of the first major programming languages, was inspired by the λ-calculus. Many functional languages, such as ML, consist of little more than the λ-calculus with additional syntax.
    [Show full text]
  • Department of Computer Science and Engineering 19600 N.W
    What is an Abstract Machine? Richard B. Kieburtz, Borislav Agapiev Oregon Graduate Institute Department of Computer Science and Engineering 19600 N.W. von Neumann Drive Beaverton. OR 97006-1999 USA Technical Report No. CS/E 91-01 1 August, 1991 What is an Abstract Machine? Richard B. Kieburtz Borislav Agapiev Oregon Graduate Institute 19600 N.W. von Neumann Dr. Beaverton, Oregon 97006 USA Technical Report CSE-091-011 August, 1991 Abstract The thesis of this paper is that categorical models provide an appropriate frame- work for the high-level specification of computer architectures. As an example of this approach, we specify a categorical abstract machine capable of normal-order reduction of lambda calculus expressions to weak head-normal form. The paper includes substan- tial theoretical development of the appropriate categories and monads, including an account of involution, analogous to negation in intuitionistic logic. An abstract machine is defined as a composite monad, a technique that emphasizes modularity of structure. In order to make control explicit in the machine model, the monad structure models continuations. This supports a formal specification of stored-program control. The categorical model is shown to be cartesian-closed. Finally, an implementation of (weak) lambda-calculus reduction by the categorical abstract machine is proved coherent with the syntactic reduction rule (B) of the cal- culus. Abstract Machines What is an Abstract Machine? Richard B. Kieburtz Borislau Agapiev Oregon Graduate Institute 19600 N.W. von Neumann Dr. Beaverton, Oregon 97006 1. Abstract machines Implementations of programming languages are often based upon an evaluation model called an 'abstract machine' [6,12,13,15,24,27].
    [Show full text]
  • Promoting Non-Strict Programming — Draft —
    Promoting Non-Strict Programming — Draft — Olaf Chitil University of Kent United Kingdom Abstract. In a non-strict functional programming language functions that yield the same result for all total arguments can still differ for partial arguments, that is, they differ in their strictness. Here a Haskell library is presented that enables the programmer to easily check whether a given function is least-strict; if it is not least-strict, then the tool suggests how to make it less strict. 1 Introduction Non-strict functional programming languages such as Haskell and Clean al- low the definition of non-strict functions. Language implementations are based on lazy evaluation, which defers computation until results are needed. John Hughes [4] showed how lazy evaluation provides a mechanism for composing a program from small, flexible, reusable parts. An intermediate data structure can be the glue between components. Such an intermediate data structure does not incur significant space costs, because only small bits of the data structure are produced on demand immediately before they are consumed and then their space can be reclaimed by the garbage collector. Additionally this programming style naturally implements online algorithms, which quickly produce part of an output before completely having processed all inputs. In practice, however, a program often requires far more space with lazy eval- uation than with standard eager evaluation, a phenomenon known as ”space leak”. I suspect that space leaks often occur because functions are not non-strict enough. When strict and non-strict functions are mixed, intermediate data struc- tures have to be constructed completely and then they use significant amounts of space.
    [Show full text]
  • The Provision of Non-Strictness, Higher Kinded Types and Higher Ranked Types on an Object Oriented Virtual Machine
    The Provision of Non-Strictness, Higher Kinded Types and Higher Ranked Types on an Object Oriented Virtual Machine A thesis submitted in partial fulfilment of the requirements for the Degree of Master of Science in the University of Canterbury by Oliver Hunt Examining Committee Dr. Wolfgang Kreutzer Supervisor Dr. Jeremy Gibbons Examiner Prof. K John Gough Examiner University of Canterbury 2006 To Mum and Dad Abstract We discuss the development of a number of algorithms and techniques to al- low object oriented virtual machines to support many of the features needed by functional and other higher level languages. These features include non- strict evaluation, partial function application, higher ranked and higher kinded types. To test the mechanisms that we have developed we have also produced a compiler to allow the functional language Haskell to be compiled to a native executable for the Common Language Runtime. This has allowed us to demonstrate that the techniques we have developed are practically viable. Table of Contents List of Figures v Chapter 1: Introduction 1 1.1 Topic................................ 1 1.2 Problems.............................. 2 1.2.1 Non-StrictEvaluation . 2 1.2.2 HigherKindedTypes. 3 1.2.3 HigherRankedTypes. 3 1.3 Evaluation............................. 4 1.4 Overview.............................. 4 Chapter 2: Background and Related Work 7 2.1 FunctionalProgrammingLanguages. 7 2.2 VirtualMachines ......................... 9 2.3 TargetinganOOVM ....................... 12 2.4 Existing Functional Language to Typed Virtual Machine Com- pilers................................ 13 2.5 Interfacing Haskell to an Object Oriented Virtual Machine .. 15 Chapter 3: Functions and Non-Strictness 17 3.1 LambdaExpressions . 17 3.2 PartialApplication . 20 3.2.1 Using The Push-Enter Model .
    [Show full text]
  • Department of Computing Science Catamorphism-Based Program
    Department of Computing Science Catamorphism-Based Program Transformations for Non-Strict Functional Languages L´aszl´oN´emeth A thesis submitted in partial fulfilment of the requirements for the degree of Doctor of Philosophy in Computing Science at the University of Glasgow November 2000 c L´aszl´oN´emeth 2000 Abstract In functional languages intermediate data structures are often used as glue to connect separate parts of a program together. These intermediate data structures are useful because they allow modularity, but they are also a cause of inefficiency: each element need to be allocated, to be examined, and to be deallocated. Warm fusion is a program transformation technique which aims to eliminate intermediate data structures. Functions in a program are first transformed into the so called build-cata form, then fused via a one-step rewrite rule, the cata-build rule. In the process of the transformation to build-cata form we attempt to replace explicit recursion with a fixed pattern of recursion (catamorphism). We analyse in detail the problem of removing — possibly mutually recursive sets of — polynomial datatypes. We have implemented the warm fusion method in the Glasgow Haskell Compiler, which has allowed practical feedback. One important conclusion is that catamorphisms and fusion in general deserve a more prominent role in the compilation process. We give a detailed measurement of our implementation on a suite of real application programs. Contents Abstract ii 1 Introduction 1 1.1 Contributions................................... 2 1.2 Structureofthethesis . .. .. .. .. .. .. .. .. 3 2 State of the Art in Intermediate Data Structure Removal 5 2.1 Wadler’s schema deforestation .
    [Show full text]
  • On the Design, Implementation, and Use of Laziness in R
    153 On the Design, Implementation, and Use of Laziness in R AVIRAL GOEL, Northeastern University, USA JAN VITEK, Czech Technical University and Northeastern University, USA The R programming language has been lazy for over twenty-five years. This paper presents a review ofthe design and implementation of call-by-need in R, and a data-driven study of how generations of programmers have put laziness to use in their code. We analyze 16,707 packages and observe the creation of 270.9 B promises. Our data suggests that there is little supporting evidence to assert that programmers use laziness to avoid unnecessary computation or to operate over infinite data structures. For the most part R code appears tohave been written without reliance on, and in many cases even knowledge of, delayed argument evaluation. The only significant exception is a small number of packages which leverage call-by-need for meta-programming. CCS Concepts: • General and reference → Empirical studies; • Software and its engineering → Gen- eral programming languages; Scripting languages; Semantics. Additional Key Words and Phrases: R language, delayed or lazy evaluation ACM Reference Format: Aviral Goel and Jan Vitek. 2019. On the Design, Implementation, and Use of Laziness in R. Proc. ACM Program. Lang. 3, OOPSLA, Article 153 (October 2019), 27 pages. https://doi.org/10.1145/3360579 1 INTRODUCTION Since its inception, in 1993, R has had a call-by-need semantics. When a function is invoked its arguments are packaged up into promises which are evaluated on demand. The values obtained by evaluating those promises are memoized to avoid the need for recomputation.
    [Show full text]
  • Foundations of Functional Programming
    Computer Science Tripos, Part IB Foundations of Functional Programming Lawrence C Paulson Computer Laboratory University of Cambridge Copyright c 1995 by Lawrence C. Paulson Contents 1 Introduction 1 2 Equality and Normalization 6 3 Encoding Data in the -Calculus 11 4 Writing Recursive Functions in the -calculus 15 5 The -Calculus and Computation Theory 21 6 ISWIM: The -calculus as a Programming Language 27 7 Lazy Evaluation via Combinators 37 8 Compiling Methods Using Combinators 42 i ii 1 1 Introduction This course is concerned with the -calculus and its close relative, combinatory logic. The -calculus is important to functional programming and to computer science gen- erally: 1. Variable binding and scoping in block-structured languages can be modelled. 2. Several function calling mechanisms — call-by-name, call-by-value, and call- by-need — can be modelled. The latter two are also known as strict evaluation and lazy evaluation. 3. The -calculus is Turing universal, and is probably the most natural model of computation. Church’s Thesis asserts that the ‘computable’ functions are pre- cisely those that can be represented in the -calculus. 4. All the usual data structures of functional programming, including infinite lists, can be represented. Computation on infinite objects can be defined formally in the -calculus. 5. Its notions of confluence (Church-Rosser property), termination, and normal form apply generally in rewriting theory. 6. Lisp, one of the first major programming languages, was inspired by the - calculus. Many functional languages, such as ML, consist of little more than the -calculus with additional syntax. 7. The two main implementation methods, the SECD machine (for strict evalua- tion) and combinator reduction (for lazy evaluation) exploit properties of the - calculus.
    [Show full text]
  • PROGRAMMING LANGUAGES Lucian Ilie
    CS342b – winter 2006 PROGRAMMING LANGUAGES Lucian Ilie c 2006 by Lucian Ilie CS342b – Programming Languages – winter 2006 – c 2006 by Lucian Ilie 2 1 Introduction - why study (concepts of) programming languages - classification of languages - programming domains - evaluation criteria - levels of programming languages - compilation and interpretation CS342b – Programming Languages – winter 2006 – c 2006 by Lucian Ilie 3 1.1 General questions 1.1.1 Why are there so many programming languages? - evolution – we’ve learned better ways of doing things over time - socio-economic factors – proprietary interests, commercial advantage - orientation toward special purposes - orientation toward special hardware - diverse ideas about what is pleasant to use 1.1.2 What makes a language successful? - easy to learn (BASIC, Pascal, LOGO, Scheme) - easy to express things – easy to use once fluent – ”powerful” (C++, Common Lisp, APL, Algol-68, Perl) - easy to implement (BASIC, Forth) - possible to compile to very good (fast/small) code (Fortran) - backing of a powerful sponsor (COBOL, PL/1, Ada, Visual Basic) - wide dissemination at minimal cost (Pascal, Turing, Java) CS342b – Programming Languages – winter 2006 – c 2006 by Lucian Ilie 4 1.1.3 Why do we have programming languages? – what is a language for? - way of thinking – way of expressing algorithms – from the user’s point of view - abstraction of virtual machine – way of specifying what you want the hardware to do without getting down into the bits – from the implementor’s point of view Knuth: “Computer
    [Show full text]
  • Denotational Semantics ______
    Denotational Semantics ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ A METHODOLOGY FOR LANGUAGE DEVELOPMENT ____________________________________________________________________________ ____________________________________________________________________________ David A. Schmidt Copyright notice: Copyright ! 1997 by David A. Schmidt. Permission to reproduce this material must be obtained from the author. Author’s address: David Schmidt, Department of Computing and Information Sciences, 234 Nichols Hall, Kansas State University, Manhattan, KS 66506. [email protected] Contents Preface viii Chapter 0 INTRODUCTION _______________________________________________________________________________________________ 1 Methods for Semantics Specification 2 Suggested Readings 3 Chapter 1 SYNTAX _____________________________________________________________________________________________________________ 5 1.1 Abstract Syntax Definitions 9 1.2 Mathematical and Structural Induction 12 Suggested Readings 15 Exercises 15 Chapter 2 SETS, FUNCTIONS, AND DOMAINS _____________________________________________________________ 17 2.1 Sets 17 2.1.1 Constructions on Sets 18 2.2 Functions 20 2.2.1 Representing Functions as Sets 21 2.2.2 Representing Functions as Equations 24 2.3 Semantic Domains 25 2.3.1 Semantic Algebras 25 Suggested Readings 27 Exercises 27 Chapter 3 DOMAIN THEORY
    [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]