<?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 : Theory</title><link>http://blogs.msdn.com/wesdyer/archive/tags/Theory/default.aspx</link><description>Tags: Theory</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>The Marvels of Monads</title><link>http://blogs.msdn.com/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx</link><pubDate>Fri, 11 Jan 2008 03:06:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7064392</guid><dc:creator>wesdyer</dc:creator><slash:comments>35</slash:comments><comments>http://blogs.msdn.com/wesdyer/comments/7064392.aspx</comments><wfw:commentRss>http://blogs.msdn.com/wesdyer/commentrss.aspx?PostID=7064392</wfw:commentRss><description>&lt;p&gt;If the word &lt;a href="http://blogs.msdn.com/wesdyer/archive/2007/12/22/continuation-passing-style.aspx"&gt;&amp;quot;continuation&amp;quot;&lt;/a&gt; causes eyes to glaze over, then the word &amp;quot;monad&amp;quot; induces mental paralysis.&amp;#160; Perhaps, this is why some have begun inventing more benign names for monads.&lt;/p&gt;  &lt;p&gt;These days, monads are the celebrities of programming language theory.&amp;#160; They gloss the cover of blogs and have been &lt;a href="http://www.loria.fr/~kow/monads/index.html"&gt;compared to everything&lt;/a&gt; from &lt;a href="http://www.haskell.org/haskellwiki/Monads_as_containers"&gt;boxes of fruit&lt;/a&gt; to &lt;a href="http://www.haskell.org/haskellwiki/Meet_Bob_The_Monadic_Lover"&gt;love affairs&lt;/a&gt;.&amp;#160; Nerds everywhere are exclaiming that the experience of understanding monads causes a &lt;a href="http://groups.google.com/group/comp.lang.functional/browse_thread/thread/346dd6767ecb3851"&gt;pleasantly&lt;/a&gt; &lt;a href="http://koweycode.blogspot.com/2006/08/does-haskell-hurt-your-brain.html"&gt;painful&lt;/a&gt; &lt;a href="http://lambda-the-ultimate.org/node/724"&gt;mental sensation&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Like continuations, monads are simpler than they sound and are very useful in many situations.&amp;#160; In fact, programmers write code in a variety of languages that implicitly use common monads without even breaking a sweat.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lambda-the-ultimate.org/node/92"&gt;With&lt;/a&gt; &lt;a href="http://james-iry.blogspot.com/2007/09/monads-are-elephants-part-1.html"&gt;all&lt;/a&gt; &lt;a href="http://www.haskell.org/tutorial/monads.html"&gt;of&lt;/a&gt; &lt;a href="http://moonbase.rydia.net/mental/writings/programming/monads-in-ruby/00introduction.html"&gt;the&lt;/a&gt; &lt;a href="http://sigfpe.blogspot.com/2006/08/you-could-have-invented-monads-and.html"&gt;attention&lt;/a&gt; &lt;a href="http://www.alpheccar.org/en/posts/show/60"&gt;that&lt;/a&gt; &lt;a href="http://patryshev.com/monad/m-intro.html"&gt;monads&lt;/a&gt; &lt;a href="http://www.engr.mun.ca/~theo/Misc/haskell_and_monads.htm"&gt;get&lt;/a&gt;, why am I writing yet another explanation of monads?&amp;#160; Not to compare them to some everyday occurrence or to chronicle my journey to understanding.&amp;#160; I explain monads because I need monads.&amp;#160; They elegantly solve programming problems in a number of languages and contexts.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Introducing Monads&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Monad_%28category_theory%29"&gt;Monads&lt;/a&gt; come from &lt;a href="http://en.wikipedia.org/wiki/Category_theory"&gt;category theory&lt;/a&gt;.&amp;#160; &lt;a href="http://www.disi.unige.it/person/MoggiE/ftp/ic91.pdf"&gt;Moggi introduced them&lt;/a&gt; to computer scientists to aid in the analysis of the semantics of computations.&amp;#160; In an excellent paper, &lt;em&gt;&lt;a href="http://coblitz.codeen.org:3125/citeseer.ist.psu.edu/cache/papers/cs/4583/http:zSzzSzcm.bell-labs.comzSzwhozSzwadlerzSztopicszSz..zSzpaperszSzessencezSzessence.pdf/wadler92essence.pdf"&gt;The Essence of Functional Programming&lt;/a&gt;&lt;/em&gt;, Wadler showed that monads are generally useful in computer programs to compose together functions which operate on amplified values rather than values.&amp;#160; &lt;a href="http://en.wikipedia.org/wiki/Monads_in_functional_programming"&gt;Monads&lt;/a&gt; became &lt;a href="http://www.haskell.org/all_about_monads/html/index.html"&gt;an important part&lt;/a&gt; of the programming language Haskell where they tackle &lt;a href="http://research.microsoft.com/~simonpj/papers/marktoberdorf/mark.pdf"&gt;the awkward squad&lt;/a&gt;: IO, concurrency, exceptions, and foreign-function calls.&lt;/p&gt;  &lt;p&gt;Monads enjoy tremendous success in Haskell, but like an actor who does well in a particular role, monads are now stereotyped in the minds of most programmers as useful only in pure lazy functional languages.&amp;#160; This is unfortunate, because monads are more broadly applicable.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Controlling Complexity&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Composition is the key to controlling complexity in software.&amp;#160; In &lt;em&gt;&lt;a href="http://mitpress.mit.edu/sicp/"&gt;The Structure and Interpretation of Computer Programs&lt;/a&gt;&lt;/em&gt;, Abelson and Sussman argue that composition beautifully expresses complex systems from simple patterns.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;In our study of program design, we have seen that expert programmers control the complexity of their designs with the same general techniques used by designers of all complex systems. They combine primitive elements to form compound objects, they abstract compound objects to form higher-level building blocks, and they preserve modularity by adopting appropriate large-scale views of system structure.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;One form of composition, function composition, succinctly describes the dependencies between function calls.&amp;#160; Function composition takes two functions and plumbs the result from the second function into the input of the first function, thereby forming one function.&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, V&amp;gt; Compose&amp;lt;T, U, V&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;U, V&amp;gt; f, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, U&amp;gt; g)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;x =&amp;gt; f(g(x));
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;For example, instead of applying &lt;em&gt;g&lt;/em&gt; to the value &lt;em&gt;x&lt;/em&gt; and then applying &lt;em&gt;f&lt;/em&gt; to the result, compose &lt;em&gt;f&lt;/em&gt; with &lt;em&gt;g&lt;/em&gt; and then apply the result to the value &lt;em&gt;x&lt;/em&gt;.&amp;#160; The key difference is the abstraction of the dependency between &lt;em&gt;f&lt;/em&gt; and &lt;em&gt;g&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;r = f(g(x));         &lt;font color="#008000"&gt;// without function composition&lt;/font&gt;
&lt;span style="color: blue"&gt;var &lt;/span&gt;r = f.Compose(g)(x); &lt;font color="#008000"&gt;// with function composition&lt;/font&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Given the function &lt;em&gt;Identity&lt;/em&gt;, function composition must obey three laws.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;T Identity&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;T value)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;value;
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;1.&amp;#160; Left identity&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Identity.Compose(f) = f&lt;/p&gt;

  &lt;p&gt;&lt;strong&gt;2.&amp;#160; Right identity&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; f.Compose(Identity) = f&lt;/p&gt;

  &lt;p&gt;&lt;strong&gt;3.&amp;#160; Associative&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; f.Compose(g.Compose(h)) = (f.Compose(g)).Compose(h)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Often, values are not enough.&amp;#160; Constructed types amplify values.&amp;#160; The type &lt;em&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/em&gt; represents a lazily computed list of values of type &lt;em&gt;T&lt;/em&gt;.&amp;#160; The type &lt;em&gt;Nullable&amp;lt;T&amp;gt;&lt;/em&gt; represents a possibly missing value of type &lt;em&gt;T&lt;/em&gt;.&amp;#160; The type &lt;em&gt;Func&amp;lt;Func&amp;lt;T, Answer&amp;gt;, Answer&amp;gt; &lt;/em&gt;represents a function, which returns an &lt;em&gt;Answer&lt;/em&gt; given a continuation, which takes a &lt;em&gt;T &lt;/em&gt;and returns an &lt;em&gt;Answer&lt;/em&gt;.&amp;#160; Each of these types amplifies the type &lt;em&gt;T&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Suppose that instead of composing functions which return values, we compose functions which take values and return amplified values.&amp;#160; Let &lt;em&gt;M&amp;lt;T&amp;gt;&lt;/em&gt; denote the type of the amplified values.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: #2b91af"&gt;M&lt;/span&gt;&amp;lt;V&amp;gt;&amp;gt; Compose&amp;lt;T, U, V&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;U, &lt;span style="color: #2b91af"&gt;M&lt;/span&gt;&amp;lt;V&amp;gt;&amp;gt; f, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: #2b91af"&gt;M&lt;/span&gt;&amp;lt;U&amp;gt;&amp;gt; g)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;x =&amp;gt; f(g(x)); &lt;span style="color: green"&gt;// error, g(x) returns M&amp;lt;U&amp;gt; and f takes U
