<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Random Hacks: Three things I don't understand about monads</title>
    <link>http://www.randomhacks.net/articles/2007/03/05/three-things-i-dont-understand-about-monads</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Technology and Other Fun Stuff</description>
    <item>
      <title>"Three things I don't understand about monads" by Stefan Ljungstrand</title>
      <description>&lt;p&gt;&lt;span class="caps"&gt;CCS &lt;/span&gt;: When &amp;#8216;o&amp;#8217; is a commutative monoid, the &amp;#8216;Writer o&amp;#8217; monad is commutative, but actions are still not discardable.&lt;/p&gt;</description>
      <pubDate>Tue, 13 Mar 2007 09:08:57 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:7751126c-6498-48e9-9398-24f0bd03eb35</guid>
      <link>http://www.randomhacks.net/articles/2007/03/05/three-things-i-dont-understand-about-monads#comment-359</link>
    </item>
    <item>
      <title>"Three things I don't understand about monads" by Chung-chieh Shan</title>
      <description>&lt;p&gt;A quick observation about commutative monads: it seems that most (all?) examples of useful commutative monads also obey the law that &amp;#8220;m &gt;&gt; n&amp;#8221; is equivalent to &amp;#8220;n&amp;#8221;.  Intuitively, if you don&amp;#8217;t need the result of an action in a commutative monad, then you don&amp;#8217;t need the effect either.&lt;/p&gt;


	&lt;p&gt;This law holds morally for environment, unique supply, and random-number generation.  It also holds for the probability monad (yay!).  It may or may not hold in the strictness monad, depending on your morality.&lt;/p&gt;


	&lt;p&gt;This law makes me wonder if we can think of &gt;&gt;= in a commutative monad as expressing data flow.  I guess I&amp;#8217;m not the first person to wonder about overloading function application (aka whitespace or juxtaposition) for some purpose&amp;#8230;&lt;/p&gt;</description>
      <pubDate>Sun, 11 Mar 2007 17:16:10 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:a7f58cb8-575d-443d-8fa9-55f9b23daa6c</guid>
      <link>http://www.randomhacks.net/articles/2007/03/05/three-things-i-dont-understand-about-monads#comment-349</link>
    </item>
    <item>
      <title>"Three things I don't understand about monads" by Neel Krishnaswami</title>
      <description>&lt;p&gt;The connection between monads and algebras is one that I&amp;#8217;m actually very surprised functional programmers don&amp;#8217;t use&amp;#8212;monads were &lt;em&gt;invented&lt;/em&gt; to talk about algebras, and are really beautiful when you need to do things like enrich a syntax with variables (eg, for unification algorithms).&lt;/p&gt;


	&lt;p&gt;Michi is right about enriched category theory being relevant here. One of the bad things about monads as they are commonly used is that there&amp;#8217;s no theory for the &lt;em&gt;operations&lt;/em&gt; in a monad, and that&amp;#8217;s the most important part! Power and Plotkin have been looking at how to derive a monad given a set of algebraic operations (for example, if you&amp;#8217;ve got get and set, can you derive the state monad?), and enriched categories show up.&lt;/p&gt;</description>
      <pubDate>Wed, 07 Mar 2007 11:57:44 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:567aed45-2969-48ed-85b0-f4c26eac0f6f</guid>
      <link>http://www.randomhacks.net/articles/2007/03/05/three-things-i-dont-understand-about-monads#comment-336</link>
    </item>
    <item>
      <title>"Three things I don't understand about monads" by sigfpe</title>
      <description>&lt;p&gt;I agree with &lt;span class="caps"&gt;SPJ&lt;/span&gt;, I&amp;#8217;d put a nice notation for commutative monads as my #2 want for Haskell. My #1 is a way to make efficient commutative monads in the first place eg. dealing with the problems of defining Set so that bind can use (==) without using ugly hacks.&lt;/p&gt;


	&lt;p&gt;Hmmm&amp;#8230;there would be a neat spinoff from a nice commutative monad notation. It would be easier to write strict code in Haskell.&lt;/p&gt;</description>
      <pubDate>Mon, 05 Mar 2007 13:30:03 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:92d283a0-83f5-4301-be95-2ac6fda5808c</guid>
      <link>http://www.randomhacks.net/articles/2007/03/05/three-things-i-dont-understand-about-monads#comment-329</link>
    </item>
    <item>
      <title>"Three things I don't understand about monads" by Eric</title>
      <description>&lt;p&gt;Whether &lt;code&gt;FooT&lt;/code&gt; and &lt;code&gt;BarT&lt;/code&gt; commute (sometimes? often? always?) depends on the algebraic properties of &lt;code&gt;Foo&lt;/code&gt; and &lt;code&gt;Bar&lt;/code&gt;. There&amp;#8217;s all sorts of neat stuff going on that I can&amp;#8217;t quite grasp.&lt;/p&gt;


	&lt;p&gt;For example, &lt;code&gt;(ListT m)&lt;/code&gt; is &lt;a href="http://citeseer.ist.psu.edu/jones93composing.html"&gt;only a monad&lt;/a&gt; when &lt;code&gt;m&lt;/code&gt; is a commutative monad. And as &lt;a href="http://sigfpe.blogspot.com/2006/11/why-isnt-listt-monad.html"&gt;Dan showed&lt;/a&gt;, this is related to properties of freely-generated semirings.&lt;/p&gt;


	&lt;p&gt;I suspect that there&amp;#8217;s something deeper to be said about these issues, and that it might be important for monad-based DSLs.&lt;/p&gt;


	&lt;p&gt;Anyway, here&amp;#8217;s a list of resources that might be relevant:&lt;/p&gt;


	&lt;p&gt;- Mark Jones and Luc Duponcheel&amp;#8217;s &lt;a href="http://citeseer.ist.psu.edu/jones93composing.html"&gt;Composing monads&lt;/a&gt;, an early paper on monad transformers that noticed some interesting limits to what could be composed.&lt;/p&gt;


	&lt;p&gt;- Edmund Robinson&amp;#8217;s &lt;a href="http://www.dcs.qmul.ac.uk/tech_reports/RR-02-01.pdf"&gt;Variations on Algebra: monadicity and generalisations of 
