<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Tom Moertel's Weblog: Tag scope</title>
    <link>http://blog.moertel.com/articles/tag/scope?tag=scope</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Quality rants on programming theory and stuff geeks like</description>
    <item>
      <title>Scope herding with delimited continuations</title>
      <description>&lt;p&gt;Recently I took advantage of delimited continuations to create a more
natural Haskell-based kernel for
&lt;acronym title="Genetics Information Manipulation Language"&gt;GIML&lt;/acronym&gt;.
The amazing scope-herding abilities of &lt;em&gt;reset&lt;/em&gt; and
&lt;em&gt;shift&lt;/em&gt; were the magic that made it possible.&lt;/p&gt;


	&lt;p&gt;Ready to get wild?  Grab an espresso and read on.&lt;/p&gt;&lt;p&gt;Consider the &lt;span class="caps"&gt;GIML&lt;/span&gt; code below.  A block, delimited by curly braces,
establishes a new evaluation frame.  Variable bindings (such as those
for &lt;em&gt;x&lt;/em&gt; below) shadow earlier bindings, and each remains in effect until
its enclosing block goes out of scope.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;# GIML code (ex. 1)
x &amp;lt;- 1
{
    x &amp;lt;- 2   # local binding shadows earlier binding
    x        # evaluates to 2
}            # local binding goes out of scope with block
x   # evaluates to 1
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The &lt;a href="http://www.haskell.org/hawiki/MonadReader"&gt;Reader monad&lt;/a&gt;
provides most of this block-scoping behavior for
free, and so it makes a natural part of the monad underlying the &lt;span class="caps"&gt;GIML&lt;/span&gt;
evaluator.  Its job is to make an environment &amp;#8211; in this case, a
Haskell &lt;a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data.Map.html"&gt;Data.Map&lt;/a&gt; containing our active bindings &amp;#8211; available to the
actions running within it.  The Reader monad provides the &lt;em&gt;ask&lt;/em&gt; and
&lt;em&gt;asks&lt;/em&gt; combinators, which we can use to get and work with our
bindings.  We can use &lt;em&gt;asks&lt;/em&gt;, for example, to write a combinator
&lt;em&gt;evalVar&lt;/em&gt; that takes a variable name and returns an action that looks
up the variable&amp;#8217;s value in the active bindings:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='comment'&gt;-- Haskell code (ex. 2)&lt;/span&gt;
&lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='varid'&gt;var&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
    &lt;span class='varid'&gt;val&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;asks&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Map&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;findWithDefault&lt;/span&gt; &lt;span class='num'&gt;0&lt;/span&gt; &lt;span class='varid'&gt;var&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
    &lt;span class='varid'&gt;return&lt;/span&gt; &lt;span class='varid'&gt;val&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The Reader monad also supplies a combinator &lt;em&gt;local&lt;/em&gt; that can be used
to run actions within a locally modified environment.  The semantics
of &lt;em&gt;local&lt;/em&gt; ensure that the modifications cannot escape: after the local
actions have been executed, the local environment goes out of scope
and the previous, containing environment is restored.  And that&amp;#8217;s
the block-scoping behavior we want.&lt;/p&gt;


	&lt;p&gt;Let&amp;#8217;s use &lt;em&gt;local&lt;/em&gt; to represent the following &lt;span class="caps"&gt;GIML&lt;/span&gt; block:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;# GIML code (ex. 3)
{
    x &amp;lt;- 2
    x
}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Here is a &lt;em&gt;local&lt;/em&gt;-based Haskell representation of the &lt;span class="caps"&gt;GIML&lt;/span&gt; block:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='comment'&gt;-- Haskell code (ex. 4)&lt;/span&gt;
&lt;span class='varid'&gt;local&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Map&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;insert&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt; &lt;span class='num'&gt;2&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
    &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The Haskell code above says, roughly translated, &amp;#8220;Create a local
environment by binding &lt;em&gt;x&lt;/em&gt; to 2 (on top of any bindings that may have
been effective within the outer environment) and then run the
&lt;em&gt;evalVar&lt;/em&gt; action within the local environment.&amp;#8221;&lt;/p&gt;


	&lt;p&gt;Any actions before or after the &lt;em&gt;local&lt;/em&gt; action will run in the outer
