Abstract
Delimited continuations are the mother of all monads! So goes the slogan inspired by Filinski’s 1994 paper, which showed that delimited continuations can implement any monadic effect, letting the programmer use an effect as easily as if it was built into the language. It’s a shame that not many languages have delimited continuations.
Luckily, exceptions and state are also the mother of all monads! In this Pearl, we show how to implement delimited continuations in terms of exceptions and state, a construction we call thermometer continuations. While traditional implementations of delimited continuations require some way of ”capturing” an intermediate state of the computation, the insight of thermometer continuations is to reach this intermediate state by replaying the entire computation from the start, guiding it using a recording so that the same thing happens until the captured point.
Along the way, we explain delimited continuations and monadic reflection, show how the Filinski construction lets thermometer continuations express any monadic effect, share an elegant special-case for nondeterminism, and discuss why our construction is not prevented by theoretical results that exceptions and state cannot macro-express continuations.
Supplemental Material
- Kenichi Asai and Oleg Kiselyov. 2011. Introduction to Programming with Shift and Reset. (2011). http://pllab.is.ocha.ac.jp/ ~asai/cw2011tutorial/main- e.pdfGoogle Scholar
- Alberto Gomez Corona. 2014. MFlow, a Continuation-Based Web Framework Without Continuations. (2014). https: //themonadreader.files.wordpress.com/2014/04/mflow.pdfGoogle Scholar
- Dan Piponi. 2008. The Mother of all Monads. http://blog.sigfpe.com/2008/12/mother- of- all- monads.html . (2008). Posted: 2008-12-24. Accessed: 2017-02-27.Google Scholar
- David Darais, Nicholas Labich, Phúc C Nguyen, and David Van Horn. 2017. Abstracting Definitional Interpreters (Functional Pearl). Proceedings of the ACM on Programming Languages 1, ICFP (2017), 12. Google Scholar
Digital Library
- R Kent Dyvbig, Simon Peyton Jones, and Amr Sabry. 2007. A Monadic Framework for Delimited Continuations. Journal of Functional Programming 17, 6 (2007), 687–730. Google Scholar
Digital Library
- Mattias Felleisen. 1988. The Theory and Practice of First-Class Prompts. In Proceedings of the 15th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages. ACM, 180–190. Google Scholar
Digital Library
- Matthias Felleisen. 1990. On the Expressive Power of Programming Languages. (1990), 134–151. Google Scholar
Digital Library
- Andrzej Filinski. 1994. Representing Monads. In Proceedings of the 21st ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’94). ACM, New York, NY, USA, 446–457. Google Scholar
Digital Library
- Andrzej Filinski. 1999. Representing Layered Monads. In Proceedings of the 26th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages. ACM, 175–188. Google Scholar
Digital Library
- Michael Hanus, Herbert Kuchen, and Juan Jose Moreno-Navarro. 1995. Curry: A Truly Functional Logic Language. In Proc. ILPS, Vol. 95. 95–107.Google Scholar
- Ralf Hinze. 2012. Kan Extensions for Program Optimisation or: Art and Dan Explain an Old Trick. In International Conference on Mathematics of Program Construction. Springer, 324–362. Google Scholar
Digital Library
- Oleg Kiselyov. 2010. Delimited Control in OCaml, Abstractly and Concretely: System Description. In International Symposium on Functional and Logic Programming. Springer, 304–320. Google Scholar
Digital Library
- Oleg Kiselyov and KC Sivaramakrishnan. 2017. Eff directly in OCaml. In Post-proceedings of the ML workshop 2016. http://kcsrk.info/papers/caml- eff17.pdfGoogle Scholar
- James Koppel, Gabriel Scherer, and Armando Solar-Lezama. 2017. Capturing the Future by Replaying the Past. CoRR abs/1710.10385 (2017). arXiv: 1710.10385 http://arxiv.org/abs/1710.10385Google Scholar
- Mark Lillibridge. 1999. Unchecked Exceptions Can Be Strictly More Powerful Than Call/CC. Higher-Order and Symbolic Computation 12, 1 (1999), 75–104. Google Scholar
Digital Library
- Greg Pettyjohn, John Clements, Joe Marshall, Shriram Krishnamurthi, and Matthias Felleisen. 2005. Continuations from Generalized Stack Inspection. In Proceedings of the Tenth ACM SIGPLAN International Conference on Functional Programming (ICFP ’05). ACM, New York, NY, USA, 216–227. Google Scholar
Digital Library
- Hayo Thielecke. 2001. Contrasting Exceptions and Continuations. Version available from http://www. cs. bham. ac. uk/hxt/research/exncontjournal. pdf (2001).Google Scholar
- Peter Thiemann. 2006. WASH Server Pages. In Functional and Logic Programming, 8th International Symposium, FLOPS 2006, Fuji-Susono, Japan, April 24-26, 2006, Proceedings. 277–293. Google Scholar
Digital Library
- TIOBE Software BV. 2017. TIOBE Index for February 2017. http://www.tiobe.com/tiobe- index/ . (2017). Posted: 2017-02-08. Accessed: 2017-02-22.Google Scholar
Index Terms
Capturing the future by replaying the past (functional pearl)
Recommendations
Heuristics Entwined with Handlers Combined: From Functional Specification to Logic Programming Implementation
PPDP '14: Proceedings of the 16th International Symposium on Principles and Practice of Declarative ProgrammingA long-standing problem in logic programming is how to cleanly separate logic and control. While solutions exist, they fall short in one of two ways: some are too intrusive, because they require significant changes to Prolog's underlying implementation; ...
Delimited dynamic binding
ICFP '06: Proceedings of the eleventh ACM SIGPLAN international conference on Functional programmingDynamic binding and delimited control are useful together in many settings, including Web applications, database cursors, and mobile code. We examine this pair of language features to show that the semantics of their interaction is ill-defined yet not ...
Modular Compilers Based on Monad Transformers
ICCL '98: Proceedings of the 1998 International Conference on Computer LanguagesThe monadic style of language specification has the advantages of modularity and extensibility: it is simple to add or change features in an interpreter to reflect modifications in the source language. It has proven difficult to extend the method to ...






Comments