<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Yet Another Language Geek : Volta</title><link>http://blogs.msdn.com/wesdyer/archive/tags/Volta/default.aspx</link><description>Tags: Volta</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Continuation-Passing Style</title><link>http://blogs.msdn.com/wesdyer/archive/2007/12/22/continuation-passing-style.aspx</link><pubDate>Sat, 22 Dec 2007 09:51:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6833509</guid><dc:creator>wesdyer</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/wesdyer/comments/6833509.aspx</comments><wfw:commentRss>http://blogs.msdn.com/wesdyer/commentrss.aspx?PostID=6833509</wfw:commentRss><description>&lt;p&gt;There are some technical words that cause quite a stir even amongst geeks.&amp;#160; When someone says the word &lt;a href="http://en.wikipedia.org/wiki/Continuation"&gt;&amp;quot;continuation&amp;quot;&lt;/a&gt;, people's eyes glaze over and they seek the first opportunity to change the subject.&amp;#160; The stir is caused because most people don't understand what a continuation is or why someone would want to use one.&amp;#160; This is unfortunate, because they really are rather more simple than they sound. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Defining Continuations&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;A continuation represents the remainder of a computation given a point in that computation.&amp;#160; For example, suppose that two methods are defined: &lt;em&gt;M&lt;/em&gt; and &lt;em&gt;Main&lt;/em&gt;.&amp;#160; The method &lt;em&gt;Main&lt;/em&gt; invokes &lt;em&gt;M&lt;/em&gt; and then writes &amp;quot;Done&amp;quot; to the console.&amp;#160; The method &lt;em&gt;M&lt;/em&gt; assigns &lt;em&gt;x &lt;/em&gt;the value five, invokes &lt;em&gt;F&lt;/em&gt;, increments &lt;em&gt;x&lt;/em&gt;, and writes &lt;em&gt;x&lt;/em&gt; to the console.&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;M()
{
    &lt;span style="color: blue"&gt;var &lt;/span&gt;x = 5;
    F();  &lt;span style="color: green"&gt;// &amp;lt;----------- Point in the computation
    &lt;/span&gt;++x;
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(x);
}

&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main()
{
    M();
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Done&amp;quot;&lt;/span&gt;);
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The continuation of the computation at the invocation of &lt;em&gt;F &lt;/em&gt;is the remainder of the program execution beginning with incrementing &lt;em&gt;x&lt;/em&gt; in the method &lt;em&gt;M&lt;/em&gt;.&amp;#160; In this case, the continuation includes incrementing &lt;em&gt;x&lt;/em&gt;, writing &lt;em&gt;x&lt;/em&gt;, returning to &lt;em&gt;Main&lt;/em&gt;, and writing &amp;quot;Done&amp;quot; to the console.&lt;/p&gt;

&lt;p&gt;Some languages give programmers explicit access to continuations.&amp;#160; For example, Scheme has a function which calls a function passing a function representing the current continuation.&amp;#160; The function is aptly named &lt;a href="http://community.schemewiki.org/?call-with-current-continuation"&gt;call with current continuation&lt;/a&gt; or &lt;em&gt;call/cc&lt;/em&gt;.&amp;#160; If such a function existed in .NET it might return a T given a delegate that returns a T given a delegate from T to T.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;T CallCC&amp;lt;T&amp;gt;(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;font color="#2b91af"&gt;Func&lt;/font&gt;&amp;lt;T,T&amp;gt;, T&amp;gt; f)&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Continuations are the functional counterparts of GOTOs both in power and inscrutability.&amp;#160; They can express arbitrary control flow like coroutines and exceptions while bewildering some of the brightest programmers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Returning Values with Continuations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The simplest use of a continuation is to simulate returning out of a function.&amp;#160; Suppose that .NET had the function &lt;em&gt;CallCC&lt;/em&gt;.&amp;#160; If &lt;em&gt;Main&lt;/em&gt; calls a function &lt;em&gt;Foo&lt;/em&gt; passing the value four and if &lt;em&gt;Foo&lt;/em&gt; immediately invokes &lt;em&gt;CallCC&lt;/em&gt; with a lambda that binds &lt;em&gt;Return&lt;/em&gt; to the continuation at the point of the call to &lt;em&gt;CallCC, &lt;/em&gt;then when &lt;em&gt;Return&lt;/em&gt; is invoked inside of the lambda with the value four, computation will immediately jump out of the lambda to the point after &lt;em&gt;CallCC&lt;/em&gt; but before the return with the value four on the stack.&amp;#160; This happens regardless what of occurs after the invocation of &lt;em&gt;Return&lt;/em&gt; inside the lambda.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static int &lt;/span&gt;Foo(&lt;span style="color: blue"&gt;int &lt;/span&gt;n)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;CallCC&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(Return =&amp;gt;
        {
            Return(n);
            &lt;span style="color: blue"&gt;return &lt;/span&gt;n + 1;
        });
}