environment, which will be unaffected by the &lt;em&gt;local&lt;/em&gt; action.  For
example, consider the following Haskell code:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;do&lt;/span&gt;
&lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
&lt;span class='varid'&gt;local&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Map&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;insert&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt; &lt;span class='num'&gt;2&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
    &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
&lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;In the code above, if the first &lt;em&gt;evalVar&lt;/em&gt; action results in 1, so must
the third &lt;em&gt;evalVar&lt;/em&gt; action because both run in the exact same
environment.  (The definition of &lt;em&gt;bind&lt;/em&gt; for the Reader monad
guarantees this.)  The second &lt;em&gt;evalVar&lt;/em&gt; action, however, runs in a
locally modified environment where &lt;em&gt;x&lt;/em&gt; is bound to 2, and so it will
result in 2.&lt;/p&gt;


	&lt;p&gt;Unfortunately, this translation scheme for blocks and bindings has a
limitation.  Once we enter a block, we can&amp;#8217;t change its environment
because the only way to make changes is via &lt;em&gt;local&lt;/em&gt;, and those changes
would be confined to a new, local environment and would not affect the
environment of the block&amp;#8217;s remaining actions.  Thus the only way we
can effect a mid-block binding is to introduce another block, nested
within the first.&lt;/p&gt;


	&lt;p&gt;For example, consider the following &lt;span class="caps"&gt;GIML&lt;/span&gt; code:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;# GIML code (ex. 5)
{
    x &amp;lt;- 2
    y &amp;lt;- x + 1
    x + y
}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;How do we translate this into Haskell?  Our earlier translation method
doesn&amp;#8217;t work on this code because we have a mid-block binding.  If we
rewrite the &lt;span class="caps"&gt;GIML&lt;/span&gt; code to make the scope of each binding explicit,
however, we get an equivalent &lt;span class="caps"&gt;GIML&lt;/span&gt; expression that &lt;em&gt;can&lt;/em&gt; be translated:
&lt;a name="ex6"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;# GIML code (ex. 6)
{
    x &amp;lt;- 2
    {
        y &amp;lt;- x + 1
        x + y
    }
}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The Haskell translation of the above: &lt;a name="ex7"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='comment'&gt;-- Haskell code (ex. 7)&lt;/span&gt;
&lt;span class='varid'&gt;local&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Map&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;insert&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt; &lt;span class='num'&gt;2&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
    &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
    &lt;span class='varid'&gt;local&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Map&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;insert&lt;/span&gt; &lt;span class='str'&gt;"y"&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;+&lt;/span&gt; &lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
        &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
        &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"y"&lt;/span&gt;
        &lt;span class='varid'&gt;return&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;+&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;This is, ultimately, the translation we want, but deriving it is
awkward. Who wants to scan the &lt;span class="caps"&gt;AST&lt;/span&gt; and insert scoping blocks?
Not me.&lt;/p&gt;


	&lt;p&gt;Instead, why not break the chains between binding and block-scoping?
We ought to be able to make a local binding anywhere, not just at the
point where a block begins.  Let&amp;#8217;s aim for code like the following, which is
a direct translation of the earlier &lt;span class="caps"&gt;GIML&lt;/span&gt; code and preserves the
user-supplied blocking:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='comment'&gt;-- Haskell code (ex. 8)&lt;/span&gt;
&lt;span class='varid'&gt;enterBlock&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
    &lt;span class='varid'&gt;bindLocal&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt; &lt;span class='num'&gt;2&lt;/span&gt;
    &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
    &lt;span class='varid'&gt;bindLocal&lt;/span&gt; &lt;span class='str'&gt;"y"&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;+&lt;/span&gt; &lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
    &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
    &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"y"&lt;/span&gt;
    &lt;span class='varid'&gt;return&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;+&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The key to this more pleasant translation is the magic pair of
combinators &lt;em&gt;enterBlock&lt;/em&gt; and &lt;em&gt;bindLocal&lt;/em&gt;.  The &lt;em&gt;enterBlock&lt;/em&gt; combinator
creates a new block, and &lt;em&gt;bindLocal&lt;/em&gt; sets up a binding that goes out
of scope when the block that contains it does.  Note that &lt;em&gt;bindLocal&lt;/em&gt;
can be used anywhere, and not just at the beginning of a block.&lt;/p&gt;


	&lt;p&gt;How, then, do we implement these new combinators?  This is where
