What’s in a Continuation (2016)
It took a long time to grok continuations. There&#x27;s an easy way to explain what they are:<p>Imagine running a program in a VM. You know how you can take a snapshot and then restore to it later? That snapshot is equivalent to a continuation.<p>Another way of phrasing it is, it&#x27;s your program frozen in time. You can snapshot your program and restore to that point later.<p>To put it technically, step through each call stack frame, serialize all the local variables, and you have yourself a continuation. To invoke it, call those functions in order and set the local variables to those values, then set the program counter to wherever it was. (You don&#x27;t literally do this, but maybe that makes it easier to understand what&#x27;s going on with it.)<p>The confusion: What about a database connection? Or a network connection of any kind? An open file handle? Etc. The answer is that those things can&#x27;t be saved in a continuation.<p>The way that this works in Scheme is that there&#x27;s a special primitive called &quot;dynamic wind&quot;. It takes three callback functions: &quot;before&quot;, &quot;during&quot;, and &quot;after&quot;. It invokes those callback functions in order. If execution leaves &quot;during&quot; for any reason whatsoever, then &quot;after&quot; is invoked.<p>Here&#x27;s the kicker: If execution <i>goes back into &quot;during&quot;</i>, then &quot;before&quot; is invoked. I.e. if you save a continuation inside &quot;during,&quot; then &quot;before&quot; is the place that you&#x27;d put the code to re-initiate a database connection or re-open a file handle. Or fail.<p>And of course, no discussion of continuations would be complete without the argument for why call&#x2F;cc is generally an anti-pattern: <a href="http:&#x2F;&#x2F;okmij.org&#x2F;ftp&#x2F;continuations&#x2F;against-callcc.html" rel="nofollow">http:&#x2F;&#x2F;okmij.org&#x2F;ftp&#x2F;continuations&#x2F;against-callcc.html</a><p>Yet &quot;On Lisp&quot; presents several interesting ways to use them, and they are extremely powerful. One particular use is that you can implement cut, fail, and mark for non-deterministic backtracking. If you&#x27;ve used emacs lisp and you&#x27;ve ever written an edebug specification for how to debug a macro (<a href="https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;emacs&#x2F;manual&#x2F;html_node&#x2F;elisp&#x2F;Specification-List.html#Specification-List" rel="nofollow">https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;emacs&#x2F;manual&#x2F;html_node&#x2F;elisp&#x2F;Sp...</a>), some of the more complex features require backtracking: <a href="https:&#x2F;&#x2F;github.com&#x2F;emacs-mirror&#x2F;emacs&#x2F;blob&#x2F;0648edf3e05e224ee8410ab244df7364f919dc58&#x2F;lisp&#x2F;emacs-lisp&#x2F;cl-macs.el#L2578-L2595" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;emacs-mirror&#x2F;emacs&#x2F;blob&#x2F;0648edf3e05e224ee...</a><p>That&#x27;s an area where continuations really shine, because the implementation can be just a few dozen lines compared to hundreds.<p>(elisp doesn&#x27;t actually use continuations -- this is just an example of the territory they&#x27;re useful in.)