<?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 languages</title>
    <link>http://blog.moertel.com/articles/tag/languages?tag=languages</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Quality rants on programming theory and stuff geeks like</description>
    <item>
      <title>Wondrous oddities: R's function-call semantics</title>
      <description>&lt;p&gt;Every so often, I am going to write about
&lt;em&gt;wondrous oddities&lt;/em&gt; &amp;#8211; obscure programming-language features
that are so cool they deserve wider notice.
Today, in the first installment, I want to show you the function-call
semantics of &lt;a href="http://www.r-project.org/about.html"&gt;R&lt;/a&gt;, a great system
for statistical computing.&lt;/p&gt;


	&lt;p&gt;You might not expect a statistics system to have a first-class
programming language at it&amp;#8217;s heart, but if you think about it, it does
make sense.  The R language, actually a dialect of the S language, is
described as &amp;#8220;a well-developed, simple and effective programming
language which includes conditionals, loops, user-defined recursive
functions and input and output facilities.&amp;#8221;  All true.  It gives me
the feeling of an infix Lisp or Scheme whose syntax is slanted toward
mathematics and vector operations.  The language has an object layer,
too, but that&amp;#8217;s not why we are here.&lt;/p&gt;


	&lt;p&gt;No, we are here to look at R&amp;#8217;s uncommonly interesting function-call semantics, in particular
argument binding and evaluation.  Let&amp;#8217;s dig in.&lt;/p&gt;&lt;h3&gt;Flexible argument binding&lt;/h3&gt;


	&lt;p&gt;Here is a simple function of two arguments:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;f &amp;lt;- function(tens, ones = tens)
    ones + 10 * tens
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The function &lt;em&gt;f&lt;/em&gt; has two formal arguments, &lt;em&gt;tens&lt;/em&gt; and &lt;em&gt;ones&lt;/em&gt;, the
second of which has a default value, defined to be &lt;em&gt;tens&lt;/em&gt;, referring
back to the first argument.  R lets you call the function like so,
passing in arguments by position:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;f(3, 4)  # 34
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;But you can also specify arguments by name, in any order:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;f(tens=3, ones=4)  # 34
f(ones=4, tens=5)  # 54
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And, if you leave off the &lt;em&gt;ones&lt;/em&gt; argument, it will get its
value from &lt;em&gt;tens&lt;/em&gt; because of its default definition:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;f(3)       # 33
f(tens=2)  # 22
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Up to this point, you&amp;#8217;re probably thinking that this is nice and all,
but not &amp;#8220;wondrous oddity&amp;#8221; material.   Hold that thought for a moment.&lt;/p&gt;


	&lt;p&gt;Moving on, you can mix positional and named arguments and even shuffle the
argument ordering:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;f(tens=2, 6)       # 26
f(6, tens=2)       # 26
f(ones=9, tens=8)  # 89
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;You can even abbreviate arguments:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;f(tens=2, o=6)  # 26
f(t=3, ones=9)  # 39
f(o=9, t=4)     # 49
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;To explore the full abbreviation semantics, we need a more complex
function:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;g &amp;lt;- function(ones=1, tens=2, hundreds=3, thousands=4)
    ones + 10 * tens + 100 * hundreds + 1000 * thousands
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;You can call the function with no arguments, as expected:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;g()  # 4321
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;But you can&amp;#8217;t get away with an ambiguous argument abbreviation:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;g(t=0) # Error in g(t = 0) :
       # argument 1 matches multiple formal arguments
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;So you must disambiguate:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;g(te=0) # 4301
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;But, R is smart enough not to consider an abbreviation ambiguous if
the ambiguity goes away when other arguments are matched exactly:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;g(t=0, thousands=9) # 9301
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Before we move on, let&amp;#8217;s review R&amp;#8217;s argument-binding features:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;you can pass arguments by position or by name&lt;/li&gt;
		&lt;li&gt;you can omit arguments that have defaults&lt;/li&gt;
		&lt;li&gt;you can abbreviate argument names&lt;/li&gt;
		&lt;li&gt;you can use any combination of the above features, provided
  the combination results in no ambiguity&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h3&gt;Lazy argument evaluation&lt;/h3&gt;


	&lt;p&gt;Unlike most programming languages, R evaluates bound arguments lazily,
meaning that the expressions you pass as arguments are not converted
into values until are they needed.  This lets you create functions
that act like &lt;a href="http://foldoc.org/foldoc.cgi?query=control+structure&amp;#38;action=Search"&gt;control
structures&lt;/a&gt;.
For example, the following function acts like an if-then-else control
structure:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;myif &amp;lt;- function(test, valT, valF)
    if (test) valT else valF

myif(T, print("true"), print("false"))  # prints "true" 
myif(F, print("true"), print("false"))  # prints "false" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Even though the &lt;em&gt;valT&lt;/em&gt; and &lt;em&gt;valF&lt;/em&gt; arguments are print statements,
they are not evaluated until they are chosen by the test argument.
The unchosen argument is not evaluated at all.&lt;/p&gt;


	&lt;p&gt;In contrast, most common languages evaluate arguments before passing
