Continuations from Generalized Stack Inspection∗

Continuations from Generalized Stack Inspection∗

Continuations from Generalized Stack Inspection ∗ Greg Pettyjohn John Clements Joe Marshall Northeastern University Northeastern University Northeastern University Shriram Krishnamurthi Matthias Felleisen Brown University Northeastern University Abstract agent) who is in charge. This user may decide to respond to the Implementing first-class continuations can pose a challenge if the query once, twice or many times (or not at all), thus re-launching target machine makes no provisions for accessing and re-installing the rest of the program’s computation once, twice, or many times. the run-time stack. In this paper, we present a novel translation Many people have therefore concluded that a language with first- that overcomes this problem. In the first half of the paper, we class continuations (such as Scheme [19]) is a strong match for introduce a theoretical model that shows how to eliminate the implementing this kind of interactive program [11, 18, 26, 33]. capture and the use of first-class continuations in the presence Over the past several years, we have explored the continuation of a generalized stack inspection mechanism. The second half of model of Web programming in two different directions. Initially, the paper explains how to translate this model into practice in we designed, implemented, and evaluated a Web server that imple- two different contexts. First, we reformulate the servlet interaction ments the run-time primitives for Web interactions via Scheme’s language in the PLT Web server, which heavily relies on first-class first-class continuations [14]. The investigation validated that this continuations. Using our technique, servlet programs can be run approach is suitable for a variety of situations and even led to a directly under the control of non-cooperative web servers such as commercial product [20]. Sadly, only programs running on a cus- Apache. Second, we show how to use our new technique to copy tom Web server can benefit from this approach. and reconstitute the stack on MSIL.Net using exception handlers. To address this concern, we also experimented with a variant This establishes that Scheme’s first-class continuations can exist on of the continuation-passing transformation (CPS) for automatically non-cooperative virtual machines. restructuring interactive programs for the Web [13, 21]. In prin- ciple, this transformation can be used with a wide range of pro- Categories and Subject Descriptors F.3.3 [Logics and Mean- gramming languages and should therefore help Web programmers ings of Programs]: Studies of Program Constructs—Control prim- in many situations. The problem, however, is that the transforma- itives; H.3.4 [Information Storage and Retrieval]: Systems and tion affects the entire program. Since modern Web applications Software—World Wide Web (WWW); I.2.2 [Artificial Intelli- are often written in multiple languages, it is nearly impossible to gence]: Automatic Programming—Program transformation; F.3.2 perform a whole-program transformation of them. Worse, CPS re- [Logics and Meanings of Programs]: Semantics of Programming quires tail-call optimization or, in its absence, trampolines, a tech- Languages—Operational semantics nique due to Jon L. White [personal communication June 2005], which can be much more expensive. It is therefore economically General Terms Languages, Theory impossible to use this idea with existing languages and run-time Keywords A-normal form, continuation-passing style, stack in- libraries. In short, we are left with the challenge of equipping con- spection, Web programming, continuations, defunctionalization, ventional programming languages with the capabilities of grabbing Scheme and reinstalling a continuation even if these languages’ run-time organizations do not support such actions. 1. Motivation: Continuations, VMs, and the Web The very same problem comes up in a different context: the implementation of languages with continuations on virtual ma- When an interactive Web program issues a query, a non-local trans- chines such as Sun’s JVM [34] or Microsoft’s CLR [22]. Mirror- fer of control takes place. It is now the user (possibly an intelligent ing traditional programming languages, these machines do not pro- ∗ vide instructions for installing and saving the run-time stack. Usu- This research has been supported by NSF awards to Felleisen and Krish- ally, Scheme-on-VM implementors give up on first-class continu- namurthi as well as a Microsoft donation to Felleisen. ations [1, 2], or they allocate the control stack in the heap of the machine [23]. As Bres, et al. [2, section 2] point out, however, with this second strategy, “it might be expected that . JITs are far less efficient on codes that manages their own stack” [sic]. Furthermore, Permission to make digital or hard copies of all or part of this work for personal or allocating the stack on the heap effectively disguises the stack, hid- classroom use is granted without fee provided that copies are not made or distributed ing it from the rich set of programming tools such as steppers, de- for profit or commercial advantage and that copies bear this notice and the full citation on the first page. To copy otherwise, to republish, to post on servers or to redistribute buggers, and profilers, as well as contemporary security managers, to lists, requires prior specific permission and/or a fee. which expect to find run-time information on the stack. ICFP ’05 September 26-28, 2005, Tallinn, Estonia In this paper, we present a solution to this dilemma, first in the Copyright c 2005 ACM 1-59593-064-7/05/0009. $5.00. context of a theoretical model and then in the context of two pro- (define (fact n) (if (= n 0) e ::= a (begin | (we) (display (c-c-m)) | (letrec ([σ v]) e) 1) (w-c-m n (∗ n (fact (− n 1)))))) | (call/cc w) (fact 3) | (case w l) 7→ l ::= (K x) ⇒ e console output: (1 2 3) a ::= w | (K a) computed value: 6 w ::= v | x v ::= (λ(x) e) | (K v) | κ.E | σ Figure 1. A factorial function with continuation marks x ∈ Variables σ ∈ References (define (fact-tr n a) (if (= n 0) where Variables ∩ References = ∅ (begin (display (c-c-m)) E ::= [] | (v E) a) Σ ::= ∅ | Σ[σ 7→ v] (w-c-m n (fact-tr (− n 1)(∗ n a))))) (fact-tr 3 1) (X denotes zero or more occurrences of X) 7→ console output: (1) Figure 3. SL Syntax computed value: 6 Figure 2. A tail-calling factorial function with continuation marks list before returning. The one in figure 1 is properly recursive, while the one in figure 2 is tail-recursive. For the properly recursive pro- gram, the console output shows that the continuation contains three totype implementations. The core idea is to translate Scheme pro- mark frames. For the tail-recursive variant, only one continuation grams with call/cc into a language with a generalized stack inspec- mark remains; the others have been overwritten during evaluation. tion mechanism. For our theoretical model and for one of our pro- totypes, we use PLT Scheme’s continuation mark mechanism; for 3. Continuations from Continuation Marks the other prototype, we show how exception handlers and exception throws can collaborate to simulate continuation marks. The result- Equipped with a basic understanding of MzScheme’s continuation ing transformation is less radical than CPS, and it is thus natural to marks, we can now show how to use this generalized stack inspec- run the resulting program on the natively available stack. tion mechanism to eliminate call/cc from Scheme programs. We present the idea in two steps. The first step is to translate programs with call/cc into semantically equivalent programs that use contin- 2. Continuation Marks uation marks to store functional representations of continuations. Most programming languages and programming environments in- The second step replaces the marks with structures, using a variant clude mechanisms for manipulating the stack in some form or an- of Reynolds’ defunctionalization [27]. other. Java, for example, implements a form of security via stack inspection [35]. Programming environments such as Visual Studio 3.1 The Source Language have privileged access to the stack for various debugging tasks. The The language in figure 3, dubbed SL for source language, is a most ubiquitous examples of such mechanisms, though, are excep- modified version of A-Normal form (ANF) [8]. It uses λ instead tion signalers and handlers. They erase portions of the control stack of let. Furthermore, we allow applications of arbitrary length. The and transfer control to exception handlers in a non-local manner. language is extended with call/cc, letrec and algebraic datatypes. MzScheme [9], the implementation language of the DrScheme The latter are needed in the target language for the operational programming environment [7], provides a novel abstraction of all semantics and are used during defunctionalization, the last step of these mechanisms: continuation marks [4]. Roughly speaking, con- our translation. For consistency they are also included in the source tinuation marks support a powerful form of stack inspection gen- language. eralizing Java’s mechanism with the same name. Using the w-c-m Instances of algebraic datatypes are constructed with construc- language form (short for “with continuation marks”), a program- tors (K, Km) and destructured with case. We leave the actual set of mer can attach values to the control stack during the execution of constructors unspecified, though we assume it contains the standard a program. Later, the stack-walking primitive c-c-m (“current con- list constructors cons and nil. For convenience, we use a shorthand tinuation marks”) can retrieve these values from the stack. for lists, where (list e0 e1 . .) stands for the aggregate construction To preserve the spirit of Scheme, attaching continuation marks (cons e0 (list e1 . .)) and (list) stands for the empty list. to the stack does not interfere with Scheme’s tail-calling require- The operational semantics is specified via the rewriting system ment.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us