Tail Recursion without Space Leaks
نویسنده
چکیده
The G-machine (Johnsson, 1987; Peyton Jones, 1987) is a compiled graph reduction machine for lazy functional languages. The G-machine compiler contains many optimisations to improve performance. One set of such optimisations is designed to improve the performance of tail recursive functions. Unfortunately the abstract machine is subject to a space leak—objects are unnecessarily preserved by the garbage collector. This paper analyses why a particular form of space leak occurs in the G-machine, and presents some ideas for fixing this problem. This phenomena in other abstract machines is also examined briefly. 1. Compilers for conventional imperative languages How might a simple Pascal procedure, like the one shown below, be implemented? procedure f; begin ... g; end; Typically the procedure which called f would set up a new stack frame for it, including such information as return addresses and any arguments passed to f. In turn, a naïve implementation of f might set up a further stack frame for g. This is called a tail call. However, it is clearly unnecessary to retain f’s stack frame since its information (apart from the return address) will never be used again. The implementation can be improved by jumping to g rather than calling it. In doing so, the same stack frame, including f’s return address, can be used for g as was used for f. If the tail call is recursive, the situation is called tail recursion, and the effect of jumping rather than calling is to turn recursion into iteration, which can be performed in constant space (Steele, 1977; Bauer, 1982). 2. Simple graph reducers get this optimisation for free Graph reduction (Wadsworth, 1971) is one method of implementing lazy functional languages. The essential idea behind it is as follows: to reduce an expression (1) construct the graph of the expression; (2) unwind the spine of this graph (leaving pointers to vertebrae—application nodes—on the stack) until the object in the function position (the leftmost, outermost term) is discovered; (3) if this object is indeed a function, and all its arguments are present, apply the function to its arguments;
منابع مشابه
Space-Efficient Latent Contracts
Standard higher-order contract monitoring breaks tail recursion and leads to space leaks that can change a program’s asymptotic complexity; space-efficiency restores tail recursion and bounds the amount of space used by contracts. Space-efficient contract monitoring for contracts enforcing simple type disciplines (a/k/a gradual typing) is well studied. Prior work establishes a space-efficient s...
متن کاملModeling Sharing and Recursion for Weak Reduction Strategies Using Explicit Substitution
We present the a w-calculus, a formal synthesis of the concepts of sharing and explicit substitution for weak reduction. We show how a w can be used as a foundation of implementations of functional programming languages by modeling the essential ingredients of such implementations, namely weak reduction strategies, recursion, space leaks, recursive data structures, and parallel evaluation, in a...
متن کاملProper Tail Recursion and Space Eeciency
The IEEE/ANSI standard for Scheme requires implementations to be properly tail recursive. This ensures that portable code can rely upon the space eeciency of continuation-passing style and other idioms. On its face, proper tail recursion concerns the eeciency of procedure calls that occur within a tail context. When examined closely, proper tail recur-sion also depends upon the fact that garbag...
متن کاملCSci 450: Org. of Programming Languages Evaluation and Efficiency
3 Evaluation and Efficiency 2 3.1 Chapter Introduction . . . . . . . . . . . . . . . . . . . . . . . . . 2 3.2 Evaluation of Functional Programs . . . . . . . . . . . . . . . . . 2 3.2.1 Referential transparency . . . . . . . . . . . . . . . . . . . 3 3.2.2 Substitution model . . . . . . . . . . . . . . . . . . . . . . 3 3.2.3 Time and space complexity . . . . . . . . . . . . . . . . . 7 3.2.4 ...
متن کاملA Critical Evaluation on Programming Paradigms to Achieve Optimal Resource Utilization of Mobile Softwares in Mobile Devices
This paper evaluates the features of mainstream programming paradigms. Imperative, object oriented programming and functional programming concepts are considered here. This is an effort to identify the programming paradigms which consume less resource from mobile devices. Designers usually depend on the programming languages, language oriented programming design is in current practice. Choosing...
متن کاملذخیره در منابع من
با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید
برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید
ثبت ناماگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید
ورودعنوان ژورنال:
- J. Funct. Program.
دوره 2 شماره
صفحات -
تاریخ انتشار 1992