equational theories&lt;/a&gt;, which describes a fascinating relationship between initial algebras and monads. (Thanks, alpheccar!)&lt;/p&gt;


	&lt;p&gt;- According to &lt;a href="http://www.blogger.com/profile/6484344"&gt;Michi&lt;/a&gt;, enriched categories provide some insight into these kinds of layerings. So far, I&amp;#8217;ve found &lt;a href="http://www.tac.mta.ca/tac/reprints/articles/10/tr10abs.html"&gt;Basic Concepts of Enriched Category Theory&lt;/a&gt; and a &lt;a href="http://en.wikipedia.org/wiki/Enriched_category"&gt;Wikipedia article&lt;/a&gt;, but I haven&amp;#8217;t dug into this area yet (it&amp;#8217;s getting a bit heavy for my current knowledge of category theory).&lt;/p&gt;</description>
      <pubDate>Mon, 05 Mar 2007 12:34:23 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:cde8046d-aee3-4c17-b6bc-b3cbdfdce19c</guid>
      <link>http://www.randomhacks.net/articles/2007/03/05/three-things-i-dont-understand-about-monads#comment-328</link>
    </item>
    <item>
      <title>"Three things I don't understand about monads" by Josef Svenningsson</title>
      <description>&lt;p&gt;About commutative monads: I totally agree that we need some nicer ways of programming with them and make use of the commutativity. I anticipate that there&amp;#8217;s a lot of fun/useful things that can come out that.&lt;/p&gt;


	&lt;p&gt;As for monad transformers and whether they commute or not: I don&amp;#8217;t see the problem. Some monad transformers commute, some don&amp;#8217;t. There might be deeper things to be said about it but you can get a long way if you just accept that commutativity is a property that some, but not all, monad transformer obeys.&lt;/p&gt;</description>
      <pubDate>Mon, 05 Mar 2007 11:56:46 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e0318f67-fc9b-4e75-871d-5086e41db87f</guid>
      <link>http://www.randomhacks.net/articles/2007/03/05/three-things-i-dont-understand-about-monads#comment-327</link>
    </item>
    <item>
      <title>Three things I don't understand about monads</title>
      <description>&lt;p&gt;Monads are a remarkably powerful tool for building specialized programming languages.  Some examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.cs.uu.nl/~daan/parsec.html"&gt;Parsers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.randomhacks.net/articles/2007/02/22/bayes-rule-and-drug-tests"&gt;Bayesian inference&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://web.cecs.pdx.edu/~mpj/pubs/modinterp.html"&gt;Modular interpreters&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But there&amp;#8217;s a bunch of things I don&amp;#8217;t understand about monads. In each case, my confusion involves some aspect of the underlying math that &amp;#8220;bubbles up&amp;#8221; to affect the design of specialized languages.&lt;/p&gt;

