<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>My Year of Learning Functional Programming With Haskell</title>
	<atom:link href="http://haskell2000.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://haskell2000.wordpress.com</link>
	<description></description>
	<lastBuildDate>Tue, 25 Oct 2011 13:46:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='haskell2000.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>My Year of Learning Functional Programming With Haskell</title>
		<link>http://haskell2000.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://haskell2000.wordpress.com/osd.xml" title="My Year of Learning Functional Programming With Haskell" />
	<atom:link rel='hub' href='http://haskell2000.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Of Love and Monads</title>
		<link>http://haskell2000.wordpress.com/2011/10/18/monads-compared-to-love/</link>
		<comments>http://haskell2000.wordpress.com/2011/10/18/monads-compared-to-love/#comments</comments>
		<pubDate>Tue, 18 Oct 2011 01:06:37 +0000</pubDate>
		<dc:creator>gongli2000</dc:creator>
				<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Monads]]></category>

		<guid isPermaLink="false">http://haskell2000.wordpress.com/?p=556</guid>
		<description><![CDATA[I didn&#8217;t mean to entirely dismiss the question of what Monads are. My objection is that some Monad tutorials launch &#8230;<p><a href="http://haskell2000.wordpress.com/2011/10/18/monads-compared-to-love/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskell2000.wordpress.com&amp;blog=10658211&amp;post=556&amp;subd=haskell2000&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I didn&#8217;t mean to entirely dismiss the question of what Monads are. My objection is that some Monad tutorials launch into vague, abstract definitions and analogies before the reader has had the chance to see an example of a Monad.</p>
<p>To make an analogy of my own, (or is it a simile?) , trying to define Monads to someone who had never seen or used them is like  trying to define &#8220;Love&#8221; to someone who had never experienced that sublime emotion. Your definition could only give the vaguest, the most simplistic, the will-o-the wispest idea of what love is and your poor, lonely reader would be as confused as the readers of most Monads tutorials. No, the best way to explain Love to a such a person is to have her experience it for herself. Armed with that experience she would be able to create her own definition. She would no longer need you to provide one. </p>
<p>And so it is with Monads and so it is with this blog. I want to give you the experience of Monadic bliss to allow you to come up with your own definition. But, after having had that experience, you may find that an explicit definition is no longer necessary &#8211; that concrete experience is better than an abstract definition.  A definition provides cold comfort on a dark stormy night, but an experience! &#8212; that&#8217;s something that you can cuddle up with and that will keep you warm when it is a cold rainy night in your soul.</p>
<p>Perhaps you will be moved to write your own Monad tutorial or better yet you might try to get out more on Saturday nights and experience a little love yourself.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/haskell2000.wordpress.com/556/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/haskell2000.wordpress.com/556/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/haskell2000.wordpress.com/556/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/haskell2000.wordpress.com/556/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/haskell2000.wordpress.com/556/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/haskell2000.wordpress.com/556/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/haskell2000.wordpress.com/556/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/haskell2000.wordpress.com/556/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/haskell2000.wordpress.com/556/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/haskell2000.wordpress.com/556/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/haskell2000.wordpress.com/556/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/haskell2000.wordpress.com/556/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/haskell2000.wordpress.com/556/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/haskell2000.wordpress.com/556/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskell2000.wordpress.com&amp;blog=10658211&amp;post=556&amp;subd=haskell2000&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://haskell2000.wordpress.com/2011/10/18/monads-compared-to-love/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c513f579abbd7d806fa0fdaae68c212b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gongli2000</media:title>
		</media:content>
	</item>
		<item>
		<title>Monads By Example: Exception Handling</title>
		<link>http://haskell2000.wordpress.com/2011/09/23/wadlers-mondad-examples/</link>
		<comments>http://haskell2000.wordpress.com/2011/09/23/wadlers-mondad-examples/#comments</comments>
		<pubDate>Fri, 23 Sep 2011 15:33:23 +0000</pubDate>
		<dc:creator>gongli2000</dc:creator>
				<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Monads]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[wadler]]></category>

		<guid isPermaLink="false">http://haskell2000.wordpress.com/?p=458</guid>
		<description><![CDATA[What are Monads? Who cares? Only mathematicians and theoretical computer scientists need to concern themselves with that question. Too many &#8230;<p><a href="http://haskell2000.wordpress.com/2011/09/23/wadlers-mondad-examples/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskell2000.wordpress.com&amp;blog=10658211&amp;post=458&amp;subd=haskell2000&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p> What are Monads? Who cares? Only mathematicians and theoretical computer scientists need to concern themselves with that question. Too many words have been written in a futile attempt to &#8220;explain&#8221; Monads to programmers. The correct questions for a working programmer to ask are: What are Monads good for and how can I use Monads in my own code? And the way to answer these question is to give examples &#8211; lots of examples. Wadler&#8217;s paper on <em> Monads for functional programming </em> explains Monads via examples. The problem is you can&#8217;t actually run those examples in Haskell and the organization and lack of details leave something to be desired  so I&#8217;m going to redo his examples , adding more details and reorganizing the presentation in order to make the explanations more clear .</p>
<p>His first example is exception handling. Since you&#8217;re reading about monads I assume you understand exception handling and can figure out what this code is doing.</p>
<pre>
data Term = Con Float | (Div Term Term) deriving (Show)
data M a = Raise Exception | Return a deriving (Show)
type Exception = String

eval (Con a) = Return a
eval (Div t u) =
  case (eval t) of
    Raise e -&gt; Raise e
    Return a -&gt;
      case (eval u ) of
        Raise e -&gt; Raise e
        Return b -&gt;
          if b == 0
            then Raise "divide by zero"
            else Return (a/b)
</pre>
<p>Using Monads we will be able to write the definition of <code> eval </code> as            </p>
<pre>
eval (Con a) = Return a
eval (Div t u) =
  do
    a &lt;- eval t
    b &lt;- eval u
    if b == 0
      then Raise &quot;divide by zero&quot;
      else return (a/b)
</pre>
<p>This example gives a first cut answer to the question: What Monads are good for? The second version of the code is arguably more succinct, more modular and more extensible. The ugly sequence of nested  case statements has been transformed into a nice sequence of what are apparently &#8220;assignment&#8221; statements. The <code>case</code>  block has somehow been hidden in this <code> &lt;- </code> operator. If we want to change the functionality of our <code> case</code> we simply have to change the definition of the <code> &lt;- </code>  instead of changing every place in the code where the <code> case </code> block occurs.</p>
<p>We have to understand how to make this transformation.  The transformation seems almost like a magic trick, but I will show you step by step how the trick is done and how you can do it too.</p>
<p>The key step is to encapsulate the <code>case</code> block into a function which we&#8217;ll call <code> bind </code>. Then we can rewrite <code>eval</code> as a sequence of nested <code>bind</code> calls. How does this help? This sequence of bind function calls represents a design pattern that we can encapsulate into a Monad. If we then make the <code>M</code> data type an instance of the <code>Monad</code> class and make the bind a method of this monad class we will be able to use the <code>do</code> notation as a more readable substitute for the sequence of bind function calls.</p>
<p>How do we write the <code>bind</code>   function? The <code>case</code>  block takes an <code>M </code> type value and looks at the value in the <code>M</code> type. If the value is <code>Return a </code> it does further processing on the <code>a  </code> value. So our <code>bind</code> function should take as arguments the <code>M</code> type value and a function to process the <code>Return</code> value of that <code>M</code> type value. So we can write <code> bind </code>  as follows.</p>
<pre>
  bind mType processValueFunction =
          case mType of
            Raise e -&gt;  Raise e
            Return mTypeValue   -&gt;  processValueFunction mTypeValue
</pre>
<p>How can we rewrite the <code>eval</code> function using this <code>bind</code>  function? We&#8217;ll work our way from innermost <code>case</code>  block to outermost. </p>
<p>The innermost <code>case</code>  block is</p>
<pre>
 case (eval u) of
        Raise e -&gt; Raise e
        Return b -&gt;
          if b == 0
            then Raise "divide by zero"
            else Return (a/b)
</pre>
<p>Its clear  <code>mType</code> in the definition of the <code> bind</code>  function is <code>eval u </code> </p>
<p> But what is  <code>processValueFunction</code> ?  We have to write </p>
<pre>
if b == 0
   then Raise "divide by zero"
   else Return (a/b)
</pre>
<p> as a function applied to <code>b</code>. </p>
<p>Clearly the above code is equivalent  to the following function call: </p>
<pre>
(\b -&gt;
  if b == 0
    then Raise "divide by zero"
    else Return (a/b) ) b
</pre>
<p>Note that I made the name of the function argument  the same as the variable to which the function is being applied, namely <code>b</code>  This is perfectly legitimate. Function argument names are completely arbitrary and don&#8217;t conflict with the names of the variables to which the function is applied.</p>
<p>So we can rewrite the case block as:</p>
<pre>
 case (eval u) of
        Raise e -&gt; Raise e
        Return b -&gt;
          (\b -&gt;if b == 0
                  then Raise "divide by zero"
                  else Return (a/b)) b
</pre>
<p>Finally we can rewrite the <code>case</code> block using <code>bind</code> as follows:<br />
and because this step is so important I&#8217;ll also repeat the <code>case</code> block and the definition of <code>bind</code>.</p>
<pre>
 bind (eval  u)
  (\b -&gt;
   if b == 0
    then Raise "divide by zero"
    else Return (a/b) )

--------------------------------------------------------
 case (eval u) of
        Raise e -&gt; Raise e
        Return b -&gt;
          if b == 0
            then Raise "divide by zero"
            else Return (a/b)
--------------------------------------------------------
 bind mType processValueFunction =
          case mType of
            Raise e -&gt;  Raise e
            Return mTypeValue   -&gt;  processValueFunction mTypeValue
</pre>
<p>This is the key step! Make you sure you understand how <code>bind</code> is equivalent to the <code>case</code>.This is the key step! Make you sure you understand how <code>bind</code> is equivalent to the <code>case</code>. Sic! I just wanted to repeat myself for emphasis.</p>
<p>Putting  <code>bind</code> into the original function we get:</p>
<pre>
eval (Div t u) =
 case (eval t) of
   Raise e -&gt; Raise e
   Return a  -&gt;
    bind (eval  u)
     (\b -&gt;
       if b == 0
         then Raise "divide by zero"
         else Return (a/b) )
</pre>
<p>Let&#8217;s convert the top most <code>case</code> block to a <code>bind</code> function call.</p>
<p>This time the <code>M</code> type is <code>(eval t)</code>.<br />
We have to rewrite the following:</p>
<pre>
 bind (eval  u)
     (\b -&gt;
       if b == 0
         then Raise "divide by zero"
         else Return (a/b) )
</pre>
<p>as  a function applied to <code>a</code><br />
We see that in this case that following function call  is  equivalent .</p>
<pre>
(\a -&gt;
   bind (eval  u)
        (\b -&gt;
          if b == 0
           then Raise "divide by zero"
           else Return (a/b) ) a
</pre>
<p>So we can rewrite our eval function as:</p>
<pre>
eval (Div t u) =
 bind (eval t)
      (\a -&gt;
        bind (eval u)
             (\b -&gt;
                 if b == 0
                   then Raise "divide by zero"
                   else Return (a/b) )
</pre>
<p>Now let&#8217;s rename bind to <code> &gt;&gt;= </code> and make it an infix operator. Then we can rewrite our code as</p>
<pre>
eval (Div t u) =
  (eval t) &gt;&gt;=
      (\a -&gt;
         (eval u) &gt;&gt;=
             (\b -&gt;
                 if b == 0
                   then Raise "divide by zero"
                   else Return (a/b) ))
</pre>
<p>Let&#8217;s reformat this slightly and remove unnecessary parenthesis.</p>
<pre>
eval (Div t u) =
  (eval t) &gt;&gt;= \a -&gt;
  (eval u) &gt;&gt;= \b -&gt;
                 if b == 0
                   then Raise "divide by zero"
                   else Return (a/b)
</pre>
<p>We&#8217;ve done it. We&#8217;ve converted the nested series of case statements into a nested sequence of function calls. You should already be able to see in the above code the outlines of the following code to which we&#8217;re aiming.</p>
<pre>
eval (Con a) = Return a
eval (Div t u) =
  do
    a &lt;- eval t
    b &lt;- eval u
    if b == 0
      then Raise &quot;divide by zero&quot;
      else return (a/b)
</pre>
<p>The final  step is to make the <code>M</code> data type an instance of the Monad class and make the <code> &gt;&gt;=</code> function a method of this monad. We also need  to a return function because… </p>
<pre>
instance Monad M
  where
   (&gt;&gt;=) mType processValueFunction =
          case mType of
            Raise e -&gt;  Raise e
            Return mTypeValue   -&gt;  processValueFunction mTypeValue
   return mTypeValue = Return  mTypeValue
</pre>
<p>Finally we can use the <code>do</code> construct to write our nested sequence of <code>bind</code> functions calls as just a series of assignments. The <code>do</code> construct is a short hand for sequencing the <code>bind</code> function.</p>
<pre>
 do p1 &lt;- e1
    some expression involving p1
</pre>
<p>is the same as</p>
<pre>
  e1 &gt;&gt;= \p1 -&gt; some expression involving p1
</pre>
<p>We said our function could be written in this <code>do</code> notation as:</p>
<pre>
eval  (Div t u) =
  do
    a &lt;- eval t
    b &lt;- eval u
    if b == 0
      then Raise &quot;divide by zero&quot;
      else return (a/b)
</pre>
<p>Let&#8217;s unwrap this and try to make it look like the version using &gt;&gt;=</p>
<p>Expanding the first <code>&lt;-</code> gives</p>
<pre>
    eval t &gt;&gt;= \a -&gt;
     b &lt;- eval u
     if b == 0
      then Raise &quot;divide by zero&quot;
      else return (a/b)
</pre>
<p>Expanding the second <code>&lt;-</code> gives</p>
<pre>
    eval t &gt;&gt;= \a -&gt;
    eval u &gt;&gt;= \b -&gt;
      if b == 0
       then Raise "divide by zero"
       else return (a/b)
</pre>
<p>Which shows that thes sequence of bind calls is equivalent to the series of <code> &lt;- </code> in the <code>do </code> construct.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/haskell2000.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/haskell2000.wordpress.com/458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/haskell2000.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/haskell2000.wordpress.com/458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/haskell2000.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/haskell2000.wordpress.com/458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/haskell2000.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/haskell2000.wordpress.com/458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/haskell2000.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/haskell2000.wordpress.com/458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/haskell2000.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/haskell2000.wordpress.com/458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/haskell2000.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/haskell2000.wordpress.com/458/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskell2000.wordpress.com&amp;blog=10658211&amp;post=458&amp;subd=haskell2000&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://haskell2000.wordpress.com/2011/09/23/wadlers-mondad-examples/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c513f579abbd7d806fa0fdaae68c212b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gongli2000</media:title>
		</media:content>
	</item>
	</channel>
</rss>