delimited continuations come in.  Take a look at the combinators&amp;#8217;
definitions:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='comment'&gt;-- Haskell code&lt;/span&gt;
&lt;span class='varid'&gt;enterBlock&lt;/span&gt; &lt;span class='varid'&gt;action&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;reset&lt;/span&gt; &lt;span class='varid'&gt;action&lt;/span&gt;
&lt;span class='varid'&gt;bindLocal&lt;/span&gt; &lt;span class='varid'&gt;var&lt;/span&gt; &lt;span class='varid'&gt;val&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;shift&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyglyph'&gt;\&lt;/span&gt;&lt;span class='varid'&gt;c&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;local&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Map&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;insert&lt;/span&gt; &lt;span class='varid'&gt;var&lt;/span&gt; &lt;span class='varid'&gt;val&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;c&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Before going further, let&amp;#8217;s review &lt;em&gt;reset&lt;/em&gt; and &lt;em&gt;shift&lt;/em&gt;, the dynamic
duo of delimited continuations.  The &lt;em&gt;reset&lt;/em&gt; combinator brackets an
action, drawing a boundary around it.  The &lt;em&gt;shift&lt;/em&gt; combinator is a bit
trickier.  (Quaff espresso now.)  When we invoke &lt;em&gt;shift&lt;/em&gt; within a
bracketed action, something amazing happens.  The bracketed action is
abortively replaced by another action that is constructed by the
template function that we supply to &lt;em&gt;shift&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;Now, here&amp;#8217;s where it gets wild.  Our template function constructs the
replacement action from an internal template, which can pull in the
&lt;em&gt;shift&lt;/em&gt; action&amp;#8217;s &lt;em&gt;context&lt;/em&gt;.  The context is a delimited continuation
that represents the &lt;em&gt;unexecuted&lt;/em&gt; portion of the &lt;em&gt;reset&lt;/em&gt;-bracketed
action &amp;#8211; except for the &lt;em&gt;shift&lt;/em&gt; action itself, which has been removed,
leaving a hole in the context.  Our template fills in the hole with a value by
passing the value to the context, which is really just another
function.&lt;/p&gt;


	&lt;p&gt;If it sounds confusing, the following example may help.  Consider this
code, which represents an action within a monad that supports
delimited continuations and IO:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='comment'&gt;-- Haskell code (ex. 9)&lt;/span&gt;
&lt;span class='varid'&gt;reset&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
    &lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='str'&gt;"before"&lt;/span&gt;
    &lt;span class='varid'&gt;msg&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;shift&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyglyph'&gt;\&lt;/span&gt;&lt;span class='varid'&gt;cxt&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
        &lt;span class='comment'&gt;-- our "template"&lt;/span&gt;
        &lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='str'&gt;"template start"&lt;/span&gt;
        &lt;span class='varid'&gt;cxt&lt;/span&gt; &lt;span class='str'&gt;"template cxt(first)"&lt;/span&gt;
        &lt;span class='varid'&gt;cxt&lt;/span&gt; &lt;span class='str'&gt;"template cxt(second)"&lt;/span&gt;
        &lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='str'&gt;"template end"&lt;/span&gt; 
    &lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='varid'&gt;msg&lt;/span&gt;
    &lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='str'&gt;"after"&lt;/span&gt;
  &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;liftIO&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Let&amp;#8217;s break it down.  First, &lt;em&gt;reset&lt;/em&gt; brackets everything between it
and the &lt;em&gt;where&lt;/em&gt; clause.&lt;/p&gt;


	&lt;p&gt;Second, the initial &lt;em&gt;puts&lt;/em&gt; action prints &amp;#8220;before&amp;#8221; to the screen.&lt;/p&gt;


	&lt;p&gt;Third, the &lt;em&gt;shift&lt;/em&gt; action takes control.  &lt;em&gt;Blammo!&lt;/em&gt; The
