The Theory and Practice of First-Class Prompts

The Theory and Practice of First-Class Prompts

The Theory and Practice of First-Class Prompts Mat t hias Felleisen Indiana University Computer Science Department Lindley Hall 101 Bloomington, IN 47405 Abstract. An analysis of the X,-C-calculus and in the calculus do not necessarily behave equivalently its problematic relationship to operational equiva- in all program contexts. The problem can be fixed lence leads to a new control facility: the promp& with a meta-theorem that distinguishes theorems in application. With the introduction of prompt- the theory as operational equalities, yet, this is not a applications, the control calculus becomes a tradi- desirable situation. tional calculus all of whose equations imply opera- In this paper, we present a different and more tional equivalence. In addition, prompt-applications promising solution. It is baaed on a new linguistic enhance the expressiveness and efficiency of the lan- facility for constraining the extent of control opera- guage. We illustrate the latter claim with examples tions: the prompt-application. The introduction of from such distinct areas as systems programming and prompt-applications transforms the control calculus tree processing. into a truly traditional calculus, an.d, in addition, prompt-applications are a practical tool for a broad variety of programming paradigms. We illustrate the latter claim with examples from such distinct areas 1 A Problem of the X,-C- as systems programming and tree processing. In the next section, we formalize the original X,- calculus C-calculus and the concept of a first-class prompt facility. We then show that the extended calculus The X,-C-calculus [2, 43 is a conservative extension is consistent and that there is a standard reduction of Plotkin’s &-calculus [ll] for reasoning about im- function. Based on this, we define an operational perative control operations in call-by-value languages. semantics and show that theorems in the calculus di- The theory satisfies modified versions of the Church- rectly imply operational equivalence. The third sec- Rosser and the Curry-Feys Standardization Theorem. tion contains an abstract stack-machine for the ex- Furthermore, the standardization procedure yields tended language and a brief discussion of the im- the same answers for programs as the evaluation func- plementation cost. Section 4 is a collection of pro- tion of an ap:propriate abstract machine. However, gramming examples, which illustrates the expressive the calculus also has a major deficiency: equality in power of prompt-applications. The fifth section is the calculus does no2 imply operational equality on an overview of related work. In the conclusion, we the machine. That is, two expressions that are equal analyze the contribution of language calculi to the “This work was partly supported by an IBM Fellowship and systematic analysis of programming :languages. Dan FEedman’s NSF grant (DCR 85 01277). Author’s cursent &dress: Rice University, Herrman Brown Hall, Houston, TX 77251-1892 2 The modified X,-C-calculus The term set A7 of the original X,-C-calculus is de- fined inductively over a set of algebraic constants, Con& and a set of variables, Vars: Permission to copy without fee all or part of this material is granted L ::= a 12 1 Xt.M 1 MN 1 FM, provided that the copies are not made or distributed for direct commercial advantage, the. ACM copyright notice and the title of the publication and its date appear, and notice is given that copying where a ranges over constants, xover Vars, and L, M, is by permission of the Association for Computing Machinery. To and Nover &-terms. The first four classes of expres- copy otherwise, or to republish, requires a fee and/or specfic permission. Proceedings of the Fifteenth Annual ACM SIGACT-SIGPLAN Symposium on Princi- @ 1988 ACM-O-89791-252-7/88/0001/0180 $1.50 180 sions have their original semantics: constants stand 3Jr: V(3’M) - 3(Ak.M(Am.k(Vm))) for basic and functional constants from some kind provided V is a value. of algebraic domain, variables are placeholders, A- abstractions represent call-by-value procedures, and combinations denote function applications. The purpose of these reductions is to push an 3-app- As usual, the variable 3: in the abstraction Xt.M is lication to the root of a term and to encode the con- called the bound variable; a variable that is not bound text of the 3-application as an abstraction. Once the by some X is free. An expression with no free variables 3-application has reached the root, the computation is called closed. Programs in this language are closed relation expressions. Like Barendregt [1:26], we identify terms 3’M D kf(h.z) that are identical modulo bound variables, and we assume that free and bound variables cannot interfere eliminates the 3-application by applying the 3- with each other. The substituion of a free variable 2: argument to the identity function, a representation by N in a term M is denoted by M[z := N]. of the empty context. The computation rule is de- The new expression type is called 3-application,’ noted with D instead of the customary - because it its subterm is an 3-argument. An 3-application can only be applied to the root: an unrestricted use transforms the current control state into a functional would make the calculus inconsistent. abstr&ion, which we call continuation.2 Next, the The nature of 3-applications and continuations is 3-application applies its argument to this continua- best illustrated with examples. Consider the program tion, thus providing the 3-argument with total power (1+(3(Xd.O))). It evaluates to 0 after the 3-applica- over the rest of the computation. In particular, if the tion erases the call to l+: corresponding actions are to be performed at all, the 3-argument must invoke this continuation. (~+vww) - T(M.(Ad.O)(Av.lc(l+v))) The equational theory is specified with a set of term D (~k.((~~.o)(h.k(1+~))))(xz.+) relations for the evaluation of expressions. Constants, - (Mo)(xV.(xz!.~)(l+?J)) variables, and X-abstractions require no further evalu- ation and are appropriately referred to as values. For - 0. the evaluation of an application of a function con- stant to a basic constant, we assume the existence of an interpretation 6 on the set of constants: Put more abstractly, 3(Ad.V) where d does not occur in the value V behaves like an abort operation that S: FuncConst x BasicConst -w Closed-A- Values. returns V. The conventional goto is a straightforward generalization of abort. If we replace the value V The b-reduction for constant applications is: with an arbitrary expression M, the term S(Xd.M) 6: fa - b(f, a). first eliminates the current state and then installs M as the new one. The evaluation of an application of a X-abstraction to When a continuation is applied, it acts like an or- a value is determined by the ,&value relation: dinary function. It performs the encoded rest of the computation and, unless an 3-application intervenes, p: (A;c.M)V - M[z: := V] provided V is a value. returns to the point of invocation: That is, an application whose first part is an abstrac- (1+(3(Xk.(k(kO))))) tion and whose second part is a value is reduced to - (3(X~.(~~.(@O)))(Xz.E(1+2))) a term that is like the function body with all oc- currences of the abstracted variable replaced by the D (Ak.((Ak.(k(kO)))(Xz.k(l+x))))(Az.z) value. - ((~~.(k(rco)))(~zc.(xz.z)(l+~))) The meaning of 3-applications is determined by - ((~z!.(~z.~)(l+z))((xz.(~z.z)(l+2?))o)) two reductions and a special computation relation. - ((~zY(~~.~)(1+~))((xz.z!)(1+0))) The reductions are: - ((xz.(~z.~)(1+2?))1) 3L: (3M)N - 3(Ak.A!r(Am.k(nzlv))), - (xz.z)(l+l) ‘3- is a generalized version of Iswim’s J [s], Reynolds’s es- - (>2.2)2 cape [13], and Scheme’s [12] call/cc. -3 2. 2WARNING: This usage is inconsistent with Scheme- , terminology. 181 The problem of this calculus is the separation of With this modification, the X,-C-calculus becomes computation and reduction relations. Since compu- a calculus in the traditional sense: tation relations can only be applied at the root of Definition. (The new &-C-calculus) The basic no- a term, an equation that is based on the compula- tion of reduction c collects all of the above relations: lion relation does not imply operational equivalence. More precisely, even if two terms are computation- ally equivalent, there are generally program contexts The one-step c-reduction, -c, is the compatible clo- that can distinguish the two expressions. A typical sure of c: example is the pair of terms 3(Ad.O) and 0. Al- though equivalent according to the computation rela- (M,N)- d M-,N tion, they behave differently in practically all con- M-,N + Xz.M-Jx.N texts. Whereas the former aborts a computation when evaluated, the second one simply evaluates to 0. M-,N + 3M-,3N A possible and obvious fix is the introduction of M-,N =+ #M-,#N a unique top-level marker # for identifying the root M-,N + LM---+,LN, MLA--+,NL; of a program. Then the computation relation can be replaced by a reduction relation: -+e stands for the transitive-reflexive closure of the #3: (# (3M)) - (# (M(Xx.x))). one-step c-reduction. However, this solution pushes the semantic division The c-congruence is defined as: into the language syntax and does not eliminate the complications of its predecessor: equations for #- M =:c M contained terms cannot predict the behavior of these M-,N =S M=:, N terms in arbitratry contexts.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 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