&lt;p&gt;(Warning: Obscure monad geeking ahead.)&lt;/p&gt;

&lt;h3&gt;Commutative monads&lt;/h3&gt;

&lt;p&gt;A &amp;#8220;commutative monad&amp;#8221; is any monad where we can replace the expression:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='hs-keyword'&gt;do&lt;/span&gt; &lt;span class='hs-varid'&gt;a&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='hs-varid'&gt;ma&lt;/span&gt;
   &lt;span class='hs-varid'&gt;b&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='hs-varid'&gt;mb&lt;/span&gt;
   &lt;span class='hs-varid'&gt;f&lt;/span&gt; &lt;span class='hs-varid'&gt;a&lt;/span&gt; &lt;span class='hs-varid'&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&amp;#8230;with:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='hs-keyword'&gt;do&lt;/span&gt; &lt;span class='hs-varid'&gt;b&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='hs-varid'&gt;mb&lt;/span&gt;
   &lt;span class='hs-varid'&gt;a&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='hs-varid'&gt;ma&lt;/span&gt;
   &lt;span class='hs-varid'&gt;f&lt;/span&gt; &lt;span class='hs-varid'&gt;a&lt;/span&gt; &lt;span class='hs-varid'&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&amp;#8230;without changing the meaning. Examples of commutative monads include &lt;code&gt;Reader&lt;/code&gt; and &lt;code&gt;Rand&lt;/code&gt;.  This is an important property, because it might allow us to parallelize the commonly-used &lt;code&gt;sequence&lt;/code&gt; function across huge numbers of processors:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='hs-definition'&gt;sequence&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;::&lt;/span&gt; &lt;span class='hs-layout'&gt;(&lt;/span&gt;&lt;span class='hs-conid'&gt;Monad&lt;/span&gt; &lt;span class='hs-varid'&gt;m&lt;/span&gt;&lt;span class='hs-layout'&gt;)&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;[&lt;/span&gt;&lt;span class='hs-varid'&gt;m&lt;/span&gt; &lt;span class='hs-varid'&gt;a&lt;/span&gt;&lt;span class='hs-keyglyph'&gt;]&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='hs-varid'&gt;m&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;[&lt;/span&gt;&lt;span class='hs-varid'&gt;a&lt;/span&gt;&lt;span class='hs-keyglyph'&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Simon Peyton Jones lists this problem as &lt;a href="http://research.microsoft.com/~simonpj/papers/haskell-retrospective/index.htm"&gt;Open Challenge #2&lt;/a&gt;, saying:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Commutative monads are very common.  (Environment, 
unique supply, random number generation.)  For these, monads over-sequentialise.&lt;/p&gt; &lt;p&gt;Wanted: theory and notation for some cool compromise.&lt;/p&gt;&lt;/blockquote&gt;

&lt;h3&gt;Commutative monad morphisms&lt;/h3&gt;