&lt;/span&gt;}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Function composition fails, because the return and input types do not match.&amp;#160; Composition with amplified values requires a function that accesses the underlying value and feeds it to the next function.&amp;#160; Call that function &amp;quot;Bind&amp;quot; and use it to define function composition.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: #2b91af"&gt;M&lt;/span&gt;&amp;lt;V&amp;gt;&amp;gt; Compose&amp;lt;T, U, V&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;U, &lt;span style="color: #2b91af"&gt;M&lt;/span&gt;&amp;lt;V&amp;gt;&amp;gt; f, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: #2b91af"&gt;M&lt;/span&gt;&amp;lt;U&amp;gt;&amp;gt; g)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;x =&amp;gt; Bind(g(x), f);
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The input and output types determine the signature of &lt;em&gt;Bind.&lt;/em&gt;&amp;#160; Therefore, &lt;em&gt;Bind&lt;/em&gt; takes an amplified value, M&amp;lt;&lt;em&gt;U&amp;gt;,&lt;/em&gt; and a function from &lt;em&gt;U&lt;/em&gt;&amp;#160; to &lt;em&gt;M&amp;lt;V&amp;gt;&lt;/em&gt;, and returns an amplified value, &lt;em&gt;M&amp;lt;V&amp;gt;&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;M&lt;/span&gt;&amp;lt;V&amp;gt; Bind&amp;lt;U, V&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;M&lt;/span&gt;&amp;lt;U&amp;gt; m, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;U, &lt;span style="color: #2b91af"&gt;M&lt;/span&gt;&amp;lt;V&amp;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;The body of &lt;em&gt;Bind&lt;/em&gt; depends on the mechanics of the amplified values, &lt;em&gt;M&amp;lt;T&amp;gt;&lt;/em&gt;.&amp;#160; Each amplified type will need a distinct definition of &lt;em&gt;Bind&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In addition to &lt;em&gt;Bind&lt;/em&gt;, define a function which takes an unamplified value and amplifies it.&amp;#160; Call this function &amp;quot;Unit&amp;quot;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;M&lt;/span&gt;&amp;lt;T&amp;gt; Unit&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;T value)&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Together the amplified type, &lt;em&gt;M&amp;lt;T&amp;gt;&lt;/em&gt;, the function &lt;em&gt;Bind&lt;/em&gt;, and the function &lt;em&gt;Unit&lt;/em&gt; enable function composition with amplified values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Meet the Monads&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Viola, we have invented monads.&lt;/p&gt;

