From 7d4bf2224fa21ee8a68ad524a61ca55338ee341e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Hillerstr=C3=B6m?= Date: Wed, 2 Dec 2020 22:26:40 +0000 Subject: [PATCH] Implementing continuations --- thesis.bib | 45 ++++++++++++++++++++++++++-- thesis.tex | 87 +++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 110 insertions(+), 22 deletions(-) diff --git a/thesis.bib b/thesis.bib index 23c1443..75fce14 100644 --- a/thesis.bib +++ b/thesis.bib @@ -74,6 +74,18 @@ year = {1996} } +@article{KumarBD98, + author = {Sanjeev Kumar and + Carl Bruggeman and + R. Kent Dybvig}, + title = {Threads Yield Continuations}, + journal = {{LISP} Symb. Comput.}, + volume = {10}, + number = {3}, + pages = {223--236}, + year = {1998} +} + # Programming with continuations @InProceedings{FriedmanHK84, author = {Daniel P. Friedman @@ -912,8 +924,6 @@ year = {2000} } - - # CPS for effect handlers @inproceedings{HillerstromLAS17, author = {Daniel Hillerstr{\"{o}}m and @@ -953,6 +963,23 @@ year = {2020} } +# SECD machine +@article{Landin64, + author = {Peter J. Landin}, + title = {The Mechanical Evaluation of Expressions}, + journal = {The Computer Journal}, + volume = {6}, + number = {4}, + pages = {308-320}, + year = {1964}, + month = {01}, + issn = {0010-4620}, + OPTdoi = {10.1093/comjnl/6.4.308}, + OPTurl = {https://doi.org/10.1093/comjnl/6.4.308}, + OPTeprint = {https://academic.oup.com/comjnl/article-pdf/6/4/308/1067901/6-4-308.pdf}, +} + + # CEK & C @InProceedings{FelleisenF86, title={Control Operators, the {SECD}-machine, and the $\lambda$-Calculus}, @@ -2408,4 +2435,18 @@ pages = {241--252}, publisher = {{ACM}}, year = {1987} +} + +# Stack copying technique without direct access to the stack. +@inproceedings{PettyjohnCMKF05, + author = {Greg Pettyjohn and + John Clements and + Joe Marshall and + Shriram Krishnamurthi and + Matthias Felleisen}, + title = {Continuations from generalized stack inspection}, + booktitle = {{ICFP}}, + pages = {216--227}, + publisher = {{ACM}}, + year = {2005} } \ No newline at end of file diff --git a/thesis.tex b/thesis.tex index af1f1ad..deedc26 100644 --- a/thesis.tex +++ b/thesis.tex @@ -2254,7 +2254,7 @@ implementation strategies. \hline Effekt & Lexical effect handlers & ??\\ \hline - Frank & N-ary effect handlers & Abstract machine \\ + Frank & N-ary effect handlers & CEK machine \\ \hline Gauche & callcc, shift/reset & Virtual machine \\ \hline @@ -2272,8 +2272,8 @@ implementation strategies. \hline Racket & callcc, \textCallcomc{}, cupto, fcontrol, control/prompt, shift/reset, splitter, spawn & Segmented stacks\\ \hline - Rhino JavaScript & JI & Interpreter \\ - \hline + % Rhino JavaScript & JI & Interpreter \\ + % \hline Scala & shift/reset & CPS\\ \hline SML/NJ & callcc & CPS\\ @@ -2339,24 +2339,70 @@ slight variation of segmented stacks optimised for one-shot continuations, which has been adapted by Multicore OCaml~\cite{DolanEHMSW17}. -\dhil{TODO: abstract machines} - -Continuation passing style (CPS) is a particular idiomatic notation -for programs, where every aspect of control flow is made explicit, -which makes it a good fit for implementing control abstractions. +Full stack copying and segmented stacks both depend on being able to +manipulate the stack directly. This is seldom possible if the language +implementer do not have control over the target runtime, +e.g. compilation to JavaScript. However, it is possible to emulate +stack copying and segmented stacks in lieu of direct stack access. For +example, \citet{PettyjohnCMKF05} describe a technique that emulates +stack copying by piggybacking on the facile stack inception facility +provided by exception handlers in order to lazily reify the control +stack. % -Using trampolines CPS is even a viable strategy in environments that -lack proper tail calls such as JavaScript~\cite{GanzFW99}. - -% \paragraph{Continuation marks} - -% \paragraph{Continuation passing style} - -% \paragraph{Abstract and virtual machines} - -% \paragraph{Segmented stacks} - -% \paragraph{Stack copying} +\citet{KumarBD98} emulated segmented stacks via threads. Each thread +has its own local stack, and as such, a collection of threads +effectively models segmented stacks. To actually implement +continuations as threads \citeauthor{KumarBD98} also made use of +standard synchronisation primitives. +% +The advantage of these techniques is that they are generally +applicable and portable. The disadvantage is the performance overhead +induced by emulation. + +Abstract and virtual machines are a form of full machine emulation. An +abstract machine is an idealised machine. Abstract machines, such as +the CEK machine~\cite{FelleisenF86}, are attractive because they +provide a suitably high-level framework for defining language +semantics in terms of control string manipulations, whilst admitting a +direct implementation. +% +We will discuss abstract machines in more detail in +Chapter~\ref{ch:abstract-machine}. +% +The term virtual machine typically connotes an abstract machine that +works on a byte code representation of programs, whereas the default +connotation of abstract machine is a machine that works on a rich +abstract syntax tree representation of programs. +% \citeauthor{Landin64}'s SECD machine was the +% first abstract machine for evaluating $\lambda$-calculus +% terms~\cite{Landin64,Danvy04}. +% +% Either machine model has an explicit representation of the control +% state in terms of an environment and a control string. Thus either machine can to the +% interpretative overhead. +The disadvantage of abstract machines is their interpretative +overhead, although, techniques such as just-in-time compilation can be +utilised to reduce this overhead. + +Continuation passing style (CPS) is a canonical implementation +strategy for continuations --- the word `continuation' even feature in +its name. +% +CPS is a particular idiomatic notation for programs, where every +function takes an additional argument, the current continuation, as +input and every function call appears in tail position. Consequently, +every aspect of control flow is made explicit, which makes CPS a good +fit for implementing control abstraction. In classic CPS the +continuation argument is typically represented as a heap allocated +closure~\cite{Appel92}, however, as we shall see in +Chapter~\ref{ch:cps} richer representations of continuations are +possible. +% +At first thought it may seem that CPS will not work well in +environments that lack proper tail calls such as JavaScript. However, +the contrary is true, because the stackless nature of CPS means it can +readily be implemented with a trampoline~\cite{GanzFW99}. Alas, at the +cost of the indirection induced by the trampoline. \part{Design} @@ -8391,6 +8437,7 @@ then in the image $\cps{M}\,k \reducesto^\ast k\,\cps{V}$. \paragraph{Partial evaluation} \chapter{Abstract machine semantics} +\label{ch:abstract-machine} \dhil{The text is this chapter needs to be reworked} In this chapter we develop an abstract machine that supports deep and