Delimited control in OCaml, abstractly and concretely

We describe the first implementation of multi-prompt delimited control operators in OCaml that is direct in that it captures only the needed part of the control stack. The implementation is a library that requires no changes to the OCaml compiler or run-time, so it is perfectly compatible with existing OCaml source and binary code. The library has been in fruitful practical use since 2006. We present the library as an implementation of an abstract machine derived by elaborating the definitional machine. The abstract view lets us distill a minimalistic API, scAPI, sufficient for implementing multi-prompt delimited control. We argue that a language system that supports exception and stack-overflow handling supports scAPI. With byte- and native-code OCaml systems as two examples, our library illustrates how to use scAPI to implement multi-prompt delimited control in a typed language. The approach is general and has been used to add multi-prompt delimited control to other existing language systems.

[1]  Eijiro Sumii,et al.  An Implementation of Transparent Migration on Standard Scheme , 2002 .

[2]  Akinori Yonezawa,et al.  Portable Implementation of Continuation Operators in Imperative Languages by Exception Handling , 2000, Advances in Exception Handling Techniques.

[3]  Chung-chieh Shan,et al.  Monolingual Probabilistic Programming Using Generalized Coroutines , 2009, UAI.

[4]  Neal Glew Type dispatch for named hierarchical types , 1999, ICFP '99.

[5]  Simon L. Peyton Jones,et al.  A monadic framework for delimited continuations , 2007, J. Funct. Program..

[6]  O. Kiselyov,et al.  Lifted inference : normalizing loops by evaluation , 2009 .

[7]  Joe Marshall,et al.  Continuations from generalized stack inspection , 2005, ICFP '05.

[8]  Carl A. Gunter Return Types for Functional Continuations , 1998 .

[9]  Christophe Deleuze,et al.  Light weight concurrency in OCaml: continuations, monads, events, and friends , 2010 .

[10]  Olivier Danvy,et al.  Abstracting control , 1990, LISP and Functional Programming.

[11]  Chung-chieh Shan,et al.  Embedded Probabilistic Programming , 2009, DSL.

[12]  Robert Hieb,et al.  Representing control in the presence of first-class continuations , 1990, PLDI '90.

[13]  Roberto Di Cosmo,et al.  Extensional normalisation and type-directed partial evaluation for typed lambda calculus with sums , 2004, POPL.

[14]  Gang Yu,et al.  Adding delimited and composable control to a production programming environment , 2007, ICFP '07.

[15]  Oleg Kiselyov Delimited Control in OCaml, Abstractly and Concretely: System Description , 2010, FLOPS.

[16]  R. Kent Dybvig,et al.  Threads Yield Continuations , 1998, LISP Symb. Comput..

[17]  Xavier Leroy,et al.  The ZINC experiment : an economical implementation of the ML language , 1990 .

[18]  Didier Rémy,et al.  A generalization of exceptions and control in ML-like languages , 1995, FPCA '95.

[19]  Martin Odersky,et al.  Implementing first-class polymorphic delimited continuations by a type-directed selective CPS-transform , 2009, ICFP.

[20]  Matthias Felleisen,et al.  The theory and practice of first-class prompts , 1988, POPL '88.

[21]  Andrzej Filinski,et al.  Representing monads , 1994, POPL '94.

[22]  William D. Clinger,et al.  Implementation Strategies for First-Class Continuations , 1999, High. Order Symb. Comput..

[23]  Peter Thiemann,et al.  Towards Deriving Type Systems and Implementations for Coroutines , 2010, APLAS.

[24]  Amr Sabry,et al.  Delimited dynamic binding , 2006, ICFP '06.

[25]  Chung-chieh Shan,et al.  Shifting the stage - Staging with delimited control , 2011, J. Funct. Program..

[26]  Michael Sperber,et al.  Final shift for call/cc:: direct implementation of shift and reset , 2002, ICFP '02.

[27]  Kenichi Asai,et al.  Direct implementation of shift and reset in the MinCaml compiler , 2009, ML '09.