&lt;p&gt;Monads are a triple consisting of a type, a &lt;em&gt;Unit&lt;/em&gt; function, and a &lt;em&gt;Bind&lt;/em&gt; function.&amp;#160; Furthermore, to be a monad, the triple must satisfy three laws:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;1.&amp;#160; Left Identity&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Bind(Unit(e), k) = k(e)&lt;/p&gt;

  &lt;p&gt;&lt;strong&gt;2.&amp;#160; Right Identity&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Bind(m, Unit) = m&lt;/p&gt;

  &lt;p&gt;&lt;strong&gt;3.&amp;#160; Associative&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Bind(m, x =&amp;gt; Bind(k(x), y =&amp;gt; h(y)) = Bind(Bind(m, x =&amp;gt; k(x)), y =&amp;gt; h(y))&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The laws are similar to those of function composition.&amp;#160; This is not a coincidence.&amp;#160; They guarantee that the monad is well behaved and composition works properly.&lt;/p&gt;

&lt;p&gt;To define a particular monad, the writer supplies the triple, thereby specifying the mechanics of the amplified values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Identity Monad&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The simplest monad is the &lt;em&gt;Identity&lt;/em&gt; monad.&amp;#160; The type represents a wrapper containing a value.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Identity&lt;/span&gt;&amp;lt;T&amp;gt;
{
    &lt;span style="color: blue"&gt;public &lt;/span&gt;T Value { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;private set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;Identity(T value) { &lt;span style="color: blue"&gt;this&lt;/span&gt;.Value = 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;The &lt;em&gt;Unit&lt;/em&gt; function takes a value and returns a new instance of &lt;em&gt;Identity&lt;/em&gt;, which wraps the value.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Identity&lt;/span&gt;&amp;lt;T&amp;gt; Unit&amp;lt;T&amp;gt;(T value)
{
    &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Identity&lt;/span&gt;&amp;lt;T&amp;gt;(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;The bind function takes an instance of &lt;em&gt;Identity&lt;/em&gt;, unwraps the value, and invokes the delegate, &lt;em&gt;k&lt;/em&gt;, with the&lt;em&gt; &lt;/em&gt;unwrapped value.&amp;#160; The result is a new instance of &lt;em&gt;Identity&lt;/em&gt;. &lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Identity&lt;/span&gt;&amp;lt;U&amp;gt; Bind&amp;lt;T,U&amp;gt;(&lt;span style="color: #2b91af"&gt;Identity&lt;/span&gt;&amp;lt;T&amp;gt; id, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T,&lt;span style="color: #2b91af"&gt;Identity&lt;/span&gt;&amp;lt;U&amp;gt;&amp;gt; k)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;k(id.Value);
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Consider a simple program that creates two &lt;em&gt;Identity &lt;/em&gt;wrappers and performs an operation on the wrapped values.&amp;#160; First, bind &lt;em&gt;x&lt;/em&gt; to the value within the wrapper containing the value five.&amp;#160; Then, bind &lt;em&gt;y &lt;/em&gt;to the value within the wrapper containing the value six.&amp;#160; Finally, add the values,&lt;em&gt; x&lt;/em&gt; and &lt;em&gt;y,&lt;/em&gt; together.&amp;#160; The result is an instance of &lt;em&gt;Identity&lt;/em&gt; wrapping the value eleven.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;font face="Courier New"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;r = Bind(Unit(5), x =&amp;gt; 

      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Bind(Unit(6), y =&amp;gt; 

      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Unit(x + y))); 

      &lt;br /&gt;

      &lt;br /&gt;&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(r.Value);&lt;/font&gt;&lt;/p&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;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;While this works, it is rather clumsy.&amp;#160; It would be nice to have syntax for dealing with the monad.&amp;#160; Fortunately, we do.&lt;/p&gt;

&lt;p&gt;C# 3.0 introduced query comprehensions which are actually monad comprehensions in disguise.&amp;#160; We can rewrite the identity monad to use LINQ.&amp;#160; Perhaps, it should have been called LINM (Language INtegrated Monads), but it just doesn't have the same ring to it.&lt;/p&gt;

&lt;p&gt;Rename the method &lt;em&gt;Unit&lt;/em&gt; to &lt;em&gt;ToIdentity &lt;/em&gt;and &lt;em&gt;Bind&lt;/em&gt; to &lt;em&gt;SelectMany&lt;/em&gt;.&amp;#160; Then, make them both extension methods.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Identity&lt;/span&gt;&amp;lt;T&amp;gt; ToIdentity&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;T value)
{
    &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Identity&lt;/span&gt;&amp;lt;T&amp;gt;(value);
}

&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Identity&lt;/span&gt;&amp;lt;U&amp;gt; SelectMany&amp;lt;T, U&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Identity&lt;/span&gt;&amp;lt;T&amp;gt; id, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: #2b91af"&gt;Identity&lt;/span&gt;&amp;lt;U&amp;gt;&amp;gt; k)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;k(id.Value);
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The changes impact the calling code.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;r = 5.ToIdentity().SelectMany(
            x =&amp;gt; 6.ToIdentity().SelectMany(
                y =&amp;gt; (x + y).ToIdentity()));

&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(r.Value);&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Equivalent methods are part of the standard query operators defined for LINQ.&amp;#160; However, the standard query operators also include a slightly different version of &lt;em&gt;SelectMany&lt;/em&gt; for performance reasons.&amp;#160; It combines &lt;em&gt;Bind&lt;/em&gt; with &lt;em&gt;Unit&lt;/em&gt;, so that lambdas are not deeply nested.&amp;#160; The signature is the same except for an extra argument that is a delegate which takes two arguments and returns a value.&amp;#160; The delegate combines the two values together.&amp;#160; This version of &lt;em&gt;SelectMany&lt;/em&gt; binds &lt;em&gt;x &lt;/em&gt;to the wrapped value, applies &lt;em&gt;k&lt;/em&gt; to &lt;em&gt;x&lt;/em&gt;, binds the result to &lt;em&gt;y&lt;/em&gt;, and then applies the combining function, &lt;em&gt;s,&lt;/em&gt; to &lt;em&gt;x&lt;/em&gt; and &lt;em&gt;y.&amp;#160; &lt;/em&gt;The resultant value is wrapped and returned.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Identity&lt;/span&gt;&amp;lt;V&amp;gt; SelectMany&amp;lt;T, U, V&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Identity&lt;/span&gt;&amp;lt;T&amp;gt; id, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: #2b91af"&gt;Identity&lt;/span&gt;&amp;lt;U&amp;gt;&amp;gt; k, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T,U,V&amp;gt; s)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;id.SelectMany(x =&amp;gt; k(x).SelectMany(y =&amp;gt; s(x, y).ToIdentity()));
}&lt;/pre&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;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Of course, we can remove some of the code from the generalized solution by using our knowledge of the &lt;em&gt;Identity&lt;/em&gt; monad.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Identity&lt;/span&gt;&amp;lt;V&amp;gt; SelectMany&amp;lt;T, U, V&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Identity&lt;/span&gt;&amp;lt;T&amp;gt; id, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: #2b91af"&gt;Identity&lt;/span&gt;&amp;lt;U&amp;gt;&amp;gt; k, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T,U,V&amp;gt; s)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;s(id.Value, k(id.Value).Value).ToIdentity();
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The call-site does not need to nest lambdas.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;r = 5.ToIdentity()
         .SelectMany(x =&amp;gt; 6.ToIdentity(), (x, y) =&amp;gt; x + y);

&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(r.Value);&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;With the new definition of &lt;em&gt;SelectMany&lt;/em&gt;, programmers can use C#'s query comprehension syntax.&amp;#160; The &lt;em&gt;from &lt;/em&gt;notation binds the introduced variable to the value wrapped by the expression on the right.&amp;#160; This allows subsequent expressions to use the wrapped values without directly calling &lt;em&gt;SelectMany&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;r = &lt;span style="color: blue"&gt;from &lt;/span&gt;x &lt;span style="color: blue"&gt;in &lt;/span&gt;5.ToIdentity()
        &lt;span style="color: blue"&gt;from &lt;/span&gt;y &lt;span style="color: blue"&gt;in &lt;/span&gt;6.ToIdentity()
        &lt;span style="color: blue"&gt;select &lt;/span&gt;x + y;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Since the original &lt;em&gt;SelectMany&lt;/em&gt; definition corresponds directly to the monadic &lt;em&gt;Bind&lt;/em&gt; function and because the existence of a generalized transformation has been demonstrated, the remainder of the post will use the original signature.&amp;#160; But, keep in mind that the second definition is the one used by the query syntax.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Maybe Monad&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;Identity&lt;/em&gt; monad is an example of a monadic container type where the &lt;em&gt;Identity&lt;/em&gt; monad wrapped a value.&amp;#160; If we change the definition to contain either a value or a missing value then we have the &lt;em&gt;Maybe&lt;/em&gt; monad.&lt;/p&gt;

&lt;p&gt;Again, we need a type definition.&amp;#160; The &lt;em&gt;Maybe&lt;/em&gt; type is similar to the &lt;em&gt;Identity&lt;/em&gt; type but adds a property denoting whether a value is missing.&amp;#160; It also has a predefined instance, &lt;em&gt;Nothing&lt;/em&gt;, representing all instances lacking a value.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Maybe&lt;/span&gt;&amp;lt;T&amp;gt;
{
    &lt;span style="color: blue"&gt;public readonly static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Maybe&lt;/span&gt;&amp;lt;T&amp;gt; Nothing = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Maybe&lt;/span&gt;&amp;lt;T&amp;gt;();
    &lt;span style="color: blue"&gt;public &lt;/span&gt;T Value { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;private set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public bool &lt;/span&gt;HasValue { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;private set&lt;/span&gt;; }
    Maybe()
    {
        HasValue = &lt;span style="color: blue"&gt;false&lt;/span&gt;;
    }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;Maybe(T value)
    {
        Value = value;
        HasValue = &lt;span style="color: blue"&gt;true&lt;/span&gt;;
    }
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;em&gt;Unit&lt;/em&gt; function takes a value and constructs a &lt;em&gt;Maybe&lt;/em&gt; instance, which wraps the value.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Maybe&lt;/span&gt;&amp;lt;T&amp;gt; ToMaybe&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;T value)
{
    &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Maybe&lt;/span&gt;&amp;lt;T&amp;gt;(value);
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The &lt;em&gt;Bind&lt;/em&gt; function takes a &lt;em&gt;Maybe&lt;/em&gt; instance and if there is a value then it applies the delegate to the contained value.&amp;#160; Otherwise, it returns &lt;em&gt;Nothing&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Maybe&lt;/span&gt;&amp;lt;U&amp;gt; SelectMany&amp;lt;T, U&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Maybe&lt;/span&gt;&amp;lt;T&amp;gt; m, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: #2b91af"&gt;Maybe&lt;/span&gt;&amp;lt;U&amp;gt;&amp;gt; k)
{
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(!m.HasValue)
        &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Maybe&lt;/span&gt;&amp;lt;U&amp;gt;.Nothing;
    &lt;span style="color: blue"&gt;return &lt;/span&gt;k(m.Value);
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The programmer can use the comprehension syntax to work with the &lt;em&gt;Maybe&lt;/em&gt; monad.&amp;#160; For example, create an instance of &lt;em&gt;Maybe&lt;/em&gt; containing the value five and add it to &lt;em&gt;Nothing&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;r = &lt;span style="color: blue"&gt;from &lt;/span&gt;x &lt;span style="color: blue"&gt;in &lt;/span&gt;5.ToMaybe()
        &lt;span style="color: blue"&gt;from &lt;/span&gt;y &lt;span style="color: blue"&gt;in &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Maybe&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;.Nothing
        &lt;span style="color: blue"&gt;select &lt;/span&gt;x + y;

&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(r.HasValue ? r.Value.ToString() : &lt;span style="color: #a31515"&gt;&amp;quot;Nothing&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;The result is &amp;quot;Nothing&amp;quot;.&amp;#160; We have implemented the null propagation of nullables without explicit language support.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The List Monad&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Another important container type is the list type.&amp;#160; In fact, the list monad is at the heart of LINQ.&amp;#160; The type &lt;em&gt;IEnumerable&amp;lt;T&amp;gt; &lt;/em&gt;denotes a lazily computed list.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;Unit&lt;/em&gt; function takes a value and returns a list, which contains only that value.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; ToList&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;T value)
{
    &lt;span style="color: blue"&gt;yield return &lt;/span&gt;value;
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The &lt;em&gt;Bind&lt;/em&gt; function takes an &lt;em&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/em&gt;, a delegate, which takes a &lt;em&gt;T&lt;/em&gt; and returns an &lt;em&gt;IEnumerable&amp;lt;U&amp;gt;&lt;/em&gt;, and returns an &lt;em&gt;IEnumerable&amp;lt;U&amp;gt;&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;U&amp;gt; SelectMany&amp;lt;T, U&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; m, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;U&amp;gt;&amp;gt; k)
{
    &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;x &lt;span style="color: blue"&gt;in &lt;/span&gt;m)
        &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;y &lt;span style="color: blue"&gt;in &lt;/span&gt;k(x))
            &lt;span style="color: blue"&gt;yield return &lt;/span&gt;y;
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now, the programmer can write the familiar query expressions with &lt;em&gt;IEnumerable&amp;lt;T&amp;gt;.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;r = &lt;span style="color: blue"&gt;from &lt;/span&gt;x &lt;span style="color: blue"&gt;in new&lt;/span&gt;[] { 0, 1, 2 }
        &lt;span style="color: blue"&gt;from &lt;/span&gt;y &lt;span style="color: blue"&gt;in new&lt;/span&gt;[] { 0, 1, 2 }
        &lt;span style="color: blue"&gt;select &lt;/span&gt;x + y;

&lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;i &lt;span style="color: blue"&gt;in &lt;/span&gt;r)
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(i);&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Remember that it is the monad that enables the magic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Continuation Monad&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The continuation monad answers &lt;a href="http://blogs.msdn.com/wesdyer/archive/2007/12/22/continuation-passing-style.aspx"&gt;the question that was posed at the end of the last post&lt;/a&gt;: how can a programmer write CPS code in a more palatable way?&lt;/p&gt;

&lt;p&gt;The type of the continuation monad, &lt;em&gt;K&lt;/em&gt;, is a delegate which when given a continuation, which takes an argument and returns an answer, will return an answer.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;delegate &lt;/span&gt;Answer &lt;span style="color: #2b91af"&gt;K&lt;/span&gt;&amp;lt;T,Answer&amp;gt;(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T,Answer&amp;gt; k);&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;The type &lt;em&gt;K&lt;/em&gt; fundamentally differs from types &lt;em&gt;Identity&amp;lt;T&amp;gt;&lt;/em&gt;, &lt;em&gt;Maybe&amp;lt;T&amp;gt;&lt;/em&gt;, and &lt;em&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/em&gt;.&amp;#160; All the other monads represent container types and allow computations to be specified in terms of the values rather than the containers, but the continuation monad contains nothing.&amp;#160; Rather, it composes together continuations the user writes.&lt;/p&gt;

&lt;p&gt;To be a monad, there must be a &lt;em&gt;Unit&lt;/em&gt; function which takes a &lt;em&gt;T &lt;/em&gt;and returns a &lt;em&gt;K&amp;lt;T&lt;/em&gt;,&lt;em&gt;Answer&amp;gt;&lt;/em&gt; for some answer type.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;K&lt;/span&gt;&amp;lt;T, Answer&amp;gt; ToContinuation&amp;lt;T, Answer&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;T value)&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;What should it do?&amp;#160; When in doubt, look to the types.&amp;#160;&amp;#160; The method takes a &lt;em&gt;T&lt;/em&gt; and returns a function, which takes a function from &lt;em&gt;T&lt;/em&gt; to &lt;em&gt;Answer&lt;/em&gt;, and returns an &lt;em&gt;Answer&lt;/em&gt;.&amp;#160; Therefore, the method must return a function and the only argument of that function must be a function from &lt;em&gt;T&lt;/em&gt; to &lt;em&gt;Answer&lt;/em&gt;.&amp;#160; Call the argument &lt;em&gt;c&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;return &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, Answer&amp;gt; c) =&amp;gt; ...&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The body of the lambda must return a value of type &lt;em&gt;Answer&lt;/em&gt;.&amp;#160; Values of type &lt;em&gt;Func&amp;lt;T,Answer&amp;gt;&lt;/em&gt; and a &lt;em&gt;T&lt;/em&gt; are&lt;em&gt;&amp;#160;&lt;/em&gt;available.&amp;#160; Apply &lt;em&gt;c&lt;/em&gt; to &lt;em&gt;value&lt;/em&gt; and the result is of type &lt;em&gt;Answer.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;return &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, Answer&amp;gt; c) =&amp;gt; c(value);&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;To be a monad, &lt;em&gt;Bind&lt;/em&gt; must take a &lt;em&gt;K&amp;lt;T,Answer&amp;gt;&lt;/em&gt; and a function from &lt;em&gt;T &lt;/em&gt;to &lt;em&gt;K&amp;lt;U, Answer&amp;gt;&lt;/em&gt; and return a &lt;em&gt;K&amp;lt;U, Answer&amp;gt;&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;K&lt;/span&gt;&amp;lt;U, Answer&amp;gt; SelectMany&amp;lt;T, U, Answer&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;K&lt;/span&gt;&amp;lt;T, Answer&amp;gt; m, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: #2b91af"&gt;K&lt;/span&gt;&amp;lt;U, Answer&amp;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;But what about the body?&amp;#160; The result must be of type &lt;em&gt;K&amp;lt;U, Answer&amp;gt;&lt;/em&gt;, but how is a result of the correct type formed?&lt;/p&gt;

&lt;p&gt;Expand &lt;em&gt;K&lt;/em&gt;'s definition to gain some insight.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;return type&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/strong&gt;&lt;font face="Courier New"&gt;&lt;span style="color: blue"&gt;&lt;font color="#008080"&gt;Func&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;font color="#008080"&gt;Func&lt;/font&gt;&amp;lt;U, Answer&amp;gt;, Answer&amp;gt;&lt;/font&gt;&lt;/p&gt;

  &lt;p&gt;&lt;strong&gt;&lt;em&gt;m&lt;/em&gt;'s type&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/strong&gt;&lt;font face="Courier New"&gt;&lt;span style="color: blue"&gt;&lt;font color="#008080"&gt;Func&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;font color="#008080"&gt;Func&lt;/font&gt;&amp;lt;T, Answer&amp;gt;, Answer&amp;gt;&lt;/font&gt;&lt;/p&gt;

  &lt;p&gt;&lt;strong&gt;&lt;em&gt;k&lt;/em&gt;'s type&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/strong&gt;&lt;font face="Courier New"&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;font color="#2b91af"&gt;Func&lt;/font&gt;&amp;lt;&lt;font color="#008080"&gt;Func&lt;/font&gt;&amp;lt;U, Answer&amp;gt;, Answer&amp;gt;&amp;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;Applying &lt;em&gt;k&lt;/em&gt; to a value of type &lt;em&gt;T&lt;/em&gt; results in a value of type &lt;em&gt;K&amp;lt;U,Answer&amp;gt;&lt;/em&gt;, but no value of type &lt;em&gt;T&lt;/em&gt; is available.&amp;#160; Build the return type directly by constructing a function, which takes a function from &lt;em&gt;U&lt;/em&gt; to &lt;em&gt;Answer&lt;/em&gt;.&amp;#160; Call the parameter &lt;em&gt;c&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;return &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;U,Answer&amp;gt; c) =&amp;gt; ...&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The body must be type of &lt;em&gt;Answer &lt;/em&gt;so that the return type of &lt;em&gt;Bind&lt;/em&gt; is &lt;em&gt;K&amp;lt;U,Answer&amp;gt;&lt;/em&gt;.&amp;#160; Perhaps, &lt;em&gt;m&lt;/em&gt; could be applied to a function from &lt;em&gt;T&lt;/em&gt; to &lt;em&gt;Answer&lt;/em&gt;.&amp;#160; The result is a value of type &lt;em&gt;Answer&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;return &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;U,Answer&amp;gt; c) =&amp;gt; m(...)&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The expression inside the invocation of &lt;em&gt;m&lt;/em&gt; must be of type &lt;em&gt;Func&amp;lt;T,Answer&amp;gt;&lt;/em&gt;.&amp;#160; Since there is nothing of that type, construct the function by creating a lambda with one parameter, &lt;em&gt;x&lt;/em&gt;, of type &lt;em&gt;T.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;return &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;U,Answer&amp;gt; c) =&amp;gt; m((T x) =&amp;gt; ...)&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The body of this lambda must be of type &lt;em&gt;Answer&lt;/em&gt;.&amp;#160; Values of type &lt;em&gt;T, &lt;/em&gt;&lt;em&gt;Func&amp;lt;U,Answer&amp;gt;, &lt;/em&gt;and &lt;em&gt;Func&amp;lt;T,Func&amp;lt;Func&amp;lt;U,Answer&amp;gt;, Answer&amp;gt;&amp;gt;&lt;/em&gt; haven't been used yet.&amp;#160; Apply &lt;em&gt;k&lt;/em&gt; to &lt;em&gt;x&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;return &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;U,Answer&amp;gt; c) =&amp;gt; m((T x) =&amp;gt; k(x)...)&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The result is a value of type &lt;em&gt;Func&amp;lt;Func&amp;lt;U,Answer&amp;gt;,Answer&amp;gt;&lt;/em&gt;.&amp;#160; Apply the result to &lt;em&gt;c&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;return &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;U,Answer&amp;gt; c) =&amp;gt; m((T x) =&amp;gt; k(x)(c));&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The continuation monad turns the computation inside out.&amp;#160; The comprehension syntax can be used to construct continuations.&lt;/p&gt;

&lt;p&gt;Construct a computation, which invokes a continuation with the value seven.&amp;#160; Pass this computation to another computation, which invokes a continuation with the value six.&amp;#160;&amp;#160; Pass this computation to another computation, which invokes a continuation with the result of adding the results of the first two continuations together.&amp;#160; Finally, pass a continuation, which replaces &amp;quot;1&amp;quot;s with &amp;quot;a&amp;quot;s, to the result.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;r = &lt;span style="color: blue"&gt;from &lt;/span&gt;x &lt;span style="color: blue"&gt;in &lt;/span&gt;7.ToContinuation&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;,&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;()
        &lt;span style="color: blue"&gt;from &lt;/span&gt;y &lt;span style="color: blue"&gt;in &lt;/span&gt;6.ToContinuation&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;,&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;()
        &lt;span style="color: blue"&gt;select &lt;/span&gt;x + y;

&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(r(z =&amp;gt; z.ToString().Replace(&lt;span style="color: #a31515"&gt;'1'&lt;/span&gt;, &lt;span style="color: #a31515"&gt;'a'&lt;/span&gt;))); &lt;font color="#008000"&gt;// displays a3&lt;/font&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;The continuation monad does the heavy-lifting of constructing the continuations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Monadic Magic&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Beautiful composition of amplified values requires monads.&amp;#160; The &lt;em&gt;Identity&lt;/em&gt;, &lt;em&gt;Maybe&lt;/em&gt;, and &lt;em&gt;IEnumerable&lt;/em&gt; monads demonstrate the power of monads as container types.&amp;#160; The continuation monad, &lt;em&gt;K&lt;/em&gt;, shows how monads can readily express complex computation.&lt;/p&gt;

&lt;p&gt;Stay tuned for more with monads.&amp;#160; Until then, see what monads can do for you.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7064392" width="1" height="1"&gt;</description><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/Theory/default.aspx">Theory</category></item></channel></rss>