&lt;em&gt;reset&lt;/em&gt;-delimited action is aborted.  Its unexecuted portion is
captured as the context, which we can approximate with an anonymous
function that looks like this:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='layout'&gt;(&lt;/span&gt; &lt;span class='keyglyph'&gt;\&lt;/span&gt;&lt;span class='varid'&gt;hole&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;reset&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
    &lt;span class='varid'&gt;msg&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;return&lt;/span&gt; &lt;span class='varid'&gt;hole&lt;/span&gt;
    &lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='varid'&gt;msg&lt;/span&gt;
    &lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='str'&gt;"after"&lt;/span&gt; &lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;(I used the variable &lt;em&gt;hole&lt;/em&gt; above to represent the hole left by
removing the &lt;em&gt;shift&lt;/em&gt; action.)  This context function is then passed by
&lt;em&gt;shift&lt;/em&gt; to our template function, which binds the context function to
its &lt;em&gt;cxt&lt;/em&gt; variable.&lt;/p&gt;


	&lt;p&gt;Then the template function builds the action that will replace the
aborted action.  Before &amp;#8220;expansion,&amp;#8221; the template looks like this:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;do&lt;/span&gt;
&lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='str'&gt;"template start"&lt;/span&gt;
&lt;span class='varid'&gt;cxt&lt;/span&gt; &lt;span class='str'&gt;"template cxt(first)"&lt;/span&gt;
&lt;span class='varid'&gt;cxt&lt;/span&gt; &lt;span class='str'&gt;"template cxt(second)"&lt;/span&gt;
&lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='str'&gt;"template end"&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Each call to the &lt;em&gt;cxt&lt;/em&gt; function drops the captured context into
the template and fills in the context&amp;#8217;s hole with the supplied argument.
Expanding these calls results in the final template:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;do&lt;/span&gt;
&lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='str'&gt;"template start"&lt;/span&gt;
&lt;span class='varid'&gt;reset&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
    &lt;span class='varid'&gt;msg&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;return&lt;/span&gt; &lt;span class='str'&gt;"template cxt(first)"&lt;/span&gt;
    &lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='varid'&gt;msg&lt;/span&gt;
    &lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='str'&gt;"after"&lt;/span&gt;
&lt;span class='varid'&gt;reset&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
    &lt;span class='varid'&gt;msg&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;return&lt;/span&gt; &lt;span class='str'&gt;"template cxt(second)"&lt;/span&gt;
    &lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='varid'&gt;msg&lt;/span&gt;
    &lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='str'&gt;"after"&lt;/span&gt;
&lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='str'&gt;"template end"&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Because the &lt;em&gt;reset&lt;/em&gt;-delimited portions of the code above
do not contain any &lt;em&gt;shift&lt;/em&gt; actions, &lt;em&gt;reset&lt;/em&gt; acts like
an identity function, and the final expansion of our
template is thus as follows:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;do&lt;/span&gt;
&lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='str'&gt;"template start"&lt;/span&gt;
&lt;span class='varid'&gt;msg&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;return&lt;/span&gt; &lt;span class='str'&gt;"template cxt(first)"&lt;/span&gt;
&lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='varid'&gt;msg&lt;/span&gt;
&lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='str'&gt;"after"&lt;/span&gt;
&lt;span class='varid'&gt;msg&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;return&lt;/span&gt; &lt;span class='str'&gt;"template cxt(second)"&lt;/span&gt;
&lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='varid'&gt;msg&lt;/span&gt;
&lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='str'&gt;"after"&lt;/span&gt;
&lt;span class='varid'&gt;puts&lt;/span&gt; &lt;span class='str'&gt;"template end"&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;As we would now expect, running the original action results in the
following output:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;before
template start
template cxt(first)
after
template cxt(second)
after
template end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;(Recall that the first line of output was emitted before the
&lt;em&gt;shift&lt;/em&gt; action was invoked.)&lt;/p&gt;


	&lt;p&gt;The ability to grab the remainder of an action and stuff it into a
template is what makes the magic of &lt;em&gt;enterBlock&lt;/em&gt; and &lt;em&gt;bindLocal&lt;/em&gt;
possible.  When they appear in our Haskellized &lt;span class="caps"&gt;GIML&lt;/span&gt; code from earlier:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='comment'&gt;-- Haskell code&lt;/span&gt;
&lt;span class='varid'&gt;enterBlock&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
    &lt;span class='varid'&gt;bindLocal&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt; &lt;span class='num'&gt;2&lt;/span&gt;
    &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
    &lt;span class='varid'&gt;bindLocal&lt;/span&gt; &lt;span class='str'&gt;"y"&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;+&lt;/span&gt; &lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
    &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
    &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"y"&lt;/span&gt;
    &lt;span class='varid'&gt;return&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;+&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;they expand into this:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;reset&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
    &lt;span class='varid'&gt;shift&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyglyph'&gt;\&lt;/span&gt;&lt;span class='varid'&gt;c&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;local&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Map&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;insert&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt; &lt;span class='num'&gt;2&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;c&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
    &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
    &lt;span class='varid'&gt;shift&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyglyph'&gt;\&lt;/span&gt;&lt;span class='varid'&gt;c&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;local&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Map&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;insert&lt;/span&gt; &lt;span class='str'&gt;"y"&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;+&lt;/span&gt; &lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;c&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
    &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
    &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"y"&lt;/span&gt;
    &lt;span class='varid'&gt;return&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;+&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;And that, thanks to delimited continuations, is &amp;#8220;re-scoped&amp;#8221; 
into to this:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;reset&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
    &lt;span class='varid'&gt;reset&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='varid'&gt;local&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Map&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;insert&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt; &lt;span class='num'&gt;2&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
        &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
        &lt;span class='varid'&gt;shift&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyglyph'&gt;\&lt;/span&gt;&lt;span class='varid'&gt;c&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;local&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Map&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;insert&lt;/span&gt; &lt;span class='str'&gt;"y"&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;+&lt;/span&gt; &lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;c&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
        &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
        &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"y"&lt;/span&gt;
        &lt;span class='varid'&gt;return&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;+&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;and then into this:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;reset&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
    &lt;span class='varid'&gt;reset&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='varid'&gt;local&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Map&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;insert&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt; &lt;span class='num'&gt;2&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
        &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
        &lt;span class='varid'&gt;reset&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='varid'&gt;local&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Map&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;insert&lt;/span&gt; &lt;span class='str'&gt;"y"&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;+&lt;/span&gt; &lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
            &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
            &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"y"&lt;/span&gt;
            &lt;span class='varid'&gt;return&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;+&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Finally, because the remaining &lt;em&gt;reset&lt;/em&gt; combinators contain no &lt;em&gt;shift&lt;/em&gt;
actions, we can treat them like identity functions to arrive at our
final code, which is identical to the Haskell code we wrote by hand
back in &lt;a href="#ex7"&gt;Example 7&lt;/a&gt; to represent the &lt;span class="caps"&gt;GIML&lt;/span&gt; code
that we added explicit blocking to (again, by hand) in
&lt;a href="#ex6"&gt;Example 6&lt;/a&gt;:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;local&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Map&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;insert&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt; &lt;span class='num'&gt;2&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
    &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
    &lt;span class='varid'&gt;local&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Map&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;insert&lt;/span&gt; &lt;span class='str'&gt;"y"&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;+&lt;/span&gt; &lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
        &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"x"&lt;/span&gt;
        &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;evalVar&lt;/span&gt; &lt;span class='str'&gt;"y"&lt;/span&gt;
        &lt;span class='varid'&gt;return&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;+&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;And &lt;em&gt;that&lt;/em&gt;, my friends, is what we were after, all along.  Only this
time around, we didn&amp;#8217;t have to do it by hand &amp;#8211; our combinators did the
hard work for us.&lt;/p&gt;


&lt;div class="update"&gt;&lt;strong&gt;Update 2007-03-15:&lt;/strong&gt; Fixed typo.&lt;/div&gt;</description>
      <pubDate>Tue, 13 Sep 2005 10:24:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:802700ce48678d98209d89c4bb081cfa</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2005/09/13/scope-herding-with-delimited-continuations</link>
      <category>functional programming</category>
      <category>programming languages</category>
      <category>haskell</category>
      <category>languages</category>
      <category>continuations</category>
      <category>scope</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/6</trackback:ping>
    </item>
  </channel>
</rss>
