Subtyping Delimited Continuations

Subtyping Delimited Continuations

Subtyping Delimited Continuations Marek Materzok Dariusz Biernacki Institute of Computer Science Institute of Computer Science University of Wrocław University of Wrocław Wrocław, Poland Wrocław, Poland [email protected] [email protected] Abstract tant role among the most fundamental concepts in the landscape We present a type system with subtyping for first-class delimited of functional programming—a language equipped with shift and continuations that generalizes Danvy and Filinski’s type system for reset is monadically complete, i.e., any computational monad can shift and reset by maintaining explicit information about the types be represented in direct style with the aid of these control op- of contexts in the metacontext. We exploit this generalization by erators [18, 19]. A long list of non-trivial applications of delim- ited continuations includes normalization by evaluation and partial considering the control operators known as shift0 and reset0 that can access arbitrary contexts in the metacontext. We use subtyping evaluation [3, 12, 15], mobile computing [33], linguistics [4], and to control the level of information about the metacontext the ex- operating systems [25] to name but a few. pression actually requires and in particular to coerce pure expres- The intuitive semantics of delimited-control operators such as sions into effectful ones. For this type system we prove strong type shift (S) and reset (hi) can be explained as follows. When the ex- soundness and termination of evaluation and we present a provably pression Sk:e is evaluated, the current—delimited by the nearest correct type reconstruction algorithm. We also introduce two CPS dynamically-enclosing reset—continuation is captured, i.e., it is re- moved and the variable k is bound to it. When later k is applied to a translations for shift0 and reset0: one targeting the untyped lambda calculus, and another—type-directed—targeting the simply-typed value, the then-current continuation is composed with the captured lambda calculus. The latter translation preserves typability and is continuation. For example, the following expression (where ++ is selective in that it keeps pure expressions in direct style. string concatenation): “Alice” ++ h“ has ” ++ Categories and Subject Descriptors D.3.3 [Programming Lan- (Sk:(k “a dog ”) ++ “and the dog” ++ (k “a cat.”))i guages]: Language Constructs and Features—Control structures; F.3.3 [Logics and Meanings of Programs]: Studies of Program Con- evaluates to the string “Alice has a dog and the dog has a cat.” structs In their first article on delimited continuations [13], Danvy and Filinski briefly described a variant of shift and reset, known as General Terms Languages, Theory shift0 (S0) and reset0, where the operational semantics of the con- Keywords Delimited Continuation, Continuation-Passing Style, trol delimiter reset0 coincides with that of reset, but shift0, when Type System, Subtyping capturing a continuation, instead of resetting it, replaces it with the inner-most delimited continuation pending on the metacontin- 1. Introduction uation. In other words, in contrast to shift, shift0 removes the con- trol delimiter along with the captured continuation, which makes it Control operators for first-class delimited continuations, introduced possible to use shift0 to access continuations arbitrarily deep in the independently by Felleisen [17] and by Danvy and Filinski [13, 14], metacontinuation by repeatedly shifting continuations. For exam- serve to abstract control in functional programming languages by ple, the following expression: reifying the current continuation as a first-class value. However, in contrast to the well-known abortive control operator call/cc h“Alice” ++ h“ has ” ++ (S0k1:S0k2:“A cat” ++ (k1(k2 “.”))ii present in Scheme [23] and SML/NJ [20], delimited-control oper- evaluates to the string “A cat has Alice.”, since the second shifting ators model composition of delimited (partial) continuations rather has access to the continuation that prepends the string “Alice” than jumps to undelimited continuations. In particular, while call/cc to a given string. The ability to access arbitrary contexts (a first- offers the programmer the power of the continuation-passing style order representation of continuations [9]) in the metacontext (a (CPS) in direct style, Danvy and Filinski’s shift and reset offer first order representation of the metacontinuation [9])) makes S0 the power of the continuation-composing style (CCS) characteris- a particularly interesting and powerful control operator. tic of the success-failure continuation model of backtracking [14]. In the untyped setting, shift and reset have been investigated It has been shown by Filinski that shift and reset play an impor- 0 0 by Shan [32], who presented a CPS translation for shift0 and reset0 that conservatively extends Plotkin’s call-by-value CPS transla- tion [28] and that leads to a simulation of shift0 and reset0 in terms of shift and reset. This CPS translation and simulation hinge on Permission to make digital or hard copies of all or part of this work for personal or the requirement that the continuations be represented by recursive classroom use is granted without fee provided that copies are not made or distributed functions taking a list of continuations as one of their arguments. 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 (The converse simulation is trivial—one simply resets the body of to lists, requires prior specific permission and/or a fee. each shift0 expression.) ICFP’11, September 19–21, 2011, Tokyo, Japan. In a typed setting, Kiselyov and Shan [26] introduced a sub- Copyright c 2011 ACM 978-1-4503-0865-6/11/09. $10.00 structural type system for shift0 and reset0 that abstractly interprets (in the sense of abstract interpretation of Cousot and Cousot [11]) added to the language. Unfortunately, in this type system, functions small-step reduction semantics of the language. Their type system without control effects (when C and D are the same arbitrary type allows for continuation answer type modifications and, in a sense, in the rule for λ-abstraction) still have an answer type chosen up- extends Danvy and Filinski’s type system which is the most ex- front and are subject to the same restrictions as described above. pressive of the known monomorphic type systems for shift and re- In a way, one can treat the continuation types from [6] as more set. Judgments in Danvy and Filinski’s type system have the form general than function types—continuations can be applied in any Γ; B ` e : A; C, with the interpretation that expression e can context, whereas functions can only be applied in contexts with be plugged into a context expecting a value of type A and produc- matching answer types. The type system of this article removes ing a value of type B, and a metacontext expecting a value of type the syntactic distinction between continuations and functions by C, where B and C can be different types. Hence, expressions are the means of subtyping of effect annotations. The subtyping rela- allowed to change the answer type of the context in which they tion allows functions to be called when more is known about the are embedded. For example, the expression h1 + Sk: truei is well types of the contexts in the metacontext than the function actually typed in this type system. The typing rules of Danvy and Filinski’s requires. In particular, a pure function, possibly representing a cap- type system have been derived from a left-to-right call-by-value tured continuation, can be arbitrarily coerced into an effectful one. evaluator in continuation-passing style [13]. The rest of this article is structured as follows. In Section 2.1, In this article we present a new type-and-effect system for we introduce the syntax and reduction semantics for the call-by- shift0 and reset0 that generalizes Danvy and Filinski’s original type value λ-calculus with shift0 and reset0. In Section 2.2, we present system for shift and reset. To this end, we first introduce a new a CPS translation for the language and we relate it to the reduc- CPS translation for shift0 and reset0 which conservatively extends tion semantics. In Section 2.3, we derive a type-and-effect system Plotkin’s call-by-value CPS translation and which turns out to be a` la Danvy and Filinski from the CPS. In Sections 3.1 and 3.2, we close to a curried version of Shan’s CPS translation [32]. From introduce subtyping to the type system and we prove several stan- this CPS translation, we derive an abstract machine and a type- dard properties for the system, including strong type soundness. In and-effect system a` la Danvy and Filinski which we refine by al- Section 3.3, we use a context-based method of reducibility pred- lowing for subtyping of types and effect annotations. We show that icates to prove termination of evaluation of well-typed programs. the type system with subtyping we present satisfies several cru- In Section 3.4, we present and prove correct a type inference al- cial properties such as strong type soundness and termination of gorithm for our type system. In Section 3.5, we present a selective evaluation, and we describe and prove correct a type inference al- type-directed CPS translation for the language. In Section 4, we gorithm for this type system. Compared to Kiselyov and Shan’s consider some practical applications of the presented language. In type system [26], the one presented here is more conventional, it Section 5, we show how the type system for shift0 and reset0 can corresponds directly to the CPS translation it has been obtained be restricted to Danvy and Filinski’s type system for shift and reset, from, and it heavily relies on subtyping built in the system.

View Full Text

Details

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