&lt;p&gt;&lt;strike&gt;Monad morphisms are the category theory equivalent of Haskell&amp;#8217;s &lt;a href="http://www.haskell.org/all_about_monads/html/transformers.html"&gt;monad transformers&lt;/a&gt;.&lt;/strike&gt; &lt;i&gt;Haskell&amp;#8217;s monad transformers &lt;a href="http://conway.rutgers.edu/~ccshan/wiki/blog/posts/Monad_transformers.html"&gt;can be expressed as&lt;/a&gt; monad layerings, which &lt;a href="http://sigfpe.blogspot.com/2007/02/monads-for-vector-spaces-probability.html#3140655259397671649"&gt;correspond&lt;/a&gt; to the monad morphisms of category theory.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;Many complicated monads &lt;a href="http://www.randomhacks.net/articles/2007/02/21/refactoring-probability-distributions"&gt;break down into a handful of monad transformers&lt;/a&gt;, often in surprising ways.&lt;/p&gt;

&lt;p&gt;But composing monad transformers is a mess, because they interact in poorly-understood ways. In general, the following two types have very different semantics:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='hs-conid'&gt;FooT&lt;/span&gt; &lt;span class='hs-layout'&gt;(&lt;/span&gt;&lt;span class='hs-conid'&gt;BarT&lt;/span&gt; &lt;span class='hs-varid'&gt;m&lt;/span&gt;&lt;span class='hs-layout'&gt;)&lt;/span&gt;
&lt;span class='hs-conid'&gt;BarT&lt;/span&gt; &lt;span class='hs-layout'&gt;(&lt;/span&gt;&lt;span class='hs-conid'&gt;FooT&lt;/span&gt; &lt;span class='hs-varid'&gt;m&lt;/span&gt;&lt;span class='hs-layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If &lt;code&gt;FooT&lt;/code&gt; and &lt;code&gt;BarT&lt;/code&gt; commute with each other, however, the two types would be equivalent. This is helpful when building large stacks of monad transformers. &lt;/p&gt;

&lt;p&gt;Chung-chieh Shan encountered a related problem when applying monad morphisms to build a &lt;a href="http://arxiv.org/abs/cs.CL/0205026"&gt;theory of natural language semantics&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;It remains to be seen whether monads would provide the appropriate 
conceptual encapsulation for a semantic theory with broader coverage. In 
particular, for both natural and programming language semantics, combining monads&amp;#8212;or perhaps monad-like objects&amp;#8212;remains an open issue that 
promises additional insight.&lt;/blockquote&gt;

&lt;h3&gt;Monad morphisms and abstract algebra&lt;/h3&gt;

&lt;p&gt;Dan Piponi has been drawing some fascinating connections between monad morphisms and abstract algebra. See, for example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://sigfpe.blogspot.com/2006/11/why-isnt-listt-monad.html"&gt;Why isn&amp;#8217;t ListT ([]) a monad?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://sigfpe.blogspot.com/2007/02/monads-for-vector-spaces-probability.html"&gt;Monads for vector spaces, probability and quantum mechanics pt. I&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This approach seems to throw a lot of light on monad morphisms&amp;#8212;but at least in my case, the light only highlights my confusion.&lt;/p&gt;

&lt;p&gt;Of the three problems listed here, this is the one most likely to be discussed in a textbook somewhere. And a solution to this problem would likely help significantly with the other two.&lt;/p&gt;

&lt;p&gt;So, my question: Does anybody have any books, papers or ideas that might help untangle this mess?&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Update: Be sure to see the comment thread on the &lt;a href="http://sigfpe.blogspot.com/2007/02/monads-for-vector-spaces-probability.html"&gt;second Dan Piponi post&lt;/a&gt; above and Chung-chieh Shan&amp;#8217;s &lt;a href="http://conway.rutgers.edu/~ccshan/wiki/blog/posts/Monad_transformers.html"&gt;excellent bibliography on monad transformers&lt;/a&gt;.&lt;/i&gt;&lt;/p&gt;</description>
      <pubDate>Mon, 05 Mar 2007 09:32:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:5db7d37a-2c34-4366-b6f0-64877955b837</guid>
      <author>Eric Kidd</author>
      <link>http://www.randomhacks.net/articles/2007/03/05/three-things-i-dont-understand-about-monads</link>
      <category>Haskell</category>
      <category>Math</category>
      <category>Monads</category>
      <trackback:ping>http://www.randomhacks.net/articles/trackback/326</trackback:ping>
    </item>
  </channel>
</rss>