&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main()
{
    Foo(4);
}&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;p&gt;The result of running the program is therefore four and not five.&lt;/p&gt;

&lt;p&gt;All of this demonstrates that returning from a function is equivalent to invoking the continuation defined at the function's call-site.&amp;#160; When a function &amp;quot;returns&amp;quot;, it implicitly invokes the continuation of its call-site.&lt;/p&gt;

&lt;p&gt;When people explain continuations, they usually discuss stack frames and instruction pointers.&amp;#160; It is easy to see why.&amp;#160; The implicit invocation of the &amp;quot;return&amp;quot; continuation restores the stack frame at the call-site and sets the instruction pointer to the instruction immediately after the call-site.&amp;#160; This is what invoking a continuation does: restore the appropriate stack frame and set the instruction pointer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Continuation-Passing Style&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is still possible to use continuations if a language does not support a function like &lt;em&gt;call/cc&lt;/em&gt;.&amp;#160; A programmer can explicitly construct the continuation of a computation and pass it directly to a function.&lt;/p&gt;

&lt;p&gt;To illustrate this transformation, suppose that a function, &lt;em&gt;Identity,&lt;/em&gt; is defined that returns the value it is given. &lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static &lt;/span&gt;T Identity&amp;lt;T&amp;gt;(T value)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;value;
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;The return statement in &lt;em&gt;Identity&lt;/em&gt; implicitly invokes the continuation of the call-site causing computation to leave &lt;em&gt;Identity&lt;/em&gt; and resume at the point immediately after the invocation of &lt;em&gt;Identity&lt;/em&gt; at the call-site.&amp;#160; If &lt;em&gt;Main&lt;/em&gt; contains only a call to &lt;em&gt;WriteLine&lt;/em&gt; passing the result of the call to &lt;em&gt;Identity,&lt;/em&gt; then computation resumes with the call to &lt;em&gt;WriteLine&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main()
{
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(Identity(&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;));
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;However, instead of implicitly invoking &lt;em&gt;Identity's&lt;/em&gt; continuation, the programmer can pass the continuation to &lt;em&gt;Identity &lt;/em&gt;explicitly.&amp;#160; An extra argument is added to &lt;em&gt;Identity&lt;/em&gt; which is a void-returning delegate with one parameter that is the same type as the return type of the former &lt;em&gt;Identity&lt;/em&gt; function.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main()
{
    Identity(&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;, s =&amp;gt; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(s));
}

&lt;span style="color: blue"&gt;static void &lt;/span&gt;Identity&amp;lt;T&amp;gt;(T value, &lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;T&amp;gt; k)
{
    k(value);
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;At the call-site, the remainder of the computation has moved into the lambda passed to &lt;em&gt;Identity&lt;/em&gt;.&amp;#160; The continuation is explicitly passed.&amp;#160; On the callee's side, the return statement is replaced by the invocation of the continuation parameter, &lt;em&gt;k&lt;/em&gt;, with the return value.&lt;/p&gt;

&lt;p&gt;This pattern is called &lt;a href="http://en.wikipedia.org/wiki/Continuation-passing_style"&gt;continuation-passing style&lt;/a&gt; or CPS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Converting to CPS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that we know enough to be dangerous, let's see what we can do.&amp;#160; Consider the typical &lt;em&gt;Max&lt;/em&gt; function.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static int &lt;/span&gt;Max(&lt;span style="color: blue"&gt;int &lt;/span&gt;n, &lt;span style="color: blue"&gt;int &lt;/span&gt;m)
{
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(n &amp;gt; m)
        &lt;span style="color: blue"&gt;return &lt;/span&gt;n;
    &lt;span style="color: blue"&gt;else
        return &lt;/span&gt;m;
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;To convert this function to CPS, follow these steps:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;1.&amp;#160; Change the return type to void&lt;/p&gt;

  &lt;p&gt;2.&amp;#160; Add an extra argument of type &lt;em&gt;Action&amp;lt;T&amp;gt;&lt;/em&gt; where &lt;em&gt;T&lt;/em&gt; is the original return type&lt;/p&gt;

  &lt;p&gt;3.&amp;#160; Replace all return statements with invocations of the new continuation argument passing the expression used in the return statement&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Applying these steps to the integer version of &lt;em&gt;Max&lt;/em&gt;, the function now returns void, takes an extra parameter, &lt;em&gt;k&lt;/em&gt;, of type &lt;em&gt;Action&amp;lt;int&amp;gt;&lt;/em&gt;, and invokes &lt;em&gt;k&lt;/em&gt; everywhere a return statement appeared.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Max(&lt;span style="color: blue"&gt;int &lt;/span&gt;n, &lt;span style="color: blue"&gt;int &lt;/span&gt;m, &lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; k)
{
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(n &amp;gt; m)
        k(n);
    &lt;span style="color: blue"&gt;else
        &lt;/span&gt;k(m);
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;To convert the call-site to CPS:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;1.&amp;#160; Remove all of the remaining computation after the call-site&lt;/p&gt;

  &lt;p&gt;2.&amp;#160; Put the remaining computation in the body of the lambda corresponding to the continuation argument&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example, suppose the user defined a method &lt;em&gt;Main&lt;/em&gt; which invokes &lt;em&gt;WriteLine&lt;/em&gt; with the result of applying &lt;em&gt;Max&lt;/em&gt; to three and four.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;font face="Courier New"&gt;&lt;span style="color: blue"&gt;&lt;font face="Verdana"&gt;&lt;/font&gt;static void &lt;/span&gt;Main()

      &lt;br /&gt;{

      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(Max(3, 4));

      &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The remaining computation after the invocation of &lt;em&gt;Max &lt;/em&gt;is the call to &lt;em&gt;WriteLine&lt;/em&gt;.&amp;#160; Therefore, the call to &lt;em&gt;WriteLine &lt;/em&gt;is moved into the lambda representing the continuation passed to &lt;em&gt;Max&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;font face="Courier New"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main()

      &lt;br /&gt;{

      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Max(3, 4, x =&amp;gt; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(x));

      &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The steps for converting the call-site skirted the issue of what to do with return statements in the continuation.&amp;#160; For example, suppose there are three methods &lt;em&gt;Main&lt;/em&gt;, &lt;em&gt;F&lt;/em&gt;, and &lt;em&gt;G&lt;/em&gt; where &lt;em&gt;Main &lt;/em&gt;calls &lt;em&gt;F&lt;/em&gt; and &lt;em&gt;F&lt;/em&gt; calls &lt;em&gt;G.&amp;#160; &lt;/em&gt;To convert &lt;em&gt;F&lt;/em&gt; and &lt;em&gt;G &lt;/em&gt;to CPS, follow the same transformation steps as with &lt;em&gt;Max&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main()
{
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(F(1) + 1);
}

&lt;span style="color: blue"&gt;static int &lt;/span&gt;F(&lt;span style="color: blue"&gt;int &lt;/span&gt;n)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;G(n + 1) + 1;
}

&lt;span style="color: blue"&gt;static int &lt;/span&gt;G(&lt;span style="color: blue"&gt;int &lt;/span&gt;n)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;n + 1;
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;However, what should the transformation do with the return statement in &lt;em&gt;F&lt;/em&gt;?&amp;#160; The continuation passed to &lt;em&gt;G&lt;/em&gt; should definitely not attempt to return a result because the continuation is a void-returning delegate.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;F(&lt;span style="color: blue"&gt;int &lt;/span&gt;n, &lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; k)
{
    G(n + 1, x =&amp;gt; { &lt;span style="color: blue"&gt;return &lt;/span&gt;x + 1; });  &lt;span style="color: green"&gt;// error, Action&amp;lt;int&amp;gt; cannot return a value
&lt;/span&gt;}&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;That wouldn't make any sense.&amp;#160; Delegates with void return-types cannot return values.&amp;#160; Furthermore, the code completely ignores &lt;em&gt;k&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Instead, the return statement should be transformed into an invocation of &lt;em&gt;k&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main()
{
    F(1, x =&amp;gt; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(x + 1));
}

&lt;span style="color: blue"&gt;static void &lt;/span&gt;F(&lt;span style="color: blue"&gt;int &lt;/span&gt;n, &lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; k)
{
    G(n + 1, x =&amp;gt; k(x + 1));
}

&lt;span style="color: blue"&gt;static void &lt;/span&gt;G(&lt;span style="color: blue"&gt;int &lt;/span&gt;n, &lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; k)
{
    k(n + 1);
}&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This is how functions that use explicit continuations are composed together.&lt;/p&gt;

&lt;p&gt;What about recursive functions like Factorial?&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main()
{
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(Factorial(5));
}

&lt;span style="color: blue"&gt;static int &lt;/span&gt;Factorial(&lt;span style="color: blue"&gt;int &lt;/span&gt;n)
{
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(n == 0)
        &lt;span style="color: blue"&gt;return &lt;/span&gt;1;
    &lt;span style="color: blue"&gt;else
        return &lt;/span&gt;n * Factorial(n - 1);
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Recursive functions can be transformed just as easily following the same steps.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main()
{
    Factorial(5, x =&amp;gt; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(x));
}

&lt;span style="color: blue"&gt;static void &lt;/span&gt;Factorial(&lt;span style="color: blue"&gt;int &lt;/span&gt;n, &lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; k)
{
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(n == 0)
        k(1);
    &lt;span style="color: blue"&gt;else
        &lt;/span&gt;Factorial(n - 1, x =&amp;gt; k(n * x));
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;CPS turns a program inside-out.&amp;#160; In the process, the programmer may feel that his brain has been turned inside-out as well.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why CPS?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What exactly can a programmer do with CPS besides showoff at parties?&lt;/p&gt;

&lt;p&gt;Compilers use a more thorough CPS transformation to produce an intermediate form amenable to many analyses.&amp;#160; UI frameworks use CPS to keep the UI responsive while allowing nonlinear program interaction.&amp;#160; Web servers use CPS to allow computation to flow asynchronously across pages.&lt;/p&gt;

&lt;p&gt;Most programmers have used functions which take a callback.&amp;#160; Often, the callback is the code that is invoked upon completion of the function.&amp;#160; In these cases, the callback is an explicitly-passed continuation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Asynchronous Calls&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hiding network latency requires asynchronous calls.&amp;#160; In the first technology preview, Volta allows programmers to add asynchronous versions of methods on tier boundaries.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;RunAtOrigin&lt;/span&gt;]
&lt;span style="color: blue"&gt;static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public static int &lt;/span&gt;F(&lt;span style="color: blue"&gt;string &lt;/span&gt;s)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;s != &lt;span style="color: blue"&gt;null &lt;/span&gt;? s.Length : 0;
    }
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;To make a method asynchronous, define the CPS-equivalent method signature and annotate it with the &lt;em&gt;Async &lt;/em&gt;attribute.&amp;#160; Volta will generate the body and modify the call-sites accordingly.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;Async&lt;/span&gt;]
&lt;span style="color: blue"&gt;public static void &lt;/span&gt;F(&lt;span style="color: blue"&gt;string &lt;/span&gt;s, &lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; k);&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;If the programmer invokes an asynchronous method &lt;em&gt;F, &lt;/em&gt;then Volta will launch the invocation on another thread and invoke the continuation upon completion of the call to &lt;em&gt;F.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;C&lt;/span&gt;.F(&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;, x =&amp;gt; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(x));&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;strong&gt;Wrapping it Up&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Continuations are powerful.&amp;#160; CPS gives programmers a way to use continuations in their day-to-day work.&amp;#160; Perhaps, someday soon we'll discuss how to make CPS more palatable, but we will need to discuss the &amp;quot;M&amp;quot; word first.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6833509" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/wesdyer/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/wesdyer/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://blogs.msdn.com/wesdyer/archive/tags/Volta/default.aspx">Volta</category></item><item><title>Volta and You</title><link>http://blogs.msdn.com/wesdyer/archive/2007/12/06/volta-and-you.aspx</link><pubDate>Thu, 06 Dec 2007 20:37:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6682387</guid><dc:creator>wesdyer</dc:creator><slash:comments>19</slash:comments><comments>http://blogs.msdn.com/wesdyer/comments/6682387.aspx</comments><wfw:commentRss>http://blogs.msdn.com/wesdyer/commentrss.aspx?PostID=6682387</wfw:commentRss><description>&lt;p&gt;Yesterday, Volta was made publicly available for the first time.&amp;#160; It is an &lt;em&gt;experimental&lt;/em&gt; project in the early stages of development.&amp;#160; The team decided to release an early technology preview so that developers everywhere can help guide the project through experience and feedback.&amp;#160; We want your feedback.&lt;/p&gt;  &lt;p&gt;The first release provides the basic feature set that will be improved upon with time.&amp;#160; It has some obvious shortcomings that we are aware of and are actively addressing.&amp;#160; But really, at this stage, the preview is more concerned with sparking your imagination about what is possible than ironing out all of the details.&lt;/p&gt;  &lt;p&gt;Perhaps you disagree.&amp;#160; Maybe the most important feature to you is the completeness of a final product.&amp;#160; If that is the case, then say so and we will seriously consider making it a higher priority for the upcoming early experimental releases.&lt;/p&gt;  &lt;p&gt;At some point, Volta may become, feed into, or inform a product, but that is a little way off yet.&amp;#160; So let's enjoy the unique opportunity of working together to make something great.&lt;/p&gt;  &lt;p&gt;In the coming months, I will alternate between three types of posts:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;1.&amp;#160; Volta focused posts: explaining the motivation, features, and technical details&lt;/p&gt;    &lt;p&gt;2.&amp;#160; C#: this includes both 3.0 and eventually 4.0 features&lt;/p&gt;    &lt;p&gt;3.&amp;#160; Random thoughts: like it says; two that will be discussed soon are programmer tests and continuations&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;I hope you enjoy the posts and I look forward to engaging with you in discussion.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6682387" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/wesdyer/archive/tags/Volta/default.aspx">Volta</category></item><item><title>Volta: Redefining Web Development</title><link>http://blogs.msdn.com/wesdyer/archive/2007/12/05/volta-redefining-web-development.aspx</link><pubDate>Wed, 05 Dec 2007 22:14:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6669478</guid><dc:creator>wesdyer</dc:creator><slash:comments>25</slash:comments><comments>http://blogs.msdn.com/wesdyer/comments/6669478.aspx</comments><wfw:commentRss>http://blogs.msdn.com/wesdyer/commentrss.aspx?PostID=6669478</wfw:commentRss><description>&lt;p&gt;Anyone who writes web applications knows that web development is not easy.&amp;#160; Developers wrangle with a soup of technologies distributed across multiple tiers.&amp;#160; We live in a world where programmers accept the fact that they need to know &lt;a href="http://en.wikipedia.org/wiki/Web_development"&gt;four or five different languages, tools, and environments&lt;/a&gt; just to get a site up and running.&amp;#160; In ancient times, the Egyptians built marvelous structures despite &lt;a href="http://books.google.com/books?id=MXaR-s9N4uEC&amp;amp;pg=PA136&amp;amp;lpg=PA136&amp;amp;dq=copper+chisels&amp;amp;source=web&amp;amp;ots=nanvWmY6qx&amp;amp;sig=LA6IC5LOR6pMssDO6OH3m_MzGc0#PPP1,M1"&gt;the primitive tools&lt;/a&gt; that &lt;a href="http://books.google.com/books?id=-sFpPTDMyrMC&amp;amp;pg=PA211&amp;amp;lpg=PA211&amp;amp;dq=copper+chisels&amp;amp;source=web&amp;amp;ots=P5yoKMAI01&amp;amp;sig=xAtHpZye37I-OerXxStPD3PZbvI"&gt;the workmen used&lt;/a&gt;.&amp;#160; Building a pyramid took most of the national resources of wealth and labor.&amp;#160; Today, we build structures which are vastly more complicated and yet require only a tiny fraction of the resources.&amp;#160; The difference is in the tools and the infrastructure.&lt;/p&gt;  &lt;p&gt;In a similar way, Volta significantly improves web development.&amp;#160; Programmers write web applications using familiar .NET languages, libraries, and tools.&amp;#160; Volta splits the application into multiple parts potentially running on different tiers, say, client and server.&amp;#160; Client code needs only a minimal, JavaScript-enabled browser, though Volta will take advantage of additional runtimes that may be present.&lt;/p&gt;  &lt;p&gt;Programmers simply refactor classes that need to run on tiers other than the client and Volta injects the boilerplate code for communication, serialization, synchronization -- all the remoting code.&amp;#160; The developer enjoys all of the benefits of .NET: a great debugger, test tools, profiling, intellisense, refactorings, etc.&lt;/p&gt;  &lt;p&gt;Just how simple is Volta?&amp;#160; Let's write an application that uses a button to query the server for a string and displays that string to the client: the hello world web application.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/wesdyer/WindowsLiveWriter/VoltaRedefiningWebDevelopment_D283/image_4.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="297" alt="image" src="http://blogs.msdn.com/blogfiles/wesdyer/WindowsLiveWriter/VoltaRedefiningWebDevelopment_D283/image_thumb_1.png" width="470" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Now, let's write the code for the web page.&amp;#160; We need a &lt;em&gt;Div&lt;/em&gt; for the output and an &lt;em&gt;Input &lt;/em&gt;for the interaction.&amp;#160; Of course, we could have constructed the page elements with HTML/CSS instead.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System;
&lt;span style="color: blue"&gt;using &lt;/span&gt;Microsoft.LiveLabs.Volta.Html;
&lt;span style="color: blue"&gt;using &lt;/span&gt;Microsoft.LiveLabs.Volta.Xml;

&lt;span style="color: blue"&gt;namespace &lt;/span&gt;HelloWorld
{
    &lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;VoltaPage1 &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;Page
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public &lt;/span&gt;VoltaPage1()
        {
            &lt;span style="color: blue"&gt;var &lt;/span&gt;output = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Div&lt;/span&gt;();

            &lt;span style="color: blue"&gt;var &lt;/span&gt;b = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Input&lt;/span&gt;();
            b.Type = &lt;span style="color: #a31515"&gt;&amp;quot;button&amp;quot;&lt;/span&gt;;
            b.Value = &lt;span style="color: #a31515"&gt;&amp;quot;Get Message&amp;quot;&lt;/span&gt;;
            b.Click += () =&amp;gt; output.InnerHtml = &lt;span style="color: #2b91af"&gt;C&lt;/span&gt;.GetMessage();

            Document.Body.AppendChild(output);
            Document.Body.AppendChild(b);
        }
    }

    &lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public static string &lt;/span&gt;GetMessage()
        {
            &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;Hello, World&amp;quot;&lt;/span&gt;;
        }
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;But we want to produce the message on the server.&amp;#160; Time to refactor.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;a href="http://blogs.msdn.com/blogfiles/wesdyer/WindowsLiveWriter/VoltaRedefiningWebDevelopment_D283/image8.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="355" alt="image" src="http://blogs.msdn.com/blogfiles/wesdyer/WindowsLiveWriter/VoltaRedefiningWebDevelopment_D283/image8_thumb.png" width="484" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Browser clients call the server, the &amp;quot;Origin&amp;quot;, because this ensures the message will come from the same server that supplied the HTML.&lt;/p&gt;

&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;RunAtOrigin&lt;/span&gt;]
&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public static string &lt;/span&gt;GetMessage()
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;Hello, World&amp;quot;&lt;/span&gt;;
    }
} &lt;/pre&gt;

&lt;p&gt;That is it.&amp;#160; Try it out.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/wesdyer/WindowsLiveWriter/VoltaRedefiningWebDevelopment_D283/image12.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="252" alt="image" src="http://blogs.msdn.com/blogfiles/wesdyer/WindowsLiveWriter/VoltaRedefiningWebDevelopment_D283/image12_thumb.png" width="513" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Now, click &amp;quot;Get Message&amp;quot;.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/wesdyer/WindowsLiveWriter/VoltaRedefiningWebDevelopment_D283/image16.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="249" alt="image" src="http://blogs.msdn.com/blogfiles/wesdyer/WindowsLiveWriter/VoltaRedefiningWebDevelopment_D283/image16_thumb.png" width="506" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Great.&amp;#160; But is it cross-browser compatible?&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/wesdyer/WindowsLiveWriter/VoltaRedefiningWebDevelopment_D283/image20.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="225" alt="image" src="http://blogs.msdn.com/blogfiles/wesdyer/WindowsLiveWriter/VoltaRedefiningWebDevelopment_D283/image20_thumb.png" width="499" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yes.&amp;#160; And you can debug it. &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;a href="http://blogs.msdn.com/blogfiles/wesdyer/WindowsLiveWriter/VoltaRedefiningWebDevelopment_D283/image30.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="759" alt="image" src="http://blogs.msdn.com/blogfiles/wesdyer/WindowsLiveWriter/VoltaRedefiningWebDevelopment_D283/image30_thumb.png" width="899" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;You can even debug across tiers.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/wesdyer/WindowsLiveWriter/VoltaRedefiningWebDevelopment_D283/image35.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="761" alt="image" src="http://blogs.msdn.com/blogfiles/wesdyer/WindowsLiveWriter/VoltaRedefiningWebDevelopment_D283/image35_thumb.png" width="900" border="0" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;There is a lot more to Volta in the first technology preview which was made publicly available at 11 am PST today and there will be a lot more to come.&lt;/p&gt;

&lt;p&gt;Still skeptical?&amp;#160; Try it out for yourself.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://labs.live.com/volta"&gt;http://labs.live.com/volta&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/wesdyer/WindowsLiveWriter/VoltaRedefiningWebDevelopment_D283/image_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="168" alt="image" src="http://blogs.msdn.com/blogfiles/wesdyer/WindowsLiveWriter/VoltaRedefiningWebDevelopment_D283/image_thumb.png" width="354" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6669478" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/wesdyer/archive/tags/Volta/default.aspx">Volta</category></item></channel></rss>