<?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>Control.Monad.Reader</title>
	<atom:link href="http://leeduhem.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://leeduhem.wordpress.com</link>
	<description>Everything About Mathematics, Functional Programming, and Haskell</description>
	<lastBuildDate>Sun, 12 Jul 2009 06:35:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='leeduhem.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Control.Monad.Reader</title>
		<link>http://leeduhem.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://leeduhem.wordpress.com/osd.xml" title="Control.Monad.Reader" />
	<atom:link rel='hub' href='http://leeduhem.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Partition An Integer n to an arithmetic series</title>
		<link>http://leeduhem.wordpress.com/2009/07/12/partition-an-integer-n-to-an-arithmetic-series/</link>
		<comments>http://leeduhem.wordpress.com/2009/07/12/partition-an-integer-n-to-an-arithmetic-series/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 04:15:13 +0000</pubDate>
		<dc:creator>Lee Duhem</dc:creator>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://leeduhem.wordpress.com/?p=77</guid>
		<description><![CDATA[Give a positive integer n, generate all partition of n to an arithmetic series with common difference 1. First, a straightforward solution: Second, a not so straightforward solution, but much more effective than the first: You can check these two solutions are equal (upto n) by Exercise: Prove solution two is right. Answer: Let hight [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=leeduhem.wordpress.com&amp;blog=7832332&amp;post=77&amp;subd=leeduhem&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Give a positive integer n, generate all partition of n to an arithmetic series with common difference 1.</p>
<p>First, a straightforward solution:<br />
<pre class="brush: python;">
import Data.List (find)
import Data.Maybe (catMaybes)

-- partition positive integer n to an arithmetic series
partitionToArithmetic0 n = catMaybes $ map partition [1..(n-2)]
    where
        partition x = let ys = [x..(n-1)] in
            case find (p x) ys of
                Just y  -&gt; Just (x, y)
                Nothing -&gt; Nothing

        p x y
            | (x+y) * (y-x+1) == 2 *n = True
            | otherwise               = False
</pre></p>
<p>Second, a not so straightforward solution, but much more effective than the first:<br />
<pre class="brush: python;">
partitionToArithmetic1 n = catMaybes $ map partition [2..hh]
    where
        hh = floor $ sqrt $ fromIntegral $ 2*n

        partition h
            | not $ s &gt; d = Nothing
            | not $ or [ hr /= 0 &amp;&amp; nh == 0, hr == 0 &amp;&amp; 2*nh == h] = Nothing
            | otherwise = Just (s-d, s+d1)
          where
            s = n `div` h
            d = (h-1) `div` 2
            d1 = d + (if even h then 1 else 0)
            hr = h `rem` 2
            nh = n `rem` h
</pre></p>
<p>You can check these two solutions are equal (upto n) by<br />
<pre class="brush: python;">
test u = and $ map (\n -&gt; (reverse $ partitionToArithmetic0 n) == partitionToArithmetic1 n) [1..u]
</pre></p>
<p>Exercise: Prove solution two is right.<br />
Answer:<br />
Let hight (or length) of an arithmetic series partition of a positive integer n is h, and</p>
<pre>
    s  = n/h - (n % h)
    d  = floor( (h-1) / 2)
    d1 = d + (if even h then 1 else 0)
</pre>
<p>We&#8217;ll prove it by three steps. </p>
<p>Step 1, [s-d .. s+d1] is an arithmetic series partition of the positive integer n.</p>
<pre>
    (s-d + s+d+[even h]) (s+d+[even h] - (s-d) + 1) / 2
    = (2*s + [even h]) (2*d + [even h] + 1) /2
    = (2*s + [even h]) * h / 2
    = n - (n % h) + [even h] * h / 2

    If [even h] * h = 2 * (n % h), we have sum [s-d .. s+d1] = n.
    ([even h] = if even h then 1 else 0)
</pre>
<p></p>
<p>Step 2, an arithmetic series partition of the positive integer n can be write as [s-d .. s+d1]. </p>
<p>To prove this, we only need to prove that s = n/h &#8211; (n%h)/h.</p>
<pre>
    (s-d + s+d+[even h]) (s+d+[even h] - (s-d) + 1) / 2 = n
</pre>
<p>so</p>
<pre>
    s = n/h - [even h]/2
</pre>
<p>Because [even h] * h = 2 * (n % h), so we have s = n/h &#8211; (n % h)/h.</p>
<p>Step 3, h &lt;= sqrt (2*n).</p>
<p>For partition n = h + (h-1) + &#8230; + 2 + 1, h get maximum value.</p>
<pre>
    (2 + (h-1)) * h / 2 &gt;= n
</pre>
<p>so</p>
<pre>
    h * (h+1) &gt;= 2*n
</pre>
<p>If h = sqrt (2*n), h * (h + 1) = 2*n + sqrt (2*n) &gt; 2*n.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/leeduhem.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/leeduhem.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/leeduhem.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/leeduhem.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/leeduhem.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/leeduhem.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/leeduhem.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/leeduhem.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/leeduhem.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/leeduhem.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/leeduhem.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/leeduhem.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/leeduhem.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/leeduhem.wordpress.com/77/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=leeduhem.wordpress.com&amp;blog=7832332&amp;post=77&amp;subd=leeduhem&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://leeduhem.wordpress.com/2009/07/12/partition-an-integer-n-to-an-arithmetic-series/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0a6aacd03deb37cb13cbc8b65e454f2b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">leeduhem</media:title>
		</media:content>
	</item>
		<item>
		<title>Understanding Functions Which Use &#8216;instance Monad []&#8216; by Equational Reasoning</title>
		<link>http://leeduhem.wordpress.com/2009/06/19/understanding-functions-which-use-list-monad-by-equational-reasoning/</link>
		<comments>http://leeduhem.wordpress.com/2009/06/19/understanding-functions-which-use-list-monad-by-equational-reasoning/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 13:52:42 +0000</pubDate>
		<dc:creator>Lee Duhem</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Understanding]]></category>

		<guid isPermaLink="false">http://leeduhem.wordpress.com/?p=74</guid>
		<description><![CDATA[GüŸnther Schmidt asked in Haskell-Cafe how to get a stream like this: &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;[&#34;a&#34;, ... , &#34;z&#34;, &#34;aa&#34;, ... , &#34;az&#34;, &#34;ba&#34;, ... , &#34;bz&#34;, ... ] and people in Haskell-Cafe offer some interesting answer for this question. On the one hand, these answers show the power of Haskell and GHC base libraries, but on the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=leeduhem.wordpress.com&amp;blog=7832332&amp;post=74&amp;subd=leeduhem&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><font face="monospace"><br />
GüŸnther Schmidt asked in Haskell-Cafe <a href="http://www.haskell.org/pipermail/haskell-cafe/2009-June/062940.html">how to get a stream like this</a>:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[&quot;a&quot;, ... , &quot;z&quot;, &quot;aa&quot;, ... , &quot;az&quot;, &quot;ba&quot;, ... , &quot;bz&quot;, ... ]</p>
<p>and people in Haskell-Cafe offer some interesting answer for this question. On the one hand, these answers show the power of Haskell and GHC base libraries, but on the other hand, understanding them is a challenge for Haskell newbie like me. But I found to understand these answers, equational reasoning is very helpful, here is why I think so.</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://www.haskell.org/pipermail/haskell-cafe/2009-June/062968.html">Answer 1 (by Matthew Brecknell)</a>:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;concat $ tail $ iterate (map (:) ['a' .. 'z'] &lt;*&gt;) [[]]</p>
<p>Well, how does this expression do what we want? concat, tail, iterate, map, are easy, looks like the magic is in (&lt;*&gt;). </p>
<p>What&#8217;s this operator mean? (&lt;*&gt;) comes from class Applicative of Control.Applicative, </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Functor f =&gt; Applicative f where<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; | Lift a value.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pure :: a -&gt; f a</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; | Sequential application.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&lt;*&gt;) :: f (a -&gt; b) -&gt; f a -&gt; f b</p>
<p>and &#8216;instance Applicative []&#8216; is</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance Applicative [] where<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pure = return<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&lt;*&gt;) = ap</p>
<p>ap comes from Control.Monad</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ap :: (Monad m) =&gt; m (a -&gt; b) -&gt; m a -&gt; m b<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ap =&nbsp;&nbsp;liftM2 id</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;liftM2&nbsp;&nbsp;:: (Monad m) =&gt; (a1 -&gt; a2 -&gt; r) -&gt; m a1 -&gt; m a2 -&gt; m r<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;liftM2 f m1 m2 = do { x1 &lt;- m1; x2 &lt;- m2; return (f x1 x2) }</p>
<p>so the key to understand (&lt;*&gt;) is understanding the meaning of liftM2.</p>
<p>liftM2 uses, hum, do-notation, so according to Haskell 98 report, this can be translated to</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;liftM2 f m1 m2<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= m1 &gt;&gt;= \x1 -&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m2 &gt;&gt;= \x2 -&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (f x1 x2)</p>
<p>When it is applied to list (you can convince yourself of this by type inference), wee need &#8216;instance Monad []&#8216;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance&nbsp;&nbsp;Monad []&nbsp;&nbsp;where<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m &gt;&gt;= k&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = foldr ((++) . k) [] m<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m &gt;&gt; k&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= foldr ((++) . (\ _ -&gt; k)) [] m<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return x&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= [x]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fail _&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= []</p>
<p>so <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;liftM2 f m1 m2<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= m1 &gt;&gt;= \x1 -&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m2 &gt;&gt;= \x2 -&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (f x1 x2)</p>
<p>let<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f1<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\x1 -&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m2 &gt;&gt;= \x2 -&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (f x1 x2)</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f2<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= \x2 -&gt; return (f x1 x2)</p>
<p>we can write</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m1 &gt;&gt;= f1<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= foldr ((++) . f1) [] m1</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m2 &gt;&gt;= f2<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= foldr ((++) . f2) [] m2</p>
<p>Now we can see for list m1, m2, how does &#8216;liftM2 f m1 m2&#8242; work</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;z1 = []<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach x1 in (reverse m1); do&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; foldr ((++) . f1) [] m1<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;z2 = []<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach x2 in (reverse m2); do&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; foldr ((++) . f2) [] m2<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;z2 = [f x1 x2] ++ z2<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;done<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;z1 = z2 ++ z1<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;done</p>
<p>Now we are ready to see how to apply (&lt;*&gt;):</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map (:) ['a' .. 'z'] &lt;*&gt; [[]]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= (map (:) ['a' .. 'z']) &lt;*&gt; [[]]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= [('a':), ..., ('z':)] &lt;*&gt; [[]]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; misuse of [...] notation<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= ap [('a':), ..., ('z':)] [[]]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= liftM2 id [('a':), ..., ('z':)] [[]]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= [('a':), ..., ('z':)] &gt;&gt;= \x1 -&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[]]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;= \x2 -&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (id x1 x2)</p>
<p>Here x1 bind to (&#8216;z&#8217;:), &#8230;, (&#8216;a&#8217;:) in turn, x2 always bind to [], and noticed that</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (id (&#8216;z&#8217;:) [])&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; f = id; x1 = (&#8216;a&#8217;:); x2 = []<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= return ((&#8216;z&#8217;:) [])<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= return ((:) &#8216;z&#8217; [])<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= return &quot;z&quot;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= [&quot;z&quot;]</p>
<p>we have<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map (:) ['a' .. 'z'] &lt;*&gt; [[]]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= liftM2 id [('a':), ..., ('z':)] [[]]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= [&quot;a&quot;, ..., &quot;z&quot;]</p>
<p>(If you can&#8217;t follow the this, work through the definition of foldr step by step will be very helpful.)</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map (:) ['a' .. 'z'] &lt;*&gt; (map (:) ['a' .. 'z'] &lt;*&gt; [[]])<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= map (:) ['a' .. 'z'] &lt;*&gt; [&quot;a&quot;, ..., &quot;z&quot;]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= liftM2 id [('a':), ..., ('z':)] [&quot;a&quot;, ..., &quot;z&quot;]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= [&quot;aa&quot;, ..., &quot;az&quot;, &quot;ba&quot;, ..., &quot;bz&quot;, ..., &quot;za&quot;, ..., &quot;zz&quot;]</p>
<p>Now it&#8217;s easy to know what we get from</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iterate (map (:) ['a' .. 'z'] &lt;*&gt;) [[]]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= [[], f [[]], f (f [[]]), &#8230;]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; f = map (:) ['a' .. 'z'] &lt;*&gt;</p>
<p>so<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;concat $ tail $ iterate (map (:) ['a' .. 'z'] &lt;*&gt;) [[]]</p>
<p>is exactly what we want.</p>
<p>Understanding Haskell codes by equational reasoning could be a very tedious process, but it&#8217;s also a very helpful and instructive process for the beginners, because it make you think slowly, check the computation process step by step, just like the compiler does. And in my opinion, this is exactly what a debugger does.</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://www.haskell.org/pipermail/haskell-cafe/2009-June/062963.html">Answer 2 (by Reid Barton)</a>:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;concatMap (\n -&gt; replicateM n ['a'..'z']) [1..]</p>
<p>In this solution, the hardest part is replicatM, which come from Control.Monad</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replicateM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:: (Monad m) =&gt; Int -&gt; m a -&gt; m [a]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replicateM n x&nbsp;&nbsp;&nbsp;&nbsp;= sequence (replicate n x)</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sequence&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :: Monad m =&gt; [m a] -&gt; m [a] <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sequence ms = foldr k (return []) ms<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;where<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k m m&#8217; = do { x &lt;- m; xs &lt;- m&#8217;; return (x:xs) }</p>
<p>recall the defintion of liftM2:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;liftM2&nbsp;&nbsp;:: (Monad m) =&gt; (a1 -&gt; a2 -&gt; r) -&gt; m a1 -&gt; m a2 -&gt; m r<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;liftM2 f m1 m2 = do { x1 &lt;- m1; x2 &lt;- m2; return (f x1 x2) }</p>
<p>so k in definition of sequence is an application of liftM2, and sequence itself is a normal foldr.</p>
<p>Exercise:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Suppose n and xs have appropriate types, prove that</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replicateM n xs = (iterate (map (:) xs &lt;*&gt;) [[]]) !! n</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;by equational reasoning.</p>
<p>Answer:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replicateM n xs = (iterate (map (:) xs &lt;*&gt;) [[]]) !! n</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Definition of replicateM:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replicateM :: (Monad m) =&gt; Int -&gt; m a -&gt; m [a]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replicateM n x = sequence (replicate n x)</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sequence :: Monad m =&gt; [m a] -&gt; m [a] <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sequence ms = foldr k (return []) ms<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;where<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k m m&#8217; = do { x &lt;- m; xs &lt;- m&#8217;; return (x:xs) }</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Definition of (&lt;*&gt;):</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance Applicative [] where<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pure = return<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&lt;*&gt;) = ap</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ap :: Monad m =&gt; m (a -&gt; b) -&gt; m a -&gt; m b<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ap = liftM2 id</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;liftM2 :: (Monad m) =&gt; (a1 -&gt; a2 -&gt; r) -&gt; m a1 -&gt; m a2 -&gt; m r<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;liftM2 f m1 m2 = do { x1 &lt;- m1; x2 &lt;- m2; return (f x1 x2) }</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;so<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replicateM n xs<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= sequence (replicate n xs)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= sequence [xs, ..., xs]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; length [xs, ..., xs] == n<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= foldr k (return []) [xs, ..., xs]&nbsp;&nbsp;&nbsp;&nbsp; &#8212; k = liftM2 (:)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= foldr k [[]] [xs, ..., xs]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= xs `k` (xs `f` &#8230; (xs `k` [[]]) &#8230;)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= k xs (k xs &#8230; (k xs [[]]) &#8230;)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= f (f &#8230; (f [[]]))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; f = liftM2 (:) xs</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for right hand, because</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map (:) xs &lt;*&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= ap (map (:) xs)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= liftM2 id (map (:) xs)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= liftM2 (:) xs</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;we have</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(iterate (map (:) xs &lt;*&gt;) [[]]) !! n<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= (iterate (liftM2 (:) xs) [[]]) !! n<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= f (f &#8230; (f [[]]))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; f = liftM2 (:) xs</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Left hand and right hand are equal.<br />
</font></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/leeduhem.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/leeduhem.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/leeduhem.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/leeduhem.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/leeduhem.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/leeduhem.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/leeduhem.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/leeduhem.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/leeduhem.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/leeduhem.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/leeduhem.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/leeduhem.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/leeduhem.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/leeduhem.wordpress.com/74/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=leeduhem.wordpress.com&amp;blog=7832332&amp;post=74&amp;subd=leeduhem&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://leeduhem.wordpress.com/2009/06/19/understanding-functions-which-use-list-monad-by-equational-reasoning/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0a6aacd03deb37cb13cbc8b65e454f2b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">leeduhem</media:title>
		</media:content>
	</item>
		<item>
		<title>Understanding &#8216;instance Monad ((-&gt;) r)&#8217; by type inference</title>
		<link>http://leeduhem.wordpress.com/2009/06/07/understanding-monad-instance-by-type-inference/</link>
		<comments>http://leeduhem.wordpress.com/2009/06/07/understanding-monad-instance-by-type-inference/#comments</comments>
		<pubDate>Sun, 07 Jun 2009 16:04:18 +0000</pubDate>
		<dc:creator>Lee Duhem</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Understanding]]></category>

		<guid isPermaLink="false">http://leeduhem.wordpress.com/?p=42</guid>
		<description><![CDATA[While reading source code of Control.Monad.Instances, I found I can&#8217;t understand &#8216;instance of Monad ((-&#62;) r)&#8217;, but after read Brent Yorgey&#8217;s reply in his blog post about The Typeclassopedia, he said, &#8216;the data constructor for (-&#62;) is called… lambda&#8217;, I suddenly found I CAN understand them by type inference. Here is how I do these. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=leeduhem.wordpress.com&amp;blog=7832332&amp;post=42&amp;subd=leeduhem&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>While reading source code of <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/src/Control-Monad-Instances.html">Control.Monad.Instances</a>, I found I can&#8217;t understand &#8216;instance of Monad ((-&gt;) r)&#8217;, but after read Brent Yorgey&#8217;s <a href="http://byorgey.wordpress.com/2009/03/16/monadreader-13-is-out/#comment-1219">reply</a> in his blog post about <a href="http://byorgey.wordpress.com/2009/03/16/monadreader-13-is-out/">The Typeclassopedia</a>, he said, &#8216;the data constructor for (-&gt;) is called… lambda&#8217;, I suddenly found I CAN understand them by type inference. Here is how I do these.</p>
<p>There are two points:</p>
<ul>
<li>follow the types</li>
<li>the data constructor for type constructor (-&gt;) is called lambda abstraction</li>
</ul>
<p>This is the source code from <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/src/Control-Monad-Instances.html">Control.Monad.Instances</a>:<br />
<pre class="brush: python;">
instance Monad ((-&gt;) r) where
        return = const
        f &gt;&gt;= k = \ r -&gt; k (f r) r
</pre><br />
The case of <code>return</code> is trivial. Look at it&#8217;s type first: <code>return :: (Monad m) =&gt; a -&gt; m a</code>, so for &#8216;instance ((-&gt;) r)&#8217;, we have <code>m = (-&gt;) r</code> then</p>
<pre>	  a -&gt; m a
	= a -&gt; ((-&gt;) r a)
	= a -&gt; r -&gt; a</pre>
<p>Now look at const&#8217;s type: <code>const :: a -&gt; b -&gt; a</code>, exactly what we want. But what we got from &#8216;return a&#8217;? It is</p>
<pre>        return a = (-&gt;) r a     -- misuse (-&gt;) at here
                 = \r -&gt; a      -- data constructor of (-&gt;) is lambda
                 = \_ -&gt; a</pre>
<p>this is exactly what we got from &#8216;const a&#8217;, i.e. <code>const a = \_ -&gt; a</code></p>
<p><code>(&gt;&gt;=)</code> is a bit complicate, but if you follow its type, you can understand it pretty easily.</p>
<p>Let&#8217;s look at it&#8217;s type first:</p>
<p><code>(&gt;&gt;=) :: (Monad m) =&gt; m a -&gt; (a -&gt; m b) -&gt; m b</code></p>
<p>As before, for &#8216;instance ((-&gt;) r)&#8217;, we have <code> m = (-&gt;) r</code>, then</p>
<pre>        f :: m a = (-&gt;) r a
        k :: a -&gt; m b = a -&gt; ((-&gt;) r b) = a -&gt; r -&gt; b</pre>
<p>and suppose</p>
<pre>        f r = a
        k a r = b</pre>
<p>then we have</p>
<pre>        f &gt;&gt;= k = \r -&gt; k (f r) r
                = \r -&gt; a r
                = \r -&gt; b
                = (-&gt;) r b      -- misuse of (-&gt;)
                = m b</pre>
<p>Err..a bit misuse of (-&gt;) and didn&#8217;t distinguish type variables and normal variables, but it works for me. If you have the similar difficult to understanding &#8216;instance Monad ((-&gt;) r)&#8217;, I hope these work for you too.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/leeduhem.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/leeduhem.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/leeduhem.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/leeduhem.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/leeduhem.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/leeduhem.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/leeduhem.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/leeduhem.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/leeduhem.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/leeduhem.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/leeduhem.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/leeduhem.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/leeduhem.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/leeduhem.wordpress.com/42/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=leeduhem.wordpress.com&amp;blog=7832332&amp;post=42&amp;subd=leeduhem&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://leeduhem.wordpress.com/2009/06/07/understanding-monad-instance-by-type-inference/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0a6aacd03deb37cb13cbc8b65e454f2b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">leeduhem</media:title>
		</media:content>
	</item>
		<item>
		<title>Finish reading of FP Implementation</title>
		<link>http://leeduhem.wordpress.com/2009/06/02/finish-reading-of-fp-implementation/</link>
		<comments>http://leeduhem.wordpress.com/2009/06/02/finish-reading-of-fp-implementation/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 16:28:45 +0000</pubDate>
		<dc:creator>Lee Duhem</dc:creator>
				<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Book]]></category>
		<category><![CDATA[Implementation]]></category>

		<guid isPermaLink="false">http://leeduhem.wordpress.com/?p=20</guid>
		<description><![CDATA[Finish reading of The Implementation of Functional Programming Languages by Simon Peyton Jones. After reading this very readable book, you will get a global conceptual understanding of implementation of functional programming languages.But because it&#8217;s published 22 years ago (1987), so it&#8217;s a little out of date now (for example, it didn&#8217;t include the newest implementation [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=leeduhem.wordpress.com&amp;blog=7832332&amp;post=20&amp;subd=leeduhem&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Finish reading of <a href="http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/">The Implementation of Functional Programming Languages</a> by <a href="http://research.microsoft.com/en-us/people/simonpj/">Simon Peyton Jones</a>.</p>
<p>After reading this very readable book, you will get a global conceptual understanding of implementation of functional programming languages.But because it&#8217;s published 22 years ago (1987), so it&#8217;s a little out of date now (for example, it didn&#8217;t include the newest implementation technics of functional programming language). Despit it&#8217;s age, everyone who interested in implementation of functional programming languages should read it.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/leeduhem.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/leeduhem.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/leeduhem.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/leeduhem.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/leeduhem.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/leeduhem.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/leeduhem.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/leeduhem.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/leeduhem.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/leeduhem.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/leeduhem.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/leeduhem.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/leeduhem.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/leeduhem.wordpress.com/20/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=leeduhem.wordpress.com&amp;blog=7832332&amp;post=20&amp;subd=leeduhem&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://leeduhem.wordpress.com/2009/06/02/finish-reading-of-fp-implementation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0a6aacd03deb37cb13cbc8b65e454f2b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">leeduhem</media:title>
		</media:content>
	</item>
		<item>
		<title>Finish reading of The Typeclassopedia</title>
		<link>http://leeduhem.wordpress.com/2009/05/30/finish-reading-of-the-typeclassopedia/</link>
		<comments>http://leeduhem.wordpress.com/2009/05/30/finish-reading-of-the-typeclassopedia/#comments</comments>
		<pubDate>Sat, 30 May 2009 11:27:30 +0000</pubDate>
		<dc:creator>Lee Duhem</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[paper]]></category>
		<category><![CDATA[typeclass]]></category>

		<guid isPermaLink="false">http://leeduhem.wordpress.com/?p=16</guid>
		<description><![CDATA[Finish reading of The Typeclassopedia from The Monad.Reader 13, but it&#8217;s just the beginning of a series of reading about papers, articles, wikibooks, blog posts, and codes, as the author said. It&#8217;s a great article, talks about Haskell&#8217;s type class and their relationship, has plenty of very useful references.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=leeduhem.wordpress.com&amp;blog=7832332&amp;post=16&amp;subd=leeduhem&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Finish reading of The Typeclassopedia from <a href="http://www.haskell.org/sitewiki/images/8/85/TMR-Issue13.pdf">The Monad.Reader 13</a>, but it&#8217;s just the beginning of a series of reading about papers, articles, wikibooks, blog posts, and codes, as the <a href="http://byorgey.wordpress.com/">author</a> said.</p>
<p>It&#8217;s a great article, talks about Haskell&#8217;s type class and their relationship, has plenty of very useful references.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/leeduhem.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/leeduhem.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/leeduhem.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/leeduhem.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/leeduhem.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/leeduhem.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/leeduhem.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/leeduhem.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/leeduhem.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/leeduhem.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/leeduhem.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/leeduhem.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/leeduhem.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/leeduhem.wordpress.com/16/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=leeduhem.wordpress.com&amp;blog=7832332&amp;post=16&amp;subd=leeduhem&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://leeduhem.wordpress.com/2009/05/30/finish-reading-of-the-typeclassopedia/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0a6aacd03deb37cb13cbc8b65e454f2b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">leeduhem</media:title>
		</media:content>
	</item>
		<item>
		<title>Sieve of Eratosthenes In Haskell</title>
		<link>http://leeduhem.wordpress.com/2009/05/20/sieve-of-eratosthenes-in-haskell/</link>
		<comments>http://leeduhem.wordpress.com/2009/05/20/sieve-of-eratosthenes-in-haskell/#comments</comments>
		<pubDate>Wed, 20 May 2009 17:12:34 +0000</pubDate>
		<dc:creator>Lee Duhem</dc:creator>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://leeduhem.wordpress.com/?p=3</guid>
		<description><![CDATA[Algorithm description, see Sieve of Eratosthenes There is a HackageDB package called primes which provides an efficient lazy wheel sieve for prime generation. This version looks nicer, but less effective:<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=leeduhem.wordpress.com&amp;blog=7832332&amp;post=3&amp;subd=leeduhem&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Algorithm description, see <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes</a><br />
<pre class="brush: python;">
module Main where

import System.Environment

isPrime p (x:xs)
	| x*x &gt; p	 = True
	| p `mod` x == 0 = False
	| otherwise	 = isPrime p xs

primes = 2 : oprimes
	where oprimes = 3 : [ p | p &lt;- [5,7..], isPrime p oprimes]

main = do
	args &lt;- getArgs
	let	n = read $ args !! 0
		x = takeWhile (&lt; n) primes
	print $ length x
	print x
</pre><br />
There is a HackageDB package called <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/primes">primes</a> which provides an efficient lazy wheel sieve for prime generation.</p>
<p>This version looks nicer, but less effective:<br />
<pre class="brush: python;">
sieve (x:xs) = x : sieve xs'
	where xs' = filter (\y -&gt; y `mod` x /= 0) xs

primes = 2 : sieve [3,5..]
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/leeduhem.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/leeduhem.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/leeduhem.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/leeduhem.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/leeduhem.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/leeduhem.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/leeduhem.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/leeduhem.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/leeduhem.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/leeduhem.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/leeduhem.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/leeduhem.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/leeduhem.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/leeduhem.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=leeduhem.wordpress.com&amp;blog=7832332&amp;post=3&amp;subd=leeduhem&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://leeduhem.wordpress.com/2009/05/20/sieve-of-eratosthenes-in-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0a6aacd03deb37cb13cbc8b65e454f2b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">leeduhem</media:title>
		</media:content>
	</item>
	</channel>
</rss>