them into functions.  For example, Ruby:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;# Ruby code

def myif(test, valT, valF)
  if (test) then valT else valF; end
end

myif(true, puts("true"), puts("false"))
# prints true *and* false
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Another benefit of R&amp;#8217;s lazy argument evaluation is that you can
provide mutually recursive defaults, which is a great way to implement
adaptive interfaces.  For example, here is a function that computes a
coordinate&amp;#8217;s representation in both &lt;a href="http://mathworld.wolfram.com/CartesianCoordinates.html"&gt;Cartesian&lt;/a&gt; and &lt;a href="http://mathworld.wolfram.com/PolarCoordinates.html"&gt;polar coordinate&lt;/a&gt;
systems.  You can specify the input coordinate in either system, and
the function adapts automatically:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;# R code

polar &amp;lt;- function(x = r * cos(theta), y = r * sin(theta),
                  r = sqrt(x*x + y*y), theta = atan2(y, x))
    c(x, y, r, theta)

polar(1,1)                    # provide (x,y) pair
# 1.0000000 1.0000000 1.4142136 0.7853982

polar(r=sqrt(2), theta=pi/4)  # provide (r, theta) pair
# 1.0000000 1.0000000 1.4142136 0.7853982
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Notice how there was no need for me to test the arguments to see how
the function was called.  All I did was define each set of argument
defaults in terms of the other set of arguments.  R can figure out the
rest based on how the function is called.  That&amp;#8217;s programmer friendly.&lt;/p&gt;


	&lt;p&gt;Let&amp;#8217;s review.  R&amp;#8217;s lazy argument evaluation provides cool benefits:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;you can define your own control structures&lt;/li&gt;
		&lt;li&gt;you can provide mutually recursive defaults for arguments, which makes
  smart, flexible interfaces easy&lt;/li&gt;
		&lt;li&gt;if you don&amp;#8217;t use an argument, you don&amp;#8217;t have to pay for R to evaluate it&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h3&gt;Split-horizon scoping&lt;/h3&gt;


	&lt;p&gt;R&amp;#8217;s scoping rules give  passed arguments and
default values different perspectives &amp;#8211; split horizons, if you
will.  Passed arguments see what was visible at the time of the call.
No biggie here; every language works this way.  Default values, on the
other hand, see what is inside of the function as it evaluates.  That
means defaults have access to bound arguments &lt;em&gt;and local variables&lt;/em&gt;,
which means you can write functions whose defaults rely upon values
computed &lt;em&gt;in the function body&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;This is a great feature that combines with R&amp;#8217;s lazy argument binding
to eliminate argument-handling logic.  For example, a lot of R&amp;#8217;s
library code takes advantage of the following idiom:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;myplot &amp;lt;- function(vals, ymin=bnds$ymin, ymax=bnds$ymax) {
    bnds &amp;lt;- compute.bounds(vals)
    # plot the values, constrained by ymin and ymax ...
}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The &lt;em&gt;myplot&lt;/em&gt; function plots the values you pass it in &lt;em&gt;vals&lt;/em&gt;.  By
default the function scales the plot to show all of the values.  If
you want, however, you can constrain the vertical extent of the plot
by passing in &lt;em&gt;ymin&lt;/em&gt; and/or &lt;em&gt;ymax&lt;/em&gt; arguments.  Note the refreshing
lack of logic to handle the arguments.  The code just gets down to business.&lt;/p&gt;


	&lt;p&gt;For comparison, here is a Ruby version of the function.  When it comes
to this kind of thing, Ruby is better than most mainstream languages,
but it still makes us do about twice the work that R does:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;def myplot(vals, ymin = nil, ymax = nil)
  bnds = compute_bounds(vals)
  ymin ||= bnds.ymin
  ymax ||= bnds.ymax
  # plot the values, constrained by ymin and ymax ...
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;To recap, R&amp;#8217;s scoping rules, when combined with lazy argument
evaluation, let you shave away tedious argument tests and placeholder
defaults such as &lt;em&gt;nil&lt;/em&gt;.  Instead, you can focus on the core logic,
letting R take care of the argument handling burdens. The win might seem
small, but when you write a lot of code, the clarity and code
reduction add up.&lt;/p&gt;


	&lt;h3&gt; That&amp;#8217;s it&lt;/h3&gt;


	&lt;p&gt;So there you have it: a surprisingly sophisticated function-call
semantics that does away with argument-handling tedium.  That
you&amp;#8217;ll find it in a statistics system and not in a mainstream
programming language makes it a &lt;em&gt;wondrous oddity&lt;/em&gt;.&lt;/p&gt;</description>
      <pubDate>Fri, 20 Jan 2006 18:02:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:7b85950a83a444d1317fae802523c404</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2006/01/20/wondrous-oddities-rs-function-call-semantics</link>
      <category>programming languages</category>
      <category>statistics</category>
      <category>wondrous oddities</category>
      <category>R</category>
      <category>languages</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/24</trackback:ping>
    </item>
    <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>
