Random Hacks: Three things I don't understand about monads
http://www.randomhacks.net/articles/2007/03/05/three-things-i-dont-understand-about-monads
en-us40Technology and Other Fun Stuff"Three things I don't understand about monads" by Stefan Ljungstrand<p><span class="caps">CCS </span>: When ‘o’ is a commutative monoid, the ‘Writer o’ monad is commutative, but actions are still not discardable.</p>Tue, 13 Mar 2007 09:08:57 +0000urn:uuid:7751126c-6498-48e9-9398-24f0bd03eb35
http://www.randomhacks.net/articles/2007/03/05/three-things-i-dont-understand-about-monads#comment-359
"Three things I don't understand about monads" by Chung-chieh Shan<p>A quick observation about commutative monads: it seems that most (all?) examples of useful commutative monads also obey the law that “m >> n” is equivalent to “n”. Intuitively, if you don’t need the result of an action in a commutative monad, then you don’t need the effect either.</p>
<p>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.</p>
<p>This law makes me wonder if we can think of >>= in a commutative monad as expressing data flow. I guess I’m not the first person to wonder about overloading function application (aka whitespace or juxtaposition) for some purpose…</p>Sun, 11 Mar 2007 17:16:10 +0000urn:uuid:a7f58cb8-575d-443d-8fa9-55f9b23daa6c
http://www.randomhacks.net/articles/2007/03/05/three-things-i-dont-understand-about-monads#comment-349
"Three things I don't understand about monads" by Neel Krishnaswami<p>The connection between monads and algebras is one that I’m actually very surprised functional programmers don’t use—monads were <em>invented</em> to talk about algebras, and are really beautiful when you need to do things like enrich a syntax with variables (eg, for unification algorithms).</p>
<p>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’s no theory for the <em>operations</em> in a monad, and that’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’ve got get and set, can you derive the state monad?), and enriched categories show up.</p>Wed, 07 Mar 2007 11:57:44 +0000urn:uuid:567aed45-2969-48ed-85b0-f4c26eac0f6f
http://www.randomhacks.net/articles/2007/03/05/three-things-i-dont-understand-about-monads#comment-336
"Three things I don't understand about monads" by sigfpe<p>I agree with <span class="caps">SPJ</span>, I’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.</p>
<p>Hmmm…there would be a neat spinoff from a nice commutative monad notation. It would be easier to write strict code in Haskell.</p>Mon, 05 Mar 2007 13:30:03 +0000urn:uuid:92d283a0-83f5-4301-be95-2ac6fda5808c
http://www.randomhacks.net/articles/2007/03/05/three-things-i-dont-understand-about-monads#comment-329
"Three things I don't understand about monads" by Eric<p>Whether <code>FooT</code> and <code>BarT</code> commute (sometimes? often? always?) depends on the algebraic properties of <code>Foo</code> and <code>Bar</code>. There’s all sorts of neat stuff going on that I can’t quite grasp.</p>
<p>For example, <code>(ListT m)</code> is <a href="http://citeseer.ist.psu.edu/jones93composing.html">only a monad</a> when <code>m</code> is a commutative monad. And as <a href="http://sigfpe.blogspot.com/2006/11/why-isnt-listt-monad.html">Dan showed</a>, this is related to properties of freely-generated semirings.</p>
<p>I suspect that there’s something deeper to be said about these issues, and that it might be important for monad-based DSLs.</p>
<p>Anyway, here’s a list of resources that might be relevant:</p>
<p>- Mark Jones and Luc Duponcheel’s <a href="http://citeseer.ist.psu.edu/jones93composing.html">Composing monads</a>, an early paper on monad transformers that noticed some interesting limits to what could be composed.</p>
<p>- Edmund Robinson’s <a href="http://www.dcs.qmul.ac.uk/tech_reports/RR-02-01.pdf">Variations on Algebra: monadicity and generalisations of
equational theories</a>, which describes a fascinating relationship between initial algebras and monads. (Thanks, alpheccar!)</p>
<p>- According to <a href="http://www.blogger.com/profile/6484344">Michi</a>, enriched categories provide some insight into these kinds of layerings. So far, I’ve found <a href="http://www.tac.mta.ca/tac/reprints/articles/10/tr10abs.html">Basic Concepts of Enriched Category Theory</a> and a <a href="http://en.wikipedia.org/wiki/Enriched_category">Wikipedia article</a>, but I haven’t dug into this area yet (it’s getting a bit heavy for my current knowledge of category theory).</p>Mon, 05 Mar 2007 12:34:23 +0000urn:uuid:cde8046d-aee3-4c17-b6bc-b3cbdfdce19c
http://www.randomhacks.net/articles/2007/03/05/three-things-i-dont-understand-about-monads#comment-328
"Three things I don't understand about monads" by Josef Svenningsson<p>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’s a lot of fun/useful things that can come out that.</p>
<p>As for monad transformers and whether they commute or not: I don’t see the problem. Some monad transformers commute, some don’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.</p>Mon, 05 Mar 2007 11:56:46 +0000urn:uuid:e0318f67-fc9b-4e75-871d-5086e41db87f
http://www.randomhacks.net/articles/2007/03/05/three-things-i-dont-understand-about-monads#comment-327
Three things I don't understand about monads<p>Monads are a remarkably powerful tool for building specialized programming languages. Some examples include:</p>
<ul>
<li><a href="http://www.cs.uu.nl/~daan/parsec.html">Parsers</a></li>
<li><a href="http://www.randomhacks.net/articles/2007/02/22/bayes-rule-and-drug-tests">Bayesian inference</a></li>
<li><a href="http://web.cecs.pdx.edu/~mpj/pubs/modinterp.html">Modular interpreters</a></li>
</ul>
<p>But there’s a bunch of things I don’t understand about monads. In each case, my confusion involves some aspect of the underlying math that “bubbles up” to affect the design of specialized languages.</p>
<p>(Warning: Obscure monad geeking ahead.)</p>
<h3>Commutative monads</h3>
<p>A “commutative monad” is any monad where we can replace the expression:</p>
<div class="typocode"><pre><code class="typocode_haskell "><span class='hs-keyword'>do</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>ma</span>
<span class='hs-varid'>b</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>mb</span>
<span class='hs-varid'>f</span> <span class='hs-varid'>a</span> <span class='hs-varid'>b</span>
</code></pre></div>
<p>…with:</p>
<div class="typocode"><pre><code class="typocode_haskell "><span class='hs-keyword'>do</span> <span class='hs-varid'>b</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>mb</span>
<span class='hs-varid'>a</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>ma</span>
<span class='hs-varid'>f</span> <span class='hs-varid'>a</span> <span class='hs-varid'>b</span>
</code></pre></div>
<p>…without changing the meaning. Examples of commutative monads include <code>Reader</code> and <code>Rand</code>. This is an important property, because it might allow us to parallelize the commonly-used <code>sequence</code> function across huge numbers of processors:</p>
<div class="typocode"><pre><code class="typocode_haskell "><span class='hs-definition'>sequence</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Monad</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>m</span> <span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span>
</code></pre></div>
<p>Simon Peyton Jones lists this problem as <a href="http://research.microsoft.com/~simonpj/papers/haskell-retrospective/index.htm">Open Challenge #2</a>, saying:</p>
<blockquote><p>Commutative monads are very common. (Environment,
unique supply, random number generation.) For these, monads over-sequentialise.</p> <p>Wanted: theory and notation for some cool compromise.</p></blockquote>
<h3>Commutative monad morphisms</h3>
<p><strike>Monad morphisms are the category theory equivalent of Haskell’s <a href="http://www.haskell.org/all_about_monads/html/transformers.html">monad transformers</a>.</strike> <i>Haskell’s monad transformers <a href="http://conway.rutgers.edu/~ccshan/wiki/blog/posts/Monad_transformers.html">can be expressed as</a> monad layerings, which <a href="http://sigfpe.blogspot.com/2007/02/monads-for-vector-spaces-probability.html#3140655259397671649">correspond</a> to the monad morphisms of category theory.</i></p>
<p>Many complicated monads <a href="http://www.randomhacks.net/articles/2007/02/21/refactoring-probability-distributions">break down into a handful of monad transformers</a>, often in surprising ways.</p>
<p>But composing monad transformers is a mess, because they interact in poorly-understood ways. In general, the following two types have very different semantics:</p>
<div class="typocode"><pre><code class="typocode_haskell "><span class='hs-conid'>FooT</span> <span class='hs-layout'>(</span><span class='hs-conid'>BarT</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span>
<span class='hs-conid'>BarT</span> <span class='hs-layout'>(</span><span class='hs-conid'>FooT</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span>
</code></pre></div>
<p>If <code>FooT</code> and <code>BarT</code> commute with each other, however, the two types would be equivalent. This is helpful when building large stacks of monad transformers. </p>
<p>Chung-chieh Shan encountered a related problem when applying monad morphisms to build a <a href="http://arxiv.org/abs/cs.CL/0205026">theory of natural language semantics</a>:</p>
<blockquote>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—or perhaps monad-like objects—remains an open issue that
promises additional insight.</blockquote>
<h3>Monad morphisms and abstract algebra</h3>
<p>Dan Piponi has been drawing some fascinating connections between monad morphisms and abstract algebra. See, for example:</p>
<ul>
<li><a href="http://sigfpe.blogspot.com/2006/11/why-isnt-listt-monad.html">Why isn’t ListT ([]) a monad?</a></li>
<li><a href="http://sigfpe.blogspot.com/2007/02/monads-for-vector-spaces-probability.html">Monads for vector spaces, probability and quantum mechanics pt. I</a></li>
</ul>
<p>This approach seems to throw a lot of light on monad morphisms—but at least in my case, the light only highlights my confusion.</p>
<p>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.</p>
<p>So, my question: Does anybody have any books, papers or ideas that might help untangle this mess?</p>
<p><i>Update: Be sure to see the comment thread on the <a href="http://sigfpe.blogspot.com/2007/02/monads-for-vector-spaces-probability.html">second Dan Piponi post</a> above and Chung-chieh Shan’s <a href="http://conway.rutgers.edu/~ccshan/wiki/blog/posts/Monad_transformers.html">excellent bibliography on monad transformers</a>.</i></p>Mon, 05 Mar 2007 09:32:00 +0000urn:uuid:5db7d37a-2c34-4366-b6f0-64877955b837Eric Kidd
http://www.randomhacks.net/articles/2007/03/05/three-things-i-dont-understand-about-monads
HaskellMathMonadshttp://www.randomhacks.net/articles/trackback/326