<?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>On Clojure</title>
	<atom:link href="http://onclojure.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://onclojure.com</link>
	<description>A blog about everything Clojure</description>
	<lastBuildDate>Fri, 05 Mar 2010 18:15:09 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='onclojure.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/6ef3db6665a831412837c92ff12b649c?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>On Clojure</title>
		<link>http://onclojure.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://onclojure.com/osd.xml" title="On Clojure" />
	<atom:link rel='hub' href='http://onclojure.com/?pushpress=hub'/>
		<item>
		<title>Pre- and post-conditions: a quest for a nicer syntax</title>
		<link>http://onclojure.com/2010/03/05/pre-and-post-conditions-a-quest-for-a-nicer-syntax/</link>
		<comments>http://onclojure.com/2010/03/05/pre-and-post-conditions-a-quest-for-a-nicer-syntax/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 18:15:09 +0000</pubDate>
		<dc:creator>khinsen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://onclojure.com/?p=104</guid>
		<description><![CDATA[When pre- and post-conditions appeared in Clojure (since version 1.1 they are &#8220;official&#8221;), I though that was a pretty neat feature and that I ought to use it. I added conditions to a couple of functions and was satisfied. But rather soon I noticed that I used conditions very sparingly, only where I expected wrong [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=104&subd=onclojure&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>When pre- and post-conditions appeared in Clojure (since version 1.1 they are &#8220;official&#8221;), I though that was a pretty neat feature and that I ought to use it. I added conditions to a couple of functions and was satisfied. But rather soon I noticed that I used conditions very sparingly, only where I expected wrong data to be fed into a function. Considering that bugs also happen where we don&#8217;t expect them, and that conditions are great documentation as well as error-checking code, I <em>should</em> use them more. So why don&#8217;t I?</p>
<p>Something I didn&#8217;t like about pre- and post-conditions is the rather heavy syntax. For short functions, the conditions take up more space than the code itself. Moreover, parsing them visually takes some effort as well, as much as reading the code itself. Wouldn&#8217;t it be nice if preconditions could be written somehow right in the argument list, and postconditions at the level of the function definition?</p>
<p>Well, this is Lisp, so if you don&#8217;t like some syntax, you just roll your own. I didn&#8217;t come up with a better <em>general</em> syntax though, but I think that what I describe below is much nicer and suitable for 90% of pre- and post-conditions used in practice. The main limitation is that each condition can depend on only one argument, or on the return value. For the other cases, there is still Clojure&#8217;s general syntax, which is perfectly compatible with my extension. For those who want to play with this themselves, <a href="http://gist.github.com/322909">here is the code</a>.</p>
<p>As a first example, here&#8217;s a pretty stupid algorithm to calculate integer powers of a number:</p>
<pre>
(defn (number?) power
  [(number?) x
   (integer?) (pos?) n]
  (apply * (repeat n x)))
</pre>
<p>The preconditions check that the first argument is a number and that the second one is a positive integer. The postcondition checks that the result is a a number &#8211; the utility of this test is a bit dubious, but it serves as an illustration. Note that you can have multiple conditions per argument, and also multiple postconditions. The full form representing the condition is constructed by inserting the argument to be tested in the second position of the supplied list. The above function definition actually expands to</p>
<pre>
(defn power
  ([x n]
   {:pre [(number? x) (integer? n) (pos? n)], :post [(number? %)]}
   (apply * (repeat n x))))
</pre>
<p>One precondition in the above example is actually too strict. The argument <code>n</code> needn&#8217;t be positive, just non-negative. There is not simple test function for &#8220;non-negative&#8221; in Clojure, but with the above rule we can write this as:</p>
<pre>
(defn (number?) power
  [(number?) x
   (integer?) (&gt;= 0) n]
  (apply * (repeat n x)))
</pre>
<p>Another possibility is to use the <code>-&gt;</code> macro:</p>
<pre>
(defn (number?) power
  [(number?) x
   (integer?) (-&gt; neg? not) n]
  (apply * (repeat n x)))
</pre>
<p>Preconditions can be combined with destructuring. Here is a variant of Clojure&#8217;s function <code>second</code> that actually verifies that its argument has at least two element:</p>
<pre>
(defn my-second
  [[f &amp; (seq) r]]
  (first r))
</pre>
<p>There is however one limitation: I couldn&#8217;t find a way to use my new syntax with map destructuring. So for now at least it works only with vector destructuring.</p>
<p>Comments on this syntax are welcome. Do you like it? Can you come up with something better? Or do you think that Clojure&#8217;s standard syntax is just fine?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/onclojure.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/onclojure.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/onclojure.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/onclojure.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/onclojure.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/onclojure.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/onclojure.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/onclojure.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/onclojure.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/onclojure.wordpress.com/104/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=104&subd=onclojure&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://onclojure.com/2010/03/05/pre-and-post-conditions-a-quest-for-a-nicer-syntax/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/305e4cf66ef1179f7e95981b1520ba1a?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">khinsen</media:title>
		</media:content>
	</item>
		<item>
		<title>Anonymous function literals and syntax-quote don&#8217;t work well together</title>
		<link>http://onclojure.com/2010/03/01/anonymous-function-literals-and-syntax-quote-dont-work-well-together/</link>
		<comments>http://onclojure.com/2010/03/01/anonymous-function-literals-and-syntax-quote-dont-work-well-together/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 10:43:05 +0000</pubDate>
		<dc:creator>khinsen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://onclojure.com/?p=99</guid>
		<description><![CDATA[Clojure has a couple of macro-like features built right into the reader, providing shorthand notation for commonly needed constructs. However, unlike real macros, which are built on a sound conceptual framework that guarantees arbitrary composability (macro calls can contain macros and expand into other macros), the different macro-like features in the reader don&#8217;t always play [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=99&subd=onclojure&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Clojure has a couple of macro-like features built right into the reader, providing shorthand notation for commonly needed constructs. However, unlike real macros, which are built on a sound conceptual framework that guarantees arbitrary composability (macro calls can contain macros and expand into other macros), the different macro-like features in the reader don&#8217;t always play well together.</p>
<p>Consider the following piece of code:</p>
<pre>
(defmacro foo [x]
  `(map #(identity %) [~x]))
</pre>
<p>At first sight, nothing looks wrong with this, other than it doesn&#8217;t do anything useful. But any use of this macro causes an error message:</p>
<pre>
(foo [:a :b])
java.lang.Exception: Can't use qualified name as parameter: user/p1__3328
</pre>
<p>What&#8217;s going on here? The error message hints at a problem with a function argument. The only function being defined is here <code>#(identity %)</code>, which uses a shorthand notation for function literals expanded by the reader. Let&#8217;s see what the macro call expands to:</p>
<pre>
(macroexpand-1 '(foo [:a :b]))
</pre>
<p>yields</p>
<pre>
(clojure.core/map (fn* [user/p1__3328] (clojure.core/identity user/p1__3328)) [[:a :b]])
</pre>
<p>So here&#8217;s the problem: <code>#(identity %)</code> is expanded by the reader into <code>(fn* [p1__3328] (identity p1__3328))</code>. This is a perfectly valid function literal, but the other reader feature used here, syntax-quote, doesn&#8217;t know about function literals. It takes the expanded function literal as an arbitrary form and does namespace resolution on all symbols. This leads to a namespace-qualified symbol for a function parameter, which is not legal Clojure syntax.</p>
<p>Moral: use reader features sparingly, ideally one at a time. Except for syntax-quote, they only save you a couple of keystrokes, a convenience that you may end up paying a high price for in terms of debugging time.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/onclojure.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/onclojure.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/onclojure.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/onclojure.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/onclojure.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/onclojure.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/onclojure.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/onclojure.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/onclojure.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/onclojure.wordpress.com/99/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=99&subd=onclojure&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://onclojure.com/2010/03/01/anonymous-function-literals-and-syntax-quote-dont-work-well-together/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/305e4cf66ef1179f7e95981b1520ba1a?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">khinsen</media:title>
		</media:content>
	</item>
		<item>
		<title>Generating deftype forms in macros</title>
		<link>http://onclojure.com/2010/02/23/generating-deftype-forms-in-macros/</link>
		<comments>http://onclojure.com/2010/02/23/generating-deftype-forms-in-macros/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 14:36:39 +0000</pubDate>
		<dc:creator>khinsen</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://onclojure.com/?p=86</guid>
		<description><![CDATA[One of the common uses of macros in Clojure, as in other Lisp dialects, is to abstract away boilerplate code. Instead of writing very similar lengthy forms several times, one defines a macro that specializes a template for each particular use, and then uses that macro a few times. The template is usually written using [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=86&subd=onclojure&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>One of the common uses of macros in Clojure, as in other Lisp dialects, is to abstract away boilerplate code. Instead of writing very similar lengthy forms several times, one defines a macro that specializes a template for each particular use, and then uses that macro a few times. The template is usually written using <a href="http://clojure.org/reader#The%20Reader--Macro%20characters">syntax-quote</a>: the template form is preceded by a backquote, and inside the template a tilde marks expressions that are replaced by their values.</p>
<p>Syntax-quote has one more effect: it resolves all symbols in the current namespace (the one in which the macro is <em>defined</em>, not the one where it is used) and replaces the unqualified symbol by its namespace-qualified equivalent. For most symbols in most forms, this is the right thing to do in order to make the macro work in any namespace, as well as to avoid unwanted variable capture. More specifically, it is the right thing to do for symbols that are defined by the macro, and for symbols that will ultimately be evaluated (names referring to vars, in particular function names). It is not the right thing to do for symbols bound locally inside the form (function parameter names, symbols bound in a let form). And it is also not the right thing to do for symbols that just stand for themselves and are used in some special way by the form that the macro expands to.</p>
<p>The latter situation is particularly frequent in macros that generate <a href="http://www.assembla.com/wiki/show/clojure/Datatypes"><code>deftype</code></a> forms. Consider for example the following <code>deftype</code> form, which is a simplified version of the type definition used in my <a href="http://code.google.com/p/clj-multiarray/">multiarray design study</a>:</p>
<pre>
(deftype multiarray

  [descriptor
   data-array]
  :as this

  Object
    (equals [o] ...)
    (hashCode [] ...)

   clojure.lang.Counted
     (count [] ...)

   clojure.lang.Indexed
     (nth [i] ..)

   clojure.lang.Sequential

   clojure.lang.Seqable
     (seq [] ...))
</pre>
<p>Of all the symbols shown in the above example, the only one for which namespace-resolution is appropriate is <code>multiarray</code>, the name of the type being defined. All the other symbols name fields of the type, Java interfaces, or methods. They must remain unqualified. In real-life deftypes, there are of course symbols that could or should be namespace-qualified, in particular most of the symbols used inside the method definitions, which are just like function definitions. However, method definitions are often short, and rarely subject to variable capture, meaning that not namespace-resolving those symbols is rarely a problem.</p>
<p>In a syntax-quote template, there are two ways to deal with symbols for which the default (namespace-resolution) is not appropriate:</p>
<ul>
<li>Prefixing with <code>~'</code> (tilde + quote). This is a special case of an expression inside a template, whose value is the quoted symbol. A tilde-quoted symbol is taken over into the instantiated template without namespace-resolution.</li>
<li>Postfixing with <code>#</code> (hash sign). Such symbols are replaced with system-generated symbols that are guaranteed to be different from any other symbol in existence. This is another technique to avoid variable capture.</li>
</ul>
<p>For generating a <code>deftype</code> form from a syntax-quote template, the only solution is thus to prefix all the symbols shown in the example above with tilde-quote. I tried: it works, but it&#8217;s a mess. It&#8217;s not very readable, and the inevitable mistakes lead to unpleasant error messages.</p>
<p>Well, this is Lisp, and in Lisp you are always free to make your own tools if you are not happy with the ones provided by the system. What I want here is a template expansion system that doesn&#8217;t do namespace resolution on symbols. However, I didn&#8217;t need a full-blown equivalent to syntax-quote templates either, given that I would use those <code>deftype</code> templates only for one application. So I came up with the following definitions, which for me are the right compromise between simplicity and useability:</p>
<pre>
(defn instantiate-template
  [substitution-map form]
  (clojure.walk/prewalk
   (fn [x] (if (and (sequential? x) (= (first x) 'clojure.core/unquote))
	     (substitution-map (second x))
	     x))
   form))

(defmacro template
  [substitutions form]
  (let [substitution-map (into {} (map (fn [[a b]]
					 [(list 'quote a) b])
				       (partition 2 substitutions)))]
    `(instantiate-template ~substitution-map (quote ~form))))
</pre>
<p>Compared to syntax-quote, this has two restrictions: it has no splicing, and it admits only symbols after a tilde, not arbitrary expressions. The <code>template</code> macro takes a let-like vector as its first argument. This vector contains the symbol-value pairs for substitution inside the template. The second argument is the template form, which presumably contains tilde-prefixed symbols for substitution. Note that the Clojure reader translates <code>~x</code> to &lt;code (clojure.core/unquote x)</code>, which is what the above code searches for.</p>
<p>Here is an example for using such templates:</p>
<pre>
(defmacro foo [typename fieldname]
  (template [type  typename
	     field fieldname]
    (deftype ~type
      [~field])))

(foo bar boo)

(bar 42)
</pre>
<p>This prints <code>#:bar{:boo 2}</code>, illustrating that the macros does what it is expected to do. Of course this is not the perfect example for the utility of my little template instantiation system, as it could just as well be written using syntax-quote!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/onclojure.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/onclojure.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/onclojure.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/onclojure.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/onclojure.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/onclojure.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/onclojure.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/onclojure.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/onclojure.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/onclojure.wordpress.com/86/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=86&subd=onclojure&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://onclojure.com/2010/02/23/generating-deftype-forms-in-macros/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/305e4cf66ef1179f7e95981b1520ba1a?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">khinsen</media:title>
		</media:content>
	</item>
		<item>
		<title>Managing namespaces</title>
		<link>http://onclojure.com/2010/02/17/managing-namespaces/</link>
		<comments>http://onclojure.com/2010/02/17/managing-namespaces/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 14:38:24 +0000</pubDate>
		<dc:creator>khinsen</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Libraries]]></category>

		<guid isPermaLink="false">http://onclojure.com/?p=80</guid>
		<description><![CDATA[One aspect of Clojure that I have not been quite happy with is namespace management. In a bigger project that consists of several namespaces, I usually end up having nearly identical :use and :require clauses in the initial ns form. These clauses set up the project-specific set of symbols that I want to work with. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=80&subd=onclojure&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>One aspect of Clojure that I have not been quite happy with is namespace management. In a bigger project that consists of several namespaces, I usually end up having nearly identical <tt>:use</tt> and <tt>:require</tt> clauses in the initial <tt>ns</tt> form. These clauses set up the project-specific set of symbols that I want to work with. Individual namespaces sometimes add symbols for their specific needs, of course. What bothers me is that I have to repeat the <tt>:use</tt> and <tt>:require</tt> clauses, often with <tt>:exclude</tt> or <tt>:</tt><tt>only</tt> options with many symbols, in every single namespace. And of course I often forget a copy when updating my symbol set. Therefore I decided to look at how namespaces work in more detail, and try to find a better way to manage symbols in namespaces.</p>
<p>For those who don&#8217;t want to read all the explanations, my solution (still a bit experimental, for the moment), is in my <a href="http://code.google.com/p/clj-nstools/">nstools library</a>, which is also on <a href="http://clojars.org/nstools">Clojars</a>.</p>
<p>As most Clojure programmers know, a namespace maps symbols to vars. Vars are mutable storage locations with well defined concurrency semantics, but this is not the topic of this post &#8211; see the <a href="http://clojure.org/vars">documentation</a> for details. But a namespace is not a simple map. To start with, a namespace stores two maps: one from symbols to their values, and one from namespace aliases to namespaces. Aliases are usually created using a <tt>(:require ... :as ...)</tt> clause in the <tt>ns</tt> form that opens a namespace. They are used in namespace-qualified symbols before the slash, as a shorthand for the full namespace name. Since aliases are used before the slash and namespace-local symbols are used after the slash (or in an unqualified name with no slash at all), there is no conflict between the two. It is thus possible to use the same symbol both as an alias and as a regular symbol in the same namespace.</p>
<p>The main symbol-to-value map is also not quite as simple as it seems. The values it stores are not always vars. A symbol can also have a Java class as its value. A symbol-to-class entry is created using <tt>import</tt> or using the <tt>:import</tt> clause in <tt>ns</tt>. A submap containing only the symbol-to-class entries of the namespace map can be obtained by calling <tt>ns-imports</tt>. Finally, the symbol-to-var entries can be divided up into two categories: those that refer to vars in the same namespace (created by <tt>def</tt> and the many macros based on it), and those that refer to vars in some other namespace. The latter are created with <tt>use</tt> or the <tt>:use</tt> clause of <tt>ns</tt>, and the submap of these symbols can be obtained by calling <tt>ns-refers</tt>. The first category, a submap of symbols to vars defined in the same namespace, is the return value of <tt>ns-interns</tt>.</p>
<p>There is one more subtlety, and an undocumented one as far as I know: Two symbols, <tt>ns</tt> and <tt>in-ns</tt>, are put in the namespace map when the namespace is created, and can&#8217;t be removed (using <tt>ns-unmap</tt>) nor redefined. This makes sense because they refer to a macro and a function needed to create new namespaces and to switch namespaces. Having them in every namespace (referring to vars in <tt>clojure.core</tt>) ensures that it is always possible to get out of the current namespace.</p>
<p>Next, let&#8217;s look at how namespaces are set up in Clojure. Pretty much all the namespace management functionality is available through the standard <tt>ns</tt> form with its various clauses and options. The one exception is removing symbols, which can be done only by calling <tt>ns-unmap</tt> explicitly. The <tt>ns</tt> form first switches to the namespace it defines, creating it if necessary. The second step is to add references to all public vars defined in namespace <tt>clojure.core</tt>. This step can be modified by specifying a <tt>:refer-clojure</tt> clause that lists the symbols to include or exclude. Then <tt>ns</tt> goes through its optional clauses. A <tt>:require</tt> clause loads another namespace, but doesn&#8217;t normally modify the namespace under construction. Only if the option <tt>:as</tt> is specified, there is an impact on the namespace: an alias is added. A <tt>:use</tt> clause first does a <tt>:require</tt> and then adds all of the newly loaded namespace&#8217;s public vars to the symbol table of the current namespace. The options <tt>:exclude</tt> and <tt>:</tt><tt>only</tt> can be used to select a subset of the public vars. Finally, an <tt>:import</tt> clause adds Java classes to the namespace&#8217;s symbol table.</p>
<p>The most dangerous, but also most convenient, <tt>ns</tt> clause is <tt>:use</tt>. In its basic form, it adds all public vars of another namespace to the symbol table of the namespace under construction. And once those symbols are there, they cannot be redefined in the namespace, except by first removing them using <tt>ns-unmap</tt>. The problem is that &#8220;all public vars of namespace X&#8221; is not something under your control. It&#8217;s the author of the <i>other</i> namespace who decides which symbols you get in <i>your</i> namespace. The next release of namespace X may well have a few more public definitions, and if those are in conflict with your own definitions, then your module will fail to load. Therefore, as a security measure, you should use the <tt>:</tt><tt>only</tt> option of <tt>:use</tt> with all namespaces that are out of your control, listing explicitly the definitions that you need, in order to be certain that you don&#8217;t get more than you expect. Unfortunately, this includes <tt>clojure.core</tt>, which also grows with every new Clojure release. To be on the safe side, you should have a <tt>:refer-clojure</tt> clause with the <tt>:</tt><tt>only</tt> in every namespace that you intend to maintain for a longer time.</p>
<p>So far for what I have, but what do I want? I&#8217;d like to be able to set up a namespace to my taste and then be able to use it as a basis for deriving other namespaces. With that possibility, I would define a master namespace once per project, being careful to always use the <tt>:</tt><tt>only</tt> option in <tt>:refer-clojure</tt> and <tt>:use</tt>. All other namespaces in my project would then be based on this master namespace and only add or remove symbols for their specific local needs.</p>
<p>To implement this functionality, I added three new clauses to <tt>ns</tt>. The <tt>:like</tt> clause takes a namespace as its only argument and adds all symbols from that namespace that refer to vars in yet another namespace to the current namespace (make sure you read this properly; there are at least three namespaces involved here!). The <tt>:clone</tt> clause does the same but also adds the symbols defined in the other namespace. In other words, <tt>:clone</tt> is equivalent to <tt>:like</tt> followed by <tt>:use</tt>. The third new clause is <tt>:remove</tt>, whose arguments are symbols to be removed from the namespace. It is explicitly allowed to &#8220;remove&#8221; symbols that aren&#8217;t there. This creates another way to protect one&#8217;s namespace against future extensions in namespaces that are <tt>:use</tt>d: simply add all symbols defined in your namespace to the <tt>:remove</tt> list.</p>
<p>The above paragraph contains a small lie: I didn&#8217;t add anything to <tt>ns</tt>, of course, though that&#8217;s what I would have liked to do. I made a copy of <tt>ns</tt> and added the new clauses to the copy. The copy is in namespace <tt>nstools.ns</tt> and it&#8217;s called <tt>ns+</tt> &#8211; as explained above, I cannot call it <tt>ns</tt>. So to use nstools, you have to replace <tt>ns</tt> by <tt>ns+</tt> and put a <tt>(use 'nstools.ns)</tt> before it.</p>
<p>As I said, this library is still a bit experimental. I am not sure for example if both <tt>:like</tt> and <tt>:clone</tt> are necessary. And perhaps <tt>:remove</tt> should be called <tt>:exclude</tt>. Of course, any feedback is welcome!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/onclojure.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/onclojure.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/onclojure.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/onclojure.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/onclojure.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/onclojure.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/onclojure.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/onclojure.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/onclojure.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/onclojure.wordpress.com/80/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=80&subd=onclojure&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://onclojure.com/2010/02/17/managing-namespaces/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/305e4cf66ef1179f7e95981b1520ba1a?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">khinsen</media:title>
		</media:content>
	</item>
		<item>
		<title>Protecting mutable state in the state monad</title>
		<link>http://onclojure.com/2009/06/24/protecting-mutable-state-in-the-state-monad/</link>
		<comments>http://onclojure.com/2009/06/24/protecting-mutable-state-in-the-state-monad/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 16:19:54 +0000</pubDate>
		<dc:creator>khinsen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://onclojure.com/?p=73</guid>
		<description><![CDATA[In part 3 of my monad tutorial for Clojure programmers, I explained that the main purpose of the state monad is to facilitate the implementation of stateful algorithms in a purely functional way. Here I will show a somewhat different use of the state monad: protecting mutable data in such a way that it is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=73&subd=onclojure&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://onclojure.com/2009/03/23/a-monad-tutorial-for-clojure-programmers-part-3/">part 3</a> of my monad tutorial for Clojure programmers, I explained that the main purpose of the state monad is to facilitate the implementation of stateful algorithms in a purely functional way. Here I will show a somewhat different use of the state monad: protecting mutable data in such a way that it is guaranteed not to be used incorrectly. I will use I/O as my example because I expect that everyone is familiar with it, but the same method can also be applied to modify mutable Java data structures, such as arrays, in a referentially transparent fashion.</p>
<p>The example I will use is stream-based I/O, based on the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/io/package-summary.html">java.io</a> library. Writing to such a stream has side-effects, so it is certainly not purely functional. But even reading from a stream is not purely functional: every time you call the read method, you get another return value. A stream object therefore represents mutable state. Passing around such an object among functions in a program makes it difficult to verify that the stream is read or written to properly.</p>
<p>Here is the basic idea of protecting state in the state monad. Suppose that the only way to create your mutable object is through a function that takes a state monad value as its argument. The function creates the object, calls the state monad value on it, and then destroys the object. Client code never obtains a reference to the object, so the only way to act on it is through state monad values that define useful operations on the object (such as &#8220;read a line from the stream&#8221;). To add another layer of protection, the object is wrapped in a protective data structure (such as a closure) before being passed to the state monad value. Only a well-defined set of state monad values gets the key to access the object, meaning that only those clearly identified operations can act on the object. You can then use those operations, and combine them in the state monad to define more complex operations. But no matter how you try, you will never get a reference to the object that you could assign to a var, pass to some function, or (ab-)use in any other way.</p>
<p>For stream-based I/O, this approach is implemented in the library <a href="http://github.com/richhickey/clojure-contrib/blob/f9b111c67752220c9d45a7d6ef22c6eecf400c87/src/clojure/contrib/monadic_io_streams.clj">clojure.contrib.monadic-io-streams</a>. Before trying any of the examples below, you have to evaluate the following form that takes care of importing the libraries:</p>
<pre>
(ns monadic-io-demo
  (:refer-clojure :exclude (read-line print println flush))
  (:use [clojure.contrib.monadic-io-streams])
  (:use [clojure.contrib.monads]))
</pre>
<p>The <code>:refer-clojure</code> clause is necessary because clojure.contrib.monadic-io-streams defines a couple of names that are also defined in clojure.core. In general this is not a good idea, but here the names are the same as those of the Java functions that are being called, which is a useful feature as well. The number of good names for functions is unfortunately not unlimited!</p>
<p>With the bookkeeping done, let&#8217;s look at a basic application:</p>
<pre>
(with-reader "my-file.txt"
  (read-line))
</pre>
<p>This returns the first line of the text file &#8220;my-file.txt&#8221;. To understand how this works, here is the definition of read-line:</p>
<pre>
(defn read-line
  []
  (fn [s] [(.readLine (unlock s)) s]))
</pre>
<p>The call <code>(read-line)</code> thus returns a state monad value: a function that takes a state argument <code>s</code>, calls the Java method <code>readLine</code> on the unlocked state, and returns a vector containing the freshly read line and the state argument. The function unlock is defined locally in a let form and is thus inaccessible from the outside. It retrieves the real state value from the wrapper that only serves to protect it.</p>
<p>Next, we need to look at <code>with-reader</code>:</p>
<pre>
(defn with-reader
  [reader-spec statement]
  (with-open [r (reader reader-spec)]
    (first (statement (lock r)))))
</pre>
<p>The function <code>reader</code> that it calls comes from clojure.contrib.duck-streams. It creates the stream reader object which is then locked (wrapped inside a closure) and passed to <code>statement</code>, which happens to be the state monad value returned by <code>(read-line)</code>. The <code>with-open</code> macro ensures that the reader is closed. The return value of the with-reader function is the first item in the return value of the monadic statement; the second item is the state which is of no interest any more.</p>
<p>There are two levels of protection here: first, the reader object is never made accessible to the outside world. It is created, injected into the monadic statement, and then made invalid by closing it. The only way to get a reference would be write a monadic statement that exposes the state. This is indeed possible, and the statement is even provided under the name <code>fetch-state</code> in clojure.contrib.monads. The following piece of code returns the state value:</p>
<pre>
(with-reader "my-file.txt"
  (fetch-state))
</pre>
<p>But here the second level of protection takes over: the return value is the <em>locked</em> version of the state, which happens to be a closure. That closure must be called with the right key in order to unlock the state, but the key is not accessible anywhere. Only a handful of functions in clojure.contrib.monadic-io-streams can unlock the state and work on it.</p>
<p>The typical way to do more complex I/O using this monadic approach is to define complex I/O statements by composing the basic I/O statements (<code>read</code>, <code>read-line</code>, <code>write</code>, <code>print</code>, etc.) in the state monad. This permits the construction of arbitrary I/O code, all in a purely functional way. In the end, the compound I/O statement is applied to an I/O stream using <code>with-reader</code> or <code>with-writer</code>. That part is necessarily not purely functional: when reading a file, nothing guarantees that the file will be the same every time it is read. But the non-functional part is now localized in a single place, and the complex aspect of I/O, the composition of I/O statements to do the required work, is purely functional.</p>
<p>As I said earlier, the same approach can be applied to working with mutable arrays. There would be function <code>make-array</code> that takes a monadic array-modifying statement as its argument. This function would create an array, run it through the statement, and return the resulting modified array. The only array-modifying functions would be defined as state monad values. The net result would be a referentially transparent way to create a new array and have it initialized by an arbitrary algorithm. However, once returned from <code>make-array</code>, the array would be immutable.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/onclojure.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/onclojure.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/onclojure.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/onclojure.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/onclojure.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/onclojure.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/onclojure.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/onclojure.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/onclojure.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/onclojure.wordpress.com/73/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=73&subd=onclojure&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://onclojure.com/2009/06/24/protecting-mutable-state-in-the-state-monad/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/305e4cf66ef1179f7e95981b1520ba1a?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">khinsen</media:title>
		</media:content>
	</item>
		<item>
		<title>Simulating dynamic scoping</title>
		<link>http://onclojure.com/2009/05/06/simulating-dynamic-scoping/</link>
		<comments>http://onclojure.com/2009/05/06/simulating-dynamic-scoping/#comments</comments>
		<pubDate>Wed, 06 May 2009 16:36:08 +0000</pubDate>
		<dc:creator>khinsen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://onclojure.com/?p=63</guid>
		<description><![CDATA[In the early days of Lisp, and programming languages in general, scoping was a hot issue of debate. What is better, lexical scoping or dynamic scoping?
Scoping refers to where a function looks for the definitions of symbols that it doesn&#8217;t have locally. Given (fn [x] (+ b x)), where does b come from? With lexical [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=63&subd=onclojure&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>In the early days of Lisp, and programming languages in general, scoping was a hot issue of debate. What is better, lexical scoping or dynamic scoping?</p>
<p>Scoping refers to where a function looks for the definitions of symbols that it doesn&#8217;t have locally. Given <code>(fn [x] (+ b x))</code>, where does <code>b</code> come from? With <em>lexical scoping</em>, b is taken from the lexical environment, i.e. the forms surrounding the function definition, or from the global namespace if the lexical environment doesn&#8217;t have a <code>b</code>. The lookup typically happens when the function is compiled. With <i>dynamic scoping</i>, the lookup happens at runtime and by following the call stack: first the calling function is checked for a definition of <code>b</code>, then the calle&#8217;s caller, etc.</p>
<p>By now the issue has been settled: lexical scoping is the default in all modern programming languages, including all modern Lisp dialects. And that includes Clojure, of course. Lexical scoping is more predictable and permits compile-time analysis of which value a symbol refers to. It also permits closures, which have become a popular technique in functional programming.</p>
<p>However, dynamic scoping is of use in some occasions and it can in fact be simulated in Clojure. In this post I will show how and what to watch out for.</p>
<p>First of all, why would one want dynamic scoping? Here is one example. Recently I wrote an<a href="http://code.google.com/p/clojure-contrib/source/browse/trunk/src/clojure/contrib/macro_utils.clj"> implementation of macrolet and symbol-macrolet</a> for Clojure. These are both macros that modify the macro expansion procedure by adding local macro definitions. This means that a stack of macro definitions must be maintained: each macrolet adds definitions to the stack, and at the end of the macrolet form the new definitions are popped off again.</p>
<p>One usual way to handle this would be to pass the stack around among the functions that do the macro expansion, which could modify it as needed. Another approach would be hiding this passed-around state using the state monad (see <a href="http://onclojure.com/2009/03/23/a-monad-tutorial-for-clojure-programmers-part-3/">part 3</a> of my monad tutorial). But in the specific situation of macro expansion, Clojure&#8217;s <code>macroexpand-1</code> enters into the call chain. I couldn&#8217;t modify this function to pass on the macro definition stack, so I had to work around it in some way. I could have avoided using <code>macroexpand-1</code>, for example. But I chose to try simulating dynamic scoping at this occasion.</p>
<p>With dynamic scoping, a function that wants to modify the stack just redefines the variable containing it, calls the expansion recursively, and sets the stack back to its old value. A function accessing the stack just uses the current value of the variable, which is looked up in the call chain.</p>
<p>How can this be simulated in Clojure? The obvious candidate is <code>binding</code>. The <code>binding</code> form redefines a var in a namespace for the duration of the execution of its body. The redefinition is valid only inside the thread that is being executed, so other threads are not affected. In my macro expansion code, one of the stack vars is defined by</p>
<pre>
(defvar- macro-fns {})
</pre>
<p>and modified in each call to <code>macrolet</code>:</p>
<pre>
(defmacro macrolet
  [fn-bindings &amp; exprs]
  (let [names      (map first fn-bindings)
	name-map   (into {} (map (fn [n] [(list 'quote n) n]) names))
	macro-map  (eval `(letfn ~fn-bindings ~name-map))]
    (binding [macro-fns     (merge macro-fns macro-map)
	      macro-symbols (apply dissoc macro-symbols names)]
      `(do ~@(map expand-all exprs)))))
</pre>
<p>Well, that&#8217;s <em>almost</em> all there is to say about it. Except that the definition of <code>macrolet</code> given above does not work.</p>
<p>The problem is laziness. The <code>binding</code> form changes the definition of macro-fns only for the duration of the execution of its body and then resets it to its previous value. But the execution of the body is just a call to <code>map</code>. This doesn&#8217;t actually do anything, it merely returns a package that calls <code>expand-all</code> as soon as the first element of the sequence is requested. And that happens only after the <code>binding</code> form has been left.</p>
<p>Once the problem is identified, the solution is simple: add a <code>doall</code> around the <code>map</code>:</p>
<pre>
(defmacro macrolet
  [fn-bindings &amp; exprs]
  (let [names      (map first fn-bindings)
	name-map   (into {} (map (fn [n] [(list 'quote n) n]) names))
	macro-map  (eval `(letfn ~fn-bindings ~name-map))]
    (binding [macro-fns     (merge macro-fns macro-map)
	      macro-symbols (apply dissoc macro-symbols names)]
      `(do ~@(doall (map expand-all exprs))))))
</pre>
<p>However, adding all then necessary doalls requires careful attention. Forgetting does cause errors, but not necessarily immediately. For this reason, simulating dynamic scoping should be avoided except when there is a good reason. In retrospect, I am not even sure if my reason was good enough, and perhaps one day I will rewrite the code in a different way.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/onclojure.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/onclojure.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/onclojure.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/onclojure.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/onclojure.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/onclojure.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/onclojure.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/onclojure.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/onclojure.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/onclojure.wordpress.com/63/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=63&subd=onclojure&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://onclojure.com/2009/05/06/simulating-dynamic-scoping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/305e4cf66ef1179f7e95981b1520ba1a?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">khinsen</media:title>
		</media:content>
	</item>
		<item>
		<title>A monad tutorial for Clojure programmers (part 4)</title>
		<link>http://onclojure.com/2009/04/24/a-monad-tutorial-for-clojure-programmers-part-4/</link>
		<comments>http://onclojure.com/2009/04/24/a-monad-tutorial-for-clojure-programmers-part-4/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 16:10:24 +0000</pubDate>
		<dc:creator>khinsen</dc:creator>
				<category><![CDATA[Libraries]]></category>

		<guid isPermaLink="false">http://onclojure.com/?p=51</guid>
		<description><![CDATA[In this fourth and last part of my monad tutorial, I will write about monad transformers. I will deal with only one of them, but it&#8217;s a start. I will also cover the probability monad, and how it can be extended using a monad transformer.
Basically, a monad transformer is a function that takes a monad [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=51&subd=onclojure&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>In this fourth and last part of my monad tutorial, I will write about monad transformers. I will deal with only one of them, but it&#8217;s a start. I will also cover the probability monad, and how it can be extended using a monad transformer.</p>
<p>Basically, a monad transformer is a function that takes a monad argument and returns another monad. The returned monad is a variant of the one passed in to which some functionality has been added. The monad transformer defines that added functionality. Many of the  common monads that I have presented before have monad transformer analogs that add the monad&#8217;s functionality to another monad. This makes monads modular by permitting client code to assemble monad building blocks into a customized monad that is just right for the task at hand.</p>
<p>Consider two monads that I have discussed before: the maybe monad and the sequence monad. The maybe monad is for computations that can fail to produce a valid value, and return nil in that case. The sequence monad is for computations that return multiple results, in the form of monadic values that are sequences. A monad combining the two can take two forms: 1) computations yielding multiple results, any of which could be <code>nil</code> indicating failure 2) computations yielding either a sequence of results or <code>nil</code> in the case of failure. The more interesting combination is 1), because 2) is of little practical use: failure can be represented more easily and with no additional effort by returning an empty result sequence.</p>
<p>So how can we create a monad that puts the maybe monad functionality inside sequence monad values? Is there a way we can reuse the existing implementations of the maybe monad and the sequence monad? It turns out that this is not possible, but we can keep one and rewrite the other one as a monad transformer, which we can then apply to the sequence monad (or in fact some other monad) to get the desired result. To get the combination we want, we need to turn the maybe monad into a transformer and apply it to the sequence monad.</p>
<p>First, as a reminder, the definitions of the maybe and the sequence monads:</p>
<pre>
(defmonad maybe-m
   [m-zero   nil
    m-result (fn [v] v)
    m-bind   (fn [mv f]
               (if (nil? mv) nil (f mv)))
    m-plus   (fn [&amp; mvs]
               (first (drop-while nil? mvs)))
    ])

(defmonad sequence-m
   [m-result (fn [v]
               (list v))
    m-bind   (fn [mv f]
               (apply concat (map f mv)))
    m-zero   (list)
    m-plus   (fn [&amp; mvs]
               (apply concat mvs))
    ])
</pre>
<p>And now the definition of the maybe monad transformer:</p>
<pre>
(defn maybe-t
  [m]
  (monad [m-result (with-monad m m-result)
          m-bind   (with-monad m
                     (fn [mv f]
                       (m-bind mv
                               (fn [x]
                                 (if (nil? x)
                                   (m-result nil)
                                   (f x))))))
          m-zero   (with-monad m m-zero)
          m-plus   (with-monad m m-plus)
          ]))
</pre>
<p>The real definition in clojure.contrib.monads is a bit more complicated, and I will explain the differences later, but for now this basic version is good enough. The combined monad is constructed by</p>
<pre>
(def maybe-in-sequence-m (maybe-t sequence-m))
</pre>
<p>which is a straightforward function call, the result of which is a monad. Let&#8217;s first look at what <code>m-result</code> does. The <code>m-result</code> of <code>maybe-m</code> is the identity function, so we&#8217;d expect that our combined monad <code>m-result</code> is just the one from <code>sequence-m</code>. This is indeed the case, as <code>(with-monad m m-result) </code>returns the <code>m-result</code> function from monad <code>m</code>. We see the same construct for <code>m-zero</code> and <code>m-plus</code>, meaning that all we need to understand is <code>m-bind</code>.</p>
<p>The combined <code>m-bind</code> calls the <code>m-bind</code> of the base monad (<code>sequence-m</code> in our case), but it modifies the function argument, i.e. the function that represents the rest of the computation. Before calling it, it first checks if its argument would<br />
be <code>nil</code>. If it isn&#8217;t, the original function is called, meaning that the combined monad behaves just like the base monad as long as no computation ever returns <code>nil</code>. If there is a <code>nil</code> value, the maybe monad says that no further computation should take place and that the final result should immediately be <code>nil</code>. However, we can&#8217;t just return <code>nil</code>, as we must return a valid monadic value in the combined monad (in our example, a sequence of possibly-<code>nil</code> values). So we feed nil into the base monad&#8217;s <code>m-result</code>, which takes care of wrapping up <code>nil</code> in the required data structure.</p>
<p>Let&#8217;s see it in action:</p>
<pre>
(domonad maybe-in-sequence-m
  [x [1 2 nil 4]
   y [10 nil 30 40]]
  (+ x y))
</pre>
<p>The output is:</p>
<pre>
(11 nil 31 41 12 nil 32 42 nil 14 nil 34 44)
</pre>
<p>As expected, there are all the combinations of non-<code>nil</code> values in both input sequences. However, it is surprising at first sight that there are four <code>nil</code> entries. Shouldn&#8217;t there be eight, resulting from the combinations of a <code>nil</code> in one sequence with the four values in the other sequence?</p>
<p>To understand why there are four <code>nil</code>s, let&#8217;s look again at how the <code>m-bind</code> definition in <code>maybe-t</code> handles them. At the top level, it will be called with the vector <code>[1 2 nil 4]</code> as the monadic value. It hands this to the <code>m-bind</code> of <code>sequence-m</code>, which calls the<br />
anonymous function in <code>maybe-t</code>&#8217;s <code>m-bind</code> four times, once for each element of the vector. For the three non-<code>nil</code> values, no special treatment is added. For the one <code>nil</code> value, the net result of the computation is <code>nil</code> and the rest of the computation is never called. The <code>nil</code> in the first input vector thus accounts for one <code>nil</code> in the result, and the rest of the computation is called three times. Each of these three rounds produces then three valid results and one <code>nil</code>. We thus have 3&#215;3 valid results, 3&#215;1 <code>nil</code> from the second vector, plus the one <code>nil</code> from the first vector. That makes nine valid results and four <code>nil</code>s.</p>
<p>Is there a way to get all sixteen combinations, with all the possible <code>nil</code> results in the result? Yes, but not using the <code>maybe-t</code> transformer. You have to use the maybe and the sequence monads separately, for example like this:</p>
<pre>
(with-monad maybe-m
  (def maybe-+ (m-lift 2 +)))

(domonad sequence-m
  [x [1 2 nil 4]
   y [10 nil 30 40]]
  (maybe-+ x y))
</pre>
<p>When you use <code>maybe-t</code>, you always get the shortcutting behaviour seen above: as soon as there is a <code>nil</code>, the total result is <code>nil</code> and the rest of the computation is never executed. In most situations, that&#8217;s what you want.</p>
<p>The combination of <code>maybe-t</code> and <code>sequence-m</code> is not so useful in practice because a much easier (and more efficient) way to handle invalid results is to remove them from the sequences before any further processing happens. But the example is simple and thus fine for explaining the basics. You are now ready for a more realistic example: the use of <code>maybe-t</code> with the<br />
probability distribution monad.</p>
<p>The probability distribution monad is made for working with finite probability distributions, i.e. probability distributions in which a finite set of values has a non-zero probability. Such a distribution is represented by a map from the values to their probabilities. The monad and various useful functions for working with finite distributions is defined in the<br />
library <a href="http://code.google.com/p/clojure-contrib/source/browse/trunk/src/clojure/contrib/probabilities/finite_distributions.clj">clojure.contrib.probabilities.finite-distributions</a>.</p>
<p>A simple example of a finite distribution:</p>
<pre>
(use 'clojure.contrib.probabilities.finite-distributions)
(def die (uniform #{1 2 3 4 5 6}))
(prob odd? die)
</pre>
<p>This prints <code>1/2</code>, the probability that throwing a single die yields an odd number. The value of <code>die</code> is the probability distribution of the outcome of throwing a die:</p>
<pre>
{6 1/6, 5 1/6, 4 1/6, 3 1/6, 2 1/6, 1 1/6}
</pre>
<p>Suppose we throw the die twice and look at the sum of the two values. What is its probability distribution? That&#8217;s where the monad comes in:</p>
<pre>
(domonad dist-m
  [d1 die
   d2 die]
  (+ d1 d2))
</pre>
<p>The result is:</p>
<pre>
{2 1/36, 3 1/18, 4 1/12, 5 1/9, 6 5/36, 7 1/6, 8 5/36, 9 1/9, 10 1/12, 11 1/18, 12 1/36}
</pre>
<p>You can read the above domonad block as &#8216;draw a value from the distribution <code>die</code> and call it <code>d1</code>, draw a value from the distribution <code>die</code> and call it <code>d2</code>, then give me the distribution of <code>(+ d1 d2)</code>&#8216;. This is a very simple example; in general, each distribution can depend on the values drawn from the preceding ones, thus creating the joint distribution of several variables. This approach is known as &#8216;ancestral sampling&#8217;.</p>
<p>The monad <code>dist-m</code> applies the basic rule of combining probabilities: if event A has probability p and event B has probability q, and if the events are independent (or at least uncorrelated), then the probability of the combined event (A and B) is p*q. Here is the definition of <code>dist-m</code>:</p>
<pre>
(defmonad dist-m
  [m-result (fn [v] {v 1})
   m-bind   (fn [mv f]
	      (letfn [(add-prob [dist [x p]]
		         (assoc dist x (+ (get dist x 0) p)))]
	        (reduce add-prob {}
		        (for [[x p] mv  [y q] (f x)]
			  [y (* q p)]))))
   ])
</pre>
<p>As usually, the interesting stuff happens in <code>m-bind</code>. Its first argument, <code>mv</code>, is a map representing a probability distribution. Its second argument, <code>f</code>, is a function representing the rest of the calculation. It is called for each possible value in the probability distribution in the <code>for</code> form. This <code>for</code> form iterates over both the possible values of the input distribution and the possible values of the distribution returned by <code>(f x)</code>, combining the probabilities by multiplication and putting them into the output distribution. This is done by reducing over the helper function <code>add-prob</code>, which checks if the value is already present in the map, and if so, adds the probability to the previously obtained one. This is necessary because the samples from the <code>(f x)</code> distribution can contain the same value more than once if they were obtained for different <code>x</code>.</p>
<p>For a more interesting example, let&#8217;s consider the famous <a href="http://en.wikipedia.org/wiki/Monty_Hall_problem">Monty Hall problem</a>. In a game show, the player faces three doors. A prize is waiting for him behind one of them, but there is nothing behind the two other ones. If he picks the right door, he gets the prize. Up to there, the problem is simple: the probability of winning is 1/3.</p>
<p>But there is a twist. After the player makes his choice, the game host open one of the two other doors, which shows an empty space. He then asks the player if he wants to change his mind and choose the last remaining door instead of his initial choice. Is this a good strategy?</p>
<p>To make this a well-defined problem, we have to assume that the game host knows where the prize is and that he would not open the corresponding door. Then we can start coding:</p>
<pre>
(def doors #{:A :B :C})

(domonad dist-m
  [prize  (uniform doors)
   choice (uniform doors)]
  (if (= choice prize) :win :loose))
</pre>
<p>Let&#8217;s go through this step by step. First, we choose the prize door by drawing from a uniform distribution over the three doors <code>:A</code>, <code>:B</code>, and <code>:C</code>. That represents what happens before the player comes in. Then the player&#8217;s initial choice is made, drawing from the same distribution. Finally, we ask for the distribution of the outcome of the game,  code&gt;:win</code> or <code>:loose</code>. The answer is, unsurprisingly, <code>{:win 1/3, :loose 2/3}</code>.</p>
<p>This covers the case in which the player does not accept the host's proposition to change his mind. If he does, the game becomes more complicated:</p>
<pre>
(domonad dist-m
  [prize  (uniform doors)
   choice (uniform doors)
   opened (uniform (disj doors prize choice))
   choice (uniform (disj doors opened choice))]
  (if (= choice prize) :win :loose))
</pre>
<p>The third step is the most interesting one: the game host opens a door which is neither the prize door nor the initial choice of the player. We model this by removing both prize and choice from the set of doors, and draw uniformly from the resulting set, which can have one or two elements depending on prize and choice. The player then changes his mind and chooses from the set of doors other than the open one and his initial choice. With the standard three-door game, that set has exactly one element, but the code above also works for a larger number of doors - try it out yourself!</p>
<p>Evaluating this piece of code yields <code>{:loose 1/3, :win 2/3}</code>, indicating that the change-your-mind strategy is indeed the better one.</p>
<p>Back to the <code>maybe-t</code> transformer. The finite-distribution library defines a second monad by</p>
<pre>
(def cond-dist-m (maybe-t dist-m))
</pre>
<p>This makes <code>nil</code> a special value in distributions, which is used to represent events that we don't want to consider as possible ones. With the definitions of <code>maybe-t</code> and <code>dist-m</code>, you can guess how <code>nil</code> values are propagated when distributions are combined: for any <code>nil</code> value, the distributions that potentially depend on it are never evaluated, and the <code>nil</code> value's probability is transferred entirely to the probability of <code>nil</code> in the output distribution. But how does <code>nil</code> ever get into a distribution? And, most of all, what is that good for?</p>
<p>Let's start with the last question. The goal of this <code>nil</code>-containing distributions is to eliminate certain values. Once the final distribution is obtained, the <code>nil</code> value is removed, and the remaining distribution is normalized to make the sum of the probabilities of the remaining values equal to one. This <code>nil</code>-removal and normalization is performed by the utility function <code>normalize-cond</code>. The <code>cond-dist-m</code> monad is thus a sophisticated way to compute conditional probabilities, and in particular to facilitate Bayesian inference, which is an important technique in all kinds of data analysis.</p>
<p>As a first exercice, let's calculate a simple conditional probability from an input distribution and a predicate. The output distribution should contain only the values satisfying the predicate, but be normalized to one:</p>
<pre>
(defn cond-prob [pred dist]
  (normalize-cond (domonad cond-dist-m
                    [v dist
                     :when (pred v)]
                    v))))
</pre>
<p>The important line is the one with the <code>:when</code> condition. As I have explained in parts 1 and 2, the <code>domonad</code> form becomes</p>
<pre>
(m-bind dist
        (fn [v]
          (if (pred v)
            (m-result v)
             m-zero)))
</pre>
<p>If you have been following carefully, you should complain now: with the definitions of <code>dist-m</code> and <code>maybe-t</code> I have given above, <code>cond-dist-m</code> should not have any <code>m-zero</code>! But as I said earlier, the <code>maybe-t</code> shown here is a simplified version. The real one checks if the base monad has <code>m-zero</code>, and if it hasn't, it substitutes its own, which is <code>(with-monad m (m-result nil))</code>. Therefore the <code>m-zero</code> of <code>cond-dist-m</code> is <code>{nil 1}</code>, the distribution whose only value is <code>nil</code>.</p>
<p>The net effect of the <code>domonad</code> form in this example is thus to keep all values that satisfy the predicate with their initial probabilities, but to transfer the probability of all values to <code>nil</code>. The call to <code>normalize-cond</code> then takes out the <code>nil</code> and re-distributes its probability to the other values. Example:</p>
<pre>
(cond-prob odd? die)
-&gt; {5 1/3, 3 1/3, 1 1/3}
</pre>
<p>The <code>cond-dist-m</code> monad really becomes interesting for Bayesian inference problems. Bayesian inference is technique for drawing conclusions from incomplete observations. It has a wide range of applications, from spam filters to weather forecasts. For an introduction to the technique and its mathematical basis, you can start with the <a href="http://en.wikipedia.org/wiki/Bayesian_inference">Wikipedia article</a>.</p>
<p>Here I will discuss a very simple inference problem and its solution in Clojure. Suppose someone has three dice, one with six faces, one with eight, and one with twelve. This person picks one die, throws it a few times, and gives us the numbers, but doesn't tell us which die it was. Given these observations, we would like to infer the probabilities for each of the three dice to have been picked. We start by defining a function that returns the distribution of a die with n faces:</p>
<pre>
(defn die-n [n] (uniform (range 1 (inc n))))
</pre>
<p>Next, we come to the core of Bayesian inference. One central ingredient is the probability for throwing a given number under the assumption that die X was used. We thus need the probability distributions for each of our three dice:</p>
<pre>
(def dice {:six     (die-n 6)
           :eight   (die-n 8 )
           :twelve  (die-n 12)})
</pre>
<p>The other central ingredient is a distribution representing our 'prior knowledge' about the chosen die. We actually know nothing at all, so each die has the same weight in this distribution:</p>
<pre>
(def prior (uniform (keys dice)))
</pre>
<p>Now we can write the inference function. It takes as input the prior-knowledge distribution and a number that was obtained from the die. It returns the <i>a posteriori</i> distribution that combines the prior information with the information from the observation.</p>
<pre>
(defn add-observation [prior observation]
  (normalize-cond
    (domonad cond-dist-m
      [die    prior
       number (get dice die)
       :when  (= number observation)]
      die)))
</pre>
<p>Let's look at the <code>domonad</code> form. The first step picks one die according to the prior knowledge. The second line "throws" that die, obtaining a number. The third line eliminates the numbers that don't match the observation. And then we ask for the distribution of the die.</p>
<p>It is instructive to compare this function with the mathematical formula for Bayes' theorem, which is the basis of Bayesian inference. Bayes' theorem is P(H|E) = P(E|H) P(H) / P(E), where H stands for the hypothesis ("the die chosen was X") and E stands for the evidence ("the number thrown was N"). P(H) is the prior knowledge. The formula must be evaluated for a fixed value of E, which is the observation.</p>
<p>The first line of our <code>domonad</code> form implements P(H), the second line implements P(E|H). These two lines together thus sample P(E, H) using ancestral sampling, as we have seen before. The <code>:when</code> line represents the observation; we wish to apply Bayes' theorem for a fixed value of E. Once E has been fixed, P(E) is just a number, required for normalization. This is handled by <code>normalize-cond</code> in our code.</p>
<p>Let's see what happens when we add a single observation:</p>
<pre>
(add-observation prior 1)
-&gt; {:twelve 2/9, :eight 1/3, :six 4/9}
</pre>
<p>We see that the highest probability is given to <code>:six</code>, then <code>:eight</code>, and finally <code>:twelve</code>. This happens because 1 is a possible value for all dice, but it is more probable as a result of throwing a six-faced die (1/6) than as a result of throwing an eight-faced die (1/8) or a twelve-faced die (1/12). The observation thus favours a die with a small number of faces.</p>
<p>If we have three observations, we can call add-observation repeatedly:</p>
<pre>
(-&gt; prior (add-observation 1)
          (add-observation 3)
          (add-observation 7))
-&gt; {:twelve 8/35, :eight 27/35}
</pre>
<p>Now we see that the candidate <code>:six</code> has disappeared. In fact, the observed value of 7 rules it out completely. Moreover, the observed numbers strongly favour <code>:eight</code> over <code>:twelve</code>, which is again due to the preference for the smallest possible die in the game.</p>
<p>This inference problem is very similar to how a spam filter works. In that case, the three dice are replaced by the choices <code>:spam</code> or <code>:no-spam</code>. For each of them, we have a distribution of words, obtained by analyzing large quantities of e-mail messages. The function add-observation is strictly the same, we'd just pick different variable names. And then we'd call it for each word in the message we wish to evaluate, starting from a prior distribution defined by the total number of <code>:spam</code> and <code>:no-spam</code> messages in our database.</p>
<p>To end this introduction to monad transformers, I will explain the <code>m-zero</code> problem in <code>maybe-t</code>. As you know, the maybe monad has an <code>m-zero</code> definition (<code>nil</code>) and an <code>m-plus</code> definition, and those two can be carried over into a monad created by applying <code>maybe-t</code> to some base monad. This is what we have seen in the case of <code>cond-dist-m</code>. However, the base monad might have its own <code>m-zero</code> and <code>m-plus</code>, as we have seen in the case of <code>sequence-m</code>. Which set of definitions should the combined monad have? Only the user of <code>maybe-t</code> can make that decision, so <code>maybe-t</code> has an optional parameter for this (see its documentation for the details). The only clear case is a base monad without <code>m-zero</code> and <code>m-plus</code>; in that case, nothing is lost if <code>maybe-t</code> imposes its own.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/onclojure.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/onclojure.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/onclojure.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/onclojure.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/onclojure.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/onclojure.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/onclojure.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/onclojure.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/onclojure.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/onclojure.wordpress.com/51/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=51&subd=onclojure&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://onclojure.com/2009/04/24/a-monad-tutorial-for-clojure-programmers-part-4/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/305e4cf66ef1179f7e95981b1520ba1a?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">khinsen</media:title>
		</media:content>
	</item>
		<item>
		<title>A monad tutorial for Clojure programmers (part 3)</title>
		<link>http://onclojure.com/2009/03/23/a-monad-tutorial-for-clojure-programmers-part-3/</link>
		<comments>http://onclojure.com/2009/03/23/a-monad-tutorial-for-clojure-programmers-part-3/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 14:10:43 +0000</pubDate>
		<dc:creator>khinsen</dc:creator>
				<category><![CDATA[Libraries]]></category>

		<guid isPermaLink="false">http://onclojure.com/?p=33</guid>
		<description><![CDATA[Before moving on to the more advanced aspects of monads, let&#8217;s recapitulate what defines a monad (see part 1 and part 2 for explanations):

 A data structure that represents the result of a computation, or the computation itself. We haven&#8217;t seen an example of the latter case yet, but it will come soon.
 A function [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=33&subd=onclojure&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Before moving on to the more advanced aspects of monads, let&#8217;s recapitulate what defines a monad (see <a href="http://onclojure.com/2009/03/05/a-monad-tutorial-for-clojure-programmers-part-1/">part 1</a> and <a href="http://onclojure.com/2009/03/06/a-monad-tutorial-for-clojure-programmers-part-2/">part 2</a> for explanations):</p>
<ol>
<li> A data structure that represents the result of a computation, or the computation itself. We haven&#8217;t seen an example of the latter case yet, but it will come soon.</li>
<li> A function <code>m-result</code> that converts an arbitrary value to a monadic data structure equivalent to that value.</li>
<li> A function <code>m-bind</code> that binds the result of a computation, represented by the monadic data structure, to a name (using a function of one argument) to make it available in the following computational step.</li>
</ol>
<p>Taking the sequence monad as an example, the data structure is the sequence, representing the outcome of a non-deterministic computation, <code>m-result</code> is the function list, which converts any value into a list containing just that value, and <code>m-bind</code>is a function that executes the remaining steps once for each element in a sequence, and removes one level of nesting in the result.</p>
<p>The three ingredients above are what defines a monad, under the condition that the three monad laws are respected. Some monads have two additional definitions that make it possible to perform additional operations. These two definitions have the names <code>m-zero</code> and <code>m-plus</code>.  <code>m-zero</code> represents a special monadic value that corresponds to a computation with no result. One example is <code>nil</code> in the maybe monad, which typically represents a failure of some kind. Another example is the empty sequence in the sequence monad. The identity monad is an example of a monad that has no <code>m-zero</code>.</p>
<p><code>m-plus</code> is a function that combines the results of two or more computations into a single one. For the sequence monad, it is the concatenation of several sequences. For the maybe monad, it is a function that returns the first of its arguments that is not <code>nil</code>.</p>
<p>There is a condition that has to be satisfied by the definitions of <code>m-zero</code> and <code>m-plus</code> for any monad:</p>
<pre>(= (m-plus m-zero monadic-expression)
   (m-plus monadic-expression m-zero)
   monadic-expression)</pre>
<p>In words, combining <code>m-zero</code> with any monadic expression must yield the same expression. You can easily verify that this is true for the two examples (maybe and sequence) given above.</p>
<p>One benefit of having an <code>m-zero</code> in a monad is the possibility to use conditions. In the first part, I promised to return to the <code>:when</code> clauses in Clojure&#8217;s for forms, and now the time has come to discuss them. A simple example is</p>
<pre>(for [a (range 5)
      :when (odd? a)]
  (* 2 a))</pre>
<p>The same construction is possible with <code>domonad</code>:</p>
<pre>(domonad sequence
  [a (range 5)
   :when (odd? a)]
  (* 2 a))</pre>
<p>Recall that <code>domonad</code> is a macro that translates a <code>let</code>-like syntax into a chain of calls to <code>m-bind</code> ending in a call to <code>m-result</code>. The clause <code>a (range 5)</code> becomes</p>
<pre>(m-bind (range 5) (fn [a] remaining-steps))</pre>
<p>where <code>remaining-steps</code> is the transformation of the rest of to the <code>domonad</code> form. A <code>:when</code> clause is of course treated specially, it becomes</p>
<pre>(if predicate remaining-steps m-zero)</pre>
<p>Our small example thus expands to</p>
<pre>(m-bind (range 5) (fn [a]
  (if (odd? a) (m-result (* 2 a)) m-zero)))</pre>
<p>Inserting the definitions of <code>m-bind</code>, <code>m-result</code>, and <code>m-zero</code>, we finally get</p>
<pre>(apply concat (map (fn [a]
  (if (odd? a) (list (* 2 a)) (list))) (range 5)))</pre>
<p>The result of <code>map</code> is a sequence of lists that have zero or one elements: zero for even values (the value of <code>m-zero</code>) and one for odd values (produced by <code>m-result</code>). <code>concat</code> makes a single flat list out of this, which contains only the elements that satisfy the <code>:when</code> clause.</p>
<p>As for <code>m-plus</code>, it is in practice used mostly with the maybe and sequence monads, or with variations of them. A typical use would be a search algorithm (think of a parser, a regular expression search, a database query) that can succeed (with one or more results) or fail (no results). <code>m-plus</code> would then be used to pursue alternative searches and combine the results into one (sequence monad), or to continue searching until a result is found (maybe monad).  Note that it is perfectly possible in principle to have a monad with an <code>m-zero</code> but no <code>m-plus</code>, though in all common cases an <code>m-plus</code> can be defined as well if an <code>m-zero</code> is known.</p>
<p>After this bit of theory, let&#8217;s get acquainted with more monads. In the beginning of this part, I mentioned that the data structure used in a monad does not always represent the result(s) of a computational step, but sometimes the computation itself. An example of such a monad is the state monad, whose data structure is a function.</p>
<p>The state monad&#8217;s purpose is to facilitate the implementation of stateful algorithms in a purely functional way. Stateful algorithms are algorithms that require updating some variables. They are of course very common in imperative languages, but not compatible with the basic principle of pure functional programs which should not have mutable data structures. One way to simulate state changes while remaining purely functional is to have a special data item (in Clojure that would typically be a map) that stores the current values of all mutable variables that the algorithm refers to. A function that in an imperative program would modify a variable now takes the current state as an additional input argument and returns an updated state along with its usual result. The changing state thus becomes explicit in the form of a data item that is passed from function to function as the algorithm&#8217;s execution progresses. The state monad is a way to hide the state-passing behind the scenes and write an algorithm in an imperative style that consults and modifies the state.</p>
<p>The state monad differs from the monads that we have seen before in that its data structure is a function. This is thus a case of a monad whose data structure represents not the result of a computation, but the computation itself. A state monad value is a function that takes a single argument, the current state of the computation, and returns a vector of length two containing the result of the computation and the updated state after the computation. In practice, these functions are typically closures, and what you use in your program code are functions that create these closures. Such state-monad-value-generating functions are the equivalent of statements in imperative languages. As you will see, the state monad allows you to compose such functions in a way that makes your code look perfectly imperative, even though it is still purely functional!</p>
<p>Let&#8217;s start with a simple but frequent situation: the state that your code deals with takes the form of a map. You may consider that map to be a namespace in an imperative languages, with each key defining a variable. Two basic operations are reading the value of a variable, and modifying that value. They are already provided in the Clojure monad library, but I will show them here anyway because they make nice examples.</p>
<p>First, we look at <code>fetch-val</code>, which retrieves the value of a variable:</p>
<pre>(defn fetch-val [key]
  (fn [s]
    [(key s) s]))</pre>
<p>Here we have a simple state-monad-value-generating function. It returns a function of a state variable <code>s</code> which, when executed, returns a vector of the return value and the new state. The return value is the value corresponding to the key in the map that is the state value. The new state is just the old one &#8211; a lookup should not change the state of course.</p>
<p>Next, let&#8217;s look at <code>set-val</code>, which modifies the value of a variable and returns the previous value:</p>
<pre>(defn set-val [key val]
  (fn [s]
    (let [old-val (get s key)
	  new-s   (assoc s key val)]
      [old-val new-s])))</pre>
<p>The pattern is the same again: <code>set-val</code> returns a function of state <code>s</code> that, when executed, returns the old value of the variable plus an updated state map in which the new value is the given one.</p>
<p>With these two ingredients, we can start composing statements. Let&#8217;s define a statement that copies the value of one variable into another one and returns the previous value of the modified variable:</p>
<pre>(defn copy-val [from to]
  (domonad state-m
    [from-val   (fetch-val from)
     old-to-val (set-val to from-val)]
    old-to-val))</pre>
<p>What is the result of <code>copy-val</code>? A state-monad value, of course: a function of a state variable <code>s</code> that, when executed, returns the old value of variable to plus the state in which the copy has taken place. Let&#8217;s try it out:</p>
<pre>(let [initial-state        {:a 1 :b 2}
      computation          (copy-val :b :a)
      [result final-state] (computation initial-state)]
  final-state)</pre>
<p>We get {:a 2, :b 2}, as expected. But how does it work? To understand the state monad, we need to look at its definitions for <code>m-result</code> and <code>m-bind</code>, of course.</p>
<p>First, <code>m-result</code>, which does not contain any surprises: it returns a function of a state variable <code>s</code> that, when executed, returns the result value <code>v</code> and the unchanged state <code>s</code>:</p>
<pre>(defn m-result [v] (fn [s] [v s]))</pre>
<p>The definition of <code>m-bind</code> is more interesting:</p>
<pre>(defn m-bind [mv f]
  (fn [s]
    (let [[v ss] (mv s)]
      ((f v) ss))))</pre>
<p>Obviously, it returns a function of a state variable <code>s</code>. When that function is executed, it first runs the computation described by <code>mv</code> (the first &#8217;statement&#8217; in the chain set up by<code> m-bind</code>) by applying it to the state <code>s</code>. The return value is decomposed into result <code>v</code> and new state <code>ss</code>. The result of the first step, <code>v</code>, is injected into the rest of the computation by calling <code>f</code> on it (like for the other <code>m-bind</code> functions that we have seen). The result of that call is of course another state-monad value, and thus a function of a state variable. When we are inside our <code>(fn [s] ...)</code>, we are already at the execution stage, so we have to call that function on the state <code>ss</code>, the one that resulted from the execution of the first computational step.</p>
<p>The state monad is one of the most basic monads, of which many variants are in use. Usually such a variant adds something to <code>m-bind</code> that is specific to the kind of state being handled. An example is the the stream monad in <code>clojure.contrib.stream-utils</code>. Its state describes a stream of data items, and the<code> m-bind</code> function checks for invalid values and for the end-of-stream condition in addition to what the basic <code>m-bind</code> of the state monad does.</p>
<p>A variant of the state monad that is so frequently used that is has itself become one of the standard monads is the writer monad. Its state is an accumulator (anything defined in <code>clojure.contrib.accumulators</code>), to which computations can add something by calling the function <code>write</code>. The name comes from a particularly popular application: logging. Take a basic computation in the identity monad, for example (remember that the identity monad is just Clojure&#8217;s built-in <code>let</code>). Now assume you want to add a protocol of the computation in the form of a list or a string that accumulates information about the progress of the computation. Just change the identity monad to the writer monad, and add calls to <code>write</code> where required!</p>
<p>Here is a concrete example: the well-known Fibonacci function in its most straightforward (and most inefficient) implementation:</p>
<pre>
(defn fib [n]
  (if (&lt; n 2)
    n
    (let [n1 (dec n)
	  n2 (dec n1)]
      (+ (fib n1) (fib n2)))))
</pre>
<p>Let&#8217;s add some protocol of the computation in order to see which calls are made to arrive at the final result. First, we rewrite the above example a bit to make every computational step explicit:</p>
<pre>
(defn fib [n]
  (if (&lt; n 2)
    n
    (let [n1 (dec n)
	  n2 (dec n1)
	  f1 (fib n1)
	  f2 (fib n2)]
      (+ f1 f2))))
</pre>
<p>Second, we replace <code>let</code> by <code>domonad</code> and choose the writer monad with a vector accumulator:</p>
<pre>
(require ['clojure.contrib.accumulators :as 'accu])

(with-monad (writer-m accu/empty-vector)

  (defn fib-trace [n]
    (if (&lt; n 2)
      (m-result n)
      (domonad
        [n1 (m-result (dec n))
	 n2 (m-result (dec n1))
	 f1 (fib-trace n1)
	 _  (write [n1 f1])
	 f2 (fib-trace n2)
	 _  (write [n2 f2])
	 ]
	(+ f1 f2))))

)
</pre>
<p>Finally, we run <code>fib-trace</code> and look at the result:</p>
<pre>
(fib-trace 3)
[2 [[1 1] [0 0] [2 1] [1 1]]]
</pre>
<p>The first element of the return value, 2, is the result of the function <code>fib</code>. The second element is the protocol vector containing the arguments and results of the recursive calls.</p>
<p>Note that it is sufficient to comment out the lines with the calls to <code>write</code> and change the monad to <code>identity-m</code> to obtain a standard fib function with no protocol &#8211; try it out for yourself!</p>
<p><a href="http://onclojure.com/2009/04/24/a-monad-tutorial-for-clojure-programmers-part-4/">Part 4</a> will show you how to define your own monads by combining monad building blocks called monad transformers. As an illustration, I will explain the probability monad and how it can be used for Bayesian estimates when combined with the maybe-transformer.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/onclojure.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/onclojure.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/onclojure.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/onclojure.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/onclojure.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/onclojure.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/onclojure.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/onclojure.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/onclojure.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/onclojure.wordpress.com/33/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=33&subd=onclojure&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://onclojure.com/2009/03/23/a-monad-tutorial-for-clojure-programmers-part-3/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/305e4cf66ef1179f7e95981b1520ba1a?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">khinsen</media:title>
		</media:content>
	</item>
		<item>
		<title>A monad tutorial for Clojure programmers (part 2)</title>
		<link>http://onclojure.com/2009/03/06/a-monad-tutorial-for-clojure-programmers-part-2/</link>
		<comments>http://onclojure.com/2009/03/06/a-monad-tutorial-for-clojure-programmers-part-2/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 13:09:45 +0000</pubDate>
		<dc:creator>khinsen</dc:creator>
				<category><![CDATA[Libraries]]></category>

		<guid isPermaLink="false">http://onclojure.com/?p=26</guid>
		<description><![CDATA[In the first part of this tutorial, I have introduced the two most basic monads: the identity monad and the maybe monad. In this part, I will continue with the sequence monad, which will be the occasion to explain the role of the mysterious m-result function. I will also show a couple of useful generic [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=26&subd=onclojure&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://onclojure.com/2009/03/05/a-monad-tutorial-for-clojure-programmers-part-1/">first part</a> of this tutorial, I have introduced the two most basic monads: the identity monad and the maybe monad. In this part, I will continue with the sequence monad, which will be the occasion to explain the role of the mysterious <code>m-result</code> function. I will also show a couple of useful generic monad operations.</p>
<p>One of the most frequently used monads is the sequence monad (known in the Haskell world as the list monad). It is in fact so common that it is built into Clojure as well, in the form of the <code>for</code> form. Let&#8217;s look at an example:</p>
<pre>
(for [a (range 5)
      b (range a)]
  (* a b))
</pre>
<p>A <code>for</code> form resembles a <code>let</code> form not only syntactically. It has the same structure: a list of binding expressions, in which each expression can use the bindings from the preceding ones, and a final result expressions that typically depends on all the bindings as well. The difference between <code>let</code> and <code>for</code> is that let binds a single value to each symbol, whereas for binds several values in sequence. The expressions in the binding list must therefore evaluate to sequences, and the result is a sequence as well. The <code>for</code> form can also contain conditions in the form of <code>:when</code> and <code>:while</code> clauses, which I will discuss later. From the monad point of view of composable computations, the sequences are seen as the results of non-deterministic computations, i.e. computations that have more than one result.</p>
<p>Using the monad library, the above loop is written as</p>
<pre>
(domonad sequence-m
  [a (range 5)
   b (range a)]
  (* a b))
</pre>
<p>Since we alread know that the domonad macro expands into a chain of <code>m-bind</code> calls ending in an expression that calls <code>m-result</code>, all that remains to be explained is how <code>m-bind</code> and <code>m-result</code> are defined to obtain the desired looping effect.</p>
<p>As we have seen before, <code>m-bind</code> calls a function of one argument that represents the rest of the computation, with the function argument representing the bound variable. To get a loop, we have to call this function repeatedly. A first attempt at such an <code>m-bind</code> function would be</p>
<pre>
(defn m-bind-first-try [sequence function]
  (map function sequence))
</pre>
<p>Let&#8217;s see what this does for our example:</p>
<pre>
(m-bind-first-try (range 5)  (fn [a]
(m-bind-first-try (range a)  (fn [b]
(* a b)))))
</pre>
<p>This yields <code>(() (0) (0 2) (0 3 6) (0 4 8 12))</code>, whereas the <code>for</code> form given above yields <code>(0 0 2 0 3 6 0 4 8 12)</code>. Something is not yet quite right. We want a single flat result sequence, but what we get is a nested sequence whose nesting level equals the number of <code>m-bind</code> calls. Since <code>m-bind</code> introduces one level of nesting, it must also remove one. That sounds like a job for <code>concat</code>. So let&#8217;s try again:</p>
<pre>
(defn m-bind-second-try [sequence function]
  (apply concat (map function sequence)))

(m-bind-second-try (range 5)  (fn [a]
(m-bind-second-try (range a)  (fn [b]
(* a b)))))
</pre>
<p>This is worse: we get an exception. Clojure tells us:</p>
<pre>java.lang.IllegalArgumentException: Don't know how to create ISeq from: Integer
</pre>
<p>Back to thinking!</p>
<p>Our current <code>m-bind</code> introduces a level of sequence nesting and also takes one away. Its result therefore has as many levels of nesting as the return value of the function that is called. The final result of our expression has as many nesting values as <code>(* a b)</code> &#8211; which means none at all. If we want one level of nesting in the result, no matter how many calls to <code>m-bind</code> we have, the only solution is to introduce one level of nesting at the end. Let&#8217;s try a quick fix:</p>
<pre>
(m-bind-second-try (range 5)  (fn [a]
(m-bind-second-try (range a)  (fn [b]
(list (* a b))))))
</pre>
<p>This works! Our <code>(fn [b] ...)</code> always returns a one-element list. The inner <code>m-bind</code> thus creates a sequence of one-element lists, one for each value of <code>b</code>, and concatenates them to make a flat list. The outermost <code>m-bind</code> then creates such a list for each value of <code>a</code> and concatenates them to make another flat list. The result of each <code>m-bind</code> thus is a flat list, as it should be. And that illustrates nicely why we need <code>m-result</code> to make a monad work. The final definition of the sequence monad is thus given by</p>
<pre>
(defn m-bind [sequence function]
  (apply concat (map function sequence)))

(defn m-result [value]
  (list value))
</pre>
<p>The role of <code>m-result</code> is to turn a bare value into the expression that, when appearing on the right-hand side in a monadic binding, binds the symbol to that value. This is one of the conditions that a pair of <code>m-bind</code> and <code>m-result</code> functions must fulfill in order to define a monad. Expressed as Clojure code, this condition reads</p>
<pre>
(= (m-bind (m-result value) function)
   (function value))
</pre>
<p>There are two more conditions that complete the three <em>monad laws</em>. One of them is</p>
<pre>
(= (m-bind monadic-expression m-result)
   monadic-expression)
</pre>
<p>with <code>monadic-expression</code> standing for any expression valid in the monad under consideration, e.g. a sequence expression for the sequence monad. This condition becomes clearer when expressed using the domonad macro:</p>
<pre>
(= (domonad
     [x monadic-expression]
      x)
   monadic-expression)
</pre>
<p>The final monad law postulates associativity:</p>
<pre>
(= (m-bind (m-bind monadic-expression
                   function1)
           function2)
   (m-bind monadic-expression
           (fn [x] (m-bind (function1 x)
                           function2))))
</pre>
<p>Again this becomes a bit clearer using domonad syntax:</p>
<pre>
(= (domonad
     [y (domonad
          [x monadic-expression]
          (function1 x))]
     (function2 y))
   (domonad
     [x monadic-expression
      y (function1 x)]
     (function2 y)))
</pre>
<p>It is not necessary to remember the monad laws for using monads, they are of importance only when you start to define your own monads. What you should remember about <code>m-result</code> is that <code>(m-result x)</code> represents the monadic computation whose result is <code>x</code>. For the sequence monad, this means a sequence with the single element <code>x</code>. For the identity monad and the maybe monad, which I have presented in the first part of the tutorial, there is no particular structure to monadic expressions, and therefore <code>m-result</code> is just the identity function.</p>
<p>Now it&#8217;s time to relax: the most difficult material has been covered. I will return to monad theory in the next part, where I will tell you more about the <code>:when</code> clauses in <code>for</code> loops. The rest of this part will be of a more pragmatic nature.</p>
<p>You may have wondered what the point of the identity and sequence monads is, given that Clojure already contains fully equivalent forms. The answer is that there are generic operations on computations that have an interpretation in any monad. Using the monad library, you can write functions that take a monad as an argument and compose computations in the given monad. I will come back to this later with a concrete example. The monad library also contains some useful predefined operations for use with any monad, which I will explain now. They all have names starting with the prefix <code>m-</code>.</p>
<p>Perhaps the most frequently used generic monad function is <code>m-lift</code>. It converts a function of <em>n</em> standard value arguments into a function of <em>n</em> monadic expressions that returns a monadic expression. The new function contains implicit <code>m-bind</code> and <code>m-result</code> calls. As a simple example, take</p>
<pre>
(def nil-respecting-addition
  (with-monad maybe-m
    (m-lift 2 +)))
</pre>
<p>This is a function that returns the sum of two arguments, just like <code>+</code> does, except that it automatically returns <code>nil</code> when either of its arguments is <code>nil</code>. Note that <code>m-lift</code> needs to know the number of arguments that the function has, as there is no way to obtain this information by inspecting the function itself.</p>
<p>To illustrate how <code>m-lift</code> works, I will show you an equivalent definition in terms of <code>domonad</code>:</p>
<pre>
(defn nil-respecting-addition
  [x y]
  (domonad maybe-m
    [a x
     b y]
    (+ a b)))
</pre>
<p>This shows that <code>m-lift</code> implies one call to <code>m-result</code> and one <code>m-bind</code> call per argument.  The same definition using the sequence monad would yield a function that returns a sequence of all possible sums of pairs from the two input sequences.</p>
<p>Exercice: The following function is equivalent to a well-known built-in Clojure function. Which one?</p>
<pre>
(defn mystery
  [f xs]
  ( (m-lift 1 f) xs ))
</pre>
<p>Another popular monad operation is <code>m-seq</code>. It takes a sequence of monadic expressions, and returns a sequence of their result values. In terms of <code>domonad</code>, the expression <code>(m-seq [a b c])</code> becomes</p>
<pre>
(domonad
  [x a
   y b
   z c]
  '(x y z))
</pre>
<p>Here is an example of how you might want to use it:</p>
<pre>
(with-monad sequence-m
   (defn ntuples [n xs]
      (m-seq (replicate n xs))))
</pre>
<p>Try it out for yourself!</p>
<p>The final monad operation I want to mention is <code>m-chain</code>. It takes a list of one-argument computations, and chains them together by calling each element of this list with the result of the preceding one. For example, <code>(m-chain [a b c])</code> is equivalent to</p>
<pre>
(fn [arg]
  (domonad
    [x (a arg)
     y (b x)
     z (c y)]
    z))
</pre>
<p>A usage example is the traversal of hierarchies. The Clojure function <code>parents</code> yields the parents of a given class or type in the hierarchy used for multimethod dispatch. When given a Java class, it returns its base classes. The following function builds on <code>parents</code> to find the n-th generation ascendants of a class:</p>
<pre>
(with-monad sequence-m
  (defn n-th-generation
    [n cls]
    ( (m-chain (replicate n parents)) cls )))

(n-th-generation 0 (class []))
(n-th-generation 1 (class []))
(n-th-generation 2 (class []))
</pre>
<p>You may notice that some classes can occur more than once in the result, because they are the base class of more than one class in the generation below. In fact, we ought to use sets instead of sequences for representing the ascendants at each generation. Well&#8230; that&#8217;s easy. Just replace <code>sequence-m</code> by <code>set-m</code> and run it again!</p>
<p>In <a href="http://onclojure.com/2009/03/23/a-monad-tutorial-for-clojure-programmers-part-3/">part 3</a>, I will come back to the <code>:when</code> clause in for loops, and show how it is implemented and generalized in terms of monads. I will also explain another monad or two. Stay tuned!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/onclojure.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/onclojure.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/onclojure.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/onclojure.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/onclojure.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/onclojure.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/onclojure.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/onclojure.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/onclojure.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/onclojure.wordpress.com/26/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=26&subd=onclojure&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://onclojure.com/2009/03/06/a-monad-tutorial-for-clojure-programmers-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/305e4cf66ef1179f7e95981b1520ba1a?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">khinsen</media:title>
		</media:content>
	</item>
		<item>
		<title>A monad tutorial for Clojure programmers (part 1)</title>
		<link>http://onclojure.com/2009/03/05/a-monad-tutorial-for-clojure-programmers-part-1/</link>
		<comments>http://onclojure.com/2009/03/05/a-monad-tutorial-for-clojure-programmers-part-1/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 18:15:03 +0000</pubDate>
		<dc:creator>khinsen</dc:creator>
				<category><![CDATA[Libraries]]></category>

		<guid isPermaLink="false">http://onclojure.com/?p=14</guid>
		<description><![CDATA[Monads in functional programming are most often associated with the Haskell language, where they play a central role in I/O and have found numerous other uses. Most introductions to monads are currently written for Haskell programmers. However, monads can be used with any functional language, even languages quite different from Haskell. Here I want to explain monads in the context [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=14&subd=onclojure&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Monads in functional programming are most often associated with the Haskell language, where they play a central role in I/O and have found numerous other uses. Most introductions to monads are currently written for Haskell programmers. However, monads can be used with any functional language, even languages quite different from Haskell. Here I want to explain monads in the context of Clojure, a modern Lisp dialect with strong support for functional programming. A monad implementation for Clojure is available in the library <a href="http://code.google.com/p/clojure-contrib/source/browse/trunk/src/clojure/contrib/monads.clj">clojure.contrib.monads</a>. Before trying out the examples given in this tutorial, type <code>(use 'clojure.contrib.monads)</code> into your Clojure REPL.</p>
<p>Monads are about composing computational steps into a bigger multi-step computation. Let&#8217;s start with the simplest monad, known as the identity monad in the Haskell world. It&#8217;s actually built into the Clojure language, and you have certainly used it: it&#8217;s the <a href="http://clojure.org/special_forms#let"><code>let</code></a> form.</p>
<p>Consider the following piece of code:</p>
<pre>(let [a  1
      b  (inc a)]
  (* a b))</pre>
<p>This can be seen as a three-step calculation:</p>
<ol>
<li>Compute <code>1</code> (a constant), and call the result <code>a</code>.</li>
<li>Compute <code>(inc a)</code>, and call the result <code>b</code>.</li>
<li>Compute <code>(* a b)</code>, which is the result of the multi-step computation.</li>
</ol>
<p>Each step has access to the results of all previous steps through the symbols to which their results have been bound.</p>
<p>Now suppose that Clojure didn&#8217;t have a let form. Could you still compose computations by binding intermediate results to symbols? The answer is yes, using functions. The following expression is in fact equivalent to the previous one:</p>
<pre>( (fn [a]  ( (fn [b] (* a b)) (inc a) ) )  1 )</pre>
<p>The outermost level defines an anonymous function of <code>a</code> and calls with with the argument <code>1</code> &#8211; this is how we bind <code>1</code> to the symbol <code>a</code>. Inside the function of <code>a</code>, the same construct is used once more: the body of <code>(fn [a] ...)</code> is a function of <code>b</code> called with argument <code>(inc a)</code>. If you don&#8217;t believe that this somewhat convoluted expression is equivalent to the original let form, just paste both into Clojure!</p>
<p>Of course the functional equivalent of the <code>let</code> form is not something you would want to work with. The computational steps appear in reverse order, and the whole construct is nearly unreadable even for this very small example. But we can clean it up and put the steps in the right order with a small helper function, bind. We will call it <code>m-bind</code> (for monadic bind) right away, because that&#8217;s the name it has in Clojure&#8217;s monad library. First, its definition:</p>
<pre>(defn m-bind [value function]
  (function value))</pre>
<p>As you can see, it does almost nothing, but it permits to write a value before the function that is applied to it. Using <code>m-bind</code>, we can write our example as</p>
<pre>(m-bind 1        (fn [a]
(m-bind (inc a)  (fn [b]
        (* a b)))))</pre>
<p>That&#8217;s still not as nice as the <code>let</code> form, but it comes a lot closer. In fact, all it takes to convert a <code>let</code> form into a chain of computations linked by <code>m-bind</code> is a little macro. This macro is called <code>domonad</code>, and it permits us to write our example as</p>
<pre>(domonad identity-m
  [a  1
   b  (inc a)]
  (* a b))</pre>
<p>This looks exactly like our original let form. Running <code>macroexpand-1</code> on it yields</p>
<pre>
(clojure.contrib.monads/with-monad identity-m
  (m-bind 1 (fn [a] (m-bind (inc a) (fn [b] (m-result (* a b)))))))
</pre>
<p>This is the expression you have seen above, wrapped in a<code> (with-monad identity-m ...)</code> block (to tell Clojure that you want to evaluate it in the identity monad) and with an additional call to <code>m-result</code> that I will explain later. For the identity monad, <code>m-result</code> is just <code>identity</code> &#8211; hence its name.</p>
<p>As you might guess from all this, monads are generalizations of the <code>let</code> form that replace the simple <code>m-bind</code> function shown above by something more complex. Each monad is defined by an implementation of <code>m-bind</code> and an associated implementation of <code>m-result</code>. A <code>with-monad</code> block simply binds (using a <code>let</code> form!) these implementations to the names <code>m-bind</code> and <code>m-result</code>, so that you can use a single syntax for composing computations in any monad. Most frequently, you will use the <code>domonad</code> macro for this.</p>
<p>As our second example, we will look at another very simple monad, but one that adds something useful that you don&#8217;t get in a <code>let</code> form. Suppose your computations can fail in some way, and signal failure by producing <code>nil</code> as a result. Let&#8217;s take our example expression again and wrap it into a function:</p>
<pre>
(defn f [x]
  (let [a  x
        b  (inc a)]
    (* a b)))
</pre>
<p>In the new setting of possibly-failing computations, you want this to return <code>nil</code> when <code>x</code> is <code>nil</code>, or when <code>(inc a)</code> yields <code>nil</code>. (Of course <code>(inc a)</code> will never yield <code>nil</code>, but that&#8217;s the nature of examples&#8230;) Anyway, the idea is that whenever a computational step yields <code>nil</code>, the final result of the computation is <code>nil</code>, and the remaining steps are never executed. All it takes to get this behaviour is a small change:</p>
<pre>
(defn f [x]
  (domonad maybe-m
    [a  x
     b  (inc a)]
    (* a b)))
</pre>
<p>The maybe monad represents computations whose result is maybe a valid value, but maybe <code>nil</code>. Its <code>m-result</code> function is still <code>identity</code>, so we don&#8217;t have to discuss <code>m-result</code> yet (be patient, we will get there in the second part of this tutorial). All the magic is in the <code>m-bind</code> function:</p>
<pre>
(defn m-bind [value function]
  (if (nil? value)
      nil
      (function value)))
</pre>
<p>If its input value is non-<code>nil</code>, it calls the supplied function, just as in the identity monad. Recall that this function represents the rest of the computation, i.e. all following steps. If the value is <code>nil</code>, then <code>m-bind</code> returns <code>nil</code> and the rest of the computation is never called. You can thus call <code>(f 1)</code>, yielding 2 as before, but also <code>(f nil)</code> yielding <code>nil</code>, without having to add <code>nil</code>-detecting code after every step of your computation, because <code>m-bind</code> does it behind the scenes.</p>
<p>In <a href="http://onclojure.com/2009/03/06/a-monad-tutorial-for-clojure-programmers-part-2/">part 2</a>, I will introduce some more monads, and look at some generic functions that can be used in any monad to aid in composing computations.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/onclojure.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/onclojure.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/onclojure.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/onclojure.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/onclojure.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/onclojure.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/onclojure.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/onclojure.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/onclojure.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/onclojure.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=onclojure.com&blog=5872423&post=14&subd=onclojure&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://onclojure.com/2009/03/05/a-monad-tutorial-for-clojure-programmers-part-1/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/305e4cf66ef1179f7e95981b1520ba1a?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">khinsen</media:title>
		</media:content>
	</item>
	</channel>
</rss>